• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #undef LOG_TAG
16 #define LOG_TAG "AudioEffectService"
17 
18 #include "audio_effect_service.h"
19 
20 #include <unordered_set>
21 
22 #include "audio_device_type.h"
23 #include "audio_effect_map.h"
24 
25 namespace OHOS {
26 namespace AudioStandard {
27 const std::set<std::string> STREAM_USAGE_SET = {
28     "STREAM_USAGE_UNKNOWN",
29     "STREAM_USAGE_MEDIA",
30     "STREAM_USAGE_MUSIC",
31     "STREAM_USAGE_VOICE_COMMUNICATION",
32     "STREAM_USAGE_VOICE_ASSISTANT",
33     "STREAM_USAGE_VOICE_CALL_ASSISTANT",
34     "STREAM_USAGE_ALARM",
35     "STREAM_USAGE_VOICE_MESSAGE",
36     "STREAM_USAGE_NOTIFICATION_RINGTONE",
37     "STREAM_USAGE_RINGTONE",
38     "STREAM_USAGE_NOTIFICATION",
39     "STREAM_USAGE_ACCESSIBILITY",
40     "STREAM_USAGE_SYSTEM",
41     "STREAM_USAGE_MOVIE",
42     "STREAM_USAGE_GAME",
43     "STREAM_USAGE_AUDIOBOOK",
44     "STREAM_USAGE_NAVIGATION",
45     "STREAM_USAGE_DTMF",
46     "STREAM_USAGE_ENFORCED_TONE",
47     "STREAM_USAGE_ULTRASONIC",
48     "STREAM_USAGE_VIDEO_COMMUNICATION",
49     "STREAM_USAGE_RANGING",
50     "STREAM_USAGE_VOICE_MODEM_COMMUNICATION",
51     "STREAM_USAGE_VOICE_RINGTONE"
52 };
AudioEffectService()53 AudioEffectService::AudioEffectService()
54 {
55     AUDIO_INFO_LOG("AudioEffectService ctor");
56 }
57 
~AudioEffectService()58 AudioEffectService::~AudioEffectService()
59 {
60 }
61 
EffectServiceInit()62 void AudioEffectService::EffectServiceInit()
63 {
64     AUDIO_INFO_LOG("In");
65     // load XML
66     std::unique_ptr<AudioEffectConfigParser> effectConfigParser = std::make_unique<AudioEffectConfigParser>();
67     int32_t ret = effectConfigParser->LoadEffectConfig(oriEffectConfig_);
68     CHECK_AND_RETURN_LOG(ret == 0, "AudioEffectService->effectConfigParser failed: %{public}d", ret);
69     AUDIO_INFO_LOG("Out");
70 }
71 
GetAvailableEffects(std::vector<Effect> & availableEffects)72 void AudioEffectService::GetAvailableEffects(std::vector<Effect> &availableEffects)
73 {
74     availableEffects = availableEffects_;
75 }
76 
GetOriginalEffectConfig(OriginalEffectConfig & oriEffectConfig)77 void AudioEffectService::GetOriginalEffectConfig(OriginalEffectConfig &oriEffectConfig)
78 {
79     oriEffectConfig = oriEffectConfig_;
80 }
81 
UpdateAvailableEffects(std::vector<Effect> & newAvailableEffects)82 void AudioEffectService::UpdateAvailableEffects(std::vector<Effect> &newAvailableEffects)
83 {
84     availableEffects_ = newAvailableEffects;
85 }
86 
QueryEffectManagerSceneMode(SupportedEffectConfig & supportedEffectConfig)87 int32_t AudioEffectService::QueryEffectManagerSceneMode(SupportedEffectConfig &supportedEffectConfig)
88 {
89     supportedEffectConfig = supportedEffectConfig_;
90     return existDefault_;
91 }
92 
GetSupportedEffectConfig(SupportedEffectConfig & supportedEffectConfig)93 void AudioEffectService::GetSupportedEffectConfig(SupportedEffectConfig &supportedEffectConfig)
94 {
95     supportedEffectConfig = supportedEffectConfig_;
96 }
97 
UpdateUnsupportedDevicePre(PreStreamScene & pp,Stream & stream,const std::string & mode,int32_t i,int32_t j)98 static void UpdateUnsupportedDevicePre(PreStreamScene &pp, Stream &stream, const std::string &mode,
99                                        int32_t i, int32_t j)
100 {
101     StreamEffectMode streamEffectMode;
102     streamEffectMode.mode = mode;
103     j = 0;
104     for (auto &device: pp.device) {
105         if (i == j) {
106             for (auto &eachDevice: device) {
107                 streamEffectMode.devicePort.push_back(eachDevice);
108             }
109             break;
110         }
111         j += 1;
112     }
113     stream.streamEffectMode.push_back(streamEffectMode);
114 }
115 
UpdateUnsupportedModePre(PreStreamScene & pp,Stream & stream,std::string & mode,int32_t i)116 static void UpdateUnsupportedModePre(PreStreamScene &pp, Stream &stream, std::string &mode, int32_t i)
117 {
118     int32_t isSupported = 0;
119     if ((mode != "ENHANCE_NONE") &&
120         (mode != "ENHANCE_DEFAULT")) {
121         AUDIO_INFO_LOG("[supportedEnhanceConfig 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         int32_t j = 0;
127         UpdateUnsupportedDevicePre(pp, stream, mode, i, j);
128     }
129 }
130 
UpdateUnsupportedDevicePost(PostStreamScene & ess,Stream & stream,const std::string & mode,int32_t i)131 static void UpdateUnsupportedDevicePost(PostStreamScene &ess, Stream &stream, const std::string &mode, int32_t i)
132 {
133     StreamEffectMode streamEffectMode;
134     streamEffectMode.mode = mode;
135     int32_t j = 0;
136     for (auto &device: ess.device) {
137         if (i == j) {
138             for (auto &a: device) {
139                 streamEffectMode.devicePort.push_back(a);
140             }
141             break;
142         }
143         j += 1;
144     }
145     stream.streamEffectMode.push_back(streamEffectMode);
146 }
147 
UpdateUnsupportedModePost(PostStreamScene & ess,Stream & stream,std::string & mode,int32_t i)148 static void UpdateUnsupportedModePost(PostStreamScene &ess, Stream &stream, std::string &mode, int32_t i)
149 {
150     int32_t isSupported = 0;
151     if ((mode != "EFFECT_NONE") &&
152         (mode != "EFFECT_DEFAULT")) {
153         AUDIO_INFO_LOG("[supportedEffectConfig LOG10]:mode-> The %{public}s mode of %{public}s is unsupported, \
154             and this mode is deleted!", mode.c_str(), stream.scene.c_str());
155         isSupported = -1;
156     }
157     if (isSupported == 0) {
158         UpdateUnsupportedDevicePost(ess, stream, mode, i);
159     }
160 }
161 
UpdateAvailableStreamPre(ProcessNew & preProcessNew,PreStreamScene & pp,ScenePriority priority)162 static int32_t UpdateAvailableStreamPre(ProcessNew &preProcessNew, PreStreamScene &pp, ScenePriority priority)
163 {
164     bool isDuplicate = false;
165     bool isSupported = false;
166     const std::unordered_map<AudioEnhanceScene, std::string> &audioEnhanceSupportedSceneTypes =
167         GetEnhanceSupportedSceneType();
168     for (auto &[scene, stream] : audioEnhanceSupportedSceneTypes) {
169         if (pp.stream == stream) {
170             isSupported = true;
171             break;
172         }
173     }
174     auto it = std::find_if(preProcessNew.stream.begin(), preProcessNew.stream.end(), [&](const Stream &x) {
175         return ((x.scene == pp.stream) && (x.priority == priority));
176     });
177     if ((it == preProcessNew.stream.end()) && isSupported) {
178         Stream stream;
179         stream.priority = priority;
180         stream.scene = pp.stream;
181         int32_t i = 0;
182         for (auto &mode: pp.mode) {
183             UpdateUnsupportedModePre(pp, stream, mode, i);
184         }
185         preProcessNew.stream.push_back(stream);
186     } else if (it != preProcessNew.stream.end()) {
187         isDuplicate = true;
188     }
189     return isDuplicate;
190 }
191 
UpdateAvailableStreamPost(ProcessNew & postProcessNew,PostStreamScene & ess,ScenePriority priority)192 static int32_t UpdateAvailableStreamPost(ProcessNew &postProcessNew, PostStreamScene &ess, ScenePriority priority)
193 {
194     bool isDuplicate = false;
195     bool isSupported = false;
196     const std::unordered_map<AudioEffectScene, std::string> &audioSupportedSceneTypes = GetSupportedSceneType();
197     for (auto &[scene, stream] : audioSupportedSceneTypes) {
198         if (ess.stream == stream) {
199             isSupported = true;
200             break;
201         }
202     }
203     auto it = std::find_if(postProcessNew.stream.begin(), postProcessNew.stream.end(), [&](const Stream &x) {
204         return ((x.scene == ess.stream) && (x.priority == priority));
205     });
206     if ((it == postProcessNew.stream.end()) && isSupported) {
207         Stream stream;
208         stream.priority = priority;
209         stream.scene = ess.stream;
210         int32_t i = 0;
211         for (auto &mode: ess.mode) {
212             UpdateUnsupportedModePost(ess, stream, mode, i);
213         }
214         postProcessNew.stream.push_back(stream);
215     } else if (it != postProcessNew.stream.end()) {
216         isDuplicate = true;
217     }
218     return isDuplicate;
219 }
220 
UpdateAvailableSceneMapPost(SceneMappingItem & item,std::vector<SceneMappingItem> & postProcessSceneMap)221 static int32_t UpdateAvailableSceneMapPost(SceneMappingItem &item, std::vector<SceneMappingItem> &postProcessSceneMap)
222 {
223     bool isDuplicate = false;
224     auto it = std::find_if(postProcessSceneMap.begin(), postProcessSceneMap.end(),
225         [&item](const SceneMappingItem &x) {
226         return x.name == item.name;
227     });
228     if ((it == postProcessSceneMap.end())) {
229         postProcessSceneMap.push_back(item);
230     } else {
231         isDuplicate = true;
232     }
233     return isDuplicate;
234 }
235 
VerifySceneMappingItem(const SceneMappingItem & item)236 bool AudioEffectService::VerifySceneMappingItem(const SceneMappingItem &item)
237 {
238     return STREAM_USAGE_SET.find(item.name) != STREAM_USAGE_SET.end() &&
239         std::find(postSceneTypeSet_.begin(), postSceneTypeSet_.end(), item.sceneType) != postSceneTypeSet_.end();
240 }
241 
UpdateEffectChains(std::vector<std::string> & availableLayout)242 void AudioEffectService::UpdateEffectChains(std::vector<std::string> &availableLayout)
243 {
244     int32_t count = 0;
245     std::vector<int> deviceDelIdx;
246     for (const auto &ec: supportedEffectConfig_.effectChains) {
247         for (const auto &effectName: ec.apply) {
248             auto it = std::find_if(availableEffects_.begin(), availableEffects_.end(),
249                 [&effectName](const Effect &effect) {
250                 return effect.name == effectName;
251             });
252             if (it == availableEffects_.end()) {
253                 deviceDelIdx.emplace_back(count);
254                 break;
255             }
256         }
257         count += 1;
258     }
259     for (auto it = deviceDelIdx.rbegin(); it != deviceDelIdx.rend(); ++it) {
260         supportedEffectConfig_.effectChains.erase(supportedEffectConfig_.effectChains.begin() + *it);
261     }
262     if (supportedEffectConfig_.effectChains.empty()) {
263         AUDIO_INFO_LOG("[supportedEffectConfig LOG1]:effectChains-> all effectChains are unavailable");
264     }
265     for (auto ec: supportedEffectConfig_.effectChains) {
266         availableLayout.emplace_back(ec.name);
267     }
268 }
269 
UpdateAvailableAEConfig(OriginalEffectConfig & aeConfig)270 void AudioEffectService::UpdateAvailableAEConfig(OriginalEffectConfig &aeConfig)
271 {
272     int32_t ret = 0;
273     supportedEffectConfig_.effectChains = aeConfig.effectChains;
274     ProcessNew preProcessNew;
275     for (PreStreamScene &pp: aeConfig.preProcess.defaultScenes) {
276         ret += UpdateAvailableStreamPre(preProcessNew, pp, DEFAULT_SCENE);
277     }
278     for (PreStreamScene &pp: aeConfig.preProcess.priorScenes) {
279         ret += UpdateAvailableStreamPre(preProcessNew, pp, PRIOR_SCENE);
280     }
281     for (PreStreamScene &pp: aeConfig.preProcess.normalScenes) {
282         ret += UpdateAvailableStreamPre(preProcessNew, pp, NORMAL_SCENE);
283     }
284 
285     ProcessNew postProcessNew;
286     for (PostStreamScene &ess: aeConfig.postProcess.defaultScenes) {
287         ret += UpdateAvailableStreamPost(postProcessNew, ess, DEFAULT_SCENE);
288     }
289     for (PostStreamScene &ess: aeConfig.postProcess.priorScenes) {
290         ret += UpdateAvailableStreamPost(postProcessNew, ess, PRIOR_SCENE);
291     }
292     for (PostStreamScene &ess: aeConfig.postProcess.normalScenes) {
293         ret += UpdateAvailableStreamPost(postProcessNew, ess, NORMAL_SCENE);
294     }
295 
296     if (ret > 0) {
297         AUDIO_INFO_LOG("[supportedEffectConfig LOG2]:stream-> duplicate streams has been deleted");
298     }
299     supportedEffectConfig_.preProcessNew = preProcessNew;
300     supportedEffectConfig_.postProcessNew = postProcessNew;
301 
302     for (Stream &ss: supportedEffectConfig_.postProcessNew.stream) {
303         postSceneTypeSet_.push_back(ss.scene);
304     }
305     AUDIO_INFO_LOG("postSceneTypeSet_ size is %{public}zu", supportedEffectConfig_.postProcessNew.stream.size());
306     std::vector<SceneMappingItem> postSceneMap;
307     for (SceneMappingItem &item: aeConfig.postProcess.sceneMap) {
308         if (!VerifySceneMappingItem(item)) {
309             AUDIO_WARNING_LOG("Invalid %{public}s-%{public}s pair has been ignored",
310                 item.name.c_str(), item.sceneType.c_str());
311             continue;
312         }
313         if (UpdateAvailableSceneMapPost(item, postSceneMap)) {
314             AUDIO_WARNING_LOG("The duplicate streamUsage-sceneType pair is deleted, \
315                 and the first configuration is retained!");
316         }
317     }
318     supportedEffectConfig_.postProcessSceneMap = postSceneMap;
319 }
320 
UpdateDuplicateBypassMode(ProcessNew & processNew)321 void AudioEffectService::UpdateDuplicateBypassMode(ProcessNew &processNew)
322 {
323     int32_t flag = 0;
324     std::vector<int32_t> deviceDelIdx;
325     for (auto &stream: processNew.stream) {
326         int32_t count = 0;
327         deviceDelIdx.clear();
328         for (const auto &streamEffectMode: stream.streamEffectMode) {
329             if (streamEffectMode.mode == "EFFECT_NONE") {
330                 deviceDelIdx.push_back(count);
331             }
332             count += 1;
333         }
334         for (auto it = deviceDelIdx.rbegin(); it != deviceDelIdx.rend(); ++it) {
335             stream.streamEffectMode[*it].devicePort = {};
336             flag = -1;
337         }
338     }
339     if (flag == -1) {
340         AUDIO_INFO_LOG("[supportedEffectConfig LOG3]:mode-> EFFECT_NONE can not configure by deveploer!");
341     }
342 }
343 
UpdateDuplicateMode(ProcessNew & processNew)344 void AudioEffectService::UpdateDuplicateMode(ProcessNew &processNew)
345 {
346     std::unordered_set<std::string> seen;
347     std::vector<int32_t> toRemove;
348     uint32_t i;
349     for (auto &stream: processNew.stream) {
350         seen.clear();
351         toRemove.clear();
352         for (i = 0; i < stream.streamEffectMode.size(); i++) {
353             if (seen.count(stream.streamEffectMode[i].mode)) {
354                 toRemove.push_back(i);
355             } else {
356                 seen.insert(stream.streamEffectMode[i].mode);
357             }
358         }
359         for (auto it = toRemove.rbegin(); it != toRemove.rend(); ++it) {
360             AUDIO_INFO_LOG("[supportedEffectConfig LOG4]:mode-> The duplicate mode of %{public}s configuration \
361                 is deleted, and the first configuration is retained!", stream.scene.c_str());
362             stream.streamEffectMode.erase(stream.streamEffectMode.begin() + *it);
363         }
364     }
365 }
366 
UpdateDuplicateDeviceRecord(StreamEffectMode & streamEffectMode,Stream & stream)367 static void UpdateDuplicateDeviceRecord(StreamEffectMode &streamEffectMode, Stream &stream)
368 {
369     uint32_t i;
370     std::unordered_set<std::string> seen;
371     std::vector<int32_t> toRemove;
372     seen.clear();
373     toRemove.clear();
374     for (i = 0; i < streamEffectMode.devicePort.size(); i++) {
375         if (seen.count(streamEffectMode.devicePort[i].type)) {
376             toRemove.push_back(i);
377         } else {
378             seen.insert(streamEffectMode.devicePort[i].type);
379         }
380     }
381     for (auto it = toRemove.rbegin(); it != toRemove.rend(); ++it) {
382         AUDIO_INFO_LOG("[supportedEffectConfig LOG5]:device-> The duplicate device of %{public}s's %{public}s \
383             mode configuration is deleted, and the first configuration is retained!",
384             stream.scene.c_str(), streamEffectMode.mode.c_str());
385         streamEffectMode.devicePort.erase(streamEffectMode.devicePort.begin() + *it);
386     }
387 }
388 
UpdateDuplicateDevice(ProcessNew & processNew)389 void AudioEffectService::UpdateDuplicateDevice(ProcessNew &processNew)
390 {
391     for (auto &stream: processNew.stream) {
392         for (auto &streamEffectMode: stream.streamEffectMode) {
393             UpdateDuplicateDeviceRecord(streamEffectMode, stream);
394         }
395     }
396 }
397 
UpdateDuplicateScene(ProcessNew & processNew)398 void AudioEffectService::UpdateDuplicateScene(ProcessNew &processNew)
399 {
400     // erase duplicate scene
401     std::unordered_set<std::string> scenes;
402     for (auto it = processNew.stream.begin(); it != processNew.stream.end();) {
403         auto &stream = *it;
404         auto its = scenes.find(stream.scene);
405         if (its == scenes.end()) {
406             scenes.insert(stream.scene);
407         } else {
408             if (stream.priority == NORMAL_SCENE) {
409                 it = processNew.stream.erase(it);
410                 continue;
411             }
412         }
413         ++it;
414     }
415 }
416 
UpdateDuplicateDefaultScene(ProcessNew & processNew)417 void AudioEffectService::UpdateDuplicateDefaultScene(ProcessNew &processNew)
418 {
419     // erase duplicate default scene
420     bool flag = false;
421     for (auto it = processNew.stream.begin(); it != processNew.stream.end();) {
422         const auto &stream = *it;
423         if (stream.priority == DEFAULT_SCENE) {
424             if (flag) {
425                 it = processNew.stream.erase(it);
426                 continue;
427             }
428             flag = true;
429         }
430         ++it;
431     }
432 
433     // add default scene if no default
434     if (!flag) {
435         for (auto it = processNew.stream.begin(); it != processNew.stream.end(); ++it) {
436             auto &stream = *it;
437             if (stream.priority == NORMAL_SCENE) {
438                 stream.priority = DEFAULT_SCENE;
439                 break;
440             }
441         }
442     }
443 }
444 
UpdateUnavailableModes(std::vector<int32_t> & modeDelIdx,Stream & stream)445 static int32_t UpdateUnavailableModes(std::vector<int32_t> &modeDelIdx, Stream &stream)
446 {
447     int32_t ret = 0;
448     for (auto it = modeDelIdx.rbegin(); it != modeDelIdx.rend(); ++it) {
449         AUDIO_INFO_LOG("[supportedEffectConfig LOG7]:mode-> %{public}s's %{public}s mode is deleted!",
450             stream.scene.c_str(), stream.streamEffectMode[*it].mode.c_str());
451         if (stream.streamEffectMode[*it].mode == "PLAYBACK_DEAFULT") {
452             ret = -1;
453         }
454         stream.streamEffectMode.erase(stream.streamEffectMode.begin() + *it);
455         if (stream.streamEffectMode.empty()) {
456             AUDIO_INFO_LOG("[supportedEffectConfig LOG8]:mode-> %{public}s's mode is only EFFECT_NONE!",
457                 stream.scene.c_str());
458             StreamEffectMode streamEffectMode;
459             streamEffectMode.mode = "EFFECT_NONE";
460             stream.streamEffectMode.push_back(streamEffectMode);
461         }
462     }
463     if (stream.streamEffectMode.empty()) {
464         AUDIO_INFO_LOG("[supportedEffectConfig LOG8]:mode-> %{public}s's mode is only EFFECT_NONE!",
465             stream.scene.c_str());
466         StreamEffectMode streamEffectMode;
467         streamEffectMode.mode = "EFFECT_NONE";
468         stream.streamEffectMode.push_back(streamEffectMode);
469     }
470     return ret;
471 }
472 
UpdateUnavailableEffectChainsRecord(std::vector<std::string> & availableLayout,Stream & stream,StreamEffectMode & streamEffectMode,std::vector<int32_t> & modeDelIdx,int32_t modeCount)473 static void UpdateUnavailableEffectChainsRecord(std::vector<std::string> &availableLayout, Stream &stream,
474     StreamEffectMode &streamEffectMode, std::vector<int32_t> &modeDelIdx, int32_t modeCount)
475 {
476     std::vector<int32_t> deviceDelIdx;
477     deviceDelIdx.clear();
478     int32_t deviceCount = 0;
479     if (streamEffectMode.devicePort.empty()) {
480         modeDelIdx.push_back(modeCount);
481     }
482     for (auto &devicePort: streamEffectMode.devicePort) {
483         auto index = std::find(availableLayout.begin(), availableLayout.end(), devicePort.chain);
484         if (index == availableLayout.end()) {
485             deviceDelIdx.push_back(deviceCount);
486         }
487         deviceCount += 1;
488     }
489     if (streamEffectMode.devicePort.size() != deviceDelIdx.size() && deviceDelIdx.size() != 0) {
490         AUDIO_INFO_LOG("[supportedEffectConfig LOG6]:device-> The unavailable effectChain \
491             of %{public}s's %{public}s mode are set to LAYOUT_BYPASS!",
492             stream.scene.c_str(), streamEffectMode.mode.c_str());
493         for (auto it = deviceDelIdx.rbegin(); it != deviceDelIdx.rend(); ++it) {
494             streamEffectMode.devicePort[*it].chain = "LAYOUT_BYPASS";
495         }
496     } else {
497         for (auto it = deviceDelIdx.rbegin(); it != deviceDelIdx.rend(); ++it) {
498             streamEffectMode.devicePort.erase(streamEffectMode.devicePort.begin() + *it);
499             if (streamEffectMode.devicePort.empty()) {
500                 modeDelIdx.push_back(modeCount);
501             }
502         }
503     }
504 }
505 
UpdateUnavailableEffectChains(std::vector<std::string> & availableLayout,ProcessNew & processNew)506 int32_t AudioEffectService::UpdateUnavailableEffectChains(std::vector<std::string> &availableLayout,
507     ProcessNew &processNew)
508 {
509     int32_t ret = 0;
510 
511     std::vector<int32_t> modeDelIdx;
512     for (auto &stream: processNew.stream) {
513         modeDelIdx.clear();
514         int32_t modeCount = 0;
515         for (auto &streamEffectMode: stream.streamEffectMode) {
516             UpdateUnavailableEffectChainsRecord(availableLayout, stream, streamEffectMode, modeDelIdx, modeCount);
517         }
518         ret = UpdateUnavailableModes(modeDelIdx, stream);
519     }
520     return ret;
521 }
522 
UpdateSupportedEffectProperty(const Device & device,std::unordered_map<std::string,std::set<std::pair<std::string,std::string>>> & device2PropertySet)523 void AudioEffectService::UpdateSupportedEffectProperty(const Device &device,
524     std::unordered_map<std::string, std::set<std::pair<std::string, std::string>>> &device2PropertySet)
525 {
526     auto chainName = device.chain;
527     auto effectChain = std::find_if(supportedEffectConfig_.effectChains.begin(),
528         supportedEffectConfig_.effectChains.end(),
529         [&chainName](const EffectChain& x) {
530             return x.name == chainName;
531         });
532     if (effectChain == supportedEffectConfig_.effectChains.end()) {
533         return;
534     }
535     for (const auto &effectName : effectChain->apply) {
536         auto effectIter = std::find_if(availableEffects_.begin(), availableEffects_.end(),
537             [&effectName](const Effect& effect) {
538             return effect.name == effectName;
539         });
540         if (effectIter == availableEffects_.end()) {
541             continue;
542         }
543         for (const auto &property : effectIter->effectProperty) {
544             auto deviceIter = device2PropertySet.find(device.type);
545             if (deviceIter == device2PropertySet.end()) {
546                 device2PropertySet[device.type].insert({effectIter->name, property});
547             } else {
548                 deviceIter->second.insert({effectIter->name, property});
549             }
550             AUDIO_INFO_LOG("device %{public}s support effect [%{public}s, %{public}s]",
551                 device.type.c_str(), effectIter->name.c_str(), property.c_str());
552         }
553     }
554 }
555 
UpdateDuplicateProcessNew(std::vector<std::string> & availableLayout,ProcessNew & processNew)556 void AudioEffectService::UpdateDuplicateProcessNew(std::vector<std::string> &availableLayout, ProcessNew &processNew)
557 {
558     UpdateEffectChains(availableLayout);
559     UpdateDuplicateBypassMode(processNew);
560     UpdateDuplicateMode(processNew);
561     UpdateDuplicateDevice(processNew);
562     UpdateDuplicateDefaultScene(processNew);
563     UpdateDuplicateScene(processNew);
564     if (UpdateUnavailableEffectChains(availableLayout, supportedEffectConfig_.preProcessNew) != 0) {
565         existDefault_ = -1;
566     }
567 }
568 
BuildAvailableAEConfig()569 void AudioEffectService::BuildAvailableAEConfig()
570 {
571     std::vector<std::string> availableLayout;
572     existDefault_ = 1;
573     if (oriEffectConfig_.effectChains.size() == 0) {
574         AUDIO_INFO_LOG("[supportedEffectConfig LOG12]: effectChains is none!");
575     }
576     if (oriEffectConfig_.preProcess.defaultScenes.size() != 1) {
577         AUDIO_INFO_LOG("[supportedEffectConfig LOG13]: pre-defaultScene is not one!");
578     }
579 
580     if (oriEffectConfig_.preProcess.normalScenes.size() == 0) {
581         AUDIO_INFO_LOG("[supportedEffectConfig LOG14]: pre-normalScene is none!");
582     }
583 
584     if (oriEffectConfig_.postProcess.defaultScenes.size() != 1) {
585         AUDIO_INFO_LOG("[supportedEffectConfig LOG15]: post-defaultScene is not one!");
586     }
587     if (oriEffectConfig_.postProcess.normalScenes.size() == 0) {
588         AUDIO_INFO_LOG("[supportedEffectConfig LOG16]: post-normalScene is none!");
589     }
590 
591     // update maxExtraSceneNum
592 
593     // Update duplicate defined modes, devices, and unsupported effect chain.
594     UpdateAvailableAEConfig(oriEffectConfig_);
595 
596     UpdateDuplicateProcessNew(availableLayout, supportedEffectConfig_.preProcessNew);
597     UpdateDuplicateProcessNew(availableLayout, supportedEffectConfig_.postProcessNew);
598 
599     for (auto &stream : supportedEffectConfig_.preProcessNew.stream) {
600         for (auto &streamMode : stream.streamEffectMode) {
601             for (auto &device : streamMode.devicePort) {
602                 UpdateSupportedEffectProperty(device, device2EnhancePropertySet_);
603             }
604         }
605     }
606     for (auto &stream : supportedEffectConfig_.postProcessNew.stream) {
607         for (auto &streamMode : stream.streamEffectMode) {
608             for (auto &device : streamMode.devicePort) {
609                 UpdateSupportedEffectProperty(device, device2EffectPropertySet_);
610             }
611         }
612     }
613 }
614 
SetMasterSinkAvailable()615 void AudioEffectService::SetMasterSinkAvailable()
616 {
617     isMasterSinkAvailable_ = true;
618 }
619 
SetEffectChainManagerAvailable()620 void AudioEffectService::SetEffectChainManagerAvailable()
621 {
622     isEffectChainManagerAvailable_ = true;
623 }
624 
CanLoadEffectSinks()625 bool AudioEffectService::CanLoadEffectSinks()
626 {
627     return (isMasterSinkAvailable_ && isEffectChainManagerAvailable_);
628 }
629 
630 template <typename T>
AddKeyValueIntoMap(std::unordered_map<T,std::string> & map,std::string & key,std::string & value)631 void AddKeyValueIntoMap(std::unordered_map<T, std::string> &map, std::string &key, std::string &value)
632 {
633     if (map.count(key)) { // if the key already register in map
634         return;
635     }
636     map[key] = value;
637 }
638 
ConstructEffectChainMode(StreamEffectMode & mode,std::string sceneType,EffectChainManagerParam & effectChainMgrParam)639 void AudioEffectService::ConstructEffectChainMode(StreamEffectMode &mode, std::string sceneType,
640                                                   EffectChainManagerParam &effectChainMgrParam)
641 {
642     std::unordered_map<std::string, std::string> &map = effectChainMgrParam.sceneTypeToChainNameMap;
643     const std::unordered_map<DeviceType, std::string> &supportDeviceType = GetSupportedDeviceType();
644 
645     std::string sceneMode = mode.mode;
646     std::string key;
647     std::string defaultChain;
648     bool defaultFlag = false;
649     for (auto &device : mode.devicePort) {
650         if (device.type == "DEVICE_TYPE_DEFAULT") {
651             defaultFlag = true;
652             defaultChain = device.chain;
653         } else {
654             key = sceneType + "_&_" + sceneMode + "_&_" + device.type;
655             AddKeyValueIntoMap(map, key, device.chain);
656         }
657         ConstructDefaultEffectProperty(device.chain, effectChainMgrParam.effectDefaultProperty);
658     }
659     if (defaultFlag) {
660         for (const auto &deviceType : supportDeviceType) {
661             key = sceneType + "_&_" + sceneMode + "_&_" + deviceType.second;
662             AddKeyValueIntoMap(map, key, defaultChain);
663         }
664     }
665 }
666 
ConstructDefaultEffectProperty(const std::string & chainName,std::unordered_map<std::string,std::string> & effectDefaultProperty)667 void AudioEffectService::ConstructDefaultEffectProperty(const std::string &chainName,
668     std::unordered_map<std::string, std::string> &effectDefaultProperty)
669 {
670     auto effectChain = std::find_if(supportedEffectConfig_.effectChains.begin(),
671         supportedEffectConfig_.effectChains.end(),
672         [&chainName](const EffectChain& x) {
673             return x.name == chainName;
674         });
675     if (effectChain == supportedEffectConfig_.effectChains.end()) {
676         return;
677     }
678     for (const auto &effectName : effectChain->apply) {
679         auto effectIter = std::find_if(availableEffects_.begin(), availableEffects_.end(),
680             [&effectName](const Effect& effect) {
681             return effect.name == effectName;
682         });
683         if (effectIter == availableEffects_.end()) {
684             continue;
685         }
686         // if 0 property, no need to set default
687         if (effectIter->effectProperty.size() > 0) {
688             // first assign, and no need to assign twice
689             if (!effectDefaultProperty.count(effectIter->name)) {
690                 // only first property is default set
691                 effectDefaultProperty[effectIter->name] = effectIter->effectProperty[0];
692                 AUDIO_INFO_LOG("effect %{public}s defaultProperty is %{public}s",
693                     effectIter->name.c_str(), effectIter->effectProperty[0].c_str());
694             }
695         }
696     }
697 }
698 
ConstructEffectChainManagerParam(EffectChainManagerParam & effectChainMgrParam)699 void AudioEffectService::ConstructEffectChainManagerParam(EffectChainManagerParam &effectChainMgrParam)
700 {
701     effectChainMgrParam.maxExtraNum = oriEffectConfig_.postProcess.maxExtSceneNum;
702     std::string sceneType;
703 
704     for (auto &scene: supportedEffectConfig_.postProcessNew.stream) {
705         sceneType = scene.scene;
706         if (scene.priority == PRIOR_SCENE) {
707             effectChainMgrParam.priorSceneList.push_back(sceneType);
708         }
709         if (scene.priority == DEFAULT_SCENE) {
710             effectChainMgrParam.defaultSceneName = sceneType;
711         }
712         for (auto &mode: scene.streamEffectMode) {
713             ConstructEffectChainMode(mode, sceneType, effectChainMgrParam);
714         }
715     }
716     AUDIO_INFO_LOG("Constructed SceneTypeAndModeToEffectChainNameMap at policy, size is %{public}d",
717         (int32_t)effectChainMgrParam.sceneTypeToChainNameMap.size());
718 }
719 
ConstructEnhanceChainManagerParam(EffectChainManagerParam & enhanceChainMgrParam)720 void AudioEffectService::ConstructEnhanceChainManagerParam(EffectChainManagerParam &enhanceChainMgrParam)
721 {
722     std::unordered_map<std::string, std::string> &map = enhanceChainMgrParam.sceneTypeToChainNameMap;
723     std::unordered_map<std::string, std::string> &enhanceDefaultProperty = enhanceChainMgrParam.effectDefaultProperty;
724     enhanceChainMgrParam.maxExtraNum = oriEffectConfig_.preProcess.maxExtSceneNum;
725 
726     std::string sceneType;
727     std::string sceneMode;
728     std::string key;
729 
730     for (auto &scene: supportedEffectConfig_.preProcessNew.stream) {
731         sceneType = scene.scene;
732         if (scene.priority == PRIOR_SCENE) {
733             enhanceChainMgrParam.priorSceneList.push_back(sceneType);
734         }
735         if (scene.priority == DEFAULT_SCENE) {
736             enhanceChainMgrParam.defaultSceneName = sceneType;
737         }
738 
739         for (auto &mode: scene.streamEffectMode) {
740             sceneMode = mode.mode;
741             for (auto &device: mode.devicePort) {
742                 key = sceneType + "_&_" + sceneMode;
743                 AddKeyValueIntoMap(map, key, device.chain);
744                 ConstructDefaultEffectProperty(device.chain, enhanceDefaultProperty);
745             }
746         }
747     }
748     AUDIO_INFO_LOG("Constructed SceneTypeAndModeToEnhanceChainNameMap at policy, size is %{public}d",
749         (int32_t)map.size());
750 }
751 
AddSupportedPropertyByDeviceInner(const DeviceType & deviceType,std::set<std::pair<std::string,std::string>> & mergedSet,const std::unordered_map<std::string,std::set<std::pair<std::string,std::string>>> & device2PropertySet)752 int32_t AudioEffectService::AddSupportedPropertyByDeviceInner(const DeviceType& deviceType,
753     std::set<std::pair<std::string, std::string>> &mergedSet,
754     const std::unordered_map<std::string, std::set<std::pair<std::string, std::string>>> &device2PropertySet)
755 {
756     const std::unordered_map<DeviceType, std::string> &supportDeviceType = GetSupportedDeviceType();
757     auto deviceIter = supportDeviceType.find(deviceType);
758     if (deviceIter == supportDeviceType.end()) {
759         AUDIO_ERR_LOG("device not supported.");
760         return -1;
761     }
762     auto deviceStr = deviceType == DEVICE_TYPE_INVALID ? "DEVICE_TYPE_DEFAULT" : deviceIter->second;
763     auto propertySetIter = device2PropertySet.find(deviceStr);
764     if (propertySetIter != device2PropertySet.end()) {
765         mergedSet.insert(propertySetIter->second.begin(), propertySetIter->second.end());
766     }
767     return AUDIO_OK;
768 }
769 
AddSupportedAudioEffectPropertyByDevice(const DeviceType & deviceType,std::set<std::pair<std::string,std::string>> & mergedSet)770 int32_t AudioEffectService::AddSupportedAudioEffectPropertyByDevice(const DeviceType &deviceType,
771     std::set<std::pair<std::string, std::string>> &mergedSet)
772 {
773     return AddSupportedPropertyByDeviceInner(deviceType, mergedSet, device2EffectPropertySet_);
774 }
775 
AddSupportedAudioEnhancePropertyByDevice(const DeviceType & deviceType,std::set<std::pair<std::string,std::string>> & mergedSet)776 int32_t AudioEffectService::AddSupportedAudioEnhancePropertyByDevice(const DeviceType &deviceType,
777     std::set<std::pair<std::string, std::string>> &mergedSet)
778 {
779     return AddSupportedPropertyByDeviceInner(deviceType, mergedSet, device2EnhancePropertySet_);
780 }
781 } // namespce AudioStandard
782 } // namespace OHOS
783