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 "AudioService"
17 #endif
18 
19 #include "audio_service.h"
20 
21 #include <thread>
22 
23 #include "ipc_skeleton.h"
24 #include "audio_errors.h"
25 #include "audio_common_log.h"
26 #include "audio_utils.h"
27 #include "policy_handler.h"
28 #include "ipc_stream_in_server.h"
29 #include "audio_capturer_source.h"
30 #include "audio_volume.h"
31 
32 namespace OHOS {
33 namespace AudioStandard {
34 
35 static uint64_t g_id = 1;
36 static const uint32_t NORMAL_ENDPOINT_RELEASE_DELAY_TIME = 10000; // 10ms
37 static const uint32_t A2DP_ENDPOINT_RELEASE_DELAY_TIME = 3000; // 3ms
38 static const uint32_t VOIP_ENDPOINT_RELEASE_DELAY_TIME = 200; // 200ms
39 static const uint32_t A2DP_ENDPOINT_RE_CREATE_RELEASE_DELAY_TIME = 200; // 200ms
40 static const int32_t MEDIA_SERVICE_UID = 1013;
41 
GetInstance()42 AudioService *AudioService::GetInstance()
43 {
44     static AudioService AudioService;
45 
46     return &AudioService;
47 }
48 
AudioService()49 AudioService::AudioService()
50 {
51     AUDIO_INFO_LOG("AudioService()");
52 }
53 
~AudioService()54 AudioService::~AudioService()
55 {
56     AUDIO_INFO_LOG("~AudioService()");
57 }
58 
OnProcessRelease(IAudioProcessStream * process,bool isSwitchStream)59 int32_t AudioService::OnProcessRelease(IAudioProcessStream *process, bool isSwitchStream)
60 {
61     std::lock_guard<std::mutex> processListLock(processListMutex_);
62     bool isFind = false;
63     int32_t ret = ERROR;
64     auto paired = linkedPairedList_.begin();
65     std::string endpointName;
66     bool needRelease = false;
67     int32_t delayTime = NORMAL_ENDPOINT_RELEASE_DELAY_TIME;
68     while (paired != linkedPairedList_.end()) {
69         if ((*paired).first == process) {
70             AUDIO_INFO_LOG("SessionId %{public}u", (*paired).first->GetSessionId());
71             auto processConfig = process->GetAudioProcessConfig();
72             if (processConfig.audioMode == AUDIO_MODE_PLAYBACK) {
73                 SetDecMaxRendererStreamCnt();
74                 CleanAppUseNumMap(processConfig.appInfo.appUid);
75             }
76             if (!isSwitchStream) {
77                 AUDIO_INFO_LOG("is not switch stream, remove from mutedSessions_");
78                 RemoveIdFromMuteControlSet((*paired).first->GetSessionId());
79             }
80             ret = UnlinkProcessToEndpoint((*paired).first, (*paired).second);
81             if ((*paired).second->GetStatus() == AudioEndpoint::EndpointStatus::UNLINKED) {
82                 needRelease = true;
83                 endpointName = (*paired).second->GetEndpointName();
84                 delayTime = GetReleaseDelayTime((*paired).second, isSwitchStream);
85             }
86             linkedPairedList_.erase(paired);
87             isFind = true;
88             break;
89         } else {
90             paired++;
91         }
92     }
93     if (isFind) {
94         AUDIO_INFO_LOG("find and release process result %{public}d", ret);
95     } else {
96         AUDIO_INFO_LOG("can not find target process, maybe already released.");
97     }
98 
99     if (needRelease) {
100         ReleaseProcess(endpointName, delayTime);
101     }
102 
103     return SUCCESS;
104 }
105 
ReleaseProcess(const std::string endpointName,const int32_t delayTime)106 void AudioService::ReleaseProcess(const std::string endpointName, const int32_t delayTime)
107 {
108     AUDIO_INFO_LOG("find endpoint unlink, call delay release.");
109     std::unique_lock<std::mutex> lock(releaseEndpointMutex_);
110     releasingEndpointSet_.insert(endpointName);
111     auto releaseMidpointThread = [this, endpointName, delayTime] () {
112         this->DelayCallReleaseEndpoint(endpointName, delayTime);
113     };
114     std::thread releaseEndpointThread(releaseMidpointThread);
115     releaseEndpointThread.detach();
116 }
117 
GetReleaseDelayTime(std::shared_ptr<AudioEndpoint> endpoint,bool isSwitchStream)118 int32_t AudioService::GetReleaseDelayTime(std::shared_ptr<AudioEndpoint> endpoint, bool isSwitchStream)
119 {
120     if (endpoint->GetEndpointType()  == AudioEndpoint::EndpointType::TYPE_VOIP_MMAP) {
121         return VOIP_ENDPOINT_RELEASE_DELAY_TIME;
122     }
123 
124     if (endpoint->GetDeviceInfo().deviceType != DEVICE_TYPE_BLUETOOTH_A2DP) {
125         return NORMAL_ENDPOINT_RELEASE_DELAY_TIME;
126     }
127     if (!isSwitchStream) {
128         return A2DP_ENDPOINT_RELEASE_DELAY_TIME;
129     }
130     // The delay for destruction and reconstruction cannot be set to 0, otherwise there may be a problem:
131     // An endpoint exists at check process, but it may be destroyed immediately - during the re-create process
132     return A2DP_ENDPOINT_RE_CREATE_RELEASE_DELAY_TIME;
133 }
134 
GetIpcStream(const AudioProcessConfig & config,int32_t & ret)135 sptr<IpcStreamInServer> AudioService::GetIpcStream(const AudioProcessConfig &config, int32_t &ret)
136 {
137     Trace trace("AudioService::GetIpcStream");
138     if (innerCapturerMgr_ == nullptr) {
139         innerCapturerMgr_ = PlaybackCapturerManager::GetInstance(); // As mgr is a singleton, lock is needless here.
140         innerCapturerMgr_->RegisterCapturerFilterListener(this);
141     }
142 
143     // in plan: GetDeviceInfoForProcess(config) and stream limit check
144     // in plan: call GetProcessDeviceInfo to load inner-cap-sink
145     sptr<IpcStreamInServer> ipcStreamInServer = IpcStreamInServer::Create(config, ret);
146 
147     // in plan: Put playback into list, check if EnableInnerCap is need.
148     if (ipcStreamInServer != nullptr && config.audioMode == AUDIO_MODE_PLAYBACK) {
149         uint32_t sessionId = 0;
150         std::shared_ptr<RendererInServer> renderer = ipcStreamInServer->GetRenderer();
151         if (renderer != nullptr && renderer->GetSessionId(sessionId) == SUCCESS) {
152             InsertRenderer(sessionId, renderer); // for all renderers
153             CheckInnerCapForRenderer(sessionId, renderer);
154             CheckRenderSessionMuteState(sessionId, renderer);
155         }
156     }
157     if (ipcStreamInServer != nullptr && config.audioMode == AUDIO_MODE_RECORD) {
158         uint32_t sessionId = 0;
159         std::shared_ptr<CapturerInServer> capturer = ipcStreamInServer->GetCapturer();
160         if (capturer != nullptr && capturer->GetSessionId(sessionId) == SUCCESS) {
161             InsertCapturer(sessionId, capturer); // for all renderers
162             CheckCaptureSessionMuteState(sessionId, capturer);
163         }
164     }
165 
166     return ipcStreamInServer;
167 }
168 
UpdateMuteControlSet(uint32_t sessionId,bool muteFlag)169 void AudioService::UpdateMuteControlSet(uint32_t sessionId, bool muteFlag)
170 {
171     if (sessionId < MIN_SESSIONID || sessionId > MAX_SESSIONID) {
172         AUDIO_WARNING_LOG("Invalid sessionid %{public}u", sessionId);
173         return;
174    }
175     std::lock_guard<std::mutex> lock(mutedSessionsMutex_);
176     if (muteFlag) {
177         mutedSessions_.insert(sessionId);
178         return;
179     }
180     if (mutedSessions_.find(sessionId) != mutedSessions_.end()) {
181         mutedSessions_.erase(sessionId);
182     } else {
183         AUDIO_WARNING_LOG("Session id %{public}u not in the set", sessionId);
184     }
185 }
186 
RemoveIdFromMuteControlSet(uint32_t sessionId)187 void AudioService::RemoveIdFromMuteControlSet(uint32_t sessionId)
188 {
189     std::lock_guard<std::mutex> mutedSessionsLock(mutedSessionsMutex_);
190     if (mutedSessions_.find(sessionId) != mutedSessions_.end()) {
191         mutedSessions_.erase(sessionId);
192     } else {
193         AUDIO_WARNING_LOG("Session id %{public}u not in the set", sessionId);
194     }
195 }
196 
CheckRenderSessionMuteState(uint32_t sessionId,std::shared_ptr<RendererInServer> renderer)197 void AudioService::CheckRenderSessionMuteState(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer)
198 {
199     std::unique_lock<std::mutex> mutedSessionsLock(mutedSessionsMutex_);
200     if (mutedSessions_.find(sessionId) != mutedSessions_.end()) {
201         mutedSessionsLock.unlock();
202         AUDIO_INFO_LOG("Session %{public}u is in control", sessionId);
203         renderer->SetNonInterruptMute(true);
204     }
205 }
206 
CheckCaptureSessionMuteState(uint32_t sessionId,std::shared_ptr<CapturerInServer> capturer)207 void AudioService::CheckCaptureSessionMuteState(uint32_t sessionId, std::shared_ptr<CapturerInServer> capturer)
208 {
209     std::unique_lock<std::mutex> mutedSessionsLock(mutedSessionsMutex_);
210     if (mutedSessions_.find(sessionId) != mutedSessions_.end()) {
211         mutedSessionsLock.unlock();
212         AUDIO_INFO_LOG("Session %{public}u is in control", sessionId);
213         capturer->SetNonInterruptMute(true);
214     }
215 }
CheckFastSessionMuteState(uint32_t sessionId,sptr<AudioProcessInServer> process)216 void AudioService::CheckFastSessionMuteState(uint32_t sessionId, sptr<AudioProcessInServer> process)
217 {
218     std::unique_lock<std::mutex> mutedSessionsLock(mutedSessionsMutex_);
219     if (mutedSessions_.find(sessionId) != mutedSessions_.end()) {
220         mutedSessionsLock.unlock();
221         AUDIO_INFO_LOG("Session %{public}u is in control", sessionId);
222         process->SetNonInterruptMute(true);
223     }
224 }
225 
InsertRenderer(uint32_t sessionId,std::shared_ptr<RendererInServer> renderer)226 void AudioService::InsertRenderer(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer)
227 {
228     std::unique_lock<std::mutex> lock(rendererMapMutex_);
229     AUDIO_INFO_LOG("Insert renderer:%{public}u into map", sessionId);
230     allRendererMap_[sessionId] = renderer;
231 }
232 
RemoveRenderer(uint32_t sessionId)233 void AudioService::RemoveRenderer(uint32_t sessionId)
234 {
235     std::unique_lock<std::mutex> lock(rendererMapMutex_);
236     AUDIO_INFO_LOG("Renderer:%{public}u will be removed.", sessionId);
237     if (!allRendererMap_.count(sessionId)) {
238         AUDIO_WARNING_LOG("Renderer in not in map!");
239         return;
240     }
241     allRendererMap_.erase(sessionId);
242     RemoveIdFromMuteControlSet(sessionId);
243 }
244 
InsertCapturer(uint32_t sessionId,std::shared_ptr<CapturerInServer> capturer)245 void AudioService::InsertCapturer(uint32_t sessionId, std::shared_ptr<CapturerInServer> capturer)
246 {
247     std::unique_lock<std::mutex> lock(capturerMapMutex_);
248     AUDIO_INFO_LOG("Insert capturer:%{public}u into map", sessionId);
249     allCapturerMap_[sessionId] = capturer;
250 }
251 
RemoveCapturer(uint32_t sessionId)252 void AudioService::RemoveCapturer(uint32_t sessionId)
253 {
254     std::unique_lock<std::mutex> lock(capturerMapMutex_);
255     AUDIO_INFO_LOG("Capturer: %{public}u will be removed.", sessionId);
256     if (!allCapturerMap_.count(sessionId)) {
257         AUDIO_WARNING_LOG("Capturer in not in map!");
258         return;
259     }
260     allCapturerMap_.erase(sessionId);
261     RemoveIdFromMuteControlSet(sessionId);
262 }
263 
CheckInnerCapForRenderer(uint32_t sessionId,std::shared_ptr<RendererInServer> renderer)264 void AudioService::CheckInnerCapForRenderer(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer)
265 {
266     CHECK_AND_RETURN_LOG(renderer != nullptr, "renderer is null.");
267 
268     std::unique_lock<std::mutex> lock(rendererMapMutex_);
269 
270     // inner-cap not working
271     if (workingInnerCapId_ == 0) {
272         return;
273     }
274     // in plan: check if meet with the workingConfig_
275     if (ShouldBeInnerCap(renderer->processConfig_)) {
276         filteredRendererMap_.push_back(renderer);
277         renderer->EnableInnerCap(); // for debug
278     }
279 }
280 
GetInnerCapFilterPolicy()281 InnerCapFilterPolicy AudioService::GetInnerCapFilterPolicy()
282 {
283     auto usagesSize = workingConfig_.filterOptions.usages.size();
284     auto pidsSize = workingConfig_.filterOptions.pids.size();
285     if (usagesSize == 0 && pidsSize == 0) {
286         AUDIO_ERR_LOG("error, invalid usages and pids");
287         return POLICY_INVALID;
288     }
289     if (usagesSize > 0 && pidsSize == 0) {
290         AUDIO_INFO_LOG("usages only");
291         return POLICY_USAGES_ONLY;
292     }
293     return POLICY_USAGES_AND_PIDS;
294 }
295 
296 template<typename T>
isFilterMatched(const std::vector<T> & params,T param,FilterMode mode)297 bool isFilterMatched(const std::vector<T> &params, T param, FilterMode mode)
298 {
299     bool isFound = std::count(params.begin(), params.end(), param) != 0;
300     return (mode == FilterMode::INCLUDE && isFound) || (mode == FilterMode::EXCLUDE && !isFound);
301 }
302 
ShouldBeInnerCap(const AudioProcessConfig & rendererConfig)303 bool AudioService::ShouldBeInnerCap(const AudioProcessConfig &rendererConfig)
304 {
305     bool canBeCaptured = rendererConfig.privacyType == AudioPrivacyType::PRIVACY_TYPE_PUBLIC;
306     if (!canBeCaptured) {
307         AUDIO_WARNING_LOG("%{public}d privacy is not public!", rendererConfig.appInfo.appPid);
308         return false;
309     }
310     InnerCapFilterPolicy filterPolicy = GetInnerCapFilterPolicy();
311     bool res = false;
312     switch (filterPolicy) {
313         case POLICY_INVALID:
314             return false;
315         case POLICY_USAGES_ONLY:
316             res = isFilterMatched(workingConfig_.filterOptions.usages,
317                 rendererConfig.rendererInfo.streamUsage, workingConfig_.filterOptions.usageFilterMode);
318             break;
319         case POLICY_USAGES_AND_PIDS:
320             res = isFilterMatched(workingConfig_.filterOptions.usages, rendererConfig.rendererInfo.streamUsage,
321                 workingConfig_.filterOptions.usageFilterMode) &&
322                 isFilterMatched(workingConfig_.filterOptions.pids, rendererConfig.appInfo.appPid,
323                 workingConfig_.filterOptions.pidFilterMode);
324             break;
325         default:
326             break;
327     }
328 
329     AUDIO_INFO_LOG("pid:%{public}d usage:%{public}d result:%{public}s", rendererConfig.appInfo.appPid,
330         rendererConfig.rendererInfo.streamUsage, res ? "true" : "false");
331     return res;
332 }
333 
ShouldBeDualTone(const AudioProcessConfig & config)334 bool AudioService::ShouldBeDualTone(const AudioProcessConfig &config)
335 {
336     CHECK_AND_RETURN_RET_LOG(Util::IsRingerOrAlarmerStreamUsage(config.rendererInfo.streamUsage), false,
337         "Wrong usage ,should not be dualtone");
338     DeviceInfo deviceInfo;
339     bool ret = PolicyHandler::GetInstance().GetProcessDeviceInfo(config, false, deviceInfo);
340     if (!ret) {
341         AUDIO_WARNING_LOG("GetProcessDeviceInfo from audio policy server failed!");
342         return false;
343     }
344     if (config.audioMode != AUDIO_MODE_PLAYBACK) {
345         AUDIO_WARNING_LOG("No playback mode!");
346         return false;
347     }
348     AUDIO_INFO_LOG("Get DeviceInfo from policy server success, deviceType: %{public}d, "
349         "supportLowLatency: %{public}d", deviceInfo.deviceType, deviceInfo.isLowLatencyDevice);
350     if (deviceInfo.deviceType == DEVICE_TYPE_WIRED_HEADSET || deviceInfo.deviceType == DEVICE_TYPE_WIRED_HEADPHONES ||
351         deviceInfo.deviceType == DEVICE_TYPE_BLUETOOTH_A2DP || deviceInfo.deviceType == DEVICE_TYPE_USB_HEADSET ||
352         deviceInfo.deviceType == DEVICE_TYPE_USB_ARM_HEADSET) {
353         switch (config.rendererInfo.streamUsage) {
354             case STREAM_USAGE_ALARM:
355             case STREAM_USAGE_VOICE_RINGTONE:
356             case STREAM_USAGE_RINGTONE:
357                 AUDIO_WARNING_LOG("Should DualTone.");
358                 return true;
359             default:
360                 return false;
361         }
362     }
363     return false;
364 }
365 
FilterAllFastProcess()366 void AudioService::FilterAllFastProcess()
367 {
368     std::unique_lock<std::mutex> lock(processListMutex_);
369     if (linkedPairedList_.size() == 0) {
370         return;
371     }
372     for (auto paired : linkedPairedList_) {
373         AudioProcessConfig temp = paired.first->processConfig_;
374         if (temp.audioMode == AUDIO_MODE_PLAYBACK && ShouldBeInnerCap(temp)) {
375             paired.first->SetInnerCapState(true);
376             paired.second->EnableFastInnerCap();
377         } else {
378             paired.first->SetInnerCapState(false);
379         }
380     }
381 
382     for (auto pair : endpointList_) {
383         if (pair.second->GetDeviceRole() == OUTPUT_DEVICE && !pair.second->ShouldInnerCap()) {
384             pair.second->DisableFastInnerCap();
385         }
386     }
387 }
388 
OnInitInnerCapList()389 int32_t AudioService::OnInitInnerCapList()
390 {
391     AUDIO_INFO_LOG("workingInnerCapId_ is %{public}d", workingInnerCapId_);
392     FilterAllFastProcess();
393 
394     // strong ref to prevent destruct before unlock
395     std::vector<std::shared_ptr<RendererInServer>> renderers;
396 
397     {
398         std::unique_lock<std::mutex> lock(rendererMapMutex_);
399         for (auto it = allRendererMap_.begin(); it != allRendererMap_.end(); it++) {
400             std::shared_ptr<RendererInServer> renderer = it->second.lock();
401             if (renderer == nullptr) {
402                 AUDIO_WARNING_LOG("Renderer is already released!");
403                 continue;
404             }
405             if (ShouldBeInnerCap(renderer->processConfig_)) {
406                 renderer->EnableInnerCap();
407                 filteredRendererMap_.push_back(renderer);
408             }
409             renderers.push_back(std::move(renderer));
410         }
411     }
412 
413     return SUCCESS;
414 }
415 
OnUpdateInnerCapList()416 int32_t AudioService::OnUpdateInnerCapList()
417 {
418     AUDIO_INFO_LOG("workingInnerCapId_ is %{public}d", workingInnerCapId_);
419 
420     std::unique_lock<std::mutex> lock(rendererMapMutex_);
421     for (size_t i = 0; i < filteredRendererMap_.size(); i++) {
422         std::shared_ptr<RendererInServer> renderer = filteredRendererMap_[i].lock();
423         if (renderer == nullptr) {
424             AUDIO_WARNING_LOG("Renderer is already released!");
425             continue;
426         }
427         if (!ShouldBeInnerCap(renderer->processConfig_)) {
428             renderer->DisableInnerCap();
429         }
430     }
431     filteredRendererMap_.clear();
432     lock.unlock();
433     // EnableInnerCap will be called twice as it's already in filteredRendererMap_.
434     return OnInitInnerCapList();
435 }
436 
EnableDualToneList(uint32_t sessionId)437 int32_t AudioService::EnableDualToneList(uint32_t sessionId)
438 {
439     workingDualToneId_ = sessionId;
440     AUDIO_INFO_LOG("EnableDualToneList sessionId is %{public}d", sessionId);
441     std::unique_lock<std::mutex> lock(rendererMapMutex_);
442     for (auto it = allRendererMap_.begin(); it != allRendererMap_.end(); it++) {
443         std::shared_ptr<RendererInServer> renderer = it->second.lock();
444         if (renderer == nullptr) {
445             AUDIO_WARNING_LOG("Renderer is already released!");
446             continue;
447         }
448         if (ShouldBeDualTone(renderer->processConfig_)) {
449             renderer->EnableDualTone();
450             filteredDualToneRendererMap_.push_back(renderer);
451         }
452     }
453     return SUCCESS;
454 }
455 
DisableDualToneList(uint32_t sessionId)456 int32_t AudioService::DisableDualToneList(uint32_t sessionId)
457 {
458     AUDIO_INFO_LOG("disable dual tone, sessionId is %{public}d", sessionId);
459     std::unique_lock<std::mutex> lock(rendererMapMutex_);
460     for (size_t i = 0; i < filteredDualToneRendererMap_.size(); i++) {
461         std::shared_ptr<RendererInServer> renderer = filteredDualToneRendererMap_[i].lock();
462         if (renderer == nullptr) {
463             AUDIO_WARNING_LOG("Renderer is already released!");
464             continue;
465         }
466         renderer->DisableDualTone();
467     }
468     filteredDualToneRendererMap_.clear();
469     return SUCCESS;
470 }
471 
472 // Only one session is working at the same time.
OnCapturerFilterChange(uint32_t sessionId,const AudioPlaybackCaptureConfig & newConfig)473 int32_t AudioService::OnCapturerFilterChange(uint32_t sessionId, const AudioPlaybackCaptureConfig &newConfig)
474 {
475     Trace trace("AudioService::OnCapturerFilterChange");
476     // in plan:
477     // step 1: if sessionId is not added before, add the sessionId and enbale the filter in allRendererMap_
478     // step 2: if sessionId is already in using, this means the config is changed. Check the filtered renderer before,
479     // call disable inner-cap for those not meet with the new config, than filter all allRendererMap_.
480     if (workingInnerCapId_ == 0) {
481         workingInnerCapId_ = sessionId;
482         workingConfig_ = newConfig;
483         return OnInitInnerCapList();
484     }
485 
486     if (workingInnerCapId_ == sessionId) {
487         workingConfig_ = newConfig;
488         return OnUpdateInnerCapList();
489     }
490 
491     AUDIO_WARNING_LOG("%{public}u is working, comming %{public}u will not work!", workingInnerCapId_, sessionId);
492     return ERR_OPERATION_FAILED;
493 }
494 
OnCapturerFilterRemove(uint32_t sessionId)495 int32_t AudioService::OnCapturerFilterRemove(uint32_t sessionId)
496 {
497     if (workingInnerCapId_ != sessionId) {
498         AUDIO_WARNING_LOG("%{public}u is working, remove %{public}u will not work!", workingInnerCapId_, sessionId);
499         return SUCCESS;
500     }
501     workingInnerCapId_ = 0;
502     workingConfig_ = {};
503 
504     std::unique_lock<std::mutex> lockEndpoint(processListMutex_);
505     for (auto pair : endpointList_) {
506         if (pair.second->GetDeviceRole() == OUTPUT_DEVICE) {
507             pair.second->DisableFastInnerCap();
508         }
509     }
510     lockEndpoint.unlock();
511 
512     // strong ref to prevent destruct before unlock
513     std::vector<std::shared_ptr<RendererInServer>> renderers;
514 
515     {
516         std::lock_guard<std::mutex> lock(rendererMapMutex_);
517         for (size_t i = 0; i < filteredRendererMap_.size(); i++) {
518             std::shared_ptr<RendererInServer> renderer = filteredRendererMap_[i].lock();
519             if (renderer == nullptr) {
520                 AUDIO_WARNING_LOG("Find renderer is already released!");
521                 continue;
522             }
523             renderer->DisableInnerCap();
524             renderers.push_back(std::move(renderer));
525         }
526         AUDIO_INFO_LOG("Filter removed, clear %{public}zu filtered renderer.", filteredRendererMap_.size());
527 
528         filteredRendererMap_.clear();
529     }
530 
531     return SUCCESS;
532 }
533 
IsEndpointTypeVoip(const AudioProcessConfig & config,DeviceInfo & deviceInfo)534 bool AudioService::IsEndpointTypeVoip(const AudioProcessConfig &config, DeviceInfo &deviceInfo)
535 {
536     if (config.rendererInfo.streamUsage == STREAM_USAGE_VOICE_COMMUNICATION ||
537         config.rendererInfo.streamUsage == STREAM_USAGE_VIDEO_COMMUNICATION) {
538         return config.rendererInfo.originalFlag == AUDIO_FLAG_VOIP_FAST || deviceInfo.networkId != LOCAL_NETWORK_ID;
539     }
540 
541     if (config.capturerInfo.sourceType == SOURCE_TYPE_VOICE_COMMUNICATION) {
542         return config.capturerInfo.originalFlag == AUDIO_FLAG_VOIP_FAST || deviceInfo.networkId != LOCAL_NETWORK_ID;
543     }
544     return false;
545 }
546 
GetAudioProcess(const AudioProcessConfig & config)547 sptr<AudioProcessInServer> AudioService::GetAudioProcess(const AudioProcessConfig &config)
548 {
549     int32_t ret =  SUCCESS;
550     if (config.streamType != STREAM_VOICE_CALL && config.streamType != STREAM_VOICE_COMMUNICATION) {
551         AudioPipeType incomingPipe = config.audioMode == AUDIO_MODE_PLAYBACK ?
552             PIPE_TYPE_LOWLATENCY_OUT : PIPE_TYPE_LOWLATENCY_IN;
553         ret = PolicyHandler::GetInstance().ActivateConcurrencyFromServer(incomingPipe);
554         CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, nullptr, "Concede incoming lowlatency stream from server");
555     }
556     Trace trace("AudioService::GetAudioProcess for " + std::to_string(config.appInfo.appPid));
557     AUDIO_INFO_LOG("GetAudioProcess dump %{public}s", ProcessConfig::DumpProcessConfig(config).c_str());
558     DeviceInfo deviceInfo = GetDeviceInfoForProcess(config);
559     std::lock_guard<std::mutex> lock(processListMutex_);
560     std::shared_ptr<AudioEndpoint> audioEndpoint = GetAudioEndpointForDevice(deviceInfo, config,
561         IsEndpointTypeVoip(config, deviceInfo));
562     CHECK_AND_RETURN_RET_LOG(audioEndpoint != nullptr, nullptr, "no endpoint found for the process");
563 
564     uint32_t totalSizeInframe = 0;
565     uint32_t spanSizeInframe = 0;
566     audioEndpoint->GetPreferBufferInfo(totalSizeInframe, spanSizeInframe);
567     CHECK_AND_RETURN_RET_LOG(*deviceInfo.audioStreamInfo.samplingRate.rbegin() > 0, nullptr,
568         "Sample rate in server is invalid.");
569 
570     sptr<AudioProcessInServer> process = AudioProcessInServer::Create(config, this);
571     CHECK_AND_RETURN_RET_LOG(process != nullptr, nullptr, "AudioProcessInServer create failed.");
572     CheckFastSessionMuteState(process->GetSessionId(), process);
573 
574     std::shared_ptr<OHAudioBuffer> buffer = audioEndpoint->GetEndpointType()
575          == AudioEndpoint::TYPE_INDEPENDENT ? audioEndpoint->GetBuffer() : nullptr;
576     ret = process->ConfigProcessBuffer(totalSizeInframe, spanSizeInframe, deviceInfo.audioStreamInfo, buffer);
577     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, nullptr, "ConfigProcessBuffer failed");
578 
579     ret = LinkProcessToEndpoint(process, audioEndpoint);
580     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, nullptr, "LinkProcessToEndpoint failed");
581 
582     linkedPairedList_.push_back(std::make_pair(process, audioEndpoint));
583     CheckInnerCapForProcess(process, audioEndpoint);
584     return process;
585 }
586 
ResetAudioEndpoint()587 void AudioService::ResetAudioEndpoint()
588 {
589     std::lock_guard<std::mutex> lock(processListMutex_);
590 
591     std::vector<std::string> audioEndpointNames;
592     for (auto paired = linkedPairedList_.begin(); paired != linkedPairedList_.end(); paired++) {
593         if (paired->second->GetEndpointType() == AudioEndpoint::TYPE_MMAP) {
594             // unlink old link
595             if (UnlinkProcessToEndpoint(paired->first, paired->second) != SUCCESS) {
596                 AUDIO_ERR_LOG("Unlink process to old endpoint failed");
597             }
598             audioEndpointNames.push_back(paired->second->GetEndpointName());
599         }
600     }
601 
602     // release old endpoint
603     for (auto &endpointName : audioEndpointNames) {
604         if (endpointList_.count(endpointName) > 0) {
605             endpointList_[endpointName]->Release();
606             AUDIO_INFO_LOG("Erase endpoint %{public}s from endpointList_", endpointName.c_str());
607             endpointList_.erase(endpointName);
608         }
609     }
610 
611     ReLinkProcessToEndpoint();
612 }
613 
ReLinkProcessToEndpoint()614 void AudioService::ReLinkProcessToEndpoint()
615 {
616     using LinkPair = std::pair<sptr<AudioProcessInServer>, std::shared_ptr<AudioEndpoint>>;
617     std::vector<std::vector<LinkPair>::iterator> errorLinkedPaireds;
618     for (auto paired = linkedPairedList_.begin(); paired != linkedPairedList_.end(); paired++) {
619         if (paired->second->GetEndpointType() == AudioEndpoint::TYPE_MMAP) {
620             AUDIO_INFO_LOG("Session id %{public}u", paired->first->GetSessionId());
621 
622             // get new endpoint
623             const AudioProcessConfig &config = paired->first->processConfig_;
624             DeviceInfo deviceInfo = GetDeviceInfoForProcess(config);
625             std::shared_ptr<AudioEndpoint> audioEndpoint = GetAudioEndpointForDevice(deviceInfo, config,
626                 IsEndpointTypeVoip(config, deviceInfo));
627             if (audioEndpoint == nullptr) {
628                 AUDIO_ERR_LOG("Get new endpoint failed");
629                 errorLinkedPaireds.push_back(paired);
630                 continue;
631             }
632             // link new endpoint
633             if (LinkProcessToEndpoint(paired->first, audioEndpoint) != SUCCESS) {
634                 AUDIO_ERR_LOG("LinkProcessToEndpoint failed");
635                 errorLinkedPaireds.push_back(paired);
636                 continue;
637             }
638             // reset shared_ptr before to new
639             paired->second.reset();
640             paired->second = audioEndpoint;
641             CheckInnerCapForProcess(paired->first, audioEndpoint);
642         }
643     }
644 
645     for (auto &paired : errorLinkedPaireds) {
646         linkedPairedList_.erase(paired);
647     }
648 }
649 
CheckInnerCapForProcess(sptr<AudioProcessInServer> process,std::shared_ptr<AudioEndpoint> endpoint)650 void AudioService::CheckInnerCapForProcess(sptr<AudioProcessInServer> process, std::shared_ptr<AudioEndpoint> endpoint)
651 {
652     Trace trace("AudioService::CheckInnerCapForProcess:" + std::to_string(process->processConfig_.appInfo.appPid));
653     // inner-cap not working
654     if (workingInnerCapId_ == 0) {
655         return;
656     }
657 
658     if (ShouldBeInnerCap(process->processConfig_)) {
659         process->SetInnerCapState(true);
660         endpoint->EnableFastInnerCap();
661     } else {
662         process->SetInnerCapState(false);
663     }
664 }
665 
NotifyStreamVolumeChanged(AudioStreamType streamType,float volume)666 int32_t AudioService::NotifyStreamVolumeChanged(AudioStreamType streamType, float volume)
667 {
668     int32_t ret = SUCCESS;
669     for (auto item : endpointList_) {
670         std::string endpointName = item.second->GetEndpointName();
671         if (endpointName == item.first) {
672             ret = ret != SUCCESS ? ret : item.second->SetVolume(streamType, volume);
673         }
674     }
675     return ret;
676 }
677 
LinkProcessToEndpoint(sptr<AudioProcessInServer> process,std::shared_ptr<AudioEndpoint> endpoint)678 int32_t AudioService::LinkProcessToEndpoint(sptr<AudioProcessInServer> process,
679     std::shared_ptr<AudioEndpoint> endpoint)
680 {
681     int32_t ret = endpoint->LinkProcessStream(process);
682     if (ret != SUCCESS && endpoint->GetLinkedProcessCount() == 0 &&
683         endpointList_.count(endpoint->GetEndpointName())) {
684         std::string endpointToErase = endpoint->GetEndpointName();
685         endpointList_.erase(endpoint->GetEndpointName());
686         AUDIO_ERR_LOG("LinkProcessStream failed, erase endpoint %{public}s", endpointToErase.c_str());
687         return ERR_OPERATION_FAILED;
688     }
689     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "LinkProcessStream to endpoint %{public}s failed",
690         endpoint->GetEndpointName().c_str());
691 
692     ret = process->AddProcessStatusListener(endpoint);
693     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "AddProcessStatusListener failed");
694 
695     std::unique_lock<std::mutex> lock(releaseEndpointMutex_);
696     if (releasingEndpointSet_.count(endpoint->GetEndpointName())) {
697         AUDIO_INFO_LOG("LinkProcessToEndpoint find endpoint is releasing, call break.");
698         releasingEndpointSet_.erase(endpoint->GetEndpointName());
699         releaseEndpointCV_.notify_all();
700     }
701     return SUCCESS;
702 }
703 
UnlinkProcessToEndpoint(sptr<AudioProcessInServer> process,std::shared_ptr<AudioEndpoint> endpoint)704 int32_t AudioService::UnlinkProcessToEndpoint(sptr<AudioProcessInServer> process,
705     std::shared_ptr<AudioEndpoint> endpoint)
706 {
707     int32_t ret = endpoint->UnlinkProcessStream(process);
708     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "UnlinkProcessStream failed");
709 
710     ret = process->RemoveProcessStatusListener(endpoint);
711     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "RemoveProcessStatusListener failed");
712 
713     return SUCCESS;
714 }
715 
DelayCallReleaseEndpoint(std::string endpointName,int32_t delayInMs)716 void AudioService::DelayCallReleaseEndpoint(std::string endpointName, int32_t delayInMs)
717 {
718     AUDIO_INFO_LOG("Delay release endpoint [%{public}s] start, delayInMs %{public}d.", endpointName.c_str(), delayInMs);
719     CHECK_AND_RETURN_LOG(endpointList_.count(endpointName),
720         "Find no such endpoint: %{public}s", endpointName.c_str());
721     std::unique_lock<std::mutex> lock(releaseEndpointMutex_);
722     if (delayInMs != 0) {
723         releaseEndpointCV_.wait_for(lock, std::chrono::milliseconds(delayInMs), [this, endpointName] {
724             if (releasingEndpointSet_.count(endpointName)) {
725                 AUDIO_DEBUG_LOG("Wake up but keep release endpoint %{public}s in delay", endpointName.c_str());
726                 return false;
727             }
728             AUDIO_DEBUG_LOG("Delay release endpoint break when reuse: %{public}s", endpointName.c_str());
729             return true;
730         });
731     }
732 
733     if (!releasingEndpointSet_.count(endpointName)) {
734         AUDIO_DEBUG_LOG("Timeout or not need to release: %{public}s", endpointName.c_str());
735         return;
736     }
737     releasingEndpointSet_.erase(endpointName);
738 
739     std::shared_ptr<AudioEndpoint> temp = nullptr;
740     CHECK_AND_RETURN_LOG(endpointList_.find(endpointName) != endpointList_.end() &&
741         endpointList_[endpointName] != nullptr, "Endpoint %{public}s not available, stop call release",
742         endpointName.c_str());
743     temp = endpointList_[endpointName];
744     if (temp->GetStatus() == AudioEndpoint::EndpointStatus::UNLINKED) {
745         AUDIO_INFO_LOG("%{public}s not in use anymore, call release!", endpointName.c_str());
746         temp->Release();
747         temp = nullptr;
748         endpointList_.erase(endpointName);
749         return;
750     }
751     AUDIO_WARNING_LOG("%{public}s is not unlinked, stop call release", endpointName.c_str());
752     return;
753 }
754 
GetDeviceInfoForProcess(const AudioProcessConfig & config)755 DeviceInfo AudioService::GetDeviceInfoForProcess(const AudioProcessConfig &config)
756 {
757     // send the config to AudioPolicyServera and get the device info.
758     DeviceInfo deviceInfo;
759     bool ret = PolicyHandler::GetInstance().GetProcessDeviceInfo(config, false, deviceInfo);
760     if (ret) {
761         AUDIO_INFO_LOG("Get DeviceInfo from policy server success, deviceType: %{public}d, "
762             "supportLowLatency: %{public}d", deviceInfo.deviceType, deviceInfo.isLowLatencyDevice);
763         return deviceInfo;
764     } else {
765         AUDIO_WARNING_LOG("GetProcessDeviceInfo from audio policy server failed!");
766     }
767 
768     if (config.audioMode == AUDIO_MODE_RECORD) {
769         deviceInfo.deviceId = 1;
770         deviceInfo.networkId = LOCAL_NETWORK_ID;
771         deviceInfo.deviceRole = INPUT_DEVICE;
772         deviceInfo.deviceType = DEVICE_TYPE_MIC;
773     } else {
774         deviceInfo.deviceId = 6; // 6 for test
775         deviceInfo.networkId = LOCAL_NETWORK_ID;
776         deviceInfo.deviceRole = OUTPUT_DEVICE;
777         deviceInfo.deviceType = DEVICE_TYPE_SPEAKER;
778     }
779     AudioStreamInfo targetStreamInfo = {SAMPLE_RATE_48000, ENCODING_PCM, SAMPLE_S16LE, STEREO}; // note: read from xml
780     deviceInfo.audioStreamInfo = targetStreamInfo;
781     deviceInfo.deviceName = "mmap_device";
782     return deviceInfo;
783 }
784 
GetAudioEndpointForDevice(DeviceInfo & deviceInfo,const AudioProcessConfig & clientConfig,bool isVoipStream)785 std::shared_ptr<AudioEndpoint> AudioService::GetAudioEndpointForDevice(DeviceInfo &deviceInfo,
786     const AudioProcessConfig &clientConfig, bool isVoipStream)
787 {
788     int32_t endpointSeparateFlag = -1;
789     GetSysPara("persist.multimedia.audioflag.fast.disableseparate", endpointSeparateFlag);
790     uint32_t uid = static_cast<uint32_t>(IPCSkeleton::GetCallingUid());
791     if (deviceInfo.deviceRole == INPUT_DEVICE || deviceInfo.networkId != LOCAL_NETWORK_ID || uid != 0 ||
792         endpointSeparateFlag == 1) {
793         // Create shared stream.
794         int32_t endpointFlag = AUDIO_FLAG_MMAP;
795         if (isVoipStream) {
796             endpointFlag = AUDIO_FLAG_VOIP_FAST;
797         }
798         std::string deviceKey = AudioEndpoint::GenerateEndpointKey(deviceInfo, endpointFlag);
799         if (endpointList_.find(deviceKey) != endpointList_.end()) {
800             AUDIO_INFO_LOG("AudioService find endpoint already exist for deviceKey:%{public}s", deviceKey.c_str());
801             return endpointList_[deviceKey];
802         } else {
803             std::shared_ptr<AudioEndpoint> endpoint = AudioEndpoint::CreateEndpoint(isVoipStream ?
804                 AudioEndpoint::TYPE_VOIP_MMAP : AudioEndpoint::TYPE_MMAP, endpointFlag, clientConfig, deviceInfo);
805             CHECK_AND_RETURN_RET_LOG(endpoint != nullptr, nullptr, "Create mmap AudioEndpoint failed.");
806             AUDIO_INFO_LOG("Add endpoint %{public}s to endpointList_", deviceKey.c_str());
807             endpointList_[deviceKey] = endpoint;
808             return endpoint;
809         }
810     } else {
811         // Create Independent stream.
812         std::string deviceKey = deviceInfo.networkId + std::to_string(deviceInfo.deviceId) + "_" + std::to_string(g_id);
813         std::shared_ptr<AudioEndpoint> endpoint = AudioEndpoint::CreateEndpoint(AudioEndpoint::TYPE_INDEPENDENT,
814             g_id, clientConfig, deviceInfo);
815         CHECK_AND_RETURN_RET_LOG(endpoint != nullptr, nullptr, "Create independent AudioEndpoint failed.");
816         g_id++;
817         AUDIO_INFO_LOG("Add endpointSeperate %{public}s to endpointList_", deviceKey.c_str());
818         endpointList_[deviceKey] = endpoint;
819         return endpoint;
820     }
821 }
822 
Dump(std::string & dumpString)823 void AudioService::Dump(std::string &dumpString)
824 {
825     AUDIO_INFO_LOG("AudioService dump begin");
826     if (workingInnerCapId_ != 0) {
827         AppendFormat(dumpString, "  - InnerCap filter: %s\n",
828             ProcessConfig::DumpInnerCapConfig(workingConfig_).c_str());
829     }
830     // dump process
831     for (auto paired : linkedPairedList_) {
832         paired.first->Dump(dumpString);
833     }
834     // dump endpoint
835     for (auto item : endpointList_) {
836         AppendFormat(dumpString, "  - Endpoint device id: %s\n", item.first.c_str());
837         item.second->Dump(dumpString);
838     }
839     // dump voip and direct
840     {
841         std::lock_guard<std::mutex> lock(rendererMapMutex_);
842         for (const auto &item : allRendererMap_) {
843             std::shared_ptr<RendererInServer> renderer = item.second.lock();
844             if (renderer) {
845                 renderer->Dump(dumpString);
846             }
847         }
848     }
849 
850     // dump appUseNumMap_ and currentRendererStreamCnt_
851     {
852         std::lock_guard<std::mutex> lock(streamLifeCycleMutex_);
853         AppendFormat(dumpString, " - currentRendererStreamCnt is %d\n", currentRendererStreamCnt_);
854         for (auto it : appUseNumMap_) {
855             AppendFormat(dumpString, "  - appUseNumMap_ appUid: %d\n", it.first);
856             AppendFormat(dumpString, "  - appUseNumMap_ appUid created stream: %d\n", it.second);
857         }
858     }
859     PolicyHandler::GetInstance().Dump(dumpString);
860     AudioVolume::GetInstance()->Dump(dumpString);
861 }
862 
GetMaxAmplitude(bool isOutputDevice)863 float AudioService::GetMaxAmplitude(bool isOutputDevice)
864 {
865     std::lock_guard<std::mutex> lock(processListMutex_);
866 
867     if (linkedPairedList_.size() == 0) {
868         return 0;
869     }
870 
871     float fastAudioMaxAmplitude = 0;
872     for (auto paired : linkedPairedList_) {
873         if (isOutputDevice && (paired.second->GetDeviceRole() == OUTPUT_DEVICE)) {
874             float curFastAudioMaxAmplitude = paired.second->GetMaxAmplitude();
875             if (curFastAudioMaxAmplitude > fastAudioMaxAmplitude) {
876                 fastAudioMaxAmplitude = curFastAudioMaxAmplitude;
877             }
878         }
879         if (!isOutputDevice && (paired.second->GetDeviceRole() == INPUT_DEVICE)) {
880             float curFastAudioMaxAmplitude = paired.second->GetMaxAmplitude();
881             if (curFastAudioMaxAmplitude > fastAudioMaxAmplitude) {
882                 fastAudioMaxAmplitude = curFastAudioMaxAmplitude;
883             }
884         }
885     }
886     return fastAudioMaxAmplitude;
887 }
888 
GetRendererBySessionID(const uint32_t & sessionID)889 std::shared_ptr<RendererInServer> AudioService::GetRendererBySessionID(const uint32_t &sessionID)
890 {
891     std::lock_guard<std::mutex> lock(rendererMapMutex_);
892     if (allRendererMap_.count(sessionID)) {
893         return allRendererMap_[sessionID].lock();
894     } else {
895         return nullptr;
896     }
897 }
898 
SetNonInterruptMute(const uint32_t sessionId,const bool muteFlag)899 void AudioService::SetNonInterruptMute(const uint32_t sessionId, const bool muteFlag)
900 {
901     AUDIO_INFO_LOG("SessionId: %{public}u, muteFlag: %{public}d", sessionId, muteFlag);
902     std::unique_lock<std::mutex> rendererLock(rendererMapMutex_);
903     if (allRendererMap_.count(sessionId)) {
904         std::shared_ptr<RendererInServer> renderer = allRendererMap_[sessionId].lock();
905         if (renderer == nullptr) {
906             AUDIO_ERR_LOG("rendererinserver is null");
907             rendererLock.unlock();
908             return;
909         }
910         renderer->SetNonInterruptMute(muteFlag);
911         AUDIO_INFO_LOG("allRendererMap_ has sessionId");
912         rendererLock.unlock();
913         return;
914     }
915     rendererLock.unlock();
916     std::unique_lock<std::mutex> capturerLock(capturerMapMutex_);
917     if (allCapturerMap_.count(sessionId)) {
918         std::shared_ptr<CapturerInServer> capturer = allCapturerMap_[sessionId].lock();
919         if (capturer == nullptr) {
920             AUDIO_ERR_LOG("capturerinserver is null");
921             capturerLock.unlock();
922             return;
923         }
924         capturer->SetNonInterruptMute(muteFlag);
925         AUDIO_INFO_LOG("allCapturerMap_ has sessionId");
926         capturerLock.unlock();
927         return;
928     }
929     capturerLock.unlock();
930     std::unique_lock<std::mutex> processListLock(processListMutex_);
931     for (auto paired : linkedPairedList_) {
932         if (paired.first == nullptr) {
933             AUDIO_ERR_LOG("processInServer is nullptr");
934             processListLock.unlock();
935             return;
936         }
937         if (paired.first->GetSessionId() == sessionId) {
938             AUDIO_INFO_LOG("linkedPairedList_ has sessionId");
939             paired.first->SetNonInterruptMute(muteFlag);
940             processListLock.unlock();
941             return;
942         }
943     }
944     processListLock.unlock();
945     AUDIO_INFO_LOG("Cannot find sessionId");
946 }
947 
UpdateSourceType(SourceType sourceType)948 int32_t AudioService::UpdateSourceType(SourceType sourceType)
949 {
950     // specialSourceType need not updateaudioroute
951     if (specialSourceTypeSet_.contains(sourceType)) {
952         return SUCCESS;
953     }
954 
955     AudioCapturerSource *audioCapturerSourceInstance = AudioCapturerSource::GetInstance("primary");
956     CHECK_AND_RETURN_RET_LOG(audioCapturerSourceInstance != nullptr, ERROR, "source is null");
957 
958     return audioCapturerSourceInstance->UpdateSourceType(sourceType);
959 }
960 
SetIncMaxRendererStreamCnt(AudioMode audioMode)961 void AudioService::SetIncMaxRendererStreamCnt(AudioMode audioMode)
962 {
963     if (audioMode == AUDIO_MODE_PLAYBACK) {
964         currentRendererStreamCnt_++;
965     }
966 }
967 
SetDecMaxRendererStreamCnt()968 void AudioService::SetDecMaxRendererStreamCnt()
969 {
970     std::lock_guard<std::mutex> lock(streamLifeCycleMutex_);
971     currentRendererStreamCnt_--;
972 }
973 
CleanAppUseNumMap(int32_t appUid)974 void AudioService::CleanAppUseNumMap(int32_t appUid)
975 {
976     std::lock_guard<std::mutex> lock(streamLifeCycleMutex_);
977     auto appUseNum = appUseNumMap_.find(appUid);
978     if (appUseNum != appUseNumMap_.end()) {
979         appUseNumMap_[appUid] = --appUseNum->second;
980     }
981 }
982 
GetCurrentRendererStreamCnt()983 int32_t AudioService::GetCurrentRendererStreamCnt()
984 {
985     return currentRendererStreamCnt_;
986 }
987 
988 // need call with streamLifeCycleMutex_ lock
IsExceedingMaxStreamCntPerUid(int32_t callingUid,int32_t appUid,int32_t maxStreamCntPerUid)989 bool AudioService::IsExceedingMaxStreamCntPerUid(int32_t callingUid, int32_t appUid,
990     int32_t maxStreamCntPerUid)
991 {
992     if (callingUid != MEDIA_SERVICE_UID) {
993         appUid = callingUid;
994     }
995 
996     auto appUseNum = appUseNumMap_.find(appUid);
997     if (appUseNum != appUseNumMap_.end()) {
998         ++appUseNum->second;
999     } else {
1000         int32_t initValue = 1;
1001         appUseNumMap_.emplace(appUid, initValue);
1002     }
1003 
1004     if (appUseNumMap_[appUid] > maxStreamCntPerUid) {
1005         --appUseNumMap_[appUid]; // actual created stream num is stream num decrease one
1006         return true;
1007     }
1008     return false;
1009 }
1010 
GetCreatedAudioStreamMostUid(int32_t & mostAppUid,int32_t & mostAppNum)1011 void AudioService::GetCreatedAudioStreamMostUid(int32_t &mostAppUid, int32_t &mostAppNum)
1012 {
1013     for (auto it = appUseNumMap_.begin(); it != appUseNumMap_.end(); it++) {
1014         if (it->second > mostAppNum) {
1015             mostAppNum = it->second;
1016             mostAppUid = it->first;
1017         }
1018     }
1019     return;
1020 }
1021 } // namespace AudioStandard
1022 } // namespace OHOS
1023