1 /* 2 * Copyright (c) 2023-2025 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 AUDIO_POLICY_CLIENT_STUB_IMPL_H 17 #define AUDIO_POLICY_CLIENT_STUB_IMPL_H 18 19 #include "audio_policy_client_stub.h" 20 #include "audio_device_info.h" 21 #include "audio_session_manager.h" 22 #include "audio_system_manager.h" 23 #include "audio_interrupt_info.h" 24 #include "audio_group_manager.h" 25 #include "audio_routing_manager.h" 26 #include "audio_spatialization_manager.h" 27 #include "audio_combine_denoising_manager.h" 28 #include "audio_policy_interface.h" 29 #include "audio_stream_manager.h" 30 31 namespace OHOS { 32 namespace AudioStandard { 33 class AudioPolicyClientStubImpl : public AudioPolicyClientStub { 34 public: 35 int32_t AddVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb); 36 int32_t RemoveVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb); 37 size_t GetVolumeKeyEventCallbackSize() const; 38 int32_t AddVolumeDegreeCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb); 39 int32_t RemoveVolumeDegreeCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb); 40 size_t GetVolumeDegreeCallbackSize() const; 41 int32_t AddSystemVolumeChangeCallback(const std::shared_ptr<SystemVolumeChangeCallback> &cb); 42 int32_t RemoveSystemVolumeChangeCallback(const std::shared_ptr<SystemVolumeChangeCallback> &cb); 43 size_t GetSystemVolumeChangeCallbackSize() const; 44 size_t GetStreamVolumeChangeCallbackSize() const; 45 std::set<StreamUsage> GetStreamVolumeChangeCallbackStreamUsages() const; 46 int32_t AddStreamVolumeChangeCallback(const std::set<StreamUsage> &streamUsages, 47 const std::shared_ptr<StreamVolumeChangeCallback> &cb); 48 int32_t RemoveStreamVolumeChangeCallback(const std::shared_ptr<StreamVolumeChangeCallback> &cb); 49 int32_t AddFocusInfoChangeCallback(const std::shared_ptr<AudioFocusInfoChangeCallback> &cb); 50 int32_t RemoveFocusInfoChangeCallback(); 51 int32_t AddDeviceChangeCallback(const DeviceFlag &flag, 52 const std::shared_ptr<AudioManagerDeviceChangeCallback> &cb); 53 int32_t RemoveDeviceChangeCallback(DeviceFlag flag, std::shared_ptr<AudioManagerDeviceChangeCallback> &cb); 54 size_t GetDeviceChangeCallbackSize() const; 55 int32_t AddRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> &cb); 56 int32_t AddAppVolumeChangeForUidCallback(const int32_t appUid, 57 const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb); 58 int32_t RemoveAppVolumeChangeForUidCallback( 59 const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb); 60 int32_t RemoveAllAppVolumeChangeForUidCallback(); 61 size_t GetAppVolumeChangeCallbackForUidSize() const; 62 size_t GetSelfAppVolumeChangeCallbackSize() const; 63 int32_t AddActiveVolumeTypeChangeCallback(const std::shared_ptr<AudioManagerActiveVolumeTypeChangeCallback> &cb); 64 int32_t RemoveAllActiveVolumeTypeChangeCallback(); 65 int32_t RemoveActiveVolumeTypeChangeCallback(const std::shared_ptr<AudioManagerActiveVolumeTypeChangeCallback> &cb); 66 int32_t AddSelfAppVolumeChangeCallback(int32_t appUid, 67 const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb); 68 int32_t RemoveSelfAppVolumeChangeCallback(int32_t appUid, 69 const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb); 70 int32_t RemoveAllSelfAppVolumeChangeCallback(int32_t appUid); 71 int32_t RemoveRingerModeCallback(); 72 int32_t RemoveRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> &cb); 73 size_t GetActiveVolumeTypeChangeCallbackSize() const; 74 size_t GetRingerModeCallbackSize() const; 75 int32_t AddMicStateChangeCallback(const std::shared_ptr<AudioManagerMicStateChangeCallback> &cb); 76 int32_t RemoveMicStateChangeCallback(); 77 size_t GetMicStateChangeCallbackSize() const; 78 bool HasMicStateChangeCallback(); 79 int32_t AddPreferredOutputDeviceChangeCallback(const AudioRendererInfo &rendererInfo, 80 const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &cb); 81 int32_t RemovePreferredOutputDeviceChangeCallback( 82 const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &cb = nullptr); 83 size_t GetPreferredOutputDeviceChangeCallbackSize() const; 84 int32_t AddPreferredInputDeviceChangeCallback(const AudioCapturerInfo &capturerInfo, 85 const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &cb); 86 int32_t RemovePreferredInputDeviceChangeCallback( 87 const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &cb = nullptr); 88 size_t GetPreferredInputDeviceChangeCallbackSize() const; 89 int32_t AddRendererStateChangeCallback(const std::shared_ptr<AudioRendererStateChangeCallback> &cb); 90 int32_t RemoveRendererStateChangeCallback( 91 const std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> &callbacks); 92 int32_t RemoveRendererStateChangeCallback( 93 const std::shared_ptr<AudioRendererStateChangeCallback> &callback); 94 size_t GetRendererStateChangeCallbackSize() const; 95 int32_t AddCapturerStateChangeCallback(const std::shared_ptr<AudioCapturerStateChangeCallback> &cb); 96 int32_t RemoveCapturerStateChangeCallback(); 97 size_t GetCapturerStateChangeCallbackSize() const; 98 int32_t AddDeviceChangeWithInfoCallback( 99 const uint32_t sessionId, const std::weak_ptr<DeviceChangeWithInfoCallback> &cb); 100 int32_t RemoveDeviceChangeWithInfoCallback(const uint32_t sessionId); 101 size_t GetDeviceChangeWithInfoCallbackkSize() const; 102 int32_t AddMicrophoneBlockedCallback(const int32_t clientId, 103 const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &cb); 104 int32_t RemoveMicrophoneBlockedCallback(const int32_t clientId, 105 const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &cb); 106 size_t GetMicrophoneBlockedCallbackSize() const; 107 int32_t AddHeadTrackingDataRequestedChangeCallback(const std::string &macAddress, 108 const std::shared_ptr<HeadTrackingDataRequestedChangeCallback> &cb); 109 int32_t RemoveHeadTrackingDataRequestedChangeCallback(const std::string &macAddress); 110 size_t GetHeadTrackingDataRequestedChangeCallbackSize() const; 111 int32_t AddSpatializationEnabledChangeCallback(const std::shared_ptr<AudioSpatializationEnabledChangeCallback> &cb); 112 int32_t RemoveSpatializationEnabledChangeCallback(); 113 size_t GetSpatializationEnabledChangeCallbackSize() const; 114 int32_t AddSpatializationEnabledChangeForCurrentDeviceCallback( 115 const std::shared_ptr<AudioSpatializationEnabledChangeForCurrentDeviceCallback> &cb); 116 int32_t RemoveSpatializationEnabledChangeForCurrentDeviceCallback(); 117 size_t GetSpatializationEnabledChangeForCurrentDeviceCallbackSize() const; 118 int32_t AddHeadTrackingEnabledChangeCallback(const std::shared_ptr<AudioHeadTrackingEnabledChangeCallback> &cb); 119 int32_t RemoveHeadTrackingEnabledChangeCallback(); 120 size_t GetHeadTrackingEnabledChangeCallbacSize() const; 121 int32_t AddNnStateChangeCallback(const std::shared_ptr<AudioNnStateChangeCallback> &cb); 122 int32_t RemoveNnStateChangeCallback(); 123 size_t GetNnStateChangeCallbackSize() const; 124 size_t GetFocusInfoChangeCallbackSize() const; 125 int32_t AddAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &cb); 126 int32_t RemoveAudioSessionCallback(); 127 int32_t RemoveAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &cb); 128 size_t GetAudioSessionCallbackSize() const; 129 int32_t AddAudioSessionStateCallback(const std::shared_ptr<AudioSessionStateChangedCallback> &cb); 130 int32_t RemoveAudioSessionStateCallback(); 131 int32_t RemoveAudioSessionStateCallback(const std::shared_ptr<AudioSessionStateChangedCallback> &cb); 132 size_t GetAudioSessionStateCallbackSize() const; 133 int32_t AddAudioSessionDeviceCallback(const std::shared_ptr<AudioSessionCurrentDeviceChangedCallback> &cb); 134 int32_t RemoveAudioSessionDeviceCallback(); 135 int32_t RemoveAudioSessionDeviceCallback(const std::shared_ptr<AudioSessionCurrentDeviceChangedCallback> &cb); 136 size_t GetAudioSessionDeviceCallbackSize() const; 137 int32_t AddAudioSceneChangedCallback(const int32_t clientId, 138 const std::shared_ptr<AudioManagerAudioSceneChangedCallback> &cb); 139 int32_t RemoveAudioSceneChangedCallback( 140 const std::shared_ptr<AudioManagerAudioSceneChangedCallback> &cb); 141 size_t GetAudioSceneChangedCallbackSize() const; 142 int32_t AddAudioFormatUnsupportedErrorCallback(const std::shared_ptr<AudioFormatUnsupportedErrorCallback> &cb); 143 int32_t RemoveAudioFormatUnsupportedErrorCallback(); 144 size_t GetAudioFormatUnsupportedErrorCallbackSize() const; 145 146 int32_t OnRecreateRendererStreamEvent(uint32_t sessionId, int32_t streamFlag, 147 const AudioStreamDeviceChangeReasonExt &reason) override; 148 int32_t OnRecreateCapturerStreamEvent(uint32_t sessionId, int32_t streamFlag, 149 const AudioStreamDeviceChangeReasonExt &reason) override; 150 int32_t OnVolumeKeyEvent(const VolumeEvent &volumeEvent) override; 151 int32_t OnVolumeDegreeEvent(const VolumeEvent &volumeEvent) override; 152 int32_t OnAudioFocusInfoChange(const std::vector<std::map<AudioInterrupt, int32_t>> &focusInfoList) override; 153 int32_t OnAudioFocusRequested(const AudioInterrupt &requestFocus) override; 154 int32_t OnAudioFocusAbandoned(const AudioInterrupt &abandonFocus) override; 155 int32_t OnDeviceChange(const DeviceChangeAction &deviceChangeAction) override; 156 int32_t OnMicrophoneBlocked(const MicrophoneBlockedInfo µphoneBlockedInfo) override; 157 int32_t OnRingerModeUpdated(int32_t ringerMode) override; 158 int32_t OnActiveVolumeTypeChanged(int32_t volumeType) override; 159 int32_t OnAppVolumeChanged(int32_t appUid, const VolumeEvent& volumeEvent) override; 160 int32_t OnMicStateUpdated(const MicStateChangeEvent &micStateChangeEvent) override; 161 int32_t OnPreferredOutputDeviceUpdated(const AudioRendererInfo &rendererInfo, 162 const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc) override; 163 int32_t OnPreferredInputDeviceUpdated(const AudioCapturerInfo &capturerInfo, 164 const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc) override; 165 int32_t OnRendererStateChange( 166 const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos) override; 167 int32_t OnCapturerStateChange( 168 const std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos) override; 169 int32_t OnRendererDeviceChange(uint32_t sessionId, 170 const AudioDeviceDescriptor &deviceInfo, const AudioStreamDeviceChangeReasonExt &reason) override; 171 int32_t OnHeadTrackingDeviceChange(const std::unordered_map<std::string, bool> &changeInfo) override; 172 int32_t OnSpatializationEnabledChange(bool enabled) override; 173 int32_t OnSpatializationEnabledChangeForAnyDevice(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, 174 bool enabled) override; 175 int32_t OnSpatializationEnabledChangeForCurrentDevice(bool enabled) override; 176 int32_t OnHeadTrackingEnabledChange(bool enabled) override; 177 int32_t OnHeadTrackingEnabledChangeForAnyDevice(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, 178 bool enabled) override; 179 int32_t OnNnStateChange(int32_t nnState) override; 180 int32_t OnAudioSessionDeactive(int32_t deactiveEvent) override; 181 int32_t OnAudioSceneChange(int32_t audioScene) override; 182 int32_t OnAudioSessionStateChanged(int32_t stateChangeHint) override; 183 int32_t OnAudioSessionCurrentDeviceChanged(const CurrentOutputDeviceChangedEvent &deviceChangedEvent) override; 184 int32_t OnFormatUnsupportedError(int32_t errorCode) override; 185 int32_t OnStreamVolumeChange(const StreamVolumeEvent &streamVolumeEvent) override; 186 int32_t OnSystemVolumeChange(const VolumeEvent &volumeEvent) override; 187 private: 188 std::vector<std::shared_ptr<AudioDeviceDescriptor>> DeviceFilterByFlag(DeviceFlag flag, 189 const std::vector<std::shared_ptr<AudioDeviceDescriptor>>& desc); 190 191 std::vector<std::weak_ptr<VolumeKeyEventCallback>> volumeKeyEventCallbackList_; 192 std::vector<std::weak_ptr<VolumeKeyEventCallback>> volumeDegreeCallbackList_; 193 std::vector<std::pair<std::set<StreamUsage>, 194 std::weak_ptr<StreamVolumeChangeCallback>>> streamVolumeChangeCallbackList_; 195 std::vector<std::weak_ptr<SystemVolumeChangeCallback>> systemVolumeChangeCallbackList_; 196 std::vector<std::shared_ptr<AudioFocusInfoChangeCallback>> focusInfoChangeCallbackList_; 197 std::vector<std::pair<DeviceFlag, std::shared_ptr<AudioManagerDeviceChangeCallback>>> deviceChangeCallbackList_; 198 std::vector<std::shared_ptr<AudioRingerModeCallback>> ringerModeCallbackList_; 199 std::vector<std::weak_ptr<AudioManagerActiveVolumeTypeChangeCallback>> activeVolumeTypeChangeCallbackList_; 200 std::vector<std::pair<int32_t, std::shared_ptr< 201 AudioManagerAppVolumeChangeCallback>>> appVolumeChangeForUidCallback_; 202 std::map<int32_t, int32_t> appVolumeChangeForUidCallbackNum; 203 std::vector<std::pair<int32_t, std::shared_ptr<AudioManagerAppVolumeChangeCallback>>> selfAppVolumeChangeCallback_; 204 std::map<int32_t, int32_t> selfAppVolumeChangeCallbackNum_; 205 std::vector<std::shared_ptr<AudioManagerMicStateChangeCallback>> micStateChangeCallbackList_; 206 std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> rendererStateChangeCallbackList_; 207 std::vector<std::weak_ptr<AudioCapturerStateChangeCallback>> capturerStateChangeCallbackList_; 208 std::vector<std::shared_ptr<AudioSpatializationEnabledChangeCallback>> spatializationEnabledChangeCallbackList_; 209 std::vector<std::shared_ptr<AudioSpatializationEnabledChangeForCurrentDeviceCallback>> 210 spatializationEnabledChangeForCurrentDeviceCallbackList_; 211 std::vector<std::shared_ptr<AudioHeadTrackingEnabledChangeCallback>> headTrackingEnabledChangeCallbackList_; 212 std::vector<std::shared_ptr<AudioNnStateChangeCallback>> nnStateChangeCallbackList_; 213 std::vector<std::shared_ptr<AudioSessionCallback>> audioSessionCallbackList_; 214 std::vector<std::weak_ptr<AudioSessionStateChangedCallback>> audioSessionStateCallbackList_; 215 std::vector<std::weak_ptr<AudioSessionCurrentDeviceChangedCallback>> audioSessionDeviceCallbackList_; 216 std::vector<std::pair<int32_t, std::shared_ptr<AudioManagerMicrophoneBlockedCallback>>> 217 microphoneBlockedCallbackList_; 218 std::vector<std::shared_ptr<AudioManagerAudioSceneChangedCallback>> audioSceneChangedCallbackList_; 219 std::vector<std::shared_ptr<AudioFormatUnsupportedErrorCallback>> AudioFormatUnsupportedErrorCallbackList_; 220 221 std::unordered_map<StreamUsage, 222 std::vector<std::shared_ptr<AudioPreferredOutputDeviceChangeCallback>>> preferredOutputDeviceCallbackMap_; 223 std::unordered_map<SourceType, 224 std::vector<std::shared_ptr<AudioPreferredInputDeviceChangeCallback>>> preferredInputDeviceCallbackMap_; 225 226 std::unordered_map<uint32_t, 227 std::weak_ptr<DeviceChangeWithInfoCallback>> deviceChangeWithInfoCallbackMap_; 228 229 std::unordered_map<std::string, 230 std::shared_ptr<HeadTrackingDataRequestedChangeCallback>> headTrackingDataRequestedChangeCallbackMap_; 231 232 mutable std::mutex focusInfoChangeMutex_; 233 mutable std::mutex rendererStateChangeMutex_; 234 mutable std::mutex capturerStateChangeMutex_; 235 mutable std::mutex pOutputDeviceChangeMutex_; 236 mutable std::mutex pInputDeviceChangeMutex_; 237 mutable std::mutex volumeKeyEventMutex_; 238 mutable std::mutex volumeDegreeEventMutex_; 239 mutable std::mutex deviceChangeMutex_; 240 mutable std::mutex ringerModeMutex_; 241 mutable std::mutex activeVolumeTypeChangeMutex_; 242 mutable std::mutex appVolumeChangeForUidMutex_; 243 mutable std::mutex selfAppVolumeChangeMutex_; 244 mutable std::mutex micStateChangeMutex_; 245 mutable std::mutex deviceChangeWithInfoCallbackMutex_; 246 mutable std::mutex headTrackingDataRequestedChangeMutex_; 247 mutable std::mutex spatializationEnabledChangeMutex_; 248 mutable std::mutex spatializationEnabledChangeForCurrentDeviceMutex_; 249 mutable std::mutex headTrackingEnabledChangeMutex_; 250 mutable std::mutex nnStateChangeMutex_; 251 mutable std::mutex audioSessionMutex_; 252 mutable std::mutex audioSessionStateMutex_; 253 mutable std::mutex audioSessionDeviceMutex_; 254 mutable std::mutex microphoneBlockedMutex_; 255 mutable std::mutex audioSceneChangedMutex_; 256 mutable std::mutex formatUnsupportedErrorMutex_; 257 mutable std::mutex streamVolumeChangeMutex_; 258 mutable std::mutex systemVolumeChangeMutex_; 259 }; 260 } // namespace AudioStandard 261 } // namespace OHOS 262 #endif // AUDIO_POLICY_CLIENT_STUB_IMPL_H