• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "AudioCapturerSession"
17 #endif
18 
19 #include "audio_capturer_session.h"
20 #include <ability_manager_client.h>
21 #include "iservice_registry.h"
22 #include "parameter.h"
23 #include "parameters.h"
24 #include "audio_policy_log.h"
25 
26 #include "audio_policy_utils.h"
27 
28 namespace OHOS {
29 namespace AudioStandard {
30 const uint32_t PCM_8_BIT = 8;
31 const float RENDER_FRAME_INTERVAL_IN_SECONDS = 0.02;
32 static const std::string PIPE_PRIMARY_INPUT = "primary_input";
33 static const std::string PIPE_WAKEUP_INPUT = "wakeup_input";
34 
35 static inline const std::unordered_set<SourceType> specialSourceTypeSet_ = {
36     SOURCE_TYPE_PLAYBACK_CAPTURE,
37     SOURCE_TYPE_WAKEUP,
38     SOURCE_TYPE_VIRTUAL_CAPTURE,
39     SOURCE_TYPE_REMOTE_CAST
40 };
41 
42 static std::map<SourceType, int> NORMAL_SOURCE_PRIORITY = {
43     // from high to low
44     {SOURCE_TYPE_VOICE_CALL, 7},
45     {SOURCE_TYPE_VOICE_COMMUNICATION, 6},
46     {SOURCE_TYPE_VOICE_TRANSCRIPTION, 5},
47     {SOURCE_TYPE_VOICE_RECOGNITION, 2},
48     {SOURCE_TYPE_CAMCORDER, 2},
49     {SOURCE_TYPE_MIC, 2},
50     {SOURCE_TYPE_UNPROCESSED, 1},
51 };
52 
IsHigherPrioritySource(SourceType newSource,SourceType currentSource)53 static bool IsHigherPrioritySource(SourceType newSource, SourceType currentSource)
54 {
55     AUDIO_INFO_LOG("newSource sourceType:%{public}d priority:%{public}d,"
56         "currentSource sourceType:%{public}d priority:%{public}d",
57         newSource, NORMAL_SOURCE_PRIORITY[newSource], currentSource, NORMAL_SOURCE_PRIORITY[currentSource]);
58     if (NORMAL_SOURCE_PRIORITY.count(newSource) == 0 ||
59         NORMAL_SOURCE_PRIORITY.count(currentSource) == 0 ||
60         (newSource == currentSource)) {
61         return false;
62     }
63     return NORMAL_SOURCE_PRIORITY[newSource] >= NORMAL_SOURCE_PRIORITY[currentSource];
64 }
65 
Init(std::shared_ptr<AudioA2dpOffloadManager> audioA2dpOffloadManager)66 void AudioCapturerSession::Init(std::shared_ptr<AudioA2dpOffloadManager> audioA2dpOffloadManager)
67 {
68     audioA2dpOffloadManager_ = audioA2dpOffloadManager;
69 }
70 
DeInit()71 void AudioCapturerSession::DeInit()
72 {
73     audioA2dpOffloadManager_ = nullptr;
74 }
75 
SetConfigParserFlag()76 void AudioCapturerSession::SetConfigParserFlag()
77 {
78     isPolicyConfigParsered_ = true;
79 }
80 
LoadInnerCapturerSink(std::string moduleName,AudioStreamInfo streamInfo)81 void AudioCapturerSession::LoadInnerCapturerSink(std::string moduleName, AudioStreamInfo streamInfo)
82 {
83 #ifdef HAS_FEATURE_INNERCAPTURER
84     AUDIO_INFO_LOG("Start");
85     uint32_t bufferSize = streamInfo.samplingRate *
86         AudioPolicyUtils::GetInstance().PcmFormatToBytes(streamInfo.format) *
87         streamInfo.channels * RENDER_FRAME_INTERVAL_IN_SECONDS;
88 
89     AudioModuleInfo moduleInfo = {};
90     moduleInfo.lib = "libmodule-inner-capturer-sink.z.so";
91     moduleInfo.format = AudioPolicyUtils::GetInstance().ConvertToHDIAudioFormat(streamInfo.format);
92     moduleInfo.name = moduleName;
93     moduleInfo.networkId = "LocalDevice";
94     moduleInfo.channels = std::to_string(streamInfo.channels);
95     moduleInfo.rate = std::to_string(streamInfo.samplingRate);
96     moduleInfo.bufferSize = std::to_string(bufferSize);
97 
98     audioIOHandleMap_.OpenPortAndInsertIOHandle(moduleInfo.name, moduleInfo);
99 #endif
100 }
101 
UnloadInnerCapturerSink(std::string moduleName)102 void AudioCapturerSession::UnloadInnerCapturerSink(std::string moduleName)
103 {
104 #ifdef HAS_FEATURE_INNERCAPTURER
105     audioIOHandleMap_.ClosePortAndEraseIOHandle(moduleName);
106 #endif
107 }
108 
HandleRemoteCastDevice(bool isConnected,AudioStreamInfo streamInfo)109 void AudioCapturerSession::HandleRemoteCastDevice(bool isConnected, AudioStreamInfo streamInfo)
110 {
111 #ifdef HAS_FEATURE_INNERCAPTURER
112     AudioDeviceDescriptor updatedDesc = AudioDeviceDescriptor(DEVICE_TYPE_REMOTE_CAST,
113         AudioPolicyUtils::GetInstance().GetDeviceRole(DEVICE_TYPE_REMOTE_CAST));
114     std::vector<std::shared_ptr<AudioDeviceDescriptor>> descForCb = {};
115     if (isConnected) {
116         // If device already in list, remove it else do not modify the list
117         audioConnectedDevice_.DelConnectedDevice(updatedDesc.networkId_, updatedDesc.deviceType_,
118             updatedDesc.macAddress_);
119         audioDeviceCommon_.UpdateConnectedDevicesWhenConnecting(updatedDesc, descForCb);
120         LoadInnerCapturerSink(REMOTE_CAST_INNER_CAPTURER_SINK_NAME, streamInfo);
121         audioPolicyManager_.ResetRemoteCastDeviceVolume();
122     } else {
123         audioDeviceCommon_.UpdateConnectedDevicesWhenDisconnecting(updatedDesc, descForCb);
124         audioDeviceCommon_.FetchDevice(true, AudioStreamDeviceChangeReasonExt::ExtEnum::OLD_DEVICE_UNAVALIABLE_EXT);
125         UnloadInnerCapturerSink(REMOTE_CAST_INNER_CAPTURER_SINK_NAME);
126     }
127     audioDeviceCommon_.FetchDevice(true);
128     audioDeviceCommon_.FetchDevice(false);
129 
130     // update a2dp offload
131     if (audioA2dpOffloadManager_) {
132         audioA2dpOffloadManager_->UpdateA2dpOffloadFlagForAllStream();
133     }
134 #endif
135 }
136 
OnCapturerSessionAdded(uint64_t sessionID,SessionInfo sessionInfo,AudioStreamInfo streamInfo)137 int32_t AudioCapturerSession::OnCapturerSessionAdded(uint64_t sessionID, SessionInfo sessionInfo,
138     AudioStreamInfo streamInfo)
139 {
140     std::lock_guard<std::mutex> lock(onCapturerSessionChangedMutex_);
141     AUDIO_INFO_LOG("sessionID: %{public}" PRIu64 " source: %{public}d", sessionID, sessionInfo.sourceType);
142     CHECK_AND_RETURN_RET_LOG(isPolicyConfigParsered_ && audioVolumeManager_.GetLoadFlag(), ERROR,
143         "policyConfig not loaded");
144 
145     if (sessionIdisRemovedSet_.count(sessionID) > 0) {
146         sessionIdisRemovedSet_.erase(sessionID);
147         AUDIO_INFO_LOG("sessionID: %{public}" PRIu64 " had already been removed earlier", sessionID);
148         return SUCCESS;
149     }
150     if (specialSourceTypeSet_.count(sessionInfo.sourceType) == 0) {
151         // normal source types, dynamic open
152         StreamPropInfo targetInfo;
153         SourceType targetSource;
154         int32_t res = audioEcManager_.FetchTargetInfoForSessionAdd(sessionInfo, targetInfo, targetSource);
155         CHECK_AND_RETURN_RET_LOG(res == SUCCESS, res,
156             "fetch target source info error");
157 
158         if (audioEcManager_.GetSourceOpened() == SOURCE_TYPE_INVALID) {
159             // normal source is not opened before
160             audioEcManager_.PrepareAndOpenNormalSource(sessionInfo, targetInfo, targetSource);
161             sessionIdUsedToOpenSource_ = sessionID;
162         } else if (IsHigherPrioritySource(targetSource, audioEcManager_.GetSourceOpened())) {
163             // reload if higher source come
164             audioEcManager_.CloseNormalSource();
165             audioEcManager_.PrepareAndOpenNormalSource(sessionInfo, targetInfo, targetSource);
166             sessionIdUsedToOpenSource_ = sessionID;
167         }
168         sessionWithNormalSourceType_[sessionID] = sessionInfo;
169     } else if (sessionInfo.sourceType == SOURCE_TYPE_REMOTE_CAST) {
170         HandleRemoteCastDevice(true, streamInfo);
171         sessionWithSpecialSourceType_[sessionID] = sessionInfo;
172     } else {
173         sessionWithSpecialSourceType_[sessionID] = sessionInfo;
174     }
175     return SUCCESS;
176 }
177 
OnCapturerSessionRemoved(uint64_t sessionID)178 void AudioCapturerSession::OnCapturerSessionRemoved(uint64_t sessionID)
179 {
180     std::lock_guard<std::mutex> lock(onCapturerSessionChangedMutex_);
181     AUDIO_INFO_LOG("sessionid:%{public}" PRIu64, sessionID);
182     if (sessionWithSpecialSourceType_.count(sessionID) > 0) {
183         if (sessionWithSpecialSourceType_[sessionID].sourceType == SOURCE_TYPE_REMOTE_CAST) {
184             HandleRemoteCastDevice(false);
185         }
186         sessionWithSpecialSourceType_.erase(sessionID);
187         return;
188     }
189 
190     if (sessionWithNormalSourceType_.count(sessionID) > 0) {
191         if (sessionWithNormalSourceType_[sessionID].sourceType == SOURCE_TYPE_VOICE_COMMUNICATION) {
192             audioEcManager_.ResetAudioEcInfo();
193         }
194         sessionWithNormalSourceType_.erase(sessionID);
195         if (!sessionWithNormalSourceType_.empty()) {
196             HandleRemainingSource();
197             return;
198         }
199         // close source when all capturer sessions removed
200         audioEcManager_.CloseNormalSource();
201         return;
202     }
203 
204     AUDIO_INFO_LOG("Sessionid:%{public}" PRIu64 " not added, directly placed into sessionIdisRemovedSet_", sessionID);
205     sessionIdisRemovedSet_.insert(sessionID);
206 }
207 
208 // HandleRemainingSource must be called with onCapturerSessionChangedMutex_ held
HandleRemainingSource()209 void AudioCapturerSession::HandleRemainingSource()
210 {
211     SourceType highestSource = SOURCE_TYPE_INVALID;
212     uint32_t highestSession = 0;
213     // find highest source in remaining session
214     for (const auto &iter : sessionWithNormalSourceType_) {
215         if (IsHigherPrioritySource(iter.second.sourceType, highestSource)) {
216             highestSession = iter.first;
217             highestSource = iter.second.sourceType;
218         }
219     }
220 
221     // Convert highestSource to highestSourceInHdi
222     SourceType highestSourceInHdi = SOURCE_TYPE_INVALID;
223     bool useMatchingPropInfo = false;
224     audioEcManager_.GetTargetSourceTypeAndMatchingFlag(highestSource, highestSourceInHdi, useMatchingPropInfo);
225 
226     // if remaining sources are all lower than current removeed one, reload with the highest source in remaining
227     if (highestSource != SOURCE_TYPE_INVALID && IsHigherPrioritySource(audioEcManager_.GetSourceOpened(),
228         highestSourceInHdi)) {
229         AUDIO_INFO_LOG("reload source %{pblic}d because higher source removed, normalSourceOpened:%{public}d, "
230             "highestSourceInHdi:%{public}d ", highestSource, audioEcManager_.GetSourceOpened(), highestSourceInHdi);
231         audioEcManager_.ReloadSourceForSession(sessionWithNormalSourceType_[highestSession]);
232         sessionIdUsedToOpenSource_ = highestSession;
233     }
234 }
235 
ConstructWakeupAudioModuleInfo(const AudioStreamInfo & streamInfo,AudioModuleInfo & audioModuleInfo)236 bool AudioCapturerSession::ConstructWakeupAudioModuleInfo(const AudioStreamInfo &streamInfo,
237     AudioModuleInfo &audioModuleInfo)
238 {
239     if (!audioConfigManager_.GetAdapterInfoFlag()) {
240         return false;
241     }
242 
243     AudioAdapterInfo info;
244     AdaptersType type = static_cast<AdaptersType>(AudioPolicyUtils::portStrToEnum[std::string(PRIMARY_WAKEUP)]);
245     bool ret = audioConfigManager_.GetAdapterInfoByType(type, info);
246     if (!ret) {
247         AUDIO_ERR_LOG("can not find adapter info");
248         return false;
249     }
250 
251     auto pipeInfo = info.GetPipeByName(PIPE_WAKEUP_INPUT);
252     if (pipeInfo == nullptr) {
253         AUDIO_ERR_LOG("wakeup pipe info is nullptr");
254         return false;
255     }
256 
257     if (!FillWakeupStreamPropInfo(streamInfo, pipeInfo, audioModuleInfo)) {
258         AUDIO_ERR_LOG("failed to fill pipe stream prop info");
259         return false;
260     }
261 
262     audioModuleInfo.adapterName = info.adapterName_;
263     audioModuleInfo.name = pipeInfo->moduleName_;
264     audioModuleInfo.lib = pipeInfo->lib_;
265     audioModuleInfo.networkId = "LocalDevice";
266     audioModuleInfo.className = "primary";
267     audioModuleInfo.fileName = "";
268     audioModuleInfo.OpenMicSpeaker = "1";
269     audioModuleInfo.sourceType = std::to_string(SourceType::SOURCE_TYPE_WAKEUP);
270 
271     AUDIO_INFO_LOG("wakeup auido module info, adapter name:%{public}s, name:%{public}s, lib:%{public}s",
272         audioModuleInfo.adapterName.c_str(), audioModuleInfo.name.c_str(), audioModuleInfo.lib.c_str());
273     return true;
274 }
275 
SetWakeUpAudioCapturer(InternalAudioCapturerOptions options)276 int32_t AudioCapturerSession::SetWakeUpAudioCapturer(InternalAudioCapturerOptions options)
277 {
278     AUDIO_INFO_LOG("set wakeup audio capturer start");
279     AudioModuleInfo moduleInfo = {};
280     if (!ConstructWakeupAudioModuleInfo(options.streamInfo, moduleInfo)) {
281         AUDIO_ERR_LOG("failed to construct wakeup audio module info");
282         return ERROR;
283     }
284     audioIOHandleMap_.OpenPortAndInsertIOHandle(moduleInfo.name, moduleInfo);
285 
286     AUDIO_DEBUG_LOG("set wakeup audio capturer end");
287     return SUCCESS;
288 }
289 
SetWakeUpAudioCapturerFromAudioServer(const AudioProcessConfig & config)290 int32_t AudioCapturerSession::SetWakeUpAudioCapturerFromAudioServer(const AudioProcessConfig &config)
291 {
292     InternalAudioCapturerOptions capturerOptions;
293     capturerOptions.streamInfo = config.streamInfo;
294     return SetWakeUpAudioCapturer(capturerOptions);
295 }
296 
CloseWakeUpAudioCapturer()297 int32_t AudioCapturerSession::CloseWakeUpAudioCapturer()
298 {
299     AUDIO_INFO_LOG("close wakeup audio capturer start");
300     return audioIOHandleMap_.ClosePortAndEraseIOHandle(std::string(PRIMARY_WAKEUP));
301 }
302 
303 // private method
FillWakeupStreamPropInfo(const AudioStreamInfo & streamInfo,PipeInfo * pipeInfo,AudioModuleInfo & audioModuleInfo)304 bool AudioCapturerSession::FillWakeupStreamPropInfo(const AudioStreamInfo &streamInfo, PipeInfo *pipeInfo,
305     AudioModuleInfo &audioModuleInfo)
306 {
307     if (pipeInfo == nullptr) {
308         AUDIO_ERR_LOG("wakeup pipe info is nullptr");
309         return false;
310     }
311 
312     if (pipeInfo->streamPropInfos_.size() == 0) {
313         AUDIO_ERR_LOG("no stream prop info");
314         return false;
315     }
316 
317     auto targetIt = pipeInfo->streamPropInfos_.begin();
318     for (auto it = pipeInfo->streamPropInfos_.begin(); it != pipeInfo->streamPropInfos_.end(); ++it) {
319         if (it -> channelLayout_ == static_cast<uint32_t>(streamInfo.channels)) {
320             targetIt = it;
321             break;
322         }
323     }
324 
325     audioModuleInfo.format = targetIt->format_;
326     audioModuleInfo.channels = std::to_string(targetIt->channelLayout_);
327     audioModuleInfo.rate = std::to_string(targetIt->sampleRate_);
328     audioModuleInfo.bufferSize =  std::to_string(targetIt->bufferSize_);
329 
330     AUDIO_INFO_LOG("stream prop info, format:%{public}s, channels:%{public}s, rate:%{public}s, buffer size:%{public}s",
331         audioModuleInfo.format.c_str(), audioModuleInfo.channels.c_str(),
332         audioModuleInfo.rate.c_str(), audioModuleInfo.bufferSize.c_str());
333     return true;
334 }
335 
IsVoipDeviceChanged(const AudioDeviceDescriptor & inputDevice,const AudioDeviceDescriptor & outputDevice)336 bool AudioCapturerSession::IsVoipDeviceChanged(const AudioDeviceDescriptor &inputDevice,
337     const AudioDeviceDescriptor &outputDevice)
338 {
339     AudioDeviceDescriptor realInputDevice = inputDevice;
340     AudioDeviceDescriptor realOutputDevice = outputDevice;
341     shared_ptr<AudioDeviceDescriptor> inputDesc =
342         audioRouterCenter_.FetchInputDevice(SOURCE_TYPE_VOICE_COMMUNICATION, -1);
343     if (inputDesc != nullptr) {
344         realInputDevice = *inputDesc;
345     }
346     vector<std::shared_ptr<AudioDeviceDescriptor>> outputDesc =
347         audioRouterCenter_.FetchOutputDevices(STREAM_USAGE_VOICE_COMMUNICATION, -1);
348     if (outputDesc.size() > 0 && outputDesc.front() != nullptr) {
349         realOutputDevice = *outputDesc.front();
350     }
351     AudioEcInfo lastEcInfo = audioEcManager_.GetAudioEcInfo();
352     if (!lastEcInfo.inputDevice.IsSameDeviceDesc(realInputDevice) ||
353         !lastEcInfo.outputDevice.IsSameDeviceDesc(realOutputDevice)) {
354         return true;
355     }
356     AUDIO_INFO_LOG("voice source reload ignore for device not change");
357     return false;
358 }
359 
ReloadSourceForDeviceChange(const AudioDeviceDescriptor & inputDevice,const AudioDeviceDescriptor & outputDevice,const std::string & caller)360 void AudioCapturerSession::ReloadSourceForDeviceChange(const AudioDeviceDescriptor &inputDevice,
361     const AudioDeviceDescriptor &outputDevice, const std::string &caller)
362 {
363     AUDIO_INFO_LOG("form caller: %{public}s", caller.c_str());
364     if (!audioEcManager_.GetEcFeatureEnable()) {
365         AUDIO_INFO_LOG("reload ignore for feature not enable");
366         return;
367     }
368     SourceType normalSourceOpened = audioEcManager_.GetSourceOpened();
369     if (normalSourceOpened != SOURCE_TYPE_VOICE_COMMUNICATION && normalSourceOpened != SOURCE_TYPE_MIC) {
370         AUDIO_INFO_LOG("reload ignore for source not voip or mic");
371         return;
372     }
373 
374     if (normalSourceOpened == SOURCE_TYPE_VOICE_COMMUNICATION) {
375         if (!IsVoipDeviceChanged(inputDevice, outputDevice)) {
376             return;
377         }
378     } else {
379         if (inputDevice.deviceType_ == DEVICE_TYPE_DEFAULT ||
380             inputDevice.IsSameDeviceDesc(GetInputDeviceTypeForReload())) {
381             AUDIO_INFO_LOG("mic source reload ignore for device not changed");
382             return;
383         }
384     }
385 
386     std::lock_guard<std::mutex> lock(onCapturerSessionChangedMutex_);
387     // reload for device change, used session is not changed
388     if (sessionWithNormalSourceType_.find(sessionIdUsedToOpenSource_) == sessionWithNormalSourceType_.end()) {
389         AUDIO_ERR_LOG("target session not found");
390         return;
391     }
392     SetInputDeviceTypeForReload(inputDevice);
393     audioEcManager_.ReloadSourceForSession(sessionWithNormalSourceType_[sessionIdUsedToOpenSource_]);
394 }
395 
SetInputDeviceTypeForReload(const AudioDeviceDescriptor & inputDevice)396 void AudioCapturerSession::SetInputDeviceTypeForReload(const AudioDeviceDescriptor &inputDevice)
397 {
398     std::lock_guard<std::mutex> lock(inputDeviceReloadMutex_);
399     inputDeviceForReload_ = inputDevice;
400 }
401 
GetInputDeviceTypeForReload()402 const AudioDeviceDescriptor& AudioCapturerSession::GetInputDeviceTypeForReload()
403 {
404     std::lock_guard<std::mutex> lock(inputDeviceReloadMutex_);
405     return inputDeviceForReload_;
406 }
407 
GetEnhancePropByNameV3(const AudioEffectPropertyArrayV3 & propertyArray,const std::string & propName)408 std::string AudioCapturerSession::GetEnhancePropByNameV3(const AudioEffectPropertyArrayV3 &propertyArray,
409     const std::string &propName)
410 {
411     std::string propValue = "";
412     auto iter = std::find_if(propertyArray.property.begin(), propertyArray.property.end(),
413         [&propName](const AudioEffectPropertyV3 &prop) {
414             return prop.name == propName;
415         });
416     if (iter != propertyArray.property.end()) {
417         propValue = iter->category;
418     }
419     return propValue;
420 }
421 
ReloadSourceForEffect(const AudioEffectPropertyArrayV3 & oldPropertyArray,const AudioEffectPropertyArrayV3 & newPropertyArray)422 void AudioCapturerSession::ReloadSourceForEffect(const AudioEffectPropertyArrayV3 &oldPropertyArray,
423     const AudioEffectPropertyArrayV3 &newPropertyArray)
424 {
425     if (!audioEcManager_.GetMicRefFeatureEnable()) {
426         AUDIO_INFO_LOG("reload ignore for feature not enable");
427         return;
428     }
429     if (audioEcManager_.GetSourceOpened() != SOURCE_TYPE_VOICE_COMMUNICATION &&
430         audioEcManager_.GetSourceOpened() != SOURCE_TYPE_MIC) {
431         AUDIO_INFO_LOG("reload ignore for source not voip or record");
432         return;
433     }
434     std::string oldRecordProp = GetEnhancePropByNameV3(oldPropertyArray, "record");
435     std::string oldVoipUpProp = GetEnhancePropByNameV3(oldPropertyArray, "voip_up");
436     std::string newRecordProp = GetEnhancePropByNameV3(newPropertyArray, "record");
437     std::string newVoipUpProp = GetEnhancePropByNameV3(newPropertyArray, "voip_up");
438     std::lock_guard<std::mutex> lock(onCapturerSessionChangedMutex_);
439     if ((!newVoipUpProp.empty() && ((oldVoipUpProp == "PNR") ^ (newVoipUpProp == "PNR"))) ||
440         (!newRecordProp.empty() && oldRecordProp != newRecordProp)) {
441         audioEcManager_.ReloadSourceForSession(sessionWithNormalSourceType_[sessionIdUsedToOpenSource_]);
442     }
443 }
444 
GetEnhancePropByName(const AudioEnhancePropertyArray & propertyArray,const std::string & propName)445 std::string AudioCapturerSession::GetEnhancePropByName(const AudioEnhancePropertyArray &propertyArray,
446     const std::string &propName)
447 {
448     std::string propValue = "";
449     auto iter = std::find_if(propertyArray.property.begin(), propertyArray.property.end(),
450         [&propName](const AudioEnhanceProperty &prop) {
451             return prop.enhanceClass == propName;
452         });
453     if (iter != propertyArray.property.end()) {
454         propValue = iter->enhanceProp;
455     }
456     return propValue;
457 }
458 
ReloadSourceForEffect(const AudioEnhancePropertyArray & oldPropertyArray,const AudioEnhancePropertyArray & newPropertyArray)459 void AudioCapturerSession::ReloadSourceForEffect(const AudioEnhancePropertyArray &oldPropertyArray,
460     const AudioEnhancePropertyArray &newPropertyArray)
461 {
462     if (!audioEcManager_.GetMicRefFeatureEnable()) {
463         AUDIO_INFO_LOG("reload ignore for feature not enable");
464         return;
465     }
466     if (audioEcManager_.GetSourceOpened() != SOURCE_TYPE_VOICE_COMMUNICATION &&
467         audioEcManager_.GetSourceOpened() != SOURCE_TYPE_MIC) {
468         AUDIO_INFO_LOG("reload ignore for source not voip or record");
469         return;
470     }
471     std::string oldRecordProp = GetEnhancePropByName(oldPropertyArray, "record");
472     std::string oldVoipUpProp = GetEnhancePropByName(oldPropertyArray, "voip_up");
473     std::string newRecordProp = GetEnhancePropByName(newPropertyArray, "record");
474     std::string newVoipUpProp = GetEnhancePropByName(newPropertyArray, "voip_up");
475     std::lock_guard<std::mutex> lock(onCapturerSessionChangedMutex_);
476     if ((!newVoipUpProp.empty() && ((oldVoipUpProp == "PNR") ^ (newVoipUpProp == "PNR"))) ||
477         (!newRecordProp.empty() && oldRecordProp != newRecordProp)) {
478         audioEcManager_.ReloadSourceForSession(sessionWithNormalSourceType_[sessionIdUsedToOpenSource_]);
479     }
480 }
481 
482 }
483 }