• 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 "AudioEffectChainManager"
17 #endif
18 
19 #include "audio_effect_chain_manager.h"
20 #include "audio_effect.h"
21 #include "audio_errors.h"
22 #include "audio_effect_log.h"
23 #include "audio_utils.h"
24 #include "securec.h"
25 
26 #define DEVICE_FLAG
27 
28 namespace OHOS {
29 namespace AudioStandard {
CheckValidEffectLibEntry(const std::shared_ptr<AudioEffectLibEntry> & libEntry,const std::string & effect,const std::string & libName)30 static int32_t CheckValidEffectLibEntry(const std::shared_ptr<AudioEffectLibEntry> &libEntry, const std::string &effect,
31     const std::string &libName)
32 {
33     CHECK_AND_RETURN_RET_LOG(libEntry != nullptr, ERROR, "Effect [%{public}s] in lib [%{public}s] is nullptr",
34         effect.c_str(), libName.c_str());
35 
36     CHECK_AND_RETURN_RET_LOG(libEntry->audioEffectLibHandle, ERROR,
37         "AudioEffectLibHandle of Effect [%{public}s] in lib [%{public}s] is nullptr", effect.c_str(), libName.c_str());
38 
39     CHECK_AND_RETURN_RET_LOG(libEntry->audioEffectLibHandle->createEffect, ERROR,
40         "CreateEffect function of Effect [%{public}s] in lib [%{public}s] is nullptr", effect.c_str(), libName.c_str());
41 
42     CHECK_AND_RETURN_RET_LOG(libEntry->audioEffectLibHandle->releaseEffect, ERROR,
43         "ReleaseEffect function of Effect [%{public}s] in lib [%{public}s] is nullptr", effect.c_str(),
44         libName.c_str());
45     return SUCCESS;
46 }
47 
FindEffectLib(const std::string & effect,const std::vector<std::shared_ptr<AudioEffectLibEntry>> & effectLibraryList,std::shared_ptr<AudioEffectLibEntry> & libEntry,std::string & libName)48 static int32_t FindEffectLib(const std::string &effect,
49     const std::vector<std::shared_ptr<AudioEffectLibEntry>> &effectLibraryList,
50     std::shared_ptr<AudioEffectLibEntry> &libEntry, std::string &libName)
51 {
52     for (const std::shared_ptr<AudioEffectLibEntry> &lib : effectLibraryList) {
53         if (lib->libraryName == effect) {
54             libName = lib->libraryName;
55             libEntry = lib;
56             return SUCCESS;
57         }
58     }
59     return ERROR;
60 }
61 
IsChannelLayoutSupported(const uint64_t channelLayout)62 static bool IsChannelLayoutSupported(const uint64_t channelLayout)
63 {
64     return find(AUDIO_EFFECT_SUPPORTED_CHANNELLAYOUTS.begin(),
65         AUDIO_EFFECT_SUPPORTED_CHANNELLAYOUTS.end(), channelLayout) != AUDIO_EFFECT_SUPPORTED_CHANNELLAYOUTS.end();
66 }
67 
AudioEffectChainManager()68 AudioEffectChainManager::AudioEffectChainManager()
69 {
70     effectToLibraryEntryMap_.clear();
71     effectToLibraryNameMap_.clear();
72     effectChainToEffectsMap_.clear();
73     sceneTypeAndModeToEffectChainNameMap_.clear();
74     sceneTypeToEffectChainMap_.clear();
75     sceneTypeToEffectChainCountMap_.clear();
76     sessionIDSet_.clear();
77     sceneTypeToSessionIDMap_.clear();
78     sessionIDToEffectInfoMap_.clear();
79     deviceType_ = DEVICE_TYPE_SPEAKER;
80     deviceSink_ = DEFAULT_DEVICE_SINK;
81     spatialDeviceType_ = EARPHONE_TYPE_OTHERS;
82     isInitialized_ = false;
83 
84 #ifdef SENSOR_ENABLE
85     headTracker_ = std::make_shared<HeadTracker>();
86 #endif
87 
88     audioEffectHdiParam_ = std::make_shared<AudioEffectHdiParam>();
89     memset_s(static_cast<void *>(effectHdiInput_), sizeof(effectHdiInput_), 0, sizeof(effectHdiInput_));
90     GetSysPara("const.build.product", deviceClass_);
91     int32_t flag = 0;
92     GetSysPara("persist.multimedia.audioflag.debugarmflag", flag);
93     debugArmFlag_ = flag == 1 ? true : false;
94 }
95 
~AudioEffectChainManager()96 AudioEffectChainManager::~AudioEffectChainManager()
97 {
98     AUDIO_INFO_LOG("~AudioEffectChainManager()");
99 }
100 
GetInstance()101 AudioEffectChainManager *AudioEffectChainManager::GetInstance()
102 {
103     static AudioEffectChainManager audioEffectChainManager;
104     return &audioEffectChainManager;
105 }
106 
UpdateDeviceInfo(int32_t device,const std::string & sinkName)107 int32_t AudioEffectChainManager::UpdateDeviceInfo(int32_t device, const std::string &sinkName)
108 {
109     if (!isInitialized_) {
110         deviceType_ = (DeviceType)device;
111         deviceSink_ = sinkName;
112         AUDIO_INFO_LOG("has not beed initialized");
113         return ERROR;
114     }
115 
116     if (deviceSink_ == sinkName) {
117         AUDIO_PRERELEASE_LOGI("Same DeviceSinkName");
118     }
119     deviceSink_ = sinkName;
120 
121     if (deviceType_ == (DeviceType)device) {
122         AUDIO_INFO_LOG("DeviceType do not need to be Updated");
123         return ERROR;
124     }
125     // Delete effectChain in AP and store in backup map
126     AUDIO_PRERELEASE_LOGI("delete all chains when device type change");
127     DeleteAllChains();
128     deviceType_ = (DeviceType)device;
129 
130     return SUCCESS;
131 }
132 
SetSpkOffloadState()133 void AudioEffectChainManager::SetSpkOffloadState()
134 {
135     int32_t ret;
136     if (deviceType_ == DEVICE_TYPE_SPEAKER) {
137         if (!spkOffloadEnabled_) {
138             effectHdiInput_[0] = HDI_INIT;
139             ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_SPEAKER);
140             if (ret != SUCCESS || !CheckIfSpkDsp()) {
141                 AUDIO_WARNING_LOG("set hdi init failed, backup speaker entered");
142                 spkOffloadEnabled_ = false;
143                 RecoverAllChains();
144             } else {
145                 AUDIO_INFO_LOG("set hdi init succeeded, normal speaker entered");
146                 spkOffloadEnabled_ = true;
147             }
148         }
149     } else {
150         if (spkOffloadEnabled_) {
151             effectHdiInput_[0] = HDI_DESTROY;
152             ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_SPEAKER);
153             if (ret != SUCCESS) {
154                 AUDIO_WARNING_LOG("set hdi destroy failed, backup speaker entered");
155             }
156             spkOffloadEnabled_ = false;
157         }
158 
159         if (deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP && (!spatializationEnabled_ || btOffloadEnabled_)) {
160             return;
161         }
162 
163         AUDIO_INFO_LOG("recover all chains if device type not bt.");
164         RecoverAllChains();
165     }
166 }
167 
SetOutputDeviceSink(int32_t device,const std::string & sinkName)168 void AudioEffectChainManager::SetOutputDeviceSink(int32_t device, const std::string &sinkName)
169 {
170     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
171     if (UpdateDeviceInfo(device, sinkName) != SUCCESS) {
172         return;
173     }
174     // recover effectChain in speaker mode
175     SetSpkOffloadState();
176     return;
177 }
178 
GetDeviceTypeName()179 std::string AudioEffectChainManager::GetDeviceTypeName()
180 {
181     std::string name = "";
182     auto device = SUPPORTED_DEVICE_TYPE.find(deviceType_);
183     if (device != SUPPORTED_DEVICE_TYPE.end()) {
184         name = device->second;
185     }
186     return name;
187 }
188 
GetOffloadEnabled()189 bool AudioEffectChainManager::GetOffloadEnabled()
190 {
191     if (deviceType_ == DEVICE_TYPE_SPEAKER) {
192         return spkOffloadEnabled_;
193     } else {
194         return btOffloadEnabled_;
195     }
196 }
197 
InitHdiState()198 void AudioEffectChainManager::InitHdiState()
199 {
200     if (audioEffectHdiParam_ == nullptr) {
201         AUDIO_INFO_LOG("audioEffectHdiParam_ is nullptr.");
202         return;
203     }
204     audioEffectHdiParam_->InitHdi();
205     effectHdiInput_[0] = HDI_BLUETOOTH_MODE;
206     effectHdiInput_[1] = 1;
207     AUDIO_INFO_LOG("set hdi bluetooth mode: %{public}d", effectHdiInput_[1]);
208     int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
209     if (ret != SUCCESS) {
210         AUDIO_WARNING_LOG("set hdi bluetooth mode failed");
211     }
212     effectHdiInput_[0] = HDI_INIT;
213     ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_SPEAKER);
214     if (ret != SUCCESS) {
215         AUDIO_WARNING_LOG("set hdi init failed, backup speaker entered");
216         spkOffloadEnabled_ = false;
217     } else {
218         AUDIO_INFO_LOG("set hdi init succeeded, normal speaker entered");
219         spkOffloadEnabled_ = true;
220     }
221 }
222 
223 // Boot initialize
InitAudioEffectChainManager(std::vector<EffectChain> & effectChains,const EffectChainManagerParam & effectChainManagerParam,std::vector<std::shared_ptr<AudioEffectLibEntry>> & effectLibraryList)224 void AudioEffectChainManager::InitAudioEffectChainManager(std::vector<EffectChain> &effectChains,
225     const EffectChainManagerParam &effectChainManagerParam,
226     std::vector<std::shared_ptr<AudioEffectLibEntry>> &effectLibraryList)
227 {
228     const std::unordered_map<std::string, std::string> &map = effectChainManagerParam.sceneTypeToChainNameMap;
229     maxEffectChainCount_ = effectChainManagerParam.maxExtraNum + 1;
230     priorSceneList_ = effectChainManagerParam.priorSceneList;
231     std::set<std::string> effectSet;
232     for (EffectChain efc: effectChains) {
233         for (std::string effect: efc.apply) {
234             effectSet.insert(effect);
235         }
236     }
237 
238     // Construct EffectToLibraryEntryMap that stores libEntry for each effect name
239     std::shared_ptr<AudioEffectLibEntry> libEntry = nullptr;
240     std::string libName;
241     for (std::string effect: effectSet) {
242         int32_t ret = FindEffectLib(effect, effectLibraryList, libEntry, libName);
243         CHECK_AND_CONTINUE_LOG(ret != ERROR, "Couldn't find libEntry of effect %{public}s", effect.c_str());
244         ret = CheckValidEffectLibEntry(libEntry, effect, libName);
245         CHECK_AND_CONTINUE_LOG(ret != ERROR, "Invalid libEntry of effect %{public}s", effect.c_str());
246         effectToLibraryEntryMap_[effect] = libEntry;
247         effectToLibraryNameMap_[effect] = libName;
248     }
249     // Construct EffectChainToEffectsMap that stores all effect names of each effect chain
250     for (EffectChain efc: effectChains) {
251         std::string key = efc.name;
252         std::vector <std::string> effects;
253         for (std::string effectName: efc.apply) {
254             effects.emplace_back(effectName);
255         }
256         effectChainToEffectsMap_[key] = effects;
257     }
258 
259     // Constrcut SceneTypeAndModeToEffectChainNameMap that stores effectMode associated with the effectChainName
260     for (auto item = map.begin(); item != map.end(); ++item) {
261         sceneTypeAndModeToEffectChainNameMap_[item->first] = item->second;
262         if (item->first.substr(0, effectChainManagerParam.defaultSceneName.size()) ==
263             effectChainManagerParam.defaultSceneName) {
264             sceneTypeAndModeToEffectChainNameMap_[DEFAULT_SCENE_TYPE + item->first.substr(
265                 effectChainManagerParam.defaultSceneName.size())] = item->second;
266         }
267     }
268 
269     AUDIO_INFO_LOG("EffectToLibraryEntryMap size %{public}zu", effectToLibraryEntryMap_.size());
270     AUDIO_DEBUG_LOG("EffectChainToEffectsMap size %{public}zu, SceneTypeAndModeToEffectChainNameMap size %{public}zu",
271         effectChainToEffectsMap_.size(), sceneTypeAndModeToEffectChainNameMap_.size());
272     InitHdiState();
273 #ifdef WINDOW_MANAGER_ENABLE
274     AUDIO_DEBUG_LOG("Call RegisterDisplayListener.");
275 #endif
276     isInitialized_ = true;
277 }
278 
CheckAndAddSessionID(const std::string & sessionID)279 bool AudioEffectChainManager::CheckAndAddSessionID(const std::string &sessionID)
280 {
281     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
282     if (sessionIDSet_.count(sessionID)) {
283         return false;
284     }
285     sessionIDSet_.insert(sessionID);
286     return true;
287 }
288 
CreateAudioEffectChainDynamic(const std::string & sceneType)289 int32_t AudioEffectChainManager::CreateAudioEffectChainDynamic(const std::string &sceneType)
290 {
291     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
292     CHECK_AND_RETURN_RET_LOG(isInitialized_, ERROR, "has not been initialized");
293     CHECK_AND_RETURN_RET_LOG(sceneType != "", ERROR, "null sceneType");
294 
295     std::shared_ptr<AudioEffectChain> audioEffectChain = nullptr;
296     std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
297     std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
298 
299     if (sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
300         if (sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] == nullptr) {
301             sceneTypeToEffectChainMap_.erase(sceneTypeAndDeviceKey);
302             sceneTypeToEffectChainCountMap_.erase(sceneTypeAndDeviceKey);
303             AUDIO_WARNING_LOG("scene type %{public}s has null effect chain", sceneTypeAndDeviceKey.c_str());
304         } else {
305             sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey]++;
306             if (isDefaultEffectChainExisted_ && sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] ==
307                 sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey]) {
308                 defaultEffectChainCount_++;
309             }
310             AUDIO_INFO_LOG("effect chain already exist, current count: %{public}d, default count: %{public}d",
311                 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey], defaultEffectChainCount_);
312             return SUCCESS;
313         }
314     }
315     bool isPriorScene = std::find(priorSceneList_.begin(), priorSceneList_.end(), sceneType) != priorSceneList_.end();
316     audioEffectChain = CreateAudioEffectChain(sceneType, isPriorScene);
317 
318     sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] = audioEffectChain;
319     sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey] = 1;
320     if (!AUDIO_SUPPORTED_SCENE_MODES.count(EFFECT_DEFAULT)) {
321         return ERROR;
322     }
323     std::string effectMode = AUDIO_SUPPORTED_SCENE_MODES.find(EFFECT_DEFAULT)->second;
324     if (!isPriorScene && !sceneTypeToSpecialEffectSet_.count(sceneType) && defaultEffectChainCount_ > 1) {
325         return SUCCESS;
326     }
327     std::string createSceneType = (isPriorScene || sceneTypeToSpecialEffectSet_.count(sceneType) > 0) ?
328         sceneType : DEFAULT_SCENE_TYPE;
329     if (SetAudioEffectChainDynamic(createSceneType, effectMode) != SUCCESS) {
330         return ERROR;
331     }
332     return SUCCESS;
333 }
334 
SetAudioEffectChainDynamic(const std::string & sceneType,const std::string & effectMode)335 int32_t AudioEffectChainManager::SetAudioEffectChainDynamic(const std::string &sceneType, const std::string &effectMode)
336 {
337     std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
338     CHECK_AND_RETURN_RET_LOG(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey), ERROR,
339         "SceneType [%{public}s] does not exist, failed to set", sceneType.c_str());
340 
341     std::shared_ptr<AudioEffectChain> audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
342 
343     std::string effectChain;
344     std::string effectChainKey = sceneType + "_&_" + effectMode + "_&_" + GetDeviceTypeName();
345     std::string effectNone = AUDIO_SUPPORTED_SCENE_MODES.find(EFFECT_NONE)->second;
346     if (!sceneTypeAndModeToEffectChainNameMap_.count(effectChainKey)) {
347         AUDIO_ERR_LOG("EffectChain key [%{public}s] does not exist, auto set to %{public}s",
348             effectChainKey.c_str(), effectNone.c_str());
349         effectChain = effectNone;
350     } else {
351         effectChain = sceneTypeAndModeToEffectChainNameMap_[effectChainKey];
352     }
353 
354     if (effectChain != effectNone && !effectChainToEffectsMap_.count(effectChain)) {
355         AUDIO_ERR_LOG("EffectChain name [%{public}s] does not exist, auto set to %{public}s",
356             effectChain.c_str(), effectNone.c_str());
357         effectChain = effectNone;
358     }
359 
360     audioEffectChain->SetEffectMode(effectMode);
361     audioEffectChain->SetExtraSceneType(extraSceneType_);
362     audioEffectChain->SetSpatialDeviceType(spatialDeviceType_);
363     audioEffectChain->SetSpatializationSceneType(spatializationSceneType_);
364     audioEffectChain->SetSpatializationEnabled(spatializationEnabled_);
365     std::string tSceneType = (sceneType == DEFAULT_SCENE_TYPE ? DEFAULT_PRESET_SCENE :sceneType);
366     for (std::string effect: effectChainToEffectsMap_[effectChain]) {
367         AudioEffectHandle handle = nullptr;
368         AudioEffectDescriptor descriptor;
369         descriptor.libraryName = effectToLibraryNameMap_[effect];
370         descriptor.effectName = effect;
371         int32_t ret = effectToLibraryEntryMap_[effect]->audioEffectLibHandle->createEffect(descriptor, &handle);
372         CHECK_AND_CONTINUE_LOG(ret == 0, "EffectToLibraryEntryMap[%{public}s] createEffect fail", effect.c_str());
373 
374         AUDIO_INFO_LOG("createEffect, EffectToLibraryEntryMap [%{public}s], effectChainKey [%{public}s]",
375             effect.c_str(), effectChainKey.c_str());
376         AudioEffectScene currSceneType;
377         UpdateCurrSceneType(currSceneType, tSceneType);
378         audioEffectChain->AddEffectHandle(handle, effectToLibraryEntryMap_[effect]->audioEffectLibHandle,
379             currSceneType);
380     }
381     audioEffectChain->ResetIoBufferConfig();
382 
383     if (audioEffectChain->IsEmptyEffectHandles()) {
384         AUDIO_PRERELEASE_LOGI("Effectchain is empty, copy bufIn to bufOut like EFFECT_NONE mode");
385     }
386 
387     AUDIO_INFO_LOG("SceneType %{public}s delay %{public}u", sceneType.c_str(), audioEffectChain->GetLatency());
388     return SUCCESS;
389 }
390 
CheckAndRemoveSessionID(const std::string & sessionID)391 bool AudioEffectChainManager::CheckAndRemoveSessionID(const std::string &sessionID)
392 {
393     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
394     if (!sessionIDSet_.count(sessionID)) {
395         return false;
396     }
397     sessionIDSet_.erase(sessionID);
398     return true;
399 }
400 
ReleaseAudioEffectChainDynamic(const std::string & sceneType)401 int32_t AudioEffectChainManager::ReleaseAudioEffectChainDynamic(const std::string &sceneType)
402 {
403     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
404     CHECK_AND_RETURN_RET_LOG(isInitialized_, ERROR, "has not been initialized");
405     CHECK_AND_RETURN_RET_LOG(sceneType != "", ERROR, "null sceneType");
406 
407     std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
408     std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
409     if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
410         sceneTypeToEffectChainCountMap_.erase(sceneTypeAndDeviceKey);
411         return SUCCESS;
412     } else if (sceneTypeToEffectChainCountMap_.count(sceneTypeAndDeviceKey) &&
413         sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey] > 1) {
414         sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey]--;
415         if (sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] ==
416             sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey]) {
417             defaultEffectChainCount_--;
418         }
419         AUDIO_INFO_LOG("effect chain still exist, current count: %{public}d, default count: %{public}d",
420             sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey], defaultEffectChainCount_);
421         return SUCCESS;
422     }
423 
424     sceneTypeToSpecialEffectSet_.erase(sceneType);
425     sceneTypeToEffectChainCountMap_.erase(sceneTypeAndDeviceKey);
426     CheckAndReleaseCommonEffectChain(sceneType);
427     sceneTypeToEffectChainMap_.erase(sceneTypeAndDeviceKey);
428 
429     if (debugArmFlag_ && !spkOffloadEnabled_ && CheckIfSpkDsp()) {
430         effectHdiInput_[0] = HDI_INIT;
431         int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_SPEAKER);
432         if (ret == SUCCESS) {
433             AUDIO_INFO_LOG("set hdi init succeeded, normal speaker entered");
434             spkOffloadEnabled_ = true;
435             DeleteAllChains();
436         }
437     }
438     if (!sceneTypeToEffectChainMap_.count(defaultSceneTypeAndDeviceKey)) {
439         isDefaultEffectChainExisted_ = false;
440     }
441     AUDIO_DEBUG_LOG("releaseEffect, sceneTypeAndDeviceKey [%{public}s]", sceneTypeAndDeviceKey.c_str());
442     return SUCCESS;
443 }
444 
ExistAudioEffectChain(const std::string & sceneType,const std::string & effectMode,const std::string & spatializationEnabled)445 bool AudioEffectChainManager::ExistAudioEffectChain(const std::string &sceneType, const std::string &effectMode,
446     const std::string &spatializationEnabled)
447 {
448     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
449     if (!isInitialized_) {
450         if (initializedLogFlag_) {
451             AUDIO_ERR_LOG("audioEffectChainManager has not been initialized");
452             initializedLogFlag_ = false;
453         }
454         return false;
455     }
456     initializedLogFlag_ = true;
457     CHECK_AND_RETURN_RET(sceneType != "", false);
458     CHECK_AND_RETURN_RET_LOG(GetDeviceTypeName() != "", false, "null deviceType");
459 
460 #ifndef DEVICE_FLAG
461     if (deviceType_ != DEVICE_TYPE_SPEAKER) {
462         return false;
463     }
464 #endif
465     if ((deviceType_ == DEVICE_TYPE_SPEAKER) && (spkOffloadEnabled_)) {
466         return false;
467     }
468 
469     if ((deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) && (btOffloadEnabled_ || (spatializationEnabled == "0"))) {
470         return false;
471     }
472 
473     std::string effectChainKey = sceneType + "_&_" + effectMode + "_&_" + GetDeviceTypeName();
474     if (!sceneTypeAndModeToEffectChainNameMap_.count(effectChainKey)) {
475         return false;
476     }
477     std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
478     // if the effectChain exist, see if it is empty
479     if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) ||
480         sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] == nullptr) {
481         return false;
482     }
483     auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
484     return !audioEffectChain->IsEmptyEffectHandles();
485 }
486 
ApplyAudioEffectChain(const std::string & sceneType,const std::unique_ptr<EffectBufferAttr> & bufferAttr)487 int32_t AudioEffectChainManager::ApplyAudioEffectChain(const std::string &sceneType,
488     const std::unique_ptr<EffectBufferAttr> &bufferAttr)
489 {
490     std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
491     size_t totLen = static_cast<size_t>(bufferAttr->frameLen * bufferAttr->numChans * sizeof(float));
492     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
493     auto it = sceneTypeToEffectChainMap_.find(sceneTypeAndDeviceKey);
494 #ifdef DEVICE_FLAG
495     if (it == sceneTypeToEffectChainMap_.end() || it->second == nullptr) {
496         CHECK_AND_RETURN_RET_LOG(memcpy_s(bufferAttr->bufOut, totLen, bufferAttr->bufIn, totLen) == 0, ERROR,
497             "memcpy error when no effect applied");
498         return ERROR;
499     }
500 #else
501     if (deviceType_ != DEVICE_TYPE_SPEAKER || it == sceneTypeToEffectChainMap_.end()
502             || it->second == nullptr) {
503         CHECK_AND_RETURN_RET_LOG(memcpy_s(bufferAttr->bufOut, totLen, bufferAttr->bufIn, totLen) == 0, ERROR,
504             "memcpy error when no effect applied");
505         return SUCCESS;
506     }
507 #endif
508     auto audioEffectChain = it->second;
509     AudioEffectProcInfo procInfo = {headTrackingEnabled_, btOffloadEnabled_};
510     audioEffectChain->ApplyEffectChain(bufferAttr->bufIn, bufferAttr->bufOut, bufferAttr->frameLen, procInfo);
511     return SUCCESS;
512 }
513 
EffectDspVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)514 int32_t AudioEffectChainManager::EffectDspVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)
515 {
516     AUDIO_INFO_LOG("send volume to dsp.");
517     CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
518     float volumeMax = 0.0f;
519     for (auto it = sceneTypeToSessionIDMap_.begin(); it != sceneTypeToSessionIDMap_.end(); ++it) {
520         std::set<std::string> sessions = sceneTypeToSessionIDMap_[it->first];
521         for (auto s = sessions.begin(); s != sessions.end(); s++) {
522             if (sessionIDToEffectInfoMap_.find(*s) == sessionIDToEffectInfoMap_.end()) {
523                 AUDIO_INFO_LOG("sessionID:%{public}s sceneType:%{public}s, no find in sessionIDToEffectInfoMap_",
524                     (*s).c_str(), it->first.c_str());
525                 continue;
526             }
527             if (sessionIDToEffectInfoMap_[*s].sceneMode == "EFFECT_NONE") {
528                 AUDIO_INFO_LOG("sessionID:%{public}s sceneType:%{public}s, sceneMode is EFFECT_NONE, no send volume",
529                     (*s).c_str(), it->first.c_str());
530                 continue;
531             }
532             float streamVolumeTemp = audioEffectVolume->GetStreamVolume(*s);
533             float systemVolumeTemp = audioEffectVolume->GetSystemVolume(it->first);
534             volumeMax = fmax((streamVolumeTemp * systemVolumeTemp), volumeMax);
535         }
536     }
537     if (static_cast<int32_t>(audioEffectVolume->GetDspVolume() * MAX_UINT_VOLUME_NUM) !=
538         static_cast<int32_t>(volumeMax * MAX_UINT_VOLUME_NUM)) {
539         audioEffectVolume->SetDspVolume(volumeMax);
540         effectHdiInput_[0] = HDI_VOLUME;
541         AUDIO_INFO_LOG("finalVolume change to %{public}f", volumeMax);
542         int32_t dspVolumeMax = static_cast<int32_t>(volumeMax * MAX_UINT_DSP_VOLUME);
543         int32_t ret = memcpy_s(&effectHdiInput_[1], SEND_HDI_COMMAND_LEN - sizeof(int8_t),
544             &dspVolumeMax, sizeof(int32_t));
545         CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "memcpy volume failed");
546         AUDIO_INFO_LOG("set hdi volume: %{public}u", *(reinterpret_cast<uint32_t *>(&effectHdiInput_[1])));
547         ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
548         CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "set hdi volume failed");
549     }
550     return SUCCESS;
551 }
552 
EffectApVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)553 int32_t AudioEffectChainManager::EffectApVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)
554 {
555     AUDIO_INFO_LOG("send volume to ap.");
556     CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
557     for (auto sessionId = sessionIDSet_.begin(); sessionId != sessionIDSet_.end(); ++sessionId) {
558         if (sessionIDToEffectInfoMap_.find(*sessionId) == sessionIDToEffectInfoMap_.end()) {
559             AUDIO_INFO_LOG("sessionID:%{public}s, no find in sessionIDToEffectInfoMap_", (*sessionId).c_str());
560             continue;
561         }
562         if (sessionIDToEffectInfoMap_[*sessionId].sceneMode == "EFFECT_NONE") {
563             AUDIO_INFO_LOG("sessionID:%{public}s, sceneMode is EFFECT_NONE, no send volume", (*sessionId).c_str());
564             continue;
565         }
566         std::string sceneTypeTemp = sessionIDToEffectInfoMap_[*sessionId].sceneType;
567         std::string sceneTypeAndDeviceKey = sceneTypeTemp + "_&_" + GetDeviceTypeName();
568         CHECK_AND_RETURN_RET_LOG(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) > 0 &&
569             sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] != nullptr, ERROR, "null audioEffectChain");
570         auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
571         float streamVolumeTemp = audioEffectVolume->GetStreamVolume(*sessionId);
572         float systemVolumeTemp = audioEffectVolume->GetSystemVolume(sceneTypeTemp);
573         float currVolumeTemp = audioEffectChain->GetCurrVolume();
574         float volumeMax = fmax((streamVolumeTemp * systemVolumeTemp), currVolumeTemp);
575         if (volumeMax > currVolumeTemp) {
576             audioEffectChain->SetCurrVolume(volumeMax);
577         }
578         audioEffectChain->SetFinalVolumeState(true);
579     }
580     return SendEffectApVolume(audioEffectVolume);
581 }
582 
SendEffectApVolume(std::shared_ptr<AudioEffectVolume> audioEffectVolume)583 int32_t AudioEffectChainManager::SendEffectApVolume(std::shared_ptr<AudioEffectVolume> audioEffectVolume)
584 {
585     AUDIO_INFO_LOG("SendEffectApVolume");
586     CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
587     for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
588         CHECK_AND_RETURN_RET_LOG(it->second != nullptr, ERROR, "null audioEffectChain");
589         auto audioEffectChain = it->second;
590         float volumeMax = audioEffectChain->GetCurrVolume();
591         if ((static_cast<int32_t>(audioEffectChain->GetFinalVolume() * MAX_UINT_VOLUME_NUM) !=
592             static_cast<int32_t>(volumeMax * MAX_UINT_VOLUME_NUM)) &&
593             audioEffectChain->GetFinalVolumeState() == true) {
594             audioEffectChain->SetFinalVolume(volumeMax);
595             int32_t ret = audioEffectChain->UpdateEffectParam();
596             CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "set ap volume failed");
597             AUDIO_INFO_LOG("The delay of SceneType %{public}s is %{public}u, finalVolume changed to %{public}f",
598                 it->first.c_str(), audioEffectChain->GetLatency(), volumeMax);
599             audioEffectChain->SetFinalVolumeState(false);
600         }
601     }
602     for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
603         CHECK_AND_RETURN_RET_LOG(it->second != nullptr, ERROR, "null audioEffectChain");
604         auto audioEffectChain = it->second;
605         float volume = 0.0f;
606         audioEffectChain->SetCurrVolume(volume);
607     }
608     return SUCCESS;
609 }
610 
EffectVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)611 int32_t AudioEffectChainManager::EffectVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)
612 {
613     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
614     int32_t ret;
615     if (((deviceType_ == DEVICE_TYPE_SPEAKER) && (spkOffloadEnabled_)) ||
616         ((deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) && (btOffloadEnabled_))) {
617         ret = EffectDspVolumeUpdate(audioEffectVolume);
618     } else {
619         ret = EffectApVolumeUpdate(audioEffectVolume);
620     }
621     return ret;
622 }
623 
StreamVolumeUpdate(const std::string sessionIDString,const float streamVolume)624 int32_t AudioEffectChainManager::StreamVolumeUpdate(const std::string sessionIDString, const float streamVolume)
625 {
626     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
627     // update streamVolume
628     std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
629     CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
630     audioEffectVolume->SetStreamVolume(sessionIDString, streamVolume);
631     int32_t ret;
632     AUDIO_INFO_LOG("streamVolume is %{public}f", audioEffectVolume->GetStreamVolume(sessionIDString));
633     ret = EffectVolumeUpdate(audioEffectVolume);
634     return ret;
635 }
636 
SetSceneTypeSystemVolume(const std::string sceneType,const float systemVolume)637 int32_t AudioEffectChainManager::SetSceneTypeSystemVolume(const std::string sceneType, const float systemVolume)
638 {
639     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
640     // set systemVolume by sceneType
641     std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
642     CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
643     audioEffectVolume->SetSystemVolume(sceneType, systemVolume);
644     AUDIO_INFO_LOG("systemVolume is %{public}f", audioEffectVolume->GetSystemVolume(sceneType));
645 
646     return SUCCESS;
647 }
648 
649 #ifdef WINDOW_MANAGER_ENABLE
EffectDspRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,const uint32_t rotationState)650 int32_t AudioEffectChainManager::EffectDspRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,
651     const uint32_t rotationState)
652 {
653     // send rotation to dsp
654     AUDIO_DEBUG_LOG("send rotation to dsp.");
655     CHECK_AND_RETURN_RET_LOG(audioEffectRotation != nullptr, ERROR, "null audioEffectRotation");
656     AUDIO_DEBUG_LOG("rotationState change, new state: %{public}d, previous state: %{public}d",
657         rotationState, audioEffectRotation->GetRotation());
658     effectHdiInput_[0] = HDI_ROTATION;
659     effectHdiInput_[1] = rotationState;
660     AUDIO_INFO_LOG("set hdi rotation: %{public}d", effectHdiInput_[1]);
661     int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
662     CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "set hdi rotation failed");
663 
664     return SUCCESS;
665 }
666 
EffectApRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,const uint32_t rotationState)667 int32_t AudioEffectChainManager::EffectApRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,
668     const uint32_t rotationState)
669 {
670     // send rotation to ap
671     AUDIO_DEBUG_LOG("send rotation to ap.");
672     CHECK_AND_RETURN_RET_LOG(audioEffectRotation != nullptr, ERROR, "null audioEffectRotation");
673     AUDIO_DEBUG_LOG("rotationState change, new state: %{public}d, previous state: %{public}d",
674         rotationState, audioEffectRotation->GetRotation());
675     for (auto it = sceneTypeToSessionIDMap_.begin(); it != sceneTypeToSessionIDMap_.end(); it++) {
676         std::string sceneTypeAndDeviceKey = it->first + "_&_" + GetDeviceTypeName();
677         if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
678             return ERROR;
679         }
680         auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
681         if (audioEffectChain == nullptr) {
682             return ERROR;
683         }
684         int32_t ret = audioEffectChain->UpdateEffectParam();
685         CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "set ap rotation failed");
686         AUDIO_INFO_LOG("The delay of SceneType %{public}s is %{public}u, rotation changed to %{public}u",
687             it->first.c_str(), audioEffectChain->GetLatency(), rotationState);
688         }
689 
690     return SUCCESS;
691 }
692 
EffectRotationUpdate(const uint32_t rotationState)693 int32_t AudioEffectChainManager::EffectRotationUpdate(const uint32_t rotationState)
694 {
695     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
696     std::shared_ptr<AudioEffectRotation> audioEffectRotation = AudioEffectRotation::GetInstance();
697     AUDIO_INFO_LOG("rotation update to %{public}u", rotationState);
698     if (audioEffectRotation->GetRotation() != rotationState) {
699         audioEffectRotation->SetRotation(rotationState);
700         EffectDspRotationUpdate(audioEffectRotation, rotationState);
701         EffectApRotationUpdate(audioEffectRotation, rotationState);
702     }
703 
704     return SUCCESS;
705 }
706 #endif
707 
UpdateMultichannelConfig(const std::string & sceneType)708 int32_t AudioEffectChainManager::UpdateMultichannelConfig(const std::string &sceneType)
709 {
710     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
711     std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
712     if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
713         return ERROR;
714     }
715     uint32_t inputChannels = DEFAULT_NUM_CHANNEL;
716     uint64_t inputChannelLayout = DEFAULT_NUM_CHANNELLAYOUT;
717     ReturnEffectChannelInfo(sceneType, inputChannels, inputChannelLayout);
718 
719     auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
720     if (audioEffectChain == nullptr) {
721         return ERROR;
722     }
723     audioEffectChain->UpdateMultichannelIoBufferConfig(inputChannels, inputChannelLayout);
724     return SUCCESS;
725 }
726 
InitAudioEffectChainDynamic(const std::string & sceneType)727 int32_t AudioEffectChainManager::InitAudioEffectChainDynamic(const std::string &sceneType)
728 {
729     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
730     CHECK_AND_RETURN_RET_LOG(isInitialized_, ERROR, "has not been initialized");
731     CHECK_AND_RETURN_RET_LOG(sceneType != "", ERROR, "null sceneType");
732 
733     std::shared_ptr<AudioEffectChain> audioEffectChain = nullptr;
734     std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
735     if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
736         return SUCCESS;
737     } else {
738         audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
739     }
740     if (audioEffectChain != nullptr) {
741         audioEffectChain->InitEffectChain();
742     }
743 
744     return SUCCESS;
745 }
746 
UpdateSpatializationState(AudioSpatializationState spatializationState)747 int32_t AudioEffectChainManager::UpdateSpatializationState(AudioSpatializationState spatializationState)
748 {
749     AUDIO_INFO_LOG("UpdateSpatializationState entered, current state: %{public}d and %{public}d, previous state: \
750         %{public}d and %{public}d", spatializationState.spatializationEnabled, spatializationState.headTrackingEnabled,
751         spatializationEnabled_.load(), headTrackingEnabled_);
752     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
753     if (spatializationEnabled_ != spatializationState.spatializationEnabled) {
754         UpdateSpatializationEnabled(spatializationState);
755     }
756     if (headTrackingEnabled_ != spatializationState.headTrackingEnabled) {
757         headTrackingEnabled_ = spatializationState.headTrackingEnabled;
758         UpdateSensorState();
759     }
760 
761     std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
762     CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
763     EffectVolumeUpdate(audioEffectVolume);
764     AUDIO_INFO_LOG("systemVolume prepare change or no change");
765 
766     return SUCCESS;
767 }
768 
UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType)769 int32_t AudioEffectChainManager::UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType)
770 {
771     int32_t ret{ SUCCESS };
772     spatialDeviceType_ = spatialDeviceType;
773 
774     effectHdiInput_[0] = HDI_UPDATE_SPATIAL_DEVICE_TYPE;
775     effectHdiInput_[1] = spatialDeviceType_;
776     AUDIO_INFO_LOG("set hdi spatialDeviceType: %{public}d", effectHdiInput_[1]);
777     ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
778     if (ret != SUCCESS) {
779         AUDIO_WARNING_LOG("set hdi update spatial device type failed");
780     }
781 
782     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
783     for (const auto& sceneType2EffectChain : sceneTypeToEffectChainMap_) {
784         auto audioEffectChain = sceneType2EffectChain.second;
785         if (audioEffectChain != nullptr) {
786             audioEffectChain->SetSpatialDeviceType(spatialDeviceType_);
787             ret = audioEffectChain->UpdateEffectParam();
788             CHECK_AND_CONTINUE_LOG(ret == SUCCESS, "UpdateEffectParam failed.");
789         }
790     }
791 
792     return SUCCESS;
793 }
794 
ReturnEffectChannelInfo(const std::string & sceneType,uint32_t & channels,uint64_t & channelLayout)795 int32_t AudioEffectChainManager::ReturnEffectChannelInfo(const std::string &sceneType, uint32_t &channels,
796     uint64_t &channelLayout)
797 {
798     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
799     std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
800     if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
801         return ERROR;
802     }
803     for (auto& scenePair : sceneTypeToSessionIDMap_) {
804         std::string pairSceneTypeAndDeviceKey = scenePair.first + "_&_" + GetDeviceTypeName();
805         if (sceneTypeToEffectChainMap_.count(pairSceneTypeAndDeviceKey) > 0 &&
806             sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] ==
807             sceneTypeToEffectChainMap_[pairSceneTypeAndDeviceKey]) {
808             std::set<std::string> sessions = scenePair.second;
809             FindMaxEffectChannels(scenePair.first, sessions, channels, channelLayout);
810         }
811     }
812     return SUCCESS;
813 }
814 
ReturnMultiChannelInfo(uint32_t * channels,uint64_t * channelLayout)815 int32_t AudioEffectChainManager::ReturnMultiChannelInfo(uint32_t *channels, uint64_t *channelLayout)
816 {
817     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
818     uint32_t tmpChannelCount = DEFAULT_NUM_CHANNEL;
819     uint64_t tmpChannelLayout = DEFAULT_NUM_CHANNELLAYOUT;
820     bool channelUpdateFlag = false;
821     for (auto it = sceneTypeToSessionIDMap_.begin(); it != sceneTypeToSessionIDMap_.end(); it++) {
822         std::set<std::string> sessions = sceneTypeToSessionIDMap_[it->first];
823         for (auto s = sessions.begin(); s != sessions.end(); ++s) {
824             SessionEffectInfo info = sessionIDToEffectInfoMap_[*s];
825             if (info.channels > tmpChannelCount &&
826                 info.channels <= DSP_MAX_NUM_CHANNEL &&
827                 !ExistAudioEffectChain(it->first, info.sceneMode, info.spatializationEnabled) &&
828                 IsChannelLayoutSupported(info.channelLayout)) {
829                 tmpChannelCount = info.channels;
830                 tmpChannelLayout = info.channelLayout;
831                 channelUpdateFlag = true;
832             }
833         }
834     }
835     if (channelUpdateFlag) {
836         *channels = tmpChannelCount;
837         *channelLayout = tmpChannelLayout;
838     }
839     return SUCCESS;
840 }
841 
SessionInfoMapAdd(const std::string & sessionID,const SessionEffectInfo & info)842 int32_t AudioEffectChainManager::SessionInfoMapAdd(const std::string &sessionID, const SessionEffectInfo &info)
843 {
844     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
845     CHECK_AND_RETURN_RET_LOG(sessionID != "", ERROR, "null sessionID");
846     if (!sessionIDToEffectInfoMap_.count(sessionID)) {
847         sceneTypeToSessionIDMap_[info.sceneType].insert(sessionID);
848         sessionIDToEffectInfoMap_[sessionID] = info;
849     } else if (sessionIDToEffectInfoMap_[sessionID].sceneMode != info.sceneMode ||
850         sessionIDToEffectInfoMap_[sessionID].spatializationEnabled != info.spatializationEnabled) {
851         sessionIDToEffectInfoMap_[sessionID] = info;
852     } else {
853         return ERROR;
854     }
855     return SUCCESS;
856 }
857 
SessionInfoMapDelete(const std::string & sceneType,const std::string & sessionID)858 int32_t AudioEffectChainManager::SessionInfoMapDelete(const std::string &sceneType, const std::string &sessionID)
859 {
860     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
861     if (!sceneTypeToSessionIDMap_.count(sceneType)) {
862         return ERROR;
863     }
864     if (sceneTypeToSessionIDMap_[sceneType].erase(sessionID)) {
865         if (sceneTypeToSessionIDMap_[sceneType].empty()) {
866             sceneTypeToSessionIDMap_.erase(sceneType);
867         }
868     } else {
869         return ERROR;
870     }
871     if (!sessionIDToEffectInfoMap_.erase(sessionID)) {
872         return ERROR;
873     }
874     return SUCCESS;
875 }
876 
SetHdiParam(const AudioEffectScene & sceneType)877 int32_t AudioEffectChainManager::SetHdiParam(const AudioEffectScene &sceneType)
878 {
879     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
880     if (!isInitialized_) {
881         if (initializedLogFlag_) {
882             AUDIO_ERR_LOG("audioEffectChainManager has not been initialized");
883             initializedLogFlag_ = false;
884         }
885         return ERROR;
886     }
887     memset_s(static_cast<void *>(effectHdiInput_), sizeof(effectHdiInput_), 0, sizeof(effectHdiInput_));
888 
889     effectHdiInput_[0] = HDI_ROOM_MODE;
890     effectHdiInput_[1] = sceneType;
891     AUDIO_PRERELEASE_LOGI("set hdi room mode sceneType: %{public}d", effectHdiInput_[1]);
892     int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
893     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR, "set hdi room mode failed, ret is %{public}d", ret);
894     return SUCCESS;
895 }
896 
UpdateSensorState()897 void AudioEffectChainManager::UpdateSensorState()
898 {
899     effectHdiInput_[0] = HDI_HEAD_MODE;
900     effectHdiInput_[1] = headTrackingEnabled_ == true ? 1 : 0;
901     AUDIO_INFO_LOG("set hdi head mode: %{public}d", effectHdiInput_[1]);
902     int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
903     if (ret != SUCCESS) {
904         AUDIO_WARNING_LOG("set hdi head mode failed");
905     }
906 
907     if (headTrackingEnabled_) {
908 #ifdef SENSOR_ENABLE
909         if (btOffloadEnabled_) {
910             headTracker_->SensorInit();
911             ret = headTracker_->SensorSetConfig(DSP_SPATIALIZER_ENGINE);
912         } else {
913             headTracker_->SensorInit();
914             ret = headTracker_->SensorSetConfig(ARM_SPATIALIZER_ENGINE);
915         }
916 
917         if (ret != SUCCESS) {
918             AUDIO_ERR_LOG("SensorSetConfig error!");
919         }
920 
921         if (headTracker_->SensorActive() != SUCCESS) {
922             AUDIO_ERR_LOG("SensorActive failed");
923         }
924 #endif
925         return;
926     }
927 
928     if (btOffloadEnabled_) {
929         return;
930     }
931 
932 #ifdef SENSOR_ENABLE
933     if (headTracker_->SensorDeactive() != SUCCESS) {
934         AUDIO_ERR_LOG("SensorDeactive failed");
935     }
936     headTracker_->SensorUnsubscribe();
937     HeadPostureData headPostureData = {1, 1.0, 0.0, 0.0, 0.0}; // ori head posturedata
938     headTracker_->SetHeadPostureData(headPostureData);
939     for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
940         auto audioEffectChain = it->second;
941         if (audioEffectChain == nullptr) {
942             continue;
943         }
944         audioEffectChain->SetHeadTrackingDisabled();
945     }
946 #endif
947 }
948 
DeleteAllChains()949 void AudioEffectChainManager::DeleteAllChains()
950 {
951     std::map<std::string, int32_t> sceneTypeToEffectChainCountBackupMap;
952     for (auto it = sceneTypeToEffectChainCountMap_.begin(); it != sceneTypeToEffectChainCountMap_.end(); ++it) {
953         AUDIO_DEBUG_LOG("sceneTypeAndDeviceKey %{public}s count:%{public}d", it->first.c_str(), it->second);
954         sceneTypeToEffectChainCountBackupMap.insert(
955             std::make_pair(it->first.substr(0, static_cast<size_t>(it->first.find("_&_"))), it->second));
956     }
957 
958     for (auto it = sceneTypeToEffectChainCountBackupMap.begin(); it != sceneTypeToEffectChainCountBackupMap.end();
959         ++it) {
960         for (int32_t k = 0; k < it->second; ++k) {
961             ReleaseAudioEffectChainDynamic(it->first);
962         }
963     }
964     return;
965 }
966 
RecoverAllChains()967 void AudioEffectChainManager::RecoverAllChains()
968 {
969     for (auto item : sceneTypeCountList_) {
970         AUDIO_DEBUG_LOG("sceneType %{public}s count:%{public}d", item.first.c_str(), item.second);
971         for (int32_t k = 0; k < item.second; ++k) {
972             CreateAudioEffectChainDynamic(item.first);
973         }
974         UpdateMultichannelConfig(item.first);
975     }
976 }
977 
GetLatency(const std::string & sessionId)978 uint32_t AudioEffectChainManager::GetLatency(const std::string &sessionId)
979 {
980     if (((deviceType_ == DEVICE_TYPE_SPEAKER) && (spkOffloadEnabled_)) ||
981         ((deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) && (btOffloadEnabled_))) {
982         AUDIO_DEBUG_LOG("offload enabled, return 0");
983         return 0;
984     }
985     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
986     CHECK_AND_RETURN_RET(sessionIDToEffectInfoMap_.count(sessionId), 0);
987     if (sessionIDToEffectInfoMap_[sessionId].sceneMode == "" ||
988         sessionIDToEffectInfoMap_[sessionId].sceneMode == "None") {
989         AUDIO_DEBUG_LOG("seceneMode is None, return 0");
990         return 0;
991     }
992     if (sessionIDToEffectInfoMap_[sessionId].spatializationEnabled == "0" &&
993         GetDeviceTypeName() == "DEVICE_TYPE_BLUETOOTH_A2DP") {
994         return 0;
995     }
996     std::string sceneTypeAndDeviceKey = sessionIDToEffectInfoMap_[sessionId].sceneType + "_&_" + GetDeviceTypeName();
997     CHECK_AND_RETURN_RET(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) &&
998         sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] != nullptr, 0);
999     return sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey]->GetLatency();
1000 }
1001 
SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType)1002 int32_t AudioEffectChainManager::SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType)
1003 {
1004     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1005     AUDIO_INFO_LOG("spatialization scene type is set to be %{public}d", spatializationSceneType);
1006     spatializationSceneType_ = spatializationSceneType;
1007 
1008     if (!spatializationEnabled_ || (GetDeviceTypeName() != "DEVICE_TYPE_BLUETOOTH_A2DP")) {
1009         return SUCCESS;
1010     }
1011 
1012     effectHdiInput_[0] = HDI_SPATIALIZATION_SCENE_TYPE;
1013     effectHdiInput_[1] = static_cast<int32_t>(spatializationSceneType_);
1014     if (audioEffectHdiParam_->UpdateHdiState(effectHdiInput_) != SUCCESS) {
1015         AUDIO_WARNING_LOG("set hdi spatialization scene type failed");
1016     }
1017 
1018     SetSpatializationSceneTypeToChains();
1019 
1020     return SUCCESS;
1021 }
1022 
UpdateExtraSceneType(const std::string & mainkey,const std::string & subkey,const std::string & extraSceneType)1023 void AudioEffectChainManager::UpdateExtraSceneType(const std::string &mainkey, const std::string &subkey,
1024     const std::string &extraSceneType)
1025 {
1026     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1027     if (mainkey == "audio_effect" && subkey == "update_audio_effect_type") {
1028         AUDIO_INFO_LOG("Set scene type: %{public}s to hdi", extraSceneType.c_str());
1029         int32_t ret{ SUCCESS };
1030         effectHdiInput_[0] = HDI_EXTRA_SCENE_TYPE;
1031         effectHdiInput_[1] = static_cast<int32_t>(std::stoi(extraSceneType));
1032         ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_SPEAKER);
1033         if (ret != SUCCESS) {
1034             AUDIO_WARNING_LOG("set hdi update rss scene type failed");
1035         }
1036         AUDIO_INFO_LOG("Set scene type: %{public}s to arm", extraSceneType.c_str());
1037         extraSceneType_ = extraSceneType;
1038         for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
1039             auto audioEffectChain = it->second;
1040             if (audioEffectChain == nullptr) {
1041                 continue;
1042             }
1043             audioEffectChain->SetExtraSceneType(extraSceneType);
1044             if (audioEffectChain->UpdateEffectParam() != SUCCESS) {
1045                 AUDIO_WARNING_LOG("Update scene type to effect chain failed");
1046                 continue;
1047             }
1048         }
1049     } else {
1050         AUDIO_INFO_LOG("UpdateExtraSceneType failed, mainkey is %{public}s, subkey is %{public}s, "
1051             "extraSceneType is %{public}s", mainkey.c_str(), subkey.c_str(), extraSceneType.c_str());
1052         return;
1053     }
1054 }
1055 
SetSpatializationSceneTypeToChains()1056 void AudioEffectChainManager::SetSpatializationSceneTypeToChains()
1057 {
1058     for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
1059         auto audioEffectChain = it->second;
1060         if (audioEffectChain == nullptr) {
1061             continue;
1062         }
1063         audioEffectChain->SetSpatializationSceneType(spatializationSceneType_);
1064         if (audioEffectChain->UpdateEffectParam() != SUCCESS) {
1065             AUDIO_WARNING_LOG("Update param to effect chain failed");
1066             continue;
1067         }
1068     }
1069 }
1070 
SetSpatializationEnabledToChains()1071 void AudioEffectChainManager::SetSpatializationEnabledToChains()
1072 {
1073     for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
1074         auto audioEffectChain = it->second;
1075         if (audioEffectChain == nullptr) {
1076             continue;
1077         }
1078         audioEffectChain->SetSpatializationEnabled(spatializationEnabled_);
1079         if (audioEffectChain->UpdateEffectParam() != SUCCESS) {
1080             AUDIO_WARNING_LOG("Update param to effect chain failed");
1081             continue;
1082         }
1083     }
1084 }
1085 
GetCurSpatializationEnabled()1086 bool AudioEffectChainManager::GetCurSpatializationEnabled()
1087 {
1088     return spatializationEnabled_;
1089 }
1090 
ResetEffectBuffer()1091 void AudioEffectChainManager::ResetEffectBuffer()
1092 {
1093     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1094     for (const auto &[sceneType, effectChain] : sceneTypeToEffectChainMap_) {
1095         if (effectChain == nullptr) continue;
1096         effectChain->InitEffectChain();
1097     }
1098 }
1099 
ResetInfo()1100 void AudioEffectChainManager::ResetInfo()
1101 {
1102     effectToLibraryEntryMap_.clear();
1103     effectToLibraryNameMap_.clear();
1104     effectChainToEffectsMap_.clear();
1105     sceneTypeAndModeToEffectChainNameMap_.clear();
1106     sceneTypeToEffectChainMap_.clear();
1107     sceneTypeToEffectChainCountMap_.clear();
1108     sessionIDSet_.clear();
1109     sceneTypeToSessionIDMap_.clear();
1110     sessionIDToEffectInfoMap_.clear();
1111     sceneTypeToSpecialEffectSet_.clear();
1112     deviceType_ = DEVICE_TYPE_SPEAKER;
1113     deviceSink_ = DEFAULT_DEVICE_SINK;
1114     isInitialized_ = false;
1115     spatializationEnabled_ = false;
1116     headTrackingEnabled_ = false;
1117     btOffloadEnabled_ = false;
1118     spkOffloadEnabled_ = false;
1119     initializedLogFlag_ = true;
1120     spatializationSceneType_ = SPATIALIZATION_SCENE_TYPE_DEFAULT;
1121     isDefaultEffectChainExisted_ = false;
1122 }
1123 
UpdateDefaultAudioEffect()1124 void AudioEffectChainManager::UpdateDefaultAudioEffect()
1125 {
1126     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1127     // for default scene type
1128     uint32_t maxDefaultSessionID = 0;
1129     uint32_t maxSessionID = 0;
1130     for (auto& scenePair : sceneTypeToSessionIDMap_) {
1131         std::set<std::string> &sessions = scenePair.second;
1132         if (!sceneTypeToSpecialEffectSet_.count(scenePair.first) &&
1133             std::find(priorSceneList_.begin(), priorSceneList_.end(),
1134             scenePair.first) == priorSceneList_.end()) {
1135             FindMaxSessionID(maxDefaultSessionID, maxDefaultSessionIDToSceneType_, scenePair.first, sessions);
1136         }
1137         FindMaxSessionID(maxSessionID, maxSessionIDToSceneType_, scenePair.first, sessions);
1138     }
1139     maxSessionID_ = maxSessionID;
1140     AUDIO_INFO_LOG("newest stream, maxDefaultSessionID: %{public}u, sceneType: %{public}s,"
1141         "maxSessionID: %{public}u, sceneType: %{public}s",
1142         maxDefaultSessionID, maxDefaultSessionIDToSceneType_.c_str(),
1143         maxSessionID_, maxSessionIDToSceneType_.c_str());
1144 
1145     std::string key = maxDefaultSessionIDToSceneType_ + "_&_" + GetDeviceTypeName();
1146     std::string maxDefaultSession = std::to_string(maxDefaultSessionID);
1147     AudioEffectScene currDefaultSceneType;
1148     UpdateCurrSceneType(currDefaultSceneType, maxDefaultSessionIDToSceneType_);
1149     if (!maxDefaultSessionIDToSceneType_.empty() && sessionIDToEffectInfoMap_.count(maxDefaultSession) &&
1150         sceneTypeToEffectChainMap_.count(key) && sceneTypeToEffectChainMap_[key] != nullptr) {
1151         SessionEffectInfo info = sessionIDToEffectInfoMap_[maxDefaultSession];
1152         std::shared_ptr<AudioEffectChain> audioEffectChain = sceneTypeToEffectChainMap_[key];
1153         audioEffectChain->SetEffectCurrSceneType(currDefaultSceneType);
1154         audioEffectChain->SetStreamUsage(info.streamUsage);
1155         audioEffectChain->UpdateEffectParam();
1156     }
1157 }
1158 
UpdateStreamUsage()1159 void AudioEffectChainManager::UpdateStreamUsage()
1160 {
1161     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1162     // for special scene type
1163     for (auto& specialSceneType : sceneTypeToSpecialEffectSet_) {
1164         uint32_t maxSpecialSessionID = 0;
1165         std::string maxSpecialSceneType = "";
1166         auto it = sceneTypeToSessionIDMap_.find(specialSceneType);
1167         if (it != sceneTypeToSessionIDMap_.end()) {
1168             std::set<std::string> &sessions = it->second;
1169             FindMaxSessionID(maxSpecialSessionID, maxSpecialSceneType, specialSceneType, sessions);
1170         }
1171         std::string maxSpecialSession = std::to_string(maxSpecialSessionID);
1172         std::string key = maxSpecialSceneType + "_&_" + GetDeviceTypeName();
1173         if (!maxSpecialSceneType.empty() && sessionIDToEffectInfoMap_.count(maxSpecialSession) &&
1174             sceneTypeToEffectChainMap_.count(key) && sceneTypeToEffectChainMap_[key] != nullptr) {
1175             SessionEffectInfo info = sessionIDToEffectInfoMap_[maxSpecialSession];
1176             std::shared_ptr<AudioEffectChain> audioEffectChain = sceneTypeToEffectChainMap_[key];
1177             audioEffectChain->SetStreamUsage(info.streamUsage);
1178             audioEffectChain->UpdateEffectParam();
1179         }
1180         AUDIO_INFO_LOG("newest stream, maxSpecialSessionID: %{public}u, sceneType: %{public}s",
1181             maxSpecialSessionID, maxSpecialSceneType.c_str());
1182     }
1183     // for prior scene type
1184     for (auto& priorSceneType : priorSceneList_) {
1185         uint32_t maxPriorSessionID = 0;
1186         std::string maxPriorSceneType = "";
1187         auto it = sceneTypeToSessionIDMap_.find(priorSceneType);
1188         if (it != sceneTypeToSessionIDMap_.end()) {
1189             std::set<std::string> &sessions = it->second;
1190             FindMaxSessionID(maxPriorSessionID, maxPriorSceneType, priorSceneType, sessions);
1191         }
1192         std::string key = maxPriorSceneType + "_&_" + GetDeviceTypeName();
1193         std::string maxPriorSession = std::to_string(maxPriorSessionID);
1194         if (!maxPriorSceneType.empty() && sessionIDToEffectInfoMap_.count(maxPriorSession) &&
1195             sceneTypeToEffectChainMap_.count(key) && sceneTypeToEffectChainMap_[key] != nullptr) {
1196             SessionEffectInfo info = sessionIDToEffectInfoMap_[maxPriorSession];
1197             std::shared_ptr<AudioEffectChain> audioEffectChain = sceneTypeToEffectChainMap_[key];
1198             audioEffectChain->SetStreamUsage(info.streamUsage);
1199             audioEffectChain->UpdateEffectParam();
1200         }
1201         AUDIO_INFO_LOG("newest stream, maxSpecialSessionID: %{public}u, sceneType: %{public}s",
1202             maxPriorSessionID, maxPriorSceneType.c_str());
1203     }
1204     // update dsp scene type and stream usage
1205     UpdateCurrSceneTypeAndStreamUsageForDsp();
1206 }
1207 
CheckSceneTypeMatch(const std::string & sinkSceneType,const std::string & sceneType)1208 bool AudioEffectChainManager::CheckSceneTypeMatch(const std::string &sinkSceneType, const std::string &sceneType)
1209 {
1210     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1211     std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1212     std::string sinkSceneTypeAndDeviceKey = sinkSceneType + "_&_" + GetDeviceTypeName();
1213     std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1214     if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) ||
1215         !sceneTypeToEffectChainMap_.count(sinkSceneTypeAndDeviceKey)) {
1216         return false;
1217     }
1218     if (sceneType == sinkSceneType && (sceneTypeToSpecialEffectSet_.count(sinkSceneType) ||
1219         std::find(priorSceneList_.begin(), priorSceneList_.end(), sceneType) != priorSceneList_.end())) {
1220         return true;
1221     }
1222     if (sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] ==
1223         sceneTypeToEffectChainMap_[sinkSceneTypeAndDeviceKey]) {
1224         return sceneTypeAndDeviceKey == defaultSceneTypeAndDeviceKey;
1225     }
1226     return false;
1227 }
1228 
UpdateCurrSceneType(AudioEffectScene & currSceneType,std::string & sceneType)1229 void AudioEffectChainManager::UpdateCurrSceneType(AudioEffectScene &currSceneType, std::string &sceneType)
1230 {
1231     currSceneType = static_cast<AudioEffectScene>(GetKeyFromValue(AUDIO_SUPPORTED_SCENE_TYPES, sceneType));
1232 }
1233 
FindMaxEffectChannels(const std::string & sceneType,const std::set<std::string> & sessions,uint32_t & channels,uint64_t & channelLayout)1234 void AudioEffectChainManager::FindMaxEffectChannels(const std::string &sceneType,
1235     const std::set<std::string> &sessions, uint32_t &channels, uint64_t &channelLayout)
1236 {
1237     for (auto s = sessions.begin(); s != sessions.end(); ++s) {
1238         SessionEffectInfo info = sessionIDToEffectInfoMap_[*s];
1239         uint32_t tmpChannelCount;
1240         uint64_t tmpChannelLayout;
1241         std::string deviceType = GetDeviceTypeName();
1242         if (((deviceType == "DEVICE_TYPE_BLUETOOTH_A2DP") || (deviceType == "DEVICE_TYPE_SPEAKER"))
1243             && ExistAudioEffectChain(sceneType, info.sceneMode, info.spatializationEnabled)
1244             && IsChannelLayoutSupported(info.channelLayout)) {
1245             tmpChannelLayout = info.channelLayout;
1246             tmpChannelCount = info.channels;
1247         } else {
1248             tmpChannelCount = DEFAULT_NUM_CHANNEL;
1249             tmpChannelLayout = DEFAULT_NUM_CHANNELLAYOUT;
1250         }
1251 
1252         if (tmpChannelCount >= channels) {
1253             channels = tmpChannelCount;
1254             channelLayout = tmpChannelLayout;
1255         }
1256     }
1257 }
1258 
CreateAudioEffectChain(const std::string & sceneType,bool isPriorScene)1259 std::shared_ptr<AudioEffectChain> AudioEffectChainManager::CreateAudioEffectChain(const std::string &sceneType,
1260     bool isPriorScene)
1261 {
1262     std::shared_ptr<AudioEffectChain> audioEffectChain = nullptr;
1263     std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1264 
1265     if (isPriorScene) {
1266         AUDIO_INFO_LOG("create prior effect chain: %{public}s", sceneType.c_str());
1267 #ifdef SENSOR_ENABLE
1268         audioEffectChain = std::make_shared<AudioEffectChain>(sceneType, headTracker_);
1269 #else
1270         audioEffectChain = std::make_shared<AudioEffectChain>(sceneType);
1271 #endif
1272         return audioEffectChain;
1273     }
1274     if ((maxEffectChainCount_ - static_cast<int32_t>(sceneTypeToSpecialEffectSet_.size())) > 1) {
1275         AUDIO_INFO_LOG("max audio effect chain count not reached, create special effect chain: %{public}s",
1276             sceneType.c_str());
1277         sceneTypeToSpecialEffectSet_.insert(sceneType);
1278 #ifdef SENSOR_ENABLE
1279         audioEffectChain = std::make_shared<AudioEffectChain>(sceneType, headTracker_);
1280 #else
1281         audioEffectChain = std::make_shared<AudioEffectChain>(sceneType);
1282 #endif
1283     } else {
1284         if (!isDefaultEffectChainExisted_) {
1285             AUDIO_INFO_LOG("max audio effect chain count reached, create current and default effect chain");
1286 #ifdef SENSOR_ENABLE
1287             audioEffectChain = std::make_shared<AudioEffectChain>(DEFAULT_SCENE_TYPE, headTracker_);
1288 #else
1289             audioEffectChain = std::make_shared<AudioEffectChain>(DEFAULT_SCENE_TYPE);
1290 #endif
1291             sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey] = audioEffectChain;
1292             defaultEffectChainCount_ = 1;
1293             isDefaultEffectChainExisted_ = true;
1294         } else {
1295             audioEffectChain = sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey];
1296             defaultEffectChainCount_++;
1297             AUDIO_INFO_LOG("max audio effect chain count reached and default effect chain already exist: %{public}d",
1298                 defaultEffectChainCount_);
1299         }
1300     }
1301     return audioEffectChain;
1302 }
1303 
CheckAndReleaseCommonEffectChain(const std::string & sceneType)1304 void AudioEffectChainManager::CheckAndReleaseCommonEffectChain(const std::string &sceneType)
1305 {
1306     AUDIO_INFO_LOG("release effect chain for scene type: %{public}s", sceneType.c_str());
1307     std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1308     std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1309     if (!isDefaultEffectChainExisted_) {
1310         return;
1311     }
1312     if (sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey] == sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey]) {
1313         if (defaultEffectChainCount_ <= 1) {
1314             sceneTypeToEffectChainMap_.erase(defaultSceneTypeAndDeviceKey);
1315             defaultEffectChainCount_= 0;
1316             isDefaultEffectChainExisted_ = false;
1317             AUDIO_INFO_LOG("default effect chain is released");
1318         } else {
1319             defaultEffectChainCount_--;
1320             AUDIO_INFO_LOG("default effect chain still exist, count is %{public}d", defaultEffectChainCount_);
1321         }
1322     }
1323 }
1324 
UpdateSpatializationEnabled(AudioSpatializationState spatializationState)1325 void AudioEffectChainManager::UpdateSpatializationEnabled(AudioSpatializationState spatializationState)
1326 {
1327     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1328     spatializationEnabled_ = spatializationState.spatializationEnabled;
1329 
1330     memset_s(static_cast<void *>(effectHdiInput_), sizeof(effectHdiInput_), 0, sizeof(effectHdiInput_));
1331     if (spatializationEnabled_) {
1332         if ((deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) && (!btOffloadSupported_)) {
1333             AUDIO_INFO_LOG("A2dp-hal, enter ARM processing");
1334             btOffloadEnabled_ = false;
1335             RecoverAllChains();
1336             SetSpatializationEnabledToChains();
1337             return;
1338         }
1339         effectHdiInput_[0] = HDI_INIT;
1340         int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
1341         if (ret != SUCCESS) {
1342             AUDIO_ERR_LOG("set hdi init failed, enter route of escape in ARM");
1343             btOffloadEnabled_ = false;
1344             RecoverAllChains();
1345         } else {
1346             AUDIO_INFO_LOG("set hdi init succeeded, normal spatialization entered");
1347             btOffloadEnabled_ = true;
1348         }
1349     } else {
1350         effectHdiInput_[0] = HDI_DESTROY;
1351         AUDIO_INFO_LOG("set hdi destroy.");
1352         int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
1353         if (ret != SUCCESS) {
1354             AUDIO_ERR_LOG("set hdi destroy failed");
1355         }
1356         if (deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) {
1357             AUDIO_INFO_LOG("delete all chains if device type is bt.");
1358             DeleteAllChains();
1359         }
1360         btOffloadEnabled_ = false;
1361     }
1362     SetSpatializationEnabledToChains();
1363 }
1364 // for AISS temporarily
CheckIfSpkDsp()1365 bool AudioEffectChainManager::CheckIfSpkDsp()
1366 {
1367     if (deviceType_ != DEVICE_TYPE_SPEAKER) {
1368         return false;
1369     }
1370     if (debugArmFlag_) {
1371         for (auto &[key, count] : sceneTypeToEffectChainCountMap_) {
1372             std::string sceneType = key.substr(0, static_cast<size_t>(key.find("_&_")));
1373             if (sceneType == "SCENE_MOVIE" && count > 0) {
1374                 return false;
1375             }
1376         }
1377     }
1378     return true;
1379 }
1380 
UpdateEffectBtOffloadSupported(const bool & isSupported)1381 void AudioEffectChainManager::UpdateEffectBtOffloadSupported(const bool &isSupported)
1382 {
1383     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1384     if (isSupported == btOffloadSupported_) {
1385         return;
1386     }
1387     if (!isSupported) {
1388         btOffloadSupported_ = isSupported;
1389         AUDIO_INFO_LOG("btOffloadSupported_ off, device disconnect from %{public}d", deviceType_);
1390         return;
1391     }
1392 
1393     if (!spatializationEnabled_) {
1394         btOffloadSupported_ = isSupported;
1395         AUDIO_INFO_LOG("btOffloadSupported_ on, but spatialization is off, do nothing");
1396         return;
1397     }
1398     // Release ARM, try offload to DSP
1399     AUDIO_INFO_LOG("btOffloadSupported_ on, try offload effect on device %{public}d", deviceType_);
1400     AudioSpatializationState oldState = {spatializationEnabled_, headTrackingEnabled_};
1401     AudioSpatializationState offState = {false, false};
1402     UpdateSpatializationState(offState);
1403     btOffloadSupported_ = isSupported;
1404     UpdateSpatializationState(oldState);
1405     return;
1406 }
1407 
UpdateSceneTypeList(const std::string & sceneType,SceneTypeOperation operation)1408 void AudioEffectChainManager::UpdateSceneTypeList(const std::string &sceneType, SceneTypeOperation operation)
1409 {
1410     std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1411     if (operation == ADD_SCENE_TYPE) {
1412         auto it = std::find_if(sceneTypeCountList_.begin(), sceneTypeCountList_.end(),
1413             [sceneType](const std::pair<std::string, int32_t> &element) {
1414                 return element.first == sceneType;
1415             });
1416         if (it == sceneTypeCountList_.end()) {
1417             sceneTypeCountList_.push_back(std::make_pair(sceneType, 1));
1418             AUDIO_INFO_LOG("scene Type %{public}s is added", sceneType.c_str());
1419         } else {
1420             it->second++;
1421             AUDIO_INFO_LOG("scene Type %{public}s count is increased to %{public}d", sceneType.c_str(), it->second);
1422         }
1423     } else if (operation == REMOVE_SCENE_TYPE) {
1424         auto it = std::find_if(sceneTypeCountList_.begin(), sceneTypeCountList_.end(),
1425             [sceneType](const std::pair<std::string, int32_t> &element) {
1426                 return element.first == sceneType;
1427             });
1428         if (it == sceneTypeCountList_.end()) {
1429             AUDIO_WARNING_LOG("scene type %{public}s to be removed is not found", sceneType.c_str());
1430             return;
1431         }
1432         if (it->second <= 1) {
1433             sceneTypeCountList_.erase(it);
1434             AUDIO_INFO_LOG("scene Type %{public}s is removed", sceneType.c_str());
1435         } else {
1436             it->second--;
1437             AUDIO_INFO_LOG("scene Type %{public}s count is decreased to %{public}d", sceneType.c_str(), it->second);
1438         }
1439     } else {
1440         AUDIO_ERR_LOG("Wrong operation to SceneTypeToEffectChainCountBackupMap.");
1441     }
1442 }
1443 
FindMaxSessionID(uint32_t & maxSessionID,std::string & sceneType,const std::string & scenePairType,std::set<std::string> & sessions)1444 void AudioEffectChainManager::FindMaxSessionID(uint32_t &maxSessionID, std::string &sceneType,
1445     const std::string &scenePairType, std::set<std::string> &sessions)
1446 {
1447     for (auto &sessionID : sessions) {
1448         if (sessionIDToEffectInfoMap_[sessionID].sceneMode == "EFFECT_NONE") {
1449             continue;
1450         }
1451         uint32_t sessionIDInt = static_cast<uint32_t>(std::stoul(sessionID));
1452         if (sessionIDInt > maxSessionID) {
1453             maxSessionID = sessionIDInt;
1454             sceneType = scenePairType;
1455         }
1456     }
1457 }
1458 
UpdateCurrSceneTypeAndStreamUsageForDsp()1459 void AudioEffectChainManager::UpdateCurrSceneTypeAndStreamUsageForDsp()
1460 {
1461     AudioEffectScene currSceneType;
1462     std::string maxSession = std::to_string(maxSessionID_);
1463     UpdateCurrSceneType(currSceneType, maxSessionIDToSceneType_);
1464     SetHdiParam(currSceneType);
1465     if (sessionIDToEffectInfoMap_.count(maxSession)) {
1466         SessionEffectInfo info = sessionIDToEffectInfoMap_[maxSession];
1467         effectHdiInput_[0] = HDI_STREAM_USAGE;
1468         effectHdiInput_[1] = info.streamUsage;
1469         int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
1470         AUDIO_INFO_LOG("set hdi streamUsage: %{public}d", info.streamUsage);
1471         if (ret != SUCCESS) {
1472             AUDIO_WARNING_LOG("set hdi streamUsage failed");
1473         }
1474     }
1475 }
1476 } // namespace AudioStandard
1477 } // namespace OHOS
1478