1 /*
2 * Copyright (c) 2023-2025 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 "audio_errors.h"
24 #include "audio_common_log.h"
25 #include "audio_utils.h"
26 #include "policy_handler.h"
27 #include "ipc_stream_in_server.h"
28 #include "common/hdi_adapter_info.h"
29 #include "manager/hdi_adapter_manager.h"
30 #include "source/i_audio_capture_source.h"
31 #include "audio_volume.h"
32 #include "audio_performance_monitor.h"
33 #include "media_monitor_manager.h"
34 #ifdef HAS_FEATURE_INNERCAPTURER
35 #include "playback_capturer_manager.h"
36 #endif
37
38 namespace OHOS {
39 namespace AudioStandard {
40
41 #ifdef SUPPORT_LOW_LATENCY
42 static uint64_t g_id = 1;
43 static const uint32_t NORMAL_ENDPOINT_RELEASE_DELAY_TIME_MS = 3000; // 3s
44 static const uint32_t A2DP_ENDPOINT_RELEASE_DELAY_TIME = 3000; // 3s
45 static const uint32_t VOIP_ENDPOINT_RELEASE_DELAY_TIME = 200; // 200ms
46 static const uint32_t A2DP_ENDPOINT_RE_CREATE_RELEASE_DELAY_TIME = 200; // 200ms
47 #endif
48 static const uint32_t BLOCK_HIBERNATE_CALLBACK_IN_MS = 5000; // 5s
49 static const int32_t MEDIA_SERVICE_UID = 1013;
50 static const int32_t RENDERER_STREAM_CNT_PER_UID_LIMIT = 40;
51 static const int32_t INVALID_APP_UID = -1;
52 static const int32_t INVALID_APP_CREATED_AUDIO_STREAM_NUM = 0;
53 namespace {
54 static inline const std::unordered_set<SourceType> specialSourceTypeSet_ = {
55 SOURCE_TYPE_PLAYBACK_CAPTURE,
56 SOURCE_TYPE_WAKEUP,
57 SOURCE_TYPE_VIRTUAL_CAPTURE,
58 SOURCE_TYPE_REMOTE_CAST
59 };
60 }
61
GetInstance()62 AudioService *AudioService::GetInstance()
63 {
64 static AudioService AudioService;
65
66 return &AudioService;
67 }
68
AudioService()69 AudioService::AudioService()
70 {
71 AUDIO_INFO_LOG("AudioService()");
72 }
73
~AudioService()74 AudioService::~AudioService()
75 {
76 AUDIO_INFO_LOG("~AudioService()");
77 }
78
79 #ifdef SUPPORT_LOW_LATENCY
OnProcessRelease(IAudioProcessStream * process,bool isSwitchStream)80 int32_t AudioService::OnProcessRelease(IAudioProcessStream *process, bool isSwitchStream)
81 {
82 std::lock_guard<std::mutex> processListLock(processListMutex_);
83 CHECK_AND_RETURN_RET_LOG(process != nullptr, ERROR, "process is nullptr");
84
85 bool isFind = false;
86 int32_t ret = ERROR;
87 auto paired = linkedPairedList_.begin();
88 std::string endpointName;
89 bool needRelease = false;
90 int32_t delayTime = NORMAL_ENDPOINT_RELEASE_DELAY_TIME_MS;
91 while (paired != linkedPairedList_.end()) {
92 if ((*paired).first == process) {
93 AUDIO_INFO_LOG("SessionId %{public}u", (*paired).first->GetSessionId());
94 AudioPerformanceMonitor::GetInstance().DeleteSilenceMonitor(process->GetAudioSessionId());
95 auto processConfig = process->GetAudioProcessConfig();
96 if (processConfig.audioMode == AUDIO_MODE_PLAYBACK) {
97 SetDecMaxRendererStreamCnt();
98 CleanAppUseNumMap(processConfig.appInfo.appUid);
99 }
100 if (!isSwitchStream) {
101 AUDIO_INFO_LOG("is not switch stream, remove from mutedSessions_");
102 RemoveIdFromMuteControlSet((*paired).first->GetSessionId());
103 }
104 ret = UnlinkProcessToEndpoint((*paired).first, (*paired).second);
105 if ((*paired).second->GetStatus() == AudioEndpoint::EndpointStatus::UNLINKED) {
106 needRelease = true;
107 endpointName = (*paired).second->GetEndpointName();
108 delayTime = GetReleaseDelayTime((*paired).second, isSwitchStream);
109 }
110 linkedPairedList_.erase(paired);
111 isFind = true;
112 break;
113 } else {
114 paired++;
115 }
116 }
117 if (isFind) {
118 AUDIO_INFO_LOG("find and release process result %{public}d", ret);
119 } else {
120 AUDIO_INFO_LOG("can not find target process, maybe already released.");
121 }
122 if (needRelease) {
123 ReleaseProcess(endpointName, delayTime);
124 }
125 return SUCCESS;
126 }
127
ReleaseProcess(const std::string endpointName,const int32_t delayTime)128 void AudioService::ReleaseProcess(const std::string endpointName, const int32_t delayTime)
129 {
130 AUDIO_INFO_LOG("find endpoint unlink, call delay release.");
131 std::unique_lock<std::mutex> lock(releaseEndpointMutex_);
132 releasingEndpointSet_.insert(endpointName);
133 auto releaseMidpointThread = [this, endpointName, delayTime] () {
134 this->DelayCallReleaseEndpoint(endpointName, delayTime);
135 };
136 std::thread releaseEndpointThread(releaseMidpointThread);
137 releaseEndpointThread.detach();
138 }
139
GetReleaseDelayTime(std::shared_ptr<AudioEndpoint> endpoint,bool isSwitchStream)140 int32_t AudioService::GetReleaseDelayTime(std::shared_ptr<AudioEndpoint> endpoint, bool isSwitchStream)
141 {
142 if (endpoint->GetEndpointType() == AudioEndpoint::EndpointType::TYPE_VOIP_MMAP) {
143 return VOIP_ENDPOINT_RELEASE_DELAY_TIME;
144 }
145
146 if (endpoint->GetDeviceInfo().deviceType_ != DEVICE_TYPE_BLUETOOTH_A2DP) {
147 return NORMAL_ENDPOINT_RELEASE_DELAY_TIME_MS;
148 }
149 if (!isSwitchStream) {
150 return A2DP_ENDPOINT_RELEASE_DELAY_TIME;
151 }
152 // The delay for destruction and reconstruction cannot be set to 0, otherwise there may be a problem:
153 // An endpoint exists at check process, but it may be destroyed immediately - during the re-create process
154 return A2DP_ENDPOINT_RE_CREATE_RELEASE_DELAY_TIME;
155 }
156 #endif
157
GetIpcStream(const AudioProcessConfig & config,int32_t & ret)158 sptr<IpcStreamInServer> AudioService::GetIpcStream(const AudioProcessConfig &config, int32_t &ret)
159 {
160 Trace trace("AudioService::GetIpcStream");
161 #ifdef HAS_FEATURE_INNERCAPTURER
162 if (!isRegisterCapturerFilterListened_) {
163 PlaybackCapturerManager::GetInstance()->RegisterCapturerFilterListener(this);
164 isRegisterCapturerFilterListened_ = true;
165 }
166 #endif
167 // in plan: GetDeviceInfoForProcess(config) and stream limit check
168 // in plan: call GetProcessDeviceInfo to load inner-cap-sink
169 sptr<IpcStreamInServer> ipcStreamInServer = IpcStreamInServer::Create(config, ret);
170
171 // in plan: Put playback into list, check if EnableInnerCap is need.
172 if (ipcStreamInServer != nullptr && config.audioMode == AUDIO_MODE_PLAYBACK) {
173 uint32_t sessionId = 0;
174 std::shared_ptr<RendererInServer> renderer = ipcStreamInServer->GetRenderer();
175 if (renderer != nullptr && renderer->GetSessionId(sessionId) == SUCCESS) {
176 InsertRenderer(sessionId, renderer); // for all renderers
177 #ifdef HAS_FEATURE_INNERCAPTURER
178 CheckInnerCapForRenderer(sessionId, renderer);
179 #endif
180 CheckRenderSessionMuteState(sessionId, renderer);
181 }
182 }
183 if (ipcStreamInServer != nullptr && config.audioMode == AUDIO_MODE_RECORD) {
184 uint32_t sessionId = 0;
185 std::shared_ptr<CapturerInServer> capturer = ipcStreamInServer->GetCapturer();
186 if (capturer != nullptr && capturer->GetSessionId(sessionId) == SUCCESS) {
187 InsertCapturer(sessionId, capturer); // for all capturers
188 CheckCaptureSessionMuteState(sessionId, capturer);
189 }
190 }
191
192 return ipcStreamInServer;
193 }
194
UpdateMuteControlSet(uint32_t sessionId,bool muteFlag)195 void AudioService::UpdateMuteControlSet(uint32_t sessionId, bool muteFlag)
196 {
197 if (sessionId < MIN_STREAMID || sessionId > MAX_STREAMID) {
198 AUDIO_WARNING_LOG("Invalid sessionid %{public}u", sessionId);
199 return;
200 }
201 std::lock_guard<std::mutex> lock(mutedSessionsMutex_);
202 if (muteFlag) {
203 mutedSessions_.insert(sessionId);
204 return;
205 }
206 if (mutedSessions_.find(sessionId) != mutedSessions_.end()) {
207 mutedSessions_.erase(sessionId);
208 } else {
209 AUDIO_WARNING_LOG("Session id %{public}u not in the set", sessionId);
210 }
211 }
212
RemoveIdFromMuteControlSet(uint32_t sessionId)213 void AudioService::RemoveIdFromMuteControlSet(uint32_t sessionId)
214 {
215 std::lock_guard<std::mutex> mutedSessionsLock(mutedSessionsMutex_);
216 if (mutedSessions_.find(sessionId) != mutedSessions_.end()) {
217 mutedSessions_.erase(sessionId);
218 } else {
219 AUDIO_WARNING_LOG("Session id %{public}u not in the set", sessionId);
220 }
221 }
222
CheckRenderSessionMuteState(uint32_t sessionId,std::shared_ptr<RendererInServer> renderer)223 void AudioService::CheckRenderSessionMuteState(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer)
224 {
225 std::unique_lock<std::mutex> mutedSessionsLock(mutedSessionsMutex_);
226 if (mutedSessions_.find(sessionId) != mutedSessions_.end() || IsMuteSwitchStream(sessionId)) {
227 mutedSessionsLock.unlock();
228 AUDIO_INFO_LOG("Session %{public}u is in control", sessionId);
229 renderer->SetNonInterruptMute(true);
230 }
231 }
232
CheckCaptureSessionMuteState(uint32_t sessionId,std::shared_ptr<CapturerInServer> capturer)233 void AudioService::CheckCaptureSessionMuteState(uint32_t sessionId, std::shared_ptr<CapturerInServer> capturer)
234 {
235 std::unique_lock<std::mutex> mutedSessionsLock(mutedSessionsMutex_);
236 if (mutedSessions_.find(sessionId) != mutedSessions_.end() || IsMuteSwitchStream(sessionId)) {
237 mutedSessionsLock.unlock();
238 AUDIO_INFO_LOG("Session %{public}u is in control", sessionId);
239 capturer->SetNonInterruptMute(true);
240 }
241 }
242
243 #ifdef SUPPORT_LOW_LATENCY
CheckFastSessionMuteState(uint32_t sessionId,sptr<AudioProcessInServer> process)244 void AudioService::CheckFastSessionMuteState(uint32_t sessionId, sptr<AudioProcessInServer> process)
245 {
246 std::unique_lock<std::mutex> mutedSessionsLock(mutedSessionsMutex_);
247 if (mutedSessions_.find(sessionId) != mutedSessions_.end() || IsMuteSwitchStream(sessionId)) {
248 mutedSessionsLock.unlock();
249 AUDIO_INFO_LOG("Session %{public}u is in control", sessionId);
250 process->SetNonInterruptMute(true);
251 }
252 }
253 #endif
254
IsMuteSwitchStream(uint32_t sessionId)255 bool AudioService::IsMuteSwitchStream(uint32_t sessionId)
256 {
257 std::lock_guard<std::mutex> muteSwitchStreamLock(muteSwitchStreamSetMutex_);
258 if (muteSwitchStreams_.count(sessionId)) {
259 AUDIO_INFO_LOG("find session %{public}u in muteSwitchStreams_", sessionId);
260 muteSwitchStreams_.erase(sessionId);
261 return true;
262 }
263 return false;
264 }
265
InsertRenderer(uint32_t sessionId,std::shared_ptr<RendererInServer> renderer)266 void AudioService::InsertRenderer(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer)
267 {
268 std::unique_lock<std::mutex> lock(rendererMapMutex_);
269 AUDIO_INFO_LOG("Insert renderer:%{public}u into map", sessionId);
270 allRendererMap_[sessionId] = renderer;
271 }
272
GetStandbyStatus(uint32_t sessionId,bool & isStandby,int64_t & enterStandbyTime)273 int32_t AudioService::GetStandbyStatus(uint32_t sessionId, bool &isStandby, int64_t &enterStandbyTime)
274 {
275 // for normal renderer.
276 std::unique_lock<std::mutex> lockRender(rendererMapMutex_);
277 if (allRendererMap_.count(sessionId)) {
278 std::shared_ptr<RendererInServer> render = allRendererMap_[sessionId].lock();
279 if (render == nullptr) {
280 return ERR_INVALID_PARAM;
281 }
282 return render->GetStandbyStatus(isStandby, enterStandbyTime);
283 }
284 lockRender.unlock();
285
286 // for fast process.
287 #ifdef SUPPORT_LOW_LATENCY
288 std::unique_lock<std::mutex> lockProcess(processListMutex_);
289 for (auto paired : linkedPairedList_) {
290 sptr<AudioProcessInServer> process = paired.first;
291 if (process->GetSessionId() == sessionId) {
292 return process->GetStandbyStatus(isStandby, enterStandbyTime);
293 }
294 }
295 #endif
296 // not found target sessionId
297 return ERR_INVALID_PARAM;
298 }
299
RemoveRenderer(uint32_t sessionId)300 void AudioService::RemoveRenderer(uint32_t sessionId)
301 {
302 std::unique_lock<std::mutex> lock(rendererMapMutex_);
303 AUDIO_INFO_LOG("Renderer:%{public}u will be removed.", sessionId);
304 if (!allRendererMap_.count(sessionId)) {
305 AUDIO_WARNING_LOG("Renderer in not in map!");
306 return;
307 }
308 allRendererMap_.erase(sessionId);
309 RemoveIdFromMuteControlSet(sessionId);
310 AudioPerformanceMonitor::GetInstance().DeleteSilenceMonitor(sessionId);
311 }
312
InsertCapturer(uint32_t sessionId,std::shared_ptr<CapturerInServer> capturer)313 void AudioService::InsertCapturer(uint32_t sessionId, std::shared_ptr<CapturerInServer> capturer)
314 {
315 std::unique_lock<std::mutex> lock(capturerMapMutex_);
316 AUDIO_INFO_LOG("Insert capturer:%{public}u into map", sessionId);
317 allCapturerMap_[sessionId] = capturer;
318 }
319
RemoveCapturer(uint32_t sessionId)320 void AudioService::RemoveCapturer(uint32_t sessionId)
321 {
322 std::unique_lock<std::mutex> lock(capturerMapMutex_);
323 AUDIO_INFO_LOG("Capturer: %{public}u will be removed.", sessionId);
324 if (!allCapturerMap_.count(sessionId)) {
325 AUDIO_WARNING_LOG("Capturer in not in map!");
326 return;
327 }
328 allCapturerMap_.erase(sessionId);
329 RemoveIdFromMuteControlSet(sessionId);
330 }
331
AddFilteredRender(int32_t innerCapId,std::shared_ptr<RendererInServer> renderer)332 void AudioService::AddFilteredRender(int32_t innerCapId, std::shared_ptr<RendererInServer> renderer)
333 {
334 if (!filteredRendererMap_.count(innerCapId)) {
335 std::vector<std::weak_ptr<RendererInServer>> renders;
336 filteredRendererMap_[innerCapId] = renders;
337 }
338 filteredRendererMap_[innerCapId].push_back(renderer);
339 }
340
341 #ifdef HAS_FEATURE_INNERCAPTURER
CheckInnerCapForRenderer(uint32_t sessionId,std::shared_ptr<RendererInServer> renderer)342 void AudioService::CheckInnerCapForRenderer(uint32_t sessionId, std::shared_ptr<RendererInServer> renderer)
343 {
344 CHECK_AND_RETURN_LOG(renderer != nullptr, "renderer is null.");
345
346 std::unique_lock<std::mutex> lock(rendererMapMutex_);
347
348 // inner-cap not working
349 if (workingConfigs_.size() == 0) {
350 return;
351 }
352 // in plan: check if meet with the workingConfig_
353 std::set<int32_t> captureIds;
354 if (ShouldBeInnerCap(renderer->processConfig_, captureIds)) {
355 for (auto innerCapId : captureIds) {
356 AddFilteredRender(innerCapId, renderer);
357 renderer->EnableInnerCap(innerCapId); // for debug
358 }
359 }
360 }
361
GetInnerCapFilterPolicy(int32_t innerCapId)362 InnerCapFilterPolicy AudioService::GetInnerCapFilterPolicy(int32_t innerCapId)
363 {
364 if (!workingConfigs_.count(innerCapId)) {
365 AUDIO_ERR_LOG("error, invalid innerCapId");
366 return POLICY_INVALID;
367 }
368 auto usagesSize = workingConfigs_[innerCapId].filterOptions.usages.size();
369 auto pidsSize = workingConfigs_[innerCapId].filterOptions.pids.size();
370 if (usagesSize == 0 && pidsSize == 0) {
371 AUDIO_ERR_LOG("error, invalid usages and pids");
372 return POLICY_INVALID;
373 }
374 if (usagesSize > 0 && pidsSize == 0) {
375 AUDIO_INFO_LOG("usages only");
376 return POLICY_USAGES_ONLY;
377 }
378 return POLICY_USAGES_AND_PIDS;
379 }
380
381 template<typename T>
isFilterMatched(const std::vector<T> & params,T param,FilterMode mode)382 bool isFilterMatched(const std::vector<T> ¶ms, T param, FilterMode mode)
383 {
384 bool isFound = std::count(params.begin(), params.end(), param) != 0;
385 return (mode == FilterMode::INCLUDE && isFound) || (mode == FilterMode::EXCLUDE && !isFound);
386 }
387
ShouldBeInnerCap(const AudioProcessConfig & rendererConfig,int32_t innerCapId)388 bool AudioService::ShouldBeInnerCap(const AudioProcessConfig &rendererConfig, int32_t innerCapId)
389 {
390 bool canBeCaptured = rendererConfig.privacyType == AudioPrivacyType::PRIVACY_TYPE_PUBLIC;
391 if (!canBeCaptured || innerCapId == 0 || !workingConfigs_.count(innerCapId)) {
392 AUDIO_WARNING_LOG("%{public}d privacy is not public!", rendererConfig.appInfo.appPid);
393 return false;
394 }
395 return CheckShouldCap(rendererConfig, innerCapId);
396 }
397
ShouldBeInnerCap(const AudioProcessConfig & rendererConfig,std::set<int32_t> & beCapIds)398 bool AudioService::ShouldBeInnerCap(const AudioProcessConfig &rendererConfig, std::set<int32_t> &beCapIds)
399 {
400 bool canBeCaptured = rendererConfig.privacyType == AudioPrivacyType::PRIVACY_TYPE_PUBLIC;
401 if (!canBeCaptured) {
402 AUDIO_WARNING_LOG("%{public}d privacy is not public!", rendererConfig.appInfo.appPid);
403 return false;
404 }
405 bool ret = false;
406 for (auto& filter : workingConfigs_) {
407 if (CheckShouldCap(rendererConfig, filter.first)) {
408 ret = true;
409 beCapIds.insert(filter.first);
410 }
411 }
412 return ret;
413 }
414
CheckShouldCap(const AudioProcessConfig & rendererConfig,int32_t innerCapId)415 bool AudioService::CheckShouldCap(const AudioProcessConfig &rendererConfig, int32_t innerCapId)
416 {
417 InnerCapFilterPolicy filterPolicy = GetInnerCapFilterPolicy(innerCapId);
418 bool res = false;
419 switch (filterPolicy) {
420 case POLICY_INVALID:
421 return false;
422 case POLICY_USAGES_ONLY:
423 res = isFilterMatched(workingConfigs_[innerCapId].filterOptions.usages,
424 rendererConfig.rendererInfo.streamUsage, workingConfigs_[innerCapId].filterOptions.usageFilterMode);
425 break;
426 case POLICY_USAGES_AND_PIDS:
427 res = isFilterMatched(workingConfigs_[innerCapId].filterOptions.usages,
428 rendererConfig.rendererInfo.streamUsage,
429 workingConfigs_[innerCapId].filterOptions.usageFilterMode) &&
430 isFilterMatched(workingConfigs_[innerCapId].filterOptions.pids, rendererConfig.appInfo.appPid,
431 workingConfigs_[innerCapId].filterOptions.pidFilterMode);
432 break;
433 default:
434 break;
435 }
436 AUDIO_INFO_LOG("pid:%{public}d usage:%{public}d result:%{public}s capId:%{public}d",
437 rendererConfig.appInfo.appPid, rendererConfig.rendererInfo.streamUsage, res ? "true" : "false", innerCapId);
438 return res;
439 }
440 #endif
441
ShouldBeDualTone(const AudioProcessConfig & config)442 bool AudioService::ShouldBeDualTone(const AudioProcessConfig &config)
443 {
444 CHECK_AND_RETURN_RET_LOG(Util::IsRingerOrAlarmerStreamUsage(config.rendererInfo.streamUsage), false,
445 "Wrong usage ,should not be dualtone");
446 AudioDeviceDescriptor deviceInfo(AudioDeviceDescriptor::DEVICE_INFO);
447 bool ret = PolicyHandler::GetInstance().GetProcessDeviceInfo(config, false, deviceInfo);
448 if (!ret) {
449 AUDIO_WARNING_LOG("GetProcessDeviceInfo from audio policy server failed!");
450 return false;
451 }
452 if (config.audioMode != AUDIO_MODE_PLAYBACK) {
453 AUDIO_WARNING_LOG("No playback mode!");
454 return false;
455 }
456 AUDIO_INFO_LOG("Get DeviceInfo from policy server success, deviceType: %{public}d, "
457 "supportLowLatency: %{public}d", deviceInfo.deviceType_, deviceInfo.isLowLatencyDevice_);
458 if (deviceInfo.deviceType_ == DEVICE_TYPE_WIRED_HEADSET || deviceInfo.deviceType_ == DEVICE_TYPE_WIRED_HEADPHONES ||
459 deviceInfo.deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP || deviceInfo.deviceType_ == DEVICE_TYPE_USB_HEADSET ||
460 deviceInfo.deviceType_ == DEVICE_TYPE_USB_ARM_HEADSET || deviceInfo.deviceType_ == DEVICE_TYPE_REMOTE_CAST ||
461 (deviceInfo.deviceType_ == DEVICE_TYPE_SPEAKER && deviceInfo.networkId_ != std::string(LOCAL_NETWORK_ID))) {
462 switch (config.rendererInfo.streamUsage) {
463 case STREAM_USAGE_ALARM:
464 case STREAM_USAGE_VOICE_RINGTONE:
465 case STREAM_USAGE_RINGTONE:
466 AUDIO_WARNING_LOG("Should DualTone.");
467 return true;
468 default:
469 return false;
470 }
471 }
472 return false;
473 }
474
475 #ifdef HAS_FEATURE_INNERCAPTURER
476 #ifdef SUPPORT_LOW_LATENCY
FilterAllFastProcess()477 void AudioService::FilterAllFastProcess()
478 {
479 std::unique_lock<std::mutex> lock(processListMutex_);
480 if (linkedPairedList_.size() == 0) {
481 return;
482 }
483 for (auto paired : linkedPairedList_) {
484 AudioProcessConfig temp = paired.first->processConfig_;
485 std::set<int32_t> captureIds;
486 if (temp.audioMode == AUDIO_MODE_PLAYBACK && ShouldBeInnerCap(temp, captureIds)) {
487 HandleFastCapture(captureIds, paired.first, paired.second);
488 }
489 }
490
491 for (auto pair : endpointList_) {
492 if (pair.second->GetDeviceRole() == OUTPUT_DEVICE) {
493 CheckDisableFastInner(pair.second);
494 }
495 }
496 }
497 #endif
498
CheckDisableFastInner(std::shared_ptr<AudioEndpoint> endpoint)499 int32_t AudioService::CheckDisableFastInner(std::shared_ptr<AudioEndpoint> endpoint)
500 {
501 for (auto workingConfig : workingConfigs_) {
502 if (!endpoint->ShouldInnerCap(workingConfig.first)) {
503 endpoint->DisableFastInnerCap(workingConfig.first);
504 }
505 }
506 return SUCCESS;
507 }
508
HandleFastCapture(std::set<int32_t> captureIds,sptr<AudioProcessInServer> audioProcessInServer,std::shared_ptr<AudioEndpoint> audioEndpoint)509 int32_t AudioService::HandleFastCapture(std::set<int32_t> captureIds, sptr<AudioProcessInServer> audioProcessInServer,
510 std::shared_ptr<AudioEndpoint> audioEndpoint)
511 {
512 for (auto captureId : captureIds) {
513 audioProcessInServer->SetInnerCapState(true, captureId);
514 audioEndpoint->EnableFastInnerCap(captureId);
515 }
516 return SUCCESS;
517 }
518
OnInitInnerCapList(int32_t innerCapId)519 int32_t AudioService::OnInitInnerCapList(int32_t innerCapId)
520 {
521 AUDIO_INFO_LOG("workingInnerCapId_ is %{public}d", innerCapId);
522 #ifdef SUPPORT_LOW_LATENCY
523 FilterAllFastProcess();
524 #endif
525
526 // strong ref to prevent destruct before unlock
527 std::vector<std::shared_ptr<RendererInServer>> renderers;
528
529 {
530 std::unique_lock<std::mutex> lock(rendererMapMutex_);
531 for (auto it = allRendererMap_.begin(); it != allRendererMap_.end(); it++) {
532 std::shared_ptr<RendererInServer> renderer = it->second.lock();
533 if (renderer == nullptr) {
534 AUDIO_WARNING_LOG("Renderer is already released!");
535 continue;
536 }
537 if (ShouldBeInnerCap(renderer->processConfig_, innerCapId)) {
538 renderer->EnableInnerCap(innerCapId);
539 AddFilteredRender(innerCapId, renderer);
540 }
541 renderers.push_back(std::move(renderer));
542 }
543 }
544
545 return SUCCESS;
546 }
547
OnUpdateInnerCapList(int32_t innerCapId)548 int32_t AudioService::OnUpdateInnerCapList(int32_t innerCapId)
549 {
550 AUDIO_INFO_LOG("workingInnerCapId_ is %{public}d", innerCapId);
551
552 std::unique_lock<std::mutex> lock(rendererMapMutex_);
553 if (filteredRendererMap_.count(innerCapId)) {
554 for (size_t i = 0; i < filteredRendererMap_[innerCapId].size(); i++) {
555 std::shared_ptr<RendererInServer> renderer = filteredRendererMap_[innerCapId][i].lock();
556 if (renderer == nullptr) {
557 AUDIO_WARNING_LOG("Renderer is already released!");
558 continue;
559 }
560 if (!ShouldBeInnerCap(renderer->processConfig_, innerCapId)) {
561 renderer->DisableInnerCap(innerCapId);
562 }
563 }
564 filteredRendererMap_.erase(innerCapId);
565 }
566 lock.unlock();
567 // EnableInnerCap will be called twice as it's already in filteredRendererMap_.
568 return OnInitInnerCapList(innerCapId);
569 }
570 #endif
571
EnableDualToneList(uint32_t sessionId)572 int32_t AudioService::EnableDualToneList(uint32_t sessionId)
573 {
574 workingDualToneId_ = sessionId;
575 AUDIO_INFO_LOG("EnableDualToneList sessionId is %{public}d", sessionId);
576 std::unique_lock<std::mutex> lock(rendererMapMutex_);
577 for (auto it = allRendererMap_.begin(); it != allRendererMap_.end(); it++) {
578 std::shared_ptr<RendererInServer> renderer = it->second.lock();
579 if (renderer == nullptr) {
580 AUDIO_WARNING_LOG("Renderer is already released!");
581 continue;
582 }
583 if (ShouldBeDualTone(renderer->processConfig_)) {
584 renderer->EnableDualTone();
585 filteredDualToneRendererMap_.push_back(renderer);
586 }
587 }
588 return SUCCESS;
589 }
590
DisableDualToneList(uint32_t sessionId)591 int32_t AudioService::DisableDualToneList(uint32_t sessionId)
592 {
593 AUDIO_INFO_LOG("disable dual tone, sessionId is %{public}d", sessionId);
594 std::unique_lock<std::mutex> lock(rendererMapMutex_);
595 for (size_t i = 0; i < filteredDualToneRendererMap_.size(); i++) {
596 std::shared_ptr<RendererInServer> renderer = filteredDualToneRendererMap_[i].lock();
597 if (renderer == nullptr) {
598 AUDIO_WARNING_LOG("Renderer is already released!");
599 continue;
600 }
601 renderer->DisableDualTone();
602 }
603 filteredDualToneRendererMap_.clear();
604 return SUCCESS;
605 }
606
607 // Only one session is working at the same time.
OnCapturerFilterChange(uint32_t sessionId,const AudioPlaybackCaptureConfig & newConfig,int32_t innerCapId)608 int32_t AudioService::OnCapturerFilterChange(uint32_t sessionId, const AudioPlaybackCaptureConfig &newConfig,
609 int32_t innerCapId)
610 {
611 #ifdef HAS_FEATURE_INNERCAPTURER
612 Trace trace("AudioService::OnCapturerFilterChange");
613 // in plan:
614 // step 1: if sessionId is not added before, add the sessionId and enbale the filter in allRendererMap_
615 // step 2: if sessionId is already in using, this means the config is changed. Check the filtered renderer before,
616 // call disable inner-cap for those not meet with the new config, than filter all allRendererMap_.
617
618 if (workingConfigs_.count(innerCapId)) {
619 workingConfigs_[innerCapId] = newConfig;
620 return OnUpdateInnerCapList(innerCapId);
621 } else {
622 workingConfigs_[innerCapId] = newConfig;
623 return OnInitInnerCapList(innerCapId);
624 }
625
626 AUDIO_WARNING_LOG("%{public}u is working, comming %{public}u will not work!", innerCapId, sessionId);
627 return ERR_OPERATION_FAILED;
628 #endif
629 return SUCCESS;
630 }
631
OnCapturerFilterRemove(uint32_t sessionId,int32_t innerCapId)632 int32_t AudioService::OnCapturerFilterRemove(uint32_t sessionId, int32_t innerCapId)
633 {
634 #ifdef HAS_FEATURE_INNERCAPTURER
635 if (!workingConfigs_.count(innerCapId)) {
636 AUDIO_WARNING_LOG("%{public}u is working, remove %{public}u will not work!", innerCapId, sessionId);
637 return SUCCESS;
638 }
639 workingConfigs_.erase(innerCapId);
640
641 #ifdef SUPPORT_LOW_LATENCY
642 std::unique_lock<std::mutex> lockEndpoint(processListMutex_);
643 for (auto pair : endpointList_) {
644 if (pair.second->GetDeviceRole() == OUTPUT_DEVICE) {
645 pair.second->DisableFastInnerCap(innerCapId);
646 }
647 }
648 lockEndpoint.unlock();
649 #endif
650
651 // strong ref to prevent destruct before unlock
652 std::vector<std::shared_ptr<RendererInServer>> renderers;
653
654 {
655 std::lock_guard<std::mutex> lock(rendererMapMutex_);
656 if (filteredRendererMap_.count(innerCapId)) {
657 for (size_t i = 0; i < filteredRendererMap_[innerCapId].size(); i++) {
658 std::shared_ptr<RendererInServer> renderer = filteredRendererMap_[innerCapId][i].lock();
659 if (renderer == nullptr) {
660 AUDIO_WARNING_LOG("Find renderer is already released!");
661 continue;
662 }
663 renderer->DisableInnerCap(innerCapId);
664 renderers.push_back(std::move(renderer));
665 }
666 AUDIO_INFO_LOG("Filter removed, clear %{public}zu filtered renderer.",
667 filteredRendererMap_[innerCapId].size());
668 filteredRendererMap_.erase(innerCapId);
669 }
670 }
671 #endif
672 return SUCCESS;
673 }
674
IsEndpointTypeVoip(const AudioProcessConfig & config,AudioDeviceDescriptor & deviceInfo)675 bool AudioService::IsEndpointTypeVoip(const AudioProcessConfig &config, AudioDeviceDescriptor &deviceInfo)
676 {
677 if (config.rendererInfo.streamUsage == STREAM_USAGE_VOICE_COMMUNICATION ||
678 config.rendererInfo.streamUsage == STREAM_USAGE_VIDEO_COMMUNICATION) {
679 return config.rendererInfo.originalFlag == AUDIO_FLAG_VOIP_FAST || deviceInfo.networkId_ != LOCAL_NETWORK_ID;
680 }
681
682 if (config.capturerInfo.sourceType == SOURCE_TYPE_VOICE_COMMUNICATION) {
683 return config.capturerInfo.originalFlag == AUDIO_FLAG_VOIP_FAST || deviceInfo.networkId_ != LOCAL_NETWORK_ID;
684 }
685 return false;
686 }
687
688 #ifdef SUPPORT_LOW_LATENCY
GetAudioProcess(const AudioProcessConfig & config)689 sptr<AudioProcessInServer> AudioService::GetAudioProcess(const AudioProcessConfig &config)
690 {
691 int32_t ret = SUCCESS;
692 if (config.streamType != STREAM_VOICE_CALL && config.streamType != STREAM_VOICE_COMMUNICATION) {
693 AudioPipeType incomingPipe = config.audioMode == AUDIO_MODE_PLAYBACK ?
694 PIPE_TYPE_LOWLATENCY_OUT : PIPE_TYPE_LOWLATENCY_IN;
695 ret = PolicyHandler::GetInstance().ActivateConcurrencyFromServer(incomingPipe);
696 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, nullptr, "Concede incoming lowlatency stream from server");
697 }
698 Trace trace("AudioService::GetAudioProcess for " + std::to_string(config.appInfo.appPid));
699 AUDIO_INFO_LOG("GetAudioProcess dump %{public}s", ProcessConfig::DumpProcessConfig(config).c_str());
700 AudioDeviceDescriptor deviceInfo = GetDeviceInfoForProcess(config);
701 std::lock_guard<std::mutex> lock(processListMutex_);
702 std::shared_ptr<AudioEndpoint> audioEndpoint = GetAudioEndpointForDevice(deviceInfo, config,
703 IsEndpointTypeVoip(config, deviceInfo));
704 CHECK_AND_RETURN_RET_LOG(audioEndpoint != nullptr, nullptr, "no endpoint found for the process");
705
706 uint32_t totalSizeInframe = 0;
707 uint32_t spanSizeInframe = 0;
708 audioEndpoint->GetPreferBufferInfo(totalSizeInframe, spanSizeInframe);
709 CHECK_AND_RETURN_RET_LOG(*deviceInfo.audioStreamInfo_.samplingRate.rbegin() > 0, nullptr,
710 "Sample rate in server is invalid.");
711
712 sptr<AudioProcessInServer> process = AudioProcessInServer::Create(config, this);
713 CHECK_AND_RETURN_RET_LOG(process != nullptr, nullptr, "AudioProcessInServer create failed.");
714 CheckFastSessionMuteState(process->GetSessionId(), process);
715
716 std::shared_ptr<OHAudioBuffer> buffer = audioEndpoint->GetEndpointType()
717 == AudioEndpoint::TYPE_INDEPENDENT ? audioEndpoint->GetBuffer() : nullptr;
718 ret = process->ConfigProcessBuffer(totalSizeInframe, spanSizeInframe, deviceInfo.audioStreamInfo_, buffer);
719 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, nullptr, "ConfigProcessBuffer failed");
720
721 ret = LinkProcessToEndpoint(process, audioEndpoint);
722 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, nullptr, "LinkProcessToEndpoint failed");
723 linkedPairedList_.push_back(std::make_pair(process, audioEndpoint));
724 #ifdef HAS_FEATURE_INNERCAPTURER
725 CheckInnerCapForProcess(process, audioEndpoint);
726 #endif
727 return process;
728 }
729
ResetAudioEndpoint()730 void AudioService::ResetAudioEndpoint()
731 {
732 std::lock_guard<std::mutex> lock(processListMutex_);
733
734 std::vector<std::string> audioEndpointNames;
735 for (auto paired = linkedPairedList_.begin(); paired != linkedPairedList_.end(); paired++) {
736 if (paired->second->GetEndpointType() == AudioEndpoint::TYPE_MMAP) {
737 // unlink old link
738 if (UnlinkProcessToEndpoint(paired->first, paired->second) != SUCCESS) {
739 AUDIO_ERR_LOG("Unlink process to old endpoint failed");
740 }
741 audioEndpointNames.push_back(paired->second->GetEndpointName());
742 }
743 }
744
745 // release old endpoint
746 for (auto &endpointName : audioEndpointNames) {
747 if (endpointList_.count(endpointName) > 0) {
748 endpointList_[endpointName]->Release();
749 AUDIO_INFO_LOG("Erase endpoint %{public}s from endpointList_", endpointName.c_str());
750 endpointList_.erase(endpointName);
751 }
752 }
753
754 ReLinkProcessToEndpoint();
755 }
756
ReLinkProcessToEndpoint()757 void AudioService::ReLinkProcessToEndpoint()
758 {
759 using LinkPair = std::pair<sptr<AudioProcessInServer>, std::shared_ptr<AudioEndpoint>>;
760 std::vector<std::vector<LinkPair>::iterator> errorLinkedPaireds;
761 for (auto paired = linkedPairedList_.begin(); paired != linkedPairedList_.end(); paired++) {
762 if (paired->second->GetEndpointType() == AudioEndpoint::TYPE_MMAP) {
763 AUDIO_INFO_LOG("Session id %{public}u", paired->first->GetSessionId());
764
765 // get new endpoint
766 const AudioProcessConfig &config = paired->first->processConfig_;
767 AudioDeviceDescriptor deviceInfo = GetDeviceInfoForProcess(config);
768 std::shared_ptr<AudioEndpoint> audioEndpoint = GetAudioEndpointForDevice(deviceInfo, config,
769 IsEndpointTypeVoip(config, deviceInfo));
770 if (audioEndpoint == nullptr) {
771 AUDIO_ERR_LOG("Get new endpoint failed");
772 errorLinkedPaireds.push_back(paired);
773 continue;
774 }
775 // link new endpoint
776 if (LinkProcessToEndpoint(paired->first, audioEndpoint) != SUCCESS) {
777 AUDIO_ERR_LOG("LinkProcessToEndpoint failed");
778 errorLinkedPaireds.push_back(paired);
779 continue;
780 }
781 // reset shared_ptr before to new
782 paired->second.reset();
783 paired->second = audioEndpoint;
784 #ifdef HAS_FEATURE_INNERCAPTURER
785 CheckInnerCapForProcess(paired->first, audioEndpoint);
786 #endif
787 }
788 }
789
790 for (auto &paired : errorLinkedPaireds) {
791 linkedPairedList_.erase(paired);
792 }
793 }
794
795 #ifdef HAS_FEATURE_INNERCAPTURER
CheckInnerCapForProcess(sptr<AudioProcessInServer> process,std::shared_ptr<AudioEndpoint> endpoint)796 void AudioService::CheckInnerCapForProcess(sptr<AudioProcessInServer> process, std::shared_ptr<AudioEndpoint> endpoint)
797 {
798 Trace trace("AudioService::CheckInnerCapForProcess:" + std::to_string(process->processConfig_.appInfo.appPid));
799 // inner-cap not working
800 std::set<int32_t> captureIds;
801 if (ShouldBeInnerCap(process->processConfig_, captureIds)) {
802 HandleFastCapture(captureIds, process, endpoint);
803 }
804 }
805 #endif
806
LinkProcessToEndpoint(sptr<AudioProcessInServer> process,std::shared_ptr<AudioEndpoint> endpoint)807 int32_t AudioService::LinkProcessToEndpoint(sptr<AudioProcessInServer> process,
808 std::shared_ptr<AudioEndpoint> endpoint)
809 {
810 int32_t ret = endpoint->LinkProcessStream(process, !GetHibernateState());
811 if (ret != SUCCESS && endpoint->GetLinkedProcessCount() == 0 &&
812 endpointList_.count(endpoint->GetEndpointName())) {
813 std::string endpointToErase = endpoint->GetEndpointName();
814 endpointList_.erase(endpoint->GetEndpointName());
815 AUDIO_ERR_LOG("LinkProcessStream failed, erase endpoint %{public}s", endpointToErase.c_str());
816 return ERR_OPERATION_FAILED;
817 }
818 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "LinkProcessStream to endpoint %{public}s failed",
819 endpoint->GetEndpointName().c_str());
820
821 ret = process->AddProcessStatusListener(endpoint);
822 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "AddProcessStatusListener failed");
823
824 std::unique_lock<std::mutex> lock(releaseEndpointMutex_);
825 if (releasingEndpointSet_.count(endpoint->GetEndpointName())) {
826 AUDIO_INFO_LOG("LinkProcessToEndpoint find endpoint is releasing, call break.");
827 releasingEndpointSet_.erase(endpoint->GetEndpointName());
828 releaseEndpointCV_.notify_all();
829 }
830 return SUCCESS;
831 }
832
UnlinkProcessToEndpoint(sptr<AudioProcessInServer> process,std::shared_ptr<AudioEndpoint> endpoint)833 int32_t AudioService::UnlinkProcessToEndpoint(sptr<AudioProcessInServer> process,
834 std::shared_ptr<AudioEndpoint> endpoint)
835 {
836 int32_t ret = endpoint->UnlinkProcessStream(process);
837 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "UnlinkProcessStream failed");
838
839 ret = process->RemoveProcessStatusListener(endpoint);
840 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "RemoveProcessStatusListener failed");
841
842 return SUCCESS;
843 }
844
DelayCallReleaseEndpoint(std::string endpointName,int32_t delayInMs)845 void AudioService::DelayCallReleaseEndpoint(std::string endpointName, int32_t delayInMs)
846 {
847 AUDIO_INFO_LOG("Delay release endpoint [%{public}s] start, delayInMs %{public}d.", endpointName.c_str(), delayInMs);
848 CHECK_AND_RETURN_LOG(endpointList_.count(endpointName),
849 "Find no such endpoint: %{public}s", endpointName.c_str());
850 std::unique_lock<std::mutex> lock(releaseEndpointMutex_);
851 if (delayInMs != 0) {
852 releaseEndpointCV_.wait_for(lock, std::chrono::milliseconds(delayInMs), [this, endpointName] {
853 if (releasingEndpointSet_.count(endpointName)) {
854 AUDIO_DEBUG_LOG("Wake up but keep release endpoint %{public}s in delay", endpointName.c_str());
855 return false;
856 }
857 AUDIO_DEBUG_LOG("Delay release endpoint break when reuse: %{public}s", endpointName.c_str());
858 return true;
859 });
860 }
861
862 if (!releasingEndpointSet_.count(endpointName)) {
863 AUDIO_DEBUG_LOG("Timeout or not need to release: %{public}s", endpointName.c_str());
864 return;
865 }
866 releasingEndpointSet_.erase(endpointName);
867
868 std::shared_ptr<AudioEndpoint> temp = nullptr;
869 CHECK_AND_RETURN_LOG(endpointList_.find(endpointName) != endpointList_.end() &&
870 endpointList_[endpointName] != nullptr, "Endpoint %{public}s not available, stop call release",
871 endpointName.c_str());
872 temp = endpointList_[endpointName];
873 if (temp->GetStatus() == AudioEndpoint::EndpointStatus::UNLINKED) {
874 AUDIO_INFO_LOG("%{public}s not in use anymore, call release!", endpointName.c_str());
875 temp->Release();
876 temp = nullptr;
877 endpointList_.erase(endpointName);
878 return;
879 }
880 AUDIO_WARNING_LOG("%{public}s is not unlinked, stop call release", endpointName.c_str());
881 return;
882 }
883
GetDeviceInfoForProcess(const AudioProcessConfig & config)884 AudioDeviceDescriptor AudioService::GetDeviceInfoForProcess(const AudioProcessConfig &config)
885 {
886 // send the config to AudioPolicyServera and get the device info.
887 AudioDeviceDescriptor deviceInfo(AudioDeviceDescriptor::DEVICE_INFO);
888 bool ret = PolicyHandler::GetInstance().GetProcessDeviceInfo(config, false, deviceInfo);
889 if (ret) {
890 AUDIO_INFO_LOG("Get DeviceInfo from policy server success, deviceType: %{public}d, "
891 "supportLowLatency: %{public}d", deviceInfo.deviceType_, deviceInfo.isLowLatencyDevice_);
892 return deviceInfo;
893 } else {
894 AUDIO_WARNING_LOG("GetProcessDeviceInfo from audio policy server failed!");
895 }
896
897 if (config.audioMode == AUDIO_MODE_RECORD) {
898 deviceInfo.deviceId_ = 1;
899 deviceInfo.networkId_ = LOCAL_NETWORK_ID;
900 deviceInfo.deviceRole_ = INPUT_DEVICE;
901 deviceInfo.deviceType_ = DEVICE_TYPE_MIC;
902 } else {
903 deviceInfo.deviceId_ = 6; // 6 for test
904 deviceInfo.networkId_ = LOCAL_NETWORK_ID;
905 deviceInfo.deviceRole_ = OUTPUT_DEVICE;
906 deviceInfo.deviceType_ = DEVICE_TYPE_SPEAKER;
907 }
908 AudioStreamInfo targetStreamInfo = {SAMPLE_RATE_48000, ENCODING_PCM, SAMPLE_S16LE, STEREO}; // note: read from xml
909 deviceInfo.audioStreamInfo_ = targetStreamInfo;
910 deviceInfo.deviceName_ = "mmap_device";
911 return deviceInfo;
912 }
913
GetAudioEndpointForDevice(AudioDeviceDescriptor & deviceInfo,const AudioProcessConfig & clientConfig,bool isVoipStream)914 std::shared_ptr<AudioEndpoint> AudioService::GetAudioEndpointForDevice(AudioDeviceDescriptor &deviceInfo,
915 const AudioProcessConfig &clientConfig, bool isVoipStream)
916 {
917 int32_t endpointSeparateFlag = -1;
918 GetSysPara("persist.multimedia.audioflag.fast.disableseparate", endpointSeparateFlag);
919 if (deviceInfo.deviceRole_ == INPUT_DEVICE || deviceInfo.networkId_ != LOCAL_NETWORK_ID ||
920 deviceInfo.deviceRole_ == OUTPUT_DEVICE || endpointSeparateFlag == 1) {
921 // Create shared stream.
922 int32_t endpointFlag = AUDIO_FLAG_MMAP;
923 if (isVoipStream) {
924 endpointFlag = AUDIO_FLAG_VOIP_FAST;
925 }
926 std::string deviceKey = AudioEndpoint::GenerateEndpointKey(deviceInfo, endpointFlag);
927 if (endpointList_.find(deviceKey) != endpointList_.end()) {
928 AUDIO_INFO_LOG("AudioService find endpoint already exist for deviceKey:%{public}s", deviceKey.c_str());
929 return endpointList_[deviceKey];
930 } else {
931 std::shared_ptr<AudioEndpoint> endpoint = AudioEndpoint::CreateEndpoint(isVoipStream ?
932 AudioEndpoint::TYPE_VOIP_MMAP : AudioEndpoint::TYPE_MMAP, endpointFlag, clientConfig, deviceInfo);
933 CHECK_AND_RETURN_RET_LOG(endpoint != nullptr, nullptr, "Create mmap AudioEndpoint failed.");
934 AUDIO_INFO_LOG("Add endpoint %{public}s to endpointList_", deviceKey.c_str());
935 endpointList_[deviceKey] = endpoint;
936 return endpoint;
937 }
938 } else {
939 // Create Independent stream.
940 std::string deviceKey = deviceInfo.networkId_ + std::to_string(deviceInfo.deviceId_) + "_" +
941 std::to_string(g_id);
942 std::shared_ptr<AudioEndpoint> endpoint = AudioEndpoint::CreateEndpoint(AudioEndpoint::TYPE_INDEPENDENT,
943 g_id, clientConfig, deviceInfo);
944 CHECK_AND_RETURN_RET_LOG(endpoint != nullptr, nullptr, "Create independent AudioEndpoint failed.");
945 g_id++;
946 AUDIO_INFO_LOG("Add endpointSeperate %{public}s to endpointList_", deviceKey.c_str());
947 endpointList_[deviceKey] = endpoint;
948 return endpoint;
949 }
950 }
951 #endif
952
NotifyStreamVolumeChanged(AudioStreamType streamType,float volume)953 int32_t AudioService::NotifyStreamVolumeChanged(AudioStreamType streamType, float volume)
954 {
955 std::lock_guard<std::mutex> lock(processListMutex_);
956 int32_t ret = SUCCESS;
957 #ifdef SUPPORT_LOW_LATENCY
958 for (auto item : endpointList_) {
959 std::string endpointName = item.second->GetEndpointName();
960 if (endpointName == item.first) {
961 ret = ret != SUCCESS ? ret : item.second->SetVolume(streamType, volume);
962 }
963 }
964 #endif
965 return ret;
966 }
967
Dump(std::string & dumpString)968 void AudioService::Dump(std::string &dumpString)
969 {
970 AUDIO_INFO_LOG("AudioService dump begin");
971 for (auto &workingConfig_ : workingConfigs_) {
972 AppendFormat(dumpString, "InnerCapid: %s - InnerCap filter: %s\n",
973 std::to_string(workingConfig_.first).c_str(),
974 ProcessConfig::DumpInnerCapConfig(workingConfig_.second).c_str());
975 }
976 #ifdef SUPPORT_LOW_LATENCY
977 // dump process
978 for (auto paired : linkedPairedList_) {
979 paired.first->Dump(dumpString);
980 }
981 // dump endpoint
982 for (auto item : endpointList_) {
983 AppendFormat(dumpString, " - Endpoint device id: %s\n", item.first.c_str());
984 item.second->Dump(dumpString);
985 }
986 #endif
987 // dump voip and direct
988 {
989 std::lock_guard<std::mutex> lock(rendererMapMutex_);
990 for (const auto &item : allRendererMap_) {
991 std::shared_ptr<RendererInServer> renderer = item.second.lock();
992 if (renderer) {
993 renderer->Dump(dumpString);
994 }
995 }
996 }
997
998 // dump appUseNumMap_ and currentRendererStreamCnt_
999 {
1000 std::lock_guard<std::mutex> lock(streamLifeCycleMutex_);
1001 AppendFormat(dumpString, " - currentRendererStreamCnt is %d\n", currentRendererStreamCnt_);
1002 for (auto it : appUseNumMap_) {
1003 AppendFormat(dumpString, " - appUseNumMap_ appUid: %d\n", it.first);
1004 AppendFormat(dumpString, " - appUseNumMap_ appUid created stream: %d\n", it.second);
1005 }
1006 }
1007 PolicyHandler::GetInstance().Dump(dumpString);
1008 AudioVolume::GetInstance()->Dump(dumpString);
1009 }
1010
GetMaxAmplitude(bool isOutputDevice)1011 float AudioService::GetMaxAmplitude(bool isOutputDevice)
1012 {
1013 #ifdef SUPPORT_LOW_LATENCY
1014 std::lock_guard<std::mutex> lock(processListMutex_);
1015 if (linkedPairedList_.size() == 0) {
1016 return 0;
1017 }
1018
1019 float fastAudioMaxAmplitude = 0;
1020 for (auto paired : linkedPairedList_) {
1021 if (isOutputDevice && (paired.second->GetDeviceRole() == OUTPUT_DEVICE)) {
1022 float curFastAudioMaxAmplitude = paired.second->GetMaxAmplitude();
1023 if (curFastAudioMaxAmplitude > fastAudioMaxAmplitude) {
1024 fastAudioMaxAmplitude = curFastAudioMaxAmplitude;
1025 }
1026 }
1027 if (!isOutputDevice && (paired.second->GetDeviceRole() == INPUT_DEVICE)) {
1028 float curFastAudioMaxAmplitude = paired.second->GetMaxAmplitude();
1029 if (curFastAudioMaxAmplitude > fastAudioMaxAmplitude) {
1030 fastAudioMaxAmplitude = curFastAudioMaxAmplitude;
1031 }
1032 }
1033 }
1034 return fastAudioMaxAmplitude;
1035 #else
1036 return 0;
1037 #endif
1038 }
1039
GetRendererBySessionID(const uint32_t & sessionID)1040 std::shared_ptr<RendererInServer> AudioService::GetRendererBySessionID(const uint32_t &sessionID)
1041 {
1042 std::lock_guard<std::mutex> lock(rendererMapMutex_);
1043 if (allRendererMap_.count(sessionID)) {
1044 return allRendererMap_[sessionID].lock();
1045 } else {
1046 return nullptr;
1047 }
1048 }
1049
GetCapturerBySessionID(const uint32_t & sessionID)1050 std::shared_ptr<CapturerInServer> AudioService::GetCapturerBySessionID(const uint32_t &sessionID)
1051 {
1052 if (allCapturerMap_.count(sessionID)) {
1053 return allCapturerMap_[sessionID].lock();
1054 } else {
1055 return std::shared_ptr<CapturerInServer>();
1056 }
1057 }
1058
SetNonInterruptMute(const uint32_t sessionId,const bool muteFlag)1059 void AudioService::SetNonInterruptMute(const uint32_t sessionId, const bool muteFlag)
1060 {
1061 AUDIO_INFO_LOG("SessionId: %{public}u, muteFlag: %{public}d", sessionId, muteFlag);
1062 std::unique_lock<std::mutex> rendererLock(rendererMapMutex_);
1063 if (allRendererMap_.count(sessionId)) {
1064 std::shared_ptr<RendererInServer> renderer = allRendererMap_[sessionId].lock();
1065 if (renderer == nullptr) {
1066 AUDIO_ERR_LOG("rendererinserver is null");
1067 rendererLock.unlock();
1068 return;
1069 }
1070 renderer->SetNonInterruptMute(muteFlag);
1071 AUDIO_INFO_LOG("allRendererMap_ has sessionId");
1072 rendererLock.unlock();
1073 return;
1074 }
1075 rendererLock.unlock();
1076 std::unique_lock<std::mutex> capturerLock(capturerMapMutex_);
1077 if (allCapturerMap_.count(sessionId)) {
1078 std::shared_ptr<CapturerInServer> capturer = allCapturerMap_[sessionId].lock();
1079 if (capturer == nullptr) {
1080 AUDIO_ERR_LOG("capturerinserver is null");
1081 return;
1082 }
1083 capturer->SetNonInterruptMute(muteFlag);
1084 AUDIO_INFO_LOG("allCapturerMap_ has sessionId");
1085 return;
1086 }
1087 capturerLock.unlock();
1088 SetNonInterruptMuteForProcess(sessionId, muteFlag);
1089 }
1090
SetNonInterruptMuteForProcess(const uint32_t sessionId,const bool muteFlag)1091 void AudioService::SetNonInterruptMuteForProcess(const uint32_t sessionId, const bool muteFlag)
1092 {
1093 #ifdef SUPPORT_LOW_LATENCY
1094 std::unique_lock<std::mutex> processListLock(processListMutex_);
1095 for (auto paired : linkedPairedList_) {
1096 if (paired.first == nullptr) {
1097 AUDIO_ERR_LOG("processInServer is nullptr");
1098 return;
1099 }
1100 if (paired.first->GetSessionId() == sessionId) {
1101 AUDIO_INFO_LOG("linkedPairedList_ has sessionId");
1102 paired.first->SetNonInterruptMute(muteFlag);
1103 return;
1104 }
1105 }
1106 processListLock.unlock();
1107 #endif
1108 AUDIO_INFO_LOG("Cannot find sessionId");
1109 // when old stream already released and new stream not create yet
1110 // set muteflag 0 but cannot find sessionId in allRendererMap_, allCapturerMap_ and linkedPairedList_
1111 // need erase it from mutedSessions_ to avoid new stream cannot be set unmute
1112 if (mutedSessions_.count(sessionId) && !muteFlag) {
1113 mutedSessions_.erase(sessionId);
1114 }
1115 // when old stream already released and new stream not create yet
1116 // set muteflag 1 but cannot find sessionId in allRendererMap_, allCapturerMap_ and linkedPairedList_
1117 // this sessionid will not add into mutedSessions_
1118 // so need save it temporarily, when new stream create, check if new stream need mute
1119 // if set muteflag 0 again before new stream create, do not mute it
1120 std::lock_guard<std::mutex> muteSwitchStreamLock(muteSwitchStreamSetMutex_);
1121 if (muteFlag) {
1122 muteSwitchStreams_.insert(sessionId);
1123 AUDIO_INFO_LOG("Insert into muteSwitchStreams_");
1124 } else if (muteSwitchStreams_.count(sessionId)) {
1125 muteSwitchStreams_.erase(sessionId);
1126 }
1127 }
1128
SetOffloadMode(uint32_t sessionId,int32_t state,bool isAppBack)1129 int32_t AudioService::SetOffloadMode(uint32_t sessionId, int32_t state, bool isAppBack)
1130 {
1131 std::unique_lock<std::mutex> lock(rendererMapMutex_);
1132 if (!allRendererMap_.count(sessionId)) {
1133 AUDIO_WARNING_LOG("Renderer %{public}u is not in map", sessionId);
1134 return ERR_INVALID_INDEX;
1135 }
1136 AUDIO_INFO_LOG("Set offload mode for renderer %{public}u", sessionId);
1137 std::shared_ptr<RendererInServer> renderer = allRendererMap_[sessionId].lock();
1138 if (renderer == nullptr) {
1139 AUDIO_WARNING_LOG("RendererInServer is nullptr");
1140 lock.unlock();
1141 return ERROR;
1142 }
1143 int32_t ret = renderer->SetOffloadMode(state, isAppBack);
1144 lock.unlock();
1145 return ret;
1146 }
1147
UnsetOffloadMode(uint32_t sessionId)1148 int32_t AudioService::UnsetOffloadMode(uint32_t sessionId)
1149 {
1150 std::unique_lock<std::mutex> lock(rendererMapMutex_);
1151 if (!allRendererMap_.count(sessionId)) {
1152 AUDIO_WARNING_LOG("Renderer %{public}u is not in map", sessionId);
1153 return ERR_INVALID_INDEX;
1154 }
1155 AUDIO_INFO_LOG("Set offload mode for renderer %{public}u", sessionId);
1156 std::shared_ptr<RendererInServer> renderer = allRendererMap_[sessionId].lock();
1157 if (renderer == nullptr) {
1158 AUDIO_WARNING_LOG("RendererInServer is nullptr");
1159 lock.unlock();
1160 return ERROR;
1161 }
1162 int32_t ret = renderer->UnsetOffloadMode();
1163 lock.unlock();
1164 return ret;
1165 }
1166
UpdateAudioSinkState(uint32_t sinkId,bool started)1167 void AudioService::UpdateAudioSinkState(uint32_t sinkId, bool started)
1168 {
1169 std::unique_lock<std::mutex> lock(allRunningSinksMutex_);
1170 if (started) {
1171 CHECK_AND_RETURN_LOG(allRunningSinks_.find(sinkId) == allRunningSinks_.end(),
1172 "Sink %{public}u already started", sinkId);
1173 allRunningSinks_.insert(sinkId);
1174 AUDIO_INFO_LOG("Sink %{public}u started", sinkId);
1175 } else {
1176 CHECK_AND_RETURN_LOG(allRunningSinks_.find(sinkId) != allRunningSinks_.end(),
1177 "Sink %{public}u already stopped or not started", sinkId);
1178 allRunningSinks_.erase(sinkId);
1179 AUDIO_INFO_LOG("Sink %{public}u stopped", sinkId);
1180 if (allRunningSinks_.empty()) {
1181 allRunningSinksCV_.notify_all();
1182 AUDIO_INFO_LOG("All sinks stop, continue to hibernate");
1183 }
1184 }
1185 return;
1186 }
1187
CheckHibernateState(bool onHibernate)1188 void AudioService::CheckHibernateState(bool onHibernate)
1189 {
1190 std::unique_lock<std::mutex> lock(allRunningSinksMutex_);
1191 onHibernate_ = onHibernate;
1192 if (onHibernate) {
1193 bool ret = true;
1194 if (allRunningSinks_.empty()) {
1195 AUDIO_INFO_LOG("No running sinks, continue to hibernate");
1196 return;
1197 }
1198 AUDIO_INFO_LOG("Wait for all sinks to stop");
1199 ret = allRunningSinksCV_.wait_for(lock, std::chrono::milliseconds(BLOCK_HIBERNATE_CALLBACK_IN_MS),
1200 [this] {return (allRunningSinks_.empty() || !onHibernate_);});
1201 if (!ret) {
1202 AUDIO_ERR_LOG("On hibernate timeout, some sinks still running");
1203 }
1204 return;
1205 } else {
1206 allRunningSinksCV_.notify_all();
1207 AUDIO_INFO_LOG("Wake up from hibernate");
1208 }
1209 }
1210
GetHibernateState()1211 bool AudioService::GetHibernateState()
1212 {
1213 std::unique_lock<std::mutex> lock(allRunningSinksMutex_);
1214 return onHibernate_;
1215 }
1216
UpdateSourceType(SourceType sourceType)1217 int32_t AudioService::UpdateSourceType(SourceType sourceType)
1218 {
1219 // specialSourceType need not updateaudioroute
1220 if (specialSourceTypeSet_.count(sourceType) != 0) {
1221 return SUCCESS;
1222 }
1223
1224 uint32_t id = HdiAdapterManager::GetInstance().GetId(HDI_ID_BASE_CAPTURE, HDI_ID_TYPE_PRIMARY);
1225 std::shared_ptr<IAudioCaptureSource> source = HdiAdapterManager::GetInstance().GetCaptureSource(id);
1226 CHECK_AND_RETURN_RET_LOG(source != nullptr, ERROR, "source is null");
1227
1228 return source->UpdateSourceType(sourceType);
1229 }
1230
SetIncMaxRendererStreamCnt(AudioMode audioMode)1231 void AudioService::SetIncMaxRendererStreamCnt(AudioMode audioMode)
1232 {
1233 if (audioMode == AUDIO_MODE_PLAYBACK) {
1234 currentRendererStreamCnt_++;
1235 }
1236 }
1237
SetDecMaxRendererStreamCnt()1238 void AudioService::SetDecMaxRendererStreamCnt()
1239 {
1240 std::lock_guard<std::mutex> lock(streamLifeCycleMutex_);
1241 currentRendererStreamCnt_--;
1242 }
1243
CleanAppUseNumMap(int32_t appUid)1244 void AudioService::CleanAppUseNumMap(int32_t appUid)
1245 {
1246 std::lock_guard<std::mutex> lock(streamLifeCycleMutex_);
1247 auto appUseNum = appUseNumMap_.find(appUid);
1248 if (appUseNum != appUseNumMap_.end()) {
1249 appUseNumMap_[appUid] = --appUseNum->second;
1250 }
1251 }
1252
HasBluetoothEndpoint()1253 bool AudioService::HasBluetoothEndpoint()
1254 {
1255 #ifdef SUPPORT_LOW_LATENCY
1256 std::lock_guard<std::mutex> lock(processListMutex_);
1257 return std::any_of(linkedPairedList_.begin(), linkedPairedList_.end(),
1258 [](const auto & linkPair) {
1259 return linkPair.second->GetDeviceInfo().getType() == DEVICE_TYPE_BLUETOOTH_A2DP;
1260 });
1261 #else
1262 return true;
1263 #endif
1264 }
1265
GetCurrentRendererStreamCnt()1266 int32_t AudioService::GetCurrentRendererStreamCnt()
1267 {
1268 return currentRendererStreamCnt_;
1269 }
1270
GetAllSinkInputs(std::vector<SinkInput> & sinkInputs)1271 void AudioService::GetAllSinkInputs(std::vector<SinkInput> &sinkInputs)
1272 {
1273 IStreamManager::GetPlaybackManager(PLAYBACK).GetAllSinkInputs(sinkInputs);
1274 }
1275
SetDefaultAdapterEnable(bool isEnable)1276 void AudioService::SetDefaultAdapterEnable(bool isEnable)
1277 {
1278 isDefaultAdapterEnable_ = isEnable;
1279 }
1280
GetDefaultAdapterEnable()1281 bool AudioService::GetDefaultAdapterEnable()
1282 {
1283 return isDefaultAdapterEnable_;
1284 }
1285
1286 // need call with streamLifeCycleMutex_ lock
IsExceedingMaxStreamCntPerUid(int32_t callingUid,int32_t appUid,int32_t maxStreamCntPerUid)1287 bool AudioService::IsExceedingMaxStreamCntPerUid(int32_t callingUid, int32_t appUid,
1288 int32_t maxStreamCntPerUid)
1289 {
1290 if (callingUid != MEDIA_SERVICE_UID) {
1291 appUid = callingUid;
1292 }
1293
1294 auto appUseNum = appUseNumMap_.find(appUid);
1295 if (appUseNum != appUseNumMap_.end()) {
1296 ++appUseNum->second;
1297 } else {
1298 int32_t initValue = 1;
1299 appUseNumMap_.emplace(appUid, initValue);
1300 }
1301
1302 if (appUseNumMap_[appUid] >= RENDERER_STREAM_CNT_PER_UID_LIMIT) {
1303 int32_t mostAppUid = INVALID_APP_UID;
1304 int32_t mostAppNum = INVALID_APP_CREATED_AUDIO_STREAM_NUM;
1305 GetCreatedAudioStreamMostUid(mostAppUid, mostAppNum);
1306 std::shared_ptr<Media::MediaMonitor::EventBean> bean = std::make_shared<Media::MediaMonitor::EventBean>(
1307 Media::MediaMonitor::ModuleId::AUDIO, Media::MediaMonitor::EventId::AUDIO_STREAM_EXHAUSTED_STATS,
1308 Media::MediaMonitor::EventType::FREQUENCY_AGGREGATION_EVENT);
1309 bean->Add("CLIENT_UID", mostAppUid);
1310 bean->Add("TIMES", mostAppNum);
1311 Media::MediaMonitor::MediaMonitorManager::GetInstance().WriteLogMsg(bean);
1312 AUDIO_WARNING_LOG("Current audio renderer stream num is greater than the renderer stream num limit per uid");
1313 }
1314
1315 if (appUseNumMap_[appUid] > maxStreamCntPerUid) {
1316 --appUseNumMap_[appUid]; // actual created stream num is stream num decrease one
1317 return true;
1318 }
1319 return false;
1320 }
1321
GetCreatedAudioStreamMostUid(int32_t & mostAppUid,int32_t & mostAppNum)1322 void AudioService::GetCreatedAudioStreamMostUid(int32_t &mostAppUid, int32_t &mostAppNum)
1323 {
1324 for (auto it = appUseNumMap_.begin(); it != appUseNumMap_.end(); it++) {
1325 if (it->second > mostAppNum) {
1326 mostAppNum = it->second;
1327 mostAppUid = it->first;
1328 }
1329 }
1330 return;
1331 }
1332
1333 #ifdef HAS_FEATURE_INNERCAPTURER
UnloadModernInnerCapSink(int32_t innerCapId)1334 int32_t AudioService::UnloadModernInnerCapSink(int32_t innerCapId)
1335 {
1336 return PolicyHandler::GetInstance().UnloadModernInnerCapSink(innerCapId);
1337 }
1338 #endif
1339
RestoreSession(uint32_t sessionId,RestoreInfo restoreInfo)1340 RestoreStatus AudioService::RestoreSession(uint32_t sessionId, RestoreInfo restoreInfo)
1341 {
1342 {
1343 std::lock_guard<std::mutex> lock(rendererMapMutex_);
1344 if (allRendererMap_.find(sessionId) != allRendererMap_.end()) {
1345 std::shared_ptr<RendererInServer> rendererInServer = allRendererMap_[sessionId].lock();
1346 CHECK_AND_RETURN_RET_LOG(rendererInServer != nullptr, RESTORE_ERROR,
1347 "Session could be released, restore failed");
1348 return rendererInServer->RestoreSession(restoreInfo);
1349 }
1350 }
1351 {
1352 std::lock_guard<std::mutex> lock(capturerMapMutex_);
1353 if (allCapturerMap_.find(sessionId) != allCapturerMap_.end()) {
1354 std::shared_ptr<CapturerInServer> capturerInServer = allCapturerMap_[sessionId].lock();
1355 CHECK_AND_RETURN_RET_LOG(capturerInServer != nullptr, RESTORE_ERROR,
1356 "Session could be released, restore failed");
1357 return capturerInServer->RestoreSession(restoreInfo);
1358 }
1359 }
1360 #ifdef SUPPORT_LOW_LATENCY
1361 {
1362 std::lock_guard<std::mutex> lock(processListMutex_);
1363 for (auto processEndpointPair : linkedPairedList_) {
1364 if (processEndpointPair.first->GetSessionId() != sessionId) {
1365 continue;
1366 }
1367 auto audioProcessInServer = processEndpointPair.first;
1368 CHECK_AND_RETURN_RET_LOG(audioProcessInServer != nullptr, RESTORE_ERROR,
1369 "Session could be released, restore failed");
1370 return audioProcessInServer->RestoreSession(restoreInfo);
1371 }
1372 }
1373 #endif
1374 AUDIO_WARNING_LOG("Session not exists, restore failed");
1375 return RESTORE_ERROR;
1376 }
1377 } // namespace AudioStandard
1378 } // namespace OHOS
1379