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_policy_base.h" 21 #include "audio_capturer_state_change_listener_stub.h" 22 #include "audio_client_tracker_callback_stub.h" 23 #include "audio_info.h" 24 #include "audio_interrupt_callback.h" 25 #include "audio_policy_manager_listener_stub.h" 26 #include "audio_renderer_state_change_listener_stub.h" 27 #include "audio_ringermode_update_listener_stub.h" 28 #include "audio_routing_manager.h" 29 #include "audio_routing_manager_listener_stub.h" 30 #include "audio_system_manager.h" 31 #include "audio_volume_key_event_callback_stub.h" 32 #include "audio_system_manager.h" 33 #include "i_audio_volume_key_event_callback.h" 34 #include "i_standard_renderer_state_change_listener.h" 35 #include "i_standard_capturer_state_change_listener.h" 36 #include "i_standard_client_tracker.h" 37 38 namespace OHOS { 39 namespace AudioStandard { 40 using InternalDeviceType = DeviceType; 41 42 class AudioPolicyManager { 43 public: GetInstance()44 static AudioPolicyManager& GetInstance() 45 { 46 static AudioPolicyManager policyManager; 47 return policyManager; 48 } 49 50 const sptr<IAudioPolicy> GetAudioPolicyManagerProxy(); 51 52 int32_t SetStreamVolume(AudioStreamType streamType, float volume); 53 54 float GetStreamVolume(AudioStreamType streamType); 55 56 int32_t SetLowPowerVolume(int32_t streamId, float volume); 57 58 float GetLowPowerVolume(int32_t streamId); 59 60 float GetSingleStreamVolume(int32_t streamId); 61 62 int32_t SetStreamMute(AudioStreamType streamType, bool mute); 63 64 bool GetStreamMute(AudioStreamType streamType); 65 66 bool IsStreamActive(AudioStreamType streamType); 67 68 int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter, 69 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors); 70 71 std::string GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType); 72 73 int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter, 74 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors); 75 76 std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag); 77 78 int32_t SetDeviceActive(InternalDeviceType deviceType, bool active); 79 80 bool IsDeviceActive(InternalDeviceType deviceType); 81 82 DeviceType GetActiveOutputDevice(); 83 84 DeviceType GetActiveInputDevice(); 85 86 int32_t SetRingerMode(AudioRingerMode ringMode); 87 88 std::vector<int32_t> GetSupportedTones(); 89 90 std::shared_ptr<ToneInfo> GetToneConfig(int32_t ltonetype); 91 92 AudioRingerMode GetRingerMode(); 93 94 int32_t SetAudioScene(AudioScene scene); 95 96 int32_t SetMicrophoneMute(bool isMute); 97 98 int32_t SetMicrophoneMuteAudioConfig(bool isMute); 99 100 bool IsMicrophoneMute(API_VERSION api_v = API_9); 101 102 AudioScene GetAudioScene(); 103 104 int32_t SetDeviceChangeCallback(const int32_t clientId, const DeviceFlag flag, 105 const std::shared_ptr<AudioManagerDeviceChangeCallback> &callback); 106 107 int32_t UnsetDeviceChangeCallback(const int32_t clientId); 108 109 int32_t SetRingerModeCallback(const int32_t clientId, 110 const std::shared_ptr<AudioRingerModeCallback> &callback); 111 112 int32_t UnsetRingerModeCallback(const int32_t clientId); 113 114 int32_t SetMicStateChangeCallback(const int32_t clientId, 115 const std::shared_ptr<AudioManagerMicStateChangeCallback> &callback); 116 117 int32_t SetAudioInterruptCallback(const uint32_t sessionID, 118 const std::shared_ptr<AudioInterruptCallback> &callback); 119 120 int32_t UnsetAudioInterruptCallback(const uint32_t sessionID); 121 122 int32_t ActivateAudioInterrupt(const AudioInterrupt &audioInterrupt); 123 124 int32_t DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt); 125 126 int32_t SetAudioManagerInterruptCallback(const uint32_t clientID, 127 const std::shared_ptr<AudioInterruptCallback> &callback); 128 129 int32_t UnsetAudioManagerInterruptCallback(const uint32_t clientID); 130 131 int32_t RequestAudioFocus(const uint32_t clientID, const AudioInterrupt &audioInterrupt); 132 133 int32_t AbandonAudioFocus(const uint32_t clientID, const AudioInterrupt &audioInterrupt); 134 135 AudioStreamType GetStreamInFocus(); 136 137 int32_t GetSessionInfoInFocus(AudioInterrupt &audioInterrupt); 138 139 int32_t SetVolumeKeyEventCallback(const int32_t clientPid, const std::shared_ptr<VolumeKeyEventCallback> &callback); 140 141 int32_t UnsetVolumeKeyEventCallback(const int32_t clientPid); 142 143 bool VerifyClientPermission(const std::string &permissionName, uint32_t appTokenId, int32_t appUid, 144 bool privacyFlag, AudioPermissionState state); 145 146 bool getUsingPemissionFromPrivacy(const std::string &permissionName, uint32_t appTokenId, 147 AudioPermissionState state); 148 149 int32_t ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType); 150 151 int32_t GetAudioLatencyFromXml(); 152 153 uint32_t GetSinkLatencyFromXml(); 154 155 int32_t RegisterAudioRendererEventListener(const int32_t clientUID, 156 const std::shared_ptr<AudioRendererStateChangeCallback> &callback); 157 158 int32_t UnregisterAudioRendererEventListener(const int32_t clientUID); 159 160 int32_t RegisterAudioCapturerEventListener(const int32_t clientUID, 161 const std::shared_ptr<AudioCapturerStateChangeCallback> &callback); 162 163 int32_t UnregisterAudioCapturerEventListener(const int32_t clientUID); 164 165 int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo, 166 const std::shared_ptr<AudioClientTracker> &clientTrackerObj); 167 168 int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo); 169 170 int32_t GetCurrentRendererChangeInfos( 171 std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos); 172 173 int32_t GetCurrentCapturerChangeInfos( 174 std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos); 175 176 int32_t UpdateStreamState(const int32_t clientUid, StreamSetState streamSetState, 177 AudioStreamType audioStreamType); 178 179 std::vector<sptr<VolumeGroupInfo>> GetVolumeGroupInfos(); 180 181 bool IsAudioRendererLowLatencySupported(const AudioStreamInfo &audioStreamInfo); 182 183 std::vector<sptr<AudioDeviceDescriptor>> GetActiveOutputDeviceDescriptors(); 184 private: AudioPolicyManager()185 AudioPolicyManager() {} ~AudioPolicyManager()186 ~AudioPolicyManager() {} 187 188 void Init(); 189 sptr<AudioPolicyManagerListenerStub> listenerStub_ = nullptr; 190 std::mutex listenerStubMutex_; 191 std::mutex volumeCallbackMutex_; 192 std::mutex stateChangelistenerStubMutex_; 193 std::mutex clientTrackerStubMutex_; 194 std::mutex ringerModelistenerStubMutex_; 195 sptr<AudioVolumeKeyEventCallbackStub> volumeKeyEventListenerStub_ = nullptr; 196 sptr<AudioRingerModeUpdateListenerStub> ringerModelistenerStub_ = nullptr; 197 sptr<AudioRendererStateChangeListenerStub> rendererStateChangelistenerStub_ = nullptr; 198 sptr<AudioCapturerStateChangeListenerStub> capturerStateChangelistenerStub_ = nullptr; 199 sptr<AudioClientTrackerCallbackStub> clientTrackerCbStub_ = nullptr; 200 void RegisterAudioPolicyServerDeathRecipient(); 201 void AudioPolicyServerDied(pid_t pid); 202 }; 203 } // namespce AudioStandard 204 } // namespace OHOS 205 206 #endif // ST_AUDIO_POLICY_MANAGER_H 207