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_POLICY_MANAGER_H 17 #define ST_AUDIO_POLICY_MANAGER_H 18 19 #include <cstdint> 20 #include "audio_capturer_state_change_listener_stub.h" 21 #include "audio_client_tracker_callback_stub.h" 22 #include "audio_effect.h" 23 #include "audio_interrupt_callback.h" 24 #include "audio_policy_manager_listener_stub.h" 25 #include "audio_renderer_state_change_listener_stub.h" 26 #include "audio_ringermode_update_listener_stub.h" 27 #include "audio_routing_manager.h" 28 #include "audio_routing_manager_listener_stub.h" 29 #include "audio_system_manager.h" 30 #include "audio_volume_key_event_callback_stub.h" 31 #include "audio_system_manager.h" 32 #include "i_audio_volume_key_event_callback.h" 33 #include "i_standard_renderer_state_change_listener.h" 34 #include "i_standard_capturer_state_change_listener.h" 35 #include "i_standard_client_tracker.h" 36 #include "audio_log.h" 37 38 namespace OHOS { 39 namespace AudioStandard { 40 using InternalDeviceType = DeviceType; 41 using InternalAudioCapturerOptions = AudioCapturerOptions; 42 43 class AudioPolicyManager { 44 public: GetInstance()45 static AudioPolicyManager& GetInstance() 46 { 47 static AudioPolicyManager policyManager; 48 return policyManager; 49 } 50 51 int32_t GetMaxVolumeLevel(AudioVolumeType volumeType); 52 53 int32_t GetMinVolumeLevel(AudioVolumeType volumeType); 54 55 int32_t SetSystemVolumeLevel(AudioStreamType streamType, int32_t volumeLevel, API_VERSION api_v = API_9); 56 57 int32_t GetSystemVolumeLevel(AudioStreamType streamType); 58 59 int32_t SetLowPowerVolume(int32_t streamId, float volume); 60 61 float GetLowPowerVolume(int32_t streamId); 62 63 float GetSingleStreamVolume(int32_t streamId); 64 65 int32_t SetStreamMute(AudioStreamType streamType, bool mute, API_VERSION api_v = API_9); 66 67 bool GetStreamMute(AudioStreamType streamType); 68 69 bool IsStreamActive(AudioStreamType streamType); 70 71 int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter, 72 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors); 73 74 std::string GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType); 75 76 int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter, 77 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors); 78 79 std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag); 80 81 int32_t SetWakeUpAudioCapturer(InternalAudioCapturerOptions options); 82 83 int32_t CloseWakeUpAudioCapturer(); 84 85 int32_t SetDeviceActive(InternalDeviceType deviceType, bool active); 86 87 bool IsDeviceActive(InternalDeviceType deviceType); 88 89 DeviceType GetActiveOutputDevice(); 90 91 DeviceType GetActiveInputDevice(); 92 93 int32_t SetRingerMode(AudioRingerMode ringMode, API_VERSION api_v = API_9); 94 95 #ifdef FEATURE_DTMF_TONE 96 std::vector<int32_t> GetSupportedTones(); 97 98 std::shared_ptr<ToneInfo> GetToneConfig(int32_t ltonetype); 99 #endif 100 101 AudioRingerMode GetRingerMode(); 102 103 int32_t SetAudioScene(AudioScene scene); 104 105 int32_t SetMicrophoneMute(bool isMute); 106 107 int32_t SetMicrophoneMuteAudioConfig(bool isMute); 108 109 bool IsMicrophoneMute(API_VERSION api_v = API_9); 110 111 AudioScene GetAudioScene(); 112 113 int32_t SetDeviceChangeCallback(const int32_t clientId, const DeviceFlag flag, 114 const std::shared_ptr<AudioManagerDeviceChangeCallback> &callback); 115 116 int32_t UnsetDeviceChangeCallback(const int32_t clientId, DeviceFlag flag); 117 118 int32_t SetRingerModeCallback(const int32_t clientId, 119 const std::shared_ptr<AudioRingerModeCallback> &callback, API_VERSION api_v = API_9); 120 121 int32_t UnsetRingerModeCallback(const int32_t clientId); 122 123 int32_t SetMicStateChangeCallback(const int32_t clientId, 124 const std::shared_ptr<AudioManagerMicStateChangeCallback> &callback); 125 126 int32_t SetAudioInterruptCallback(const uint32_t sessionID, 127 const std::shared_ptr<AudioInterruptCallback> &callback); 128 129 int32_t UnsetAudioInterruptCallback(const uint32_t sessionID); 130 131 int32_t ActivateAudioInterrupt(const AudioInterrupt &audioInterrupt); 132 133 int32_t DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt); 134 135 int32_t SetAudioManagerInterruptCallback(const int32_t clientId, 136 const std::shared_ptr<AudioInterruptCallback> &callback); 137 138 int32_t UnsetAudioManagerInterruptCallback(const int32_t clientId); 139 140 int32_t RequestAudioFocus(const int32_t clientId, const AudioInterrupt &audioInterrupt); 141 142 int32_t AbandonAudioFocus(const int32_t clientId, const AudioInterrupt &audioInterrupt); 143 144 AudioStreamType GetStreamInFocus(); 145 146 int32_t GetSessionInfoInFocus(AudioInterrupt &audioInterrupt); 147 148 int32_t SetVolumeKeyEventCallback(const int32_t clientPid, 149 const std::shared_ptr<VolumeKeyEventCallback> &callback, API_VERSION api_v = API_9); 150 151 int32_t UnsetVolumeKeyEventCallback(const int32_t clientPid); 152 153 bool CheckRecordingCreate(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid); 154 155 bool CheckRecordingStateChange(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid, 156 AudioPermissionState state); 157 158 int32_t ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType); 159 160 int32_t GetAudioLatencyFromXml(); 161 162 uint32_t GetSinkLatencyFromXml(); 163 164 int32_t RegisterAudioRendererEventListener(const int32_t clientPid, 165 const std::shared_ptr<AudioRendererStateChangeCallback> &callback); 166 167 int32_t UnregisterAudioRendererEventListener(const int32_t clientPid); 168 169 int32_t RegisterAudioCapturerEventListener(const int32_t clientPid, 170 const std::shared_ptr<AudioCapturerStateChangeCallback> &callback); 171 172 int32_t UnregisterAudioCapturerEventListener(const int32_t clientPid); 173 174 int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo, 175 const std::shared_ptr<AudioClientTracker> &clientTrackerObj); 176 177 int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo); 178 179 int32_t GetCurrentRendererChangeInfos( 180 std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos); 181 182 int32_t GetCurrentCapturerChangeInfos( 183 std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos); 184 185 int32_t UpdateStreamState(const int32_t clientUid, StreamSetState streamSetState, 186 AudioStreamType audioStreamType); 187 188 int32_t GetVolumeGroupInfos(std::string networkId, std::vector<sptr<VolumeGroupInfo>> &infos); 189 190 int32_t GetNetworkIdByGroupId(int32_t groupId, std::string &networkId); 191 192 bool IsAudioRendererLowLatencySupported(const AudioStreamInfo &audioStreamInfo); 193 194 std::vector<sptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescriptors(AudioRendererInfo &rendererInfo); 195 196 std::vector<sptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescriptors(AudioCapturerInfo &captureInfo); 197 198 int32_t SetPreferredOutputDeviceChangeCallback(const int32_t clientId, 199 const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &callback); 200 201 int32_t SetPreferredInputDeviceChangeCallback( 202 const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &callback); 203 204 int32_t UnsetPreferredOutputDeviceChangeCallback(const int32_t clientId); 205 206 int32_t UnsetPreferredInputDeviceChangeCallback(); 207 208 int32_t GetAudioFocusInfoList(std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList); 209 210 int32_t RegisterFocusInfoChangeCallback(const int32_t clientId, 211 const std::shared_ptr<AudioFocusInfoChangeCallback> &callback); 212 213 int32_t UnregisterFocusInfoChangeCallback(const int32_t clientId); 214 215 static void AudioPolicyServerDied(pid_t pid); 216 217 int32_t SetSystemSoundUri(const std::string &key, const std::string &uri); 218 219 std::string GetSystemSoundUri(const std::string &key); 220 221 float GetMinStreamVolume(void); 222 223 float GetMaxStreamVolume(void); 224 int32_t RegisterAudioPolicyServerDiedCb(const int32_t clientPid, 225 const std::weak_ptr<AudioRendererPolicyServiceDiedCallback> &callback); 226 int32_t UnregisterAudioPolicyServerDiedCb(const int32_t clientPid); 227 228 bool IsVolumeUnadjustable(); 229 230 int32_t AdjustVolumeByStep(VolumeAdjustType adjustType); 231 232 int32_t AdjustSystemVolumeByStep(AudioVolumeType volumeType, VolumeAdjustType adjustType); 233 234 float GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType); 235 236 int32_t GetMaxRendererInstances(); 237 238 int32_t QueryEffectSceneMode(SupportedEffectConfig &supportedEffectConfig); 239 240 int32_t SetPlaybackCapturerFilterInfos(const AudioPlaybackCaptureConfig &config, uint32_t appTokenId); 241 242 private: AudioPolicyManager()243 AudioPolicyManager() 244 { 245 AUDIO_INFO_LOG("Enter AudioPolicyManager::AudioPolicyManager"); 246 } ~AudioPolicyManager()247 ~AudioPolicyManager() {} 248 249 void Init(); 250 sptr<AudioPolicyManagerListenerStub> listenerStub_ = nullptr; 251 std::mutex listenerStubMutex_; 252 std::mutex volumeCallbackMutex_; 253 std::mutex stateChangelistenerStubMutex_; 254 std::mutex clientTrackerStubMutex_; 255 std::mutex ringerModelistenerStubMutex_; 256 sptr<AudioVolumeKeyEventCallbackStub> volumeKeyEventListenerStub_ = nullptr; 257 sptr<AudioRingerModeUpdateListenerStub> ringerModelistenerStub_ = nullptr; 258 sptr<AudioRendererStateChangeListenerStub> rendererStateChangelistenerStub_ = nullptr; 259 sptr<AudioCapturerStateChangeListenerStub> capturerStateChangelistenerStub_ = nullptr; 260 sptr<AudioClientTrackerCallbackStub> clientTrackerCbStub_ = nullptr; 261 static std::unordered_map<int32_t, std::weak_ptr<AudioRendererPolicyServiceDiedCallback>> rendererCBMap_; 262 }; 263 } // namespce AudioStandard 264 } // namespace OHOS 265 266 #endif // ST_AUDIO_POLICY_MANAGER_H 267