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 30 namespace OHOS { 31 namespace AudioStandard { 32 class AudioPolicyClientStubImpl : public AudioPolicyClientStub { 33 public: 34 int32_t AddVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb); 35 int32_t RemoveVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb); 36 size_t GetVolumeKeyEventCallbackSize() const; 37 int32_t AddFocusInfoChangeCallback(const std::shared_ptr<AudioFocusInfoChangeCallback> &cb); 38 int32_t RemoveFocusInfoChangeCallback(); 39 int32_t AddDeviceChangeCallback(const DeviceFlag &flag, 40 const std::shared_ptr<AudioManagerDeviceChangeCallback> &cb); 41 int32_t RemoveDeviceChangeCallback(DeviceFlag flag, std::shared_ptr<AudioManagerDeviceChangeCallback> &cb); 42 size_t GetDeviceChangeCallbackSize() const; 43 int32_t AddRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> &cb); 44 int32_t AddAppVolumeChangeForUidCallback(const int32_t appUid, 45 const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb); 46 int32_t RemoveAppVolumeChangeForUidCallback( 47 const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb); 48 int32_t RemoveAllAppVolumeChangeForUidCallback(); 49 size_t GetAppVolumeChangeCallbackForUidSize() const; 50 size_t GetSelfAppVolumeChangeCallbackSize() const; 51 int32_t AddSelfAppVolumeChangeCallback(int32_t appUid, 52 const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb); 53 int32_t RemoveSelfAppVolumeChangeCallback(int32_t appUid, 54 const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb); 55 int32_t RemoveAllSelfAppVolumeChangeCallback(int32_t appUid); 56 int32_t RemoveRingerModeCallback(); 57 int32_t RemoveRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> &cb); 58 size_t GetRingerModeCallbackSize() const; 59 int32_t AddMicStateChangeCallback(const std::shared_ptr<AudioManagerMicStateChangeCallback> &cb); 60 int32_t RemoveMicStateChangeCallback(); 61 size_t GetMicStateChangeCallbackSize() const; 62 bool HasMicStateChangeCallback(); 63 int32_t AddPreferredOutputDeviceChangeCallback(const AudioRendererInfo &rendererInfo, 64 const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &cb); 65 int32_t RemovePreferredOutputDeviceChangeCallback( 66 const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &cb = nullptr); 67 size_t GetPreferredOutputDeviceChangeCallbackSize() const; 68 int32_t AddPreferredInputDeviceChangeCallback(const AudioCapturerInfo &capturerInfo, 69 const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &cb); 70 int32_t RemovePreferredInputDeviceChangeCallback( 71 const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &cb = nullptr); 72 size_t GetPreferredInputDeviceChangeCallbackSize() const; 73 int32_t AddRendererStateChangeCallback(const std::shared_ptr<AudioRendererStateChangeCallback> &cb); 74 int32_t RemoveRendererStateChangeCallback( 75 const std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> &callbacks); 76 int32_t RemoveRendererStateChangeCallback( 77 const std::shared_ptr<AudioRendererStateChangeCallback> &callback); 78 size_t GetRendererStateChangeCallbackSize() const; 79 int32_t AddCapturerStateChangeCallback(const std::shared_ptr<AudioCapturerStateChangeCallback> &cb); 80 int32_t RemoveCapturerStateChangeCallback(); 81 size_t GetCapturerStateChangeCallbackSize() const; 82 int32_t AddDeviceChangeWithInfoCallback( 83 const uint32_t sessionId, const std::weak_ptr<DeviceChangeWithInfoCallback> &cb); 84 int32_t RemoveDeviceChangeWithInfoCallback(const uint32_t sessionId); 85 size_t GetDeviceChangeWithInfoCallbackkSize() const; 86 int32_t AddMicrophoneBlockedCallback(const int32_t clientId, 87 const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &cb); 88 int32_t RemoveMicrophoneBlockedCallback(const int32_t clientId, 89 const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &cb); 90 size_t GetMicrophoneBlockedCallbackSize() const; 91 int32_t AddHeadTrackingDataRequestedChangeCallback(const std::string &macAddress, 92 const std::shared_ptr<HeadTrackingDataRequestedChangeCallback> &cb); 93 int32_t RemoveHeadTrackingDataRequestedChangeCallback(const std::string &macAddress); 94 size_t GetHeadTrackingDataRequestedChangeCallbackSize() const; 95 int32_t AddSpatializationEnabledChangeCallback(const std::shared_ptr<AudioSpatializationEnabledChangeCallback> &cb); 96 int32_t RemoveSpatializationEnabledChangeCallback(); 97 size_t GetSpatializationEnabledChangeCallbackSize() const; 98 int32_t AddSpatializationEnabledChangeForCurrentDeviceCallback( 99 const std::shared_ptr<AudioSpatializationEnabledChangeForCurrentDeviceCallback> &cb); 100 int32_t RemoveSpatializationEnabledChangeForCurrentDeviceCallback(); 101 size_t GetSpatializationEnabledChangeForCurrentDeviceCallbackSize() const; 102 int32_t AddHeadTrackingEnabledChangeCallback(const std::shared_ptr<AudioHeadTrackingEnabledChangeCallback> &cb); 103 int32_t RemoveHeadTrackingEnabledChangeCallback(); 104 size_t GetHeadTrackingEnabledChangeCallbacSize() const; 105 int32_t AddNnStateChangeCallback(const std::shared_ptr<AudioNnStateChangeCallback> &cb); 106 int32_t RemoveNnStateChangeCallback(); 107 size_t GetNnStateChangeCallbackSize() const; 108 size_t GetFocusInfoChangeCallbackSize() const; 109 int32_t AddAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &cb); 110 int32_t RemoveAudioSessionCallback(); 111 int32_t RemoveAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &cb); 112 size_t GetAudioSessionCallbackSize() const; 113 int32_t AddAudioSceneChangedCallback(const int32_t clientId, 114 const std::shared_ptr<AudioManagerAudioSceneChangedCallback> &cb); 115 int32_t RemoveAudioSceneChangedCallback( 116 const std::shared_ptr<AudioManagerAudioSceneChangedCallback> &cb); 117 size_t GetAudioSceneChangedCallbackSize() const; 118 int32_t SetDistribuitedOutputChangeCallback(const std::shared_ptr<AudioDistribuitedOutputChangeCallback> &cb); 119 120 void OnRecreateRendererStreamEvent(const uint32_t sessionId, const int32_t streamFlag, 121 const AudioStreamDeviceChangeReasonExt reason) override; 122 void OnRecreateCapturerStreamEvent(const uint32_t sessionId, const int32_t streamFlag, 123 const AudioStreamDeviceChangeReasonExt reason) override; 124 void OnVolumeKeyEvent(VolumeEvent volumeEvent) override; 125 void OnAudioFocusInfoChange(const std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList) override; 126 void OnAudioFocusRequested(const AudioInterrupt &requestFocus) override; 127 void OnAudioFocusAbandoned(const AudioInterrupt &abandonFocus) override; 128 void OnDeviceChange(const DeviceChangeAction &deviceChangeAction) override; 129 void OnMicrophoneBlocked(const MicrophoneBlockedInfo µphoneBlockedInfo) override; 130 void OnRingerModeUpdated(const AudioRingerMode &ringerMode) override; 131 void OnAppVolumeChanged(int32_t appUid, const VolumeEvent& volumeEvent) override; 132 void OnMicStateUpdated(const MicStateChangeEvent &micStateChangeEvent) override; 133 void OnPreferredOutputDeviceUpdated(const AudioRendererInfo &rendererInfo, 134 const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc) override; 135 void OnPreferredInputDeviceUpdated(const AudioCapturerInfo &capturerInfo, 136 const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc) override; 137 void OnRendererStateChange( 138 std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos) override; 139 void OnCapturerStateChange( 140 std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos) override; 141 void OnRendererDeviceChange(const uint32_t sessionId, 142 const AudioDeviceDescriptor &deviceInfo, const AudioStreamDeviceChangeReasonExt reason) override; 143 void OnDistribuitedOutputChange(const AudioDeviceDescriptor &deviceDesc, bool isRemote) override; 144 void OnHeadTrackingDeviceChange(const std::unordered_map<std::string, bool> &changeInfo) override; 145 void OnSpatializationEnabledChange(const bool &enabled) override; 146 void OnSpatializationEnabledChangeForAnyDevice(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, 147 const bool &enabled) override; 148 void OnSpatializationEnabledChangeForCurrentDevice(const bool &enabled) override; 149 void OnHeadTrackingEnabledChange(const bool &enabled) override; 150 void OnHeadTrackingEnabledChangeForAnyDevice(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, 151 const bool &enabled) override; 152 void OnNnStateChange(const int32_t &nnState) override; 153 void OnAudioSessionDeactive(const AudioSessionDeactiveEvent &deactiveEvent) override; 154 void OnAudioSceneChange(const AudioScene &audioScene) override; 155 156 private: 157 std::vector<std::shared_ptr<AudioDeviceDescriptor>> DeviceFilterByFlag(DeviceFlag flag, 158 const std::vector<std::shared_ptr<AudioDeviceDescriptor>>& desc); 159 160 std::vector<std::weak_ptr<VolumeKeyEventCallback>> volumeKeyEventCallbackList_; 161 std::vector<std::shared_ptr<AudioFocusInfoChangeCallback>> focusInfoChangeCallbackList_; 162 std::vector<std::pair<DeviceFlag, std::shared_ptr<AudioManagerDeviceChangeCallback>>> deviceChangeCallbackList_; 163 std::vector<std::shared_ptr<AudioRingerModeCallback>> ringerModeCallbackList_; 164 std::vector<std::pair<int32_t, std::shared_ptr< 165 AudioManagerAppVolumeChangeCallback>>> appVolumeChangeForUidCallback_; 166 std::map<int32_t, int32_t> appVolumeChangeForUidCallbackNum; 167 std::vector<std::pair<int32_t, std::shared_ptr<AudioManagerAppVolumeChangeCallback>>> selfAppVolumeChangeCallback_; 168 std::map<int32_t, int32_t> selfAppVolumeChangeCallbackNum_; 169 std::vector<std::shared_ptr<AudioManagerMicStateChangeCallback>> micStateChangeCallbackList_; 170 std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> rendererStateChangeCallbackList_; 171 std::vector<std::weak_ptr<AudioCapturerStateChangeCallback>> capturerStateChangeCallbackList_; 172 std::vector<std::shared_ptr<AudioSpatializationEnabledChangeCallback>> spatializationEnabledChangeCallbackList_; 173 std::vector<std::shared_ptr<AudioSpatializationEnabledChangeForCurrentDeviceCallback>> 174 spatializationEnabledChangeForCurrentDeviceCallbackList_; 175 std::vector<std::shared_ptr<AudioHeadTrackingEnabledChangeCallback>> headTrackingEnabledChangeCallbackList_; 176 std::vector<std::shared_ptr<AudioNnStateChangeCallback>> nnStateChangeCallbackList_; 177 std::vector<std::shared_ptr<AudioSessionCallback>> audioSessionCallbackList_; 178 std::vector<std::pair<int32_t, std::shared_ptr<AudioManagerMicrophoneBlockedCallback>>> 179 microphoneBlockedCallbackList_; 180 std::vector<std::shared_ptr<AudioManagerAudioSceneChangedCallback>> audioSceneChangedCallbackList_; 181 std::vector<std::shared_ptr<AudioDistribuitedOutputChangeCallback>> distribuitedOutputChangeCallback_; 182 183 std::unordered_map<StreamUsage, 184 std::vector<std::shared_ptr<AudioPreferredOutputDeviceChangeCallback>>> preferredOutputDeviceCallbackMap_; 185 std::unordered_map<SourceType, 186 std::vector<std::shared_ptr<AudioPreferredInputDeviceChangeCallback>>> preferredInputDeviceCallbackMap_; 187 188 std::unordered_map<uint32_t, 189 std::weak_ptr<DeviceChangeWithInfoCallback>> deviceChangeWithInfoCallbackMap_; 190 191 std::unordered_map<std::string, 192 std::shared_ptr<HeadTrackingDataRequestedChangeCallback>> headTrackingDataRequestedChangeCallbackMap_; 193 194 mutable std::mutex focusInfoChangeMutex_; 195 mutable std::mutex rendererStateChangeMutex_; 196 mutable std::mutex capturerStateChangeMutex_; 197 mutable std::mutex pOutputDeviceChangeMutex_; 198 mutable std::mutex pInputDeviceChangeMutex_; 199 mutable std::mutex volumeKeyEventMutex_; 200 mutable std::mutex deviceChangeMutex_; 201 mutable std::mutex ringerModeMutex_; 202 mutable std::mutex appVolumeChangeForUidMutex_; 203 mutable std::mutex selfAppVolumeChangeMutex_; 204 mutable std::mutex micStateChangeMutex_; 205 mutable std::mutex deviceChangeWithInfoCallbackMutex_; 206 mutable std::mutex headTrackingDataRequestedChangeMutex_; 207 mutable std::mutex spatializationEnabledChangeMutex_; 208 mutable std::mutex spatializationEnabledChangeForCurrentDeviceMutex_; 209 mutable std::mutex headTrackingEnabledChangeMutex_; 210 mutable std::mutex nnStateChangeMutex_; 211 mutable std::mutex audioSessionMutex_; 212 mutable std::mutex microphoneBlockedMutex_; 213 mutable std::mutex audioSceneChangedMutex_; 214 }; 215 } // namespace AudioStandard 216 } // namespace OHOS 217 #endif // AUDIO_POLICY_CLIENT_STUB_IMPL_H