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 "audio_policy_client.h" 26 #include "i_standard_concurrency_state_listener.h" 27 #include "i_standard_audio_policy_manager_listener.h" 28 #include "i_standard_audio_routing_manager_listener.h" 29 #include "i_audio_interrupt_event_dispatcher.h" 30 #include "i_audio_concurrency_event_dispatcher.h" 31 32 namespace OHOS { 33 namespace AudioStandard { 34 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 MIC_STATE_CHANGE_EVENT, 54 MIC_STATE_CHANGE_EVENT_WITH_CLIENTID, 55 INTERRUPT_EVENT, 56 INTERRUPT_EVENT_WITH_STREAMID, 57 INTERRUPT_EVENT_WITH_CLIENTID, 58 PREFERRED_OUTPUT_DEVICE_UPDATED, 59 PREFERRED_INPUT_DEVICE_UPDATED, 60 DISTRIBUTED_ROUTING_ROLE_CHANGE, 61 RENDERER_INFO_EVENT, 62 CAPTURER_INFO_EVENT, 63 RENDERER_DEVICE_CHANGE_EVENT, 64 ON_CAPTURER_CREATE, 65 ON_CAPTURER_REMOVED, 66 ON_WAKEUP_CLOSE, 67 RECREATE_RENDERER_STREAM_EVENT, 68 RECREATE_CAPTURER_STREAM_EVENT, 69 HEAD_TRACKING_DEVICE_CHANGE, 70 SPATIALIZATION_ENABLED_CHANGE, 71 SPATIALIZATION_ENABLED_CHANGE_FOR_ANY_DEVICE, 72 HEAD_TRACKING_ENABLED_CHANGE, 73 HEAD_TRACKING_ENABLED_CHANGE_FOR_ANY_DEVICE, 74 PIPE_STREAM_CLEAN_EVENT, 75 CONCURRENCY_EVENT_WITH_SESSIONID, 76 AUDIO_SESSION_DEACTIVE_EVENT, 77 MICROPHONE_BLOCKED, 78 NN_STATE_CHANGE, 79 AUDIO_SCENE_CHANGE, 80 SPATIALIZATION_ENABLED_CHANGE_FOR_CURRENT_DEVICE, 81 DISTRIBUTED_OUTPUT_CHANGE, 82 }; 83 /* event data */ 84 class EventContextObj { 85 public: 86 DeviceChangeAction deviceChangeAction; 87 MicrophoneBlockedInfo microphoneBlockedInfo; 88 VolumeEvent volumeEvent; 89 AudioInterrupt audioInterrupt; 90 std::list<std::pair<AudioInterrupt, AudioFocuState>> focusInfoList; 91 AudioRingerMode ringMode; 92 MicStateChangeEvent micStateChangeEvent; 93 InterruptEventInternal interruptEvent; 94 uint32_t sessionId; 95 int32_t clientId; 96 std::shared_ptr<AudioDeviceDescriptor> descriptor; 97 CastType type; 98 bool spatializationEnabled; 99 bool headTrackingEnabled; 100 AudioScene audioScene; 101 int32_t nnState; 102 std::vector<std::shared_ptr<AudioRendererChangeInfo>> audioRendererChangeInfos; 103 std::vector<std::shared_ptr<AudioCapturerChangeInfo>> audioCapturerChangeInfos; 104 int32_t streamFlag; 105 int32_t appUid; 106 std::unordered_map<std::string, bool> headTrackingDeviceChangeInfo; 107 AudioStreamDeviceChangeReasonExt reason_ = AudioStreamDeviceChangeReasonExt::ExtEnum::UNKNOWN; 108 std::pair<int32_t, AudioSessionDeactiveEvent> sessionDeactivePair; 109 }; 110 111 struct RendererDeviceChangeEvent { 112 RendererDeviceChangeEvent() = delete; RendererDeviceChangeEventRendererDeviceChangeEvent113 RendererDeviceChangeEvent(const int32_t clientPid, const uint32_t sessionId, 114 const AudioDeviceDescriptor outputDeviceInfo, const AudioStreamDeviceChangeReason &reason) 115 : clientPid_(clientPid), sessionId_(sessionId), outputDeviceInfo_(outputDeviceInfo), reason_(reason) 116 {} 117 118 const int32_t clientPid_; 119 const uint32_t sessionId_; 120 const AudioDeviceDescriptor outputDeviceInfo_ = AudioDeviceDescriptor(AudioDeviceDescriptor::DEVICE_INFO); 121 AudioStreamDeviceChangeReasonExt reason_ = AudioStreamDeviceChangeReasonExt::ExtEnum::UNKNOWN; 122 }; 123 124 struct DistributedOutputChangeEvent { 125 DistributedOutputChangeEvent() = delete; DistributedOutputChangeEventDistributedOutputChangeEvent126 DistributedOutputChangeEvent(const AudioDeviceDescriptor &deviceDesc, const bool isRemote) 127 : deviceDesc_(deviceDesc), isRemote_(isRemote) {} 128 129 const AudioDeviceDescriptor &deviceDesc_; 130 const bool isRemote_; 131 }; 132 133 struct CapturerCreateEvent { 134 CapturerCreateEvent() = delete; CapturerCreateEventCapturerCreateEvent135 CapturerCreateEvent(const AudioCapturerInfo &capturerInfo, const AudioStreamInfo &streamInfo, 136 uint64_t sessionId, int32_t error) 137 : capturerInfo_(capturerInfo), streamInfo_(streamInfo), sessionId_(sessionId), error_(error) 138 {} 139 AudioCapturerInfo capturerInfo_; 140 AudioStreamInfo streamInfo_; 141 uint64_t sessionId_; 142 int32_t error_; 143 }; 144 145 void Init(std::shared_ptr<IAudioInterruptEventDispatcher> dispatcher); 146 147 void AddAudioPolicyClientProxyMap(int32_t clientPid, const sptr<IAudioPolicyClient> &cb); 148 void RemoveAudioPolicyClientProxyMap(pid_t clientPid); 149 void AddExternInterruptCbsMap(int32_t clientId, const std::shared_ptr<AudioInterruptCallback> &callback); 150 int32_t RemoveExternInterruptCbsMap(int32_t clientId); 151 void AddAvailableDeviceChangeMap(int32_t clientId, const AudioDeviceUsage usage, 152 const sptr<IStandardAudioPolicyManagerListener> &callback); 153 void RemoveAvailableDeviceChangeMap(const int32_t clientId, AudioDeviceUsage usage); 154 void AddDistributedRoutingRoleChangeCbsMap(int32_t clientId, 155 const sptr<IStandardAudioRoutingManagerListener> &callback); 156 int32_t RemoveDistributedRoutingRoleChangeCbsMap(int32_t clientId); 157 void AddConcurrencyEventDispatcher(std::shared_ptr<IAudioConcurrencyEventDispatcher> dispatcher); 158 bool SendDeviceChangedCallback(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc, bool isConnected); 159 bool SendAvailableDeviceChange(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc, bool isConnected); 160 bool SendMicrophoneBlockedCallback(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc, 161 DeviceBlockStatus status); 162 void HandleMicrophoneBlockedCallback(const AppExecFwk::InnerEvent::Pointer &event); 163 bool SendVolumeKeyEventCallback(const VolumeEvent &volumeEvent); 164 bool SendAudioFocusInfoChangeCallback(int32_t callbackCategory, const AudioInterrupt &audioInterrupt, 165 const std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList); 166 bool SendRingerModeUpdatedCallback(const AudioRingerMode &ringMode); 167 bool SendAppVolumeChangeCallback(int32_t appUid, const VolumeEvent &volumeEvent); 168 bool SendMicStateUpdatedCallback(const MicStateChangeEvent &micStateChangeEvent); 169 bool SendMicStateWithClientIdCallback(const MicStateChangeEvent &micStateChangeEvent, int32_t clientId); 170 bool SendInterruptEventInternalCallback(const InterruptEventInternal &interruptEvent); 171 bool SendInterruptEventWithStreamIdCallback(const InterruptEventInternal &interruptEvent, 172 const uint32_t &streamId); 173 bool SendInterruptEventWithClientIdCallback(const InterruptEventInternal &interruptEvent, 174 const int32_t &clientId); 175 bool SendPreferredOutputDeviceUpdated(); 176 bool SendPreferredInputDeviceUpdated(); 177 bool SendDistributedRoutingRoleChange(const std::shared_ptr<AudioDeviceDescriptor> descriptor, 178 const CastType &type); 179 bool SendRendererInfoEvent(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos); 180 bool SendCapturerInfoEvent(const std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos); 181 bool SendRendererDeviceChangeEvent(const int32_t clientPid, const uint32_t sessionId, 182 const AudioDeviceDescriptor &outputDeviceInfo, const AudioStreamDeviceChangeReasonExt reason); 183 bool SendDistribuitedOutputChangeEvent(const AudioDeviceDescriptor &desc, bool isRemote); 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, int32_t streamFlag, 189 const AudioStreamDeviceChangeReasonExt reason); 190 bool SendRecreateCapturerStreamEvent(int32_t clientId, uint32_t sessionID, int32_t streamFlag, 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 bool SendConcurrencyEventWithSessionIDCallback(const uint32_t sessionID); 204 int32_t SetClientCallbacksEnable(const CallbackChange &callbackchange, const bool &enable); 205 int32_t SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo); 206 int32_t SetCallbackCapturerInfo(const AudioCapturerInfo &capturerInfo); 207 bool SendAudioSceneChangeEvent(const AudioScene &audioScene); 208 bool SendAudioSessionDeactiveCallback(const std::pair<int32_t, AudioSessionDeactiveEvent> &sessionDeactivePair); 209 bool SendNnStateChangeCallback(const int32_t &state); 210 211 protected: 212 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 213 214 private: 215 /* Handle Event*/ 216 void HandleDeviceChangedCallback(const AppExecFwk::InnerEvent::Pointer &event); 217 void HandleAvailableDeviceChange(const AppExecFwk::InnerEvent::Pointer &event); 218 void HandleVolumeKeyEvent(const AppExecFwk::InnerEvent::Pointer &event); 219 void HandleRequestCateGoryEvent(const AppExecFwk::InnerEvent::Pointer &event); 220 void HandleAbandonCateGoryEvent(const AppExecFwk::InnerEvent::Pointer &event); 221 void HandleFocusInfoChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 222 void HandleRingerModeUpdatedEvent(const AppExecFwk::InnerEvent::Pointer &event); 223 void HandleMicStateUpdatedEvent(const AppExecFwk::InnerEvent::Pointer &event); 224 void HandleMicStateUpdatedEventWithClientId(const AppExecFwk::InnerEvent::Pointer &event); 225 void HandleInterruptEvent(const AppExecFwk::InnerEvent::Pointer &event); 226 void HandleInterruptEventWithStreamId(const AppExecFwk::InnerEvent::Pointer &event); 227 void HandleInterruptEventWithClientId(const AppExecFwk::InnerEvent::Pointer &event); 228 void HandlePreferredOutputDeviceUpdated(); 229 void HandlePreferredInputDeviceUpdated(); 230 void HandleDistributedRoutingRoleChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 231 void HandleRendererInfoEvent(const AppExecFwk::InnerEvent::Pointer &event); 232 void HandleCapturerInfoEvent(const AppExecFwk::InnerEvent::Pointer &event); 233 void HandleRendererDeviceChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 234 void HandleDistributedOutputChange(const AppExecFwk::InnerEvent::Pointer &event); 235 void HandleCapturerCreateEvent(const AppExecFwk::InnerEvent::Pointer &event); 236 void HandleCapturerRemovedEvent(const AppExecFwk::InnerEvent::Pointer &event); 237 void HandleWakeupCloseEvent(const AppExecFwk::InnerEvent::Pointer &event); 238 void HandleSendRecreateRendererStreamEvent(const AppExecFwk::InnerEvent::Pointer &event); 239 void HandleSendRecreateCapturerStreamEvent(const AppExecFwk::InnerEvent::Pointer &event); 240 void HandleHeadTrackingDeviceChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 241 void HandleSpatializatonEnabledChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 242 void HandleSpatializatonEnabledChangeForAnyDeviceEvent(const AppExecFwk::InnerEvent::Pointer &event); 243 void HandleSpatializatonEnabledChangeForCurrentDeviceEvent(const AppExecFwk::InnerEvent::Pointer &event); 244 void HandleHeadTrackingEnabledChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 245 void HandleHeadTrackingEnabledChangeForAnyDeviceEvent(const AppExecFwk::InnerEvent::Pointer &event); 246 void HandlePipeStreamCleanEvent(const AppExecFwk::InnerEvent::Pointer &event); 247 void HandleConcurrencyEventWithSessionID(const AppExecFwk::InnerEvent::Pointer &event); 248 void HandleAudioSessionDeactiveCallback(const AppExecFwk::InnerEvent::Pointer &event); 249 void HandleNnStateChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 250 void HandleAudioSceneChange(const AppExecFwk::InnerEvent::Pointer &event); 251 void HandleAppVolumeChangeEvent(const AppExecFwk::InnerEvent::Pointer &event); 252 void HandleServiceEvent(const uint32_t &eventId, const AppExecFwk::InnerEvent::Pointer &event); 253 254 void HandleOtherServiceEvent(const uint32_t &eventId, const AppExecFwk::InnerEvent::Pointer &event); 255 256 std::vector<AudioRendererInfo> GetCallbackRendererInfoList(int32_t clientPid); 257 std::vector<AudioCapturerInfo> GetCallbackCapturerInfoList(int32_t clientPid); 258 259 std::mutex runnerMutex_; 260 std::mutex handleMapMutex_; 261 std::mutex clientCbRendererInfoMapMutex_; 262 std::mutex clientCbCapturerInfoMapMutex_; 263 std::weak_ptr<IAudioInterruptEventDispatcher> interruptEventDispatcher_; 264 std::weak_ptr<IAudioConcurrencyEventDispatcher> concurrencyEventDispatcher_; 265 266 std::unordered_map<int32_t, sptr<IAudioPolicyClient>> audioPolicyClientProxyAPSCbsMap_; 267 std::string pidsStrForPrinting_ = "[]"; 268 269 std::unordered_map<int32_t, std::shared_ptr<AudioInterruptCallback>> amInterruptCbsMap_; 270 std::map<std::pair<int32_t, AudioDeviceUsage>, 271 sptr<IStandardAudioPolicyManagerListener>> availableDeviceChangeCbsMap_; 272 std::unordered_map<int32_t, sptr<IStandardAudioRoutingManagerListener>> distributedRoutingRoleChangeCbsMap_; 273 std::unordered_map<int32_t, std::unordered_map<CallbackChange, bool>> clientCallbacksMap_; 274 std::unordered_map<int32_t, std::vector<AudioRendererInfo>> clientCbRendererInfoMap_; 275 std::unordered_map<int32_t, std::vector<AudioCapturerInfo>> clientCbCapturerInfoMap_; 276 }; 277 } // namespace AudioStandard 278 } // namespace OHOS 279 #endif // AUDIO_POLICY_SERVER_HANDLER_H 280