1 /*
2 * Copyright (c) 2024 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 "AudioServer"
17 #endif
18
19 #include "audio_server.h"
20
21 #include <cinttypes>
22 #include <codecvt>
23 #include <csignal>
24 #include <fstream>
25 #include <sstream>
26 #include <thread>
27 #include <unordered_map>
28 #include <vector>
29
30 #include "bundle_mgr_interface.h"
31 #include "bundle_mgr_proxy.h"
32 #include "iservice_registry.h"
33 #include "system_ability_definition.h"
34 #include "hisysevent.h"
35
36 #include "audio_capturer_source.h"
37 #include "fast_audio_capturer_source.h"
38 #include "audio_errors.h"
39 #include "audio_service_log.h"
40 #include "audio_asr.h"
41 #include "audio_manager_listener_proxy.h"
42 #include "audio_service.h"
43 #include "audio_schedule.h"
44 #include "audio_info.h"
45 #include "audio_utils.h"
46 #include "i_audio_capturer_source.h"
47 #include "i_audio_renderer_sink.h"
48 #include "audio_renderer_sink.h"
49 #include "i_standard_audio_server_manager_listener.h"
50 #include "audio_effect_chain_manager.h"
51 #include "audio_enhance_chain_manager.h"
52 #include "playback_capturer_manager.h"
53 #include "policy_handler.h"
54 #include "config/audio_param_parser.h"
55 #include "media_monitor_manager.h"
56
57 static const std::map<std::string, AsrAecMode> AEC_MODE_MAP = {
58 {"BYPASS", AsrAecMode::BYPASS},
59 {"STANDARD", AsrAecMode::STANDARD}
60 };
61
62 static const std::map<AsrAecMode, std::string> AEC_MODE_MAP_VERSE = {
63 {AsrAecMode::BYPASS, "BYPASS"},
64 {AsrAecMode::STANDARD, "STANDARD"}
65 };
66
67 static const std::map<std::string, AsrNoiseSuppressionMode> NS_MODE_MAP = {
68 {"BYPASS", AsrNoiseSuppressionMode::BYPASS},
69 {"STANDARD", AsrNoiseSuppressionMode::STANDARD},
70 {"NEAR_FIELD", AsrNoiseSuppressionMode::NEAR_FIELD},
71 {"FAR_FIELD", AsrNoiseSuppressionMode::FAR_FIELD}
72 };
73
74 static const std::map<AsrNoiseSuppressionMode, std::string> NS_MODE_MAP_VERSE = {
75 {AsrNoiseSuppressionMode::BYPASS, "BYPASS"},
76 {AsrNoiseSuppressionMode::STANDARD, "STANDARD"},
77 {AsrNoiseSuppressionMode::NEAR_FIELD, "NEAR_FIELD"},
78 {AsrNoiseSuppressionMode::FAR_FIELD, "FAR_FIELD"}
79 };
80
81 static const std::map<std::string, AsrWhisperDetectionMode> WHISPER_DETECTION_MODE_MAP = {
82 {"BYPASS", AsrWhisperDetectionMode::BYPASS},
83 {"STANDARD", AsrWhisperDetectionMode::STANDARD},
84 };
85
86 static const std::map<AsrWhisperDetectionMode, std::string> WHISPER_DETECTION_MODE_MAP_VERSE = {
87 {AsrWhisperDetectionMode::BYPASS, "BYPASS"},
88 {AsrWhisperDetectionMode::STANDARD, "STANDARD"},
89 };
90
91 static const std::map<std::string, AsrVoiceControlMode> VC_MODE_MAP = {
92 {"audio2voicetx", AsrVoiceControlMode::AUDIO_2_VOICETX},
93 {"audiomix2voicetx", AsrVoiceControlMode::AUDIO_MIX_2_VOICETX},
94 {"audio2voicetxex", AsrVoiceControlMode::AUDIO_2_VOICE_TX_EX},
95 {"audiomix2voicetxex", AsrVoiceControlMode::AUDIO_MIX_2_VOICE_TX_EX},
96 };
97
98 static const std::map<AsrVoiceControlMode, std::string> VC_MODE_MAP_VERSE = {
99 {AsrVoiceControlMode::AUDIO_2_VOICETX, "audio2voicetx"},
100 {AsrVoiceControlMode::AUDIO_MIX_2_VOICETX, "audiomix2voicetx"},
101 {AsrVoiceControlMode::AUDIO_2_VOICE_TX_EX, "audio2voicetxex"},
102 {AsrVoiceControlMode::AUDIO_MIX_2_VOICE_TX_EX, "audiomix2voicetxex"},
103 };
104
105 static const std::map<AsrVoiceControlMode, std::vector<std::string>> VOICE_CALL_ASSISTANT_SUPPRESSION = {
106 {AsrVoiceControlMode::AUDIO_SUPPRESSION_OPPOSITE, {"TTS_2_DEVICE", "TTS_2_MODEM"}},
107 {AsrVoiceControlMode::AUDIO_SUPPRESSION_LOCAL, {"TTS_2_DEVICE", "TTS_2_MODEM"}},
108 {AsrVoiceControlMode::VOICE_TXRX_DECREASE, {"MIC_2_MODEM", "MODEM_2_DEVICE"}},
109 };
110
111 static const std::map<AsrVoiceControlMode, std::set<std::string>> VOICE_CALL_ASSISTANT_NEED_SUPPRESSION = {
112 {AsrVoiceControlMode::AUDIO_SUPPRESSION_OPPOSITE, {"TTS_2_MODEM"}},
113 {AsrVoiceControlMode::AUDIO_SUPPRESSION_LOCAL, {"TTS_2_DEVICE"}},
114 {AsrVoiceControlMode::VOICE_TXRX_DECREASE, {"MIC_2_MODEM", "MODEM_2_DEVICE"}},
115 };
116
117 static const std::string VOICE_CALL_SUPPRESSION_VOLUME = "3";
118 static const std::string VOICE_CALL_FULL_VOLUME = "32";
119
120 static const std::map<std::string, AsrVoiceMuteMode> VM_MODE_MAP = {
121 {"output_mute", AsrVoiceMuteMode::OUTPUT_MUTE},
122 {"input_mute", AsrVoiceMuteMode::INPUT_MUTE},
123 {"mute_tts", AsrVoiceMuteMode::TTS_MUTE},
124 {"mute_call", AsrVoiceMuteMode::CALL_MUTE},
125 {"ouput_mute_ex", AsrVoiceMuteMode::OUTPUT_MUTE_EX},
126 };
127
128 static const std::map<AsrVoiceMuteMode, std::string> VM_MODE_MAP_VERSE = {
129 {AsrVoiceMuteMode::OUTPUT_MUTE, "output_mute"},
130 {AsrVoiceMuteMode::INPUT_MUTE, "input_mute"},
131 {AsrVoiceMuteMode::TTS_MUTE, "mute_tts"},
132 {AsrVoiceMuteMode::CALL_MUTE, "mute_call"},
133 {AsrVoiceMuteMode::OUTPUT_MUTE_EX, "ouput_mute_ex"},
134 };
135
136 static const std::map<std::string, bool> RES_MAP = {
137 {"true", true},
138 {"false", false},
139 };
140
141 static const std::map<bool, std::string> RES_MAP_VERSE = {
142 {true, "true"},
143 {false, "false"},
144 };
145
146 using namespace std;
147
148 namespace OHOS {
149 namespace AudioStandard {
150
splitString(const std::string & str,const std::string & pattern)151 std::vector<std::string> splitString(const std::string& str, const std::string& pattern)
152 {
153 std::vector<std::string> res;
154 if (str == "")
155 return res;
156 std::string strs = str + pattern;
157 size_t pos = strs.find(pattern);
158
159 while (pos != strs.npos) {
160 std::string temp = strs.substr(0, pos);
161 res.push_back(temp);
162 strs = strs.substr(pos + 1, strs.size());
163 pos = strs.find(pattern);
164 }
165 return res;
166 }
167
SetAsrAecMode(AsrAecMode asrAecMode)168 int32_t AudioServer::SetAsrAecMode(AsrAecMode asrAecMode)
169 {
170 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySystemPermission(), ERR_SYSTEM_PERMISSION_DENIED,
171 "Check playback permission failed, no system permission");
172 std::lock_guard<std::mutex> lockSet(audioParameterMutex_);
173 std::string key = "asr_aec_mode";
174 std::string value = key + "=";
175 std::string keyAec = "ASR_AEC";
176 std::string valueAec = "";
177
178 auto it = AEC_MODE_MAP_VERSE.find(asrAecMode);
179 if (it != AEC_MODE_MAP_VERSE.end()) {
180 value = key + "=" + it->second;
181 if (it->second == "STANDARD") {
182 valueAec = "ASR_AEC=ON";
183 } else {
184 valueAec = "ASR_AEC=OFF";
185 }
186 } else {
187 AUDIO_ERR_LOG("get value failed.");
188 return ERR_INVALID_PARAM;
189 }
190 AudioServer::audioParameters[key] = value;
191 AudioServer::audioParameters[keyAec] = valueAec;
192 AudioParamKey parmKey = AudioParamKey::NONE;
193 IAudioRendererSink *audioRendererSinkInstance = IAudioRendererSink::GetInstance("primary", "");
194 CHECK_AND_RETURN_RET_LOG(audioRendererSinkInstance != nullptr, ERROR, "has no valid sink");
195 audioRendererSinkInstance->SetAudioParameter(parmKey, "", value);
196 audioRendererSinkInstance->SetAudioParameter(parmKey, "", valueAec);
197 return 0;
198 }
199
GetAsrAecMode(AsrAecMode & asrAecMode)200 int32_t AudioServer::GetAsrAecMode(AsrAecMode& asrAecMode)
201 {
202 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySystemPermission(), ERR_SYSTEM_PERMISSION_DENIED,
203 "Check playback permission failed, no system permission");
204 std::lock_guard<std::mutex> lockSet(audioParameterMutex_);
205 std::string key = "asr_aec_mode";
206 std::string keyAec = "ASR_AEC";
207 AudioParamKey parmKey = AudioParamKey::NONE;
208 IAudioRendererSink *audioRendererSinkInstance = IAudioRendererSink::GetInstance("primary", "");
209 CHECK_AND_RETURN_RET_LOG(audioRendererSinkInstance != nullptr, ERROR, "has no valid sink");
210 std::string asrAecModeSink = audioRendererSinkInstance->GetAudioParameter(parmKey, key);
211 auto it = AudioServer::audioParameters.find(key);
212 if (it != AudioServer::audioParameters.end()) {
213 asrAecModeSink = it->second;
214 } else {
215 // if asr_aec_mode null, return ASR_AEC.
216 // if asr_aec_mode null and ASR_AEC null, return err.
217 auto itAec = AudioServer::audioParameters.find(keyAec);
218 std::string asrAecSink = itAec->second;
219 if (asrAecSink == "ASR_AEC=ON") {
220 asrAecMode = AsrAecMode::STANDARD;
221 } else if (asrAecSink == "ASR_AEC=OFF") {
222 asrAecMode = AsrAecMode::BYPASS;
223 } else {
224 AUDIO_ERR_LOG("get value failed.");
225 return ERR_INVALID_PARAM;
226 }
227 return 0;
228 }
229
230 std::vector<std::string> resMode = splitString(asrAecModeSink, "=");
231 const int32_t resSize = 2;
232 std::string modeString = "";
233 if (resMode.size() == resSize) {
234 modeString = resMode[1];
235 auto it = AEC_MODE_MAP.find(modeString);
236 if (it != AEC_MODE_MAP.end()) {
237 asrAecMode = it->second;
238 } else {
239 AUDIO_ERR_LOG("get value failed.");
240 return ERR_INVALID_PARAM;
241 }
242 } else {
243 AUDIO_ERR_LOG("get value failed.");
244 return ERR_INVALID_PARAM;
245 }
246 return 0;
247 }
248
SetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode asrNoiseSuppressionMode)249 int32_t AudioServer::SetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode asrNoiseSuppressionMode)
250 {
251 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySystemPermission(), ERR_SYSTEM_PERMISSION_DENIED,
252 "Check playback permission failed, no system permission");
253 std::lock_guard<std::mutex> lockSet(audioParameterMutex_);
254 std::string key = "asr_ns_mode";
255 std::string value = key + "=";
256
257 auto it = NS_MODE_MAP_VERSE.find(asrNoiseSuppressionMode);
258 if (it != NS_MODE_MAP_VERSE.end()) {
259 value = key + "=" + it->second;
260 } else {
261 AUDIO_ERR_LOG("get value failed.");
262 return ERR_INVALID_PARAM;
263 }
264 AudioServer::audioParameters[key] = value;
265 AudioParamKey parmKey = AudioParamKey::NONE;
266 IAudioRendererSink *audioRendererSinkInstance = IAudioRendererSink::GetInstance("primary", "");
267 CHECK_AND_RETURN_RET_LOG(audioRendererSinkInstance != nullptr, ERROR, "has no valid sink");
268 audioRendererSinkInstance->SetAudioParameter(parmKey, "", value);
269 return 0;
270 }
271
GetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode & asrNoiseSuppressionMode)272 int32_t AudioServer::GetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode& asrNoiseSuppressionMode)
273 {
274 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySystemPermission(), ERR_SYSTEM_PERMISSION_DENIED,
275 "Check playback permission failed, no system permission");
276 std::lock_guard<std::mutex> lockSet(audioParameterMutex_);
277 std::string key = "asr_ns_mode";
278 AudioParamKey parmKey = AudioParamKey::NONE;
279 IAudioRendererSink *audioRendererSinkInstance = IAudioRendererSink::GetInstance("primary", "");
280 CHECK_AND_RETURN_RET_LOG(audioRendererSinkInstance != nullptr, ERROR, "has no valid sink");
281 std::string asrNoiseSuppressionModeSink = audioRendererSinkInstance->GetAudioParameter(parmKey, key);
282 auto it = AudioServer::audioParameters.find(key);
283 if (it != AudioServer::audioParameters.end()) {
284 asrNoiseSuppressionModeSink = it->second;
285 } else {
286 AUDIO_ERR_LOG("get value failed.");
287 return ERR_INVALID_PARAM;
288 }
289
290 std::vector<std::string> resMode = splitString(asrNoiseSuppressionModeSink, "=");
291 const int32_t resSize = 2;
292 std::string modeString = "";
293 if (resMode.size() == resSize) {
294 modeString = resMode[1];
295 auto it = NS_MODE_MAP.find(modeString);
296 if (it != NS_MODE_MAP.end()) {
297 asrNoiseSuppressionMode = it->second;
298 } else {
299 AUDIO_ERR_LOG("get value failed.");
300 return ERR_INVALID_PARAM;
301 }
302 } else {
303 AUDIO_ERR_LOG("get value failed.");
304 return ERR_INVALID_PARAM;
305 }
306 return 0;
307 }
308
SetAsrWhisperDetectionMode(AsrWhisperDetectionMode asrWhisperDetectionMode)309 int32_t AudioServer::SetAsrWhisperDetectionMode(AsrWhisperDetectionMode asrWhisperDetectionMode)
310 {
311 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySystemPermission(), ERR_SYSTEM_PERMISSION_DENIED,
312 "Check playback permission failed, no system permission");
313 std::lock_guard<std::mutex> lockSet(audioParameterMutex_);
314 std::string key = "asr_wd_mode";
315 std::string value = key + "=";
316
317 auto it = WHISPER_DETECTION_MODE_MAP_VERSE.find(asrWhisperDetectionMode);
318 if (it != WHISPER_DETECTION_MODE_MAP_VERSE.end()) {
319 value = key + "=" + it->second;
320 } else {
321 AUDIO_ERR_LOG("get value failed.");
322 return ERR_INVALID_PARAM;
323 }
324 AudioServer::audioParameters[key] = value;
325 AudioParamKey parmKey = AudioParamKey::NONE;
326 IAudioRendererSink *audioRendererSinkInstance = IAudioRendererSink::GetInstance("primary", "");
327 CHECK_AND_RETURN_RET_LOG(audioRendererSinkInstance != nullptr, ERROR, "has no valid sink");
328 audioRendererSinkInstance->SetAudioParameter(parmKey, "", value);
329 return 0;
330 }
331
GetAsrWhisperDetectionMode(AsrWhisperDetectionMode & asrWhisperDetectionMode)332 int32_t AudioServer::GetAsrWhisperDetectionMode(AsrWhisperDetectionMode& asrWhisperDetectionMode)
333 {
334 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySystemPermission(), ERR_SYSTEM_PERMISSION_DENIED,
335 "Check playback permission failed, no system permission");
336 std::lock_guard<std::mutex> lockSet(audioParameterMutex_);
337 std::string key = "asr_wd_mode";
338 AudioParamKey parmKey = AudioParamKey::NONE;
339 IAudioRendererSink *audioRendererSinkInstance = IAudioRendererSink::GetInstance("primary", "");
340 CHECK_AND_RETURN_RET_LOG(audioRendererSinkInstance != nullptr, ERROR, "has no valid sink");
341 std::string asrWhisperDetectionModeSink = audioRendererSinkInstance->GetAudioParameter(parmKey, key);
342 auto it = AudioServer::audioParameters.find(key);
343 if (it != AudioServer::audioParameters.end()) {
344 asrWhisperDetectionModeSink = it->second;
345 } else {
346 AUDIO_ERR_LOG("get value failed.");
347 return ERR_INVALID_PARAM;
348 }
349
350 std::vector<std::string> resMode = splitString(asrWhisperDetectionModeSink, "=");
351 const int32_t resSize = 2;
352 std::string modeString = "";
353 if (resMode.size() == resSize) {
354 modeString = resMode[1];
355 auto it = WHISPER_DETECTION_MODE_MAP.find(modeString);
356 if (it != WHISPER_DETECTION_MODE_MAP.end()) {
357 asrWhisperDetectionMode = it->second;
358 } else {
359 AUDIO_ERR_LOG("get value failed.");
360 return ERR_INVALID_PARAM;
361 }
362 } else {
363 AUDIO_ERR_LOG("get value failed.");
364 return ERR_INVALID_PARAM;
365 }
366 return 0;
367 }
368
SetAsrVoiceControlMode(AsrVoiceControlMode asrVoiceControlMode,bool on)369 int32_t AudioServer::SetAsrVoiceControlMode(AsrVoiceControlMode asrVoiceControlMode, bool on)
370 {
371 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySystemPermission(), ERR_SYSTEM_PERMISSION_DENIED,
372 "Check playback permission failed, no system permission");
373 std::lock_guard<std::mutex> lockSet(audioParameterMutex_);
374 std::string key = "avcm";
375 std::string value = key + "=";
376
377 auto itVerse = VC_MODE_MAP_VERSE.find(asrVoiceControlMode);
378 auto itCallAssistant = VOICE_CALL_ASSISTANT_SUPPRESSION.find(asrVoiceControlMode);
379 auto res = RES_MAP_VERSE.find(on);
380 if ((itVerse == VC_MODE_MAP_VERSE.end() && itCallAssistant == VOICE_CALL_ASSISTANT_SUPPRESSION.end()) ||
381 res == RES_MAP_VERSE.end()) {
382 AUDIO_ERR_LOG("get value failed.");
383 return ERR_INVALID_PARAM;
384 }
385
386 AudioParamKey parmKey = AudioParamKey::NONE;
387 IAudioRendererSink *audioRendererSinkInstance = IAudioRendererSink::GetInstance("primary", "");
388 CHECK_AND_RETURN_RET_LOG(audioRendererSinkInstance != nullptr, ERROR, "has no valid sink");
389 if ((itVerse != VC_MODE_MAP_VERSE.end()) && (res != RES_MAP_VERSE.end())) {
390 value = itVerse->second + "=" + res->second;
391 AudioServer::audioParameters[key] = value;
392 audioRendererSinkInstance->SetAudioParameter(parmKey, "", value);
393 return 0;
394 }
395 if ((itCallAssistant != VOICE_CALL_ASSISTANT_SUPPRESSION.end()) && (res != RES_MAP_VERSE.end())) {
396 std::vector<std::string> modes = VOICE_CALL_ASSISTANT_SUPPRESSION.at(asrVoiceControlMode);
397 std::set<std::string> needSuppression = VOICE_CALL_ASSISTANT_NEED_SUPPRESSION.at(asrVoiceControlMode);
398 for (size_t i = 0; i < modes.size(); i++) {
399 if (needSuppression.contains(modes[i]) && on) {
400 audioRendererSinkInstance->SetAudioParameter(parmKey, "",
401 modes[i] + "=" + VOICE_CALL_SUPPRESSION_VOLUME);
402 continue;
403 }
404 audioRendererSinkInstance->SetAudioParameter(parmKey, "",
405 modes[i] + "=" + VOICE_CALL_FULL_VOLUME);
406 }
407 }
408
409 return 0;
410 }
411
SetAsrVoiceMuteMode(AsrVoiceMuteMode asrVoiceMuteMode,bool on)412 int32_t AudioServer::SetAsrVoiceMuteMode(AsrVoiceMuteMode asrVoiceMuteMode, bool on)
413 {
414 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySystemPermission(), ERR_SYSTEM_PERMISSION_DENIED,
415 "Check playback permission failed, no system permission");
416 std::lock_guard<std::mutex> lockSet(audioParameterMutex_);
417 std::string key = "avmm";
418 std::string value = key + "=";
419
420 auto it = VM_MODE_MAP_VERSE.find(asrVoiceMuteMode);
421 auto res = RES_MAP_VERSE.find(on);
422 if ((it != VM_MODE_MAP_VERSE.end()) && (res != RES_MAP_VERSE.end())) {
423 value = it->second + "=" + res->second;
424 } else {
425 AUDIO_ERR_LOG("get value failed.");
426 return ERR_INVALID_PARAM;
427 }
428 AudioServer::audioParameters[key] = value;
429 AudioParamKey parmKey = AudioParamKey::NONE;
430 IAudioRendererSink *audioRendererSinkInstance = IAudioRendererSink::GetInstance("primary", "");
431 CHECK_AND_RETURN_RET_LOG(audioRendererSinkInstance != nullptr, ERROR, "has no valid sink");
432 audioRendererSinkInstance->SetAudioParameter(parmKey, "", value);
433 return 0;
434 }
435
IsWhispering()436 int32_t AudioServer::IsWhispering()
437 {
438 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySystemPermission(), ERR_SYSTEM_PERMISSION_DENIED,
439 "Check playback permission failed, no system permission");
440 std::lock_guard<std::mutex> lockSet(audioParameterMutex_);
441 std::string key = "asr_is_whisper";
442 AudioParamKey parmKey = AudioParamKey::NONE;
443 IAudioRendererSink *audioRendererSinkInstance = IAudioRendererSink::GetInstance("primary", "");
444 CHECK_AND_RETURN_RET_LOG(audioRendererSinkInstance != nullptr, ERROR, "has no valid sink");
445
446 std::string isWhisperSink = audioRendererSinkInstance->GetAudioParameter(parmKey, key);
447 int32_t whisperRes = 0;
448 if (isWhisperSink == "TRUE") {
449 whisperRes = 1;
450 }
451 return whisperRes;
452 }
453
454 } // namespace AudioStandard
455 } // namespace OHOS