1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef LOG_TAG
16 #define LOG_TAG "AudioProcessInServer"
17 #endif
18
19 #include "audio_process_in_server.h"
20 #include "policy_handler.h"
21
22 #include "securec.h"
23
24 #include "audio_errors.h"
25 #include "audio_capturer_log.h"
26 #include "audio_service.h"
27 #include "audio_schedule.h"
28 #include "audio_utils.h"
29 #include "media_monitor_manager.h"
30 #include "audio_dump_pcm.h"
31
32 namespace OHOS {
33 namespace AudioStandard {
34 namespace {
35 static constexpr int32_t VOLUME_SHIFT_NUMBER = 16; // 1 >> 16 = 65536, max volume
36 }
37
Create(const AudioProcessConfig & processConfig,ProcessReleaseCallback * releaseCallback)38 sptr<AudioProcessInServer> AudioProcessInServer::Create(const AudioProcessConfig &processConfig,
39 ProcessReleaseCallback *releaseCallback)
40 {
41 sptr<AudioProcessInServer> process = new(std::nothrow) AudioProcessInServer(processConfig, releaseCallback);
42
43 return process;
44 }
45
AudioProcessInServer(const AudioProcessConfig & processConfig,ProcessReleaseCallback * releaseCallback)46 AudioProcessInServer::AudioProcessInServer(const AudioProcessConfig &processConfig,
47 ProcessReleaseCallback *releaseCallback) : processConfig_(processConfig), releaseCallback_(releaseCallback)
48 {
49 if (processConfig.originalSessionId < MIN_SESSIONID || processConfig.originalSessionId > MAX_SESSIONID) {
50 sessionId_ = PolicyHandler::GetInstance().GenerateSessionId(processConfig_.appInfo.appUid);
51 } else {
52 sessionId_ = processConfig.originalSessionId;
53 }
54
55 const auto [samplingRate, encoding, format, channels, channelLayout] = processConfig.streamInfo;
56 // eg: 100005_dump_process_server_audio_48000_2_1.pcm
57 dumpFileName_ = std::to_string(sessionId_) + '_' + "_dump_process_server_audio_" +
58 std::to_string(samplingRate) + '_' + std::to_string(channels) + '_' + std::to_string(format) +
59 ".pcm";
60 DumpFileUtil::OpenDumpFile(DUMP_SERVER_PARA, dumpFileName_, &dumpFile_);
61 }
62
~AudioProcessInServer()63 AudioProcessInServer::~AudioProcessInServer()
64 {
65 AUDIO_INFO_LOG("~AudioProcessInServer()");
66 if (convertedBuffer_.buffer != nullptr) {
67 delete [] convertedBuffer_.buffer;
68 }
69 if (processConfig_.audioMode == AUDIO_MODE_RECORD && needCheckBackground_) {
70 uint32_t tokenId = processConfig_.appInfo.appTokenId;
71 PermissionUtil::NotifyStop(tokenId, sessionId_);
72 }
73 DumpFileUtil::CloseDumpFile(&dumpFile_);
74 }
75
GetSessionId(uint32_t & sessionId)76 int32_t AudioProcessInServer::GetSessionId(uint32_t &sessionId)
77 {
78 sessionId = sessionId_;
79 return SUCCESS;
80 }
81
SetNonInterruptMute(const bool muteFlag)82 void AudioProcessInServer::SetNonInterruptMute(const bool muteFlag)
83 {
84 muteFlag_ = muteFlag;
85 AUDIO_INFO_LOG("muteFlag_: %{public}d", muteFlag);
86 AudioService::GetInstance()->UpdateMuteControlSet(sessionId_, muteFlag);
87 }
88
GetMuteState()89 bool AudioProcessInServer::GetMuteState()
90 {
91 return muteFlag_ || silentModeAndMixWithOthers_;
92 }
93
GetSessionId()94 uint32_t AudioProcessInServer::GetSessionId()
95 {
96 return sessionId_;
97 }
98
GetStandbyStatus(bool & isStandby,int64_t & enterStandbyTime)99 int32_t AudioProcessInServer::GetStandbyStatus(bool &isStandby, int64_t &enterStandbyTime)
100 {
101 if (processBuffer_ == nullptr || processBuffer_->GetStreamStatus() == nullptr) {
102 AUDIO_ERR_LOG("GetStandbyStatus failed, buffer is nullptr.");
103 return ERR_OPERATION_FAILED;
104 }
105 isStandby = processBuffer_->GetStreamStatus()->load() == STREAM_STAND_BY;
106 if (isStandby) {
107 enterStandbyTime = enterStandbyTime_;
108 } else {
109 enterStandbyTime = 0;
110 }
111
112 return SUCCESS;
113 }
114
EnableStandby()115 void AudioProcessInServer::EnableStandby()
116 {
117 CHECK_AND_RETURN_LOG(processBuffer_ != nullptr && processBuffer_->GetStreamStatus() != nullptr, "failed: nullptr");
118 processBuffer_->GetStreamStatus()->store(StreamStatus::STREAM_STAND_BY);
119 enterStandbyTime_ = ClockTime::GetCurNano();
120 }
121
ResolveBuffer(std::shared_ptr<OHAudioBuffer> & buffer)122 int32_t AudioProcessInServer::ResolveBuffer(std::shared_ptr<OHAudioBuffer> &buffer)
123 {
124 AUDIO_INFO_LOG("ResolveBuffer start");
125 CHECK_AND_RETURN_RET_LOG(isBufferConfiged_, ERR_ILLEGAL_STATE,
126 "ResolveBuffer failed, buffer is not configed.");
127
128 if (processBuffer_ == nullptr) {
129 AUDIO_ERR_LOG("ResolveBuffer failed, buffer is nullptr.");
130 }
131 buffer = processBuffer_;
132 CHECK_AND_RETURN_RET_LOG(buffer != nullptr, ERR_ILLEGAL_STATE, "ResolveBuffer failed, processBuffer_ is null.");
133
134 return SUCCESS;
135 }
136
RequestHandleInfo(bool isAsync)137 int32_t AudioProcessInServer::RequestHandleInfo(bool isAsync)
138 {
139 CHECK_AND_RETURN_RET_LOG(isInited_, ERR_ILLEGAL_STATE, "not inited!");
140 CHECK_AND_RETURN_RET_LOG(processBuffer_ != nullptr, ERR_ILLEGAL_STATE, "buffer not inited!");
141
142 for (size_t i = 0; i < listenerList_.size(); i++) {
143 listenerList_[i]->OnUpdateHandleInfo(this);
144 }
145 return SUCCESS;
146 }
147
Start()148 int32_t AudioProcessInServer::Start()
149 {
150 CHECK_AND_RETURN_RET_LOG(isInited_, ERR_ILLEGAL_STATE, "not inited!");
151
152 std::lock_guard<std::mutex> lock(statusLock_);
153 CHECK_AND_RETURN_RET_LOG(streamStatus_->load() == STREAM_STARTING || streamStatus_->load() == STREAM_STAND_BY,
154 ERR_ILLEGAL_STATE, "Start failed, invalid status.");
155
156 if (processConfig_.audioMode != AUDIO_MODE_PLAYBACK && !needCheckBackground_ &&
157 PermissionUtil::NeedVerifyBackgroundCapture(processConfig_.callerUid, processConfig_.capturerInfo.sourceType)) {
158 AUDIO_INFO_LOG("set needCheckBackground_: true");
159 needCheckBackground_ = true;
160 }
161 if (processConfig_.audioMode != AUDIO_MODE_PLAYBACK && needCheckBackground_) {
162 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyBackgroundCapture(processConfig_.appInfo.appTokenId,
163 processConfig_.appInfo.appFullTokenId), ERR_OPERATION_FAILED, "VerifyBackgroundCapture failed!");
164 CHECK_AND_RETURN_RET_LOG(PermissionUtil::NotifyStart(processConfig_.appInfo.appTokenId, sessionId_),
165 ERR_PERMISSION_DENIED, "NotifyPrivacy failed!");
166 }
167
168 for (size_t i = 0; i < listenerList_.size(); i++) {
169 listenerList_[i]->OnStart(this);
170 }
171
172 if (streamStatus_->load() == STREAM_STAND_BY) {
173 AUDIO_INFO_LOG("Call start while in stand-by, session %{public}u", sessionId_);
174 WriterRenderStreamStandbySysEvent(sessionId_, 0);
175 streamStatus_->store(STREAM_STARTING);
176 enterStandbyTime_ = 0;
177 }
178 processBuffer_->SetLastWrittenTime(ClockTime::GetCurNano());
179
180 AUDIO_INFO_LOG("Start in server success!");
181 return SUCCESS;
182 }
183
Pause(bool isFlush)184 int32_t AudioProcessInServer::Pause(bool isFlush)
185 {
186 CHECK_AND_RETURN_RET_LOG(isInited_, ERR_ILLEGAL_STATE, "not inited!");
187
188 (void)isFlush;
189 std::lock_guard<std::mutex> lock(statusLock_);
190 CHECK_AND_RETURN_RET_LOG(streamStatus_->load() == STREAM_PAUSING,
191 ERR_ILLEGAL_STATE, "Pause failed, invalid status.");
192 if (processConfig_.audioMode != AUDIO_MODE_PLAYBACK && needCheckBackground_) {
193 uint32_t tokenId = processConfig_.appInfo.appTokenId;
194 PermissionUtil::NotifyStop(tokenId, sessionId_);
195 }
196 for (size_t i = 0; i < listenerList_.size(); i++) {
197 listenerList_[i]->OnPause(this);
198 }
199
200 AUDIO_PRERELEASE_LOGI("Pause in server success!");
201 return SUCCESS;
202 }
203
Resume()204 int32_t AudioProcessInServer::Resume()
205 {
206 CHECK_AND_RETURN_RET_LOG(isInited_, ERR_ILLEGAL_STATE, "not inited!");
207 std::lock_guard<std::mutex> lock(statusLock_);
208 CHECK_AND_RETURN_RET_LOG(streamStatus_->load() == STREAM_STARTING,
209 ERR_ILLEGAL_STATE, "Resume failed, invalid status.");
210 if (processConfig_.audioMode != AUDIO_MODE_PLAYBACK && !needCheckBackground_ &&
211 PermissionUtil::NeedVerifyBackgroundCapture(processConfig_.callerUid, processConfig_.capturerInfo.sourceType)) {
212 AUDIO_INFO_LOG("set needCheckBackground_: true");
213 needCheckBackground_ = true;
214 }
215 if (processConfig_.audioMode != AUDIO_MODE_PLAYBACK && needCheckBackground_) {
216 uint32_t tokenId = processConfig_.appInfo.appTokenId;
217 uint64_t fullTokenId = processConfig_.appInfo.appFullTokenId;
218 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyBackgroundCapture(tokenId, fullTokenId), ERR_OPERATION_FAILED,
219 "VerifyBackgroundCapture failed!");
220 CHECK_AND_RETURN_RET_LOG(PermissionUtil::NotifyStart(tokenId, sessionId_), ERR_PERMISSION_DENIED,
221 "NotifyPrivacy failed!");
222 }
223
224 for (size_t i = 0; i < listenerList_.size(); i++) {
225 listenerList_[i]->OnStart(this);
226 }
227
228 AUDIO_PRERELEASE_LOGI("Resume in server success!");
229 return SUCCESS;
230 }
231
Stop()232 int32_t AudioProcessInServer::Stop()
233 {
234 CHECK_AND_RETURN_RET_LOG(isInited_, ERR_ILLEGAL_STATE, "not inited!");
235
236 std::lock_guard<std::mutex> lock(statusLock_);
237 CHECK_AND_RETURN_RET_LOG(streamStatus_->load() == STREAM_STOPPING,
238 ERR_ILLEGAL_STATE, "Stop failed, invalid status.");
239 if (processConfig_.audioMode != AUDIO_MODE_PLAYBACK && needCheckBackground_) {
240 uint32_t tokenId = processConfig_.appInfo.appTokenId;
241 PermissionUtil::NotifyStop(tokenId, sessionId_);
242 }
243 for (size_t i = 0; i < listenerList_.size(); i++) {
244 listenerList_[i]->OnPause(this); // notify endpoint?
245 }
246
247 AUDIO_INFO_LOG("Stop in server success!");
248 return SUCCESS;
249 }
250
Release(bool isSwitchStream)251 int32_t AudioProcessInServer::Release(bool isSwitchStream)
252 {
253 CHECK_AND_RETURN_RET_LOG(isInited_, ERR_ILLEGAL_STATE, "not inited or already released");
254 UnscheduleReportData(processConfig_.appInfo.appPid, clientTid_, clientBundleName_.c_str());
255 clientThreadPriorityRequested_ = false;
256 isInited_ = false;
257 std::lock_guard<std::mutex> lock(statusLock_);
258 CHECK_AND_RETURN_RET_LOG(releaseCallback_ != nullptr, ERR_OPERATION_FAILED, "Failed: no service to notify.");
259
260 if (processConfig_.audioMode != AUDIO_MODE_PLAYBACK && needCheckBackground_) {
261 uint32_t tokenId = processConfig_.appInfo.appTokenId;
262 PermissionUtil::NotifyStop(tokenId, sessionId_);
263 }
264 int32_t ret = releaseCallback_->OnProcessRelease(this, isSwitchStream);
265 AUDIO_INFO_LOG("notify service release result: %{public}d", ret);
266 return SUCCESS;
267 }
268
ProcessDeathRecipient(AudioProcessInServer * processInServer,ProcessReleaseCallback * processHolder)269 ProcessDeathRecipient::ProcessDeathRecipient(AudioProcessInServer *processInServer,
270 ProcessReleaseCallback *processHolder)
271 {
272 processInServer_ = processInServer;
273 processHolder_ = processHolder;
274 }
275
OnRemoteDied(const wptr<IRemoteObject> & remote)276 void ProcessDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
277 {
278 CHECK_AND_RETURN_LOG(processHolder_ != nullptr, "processHolder_ is null.");
279 int32_t ret = processHolder_->OnProcessRelease(processInServer_);
280 AUDIO_INFO_LOG("OnRemoteDied, call release ret: %{public}d", ret);
281 }
282
RegisterProcessCb(sptr<IRemoteObject> object)283 int32_t AudioProcessInServer::RegisterProcessCb(sptr<IRemoteObject> object)
284 {
285 sptr<IProcessCb> processCb = iface_cast<IProcessCb>(object);
286 CHECK_AND_RETURN_RET_LOG(processCb != nullptr, ERR_INVALID_PARAM, "RegisterProcessCb obj cast failed");
287 bool result = object->AddDeathRecipient(new ProcessDeathRecipient(this, releaseCallback_));
288 CHECK_AND_RETURN_RET_LOG(result, ERR_OPERATION_FAILED, "AddDeathRecipient failed.");
289
290 return SUCCESS;
291 }
292
SetInnerCapState(bool isInnerCapped)293 void AudioProcessInServer::SetInnerCapState(bool isInnerCapped)
294 {
295 AUDIO_INFO_LOG("process[%{public}u] innercapped: %{public}s", sessionId_, isInnerCapped ? "true" : "false");
296 isInnerCapped_ = isInnerCapped;
297 }
298
GetInnerCapState()299 bool AudioProcessInServer::GetInnerCapState()
300 {
301 return isInnerCapped_;
302 }
303
GetAppInfo()304 AppInfo AudioProcessInServer::GetAppInfo()
305 {
306 return processConfig_.appInfo;
307 }
308
GetConvertedBuffer()309 BufferDesc &AudioProcessInServer::GetConvertedBuffer()
310 {
311 return convertedBuffer_;
312 }
313
Dump(int fd,const std::vector<std::u16string> & args)314 int AudioProcessInServer::Dump(int fd, const std::vector<std::u16string> &args)
315 {
316 return SUCCESS;
317 }
318
Dump(std::string & dumpString)319 void AudioProcessInServer::Dump(std::string &dumpString)
320 {
321 AppendFormat(dumpString, "\n - uid: %d\n", processConfig_.appInfo.appUid);
322 AppendFormat(dumpString, "- pid: %d\n", processConfig_.appInfo.appUid);
323 dumpString += "process info:\n";
324 dumpString += "stream info:\n";
325 AppendFormat(dumpString, " - samplingRate: %d\n", processConfig_.streamInfo.samplingRate);
326 AppendFormat(dumpString, " - channels: %d\n", processConfig_.streamInfo.channels);
327 AppendFormat(dumpString, " - format: %d\n", processConfig_.streamInfo.format);
328 AppendFormat(dumpString, " - encoding: %d\n", processConfig_.streamInfo.encoding);
329 if (streamStatus_ != nullptr) {
330 AppendFormat(dumpString, " - Status: %d\n", streamStatus_->load());
331 }
332 dumpString += "\n";
333 }
334
GetStreamBuffer()335 std::shared_ptr<OHAudioBuffer> AudioProcessInServer::GetStreamBuffer()
336 {
337 CHECK_AND_RETURN_RET_LOG(isBufferConfiged_ && processBuffer_ != nullptr,
338 nullptr, "GetStreamBuffer failed:process buffer not config.");
339 return processBuffer_;
340 }
341
GetStreamInfo()342 AudioStreamInfo AudioProcessInServer::GetStreamInfo()
343 {
344 return processConfig_.streamInfo;
345 }
346
GetAudioSessionId()347 uint32_t AudioProcessInServer::GetAudioSessionId()
348 {
349 return sessionId_;
350 }
351
GetAudioStreamType()352 AudioStreamType AudioProcessInServer::GetAudioStreamType()
353 {
354 return processConfig_.streamType;
355 }
356
GetAudioProcessConfig()357 AudioProcessConfig AudioProcessInServer::GetAudioProcessConfig()
358 {
359 return processConfig_;
360 }
361
PcmFormatToBits(AudioSampleFormat format)362 inline uint32_t PcmFormatToBits(AudioSampleFormat format)
363 {
364 switch (format) {
365 case SAMPLE_U8:
366 return 1; // 1 byte
367 case SAMPLE_S16LE:
368 return 2; // 2 byte
369 case SAMPLE_S24LE:
370 return 3; // 3 byte
371 case SAMPLE_S32LE:
372 return 4; // 4 byte
373 case SAMPLE_F32LE:
374 return 4; // 4 byte
375 default:
376 return 2; // 2 byte
377 }
378 }
379
InitBufferStatus()380 int32_t AudioProcessInServer::InitBufferStatus()
381 {
382 CHECK_AND_RETURN_RET_LOG(processBuffer_ != nullptr, ERR_ILLEGAL_STATE,
383 "InitBufferStatus failed, null buffer.");
384
385 uint32_t spanCount = processBuffer_->GetSpanCount();
386 for (uint32_t i = 0; i < spanCount; i++) {
387 SpanInfo *spanInfo = processBuffer_->GetSpanInfoByIndex(i);
388 CHECK_AND_RETURN_RET_LOG(spanInfo != nullptr, ERR_ILLEGAL_STATE,
389 "InitBufferStatus failed, null spaninfo");
390 spanInfo->spanStatus = SPAN_READ_DONE;
391 spanInfo->offsetInFrame = 0;
392
393 spanInfo->readStartTime = 0;
394 spanInfo->readDoneTime = 0;
395
396 spanInfo->writeStartTime = 0;
397 spanInfo->writeDoneTime = 0;
398
399 spanInfo->volumeStart = 1 << VOLUME_SHIFT_NUMBER; // 65536 for initialize
400 spanInfo->volumeEnd = 1 << VOLUME_SHIFT_NUMBER; // 65536 for initialize
401 spanInfo->isMute = false;
402 }
403 processBuffer_->SetLastWrittenTime(ClockTime::GetCurNano());
404 return SUCCESS;
405 }
406
ConfigProcessBuffer(uint32_t & totalSizeInframe,uint32_t & spanSizeInframe,DeviceStreamInfo & serverStreamInfo,const std::shared_ptr<OHAudioBuffer> & buffer)407 int32_t AudioProcessInServer::ConfigProcessBuffer(uint32_t &totalSizeInframe,
408 uint32_t &spanSizeInframe, DeviceStreamInfo &serverStreamInfo, const std::shared_ptr<OHAudioBuffer> &buffer)
409 {
410 if (processBuffer_ != nullptr) {
411 AUDIO_INFO_LOG("ConfigProcessBuffer: process buffer already configed!");
412 return SUCCESS;
413 }
414 // check
415 CHECK_AND_RETURN_RET_LOG(totalSizeInframe != 0 && spanSizeInframe != 0 && totalSizeInframe % spanSizeInframe == 0,
416 ERR_INVALID_PARAM, "ConfigProcessBuffer failed: ERR_INVALID_PARAM");
417 CHECK_AND_RETURN_RET_LOG(serverStreamInfo.samplingRate.size() > 0 && serverStreamInfo.channels.size() > 0,
418 ERR_INVALID_PARAM, "Invalid stream info in server");
419 uint32_t spanTime = spanSizeInframe * AUDIO_MS_PER_SECOND / *serverStreamInfo.samplingRate.rbegin();
420 spanSizeInframe_ = spanTime * processConfig_.streamInfo.samplingRate / AUDIO_MS_PER_SECOND;
421 totalSizeInframe_ = totalSizeInframe / spanSizeInframe * spanSizeInframe_;
422
423 uint32_t channel = processConfig_.streamInfo.channels;
424 uint32_t formatbyte = PcmFormatToBits(processConfig_.streamInfo.format);
425 byteSizePerFrame_ = channel * formatbyte;
426 if (*serverStreamInfo.channels.rbegin() != processConfig_.streamInfo.channels) {
427 size_t spanSizeInByte = 0;
428 if (processConfig_.audioMode == AUDIO_MODE_PLAYBACK) {
429 uint32_t serverByteSize = *serverStreamInfo.channels.rbegin() * PcmFormatToBits(serverStreamInfo.format);
430 spanSizeInByte = static_cast<size_t>(spanSizeInframe * serverByteSize);
431 } else {
432 spanSizeInByte = static_cast<size_t>(spanSizeInframe_ * byteSizePerFrame_);
433 }
434 convertedBuffer_.buffer = new uint8_t[spanSizeInByte];
435 convertedBuffer_.bufLength = spanSizeInByte;
436 convertedBuffer_.dataLength = spanSizeInByte;
437 }
438
439 if (buffer == nullptr) {
440 // create OHAudioBuffer in server.
441 processBuffer_ = OHAudioBuffer::CreateFromLocal(totalSizeInframe_, spanSizeInframe_, byteSizePerFrame_);
442 CHECK_AND_RETURN_RET_LOG(processBuffer_ != nullptr, ERR_OPERATION_FAILED, "Create process buffer failed.");
443
444 CHECK_AND_RETURN_RET_LOG(processBuffer_->GetBufferHolder() == AudioBufferHolder::AUDIO_SERVER_SHARED,
445 ERR_ILLEGAL_STATE, "CreateFormLocal in server failed.");
446 AUDIO_INFO_LOG("Config: totalSizeInframe:%{public}d spanSizeInframe:%{public}d byteSizePerFrame:%{public}d",
447 totalSizeInframe_, spanSizeInframe_, byteSizePerFrame_);
448
449 // we need to clear data buffer to avoid dirty data.
450 memset_s(processBuffer_->GetDataBase(), processBuffer_->GetDataSize(), 0, processBuffer_->GetDataSize());
451 int32_t ret = InitBufferStatus();
452 AUDIO_DEBUG_LOG("clear data buffer, ret:%{public}d", ret);
453 } else {
454 processBuffer_ = buffer;
455 AUDIO_INFO_LOG("ConfigBuffer in server separate, base: %{public}d", *processBuffer_->GetDataBase());
456 }
457
458 streamStatus_ = processBuffer_->GetStreamStatus();
459 CHECK_AND_RETURN_RET_LOG(streamStatus_ != nullptr, ERR_OPERATION_FAILED, "Create process buffer failed.");
460 isBufferConfiged_ = true;
461 isInited_ = true;
462 return SUCCESS;
463 }
464
AddProcessStatusListener(std::shared_ptr<IProcessStatusListener> listener)465 int32_t AudioProcessInServer::AddProcessStatusListener(std::shared_ptr<IProcessStatusListener> listener)
466 {
467 std::lock_guard<std::mutex> lock(listenerListLock_);
468 listenerList_.push_back(listener);
469 return SUCCESS;
470 }
471
RemoveProcessStatusListener(std::shared_ptr<IProcessStatusListener> listener)472 int32_t AudioProcessInServer::RemoveProcessStatusListener(std::shared_ptr<IProcessStatusListener> listener)
473 {
474 std::lock_guard<std::mutex> lock(listenerListLock_);
475 std::vector<std::shared_ptr<IProcessStatusListener>>::iterator it = listenerList_.begin();
476 bool isFind = false;
477 while (it != listenerList_.end()) {
478 if (*it == listener) {
479 listenerList_.erase(it);
480 isFind = true;
481 break;
482 } else {
483 it++;
484 }
485 }
486
487 AUDIO_INFO_LOG("%{public}s the endpoint.", (isFind ? "find and remove" : "not find"));
488 return SUCCESS;
489 }
490
RegisterThreadPriority(uint32_t tid,const std::string & bundleName)491 int32_t AudioProcessInServer::RegisterThreadPriority(uint32_t tid, const std::string &bundleName)
492 {
493 if (!clientThreadPriorityRequested_) {
494 clientTid_ = tid;
495 clientBundleName_ = bundleName;
496 ScheduleReportData(processConfig_.appInfo.appPid, tid, bundleName.c_str());
497 return SUCCESS;
498 } else {
499 AUDIO_ERR_LOG("client thread priority requested");
500 return ERR_OPERATION_FAILED;
501 }
502 }
503
WriterRenderStreamStandbySysEvent(uint32_t sessionId,int32_t standby)504 void AudioProcessInServer::WriterRenderStreamStandbySysEvent(uint32_t sessionId, int32_t standby)
505 {
506 std::shared_ptr<Media::MediaMonitor::EventBean> bean = std::make_shared<Media::MediaMonitor::EventBean>(
507 Media::MediaMonitor::AUDIO, Media::MediaMonitor::STREAM_STANDBY,
508 Media::MediaMonitor::BEHAVIOR_EVENT);
509 bean->Add("STREAMID", static_cast<int32_t>(sessionId));
510 bean->Add("STANDBY", standby);
511 Media::MediaMonitor::MediaMonitorManager::GetInstance().WriteLogMsg(bean);
512 }
513
WriteDumpFile(void * buffer,size_t bufferSize)514 void AudioProcessInServer::WriteDumpFile(void *buffer, size_t bufferSize)
515 {
516 if (AudioDump::GetInstance().GetVersionType() == BETA_VERSION) {
517 DumpFileUtil::WriteDumpFile(dumpFile_, buffer, bufferSize);
518 AudioCacheMgr::GetInstance().CacheData(dumpFileName_, buffer, bufferSize);
519 }
520 }
521
SetSilentModeAndMixWithOthers(bool on)522 int32_t AudioProcessInServer::SetSilentModeAndMixWithOthers(bool on)
523 {
524 silentModeAndMixWithOthers_ = on;
525 AUDIO_INFO_LOG("%{public}d", on);
526 return SUCCESS;
527 }
528
529 } // namespace AudioStandard
530 } // namespace OHOS
531