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