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 16 #ifndef ST_AUDIO_ADAPTER_MANAGER_H 17 #define ST_AUDIO_ADAPTER_MANAGER_H 18 19 #include <list> 20 #include <unordered_map> 21 #include <cinttypes> 22 23 #include "audio_adapter_manager_handler.h" 24 #include "audio_service_adapter.h" 25 #include "distributed_kv_data_manager.h" 26 #include "iaudio_policy_interface.h" 27 #include "types.h" 28 #include "audio_policy_log.h" 29 #include "audio_volume_config.h" 30 #include "audio_policy_server_handler.h" 31 #include "volume_data_maintainer.h" 32 #include "audio_utils.h" 33 34 namespace OHOS { 35 namespace AudioStandard { 36 using namespace OHOS::DistributedKv; 37 38 class AudioOsAccountInfo; 39 40 class AudioAdapterManager : public IAudioPolicyInterface { 41 public: 42 static constexpr std::string_view SPLIT_STREAM_SINK = "libmodule-split-stream-sink.z.so"; 43 static constexpr std::string_view HDI_SINK = "libmodule-hdi-sink.z.so"; 44 static constexpr std::string_view HDI_SOURCE = "libmodule-hdi-source.z.so"; 45 static constexpr std::string_view PIPE_SINK = "libmodule-pipe-sink.z.so"; 46 static constexpr std::string_view PIPE_SOURCE = "libmodule-pipe-source.z.so"; 47 static constexpr std::string_view CLUSTER_SINK = "libmodule-cluster-sink.z.so"; 48 static constexpr std::string_view EFFECT_SINK = "libmodule-effect-sink.z.so"; 49 static constexpr std::string_view INNER_CAPTURER_SINK = "libmodule-inner-capturer-sink.z.so"; 50 static constexpr std::string_view RECEIVER_SINK = "libmodule-receiver-sink.z.so"; 51 static constexpr uint32_t KVSTORE_CONNECT_RETRY_COUNT = 5; 52 static constexpr uint32_t KVSTORE_CONNECT_RETRY_DELAY_TIME = 200000; 53 static constexpr float MIN_VOLUME = 0.0f; 54 static constexpr uint32_t NUMBER_TWO = 2; 55 bool Init(); 56 void Deinit(void); 57 void InitKVStore(); 58 bool ConnectServiceAdapter(); 59 GetInstance()60 static IAudioPolicyInterface& GetInstance() 61 { 62 static AudioAdapterManager audioAdapterManager; 63 return audioAdapterManager; 64 } 65 ~AudioAdapterManager()66 virtual ~AudioAdapterManager() {} 67 68 int32_t GetMaxVolumeLevel(AudioVolumeType volumeType); 69 70 int32_t GetMinVolumeLevel(AudioVolumeType volumeType); 71 72 int32_t SetSystemVolumeLevel(AudioStreamType streamType, int32_t volumeLevel); 73 74 int32_t GetSystemVolumeLevel(AudioStreamType streamType); 75 76 int32_t GetSystemVolumeLevelNoMuteState(AudioStreamType streamType); 77 78 float GetSystemVolumeDb(AudioStreamType streamType); 79 80 int32_t SetStreamMute(AudioStreamType streamType, bool mute, StreamUsage streamUsage = STREAM_USAGE_UNKNOWN); 81 82 int32_t SetSourceOutputStreamMute(int32_t uid, bool setMute); 83 84 bool GetStreamMute(AudioStreamType streamType); 85 86 std::vector<SinkInfo> GetAllSinks(); 87 88 std::vector<SinkInput> GetAllSinkInputs(); 89 90 std::vector<SourceOutput> GetAllSourceOutputs(); 91 92 AudioIOHandle OpenAudioPort(const AudioModuleInfo &audioModuleInfo); 93 94 int32_t CloseAudioPort(AudioIOHandle ioHandle); 95 96 int32_t SelectDevice(DeviceRole deviceRole, InternalDeviceType deviceType, std::string name); 97 98 int32_t SetDeviceActive(InternalDeviceType deviceType, std::string name, bool active, 99 DeviceFlag flag = ALL_DEVICES_FLAG); 100 101 void SetVolumeForSwitchDevice(InternalDeviceType deviceType); 102 103 int32_t MoveSinkInputByIndexOrName(uint32_t sinkInputId, uint32_t sinkIndex, std::string sinkName); 104 105 int32_t MoveSourceOutputByIndexOrName(uint32_t sourceOutputId, uint32_t sourceIndex, std::string sourceName); 106 107 int32_t SetRingerMode(AudioRingerMode ringerMode); 108 109 AudioRingerMode GetRingerMode(void) const; 110 111 int32_t SetAudioStreamRemovedCallback(AudioStreamRemovedCallback *callback); 112 113 int32_t SuspendAudioDevice(std::string &name, bool isSuspend); 114 115 bool SetSinkMute(const std::string &sinkName, bool isMute, bool isSync = false); 116 117 float CalculateVolumeDb(int32_t volumeLevel); 118 119 int32_t SetSystemSoundUri(const std::string &key, const std::string &uri); 120 121 std::string GetSystemSoundUri(const std::string &key); 122 123 float GetMinStreamVolume(void) const; 124 125 float GetMaxStreamVolume(void) const; 126 127 bool IsVolumeUnadjustable(); 128 129 float CalculateVolumeDbNonlinear(AudioStreamType streamType, DeviceType deviceType, int32_t volumeLevel); 130 131 void GetStreamVolumeInfoMap(StreamVolumeInfoMap &streamVolumeInfos); 132 133 DeviceVolumeType GetDeviceCategory(DeviceType deviceType); 134 135 void SetActiveDevice(DeviceType deviceType); 136 137 DeviceType GetActiveDevice(); 138 139 float GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType); 140 IsUseNonlinearAlgo()141 bool IsUseNonlinearAlgo() { return useNonlinearAlgo_; } 142 143 void SetAbsVolumeScene(bool isAbsVolumeScene); 144 145 bool IsAbsVolumeScene() const; 146 147 void SetAbsVolumeMute(bool mute); 148 149 void SetDataShareReady(std::atomic<bool> isDataShareReady); 150 151 bool IsAbsVolumeMute() const; 152 153 void SetVgsVolumeSupported(bool isVgsSupported); 154 155 bool IsVgsVolumeSupported() const; 156 157 std::string GetModuleArgs(const AudioModuleInfo &audioModuleInfo) const; 158 159 void ResetRemoteCastDeviceVolume(); 160 161 int32_t GetStreamVolume(AudioStreamType streamType); 162 163 void NotifyAccountsChanged(const int &id); 164 165 void SafeVolumeDump(std::string &dumpString); 166 167 int32_t DoRestoreData(); 168 SafeStatus GetCurrentDeviceSafeStatus(DeviceType deviceType); 169 170 int64_t GetCurentDeviceSafeTime(DeviceType deviceType); 171 172 int32_t SetDeviceSafeStatus(DeviceType deviceType, SafeStatus status); 173 174 int32_t SetDeviceSafeTime(DeviceType deviceType, int64_t time); 175 176 int32_t GetSafeVolumeLevel() const; 177 178 int32_t GetSafeVolumeTimeout() const; 179 180 int32_t GetCurActivateCount(void) const; 181 182 void HandleKvData(bool isFirstBoot); 183 184 int32_t SetPersistMicMuteState(const bool isMute); 185 186 int32_t GetPersistMicMuteState(bool &isMute) const; 187 188 void HandleSaveVolume(DeviceType deviceType, AudioStreamType streamType, int32_t volumeLevel); 189 190 void HandleStreamMuteStatus(AudioStreamType streamType, bool mute, StreamUsage streamUsage = STREAM_USAGE_UNKNOWN); 191 192 void HandleRingerMode(AudioRingerMode ringerMode); 193 194 void SetAudioServerProxy(sptr<IStandardAudioService> gsp); 195 196 void SetOffloadSessionId(uint32_t sessionId); 197 198 void ResetOffloadSessionId(); 199 200 int32_t SetDoubleRingVolumeDb(const AudioStreamType &streamType, const int32_t &volumeLevel); 201 private: 202 friend class PolicyCallbackImpl; 203 204 static constexpr int32_t MAX_VOLUME_LEVEL = 15; 205 static constexpr int32_t MIN_VOLUME_LEVEL = 0; 206 static constexpr int32_t DEFAULT_VOLUME_LEVEL = 7; 207 static constexpr int32_t CONST_FACTOR = 100; 208 static constexpr int32_t DEFAULT_SAFE_VOLUME_TIMEOUT = 1140; 209 static constexpr int32_t CONVERT_FROM_MS_TO_SECONDS = 1000; 210 static constexpr float MIN_STREAM_VOLUME = 0.0f; 211 static constexpr float MAX_STREAM_VOLUME = 1.0f; 212 213 struct UserData { 214 AudioAdapterManager *thiz; 215 AudioStreamType streamType; 216 float volume; 217 bool mute; 218 bool isCorked; 219 uint32_t idx; 220 }; 221 AudioAdapterManager()222 AudioAdapterManager() 223 : ringerMode_(RINGER_MODE_NORMAL), 224 audioPolicyKvStore_(nullptr), 225 audioPolicyServerHandler_(DelayedSingleton<AudioPolicyServerHandler>::GetInstance()), 226 volumeDataMaintainer_(VolumeDataMaintainer::GetVolumeDataMaintainer()) 227 { 228 InitVolumeMapIndex(); 229 } 230 231 AudioStreamType GetStreamIDByType(std::string streamType); 232 int32_t ReInitKVStore(); 233 bool InitAudioPolicyKvStore(bool& isFirstBoot); 234 void InitVolumeMap(bool isFirstBoot); 235 bool LoadVolumeMap(void); 236 std::string GetVolumeKeyForKvStore(DeviceType deviceType, AudioStreamType streamType); 237 void InitRingerMode(bool isFirstBoot); 238 void InitMuteStatusMap(bool isFirstBoot); 239 bool LoadMuteStatusMap(void); 240 std::string GetMuteKeyForKvStore(DeviceType deviceType, AudioStreamType streamType); 241 void InitSystemSoundUriMap(); 242 void InitVolumeMapIndex(); 243 void UpdateVolumeMapIndex(); 244 void GetVolumePoints(AudioVolumeType streamType, DeviceVolumeType deviceType, 245 std::vector<VolumePoint> &volumePoints); 246 uint32_t GetPositionInVolumePoints(std::vector<VolumePoint> &volumePoints, int32_t idx); 247 void SaveRingtoneVolumeToLocal(AudioVolumeType volumeType, int32_t volumeLevel); 248 int32_t SetVolumeDb(AudioStreamType streamType); 249 void SetAudioVolume(AudioStreamType streamType, float volumeDb); 250 void SetOffloadVolume(AudioStreamType streamType, float volumeDb); 251 bool GetStreamMuteInternal(AudioStreamType streamType); 252 int32_t SetRingerModeInternal(AudioRingerMode ringerMode); 253 int32_t SetStreamMuteInternal(AudioStreamType streamType, bool mute, StreamUsage streamUsage); 254 void InitKVStoreInternal(void); 255 void DeleteAudioPolicyKvStore(); 256 void TransferMuteStatus(void); 257 void CloneMuteStatusMap(void); 258 void CloneVolumeMap(void); 259 void CloneSystemSoundUrl(void); 260 void InitSafeStatus(bool isFirstBoot); 261 void InitSafeTime(bool isFirstBoot); 262 void ConvertSafeTime(void); 263 void UpdateSafeVolume(); 264 void CheckAndDealMuteStatus(const DeviceType &deviceType, const AudioStreamType &streamType); 265 void SetVolumeCallbackAfterClone(); 266 template<typename T> TransferTypeToByteArray(const T & t)267 std::vector<uint8_t> TransferTypeToByteArray(const T &t) 268 { 269 return std::vector<uint8_t>(reinterpret_cast<uint8_t *>(const_cast<T *>(&t)), 270 reinterpret_cast<uint8_t *>(const_cast<T *>(&t)) + sizeof(T)); 271 } 272 273 template<typename T> TransferByteArrayToType(const std::vector<uint8_t> & data)274 T TransferByteArrayToType(const std::vector<uint8_t> &data) 275 { 276 if (data.size() != sizeof(T) || data.size() == 0) { 277 constexpr int tSize = sizeof(T); 278 uint8_t tContent[tSize] = { 0 }; 279 return *reinterpret_cast<T *>(tContent); 280 } 281 return *reinterpret_cast<T *>(const_cast<uint8_t *>(&data[0])); 282 } 283 284 std::unique_ptr<AudioServiceAdapter> audioServiceAdapter_; 285 std::unordered_map<AudioStreamType, int> minVolumeIndexMap_; 286 std::unordered_map<AudioStreamType, int> maxVolumeIndexMap_; 287 std::mutex systemSoundMutex_; 288 std::unordered_map<std::string, std::string> systemSoundUriMap_; 289 StreamVolumeInfoMap streamVolumeInfos_; 290 DeviceType currentActiveDevice_ = DeviceType::DEVICE_TYPE_SPEAKER; 291 AudioRingerMode ringerMode_; 292 int32_t safeVolume_ = 0; 293 SafeStatus safeStatus_ = SAFE_ACTIVE; 294 SafeStatus safeStatusBt_ = SAFE_ACTIVE; 295 int64_t safeActiveTime_ = 0; 296 int64_t safeActiveBtTime_ = 0; 297 int32_t safeVolumeTimeout_ = DEFAULT_SAFE_VOLUME_TIMEOUT; 298 bool isWiredBoot_ = true; 299 bool isBtBoot_ = true; 300 bool isBtFirstSetVolume_ = true; 301 int32_t curActiveCount_ = 0; 302 303 std::shared_ptr<AudioAdapterManagerHandler> handler_ = nullptr; 304 305 std::shared_ptr<SingleKvStore> audioPolicyKvStore_; 306 std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler_; 307 AudioStreamRemovedCallback *sessionCallback_ = nullptr; 308 VolumeDataMaintainer &volumeDataMaintainer_; 309 bool isVolumeUnadjustable_ = false; 310 bool testModeOn_ {false}; 311 float getSystemVolumeInDb_ = 0.0f; 312 bool useNonlinearAlgo_ = false; 313 bool isAbsVolumeScene_ = false; 314 bool isAbsVolumeMute_ = false; 315 bool isVgsVolumeSupported_ = false; 316 bool isNeedCopyVolumeData_ = false; 317 bool isNeedCopyMuteData_ = false; 318 bool isNeedCopyRingerModeData_ = false; 319 bool isNeedCopySystemUrlData_ = false; 320 bool isLoaded_ = false; 321 bool isAllCopyDone_ = false; 322 bool isNeedConvertSafeTime_ = false; 323 sptr<IStandardAudioService> audioServerProxy_ = nullptr; 324 std::optional<uint32_t> offloadSessionID_; 325 std::mutex audioVolumeMutex_; 326 std::mutex activeDeviceMutex_; 327 }; 328 329 class PolicyCallbackImpl : public AudioServiceAdapterCallback { 330 public: PolicyCallbackImpl(AudioAdapterManager * audioAdapterManager)331 explicit PolicyCallbackImpl(AudioAdapterManager *audioAdapterManager) 332 { 333 audioAdapterManager_ = audioAdapterManager; 334 } 335 ~PolicyCallbackImpl()336 ~PolicyCallbackImpl() 337 { 338 AUDIO_WARNING_LOG("Destructor PolicyCallbackImpl"); 339 } 340 OnAudioStreamRemoved(const uint64_t sessionID)341 void OnAudioStreamRemoved(const uint64_t sessionID) 342 { 343 AUDIO_DEBUG_LOG("PolicyCallbackImpl OnAudioStreamRemoved: Session ID %{public}" PRIu64"", sessionID); 344 if (audioAdapterManager_->sessionCallback_ == nullptr) { 345 AUDIO_DEBUG_LOG("PolicyCallbackImpl audioAdapterManager_->sessionCallback_ == nullptr" 346 "not firing OnAudioStreamRemoved"); 347 } else { 348 audioAdapterManager_->sessionCallback_->OnAudioStreamRemoved(sessionID); 349 } 350 } 351 OnSetVolumeDbCb()352 void OnSetVolumeDbCb() 353 { 354 if (!isFirstBoot_) { 355 return; 356 } 357 isFirstBoot_ = false; 358 static const std::vector<AudioVolumeType> VOLUME_TYPE_LIST = { 359 STREAM_VOICE_CALL, 360 STREAM_RING, 361 STREAM_MUSIC, 362 STREAM_VOICE_ASSISTANT, 363 STREAM_ALARM, 364 STREAM_ACCESSIBILITY, 365 STREAM_ULTRASONIC, 366 STREAM_VOICE_CALL_ASSISTANT, 367 STREAM_ALL 368 }; 369 for (auto &volumeType : VOLUME_TYPE_LIST) { 370 audioAdapterManager_->SetVolumeDb(volumeType); 371 } 372 } 373 374 private: 375 AudioAdapterManager *audioAdapterManager_; 376 bool isFirstBoot_ = true; 377 }; 378 } // namespace AudioStandard 379 } // namespace OHOS 380 #endif // ST_PULSEAUDIO_ADAPTER_MANAGER_H 381