1 /*
2  * Copyright (c) 2022 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 ST_AUDIO_STREAM_MANAGER_H
17 #define ST_AUDIO_STREAM_MANAGER_H
18 
19 #include <iostream>
20 #include <map>
21 #include "audio_effect.h"
22 #include "audio_system_manager.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
26 class AudioRendererStateChangeCallback {
27 public:
28     virtual ~AudioRendererStateChangeCallback() = default;
29     /**
30      * Called when the renderer state changes
31      *
32      * @param rendererChangeInfo Contains the renderer state information.
33      */
34     virtual void OnRendererStateChange(
35         const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos) = 0;
36 };
37 
38 class DeviceChangeWithInfoCallback {
39 public:
40     virtual ~DeviceChangeWithInfoCallback() = default;
41 
42     virtual void OnDeviceChangeWithInfo(
43         const uint32_t sessionId, const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReasonExt reason) = 0;
44 
45     virtual void OnRecreateStreamEvent(const uint32_t sessionId, const int32_t streamFlag,
46         const AudioStreamDeviceChangeReasonExt reason) = 0;
47 };
48 
49 class AudioCapturerStateChangeCallback {
50 public:
51     virtual ~AudioCapturerStateChangeCallback() = default;
52     /**
53      * Called when the capturer state changes
54      *
55      * @param capturerChangeInfo Contains the renderer state information.
56      */
57     virtual void OnCapturerStateChange(
58         const std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos) = 0;
59     std::mutex cbMutex_;
60 };
61 
62 class AudioClientTracker {
63 public:
64     virtual ~AudioClientTracker() = default;
65 
66     /**
67      * Paused Stream was controlled by system application
68      *
69      * @param streamSetStateEventInternal Contains the set even information.
70      */
71     virtual void PausedStreamImpl(const StreamSetStateEventInternal &streamSetStateEventInternal) = 0;
72 
73      /**
74      * Resumed Stream was controlled by system application
75      *
76      * @param streamSetStateEventInternal Contains the set even information.
77      */
78     virtual void ResumeStreamImpl(const StreamSetStateEventInternal &streamSetStateEventInternal) = 0;
79 
80     /**
81      * Set low power volume was controlled by system application
82      *
83      * @param volume volume value.
84      */
85     virtual void SetLowPowerVolumeImpl(float volume) = 0;
86 
87     /**
88      * Get low power volume was controlled by system application
89      *
90      * @param volume volume value.
91      */
92     virtual void GetLowPowerVolumeImpl(float &volume) = 0;
93 
94     /**
95      * Set Stream into a specified Offload state
96      *
97      * @param state power state.
98      * @param isAppBack app state.
99      */
100     virtual void SetOffloadModeImpl(int32_t state, bool isAppBack) = 0;
101 
102     /**
103      * Unset Stream out of Offload state
104      *
105      */
106     virtual void UnsetOffloadModeImpl() = 0;
107 
108     /**
109      * Get single stream was controlled by system application
110      *
111      * @param volume volume value.
112      */
113     virtual void GetSingleStreamVolumeImpl(float &volume) = 0;
114 };
115 
116 class AudioStreamManager {
117 public:
118     AudioStreamManager() = default;
119     virtual ~AudioStreamManager() = default;
120 
121     static AudioStreamManager *GetInstance();
122 
123     /**
124      * @brief Registers the renderer event callback listener.
125      *
126      * @param clientPid client PID
127      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
128      * defined in {@link audio_errors.h} otherwise.
129      * @since 9
130      * @deprecated since 12
131      */
132     int32_t RegisterAudioRendererEventListener(const int32_t clientPid,
133                                               const std::shared_ptr<AudioRendererStateChangeCallback> &callback);
134 
135     /**
136      * @brief Unregisters the renderer event callback listener.
137      *
138      * @param clientPid client PID
139      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
140      * defined in {@link audio_errors.h} otherwise.
141      * @since 9
142      * @deprecated since 12
143      */
144     int32_t UnregisterAudioRendererEventListener(const int32_t clientPid);
145 
146     /**
147      * @brief Registers the renderer event callback listener.
148      *
149      * @param callback
150      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
151      * defined in {@link audio_errors.h} otherwise.
152      * @since 12
153      */
154     int32_t RegisterAudioRendererEventListener(const std::shared_ptr<AudioRendererStateChangeCallback> &callback);
155 
156     /**
157      * @brief Unregisters the renderer event callback listener.
158      *
159      * @param callback need to unregister.
160      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
161      * defined in {@link audio_errors.h} otherwise.
162      * @since 12
163      */
164     int32_t UnregisterAudioRendererEventListener(const std::shared_ptr<AudioRendererStateChangeCallback> &callback);
165 
166     /**
167      * @brief Registers the capturer event callback listener.
168      *
169      * @param clientPid client PID
170      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
171      * defined in {@link audio_errors.h} otherwise.
172      * @since 9
173      */
174     int32_t RegisterAudioCapturerEventListener(const int32_t clientPid,
175         const std::shared_ptr<AudioCapturerStateChangeCallback> &callback);
176 
177     /**
178      * @brief Unregisters the capturer event callback listener.
179      *
180      * @param clientPid client PID
181      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
182      * defined in {@link audio_errors.h} otherwise.
183      * @since 9
184      */
185     int32_t UnregisterAudioCapturerEventListener(const int32_t clientPid);
186 
187     /**
188      * @brief Get current renderer change Infos.
189      *
190      * @param audioRendererChangeInfos  audioRendererChangeInfos
191      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
192      * defined in {@link audio_errors.h} otherwise.
193      * @since 9
194      */
195     int32_t GetCurrentRendererChangeInfos(
196         std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos);
197 
198     /**
199      * @brief Get current capturer change Infos.
200      *
201      * @param audioRendererChangeInfos  audioRendererChangeInfos
202      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
203      * defined in {@link audio_errors.h} otherwise.
204      * @since 9
205      */
206     int32_t GetCurrentCapturerChangeInfos(
207         std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos);
208 
209     /**
210      * @brief Is audio renderer low latency supported.
211      *
212      * @param audioStreamInfo  audioStreamInfo
213      * @return Returns <b>true</b> if the timestamp is successfully obtained; returns <b>false</b> otherwise.
214      * @since 9
215      */
216     bool IsAudioRendererLowLatencySupported(const AudioStreamInfo &audioStreamInfo);
217 
218     /**
219      * @brief Get Audio Effect Infos.
220      *
221      * @param AudioSceneEffectInfo  AudioSceneEffectInfo
222      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
223      * defined in {@link audio_errors.h} otherwise.
224      * @since 9
225      */
226     int32_t GetEffectInfoArray(AudioSceneEffectInfo &audioSceneEffectInfo, StreamUsage streamUsage);
227 
228     /**
229      * @brief Is stream active.
230      *
231      * @param volumeType audio volume type.
232      * @return Returns <b>true</b> if the rendering is successfully started; returns <b>false</b> otherwise.
233      * @since 9
234      */
235     bool IsStreamActive(AudioVolumeType volumeType) const;
236 
237     /**
238      * @brief Gets sampling rate for hardware output.
239      *
240      * @param AudioDeviceDescriptor Target output device.
241      * @return The sampling rate for output.
242      * @since 11
243      */
244     int32_t GetHardwareOutputSamplingRate(sptr<AudioDeviceDescriptor> &desc);
245 
246 private:
247     std::mutex rendererStateChangeCallbacksMutex_;
248     std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> rendererStateChangeCallbacks_;
249 };
250 
251 static const std::map<std::string, AudioEffectMode> effectModeMap = {
252     {"EFFECT_NONE", EFFECT_NONE},
253     {"EFFECT_DEFAULT", EFFECT_DEFAULT}
254 };
255 } // namespace AudioStandard
256 } // namespace OHOS
257 #endif // ST_AUDIO_STREAM_MANAGER_H
258