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(
534 sessionIDToEffectInfoMap_[*s].systemVolumeType);
535 volumeMax = fmax((streamVolumeTemp * systemVolumeTemp), volumeMax);
536 }
537 }
538 if (static_cast<int32_t>(audioEffectVolume->GetDspVolume() * MAX_UINT_VOLUME_NUM) !=
539 static_cast<int32_t>(volumeMax * MAX_UINT_VOLUME_NUM)) {
540 audioEffectVolume->SetDspVolume(volumeMax);
541 effectHdiInput_[0] = HDI_VOLUME;
542 AUDIO_INFO_LOG("finalVolume change to %{public}f", volumeMax);
543 int32_t dspVolumeMax = static_cast<int32_t>(volumeMax * MAX_UINT_DSP_VOLUME);
544 int32_t ret = memcpy_s(&effectHdiInput_[1], SEND_HDI_COMMAND_LEN - sizeof(int8_t),
545 &dspVolumeMax, sizeof(int32_t));
546 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "memcpy volume failed");
547 AUDIO_INFO_LOG("set hdi volume: %{public}u", *(reinterpret_cast<uint32_t *>(&effectHdiInput_[1])));
548 ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
549 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "set hdi volume failed");
550 }
551 return SUCCESS;
552 }
553
EffectApVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)554 int32_t AudioEffectChainManager::EffectApVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)
555 {
556 AUDIO_INFO_LOG("send volume to ap.");
557 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
558 for (auto sessionId = sessionIDSet_.begin(); sessionId != sessionIDSet_.end(); ++sessionId) {
559 if (sessionIDToEffectInfoMap_.find(*sessionId) == sessionIDToEffectInfoMap_.end()) {
560 AUDIO_INFO_LOG("sessionID:%{public}s, no find in sessionIDToEffectInfoMap_", (*sessionId).c_str());
561 continue;
562 }
563 if (sessionIDToEffectInfoMap_[*sessionId].sceneMode == "EFFECT_NONE") {
564 AUDIO_INFO_LOG("sessionID:%{public}s, sceneMode is EFFECT_NONE, no send volume", (*sessionId).c_str());
565 continue;
566 }
567 std::string sceneTypeTemp = sessionIDToEffectInfoMap_[*sessionId].sceneType;
568 std::string sceneTypeAndDeviceKey = sceneTypeTemp + "_&_" + GetDeviceTypeName();
569 CHECK_AND_RETURN_RET_LOG(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) > 0 &&
570 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] != nullptr, ERROR, "null audioEffectChain");
571 auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
572 float streamVolumeTemp = audioEffectVolume->GetStreamVolume(*sessionId);
573 float systemVolumeTemp = audioEffectVolume->GetSystemVolume(
574 sessionIDToEffectInfoMap_[*sessionId].systemVolumeType);
575 float currVolumeTemp = audioEffectChain->GetCurrVolume();
576 float volumeMax = fmax((streamVolumeTemp * systemVolumeTemp), currVolumeTemp);
577 if (volumeMax > currVolumeTemp) {
578 audioEffectChain->SetCurrVolume(volumeMax);
579 }
580 audioEffectChain->SetFinalVolumeState(true);
581 }
582 return SendEffectApVolume(audioEffectVolume);
583 }
584
SendEffectApVolume(std::shared_ptr<AudioEffectVolume> audioEffectVolume)585 int32_t AudioEffectChainManager::SendEffectApVolume(std::shared_ptr<AudioEffectVolume> audioEffectVolume)
586 {
587 AUDIO_INFO_LOG("SendEffectApVolume");
588 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
589 for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
590 CHECK_AND_RETURN_RET_LOG(it->second != nullptr, ERROR, "null audioEffectChain");
591 auto audioEffectChain = it->second;
592 float volumeMax = audioEffectChain->GetCurrVolume();
593 if ((static_cast<int32_t>(audioEffectChain->GetFinalVolume() * MAX_UINT_VOLUME_NUM) !=
594 static_cast<int32_t>(volumeMax * MAX_UINT_VOLUME_NUM)) &&
595 audioEffectChain->GetFinalVolumeState() == true) {
596 audioEffectChain->SetFinalVolume(volumeMax);
597 int32_t ret = audioEffectChain->UpdateEffectParam();
598 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "set ap volume failed");
599 AUDIO_INFO_LOG("The delay of SceneType %{public}s is %{public}u, finalVolume changed to %{public}f",
600 it->first.c_str(), audioEffectChain->GetLatency(), volumeMax);
601 audioEffectChain->SetFinalVolumeState(false);
602 }
603 }
604 for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
605 CHECK_AND_RETURN_RET_LOG(it->second != nullptr, ERROR, "null audioEffectChain");
606 auto audioEffectChain = it->second;
607 float volume = 0.0f;
608 audioEffectChain->SetCurrVolume(volume);
609 }
610 return SUCCESS;
611 }
612
EffectVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)613 int32_t AudioEffectChainManager::EffectVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)
614 {
615 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
616 int32_t ret;
617 if (((deviceType_ == DEVICE_TYPE_SPEAKER) && (spkOffloadEnabled_)) ||
618 ((deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) && (btOffloadEnabled_))) {
619 ret = EffectDspVolumeUpdate(audioEffectVolume);
620 } else {
621 ret = EffectApVolumeUpdate(audioEffectVolume);
622 }
623 return ret;
624 }
625
StreamVolumeUpdate(const std::string sessionIDString,const float streamVolume)626 int32_t AudioEffectChainManager::StreamVolumeUpdate(const std::string sessionIDString, const float streamVolume)
627 {
628 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
629 // update streamVolume
630 std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
631 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
632 audioEffectVolume->SetStreamVolume(sessionIDString, streamVolume);
633 int32_t ret;
634 AUDIO_INFO_LOG("streamVolume is %{public}f", audioEffectVolume->GetStreamVolume(sessionIDString));
635 ret = EffectVolumeUpdate(audioEffectVolume);
636 return ret;
637 }
638
SetEffectSystemVolume(const int32_t systemVolumeType,const float systemVolume)639 int32_t AudioEffectChainManager::SetEffectSystemVolume(const int32_t systemVolumeType, const float systemVolume)
640 {
641 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
642 // set systemVolume by systemVolumeType
643 std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
644 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
645 audioEffectVolume->SetSystemVolume(systemVolumeType, systemVolume);
646 AUDIO_INFO_LOG("systemVolumeType: %{punlic}d, systemVolume: %{public}f", systemVolumeType,
647 audioEffectVolume->GetSystemVolume(systemVolumeType));
648
649 return SUCCESS;
650 }
651
652 #ifdef WINDOW_MANAGER_ENABLE
EffectDspRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,const uint32_t rotationState)653 int32_t AudioEffectChainManager::EffectDspRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,
654 const uint32_t rotationState)
655 {
656 // send rotation to dsp
657 AUDIO_DEBUG_LOG("send rotation to dsp.");
658 CHECK_AND_RETURN_RET_LOG(audioEffectRotation != nullptr, ERROR, "null audioEffectRotation");
659 AUDIO_DEBUG_LOG("rotationState change, new state: %{public}d, previous state: %{public}d",
660 rotationState, audioEffectRotation->GetRotation());
661 effectHdiInput_[0] = HDI_ROTATION;
662 effectHdiInput_[1] = rotationState;
663 AUDIO_INFO_LOG("set hdi rotation: %{public}d", effectHdiInput_[1]);
664 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
665 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "set hdi rotation failed");
666
667 return SUCCESS;
668 }
669
EffectApRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,const uint32_t rotationState)670 int32_t AudioEffectChainManager::EffectApRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,
671 const uint32_t rotationState)
672 {
673 // send rotation to ap
674 AUDIO_DEBUG_LOG("send rotation to ap.");
675 CHECK_AND_RETURN_RET_LOG(audioEffectRotation != nullptr, ERROR, "null audioEffectRotation");
676 AUDIO_DEBUG_LOG("rotationState change, new state: %{public}d, previous state: %{public}d",
677 rotationState, audioEffectRotation->GetRotation());
678 for (auto it = sceneTypeToSessionIDMap_.begin(); it != sceneTypeToSessionIDMap_.end(); it++) {
679 std::string sceneTypeAndDeviceKey = it->first + "_&_" + GetDeviceTypeName();
680 if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
681 return ERROR;
682 }
683 auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
684 if (audioEffectChain == nullptr) {
685 return ERROR;
686 }
687 int32_t ret = audioEffectChain->UpdateEffectParam();
688 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "set ap rotation failed");
689 AUDIO_INFO_LOG("The delay of SceneType %{public}s is %{public}u, rotation changed to %{public}u",
690 it->first.c_str(), audioEffectChain->GetLatency(), rotationState);
691 }
692
693 return SUCCESS;
694 }
695
EffectRotationUpdate(const uint32_t rotationState)696 int32_t AudioEffectChainManager::EffectRotationUpdate(const uint32_t rotationState)
697 {
698 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
699 std::shared_ptr<AudioEffectRotation> audioEffectRotation = AudioEffectRotation::GetInstance();
700 AUDIO_INFO_LOG("rotation update to %{public}u", rotationState);
701 if (audioEffectRotation->GetRotation() != rotationState) {
702 audioEffectRotation->SetRotation(rotationState);
703 EffectDspRotationUpdate(audioEffectRotation, rotationState);
704 EffectApRotationUpdate(audioEffectRotation, rotationState);
705 }
706
707 return SUCCESS;
708 }
709 #endif
710
UpdateMultichannelConfig(const std::string & sceneType)711 int32_t AudioEffectChainManager::UpdateMultichannelConfig(const std::string &sceneType)
712 {
713 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
714 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
715 if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
716 return ERROR;
717 }
718 uint32_t inputChannels = DEFAULT_NUM_CHANNEL;
719 uint64_t inputChannelLayout = DEFAULT_NUM_CHANNELLAYOUT;
720 ReturnEffectChannelInfo(sceneType, inputChannels, inputChannelLayout);
721
722 auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
723 if (audioEffectChain == nullptr) {
724 return ERROR;
725 }
726 audioEffectChain->UpdateMultichannelIoBufferConfig(inputChannels, inputChannelLayout);
727 return SUCCESS;
728 }
729
InitAudioEffectChainDynamic(const std::string & sceneType)730 int32_t AudioEffectChainManager::InitAudioEffectChainDynamic(const std::string &sceneType)
731 {
732 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
733 return InitAudioEffectChainDynamicInner(sceneType);
734 }
735
InitAudioEffectChainDynamicInner(const std::string & sceneType)736 int32_t AudioEffectChainManager::InitAudioEffectChainDynamicInner(const std::string &sceneType)
737 {
738 CHECK_AND_RETURN_RET_LOG(isInitialized_, ERROR, "has not been initialized");
739 CHECK_AND_RETURN_RET_LOG(sceneType != "", ERROR, "null sceneType");
740
741 std::shared_ptr<AudioEffectChain> audioEffectChain = nullptr;
742 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
743 if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
744 return SUCCESS;
745 } else {
746 audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
747 }
748 if (audioEffectChain != nullptr) {
749 audioEffectChain->InitEffectChain();
750 AUDIO_INFO_LOG("init effect buffer");
751 }
752
753 return SUCCESS;
754 }
755
UpdateSpatializationState(AudioSpatializationState spatializationState)756 int32_t AudioEffectChainManager::UpdateSpatializationState(AudioSpatializationState spatializationState)
757 {
758 AUDIO_INFO_LOG("UpdateSpatializationState entered, current state: %{public}d and %{public}d, previous state: \
759 %{public}d and %{public}d", spatializationState.spatializationEnabled, spatializationState.headTrackingEnabled,
760 spatializationEnabled_.load(), headTrackingEnabled_);
761 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
762 if (spatializationEnabled_ != spatializationState.spatializationEnabled) {
763 UpdateSpatializationEnabled(spatializationState);
764 }
765 if (headTrackingEnabled_ != spatializationState.headTrackingEnabled) {
766 headTrackingEnabled_ = spatializationState.headTrackingEnabled;
767 UpdateSensorState();
768 }
769
770 std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
771 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
772 EffectVolumeUpdate(audioEffectVolume);
773 AUDIO_INFO_LOG("systemVolume prepare change or no change");
774
775 return SUCCESS;
776 }
777
UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType)778 int32_t AudioEffectChainManager::UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType)
779 {
780 int32_t ret{ SUCCESS };
781 spatialDeviceType_ = spatialDeviceType;
782
783 effectHdiInput_[0] = HDI_UPDATE_SPATIAL_DEVICE_TYPE;
784 effectHdiInput_[1] = spatialDeviceType_;
785 AUDIO_INFO_LOG("set hdi spatialDeviceType: %{public}d", effectHdiInput_[1]);
786 ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
787 if (ret != SUCCESS) {
788 AUDIO_WARNING_LOG("set hdi update spatial device type failed");
789 }
790
791 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
792 for (const auto& sceneType2EffectChain : sceneTypeToEffectChainMap_) {
793 auto audioEffectChain = sceneType2EffectChain.second;
794 if (audioEffectChain != nullptr) {
795 audioEffectChain->SetSpatialDeviceType(spatialDeviceType_);
796 ret = audioEffectChain->UpdateEffectParam();
797 CHECK_AND_CONTINUE_LOG(ret == SUCCESS, "UpdateEffectParam failed.");
798 }
799 }
800
801 return SUCCESS;
802 }
803
ReturnEffectChannelInfo(const std::string & sceneType,uint32_t & channels,uint64_t & channelLayout)804 int32_t AudioEffectChainManager::ReturnEffectChannelInfo(const std::string &sceneType, uint32_t &channels,
805 uint64_t &channelLayout)
806 {
807 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
808 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
809 if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
810 return ERROR;
811 }
812 for (auto& scenePair : sceneTypeToSessionIDMap_) {
813 std::string pairSceneTypeAndDeviceKey = scenePair.first + "_&_" + GetDeviceTypeName();
814 if (sceneTypeToEffectChainMap_.count(pairSceneTypeAndDeviceKey) > 0 &&
815 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] ==
816 sceneTypeToEffectChainMap_[pairSceneTypeAndDeviceKey]) {
817 std::set<std::string> sessions = scenePair.second;
818 FindMaxEffectChannels(scenePair.first, sessions, channels, channelLayout);
819 }
820 }
821 return SUCCESS;
822 }
823
ReturnMultiChannelInfo(uint32_t * channels,uint64_t * channelLayout)824 int32_t AudioEffectChainManager::ReturnMultiChannelInfo(uint32_t *channels, uint64_t *channelLayout)
825 {
826 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
827 uint32_t tmpChannelCount = DEFAULT_NUM_CHANNEL;
828 uint64_t tmpChannelLayout = DEFAULT_NUM_CHANNELLAYOUT;
829 bool channelUpdateFlag = false;
830 for (auto it = sceneTypeToSessionIDMap_.begin(); it != sceneTypeToSessionIDMap_.end(); it++) {
831 std::set<std::string> sessions = sceneTypeToSessionIDMap_[it->first];
832 for (auto s = sessions.begin(); s != sessions.end(); ++s) {
833 SessionEffectInfo info = sessionIDToEffectInfoMap_[*s];
834 if (info.channels > tmpChannelCount &&
835 info.channels <= DSP_MAX_NUM_CHANNEL &&
836 !ExistAudioEffectChain(it->first, info.sceneMode, info.spatializationEnabled) &&
837 IsChannelLayoutSupported(info.channelLayout)) {
838 tmpChannelCount = info.channels;
839 tmpChannelLayout = info.channelLayout;
840 channelUpdateFlag = true;
841 }
842 }
843 }
844 if (channelUpdateFlag) {
845 *channels = tmpChannelCount;
846 *channelLayout = tmpChannelLayout;
847 }
848 return SUCCESS;
849 }
850
SessionInfoMapAdd(const std::string & sessionID,const SessionEffectInfo & info)851 int32_t AudioEffectChainManager::SessionInfoMapAdd(const std::string &sessionID, const SessionEffectInfo &info)
852 {
853 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
854 CHECK_AND_RETURN_RET_LOG(sessionID != "", ERROR, "null sessionID");
855 if (!sessionIDToEffectInfoMap_.count(sessionID)) {
856 sceneTypeToSessionIDMap_[info.sceneType].insert(sessionID);
857 sessionIDToEffectInfoMap_[sessionID] = info;
858 } else if (sessionIDToEffectInfoMap_[sessionID].sceneMode != info.sceneMode ||
859 sessionIDToEffectInfoMap_[sessionID].spatializationEnabled != info.spatializationEnabled) {
860 sessionIDToEffectInfoMap_[sessionID] = info;
861 } else {
862 return ERROR;
863 }
864 return SUCCESS;
865 }
866
SessionInfoMapDelete(const std::string & sceneType,const std::string & sessionID)867 int32_t AudioEffectChainManager::SessionInfoMapDelete(const std::string &sceneType, const std::string &sessionID)
868 {
869 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
870 if (!sceneTypeToSessionIDMap_.count(sceneType)) {
871 return ERROR;
872 }
873 if (sceneTypeToSessionIDMap_[sceneType].erase(sessionID)) {
874 if (sceneTypeToSessionIDMap_[sceneType].empty()) {
875 sceneTypeToSessionIDMap_.erase(sceneType);
876 }
877 } else {
878 return ERROR;
879 }
880 if (!sessionIDToEffectInfoMap_.erase(sessionID)) {
881 return ERROR;
882 }
883 return SUCCESS;
884 }
885
SetHdiParam(const AudioEffectScene & sceneType)886 int32_t AudioEffectChainManager::SetHdiParam(const AudioEffectScene &sceneType)
887 {
888 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
889 if (!isInitialized_) {
890 if (initializedLogFlag_) {
891 AUDIO_ERR_LOG("audioEffectChainManager has not been initialized");
892 initializedLogFlag_ = false;
893 }
894 return ERROR;
895 }
896 memset_s(static_cast<void *>(effectHdiInput_), sizeof(effectHdiInput_), 0, sizeof(effectHdiInput_));
897
898 effectHdiInput_[0] = HDI_ROOM_MODE;
899 effectHdiInput_[1] = sceneType;
900 AUDIO_PRERELEASE_LOGI("set hdi room mode sceneType: %{public}d", effectHdiInput_[1]);
901 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
902 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR, "set hdi room mode failed, ret is %{public}d", ret);
903 return SUCCESS;
904 }
905
UpdateSensorState()906 void AudioEffectChainManager::UpdateSensorState()
907 {
908 effectHdiInput_[0] = HDI_HEAD_MODE;
909 effectHdiInput_[1] = headTrackingEnabled_ == true ? 1 : 0;
910 AUDIO_INFO_LOG("set hdi head mode: %{public}d", effectHdiInput_[1]);
911 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
912 if (ret != SUCCESS) {
913 AUDIO_WARNING_LOG("set hdi head mode failed");
914 }
915
916 if (headTrackingEnabled_) {
917 #ifdef SENSOR_ENABLE
918 if (btOffloadEnabled_) {
919 headTracker_->SensorInit();
920 ret = headTracker_->SensorSetConfig(DSP_SPATIALIZER_ENGINE);
921 } else {
922 headTracker_->SensorInit();
923 ret = headTracker_->SensorSetConfig(ARM_SPATIALIZER_ENGINE);
924 }
925
926 if (ret != SUCCESS) {
927 AUDIO_ERR_LOG("SensorSetConfig error!");
928 }
929
930 if (headTracker_->SensorActive() != SUCCESS) {
931 AUDIO_ERR_LOG("SensorActive failed");
932 }
933 #endif
934 return;
935 }
936
937 if (btOffloadEnabled_) {
938 return;
939 }
940
941 #ifdef SENSOR_ENABLE
942 if (headTracker_->SensorDeactive() != SUCCESS) {
943 AUDIO_ERR_LOG("SensorDeactive failed");
944 }
945 headTracker_->SensorUnsubscribe();
946 HeadPostureData headPostureData = {1, 1.0, 0.0, 0.0, 0.0}; // ori head posturedata
947 headTracker_->SetHeadPostureData(headPostureData);
948 for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
949 auto audioEffectChain = it->second;
950 if (audioEffectChain == nullptr) {
951 continue;
952 }
953 audioEffectChain->SetHeadTrackingDisabled();
954 }
955 #endif
956 }
957
DeleteAllChains()958 void AudioEffectChainManager::DeleteAllChains()
959 {
960 std::map<std::string, int32_t> sceneTypeToEffectChainCountBackupMap;
961 for (auto it = sceneTypeToEffectChainCountMap_.begin(); it != sceneTypeToEffectChainCountMap_.end(); ++it) {
962 AUDIO_DEBUG_LOG("sceneTypeAndDeviceKey %{public}s count:%{public}d", it->first.c_str(), it->second);
963 sceneTypeToEffectChainCountBackupMap.insert(
964 std::make_pair(it->first.substr(0, static_cast<size_t>(it->first.find("_&_"))), it->second));
965 }
966
967 for (auto it = sceneTypeToEffectChainCountBackupMap.begin(); it != sceneTypeToEffectChainCountBackupMap.end();
968 ++it) {
969 for (int32_t k = 0; k < it->second; ++k) {
970 ReleaseAudioEffectChainDynamic(it->first);
971 }
972 }
973 return;
974 }
975
RecoverAllChains()976 void AudioEffectChainManager::RecoverAllChains()
977 {
978 for (auto item : sceneTypeCountList_) {
979 AUDIO_DEBUG_LOG("sceneType %{public}s count:%{public}d", item.first.c_str(), item.second);
980 for (int32_t k = 0; k < item.second; ++k) {
981 CreateAudioEffectChainDynamic(item.first);
982 }
983 UpdateMultichannelConfig(item.first);
984 }
985 }
986
GetLatency(const std::string & sessionId)987 uint32_t AudioEffectChainManager::GetLatency(const std::string &sessionId)
988 {
989 if (((deviceType_ == DEVICE_TYPE_SPEAKER) && (spkOffloadEnabled_)) ||
990 ((deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) && (btOffloadEnabled_))) {
991 AUDIO_DEBUG_LOG("offload enabled, return 0");
992 return 0;
993 }
994 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
995 CHECK_AND_RETURN_RET(sessionIDToEffectInfoMap_.count(sessionId), 0);
996 if (sessionIDToEffectInfoMap_[sessionId].sceneMode == "" ||
997 sessionIDToEffectInfoMap_[sessionId].sceneMode == "None") {
998 AUDIO_DEBUG_LOG("seceneMode is None, return 0");
999 return 0;
1000 }
1001 if (sessionIDToEffectInfoMap_[sessionId].spatializationEnabled == "0" &&
1002 GetDeviceTypeName() == "DEVICE_TYPE_BLUETOOTH_A2DP") {
1003 return 0;
1004 }
1005 std::string sceneTypeAndDeviceKey = sessionIDToEffectInfoMap_[sessionId].sceneType + "_&_" + GetDeviceTypeName();
1006 CHECK_AND_RETURN_RET(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) &&
1007 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] != nullptr, 0);
1008 return sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey]->GetLatency();
1009 }
1010
SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType)1011 int32_t AudioEffectChainManager::SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType)
1012 {
1013 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1014 AUDIO_INFO_LOG("spatialization scene type is set to be %{public}d", spatializationSceneType);
1015 spatializationSceneType_ = spatializationSceneType;
1016
1017 if (!spatializationEnabled_ || (GetDeviceTypeName() != "DEVICE_TYPE_BLUETOOTH_A2DP")) {
1018 return SUCCESS;
1019 }
1020
1021 effectHdiInput_[0] = HDI_SPATIALIZATION_SCENE_TYPE;
1022 effectHdiInput_[1] = static_cast<int32_t>(spatializationSceneType_);
1023 if (audioEffectHdiParam_->UpdateHdiState(effectHdiInput_) != SUCCESS) {
1024 AUDIO_WARNING_LOG("set hdi spatialization scene type failed");
1025 }
1026
1027 SetSpatializationSceneTypeToChains();
1028
1029 return SUCCESS;
1030 }
1031
UpdateExtraSceneType(const std::string & mainkey,const std::string & subkey,const std::string & extraSceneType)1032 void AudioEffectChainManager::UpdateExtraSceneType(const std::string &mainkey, const std::string &subkey,
1033 const std::string &extraSceneType)
1034 {
1035 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1036 if (mainkey == "audio_effect" && subkey == "update_audio_effect_type") {
1037 AUDIO_INFO_LOG("Set scene type: %{public}s to hdi", extraSceneType.c_str());
1038 int32_t ret{ SUCCESS };
1039 effectHdiInput_[0] = HDI_EXTRA_SCENE_TYPE;
1040 effectHdiInput_[1] = static_cast<int32_t>(std::stoi(extraSceneType));
1041 ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_SPEAKER);
1042 if (ret != SUCCESS) {
1043 AUDIO_WARNING_LOG("set hdi update rss scene type failed");
1044 }
1045 AUDIO_INFO_LOG("Set scene type: %{public}s to arm", extraSceneType.c_str());
1046 extraSceneType_ = extraSceneType;
1047 for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
1048 auto audioEffectChain = it->second;
1049 if (audioEffectChain == nullptr) {
1050 continue;
1051 }
1052 audioEffectChain->SetExtraSceneType(extraSceneType);
1053 if (audioEffectChain->UpdateEffectParam() != SUCCESS) {
1054 AUDIO_WARNING_LOG("Update scene type to effect chain failed");
1055 continue;
1056 }
1057 }
1058 } else {
1059 AUDIO_INFO_LOG("UpdateExtraSceneType failed, mainkey is %{public}s, subkey is %{public}s, "
1060 "extraSceneType is %{public}s", mainkey.c_str(), subkey.c_str(), extraSceneType.c_str());
1061 return;
1062 }
1063 }
1064
SetSpatializationSceneTypeToChains()1065 void AudioEffectChainManager::SetSpatializationSceneTypeToChains()
1066 {
1067 for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
1068 auto audioEffectChain = it->second;
1069 if (audioEffectChain == nullptr) {
1070 continue;
1071 }
1072 audioEffectChain->SetSpatializationSceneType(spatializationSceneType_);
1073 if (audioEffectChain->UpdateEffectParam() != SUCCESS) {
1074 AUDIO_WARNING_LOG("Update param to effect chain failed");
1075 continue;
1076 }
1077 }
1078 }
1079
SetSpatializationEnabledToChains()1080 void AudioEffectChainManager::SetSpatializationEnabledToChains()
1081 {
1082 for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
1083 auto audioEffectChain = it->second;
1084 if (audioEffectChain == nullptr) {
1085 continue;
1086 }
1087 audioEffectChain->SetSpatializationEnabled(spatializationEnabled_);
1088 if (audioEffectChain->UpdateEffectParam() != SUCCESS) {
1089 AUDIO_WARNING_LOG("Update param to effect chain failed");
1090 continue;
1091 }
1092 }
1093 }
1094
GetCurSpatializationEnabled()1095 bool AudioEffectChainManager::GetCurSpatializationEnabled()
1096 {
1097 return spatializationEnabled_;
1098 }
1099
ResetEffectBuffer()1100 void AudioEffectChainManager::ResetEffectBuffer()
1101 {
1102 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1103 for (const auto &[sceneType, effectChain] : sceneTypeToEffectChainMap_) {
1104 if (effectChain == nullptr) continue;
1105 effectChain->InitEffectChain();
1106 }
1107 }
1108
ResetInfo()1109 void AudioEffectChainManager::ResetInfo()
1110 {
1111 effectToLibraryEntryMap_.clear();
1112 effectToLibraryNameMap_.clear();
1113 effectChainToEffectsMap_.clear();
1114 sceneTypeAndModeToEffectChainNameMap_.clear();
1115 sceneTypeToEffectChainMap_.clear();
1116 sceneTypeToEffectChainCountMap_.clear();
1117 sessionIDSet_.clear();
1118 sceneTypeToSessionIDMap_.clear();
1119 sessionIDToEffectInfoMap_.clear();
1120 sceneTypeToSpecialEffectSet_.clear();
1121 deviceType_ = DEVICE_TYPE_SPEAKER;
1122 deviceSink_ = DEFAULT_DEVICE_SINK;
1123 isInitialized_ = false;
1124 spatializationEnabled_ = false;
1125 headTrackingEnabled_ = false;
1126 btOffloadEnabled_ = false;
1127 spkOffloadEnabled_ = false;
1128 initializedLogFlag_ = true;
1129 spatializationSceneType_ = SPATIALIZATION_SCENE_TYPE_DEFAULT;
1130 isDefaultEffectChainExisted_ = false;
1131 }
1132
UpdateDefaultAudioEffect()1133 void AudioEffectChainManager::UpdateDefaultAudioEffect()
1134 {
1135 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1136 // for default scene type
1137 uint32_t maxDefaultSessionID = 0;
1138 uint32_t maxSessionID = 0;
1139 for (auto& scenePair : sceneTypeToSessionIDMap_) {
1140 std::set<std::string> &sessions = scenePair.second;
1141 if (!sceneTypeToSpecialEffectSet_.count(scenePair.first) &&
1142 std::find(priorSceneList_.begin(), priorSceneList_.end(),
1143 scenePair.first) == priorSceneList_.end()) {
1144 FindMaxSessionID(maxDefaultSessionID, maxDefaultSessionIDToSceneType_, scenePair.first, sessions);
1145 }
1146 FindMaxSessionID(maxSessionID, maxSessionIDToSceneType_, scenePair.first, sessions);
1147 }
1148 maxSessionID_ = maxSessionID;
1149 AUDIO_INFO_LOG("newest stream, maxDefaultSessionID: %{public}u, sceneType: %{public}s,"
1150 "maxSessionID: %{public}u, sceneType: %{public}s",
1151 maxDefaultSessionID, maxDefaultSessionIDToSceneType_.c_str(),
1152 maxSessionID_, maxSessionIDToSceneType_.c_str());
1153
1154 std::string key = maxDefaultSessionIDToSceneType_ + "_&_" + GetDeviceTypeName();
1155 std::string maxDefaultSession = std::to_string(maxDefaultSessionID);
1156 AudioEffectScene currDefaultSceneType;
1157 UpdateCurrSceneType(currDefaultSceneType, maxDefaultSessionIDToSceneType_);
1158 if (!maxDefaultSessionIDToSceneType_.empty() && sessionIDToEffectInfoMap_.count(maxDefaultSession) &&
1159 sceneTypeToEffectChainMap_.count(key) && sceneTypeToEffectChainMap_[key] != nullptr) {
1160 SessionEffectInfo info = sessionIDToEffectInfoMap_[maxDefaultSession];
1161 std::shared_ptr<AudioEffectChain> audioEffectChain = sceneTypeToEffectChainMap_[key];
1162 audioEffectChain->SetEffectCurrSceneType(currDefaultSceneType);
1163 audioEffectChain->SetStreamUsage(info.streamUsage);
1164 audioEffectChain->UpdateEffectParam();
1165 }
1166 }
1167
UpdateStreamUsage()1168 void AudioEffectChainManager::UpdateStreamUsage()
1169 {
1170 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1171 // for special scene type
1172 for (auto& specialSceneType : sceneTypeToSpecialEffectSet_) {
1173 uint32_t maxSpecialSessionID = 0;
1174 std::string maxSpecialSceneType = "";
1175 auto it = sceneTypeToSessionIDMap_.find(specialSceneType);
1176 if (it != sceneTypeToSessionIDMap_.end()) {
1177 std::set<std::string> &sessions = it->second;
1178 FindMaxSessionID(maxSpecialSessionID, maxSpecialSceneType, specialSceneType, sessions);
1179 }
1180 std::string maxSpecialSession = std::to_string(maxSpecialSessionID);
1181 std::string key = maxSpecialSceneType + "_&_" + GetDeviceTypeName();
1182 if (!maxSpecialSceneType.empty() && sessionIDToEffectInfoMap_.count(maxSpecialSession) &&
1183 sceneTypeToEffectChainMap_.count(key) && sceneTypeToEffectChainMap_[key] != nullptr) {
1184 SessionEffectInfo info = sessionIDToEffectInfoMap_[maxSpecialSession];
1185 std::shared_ptr<AudioEffectChain> audioEffectChain = sceneTypeToEffectChainMap_[key];
1186 audioEffectChain->SetStreamUsage(info.streamUsage);
1187 audioEffectChain->UpdateEffectParam();
1188 }
1189 AUDIO_INFO_LOG("newest stream, maxSpecialSessionID: %{public}u, sceneType: %{public}s",
1190 maxSpecialSessionID, maxSpecialSceneType.c_str());
1191 }
1192 // for prior scene type
1193 for (auto& priorSceneType : priorSceneList_) {
1194 uint32_t maxPriorSessionID = 0;
1195 std::string maxPriorSceneType = "";
1196 auto it = sceneTypeToSessionIDMap_.find(priorSceneType);
1197 if (it != sceneTypeToSessionIDMap_.end()) {
1198 std::set<std::string> &sessions = it->second;
1199 FindMaxSessionID(maxPriorSessionID, maxPriorSceneType, priorSceneType, sessions);
1200 }
1201 std::string key = maxPriorSceneType + "_&_" + GetDeviceTypeName();
1202 std::string maxPriorSession = std::to_string(maxPriorSessionID);
1203 if (!maxPriorSceneType.empty() && sessionIDToEffectInfoMap_.count(maxPriorSession) &&
1204 sceneTypeToEffectChainMap_.count(key) && sceneTypeToEffectChainMap_[key] != nullptr) {
1205 SessionEffectInfo info = sessionIDToEffectInfoMap_[maxPriorSession];
1206 std::shared_ptr<AudioEffectChain> audioEffectChain = sceneTypeToEffectChainMap_[key];
1207 audioEffectChain->SetStreamUsage(info.streamUsage);
1208 audioEffectChain->UpdateEffectParam();
1209 }
1210 AUDIO_INFO_LOG("newest stream, maxSpecialSessionID: %{public}u, sceneType: %{public}s",
1211 maxPriorSessionID, maxPriorSceneType.c_str());
1212 }
1213 // update dsp scene type and stream usage
1214 UpdateCurrSceneTypeAndStreamUsageForDsp();
1215 }
1216
CheckSceneTypeMatch(const std::string & sinkSceneType,const std::string & sceneType)1217 bool AudioEffectChainManager::CheckSceneTypeMatch(const std::string &sinkSceneType, const std::string &sceneType)
1218 {
1219 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1220 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1221 std::string sinkSceneTypeAndDeviceKey = sinkSceneType + "_&_" + GetDeviceTypeName();
1222 std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1223 if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) ||
1224 !sceneTypeToEffectChainMap_.count(sinkSceneTypeAndDeviceKey)) {
1225 return false;
1226 }
1227 if (sceneType == sinkSceneType && (sceneTypeToSpecialEffectSet_.count(sinkSceneType) ||
1228 std::find(priorSceneList_.begin(), priorSceneList_.end(), sceneType) != priorSceneList_.end())) {
1229 return true;
1230 }
1231 if (sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] ==
1232 sceneTypeToEffectChainMap_[sinkSceneTypeAndDeviceKey]) {
1233 return sceneTypeAndDeviceKey == defaultSceneTypeAndDeviceKey;
1234 }
1235 return false;
1236 }
1237
UpdateCurrSceneType(AudioEffectScene & currSceneType,std::string & sceneType)1238 void AudioEffectChainManager::UpdateCurrSceneType(AudioEffectScene &currSceneType, std::string &sceneType)
1239 {
1240 currSceneType = static_cast<AudioEffectScene>(GetKeyFromValue(AUDIO_SUPPORTED_SCENE_TYPES, sceneType));
1241 }
1242
FindMaxEffectChannels(const std::string & sceneType,const std::set<std::string> & sessions,uint32_t & channels,uint64_t & channelLayout)1243 void AudioEffectChainManager::FindMaxEffectChannels(const std::string &sceneType,
1244 const std::set<std::string> &sessions, uint32_t &channels, uint64_t &channelLayout)
1245 {
1246 for (auto s = sessions.begin(); s != sessions.end(); ++s) {
1247 SessionEffectInfo info = sessionIDToEffectInfoMap_[*s];
1248 uint32_t tmpChannelCount;
1249 uint64_t tmpChannelLayout;
1250 std::string deviceType = GetDeviceTypeName();
1251 if (((deviceType == "DEVICE_TYPE_BLUETOOTH_A2DP") || (deviceType == "DEVICE_TYPE_SPEAKER"))
1252 && ExistAudioEffectChain(sceneType, info.sceneMode, info.spatializationEnabled)
1253 && IsChannelLayoutSupported(info.channelLayout)) {
1254 tmpChannelLayout = info.channelLayout;
1255 tmpChannelCount = info.channels;
1256 } else {
1257 tmpChannelCount = DEFAULT_NUM_CHANNEL;
1258 tmpChannelLayout = DEFAULT_NUM_CHANNELLAYOUT;
1259 }
1260
1261 if (tmpChannelCount >= channels) {
1262 channels = tmpChannelCount;
1263 channelLayout = tmpChannelLayout;
1264 }
1265 }
1266 }
1267
CreateAudioEffectChain(const std::string & sceneType,bool isPriorScene)1268 std::shared_ptr<AudioEffectChain> AudioEffectChainManager::CreateAudioEffectChain(const std::string &sceneType,
1269 bool isPriorScene)
1270 {
1271 std::shared_ptr<AudioEffectChain> audioEffectChain = nullptr;
1272 std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1273
1274 if (isPriorScene) {
1275 AUDIO_INFO_LOG("create prior effect chain: %{public}s", sceneType.c_str());
1276 #ifdef SENSOR_ENABLE
1277 audioEffectChain = std::make_shared<AudioEffectChain>(sceneType, headTracker_);
1278 #else
1279 audioEffectChain = std::make_shared<AudioEffectChain>(sceneType);
1280 #endif
1281 return audioEffectChain;
1282 }
1283 if ((maxEffectChainCount_ - static_cast<int32_t>(sceneTypeToSpecialEffectSet_.size())) > 1) {
1284 AUDIO_INFO_LOG("max audio effect chain count not reached, create special effect chain: %{public}s",
1285 sceneType.c_str());
1286 sceneTypeToSpecialEffectSet_.insert(sceneType);
1287 #ifdef SENSOR_ENABLE
1288 audioEffectChain = std::make_shared<AudioEffectChain>(sceneType, headTracker_);
1289 #else
1290 audioEffectChain = std::make_shared<AudioEffectChain>(sceneType);
1291 #endif
1292 } else {
1293 if (!isDefaultEffectChainExisted_) {
1294 AUDIO_INFO_LOG("max audio effect chain count reached, create current and default effect chain");
1295 #ifdef SENSOR_ENABLE
1296 audioEffectChain = std::make_shared<AudioEffectChain>(DEFAULT_SCENE_TYPE, headTracker_);
1297 #else
1298 audioEffectChain = std::make_shared<AudioEffectChain>(DEFAULT_SCENE_TYPE);
1299 #endif
1300 sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey] = audioEffectChain;
1301 defaultEffectChainCount_ = 1;
1302 isDefaultEffectChainExisted_ = true;
1303 } else {
1304 audioEffectChain = sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey];
1305 defaultEffectChainCount_++;
1306 AUDIO_INFO_LOG("max audio effect chain count reached and default effect chain already exist: %{public}d",
1307 defaultEffectChainCount_);
1308 }
1309 }
1310 return audioEffectChain;
1311 }
1312
CheckAndReleaseCommonEffectChain(const std::string & sceneType)1313 void AudioEffectChainManager::CheckAndReleaseCommonEffectChain(const std::string &sceneType)
1314 {
1315 AUDIO_INFO_LOG("release effect chain for scene type: %{public}s", sceneType.c_str());
1316 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1317 std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1318 if (!isDefaultEffectChainExisted_) {
1319 return;
1320 }
1321 if (sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey] == sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey]) {
1322 if (defaultEffectChainCount_ <= 1) {
1323 sceneTypeToEffectChainMap_.erase(defaultSceneTypeAndDeviceKey);
1324 defaultEffectChainCount_= 0;
1325 isDefaultEffectChainExisted_ = false;
1326 AUDIO_INFO_LOG("default effect chain is released");
1327 } else {
1328 defaultEffectChainCount_--;
1329 AUDIO_INFO_LOG("default effect chain still exist, count is %{public}d", defaultEffectChainCount_);
1330 }
1331 }
1332 }
1333
UpdateSpatializationEnabled(AudioSpatializationState spatializationState)1334 void AudioEffectChainManager::UpdateSpatializationEnabled(AudioSpatializationState spatializationState)
1335 {
1336 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1337 spatializationEnabled_ = spatializationState.spatializationEnabled;
1338
1339 memset_s(static_cast<void *>(effectHdiInput_), sizeof(effectHdiInput_), 0, sizeof(effectHdiInput_));
1340 if (spatializationEnabled_) {
1341 if ((deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) && (!btOffloadSupported_)) {
1342 AUDIO_INFO_LOG("A2dp-hal, enter ARM processing");
1343 btOffloadEnabled_ = false;
1344 RecoverAllChains();
1345 SetSpatializationEnabledToChains();
1346 return;
1347 }
1348 effectHdiInput_[0] = HDI_INIT;
1349 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
1350 if (ret != SUCCESS) {
1351 AUDIO_ERR_LOG("set hdi init failed, enter route of escape in ARM");
1352 btOffloadEnabled_ = false;
1353 RecoverAllChains();
1354 } else {
1355 AUDIO_INFO_LOG("set hdi init succeeded, normal spatialization entered");
1356 btOffloadEnabled_ = true;
1357 }
1358 } else {
1359 effectHdiInput_[0] = HDI_DESTROY;
1360 AUDIO_INFO_LOG("set hdi destroy.");
1361 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
1362 if (ret != SUCCESS) {
1363 AUDIO_ERR_LOG("set hdi destroy failed");
1364 }
1365 if (deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) {
1366 AUDIO_INFO_LOG("delete all chains if device type is bt.");
1367 DeleteAllChains();
1368 }
1369 btOffloadEnabled_ = false;
1370 }
1371 SetSpatializationEnabledToChains();
1372 }
1373 // for AISS temporarily
CheckIfSpkDsp()1374 bool AudioEffectChainManager::CheckIfSpkDsp()
1375 {
1376 if (deviceType_ != DEVICE_TYPE_SPEAKER) {
1377 return false;
1378 }
1379 if (debugArmFlag_) {
1380 for (auto &[key, count] : sceneTypeToEffectChainCountMap_) {
1381 std::string sceneType = key.substr(0, static_cast<size_t>(key.find("_&_")));
1382 if (sceneType == "SCENE_MOVIE" && count > 0) {
1383 return false;
1384 }
1385 }
1386 }
1387 return true;
1388 }
1389
UpdateEffectBtOffloadSupported(const bool & isSupported)1390 void AudioEffectChainManager::UpdateEffectBtOffloadSupported(const bool &isSupported)
1391 {
1392 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1393 if (isSupported == btOffloadSupported_) {
1394 return;
1395 }
1396 if (!isSupported) {
1397 btOffloadSupported_ = isSupported;
1398 AUDIO_INFO_LOG("btOffloadSupported_ off, device disconnect from %{public}d", deviceType_);
1399 return;
1400 }
1401
1402 if (!spatializationEnabled_) {
1403 btOffloadSupported_ = isSupported;
1404 AUDIO_INFO_LOG("btOffloadSupported_ on, but spatialization is off, do nothing");
1405 return;
1406 }
1407 // Release ARM, try offload to DSP
1408 AUDIO_INFO_LOG("btOffloadSupported_ on, try offload effect on device %{public}d", deviceType_);
1409 AudioSpatializationState oldState = {spatializationEnabled_, headTrackingEnabled_};
1410 AudioSpatializationState offState = {false, false};
1411 UpdateSpatializationState(offState);
1412 btOffloadSupported_ = isSupported;
1413 UpdateSpatializationState(oldState);
1414 return;
1415 }
1416
UpdateSceneTypeList(const std::string & sceneType,SceneTypeOperation operation)1417 void AudioEffectChainManager::UpdateSceneTypeList(const std::string &sceneType, SceneTypeOperation operation)
1418 {
1419 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1420 if (operation == ADD_SCENE_TYPE) {
1421 auto it = std::find_if(sceneTypeCountList_.begin(), sceneTypeCountList_.end(),
1422 [sceneType](const std::pair<std::string, int32_t> &element) {
1423 return element.first == sceneType;
1424 });
1425 if (it == sceneTypeCountList_.end()) {
1426 sceneTypeCountList_.push_back(std::make_pair(sceneType, 1));
1427 AUDIO_INFO_LOG("scene Type %{public}s is added", sceneType.c_str());
1428 } else {
1429 it->second++;
1430 AUDIO_INFO_LOG("scene Type %{public}s count is increased to %{public}d", sceneType.c_str(), it->second);
1431 }
1432 } else if (operation == REMOVE_SCENE_TYPE) {
1433 auto it = std::find_if(sceneTypeCountList_.begin(), sceneTypeCountList_.end(),
1434 [sceneType](const std::pair<std::string, int32_t> &element) {
1435 return element.first == sceneType;
1436 });
1437 if (it == sceneTypeCountList_.end()) {
1438 AUDIO_WARNING_LOG("scene type %{public}s to be removed is not found", sceneType.c_str());
1439 return;
1440 }
1441 if (it->second <= 1) {
1442 sceneTypeCountList_.erase(it);
1443 AUDIO_INFO_LOG("scene Type %{public}s is removed", sceneType.c_str());
1444 } else {
1445 it->second--;
1446 AUDIO_INFO_LOG("scene Type %{public}s count is decreased to %{public}d", sceneType.c_str(), it->second);
1447 }
1448 } else {
1449 AUDIO_ERR_LOG("Wrong operation to SceneTypeToEffectChainCountBackupMap.");
1450 }
1451 }
1452
FindMaxSessionID(uint32_t & maxSessionID,std::string & sceneType,const std::string & scenePairType,std::set<std::string> & sessions)1453 void AudioEffectChainManager::FindMaxSessionID(uint32_t &maxSessionID, std::string &sceneType,
1454 const std::string &scenePairType, std::set<std::string> &sessions)
1455 {
1456 for (auto &sessionID : sessions) {
1457 if (sessionIDToEffectInfoMap_[sessionID].sceneMode == "EFFECT_NONE") {
1458 continue;
1459 }
1460 uint32_t sessionIDInt = static_cast<uint32_t>(std::stoul(sessionID));
1461 if (sessionIDInt > maxSessionID) {
1462 maxSessionID = sessionIDInt;
1463 sceneType = scenePairType;
1464 }
1465 }
1466 }
1467
UpdateCurrSceneTypeAndStreamUsageForDsp()1468 void AudioEffectChainManager::UpdateCurrSceneTypeAndStreamUsageForDsp()
1469 {
1470 AudioEffectScene currSceneType;
1471 std::string maxSession = std::to_string(maxSessionID_);
1472 UpdateCurrSceneType(currSceneType, maxSessionIDToSceneType_);
1473 SetHdiParam(currSceneType);
1474 if (sessionIDToEffectInfoMap_.count(maxSession)) {
1475 SessionEffectInfo info = sessionIDToEffectInfoMap_[maxSession];
1476 effectHdiInput_[0] = HDI_STREAM_USAGE;
1477 effectHdiInput_[1] = info.streamUsage;
1478 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
1479 AUDIO_INFO_LOG("set hdi streamUsage: %{public}d", info.streamUsage);
1480 if (ret != SUCCESS) {
1481 AUDIO_WARNING_LOG("set hdi streamUsage failed");
1482 }
1483 }
1484 }
1485
InitEffectBuffer(const std::string & sessionID)1486 int32_t AudioEffectChainManager::InitEffectBuffer(const std::string &sessionID)
1487 {
1488 std::lock_guard<std::recursive_mutex> lock(dynamicMutex_);
1489 return InitEffectBufferInner(sessionID);
1490 }
1491
InitEffectBufferInner(const std::string & sessionID)1492 int32_t AudioEffectChainManager::InitEffectBufferInner(const std::string &sessionID)
1493 {
1494 if (sessionIDToEffectInfoMap_.find(sessionID) == sessionIDToEffectInfoMap_.end()) {
1495 return SUCCESS;
1496 }
1497 std::string sceneTypeTemp = sessionIDToEffectInfoMap_[sessionID].sceneType;
1498 if (IsEffectChainStop(sceneTypeTemp, sessionID)) {
1499 return InitAudioEffectChainDynamicInner(sceneTypeTemp);
1500 }
1501 return SUCCESS;
1502 }
1503
IsEffectChainStop(const std::string & sceneType,const std::string & sessionID)1504 bool AudioEffectChainManager::IsEffectChainStop(const std::string &sceneType, const std::string &sessionID)
1505 {
1506 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1507 CHECK_AND_RETURN_RET_LOG(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) > 0 &&
1508 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] != nullptr, ERROR, "null audioEffectChain");
1509 auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
1510 for (auto it = sessionIDToEffectInfoMap_.begin(); it != sessionIDToEffectInfoMap_.end(); ++it) {
1511 if (it->first == sessionID || it->second.sceneMode == "EFFECT_NONE") {
1512 continue;
1513 }
1514 std::string sceneTypeTemp = it->second.sceneType;
1515 std::string sceneTypeAndDeviceKeyTemp = sceneTypeTemp + "_&_" + GetDeviceTypeName();
1516 CHECK_AND_RETURN_RET_LOG(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKeyTemp) > 0 &&
1517 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKeyTemp] != nullptr, ERROR, "null audioEffectChain");
1518 auto audioEffectChainTemp = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKeyTemp];
1519 if (audioEffectChainTemp == audioEffectChain) {
1520 return false;
1521 }
1522 }
1523 return true;
1524 }
1525 } // namespace AudioStandard
1526 } // namespace OHOS
1527