1 /*
2  * Copyright (c) 2023 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 
28 namespace OHOS {
29 namespace AudioStandard {
30 class AudioPolicyClientStubImpl : public AudioPolicyClientStub {
31 public:
32     int32_t AddVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb);
33     int32_t RemoveVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb);
34     size_t GetVolumeKeyEventCallbackSize() const;
35     int32_t AddFocusInfoChangeCallback(const std::shared_ptr<AudioFocusInfoChangeCallback> &cb);
36     int32_t RemoveFocusInfoChangeCallback();
37     int32_t AddDeviceChangeCallback(const DeviceFlag &flag,
38         const std::shared_ptr<AudioManagerDeviceChangeCallback> &cb);
39     int32_t RemoveDeviceChangeCallback(DeviceFlag flag, std::shared_ptr<AudioManagerDeviceChangeCallback> &cb);
40     size_t GetDeviceChangeCallbackSize() const;
41     int32_t AddRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> &cb);
42     int32_t RemoveRingerModeCallback();
43     int32_t RemoveRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> &cb);
44     size_t GetRingerModeCallbackSize() const;
45     int32_t AddMicStateChangeCallback(const std::shared_ptr<AudioManagerMicStateChangeCallback> &cb);
46     int32_t RemoveMicStateChangeCallback();
47     size_t GetMicStateChangeCallbackSize() const;
48     bool HasMicStateChangeCallback();
49     int32_t AddPreferredOutputDeviceChangeCallback(
50         const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &cb);
51     int32_t RemovePreferredOutputDeviceChangeCallback();
52     size_t GetPreferredOutputDeviceChangeCallbackSize() const;
53     int32_t AddPreferredInputDeviceChangeCallback(
54         const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &cb);
55     int32_t RemovePreferredInputDeviceChangeCallback();
56     size_t GetPreferredInputDeviceChangeCallbackSize() const;
57     int32_t AddRendererStateChangeCallback(const std::shared_ptr<AudioRendererStateChangeCallback> &cb);
58     int32_t RemoveRendererStateChangeCallback(
59         const std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> &callbacks);
60     int32_t RemoveRendererStateChangeCallback(
61         const std::shared_ptr<AudioRendererStateChangeCallback> &callback);
62     size_t GetRendererStateChangeCallbackSize() const;
63     int32_t AddCapturerStateChangeCallback(const std::shared_ptr<AudioCapturerStateChangeCallback> &cb);
64     int32_t RemoveCapturerStateChangeCallback();
65     size_t GetCapturerStateChangeCallbackSize() const;
66     int32_t AddDeviceChangeWithInfoCallback(
67         const uint32_t sessionId, const std::weak_ptr<DeviceChangeWithInfoCallback> &cb);
68     int32_t RemoveDeviceChangeWithInfoCallback(const uint32_t sessionId);
69     size_t GetDeviceChangeWithInfoCallbackkSize() const;
70     int32_t AddMicrophoneBlockedCallback(const int32_t clientId,
71         const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &cb);
72     int32_t RemoveMicrophoneBlockedCallback(const int32_t clientId,
73         const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &cb);
74     size_t GetMicrophoneBlockedCallbackSize() const;
75     int32_t AddHeadTrackingDataRequestedChangeCallback(const std::string &macAddress,
76         const std::shared_ptr<HeadTrackingDataRequestedChangeCallback> &cb);
77     int32_t RemoveHeadTrackingDataRequestedChangeCallback(const std::string &macAddress);
78     size_t GetHeadTrackingDataRequestedChangeCallbackSize() const;
79     int32_t AddSpatializationEnabledChangeCallback(const std::shared_ptr<AudioSpatializationEnabledChangeCallback> &cb);
80     int32_t RemoveSpatializationEnabledChangeCallback();
81     size_t GetSpatializationEnabledChangeCallbackSize() const;
82     int32_t AddHeadTrackingEnabledChangeCallback(const std::shared_ptr<AudioHeadTrackingEnabledChangeCallback> &cb);
83     int32_t RemoveHeadTrackingEnabledChangeCallback();
84     size_t GetHeadTrackingEnabledChangeCallbacSize() const;
85     size_t GetFocusInfoChangeCallbackSize() const;
86     int32_t AddAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &cb);
87     int32_t RemoveAudioSessionCallback();
88     int32_t RemoveAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &cb);
89     size_t GetAudioSessionCallbackSize() const;
90 
91     void OnRecreateRendererStreamEvent(const uint32_t sessionId, const int32_t streamFlag,
92         const AudioStreamDeviceChangeReasonExt reason) override;
93     void OnRecreateCapturerStreamEvent(const uint32_t sessionId, const int32_t streamFlag,
94         const AudioStreamDeviceChangeReasonExt reason) override;
95     void OnVolumeKeyEvent(VolumeEvent volumeEvent) override;
96     void OnAudioFocusInfoChange(const std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList) override;
97     void OnAudioFocusRequested(const AudioInterrupt &requestFocus) override;
98     void OnAudioFocusAbandoned(const AudioInterrupt &abandonFocus) override;
99     void OnDeviceChange(const DeviceChangeAction &deviceChangeAction) override;
100     void OnMicrophoneBlocked(const MicrophoneBlockedInfo &microphoneBlockedInfo) override;
101     void OnRingerModeUpdated(const AudioRingerMode &ringerMode) override;
102     void OnMicStateUpdated(const MicStateChangeEvent &micStateChangeEvent) override;
103     void OnPreferredOutputDeviceUpdated(const std::vector<sptr<AudioDeviceDescriptor>> &desc) override;
104     void OnPreferredInputDeviceUpdated(const std::vector<sptr<AudioDeviceDescriptor>> &desc) override;
105     void OnRendererStateChange(
106         std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos) override;
107     void OnCapturerStateChange(
108         std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos) override;
109     void OnRendererDeviceChange(const uint32_t sessionId,
110         const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReasonExt reason) override;
111     void OnHeadTrackingDeviceChange(const std::unordered_map<std::string, bool> &changeInfo) override;
112     void OnSpatializationEnabledChange(const bool &enabled) override;
113     void OnSpatializationEnabledChangeForAnyDevice(const sptr<AudioDeviceDescriptor> &deviceDescriptor,
114         const bool &enabled) override;
115     void OnHeadTrackingEnabledChange(const bool &enabled) override;
116     void OnHeadTrackingEnabledChangeForAnyDevice(const sptr<AudioDeviceDescriptor> &deviceDescriptor,
117         const bool &enabled) override;
118     void OnAudioSessionDeactive(const AudioSessionDeactiveEvent &deactiveEvent) override;
119 
120 private:
121     std::vector<sptr<AudioDeviceDescriptor>> DeviceFilterByFlag(DeviceFlag flag,
122         const std::vector<sptr<AudioDeviceDescriptor>>& desc);
123 
124     std::vector<std::weak_ptr<VolumeKeyEventCallback>> volumeKeyEventCallbackList_;
125     std::vector<std::shared_ptr<AudioFocusInfoChangeCallback>> focusInfoChangeCallbackList_;
126     std::vector<std::pair<DeviceFlag, std::shared_ptr<AudioManagerDeviceChangeCallback>>> deviceChangeCallbackList_;
127     std::vector<std::shared_ptr<AudioRingerModeCallback>> ringerModeCallbackList_;
128     std::vector<std::shared_ptr<AudioManagerMicStateChangeCallback>> micStateChangeCallbackList_;
129     std::vector<std::shared_ptr<AudioPreferredOutputDeviceChangeCallback>> preferredOutputDeviceCallbackList_;
130     std::vector<std::shared_ptr<AudioPreferredInputDeviceChangeCallback>> preferredInputDeviceCallbackList_;
131     std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> rendererStateChangeCallbackList_;
132     std::vector<std::weak_ptr<AudioCapturerStateChangeCallback>> capturerStateChangeCallbackList_;
133     std::vector<std::shared_ptr<AudioSpatializationEnabledChangeCallback>> spatializationEnabledChangeCallbackList_;
134     std::vector<std::shared_ptr<AudioHeadTrackingEnabledChangeCallback>> headTrackingEnabledChangeCallbackList_;
135     std::vector<std::shared_ptr<AudioSessionCallback>> audioSessionCallbackList_;
136     std::vector<std::pair<int32_t, std::shared_ptr<AudioManagerMicrophoneBlockedCallback>>>
137         microphoneBlockedCallbackList_;
138 
139     std::unordered_map<uint32_t,
140         std::weak_ptr<DeviceChangeWithInfoCallback>> deviceChangeWithInfoCallbackMap_;
141 
142     std::unordered_map<std::string,
143         std::shared_ptr<HeadTrackingDataRequestedChangeCallback>> headTrackingDataRequestedChangeCallbackMap_;
144 
145     mutable std::mutex focusInfoChangeMutex_;
146     mutable std::mutex rendererStateChangeMutex_;
147     mutable std::mutex capturerStateChangeMutex_;
148     mutable std::mutex pOutputDeviceChangeMutex_;
149     mutable std::mutex pInputDeviceChangeMutex_;
150     mutable std::mutex volumeKeyEventMutex_;
151     mutable std::mutex deviceChangeMutex_;
152     mutable std::mutex ringerModeMutex_;
153     mutable std::mutex micStateChangeMutex_;
154     mutable std::mutex deviceChangeWithInfoCallbackMutex_;
155     mutable std::mutex headTrackingDataRequestedChangeMutex_;
156     mutable std::mutex spatializationEnabledChangeMutex_;
157     mutable std::mutex headTrackingEnabledChangeMutex_;
158     mutable std::mutex audioSessionMutex_;
159     mutable std::mutex microphoneBlockedMutex_;
160 };
161 } // namespace AudioStandard
162 } // namespace OHOS
163 #endif // AUDIO_POLICY_CLIENT_STUB_IMPL_H