• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "AudioAdapterManager"
17 #endif
18 
19 #include "audio_adapter_manager.h"
20 
21 
22 #include "parameter.h"
23 #include "parameters.h"
24 
25 #include "audio_volume_parser.h"
26 #include "audio_utils.h"
27 #include "audio_volume.h"
28 
29 using namespace std;
30 
31 namespace OHOS {
32 namespace AudioStandard {
33 static const std::vector<AudioStreamType> VOLUME_TYPE_LIST = {
34     // all volume types except STREAM_ALL
35     STREAM_MUSIC,
36     STREAM_RING,
37     STREAM_VOICE_CALL,
38     STREAM_VOICE_ASSISTANT,
39     STREAM_ALARM,
40     STREAM_ACCESSIBILITY,
41     STREAM_ULTRASONIC,
42     STREAM_VOICE_CALL_ASSISTANT
43 };
44 
45 static const std::vector<DeviceType> VOLUME_GROUP_TYPE_LIST = {
46     DEVICE_TYPE_EARPIECE,
47     DEVICE_TYPE_SPEAKER,
48     DEVICE_TYPE_BLUETOOTH_A2DP,
49     DEVICE_TYPE_WIRED_HEADSET,
50     DEVICE_TYPE_REMOTE_CAST
51 };
52 
53 static const std::vector<AudioStreamType> VOICE_CALL_VOLUME_TYPE_LIST = {
54     // all stream types for voice call volume type
55     STREAM_VOICE_CALL,
56     STREAM_VOICE_MESSAGE,
57     STREAM_VOICE_COMMUNICATION
58 };
59 
60 static const std::vector<AudioStreamType> RINGTONE_VOLUME_TYPE_LIST = {
61     // all stream types for ringtone volume type
62     STREAM_RING,
63     STREAM_VOICE_RING,
64     STREAM_SYSTEM,
65     STREAM_NOTIFICATION,
66     STREAM_SYSTEM_ENFORCED,
67     STREAM_DTMF
68 };
69 
70 static const std::vector<AudioStreamType> MEDIA_VOLUME_TYPE_LIST = {
71     // all stream types for media volume type
72     STREAM_MUSIC,
73     STREAM_MOVIE,
74     STREAM_GAME,
75     STREAM_SPEECH,
76     STREAM_NAVIGATION
77 };
78 
79 static const std::vector<std::string> SYSTEM_SOUND_KEY_LIST = {
80     // all keys for system sound uri
81     "ringtone_for_sim_card_0",
82     "ringtone_for_sim_card_1",
83     "system_tone_for_sim_card_0",
84     "system_tone_for_sim_card_1",
85     "system_tone_for_notification"
86 };
87 
88 // LCOV_EXCL_START
Init()89 bool AudioAdapterManager::Init()
90 {
91     char testMode[10] = {0}; // 10 for system parameter usage
92     auto res = GetParameter("debug.audio_service.testmodeon", "0", testMode, sizeof(testMode));
93     if (res == 1 && testMode[0] == '1') {
94         AUDIO_DEBUG_LOG("testMode on");
95         testModeOn_ = true;
96     }
97 
98     std::unique_ptr<AudioVolumeParser> audiovolumeParser = make_unique<AudioVolumeParser>();
99     if (!audiovolumeParser->LoadConfig(streamVolumeInfos_)) {
100         AUDIO_INFO_LOG("Audio Volume Config Load Configuration successfully");
101         useNonlinearAlgo_ = 1;
102         UpdateVolumeMapIndex();
103     }
104 
105     // init volume before kvstore start by local prop for bootanimation
106     char currentVolumeValue[3] = {0};
107     auto ret = GetParameter("persist.multimedia.audio.ringtonevolume", "7",
108         currentVolumeValue, sizeof(currentVolumeValue));
109     if (ret > 0) {
110         int32_t ringtoneVolumeLevel = atoi(currentVolumeValue);
111         volumeDataMaintainer_.SetStreamVolume(STREAM_RING, ringtoneVolumeLevel);
112         AUDIO_INFO_LOG("Init: Get ringtone volume to map success %{public}d",
113             volumeDataMaintainer_.GetStreamVolume(STREAM_RING));
114     } else {
115         AUDIO_ERR_LOG("Init: Get volume parameter failed %{public}d", ret);
116     }
117 
118     std::string defaultSafeVolume = std::to_string(GetMaxVolumeLevel(STREAM_MUSIC));
119     AUDIO_INFO_LOG("defaultSafeVolume %{public}s", defaultSafeVolume.c_str());
120     char currentSafeVolumeValue[3] = {0};
121     ret = GetParameter("const.audio.safe_media_volume", defaultSafeVolume.c_str(),
122         currentSafeVolumeValue, sizeof(currentSafeVolumeValue));
123     if (ret > 0) {
124         safeVolume_ = atoi(currentSafeVolumeValue);
125         AUDIO_INFO_LOG("Get currentSafeVolumeValue success %{public}d", safeVolume_);
126     } else {
127         AUDIO_ERR_LOG("Get currentSafeVolumeValue failed %{public}d", ret);
128     }
129 
130     char safeVolumeTimeout[6] = {0};
131     ret = GetParameter("persist.multimedia.audio.safevolume.timeout", "1140",
132         safeVolumeTimeout, sizeof(safeVolumeTimeout));
133     if (ret > 0) {
134         safeVolumeTimeout_ = atoi(safeVolumeTimeout);
135         AUDIO_INFO_LOG("Get safeVolumeTimeout success %{public}d", safeVolumeTimeout_);
136     } else {
137         AUDIO_ERR_LOG("Get safeVolumeTimeout failed %{public}d", ret);
138     }
139 
140     handler_ = std::make_shared<AudioAdapterManagerHandler>();
141 
142     isVolumeUnadjustable_ = system::GetBoolParameter("const.multimedia.audio.fixedvolume", false);
143     AUDIO_INFO_LOG("Get fixdvolume parameter success %{public}d", isVolumeUnadjustable_);
144 
145     return true;
146 }
147 
ConnectServiceAdapter()148 bool AudioAdapterManager::ConnectServiceAdapter()
149 {
150     std::unique_ptr<PolicyCallbackImpl> policyCallbackImpl = std::make_unique<PolicyCallbackImpl>(this);
151     audioServiceAdapter_ = AudioServiceAdapter::CreateAudioAdapter(std::move(policyCallbackImpl));
152     CHECK_AND_RETURN_RET_LOG(audioServiceAdapter_, false,
153         "[AudioAdapterManager] Error in audio adapter initialization");
154 
155     bool result = audioServiceAdapter_->Connect();
156     CHECK_AND_RETURN_RET_LOG(result, false, "[AudioAdapterManager] Error in connecting audio adapter");
157 
158     return true;
159 }
160 
InitKVStore()161 void AudioAdapterManager::InitKVStore()
162 {
163     InitKVStoreInternal();
164 }
165 
InitKVStoreInternal()166 void AudioAdapterManager::InitKVStoreInternal()
167 {
168     CHECK_AND_RETURN_LOG(!isLoaded_, "InitKVStore: the database value is loaded");
169 
170     AUDIO_INFO_LOG("in");
171     bool isFirstBoot = false;
172     volumeDataMaintainer_.RegisterCloned();
173     InitAudioPolicyKvStore(isFirstBoot);
174 
175     if (handler_ != nullptr) {
176         handler_->SendKvDataUpdate(isFirstBoot);
177     }
178 }
179 
HandleKvData(bool isFirstBoot)180 void AudioAdapterManager::HandleKvData(bool isFirstBoot)
181 {
182     InitVolumeMap(isFirstBoot);
183     InitRingerMode(isFirstBoot);
184     InitMuteStatusMap(isFirstBoot);
185     InitSafeStatus(isFirstBoot);
186     InitSafeTime(isFirstBoot);
187 
188     if (isNeedCopySystemUrlData_) {
189         CloneSystemSoundUrl();
190     }
191 
192     if (!isNeedCopyVolumeData_ && !isNeedCopyMuteData_ && !isNeedCopyRingerModeData_ && !isNeedCopySystemUrlData_) {
193         isAllCopyDone_ = true;
194         if (audioPolicyServerHandler_ != nullptr) {
195             audioPolicyServerHandler_->SendRingerModeUpdatedCallback(ringerMode_);
196             SetVolumeCallbackAfterClone();
197         }
198     }
199 
200     if (isAllCopyDone_ && audioPolicyKvStore_ != nullptr) {
201         // delete KvStore
202         InitSafeStatus(true);
203         InitSafeTime(true);
204         AUDIO_INFO_LOG("Copy audio_policy private database success to settings database, delete private database...");
205         DeleteAudioPolicyKvStore();
206     }
207 
208     // Make sure that the volume value is applied.
209     auto iter = VOLUME_TYPE_LIST.begin();
210     while (iter != VOLUME_TYPE_LIST.end()) {
211         SetVolumeDb(*iter);
212         iter++;
213     }
214 }
215 
ReInitKVStore()216 int32_t AudioAdapterManager::ReInitKVStore()
217 {
218     CHECK_AND_RETURN_RET_LOG(audioPolicyKvStore_ != nullptr, ERR_INVALID_OPERATION,
219         "audioPolicyKvStore_ is already nullptr");
220     audioPolicyKvStore_ = nullptr;
221     DistributedKvDataManager manager;
222     Options options;
223 
224     AppId appId;
225     appId.appId = "audio_policy_manager";
226     options.baseDir = std::string("/data/service/el1/public/database/") + appId.appId;
227 
228     StoreId storeId;
229     storeId.storeId = "audiopolicy";
230     Status status = Status::SUCCESS;
231 
232     status = manager.CloseKvStore(appId, storeId);
233     AUDIO_ERR_LOG("CloseKvStore status: %{public}d", status);
234     CHECK_AND_RETURN_RET_LOG(status == Status::SUCCESS, ERR_ILLEGAL_STATE, "CloseKvStore failed!");
235 
236     status = manager.DeleteKvStore(appId, storeId, options.baseDir);
237     CHECK_AND_RETURN_RET_LOG(status == Status::SUCCESS, ERR_ILLEGAL_STATE, "CloseKvStore failed!");
238 
239     InitKVStoreInternal();
240     return SUCCESS;
241 }
242 
Deinit(void)243 void AudioAdapterManager::Deinit(void)
244 {
245     CHECK_AND_RETURN_LOG(audioServiceAdapter_, "Deinit audio adapter null");
246 
247     if (handler_ != nullptr) {
248         AUDIO_INFO_LOG("release handler");
249         handler_->ReleaseEventRunner();
250         handler_ = nullptr;
251     }
252     return audioServiceAdapter_->Disconnect();
253 }
254 
SetAudioStreamRemovedCallback(AudioStreamRemovedCallback * callback)255 int32_t AudioAdapterManager::SetAudioStreamRemovedCallback(AudioStreamRemovedCallback *callback)
256 {
257     CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_INVALID_PARAM,
258         "SetAudioStreamRemovedCallback callback == nullptr");
259 
260     sessionCallback_ = callback;
261     return SUCCESS;
262 }
263 
264 // LCOV_EXCL_STOP
GetMaxVolumeLevel(AudioVolumeType volumeType)265 int32_t AudioAdapterManager::GetMaxVolumeLevel(AudioVolumeType volumeType)
266 {
267     CHECK_AND_RETURN_RET_LOG(volumeType >= STREAM_VOICE_CALL && volumeType <= STREAM_TYPE_MAX,
268         ERR_INVALID_PARAM, "Invalid stream type");
269     return maxVolumeIndexMap_[volumeType];
270 }
271 
GetMinVolumeLevel(AudioVolumeType volumeType)272 int32_t AudioAdapterManager::GetMinVolumeLevel(AudioVolumeType volumeType)
273 {
274     CHECK_AND_RETURN_RET_LOG(volumeType >= STREAM_VOICE_CALL && volumeType <= STREAM_TYPE_MAX,
275         ERR_INVALID_PARAM, "Invalid stream type");
276     return minVolumeIndexMap_[volumeType];
277 }
278 
SaveRingtoneVolumeToLocal(AudioVolumeType volumeType,int32_t volumeLevel)279 void AudioAdapterManager::SaveRingtoneVolumeToLocal(AudioVolumeType volumeType, int32_t volumeLevel)
280 {
281     if (volumeType == STREAM_RING) {
282         int32_t ret = SetParameter("persist.multimedia.audio.ringtonevolume", std::to_string(volumeLevel).c_str());
283         if (ret == 0) {
284             AUDIO_INFO_LOG("Save ringtone volume for boot success %{public}d", volumeLevel);
285         } else {
286             AUDIO_ERR_LOG("Save ringtone volume for boot failed, result %{public}d", ret);
287         }
288     }
289 }
290 
SetSystemVolumeLevel(AudioStreamType streamType,int32_t volumeLevel)291 int32_t AudioAdapterManager::SetSystemVolumeLevel(AudioStreamType streamType, int32_t volumeLevel)
292 {
293     if (GetSystemVolumeLevel(streamType) == volumeLevel && currentActiveDevice_ != DEVICE_TYPE_BLUETOOTH_SCO &&
294         currentActiveDevice_ != DEVICE_TYPE_BLUETOOTH_A2DP) {
295         AUDIO_INFO_LOG("The volume is the same as before.");
296         return SUCCESS;
297     }
298     AUDIO_INFO_LOG("SetSystemVolumeLevel: streamType: %{public}d, deviceType: %{public}d, volumeLevel:%{public}d",
299         streamType, currentActiveDevice_, volumeLevel);
300     if (volumeLevel == 0 &&
301         (streamType == STREAM_VOICE_ASSISTANT || streamType == STREAM_VOICE_CALL ||
302         streamType == STREAM_ALARM || streamType == STREAM_ACCESSIBILITY ||
303         streamType == STREAM_VOICE_COMMUNICATION)) {
304         // these types can not set to mute, but don't return error
305         AUDIO_ERR_LOG("SetSystemVolumeLevel this type can not set mute");
306         return SUCCESS;
307     }
308     int32_t mimRet = GetMinVolumeLevel(streamType);
309     int32_t maxRet = GetMaxVolumeLevel(streamType);
310     CHECK_AND_RETURN_RET_LOG(volumeLevel >= mimRet && volumeLevel <= maxRet, ERR_OPERATION_FAILED,
311         "volumeLevel not in scope.");
312 
313     // In case if KvStore didnot connect during bootup
314     if (!isLoaded_) {
315         InitKVStoreInternal();
316     }
317 
318     if (currentActiveDevice_ == DEVICE_TYPE_BLUETOOTH_SCO || currentActiveDevice_ == DEVICE_TYPE_BLUETOOTH_A2DP) {
319         if (isBtFirstSetVolume_ && volumeLevel > safeVolume_) {
320             volumeLevel = safeVolume_;
321             isBtFirstSetVolume_ = false;
322         }
323     }
324 
325     volumeDataMaintainer_.SetStreamVolume(streamType, volumeLevel);
326 
327     if (handler_ != nullptr) {
328         if (Util::IsDualToneStreamType(streamType)) {
329             AUDIO_INFO_LOG("DualToneStreamType. Save volume for speaker.");
330             handler_->SendSaveVolume(DEVICE_TYPE_SPEAKER, streamType, volumeLevel);
331         } else {
332             handler_->SendSaveVolume(currentActiveDevice_, streamType, volumeLevel);
333         }
334     }
335 
336     return SetVolumeDb(streamType);
337 }
338 
HandleSaveVolume(DeviceType deviceType,AudioStreamType streamType,int32_t volumeLevel)339 void AudioAdapterManager::HandleSaveVolume(DeviceType deviceType, AudioStreamType streamType, int32_t volumeLevel)
340 {
341     volumeDataMaintainer_.SaveVolume(deviceType, streamType, volumeLevel);
342 }
343 
HandleStreamMuteStatus(AudioStreamType streamType,bool mute,StreamUsage streamUsage)344 void AudioAdapterManager::HandleStreamMuteStatus(AudioStreamType streamType, bool mute, StreamUsage streamUsage)
345 {
346     volumeDataMaintainer_.SaveMuteStatus(currentActiveDevice_, streamType, mute);
347 }
348 
HandleRingerMode(AudioRingerMode ringerMode)349 void AudioAdapterManager::HandleRingerMode(AudioRingerMode ringerMode)
350 {
351     // In case if KvStore didnot connect during bootup
352     if (!isLoaded_) {
353         InitKVStoreInternal();
354     }
355 
356     AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(STREAM_RING);
357     int32_t volumeLevel =
358         volumeDataMaintainer_.GetStreamVolume(STREAM_RING) * ((ringerMode != RINGER_MODE_NORMAL) ? 0 : 1);
359 
360     // Save volume in local prop for bootanimation
361     SaveRingtoneVolumeToLocal(streamForVolumeMap, volumeLevel);
362 
363     volumeDataMaintainer_.SaveRingerMode(ringerMode);
364 }
365 
SetAudioServerProxy(sptr<IStandardAudioService> gsp)366 void AudioAdapterManager::SetAudioServerProxy(sptr<IStandardAudioService> gsp)
367 {
368     CHECK_AND_RETURN_LOG(gsp != nullptr, "audioServerProxy null");
369     audioServerProxy_ = gsp;
370 }
371 
SetVolumeDb(AudioStreamType streamType)372 int32_t AudioAdapterManager::SetVolumeDb(AudioStreamType streamType)
373 {
374     AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
375     int32_t volumeLevel =
376         volumeDataMaintainer_.GetStreamVolume(streamType) * (GetStreamMute(streamType) ? 0 : 1);
377 
378     // Save volume in local prop for bootanimation
379     SaveRingtoneVolumeToLocal(streamForVolumeMap, volumeLevel);
380 
381     float volumeDb = 1.0f;
382     if (useNonlinearAlgo_) {
383         if (Util::IsDualToneStreamType(streamType)) {
384             volumeDb = CalculateVolumeDbNonlinear(streamType, DEVICE_TYPE_SPEAKER, volumeLevel);
385         } else {
386             volumeDb = CalculateVolumeDbNonlinear(streamType, currentActiveDevice_, volumeLevel);
387         }
388     } else {
389         volumeDb = CalculateVolumeDb(volumeLevel);
390     }
391     // Set voice call assistant stream to full volume
392     if (streamType == STREAM_VOICE_CALL_ASSISTANT) {
393         volumeDb = 1.0f;
394     }
395 
396     CHECK_AND_RETURN_RET_LOG(audioServiceAdapter_, ERR_OPERATION_FAILED,
397         "SetSystemVolumeLevel audio adapter null");
398 
399     AUDIO_INFO_LOG("streamType:%{public}d volumeDb:%{public}f volume:%{public}d", streamType, volumeDb, volumeLevel);
400     if (streamType == STREAM_VOICE_CALL || streamType == STREAM_VOICE_COMMUNICATION) {
401         return SetVolumeDbForVolumeTypeGroup(VOICE_CALL_VOLUME_TYPE_LIST, volumeDb);
402     } else if (streamType == STREAM_MUSIC) {
403         return SetVolumeDbForVolumeTypeGroup(MEDIA_VOLUME_TYPE_LIST, volumeDb);
404     } else if (streamType == STREAM_RING || streamType == STREAM_VOICE_RING) {
405         return SetVolumeDbForVolumeTypeGroup(RINGTONE_VOLUME_TYPE_LIST, volumeDb);
406     }
407 
408     // VGS feature
409     if (IsVgsVolumeSupported()) {
410         float roundValue = static_cast<int>(1.0 * CONST_FACTOR);
411         volumeDb = static_cast<float>(roundValue) / CONST_FACTOR;
412         AUDIO_INFO_LOG("volumeDb: %{public}f", volumeDb);
413     }
414 
415     // audio volume
416     SetAudioVolume(streamType, volumeDb);
417 
418     return audioServiceAdapter_->SetVolumeDb(streamType, volumeDb);
419 }
420 
SetVolumeDbForVolumeTypeGroup(const std::vector<AudioStreamType> & volumeTypeGroup,float volumeDb)421 int32_t AudioAdapterManager::SetVolumeDbForVolumeTypeGroup(const std::vector<AudioStreamType> &volumeTypeGroup,
422     float volumeDb)
423 {
424     int32_t result = SUCCESS;
425     // audio volume
426     if (volumeTypeGroup.size() > 0) {
427         SetAudioVolume(volumeTypeGroup[0], volumeDb);
428     }
429 
430     for (auto &streamType: volumeTypeGroup) {
431         result = audioServiceAdapter_->SetVolumeDb(streamType, volumeDb);
432         if (result != SUCCESS) {
433             // The operation of setting volume has failed, return error directly.
434             return result;
435         }
436     }
437     return result;
438 }
439 
SetAudioVolume(AudioStreamType streamType,float volumeDb)440 void AudioAdapterManager::SetAudioVolume(AudioStreamType streamType, float volumeDb)
441 {
442     static std::unordered_map<DeviceType, std::vector<std::string>> deviceClassMap = {
443         {DEVICE_TYPE_SPEAKER, {PRIMARY_CLASS, MCH_CLASS, REMOTE_CLASS, OFFLOAD_CLASS}},
444         {DEVICE_TYPE_USB_HEADSET, {PRIMARY_CLASS, MCH_CLASS, OFFLOAD_CLASS}},
445         {DEVICE_TYPE_BLUETOOTH_A2DP, {A2DP_CLASS, PRIMARY_CLASS, MCH_CLASS, OFFLOAD_CLASS}},
446         {DEVICE_TYPE_BLUETOOTH_SCO, {PRIMARY_CLASS, MCH_CLASS}},
447         {DEVICE_TYPE_EARPIECE, {PRIMARY_CLASS, MCH_CLASS}},
448         {DEVICE_TYPE_WIRED_HEADSET, {PRIMARY_CLASS, MCH_CLASS}},
449         {DEVICE_TYPE_WIRED_HEADPHONES, {PRIMARY_CLASS, MCH_CLASS}},
450         {DEVICE_TYPE_USB_ARM_HEADSET, {USB_CLASS}},
451         {DEVICE_TYPE_REMOTE_CAST, {REMOTE_CAST_INNER_CAPTURER_SINK_NAME}},
452         {DEVICE_TYPE_DP, {DP_CLASS}},
453         {DEVICE_TYPE_FILE_SINK, {FILE_CLASS}},
454         {DEVICE_TYPE_FILE_SOURCE, {FILE_CLASS}},
455     };
456 
457     AudioStreamType volumeType = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
458     bool isMuted = GetStreamMute(volumeType);
459     int32_t volumeLevel = volumeDataMaintainer_.GetStreamVolume(volumeType) * (isMuted ? 0 : 1);
460     if (GetActiveDevice() == DEVICE_TYPE_BLUETOOTH_A2DP && IsAbsVolumeScene() && volumeType == STREAM_MUSIC) {
461         isMuted = IsAbsVolumeMute();
462         volumeLevel = volumeDataMaintainer_.GetStreamVolume(volumeType) * (isMuted ? 0 : 1);
463         volumeDb = isMuted ? 0.0f : 0.63957f; // 0.63957 = -4dB
464     }
465     auto audioVolume = AudioVolume::GetInstance();
466     CHECK_AND_RETURN_LOG(audioVolume != nullptr, "audioVolume handle null");
467     auto it = deviceClassMap.find(GetActiveDevice());
468     if (it == deviceClassMap.end()) {
469         AUDIO_ERR_LOG("unkown device type %{public}d", GetActiveDevice());
470         return;
471     }
472     for (auto &deviceClass : it->second) {
473         SystemVolume systemVolume(volumeType, deviceClass, volumeDb, volumeLevel, isMuted);
474         if (deviceClass != OFFLOAD_CLASS) {
475             audioVolume->SetSystemVolume(systemVolume);
476         } else if (deviceClass == OFFLOAD_CLASS && volumeType == STREAM_MUSIC) {
477             audioVolume->SetSystemVolume(systemVolume);
478             SetOffloadVolume(volumeType, volumeDb);
479         }
480     }
481 }
482 
SetOffloadVolume(AudioStreamType streamType,float volumeDb)483 void AudioAdapterManager::SetOffloadVolume(AudioStreamType streamType, float volumeDb)
484 {
485     if (!(streamType == STREAM_MUSIC || streamType == STREAM_SPEECH)) {
486         return;
487     }
488     DeviceType dev = GetActiveDevice();
489     if (!(dev == DEVICE_TYPE_SPEAKER || dev == DEVICE_TYPE_BLUETOOTH_A2DP || dev == DEVICE_TYPE_USB_HEADSET)) {
490         return;
491     }
492     CHECK_AND_RETURN_LOG(audioServerProxy_ != nullptr, "audioServerProxy_ null");
493     std::string identity = IPCSkeleton::ResetCallingIdentity();
494     audioServerProxy_->OffloadSetVolume(volumeDb);
495     IPCSkeleton::SetCallingIdentity(identity);
496 }
497 
GetSystemVolumeLevel(AudioStreamType streamType)498 int32_t AudioAdapterManager::GetSystemVolumeLevel(AudioStreamType streamType)
499 {
500     if (GetStreamMuteInternal(streamType)) {
501         return MIN_VOLUME_LEVEL;
502     }
503 
504     return volumeDataMaintainer_.GetStreamVolume(streamType);
505 }
506 
GetSystemVolumeLevelNoMuteState(AudioStreamType streamType)507 int32_t AudioAdapterManager::GetSystemVolumeLevelNoMuteState(AudioStreamType streamType)
508 {
509     return volumeDataMaintainer_.GetStreamVolume(streamType);
510 }
511 
GetSystemVolumeDb(AudioStreamType streamType)512 float AudioAdapterManager::GetSystemVolumeDb(AudioStreamType streamType)
513 {
514     int32_t volumeLevel = volumeDataMaintainer_.GetStreamVolume(streamType);
515     return CalculateVolumeDb(volumeLevel);
516 }
517 
SetStreamMute(AudioStreamType streamType,bool mute,StreamUsage streamUsage)518 int32_t AudioAdapterManager::SetStreamMute(AudioStreamType streamType, bool mute, StreamUsage streamUsage)
519 {
520     return SetStreamMuteInternal(streamType, mute, streamUsage);
521 }
522 
SetStreamMuteInternal(AudioStreamType streamType,bool mute,StreamUsage streamUsage)523 int32_t AudioAdapterManager::SetStreamMuteInternal(AudioStreamType streamType, bool mute,
524     StreamUsage streamUsage)
525 {
526     AUDIO_INFO_LOG("stream type %{public}d, mute:%{public}d, streamUsage:%{public}d", streamType, mute, streamUsage);
527     if (mute &&
528         (streamType == STREAM_VOICE_ASSISTANT || streamType == STREAM_VOICE_CALL ||
529         streamType == STREAM_ALARM || streamType == STREAM_ACCESSIBILITY ||
530         streamType == STREAM_VOICE_COMMUNICATION)) {
531         // these types can not set to mute, but don't return error
532         AUDIO_ERR_LOG("SetStreamMute: this type can not set mute");
533         return SUCCESS;
534     }
535     if (Util::IsDualToneStreamType(streamType) && currentActiveDevice_ != DEVICE_TYPE_SPEAKER &&
536         GetRingerMode() != RINGER_MODE_NORMAL && mute && Util::IsRingerOrAlarmerStreamUsage(streamUsage)) {
537         AUDIO_INFO_LOG("Dual tone stream type %{public}d, current active device:[%{public}d] is no speaker, dont mute",
538             streamType, currentActiveDevice_);
539         return SUCCESS;
540     }
541 
542     // set stream mute status to mem.
543     volumeDataMaintainer_.SetStreamMuteStatus(streamType, mute);
544 
545     if (handler_ != nullptr) {
546         handler_->SendStreamMuteStatusUpdate(streamType, mute, streamUsage);
547     }
548 
549     // Achieve the purpose of adjusting the mute status by adjusting the stream volume.
550     return SetVolumeDb(streamType);
551 }
552 
SetPersistMicMuteState(const bool isMute)553 int32_t AudioAdapterManager::SetPersistMicMuteState(const bool isMute)
554 {
555     AUDIO_INFO_LOG("Save mute state: %{public}d in setting db", isMute);
556     bool res = volumeDataMaintainer_.SaveMicMuteState(isMute);
557 
558     return res == true ? SUCCESS : ERROR;
559 }
560 
GetPersistMicMuteState(bool & isMute) const561 int32_t AudioAdapterManager::GetPersistMicMuteState(bool &isMute) const
562 {
563     bool res = volumeDataMaintainer_.GetMicMuteState(isMute);
564     AUDIO_INFO_LOG("Get mute state from setting db is: %{public}d", isMute);
565 
566     return res == true ? SUCCESS : ERROR;
567 }
568 
SetSourceOutputStreamMute(int32_t uid,bool setMute)569 int32_t AudioAdapterManager::SetSourceOutputStreamMute(int32_t uid, bool setMute)
570 {
571     CHECK_AND_RETURN_RET_LOG(audioServiceAdapter_, ERR_OPERATION_FAILED,
572         "SetSourceOutputStreamMute audio adapter null");
573     return audioServiceAdapter_->SetSourceOutputMute(uid, setMute);
574 }
575 
GetStreamMute(AudioStreamType streamType)576 bool AudioAdapterManager::GetStreamMute(AudioStreamType streamType)
577 {
578     return GetStreamMuteInternal(streamType);
579 }
580 
GetStreamVolume(AudioStreamType streamType)581 int32_t AudioAdapterManager::GetStreamVolume(AudioStreamType streamType)
582 {
583     return volumeDataMaintainer_.GetStreamVolume(streamType);
584 }
585 
GetStreamMuteInternal(AudioStreamType streamType)586 bool AudioAdapterManager::GetStreamMuteInternal(AudioStreamType streamType)
587 {
588     return volumeDataMaintainer_.GetStreamMute(streamType);
589 }
590 
591 // LCOV_EXCL_START
GetAllSinks()592 vector<SinkInfo> AudioAdapterManager::GetAllSinks()
593 {
594     if (!audioServiceAdapter_) {
595         AUDIO_ERR_LOG("GetAllSinks audio adapter null");
596         vector<SinkInfo> sinkInputList;
597         return sinkInputList;
598     }
599 
600     return audioServiceAdapter_->GetAllSinks();
601 }
602 
GetAllSinkInputs()603 vector<SinkInput> AudioAdapterManager::GetAllSinkInputs()
604 {
605     if (!audioServiceAdapter_) {
606         AUDIO_ERR_LOG("GetAllSinkInputs audio adapter null");
607         vector<SinkInput> sinkInputList;
608         return sinkInputList;
609     }
610 
611     return audioServiceAdapter_->GetAllSinkInputs();
612 }
613 
GetAllSourceOutputs()614 vector<SourceOutput> AudioAdapterManager::GetAllSourceOutputs()
615 {
616     if (!audioServiceAdapter_) {
617         AUDIO_ERR_LOG("GetAllSourceOutputs audio adapter null");
618         vector<SourceOutput> sourceOutputList;
619         return sourceOutputList;
620     }
621 
622     return audioServiceAdapter_->GetAllSourceOutputs();
623 }
624 
SuspendAudioDevice(std::string & portName,bool isSuspend)625 int32_t AudioAdapterManager::SuspendAudioDevice(std::string &portName, bool isSuspend)
626 {
627     CHECK_AND_RETURN_RET_LOG(audioServiceAdapter_, ERR_OPERATION_FAILED,
628         "SuspendAudioDevice audio adapter null");
629 
630     return audioServiceAdapter_->SuspendAudioDevice(portName, isSuspend);
631 }
632 
SetSinkMute(const std::string & sinkName,bool isMute,bool isSync)633 bool AudioAdapterManager::SetSinkMute(const std::string &sinkName, bool isMute, bool isSync)
634 {
635     static std::unordered_map<std::string, std::string> sinkNameMap = {
636         {PRIMARY_SPEAKER, PRIMARY_CLASS},
637         {OFFLOAD_PRIMARY_SPEAKER, OFFLOAD_CLASS},
638         {BLUETOOTH_SPEAKER, A2DP_CLASS},
639         {MCH_PRIMARY_SPEAKER, MCH_CLASS},
640         {USB_SPEAKER, USB_CLASS},
641         {DP_SINK, DP_CLASS},
642         {FILE_SINK, FILE_CLASS},
643         {REMOTE_CAST_INNER_CAPTURER_SINK_NAME, REMOTE_CAST_INNER_CAPTURER_SINK_NAME},
644     };
645     CHECK_AND_RETURN_RET_LOG(audioServiceAdapter_, false, "SetSinkMute audio adapter null");
646     auto audioVolume = AudioVolume::GetInstance();
647     CHECK_AND_RETURN_RET_LOG(audioVolume, false, "SetSinkMute audioVolume handle null");
648     auto it = sinkNameMap.find(sinkName);
649     for (auto &volumeType : VOLUME_TYPE_LIST) {
650         if (it != sinkNameMap.end()) {
651             if ((it->second == OFFLOAD_CLASS && volumeType == STREAM_MUSIC) ||
652                 it->second != OFFLOAD_CLASS) {
653                 audioVolume->SetSystemVolumeMute(volumeType, it->second, isMute);
654             }
655         } else if (sinkName.find("_out") != std::string::npos &&
656             sinkName.find(LOCAL_NETWORK_ID) == std::string::npos) {
657             audioVolume->SetSystemVolumeMute(volumeType, REMOTE_CLASS, isMute);
658         } else {
659             AUDIO_ERR_LOG("unkown sink name %{public}s", sinkName.c_str());
660         }
661     }
662 
663     return audioServiceAdapter_->SetSinkMute(sinkName, isMute, isSync);
664 }
665 
SelectDevice(DeviceRole deviceRole,InternalDeviceType deviceType,std::string name)666 int32_t AudioAdapterManager::SelectDevice(DeviceRole deviceRole, InternalDeviceType deviceType, std::string name)
667 {
668     CHECK_AND_RETURN_RET_LOG(audioServiceAdapter_, ERR_OPERATION_FAILED,
669         "SelectDevice audio adapter null");
670     switch (deviceRole) {
671         case DeviceRole::INPUT_DEVICE:
672             return audioServiceAdapter_->SetDefaultSource(name);
673         case DeviceRole::OUTPUT_DEVICE: {
674             AUDIO_INFO_LOG("SetDefaultSink %{public}d", deviceType);
675             return audioServiceAdapter_->SetDefaultSink(name);
676         }
677         default:
678             AUDIO_ERR_LOG("SelectDevice error deviceRole %{public}d", deviceRole);
679             return ERR_OPERATION_FAILED;
680     }
681     return SUCCESS;
682 }
683 
SetDeviceActive(InternalDeviceType deviceType,std::string name,bool active,DeviceFlag flag)684 int32_t AudioAdapterManager::SetDeviceActive(InternalDeviceType deviceType,
685     std::string name, bool active, DeviceFlag flag)
686 {
687     CHECK_AND_RETURN_RET_LOG(audioServiceAdapter_, ERR_OPERATION_FAILED,
688         "SetDeviceActive audio adapter null");
689 
690     switch (deviceType) {
691         case InternalDeviceType::DEVICE_TYPE_USB_ARM_HEADSET: {
692             if (name == USB_SPEAKER) {
693                 return audioServiceAdapter_->SetDefaultSink(name);
694             } else {
695                 return audioServiceAdapter_->SetDefaultSource(name);
696             }
697         }
698         default: {
699             int32_t ret = SUCCESS;
700             int32_t errs[2]{SUCCESS, SUCCESS};
701             if (IsInputDevice(deviceType) && (flag & INPUT_DEVICES_FLAG)) {
702                 AUDIO_INFO_LOG("SetDefaultSource %{public}d", deviceType);
703                 errs[0] = audioServiceAdapter_->SetDefaultSource(name);
704                 if (errs[0] != SUCCESS) {
705                     AUDIO_ERR_LOG("SetDefaultSource err: %{public}d", errs[0]);
706                     ret = errs[0];
707                 }
708             }
709             if (IsOutputDevice(deviceType) && (flag & OUTPUT_DEVICES_FLAG)) {
710                 AUDIO_INFO_LOG("SetDefaultSink %{public}d", deviceType);
711                 errs[1] = audioServiceAdapter_->SetDefaultSink(name);
712                 if (errs[1] != SUCCESS) {
713                     AUDIO_ERR_LOG("SetDefaultSink err: %{public}d", errs[1]);
714                     ret = errs[1];
715                 }
716             }
717             // Ensure compatibility across different platforms and versions
718             if (errs[0] == SUCCESS || errs[1] == SUCCESS) {
719                 return SUCCESS;
720             }
721             return ret;
722         }
723     }
724     return SUCCESS;
725 }
726 
SetVolumeForSwitchDevice(InternalDeviceType deviceType)727 void AudioAdapterManager::SetVolumeForSwitchDevice(InternalDeviceType deviceType)
728 {
729     if (!isLoaded_) {
730         AUDIO_ERR_LOG("The data base is not loaded. Can not load new volume for new device!");
731         // The ring volume is also saved in audio_config.para.
732         // So the boot animation can still play with right volume.
733         return;
734     }
735 
736     if (deviceType == DEVICE_TYPE_BLUETOOTH_A2DP && IsAbsVolumeScene()) {
737         SetVolumeDb(STREAM_MUSIC);
738         currentActiveDevice_ = deviceType;
739         return;
740     }
741 
742     // The same device does not set the volume
743     // Except for A2dp, because the currentActiveDevice_ has already been set in Activea2dpdevice.
744     bool isSameVolumeGroup = GetVolumeGroupForDevice(currentActiveDevice_) == GetVolumeGroupForDevice(deviceType);
745     if (currentActiveDevice_ == deviceType &&
746         deviceType != DEVICE_TYPE_BLUETOOTH_A2DP && deviceType != DEVICE_TYPE_BLUETOOTH_SCO) {
747         AUDIO_INFO_LOG("Old device: %{public}d. New device: %{public}d. No need to update volume",
748             currentActiveDevice_, deviceType);
749         return;
750     }
751 
752     AUDIO_INFO_LOG("SetVolumeForSwitchDevice: Load volume and mute status for new device %{public}d,"
753         "same volume group %{public}d", deviceType, isSameVolumeGroup);
754     // Current device must be updated even if kvStore is nullptr.
755     currentActiveDevice_ = deviceType;
756 
757     if (!isSameVolumeGroup || deviceType == DEVICE_TYPE_BLUETOOTH_A2DP ||
758         deviceType == DEVICE_TYPE_BLUETOOTH_SCO) {
759         LoadVolumeMap();
760         LoadMuteStatusMap();
761         UpdateSafeVolume();
762     }
763 
764     auto iter = VOLUME_TYPE_LIST.begin();
765     while (iter != VOLUME_TYPE_LIST.end()) {
766         // update volume level and mute status for each stream type
767         SetVolumeDb(*iter);
768         AUDIO_INFO_LOG("SetVolumeForSwitchDevice: volume: %{public}d, mute: %{public}d for stream type %{public}d",
769             volumeDataMaintainer_.GetStreamVolume(*iter), volumeDataMaintainer_.GetStreamMute(*iter), *iter);
770         iter++;
771     }
772 }
773 
MoveSinkInputByIndexOrName(uint32_t sinkInputId,uint32_t sinkIndex,std::string sinkName)774 int32_t AudioAdapterManager::MoveSinkInputByIndexOrName(uint32_t sinkInputId, uint32_t sinkIndex, std::string sinkName)
775 {
776     return audioServiceAdapter_->MoveSinkInputByIndexOrName(sinkInputId, sinkIndex, sinkName);
777 }
778 
MoveSourceOutputByIndexOrName(uint32_t sourceOutputId,uint32_t sourceIndex,std::string sourceName)779 int32_t AudioAdapterManager::MoveSourceOutputByIndexOrName(uint32_t sourceOutputId, uint32_t sourceIndex,
780     std::string sourceName)
781 {
782     return audioServiceAdapter_->MoveSourceOutputByIndexOrName(sourceOutputId, sourceIndex, sourceName);
783 }
784 
785 // LCOV_EXCL_STOP
SetRingerMode(AudioRingerMode ringerMode)786 int32_t AudioAdapterManager::SetRingerMode(AudioRingerMode ringerMode)
787 {
788     return SetRingerModeInternal(ringerMode);
789 }
790 
SetRingerModeInternal(AudioRingerMode ringerMode)791 int32_t AudioAdapterManager::SetRingerModeInternal(AudioRingerMode ringerMode)
792 {
793     AUDIO_INFO_LOG("SetRingerMode: %{public}d", ringerMode);
794     ringerMode_ = ringerMode;
795 
796     if (handler_ != nullptr) {
797         handler_->SendRingerModeUpdate(ringerMode);
798     }
799     return SUCCESS;
800 }
801 
GetRingerMode() const802 AudioRingerMode AudioAdapterManager::GetRingerMode() const
803 {
804     return ringerMode_;
805 }
806 
807 // LCOV_EXCL_START
OpenAudioPort(const AudioModuleInfo & audioModuleInfo)808 AudioIOHandle AudioAdapterManager::OpenAudioPort(const AudioModuleInfo &audioModuleInfo)
809 {
810     std::string moduleArgs = GetModuleArgs(audioModuleInfo);
811 
812     AUDIO_INFO_LOG("[Adapter load-module] %{public}s %{public}s",
813         audioModuleInfo.lib.c_str(), audioModuleInfo.className.c_str());
814 
815     CHECK_AND_RETURN_RET_LOG(audioServiceAdapter_ != nullptr, ERR_OPERATION_FAILED, "ServiceAdapter is null");
816     curActiveCount_++;
817     AudioIOHandle ioHandle = audioServiceAdapter_->OpenAudioPort(audioModuleInfo.lib, moduleArgs.c_str());
818     AUDIO_INFO_LOG("Open %{public}d port end.", static_cast<int32_t>(ioHandle));
819     return ioHandle;
820 }
821 
CloseAudioPort(AudioIOHandle ioHandle)822 int32_t AudioAdapterManager::CloseAudioPort(AudioIOHandle ioHandle)
823 {
824     CHECK_AND_RETURN_RET_LOG(audioServiceAdapter_ != nullptr, ERR_OPERATION_FAILED, "ServiceAdapter is null");
825     curActiveCount_--;
826     int32_t ret = audioServiceAdapter_->CloseAudioPort(ioHandle);
827     AUDIO_INFO_LOG("Close %{public}d port end.", static_cast<int32_t>(ioHandle));
828     return ret;
829 }
830 
GetCurActivateCount() const831 int32_t AudioAdapterManager::GetCurActivateCount() const
832 {
833     return curActiveCount_ > 0 ? curActiveCount_ : 0;
834 }
835 
UpdateSinkArgs(const AudioModuleInfo & audioModuleInfo,std::string & args)836 void UpdateSinkArgs(const AudioModuleInfo &audioModuleInfo, std::string &args)
837 {
838     if (!audioModuleInfo.name.empty()) {
839         args.append(" sink_name=");
840         args.append(audioModuleInfo.name);
841     }
842 
843     if (!audioModuleInfo.adapterName.empty()) {
844         args.append(" adapter_name=");
845         args.append(audioModuleInfo.adapterName);
846     }
847 
848     if (!audioModuleInfo.className.empty()) {
849         args.append(" device_class=");
850         args.append(audioModuleInfo.className);
851     }
852 
853     if (!audioModuleInfo.fileName.empty()) {
854         args.append(" file_path=");
855         args.append(audioModuleInfo.fileName);
856     }
857     if (!audioModuleInfo.sinkLatency.empty()) {
858         args.append(" sink_latency=");
859         args.append(audioModuleInfo.sinkLatency);
860     }
861 
862     if (!audioModuleInfo.networkId.empty()) {
863         args.append(" network_id=");
864         args.append(audioModuleInfo.networkId);
865     } else {
866         args.append(" network_id=LocalDevice");
867     }
868 
869     if (!audioModuleInfo.deviceType.empty()) {
870         args.append(" device_type=");
871         args.append(audioModuleInfo.deviceType);
872     }
873 
874     if (!audioModuleInfo.extra.empty()) {
875         args.append(" split_mode=");
876         args.append(audioModuleInfo.extra);
877     }
878 }
879 
UpdateSourceArgs(const AudioModuleInfo & audioModuleInfo,std::string & args)880 void UpdateSourceArgs(const AudioModuleInfo &audioModuleInfo, std::string &args)
881 {
882     if (!audioModuleInfo.name.empty()) {
883         args.append(" source_name=");
884         args.append(audioModuleInfo.name);
885     }
886 
887     if (!audioModuleInfo.adapterName.empty()) {
888         args.append(" adapter_name=");
889         args.append(audioModuleInfo.adapterName);
890     }
891 
892     if (!audioModuleInfo.className.empty()) {
893         args.append(" device_class=");
894         args.append(audioModuleInfo.className);
895     }
896 
897     if (!audioModuleInfo.fileName.empty()) {
898         args.append(" file_path=");
899         args.append(audioModuleInfo.fileName);
900     }
901 
902     if (!audioModuleInfo.networkId.empty()) {
903         args.append(" network_id=");
904         args.append(audioModuleInfo.networkId);
905     } else {
906         args.append(" network_id=LocalDevice");
907     }
908 
909     if (!audioModuleInfo.deviceType.empty()) {
910         args.append(" device_type=");
911         args.append(audioModuleInfo.deviceType);
912     }
913 
914     if (!audioModuleInfo.sourceType.empty()) {
915         args.append(" source_type=");
916         args.append(audioModuleInfo.sourceType);
917     }
918 }
919 
UpdateCommonArgs(const AudioModuleInfo & audioModuleInfo,std::string & args)920 void UpdateCommonArgs(const AudioModuleInfo &audioModuleInfo, std::string &args)
921 {
922     if (!audioModuleInfo.rate.empty()) {
923         args = "rate=";
924         args.append(audioModuleInfo.rate);
925     }
926 
927     if (!audioModuleInfo.channels.empty()) {
928         args.append(" channels=");
929         args.append(audioModuleInfo.channels);
930     }
931 
932     if (!audioModuleInfo.bufferSize.empty()) {
933         args.append(" buffer_size=");
934         args.append(audioModuleInfo.bufferSize);
935     }
936 
937     if (!audioModuleInfo.format.empty()) {
938         args.append(" format=");
939         args.append(audioModuleInfo.format);
940     }
941 
942     if (!audioModuleInfo.fixedLatency.empty()) {
943         args.append(" fixed_latency=");
944         args.append(audioModuleInfo.fixedLatency);
945     }
946 
947     if (!audioModuleInfo.renderInIdleState.empty()) {
948         args.append(" render_in_idle_state=");
949         args.append(audioModuleInfo.renderInIdleState);
950     }
951 
952     if (!audioModuleInfo.OpenMicSpeaker.empty()) {
953         args.append(" open_mic_speaker=");
954         args.append(audioModuleInfo.OpenMicSpeaker);
955     }
956 
957     if (!audioModuleInfo.offloadEnable.empty()) {
958         args.append(" offload_enable=");
959         args.append(audioModuleInfo.offloadEnable);
960     }
961     AUDIO_INFO_LOG("[Adapter load-module] [PolicyManager] common args:%{public}s", args.c_str());
962 }
963 
964 // Private Members
GetModuleArgs(const AudioModuleInfo & audioModuleInfo) const965 std::string AudioAdapterManager::GetModuleArgs(const AudioModuleInfo &audioModuleInfo) const
966 {
967     std::string args;
968 
969     if (audioModuleInfo.lib == HDI_SINK) {
970         UpdateCommonArgs(audioModuleInfo, args);
971         UpdateSinkArgs(audioModuleInfo, args);
972         if (testModeOn_) {
973             args.append(" test_mode_on=");
974             args.append("1");
975         }
976     } else if (audioModuleInfo.lib == SPLIT_STREAM_SINK) {
977         UpdateCommonArgs(audioModuleInfo, args);
978         UpdateSinkArgs(audioModuleInfo, args);
979     } else if (audioModuleInfo.lib == HDI_SOURCE) {
980         UpdateCommonArgs(audioModuleInfo, args);
981         UpdateSourceArgs(audioModuleInfo, args);
982     } else if (audioModuleInfo.lib == PIPE_SINK) {
983         if (!audioModuleInfo.fileName.empty()) {
984             args = "file=";
985             args.append(audioModuleInfo.fileName);
986         }
987     } else if (audioModuleInfo.lib == PIPE_SOURCE) {
988         if (!audioModuleInfo.fileName.empty()) {
989             args = "file=";
990             args.append(audioModuleInfo.fileName);
991         }
992     } else if (audioModuleInfo.lib == CLUSTER_SINK) {
993         UpdateCommonArgs(audioModuleInfo, args);
994         if (!audioModuleInfo.name.empty()) {
995             args.append(" sink_name=");
996             args.append(audioModuleInfo.name);
997         }
998     } else if (audioModuleInfo.lib == EFFECT_SINK) {
999         UpdateCommonArgs(audioModuleInfo, args);
1000         if (!audioModuleInfo.name.empty()) {
1001             args.append(" sink_name=");
1002             args.append(audioModuleInfo.name);
1003         }
1004         if (!audioModuleInfo.sceneName.empty()) {
1005             args.append(" scene_name=");
1006             args.append(audioModuleInfo.sceneName);
1007         }
1008     } else if (audioModuleInfo.lib == INNER_CAPTURER_SINK || audioModuleInfo.lib == RECEIVER_SINK) {
1009         UpdateCommonArgs(audioModuleInfo, args);
1010         if (!audioModuleInfo.name.empty()) {
1011             args.append(" sink_name=");
1012             args.append(audioModuleInfo.name);
1013         }
1014     }
1015     return args;
1016 }
1017 
GetVolumeKeyForKvStore(DeviceType deviceType,AudioStreamType streamType)1018 std::string AudioAdapterManager::GetVolumeKeyForKvStore(DeviceType deviceType, AudioStreamType streamType)
1019 {
1020     DeviceGroup type = GetVolumeGroupForDevice(deviceType);
1021     std::string typeStr = std::to_string(type);
1022     CHECK_AND_RETURN_RET_LOG(type != DEVICE_GROUP_INVALID, typeStr,
1023         "Device %{public}d is not supported for kvStore", deviceType);
1024 
1025     switch (streamType) {
1026         case STREAM_MUSIC:
1027             return typeStr + "_music_volume";
1028         case STREAM_RING:
1029         case STREAM_VOICE_RING:
1030             return typeStr + "_ring_volume";
1031         case STREAM_SYSTEM:
1032             return typeStr + "_system_volume";
1033         case STREAM_NOTIFICATION:
1034             return typeStr + "_notification_volume";
1035         case STREAM_ALARM:
1036             return typeStr + "_alarm_volume";
1037         case STREAM_DTMF:
1038             return typeStr + "_dtmf_volume";
1039         case STREAM_VOICE_CALL:
1040         case STREAM_VOICE_COMMUNICATION:
1041             return typeStr + "_voice_call_volume";
1042         case STREAM_VOICE_ASSISTANT:
1043             return typeStr + "_voice_assistant_volume";
1044         case STREAM_ACCESSIBILITY:
1045             return typeStr + "_accessibility_volume";
1046         case STREAM_ULTRASONIC:
1047             return typeStr + "_ultrasonic_volume";
1048         case STREAM_WAKEUP:
1049             return typeStr + "wakeup";
1050         default:
1051             AUDIO_ERR_LOG("GetVolumeKeyForKvStore: streamType %{public}d is not supported for kvStore", streamType);
1052             return "";
1053     }
1054 }
1055 
GetStreamIDByType(std::string streamType)1056 AudioStreamType AudioAdapterManager::GetStreamIDByType(std::string streamType)
1057 {
1058     AudioStreamType stream = STREAM_MUSIC;
1059 
1060     if (!streamType.compare(std::string("music")))
1061         stream = STREAM_MUSIC;
1062     else if (!streamType.compare(std::string("ring")))
1063         stream = STREAM_RING;
1064     else if (!streamType.compare(std::string("voice_call")))
1065         stream = STREAM_VOICE_CALL;
1066     else if (!streamType.compare(std::string("system")))
1067         stream = STREAM_SYSTEM;
1068     else if (!streamType.compare(std::string("notification")))
1069         stream = STREAM_NOTIFICATION;
1070     else if (!streamType.compare(std::string("alarm")))
1071         stream = STREAM_ALARM;
1072     else if (!streamType.compare(std::string("voice_assistant")))
1073         stream = STREAM_VOICE_ASSISTANT;
1074     else if (!streamType.compare(std::string("accessibility")))
1075         stream = STREAM_ACCESSIBILITY;
1076     else if (!streamType.compare(std::string("ultrasonic")))
1077         stream = STREAM_ULTRASONIC;
1078     else if (!streamType.compare(std::string("camcorder")))
1079         stream = STREAM_CAMCORDER;
1080     return stream;
1081 }
1082 
GetDeviceCategory(DeviceType deviceType)1083 DeviceVolumeType AudioAdapterManager::GetDeviceCategory(DeviceType deviceType)
1084 {
1085     switch (deviceType) {
1086         case DEVICE_TYPE_EARPIECE:
1087             return EARPIECE_VOLUME_TYPE;
1088         case DEVICE_TYPE_SPEAKER:
1089         case DEVICE_TYPE_FILE_SOURCE:
1090             return SPEAKER_VOLUME_TYPE;
1091         case DEVICE_TYPE_WIRED_HEADSET:
1092         case DEVICE_TYPE_WIRED_HEADPHONES:
1093         case DEVICE_TYPE_BLUETOOTH_SCO:
1094         case DEVICE_TYPE_BLUETOOTH_A2DP:
1095         case DEVICE_TYPE_USB_HEADSET:
1096         case DEVICE_TYPE_DP:
1097             return HEADSET_VOLUME_TYPE;
1098         default:
1099             return SPEAKER_VOLUME_TYPE;
1100     }
1101 }
1102 
InitAudioPolicyKvStore(bool & isFirstBoot)1103 bool AudioAdapterManager::InitAudioPolicyKvStore(bool& isFirstBoot)
1104 {
1105     DistributedKvDataManager manager;
1106     Options options;
1107 
1108     AppId appId;
1109     appId.appId = "audio_policy_manager";
1110 
1111     options.securityLevel = S1;
1112     options.createIfMissing = false;
1113     options.encrypt = false;
1114     options.autoSync = false;
1115     options.kvStoreType = KvStoreType::SINGLE_VERSION;
1116     options.area = EL1;
1117     options.baseDir = std::string("/data/service/el1/public/database/") + appId.appId;
1118 
1119     StoreId storeId;
1120     storeId.storeId = "audiopolicy";
1121     Status status = Status::SUCCESS;
1122 
1123     // open and initialize kvstore instance.
1124     if (audioPolicyKvStore_ == nullptr) {
1125         uint32_t retries = 0;
1126 
1127         do {
1128             status = manager.GetSingleKvStore(options, appId, storeId, audioPolicyKvStore_);
1129             AUDIO_ERR_LOG("GetSingleKvStore status: %{public}d", status);
1130             if ((status == Status::SUCCESS) || (status == Status::INVALID_ARGUMENT) ||
1131                 (status == Status::DATA_CORRUPTED) || (status == Status::CRYPT_ERROR)) {
1132                 break;
1133             } else {
1134                 AUDIO_ERR_LOG("InitAudioPolicyKvStore: Kvstore Connect failed! Retrying.");
1135                 retries++;
1136                 usleep(KVSTORE_CONNECT_RETRY_DELAY_TIME);
1137             }
1138         } while (retries <= KVSTORE_CONNECT_RETRY_COUNT);
1139     }
1140 
1141     if (audioPolicyKvStore_ != nullptr) {
1142         isNeedCopyVolumeData_ = true;
1143         isNeedCopyMuteData_ = true;
1144         isNeedCopyRingerModeData_ = true;
1145         isNeedCopySystemUrlData_ = true;
1146         volumeDataMaintainer_.SetFirstBoot(false);
1147         return true;
1148     }
1149     // first boot
1150     if (!volumeDataMaintainer_.GetFirstBoot(isFirstBoot)) {
1151         AUDIO_INFO_LOG("first boot, ready init data to database");
1152         isFirstBoot = true;
1153         volumeDataMaintainer_.SetFirstBoot(false);
1154     }
1155 
1156     return true;
1157 }
1158 
DeleteAudioPolicyKvStore()1159 void AudioAdapterManager::DeleteAudioPolicyKvStore()
1160 {
1161     DistributedKvDataManager manager;
1162     Options options;
1163 
1164     AppId appId;
1165     appId.appId = "audio_policy_manager";
1166 
1167     options.securityLevel = S1;
1168     options.createIfMissing = false;
1169     options.encrypt = false;
1170     options.autoSync = false;
1171     options.kvStoreType = KvStoreType::SINGLE_VERSION;
1172     options.area = EL1;
1173     options.baseDir = std::string("/data/service/el1/public/database/") + appId.appId;
1174 
1175     StoreId storeId;
1176     storeId.storeId = "audiopolicy";
1177     Status status = Status::SUCCESS;
1178 
1179     if (audioPolicyKvStore_ != nullptr) {
1180         status = manager.CloseKvStore(appId, storeId);
1181         if (status != Status::SUCCESS) {
1182             AUDIO_ERR_LOG("close KvStore failed");
1183         }
1184         status = manager.DeleteKvStore(appId, storeId, options.baseDir);
1185         if (status != Status::SUCCESS) {
1186             AUDIO_ERR_LOG("DeleteKvStore failed");
1187         }
1188         audioPolicyKvStore_ = nullptr;
1189     }
1190 }
1191 
UpdateSafeVolume()1192 void AudioAdapterManager::UpdateSafeVolume()
1193 {
1194     if (volumeDataMaintainer_.GetStreamVolume(STREAM_MUSIC) <= safeVolume_) {
1195         return;
1196     }
1197     AUDIO_INFO_LOG("update current volume to safevolume, device:%{public}d", currentActiveDevice_);
1198     switch (currentActiveDevice_) {
1199         case DEVICE_TYPE_WIRED_HEADSET:
1200         case DEVICE_TYPE_WIRED_HEADPHONES:
1201         case DEVICE_TYPE_USB_HEADSET:
1202         case DEVICE_TYPE_USB_ARM_HEADSET:
1203             if (isWiredBoot_) {
1204                 volumeDataMaintainer_.SetStreamVolume(STREAM_MUSIC, safeVolume_);
1205                 volumeDataMaintainer_.SaveVolume(currentActiveDevice_, STREAM_MUSIC, safeVolume_);
1206                 isWiredBoot_ = false;
1207             }
1208             break;
1209         case DEVICE_TYPE_BLUETOOTH_SCO:
1210         case DEVICE_TYPE_BLUETOOTH_A2DP:
1211             if (isBtBoot_) {
1212                 volumeDataMaintainer_.SetStreamVolume(STREAM_MUSIC, safeVolume_);
1213                 volumeDataMaintainer_.SaveVolume(currentActiveDevice_, STREAM_MUSIC, safeVolume_);
1214                 isBtBoot_ = false;
1215             }
1216             break;
1217         default:
1218             AUDIO_ERR_LOG("current device: %{public}d is not support", currentActiveDevice_);
1219             break;
1220     }
1221 }
1222 
InitVolumeMap(bool isFirstBoot)1223 void AudioAdapterManager::InitVolumeMap(bool isFirstBoot)
1224 {
1225     if (!isFirstBoot) {
1226         LoadVolumeMap();
1227         UpdateSafeVolume();
1228         return;
1229     }
1230     AUDIO_INFO_LOG("InitVolumeMap: Wrote default stream volumes to KvStore");
1231     std::unordered_map<AudioStreamType, int32_t> volumeLevelMapTemp = volumeDataMaintainer_.GetVolumeMap();
1232     for (auto &deviceType: VOLUME_GROUP_TYPE_LIST) {
1233         for (auto &streamType: VOLUME_TYPE_LIST) {
1234             // if GetVolume failed, wirte default value
1235             if (!volumeDataMaintainer_.GetVolume(deviceType, streamType)) {
1236                 volumeDataMaintainer_.SaveVolume(deviceType, streamType, volumeLevelMapTemp[streamType]);
1237             }
1238         }
1239     }
1240     // reLoad the current device volume
1241     LoadVolumeMap();
1242     UpdateSafeVolume();
1243 }
1244 
ResetRemoteCastDeviceVolume()1245 void AudioAdapterManager::ResetRemoteCastDeviceVolume()
1246 {
1247     for (auto &streamType: VOLUME_TYPE_LIST) {
1248         AudioStreamType streamAlias = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
1249         int32_t volumeLevel = GetMaxVolumeLevel(streamAlias);
1250         volumeDataMaintainer_.SaveVolume(DEVICE_TYPE_REMOTE_CAST, streamType, volumeLevel);
1251     }
1252 }
1253 
InitRingerMode(bool isFirstBoot)1254 void AudioAdapterManager::InitRingerMode(bool isFirstBoot)
1255 {
1256     if (isFirstBoot) {
1257         ringerMode_ = RINGER_MODE_NORMAL;
1258         isLoaded_ = true;
1259         if (!volumeDataMaintainer_.GetRingerMode(ringerMode_)) {
1260             isLoaded_ = volumeDataMaintainer_.SaveRingerMode(RINGER_MODE_NORMAL);
1261         }
1262         AUDIO_INFO_LOG("InitRingerMode first boot ringermode:%{public}d", ringerMode_);
1263     } else {
1264         // read ringerMode from private kvStore
1265         if (isNeedCopyRingerModeData_ && audioPolicyKvStore_ != nullptr) {
1266             AUDIO_INFO_LOG("copy ringerMode from private database to share database");
1267             Key key = "ringermode";
1268             Value value;
1269             Status status = audioPolicyKvStore_->Get(key, value);
1270             if (status == Status::SUCCESS) {
1271                 ringerMode_ = static_cast<AudioRingerMode>(TransferByteArrayToType<int>(value.Data()));
1272                 volumeDataMaintainer_.SaveRingerMode(ringerMode_);
1273             }
1274             isNeedCopyRingerModeData_ = false;
1275         }
1276         // if read ringer mode success, data is loaded.
1277         isLoaded_ = volumeDataMaintainer_.GetRingerMode(ringerMode_);
1278     }
1279     AudioStreamType streamForVolumeMap = VolumeUtils::GetVolumeTypeFromStreamType(STREAM_RING);
1280     int32_t volumeLevel =
1281         volumeDataMaintainer_.GetStreamVolume(STREAM_RING) * ((ringerMode_ != RINGER_MODE_NORMAL) ? 0 : 1);
1282     // Save volume in local prop for bootanimation
1283     SaveRingtoneVolumeToLocal(streamForVolumeMap, volumeLevel);
1284 }
1285 
CloneVolumeMap(void)1286 void AudioAdapterManager::CloneVolumeMap(void)
1287 {
1288     CHECK_AND_RETURN_LOG(audioPolicyKvStore_ != nullptr, "clone volumemap failed, audioPolicyKvStore_nullptr");
1289     // read volume from private Kvstore
1290     AUDIO_INFO_LOG("Copy Volume from private database to shareDatabase");
1291     for (auto &deviceType : VOLUME_GROUP_TYPE_LIST) {
1292         for (auto &streamType : VOLUME_TYPE_LIST) {
1293             std::string volumeKey = GetVolumeKeyForKvStore(deviceType, streamType);
1294             Key key = volumeKey;
1295             Value value;
1296             Status status = audioPolicyKvStore_->Get(volumeKey, value);
1297             if (status != SUCCESS) {
1298                 AUDIO_WARNING_LOG("get volumeLevel failed, deviceType:%{public}d, streanType:%{public}d",
1299                     deviceType, streamType);
1300                 continue;
1301             }
1302             int32_t volumeLevel = TransferByteArrayToType<int>(value.Data());
1303             // clone data to VolumeToShareData
1304             volumeDataMaintainer_.SaveVolume(deviceType, streamType, volumeLevel);
1305         }
1306     }
1307 
1308     isNeedCopyVolumeData_ = false;
1309 }
1310 
LoadVolumeMap(void)1311 bool AudioAdapterManager::LoadVolumeMap(void)
1312 {
1313     if (isNeedCopyVolumeData_ && (audioPolicyKvStore_ != nullptr)) {
1314         CloneVolumeMap();
1315     }
1316 
1317     bool result = false;
1318     for (auto &streamType: VOLUME_TYPE_LIST) {
1319         if (Util::IsDualToneStreamType(streamType)) {
1320             result = volumeDataMaintainer_.GetVolume(DEVICE_TYPE_SPEAKER, streamType);
1321         } else {
1322             result = volumeDataMaintainer_.GetVolume(currentActiveDevice_, streamType);
1323         }
1324         if (!result) {
1325             AUDIO_ERR_LOG("LoadVolumeMap: Could not load volume for streamType[%{public}d] from kvStore", streamType);
1326         }
1327     }
1328 
1329     return true;
1330 }
1331 
TransferMuteStatus(void)1332 void AudioAdapterManager::TransferMuteStatus(void)
1333 {
1334     // read mute_streams_affected and transfer
1335     int32_t mute_streams_affected = 0;
1336     bool isNeedTransferMute = true;
1337     bool ret = volumeDataMaintainer_.GetMuteAffected(mute_streams_affected) &&
1338         volumeDataMaintainer_.GetMuteTransferStatus(isNeedTransferMute);
1339     if (!ret && (mute_streams_affected > 0) && isNeedTransferMute) {
1340         AUDIO_INFO_LOG("start transfer mute value");
1341         volumeDataMaintainer_.SetMuteAffectedToMuteStatusDataBase(mute_streams_affected);
1342         volumeDataMaintainer_.SaveMuteTransferStatus(false);
1343     }
1344 }
1345 
InitMuteStatusMap(bool isFirstBoot)1346 void AudioAdapterManager::InitMuteStatusMap(bool isFirstBoot)
1347 {
1348     if (isFirstBoot) {
1349         for (auto &deviceType : VOLUME_GROUP_TYPE_LIST) {
1350             for (auto &streamType : VOLUME_TYPE_LIST) {
1351                 CheckAndDealMuteStatus(deviceType, streamType);
1352             }
1353         }
1354         TransferMuteStatus();
1355     } else {
1356         LoadMuteStatusMap();
1357     }
1358 }
1359 
CheckAndDealMuteStatus(const DeviceType & deviceType,const AudioStreamType & streamType)1360 void  AudioAdapterManager::CheckAndDealMuteStatus(const DeviceType &deviceType, const AudioStreamType &streamType)
1361 {
1362     if (streamType == STREAM_RING) {
1363         bool muteStateForStreamRing = (ringerMode_ == RINGER_MODE_NORMAL) ? false : true;
1364         AUDIO_INFO_LOG("fist boot ringer mode:%{public}d, stream ring mute state:%{public}d", ringerMode_,
1365             muteStateForStreamRing);
1366         // set stream mute status to mem.
1367         if (currentActiveDevice_ == deviceType) {
1368             volumeDataMaintainer_.SetStreamMuteStatus(streamType, muteStateForStreamRing);
1369         }
1370         volumeDataMaintainer_.SaveMuteStatus(deviceType, streamType, muteStateForStreamRing);
1371     } else if (!volumeDataMaintainer_.GetMuteStatus(deviceType, streamType)) {
1372         if (currentActiveDevice_ == deviceType) {
1373             volumeDataMaintainer_.SetStreamMuteStatus(streamType, false);
1374         }
1375         volumeDataMaintainer_.SaveMuteStatus(deviceType, streamType, false);
1376     }
1377     if (currentActiveDevice_ == deviceType) {
1378         SetVolumeDb(streamType);
1379     }
1380 }
1381 
SetVolumeCallbackAfterClone()1382 void AudioAdapterManager::SetVolumeCallbackAfterClone()
1383 {
1384     for (auto &streamType : VOLUME_TYPE_LIST) {
1385         VolumeEvent volumeEvent;
1386         volumeEvent.volumeType = streamType;
1387         volumeEvent.volume = GetSystemVolumeLevel(streamType);
1388         volumeEvent.updateUi = false;
1389         volumeEvent.volumeGroupId = 0;
1390         volumeEvent.networkId = LOCAL_NETWORK_ID;
1391         if (audioPolicyServerHandler_ != nullptr) {
1392             audioPolicyServerHandler_->SendVolumeKeyEventCallback(volumeEvent);
1393         }
1394     }
1395 }
1396 
CloneMuteStatusMap(void)1397 void AudioAdapterManager::CloneMuteStatusMap(void)
1398 {
1399     // read mute status from private Kvstore
1400     CHECK_AND_RETURN_LOG(audioPolicyKvStore_ != nullptr, "clone mute status failed, audioPolicyKvStore_ nullptr");
1401     AUDIO_INFO_LOG("Copy mute from private database to shareDatabase");
1402     for (auto &deviceType : VOLUME_GROUP_TYPE_LIST) {
1403         for (auto &streamType : VOLUME_TYPE_LIST) {
1404             std::string muteKey = GetMuteKeyForKvStore(deviceType, streamType);
1405             Key key = muteKey;
1406             Value value;
1407             Status status = audioPolicyKvStore_->Get(key, value);
1408             if (status != SUCCESS) {
1409                 AUDIO_WARNING_LOG("get muteStatus:failed, deviceType:%{public}d, streanType:%{public}d",
1410                     deviceType, streamType);
1411                 continue;
1412             }
1413             bool muteStatus = TransferByteArrayToType<int>(value.Data());
1414             // clone data to VolumeToShareData
1415             if (currentActiveDevice_ == deviceType) {
1416                 volumeDataMaintainer_.SetStreamMuteStatus(streamType, muteStatus);
1417             }
1418             volumeDataMaintainer_.SaveMuteStatus(deviceType, streamType, muteStatus);
1419         }
1420     }
1421     isNeedCopyMuteData_ = false;
1422 }
1423 
LoadMuteStatusMap(void)1424 bool AudioAdapterManager::LoadMuteStatusMap(void)
1425 {
1426     if (isNeedCopyMuteData_ && (audioPolicyKvStore_ != nullptr)) {
1427         CloneMuteStatusMap();
1428     }
1429 
1430     TransferMuteStatus();
1431 
1432     for (auto &streamType: VOLUME_TYPE_LIST) {
1433         bool result = volumeDataMaintainer_.GetMuteStatus(currentActiveDevice_, streamType);
1434         if (!result) {
1435             AUDIO_WARNING_LOG("Could not load mute status for stream type %{public}d from database.", streamType);
1436         }
1437         if (streamType == STREAM_RING) {
1438             bool muteStateForStreamRing = (ringerMode_ == RINGER_MODE_NORMAL) ? false : true;
1439             if (currentActiveDevice_ != DEVICE_TYPE_SPEAKER) {
1440                 continue;
1441             }
1442             AUDIO_INFO_LOG("ringer mode:%{public}d, stream ring mute state:%{public}d", ringerMode_,
1443                 muteStateForStreamRing);
1444             if (muteStateForStreamRing == GetStreamMute(streamType)) {
1445                 continue;
1446             }
1447             volumeDataMaintainer_.SaveMuteStatus(currentActiveDevice_, streamType, muteStateForStreamRing);
1448             SetStreamMute(streamType, muteStateForStreamRing);
1449         }
1450     }
1451     return true;
1452 }
1453 
InitSafeStatus(bool isFirstBoot)1454 void AudioAdapterManager::InitSafeStatus(bool isFirstBoot)
1455 {
1456     if (isFirstBoot) {
1457         AUDIO_INFO_LOG("Wrote default safe status to KvStore");
1458         for (auto &deviceType : VOLUME_GROUP_TYPE_LIST) {
1459             // Adapt to safe volume upgrade scenarios
1460             if (!volumeDataMaintainer_.GetSafeStatus(DEVICE_TYPE_WIRED_HEADSET, safeStatus_) &&
1461                 (deviceType == DEVICE_TYPE_WIRED_HEADSET)) {
1462                 volumeDataMaintainer_.SaveSafeStatus(DEVICE_TYPE_WIRED_HEADSET, SAFE_ACTIVE);
1463             }
1464             if (!volumeDataMaintainer_.GetSafeStatus(DEVICE_TYPE_BLUETOOTH_A2DP, safeStatusBt_) &&
1465                 (deviceType == DEVICE_TYPE_BLUETOOTH_A2DP)) {
1466                 volumeDataMaintainer_.SaveSafeStatus(DEVICE_TYPE_BLUETOOTH_A2DP, SAFE_ACTIVE);
1467             }
1468         }
1469     } else {
1470         volumeDataMaintainer_.GetSafeStatus(DEVICE_TYPE_WIRED_HEADSET, safeStatus_);
1471         volumeDataMaintainer_.GetSafeStatus(DEVICE_TYPE_BLUETOOTH_A2DP, safeStatusBt_);
1472     }
1473 }
1474 
InitSafeTime(bool isFirstBoot)1475 void AudioAdapterManager::InitSafeTime(bool isFirstBoot)
1476 {
1477     if (isFirstBoot) {
1478         AUDIO_INFO_LOG("Wrote default safe status to KvStore");
1479         for (auto &deviceType : VOLUME_GROUP_TYPE_LIST) {
1480             if (!volumeDataMaintainer_.GetSafeVolumeTime(DEVICE_TYPE_WIRED_HEADSET, safeActiveTime_) &&
1481                 (deviceType == DEVICE_TYPE_WIRED_HEADSET)) {
1482                 volumeDataMaintainer_.SaveSafeVolumeTime(DEVICE_TYPE_WIRED_HEADSET, 0);
1483             }
1484             if (!volumeDataMaintainer_.GetSafeVolumeTime(DEVICE_TYPE_BLUETOOTH_A2DP, safeActiveBtTime_) &&
1485                 (deviceType == DEVICE_TYPE_BLUETOOTH_A2DP)) {
1486                 volumeDataMaintainer_.SaveSafeVolumeTime(DEVICE_TYPE_BLUETOOTH_A2DP, 0);
1487             }
1488             ConvertSafeTime();
1489             isNeedConvertSafeTime_ = false;
1490         }
1491     } else {
1492         volumeDataMaintainer_.GetSafeVolumeTime(DEVICE_TYPE_WIRED_HEADSET, safeActiveTime_);
1493         volumeDataMaintainer_.GetSafeVolumeTime(DEVICE_TYPE_BLUETOOTH_A2DP, safeActiveBtTime_);
1494         if (isNeedConvertSafeTime_) {
1495             ConvertSafeTime();
1496             isNeedConvertSafeTime_ = false;
1497         }
1498     }
1499 }
1500 
ConvertSafeTime(void)1501 void AudioAdapterManager::ConvertSafeTime(void)
1502 {
1503     // Adapt to safe volume time when upgrade scenarios
1504     if (safeActiveTime_ > 0) {
1505         safeActiveTime_ = safeActiveTime_ / CONVERT_FROM_MS_TO_SECONDS;
1506         volumeDataMaintainer_.SaveSafeVolumeTime(DEVICE_TYPE_WIRED_HEADSET, safeActiveTime_);
1507     }
1508     if (safeActiveBtTime_ > 0) {
1509         safeActiveBtTime_ = safeActiveBtTime_ / CONVERT_FROM_MS_TO_SECONDS;
1510         volumeDataMaintainer_.SaveSafeVolumeTime(DEVICE_TYPE_BLUETOOTH_A2DP, safeActiveBtTime_);
1511     }
1512 }
1513 
GetCurrentDeviceSafeStatus(DeviceType deviceType)1514 SafeStatus AudioAdapterManager::GetCurrentDeviceSafeStatus(DeviceType deviceType)
1515 {
1516     switch (deviceType) {
1517         case DEVICE_TYPE_WIRED_HEADSET:
1518         case DEVICE_TYPE_WIRED_HEADPHONES:
1519         case DEVICE_TYPE_USB_HEADSET:
1520         case DEVICE_TYPE_USB_ARM_HEADSET:
1521             volumeDataMaintainer_.GetSafeStatus(DEVICE_TYPE_WIRED_HEADSET, safeStatus_);
1522             return safeStatus_;
1523         case DEVICE_TYPE_BLUETOOTH_SCO:
1524         case DEVICE_TYPE_BLUETOOTH_A2DP:
1525             volumeDataMaintainer_.GetSafeStatus(DEVICE_TYPE_BLUETOOTH_A2DP, safeStatusBt_);
1526             return safeStatusBt_;
1527         default:
1528             AUDIO_ERR_LOG("current device : %{public}d is not support", deviceType);
1529             break;
1530     }
1531 
1532     return SAFE_UNKNOWN;
1533 }
1534 
GetCurentDeviceSafeTime(DeviceType deviceType)1535 int64_t AudioAdapterManager::GetCurentDeviceSafeTime(DeviceType deviceType)
1536 {
1537     switch (deviceType) {
1538         case DEVICE_TYPE_WIRED_HEADSET:
1539         case DEVICE_TYPE_WIRED_HEADPHONES:
1540         case DEVICE_TYPE_USB_HEADSET:
1541         case DEVICE_TYPE_USB_ARM_HEADSET:
1542             volumeDataMaintainer_.GetSafeVolumeTime(DEVICE_TYPE_WIRED_HEADSET, safeActiveTime_);
1543             return safeActiveTime_;
1544         case DEVICE_TYPE_BLUETOOTH_SCO:
1545         case DEVICE_TYPE_BLUETOOTH_A2DP:
1546             volumeDataMaintainer_.GetSafeVolumeTime(DEVICE_TYPE_BLUETOOTH_A2DP, safeActiveBtTime_);
1547             return safeActiveBtTime_;
1548         default:
1549             AUDIO_ERR_LOG("current device : %{public}d is not support", deviceType);
1550             break;
1551     }
1552 
1553     return -1;
1554 }
1555 
SetDeviceSafeStatus(DeviceType deviceType,SafeStatus status)1556 int32_t AudioAdapterManager::SetDeviceSafeStatus(DeviceType deviceType, SafeStatus status)
1557 {
1558     if (deviceType == DEVICE_TYPE_BLUETOOTH_A2DP) {
1559         safeStatusBt_ = status;
1560     } else if (deviceType == DEVICE_TYPE_WIRED_HEADSET) {
1561         safeStatus_ = status;
1562     }
1563     bool ret = volumeDataMaintainer_.SaveSafeStatus(deviceType, status);
1564     CHECK_AND_RETURN_RET(ret, ERROR, "SaveSafeStatus failed");
1565     return SUCCESS;
1566 }
1567 
SetDeviceSafeTime(DeviceType deviceType,int64_t time)1568 int32_t AudioAdapterManager::SetDeviceSafeTime(DeviceType deviceType, int64_t time)
1569 {
1570     if (deviceType == DEVICE_TYPE_BLUETOOTH_A2DP) {
1571         safeActiveBtTime_ = time;
1572     } else if (deviceType == DEVICE_TYPE_WIRED_HEADSET) {
1573         safeActiveTime_ = time;
1574     }
1575     bool ret = volumeDataMaintainer_.SaveSafeVolumeTime(deviceType, time);
1576     CHECK_AND_RETURN_RET(ret, ERROR, "SetDeviceSafeTime failed");
1577     return SUCCESS;
1578 }
1579 
GetMuteKeyForKvStore(DeviceType deviceType,AudioStreamType streamType)1580 std::string AudioAdapterManager::GetMuteKeyForKvStore(DeviceType deviceType, AudioStreamType streamType)
1581 {
1582     std::string type = "";
1583     switch (deviceType) {
1584         case DEVICE_TYPE_EARPIECE:
1585         case DEVICE_TYPE_SPEAKER:
1586             type = "build-in";
1587             break;
1588         case DEVICE_TYPE_BLUETOOTH_A2DP:
1589         case DEVICE_TYPE_BLUETOOTH_SCO:
1590             type = "wireless";
1591             break;
1592         case DEVICE_TYPE_WIRED_HEADSET:
1593         case DEVICE_TYPE_USB_HEADSET:
1594         case DEVICE_TYPE_USB_ARM_HEADSET:
1595         case DEVICE_TYPE_DP:
1596             type = "wired";
1597             break;
1598         default:
1599             AUDIO_ERR_LOG("GetMuteKeyForKvStore: device %{public}d is not supported for kvStore", deviceType);
1600             return "";
1601     }
1602 
1603     switch (streamType) {
1604         case STREAM_MUSIC:
1605             return type + "_music_mute_status";
1606         case STREAM_RING:
1607         case STREAM_VOICE_RING:
1608             return type + "_ring_mute_status";
1609         case STREAM_SYSTEM:
1610             return type + "_system_mute_status";
1611         case STREAM_NOTIFICATION:
1612             return type + "_notification_mute_status";
1613         case STREAM_ALARM:
1614             return type + "_alarm_mute_status";
1615         case STREAM_DTMF:
1616             return type + "_dtmf_mute_status";
1617         case STREAM_VOICE_CALL:
1618         case STREAM_VOICE_COMMUNICATION:
1619             return type + "_voice_call_mute_status";
1620         case STREAM_VOICE_ASSISTANT:
1621             return type + "_voice_assistant_mute_status";
1622         case STREAM_ACCESSIBILITY:
1623             return type + "_accessibility_mute_status";
1624         case STREAM_ULTRASONIC:
1625             return type + "_unltrasonic_mute_status";
1626         default:
1627             AUDIO_ERR_LOG("GetMuteKeyForKvStore: streamType %{public}d is not supported for kvStore", streamType);
1628             return "";
1629     }
1630 }
1631 
CalculateVolumeDb(int32_t volumeLevel)1632 float AudioAdapterManager::CalculateVolumeDb(int32_t volumeLevel)
1633 {
1634     float value = static_cast<float>(volumeLevel) / MAX_VOLUME_LEVEL;
1635     float roundValue = static_cast<int>(value * CONST_FACTOR);
1636 
1637     return static_cast<float>(roundValue) / CONST_FACTOR;
1638 }
1639 
CloneSystemSoundUrl(void)1640 void AudioAdapterManager::CloneSystemSoundUrl(void)
1641 {
1642     CHECK_AND_RETURN_LOG(isNeedCopySystemUrlData_ && (audioPolicyKvStore_ != nullptr),
1643         "audioPolicyKvStore_ is nullptr,clone systemurl failed");
1644     for (auto &key: SYSTEM_SOUND_KEY_LIST) {
1645         Value value;
1646         Status status = audioPolicyKvStore_->Get(key, value);
1647         if (status == Status::SUCCESS) {
1648             std::string systemSoundUri = value.ToString();
1649             systemSoundUriMap_[key] = systemSoundUri;
1650             volumeDataMaintainer_.SaveSystemSoundUrl(key, systemSoundUri);
1651         }
1652     }
1653     isNeedCopySystemUrlData_ = false;
1654 }
1655 
InitSystemSoundUriMap()1656 void AudioAdapterManager::InitSystemSoundUriMap()
1657 {
1658     for (auto &key: SYSTEM_SOUND_KEY_LIST) {
1659         std::string systemSoundUri = "";
1660         volumeDataMaintainer_.GetSystemSoundUrl(key, systemSoundUri);
1661         if (systemSoundUri == "") {
1662             AUDIO_WARNING_LOG("Could not load system sound uri for %{public}s from kvStore", key.c_str());
1663         }
1664         systemSoundUriMap_[key] = systemSoundUri;
1665     }
1666 }
1667 
SetSystemSoundUri(const std::string & key,const std::string & uri)1668 int32_t AudioAdapterManager::SetSystemSoundUri(const std::string &key, const std::string &uri)
1669 {
1670     auto pos = std::find(SYSTEM_SOUND_KEY_LIST.begin(), SYSTEM_SOUND_KEY_LIST.end(), key);
1671     if (pos == SYSTEM_SOUND_KEY_LIST.end()) {
1672         AUDIO_ERR_LOG("Invalid key %{public}s for system sound uri", key.c_str());
1673         return ERR_INVALID_PARAM;
1674     }
1675     std::lock_guard<std::mutex> lock(systemSoundMutex_);
1676     if (systemSoundUriMap_.size() == 0) {
1677         InitSystemSoundUriMap();
1678         CHECK_AND_RETURN_RET_LOG(systemSoundUriMap_.size() != 0, ERR_OPERATION_FAILED,
1679             "Failed to init system sound uri map.");
1680     }
1681     systemSoundUriMap_[key] = uri;
1682     if (!volumeDataMaintainer_.SaveSystemSoundUrl(key, uri)) {
1683         AUDIO_ERR_LOG("SetSystemSoundUri failed");
1684         return ERROR;
1685     }
1686     return SUCCESS;
1687 }
1688 
GetSystemSoundUri(const std::string & key)1689 std::string AudioAdapterManager::GetSystemSoundUri(const std::string &key)
1690 {
1691     auto pos = std::find(SYSTEM_SOUND_KEY_LIST.begin(), SYSTEM_SOUND_KEY_LIST.end(), key);
1692     if (pos == SYSTEM_SOUND_KEY_LIST.end()) {
1693         AUDIO_ERR_LOG("Invalid key %{public}s for system sound uri", key.c_str());
1694         return "";
1695     }
1696     std::lock_guard<std::mutex> lock(systemSoundMutex_);
1697     if (systemSoundUriMap_.size() == 0) {
1698         InitSystemSoundUriMap();
1699         CHECK_AND_RETURN_RET_LOG(systemSoundUriMap_.size() != 0, "",
1700             "Failed to init system sound uri map.");
1701     }
1702     return systemSoundUriMap_[key];
1703 }
1704 
GetMinStreamVolume() const1705 float AudioAdapterManager::GetMinStreamVolume() const
1706 {
1707     return MIN_STREAM_VOLUME;
1708 }
1709 
GetMaxStreamVolume() const1710 float AudioAdapterManager::GetMaxStreamVolume() const
1711 {
1712     return MAX_STREAM_VOLUME;
1713 }
1714 
IsVolumeUnadjustable()1715 bool AudioAdapterManager::IsVolumeUnadjustable()
1716 {
1717     return isVolumeUnadjustable_;
1718 }
1719 
GetSystemVolumeInDb(AudioVolumeType volumeType,int32_t volumeLevel,DeviceType deviceType)1720 float AudioAdapterManager::GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType)
1721 {
1722     AUDIO_DEBUG_LOG("GetSystemVolumeInDb for volumeType: %{public}d deviceType:%{public}d volumeLevel:%{public}d",
1723         volumeType, deviceType, volumeLevel);
1724     if (useNonlinearAlgo_) {
1725         getSystemVolumeInDb_ = CalculateVolumeDbNonlinear(volumeType, deviceType, volumeLevel);
1726     } else {
1727         getSystemVolumeInDb_ = CalculateVolumeDb(volumeLevel);
1728     }
1729     AUDIO_DEBUG_LOG("Get system volume in db success %{public}f", getSystemVolumeInDb_);
1730 
1731     return getSystemVolumeInDb_;
1732 }
1733 
GetPositionInVolumePoints(std::vector<VolumePoint> & volumePoints,int32_t idx)1734 uint32_t AudioAdapterManager::GetPositionInVolumePoints(std::vector<VolumePoint> &volumePoints, int32_t idx)
1735 {
1736     int32_t leftPos = 0;
1737     int32_t rightPos = static_cast<int32_t>(volumePoints.size() - 1);
1738     while (leftPos <= rightPos) {
1739         int32_t midPos = leftPos + (rightPos - leftPos)/NUMBER_TWO;
1740         int32_t c = static_cast<int32_t>(volumePoints[midPos].index) - idx;
1741         if (c == 0) {
1742             leftPos = midPos;
1743             break;
1744         } else if (c < 0) {
1745             leftPos = midPos + 1;
1746         } else {
1747             rightPos = midPos - 1;
1748         }
1749     }
1750     return leftPos;
1751 }
1752 
CalculateVolumeDbNonlinear(AudioStreamType streamType,DeviceType deviceType,int32_t volumeLevel)1753 float AudioAdapterManager::CalculateVolumeDbNonlinear(AudioStreamType streamType,
1754     DeviceType deviceType, int32_t volumeLevel)
1755 {
1756     AUDIO_DEBUG_LOG("CalculateVolumeDbNonlinear for stream: %{public}d devicetype:%{public}d volumeLevel:%{public}d",
1757         streamType, deviceType, volumeLevel);
1758     AudioStreamType streamAlias = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
1759     int32_t minVolIndex = GetMinVolumeLevel(streamAlias);
1760     int32_t maxVolIndex = GetMaxVolumeLevel(streamAlias);
1761     if (minVolIndex < 0 || maxVolIndex < 0 || minVolIndex >= maxVolIndex) {
1762         return 0.0f;
1763     }
1764     if (volumeLevel < minVolIndex) {
1765         volumeLevel = minVolIndex;
1766     }
1767     if (volumeLevel > maxVolIndex) {
1768         volumeLevel = maxVolIndex;
1769     }
1770 
1771     DeviceVolumeType deviceCategory = GetDeviceCategory(deviceType);
1772     std::vector<VolumePoint> volumePoints;
1773     GetVolumePoints(streamAlias, deviceCategory, volumePoints);
1774     uint32_t pointSize = volumePoints.size();
1775 
1776     CHECK_AND_RETURN_RET_LOG(pointSize != 0, 1.0f, "pointSize is 0");
1777     int32_t volSteps = static_cast<int32_t>(1 + volumePoints[pointSize - 1].index - volumePoints[0].index);
1778     int32_t idxRatio = (volSteps * (volumeLevel - minVolIndex)) / (maxVolIndex - minVolIndex);
1779     int32_t position = static_cast<int32_t>(GetPositionInVolumePoints(volumePoints, idxRatio));
1780     if (position == 0) {
1781         if (minVolIndex != 0) {
1782             AUDIO_INFO_LOG("Min volume index not zero, use min db: %{public}0.1f", volumePoints[0].dbValue / 100.0f);
1783             return exp((volumePoints[0].dbValue / 100.0f) * 0.115129f);
1784         }
1785         AUDIO_DEBUG_LOG("position = 0, return 0.0");
1786         return 0.0f;
1787     } else if (position >= static_cast<int32_t>(pointSize)) {
1788         AUDIO_DEBUG_LOG("position > pointSize, return %{public}f",
1789             exp(volumePoints[pointSize - 1].dbValue * 0.115129f));
1790         return exp((volumePoints[pointSize - 1].dbValue / 100.0f) * 0.115129f);
1791     }
1792     float indexFactor = (static_cast<float>(idxRatio - static_cast<int32_t>(volumePoints[position - 1].index))) /
1793         (static_cast<float>(volumePoints[position].index - volumePoints[position - 1].index));
1794 
1795     float dbValue = (volumePoints[position - 1].dbValue / 100.0f) +
1796         indexFactor * ((volumePoints[position].dbValue / 100.0f) - (volumePoints[position - 1].dbValue / 100.0f));
1797 
1798     AUDIO_DEBUG_LOG(" index=[%{public}d, %{public}d, %{public}d]"
1799         "db=[%{public}0.1f %{public}0.1f %{public}0.1f] factor=[%{public}f]",
1800         volumePoints[position - 1].index, idxRatio, volumePoints[position].index,
1801         (static_cast<float>(volumePoints[position - 1].dbValue) / 100.0f), dbValue,
1802         (static_cast<float>(volumePoints[position].dbValue) / 100.0f), exp(dbValue * 0.115129f));
1803 
1804     return exp(dbValue * 0.115129f);
1805 }
1806 
InitVolumeMapIndex()1807 void AudioAdapterManager::InitVolumeMapIndex()
1808 {
1809     useNonlinearAlgo_ = 0;
1810     for (auto streamType : VOLUME_TYPE_LIST) {
1811         minVolumeIndexMap_[streamType] = MIN_VOLUME_LEVEL;
1812         maxVolumeIndexMap_[streamType] = MAX_VOLUME_LEVEL;
1813         volumeDataMaintainer_.SetStreamVolume(streamType, DEFAULT_VOLUME_LEVEL);
1814         AUDIO_DEBUG_LOG("streamType %{public}d index = [%{public}d, %{public}d, %{public}d]",
1815             streamType, minVolumeIndexMap_[streamType],
1816             maxVolumeIndexMap_[streamType], volumeDataMaintainer_.GetStreamVolume(streamType));
1817     }
1818 
1819     volumeDataMaintainer_.SetStreamVolume(STREAM_VOICE_CALL_ASSISTANT, MAX_VOLUME_LEVEL);
1820     volumeDataMaintainer_.SetStreamVolume(STREAM_ULTRASONIC, MAX_VOLUME_LEVEL);
1821 }
1822 
UpdateVolumeMapIndex()1823 void AudioAdapterManager::UpdateVolumeMapIndex()
1824 {
1825     for (auto streamVolInfoPair : streamVolumeInfos_) {
1826         auto streamVolInfo = streamVolInfoPair.second;
1827         minVolumeIndexMap_[streamVolInfo->streamType] = streamVolInfo->minLevel;
1828         maxVolumeIndexMap_[streamVolInfo->streamType] = streamVolInfo->maxLevel;
1829         volumeDataMaintainer_.SetStreamVolume(streamVolInfo->streamType, streamVolInfo->defaultLevel);
1830         AUDIO_DEBUG_LOG("update streamType %{public}d index = [%{public}d, %{public}d, %{public}d]",
1831             streamVolInfo->streamType, minVolumeIndexMap_[streamVolInfo->streamType],
1832             maxVolumeIndexMap_[streamVolInfo->streamType],
1833             volumeDataMaintainer_.GetStreamVolume(streamVolInfo->streamType));
1834     }
1835 }
1836 
GetVolumePoints(AudioVolumeType streamType,DeviceVolumeType deviceType,std::vector<VolumePoint> & volumePoints)1837 void AudioAdapterManager::GetVolumePoints(AudioVolumeType streamType, DeviceVolumeType deviceType,
1838     std::vector<VolumePoint> &volumePoints)
1839 {
1840     auto streamVolInfo = streamVolumeInfos_.find(streamType);
1841     auto deviceVolInfo = streamVolInfo->second->deviceVolumeInfos.find(deviceType);
1842     volumePoints = deviceVolInfo->second->volumePoints;
1843 }
1844 
GetStreamVolumeInfoMap(StreamVolumeInfoMap & streamVolumeInfos)1845 void AudioAdapterManager::GetStreamVolumeInfoMap(StreamVolumeInfoMap &streamVolumeInfos)
1846 {
1847     streamVolumeInfos = streamVolumeInfos_;
1848 }
1849 
SetActiveDevice(DeviceType deviceType)1850 void AudioAdapterManager::SetActiveDevice(DeviceType deviceType)
1851 {
1852     currentActiveDevice_ = deviceType;
1853 }
1854 
GetActiveDevice()1855 DeviceType AudioAdapterManager::GetActiveDevice()
1856 {
1857     return currentActiveDevice_;
1858 }
1859 
SetAbsVolumeScene(bool isAbsVolumeScene)1860 void AudioAdapterManager::SetAbsVolumeScene(bool isAbsVolumeScene)
1861 {
1862     isAbsVolumeScene_ = isAbsVolumeScene;
1863 }
1864 
IsAbsVolumeScene() const1865 bool AudioAdapterManager::IsAbsVolumeScene() const
1866 {
1867     return isAbsVolumeScene_;
1868 }
1869 
SetAbsVolumeMute(bool mute)1870 void AudioAdapterManager::SetAbsVolumeMute(bool mute)
1871 {
1872     isAbsVolumeMute_ = mute;
1873     float volumeDb = mute ? 0.0f : 0.63957f; // 0.63957 = -4dB
1874     SetVolumeDbForVolumeTypeGroup(MEDIA_VOLUME_TYPE_LIST, volumeDb);
1875 }
1876 
1877 
IsAbsVolumeMute() const1878 bool AudioAdapterManager::IsAbsVolumeMute() const
1879 {
1880     return isAbsVolumeMute_;
1881 }
1882 
NotifyAccountsChanged(const int & id)1883 void AudioAdapterManager::NotifyAccountsChanged(const int &id)
1884 {
1885     AUDIO_INFO_LOG("start reload the kv data, current id:%{public}d", id);
1886     LoadVolumeMap();
1887     LoadMuteStatusMap();
1888 }
1889 
DoRestoreData()1890 int32_t AudioAdapterManager::DoRestoreData()
1891 {
1892     isLoaded_ = false;
1893     isNeedConvertSafeTime_ = true; // reset convert safe volume status
1894     volumeDataMaintainer_.SaveMuteTransferStatus(true); // reset mute convert status
1895     InitKVStore();
1896     return SUCCESS;
1897 }
1898 
GetSafeVolumeLevel() const1899 int32_t AudioAdapterManager::GetSafeVolumeLevel() const
1900 {
1901     return safeVolume_;
1902 }
1903 
GetSafeVolumeTimeout() const1904 int32_t AudioAdapterManager::GetSafeVolumeTimeout() const
1905 {
1906     if (safeVolumeTimeout_ <= 0) {
1907         AUDIO_INFO_LOG("safeVolumeTimeout is invalid, return default value:%{public}d", DEFAULT_SAFE_VOLUME_TIMEOUT);
1908         return DEFAULT_SAFE_VOLUME_TIMEOUT;
1909     }
1910     return safeVolumeTimeout_;
1911 }
1912 
SafeVolumeDump(std::string & dumpString)1913 void AudioAdapterManager::SafeVolumeDump(std::string &dumpString)
1914 {
1915     dumpString += "SafeVolume info:\n";
1916     for (auto &streamType : VOLUME_TYPE_LIST) {
1917         AppendFormat(dumpString, "  - samplingAudioStreamTypeate: %d", streamType);
1918         AppendFormat(dumpString, "   volumeLevel: %d\n", volumeDataMaintainer_.GetStreamVolume(streamType));
1919         AppendFormat(dumpString, "  - AudioStreamType: %d", streamType);
1920         AppendFormat(dumpString, "   streamMuteStatus: %d\n", volumeDataMaintainer_.GetStreamMute(streamType));
1921     }
1922     std::string statusBt = (safeStatusBt_ == SAFE_ACTIVE) ? "SAFE_ACTIVE" : "SAFE_INACTIVE";
1923     std::string status = (safeStatus_ == SAFE_ACTIVE) ? "SAFE_ACTIVE" : "SAFE_INACTIVE";
1924     AppendFormat(dumpString, "  - ringerMode: %d\n", ringerMode_);
1925     AppendFormat(dumpString, "  - SafeVolume: %d\n", safeVolume_);
1926     AppendFormat(dumpString, "  - BtSafeStatus: %s\n", statusBt.c_str());
1927     AppendFormat(dumpString, "  - SafeStatus: %s\n", status.c_str());
1928     AppendFormat(dumpString, "  - ActiveBtSafeTime: %lld\n", safeActiveBtTime_);
1929     AppendFormat(dumpString, "  - ActiveSafeTime: %lld\n", safeActiveTime_);
1930 }
1931 
SetVgsVolumeSupported(bool isVgsSupported)1932 void AudioAdapterManager::SetVgsVolumeSupported(bool isVgsSupported)
1933 {
1934     isVgsVolumeSupported_ = isVgsSupported;
1935 }
1936 
IsVgsVolumeSupported() const1937 bool AudioAdapterManager::IsVgsVolumeSupported() const
1938 {
1939     return isVgsVolumeSupported_;
1940 }
1941 // LCOV_EXCL_STOP
1942 } // namespace AudioStandard
1943 } // namespace OHOS
1944