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 #ifndef AUDIO_POLICY_SERVER_HANDLER_H 16 #define AUDIO_POLICY_SERVER_HANDLER_H 17 #include <mutex> 18 19 #include "singleton.h" 20 #include "event_handler.h" 21 #include "event_runner.h" 22 23 #include "audio_policy_log.h" 24 #include "audio_system_manager.h" 25 #include "istandard_audio_policy_manager_listener.h" 26 #include "i_audio_interrupt_event_dispatcher.h" 27 #include "i_audio_zone_event_dispatcher.h" 28 29 namespace OHOS { 30 namespace AudioStandard { 31 32 class IStandardAudioRoutingManagerListener; 33 class AudioPolicyClientHolder; 34 class AudioPolicyManagerListenerCallback; 35 class AudioPolicyServerHandler : public AppExecFwk::EventHandler { 36 DECLARE_DELAYED_SINGLETON(AudioPolicyServerHandler) 37 public: 38 enum FocusCallbackCategory : int32_t { 39 NONE_CALLBACK_CATEGORY, 40 REQUEST_CALLBACK_CATEGORY, 41 ABANDON_CALLBACK_CATEGORY, 42 }; 43 44 enum EventAudioServerCmd { 45 AUDIO_DEVICE_CHANGE, 46 AVAILABLE_AUDIO_DEVICE_CHANGE, 47 VOLUME_KEY_EVENT, 48 REQUEST_CATEGORY_EVENT, 49 ABANDON_CATEGORY_EVENT, 50 FOCUS_INFOCHANGE, 51 RINGER_MODEUPDATE_EVENT, 52 APP_VOLUME_CHANGE_EVENT, 53 ACTIVE_VOLUME_TYPE_CHANGE_EVENT, 54 MIC_STATE_CHANGE_EVENT, 55 MIC_STATE_CHANGE_EVENT_WITH_CLIENTID, 56 INTERRUPT_EVENT, 57 INTERRUPT_EVENT_WITH_STREAMID, 58 INTERRUPT_EVENT_WITH_CLIENTID, 59 PREFERRED_OUTPUT_DEVICE_UPDATED, 60 PREFERRED_INPUT_DEVICE_UPDATED, 61 DISTRIBUTED_ROUTING_ROLE_CHANGE, 62 RENDERER_INFO_EVENT, 63 CAPTURER_INFO_EVENT, 64 RENDERER_DEVICE_CHANGE_EVENT, 65 ON_CAPTURER_CREATE, 66 ON_CAPTURER_REMOVED, 67 ON_WAKEUP_CLOSE, 68 RECREATE_RENDERER_STREAM_EVENT, 69 RECREATE_CAPTURER_STREAM_EVENT, 70 HEAD_TRACKING_DEVICE_CHANGE, 71 SPATIALIZATION_ENABLED_CHANGE, 72 SPATIALIZATION_ENABLED_CHANGE_FOR_ANY_DEVICE, 73 HEAD_TRACKING_ENABLED_CHANGE, 74 HEAD_TRACKING_ENABLED_CHANGE_FOR_ANY_DEVICE, 75 PIPE_STREAM_CLEAN_EVENT, 76 AUDIO_SESSION_DEACTIVE_EVENT, 77 MICROPHONE_BLOCKED, 78 NN_STATE_CHANGE, 79 AUDIO_SCENE_CHANGE, 80 SPATIALIZATION_ENABLED_CHANGE_FOR_CURRENT_DEVICE, 81 AUDIO_ZONE_EVENT, 82 FORMAT_UNSUPPORTED_ERROR, 83 SESSION_DEVICE_CHANGE, 84 INTERRUPT_EVENT_FOR_AUDIO_SESSION, 85 }; 86 /* event data */ 87 class EventContextObj { 88 public: 89 DeviceChangeAction deviceChangeAction; 90 MicrophoneBlockedInfo microphoneBlockedInfo; 91 VolumeEvent volumeEvent; 92 AudioVolumeType volumeType; 93 AudioInterrupt audioInterrupt; 94 std::list<std::pair<AudioInterrupt, AudioFocuState>> focusInfoList; 95 AudioRingerMode ringMode; 96 MicStateChangeEvent micStateChangeEvent; 97 InterruptEventInternal interruptEvent; 98 uint32_t sessionId; 99 int32_t clientId; 100 std::shared_ptr<AudioDeviceDescriptor> descriptor; 101 CastType type; 102 bool spatializationEnabled; 103 bool headTrackingEnabled; 104 AudioScene audioScene; 105 int32_t nnState; 106 std::vector<std::shared_ptr<AudioRendererChangeInfo>> audioRendererChangeInfos; 107 std::vector<std::shared_ptr<AudioCapturerChangeInfo>> audioCapturerChangeInfos; 108 int32_t streamFlag; 109 int32_t appUid; 110 std::unordered_map<std::string, bool> headTrackingDeviceChangeInfo; 111 AudioStreamDeviceChangeReasonExt reason_ = AudioStreamDeviceChangeReasonExt::ExtEnum::UNKNOWN; 112 std::pair<int32_t, AudioSessionDeactiveEvent> sessionDeactivePair; 113 std::shared_ptr<AudioZoneEvent> audioZoneEvent; 114 uint32_t routeFlag; 115 AudioErrors errorCode; 116 int32_t callerPid_ = -1; 117 }; 118 119 struct RendererDeviceChangeEvent { 120 RendererDeviceChangeEvent() = delete; RendererDeviceChangeEventRendererDeviceChangeEvent121 RendererDeviceChangeEvent(const int32_t clientPid, const uint32_t sessionId, 122 const AudioDeviceDescriptor outputDeviceInfo, const AudioStreamDeviceChangeReason &reason) 123 : clientPid_(clientPid), sessionId_(sessionId), outputDeviceInfo_(outputDeviceInfo), reason_(reason) 124 {} 125 126 const int32_t clientPid_; 127 const uint32_t sessionId_; 128 const AudioDeviceDescriptor outputDeviceInfo_ = AudioDeviceDescriptor(AudioDeviceDescriptor::DEVICE_INFO); 129 AudioStreamDeviceChangeReasonExt reason_ = AudioStreamDeviceChangeReasonExt::ExtEnum::UNKNOWN; 130 }; 131 132 struct CapturerCreateEvent { 133 CapturerCreateEvent() = delete; CapturerCreateEventCapturerCreateEvent134 CapturerCreateEvent(const AudioCapturerInfo &capturerInfo, const AudioStreamInfo &streamInfo, 135 uint64_t sessionId, int32_t error) 136 : capturerInfo_(capturerInfo), streamInfo_(streamInfo), sessionId_(sessionId), error_(error) 137 {} 138 AudioCapturerInfo capturerInfo_; 139 AudioStreamInfo streamInfo_; 140 uint64_t sessionId_; 141 int32_t error_; 142 }; 143 144 void Init(std::shared_ptr<IAudioInterruptEventDispatcher> dispatcher); 145 146 void AddAudioPolicyClientProxyMap(int32_t clientPid, const std::shared_ptr<AudioPolicyClientHolder> &cb); 147 void RemoveAudioPolicyClientProxyMap(pid_t clientPid); 148 void AddExternInterruptCbsMap(int32_t clientId, const std::shared_ptr<AudioInterruptCallback> &callback); 149 int32_t RemoveExternInterruptCbsMap(int32_t clientId); 150 void AddAvailableDeviceChangeMap(int32_t clientId, const AudioDeviceUsage usage, 151 const std::shared_ptr<AudioPolicyManagerListenerCallback> &callback); 152 void RemoveAvailableDeviceChangeMap(const int32_t clientId, AudioDeviceUsage usage); 153 void AddDistributedRoutingRoleChangeCbsMap(int32_t clientId, 154 const sptr<IStandardAudioRoutingManagerListener> &callback); 155 int32_t RemoveDistributedRoutingRoleChangeCbsMap(int32_t clientId); 156 bool SendDeviceChangedCallback(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc, bool isConnected); 157 bool SendAvailableDeviceChange(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc, bool isConnected); 158 bool SendMicrophoneBlockedCallback(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc, 159 DeviceBlockStatus status); 160 void HandleMicrophoneBlockedCallback(const AppExecFwk::InnerEvent::Pointer &event); 161 bool SendVolumeKeyEventCallback(const VolumeEvent &volumeEvent); 162 bool SendAudioFocusInfoChangeCallback(int32_t callbackCategory, const AudioInterrupt &audioInterrupt, 163 const std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList); 164 bool SendRingerModeUpdatedCallback(const AudioRingerMode &ringMode); 165 bool SendActiveVolumeTypeChangeCallback(const AudioVolumeType &volumeType); 166 bool SendAppVolumeChangeCallback(int32_t appUid, const VolumeEvent &volumeEvent); 167 bool SendMicStateUpdatedCallback(const MicStateChangeEvent &micStateChangeEvent); 168 bool SendMicStateWithClientIdCallback(const MicStateChangeEvent &micStateChangeEvent, int32_t clientId); 169 bool SendInterruptEventInternalCallback(const InterruptEventInternal &interruptEvent); 170 bool SendInterruptEventWithStreamIdCallback(const InterruptEventInternal &interruptEvent, 171 const uint32_t &streamId); 172 bool SendInterruptEventCallbackForAudioSession(const InterruptEventInternal &interruptEvent, 173 const AudioInterrupt &audioInterrupt); 174 bool SendInterruptEventWithClientIdCallback(const InterruptEventInternal &interruptEvent, 175 const int32_t &clientId); 176 bool SendPreferredOutputDeviceUpdated(); 177 bool SendPreferredInputDeviceUpdated(); 178 bool SendDistributedRoutingRoleChange(const std::shared_ptr<AudioDeviceDescriptor> descriptor, 179 const CastType &type); 180 bool SendRendererInfoEvent(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos); 181 bool SendCapturerInfoEvent(const std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos); 182 bool SendRendererDeviceChangeEvent(const int32_t clientPid, const uint32_t sessionId, 183 const AudioDeviceDescriptor &outputDeviceInfo, const AudioStreamDeviceChangeReasonExt reason); 184 bool SendCapturerCreateEvent(AudioCapturerInfo capturerInfo, AudioStreamInfo streamInfo, 185 uint64_t sessionId, bool isSync, int32_t &error); 186 bool SendCapturerRemovedEvent(uint64_t sessionId, bool isSync); 187 bool SendWakeupCloseEvent(bool isSync); 188 bool SendRecreateRendererStreamEvent(int32_t clientId, uint32_t sessionID, uint32_t routeFlag, 189 const AudioStreamDeviceChangeReasonExt reason); 190 bool SendRecreateCapturerStreamEvent(int32_t clientId, uint32_t sessionID, uint32_t routeFlag, 191 const AudioStreamDeviceChangeReasonExt reason); 192 bool SendHeadTrackingDeviceChangeEvent(const std::unordered_map<std::string, bool> &changeInfo); 193 void AddAudioDeviceRefinerCb(const sptr<IStandardAudioRoutingManagerListener> &callback); 194 int32_t RemoveAudioDeviceRefinerCb(); 195 bool SendSpatializatonEnabledChangeEvent(const bool &enabled); 196 bool SendSpatializatonEnabledChangeForAnyDeviceEvent( 197 const std::shared_ptr<AudioDeviceDescriptor> &selectedAudioDevice, const bool &enabled); 198 bool SendSpatializatonEnabledChangeForCurrentDeviceEvent(const bool &enabled); 199 bool SendHeadTrackingEnabledChangeEvent(const bool &enabled); 200 bool SendHeadTrackingEnabledChangeForAnyDeviceEvent( 201 const std::shared_ptr<AudioDeviceDescriptor> &selectedAudioDevice, const bool &enabled); 202 bool SendPipeStreamCleanEvent(AudioPipeType pipeType); 203 int32_t SetClientCallbacksEnable(const CallbackChange &callbackchange, const bool &enable); 204 int32_t SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo); 205 int32_t SetCallbackCapturerInfo(const AudioCapturerInfo &capturerInfo); 206 bool SendAudioSceneChangeEvent(const AudioScene &audioScene); 207 bool SendAudioSessionDeactiveCallback(const std::pair<int32_t, AudioSessionDeactiveEvent> &sessionDeactivePair); 208 bool SendNnStateChangeCallback(const int32_t &state); 209 void SetAudioZoneEventDispatcher(const std::shared_ptr<IAudioZoneEventDispatcher> dispatcher); 210 bool SendAudioZoneEvent(std::shared_ptr<AudioZoneEvent> event); 211 bool SendFormatUnsupportedErrorEvent(const AudioErrors &errorCode); 212 int32_t SetCallbackStreamUsageInfo(const std::set<StreamUsage> &streamUsages); 213 bool SendAudioSessionDeviceChange(const AudioStreamDeviceChangeReason changeReason, int32_t callerPid = -1); 214 215 protected: 216 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 217 218 private: 219 /* Handle Event*/ 220 void HandleDeviceChangedCallback(const AppExecFwk::InnerEvent::Pointer &event); 221 void HandleAvailableDeviceChange(const AppExecFwk::InnerEvent::Pointer &event); 222 void HandleVolumeKeyEvent(const AppExecFwk::InnerEvent::Pointer &event); 223 void HandleRequestCateGoryEvent(const AppExecFwk::InnerEvent::Pointer &event); 224 void HandleAbandonCateGoryEvent(const AppExecFwk::InnerEvent::Pointer &event); 225 void HandleFocusInfoChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 226 void HandleRingerModeUpdatedEvent(const AppExecFwk::InnerEvent::Pointer &event); 227 void HandleMicStateUpdatedEvent(const AppExecFwk::InnerEvent::Pointer &event); 228 void HandleMicStateUpdatedEventWithClientId(const AppExecFwk::InnerEvent::Pointer &event); 229 void HandleInterruptEvent(const AppExecFwk::InnerEvent::Pointer &event); 230 void HandleInterruptEventForAudioSession(const AppExecFwk::InnerEvent::Pointer &event); 231 void HandleInterruptEventWithStreamId(const AppExecFwk::InnerEvent::Pointer &event); 232 void HandleInterruptEventWithClientId(const AppExecFwk::InnerEvent::Pointer &event); 233 void HandlePreferredOutputDeviceUpdated(); 234 void HandlePreferredInputDeviceUpdated(); 235 void HandleDistributedRoutingRoleChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 236 void HandleAudioSessionDeviceChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 237 void HandleRendererInfoEvent(const AppExecFwk::InnerEvent::Pointer &event); 238 void HandleCapturerInfoEvent(const AppExecFwk::InnerEvent::Pointer &event); 239 void HandleRendererDeviceChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 240 void HandleCapturerCreateEvent(const AppExecFwk::InnerEvent::Pointer &event); 241 void HandleCapturerRemovedEvent(const AppExecFwk::InnerEvent::Pointer &event); 242 void HandleWakeupCloseEvent(const AppExecFwk::InnerEvent::Pointer &event); 243 void HandleSendRecreateRendererStreamEvent(const AppExecFwk::InnerEvent::Pointer &event); 244 void HandleSendRecreateCapturerStreamEvent(const AppExecFwk::InnerEvent::Pointer &event); 245 void HandleHeadTrackingDeviceChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 246 void HandleSpatializatonEnabledChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 247 void HandleSpatializatonEnabledChangeForAnyDeviceEvent(const AppExecFwk::InnerEvent::Pointer &event); 248 void HandleSpatializatonEnabledChangeForCurrentDeviceEvent(const AppExecFwk::InnerEvent::Pointer &event); 249 void HandleHeadTrackingEnabledChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 250 void HandleHeadTrackingEnabledChangeForAnyDeviceEvent(const AppExecFwk::InnerEvent::Pointer &event); 251 void HandlePipeStreamCleanEvent(const AppExecFwk::InnerEvent::Pointer &event); 252 void HandleAudioSessionDeactiveCallback(const AppExecFwk::InnerEvent::Pointer &event); 253 void HandleNnStateChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 254 void HandleAudioSceneChange(const AppExecFwk::InnerEvent::Pointer &event); 255 void HandleAppVolumeChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 256 void HandleAudioZoneEvent(const AppExecFwk::InnerEvent::Pointer &event); 257 void HandleFormatUnsupportedErrorEvent(const AppExecFwk::InnerEvent::Pointer &event); 258 void HandleActiveVolumeTypeChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 259 260 void HandleServiceEvent(const uint32_t &eventId, const AppExecFwk::InnerEvent::Pointer &event); 261 262 void HandleOtherServiceEvent(const uint32_t &eventId, const AppExecFwk::InnerEvent::Pointer &event); 263 264 void HandleVolumeChangeCallback(int32_t clientId, std::shared_ptr<AudioPolicyClientHolder> audioPolicyClient, 265 const VolumeEvent &volumeEvent); 266 267 void HandleVolumeKeyEventToRssWhenAccountsChange(std::shared_ptr<EventContextObj> &eventContextObj); 268 269 std::vector<AudioRendererInfo> GetCallbackRendererInfoList(int32_t clientPid); 270 std::vector<AudioCapturerInfo> GetCallbackCapturerInfoList(int32_t clientPid); 271 272 std::mutex runnerMutex_; 273 std::mutex handleMapMutex_; 274 std::mutex clientCbRendererInfoMapMutex_; 275 std::mutex clientCbCapturerInfoMapMutex_; 276 std::mutex clientCbStreamUsageMapMutex_; 277 std::weak_ptr<IAudioInterruptEventDispatcher> interruptEventDispatcher_; 278 std::weak_ptr<IAudioZoneEventDispatcher> audioZoneEventDispatcher_; 279 280 std::unordered_map<int32_t, std::shared_ptr<AudioPolicyClientHolder>> audioPolicyClientProxyAPSCbsMap_; 281 std::string pidsStrForPrinting_ = "[]"; 282 283 std::unordered_map<int32_t, std::shared_ptr<AudioInterruptCallback>> amInterruptCbsMap_; 284 std::map<std::pair<int32_t, AudioDeviceUsage>, 285 std::shared_ptr<AudioPolicyManagerListenerCallback>> availableDeviceChangeCbsMap_; 286 std::unordered_map<int32_t, sptr<IStandardAudioRoutingManagerListener>> distributedRoutingRoleChangeCbsMap_; 287 std::unordered_map<int32_t, std::unordered_map<CallbackChange, bool>> clientCallbacksMap_; 288 int32_t pidOfRss_ = -1; 289 std::unordered_map<int32_t, std::vector<AudioRendererInfo>> clientCbRendererInfoMap_; 290 std::unordered_map<int32_t, std::vector<AudioCapturerInfo>> clientCbCapturerInfoMap_; 291 std::unordered_map<int32_t, std::set<StreamUsage>> clientCbStreamUsageMap_; 292 }; 293 } // namespace AudioStandard 294 } // namespace OHOS 295 #endif // AUDIO_POLICY_SERVER_HANDLER_H 296