1 /* 2 * Copyright (c) 2021-2024 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_client_tracker_callback_stub.h" 21 #include "audio_effect.h" 22 #include "audio_concurrency_callback.h" 23 #include "audio_concurrency_state_listener_stub.h" 24 #include "audio_interrupt_callback.h" 25 #include "audio_policy_base.h" 26 #include "audio_policy_manager_listener_stub.h" 27 #include "audio_policy_client_stub_impl.h" 28 #include "audio_routing_manager.h" 29 #include "audio_routing_manager_listener_stub.h" 30 #include "audio_system_manager.h" 31 #include "i_standard_client_tracker.h" 32 #include "audio_log.h" 33 #include "microphone_descriptor.h" 34 #include "audio_spatialization_manager.h" 35 #include "audio_spatialization_state_change_listener_stub.h" 36 #include "i_standard_spatialization_state_change_listener.h" 37 38 namespace OHOS { 39 namespace AudioStandard { 40 using InternalDeviceType = DeviceType; 41 using InternalAudioCapturerOptions = AudioCapturerOptions; 42 43 struct CallbackChangeInfo { 44 std::mutex mutex; 45 bool isEnable = false; 46 }; 47 48 class AudioPolicyManager { 49 public: 50 static AudioPolicyManager& GetInstance(); 51 static const sptr<IAudioPolicy> GetAudioPolicyManagerProxy(); 52 53 int32_t GetMaxVolumeLevel(AudioVolumeType volumeType); 54 55 int32_t GetMinVolumeLevel(AudioVolumeType volumeType); 56 57 int32_t SetSystemVolumeLevel(AudioVolumeType volumeType, int32_t volumeLevel, bool isLegacy = false, 58 int32_t volumeFlag = 0); 59 60 AudioStreamType GetSystemActiveVolumeType(const int32_t clientUid); 61 62 int32_t GetSystemVolumeLevel(AudioVolumeType volumeType); 63 64 int32_t SetLowPowerVolume(int32_t streamId, float volume); 65 66 float GetLowPowerVolume(int32_t streamId); 67 68 float GetSingleStreamVolume(int32_t streamId); 69 70 int32_t SetStreamMute(AudioVolumeType volumeType, bool mute, bool isLegacy = false); 71 72 bool GetStreamMute(AudioVolumeType volumeType); 73 74 bool IsStreamActive(AudioVolumeType volumeType); 75 76 int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter, 77 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors); 78 79 std::string GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType); 80 81 int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter, 82 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors); 83 84 std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag); 85 86 std::vector<sptr<AudioDeviceDescriptor>> GetDevicesInner(DeviceFlag deviceFlag); 87 88 int32_t SetDeviceActive(InternalDeviceType deviceType, bool active); 89 90 bool IsDeviceActive(InternalDeviceType deviceType); 91 92 DeviceType GetActiveOutputDevice(); 93 94 DeviceType GetActiveInputDevice(); 95 96 int32_t SetRingerModeLegacy(AudioRingerMode ringMode); 97 98 int32_t SetRingerMode(AudioRingerMode ringMode); 99 100 #ifdef FEATURE_DTMF_TONE 101 std::vector<int32_t> GetSupportedTones(); 102 103 std::shared_ptr<ToneInfo> GetToneConfig(int32_t ltonetype); 104 #endif 105 106 AudioRingerMode GetRingerMode(); 107 108 int32_t SetAudioScene(AudioScene scene); 109 110 int32_t SetMicrophoneMute(bool isMute); 111 112 int32_t SetMicrophoneMuteAudioConfig(bool isMute); 113 114 int32_t SetMicrophoneMutePersistent(const bool isMute, const PolicyType type); 115 116 bool GetPersistentMicMuteState(); 117 118 bool IsMicrophoneMuteLegacy(); 119 120 bool IsMicrophoneMute(); 121 122 AudioScene GetAudioScene(); 123 124 int32_t SetDeviceChangeCallback(const int32_t clientId, const DeviceFlag flag, 125 const std::shared_ptr<AudioManagerDeviceChangeCallback> &callback); 126 127 int32_t UnsetDeviceChangeCallback(const int32_t clientId, DeviceFlag flag, 128 std::shared_ptr<AudioManagerDeviceChangeCallback> &cb); 129 130 int32_t SetRingerModeCallback(const int32_t clientId, 131 const std::shared_ptr<AudioRingerModeCallback> &callback, API_VERSION api_v = API_9); 132 133 int32_t UnsetRingerModeCallback(const int32_t clientId); 134 135 int32_t UnsetRingerModeCallback(const int32_t clientId, 136 const std::shared_ptr<AudioRingerModeCallback> &callback); 137 138 int32_t SetMicStateChangeCallback(const int32_t clientId, 139 const std::shared_ptr<AudioManagerMicStateChangeCallback> &callback); 140 141 int32_t UnsetMicStateChangeCallback(const std::shared_ptr<AudioManagerMicStateChangeCallback> &callback); 142 143 int32_t SetAudioInterruptCallback(const uint32_t sessionID, 144 const std::shared_ptr<AudioInterruptCallback> &callback, uint32_t clientUid, const int32_t zoneID = 0); 145 146 int32_t UnsetAudioInterruptCallback(const uint32_t sessionID, const int32_t zoneID = 0); 147 148 int32_t ActivateAudioInterrupt( 149 const AudioInterrupt &audioInterrupt, const int32_t zoneID = 0, const bool isUpdatedAudioStrategy = false); 150 151 int32_t DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt, const int32_t zoneID = 0); 152 153 int32_t SetQueryClientTypeCallback(const std::shared_ptr<AudioQueryClientTypeCallback> &callback); 154 155 int32_t SetAudioManagerInterruptCallback(const int32_t clientId, 156 const std::shared_ptr<AudioInterruptCallback> &callback); 157 158 int32_t UnsetAudioManagerInterruptCallback(const int32_t clientId); 159 160 int32_t RequestAudioFocus(const int32_t clientId, const AudioInterrupt &audioInterrupt); 161 162 int32_t AbandonAudioFocus(const int32_t clientId, const AudioInterrupt &audioInterrupt); 163 164 AudioStreamType GetStreamInFocus(const int32_t zoneID = 0); 165 166 int32_t GetSessionInfoInFocus(AudioInterrupt &audioInterrupt, const int32_t zoneID = 0); 167 168 int32_t ActivateAudioSession(const AudioSessionStrategy &strategy); 169 170 int32_t DeactivateAudioSession(); 171 172 bool IsAudioSessionActivated(); 173 174 int32_t SetAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &audioSessionCallback); 175 176 int32_t UnsetAudioSessionCallback(); 177 178 int32_t UnsetAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &audioSessionCallback); 179 180 int32_t SetVolumeKeyEventCallback(const int32_t clientPid, 181 const std::shared_ptr<VolumeKeyEventCallback> &callback, API_VERSION api_v = API_9); 182 183 int32_t UnsetVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &callback); 184 185 bool CheckRecordingCreate(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid, 186 SourceType sourceType = SOURCE_TYPE_MIC); 187 188 bool CheckRecordingStateChange(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid, 189 AudioPermissionState state); 190 191 int32_t ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType); 192 193 int32_t GetAudioLatencyFromXml(); 194 195 uint32_t GetSinkLatencyFromXml(); 196 197 int32_t GetPreferredOutputStreamType(AudioRendererInfo &rendererInfo); 198 199 int32_t GetPreferredInputStreamType(AudioCapturerInfo &capturerInfo); 200 201 int32_t RegisterAudioRendererEventListener(const std::shared_ptr<AudioRendererStateChangeCallback> &callback); 202 203 int32_t UnregisterAudioRendererEventListener( 204 const std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> &callbacks); 205 206 int32_t UnregisterAudioRendererEventListener( 207 const std::shared_ptr<AudioRendererStateChangeCallback> &callback); 208 209 int32_t RegisterAudioCapturerEventListener(const int32_t clientPid, 210 const std::shared_ptr<AudioCapturerStateChangeCallback> &callback); 211 212 int32_t UnregisterAudioCapturerEventListener(const int32_t clientPid); 213 214 int32_t RegisterDeviceChangeWithInfoCallback( 215 const uint32_t sessionID, const std::weak_ptr<DeviceChangeWithInfoCallback> &callback); 216 217 int32_t UnregisterDeviceChangeWithInfoCallback(const uint32_t sessionID); 218 219 int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo, 220 const std::shared_ptr<AudioClientTracker> &clientTrackerObj); 221 222 int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo); 223 224 int32_t GetCurrentRendererChangeInfos( 225 std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos); 226 227 int32_t GetCurrentCapturerChangeInfos( 228 std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos); 229 230 int32_t UpdateStreamState(const int32_t clientUid, StreamSetState streamSetState, 231 StreamUsage streamUsage); 232 233 int32_t GetVolumeGroupInfos(std::string networkId, std::vector<sptr<VolumeGroupInfo>> &infos); 234 235 int32_t GetNetworkIdByGroupId(int32_t groupId, std::string &networkId); 236 237 bool IsAudioRendererLowLatencySupported(const AudioStreamInfo &audioStreamInfo); 238 239 std::vector<sptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescriptors(AudioRendererInfo &rendererInfo); 240 241 std::vector<sptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescriptors(AudioCapturerInfo &captureInfo); 242 243 int32_t SetPreferredOutputDeviceChangeCallback(const int32_t clientId, 244 const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &callback); 245 246 int32_t SetPreferredInputDeviceChangeCallback( 247 const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &callback); 248 249 int32_t UnsetPreferredOutputDeviceChangeCallback(const int32_t clientId); 250 251 int32_t UnsetPreferredInputDeviceChangeCallback(); 252 253 int32_t GetAudioFocusInfoList(std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList, 254 const int32_t zoneID = 0); 255 256 int32_t RegisterFocusInfoChangeCallback(const int32_t clientId, 257 const std::shared_ptr<AudioFocusInfoChangeCallback> &callback); 258 259 int32_t UnregisterFocusInfoChangeCallback(const int32_t clientId); 260 261 static void AudioPolicyServerDied(pid_t pid); 262 263 int32_t SetSystemSoundUri(const std::string &key, const std::string &uri); 264 265 std::string GetSystemSoundUri(const std::string &key); 266 267 float GetMinStreamVolume(void); 268 269 float GetMaxStreamVolume(void); 270 int32_t RegisterAudioPolicyServerDiedCb(const int32_t clientPid, 271 const std::shared_ptr<AudioRendererPolicyServiceDiedCallback> &callback); 272 int32_t UnregisterAudioPolicyServerDiedCb(const int32_t clientPid); 273 274 int32_t RegisterAudioStreamPolicyServerDiedCb( 275 const std::shared_ptr<AudioStreamPolicyServiceDiedCallback> &callback); 276 int32_t UnregisterAudioStreamPolicyServerDiedCb( 277 const std::shared_ptr<AudioStreamPolicyServiceDiedCallback> &callback); 278 279 bool IsVolumeUnadjustable(); 280 281 int32_t AdjustVolumeByStep(VolumeAdjustType adjustType); 282 283 int32_t AdjustSystemVolumeByStep(AudioVolumeType volumeType, VolumeAdjustType adjustType); 284 285 float GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType); 286 287 int32_t CheckMaxRendererInstances(); 288 289 int32_t QueryEffectSceneMode(SupportedEffectConfig &supportedEffectConfig); 290 291 int32_t SetPlaybackCapturerFilterInfos(const AudioPlaybackCaptureConfig &config, uint32_t appTokenId); 292 293 int32_t SetCaptureSilentState(bool state); 294 295 int32_t GetHardwareOutputSamplingRate(const sptr<AudioDeviceDescriptor> &desc); 296 297 void RecoverAudioPolicyCallbackClient(); 298 299 std::vector<sptr<MicrophoneDescriptor>> GetAudioCapturerMicrophoneDescriptors(int32_t sessionID); 300 301 std::vector<sptr<MicrophoneDescriptor>> GetAvailableMicrophones(); 302 303 int32_t SetDeviceAbsVolumeSupported(const std::string &macAddress, const bool support); 304 305 bool IsAbsVolumeScene(); 306 307 bool IsVgsVolumeSupported(); 308 309 int32_t SetA2dpDeviceVolume(const std::string &macAddress, const int32_t volume, const bool updateUi); 310 311 std::vector<std::unique_ptr<AudioDeviceDescriptor>> GetAvailableDevices(AudioDeviceUsage usage); 312 313 int32_t SetAvailableDeviceChangeCallback(const int32_t clientId, const AudioDeviceUsage usage, 314 const std::shared_ptr<AudioManagerAvailableDeviceChangeCallback>& callback); 315 316 int32_t UnsetAvailableDeviceChangeCallback(const int32_t clientId, AudioDeviceUsage usage); 317 318 bool IsSpatializationEnabled(); 319 320 bool IsSpatializationEnabled(const std::string address); 321 322 int32_t SetSpatializationEnabled(const bool enable); 323 324 int32_t SetSpatializationEnabled(const sptr<AudioDeviceDescriptor> &selectedAudioDevice, const bool enable); 325 326 bool IsHeadTrackingEnabled(); 327 328 bool IsHeadTrackingEnabled(const std::string address); 329 330 int32_t SetHeadTrackingEnabled(const bool enable); 331 332 int32_t SetHeadTrackingEnabled(const sptr<AudioDeviceDescriptor> &selectedAudioDevice, const bool enable); 333 334 int32_t RegisterSpatializationEnabledEventListener( 335 const std::shared_ptr<AudioSpatializationEnabledChangeCallback> &callback); 336 337 int32_t RegisterHeadTrackingEnabledEventListener( 338 const std::shared_ptr<AudioHeadTrackingEnabledChangeCallback> &callback); 339 340 int32_t UnregisterSpatializationEnabledEventListener(); 341 342 int32_t UnregisterHeadTrackingEnabledEventListener(); 343 344 AudioSpatializationState GetSpatializationState(const StreamUsage streamUsage); 345 346 bool IsSpatializationSupported(); 347 348 bool IsSpatializationSupportedForDevice(const std::string address); 349 350 bool IsHeadTrackingSupported(); 351 352 bool IsHeadTrackingSupportedForDevice(const std::string address); 353 354 int32_t UpdateSpatialDeviceState(const AudioSpatialDeviceState audioSpatialDeviceState); 355 356 int32_t RegisterSpatializationStateEventListener(const uint32_t sessionID, const StreamUsage streamUsage, 357 const std::shared_ptr<AudioSpatializationStateChangeCallback> &callback); 358 359 int32_t ConfigDistributedRoutingRole(sptr<AudioDeviceDescriptor> descriptor, CastType type); 360 361 int32_t SetDistributedRoutingRoleCallback(const std::shared_ptr<AudioDistributedRoutingRoleCallback> &callback); 362 363 int32_t UnsetDistributedRoutingRoleCallback(); 364 365 int32_t UnregisterSpatializationStateEventListener(const uint32_t sessionID); 366 367 int32_t CreateAudioInterruptZone(const std::set<int32_t> &pids, const int32_t zoneID); 368 369 int32_t AddAudioInterruptZonePids(const std::set<int32_t> &pids, const int32_t zoneID); 370 371 int32_t RemoveAudioInterruptZonePids(const std::set<int32_t> &pids, const int32_t zoneID); 372 373 int32_t ReleaseAudioInterruptZone(const int32_t zoneID); 374 375 int32_t SetCallDeviceActive(InternalDeviceType deviceType, bool active, std::string address); 376 377 std::unique_ptr<AudioDeviceDescriptor> GetActiveBluetoothDevice(); 378 379 int32_t NotifyCapturerAdded(AudioCapturerInfo capturerInfo, AudioStreamInfo streamInfo, uint32_t sessionId); 380 381 ConverterConfig GetConverterConfig(); 382 383 void FetchOutputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo, 384 const AudioStreamDeviceChangeReasonExt reason); 385 386 void FetchInputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo); 387 388 bool IsHighResolutionExist(); 389 390 int32_t SetHighResolutionExist(bool highResExist); 391 392 AudioSpatializationSceneType GetSpatializationSceneType(); 393 394 int32_t SetSpatializationSceneType(const AudioSpatializationSceneType spatializationSceneType); 395 396 float GetMaxAmplitude(const int32_t deviceId); 397 398 int32_t DisableSafeMediaVolume(); 399 400 bool IsHeadTrackingDataRequested(const std::string &macAddress); 401 402 int32_t RegisterHeadTrackingDataRequestedEventListener(const std::string &macAddress, 403 const std::shared_ptr<HeadTrackingDataRequestedChangeCallback> &callback); 404 405 int32_t UnregisterHeadTrackingDataRequestedEventListener(const std::string &macAddress); 406 407 int32_t SetAudioDeviceRefinerCallback(const std::shared_ptr<AudioDeviceRefiner> &callback); 408 409 int32_t UnsetAudioDeviceRefinerCallback(); 410 411 int32_t TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason); 412 413 int32_t SetPreferredDevice(const PreferredType preferredType, const sptr<AudioDeviceDescriptor> &desc); 414 415 int32_t MoveToNewPipe(const uint32_t sessionId, const AudioPipeType pipeType); 416 417 int32_t SetAudioConcurrencyCallback(const uint32_t sessionID, 418 const std::shared_ptr<AudioConcurrencyCallback> &callback); 419 420 int32_t UnsetAudioConcurrencyCallback(const uint32_t sessionID); 421 422 int32_t ActivateAudioConcurrency(const AudioPipeType &pipeType); 423 424 int32_t InjectInterruption(const std::string networkId, InterruptEvent &event); 425 426 int32_t SetMicrophoneBlockedCallback(const int32_t clientId, 427 const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &callback); 428 429 int32_t UnsetMicrophoneBlockedCallback(const int32_t clientId, 430 const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &callback); 431 432 int32_t SetDefaultOutputDevice(const DeviceType deviceType, const uint32_t sessionID, 433 const StreamUsage streamUsage, bool isRunning); 434 435 void SaveRemoteInfo(const std::string &networkId, DeviceType deviceType); 436 437 int32_t LoadSplitModule(const std::string &splitArgs, const std::string &networkId); 438 439 int32_t SetVoiceRingtoneMute(bool isMute); 440 private: AudioPolicyManager()441 AudioPolicyManager() {} ~AudioPolicyManager()442 ~AudioPolicyManager() {} 443 444 int32_t RegisterPolicyCallbackClientFunc(const sptr<IAudioPolicy> &gsp); 445 int32_t SetClientCallbacksEnable(const CallbackChange &callbackchange, const bool &enable); 446 447 std::mutex listenerStubMutex_; 448 std::mutex registerCallbackMutex_; 449 std::mutex stateChangelistenerStubMutex_; 450 std::mutex clientTrackerStubMutex_; 451 sptr<AudioPolicyClientStubImpl> audioPolicyClientStubCB_; 452 std::atomic<bool> isAudioPolicyClientRegisted_ = false; 453 454 static std::unordered_map<int32_t, std::weak_ptr<AudioRendererPolicyServiceDiedCallback>> rendererCBMap_; 455 static std::vector<std::weak_ptr<AudioStreamPolicyServiceDiedCallback>> audioStreamCBMap_; 456 457 bool isAudioRendererEventListenerRegistered = false; 458 bool isAudioCapturerEventListenerRegistered = false; 459 460 std::array<CallbackChangeInfo, CALLBACK_MAX> callbackChangeInfos_ = {}; 461 }; 462 } // namespce AudioStandard 463 } // namespace OHOS 464 465 #endif // ST_AUDIO_POLICY_MANAGER_H 466