• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef ST_AUDIO_SPATIALIZATION_MANAGER_H
17 #define ST_AUDIO_SPATIALIZATION_MANAGER_H
18 
19 #include <cstdlib>
20 #include <list>
21 #include <map>
22 #include <mutex>
23 #include <vector>
24 #include <unordered_map>
25 
26 #include "parcel.h"
27 #include "audio_effect.h"
28 #include "audio_system_manager.h"
29 
30 namespace OHOS {
31 namespace AudioStandard {
32 class AudioSpatializationEnabledChangeCallback {
33 public:
34     virtual ~AudioSpatializationEnabledChangeCallback() = default;
35     /**
36      * @brief AudioSpatializationEnabledChangeCallback will be executed when spatialization enabled state changes
37      *
38      * @param enabled the spatialization enabled state.
39      * @since 11
40      */
41     virtual void OnSpatializationEnabledChange(const bool &enabled) = 0;
42 
43     /**
44      * @brief AudioSpatializationEnabledChangeCallback will be executed when spatialization enabled state changes
45      *
46      * @param deviceDescriptor audio device description.
47      * @param enabled the spatialization enabled state.
48      * @since 12
49      */
50     virtual void OnSpatializationEnabledChangeForAnyDevice(
51         const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, const bool &enabled) = 0;
52 };
53 
54 class AudioSpatializationEnabledChangeForCurrentDeviceCallback {
55 public:
56     virtual ~AudioSpatializationEnabledChangeForCurrentDeviceCallback() = default;
57     /**
58      * @brief AudioSpatializationEnabledChangeForAnyDeviceCallback will be executed
59      *  when spatialization enabled state changes
60      *
61      * @param enabled the spatialization enabled state for current device.
62      * @since 16
63      */
64     virtual void OnSpatializationEnabledChangeForCurrentDevice(const bool &enabled) = 0;
65 };
66 
67 class AudioHeadTrackingEnabledChangeCallback {
68 public:
69     virtual ~AudioHeadTrackingEnabledChangeCallback() = default;
70     /**
71      * @brief AudioHeadTrackingEnabledChangeCallback will be executed when head tracking enabled state changes
72      *
73      * @param enabled the head tracking enabled state.
74      * @since 11
75      */
76     virtual void OnHeadTrackingEnabledChange(const bool &enabled) = 0;
77 
78     /**
79      * @brief AudioHeadTrackingEnabledChangeCallback will be executed when head tracking enabled state changes
80      *
81      * @param deviceDescriptor audio device description.
82      * @param enabled the head tracking enabled state.
83      * @since 12
84      */
85     virtual void OnHeadTrackingEnabledChangeForAnyDevice(
86         const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, const bool &enabled) = 0;
87 };
88 
89 class AudioSpatializationStateChangeCallback {
90 public:
91     virtual ~AudioSpatializationStateChangeCallback() = default;
92     /**
93      * @brief AudioSpatializationStateChangeCallback will be executed when spatialization state changes
94      *
95      * @param enabled the spatialization state.
96      * @since 11
97      */
98     virtual void OnSpatializationStateChange(const AudioSpatializationState &spatializationState) = 0;
99 };
100 
101 class HeadTrackingDataRequestedChangeCallback {
102 public:
103     virtual ~HeadTrackingDataRequestedChangeCallback() = default;
104     /**
105      * @brief HeadTrackingDataRequestedChangeCallback will be executed when
106      * whether head tracking data is requested changes
107      *
108      * @param isRequested whethet the head tracking data is requested.
109      * @since 12
110      */
111     virtual void OnHeadTrackingDataRequestedChange(bool isRequested) = 0;
112 };
113 
114 /**
115  * @brief The AudioSpatializationManager class is an abstract definition of audio spatialization manager.
116  *        Provides a series of client/interfaces for audio spatialization management
117  */
118 
119 class AudioSpatializationManager {
120 public:
121     static AudioSpatializationManager *GetInstance();
122 
123     /**
124      * @brief Check whether the spatialization is enabled
125      *
126      * @return Returns <b>true</b> if the spatialization is successfully enabled; returns <b>false</b> otherwise.
127      * @since 11
128      */
129     bool IsSpatializationEnabled();
130 
131     /**
132      * @brief Check whether the spatialization is enabled by the specified device.
133      *
134      * @return Returns <b>true</b> if the spatialization is successfully enabled; returns <b>false</b> otherwise.
135      * @since 12
136      */
137     bool IsSpatializationEnabled(const std::shared_ptr<AudioDeviceDescriptor> &selectedAudioDevice);
138 
139     /**
140      * @brief Check whether the spatialization is enabled by the current device
141      *
142      * @return Returns <b>true</b> if the spatialization is successfully enabled; returns <b>false</b> otherwise.
143      * @since 16
144      */
145     bool IsSpatializationEnabledForCurrentDevice();
146 
147     /**
148      * @brief Set the spatialization enabled or disabled
149      *
150      * @return Returns success or not
151      * @since 11
152      */
153     int32_t SetSpatializationEnabled(const bool enable);
154 
155     /**
156      * @brief Set the spatialization enabled or disabled by the specified device.
157      *
158      * @return Returns success or not
159      * @since 12
160      */
161     int32_t SetSpatializationEnabled(
162         const std::shared_ptr<AudioDeviceDescriptor> &selectedAudioDevice, const bool enable);
163 
164     /**
165      * @brief Check whether the head tracking is enabled
166      *
167      * @return Returns <b>true</b> if the head tracking is successfully enabled; returns <b>false</b> otherwise.
168      * @since 11
169      */
170     bool IsHeadTrackingEnabled();
171 
172     /**
173      * @brief Check whether the head tracking is enabled by the specified device.
174      *
175      * @return Returns <b>true</b> if the head tracking is successfully enabled; returns <b>false</b> otherwise.
176      * @since 12
177      */
178     bool IsHeadTrackingEnabled(const std::shared_ptr<AudioDeviceDescriptor> &selectedAudioDevice);
179 
180     /**
181      * @brief Set the head tracking enabled or disabled
182      *
183      * @return Returns success or not
184      * @since 11
185      */
186     int32_t SetHeadTrackingEnabled(const bool enable);
187 
188     /**
189      * @brief Set the head tracking enabled or disabled by the specified device.
190      *
191      * @return Returns success or not
192      * @since 12
193      */
194     int32_t SetHeadTrackingEnabled(
195         const std::shared_ptr<AudioDeviceDescriptor> &selectedAudioDevice, const bool enable);
196 
197     /**
198      * @brief Register the spatialization enabled change callback listener
199      *
200      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
201      * defined in {@link audio_errors.h} otherwise.
202      * @since 11
203      */
204     int32_t RegisterSpatializationEnabledEventListener(
205         const std::shared_ptr<AudioSpatializationEnabledChangeCallback> &callback);
206 
207     /**
208      * @brief Register the spatialization enabled change for current device callback listener
209      *
210      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
211      * defined in {@link audio_errors.h} otherwise.
212      * @since 16
213      */
214     int32_t RegisterSpatializationEnabledForCurrentDeviceEventListener(
215         const std::shared_ptr<AudioSpatializationEnabledChangeForCurrentDeviceCallback> &callback);
216 
217     /**
218      * @brief Register the head tracking enabled change callback listener
219      *
220      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
221      * defined in {@link audio_errors.h} otherwise.
222      * @since 11
223      */
224     int32_t RegisterHeadTrackingEnabledEventListener(
225         const std::shared_ptr<AudioHeadTrackingEnabledChangeCallback> &callback);
226 
227     /**
228      * @brief Unregister the spatialization enabled change callback listener
229      *
230      * @return Returns {@link SUCCESS} if callback unregistration is successful; returns an error code
231      * defined in {@link audio_errors.h} otherwise.
232      * @since 11
233      */
234     int32_t UnregisterSpatializationEnabledEventListener();
235 
236     /**
237      * @brief Unregister the spatialization enabled change for current device callback listener
238      *
239      * @return Returns {@link SUCCESS} if callback unregistration is successful; returns an error code
240      * defined in {@link audio_errors.h} otherwise.
241      * @since 16
242      */
243     int32_t UnregisterSpatializationEnabledForCurrentDeviceEventListener();
244 
245     /**
246      * @brief Unregister the head tracking enabled change callback listener
247      *
248      * @return Returns {@link SUCCESS} if callback unregistration is successful; returns an error code
249      * defined in {@link audio_errors.h} otherwise.
250      * @since 11
251      */
252     int32_t UnregisterHeadTrackingEnabledEventListener();
253 
254     /**
255      * @brief Check whether the spatialization is supported
256      *
257      * @return Returns <b>true</b> if the spatialization is supported; returns <b>false</b> otherwise.
258      * @since 11
259      */
260     bool IsSpatializationSupported();
261 
262     /**
263      * @brief Check whether the spatialization is supported for some device
264      *
265      * @return Returns <b>true</b> if the spatialization is supported; returns <b>false</b> otherwise.
266      * @since 11
267      */
268     bool IsSpatializationSupportedForDevice(const std::shared_ptr<AudioDeviceDescriptor> &selectedAudioDevice);
269 
270     /**
271      * @brief Check whether the Head Tracking is supported
272      *
273      * @return Returns <b>true</b> if the Head Tracking is supported; returns <b>false</b> otherwise.
274      * @since 11
275      */
276     bool IsHeadTrackingSupported();
277 
278     /**
279      * @brief Check whether the head tracking is supported for some device
280      *
281      * @return Returns <b>true</b> if the head tracking is supported; returns <b>false</b> otherwise.
282      * @since 11
283      */
284     bool IsHeadTrackingSupportedForDevice(const std::shared_ptr<AudioDeviceDescriptor> &selectedAudioDevice);
285 
286     /**
287      * @brief Update the state of the spatial Device
288      *
289      * @since 11
290      */
291     int32_t UpdateSpatialDeviceState(const AudioSpatialDeviceState audioSpatialDeviceState);
292 
293     /**
294      * @brief Get current spatialization rendering scene type
295      *
296      * @return Returns current spatialization scene type enum defined in {@link audio_effect.h}.
297      * @since 12
298      */
299     AudioSpatializationSceneType GetSpatializationSceneType();
300 
301     /**
302      * @brief Set spatialization rendering scene type
303      *
304      * @return Returns {@link SUCCESS} if setting spatialization scene type is successful; returns an error code
305      * defined in {@link audio_errors.h} otherwise.
306      * @since 12
307      */
308     int32_t SetSpatializationSceneType(const AudioSpatializationSceneType spatializationSceneType);
309 
310     /**
311      * @brief Check whether head tracking data is requested
312      *
313      * @return Returns <b>true</b> if the head tracking data is requested; returns <b>false</b> otherwise.
314      * @since 12
315      */
316     bool IsHeadTrackingDataRequested(const std::string &macAddress);
317 
318     /**
319      * @brief Register the head tracking data requested change callback listener for the specified device
320      *
321      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
322      * defined in {@link audio_errors.h} otherwise.
323      * @since 12
324      */
325     int32_t RegisterHeadTrackingDataRequestedEventListener(const std::string &macAddress,
326         const std::shared_ptr<HeadTrackingDataRequestedChangeCallback> &callback);
327 
328     /**
329      * @brief Unregister the head tracking data requested change callback listener for the specified device
330      *
331      * @return Returns {@link SUCCESS} if callback unregistration is successful; returns an error code
332      * defined in {@link audio_errors.h} otherwise.
333      * @since 12
334      */
335     int32_t UnregisterHeadTrackingDataRequestedEventListener(const std::string &macAddress);
336 private:
337     AudioSpatializationManager();
338     virtual ~AudioSpatializationManager();
339 };
340 } // namespace AudioStandard
341 } // namespace OHOS
342 #endif // ST_AUDIO_SPATIALIZATION_MANAGER_H
343