1 /*
2  * Copyright (c) 2023-2024 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_info.h"
25 #include "audio_system_manager.h"
26 #include "audio_policy_client.h"
27 #include "i_standard_concurrency_state_listener.h"
28 #include "i_standard_audio_policy_manager_listener.h"
29 #include "i_standard_audio_routing_manager_listener.h"
30 #include "i_audio_interrupt_event_dispatcher.h"
31 #include "i_audio_concurrency_event_dispatcher.h"
32 
33 namespace OHOS {
34 namespace AudioStandard {
35 
36 class AudioPolicyServerHandler : public AppExecFwk::EventHandler {
37     DECLARE_DELAYED_SINGLETON(AudioPolicyServerHandler)
38 public:
39     enum FocusCallbackCategory : int32_t {
40         NONE_CALLBACK_CATEGORY,
41         REQUEST_CALLBACK_CATEGORY,
42         ABANDON_CALLBACK_CATEGORY,
43     };
44 
45     enum EventAudioServerCmd {
46         AUDIO_DEVICE_CHANGE,
47         AVAILABLE_AUDIO_DEVICE_CHANGE,
48         VOLUME_KEY_EVENT,
49         REQUEST_CATEGORY_EVENT,
50         ABANDON_CATEGORY_EVENT,
51         FOCUS_INFOCHANGE,
52         RINGER_MODEUPDATE_EVENT,
53         MIC_STATE_CHANGE_EVENT,
54         MIC_STATE_CHANGE_EVENT_WITH_CLIENTID,
55         INTERRUPT_EVENT,
56         INTERRUPT_EVENT_WITH_SESSIONID,
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         DATABASE_UPDATE,
74         HEAD_TRACKING_ENABLED_CHANGE_FOR_ANY_DEVICE,
75         PIPE_STREAM_CLEAN_EVENT,
76         CONCURRENCY_EVENT_WITH_SESSIONID,
77         AUDIO_SESSION_DEACTIVE_EVENT,
78         MICROPHONE_BLOCKED,
79     };
80     /* event data */
81     class EventContextObj {
82     public:
83         DeviceChangeAction deviceChangeAction;
84         MicrophoneBlockedInfo microphoneBlockedInfo;
85         VolumeEvent volumeEvent;
86         AudioInterrupt audioInterrupt;
87         std::list<std::pair<AudioInterrupt, AudioFocuState>> focusInfoList;
88         AudioRingerMode ringMode;
89         MicStateChangeEvent micStateChangeEvent;
90         InterruptEventInternal interruptEvent;
91         uint32_t sessionId;
92         int32_t clientId;
93         sptr<AudioDeviceDescriptor> descriptor;
94         CastType type;
95         bool spatializationEnabled;
96         bool headTrackingEnabled;
97         std::vector<std::unique_ptr<AudioRendererChangeInfo>> audioRendererChangeInfos;
98         std::vector<std::unique_ptr<AudioCapturerChangeInfo>> audioCapturerChangeInfos;
99         int32_t streamFlag;
100         std::unordered_map<std::string, bool> headTrackingDeviceChangeInfo;
101         AudioStreamDeviceChangeReasonExt reason_ = AudioStreamDeviceChangeReasonExt::ExtEnum::UNKNOWN;
102         std::pair<int32_t, AudioSessionDeactiveEvent> sessionDeactivePair;
103     };
104 
105     struct RendererDeviceChangeEvent {
106         RendererDeviceChangeEvent() = delete;
RendererDeviceChangeEventRendererDeviceChangeEvent107         RendererDeviceChangeEvent(const int32_t clientPid, const uint32_t sessionId,
108             const DeviceInfo outputDeviceInfo, const AudioStreamDeviceChangeReason &reason)
109             : clientPid_(clientPid), sessionId_(sessionId), outputDeviceInfo_(outputDeviceInfo), reason_(reason)
110         {}
111 
112         const int32_t clientPid_;
113         const uint32_t sessionId_;
114         const DeviceInfo outputDeviceInfo_;
115         AudioStreamDeviceChangeReasonExt reason_ = AudioStreamDeviceChangeReasonExt::ExtEnum::UNKNOWN;
116     };
117 
118     struct CapturerCreateEvent {
119         CapturerCreateEvent() = delete;
CapturerCreateEventCapturerCreateEvent120         CapturerCreateEvent(const AudioCapturerInfo &capturerInfo, const AudioStreamInfo &streamInfo,
121             uint64_t sessionId, int32_t error)
122             : capturerInfo_(capturerInfo), streamInfo_(streamInfo), sessionId_(sessionId), error_(error)
123         {}
124         AudioCapturerInfo capturerInfo_;
125         AudioStreamInfo streamInfo_;
126         uint64_t sessionId_;
127         int32_t error_;
128     };
129 
130     void Init(std::shared_ptr<IAudioInterruptEventDispatcher> dispatcher);
131 
132     void AddAudioPolicyClientProxyMap(int32_t clientPid, const sptr<IAudioPolicyClient> &cb);
133     void RemoveAudioPolicyClientProxyMap(pid_t clientPid);
134     void AddExternInterruptCbsMap(int32_t clientId, const std::shared_ptr<AudioInterruptCallback> &callback);
135     int32_t RemoveExternInterruptCbsMap(int32_t clientId);
136     void AddAvailableDeviceChangeMap(int32_t clientId, const AudioDeviceUsage usage,
137         const sptr<IStandardAudioPolicyManagerListener> &callback);
138     void RemoveAvailableDeviceChangeMap(const int32_t clientId, AudioDeviceUsage usage);
139     void AddDistributedRoutingRoleChangeCbsMap(int32_t clientId,
140         const sptr<IStandardAudioRoutingManagerListener> &callback);
141     int32_t RemoveDistributedRoutingRoleChangeCbsMap(int32_t clientId);
142     void AddConcurrencyEventDispatcher(std::shared_ptr<IAudioConcurrencyEventDispatcher> dispatcher);
143     bool SendDeviceChangedCallback(const std::vector<sptr<AudioDeviceDescriptor>> &desc, bool isConnected);
144     bool SendAvailableDeviceChange(const std::vector<sptr<AudioDeviceDescriptor>> &desc, bool isConnected);
145     bool SendMicrophoneBlockedCallback(const std::vector<sptr<AudioDeviceDescriptor>> &desc,
146         DeviceBlockStatus status);
147     void HandleMicrophoneBlockedCallback(const AppExecFwk::InnerEvent::Pointer &event);
148     bool SendVolumeKeyEventCallback(const VolumeEvent &volumeEvent);
149     bool SendAudioFocusInfoChangeCallback(int32_t callbackCategory, const AudioInterrupt &audioInterrupt,
150         const std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList);
151     bool SendRingerModeUpdatedCallback(const AudioRingerMode &ringMode);
152     bool SendMicStateUpdatedCallback(const MicStateChangeEvent &micStateChangeEvent);
153     bool SendMicStateWithClientIdCallback(const MicStateChangeEvent &micStateChangeEvent, int32_t clientId);
154     bool SendInterruptEventInternalCallback(const InterruptEventInternal &interruptEvent);
155     bool SendInterruptEventWithSessionIdCallback(const InterruptEventInternal &interruptEvent,
156         const uint32_t &sessionId);
157     bool SendInterruptEventWithClientIdCallback(const InterruptEventInternal &interruptEvent,
158         const int32_t &clientId);
159     bool SendPreferredOutputDeviceUpdated();
160     bool SendPreferredInputDeviceUpdated();
161     bool SendDistributedRoutingRoleChange(const sptr<AudioDeviceDescriptor> descriptor,
162         const CastType &type);
163     bool SendRendererInfoEvent(const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos);
164     bool SendCapturerInfoEvent(const std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos);
165     bool SendRendererDeviceChangeEvent(const int32_t clientPid, const uint32_t sessionId,
166         const DeviceInfo &outputDeviceInfo, const AudioStreamDeviceChangeReasonExt reason);
167     bool SendCapturerCreateEvent(AudioCapturerInfo capturerInfo, AudioStreamInfo streamInfo,
168         uint64_t sessionId, bool isSync, int32_t &error);
169     bool SendCapturerRemovedEvent(uint64_t sessionId, bool isSync);
170     bool SendWakeupCloseEvent(bool isSync);
171     bool SendRecreateRendererStreamEvent(int32_t clientId, uint32_t sessionID, int32_t streamFlag,
172         const AudioStreamDeviceChangeReasonExt reason);
173     bool SendRecreateCapturerStreamEvent(int32_t clientId, uint32_t sessionID, int32_t streamFlag,
174         const AudioStreamDeviceChangeReasonExt reason);
175     bool SendHeadTrackingDeviceChangeEvent(const std::unordered_map<std::string, bool> &changeInfo);
176     void AddAudioDeviceRefinerCb(const sptr<IStandardAudioRoutingManagerListener> &callback);
177     int32_t RemoveAudioDeviceRefinerCb();
178     bool SendSpatializatonEnabledChangeEvent(const bool &enabled);
179     bool SendSpatializatonEnabledChangeForAnyDeviceEvent(const sptr<AudioDeviceDescriptor> &selectedAudioDevice,
180         const bool &enabled);
181     bool SendHeadTrackingEnabledChangeEvent(const bool &enabled);
182     int32_t SetClientCallbacksEnable(const CallbackChange &callbackchange, const bool &enable);
183     bool SendKvDataUpdate(const bool &isFirstBoot);
184     bool SendHeadTrackingEnabledChangeForAnyDeviceEvent(const sptr<AudioDeviceDescriptor> &selectedAudioDevice,
185     const bool &enabled);
186     bool SendPipeStreamCleanEvent(AudioPipeType pipeType);
187     bool SendConcurrencyEventWithSessionIDCallback(const uint32_t sessionID);
188     bool SendAudioSessionDeactiveCallback(const std::pair<int32_t, AudioSessionDeactiveEvent> &sessionDeactivePair);
189 
190 protected:
191     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
192 
193 private:
194     /* Handle Event*/
195     void HandleDeviceChangedCallback(const AppExecFwk::InnerEvent::Pointer &event);
196     void HandleAvailableDeviceChange(const AppExecFwk::InnerEvent::Pointer &event);
197     void HandleVolumeKeyEvent(const AppExecFwk::InnerEvent::Pointer &event);
198     void HandleRequestCateGoryEvent(const AppExecFwk::InnerEvent::Pointer &event);
199     void HandleAbandonCateGoryEvent(const AppExecFwk::InnerEvent::Pointer &event);
200     void HandleFocusInfoChangeEvent(const AppExecFwk::InnerEvent::Pointer &event);
201     void HandleRingerModeUpdatedEvent(const AppExecFwk::InnerEvent::Pointer &event);
202     void HandleMicStateUpdatedEvent(const AppExecFwk::InnerEvent::Pointer &event);
203     void HandleMicStateUpdatedEventWithClientId(const AppExecFwk::InnerEvent::Pointer &event);
204     void HandleInterruptEvent(const AppExecFwk::InnerEvent::Pointer &event);
205     void HandleInterruptEventWithSessionId(const AppExecFwk::InnerEvent::Pointer &event);
206     void HandleInterruptEventWithClientId(const AppExecFwk::InnerEvent::Pointer &event);
207     void HandlePreferredOutputDeviceUpdated();
208     void HandlePreferredInputDeviceUpdated();
209     void HandleDistributedRoutingRoleChangeEvent(const AppExecFwk::InnerEvent::Pointer &event);
210     void HandleRendererInfoEvent(const AppExecFwk::InnerEvent::Pointer &event);
211     void HandleCapturerInfoEvent(const AppExecFwk::InnerEvent::Pointer &event);
212     void HandleRendererDeviceChangeEvent(const AppExecFwk::InnerEvent::Pointer &event);
213     void HandleCapturerCreateEvent(const AppExecFwk::InnerEvent::Pointer &event);
214     void HandleCapturerRemovedEvent(const AppExecFwk::InnerEvent::Pointer &event);
215     void HandleWakeupCloseEvent(const AppExecFwk::InnerEvent::Pointer &event);
216     void HandleSendRecreateRendererStreamEvent(const AppExecFwk::InnerEvent::Pointer &event);
217     void HandleSendRecreateCapturerStreamEvent(const AppExecFwk::InnerEvent::Pointer &event);
218     void HandleHeadTrackingDeviceChangeEvent(const AppExecFwk::InnerEvent::Pointer &event);
219     void HandleSpatializatonEnabledChangeEvent(const AppExecFwk::InnerEvent::Pointer &event);
220     void HandleSpatializatonEnabledChangeForAnyDeviceEvent(const AppExecFwk::InnerEvent::Pointer &event);
221     void HandleHeadTrackingEnabledChangeEvent(const AppExecFwk::InnerEvent::Pointer &event);
222     void HandleUpdateKvDataEvent(const AppExecFwk::InnerEvent::Pointer &event);
223     void HandleHeadTrackingEnabledChangeForAnyDeviceEvent(const AppExecFwk::InnerEvent::Pointer &event);
224     void HandlePipeStreamCleanEvent(const AppExecFwk::InnerEvent::Pointer &event);
225     void HandleConcurrencyEventWithSessionID(const AppExecFwk::InnerEvent::Pointer &event);
226     void HandleAudioSessionDeactiveCallback(const AppExecFwk::InnerEvent::Pointer &event);
227 
228     void HandleServiceEvent(const uint32_t &eventId, const AppExecFwk::InnerEvent::Pointer &event);
229 
230     void HandleOtherServiceEvent(const uint32_t &eventId, const AppExecFwk::InnerEvent::Pointer &event);
231 
232     void ResetRingerModeMute(const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos);
233 
234     std::mutex runnerMutex_;
235     std::weak_ptr<IAudioInterruptEventDispatcher> interruptEventDispatcher_;
236     std::weak_ptr<IAudioConcurrencyEventDispatcher> concurrencyEventDispatcher_;
237 
238     std::unordered_map<int32_t, sptr<IAudioPolicyClient>> audioPolicyClientProxyAPSCbsMap_;
239     std::string pidsStrForPrinting_ = "[]";
240 
241     std::unordered_map<int32_t, std::shared_ptr<AudioInterruptCallback>> amInterruptCbsMap_;
242     std::map<std::pair<int32_t, AudioDeviceUsage>,
243         sptr<IStandardAudioPolicyManagerListener>> availableDeviceChangeCbsMap_;
244     std::unordered_map<int32_t, sptr<IStandardAudioRoutingManagerListener>> distributedRoutingRoleChangeCbsMap_;
245     std::unordered_map<int32_t,  std::unordered_map<CallbackChange, bool>> clientCallbacksMap_;
246 };
247 } // namespace AudioStandard
248 } // namespace OHOS
249 #endif // AUDIO_POLICY_SERVER_HANDLER_H
250