1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef LOG_TAG
16 #define LOG_TAG "AudioEffectManager"
17 #endif
18 
19 #include "audio_effect_manager.h"
20 
21 namespace OHOS {
22 namespace AudioStandard {
AudioEffectManager()23 AudioEffectManager::AudioEffectManager()
24 {
25     AUDIO_INFO_LOG("AudioEffectManager ctor");
26 }
27 
~AudioEffectManager()28 AudioEffectManager::~AudioEffectManager()
29 {
30 }
31 
EffectManagerInit()32 void AudioEffectManager::EffectManagerInit()
33 {
34     // load XML
35     std::unique_ptr<AudioEffectConfigParser> effectConfigParser = std::make_unique<AudioEffectConfigParser>();
36     int32_t ret = effectConfigParser->LoadEffectConfig(oriEffectConfig_);
37     CHECK_AND_RETURN_LOG(ret == 0, "AudioEffectManager->effectConfigParser failed: %{public}d", ret);
38 }
39 
GetAvailableEffects(std::vector<Effect> & availableEffects)40 void AudioEffectManager::GetAvailableEffects(std::vector<Effect> &availableEffects)
41 {
42     availableEffects = availableEffects_;
43 }
44 
GetOriginalEffectConfig(OriginalEffectConfig & oriEffectConfig)45 void AudioEffectManager::GetOriginalEffectConfig(OriginalEffectConfig &oriEffectConfig)
46 {
47     oriEffectConfig = oriEffectConfig_;
48 }
49 
UpdateAvailableEffects(std::vector<Effect> & newAvailableEffects)50 void AudioEffectManager::UpdateAvailableEffects(std::vector<Effect> &newAvailableEffects)
51 {
52     availableEffects_ = newAvailableEffects;
53 }
54 
QueryEffectManagerSceneMode(SupportedEffectConfig & supportedEffectConfig)55 int32_t AudioEffectManager::QueryEffectManagerSceneMode(SupportedEffectConfig &supportedEffectConfig)
56 {
57     supportedEffectConfig = supportedEffectConfig_;
58     return existDefault_;
59 }
60 
GetSupportedEffectConfig(SupportedEffectConfig & supportedEffectConfig)61 void AudioEffectManager::GetSupportedEffectConfig(SupportedEffectConfig &supportedEffectConfig)
62 {
63     supportedEffectConfig = supportedEffectConfig_;
64 }
65 
UpdateUnsupportedDevicePre(PreStreamScene & pp,Stream & stream,const std::string & mode,int32_t i,int32_t j)66 static void UpdateUnsupportedDevicePre(PreStreamScene &pp, Stream &stream, const std::string &mode,
67                                        int32_t i, int32_t j)
68 {
69     StreamEffectMode streamEffectMode;
70     streamEffectMode.mode = mode;
71     j = 0;
72     for (auto &device: pp.device) {
73         if (i == j) {
74             for (auto &eachDevice: device) {
75                 streamEffectMode.devicePort.push_back(eachDevice);
76             }
77             break;
78         }
79         j += 1;
80     }
81     stream.streamEffectMode.push_back(streamEffectMode);
82 }
83 
UpdateUnsupportedModePre(PreStreamScene & pp,Stream & stream,std::string & mode,int32_t i)84 static void UpdateUnsupportedModePre(PreStreamScene &pp, Stream &stream, std::string &mode, int32_t i)
85 {
86     int32_t isSupported = 0;
87     if ((mode != "EFFECT_NONE") &&
88         (mode != "EFFECT_DEFAULT")) {
89         AUDIO_INFO_LOG("[supportedEffectConfig LOG10]:mode-> The %{public}s mode of %{public}s is unsupported, \
90             and this mode is deleted!", mode.c_str(), stream.scene.c_str());
91         isSupported = -1;
92     }
93     if (isSupported == 0) {
94         int32_t j = 0;
95         UpdateUnsupportedDevicePre(pp, stream, mode, i, j);
96     }
97 }
98 
UpdateUnsupportedDevicePost(PostStreamScene & ess,Stream & stream,const std::string & mode,int32_t i)99 static void UpdateUnsupportedDevicePost(PostStreamScene &ess, Stream &stream, const std::string &mode, int32_t i)
100 {
101     StreamEffectMode streamEffectMode;
102     streamEffectMode.mode = mode;
103     int32_t j = 0;
104     for (auto &device: ess.device) {
105         if (i == j) {
106             for (auto &a: device) {
107                 streamEffectMode.devicePort.push_back(a);
108             }
109             break;
110         }
111         j += 1;
112     }
113     stream.streamEffectMode.push_back(streamEffectMode);
114 }
115 
UpdateUnsupportedModePost(PostStreamScene & ess,Stream & stream,std::string & mode,int32_t i)116 static void UpdateUnsupportedModePost(PostStreamScene &ess, Stream &stream, std::string &mode, int32_t i)
117 {
118     int32_t isSupported = 0;
119     if ((mode != "EFFECT_NONE") &&
120         (mode != "EFFECT_DEFAULT")) {
121         AUDIO_INFO_LOG("[supportedEffectConfig LOG10]:mode-> The %{public}s mode of %{public}s is unsupported, \
122             and this mode is deleted!", mode.c_str(), stream.scene.c_str());
123         isSupported = -1;
124     }
125     if (isSupported == 0) {
126         UpdateUnsupportedDevicePost(ess, stream, mode, i);
127     }
128 }
129 
UpdateAvailableStreamPre(ProcessNew & preProcessNew,PreStreamScene & pp,ScenePriority priority)130 static int32_t UpdateAvailableStreamPre(ProcessNew &preProcessNew, PreStreamScene &pp, ScenePriority priority)
131 {
132     bool isDuplicate = false;
133     bool isSupported = false;
134     for (auto &[scene, stream] : AUDIO_ENHANCE_SUPPORTED_SCENE_TYPES) {
135         if (pp.stream == stream) {
136             isSupported = true;
137             break;
138         }
139     }
140     auto it = std::find_if(preProcessNew.stream.begin(), preProcessNew.stream.end(), [&](const Stream &x) {
141         return ((x.scene == pp.stream) && (x.priority == priority));
142     });
143     if ((it == preProcessNew.stream.end()) && isSupported) {
144         Stream stream;
145         stream.priority = priority;
146         stream.scene = pp.stream;
147         int32_t i = 0;
148         for (auto &mode: pp.mode) {
149             UpdateUnsupportedModePre(pp, stream, mode, i);
150         }
151         preProcessNew.stream.push_back(stream);
152     } else if (it != preProcessNew.stream.end()) {
153         isDuplicate = true;
154     }
155     return isDuplicate;
156 }
157 
UpdateAvailableStreamPost(ProcessNew & postProcessNew,PostStreamScene & ess,ScenePriority priority)158 static int32_t UpdateAvailableStreamPost(ProcessNew &postProcessNew, PostStreamScene &ess, ScenePriority priority)
159 {
160     bool isDuplicate = false;
161     bool isSupported = false;
162     for (auto &[scene, stream] : AUDIO_SUPPORTED_SCENE_TYPES) {
163         if (ess.stream == stream) {
164             isSupported = true;
165             break;
166         }
167     }
168     auto it = std::find_if(postProcessNew.stream.begin(), postProcessNew.stream.end(), [&](const Stream &x) {
169         return ((x.scene == ess.stream) && (x.priority == priority));
170     });
171     if ((it == postProcessNew.stream.end()) && isSupported) {
172         Stream stream;
173         stream.priority = priority;
174         stream.scene = ess.stream;
175         int32_t i = 0;
176         for (auto &mode: ess.mode) {
177             UpdateUnsupportedModePost(ess, stream, mode, i);
178         }
179         postProcessNew.stream.push_back(stream);
180     } else if (it != postProcessNew.stream.end()) {
181         isDuplicate = true;
182     }
183     return isDuplicate;
184 }
185 
UpdateAvailableSceneMapPost(SceneMappingItem & item,std::vector<SceneMappingItem> & postProcessSceneMap)186 static int32_t UpdateAvailableSceneMapPost(SceneMappingItem &item, std::vector<SceneMappingItem> &postProcessSceneMap)
187 {
188     bool isDuplicate = false;
189     auto it = std::find_if(postProcessSceneMap.begin(), postProcessSceneMap.end(),
190         [&item](const SceneMappingItem &x) {
191         return x.name == item.name;
192     });
193     if ((it == postProcessSceneMap.end())) {
194         postProcessSceneMap.push_back(item);
195     } else {
196         isDuplicate = true;
197     }
198     return isDuplicate;
199 }
200 
VerifySceneMappingItem(const SceneMappingItem & item)201 bool AudioEffectManager::VerifySceneMappingItem(const SceneMappingItem &item)
202 {
203     return STREAM_USAGE_MAP.find(item.name) != STREAM_USAGE_MAP.end() &&
204         std::find(postSceneTypeSet_.begin(), postSceneTypeSet_.end(), item.sceneType) != postSceneTypeSet_.end();
205 }
206 
UpdateEffectChains(std::vector<std::string> & availableLayout)207 void AudioEffectManager::UpdateEffectChains(std::vector<std::string> &availableLayout)
208 {
209     int32_t count = 0;
210     std::vector<int> deviceDelIdx;
211     for (const auto &ec: supportedEffectConfig_.effectChains) {
212         for (auto &effectName: ec.apply) {
213             auto it = std::find_if(availableEffects_.begin(), availableEffects_.end(),
214                 [&effectName](const Effect &effect) {
215                 return effect.name == effectName;
216             });
217             if (it == availableEffects_.end()) {
218                 deviceDelIdx.emplace_back(count);
219                 break;
220             }
221         }
222         count += 1;
223     }
224     for (auto it = deviceDelIdx.rbegin(); it != deviceDelIdx.rend(); ++it) {
225         supportedEffectConfig_.effectChains.erase(supportedEffectConfig_.effectChains.begin() + *it);
226     }
227     if (supportedEffectConfig_.effectChains.empty()) {
228         AUDIO_INFO_LOG("[supportedEffectConfig LOG1]:effectChains-> all effectChains are unavailable");
229     }
230     for (auto ec: supportedEffectConfig_.effectChains) {
231         availableLayout.emplace_back(ec.name);
232     }
233 }
234 
UpdateAvailableAEConfig(OriginalEffectConfig & aeConfig)235 void AudioEffectManager::UpdateAvailableAEConfig(OriginalEffectConfig &aeConfig)
236 {
237     int32_t ret = 0;
238     supportedEffectConfig_.effectChains = aeConfig.effectChains;
239     ProcessNew preProcessNew;
240     for (PreStreamScene &pp: aeConfig.preProcess.defaultScenes) {
241         ret += UpdateAvailableStreamPre(preProcessNew, pp, DEFAULT_SCENE);
242     }
243     for (PreStreamScene &pp: aeConfig.preProcess.priorScenes) {
244         ret += UpdateAvailableStreamPre(preProcessNew, pp, PRIOR_SCENE);
245     }
246     for (PreStreamScene &pp: aeConfig.preProcess.normalScenes) {
247         ret += UpdateAvailableStreamPre(preProcessNew, pp, NORMAL_SCENE);
248     }
249 
250     ProcessNew postProcessNew;
251     for (PostStreamScene &ess: aeConfig.postProcess.defaultScenes) {
252         ret += UpdateAvailableStreamPost(postProcessNew, ess, DEFAULT_SCENE);
253     }
254     for (PostStreamScene &ess: aeConfig.postProcess.priorScenes) {
255         ret += UpdateAvailableStreamPost(postProcessNew, ess, PRIOR_SCENE);
256     }
257     for (PostStreamScene &ess: aeConfig.postProcess.normalScenes) {
258         ret += UpdateAvailableStreamPost(postProcessNew, ess, NORMAL_SCENE);
259     }
260 
261     if (ret > 0) {
262         AUDIO_INFO_LOG("[supportedEffectConfig LOG2]:stream-> duplicate streams has been deleted");
263     }
264     supportedEffectConfig_.preProcessNew = preProcessNew;
265     supportedEffectConfig_.postProcessNew = postProcessNew;
266 
267     for (Stream &ss: supportedEffectConfig_.postProcessNew.stream) {
268         postSceneTypeSet_.push_back(ss.scene);
269     }
270     AUDIO_INFO_LOG("postSceneTypeSet_ size is %{public}zu", supportedEffectConfig_.postProcessNew.stream.size());
271     std::vector<SceneMappingItem> postSceneMap;
272     for (SceneMappingItem &item: aeConfig.postProcess.sceneMap) {
273         if (!VerifySceneMappingItem(item)) {
274             AUDIO_WARNING_LOG("Invalid %{public}s-%{public}s pair has been ignored",
275                 item.name.c_str(), item.sceneType.c_str());
276             continue;
277         }
278         if (UpdateAvailableSceneMapPost(item, postSceneMap)) {
279             AUDIO_WARNING_LOG("The duplicate streamUsage-sceneType pair is deleted, \
280                 and the first configuration is retained!");
281         }
282     }
283     supportedEffectConfig_.postProcessSceneMap = postSceneMap;
284 }
285 
UpdateDuplicateBypassMode(ProcessNew & processNew)286 void AudioEffectManager::UpdateDuplicateBypassMode(ProcessNew &processNew)
287 {
288     int32_t flag = 0;
289     std::vector<int32_t> deviceDelIdx;
290     for (auto &stream: processNew.stream) {
291         int32_t count = 0;
292         deviceDelIdx.clear();
293         for (const auto &streamEffectMode: stream.streamEffectMode) {
294             if (streamEffectMode.mode == "EFFECT_NONE") {
295                 deviceDelIdx.push_back(count);
296             }
297             count += 1;
298         }
299         for (auto it = deviceDelIdx.rbegin(); it != deviceDelIdx.rend(); ++it) {
300             stream.streamEffectMode[*it].devicePort = {};
301             flag = -1;
302         }
303     }
304     if (flag == -1) {
305         AUDIO_INFO_LOG("[supportedEffectConfig LOG3]:mode-> EFFECT_NONE can not configure by deveploer!");
306     }
307 }
308 
UpdateDuplicateMode(ProcessNew & processNew)309 void AudioEffectManager::UpdateDuplicateMode(ProcessNew &processNew)
310 {
311     std::unordered_set<std::string> seen;
312     std::vector<int32_t> toRemove;
313     uint32_t i;
314     for (auto &stream: processNew.stream) {
315         seen.clear();
316         toRemove.clear();
317         for (i = 0; i < stream.streamEffectMode.size(); i++) {
318             if (seen.count(stream.streamEffectMode[i].mode)) {
319                 toRemove.push_back(i);
320             } else {
321                 seen.insert(stream.streamEffectMode[i].mode);
322             }
323         }
324         for (auto it = toRemove.rbegin(); it != toRemove.rend(); ++it) {
325             AUDIO_INFO_LOG("[supportedEffectConfig LOG4]:mode-> The duplicate mode of %{public}s configuration \
326                 is deleted, and the first configuration is retained!", stream.scene.c_str());
327             stream.streamEffectMode.erase(stream.streamEffectMode.begin() + *it);
328         }
329     }
330 }
331 
UpdateDuplicateDeviceRecord(StreamEffectMode & streamEffectMode,Stream & stream)332 static void UpdateDuplicateDeviceRecord(StreamEffectMode &streamEffectMode, Stream &stream)
333 {
334     uint32_t i;
335     std::unordered_set<std::string> seen;
336     std::vector<int32_t> toRemove;
337     seen.clear();
338     toRemove.clear();
339     for (i = 0; i < streamEffectMode.devicePort.size(); i++) {
340         if (seen.count(streamEffectMode.devicePort[i].type)) {
341             toRemove.push_back(i);
342         } else {
343             seen.insert(streamEffectMode.devicePort[i].type);
344         }
345     }
346     for (auto it = toRemove.rbegin(); it != toRemove.rend(); ++it) {
347         AUDIO_INFO_LOG("[supportedEffectConfig LOG5]:device-> The duplicate device of %{public}s's %{public}s \
348             mode configuration is deleted, and the first configuration is retained!",
349             stream.scene.c_str(), streamEffectMode.mode.c_str());
350         streamEffectMode.devicePort.erase(streamEffectMode.devicePort.begin() + *it);
351     }
352 }
353 
UpdateDuplicateDevice(ProcessNew & processNew)354 void AudioEffectManager::UpdateDuplicateDevice(ProcessNew &processNew)
355 {
356     for (auto &stream: processNew.stream) {
357         for (auto &streamEffectMode: stream.streamEffectMode) {
358             UpdateDuplicateDeviceRecord(streamEffectMode, stream);
359         }
360     }
361 }
362 
UpdateDuplicateScene(ProcessNew & processNew)363 void AudioEffectManager::UpdateDuplicateScene(ProcessNew &processNew)
364 {
365     // erase duplicate scene
366     std::unordered_set<std::string> scenes;
367     for (auto it = processNew.stream.begin(); it != processNew.stream.end();) {
368         auto &stream = *it;
369         auto its = scenes.find(stream.scene);
370         if (its == scenes.end()) {
371             scenes.insert(stream.scene);
372         } else {
373             if (stream.priority == NORMAL_SCENE) {
374                 it = processNew.stream.erase(it);
375                 continue;
376             }
377         }
378         ++it;
379     }
380 }
381 
UpdateDuplicateDefaultScene(ProcessNew & processNew)382 void AudioEffectManager::UpdateDuplicateDefaultScene(ProcessNew &processNew)
383 {
384     // erase duplicate default scene
385     bool flag = false;
386     for (auto it = processNew.stream.begin(); it != processNew.stream.end();) {
387         const auto &stream = *it;
388         if (stream.priority == DEFAULT_SCENE) {
389             if (flag) {
390                 it = processNew.stream.erase(it);
391                 continue;
392             }
393             flag = true;
394         }
395         ++it;
396     }
397 
398     // add default scene if no default
399     if (!flag) {
400         for (auto it = processNew.stream.begin(); it != processNew.stream.end(); ++it) {
401             auto &stream = *it;
402             if (stream.priority == NORMAL_SCENE) {
403                 stream.priority = DEFAULT_SCENE;
404                 break;
405             }
406         }
407     }
408 }
409 
UpdateUnavailableModes(std::vector<int32_t> & modeDelIdx,Stream & stream)410 static int32_t UpdateUnavailableModes(std::vector<int32_t> &modeDelIdx, Stream &stream)
411 {
412     int32_t ret = 0;
413     for (auto it = modeDelIdx.rbegin(); it != modeDelIdx.rend(); ++it) {
414         AUDIO_INFO_LOG("[supportedEffectConfig LOG7]:mode-> %{public}s's %{public}s mode is deleted!",
415             stream.scene.c_str(), stream.streamEffectMode[*it].mode.c_str());
416         if (stream.streamEffectMode[*it].mode == "PLAYBACK_DEAFULT") {
417             ret = -1;
418         }
419         stream.streamEffectMode.erase(stream.streamEffectMode.begin() + *it);
420         if (stream.streamEffectMode.empty()) {
421             AUDIO_INFO_LOG("[supportedEffectConfig LOG8]:mode-> %{public}s's mode is only EFFECT_NONE!",
422                 stream.scene.c_str());
423             StreamEffectMode streamEffectMode;
424             streamEffectMode.mode = "EFFECT_NONE";
425             stream.streamEffectMode.push_back(streamEffectMode);
426         }
427     }
428     if (stream.streamEffectMode.empty()) {
429         AUDIO_INFO_LOG("[supportedEffectConfig LOG8]:mode-> %{public}s's mode is only EFFECT_NONE!",
430             stream.scene.c_str());
431         StreamEffectMode streamEffectMode;
432         streamEffectMode.mode = "EFFECT_NONE";
433         stream.streamEffectMode.push_back(streamEffectMode);
434     }
435     return ret;
436 }
437 
UpdateUnavailableEffectChainsRecord(std::vector<std::string> & availableLayout,Stream & stream,StreamEffectMode & streamEffectMode,std::vector<int32_t> & modeDelIdx,int32_t modeCount)438 static void UpdateUnavailableEffectChainsRecord(std::vector<std::string> &availableLayout, Stream &stream,
439     StreamEffectMode &streamEffectMode, std::vector<int32_t> &modeDelIdx, int32_t modeCount)
440 {
441     std::vector<int32_t> deviceDelIdx;
442     deviceDelIdx.clear();
443     int32_t deviceCount = 0;
444     if (streamEffectMode.devicePort.empty()) {
445         modeDelIdx.push_back(modeCount);
446     }
447     for (auto &devicePort: streamEffectMode.devicePort) {
448         auto index = std::find(availableLayout.begin(), availableLayout.end(), devicePort.chain);
449         if (index == availableLayout.end()) {
450             deviceDelIdx.push_back(deviceCount);
451         }
452         deviceCount += 1;
453     }
454     if (streamEffectMode.devicePort.size() != deviceDelIdx.size() && deviceDelIdx.size() != 0) {
455         AUDIO_INFO_LOG("[supportedEffectConfig LOG6]:device-> The unavailable effectChain \
456             of %{public}s's %{public}s mode are set to LAYOUT_BYPASS!",
457             stream.scene.c_str(), streamEffectMode.mode.c_str());
458         for (auto it = deviceDelIdx.rbegin(); it != deviceDelIdx.rend(); ++it) {
459             streamEffectMode.devicePort[*it].chain = "LAYOUT_BYPASS";
460         }
461     } else {
462         for (auto it = deviceDelIdx.rbegin(); it != deviceDelIdx.rend(); ++it) {
463             streamEffectMode.devicePort.erase(streamEffectMode.devicePort.begin() + *it);
464             if (streamEffectMode.devicePort.empty()) {
465                 modeDelIdx.push_back(modeCount);
466             }
467         }
468     }
469 }
470 
UpdateUnavailableEffectChains(std::vector<std::string> & availableLayout,ProcessNew & processNew)471 int32_t AudioEffectManager::UpdateUnavailableEffectChains(std::vector<std::string> &availableLayout,
472     ProcessNew &processNew)
473 {
474     int32_t ret = 0;
475 
476     std::vector<int32_t> modeDelIdx;
477     for (auto &stream: processNew.stream) {
478         modeDelIdx.clear();
479         int32_t modeCount = 0;
480         for (auto &streamEffectMode: stream.streamEffectMode) {
481             UpdateUnavailableEffectChainsRecord(availableLayout, stream, streamEffectMode, modeDelIdx, modeCount);
482         }
483         ret = UpdateUnavailableModes(modeDelIdx, stream);
484     }
485     return ret;
486 }
487 
BuildAvailableAEConfig()488 void AudioEffectManager::BuildAvailableAEConfig()
489 {
490     int32_t ret = 0 ;
491     std::vector<std::string> availableLayout;
492     existDefault_ = 1;
493     if (oriEffectConfig_.effectChains.size() == 0) {
494         AUDIO_INFO_LOG("[supportedEffectConfig LOG12]: effectChains is none!");
495     }
496     if (oriEffectConfig_.preProcess.defaultScenes.size() != 1) {
497         AUDIO_INFO_LOG("[supportedEffectConfig LOG13]: pre-defaultScene is not one!");
498     }
499 
500     if (oriEffectConfig_.preProcess.normalScenes.size() == 0) {
501         AUDIO_INFO_LOG("[supportedEffectConfig LOG14]: pre-normalScene is none!");
502     }
503 
504     if (oriEffectConfig_.postProcess.defaultScenes.size() != 1) {
505         AUDIO_INFO_LOG("[supportedEffectConfig LOG15]: post-defaultScene is not one!");
506     }
507     if (oriEffectConfig_.postProcess.normalScenes.size() == 0) {
508         AUDIO_INFO_LOG("[supportedEffectConfig LOG16]: post-normalScene is none!");
509     }
510 
511     // update maxExtraSceneNum
512 
513     // Update duplicate defined modes, devices, and unsupported effect chain.
514     UpdateAvailableAEConfig(oriEffectConfig_);
515 
516     ProcessNew preProcessNew = supportedEffectConfig_.preProcessNew;
517     ProcessNew postProcessNew = supportedEffectConfig_.postProcessNew;
518 
519     UpdateEffectChains(availableLayout);
520     UpdateDuplicateBypassMode(supportedEffectConfig_.preProcessNew);
521     UpdateDuplicateMode(supportedEffectConfig_.preProcessNew);
522     UpdateDuplicateDevice(supportedEffectConfig_.preProcessNew);
523     UpdateDuplicateDefaultScene(supportedEffectConfig_.preProcessNew);
524     UpdateDuplicateScene(supportedEffectConfig_.preProcessNew);
525     ret = UpdateUnavailableEffectChains(availableLayout, supportedEffectConfig_.preProcessNew);
526     if (ret != 0) {
527         existDefault_ = -1;
528     }
529 
530     UpdateDuplicateBypassMode(supportedEffectConfig_.postProcessNew);
531     UpdateDuplicateMode(supportedEffectConfig_.postProcessNew);
532     UpdateDuplicateDevice(supportedEffectConfig_.postProcessNew);
533     UpdateDuplicateDefaultScene(supportedEffectConfig_.postProcessNew);
534     UpdateDuplicateScene(supportedEffectConfig_.postProcessNew);
535     ret = UpdateUnavailableEffectChains(availableLayout, supportedEffectConfig_.postProcessNew);
536     if (ret != 0) {
537         existDefault_ = -1;
538     }
539 }
540 
SetMasterSinkAvailable()541 void AudioEffectManager::SetMasterSinkAvailable()
542 {
543     isMasterSinkAvailable_ = true;
544 }
545 
SetEffectChainManagerAvailable()546 void AudioEffectManager::SetEffectChainManagerAvailable()
547 {
548     isEffectChainManagerAvailable_ = true;
549 }
550 
CanLoadEffectSinks()551 bool AudioEffectManager::CanLoadEffectSinks()
552 {
553     return (isMasterSinkAvailable_ && isEffectChainManagerAvailable_);
554 }
555 
556 template <typename T>
AddKeyValueIntoMap(std::unordered_map<T,std::string> & map,std::string & key,std::string & value)557 void AddKeyValueIntoMap(std::unordered_map<T, std::string> &map, std::string &key, std::string &value)
558 {
559     if (map.count(key)) { // if the key already register in map
560         return;
561     }
562     map[key] = value;
563 }
564 
ConstructEffectChainManagerParam(EffectChainManagerParam & effectChainMgrParam)565 void AudioEffectManager::ConstructEffectChainManagerParam(EffectChainManagerParam &effectChainMgrParam)
566 {
567     std::unordered_map<std::string, std::string> &map = effectChainMgrParam.sceneTypeToChainNameMap;
568     effectChainMgrParam.maxExtraNum = static_cast <uint32_t>(oriEffectConfig_.postProcess.maxExtSceneNum);
569     std::string sceneType;
570     std::string sceneMode;
571     std::string key;
572     for (auto &scene: supportedEffectConfig_.postProcessNew.stream) {
573         sceneType = scene.scene;
574         if (scene.priority == PRIOR_SCENE) {
575             effectChainMgrParam.priorSceneList.push_back(sceneType);
576         }
577         if (scene.priority == DEFAULT_SCENE) {
578             effectChainMgrParam.defaultSceneName = sceneType;
579         }
580         for (auto &mode: scene.streamEffectMode) {
581             sceneMode = mode.mode;
582             for (auto &device: mode.devicePort) {
583                 key = sceneType + "_&_" + sceneMode + "_&_" + device.type;
584                 AddKeyValueIntoMap(map, key, device.chain);
585             }
586         }
587     }
588     AUDIO_INFO_LOG("Constructed SceneTypeAndModeToEffectChainNameMap at policy, size is %{public}d",
589         (int32_t)map.size());
590 }
591 
ConstructEnhanceChainManagerParam(EffectChainManagerParam & enhanceChainMgrParam)592 void AudioEffectManager::ConstructEnhanceChainManagerParam(EffectChainManagerParam &enhanceChainMgrParam)
593 {
594     std::unordered_map<std::string, std::string> &map = enhanceChainMgrParam.sceneTypeToChainNameMap;
595     enhanceChainMgrParam.maxExtraNum = static_cast <uint32_t>(oriEffectConfig_.preProcess.maxExtSceneNum);
596 
597     std::string sceneType;
598     std::string sceneMode;
599     std::string key;
600 
601     for (auto &scene: supportedEffectConfig_.preProcessNew.stream) {
602         sceneType = scene.scene;
603         if (scene.priority == PRIOR_SCENE) {
604             enhanceChainMgrParam.priorSceneList.push_back(sceneType);
605         }
606         if (scene.priority == DEFAULT_SCENE) {
607             enhanceChainMgrParam.defaultSceneName = sceneType;
608         }
609 
610         for (auto &mode: scene.streamEffectMode) {
611             sceneMode = mode.mode;
612             for (auto &device: mode.devicePort) {
613                 key = sceneType + "_&_" + sceneMode;
614                 AddKeyValueIntoMap(map, key, device.chain);
615             }
616         }
617     }
618     AUDIO_INFO_LOG("Constructed SceneTypeAndModeToEnhanceChainNameMap at policy, size is %{public}d",
619         (int32_t)map.size());
620 }
621 
622 } // namespce AudioStandard
623 } // namespace OHOS
624