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 22 #include "audio_service_adapter.h" 23 #include "distributed_kv_data_manager.h" 24 #include "iaudio_policy_interface.h" 25 #include "types.h" 26 #include "audio_log.h" 27 #include "audio_volume_config.h" 28 29 namespace OHOS { 30 namespace AudioStandard { 31 using namespace OHOS::DistributedKv; 32 33 class AudioAdapterManager : public IAudioPolicyInterface { 34 public: 35 static constexpr std::string_view HDI_SINK = "libmodule-hdi-sink.z.so"; 36 static constexpr std::string_view HDI_SOURCE = "libmodule-hdi-source.z.so"; 37 static constexpr std::string_view PIPE_SINK = "libmodule-pipe-sink.z.so"; 38 static constexpr std::string_view PIPE_SOURCE = "libmodule-pipe-source.z.so"; 39 static constexpr std::string_view CLUSTER_SINK = "libmodule-cluster-sink.z.so"; 40 static constexpr std::string_view EFFECT_SINK = "libmodule-effect-sink.z.so"; 41 static constexpr std::string_view INNER_CAPTURER_SINK = "libmodule-inner-capturer-sink.z.so"; 42 static constexpr std::string_view RECEIVER_SINK = "libmodule-receiver-sink.z.so"; 43 static constexpr uint32_t KVSTORE_CONNECT_RETRY_COUNT = 5; 44 static constexpr uint32_t KVSTORE_CONNECT_RETRY_DELAY_TIME = 200000; 45 static constexpr float MIN_VOLUME = 0.0f; 46 static constexpr uint32_t NUMBER_TWO = 2; 47 bool Init(); 48 void Deinit(void); 49 void InitKVStore(); 50 bool ConnectServiceAdapter(); 51 GetInstance()52 static IAudioPolicyInterface& GetInstance() 53 { 54 static AudioAdapterManager audioAdapterManager; 55 return audioAdapterManager; 56 } 57 ~AudioAdapterManager()58 virtual ~AudioAdapterManager() {} 59 60 int32_t GetMaxVolumeLevel(AudioVolumeType volumeType); 61 62 int32_t GetMinVolumeLevel(AudioVolumeType volumeType); 63 64 int32_t SetSystemVolumeLevel(AudioStreamType streamType, int32_t volumeLevel, bool isFromVolumeKey = false); 65 66 int32_t GetSystemVolumeLevel(AudioStreamType streamType, bool isFromVolumeKey = false); 67 68 float GetSystemVolumeDb(AudioStreamType streamType); 69 70 int32_t SetStreamMute(AudioStreamType streamType, bool mute); 71 72 int32_t SetSourceOutputStreamMute(int32_t uid, bool setMute); 73 74 bool GetStreamMute(AudioStreamType streamType); 75 76 bool IsStreamActive(AudioStreamType streamType); 77 78 std::vector<SinkInfo> GetAllSinks(); 79 80 std::vector<SinkInput> GetAllSinkInputs(); 81 82 std::vector<SourceOutput> GetAllSourceOutputs(); 83 84 AudioIOHandle OpenAudioPort(const AudioModuleInfo &audioModuleInfo); 85 86 AudioIOHandle LoadLoopback(const LoopbackModuleInfo &moduleInfo); 87 88 int32_t CloseAudioPort(AudioIOHandle ioHandle); 89 90 int32_t SelectDevice(DeviceRole deviceRole, InternalDeviceType deviceType, std::string name); 91 92 int32_t SetDeviceActive(AudioIOHandle ioHandle, InternalDeviceType deviceType, std::string name, bool active); 93 94 void SetVolumeForSwitchDevice(InternalDeviceType deviceType); 95 96 int32_t MoveSinkInputByIndexOrName(uint32_t sinkInputId, uint32_t sinkIndex, std::string sinkName); 97 98 int32_t MoveSourceOutputByIndexOrName(uint32_t sourceOutputId, uint32_t sourceIndex, std::string sourceName); 99 100 int32_t SetRingerMode(AudioRingerMode ringerMode); 101 102 AudioRingerMode GetRingerMode(void) const; 103 104 int32_t SetAudioSessionCallback(AudioSessionCallback *callback); 105 106 int32_t SuspendAudioDevice(std::string &name, bool isSuspend); 107 108 bool SetSinkMute(const std::string &sinkName, bool isMute); 109 110 float CalculateVolumeDb(int32_t volumeLevel); 111 112 int32_t SetSystemSoundUri(const std::string &key, const std::string &uri); 113 114 std::string GetSystemSoundUri(const std::string &key); 115 116 float GetMinStreamVolume(void) const; 117 118 float GetMaxStreamVolume(void) const; 119 120 int32_t UpdateSwapDeviceStatus(); 121 122 bool IsVolumeUnadjustable(); 123 124 float CalculateVolumeDbNonlinear(AudioStreamType streamType, DeviceType deviceType, int32_t volumeLevel); 125 126 void GetStreamVolumeInfoMap(StreamVolumeInfoMap &streamVolumeInfos); 127 128 DeviceVolumeType GetDeviceCategory(DeviceType deviceType); 129 130 DeviceType GetActiveDevice(); 131 132 float GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType); 133 IsUseNonlinearAlgo()134 bool IsUseNonlinearAlgo() { return useNonlinearAlgo_; } 135 private: 136 friend class PolicyCallbackImpl; 137 138 static constexpr int32_t MAX_VOLUME_LEVEL = 15; 139 static constexpr int32_t MIN_VOLUME_LEVEL = 0; 140 static constexpr int32_t DEFAULT_VOLUME_LEVEL = 7; 141 static constexpr int32_t CONST_FACTOR = 100; 142 static constexpr float MIN_STREAM_VOLUME = 0.0f; 143 static constexpr float MAX_STREAM_VOLUME = 1.0f; 144 145 struct UserData { 146 AudioAdapterManager *thiz; 147 AudioStreamType streamType; 148 float volume; 149 bool mute; 150 bool isCorked; 151 uint32_t idx; 152 }; 153 AudioAdapterManager()154 AudioAdapterManager() 155 : ringerMode_(RINGER_MODE_NORMAL), 156 audioPolicyKvStore_(nullptr) 157 { 158 InitVolumeMapIndex(); 159 } 160 161 std::string GetModuleArgs(const AudioModuleInfo &audioModuleInfo) const; 162 std::string GetLoopbackModuleArgs(const LoopbackModuleInfo &moduleInfo) const; 163 AudioStreamType GetStreamIDByType(std::string streamType); 164 AudioStreamType GetStreamForVolumeMap(AudioStreamType streamType); 165 bool InitAudioPolicyKvStore(bool& isFirstBoot); 166 void InitVolumeMap(bool isFirstBoot); 167 bool LoadVolumeMap(void); 168 void WriteVolumeToKvStore(DeviceType type, AudioStreamType streamType, int32_t volumeLevel); 169 bool LoadVolumeFromKvStore(DeviceType type, AudioStreamType streamType); 170 std::string GetVolumeKeyForKvStore(DeviceType deviceType, AudioStreamType streamType); 171 void InitRingerMode(bool isFirstBoot); 172 bool LoadRingerMode(void); 173 void WriteRingerModeToKvStore(AudioRingerMode ringerMode); 174 void InitMuteStatusMap(bool isFirstBoot); 175 bool LoadMuteStatusMap(void); 176 bool LoadMuteStatusFromKvStore(DeviceType deviceType, AudioStreamType streamType); 177 void WriteMuteStatusToKvStore(DeviceType deviceType, AudioStreamType streamType, bool muteStatus); 178 std::string GetMuteKeyForKvStore(DeviceType deviceType, AudioStreamType streamType); 179 int32_t WriteSystemSoundUriToKvStore(const std::string &key, const std::string &uri); 180 std::string LoadSystemSoundUriFromKvStore(const std::string &key); 181 void InitVolumeMapIndex(); 182 void UpdateVolumeMapIndex(); 183 void GetVolumePoints(AudioVolumeType streamType, DeviceVolumeType deviceType, 184 std::vector<VolumePoint> &volumePoints); 185 uint32_t GetPositionInVolumePoints(std::vector<VolumePoint> &volumePoints, int32_t idx); 186 void SaveMediaVolumeToLocal(AudioStreamType streamType, int32_t volumeLevel); 187 void UpdateRingerModeForVolume(AudioStreamType streamType, int32_t volumeLevel); 188 void UpdateMuteStatusForVolume(AudioStreamType streamType, int32_t volumeLevel); 189 int32_t SetVolumeDb(AudioStreamType streamType); 190 int32_t SetVolumeDbForVolumeTypeGroup(const std::vector<AudioStreamType> &volumeTypeGroup, float volumeDb); 191 bool IsStreamActiveForVolumeTypeGroup(const std::vector<AudioStreamType> &volumeTypeGroup); 192 193 template<typename T> TransferTypeToByteArray(const T & t)194 std::vector<uint8_t> TransferTypeToByteArray(const T &t) 195 { 196 return std::vector<uint8_t>(reinterpret_cast<uint8_t *>(const_cast<T *>(&t)), 197 reinterpret_cast<uint8_t *>(const_cast<T *>(&t)) + sizeof(T)); 198 } 199 200 template<typename T> TransferByteArrayToType(const std::vector<uint8_t> & data)201 T TransferByteArrayToType(const std::vector<uint8_t> &data) 202 { 203 if (data.size() != sizeof(T) || data.size() == 0) { 204 constexpr int tSize = sizeof(T); 205 uint8_t tContent[tSize] = { 0 }; 206 return *reinterpret_cast<T *>(tContent); 207 } 208 return *reinterpret_cast<T *>(const_cast<uint8_t *>(&data[0])); 209 } 210 211 std::unique_ptr<AudioServiceAdapter> audioServiceAdapter_; 212 std::unordered_map<AudioStreamType, int32_t> volumeLevelMap_; 213 std::unordered_map<AudioStreamType, int> minVolumeIndexMap_; 214 std::unordered_map<AudioStreamType, int> maxVolumeIndexMap_; 215 StreamVolumeInfoMap streamVolumeInfos_; 216 std::unordered_map<AudioStreamType, bool> muteStatusMap_; 217 DeviceType currentActiveDevice_ = DeviceType::DEVICE_TYPE_SPEAKER; 218 AudioRingerMode ringerMode_; 219 std::shared_ptr<SingleKvStore> audioPolicyKvStore_; 220 AudioSessionCallback *sessionCallback_; 221 bool isVolumeUnadjustable_; 222 bool testModeOn_ {false}; 223 float getSystemVolumeInDb_; 224 bool useNonlinearAlgo_; 225 }; 226 227 class PolicyCallbackImpl : public AudioServiceAdapterCallback { 228 public: PolicyCallbackImpl(std::shared_ptr<AudioAdapterManager> audioAdapterManager)229 explicit PolicyCallbackImpl(std::shared_ptr<AudioAdapterManager> audioAdapterManager) 230 { 231 audioAdapterManager_ = audioAdapterManager; 232 } 233 ~PolicyCallbackImpl()234 ~PolicyCallbackImpl() 235 { 236 audioAdapterManager_ = nullptr; 237 } 238 OnGetVolumeDbCb(AudioStreamType streamType)239 float OnGetVolumeDbCb(AudioStreamType streamType) 240 { 241 AudioStreamType streamForVolumeMap = audioAdapterManager_->GetStreamForVolumeMap(streamType); 242 243 bool muteStatus = audioAdapterManager_->muteStatusMap_[streamForVolumeMap]; 244 if (muteStatus) { 245 return 0.0f; 246 } 247 248 int32_t volumeLevel = audioAdapterManager_->volumeLevelMap_[streamForVolumeMap]; 249 float volumeDb = 1.0f; 250 if (audioAdapterManager_->IsUseNonlinearAlgo()) { 251 volumeDb = audioAdapterManager_->CalculateVolumeDbNonlinear(streamForVolumeMap, 252 audioAdapterManager_->GetActiveDevice(), volumeLevel); 253 } else { 254 volumeDb = audioAdapterManager_->CalculateVolumeDb(volumeLevel); 255 } 256 return volumeDb; 257 } 258 OnSessionRemoved(const uint32_t sessionID)259 void OnSessionRemoved(const uint32_t sessionID) 260 { 261 AUDIO_DEBUG_LOG("PolicyCallbackImpl OnSessionRemoved: Session ID %{public}d", sessionID); 262 if (audioAdapterManager_->sessionCallback_ == nullptr) { 263 AUDIO_DEBUG_LOG("PolicyCallbackImpl audioAdapterManager_->sessionCallback_ == nullptr" 264 "not firing OnSessionRemoved"); 265 } else { 266 audioAdapterManager_->sessionCallback_->OnSessionRemoved(sessionID); 267 } 268 } 269 OnPlaybackCapturerStop()270 void OnPlaybackCapturerStop() 271 { 272 AUDIO_INFO_LOG("PolicyCallbackImpl OnPlaybackCapturerStop"); 273 if (audioAdapterManager_->sessionCallback_ == nullptr) { 274 AUDIO_DEBUG_LOG("PolicyCallbackImpl sessionCallback_ nullptr"); 275 } else { 276 audioAdapterManager_->sessionCallback_->OnPlaybackCapturerStop(); 277 } 278 } 279 OnWakeupCapturerStop()280 void OnWakeupCapturerStop() 281 { 282 AUDIO_INFO_LOG("PolicyCallbackImpl OnWakeupCapturerStop"); 283 if (audioAdapterManager_->sessionCallback_ == nullptr) { 284 AUDIO_DEBUG_LOG("PolicyCallbackImpl sessionCallback_ nullptr"); 285 } else { 286 audioAdapterManager_->sessionCallback_->OnWakeupCapturerStop(); 287 } 288 } 289 private: 290 std::shared_ptr<AudioAdapterManager> audioAdapterManager_; 291 }; 292 } // namespace AudioStandard 293 } // namespace OHOS 294 #endif // ST_PULSEAUDIO_ADAPTER_MANAGER_H 295