• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetStandbyStatus(uint32_t sessionId,bool & isStandby,int64_t & enterStandbyTime)233 int32_t AudioService::GetStandbyStatus(uint32_t sessionId, bool &isStandby, int64_t &enterStandbyTime)
234 {
235     // for normal renderer.
236     std::unique_lock<std::mutex> lockRender(rendererMapMutex_);
237     if (allRendererMap_.count(sessionId)) {
238         std::shared_ptr<RendererInServer> render = allRendererMap_[sessionId].lock();
239         if (render == nullptr) {
240             return ERR_INVALID_PARAM;
241         }
242         return render->GetStandbyStatus(isStandby, enterStandbyTime);
243     }
244     lockRender.unlock();
245 
246     // for fast process.
247     std::unique_lock<std::mutex> lockProcess(processListMutex_);
248     for (auto paired : linkedPairedList_) {
249         sptr<AudioProcessInServer> process = paired.first;
250         if (process->GetSessionId() == sessionId) {
251             return process->GetStandbyStatus(isStandby, enterStandbyTime);
252         }
253     }
254     // not found target sessionId
255     return ERR_INVALID_PARAM;
256 }
257 
RemoveRenderer(uint32_t sessionId)258 void AudioService::RemoveRenderer(uint32_t sessionId)
259 {
260     std::unique_lock<std::mutex> lock(rendererMapMutex_);
261     AUDIO_INFO_LOG("Renderer:%{public}u will be removed.", sessionId);
262     if (!allRendererMap_.count(sessionId)) {
263         AUDIO_WARNING_LOG("Renderer in not in map!");
264         return;
265     }
266     allRendererMap_.erase(sessionId);
267     RemoveIdFromMuteControlSet(sessionId);
268 }
269 
InsertCapturer(uint32_t sessionId,std::shared_ptr<CapturerInServer> capturer)270 void AudioService::InsertCapturer(uint32_t sessionId, std::shared_ptr<CapturerInServer> capturer)
271 {
272     std::unique_lock<std::mutex> lock(capturerMapMutex_);
273     AUDIO_INFO_LOG("Insert capturer:%{public}u into map", sessionId);
274     allCapturerMap_[sessionId] = capturer;
275 }
276 
RemoveCapturer(uint32_t sessionId)277 void AudioService::RemoveCapturer(uint32_t sessionId)
278 {
279     std::unique_lock<std::mutex> lock(capturerMapMutex_);
280     AUDIO_INFO_LOG("Capturer: %{public}u will be removed.", sessionId);
281     if (!allCapturerMap_.count(sessionId)) {
282         AUDIO_WARNING_LOG("Capturer in not in map!");
283         return;
284     }
285     allCapturerMap_.erase(sessionId);
286     RemoveIdFromMuteControlSet(sessionId);
287 }
288 
CheckInnerCapForRenderer(uint32_t sessionId,std::shared_ptr<RendererInServer> renderer)289 void AudioService::CheckInnerCapForRenderer(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer)
290 {
291     CHECK_AND_RETURN_LOG(renderer != nullptr, "renderer is null.");
292 
293     std::unique_lock<std::mutex> lock(rendererMapMutex_);
294 
295     // inner-cap not working
296     if (workingInnerCapId_ == 0) {
297         return;
298     }
299     // in plan: check if meet with the workingConfig_
300     if (ShouldBeInnerCap(renderer->processConfig_)) {
301         filteredRendererMap_.push_back(renderer);
302         renderer->EnableInnerCap(); // for debug
303     }
304 }
305 
GetInnerCapFilterPolicy()306 InnerCapFilterPolicy AudioService::GetInnerCapFilterPolicy()
307 {
308     auto usagesSize = workingConfig_.filterOptions.usages.size();
309     auto pidsSize = workingConfig_.filterOptions.pids.size();
310     if (usagesSize == 0 && pidsSize == 0) {
311         AUDIO_ERR_LOG("error, invalid usages and pids");
312         return POLICY_INVALID;
313     }
314     if (usagesSize > 0 && pidsSize == 0) {
315         AUDIO_INFO_LOG("usages only");
316         return POLICY_USAGES_ONLY;
317     }
318     return POLICY_USAGES_AND_PIDS;
319 }
320 
321 template<typename T>
isFilterMatched(const std::vector<T> & params,T param,FilterMode mode)322 bool isFilterMatched(const std::vector<T> &params, T param, FilterMode mode)
323 {
324     bool isFound = std::count(params.begin(), params.end(), param) != 0;
325     return (mode == FilterMode::INCLUDE && isFound) || (mode == FilterMode::EXCLUDE && !isFound);
326 }
327 
ShouldBeInnerCap(const AudioProcessConfig & rendererConfig)328 bool AudioService::ShouldBeInnerCap(const AudioProcessConfig &rendererConfig)
329 {
330     bool canBeCaptured = rendererConfig.privacyType == AudioPrivacyType::PRIVACY_TYPE_PUBLIC;
331     if (!canBeCaptured) {
332         AUDIO_WARNING_LOG("%{public}d privacy is not public!", rendererConfig.appInfo.appPid);
333         return false;
334     }
335     InnerCapFilterPolicy filterPolicy = GetInnerCapFilterPolicy();
336     bool res = false;
337     switch (filterPolicy) {
338         case POLICY_INVALID:
339             return false;
340         case POLICY_USAGES_ONLY:
341             res = isFilterMatched(workingConfig_.filterOptions.usages,
342                 rendererConfig.rendererInfo.streamUsage, workingConfig_.filterOptions.usageFilterMode);
343             break;
344         case POLICY_USAGES_AND_PIDS:
345             res = isFilterMatched(workingConfig_.filterOptions.usages, rendererConfig.rendererInfo.streamUsage,
346                 workingConfig_.filterOptions.usageFilterMode) &&
347                 isFilterMatched(workingConfig_.filterOptions.pids, rendererConfig.appInfo.appPid,
348                 workingConfig_.filterOptions.pidFilterMode);
349             break;
350         default:
351             break;
352     }
353 
354     AUDIO_INFO_LOG("pid:%{public}d usage:%{public}d result:%{public}s", rendererConfig.appInfo.appPid,
355         rendererConfig.rendererInfo.streamUsage, res ? "true" : "false");
356     return res;
357 }
358 
ShouldBeDualTone(const AudioProcessConfig & config)359 bool AudioService::ShouldBeDualTone(const AudioProcessConfig &config)
360 {
361     CHECK_AND_RETURN_RET_LOG(Util::IsRingerOrAlarmerStreamUsage(config.rendererInfo.streamUsage), false,
362         "Wrong usage ,should not be dualtone");
363     AudioDeviceDescriptor deviceInfo(AudioDeviceDescriptor::DEVICE_INFO);
364     bool ret = PolicyHandler::GetInstance().GetProcessDeviceInfo(config, false, deviceInfo);
365     if (!ret) {
366         AUDIO_WARNING_LOG("GetProcessDeviceInfo from audio policy server failed!");
367         return false;
368     }
369     if (config.audioMode != AUDIO_MODE_PLAYBACK) {
370         AUDIO_WARNING_LOG("No playback mode!");
371         return false;
372     }
373     AUDIO_INFO_LOG("Get DeviceInfo from policy server success, deviceType: %{public}d, "
374         "supportLowLatency: %{public}d", deviceInfo.deviceType_, deviceInfo.isLowLatencyDevice_);
375     if (deviceInfo.deviceType_ == DEVICE_TYPE_WIRED_HEADSET || deviceInfo.deviceType_ == DEVICE_TYPE_WIRED_HEADPHONES ||
376         deviceInfo.deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP || deviceInfo.deviceType_ == DEVICE_TYPE_USB_HEADSET ||
377         deviceInfo.deviceType_ == DEVICE_TYPE_USB_ARM_HEADSET) {
378         switch (config.rendererInfo.streamUsage) {
379             case STREAM_USAGE_ALARM:
380             case STREAM_USAGE_VOICE_RINGTONE:
381             case STREAM_USAGE_RINGTONE:
382                 AUDIO_WARNING_LOG("Should DualTone.");
383                 return true;
384             default:
385                 return false;
386         }
387     }
388     return false;
389 }
390 
FilterAllFastProcess()391 void AudioService::FilterAllFastProcess()
392 {
393     std::unique_lock<std::mutex> lock(processListMutex_);
394     if (linkedPairedList_.size() == 0) {
395         return;
396     }
397     for (auto paired : linkedPairedList_) {
398         AudioProcessConfig temp = paired.first->processConfig_;
399         if (temp.audioMode == AUDIO_MODE_PLAYBACK && ShouldBeInnerCap(temp)) {
400             paired.first->SetInnerCapState(true);
401             paired.second->EnableFastInnerCap();
402         } else {
403             paired.first->SetInnerCapState(false);
404         }
405     }
406 
407     for (auto pair : endpointList_) {
408         if (pair.second->GetDeviceRole() == OUTPUT_DEVICE && !pair.second->ShouldInnerCap()) {
409             pair.second->DisableFastInnerCap();
410         }
411     }
412 }
413 
OnInitInnerCapList()414 int32_t AudioService::OnInitInnerCapList()
415 {
416     AUDIO_INFO_LOG("workingInnerCapId_ is %{public}d", workingInnerCapId_);
417     FilterAllFastProcess();
418 
419     // strong ref to prevent destruct before unlock
420     std::vector<std::shared_ptr<RendererInServer>> renderers;
421 
422     {
423         std::unique_lock<std::mutex> lock(rendererMapMutex_);
424         for (auto it = allRendererMap_.begin(); it != allRendererMap_.end(); it++) {
425             std::shared_ptr<RendererInServer> renderer = it->second.lock();
426             if (renderer == nullptr) {
427                 AUDIO_WARNING_LOG("Renderer is already released!");
428                 continue;
429             }
430             if (ShouldBeInnerCap(renderer->processConfig_)) {
431                 renderer->EnableInnerCap();
432                 filteredRendererMap_.push_back(renderer);
433             }
434             renderers.push_back(std::move(renderer));
435         }
436     }
437 
438     return SUCCESS;
439 }
440 
OnUpdateInnerCapList()441 int32_t AudioService::OnUpdateInnerCapList()
442 {
443     AUDIO_INFO_LOG("workingInnerCapId_ is %{public}d", workingInnerCapId_);
444 
445     std::unique_lock<std::mutex> lock(rendererMapMutex_);
446     for (size_t i = 0; i < filteredRendererMap_.size(); i++) {
447         std::shared_ptr<RendererInServer> renderer = filteredRendererMap_[i].lock();
448         if (renderer == nullptr) {
449             AUDIO_WARNING_LOG("Renderer is already released!");
450             continue;
451         }
452         if (!ShouldBeInnerCap(renderer->processConfig_)) {
453             renderer->DisableInnerCap();
454         }
455     }
456     filteredRendererMap_.clear();
457     lock.unlock();
458     // EnableInnerCap will be called twice as it's already in filteredRendererMap_.
459     return OnInitInnerCapList();
460 }
461 
EnableDualToneList(uint32_t sessionId)462 int32_t AudioService::EnableDualToneList(uint32_t sessionId)
463 {
464     workingDualToneId_ = sessionId;
465     AUDIO_INFO_LOG("EnableDualToneList sessionId is %{public}d", sessionId);
466     std::unique_lock<std::mutex> lock(rendererMapMutex_);
467     for (auto it = allRendererMap_.begin(); it != allRendererMap_.end(); it++) {
468         std::shared_ptr<RendererInServer> renderer = it->second.lock();
469         if (renderer == nullptr) {
470             AUDIO_WARNING_LOG("Renderer is already released!");
471             continue;
472         }
473         if (ShouldBeDualTone(renderer->processConfig_)) {
474             renderer->EnableDualTone();
475             filteredDualToneRendererMap_.push_back(renderer);
476         }
477     }
478     return SUCCESS;
479 }
480 
DisableDualToneList(uint32_t sessionId)481 int32_t AudioService::DisableDualToneList(uint32_t sessionId)
482 {
483     AUDIO_INFO_LOG("disable dual tone, sessionId is %{public}d", sessionId);
484     std::unique_lock<std::mutex> lock(rendererMapMutex_);
485     for (size_t i = 0; i < filteredDualToneRendererMap_.size(); i++) {
486         std::shared_ptr<RendererInServer> renderer = filteredDualToneRendererMap_[i].lock();
487         if (renderer == nullptr) {
488             AUDIO_WARNING_LOG("Renderer is already released!");
489             continue;
490         }
491         renderer->DisableDualTone();
492     }
493     filteredDualToneRendererMap_.clear();
494     return SUCCESS;
495 }
496 
497 // Only one session is working at the same time.
OnCapturerFilterChange(uint32_t sessionId,const AudioPlaybackCaptureConfig & newConfig)498 int32_t AudioService::OnCapturerFilterChange(uint32_t sessionId, const AudioPlaybackCaptureConfig &newConfig)
499 {
500     Trace trace("AudioService::OnCapturerFilterChange");
501     // in plan:
502     // step 1: if sessionId is not added before, add the sessionId and enbale the filter in allRendererMap_
503     // step 2: if sessionId is already in using, this means the config is changed. Check the filtered renderer before,
504     // call disable inner-cap for those not meet with the new config, than filter all allRendererMap_.
505     if (workingInnerCapId_ == 0) {
506         workingInnerCapId_ = sessionId;
507         workingConfig_ = newConfig;
508         return OnInitInnerCapList();
509     }
510 
511     if (workingInnerCapId_ == sessionId) {
512         workingConfig_ = newConfig;
513         return OnUpdateInnerCapList();
514     }
515 
516     AUDIO_WARNING_LOG("%{public}u is working, comming %{public}u will not work!", workingInnerCapId_, sessionId);
517     return ERR_OPERATION_FAILED;
518 }
519 
OnCapturerFilterRemove(uint32_t sessionId)520 int32_t AudioService::OnCapturerFilterRemove(uint32_t sessionId)
521 {
522     if (workingInnerCapId_ != sessionId) {
523         AUDIO_WARNING_LOG("%{public}u is working, remove %{public}u will not work!", workingInnerCapId_, sessionId);
524         return SUCCESS;
525     }
526     workingInnerCapId_ = 0;
527     workingConfig_ = {};
528 
529     std::unique_lock<std::mutex> lockEndpoint(processListMutex_);
530     for (auto pair : endpointList_) {
531         if (pair.second->GetDeviceRole() == OUTPUT_DEVICE) {
532             pair.second->DisableFastInnerCap();
533         }
534     }
535     lockEndpoint.unlock();
536 
537     // strong ref to prevent destruct before unlock
538     std::vector<std::shared_ptr<RendererInServer>> renderers;
539 
540     {
541         std::lock_guard<std::mutex> lock(rendererMapMutex_);
542         for (size_t i = 0; i < filteredRendererMap_.size(); i++) {
543             std::shared_ptr<RendererInServer> renderer = filteredRendererMap_[i].lock();
544             if (renderer == nullptr) {
545                 AUDIO_WARNING_LOG("Find renderer is already released!");
546                 continue;
547             }
548             renderer->DisableInnerCap();
549             renderers.push_back(std::move(renderer));
550         }
551         AUDIO_INFO_LOG("Filter removed, clear %{public}zu filtered renderer.", filteredRendererMap_.size());
552 
553         filteredRendererMap_.clear();
554     }
555 
556     return SUCCESS;
557 }
558 
IsEndpointTypeVoip(const AudioProcessConfig & config,AudioDeviceDescriptor & deviceInfo)559 bool AudioService::IsEndpointTypeVoip(const AudioProcessConfig &config, AudioDeviceDescriptor &deviceInfo)
560 {
561     if (config.rendererInfo.streamUsage == STREAM_USAGE_VOICE_COMMUNICATION ||
562         config.rendererInfo.streamUsage == STREAM_USAGE_VIDEO_COMMUNICATION) {
563         return config.rendererInfo.originalFlag == AUDIO_FLAG_VOIP_FAST || deviceInfo.networkId_ != LOCAL_NETWORK_ID;
564     }
565 
566     if (config.capturerInfo.sourceType == SOURCE_TYPE_VOICE_COMMUNICATION) {
567         return config.capturerInfo.originalFlag == AUDIO_FLAG_VOIP_FAST || deviceInfo.networkId_ != LOCAL_NETWORK_ID;
568     }
569     return false;
570 }
571 
GetAudioProcess(const AudioProcessConfig & config)572 sptr<AudioProcessInServer> AudioService::GetAudioProcess(const AudioProcessConfig &config)
573 {
574     int32_t ret =  SUCCESS;
575     if (config.streamType != STREAM_VOICE_CALL && config.streamType != STREAM_VOICE_COMMUNICATION) {
576         AudioPipeType incomingPipe = config.audioMode == AUDIO_MODE_PLAYBACK ?
577             PIPE_TYPE_LOWLATENCY_OUT : PIPE_TYPE_LOWLATENCY_IN;
578         ret = PolicyHandler::GetInstance().ActivateConcurrencyFromServer(incomingPipe);
579         CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, nullptr, "Concede incoming lowlatency stream from server");
580     }
581     Trace trace("AudioService::GetAudioProcess for " + std::to_string(config.appInfo.appPid));
582     AUDIO_INFO_LOG("GetAudioProcess dump %{public}s", ProcessConfig::DumpProcessConfig(config).c_str());
583     AudioDeviceDescriptor deviceInfo = GetDeviceInfoForProcess(config);
584     std::lock_guard<std::mutex> lock(processListMutex_);
585     std::shared_ptr<AudioEndpoint> audioEndpoint = GetAudioEndpointForDevice(deviceInfo, config,
586         IsEndpointTypeVoip(config, deviceInfo));
587     CHECK_AND_RETURN_RET_LOG(audioEndpoint != nullptr, nullptr, "no endpoint found for the process");
588 
589     uint32_t totalSizeInframe = 0;
590     uint32_t spanSizeInframe = 0;
591     audioEndpoint->GetPreferBufferInfo(totalSizeInframe, spanSizeInframe);
592     CHECK_AND_RETURN_RET_LOG(*deviceInfo.audioStreamInfo_.samplingRate.rbegin() > 0, nullptr,
593         "Sample rate in server is invalid.");
594 
595     sptr<AudioProcessInServer> process = AudioProcessInServer::Create(config, this);
596     CHECK_AND_RETURN_RET_LOG(process != nullptr, nullptr, "AudioProcessInServer create failed.");
597     CheckFastSessionMuteState(process->GetSessionId(), process);
598 
599     std::shared_ptr<OHAudioBuffer> buffer = audioEndpoint->GetEndpointType()
600          == AudioEndpoint::TYPE_INDEPENDENT ? audioEndpoint->GetBuffer() : nullptr;
601     ret = process->ConfigProcessBuffer(totalSizeInframe, spanSizeInframe, deviceInfo.audioStreamInfo_, buffer);
602     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, nullptr, "ConfigProcessBuffer failed");
603 
604     ret = LinkProcessToEndpoint(process, audioEndpoint);
605     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, nullptr, "LinkProcessToEndpoint failed");
606 
607     linkedPairedList_.push_back(std::make_pair(process, audioEndpoint));
608     CheckInnerCapForProcess(process, audioEndpoint);
609     return process;
610 }
611 
ResetAudioEndpoint()612 void AudioService::ResetAudioEndpoint()
613 {
614     std::lock_guard<std::mutex> lock(processListMutex_);
615 
616     std::vector<std::string> audioEndpointNames;
617     for (auto paired = linkedPairedList_.begin(); paired != linkedPairedList_.end(); paired++) {
618         if (paired->second->GetEndpointType() == AudioEndpoint::TYPE_MMAP) {
619             // unlink old link
620             if (UnlinkProcessToEndpoint(paired->first, paired->second) != SUCCESS) {
621                 AUDIO_ERR_LOG("Unlink process to old endpoint failed");
622             }
623             audioEndpointNames.push_back(paired->second->GetEndpointName());
624         }
625     }
626 
627     // release old endpoint
628     for (auto &endpointName : audioEndpointNames) {
629         if (endpointList_.count(endpointName) > 0) {
630             endpointList_[endpointName]->Release();
631             AUDIO_INFO_LOG("Erase endpoint %{public}s from endpointList_", endpointName.c_str());
632             endpointList_.erase(endpointName);
633         }
634     }
635 
636     ReLinkProcessToEndpoint();
637 }
638 
ReLinkProcessToEndpoint()639 void AudioService::ReLinkProcessToEndpoint()
640 {
641     using LinkPair = std::pair<sptr<AudioProcessInServer>, std::shared_ptr<AudioEndpoint>>;
642     std::vector<std::vector<LinkPair>::iterator> errorLinkedPaireds;
643     for (auto paired = linkedPairedList_.begin(); paired != linkedPairedList_.end(); paired++) {
644         if (paired->second->GetEndpointType() == AudioEndpoint::TYPE_MMAP) {
645             AUDIO_INFO_LOG("Session id %{public}u", paired->first->GetSessionId());
646 
647             // get new endpoint
648             const AudioProcessConfig &config = paired->first->processConfig_;
649             AudioDeviceDescriptor deviceInfo = GetDeviceInfoForProcess(config);
650             std::shared_ptr<AudioEndpoint> audioEndpoint = GetAudioEndpointForDevice(deviceInfo, config,
651                 IsEndpointTypeVoip(config, deviceInfo));
652             if (audioEndpoint == nullptr) {
653                 AUDIO_ERR_LOG("Get new endpoint failed");
654                 errorLinkedPaireds.push_back(paired);
655                 continue;
656             }
657             // link new endpoint
658             if (LinkProcessToEndpoint(paired->first, audioEndpoint) != SUCCESS) {
659                 AUDIO_ERR_LOG("LinkProcessToEndpoint failed");
660                 errorLinkedPaireds.push_back(paired);
661                 continue;
662             }
663             // reset shared_ptr before to new
664             paired->second.reset();
665             paired->second = audioEndpoint;
666             CheckInnerCapForProcess(paired->first, audioEndpoint);
667         }
668     }
669 
670     for (auto &paired : errorLinkedPaireds) {
671         linkedPairedList_.erase(paired);
672     }
673 }
674 
CheckInnerCapForProcess(sptr<AudioProcessInServer> process,std::shared_ptr<AudioEndpoint> endpoint)675 void AudioService::CheckInnerCapForProcess(sptr<AudioProcessInServer> process, std::shared_ptr<AudioEndpoint> endpoint)
676 {
677     Trace trace("AudioService::CheckInnerCapForProcess:" + std::to_string(process->processConfig_.appInfo.appPid));
678     // inner-cap not working
679     if (workingInnerCapId_ == 0) {
680         return;
681     }
682 
683     if (ShouldBeInnerCap(process->processConfig_)) {
684         process->SetInnerCapState(true);
685         endpoint->EnableFastInnerCap();
686     } else {
687         process->SetInnerCapState(false);
688     }
689 }
690 
NotifyStreamVolumeChanged(AudioStreamType streamType,float volume)691 int32_t AudioService::NotifyStreamVolumeChanged(AudioStreamType streamType, float volume)
692 {
693     int32_t ret = SUCCESS;
694     for (auto item : endpointList_) {
695         std::string endpointName = item.second->GetEndpointName();
696         if (endpointName == item.first) {
697             ret = ret != SUCCESS ? ret : item.second->SetVolume(streamType, volume);
698         }
699     }
700     return ret;
701 }
702 
LinkProcessToEndpoint(sptr<AudioProcessInServer> process,std::shared_ptr<AudioEndpoint> endpoint)703 int32_t AudioService::LinkProcessToEndpoint(sptr<AudioProcessInServer> process,
704     std::shared_ptr<AudioEndpoint> endpoint)
705 {
706     int32_t ret = endpoint->LinkProcessStream(process);
707     if (ret != SUCCESS && endpoint->GetLinkedProcessCount() == 0 &&
708         endpointList_.count(endpoint->GetEndpointName())) {
709         std::string endpointToErase = endpoint->GetEndpointName();
710         endpointList_.erase(endpoint->GetEndpointName());
711         AUDIO_ERR_LOG("LinkProcessStream failed, erase endpoint %{public}s", endpointToErase.c_str());
712         return ERR_OPERATION_FAILED;
713     }
714     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "LinkProcessStream to endpoint %{public}s failed",
715         endpoint->GetEndpointName().c_str());
716 
717     ret = process->AddProcessStatusListener(endpoint);
718     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "AddProcessStatusListener failed");
719 
720     std::unique_lock<std::mutex> lock(releaseEndpointMutex_);
721     if (releasingEndpointSet_.count(endpoint->GetEndpointName())) {
722         AUDIO_INFO_LOG("LinkProcessToEndpoint find endpoint is releasing, call break.");
723         releasingEndpointSet_.erase(endpoint->GetEndpointName());
724         releaseEndpointCV_.notify_all();
725     }
726     return SUCCESS;
727 }
728 
UnlinkProcessToEndpoint(sptr<AudioProcessInServer> process,std::shared_ptr<AudioEndpoint> endpoint)729 int32_t AudioService::UnlinkProcessToEndpoint(sptr<AudioProcessInServer> process,
730     std::shared_ptr<AudioEndpoint> endpoint)
731 {
732     int32_t ret = endpoint->UnlinkProcessStream(process);
733     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "UnlinkProcessStream failed");
734 
735     ret = process->RemoveProcessStatusListener(endpoint);
736     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "RemoveProcessStatusListener failed");
737 
738     return SUCCESS;
739 }
740 
DelayCallReleaseEndpoint(std::string endpointName,int32_t delayInMs)741 void AudioService::DelayCallReleaseEndpoint(std::string endpointName, int32_t delayInMs)
742 {
743     AUDIO_INFO_LOG("Delay release endpoint [%{public}s] start, delayInMs %{public}d.", endpointName.c_str(), delayInMs);
744     CHECK_AND_RETURN_LOG(endpointList_.count(endpointName),
745         "Find no such endpoint: %{public}s", endpointName.c_str());
746     std::unique_lock<std::mutex> lock(releaseEndpointMutex_);
747     if (delayInMs != 0) {
748         releaseEndpointCV_.wait_for(lock, std::chrono::milliseconds(delayInMs), [this, endpointName] {
749             if (releasingEndpointSet_.count(endpointName)) {
750                 AUDIO_DEBUG_LOG("Wake up but keep release endpoint %{public}s in delay", endpointName.c_str());
751                 return false;
752             }
753             AUDIO_DEBUG_LOG("Delay release endpoint break when reuse: %{public}s", endpointName.c_str());
754             return true;
755         });
756     }
757 
758     if (!releasingEndpointSet_.count(endpointName)) {
759         AUDIO_DEBUG_LOG("Timeout or not need to release: %{public}s", endpointName.c_str());
760         return;
761     }
762     releasingEndpointSet_.erase(endpointName);
763 
764     std::shared_ptr<AudioEndpoint> temp = nullptr;
765     CHECK_AND_RETURN_LOG(endpointList_.find(endpointName) != endpointList_.end() &&
766         endpointList_[endpointName] != nullptr, "Endpoint %{public}s not available, stop call release",
767         endpointName.c_str());
768     temp = endpointList_[endpointName];
769     if (temp->GetStatus() == AudioEndpoint::EndpointStatus::UNLINKED) {
770         AUDIO_INFO_LOG("%{public}s not in use anymore, call release!", endpointName.c_str());
771         temp->Release();
772         temp = nullptr;
773         endpointList_.erase(endpointName);
774         return;
775     }
776     AUDIO_WARNING_LOG("%{public}s is not unlinked, stop call release", endpointName.c_str());
777     return;
778 }
779 
GetDeviceInfoForProcess(const AudioProcessConfig & config)780 AudioDeviceDescriptor AudioService::GetDeviceInfoForProcess(const AudioProcessConfig &config)
781 {
782     // send the config to AudioPolicyServera and get the device info.
783     AudioDeviceDescriptor deviceInfo(AudioDeviceDescriptor::DEVICE_INFO);
784     bool ret = PolicyHandler::GetInstance().GetProcessDeviceInfo(config, false, deviceInfo);
785     if (ret) {
786         AUDIO_INFO_LOG("Get DeviceInfo from policy server success, deviceType: %{public}d, "
787             "supportLowLatency: %{public}d", deviceInfo.deviceType_, deviceInfo.isLowLatencyDevice_);
788         return deviceInfo;
789     } else {
790         AUDIO_WARNING_LOG("GetProcessDeviceInfo from audio policy server failed!");
791     }
792 
793     if (config.audioMode == AUDIO_MODE_RECORD) {
794         deviceInfo.deviceId_ = 1;
795         deviceInfo.networkId_ = LOCAL_NETWORK_ID;
796         deviceInfo.deviceRole_ = INPUT_DEVICE;
797         deviceInfo.deviceType_ = DEVICE_TYPE_MIC;
798     } else {
799         deviceInfo.deviceId_ = 6; // 6 for test
800         deviceInfo.networkId_ = LOCAL_NETWORK_ID;
801         deviceInfo.deviceRole_ = OUTPUT_DEVICE;
802         deviceInfo.deviceType_ = DEVICE_TYPE_SPEAKER;
803     }
804     AudioStreamInfo targetStreamInfo = {SAMPLE_RATE_48000, ENCODING_PCM, SAMPLE_S16LE, STEREO}; // note: read from xml
805     deviceInfo.audioStreamInfo_ = targetStreamInfo;
806     deviceInfo.deviceName_ = "mmap_device";
807     return deviceInfo;
808 }
809 
GetAudioEndpointForDevice(AudioDeviceDescriptor & deviceInfo,const AudioProcessConfig & clientConfig,bool isVoipStream)810 std::shared_ptr<AudioEndpoint> AudioService::GetAudioEndpointForDevice(AudioDeviceDescriptor &deviceInfo,
811     const AudioProcessConfig &clientConfig, bool isVoipStream)
812 {
813     int32_t endpointSeparateFlag = -1;
814     GetSysPara("persist.multimedia.audioflag.fast.disableseparate", endpointSeparateFlag);
815     uint32_t uid = static_cast<uint32_t>(IPCSkeleton::GetCallingUid());
816     if (deviceInfo.deviceRole_ == INPUT_DEVICE || deviceInfo.networkId_ != LOCAL_NETWORK_ID || uid != 0 ||
817         endpointSeparateFlag == 1) {
818         // Create shared stream.
819         int32_t endpointFlag = AUDIO_FLAG_MMAP;
820         if (isVoipStream) {
821             endpointFlag = AUDIO_FLAG_VOIP_FAST;
822         }
823         std::string deviceKey = AudioEndpoint::GenerateEndpointKey(deviceInfo, endpointFlag);
824         if (endpointList_.find(deviceKey) != endpointList_.end()) {
825             AUDIO_INFO_LOG("AudioService find endpoint already exist for deviceKey:%{public}s", deviceKey.c_str());
826             return endpointList_[deviceKey];
827         } else {
828             std::shared_ptr<AudioEndpoint> endpoint = AudioEndpoint::CreateEndpoint(isVoipStream ?
829                 AudioEndpoint::TYPE_VOIP_MMAP : AudioEndpoint::TYPE_MMAP, endpointFlag, clientConfig, deviceInfo);
830             CHECK_AND_RETURN_RET_LOG(endpoint != nullptr, nullptr, "Create mmap AudioEndpoint failed.");
831             AUDIO_INFO_LOG("Add endpoint %{public}s to endpointList_", deviceKey.c_str());
832             endpointList_[deviceKey] = endpoint;
833             return endpoint;
834         }
835     } else {
836         // Create Independent stream.
837         std::string deviceKey = deviceInfo.networkId_ + std::to_string(deviceInfo.deviceId_) + "_" +
838             std::to_string(g_id);
839         std::shared_ptr<AudioEndpoint> endpoint = AudioEndpoint::CreateEndpoint(AudioEndpoint::TYPE_INDEPENDENT,
840             g_id, clientConfig, deviceInfo);
841         CHECK_AND_RETURN_RET_LOG(endpoint != nullptr, nullptr, "Create independent AudioEndpoint failed.");
842         g_id++;
843         AUDIO_INFO_LOG("Add endpointSeperate %{public}s to endpointList_", deviceKey.c_str());
844         endpointList_[deviceKey] = endpoint;
845         return endpoint;
846     }
847 }
848 
Dump(std::string & dumpString)849 void AudioService::Dump(std::string &dumpString)
850 {
851     AUDIO_INFO_LOG("AudioService dump begin");
852     if (workingInnerCapId_ != 0) {
853         AppendFormat(dumpString, "  - InnerCap filter: %s\n",
854             ProcessConfig::DumpInnerCapConfig(workingConfig_).c_str());
855     }
856     // dump process
857     for (auto paired : linkedPairedList_) {
858         paired.first->Dump(dumpString);
859     }
860     // dump endpoint
861     for (auto item : endpointList_) {
862         AppendFormat(dumpString, "  - Endpoint device id: %s\n", item.first.c_str());
863         item.second->Dump(dumpString);
864     }
865     // dump voip and direct
866     {
867         std::lock_guard<std::mutex> lock(rendererMapMutex_);
868         for (const auto &item : allRendererMap_) {
869             std::shared_ptr<RendererInServer> renderer = item.second.lock();
870             if (renderer) {
871                 renderer->Dump(dumpString);
872             }
873         }
874     }
875 
876     // dump appUseNumMap_ and currentRendererStreamCnt_
877     {
878         std::lock_guard<std::mutex> lock(streamLifeCycleMutex_);
879         AppendFormat(dumpString, " - currentRendererStreamCnt is %d\n", currentRendererStreamCnt_);
880         for (auto it : appUseNumMap_) {
881             AppendFormat(dumpString, "  - appUseNumMap_ appUid: %d\n", it.first);
882             AppendFormat(dumpString, "  - appUseNumMap_ appUid created stream: %d\n", it.second);
883         }
884     }
885     PolicyHandler::GetInstance().Dump(dumpString);
886     AudioVolume::GetInstance()->Dump(dumpString);
887 }
888 
GetMaxAmplitude(bool isOutputDevice)889 float AudioService::GetMaxAmplitude(bool isOutputDevice)
890 {
891     std::lock_guard<std::mutex> lock(processListMutex_);
892 
893     if (linkedPairedList_.size() == 0) {
894         return 0;
895     }
896 
897     float fastAudioMaxAmplitude = 0;
898     for (auto paired : linkedPairedList_) {
899         if (isOutputDevice && (paired.second->GetDeviceRole() == OUTPUT_DEVICE)) {
900             float curFastAudioMaxAmplitude = paired.second->GetMaxAmplitude();
901             if (curFastAudioMaxAmplitude > fastAudioMaxAmplitude) {
902                 fastAudioMaxAmplitude = curFastAudioMaxAmplitude;
903             }
904         }
905         if (!isOutputDevice && (paired.second->GetDeviceRole() == INPUT_DEVICE)) {
906             float curFastAudioMaxAmplitude = paired.second->GetMaxAmplitude();
907             if (curFastAudioMaxAmplitude > fastAudioMaxAmplitude) {
908                 fastAudioMaxAmplitude = curFastAudioMaxAmplitude;
909             }
910         }
911     }
912     return fastAudioMaxAmplitude;
913 }
914 
GetRendererBySessionID(const uint32_t & sessionID)915 std::shared_ptr<RendererInServer> AudioService::GetRendererBySessionID(const uint32_t &sessionID)
916 {
917     std::lock_guard<std::mutex> lock(rendererMapMutex_);
918     if (allRendererMap_.count(sessionID)) {
919         return allRendererMap_[sessionID].lock();
920     } else {
921         return nullptr;
922     }
923 }
924 
SetNonInterruptMute(const uint32_t sessionId,const bool muteFlag)925 void AudioService::SetNonInterruptMute(const uint32_t sessionId, const bool muteFlag)
926 {
927     AUDIO_INFO_LOG("SessionId: %{public}u, muteFlag: %{public}d", sessionId, muteFlag);
928     std::unique_lock<std::mutex> rendererLock(rendererMapMutex_);
929     if (allRendererMap_.count(sessionId)) {
930         std::shared_ptr<RendererInServer> renderer = allRendererMap_[sessionId].lock();
931         if (renderer == nullptr) {
932             AUDIO_ERR_LOG("rendererinserver is null");
933             rendererLock.unlock();
934             return;
935         }
936         renderer->SetNonInterruptMute(muteFlag);
937         AUDIO_INFO_LOG("allRendererMap_ has sessionId");
938         rendererLock.unlock();
939         return;
940     }
941     rendererLock.unlock();
942     std::unique_lock<std::mutex> capturerLock(capturerMapMutex_);
943     if (allCapturerMap_.count(sessionId)) {
944         std::shared_ptr<CapturerInServer> capturer = allCapturerMap_[sessionId].lock();
945         if (capturer == nullptr) {
946             AUDIO_ERR_LOG("capturerinserver is null");
947             capturerLock.unlock();
948             return;
949         }
950         capturer->SetNonInterruptMute(muteFlag);
951         AUDIO_INFO_LOG("allCapturerMap_ has sessionId");
952         capturerLock.unlock();
953         return;
954     }
955     capturerLock.unlock();
956     std::unique_lock<std::mutex> processListLock(processListMutex_);
957     for (auto paired : linkedPairedList_) {
958         if (paired.first == nullptr) {
959             AUDIO_ERR_LOG("processInServer is nullptr");
960             processListLock.unlock();
961             return;
962         }
963         if (paired.first->GetSessionId() == sessionId) {
964             AUDIO_INFO_LOG("linkedPairedList_ has sessionId");
965             paired.first->SetNonInterruptMute(muteFlag);
966             processListLock.unlock();
967             return;
968         }
969     }
970     processListLock.unlock();
971     AUDIO_INFO_LOG("Cannot find sessionId");
972 }
973 
UpdateSourceType(SourceType sourceType)974 int32_t AudioService::UpdateSourceType(SourceType sourceType)
975 {
976     // specialSourceType need not updateaudioroute
977     if (specialSourceTypeSet_.contains(sourceType)) {
978         return SUCCESS;
979     }
980 
981     AudioCapturerSource *audioCapturerSourceInstance = AudioCapturerSource::GetInstance("primary");
982     CHECK_AND_RETURN_RET_LOG(audioCapturerSourceInstance != nullptr, ERROR, "source is null");
983 
984     return audioCapturerSourceInstance->UpdateSourceType(sourceType);
985 }
986 
SetIncMaxRendererStreamCnt(AudioMode audioMode)987 void AudioService::SetIncMaxRendererStreamCnt(AudioMode audioMode)
988 {
989     if (audioMode == AUDIO_MODE_PLAYBACK) {
990         currentRendererStreamCnt_++;
991     }
992 }
993 
SetDecMaxRendererStreamCnt()994 void AudioService::SetDecMaxRendererStreamCnt()
995 {
996     std::lock_guard<std::mutex> lock(streamLifeCycleMutex_);
997     currentRendererStreamCnt_--;
998 }
999 
CleanAppUseNumMap(int32_t appUid)1000 void AudioService::CleanAppUseNumMap(int32_t appUid)
1001 {
1002     std::lock_guard<std::mutex> lock(streamLifeCycleMutex_);
1003     auto appUseNum = appUseNumMap_.find(appUid);
1004     if (appUseNum != appUseNumMap_.end()) {
1005         appUseNumMap_[appUid] = --appUseNum->second;
1006     }
1007 }
1008 
GetCurrentRendererStreamCnt()1009 int32_t AudioService::GetCurrentRendererStreamCnt()
1010 {
1011     return currentRendererStreamCnt_;
1012 }
1013 
1014 // need call with streamLifeCycleMutex_ lock
IsExceedingMaxStreamCntPerUid(int32_t callingUid,int32_t appUid,int32_t maxStreamCntPerUid)1015 bool AudioService::IsExceedingMaxStreamCntPerUid(int32_t callingUid, int32_t appUid,
1016     int32_t maxStreamCntPerUid)
1017 {
1018     if (callingUid != MEDIA_SERVICE_UID) {
1019         appUid = callingUid;
1020     }
1021 
1022     auto appUseNum = appUseNumMap_.find(appUid);
1023     if (appUseNum != appUseNumMap_.end()) {
1024         ++appUseNum->second;
1025     } else {
1026         int32_t initValue = 1;
1027         appUseNumMap_.emplace(appUid, initValue);
1028     }
1029 
1030     if (appUseNumMap_[appUid] > maxStreamCntPerUid) {
1031         --appUseNumMap_[appUid]; // actual created stream num is stream num decrease one
1032         return true;
1033     }
1034     return false;
1035 }
1036 
GetCreatedAudioStreamMostUid(int32_t & mostAppUid,int32_t & mostAppNum)1037 void AudioService::GetCreatedAudioStreamMostUid(int32_t &mostAppUid, int32_t &mostAppNum)
1038 {
1039     for (auto it = appUseNumMap_.begin(); it != appUseNumMap_.end(); it++) {
1040         if (it->second > mostAppNum) {
1041             mostAppNum = it->second;
1042             mostAppUid = it->first;
1043         }
1044     }
1045     return;
1046 }
1047 } // namespace AudioStandard
1048 } // namespace OHOS
1049