• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief The framework interface and callback function of a2dp source are defined.
21  *
22  * @since 6
23  */
24 
25 #ifndef BLUETOOTH_A2DP_SRC_H
26 #define BLUETOOTH_A2DP_SRC_H
27 
28 #include <vector>
29 
30 #include "bluetooth_def.h"
31 #include "bluetooth_types.h"
32 #include "bluetooth_remote_device.h"
33 #include "bluetooth_a2dp_codec.h"
34 #include "bluetooth_no_destructor.h"
35 
36 namespace OHOS {
37 namespace Bluetooth {
38 /**
39  * @brief A2dp source API callback function.
40  *
41  * @since 6.0
42  */
43 class A2dpSourceObserver {
44 public:
45     /**
46      * @brief A destructor used to delete the a2dp source Observer instance.
47      *
48      * @since 6.0
49      */
50     virtual ~A2dpSourceObserver() = default;
51 
52     /**
53      * @brief The callback function after device's playing state changed.
54      *
55      * @param device  the remote bluetooth device.
56      * @param playingState  the playing state after changing.
57      * @param error  the error information.
58      * @since 6.0
59      */
OnPlayingStatusChanged(const BluetoothRemoteDevice & device,int playingState,int error)60     virtual void OnPlayingStatusChanged(const BluetoothRemoteDevice &device, int playingState, int error)
61     {}
62 
63     /**
64      * @brief The callback function after device's codec information changed.
65      *
66      * @param device  the remote bluetooth device.
67      * @param info  the device's codec information.
68      * @param error  the error information.
69      * @since 6.0
70      */
OnConfigurationChanged(const BluetoothRemoteDevice & device,const A2dpCodecInfo & info,int error)71     virtual void OnConfigurationChanged(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info, int error)
72     {}
73 
74     /**
75      * @brief ConnectionState Changed observer.
76      * @param device bluetooth device address.
77      * @param state Connection state.
78      * @param cause Connecton cause.
79      * @since 12
80      */
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state,int cause)81     virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause)
82     {}
83 
84     /**
85      * @brief Media Stack Changed observer.
86      * @param device bluetooth device address.
87      * @param Action on the device.
88      * @since 11.0
89      */
OnMediaStackChanged(const BluetoothRemoteDevice & device,int action)90     virtual void OnMediaStackChanged(const BluetoothRemoteDevice &device, int action)
91     {}
92 
93     /**
94      * @brief The observer function to notify virtual device changed.
95      *
96      * @param Action on the device.
97      * @param device bluetooth device address.
98      * @since 12
99      */
OnVirtualDeviceChanged(int32_t action,std::string address)100     virtual void OnVirtualDeviceChanged(int32_t action, std::string address)
101     {}
102 
103     /**
104      * @brief Hdap ConnectionState Changed observer.
105      * @param device bluetooth device address.
106      * @param state Connection state.
107      * @param info the device's codec information.
108      * @since 12
109      */
OnCaptureConnectionStateChanged(const BluetoothRemoteDevice & device,int state,const A2dpCodecInfo & info)110     virtual void OnCaptureConnectionStateChanged(const BluetoothRemoteDevice &device, int state,
111         const A2dpCodecInfo &info)
112     {}
113 };
114 
115 /**
116  * @brief audio stream details.
117  *
118  * @since 11.0
119  */
120 struct A2dpStreamInfo {
121     int32_t sessionId;
122     int32_t streamType;
123     int32_t sampleRate;
124     bool isSpatialAudio;
125 };
126 
127 /**
128  * @brief a2dp audio stream encode type.
129  *
130  * @since 11.0
131  */
132 enum A2dpStreamEncodeType : uint8_t {
133     A2DP_STREAM_ENCODE_UNKNOWN = 0,
134     A2DP_STREAM_ENCODE_SOFTWARE,
135     A2DP_STREAM_ENCODE_HARDWARE,
136 };
137 
138 /**
139  * @brief A2dp source API.
140  *
141  * @since 6.0
142  */
143 class BLUETOOTH_API A2dpSource {
144 public:
145     /**
146      * @brief Get a2dp source instance.
147      *
148      * @return Returns an instance of a2dp source.
149      * @since 6.0
150      */
151     static A2dpSource *GetProfile();
152 
153     /**
154      * @brief Get devices by connection states.
155      *
156      * @param states The connection states of the bluetooth device.
157      * @return Returns devices that match the connection states.
158      * @since 6.0
159      */
160     int GetDevicesByStates(const std::vector<int> &states, std::vector<BluetoothRemoteDevice> &devices) const;
161 
162     /**
163      * @brief Get device connection state by address.
164      *
165      * @param device The address of the peer bluetooth device.
166      * @return Returns <b>A2DP_DISCONNECTED</b> if device connect state is disconnected;
167      *         Returns <b>A2DP_DISCONNECTING</b> if device connect state is disconnecting;
168      *         Returns <b>A2DP_CONNECTED</b> if device connect state is connected;
169      *         Returns <b>A2DP_CONNECTING</b> if device connect state is connecting;
170      *         Returns <b>A2DP_INVALID_STATUS</b> if can not find peer device.
171      * @since 6.0
172      */
173     int GetDeviceState(const BluetoothRemoteDevice &device, int &state) const;
174 
175     /**
176      * @brief Get device playing state by address when peer device is on connected.
177      *
178      * @param device The address of the peer bluetooth device.
179      * @return Returns <b>1</b> if device is on playing;
180      *         Returns <b>0</b> if device is not on playing.
181      * @since 6.0
182      */
183     int GetPlayingState(const BluetoothRemoteDevice &device) const;
184 
185     /**
186      * @brief Get device playing state by address when peer device is on connected.
187      *
188      * @param device The address of the peer bluetooth device.
189      * @param state The playing state of the peer bluetooth device.
190      * @return Returns operation result;
191      * @since 6.0
192      */
193     int GetPlayingState(const BluetoothRemoteDevice &device, int &state) const;
194 
195     /**
196      * @brief Connect to the peer bluetooth device.
197      *
198      * @param device The address of the peer bluetooth device.
199      * @return Returns <b>true</b> Perform normal connection processing.
200      *         Returns <b>false</b> Target device is on connected,or connecting,
201                                     or device is not allowed to connect,or the connection fails.
202      * @since 6.0
203      */
204     int32_t Connect(const BluetoothRemoteDevice &device);
205 
206     /**
207      * @brief Disconnect with the peer bluetooth service.
208      *
209      * @param device The address of the peer bluetooth device.
210      * @return Returns <b>true</b> if perform normal disconnection processing.
211      *         Returns <b>false</b> if target device is on disconnected,or disconnecting,or disconnection fails.
212      * @since 6.0
213      */
214     int32_t Disconnect(const BluetoothRemoteDevice &device);
215 
216     /**
217      * @brief Set target device as active device.
218      *
219      * @param device The address of the peer bluetooth device.
220      * @return Returns <b>RET_NO_ERROR</b> Target device has already been active, or perform normal setting processing.
221      *         Returns <b>RET_BAD_PARAM</b> Input error.
222      *         Returns <b>RET_BAD_STATUS</b> Target device is not on connected, or set fails.
223      * @since 6.0
224      */
225     int SetActiveSinkDevice(const BluetoothRemoteDevice &device);
226 
227     /**
228      * @brief Get active device.
229      * @return Returns active device.
230      * @since 6.0
231      */
232     const BluetoothRemoteDevice &GetActiveSinkDevice() const;
233 
234     /**
235      * @brief Set connection strategy for peer bluetooth device.
236      *        If peer device is connected and the policy is set not allowed,then perform disconnect operation.
237      *        If peer device is disconnected and the policy is set allowed,then perform connect operation.
238      *
239      * @param device The address of the peer bluetooth device.
240      * @param strategy The device connect strategy.
241      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
242      *         Returns <b>BT_ERR_PERMISSION_FAILED</b> Permission denied.
243      *         Returns <b>BT_ERR_INVALID_PARAM</b> Input error.
244      *         Returns <b>BT_ERR_INVALID_STATE</b> BT_ERR_INVALID_STATE.
245      *         Returns <b>BT_ERR_INTERNAL_ERROR</b> Operation failed.
246      * @since 6.0
247      */
248     int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy);
249 
250     /**
251      * @brief Get connection strategy of peer bluetooth device.
252      *
253      * @param device The address of the peer bluetooth device.
254      * @param strategy The device connect strategy.
255      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
256      *         Returns <b>BT_ERR_PERMISSION_FAILED</b> Permission denied.
257      *         Returns <b>BT_ERR_INVALID_PARAM</b> Input error.
258      *         Returns <b>BT_ERR_INVALID_STATE</b> BT_ERR_INVALID_STATE.
259      *         Returns <b>BT_ERR_INTERNAL_ERROR</b> Operation failed.
260      * @since 6.0
261      */
262     int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const;
263 
264     /**
265      * @brief Get codec status information of connected device.
266      *
267      * @param device The address of the bluetooth device.
268      * @return Returns codec status information of connected device.
269      * @since 6.0
270      */
271     A2dpCodecStatus GetCodecStatus(const BluetoothRemoteDevice &device) const;
272 
273     /**
274      * @brief Get the codec encoding preferences of the specified device.
275      *
276      * @param device The address of the bluetooth device.
277      * @param info The codec encoding information.
278      * @return Return the result getted.
279      * @since 6.0
280      */
281     int GetCodecPreference(const BluetoothRemoteDevice &device, A2dpCodecInfo &info);
282 
283     /**
284      * @brief Set the codec encoding preferences of the specified device.
285      *
286      * @param device The address of the bluetooth device.
287      * @param info The codec encoding information.
288      * @return Return the result setted.
289      * @since 6.0
290      */
291     int SetCodecPreference(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info);
292 
293     /**
294      * @brief Set whether enables the optional codec.
295      *
296      * @param device The address of the bluetooth device.
297      * @param isEnable Set true if enables the optional codec and set optional codec's priority high.
298      *                 Set false if disables the optional codec and set optional codec's priority low.
299      * @since 6.0
300      */
301     void SwitchOptionalCodecs(const BluetoothRemoteDevice &device, bool isEnable);
302 
303     /**
304      * @brief Get whether the peer bluetooth device supports optional codec.
305      *
306      * @param device The address of the bluetooth device.
307      * @return Returns <b>A2DP_OPTIONAL_SUPPORT</b> The device supports optional codec.
308      *         Returns <b>A2DP_OPTIONAL_NOT_SUPPORT</b> The device dosn't support optional codec.
309      *         Returns <b>A2DP_OPTIONAL_SUPPORT_UNKNOWN</b> Don't know if the device support optional codec.
310      * @since 6.0
311      */
312     int GetOptionalCodecsSupportState(const BluetoothRemoteDevice &device) const;
313 
314     /**
315      * @brief Audio start streaming.
316      *
317      * @param device The address of the bluetooth device.
318      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
319      *         Returns <b>RET_BAD_PARAM</b> Input error.
320      *         Returns <b>RET_BAD_STATUS</b> if the operation fails.
321      * @since 6.0
322      */
323     int StartPlaying(const BluetoothRemoteDevice &device);
324 
325     /**
326      * @brief Audio suspend streaming.
327      *
328      * @param device The address of the bluetooth device.
329      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
330      *         Returns <b>RET_BAD_PARAM</b> Input error.
331      *         Returns <b>RET_BAD_STATUS</b> if the operation fails.
332      * @since 6.0
333      */
334     int SuspendPlaying(const BluetoothRemoteDevice &device);
335 
336     /**
337      * @brief Audio stop streaming.
338      *
339      * @param device The address of the bluetooth device.
340      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
341      *         Returns <b>RET_BAD_PARAM</b> Input error.
342      *         Returns <b>RET_BAD_STATUS</b> if the operation fails.
343      * @since 6.0
344      */
345     int StopPlaying(const BluetoothRemoteDevice &device);
346 
347     /**
348      * @brief Register callback function of framework.
349      *
350      * @param observer Reference to the a2dp source observer.
351      * @since 6.0
352      */
353     void RegisterObserver(std::shared_ptr<A2dpSourceObserver> observer);
354 
355     /**
356      * @brief Deregister callback function of framework.
357      *
358      * @param observer Reference to the a2dp source observer.
359      * @since 6.0
360      */
361     void DeregisterObserver(std::shared_ptr<A2dpSourceObserver> observer);
362 
363     /**
364      * @brief Write the pcm data to a2dp source profile.
365      *
366      * @param data Pointer of the data.
367      * @param size Size of the data
368      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
369      *         Returns <b>RET_BAD_PARAM</b> Input error.
370      *         Returns <b>RET_BAD_STATUS</b> if the operation fails.
371      *         Returns <b>RET_NO_SPACE</b> if the buffer of a2dp source profile is full.
372      * @since 6.0
373      */
374     int WriteFrame(const uint8_t *data, uint32_t size);
375 
376     /**
377      * @brief Get the information of the current rendered position.
378      * @param device The address of the peer bluetooth device.
379      * @param[out] dalayValue is the delayed time
380      * @param[out] sendDataSize is the data size that has been sent
381      * @param[out] timeStamp is the current time stamp
382      * @since 6.0
383      */
384     int GetRenderPosition(const BluetoothRemoteDevice &device, uint32_t &delayValue, uint64_t &sendDataSize,
385                           uint32_t &timeStamp);
386 
387     /**
388      * @brief Audio start offload streaming for hardware encoding datapath.
389      *
390      * @param device remote bluetooth sink device.
391      * @return Returns general <b>enum BtErrCode</b> for the operation.
392      * @since 6.0
393      */
394     int OffloadStartPlaying(const BluetoothRemoteDevice &device, const std::vector<int32_t> &sessionsId);
395 
396     /**
397      * @brief Audio stop offload streaming for hardware encoding datapath.
398      *
399      * @param device remote bluetooth sink device.
400      * @return Returns general <b>enum BtErrCode</b> for the operation.
401      * @since 6.0
402      */
403     int OffloadStopPlaying(const BluetoothRemoteDevice &device, const std::vector<int32_t> &sessionsId);
404 
405     /**
406      * brief Get a2dp encoding data path information of connected sink device.
407      *
408      * @param device remote bluetooth sink device.
409      * @info streams detail information
410      * @return Returns <b>UNKNOWN_ENCODING_PATH: 0</b>, bt unable to judge encoding data path
411      *         Returns <b>SOFTWARE_ENCODING_PATH: 1</b>, a2dp audio encoding path should select a2dp hdi.
412      *         Returns <b>HARDWARE_ENCODING_PATH: 2</b>, a2dp audio encoding path should select a2dp offload hdi.
413      *         Returns general <b>enum BtErrCode</b> for the operation.
414      */
415     int A2dpOffloadSessionRequest(const BluetoothRemoteDevice &device, const std::vector<A2dpStreamInfo> &info);
416 
417     /**
418      * @brief Get A2dp Offload codec status information of connected device.
419      *
420      * @param device remote bluetooth sink device.
421      * @return a2dp offload configration information of connected device.
422      * @since 6.0
423      */
424     A2dpOffloadCodecStatus GetOffloadCodecStatus(const BluetoothRemoteDevice &device) const;
425 
426     /**
427      * Allow devices to automatically play music when connected.
428      *
429      * @param device Remote bluetooth sink device.
430      * @return Returns general <b>enum BtErrCode</b> for the operation.
431      * @since 12
432      */
433     int EnableAutoPlay(const BluetoothRemoteDevice &device);
434 
435     /**
436      * Restriction devices to play music within {@code duration} milliseconds of connection.
437      *
438      * @param device Remote bluetooth sink device.
439      * @param duration Restricted duration <milliseconds>.
440      * @return Returns general <b>enum BtErrCode</b> for the operation.
441      * @since 12
442      */
443     int DisableAutoPlay(const BluetoothRemoteDevice &device, const int duration);
444 
445     /**
446      * Obtains the duration for which automatic playback is disabled.
447      *
448      * @param device Remote bluetooth sink device.
449      * @param duration Restricted duration <milliseconds>.
450      * @return Returns general <b>enum BtErrCode</b> for the operation.
451      * @since 12
452      */
453     int GetAutoPlayDisabledDuration(const BluetoothRemoteDevice &device, int &duration);
454 
455     /**
456      * get virtual device list.
457      *
458      * @param devices virtual device list.
459      * @since 12
460      */
461     void GetVirtualDeviceList(std::vector<std::string> &devices);
462 private:
463     /**
464      * @brief A constructor used to create a a2dp source instance.
465      *
466      * @since 6.0
467      */
468     A2dpSource(void);
469 
470     /**
471      * @brief A destructor used to delete the a2dp source instance.
472      *
473      * @since 6.0
474      */
475     ~A2dpSource(void);
476     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(A2dpSource);
477     BLUETOOTH_DECLARE_IMPL();
478 
479 #ifdef DTFUZZ_TEST
480     friend class BluetoothNoDestructor<A2dpSource>;
481 #endif
482 };
483 }  // namespace Bluetooth
484 }  // namespace OHOS
485 #endif  // BLUETOOTH_A2DP_SRC_H
486