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 28 namespace OHOS { 29 namespace AudioStandard { 30 using namespace OHOS::DistributedKv; 31 32 class AudioAdapterManager : public IAudioPolicyInterface { 33 public: 34 static constexpr std::string_view HDI_SINK = "libmodule-hdi-sink.z.so"; 35 static constexpr std::string_view HDI_SOURCE = "libmodule-hdi-source.z.so"; 36 static constexpr std::string_view PIPE_SINK = "libmodule-pipe-sink.z.so"; 37 static constexpr std::string_view PIPE_SOURCE = "libmodule-pipe-source.z.so"; 38 static constexpr uint32_t KVSTORE_CONNECT_RETRY_COUNT = 5; 39 static constexpr uint32_t KVSTORE_CONNECT_RETRY_DELAY_TIME = 200000; 40 static constexpr float MAX_VOLUME = 1.0f; 41 static constexpr float MIN_VOLUME = 0.0f; 42 43 bool Init(); 44 void Deinit(void); 45 void InitKVStore(); 46 bool ConnectServiceAdapter(); 47 48 std::string GetPolicyManagerName(); 49 GetInstance()50 static IAudioPolicyInterface& GetInstance() 51 { 52 static AudioAdapterManager audioAdapterManager; 53 return audioAdapterManager; 54 } 55 56 int32_t SetStreamVolume(AudioStreamType streamType, float volume); 57 58 float GetStreamVolume(AudioStreamType streamType); 59 60 int32_t SetStreamMute(AudioStreamType streamType, bool mute); 61 62 int32_t SetSourceOutputStreamMute(int32_t uid, bool setMute); 63 64 bool GetStreamMute(AudioStreamType streamType); 65 66 bool IsStreamActive(AudioStreamType streamType); 67 68 std::vector<SinkInput> GetAllSinkInputs(); 69 70 std::vector<SourceOutput> GetAllSourceOutputs(); 71 72 AudioIOHandle OpenAudioPort(const AudioModuleInfo &audioModuleInfo); 73 74 int32_t CloseAudioPort(AudioIOHandle ioHandle); 75 76 int32_t SelectDevice(DeviceRole deviceRole, InternalDeviceType deviceType, std::string name); 77 78 int32_t SetDeviceActive(AudioIOHandle ioHandle, InternalDeviceType deviceType, std::string name, bool active); 79 80 void SetVolumeForSwitchDevice(InternalDeviceType deviceType); 81 82 int32_t MoveSinkInputByIndexOrName(uint32_t sinkInputId, uint32_t sinkIndex, std::string sinkName); 83 84 int32_t MoveSourceOutputByIndexOrName(uint32_t sourceOutputId, uint32_t sourceIndex, std::string sourceName); 85 86 int32_t SetRingerMode(AudioRingerMode ringerMode); 87 88 AudioRingerMode GetRingerMode(void) const; 89 90 int32_t SetAudioSessionCallback(AudioSessionCallback *callback); 91 92 int32_t SuspendAudioDevice(std::string &name, bool isSuspend); 93 ~AudioAdapterManager()94 virtual ~AudioAdapterManager() {} 95 96 private: 97 struct UserData { 98 AudioAdapterManager *thiz; 99 AudioStreamType streamType; 100 float volume; 101 bool mute; 102 bool isCorked; 103 uint32_t idx; 104 }; 105 AudioAdapterManager()106 AudioAdapterManager() 107 : mRingerMode(RINGER_MODE_NORMAL), 108 mAudioPolicyKvStore(nullptr) 109 { 110 mVolumeMap[STREAM_MUSIC] = MAX_VOLUME; 111 mVolumeMap[STREAM_RING] = MAX_VOLUME; 112 mVolumeMap[STREAM_VOICE_CALL] = MAX_VOLUME; 113 mVolumeMap[STREAM_VOICE_ASSISTANT] = MAX_VOLUME; 114 } 115 116 bool ConnectToPulseAudio(void); 117 std::string GetModuleArgs(const AudioModuleInfo &audioModuleInfo) const; 118 std::string GetStreamNameByStreamType(DeviceType deviceType, AudioStreamType streamType); 119 AudioStreamType GetStreamIDByType(std::string streamType); 120 AudioStreamType GetStreamForVolumeMap(AudioStreamType streamType); 121 bool InitAudioPolicyKvStore(bool& isFirstBoot); 122 void InitVolumeMap(bool isFirstBoot); 123 bool LoadVolumeMap(void); 124 void WriteVolumeToKvStore(DeviceType type, AudioStreamType streamType, float volume); 125 bool LoadVolumeFromKvStore(DeviceType type, AudioStreamType streamType); 126 void InitRingerMode(bool isFirstBoot); 127 bool LoadRingerMode(void); 128 void WriteRingerModeToKvStore(AudioRingerMode ringerMode); 129 void InitMuteStatusMap(bool isFirstBoot); 130 bool LoadMuteStatusMap(void); 131 bool LoadMuteStatusFromKvStore(AudioStreamType streamType); 132 void WriteMuteStatusToKvStore(DeviceType deviceType, AudioStreamType streamType, bool muteStatus); 133 std::string GetStreamTypeKeyForMute(DeviceType deviceType, AudioStreamType streamType); 134 std::unique_ptr<AudioServiceAdapter> mAudioServiceAdapter; 135 std::unordered_map<AudioStreamType, float> mVolumeMap; 136 std::unordered_map<AudioStreamType, int> mMuteStatusMap; 137 DeviceType currentActiveDevice_ = DeviceType::DEVICE_TYPE_SPEAKER; 138 AudioRingerMode mRingerMode; 139 std::shared_ptr<SingleKvStore> mAudioPolicyKvStore; 140 141 AudioSessionCallback *sessionCallback_; 142 friend class PolicyCallbackImpl; 143 bool testModeOn_ {false}; 144 145 std::vector<DeviceType> deviceList_ = { 146 DEVICE_TYPE_SPEAKER, 147 DEVICE_TYPE_USB_HEADSET, 148 DEVICE_TYPE_BLUETOOTH_A2DP, 149 DEVICE_TYPE_WIRED_HEADSET 150 }; 151 }; 152 153 class PolicyCallbackImpl : public AudioServiceAdapterCallback { 154 public: PolicyCallbackImpl(std::shared_ptr<AudioAdapterManager> audioAdapterManager)155 explicit PolicyCallbackImpl(std::shared_ptr<AudioAdapterManager> audioAdapterManager) 156 { 157 audioAdapterManager_ = audioAdapterManager; 158 } 159 ~PolicyCallbackImpl()160 ~PolicyCallbackImpl() 161 { 162 audioAdapterManager_ = nullptr; 163 } 164 OnGetVolumeCb(std::string streamType)165 float OnGetVolumeCb(std::string streamType) 166 { 167 AudioStreamType streamForVolumeMap = audioAdapterManager_->GetStreamForVolumeMap( 168 audioAdapterManager_->GetStreamIDByType(streamType)); 169 if (audioAdapterManager_->mRingerMode != RINGER_MODE_NORMAL) { 170 if (streamForVolumeMap == STREAM_RING) { 171 return AudioAdapterManager::MIN_VOLUME; 172 } 173 } 174 175 if (audioAdapterManager_->GetStreamMute(streamForVolumeMap)) { 176 return AudioAdapterManager::MIN_VOLUME; 177 } 178 return audioAdapterManager_->mVolumeMap[streamForVolumeMap]; 179 } 180 OnSessionRemoved(const uint32_t sessionID)181 void OnSessionRemoved(const uint32_t sessionID) 182 { 183 AUDIO_DEBUG_LOG("AudioAdapterManager: PolicyCallbackImpl OnSessionRemoved: Session ID %{public}d", sessionID); 184 if (audioAdapterManager_->sessionCallback_ == nullptr) { 185 AUDIO_DEBUG_LOG("AudioAdapterManager: PolicyCallbackImpl audioAdapterManager_->sessionCallback_ == nullptr" 186 "not firing OnSessionRemoved"); 187 } else { 188 audioAdapterManager_->sessionCallback_->OnSessionRemoved(sessionID); 189 } 190 } 191 private: 192 std::shared_ptr<AudioAdapterManager> audioAdapterManager_; 193 }; 194 } // namespace AudioStandard 195 } // namespace OHOS 196 #endif // ST_PULSEAUDIO_ADAPTER_MANAGER_H 197