1 /*
2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef LOG_TAG
16 #define LOG_TAG "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 #include "system_ability_definition.h"
26 #include "audio_setting_provider.h"
27 #include "audio_device_type.h"
28 #include "audio_effect_map.h"
29 #include "audio_effect_hdi_param.h"
30
31 namespace OHOS {
32 namespace AudioStandard {
33 namespace {
34 constexpr int32_t RELEASE_WAIT_TIME_MS = 3000; // 3000ms
35 const std::unordered_map<std::string, std::string> AUDIO_PERSISTENCE_EFFECT_KEY {
36 {"voip_down", "settings.sound_ai_voip_down_selection"},
37 };
38 const std::vector<std::string> AUDIO_PERSISTENCE_SCENE {"SCENE_VOIP_DOWN"};
39 }
CheckValidEffectLibEntry(const std::shared_ptr<AudioEffectLibEntry> & libEntry,const std::string & effect,const std::string & libName)40 static int32_t CheckValidEffectLibEntry(const std::shared_ptr<AudioEffectLibEntry> &libEntry, const std::string &effect,
41 const std::string &libName)
42 {
43 CHECK_AND_RETURN_RET_LOG(libEntry != nullptr, ERROR, "Effect [%{public}s] in lib [%{public}s] is nullptr",
44 effect.c_str(), libName.c_str());
45
46 CHECK_AND_RETURN_RET_LOG(libEntry->audioEffectLibHandle, ERROR,
47 "AudioEffectLibHandle of Effect [%{public}s] in lib [%{public}s] is nullptr", effect.c_str(), libName.c_str());
48
49 CHECK_AND_RETURN_RET_LOG(libEntry->audioEffectLibHandle->createEffect, ERROR,
50 "CreateEffect function of Effect [%{public}s] in lib [%{public}s] is nullptr", effect.c_str(), libName.c_str());
51
52 CHECK_AND_RETURN_RET_LOG(libEntry->audioEffectLibHandle->releaseEffect, ERROR,
53 "ReleaseEffect function of Effect [%{public}s] in lib [%{public}s] is nullptr", effect.c_str(),
54 libName.c_str());
55 return SUCCESS;
56 }
57
FindEffectLib(const std::string & effect,const std::vector<std::shared_ptr<AudioEffectLibEntry>> & effectLibraryList,std::shared_ptr<AudioEffectLibEntry> & libEntry,std::string & libName)58 static int32_t FindEffectLib(const std::string &effect,
59 const std::vector<std::shared_ptr<AudioEffectLibEntry>> &effectLibraryList,
60 std::shared_ptr<AudioEffectLibEntry> &libEntry, std::string &libName)
61 {
62 for (const std::shared_ptr<AudioEffectLibEntry> &lib : effectLibraryList) {
63 if (std::any_of(lib->effectName.begin(), lib->effectName.end(),
64 [&effect](const std::string &effectName) {
65 return effectName == effect;
66 })) {
67 libName = lib->libraryName;
68 libEntry = lib;
69 return SUCCESS;
70 }
71 }
72 return ERROR;
73 }
74
IsChannelLayoutSupported(const uint64_t channelLayout)75 static bool IsChannelLayoutSupported(const uint64_t channelLayout)
76 {
77 return find(AUDIO_EFFECT_SUPPORTED_CHANNELLAYOUTS.begin(),
78 AUDIO_EFFECT_SUPPORTED_CHANNELLAYOUTS.end(), channelLayout) != AUDIO_EFFECT_SUPPORTED_CHANNELLAYOUTS.end();
79 }
80
AudioEffectChainManager()81 AudioEffectChainManager::AudioEffectChainManager()
82 {
83 effectToLibraryEntryMap_.clear();
84 effectToLibraryNameMap_.clear();
85 effectChainToEffectsMap_.clear();
86 sceneTypeAndModeToEffectChainNameMap_.clear();
87 sceneTypeToEffectChainMap_.clear();
88 sceneTypeToEffectChainCountMap_.clear();
89 sessionIDSet_.clear();
90 sceneTypeToSessionIDMap_.clear();
91 sessionIDToEffectInfoMap_.clear();
92 effectPropertyMap_.clear();
93 deviceType_ = DEVICE_TYPE_SPEAKER;
94 deviceSink_ = DEFAULT_DEVICE_SINK;
95 spatialDeviceType_ = EARPHONE_TYPE_OTHERS;
96 isInitialized_ = false;
97 defaultPropertyMap_.clear();
98 #ifdef SENSOR_ENABLE
99 headTracker_ = std::make_shared<HeadTracker>();
100 #endif
101
102 audioEffectHdiParam_ = std::make_shared<AudioEffectHdiParam>();
103 memset_s(static_cast<void *>(effectHdiInput_), sizeof(effectHdiInput_), 0, sizeof(effectHdiInput_));
104 GetSysPara("const.build.product", deviceClass_);
105 }
106
~AudioEffectChainManager()107 AudioEffectChainManager::~AudioEffectChainManager()
108 {
109 AUDIO_INFO_LOG("~AudioEffectChainManager()");
110 }
111
GetInstance()112 AudioEffectChainManager *AudioEffectChainManager::GetInstance()
113 {
114 static AudioEffectChainManager audioEffectChainManager;
115 return &audioEffectChainManager;
116 }
117
UpdateDeviceInfo(int32_t device,const std::string & sinkName)118 int32_t AudioEffectChainManager::UpdateDeviceInfo(int32_t device, const std::string &sinkName)
119 {
120 if (!isInitialized_) {
121 deviceType_ = (DeviceType)device;
122 deviceSink_ = sinkName;
123 AUDIO_INFO_LOG("has not beed initialized");
124 return ERROR;
125 }
126 deviceSink_ = sinkName;
127
128 if (deviceType_ == (DeviceType)device) {
129 return ERROR;
130 }
131 // Delete effectChain in AP and store in backup map
132 AUDIO_PRERELEASE_LOGI("delete all chains when device type change");
133 DeleteAllChains();
134 deviceType_ = (DeviceType)device;
135
136 return SUCCESS;
137 }
138
SetSpkOffloadState()139 void AudioEffectChainManager::SetSpkOffloadState()
140 {
141 int32_t ret;
142 if (deviceType_ == DEVICE_TYPE_SPEAKER) {
143 if (!spkOffloadEnabled_) {
144 effectHdiInput_[0] = HDI_INIT;
145 ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_SPEAKER);
146 if (ret != SUCCESS || !CheckIfSpkDsp()) {
147 AUDIO_WARNING_LOG("set hdi init failed, backup speaker entered");
148 spkOffloadEnabled_ = false;
149 RecoverAllChains();
150 } else {
151 AUDIO_INFO_LOG("set hdi init succeeded, normal speaker entered");
152 spkOffloadEnabled_ = true;
153 }
154 }
155 } else {
156 if (spkOffloadEnabled_) {
157 effectHdiInput_[0] = HDI_DESTROY;
158 ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_SPEAKER);
159 if (ret != SUCCESS) {
160 AUDIO_WARNING_LOG("set hdi destroy failed, backup speaker entered");
161 }
162 spkOffloadEnabled_ = false;
163 }
164
165 if (deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP && (btOffloadEnabled_)) {
166 return;
167 }
168
169 AUDIO_INFO_LOG("recover all chains if device type not bt.");
170 RecoverAllChains();
171 }
172 }
173
SetOutputDeviceSink(int32_t device,const std::string & sinkName)174 void AudioEffectChainManager::SetOutputDeviceSink(int32_t device, const std::string &sinkName)
175 {
176 std::lock_guard<std::mutex> lock(dynamicMutex_);
177 if (UpdateDeviceInfo(device, sinkName) != SUCCESS) {
178 return;
179 }
180 // recover effectChain in speaker mode
181 SetSpkOffloadState();
182 return;
183 }
184
GetDeviceTypeName()185 std::string AudioEffectChainManager::GetDeviceTypeName()
186 {
187 std::string name = "";
188 const std::unordered_map<DeviceType, std::string> &supportDeviceType = GetSupportedDeviceType();
189 auto device = supportDeviceType.find(deviceType_);
190 if (device != supportDeviceType.end()) {
191 name = device->second;
192 }
193 return name;
194 }
195
GetOffloadEnabled()196 bool AudioEffectChainManager::GetOffloadEnabled()
197 {
198 if (deviceType_ == DEVICE_TYPE_SPEAKER) {
199 return spkOffloadEnabled_;
200 } else {
201 return btOffloadEnabled_;
202 }
203 }
204
InitHdiState()205 void AudioEffectChainManager::InitHdiState()
206 {
207 std::lock_guard<std::mutex> lock(dynamicMutex_);
208 InitHdiStateInner();
209 }
210
InitHdiStateInner()211 void AudioEffectChainManager::InitHdiStateInner()
212 {
213 if (audioEffectHdiParam_ == nullptr) {
214 AUDIO_INFO_LOG("audioEffectHdiParam_ is nullptr.");
215 return;
216 }
217 audioEffectHdiParam_->InitHdi();
218 effectHdiInput_[0] = HDI_BLUETOOTH_MODE;
219 effectHdiInput_[1] = 1;
220 AUDIO_INFO_LOG("set hdi bluetooth mode: %{public}d", effectHdiInput_[1]);
221 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
222 if (ret != SUCCESS) {
223 AUDIO_WARNING_LOG("set hdi bluetooth mode failed");
224 }
225 effectHdiInput_[0] = HDI_INIT;
226 ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_SPEAKER);
227 if (ret != SUCCESS) {
228 AUDIO_WARNING_LOG("set hdi init failed, backup speaker entered");
229 spkOffloadEnabled_ = false;
230 } else {
231 AUDIO_INFO_LOG("set hdi init succeeded, normal speaker entered");
232 spkOffloadEnabled_ = true;
233 }
234 }
235
236 // Boot initialize
InitAudioEffectChainManager(const std::vector<EffectChain> & effectChains,const EffectChainManagerParam & effectChainManagerParam,const std::vector<std::shared_ptr<AudioEffectLibEntry>> & effectLibraryList)237 void AudioEffectChainManager::InitAudioEffectChainManager(const std::vector<EffectChain> &effectChains,
238 const EffectChainManagerParam &effectChainManagerParam,
239 const std::vector<std::shared_ptr<AudioEffectLibEntry>> &effectLibraryList)
240 {
241 std::lock_guard<std::mutex> lock(dynamicMutex_);
242 maxEffectChainCount_ = effectChainManagerParam.maxExtraNum + 1;
243 priorSceneList_ = effectChainManagerParam.priorSceneList;
244 ConstructEffectChainMgrMaps(effectChains, effectChainManagerParam, effectLibraryList);
245 AUDIO_INFO_LOG("EffectToLibraryEntryMap size %{public}zu", effectToLibraryEntryMap_.size());
246 AUDIO_DEBUG_LOG("EffectChainToEffectsMap size %{public}zu, SceneTypeAndModeToEffectChainNameMap size %{public}zu",
247 effectChainToEffectsMap_.size(), sceneTypeAndModeToEffectChainNameMap_.size());
248 InitHdiStateInner();
249 isInitialized_ = true;
250 RecoverAllChains();
251 }
252
ConstructEffectChainMgrMaps(const std::vector<EffectChain> & effectChains,const EffectChainManagerParam & effectChainManagerParam,const std::vector<std::shared_ptr<AudioEffectLibEntry>> & effectLibraryList)253 void AudioEffectChainManager::ConstructEffectChainMgrMaps(const std::vector<EffectChain> &effectChains,
254 const EffectChainManagerParam &effectChainManagerParam,
255 const std::vector<std::shared_ptr<AudioEffectLibEntry>> &effectLibraryList)
256 {
257 const std::unordered_map<std::string, std::string> &map = effectChainManagerParam.sceneTypeToChainNameMap;
258 std::set<std::string> effectSet;
259 for (EffectChain efc: effectChains) {
260 for (std::string effect: efc.apply) {
261 effectSet.insert(effect);
262 }
263 }
264 // Construct EffectToLibraryEntryMap that stores libEntry for each effect name
265 std::shared_ptr<AudioEffectLibEntry> libEntry = nullptr;
266 std::string libName;
267 for (std::string effect: effectSet) {
268 int32_t ret = FindEffectLib(effect, effectLibraryList, libEntry, libName);
269 CHECK_AND_CONTINUE_LOG(ret != ERROR, "Couldn't find libEntry of effect %{public}s", effect.c_str());
270 ret = CheckValidEffectLibEntry(libEntry, effect, libName);
271 CHECK_AND_CONTINUE_LOG(ret != ERROR, "Invalid libEntry of effect %{public}s", effect.c_str());
272 effectToLibraryEntryMap_[effect] = libEntry;
273 effectToLibraryNameMap_[effect] = libName;
274 }
275 // Construct EffectChainToEffectsMap that stores all effect names of each effect chain
276 for (EffectChain efc: effectChains) {
277 std::string key = efc.name;
278 std::vector <std::string> effects;
279 for (std::string effectName: efc.apply) {
280 if (effectToLibraryEntryMap_.count(effectName)) {
281 effects.emplace_back(effectName);
282 }
283 }
284 effectChainToEffectsMap_[key] = effects;
285 }
286
287 // Constrcut SceneTypeAndModeToEffectChainNameMap that stores effectMode associated with the effectChainName
288 for (auto item = map.begin(); item != map.end(); ++item) {
289 sceneTypeAndModeToEffectChainNameMap_[item->first] = item->second;
290 if (item->first.substr(0, effectChainManagerParam.defaultSceneName.size()) ==
291 effectChainManagerParam.defaultSceneName) {
292 sceneTypeAndModeToEffectChainNameMap_[DEFAULT_SCENE_TYPE + item->first.substr(
293 effectChainManagerParam.defaultSceneName.size())] = item->second;
294 }
295 }
296 // Construct effectPropertyMap_ that stores effect's property
297 effectPropertyMap_ = effectChainManagerParam.effectDefaultProperty;
298 defaultPropertyMap_ = effectChainManagerParam.effectDefaultProperty;
299 }
300
CheckAndAddSessionID(const std::string & sessionID)301 bool AudioEffectChainManager::CheckAndAddSessionID(const std::string &sessionID)
302 {
303 std::lock_guard<std::mutex> lock(dynamicMutex_);
304 if (sessionIDSet_.count(sessionID)) {
305 return false;
306 }
307 sessionIDSet_.insert(sessionID);
308 return true;
309 }
310
CreateAudioEffectChainDynamic(const std::string & sceneType)311 int32_t AudioEffectChainManager::CreateAudioEffectChainDynamic(const std::string &sceneType)
312 {
313 std::lock_guard<std::mutex> lock(dynamicMutex_);
314 return CreateAudioEffectChainDynamicInner(sceneType);
315 }
316
SetAudioEffectChainDynamic(std::string & sceneType,const std::string & effectMode)317 int32_t AudioEffectChainManager::SetAudioEffectChainDynamic(std::string &sceneType, const std::string &effectMode)
318 {
319 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
320 CHECK_AND_RETURN_RET_LOG(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey), ERROR,
321 "SceneType [%{public}s] does not exist, failed to set", sceneType.c_str());
322
323 std::shared_ptr<AudioEffectChain> audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
324 const std::unordered_map<AudioEffectMode, std::string> &audioSupportedSceneModes = GetAudioSupportedSceneModes();
325 std::string effectChain;
326 std::string effectChainKey = sceneType + "_&_" + effectMode + "_&_" + GetDeviceTypeName();
327 std::string effectNone = audioSupportedSceneModes.find(EFFECT_NONE)->second;
328 if (!sceneTypeAndModeToEffectChainNameMap_.count(effectChainKey)) {
329 AUDIO_ERR_LOG("EffectChain key [%{public}s] does not exist, auto set to %{public}s",
330 effectChainKey.c_str(), effectNone.c_str());
331 effectChain = effectNone;
332 } else {
333 effectChain = sceneTypeAndModeToEffectChainNameMap_[effectChainKey];
334 }
335
336 if (effectChain != effectNone && !effectChainToEffectsMap_.count(effectChain)) {
337 AUDIO_ERR_LOG("EffectChain name [%{public}s] does not exist, auto set to %{public}s",
338 effectChain.c_str(), effectNone.c_str());
339 effectChain = effectNone;
340 }
341
342 ConfigureAudioEffectChain(audioEffectChain, effectMode);
343 bool exists = std::find(AUDIO_PERSISTENCE_SCENE.begin(), AUDIO_PERSISTENCE_SCENE.end(), sceneType) !=
344 AUDIO_PERSISTENCE_SCENE.end();
345 if (exists && !hasLoadedEffectProperties_) {
346 LoadEffectProperties();
347 }
348 std::string tSceneType = (sceneType == DEFAULT_SCENE_TYPE ? DEFAULT_PRESET_SCENE : sceneType);
349 for (std::string effect: effectChainToEffectsMap_[effectChain]) {
350 AudioEffectHandle handle = nullptr;
351 AudioEffectDescriptor descriptor;
352 descriptor.libraryName = effectToLibraryNameMap_[effect];
353 descriptor.effectName = effect;
354 int32_t ret = effectToLibraryEntryMap_[effect]->audioEffectLibHandle->createEffect(descriptor, &handle);
355 CHECK_AND_CONTINUE_LOG(ret == 0, "EffectToLibraryEntryMap[%{public}s] createEffect fail", effect.c_str());
356
357 AUDIO_INFO_LOG("createEffect, EffectToLibraryEntryMap [%{public}s], effectChainKey [%{public}s]",
358 effect.c_str(), effectChainKey.c_str());
359 AudioEffectScene currSceneType;
360 UpdateCurrSceneType(currSceneType, tSceneType);
361 auto propIter = effectPropertyMap_.find(effect);
362 audioEffectChain->AddEffectHandle(handle, effectToLibraryEntryMap_[effect]->audioEffectLibHandle,
363 currSceneType, effect, propIter == effectPropertyMap_.end() ? "" : propIter->second);
364 }
365 audioEffectChain->ResetIoBufferConfig();
366
367 if (audioEffectChain->IsEmptyEffectHandles()) {
368 AUDIO_PRERELEASE_LOGI("Effectchain is empty, copy bufIn to bufOut like EFFECT_NONE mode");
369 }
370
371 AUDIO_INFO_LOG("SceneType %{public}s delay %{public}u", sceneType.c_str(), audioEffectChain->GetLatency());
372 return SUCCESS;
373 }
374
ConfigureAudioEffectChain(std::shared_ptr<AudioEffectChain> audioEffectChain,const std::string & effectMode)375 void AudioEffectChainManager::ConfigureAudioEffectChain(std::shared_ptr<AudioEffectChain> audioEffectChain,
376 const std::string &effectMode)
377 {
378 audioEffectChain->SetEffectMode(effectMode);
379 audioEffectChain->SetExtraSceneType(extraSceneType_);
380 audioEffectChain->SetSpatialDeviceType(spatialDeviceType_);
381 audioEffectChain->SetSpatializationSceneType(spatializationSceneType_);
382 audioEffectChain->SetSpatializationEnabled(spatializationEnabled_);
383 audioEffectChain->SetLidState(lidState_);
384 audioEffectChain->SetFoldState(foldState_);
385 audioEffectChain->SetAbsVolumeStateToEffectChain(absVolumeState_);
386 }
387
CheckAndRemoveSessionID(const std::string & sessionID)388 bool AudioEffectChainManager::CheckAndRemoveSessionID(const std::string &sessionID)
389 {
390 std::lock_guard<std::mutex> lock(dynamicMutex_);
391 if (!sessionIDSet_.count(sessionID)) {
392 return false;
393 }
394 sessionIDSet_.erase(sessionID);
395 return true;
396 }
397
ReleaseAudioEffectChainDynamic(const std::string & sceneType)398 int32_t AudioEffectChainManager::ReleaseAudioEffectChainDynamic(const std::string &sceneType)
399 {
400 std::lock_guard<std::mutex> lock(dynamicMutex_);
401 return ReleaseAudioEffectChainDynamicInner(sceneType);
402 }
403
ExistAudioEffectChain(const std::string & sceneType,const std::string & effectMode)404 bool AudioEffectChainManager::ExistAudioEffectChain(const std::string &sceneType, const std::string &effectMode)
405 {
406 std::lock_guard<std::mutex> lock(dynamicMutex_);
407 return ExistAudioEffectChainInner(sceneType, effectMode);
408 }
409
GetOutputChannelInfo(const std::string & sceneType,uint32_t & channels,uint64_t & channelLayout)410 int32_t AudioEffectChainManager::GetOutputChannelInfo(const std::string &sceneType,
411 uint32_t &channels, uint64_t &channelLayout)
412 {
413 std::lock_guard<std::mutex> lock(dynamicMutex_);
414 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
415
416 auto it = sceneTypeToEffectChainMap_.find(sceneTypeAndDeviceKey);
417 CHECK_AND_RETURN_RET_LOG(it != sceneTypeToEffectChainMap_.end() && it->second != nullptr,
418 ERROR, "effect chain not found for scene type: %{public}s", sceneTypeAndDeviceKey.c_str());
419
420 auto audioEffectChain = it->second;
421 audioEffectChain->UpdateBufferConfig(channels, channelLayout);
422 return SUCCESS;
423 }
424
ApplyAudioEffectChain(const std::string & sceneType,std::unique_ptr<EffectBufferAttr> & bufferAttr)425 int32_t AudioEffectChainManager::ApplyAudioEffectChain(const std::string &sceneType,
426 std::unique_ptr<EffectBufferAttr> &bufferAttr)
427 {
428 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
429 size_t totLen = static_cast<size_t>(bufferAttr->frameLen * bufferAttr->numChans * sizeof(float));
430 std::lock_guard<std::mutex> lock(dynamicMutex_);
431
432 auto it = sceneTypeToEffectChainMap_.find(sceneTypeAndDeviceKey);
433 if (it == sceneTypeToEffectChainMap_.end() || it->second == nullptr) {
434 CHECK_AND_RETURN_RET_LOG(memcpy_s(bufferAttr->bufOut, totLen, bufferAttr->bufIn, totLen) == 0, ERROR,
435 "memcpy error when no effect applied");
436 return ERROR;
437 }
438
439 auto audioEffectChain = it->second;
440 AudioEffectProcInfo procInfo = {headTrackingEnabled_, btOffloadEnabled_};
441 audioEffectChain->ApplyEffectChain(bufferAttr->bufIn, bufferAttr->bufOut, bufferAttr->frameLen, procInfo);
442 audioEffectChain->UpdateBufferConfig(bufferAttr->outChannels, bufferAttr->outChannelLayout);
443 return SUCCESS;
444 }
445
EffectDspVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)446 int32_t AudioEffectChainManager::EffectDspVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)
447 {
448 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
449 float volumeMax = 0.0f;
450 for (auto it = sceneTypeToSessionIDMap_.begin(); it != sceneTypeToSessionIDMap_.end(); ++it) {
451 std::set<std::string> sessions = sceneTypeToSessionIDMap_[it->first];
452 for (auto s = sessions.begin(); s != sessions.end(); s++) {
453 if (sessionIDToEffectInfoMap_.find(*s) == sessionIDToEffectInfoMap_.end()) {
454 continue;
455 }
456 CHECK_AND_CONTINUE_LOG(sessionIDToEffectInfoMap_[*s].sceneMode != "EFFECT_NONE",
457 "sessionID:%{public}s sceneType:%{public}s, sceneMode is EFFECT_NONE, no send volume",
458 (*s).c_str(), it->first.c_str());
459 float streamVolumeTemp = audioEffectVolume->GetStreamVolume(*s);
460 float systemVolumeTemp = audioEffectVolume->GetSystemVolume(
461 sessionIDToEffectInfoMap_[*s].systemVolumeType);
462 volumeMax = fmax((streamVolumeTemp * systemVolumeTemp), volumeMax);
463 }
464 }
465 if (static_cast<int32_t>(audioEffectVolume->GetDspVolume() * MAX_UINT_VOLUME_NUM) !=
466 static_cast<int32_t>(volumeMax * MAX_UINT_VOLUME_NUM)) {
467 audioEffectVolume->SetDspVolume(volumeMax);
468 effectHdiInput_[0] = HDI_VOLUME;
469 AUDIO_INFO_LOG("finalVolume change to %{public}f", volumeMax);
470 int32_t dspVolumeMax = static_cast<int32_t>(volumeMax * MAX_UINT_DSP_VOLUME);
471 int32_t ret = memcpy_s(&effectHdiInput_[1], SEND_HDI_COMMAND_LEN - sizeof(int8_t),
472 &dspVolumeMax, sizeof(int32_t));
473 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "memcpy volume failed");
474 AUDIO_INFO_LOG("set hdi volume: %{public}u", *(reinterpret_cast<uint32_t *>(&effectHdiInput_[1])));
475 ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
476 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "set hdi volume failed");
477 }
478 return SUCCESS;
479 }
480
EffectApVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)481 int32_t AudioEffectChainManager::EffectApVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume)
482 {
483 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
484 for (auto sessionId = sessionIDSet_.begin(); sessionId != sessionIDSet_.end(); ++sessionId) {
485 if (sessionIDToEffectInfoMap_.find(*sessionId) == sessionIDToEffectInfoMap_.end()) {
486 continue;
487 }
488 CHECK_AND_CONTINUE_LOG(sessionIDToEffectInfoMap_[*sessionId].sceneMode != "EFFECT_NONE",
489 "sessionID:%{public}s, sceneMode is EFFECT_NONE, no send volume", (*sessionId).c_str());
490 std::string sceneTypeTemp = sessionIDToEffectInfoMap_[*sessionId].sceneType;
491 std::string sceneTypeAndDeviceKey = sceneTypeTemp + "_&_" + GetDeviceTypeName();
492 CHECK_AND_CONTINUE_LOG(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) != 0 &&
493 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] != nullptr,
494 "null audioEffectChain, sceneType: %{public}s", sceneTypeTemp.c_str());
495 auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
496 float streamVolumeTemp = audioEffectVolume->GetStreamVolume(*sessionId);
497 float systemVolumeTemp = audioEffectVolume->GetSystemVolume(
498 sessionIDToEffectInfoMap_[*sessionId].systemVolumeType);
499 float currVolumeTemp = audioEffectChain->GetCurrVolume();
500 float volumeMax = fmax((streamVolumeTemp * systemVolumeTemp), currVolumeTemp);
501 if (volumeMax > currVolumeTemp) {
502 audioEffectChain->SetCurrVolume(volumeMax);
503 }
504 audioEffectChain->SetFinalVolumeState(true);
505 }
506 return SendEffectApVolume(audioEffectVolume);
507 }
508
SendEffectApVolume(std::shared_ptr<AudioEffectVolume> audioEffectVolume)509 int32_t AudioEffectChainManager::SendEffectApVolume(std::shared_ptr<AudioEffectVolume> audioEffectVolume)
510 {
511 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
512 for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
513 if (it->second == nullptr) {
514 AUDIO_INFO_LOG("null audioEffectChain, sceneType: %{public}s", it->first.c_str());
515 continue;
516 }
517 auto audioEffectChain = it->second;
518 float volumeMax = audioEffectChain->GetCurrVolume();
519 if (static_cast<int32_t>(audioEffectChain->GetFinalVolume() * MAX_UINT_VOLUME_NUM) ==
520 static_cast<int32_t>(volumeMax * MAX_UINT_VOLUME_NUM)) {
521 audioEffectChain->SetFinalVolumeState(false);
522 } else {
523 if (audioEffectChain->GetFinalVolumeState() == true) {
524 audioEffectChain->SetFinalVolume(volumeMax);
525 int32_t ret = audioEffectChain->UpdateEffectParam();
526 CHECK_AND_CONTINUE_LOG(ret == 0, "set ap volume failed, ret: %{public}d", ret);
527 AUDIO_INFO_LOG("The delay of SceneType %{public}s is %{public}u, finalVolume changed to %{public}f",
528 it->first.c_str(), audioEffectChain->GetLatency(), volumeMax);
529 audioEffectChain->SetFinalVolumeState(false);
530 }
531 }
532 }
533 for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
534 CHECK_AND_CONTINUE_LOG(it->second != nullptr, "null audioEffectChain, sceneType: %{public}s",
535 it->first.c_str());
536 auto audioEffectChain = it->second;
537 float volume = 0.0f;
538 audioEffectChain->SetCurrVolume(volume);
539 }
540 return SUCCESS;
541 }
542
EffectVolumeUpdate()543 int32_t AudioEffectChainManager::EffectVolumeUpdate()
544 {
545 std::lock_guard<std::mutex> lock(dynamicMutex_);
546 std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
547 return EffectVolumeUpdateInner(audioEffectVolume);
548 }
549
StreamVolumeUpdate(const std::string sessionIDString,const float streamVolume)550 int32_t AudioEffectChainManager::StreamVolumeUpdate(const std::string sessionIDString, const float streamVolume)
551 {
552 std::lock_guard<std::mutex> lock(dynamicMutex_);
553 // update streamVolume
554 std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
555 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
556 audioEffectVolume->SetStreamVolume(sessionIDString, streamVolume);
557 int32_t ret;
558 AUDIO_INFO_LOG("sessionId: %{public}s, set streamVolume: %{public}f",
559 sessionIDString.c_str(), audioEffectVolume->GetStreamVolume(sessionIDString));
560 ret = EffectVolumeUpdateInner(audioEffectVolume);
561 return ret;
562 }
563
DeleteStreamVolume(const std::string StringSessionID)564 int32_t AudioEffectChainManager::DeleteStreamVolume(const std::string StringSessionID)
565 {
566 // delete streamVolume by sessionId, but don't update volume
567 std::lock_guard<std::mutex> lock(dynamicMutex_);
568 return DeleteStreamVolumeInner(StringSessionID);
569 }
570
DeleteStreamVolumeInner(const std::string StringSessionID)571 int32_t AudioEffectChainManager::DeleteStreamVolumeInner(const std::string StringSessionID)
572 {
573 std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
574 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
575 audioEffectVolume->StreamVolumeDelete(StringSessionID);
576 AUDIO_INFO_LOG("delete streamVolume, sessionId: %{public}s", StringSessionID.c_str());
577 return SUCCESS;
578 }
579
SetEffectSystemVolume(const int32_t systemVolumeType,const float systemVolume)580 int32_t AudioEffectChainManager::SetEffectSystemVolume(const int32_t systemVolumeType, const float systemVolume)
581 {
582 std::lock_guard<std::mutex> lock(dynamicMutex_);
583 // set systemVolume by systemVolumeType
584 std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
585 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
586 audioEffectVolume->SetSystemVolume(systemVolumeType, systemVolume);
587 AUDIO_INFO_LOG("systemVolumeType: %{public}d, systemVolume: %{public}f", systemVolumeType,
588 audioEffectVolume->GetSystemVolume(systemVolumeType));
589
590 return EffectVolumeUpdateInner(audioEffectVolume);
591 }
592
593 #ifdef WINDOW_MANAGER_ENABLE
EffectDspRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,const uint32_t rotationState)594 int32_t AudioEffectChainManager::EffectDspRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,
595 const uint32_t rotationState)
596 {
597 // send rotation to dsp
598 AUDIO_DEBUG_LOG("send rotation to dsp.");
599 CHECK_AND_RETURN_RET_LOG(audioEffectRotation != nullptr, ERROR, "null audioEffectRotation");
600 AUDIO_DEBUG_LOG("rotationState change, new state: %{public}d, previous state: %{public}d",
601 rotationState, audioEffectRotation->GetRotation());
602 effectHdiInput_[0] = HDI_ROTATION;
603 effectHdiInput_[1] = rotationState;
604 AUDIO_INFO_LOG("set hdi rotation: %{public}d", effectHdiInput_[1]);
605 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
606 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "set hdi rotation failed");
607
608 return SUCCESS;
609 }
610
EffectApRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,const uint32_t rotationState)611 int32_t AudioEffectChainManager::EffectApRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,
612 const uint32_t rotationState)
613 {
614 // send rotation to ap
615 AUDIO_DEBUG_LOG("send rotation to ap.");
616 CHECK_AND_RETURN_RET_LOG(audioEffectRotation != nullptr, ERROR, "null audioEffectRotation");
617 AUDIO_DEBUG_LOG("rotationState change, new state: %{public}d, previous state: %{public}d",
618 rotationState, audioEffectRotation->GetRotation());
619 for (auto it = sceneTypeToSessionIDMap_.begin(); it != sceneTypeToSessionIDMap_.end(); it++) {
620 std::string sceneTypeAndDeviceKey = it->first + "_&_" + GetDeviceTypeName();
621 if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
622 return ERROR;
623 }
624 auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
625 if (audioEffectChain == nullptr) {
626 return ERROR;
627 }
628 int32_t ret = audioEffectChain->UpdateEffectParam();
629 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "set ap rotation failed");
630 AUDIO_INFO_LOG("The delay of SceneType %{public}s is %{public}u, rotation changed to %{public}u",
631 it->first.c_str(), audioEffectChain->GetLatency(), rotationState);
632 }
633
634 return SUCCESS;
635 }
636
EffectRotationUpdate(const uint32_t rotationState)637 int32_t AudioEffectChainManager::EffectRotationUpdate(const uint32_t rotationState)
638 {
639 std::lock_guard<std::mutex> lock(dynamicMutex_);
640 std::shared_ptr<AudioEffectRotation> audioEffectRotation = AudioEffectRotation::GetInstance();
641 AUDIO_INFO_LOG("rotation update to %{public}u", rotationState);
642 if (audioEffectRotation->GetRotation() != rotationState) {
643 audioEffectRotation->SetRotation(rotationState);
644 EffectDspRotationUpdate(audioEffectRotation, rotationState);
645 EffectApRotationUpdate(audioEffectRotation, rotationState);
646 }
647
648 return SUCCESS;
649 }
650 #endif
651
UpdateMultichannelConfig(const std::string & sceneType)652 int32_t AudioEffectChainManager::UpdateMultichannelConfig(const std::string &sceneType)
653 {
654 std::lock_guard<std::mutex> lock(dynamicMutex_);
655 return UpdateMultichannelConfigInner(sceneType);
656 }
657
InitAudioEffectChainDynamic(const std::string & sceneType)658 int32_t AudioEffectChainManager::InitAudioEffectChainDynamic(const std::string &sceneType)
659 {
660 std::lock_guard<std::mutex> lock(dynamicMutex_);
661 return InitAudioEffectChainDynamicInner(sceneType);
662 }
663
InitAudioEffectChainDynamicInner(const std::string & sceneType)664 int32_t AudioEffectChainManager::InitAudioEffectChainDynamicInner(const std::string &sceneType)
665 {
666 CHECK_AND_RETURN_RET_LOG(isInitialized_, ERROR, "has not been initialized");
667 CHECK_AND_RETURN_RET_LOG(sceneType != "", ERROR, "null sceneType");
668
669 std::shared_ptr<AudioEffectChain> audioEffectChain = nullptr;
670 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
671 if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
672 return SUCCESS;
673 } else {
674 audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
675 }
676 if (audioEffectChain != nullptr) {
677 audioEffectChain->InitEffectChain();
678 AUDIO_INFO_LOG("init effect buffer");
679 }
680
681 return SUCCESS;
682 }
683
UpdateSpatializationState(AudioSpatializationState spatializationState)684 int32_t AudioEffectChainManager::UpdateSpatializationState(AudioSpatializationState spatializationState)
685 {
686 std::lock_guard<std::mutex> lock(dynamicMutex_);
687 return UpdateSpatializationStateInner(spatializationState);
688 }
689
UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType)690 int32_t AudioEffectChainManager::UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType)
691 {
692 std::lock_guard<std::mutex> lock(dynamicMutex_);
693 int32_t ret{ SUCCESS };
694 spatialDeviceType_ = spatialDeviceType;
695
696 effectHdiInput_[0] = HDI_UPDATE_SPATIAL_DEVICE_TYPE;
697 effectHdiInput_[1] = spatialDeviceType_;
698 AUDIO_INFO_LOG("set hdi spatialDeviceType: %{public}d", effectHdiInput_[1]);
699 ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
700 if (ret != SUCCESS) {
701 AUDIO_WARNING_LOG("set hdi update spatial device type failed");
702 }
703
704 for (const auto& sceneType2EffectChain : sceneTypeToEffectChainMap_) {
705 auto audioEffectChain = sceneType2EffectChain.second;
706 if (audioEffectChain != nullptr) {
707 audioEffectChain->SetSpatialDeviceType(spatialDeviceType_);
708 ret = audioEffectChain->UpdateEffectParam();
709 CHECK_AND_CONTINUE_LOG(ret == SUCCESS, "UpdateEffectParam failed.");
710 }
711 }
712
713 return SUCCESS;
714 }
715
ReturnEffectChannelInfo(const std::string & sceneType,uint32_t & channels,uint64_t & channelLayout)716 int32_t AudioEffectChainManager::ReturnEffectChannelInfo(const std::string &sceneType, uint32_t &channels,
717 uint64_t &channelLayout)
718 {
719 std::lock_guard<std::mutex> lock(dynamicMutex_);
720 return ReturnEffectChannelInfoInner(sceneType, channels, channelLayout);
721 }
722
ReturnMultiChannelInfo(uint32_t * channels,uint64_t * channelLayout)723 int32_t AudioEffectChainManager::ReturnMultiChannelInfo(uint32_t *channels, uint64_t *channelLayout)
724 {
725 std::lock_guard<std::mutex> lock(dynamicMutex_);
726 uint32_t tmpChannelCount = DEFAULT_NUM_CHANNEL;
727 uint64_t tmpChannelLayout = DEFAULT_NUM_CHANNELLAYOUT;
728 bool channelUpdateFlag = false;
729 for (auto it = sceneTypeToSessionIDMap_.begin(); it != sceneTypeToSessionIDMap_.end(); it++) {
730 std::set<std::string> sessions = sceneTypeToSessionIDMap_[it->first];
731 for (auto s = sessions.begin(); s != sessions.end(); ++s) {
732 SessionEffectInfo info = sessionIDToEffectInfoMap_[*s];
733 if (info.channels > tmpChannelCount &&
734 info.channels <= DSP_MAX_NUM_CHANNEL &&
735 !ExistAudioEffectChainInner(it->first, info.sceneMode) &&
736 IsChannelLayoutSupported(info.channelLayout)) {
737 tmpChannelCount = info.channels;
738 tmpChannelLayout = info.channelLayout;
739 channelUpdateFlag = true;
740 }
741 }
742 }
743 if (channelUpdateFlag) {
744 *channels = tmpChannelCount;
745 *channelLayout = tmpChannelLayout;
746 }
747 return SUCCESS;
748 }
749
SessionInfoMapAdd(const std::string & sessionID,const SessionEffectInfo & info)750 int32_t AudioEffectChainManager::SessionInfoMapAdd(const std::string &sessionID, const SessionEffectInfo &info)
751 {
752 std::lock_guard<std::mutex> lock(dynamicMutex_);
753 CHECK_AND_RETURN_RET_LOG(sessionID != "", ERROR, "null sessionID");
754 if (!sessionIDToEffectInfoMap_.count(sessionID)) {
755 sceneTypeToSessionIDMap_[info.sceneType].insert(sessionID);
756 sessionIDToEffectInfoMap_[sessionID] = info;
757 } else if (sessionIDToEffectInfoMap_[sessionID].sceneMode != info.sceneMode) {
758 sessionIDToEffectInfoMap_[sessionID] = info;
759 } else {
760 return ERROR;
761 }
762 return SUCCESS;
763 }
764
SessionInfoMapDelete(const std::string & sceneType,const std::string & sessionID)765 int32_t AudioEffectChainManager::SessionInfoMapDelete(const std::string &sceneType, const std::string &sessionID)
766 {
767 std::lock_guard<std::mutex> lock(dynamicMutex_);
768 if (!sceneTypeToSessionIDMap_.count(sceneType)) {
769 return ERROR;
770 }
771 if (sceneTypeToSessionIDMap_[sceneType].erase(sessionID)) {
772 if (sceneTypeToSessionIDMap_[sceneType].empty()) {
773 sceneTypeToSessionIDMap_.erase(sceneType);
774 }
775 } else {
776 return ERROR;
777 }
778 if (!sessionIDToEffectInfoMap_.erase(sessionID)) {
779 return ERROR;
780 }
781 return SUCCESS;
782 }
783
SetHdiParam(const AudioEffectScene & sceneType)784 int32_t AudioEffectChainManager::SetHdiParam(const AudioEffectScene &sceneType)
785 {
786 if (!isInitialized_) {
787 if (initializedLogFlag_) {
788 AUDIO_ERR_LOG("audioEffectChainManager has not been initialized");
789 initializedLogFlag_ = false;
790 }
791 return ERROR;
792 }
793 memset_s(static_cast<void *>(effectHdiInput_), sizeof(effectHdiInput_), 0, sizeof(effectHdiInput_));
794
795 effectHdiInput_[0] = HDI_ROOM_MODE;
796 effectHdiInput_[1] = sceneType;
797 AUDIO_PRERELEASE_LOGI("set hdi room mode sceneType: %{public}d", effectHdiInput_[1]);
798 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
799 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR, "set hdi room mode failed, ret is %{public}d", ret);
800 return SUCCESS;
801 }
802
QueryHdiSupportedChannelInfo(uint32_t & channels,uint64_t & channelLayout)803 int32_t AudioEffectChainManager::QueryHdiSupportedChannelInfo(uint32_t &channels, uint64_t &channelLayout)
804 {
805 std::lock_guard<std::mutex> lock(dynamicMutex_);
806 uint32_t tmpChannelCount = DEFAULT_NUM_CHANNEL;
807 uint64_t tmpChannelLayout = DEFAULT_NUM_CHANNELLAYOUT;
808 if (sceneTypeToSessionIDMap_.size() == 0) {
809 return SUCCESS;
810 }
811 for (auto it = sceneTypeToSessionIDMap_.begin(); it != sceneTypeToSessionIDMap_.end(); it++) {
812 std::set<std::string> sessions = sceneTypeToSessionIDMap_[it->first];
813 for (auto s = sessions.begin(); s != sessions.end(); ++s) {
814 SessionEffectInfo info = sessionIDToEffectInfoMap_[*s];
815 if (info.channels > tmpChannelCount &&
816 info.channels <= DSP_MAX_NUM_CHANNEL &&
817 !ExistAudioEffectChainInner(it->first, info.sceneMode)) {
818 tmpChannelCount = info.channels;
819 tmpChannelLayout = info.channelLayout;
820 }
821 }
822 }
823 if (tmpChannelLayout != channelLayout) {
824 if (!isInitialized_) {
825 if (initializedLogFlag_) {
826 AUDIO_ERR_LOG("audioEffectChainManager has not been initialized");
827 initializedLogFlag_ = false;
828 }
829 return ERROR;
830 }
831 memset_s(static_cast<void *>(effectHdiInput_), sizeof(effectHdiInput_), 0, sizeof(effectHdiInput_));
832
833 effectHdiInput_[0] = HDI_QUERY_CHANNELLAYOUT;
834 uint64_t* tempChannelLayout = (uint64_t *)(effectHdiInput_ + 1);
835 *tempChannelLayout = tmpChannelLayout;
836 AUDIO_PRERELEASE_LOGI("set hdi channel: %{public}d", channels);
837 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
838 if (ret != SUCCESS) {
839 channels = DEFAULT_MCH_NUM_CHANNEL;
840 channelLayout = DEFAULT_MCH_NUM_CHANNELLAYOUT;
841 } else {
842 channels = tmpChannelCount;
843 channelLayout = tmpChannelLayout;
844 }
845 }
846 return SUCCESS;
847 }
848
UpdateSensorState()849 void AudioEffectChainManager::UpdateSensorState()
850 {
851 effectHdiInput_[0] = HDI_HEAD_MODE;
852 effectHdiInput_[1] = headTrackingEnabled_ == true ? 1 : 0;
853 AUDIO_INFO_LOG("set hdi head mode: %{public}d", effectHdiInput_[1]);
854 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
855 if (ret != SUCCESS) {
856 AUDIO_WARNING_LOG("set hdi head mode failed");
857 }
858
859 if (headTrackingEnabled_) {
860 #ifdef SENSOR_ENABLE
861 if (btOffloadEnabled_) {
862 headTracker_->SensorInit();
863 ret = headTracker_->SensorSetConfig(DSP_SPATIALIZER_ENGINE);
864 } else {
865 headTracker_->SensorInit();
866 ret = headTracker_->SensorSetConfig(ARM_SPATIALIZER_ENGINE);
867 }
868
869 if (ret != SUCCESS) {
870 AUDIO_ERR_LOG("SensorSetConfig error!");
871 }
872
873 if (headTracker_->SensorActive() != SUCCESS) {
874 AUDIO_ERR_LOG("SensorActive failed");
875 }
876 #endif
877 return;
878 }
879
880 if (btOffloadEnabled_) {
881 return;
882 }
883
884 #ifdef SENSOR_ENABLE
885 if (headTracker_->SensorDeactive() != SUCCESS) {
886 AUDIO_ERR_LOG("SensorDeactive failed");
887 }
888 headTracker_->SensorUnsubscribe();
889 HeadPostureData headPostureData = {1, 1.0, 0.0, 0.0, 0.0}; // ori head posturedata
890 headTracker_->SetHeadPostureData(headPostureData);
891 for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
892 auto audioEffectChain = it->second;
893 if (audioEffectChain == nullptr) {
894 continue;
895 }
896 audioEffectChain->SetHeadTrackingDisabled();
897 }
898 #endif
899 }
900
DeleteAllChains()901 void AudioEffectChainManager::DeleteAllChains()
902 {
903 std::map<std::string, int32_t> sceneTypeToEffectChainCountBackupMap;
904 for (auto it = sceneTypeToEffectChainCountMap_.begin(); it != sceneTypeToEffectChainCountMap_.end(); ++it) {
905 AUDIO_INFO_LOG("sceneTypeAndDeviceKey %{public}s count:%{public}d", it->first.c_str(), it->second);
906 sceneTypeToEffectChainCountBackupMap.insert(std::make_pair(it->first, it->second));
907 }
908
909 for (auto it = sceneTypeToEffectChainCountBackupMap.begin(); it != sceneTypeToEffectChainCountBackupMap.end();
910 ++it) {
911 std::string sceneType = it->first.substr(0, static_cast<size_t>(it->first.find("_&_")));
912 for (int32_t k = 0; k < it->second; ++k) {
913 ReleaseAudioEffectChainDynamicInner(sceneType);
914 }
915 }
916 return;
917 }
918
RecoverAllChains()919 void AudioEffectChainManager::RecoverAllChains()
920 {
921 for (auto item : sceneTypeCountList_) {
922 AUDIO_INFO_LOG("sceneType %{public}s count:%{public}d", item.first.c_str(), item.second);
923 for (int32_t k = 0; k < item.second; ++k) {
924 CreateAudioEffectChainDynamicInner(item.first);
925 }
926 UpdateMultichannelConfigInner(item.first);
927 }
928 UpdateDefaultAudioEffectInner();
929 UpdateStreamUsageInner();
930 }
931
GetLatency(const std::string & sessionId)932 uint32_t AudioEffectChainManager::GetLatency(const std::string &sessionId)
933 {
934 std::lock_guard<std::mutex> lock(dynamicMutex_);
935 if (((deviceType_ == DEVICE_TYPE_SPEAKER) && (spkOffloadEnabled_)) ||
936 ((deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) && (btOffloadEnabled_))) {
937 AUDIO_DEBUG_LOG("offload enabled, return 0");
938 return 0;
939 }
940
941 CHECK_AND_RETURN_RET(sessionIDToEffectInfoMap_.count(sessionId), 0);
942 if (sessionIDToEffectInfoMap_[sessionId].sceneMode == "" ||
943 sessionIDToEffectInfoMap_[sessionId].sceneMode == "None") {
944 AUDIO_DEBUG_LOG("seceneMode is None, return 0");
945 return 0;
946 }
947
948 std::string sceneTypeAndDeviceKey = sessionIDToEffectInfoMap_[sessionId].sceneType + "_&_" + GetDeviceTypeName();
949 CHECK_AND_RETURN_RET(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) &&
950 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] != nullptr, 0);
951 return sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey]->GetLatency();
952 }
953
SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType)954 int32_t AudioEffectChainManager::SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType)
955 {
956 std::lock_guard<std::mutex> lock(dynamicMutex_);
957 AUDIO_INFO_LOG("spatialization scene type is set to be %{public}d", spatializationSceneType);
958 spatializationSceneType_ = spatializationSceneType;
959
960 if (!spatializationEnabled_ || (GetDeviceTypeName() != "DEVICE_TYPE_BLUETOOTH_A2DP")) {
961 return SUCCESS;
962 }
963
964 effectHdiInput_[0] = HDI_SPATIALIZATION_SCENE_TYPE;
965 effectHdiInput_[1] = static_cast<int32_t>(spatializationSceneType_);
966 if (audioEffectHdiParam_->UpdateHdiState(effectHdiInput_) != SUCCESS) {
967 AUDIO_WARNING_LOG("set hdi spatialization scene type failed");
968 }
969
970 SetSpatializationSceneTypeToChains();
971
972 return SUCCESS;
973 }
974
SendAudioParamToHDI(HdiSetParamCommandCode code,const std::string & value,DeviceType device)975 void AudioEffectChainManager::SendAudioParamToHDI(
976 HdiSetParamCommandCode code, const std::string &value, DeviceType device)
977 {
978 effectHdiInput_[0] = code;
979 CHECK_AND_RETURN_LOG(StringConverter(value, effectHdiInput_[1]),
980 "convert invalid bufferSize: %{public}s", value.c_str());
981 if (audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, device) != SUCCESS) {
982 AUDIO_WARNING_LOG("set hdi parameter failed for code %{public}d and value %{public}s", code, value.c_str());
983 }
984 }
985
SendAudioParamToARM(HdiSetParamCommandCode code,const std::string & value)986 void AudioEffectChainManager::SendAudioParamToARM(HdiSetParamCommandCode code, const std::string &value)
987 {
988 for (const auto &[scene, audioEffectChain] : sceneTypeToEffectChainMap_) {
989 if (audioEffectChain == nullptr) {
990 continue;
991 }
992
993 bool paramUpdated = false;
994 switch (code) {
995 case HDI_EXTRA_SCENE_TYPE:
996 audioEffectChain->SetExtraSceneType(value);
997 paramUpdated = true;
998 break;
999 case HDI_FOLD_STATE:
1000 audioEffectChain->SetFoldState(value);
1001 paramUpdated = true;
1002 break;
1003 case HDI_LID_STATE:
1004 audioEffectChain->SetLidState(value);
1005 paramUpdated = true;
1006 break;
1007 default:
1008 break;
1009 }
1010
1011 if (paramUpdated && audioEffectChain->UpdateEffectParam() != SUCCESS) {
1012 AUDIO_WARNING_LOG("Update effect chain failed for code %{public}d and value %{public}s",
1013 code, value.c_str());
1014 }
1015 }
1016 }
1017
UpdateParamExtra(const std::string & mainkey,const std::string & subkey,const std::string & value)1018 void AudioEffectChainManager::UpdateParamExtra(
1019 const std::string &mainkey, const std::string &subkey, const std::string &value)
1020 {
1021 std::lock_guard<std::mutex> lock(dynamicMutex_);
1022 auto updateParam = [&](std::string ¶m, HdiSetParamCommandCode code) {
1023 AUDIO_INFO_LOG("Set %{public}s: %{public}s to hdi and arm", subkey.c_str(), value.c_str());
1024 param = value;
1025 SendAudioParamToHDI(code, value, DEVICE_TYPE_SPEAKER);
1026 SendAudioParamToARM(code, value);
1027 };
1028
1029 if (mainkey == "audio_effect" && subkey == "update_audio_effect_type") {
1030 updateParam(extraSceneType_, HDI_EXTRA_SCENE_TYPE);
1031 } else if (mainkey == "device_status" && subkey == "fold_state") {
1032 updateParam(foldState_, HDI_FOLD_STATE);
1033 } else if (mainkey == "device_status" && subkey == "lid_state") {
1034 updateParam(lidState_, HDI_LID_STATE);
1035 } else {
1036 AUDIO_INFO_LOG("UpdateParamExtra failed, mainkey is %{public}s, subkey is %{public}s, "
1037 "value is %{public}s", mainkey.c_str(), subkey.c_str(), value.c_str());
1038 return;
1039 }
1040 }
1041
SetSpatializationSceneTypeToChains()1042 void AudioEffectChainManager::SetSpatializationSceneTypeToChains()
1043 {
1044 for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
1045 auto audioEffectChain = it->second;
1046 if (audioEffectChain == nullptr) {
1047 continue;
1048 }
1049 audioEffectChain->SetSpatializationSceneType(spatializationSceneType_);
1050 if (audioEffectChain->UpdateEffectParam() != SUCCESS) {
1051 AUDIO_WARNING_LOG("Update param to effect chain failed");
1052 continue;
1053 }
1054 }
1055 }
1056
SetSpatializationEnabledToChains()1057 void AudioEffectChainManager::SetSpatializationEnabledToChains()
1058 {
1059 for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); ++it) {
1060 auto audioEffectChain = it->second;
1061 if (audioEffectChain == nullptr) {
1062 continue;
1063 }
1064 audioEffectChain->SetSpatializationEnabledForFading(spatializationEnabled_);
1065 }
1066 }
1067
ResetInfo()1068 void AudioEffectChainManager::ResetInfo()
1069 {
1070 effectToLibraryEntryMap_.clear();
1071 effectToLibraryNameMap_.clear();
1072 effectChainToEffectsMap_.clear();
1073 sceneTypeAndModeToEffectChainNameMap_.clear();
1074 sceneTypeToEffectChainMap_.clear();
1075 sceneTypeToEffectChainCountMap_.clear();
1076 sessionIDSet_.clear();
1077 sceneTypeToSessionIDMap_.clear();
1078 sessionIDToEffectInfoMap_.clear();
1079 sceneTypeToSpecialEffectSet_.clear();
1080 effectPropertyMap_.clear();
1081 deviceType_ = DEVICE_TYPE_SPEAKER;
1082 deviceSink_ = DEFAULT_DEVICE_SINK;
1083 isInitialized_ = false;
1084 spatializationEnabled_ = false;
1085 headTrackingEnabled_ = false;
1086 btOffloadEnabled_ = false;
1087 spkOffloadEnabled_ = false;
1088 initializedLogFlag_ = true;
1089 spatializationSceneType_ = SPATIALIZATION_SCENE_TYPE_MUSIC;
1090 isDefaultEffectChainExisted_ = false;
1091 }
1092
UpdateDefaultAudioEffect()1093 void AudioEffectChainManager::UpdateDefaultAudioEffect()
1094 {
1095 std::lock_guard<std::mutex> lock(dynamicMutex_);
1096 UpdateDefaultAudioEffectInner();
1097 }
1098
UpdateDefaultAudioEffectInner()1099 void AudioEffectChainManager::UpdateDefaultAudioEffectInner()
1100 {
1101 // for default scene type
1102 uint32_t maxDefaultSessionID = 0;
1103 uint32_t maxSessionID = 0;
1104 for (auto& scenePair : sceneTypeToSessionIDMap_) {
1105 std::set<std::string> &sessions = scenePair.second;
1106 if (!sceneTypeToSpecialEffectSet_.count(scenePair.first) &&
1107 std::find(priorSceneList_.begin(), priorSceneList_.end(),
1108 scenePair.first) == priorSceneList_.end()) {
1109 FindMaxSessionID(maxDefaultSessionID, maxDefaultSessionIDToSceneType_, scenePair.first, sessions);
1110 }
1111 FindMaxSessionID(maxSessionID, maxSessionIDToSceneType_, scenePair.first, sessions);
1112 }
1113 maxSessionID_ = maxSessionID;
1114 AUDIO_INFO_LOG("newest stream, maxDefaultSessionID: %{public}u, sceneType: %{public}s,"
1115 "maxSessionID: %{public}u, sceneType: %{public}s",
1116 maxDefaultSessionID, maxDefaultSessionIDToSceneType_.c_str(),
1117 maxSessionID_, maxSessionIDToSceneType_.c_str());
1118
1119 std::string key = maxDefaultSessionIDToSceneType_ + "_&_" + GetDeviceTypeName();
1120 std::string maxDefaultSession = std::to_string(maxDefaultSessionID);
1121 AudioEffectScene currDefaultSceneType;
1122 UpdateCurrSceneType(currDefaultSceneType, maxDefaultSessionIDToSceneType_);
1123 if (!maxDefaultSessionIDToSceneType_.empty() && sessionIDToEffectInfoMap_.count(maxDefaultSession) &&
1124 sceneTypeToEffectChainMap_.count(key) && sceneTypeToEffectChainMap_[key] != nullptr) {
1125 SessionEffectInfo info = sessionIDToEffectInfoMap_[maxDefaultSession];
1126 std::shared_ptr<AudioEffectChain> audioEffectChain = sceneTypeToEffectChainMap_[key];
1127 audioEffectChain->SetEffectCurrSceneType(currDefaultSceneType);
1128 audioEffectChain->SetStreamUsage(info.streamUsage);
1129 audioEffectChain->UpdateEffectParam();
1130 }
1131 }
1132
UpdateStreamUsage()1133 void AudioEffectChainManager::UpdateStreamUsage()
1134 {
1135 std::lock_guard<std::mutex> lock(dynamicMutex_);
1136 UpdateStreamUsageInner();
1137 }
1138
UpdateStreamUsageInner()1139 void AudioEffectChainManager::UpdateStreamUsageInner()
1140 {
1141 // for special scene type
1142 for (auto& specialSceneType : sceneTypeToSpecialEffectSet_) {
1143 uint32_t maxSpecialSessionID = 0;
1144 std::string maxSpecialSceneType = "";
1145 auto it = sceneTypeToSessionIDMap_.find(specialSceneType);
1146 if (it != sceneTypeToSessionIDMap_.end()) {
1147 std::set<std::string> &sessions = it->second;
1148 FindMaxSessionID(maxSpecialSessionID, maxSpecialSceneType, specialSceneType, sessions);
1149 }
1150 std::string maxSpecialSession = std::to_string(maxSpecialSessionID);
1151 std::string key = maxSpecialSceneType + "_&_" + GetDeviceTypeName();
1152 if (!maxSpecialSceneType.empty() && sessionIDToEffectInfoMap_.count(maxSpecialSession) &&
1153 sceneTypeToEffectChainMap_.count(key) && sceneTypeToEffectChainMap_[key] != nullptr) {
1154 SessionEffectInfo info = sessionIDToEffectInfoMap_[maxSpecialSession];
1155 std::shared_ptr<AudioEffectChain> audioEffectChain = sceneTypeToEffectChainMap_[key];
1156 audioEffectChain->SetStreamUsage(info.streamUsage);
1157 audioEffectChain->UpdateEffectParam();
1158 }
1159 AUDIO_INFO_LOG("newest stream, maxSpecialSessionID: %{public}u, sceneType: %{public}s",
1160 maxSpecialSessionID, maxSpecialSceneType.c_str());
1161 }
1162 // for prior scene type
1163 for (auto& priorSceneType : priorSceneList_) {
1164 uint32_t maxPriorSessionID = 0;
1165 std::string maxPriorSceneType = "";
1166 auto it = sceneTypeToSessionIDMap_.find(priorSceneType);
1167 if (it != sceneTypeToSessionIDMap_.end()) {
1168 std::set<std::string> &sessions = it->second;
1169 FindMaxSessionID(maxPriorSessionID, maxPriorSceneType, priorSceneType, sessions);
1170 }
1171 std::string key = maxPriorSceneType + "_&_" + GetDeviceTypeName();
1172 std::string maxPriorSession = std::to_string(maxPriorSessionID);
1173 if (!maxPriorSceneType.empty() && sessionIDToEffectInfoMap_.count(maxPriorSession) &&
1174 sceneTypeToEffectChainMap_.count(key) && sceneTypeToEffectChainMap_[key] != nullptr) {
1175 SessionEffectInfo info = sessionIDToEffectInfoMap_[maxPriorSession];
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 maxPriorSessionID, maxPriorSceneType.c_str());
1182 }
1183 // update dsp scene type and stream usage
1184 UpdateCurrSceneTypeAndStreamUsageForDsp();
1185 }
1186
CheckSceneTypeMatch(const std::string & sinkSceneType,const std::string & sceneType)1187 bool AudioEffectChainManager::CheckSceneTypeMatch(const std::string &sinkSceneType, const std::string &sceneType)
1188 {
1189 std::lock_guard<std::mutex> lock(dynamicMutex_);
1190 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1191 std::string sinkSceneTypeAndDeviceKey = sinkSceneType + "_&_" + GetDeviceTypeName();
1192 std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1193 if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) ||
1194 !sceneTypeToEffectChainMap_.count(sinkSceneTypeAndDeviceKey)) {
1195 return false;
1196 }
1197 if (sceneType == sinkSceneType && (sceneTypeToSpecialEffectSet_.count(sinkSceneType) ||
1198 std::find(priorSceneList_.begin(), priorSceneList_.end(), sceneType) != priorSceneList_.end())) {
1199 return true;
1200 }
1201 if (sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] ==
1202 sceneTypeToEffectChainMap_[sinkSceneTypeAndDeviceKey]) {
1203 return sceneTypeAndDeviceKey == defaultSceneTypeAndDeviceKey;
1204 }
1205 return false;
1206 }
1207
UpdateCurrSceneType(AudioEffectScene & currSceneType,const std::string & sceneType)1208 void AudioEffectChainManager::UpdateCurrSceneType(AudioEffectScene &currSceneType, const std::string &sceneType)
1209 {
1210 const std::unordered_map<AudioEffectScene, std::string> &audioSupportedSceneTypes = GetSupportedSceneType();
1211 currSceneType = static_cast<AudioEffectScene>(GetKeyFromValue(audioSupportedSceneTypes, sceneType));
1212 }
1213
FindMaxEffectChannels(const std::string & sceneType,const std::set<std::string> & sessions,uint32_t & channels,uint64_t & channelLayout)1214 void AudioEffectChainManager::FindMaxEffectChannels(const std::string &sceneType,
1215 const std::set<std::string> &sessions, uint32_t &channels, uint64_t &channelLayout)
1216 {
1217 for (auto s = sessions.begin(); s != sessions.end(); ++s) {
1218 SessionEffectInfo info = sessionIDToEffectInfoMap_[*s];
1219 uint32_t tmpChannelCount;
1220 uint64_t tmpChannelLayout;
1221 std::string deviceType = GetDeviceTypeName();
1222 if (((deviceType == "DEVICE_TYPE_BLUETOOTH_A2DP") || (deviceType == "DEVICE_TYPE_SPEAKER"))
1223 && ExistAudioEffectChainInner(sceneType, info.sceneMode)) {
1224 tmpChannelLayout = info.channelLayout;
1225 tmpChannelCount = info.channels;
1226 } else {
1227 tmpChannelCount = DEFAULT_NUM_CHANNEL;
1228 tmpChannelLayout = DEFAULT_NUM_CHANNELLAYOUT;
1229 }
1230
1231 if (tmpChannelCount >= channels) {
1232 channels = tmpChannelCount;
1233 channelLayout = tmpChannelLayout;
1234 }
1235 }
1236 }
1237
CreateAudioEffectChain(const std::string & sceneType,bool isPriorScene)1238 std::shared_ptr<AudioEffectChain> AudioEffectChainManager::CreateAudioEffectChain(const std::string &sceneType,
1239 bool isPriorScene)
1240 {
1241 std::shared_ptr<AudioEffectChain> audioEffectChain = nullptr;
1242 std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1243 defaultEffectChainCreated_ = false;
1244 if (isPriorScene) {
1245 AUDIO_INFO_LOG("create prior effect chain: %{public}s", sceneType.c_str());
1246 #ifdef SENSOR_ENABLE
1247 audioEffectChain = std::make_shared<AudioEffectChain>(sceneType, headTracker_);
1248 #else
1249 audioEffectChain = std::make_shared<AudioEffectChain>(sceneType);
1250 #endif
1251 return audioEffectChain;
1252 }
1253 if ((maxEffectChainCount_ - static_cast<int32_t>(sceneTypeToSpecialEffectSet_.size())) > 1) {
1254 AUDIO_INFO_LOG("max audio effect chain count not reached, create special effect chain: %{public}s",
1255 sceneType.c_str());
1256 sceneTypeToSpecialEffectSet_.insert(sceneType);
1257 #ifdef SENSOR_ENABLE
1258 audioEffectChain = std::make_shared<AudioEffectChain>(sceneType, headTracker_);
1259 #else
1260 audioEffectChain = std::make_shared<AudioEffectChain>(sceneType);
1261 #endif
1262 } else {
1263 if (!isDefaultEffectChainExisted_) {
1264 AUDIO_INFO_LOG("max audio effect chain count reached, create current and default effect chain");
1265 #ifdef SENSOR_ENABLE
1266 audioEffectChain = std::make_shared<AudioEffectChain>(DEFAULT_SCENE_TYPE, headTracker_);
1267 #else
1268 audioEffectChain = std::make_shared<AudioEffectChain>(DEFAULT_SCENE_TYPE);
1269 #endif
1270 defaultEffectChainCount_ = 1;
1271 isDefaultEffectChainExisted_ = true;
1272 defaultEffectChainCreated_ = true;
1273 } else {
1274 audioEffectChain = sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey];
1275 defaultEffectChainCount_++;
1276 AUDIO_INFO_LOG("max audio effect chain count reached and default effect chain already exist: %{public}d",
1277 defaultEffectChainCount_);
1278 }
1279 }
1280 return audioEffectChain;
1281 }
1282
CheckAndReleaseCommonEffectChain(const std::string & sceneType)1283 int32_t AudioEffectChainManager::CheckAndReleaseCommonEffectChain(const std::string &sceneType)
1284 {
1285 AUDIO_INFO_LOG("release effect chain for scene type: %{public}s", sceneType.c_str());
1286 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1287 std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1288 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey] = 0;
1289 if (sceneTypeToSpecialEffectSet_.erase(sceneType) > 0) {
1290 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey]->InitEffectChain();
1291 }
1292 if (!isDefaultEffectChainExisted_) {
1293 return ERROR;
1294 }
1295 if (sceneTypeToEffectChainMap_.count(defaultSceneTypeAndDeviceKey) != 0 &&
1296 sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey] != nullptr &&
1297 sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey] ==
1298 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey]) {
1299 if (defaultEffectChainCount_ <= 1) {
1300 defaultEffectChainCount_= 0;
1301 isDefaultEffectChainExisted_ = false;
1302 sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey]->InitEffectChain();
1303 AUDIO_INFO_LOG("default effect chain will be released");
1304 return SUCCESS;
1305 } else {
1306 defaultEffectChainCount_--;
1307 AUDIO_INFO_LOG("default effect chain still exist, count is %{public}d", defaultEffectChainCount_);
1308 }
1309 }
1310 return ERROR;
1311 }
1312
UpdateSpatializationEnabled(AudioSpatializationState spatializationState)1313 void AudioEffectChainManager::UpdateSpatializationEnabled(AudioSpatializationState spatializationState)
1314 {
1315 spatializationEnabled_ = spatializationState.spatializationEnabled;
1316
1317 memset_s(static_cast<void *>(effectHdiInput_), sizeof(effectHdiInput_), 0, sizeof(effectHdiInput_));
1318 if (spatializationEnabled_) {
1319 if ((deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) && (!btOffloadSupported_)) {
1320 AUDIO_INFO_LOG("A2dp-hal, enter ARM processing");
1321 btOffloadEnabled_ = false;
1322 SetSpatializationEnabledToChains();
1323 return;
1324 }
1325 effectHdiInput_[0] = HDI_INIT;
1326 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
1327 if (ret != SUCCESS) {
1328 AUDIO_ERR_LOG("set hdi init failed, enter route of escape in ARM");
1329 btOffloadEnabled_ = false;
1330 } else {
1331 AUDIO_INFO_LOG("set hdi init succeeded, normal spatialization entered");
1332 btOffloadEnabled_ = true;
1333 }
1334 } else {
1335 effectHdiInput_[0] = HDI_DESTROY;
1336 AUDIO_INFO_LOG("set hdi destroy.");
1337 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_, DEVICE_TYPE_BLUETOOTH_A2DP);
1338 if (ret != SUCCESS) {
1339 AUDIO_ERR_LOG("set hdi destroy failed");
1340 }
1341 if (deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) {
1342 AUDIO_INFO_LOG("delete all chains if device type is bt.");
1343 }
1344 btOffloadEnabled_ = false;
1345 }
1346 SetSpatializationEnabledToChains();
1347 }
1348 // for AISS temporarily
CheckIfSpkDsp()1349 bool AudioEffectChainManager::CheckIfSpkDsp()
1350 {
1351 return deviceType_ == DEVICE_TYPE_SPEAKER;
1352 }
1353
UpdateEffectBtOffloadSupported(const bool & isSupported)1354 void AudioEffectChainManager::UpdateEffectBtOffloadSupported(const bool &isSupported)
1355 {
1356 std::lock_guard<std::mutex> lock(dynamicMutex_);
1357 if (isSupported == btOffloadSupported_) {
1358 return;
1359 }
1360 if (!isSupported) {
1361 btOffloadSupported_ = isSupported;
1362 AUDIO_INFO_LOG("btOffloadSupported_ off, device disconnect from %{public}d", deviceType_);
1363 return;
1364 }
1365
1366 if (!spatializationEnabled_) {
1367 btOffloadSupported_ = isSupported;
1368 AUDIO_INFO_LOG("btOffloadSupported_ on, but spatialization is off, do nothing");
1369 return;
1370 }
1371 // Release ARM, try offload to DSP
1372 AUDIO_INFO_LOG("btOffloadSupported_ on, try offload effect on device %{public}d", deviceType_);
1373 AudioSpatializationState oldState = {spatializationEnabled_, headTrackingEnabled_};
1374 AudioSpatializationState offState = {false, false};
1375 UpdateSpatializationStateInner(offState);
1376 btOffloadSupported_ = isSupported;
1377 UpdateSpatializationStateInner(oldState);
1378 return;
1379 }
1380
SetAudioEffectProperty(const AudioEffectPropertyArrayV3 & propertyArray)1381 int32_t AudioEffectChainManager::SetAudioEffectProperty(const AudioEffectPropertyArrayV3 &propertyArray)
1382 {
1383 int32_t ret = AUDIO_OK;
1384 std::lock_guard<std::mutex> lock(dynamicMutex_);
1385 for (const auto &property : propertyArray.property) {
1386 effectPropertyMap_.insert_or_assign(property.name, property.category);
1387 for (const auto &[sceneType, effectChain] : sceneTypeToEffectChainMap_) {
1388 if (effectChain) {
1389 AUDIO_DEBUG_LOG("effectClass->name %{public}s effectProp->category %{public}s",
1390 property.name.c_str(), property.category.c_str());
1391 ret = effectChain->SetEffectProperty(property.name, property.category);
1392 CHECK_AND_CONTINUE_LOG(ret == AUDIO_OK, "set property failed[%{public}d]", ret);
1393 }
1394 }
1395 }
1396 return ret;
1397 }
1398
LoadEffectProperties()1399 void AudioEffectChainManager::LoadEffectProperties()
1400 {
1401 hasLoadedEffectProperties_ = false;
1402 AudioSettingProvider &settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
1403 CHECK_AND_RETURN_LOG(settingProvider.CheckOsAccountReady(), "os account not ready");
1404 for (const auto &[effect, key] : AUDIO_PERSISTENCE_EFFECT_KEY) {
1405 std::string prop = "";
1406 ErrCode ret = settingProvider.GetStringValue(key, prop, "system");
1407 if (!prop.empty() && ret == SUCCESS) {
1408 AUDIO_INFO_LOG("effect->name %{public}s prop %{public}s", effect.c_str(), prop.c_str());
1409 effectPropertyMap_[effect] = prop;
1410 } else {
1411 AUDIO_ERR_LOG("get prop failed for key %{public}s", key.c_str());
1412 if (defaultPropertyMap_.count(effect) != 0) {
1413 AUDIO_INFO_LOG("effect->name %{public}s defaultProp %{public}s", effect.c_str(), prop.c_str());
1414 effectPropertyMap_[effect] = defaultPropertyMap_[effect];
1415 }
1416 }
1417 }
1418 hasLoadedEffectProperties_ = true;
1419 }
1420
SetAudioEffectProperty(const AudioEffectPropertyArray & propertyArray)1421 int32_t AudioEffectChainManager::SetAudioEffectProperty(const AudioEffectPropertyArray &propertyArray)
1422 {
1423 std::lock_guard<std::mutex> lock(dynamicMutex_);
1424 for (const auto &property : propertyArray.property) {
1425 effectPropertyMap_.insert_or_assign(property.effectClass, property.effectProp);
1426 for (const auto &[sceneType, effectChain] : sceneTypeToEffectChainMap_) {
1427 if (effectChain) {
1428 effectChain->SetEffectProperty(property.effectClass, property.effectProp);
1429 }
1430 }
1431 }
1432 return AUDIO_OK;
1433 }
1434
GetAudioEffectProperty(AudioEffectPropertyArrayV3 & propertyArray)1435 int32_t AudioEffectChainManager::GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray)
1436 {
1437 std::lock_guard<std::mutex> lock(dynamicMutex_);
1438 propertyArray.property.clear();
1439 for (const auto &[effect, prop] : effectPropertyMap_) {
1440 if (!prop.empty()) {
1441 AUDIO_DEBUG_LOG("effect->name %{public}s prop->category %{public}s",
1442 effect.c_str(), prop.c_str());
1443 propertyArray.property.emplace_back(AudioEffectPropertyV3{effect, prop, RENDER_EFFECT_FLAG});
1444 }
1445 }
1446 return AUDIO_OK;
1447 }
1448
GetAudioEffectProperty(AudioEffectPropertyArray & propertyArray)1449 int32_t AudioEffectChainManager::GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray)
1450 {
1451 std::lock_guard<std::mutex> lock(dynamicMutex_);
1452 propertyArray.property.clear();
1453 for (const auto &[effect, prop] : effectPropertyMap_) {
1454 if (!prop.empty()) {
1455 propertyArray.property.emplace_back(AudioEffectProperty{effect, prop});
1456 }
1457 }
1458 return AUDIO_OK;
1459 }
1460
UpdateSceneTypeList(const std::string & sceneType,SceneTypeOperation operation)1461 int32_t AudioEffectChainManager::UpdateSceneTypeList(const std::string &sceneType, SceneTypeOperation operation)
1462 {
1463 std::lock_guard<std::mutex> lock(dynamicMutex_);
1464 CHECK_AND_RETURN_RET_LOG(sceneType != "", ERROR, "null sceneType");
1465 if (operation == ADD_SCENE_TYPE) {
1466 auto it = std::find_if(sceneTypeCountList_.begin(), sceneTypeCountList_.end(),
1467 [sceneType](const std::pair<std::string, int32_t> &element) {
1468 return element.first == sceneType;
1469 });
1470 if (it == sceneTypeCountList_.end()) {
1471 sceneTypeCountList_.push_back(std::make_pair(sceneType, 1));
1472 AUDIO_INFO_LOG("scene Type %{public}s is added", sceneType.c_str());
1473 } else {
1474 it->second++;
1475 AUDIO_INFO_LOG("scene Type %{public}s count is increased to %{public}d", sceneType.c_str(), it->second);
1476 }
1477 } else if (operation == REMOVE_SCENE_TYPE) {
1478 auto it = std::find_if(sceneTypeCountList_.begin(), sceneTypeCountList_.end(),
1479 [sceneType](const std::pair<std::string, int32_t> &element) {
1480 return element.first == sceneType;
1481 });
1482 if (it == sceneTypeCountList_.end()) {
1483 AUDIO_WARNING_LOG("scene type %{public}s to be removed is not found", sceneType.c_str());
1484 return ERROR;
1485 }
1486 if (it->second <= 1) {
1487 sceneTypeCountList_.erase(it);
1488 AUDIO_INFO_LOG("scene Type %{public}s is removed", sceneType.c_str());
1489 } else {
1490 it->second--;
1491 AUDIO_INFO_LOG("scene Type %{public}s count is decreased to %{public}d", sceneType.c_str(), it->second);
1492 }
1493 } else {
1494 AUDIO_ERR_LOG("Wrong operation to sceneTypeToEffectChainCountBackupMap.");
1495 return ERROR;
1496 }
1497 return SUCCESS;
1498 }
1499
GetSceneTypeToChainCount(const std::string & sceneType)1500 uint32_t AudioEffectChainManager::GetSceneTypeToChainCount(const std::string &sceneType)
1501 {
1502 std::lock_guard<std::mutex> lock(dynamicMutex_);
1503
1504 if (sceneType == DEFAULT_SCENE_TYPE) {
1505 return defaultEffectChainCount_;
1506 }
1507 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1508 std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1509
1510 if (sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
1511 if (sceneTypeToEffectChainMap_.count(defaultSceneTypeAndDeviceKey) &&
1512 (sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] ==
1513 sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey])) {
1514 return 0;
1515 } else {
1516 return sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey];
1517 }
1518 }
1519 return 0;
1520 }
1521
FindMaxSessionID(uint32_t & maxSessionID,std::string & sceneType,const std::string & scenePairType,std::set<std::string> & sessions)1522 void AudioEffectChainManager::FindMaxSessionID(uint32_t &maxSessionID, std::string &sceneType,
1523 const std::string &scenePairType, std::set<std::string> &sessions)
1524 {
1525 for (auto &sessionID : sessions) {
1526 if (sessionIDToEffectInfoMap_[sessionID].sceneMode == "EFFECT_NONE") {
1527 continue;
1528 }
1529 uint32_t sessionIDInt = static_cast<uint32_t>(std::stoul(sessionID));
1530 if (sessionIDInt > maxSessionID) {
1531 maxSessionID = sessionIDInt;
1532 sceneType = scenePairType;
1533 }
1534 }
1535 }
1536
UpdateCurrSceneTypeAndStreamUsageForDsp()1537 void AudioEffectChainManager::UpdateCurrSceneTypeAndStreamUsageForDsp()
1538 {
1539 AudioEffectScene currSceneType;
1540 std::string maxSession = std::to_string(maxSessionID_);
1541 UpdateCurrSceneType(currSceneType, maxSessionIDToSceneType_);
1542 SetHdiParam(currSceneType);
1543 if (sessionIDToEffectInfoMap_.count(maxSession)) {
1544 SessionEffectInfo info = sessionIDToEffectInfoMap_[maxSession];
1545 effectHdiInput_[0] = HDI_STREAM_USAGE;
1546 effectHdiInput_[1] = info.streamUsage;
1547 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
1548 AUDIO_INFO_LOG("set hdi streamUsage: %{public}d", info.streamUsage);
1549 if (ret != SUCCESS) {
1550 AUDIO_WARNING_LOG("set hdi streamUsage failed");
1551 }
1552 }
1553 }
1554
NotifyAndCreateAudioEffectChain(const std::string & sceneType)1555 int32_t AudioEffectChainManager::NotifyAndCreateAudioEffectChain(const std::string &sceneType)
1556 {
1557 std::shared_ptr<AudioEffectChain> audioEffectChain = nullptr;
1558 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1559 std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1560 const std::unordered_map<AudioEffectMode, std::string> &audioSupportedSceneModes = GetAudioSupportedSceneModes();
1561
1562 bool isPriorScene = std::find(priorSceneList_.begin(), priorSceneList_.end(), sceneType) != priorSceneList_.end();
1563 audioEffectChain = CreateAudioEffectChain(sceneType, isPriorScene);
1564
1565 if (sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) &&
1566 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] != nullptr &&
1567 sceneTypeToEffectChainCountMap_.count(sceneTypeAndDeviceKey) &&
1568 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey] == 0) {
1569 if (defaultEffectChainCreated_ == true) {
1570 sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey] =
1571 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
1572 } else if (isDefaultEffectChainExisted_ == true) {
1573 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] = audioEffectChain;
1574 }
1575 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey] = 1;
1576 AUDIO_INFO_LOG("Reusing existing sceneTypeAndDeviceKey [%{public}s]", sceneTypeAndDeviceKey.c_str());
1577 cv_.notify_all();
1578 return SUCCESS;
1579 }
1580 if (defaultEffectChainCreated_ == true) {
1581 sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey] = audioEffectChain;
1582 }
1583 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] = audioEffectChain;
1584 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey] = 1;
1585 AUDIO_INFO_LOG("Create new sceneTypeAndDeviceKey [%{public}s]", sceneTypeAndDeviceKey.c_str());
1586 if (!audioSupportedSceneModes.count(EFFECT_DEFAULT)) {
1587 return ERROR;
1588 }
1589 std::string effectMode = audioSupportedSceneModes.find(EFFECT_DEFAULT)->second;
1590 if (!isPriorScene && !sceneTypeToSpecialEffectSet_.count(sceneType) && defaultEffectChainCount_ > 1) {
1591 return SUCCESS;
1592 }
1593 std::string createSceneType = (isPriorScene || sceneTypeToSpecialEffectSet_.count(sceneType) > 0) ?
1594 sceneType : DEFAULT_SCENE_TYPE;
1595 if (SetAudioEffectChainDynamic(createSceneType, effectMode) != SUCCESS) {
1596 return ERROR;
1597 }
1598 return SUCCESS;
1599 }
1600
CreateAudioEffectChainDynamicInner(const std::string & sceneType)1601 int32_t AudioEffectChainManager::CreateAudioEffectChainDynamicInner(const std::string &sceneType)
1602 {
1603 CHECK_AND_RETURN_RET_LOG(isInitialized_, ERROR, "has not been initialized");
1604 CHECK_AND_RETURN_RET_LOG(sceneType != "", ERROR, "null sceneType");
1605
1606 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1607 std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1608 if (sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) &&
1609 sceneTypeToEffectChainCountMap_.count(sceneTypeAndDeviceKey) &&
1610 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey] > 0) {
1611 if (sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] == nullptr) {
1612 sceneTypeToEffectChainMap_.erase(sceneTypeAndDeviceKey);
1613 sceneTypeToEffectChainCountMap_.erase(sceneTypeAndDeviceKey);
1614 AUDIO_WARNING_LOG("scene type %{public}s has null effect chain", sceneTypeAndDeviceKey.c_str());
1615 } else {
1616 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey]++;
1617 if (isDefaultEffectChainExisted_ && sceneTypeToEffectChainMap_.count(defaultSceneTypeAndDeviceKey) != 0 &&
1618 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] ==
1619 sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey]) {
1620 defaultEffectChainCount_++;
1621 }
1622 AUDIO_INFO_LOG("effect chain %{public}s still exist, current count: %{public}d, default count: %{public}d",
1623 sceneType.c_str(), sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey], defaultEffectChainCount_);
1624 return SUCCESS;
1625 }
1626 }
1627 return NotifyAndCreateAudioEffectChain(sceneType);
1628 }
1629
WaitAndReleaseEffectChain(const std::string & sceneType,const std::string & sceneTypeAndDeviceKey,const std::string & defaultSceneTypeAndDeviceKey,int32_t ret)1630 void AudioEffectChainManager::WaitAndReleaseEffectChain(const std::string &sceneType,
1631 const std::string &sceneTypeAndDeviceKey, const std::string &defaultSceneTypeAndDeviceKey, int32_t ret)
1632 {
1633 std::unique_lock<std::mutex> lock(dynamicMutex_);
1634
1635 auto condition = [this, sceneType, sceneTypeAndDeviceKey, defaultSceneTypeAndDeviceKey]() {
1636 return sceneTypeToEffectChainCountMap_.count(sceneTypeAndDeviceKey) &&
1637 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey] > 0;
1638 };
1639 if (cv_.wait_for(lock, std::chrono::milliseconds(RELEASE_WAIT_TIME_MS), condition)) {
1640 AUDIO_INFO_LOG("New sceneTypeAndDeviceKey [%{public}s] is being created, cancelling previous release",
1641 sceneTypeAndDeviceKey.c_str());
1642 return;
1643 }
1644
1645 if (sceneTypeToEffectChainCountMap_.count(sceneTypeAndDeviceKey) &&
1646 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey] == 0) {
1647 sceneTypeToEffectChainCountMap_.erase(sceneTypeAndDeviceKey);
1648 if (ret == SUCCESS && defaultEffectChainCount_ == 0 &&
1649 sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey] ==
1650 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey]) {
1651 sceneTypeToEffectChainMap_.erase(defaultSceneTypeAndDeviceKey);
1652 AUDIO_INFO_LOG("default effect chain is released");
1653 }
1654 sceneTypeToEffectChainMap_.erase(sceneTypeAndDeviceKey);
1655 AUDIO_INFO_LOG("sceneTypeAndDeviceKey [%{public}s] is being released", sceneTypeAndDeviceKey.c_str());
1656 }
1657 }
1658
ReleaseAudioEffectChainDynamicInner(const std::string & sceneType)1659 int32_t AudioEffectChainManager::ReleaseAudioEffectChainDynamicInner(const std::string &sceneType)
1660 {
1661 CHECK_AND_RETURN_RET_LOG(isInitialized_, ERROR, "has not been initialized");
1662 CHECK_AND_RETURN_RET_LOG(sceneType != "", ERROR, "null sceneType");
1663
1664 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1665 std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1666 if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) ||
1667 (sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] == nullptr) ||
1668 (sceneTypeToEffectChainCountMap_.count(sceneTypeAndDeviceKey) &&
1669 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey] == 0)) {
1670 sceneTypeToEffectChainCountMap_.erase(sceneTypeAndDeviceKey);
1671 AUDIO_WARNING_LOG("scene type %{public}s has null effect chain", sceneTypeAndDeviceKey.c_str());
1672 return SUCCESS;
1673 } else if (sceneTypeToEffectChainCountMap_.count(sceneTypeAndDeviceKey) &&
1674 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey] > 1) {
1675 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey]--;
1676 if (sceneTypeToEffectChainMap_.count(defaultSceneTypeAndDeviceKey) != 0 &&
1677 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] ==
1678 sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey]) {
1679 defaultEffectChainCount_--;
1680 }
1681 AUDIO_INFO_LOG("effect chain %{public}s still exist, current count: %{public}d, default count: %{public}d",
1682 sceneType.c_str(), sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey], defaultEffectChainCount_);
1683 return SUCCESS;
1684 }
1685 int32_t ret = CheckAndReleaseCommonEffectChain(sceneType);
1686 std::thread([this, sceneType, sceneTypeAndDeviceKey, defaultSceneTypeAndDeviceKey, ret]() {
1687 WaitAndReleaseEffectChain(sceneType, sceneTypeAndDeviceKey, defaultSceneTypeAndDeviceKey, ret);
1688 }).detach();
1689 return SUCCESS;
1690 }
1691
ExistAudioEffectChainInner(const std::string & sceneType,const std::string & effectMode)1692 bool AudioEffectChainManager::ExistAudioEffectChainInner(const std::string &sceneType, const std::string &effectMode)
1693 {
1694 if (!isInitialized_) {
1695 if (initializedLogFlag_) {
1696 AUDIO_ERR_LOG("audioEffectChainManager has not been initialized");
1697 initializedLogFlag_ = false;
1698 }
1699 return false;
1700 }
1701 initializedLogFlag_ = true;
1702 CHECK_AND_RETURN_RET(sceneType != "", false);
1703 CHECK_AND_RETURN_RET_LOG(GetDeviceTypeName() != "", false, "null deviceType");
1704
1705 if ((deviceType_ == DEVICE_TYPE_SPEAKER) && (spkOffloadEnabled_)) {
1706 return false;
1707 }
1708
1709 if ((deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) && (btOffloadEnabled_)) {
1710 return false;
1711 }
1712
1713 std::string effectChainKey = sceneType + "_&_" + effectMode + "_&_" + GetDeviceTypeName();
1714 if (!sceneTypeAndModeToEffectChainNameMap_.count(effectChainKey)) {
1715 return false;
1716 }
1717 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1718 // if the effectChain exist, see if it is empty
1719 if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) ||
1720 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] == nullptr) {
1721 return false;
1722 }
1723 auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
1724 return !audioEffectChain->IsEmptyEffectHandles();
1725 }
1726
UpdateMultichannelConfigInner(const std::string & sceneType)1727 int32_t AudioEffectChainManager::UpdateMultichannelConfigInner(const std::string &sceneType)
1728 {
1729 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1730 if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey)) {
1731 return ERROR;
1732 }
1733 uint32_t inputChannels = DEFAULT_NUM_CHANNEL;
1734 uint64_t inputChannelLayout = DEFAULT_NUM_CHANNELLAYOUT;
1735 ReturnEffectChannelInfoInner(sceneType, inputChannels, inputChannelLayout);
1736
1737 auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
1738 if (audioEffectChain == nullptr) {
1739 return ERROR;
1740 }
1741 audioEffectChain->UpdateMultichannelIoBufferConfig(inputChannels, inputChannelLayout);
1742 return SUCCESS;
1743 }
1744
UpdateSpatializationStateInner(AudioSpatializationState spatializationState)1745 int32_t AudioEffectChainManager::UpdateSpatializationStateInner(AudioSpatializationState spatializationState)
1746 {
1747 AUDIO_INFO_LOG("begin to update spatialization state, current state: %{public}d and %{public}d, previous state: \
1748 %{public}d and %{public}d", spatializationState.spatializationEnabled, spatializationState.headTrackingEnabled,
1749 spatializationEnabled_.load(), headTrackingEnabled_);
1750
1751 if (spatializationEnabled_ != spatializationState.spatializationEnabled) {
1752 UpdateSpatializationEnabled(spatializationState);
1753 }
1754 if (headTrackingEnabled_ != spatializationState.headTrackingEnabled) {
1755 headTrackingEnabled_ = spatializationState.headTrackingEnabled;
1756 UpdateSensorState();
1757 }
1758
1759 std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
1760 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
1761 EffectVolumeUpdateInner(audioEffectVolume);
1762 return SUCCESS;
1763 }
1764
ReturnEffectChannelInfoInner(const std::string & sceneType,uint32_t & channels,uint64_t & channelLayout)1765 int32_t AudioEffectChainManager::ReturnEffectChannelInfoInner(const std::string &sceneType, uint32_t &channels,
1766 uint64_t &channelLayout)
1767 {
1768 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1769 if (!sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) ||
1770 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] == nullptr) {
1771 return ERROR;
1772 }
1773 for (auto& scenePair : sceneTypeToSessionIDMap_) {
1774 std::string pairSceneTypeAndDeviceKey = scenePair.first + "_&_" + GetDeviceTypeName();
1775 if (sceneTypeToEffectChainMap_.count(pairSceneTypeAndDeviceKey) > 0 &&
1776 sceneTypeToEffectChainMap_[pairSceneTypeAndDeviceKey] != nullptr &&
1777 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] ==
1778 sceneTypeToEffectChainMap_[pairSceneTypeAndDeviceKey]) {
1779 FindMaxEffectChannels(scenePair.first, scenePair.second, channels, channelLayout);
1780 }
1781 }
1782 auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
1783 audioEffectChain->SetCurrChannelNoCheck(channels);
1784 audioEffectChain->SetCurrChannelLayoutNoCheck(channelLayout);
1785 return SUCCESS;
1786 }
1787
EffectVolumeUpdateInner(std::shared_ptr<AudioEffectVolume> audioEffectVolume)1788 int32_t AudioEffectChainManager::EffectVolumeUpdateInner(std::shared_ptr<AudioEffectVolume> audioEffectVolume)
1789 {
1790 int32_t ret;
1791 if (((deviceType_ == DEVICE_TYPE_SPEAKER) && (spkOffloadEnabled_)) ||
1792 ((deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) && (btOffloadEnabled_))) {
1793 ret = EffectDspVolumeUpdate(audioEffectVolume);
1794 } else {
1795 ret = EffectApVolumeUpdate(audioEffectVolume);
1796 }
1797 return ret;
1798 }
1799
InitEffectBuffer(const std::string & sessionID)1800 int32_t AudioEffectChainManager::InitEffectBuffer(const std::string &sessionID)
1801 {
1802 std::lock_guard<std::mutex> lock(dynamicMutex_);
1803 return InitEffectBufferInner(sessionID);
1804 }
1805
InitEffectBufferInner(const std::string & sessionID)1806 int32_t AudioEffectChainManager::InitEffectBufferInner(const std::string &sessionID)
1807 {
1808 if (sessionIDToEffectInfoMap_.find(sessionID) == sessionIDToEffectInfoMap_.end() ||
1809 sessionIDToEffectInfoMap_[sessionID].sceneMode == "EFFECT_NONE") {
1810 AUDIO_INFO_LOG("sessionIDToEffectInfoMap not find sessionID or sceneMode is EFFECT_NONE: %{public}s",
1811 sessionID.c_str());
1812 return SUCCESS;
1813 }
1814 std::string sceneTypeTemp = sessionIDToEffectInfoMap_[sessionID].sceneType;
1815 if (IsEffectChainStop(sceneTypeTemp, sessionID)) {
1816 AUDIO_INFO_LOG("sessionID: %{public}s sceneType: %{public}s: make init effect buffer",
1817 sessionID.c_str(), sceneTypeTemp.c_str());
1818 return InitAudioEffectChainDynamicInner(sceneTypeTemp);
1819 }
1820 AUDIO_INFO_LOG("sessionID: %{public}s, don't need init effect buffer", sessionID.c_str());
1821 return SUCCESS;
1822 }
1823
IsEffectChainStop(const std::string & sceneType,const std::string & sessionID)1824 bool AudioEffectChainManager::IsEffectChainStop(const std::string &sceneType, const std::string &sessionID)
1825 {
1826 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1827 CHECK_AND_RETURN_RET_LOG(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) > 0 &&
1828 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] != nullptr, false, "null audioEffectChain");
1829 auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
1830 for (auto it = sessionIDToEffectInfoMap_.begin(); it != sessionIDToEffectInfoMap_.end(); ++it) {
1831 if (it->first == sessionID || it->second.sceneMode == "EFFECT_NONE") {
1832 continue;
1833 }
1834 std::string sceneTypeTemp = it->second.sceneType;
1835 std::string sceneTypeAndDeviceKeyTemp = sceneTypeTemp + "_&_" + GetDeviceTypeName();
1836 CHECK_AND_RETURN_RET_LOG(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKeyTemp) > 0 &&
1837 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKeyTemp] != nullptr, false, "null audioEffectChain");
1838 auto audioEffectChainTemp = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKeyTemp];
1839 if (audioEffectChainTemp == audioEffectChain) {
1840 return false;
1841 }
1842 }
1843 return true;
1844 }
1845
CheckProcessClusterInstances(const std::string & sceneType)1846 ProcessClusterOperation AudioEffectChainManager::CheckProcessClusterInstances(const std::string &sceneType)
1847 {
1848 std::lock_guard<std::mutex> lock(dynamicMutex_);
1849 CHECK_AND_RETURN_RET_LOG(sceneType != "SCENE_EXTRA", CREATE_EXTRA_PROCESSCLUSTER, "scene type is extra");
1850 CHECK_AND_RETURN_RET_LOG(!GetOffloadEnabled(), USE_NONE_PROCESSCLUSTER, "offload, use none processCluster");
1851 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1852 std::string defaultSceneTypeAndDeviceKey = DEFAULT_SCENE_TYPE + "_&_" + GetDeviceTypeName();
1853
1854 if (sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) &&
1855 sceneTypeToEffectChainCountMap_.count(sceneTypeAndDeviceKey) &&
1856 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey] > 0) {
1857 if (sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] == nullptr) {
1858 AUDIO_WARNING_LOG("scene type %{public}s has null process cluster", sceneTypeAndDeviceKey.c_str());
1859 } else {
1860 AUDIO_INFO_LOG("process cluster already exist, current count: %{public}d, default count: %{public}d",
1861 sceneTypeToEffectChainCountMap_[sceneTypeAndDeviceKey], defaultEffectChainCount_);
1862 if (isDefaultEffectChainExisted_ && sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] ==
1863 sceneTypeToEffectChainMap_[defaultSceneTypeAndDeviceKey]) {
1864 return USE_DEFAULT_PROCESSCLUSTER;
1865 }
1866 return NO_NEED_TO_CREATE_PROCESSCLUSTER;
1867 }
1868 }
1869
1870 bool isPriorScene = std::find(priorSceneList_.begin(), priorSceneList_.end(), sceneType) != priorSceneList_.end();
1871 if (isPriorScene) {
1872 AUDIO_INFO_LOG("create prior process cluster: %{public}s", sceneType.c_str());
1873 return CREATE_NEW_PROCESSCLUSTER;
1874 }
1875 if ((maxEffectChainCount_ - static_cast<int32_t>(sceneTypeToSpecialEffectSet_.size())) > 1) {
1876 AUDIO_INFO_LOG("max audio process cluster count not reached, create special process cluster: %{public}s",
1877 sceneType.c_str());
1878 return CREATE_NEW_PROCESSCLUSTER;
1879 } else if (!isDefaultEffectChainExisted_) {
1880 AUDIO_INFO_LOG("max audio process cluster count reached, create current and default process cluster");
1881 return CREATE_DEFAULT_PROCESSCLUSTER;
1882 } else {
1883 AUDIO_INFO_LOG("max audio process cluster count reached and default already exist: %{public}d",
1884 defaultEffectChainCount_);
1885 return USE_DEFAULT_PROCESSCLUSTER;
1886 }
1887 }
1888
QueryEffectChannelInfo(const std::string & sceneType,uint32_t & channels,uint64_t & channelLayout)1889 int32_t AudioEffectChainManager::QueryEffectChannelInfo(const std::string &sceneType, uint32_t &channels,
1890 uint64_t &channelLayout)
1891 {
1892 std::lock_guard<std::mutex> lock(dynamicMutex_);
1893 return QueryEffectChannelInfoInner(sceneType, channels, channelLayout);
1894 }
1895
QueryEffectChannelInfoInner(const std::string & sceneType,uint32_t & channels,uint64_t & channelLayout)1896 int32_t AudioEffectChainManager::QueryEffectChannelInfoInner(const std::string &sceneType, uint32_t &channels,
1897 uint64_t &channelLayout)
1898 {
1899 channels = DEFAULT_NUM_CHANNEL;
1900 channelLayout = DEFAULT_NUM_CHANNELLAYOUT;
1901 std::string sceneTypeAndDeviceKey = sceneType + "_&_" + GetDeviceTypeName();
1902 CHECK_AND_RETURN_RET_LOG(sceneTypeToEffectChainMap_.count(sceneTypeAndDeviceKey) > 0 &&
1903 sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey] != nullptr, ERROR, "null audioEffectChain");
1904 auto audioEffectChain = sceneTypeToEffectChainMap_[sceneTypeAndDeviceKey];
1905 audioEffectChain->GetInputChannelInfo(channels, channelLayout);
1906 return SUCCESS;
1907 }
1908
SetAbsVolumeStateToEffect(const bool absVolumeState)1909 int32_t AudioEffectChainManager::SetAbsVolumeStateToEffect(const bool absVolumeState)
1910 {
1911 std::lock_guard<std::mutex> lock(dynamicMutex_);
1912 return SetAbsVolumeStateToEffectInner(absVolumeState);
1913 }
1914
SetAbsVolumeStateToEffectInner(const bool absVolumeState)1915 int32_t AudioEffectChainManager::SetAbsVolumeStateToEffectInner(const bool absVolumeState)
1916 {
1917 if (absVolumeState_ != absVolumeState) {
1918 EffectDspAbsVolumeStateUpdate(absVolumeState);
1919 EffectApAbsVolumeStateUpdate(absVolumeState);
1920 absVolumeState_ = absVolumeState;
1921 AUDIO_INFO_LOG("absVolumeStateUpdate absVolumeState: %{public}d", absVolumeState_);
1922 } else {
1923 AUDIO_INFO_LOG("absVolumeState is not changed");
1924 }
1925
1926 return SUCCESS;
1927 }
1928
EffectDspAbsVolumeStateUpdate(const bool absVolumeState)1929 int32_t AudioEffectChainManager::EffectDspAbsVolumeStateUpdate(const bool absVolumeState)
1930 {
1931 //send absVolumeState to dsp, but no use now
1932
1933 effectHdiInput_[0] = HDI_ABS_VOLUME_STATE;
1934 effectHdiInput_[1] = static_cast<int8_t>(absVolumeState);
1935 int32_t ret = audioEffectHdiParam_->UpdateHdiState(effectHdiInput_);
1936 AUDIO_INFO_LOG("absVolumeState change, new state: %{public}d, previous state: %{public}d, ret: %{public}d",
1937 effectHdiInput_[1], absVolumeState_, ret);
1938 return SUCCESS;
1939 }
1940
EffectApAbsVolumeStateUpdate(const bool absVolumeState)1941 int32_t AudioEffectChainManager::EffectApAbsVolumeStateUpdate(const bool absVolumeState)
1942 {
1943 //send absVolumeState to ap
1944
1945 for (auto it = sceneTypeToEffectChainMap_.begin(); it != sceneTypeToEffectChainMap_.end(); it++) {
1946 auto audioEffectChain = it->second;
1947 if (audioEffectChain == nullptr) {
1948 continue;
1949 }
1950
1951 audioEffectChain->SetAbsVolumeStateToEffectChain(absVolumeState);
1952 int32_t ret = audioEffectChain->UpdateEffectParam();
1953 CHECK_AND_CONTINUE_LOG(ret == 0, "set ap absVolumeState failed");
1954 AUDIO_INFO_LOG("The delay of SceneType %{public}s is %{public}u, new state: %{public}d, "
1955 "previous state: %{public}d",
1956 it->first.c_str(), audioEffectChain->GetLatency(), absVolumeState, absVolumeState_);
1957 }
1958
1959 return SUCCESS;
1960 }
1961 } // namespace AudioStandard
1962 } // namespace OHOS
1963