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