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_info.h" 21 #include "audio_interrupt_callback.h" 22 #include "audio_policy_manager_listener_stub.h" 23 #include "audio_ringermode_update_listener_stub.h" 24 #include "audio_system_manager.h" 25 #include "audio_volume_key_event_callback_stub.h" 26 #include "i_audio_volume_key_event_callback.h" 27 28 namespace OHOS { 29 namespace AudioStandard { 30 using InternalDeviceType = DeviceType; 31 32 class AudioPolicyManager { 33 public: GetInstance()34 static AudioPolicyManager& GetInstance() 35 { 36 static AudioPolicyManager policyManager; 37 if (!serverConnected) { 38 policyManager.Init(); 39 } 40 41 return policyManager; 42 } 43 44 int32_t SetStreamVolume(AudioStreamType streamType, float volume); 45 46 float GetStreamVolume(AudioStreamType streamType); 47 48 int32_t SetStreamMute(AudioStreamType streamType, bool mute); 49 50 bool GetStreamMute(AudioStreamType streamType); 51 52 bool IsStreamActive(AudioStreamType streamType); 53 54 std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag); 55 56 int32_t SetDeviceActive(InternalDeviceType deviceType, bool active); 57 58 bool IsDeviceActive(InternalDeviceType deviceType); 59 60 int32_t SetRingerMode(AudioRingerMode ringMode); 61 62 AudioRingerMode GetRingerMode(); 63 64 int32_t SetAudioScene(AudioScene scene); 65 66 AudioScene GetAudioScene(); 67 68 int32_t SetDeviceChangeCallback(const int32_t clientId, 69 const std::shared_ptr<AudioManagerDeviceChangeCallback> &callback); 70 71 int32_t UnsetDeviceChangeCallback(const int32_t clientId); 72 73 int32_t SetRingerModeCallback(const int32_t clientId, 74 const std::shared_ptr<AudioRingerModeCallback> &callback); 75 76 int32_t UnsetRingerModeCallback(const int32_t clientId); 77 78 int32_t SetAudioInterruptCallback(const uint32_t sessionID, 79 const std::shared_ptr<AudioInterruptCallback> &callback); 80 81 int32_t UnsetAudioInterruptCallback(const uint32_t sessionID); 82 83 int32_t ActivateAudioInterrupt(const AudioInterrupt &audioInterrupt); 84 85 int32_t DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt); 86 87 int32_t SetAudioManagerInterruptCallback(const uint32_t clientID, 88 const std::shared_ptr<AudioInterruptCallback> &callback); 89 90 int32_t UnsetAudioManagerInterruptCallback(const uint32_t clientID); 91 92 int32_t RequestAudioFocus(const uint32_t clientID, const AudioInterrupt &audioInterrupt); 93 94 int32_t AbandonAudioFocus(const uint32_t clientID, const AudioInterrupt &audioInterrupt); 95 96 AudioStreamType GetStreamInFocus(); 97 98 int32_t GetSessionInfoInFocus(AudioInterrupt &audioInterrupt); 99 100 int32_t SetVolumeKeyEventCallback(const int32_t clientPid, 101 const std::shared_ptr<VolumeKeyEventCallback> &callback); 102 int32_t UnsetVolumeKeyEventCallback(const int32_t clientPid); 103 private: AudioPolicyManager()104 AudioPolicyManager() 105 { 106 Init(); 107 } ~AudioPolicyManager()108 ~AudioPolicyManager() {} 109 110 void Init(); 111 sptr<AudioPolicyManagerListenerStub> listenerStub_ = nullptr; 112 std::mutex listenerStubMutex_; 113 std::mutex volumeCallbackMutex_; 114 115 sptr<AudioVolumeKeyEventCallbackStub> volumeKeyEventListenerStub_ = nullptr; 116 sptr<AudioRingerModeUpdateListenerStub> ringerModelistenerStub_ = nullptr; 117 static bool serverConnected; 118 void RegisterAudioPolicyServerDeathRecipient(); 119 void AudioPolicyServerDied(pid_t pid); 120 }; 121 } // namespce AudioStandard 122 } // namespace OHOS 123 124 #endif // ST_AUDIO_POLICY_MANAGER_H 125