• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #ifndef ST_AUDIO_SESSION_MANAGER_H
17 #define ST_AUDIO_SESSION_MANAGER_H
18 
19 #include "audio_system_manager.h"
20 #include "audio_session_device_info.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
24 
25 class AudioSessionRestoreParame {
26 public:
27     enum class OperationType {
28         AUDIO_SESSION_ACTIVATE,
29         AUDIO_SESSION_SET_SCENE,
30     };
31 
32     struct AudioSessionAction {
33         OperationType type;
34         int32_t optValue;
35 
AudioSessionActionAudioSessionAction36         AudioSessionAction(const OperationType type, const int32_t value)
37             : type(type), optValue(value) {}
38 
39         ~AudioSessionAction() = default;
40     };
41 
42     explicit AudioSessionRestoreParame() = default;
43 
44     ~AudioSessionRestoreParame() = default;
45 
46     void OnAudioSessionDeactive();
47     void OnAudioSessionStateChanged(AudioSessionStateChangeHint audioSessionStateChangeHint);
48     void RecordAudioSessionOpt(const OperationType type, const int32_t value);
49     bool RestoreParame(void);
50 
51 private:
52     std::mutex actionsMutex_;
53     std::vector<std::unique_ptr<AudioSessionAction>> actions_;
54 };
55 
56 class AudioSessionCallback {
57 public:
58     virtual ~AudioSessionCallback() = default;
59     /**
60      * @brief OnAudioSessionDeactive will be executed when the audio session is deactivated be others.
61      *
62      * @param deactiveEvent the audio session deactive event info.
63      * @since 12
64      */
65     virtual void OnAudioSessionDeactive(const AudioSessionDeactiveEvent &deactiveEvent) = 0;
66 };
67 
68 class AudioSessionStateChangedCallback {
69 public:
70     virtual ~AudioSessionStateChangedCallback() = default;
71     /**
72      * @brief The function will be executed when the audio session state changed.
73      *
74      * @param stateChangedEvent the audio session state changed event.
75      * @since 20
76      */
77     virtual void OnAudioSessionStateChanged(const AudioSessionStateChangedEvent &stateChangedEvent) = 0;
78 };
79 
80 class AudioSessionCurrentDeviceChangedCallback {
81 public:
82     virtual ~AudioSessionCurrentDeviceChangedCallback() = default;
83     /**
84      * @brief
85      *
86      * @param deviceChangedEvent the audio session current device changed event.
87      * @since 20
88      */
89     virtual void OnAudioSessionCurrentDeviceChanged(const CurrentOutputDeviceChangedEvent &deviceChangedEvent) = 0;
90 };
91 
92 class AudioSessionManager {
93 public:
94     AudioSessionManager() = default;
95     virtual ~AudioSessionManager() = default;
96 
97     static AudioSessionManager *GetInstance();
98 
99     /**
100      * @brief Activate audio session.
101      *
102      * @param strategy Target audio session strategy.
103      * @return Returns {@link SUCCESS} if the operation is successful; returns an error code
104      * defined in {@link audio_errors.h} otherwise.
105      * @since 12
106      */
107     int32_t ActivateAudioSession(const AudioSessionStrategy &strategy);
108 
109     /**
110      * @brief Deactivate audio session.
111      *
112      * @return Returns {@link SUCCESS} if the operation is successful; returns an error code
113      * defined in {@link audio_errors.h} otherwise.
114      * @since 12
115      */
116     int32_t DeactivateAudioSession();
117 
118     /**
119      * @brief Query whether the audio session is active.
120      *
121      * @return Returns <b>true</b> if the audio session is active; returns <b>false</b> otherwise.
122      * @since 12
123      */
124     bool IsAudioSessionActivated();
125 
126     /**
127      * @brief Set audio session callback.
128      *
129      * @param audioSessionCallback The audio session callback.
130      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
131      * defined in {@link audio_errors.h} otherwise.
132      * @since 12
133      */
134     int32_t SetAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &audioSessionCallback);
135 
136     /**
137      * @brief Unset all audio session callbacks.
138      *
139      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
140      * defined in {@link audio_errors.h} otherwise.
141      * @since 12
142      */
143     int32_t UnsetAudioSessionCallback();
144 
145     /**
146      * @brief Unset audio session callback.
147      *
148      * @param audioSessionCallback The audio session callback.
149      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
150      * defined in {@link audio_errors.h} otherwise.
151      * @since 12
152      */
153     int32_t UnsetAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &audioSessionCallback);
154 
155     /**
156      * @brief Set scene for audio session.
157      *
158      * @param audioSessionScene - Audio session scene.
159      * @return Returns {@link SUCCESS} if the operation is successful; returns an error code
160      * defined in {@link audio_errors.h} otherwise.
161      * @since 20
162      */
163     int32_t SetAudioSessionScene(const AudioSessionScene audioSessionScene);
164 
165     /**
166      * @brief Listens for audio session state changed event.
167      * When the audio session state change, registered clients will receive the callback.
168      *
169      * @param stateChangedCallback The audio session state changed callback.
170      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
171      * defined in {@link audio_errors.h} otherwise.
172      * @since 20
173      */
174     int32_t SetAudioSessionStateChangeCallback(
175         const std::shared_ptr<AudioSessionStateChangedCallback> &stateChangedCallback);
176 
177     /**
178      * @brief Unset all audio session state changed callbacks.
179      *
180      * @return Returns {@link SUCCESS} if callback unregistration is successful; returns an error code
181      * defined in {@link audio_errors.h} otherwise.
182      * @since 20
183      */
184     int32_t UnsetAudioSessionStateChangeCallback();
185 
186     /**
187      * @brief Unset the audio session state changed callback.
188      *
189      * @param stateChangedCallback The audio session state changed callback.
190      * @return Returns {@link SUCCESS} if callback unregistration is successful; returns an error code
191      * defined in {@link audio_errors.h} otherwise.
192      * @since 20
193      */
194     int32_t UnsetAudioSessionStateChangeCallback(
195         const std::shared_ptr<AudioSessionStateChangedCallback> &stateChangedCallback);
196 
197     /**
198      * @brief Get default output device type.
199      *
200      * @param deviceType The default output device type.
201      * @return Returns {@link SUCCESS} if the operation is successful; returns an error code
202      * defined in {@link audio_errors.h} otherwise.
203      * @since 20
204      */
205     int32_t GetDefaultOutputDevice(DeviceType &deviceType);
206 
207     /**
208      * @brief Set the default output device for audio session scene.
209      *
210      * @param deviceType. The available deviceTypes are EARPIECE/SPEAKER/DEFAULT.
211      * @return Returns {@link SUCCESS} if the operation is successful; returns an error code
212      * defined in {@link audio_errors.h} otherwise.
213      * @since 20
214      */
215     int32_t SetDefaultOutputDevice(DeviceType deviceType);
216 
217     /**
218      * @brief Subscribes device changed event callback.
219      * The event is triggered when device changed.
220      *
221      * @param deviceChangedCallback The audio session device changed callback.
222      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
223      * defined in {@link audio_errors.h} otherwise.
224      * @since 20
225      */
226     int32_t SetAudioSessionCurrentDeviceChangeCallback(
227         const std::shared_ptr<AudioSessionCurrentDeviceChangedCallback> &deviceChangedCallback);
228 
229     /**
230      * @brief Unset all audio session device changed callbacks.
231      *
232      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
233      * defined in {@link audio_errors.h} otherwise.
234      * @since 20
235      */
236     int32_t UnsetAudioSessionCurrentDeviceChangeCallback();
237 
238     /**
239      * @brief Unset the audio session device changed callback.
240      *
241      * @param deviceChangedCallback The audio session device changed callback.
242      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
243      * defined in {@link audio_errors.h} otherwise.
244      * @since 20
245      */
246     int32_t UnsetAudioSessionCurrentDeviceChangeCallback(
247         const std::shared_ptr<AudioSessionCurrentDeviceChangedCallback> &deviceChangedCallback);
248 
249     /**
250      * @brief Register AudioPolicyServer died callback.
251      *
252      * @since 20
253      */
254     void RegisterAudioPolicyServerDiedCb();
255 
256     /**
257      * @brief Restore all audio session parame when AudioPolicyServer died.
258      *
259      * @since 20
260      */
261     bool Restore();
262 
263     /**
264      * @brief Clear restoreParame opt when session deactived.
265      *
266      * @since 20
267      */
268     void OnAudioSessionDeactive(const AudioSessionDeactiveEvent &deactiveEvent);
269 
270     /**
271      * @brief Clear restoreParame opt when session scene state changed.
272      *
273      * @since 20
274      */
275     void OnAudioSessionStateChanged(const AudioSessionStateChangedEvent &stateChangedEvent);
276 
277 private:
278     std::mutex setDefaultOutputDeviceMutex_;
279     bool setDefaultOutputDevice_ = false;
280     DeviceType setDeviceType_ = DEVICE_TYPE_INVALID;
281 
282     // used by restore
283     std::mutex sessionManagerRestoreMutex_;
284     bool policyServerDiedCbRegistered_ = false;
285     std::shared_ptr<AudioSessionManagerPolicyServiceDiedCallback> sessionManagerRestoreCb_ = nullptr;
286 
287     AudioSessionRestoreParame restoreParame_;
288 };
289 
290 class AudioSessionManagerServiceDiedRestore : public AudioSessionManagerPolicyServiceDiedCallback {
291 public:
292     AudioSessionManagerServiceDiedRestore() = default;
293 
294     void OnAudioPolicyServiceDied() override;
295 
296     ~AudioSessionManagerServiceDiedRestore() = default;
297 };
298 
299 class AudioSessionManagerStateCallback : public AudioSessionStateChangedCallback {
300 public:
301     explicit AudioSessionManagerStateCallback() = default;
302 
303     void OnAudioSessionStateChanged(const AudioSessionStateChangedEvent &stateChangedEvent) override;
304 
305     ~AudioSessionManagerStateCallback() = default;
306 };
307 
308 class AudioSessionManagerDeactivedCallback : public AudioSessionCallback {
309 public:
310     explicit AudioSessionManagerDeactivedCallback() = default;
311 
312     void OnAudioSessionDeactive(const AudioSessionDeactiveEvent &deactiveEvent) override;
313 
314     ~AudioSessionManagerDeactivedCallback() = default;
315 };
316 
317 } // namespace AudioStandard
318 } // namespace OHOS
319 #endif // ST_AUDIO_SESSION_MANAGER_H
320