• 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 
35 namespace OHOS {
36 namespace Bluetooth {
37 /**
38  * @brief A2dp source API callback function.
39  *
40  * @since 6.0
41  */
42 class A2dpSourceObserver {
43 public:
44     /**
45      * @brief A destructor used to delete the a2dp source Observer instance.
46      *
47      * @since 6.0
48      */
49     virtual ~A2dpSourceObserver() = default;
50 
51     /**
52      * @brief The callback function after device's playing state changed.
53      *
54      * @param device  the remote bluetooth device.
55      * @param playingState  the playing state after changing.
56      * @param error  the error information.
57      * @since 6.0
58      */
OnPlayingStatusChanged(const BluetoothRemoteDevice & device,int playingState,int error)59     virtual void OnPlayingStatusChanged(const BluetoothRemoteDevice &device, int playingState, int error)
60     {}
61 
62     /**
63      * @brief The callback function after device's codec information changed.
64      *
65      * @param device  the remote bluetooth device.
66      * @param info  the device's codec information.
67      * @param error  the error information.
68      * @since 6.0
69      */
OnConfigurationChanged(const BluetoothRemoteDevice & device,const A2dpCodecInfo & info,int error)70     virtual void OnConfigurationChanged(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info, int error)
71     {}
72 
73     /**
74      * @brief ConnectionState Changed observer.
75      * @param device bluetooth device address.
76      * @param state Connection state.
77      * @since 6.0
78      */
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state)79     virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state)
80     {}
81 };
82 
83 /**
84  * @brief A2dp source API.
85  *
86  * @since 6.0
87  */
88 class BLUETOOTH_API A2dpSource {
89 public:
90     /**
91      * @brief Get a2dp source instance.
92      *
93      * @return Returns an instance of a2dp source.
94      * @since 6.0
95      */
96     static A2dpSource *GetProfile();
97 
98     /**
99      * @brief Get devices by connection states.
100      *
101      * @param states The connection states of the bluetooth device.
102      * @return Returns devices that match the connection states.
103      * @since 6.0
104      */
105     int GetDevicesByStates(const std::vector<int> &states, std::vector<BluetoothRemoteDevice> &devices) const;
106 
107     /**
108      * @brief Get device connection state by address.
109      *
110      * @param device The address of the peer bluetooth device.
111      * @return Returns <b>A2DP_DISCONNECTED</b> if device connect state is disconnected;
112      *         Returns <b>A2DP_DISCONNECTING</b> if device connect state is disconnecting;
113      *         Returns <b>A2DP_CONNECTED</b> if device connect state is connected;
114      *         Returns <b>A2DP_CONNECTING</b> if device connect state is connecting;
115      *         Returns <b>A2DP_INVALID_STATUS</b> if can not find peer device.
116      * @since 6.0
117      */
118     int GetDeviceState(const BluetoothRemoteDevice &device, int &state) const;
119 
120     /**
121      * @brief Get device playing state by address when peer device is on connected.
122      *
123      * @param device The address of the peer bluetooth device.
124      * @return Returns <b>1</b> if device is on playing;
125      *         Returns <b>0</b> if device is not on playing.
126      * @since 6.0
127      */
128     int GetPlayingState(const BluetoothRemoteDevice &device) const;
129 
130     /**
131      * @brief Get device playing state by address when peer device is on connected.
132      *
133      * @param device The address of the peer bluetooth device.
134      * @param state The playing state of the peer bluetooth device.
135      * @return Returns operation result;
136      * @since 6.0
137      */
138     int GetPlayingState(const BluetoothRemoteDevice &device, int &state) const;
139 
140     /**
141      * @brief Connect to the peer bluetooth device.
142      *
143      * @param device The address of the peer bluetooth device.
144      * @return Returns <b>true</b> Perform normal connection processing.
145      *         Returns <b>false</b> Target device is on connected,or connecting,
146                                     or device is not allowed to connect,or the connection fails.
147      * @since 6.0
148      */
149     int32_t Connect(const BluetoothRemoteDevice &device);
150 
151     /**
152      * @brief Disconnect with the peer bluetooth service.
153      *
154      * @param device The address of the peer bluetooth device.
155      * @return Returns <b>true</b> if perform normal disconnection processing.
156      *         Returns <b>false</b> if target device is on disconnected,or disconnecting,or disconnection fails.
157      * @since 6.0
158      */
159     int32_t Disconnect(const BluetoothRemoteDevice &device);
160 
161     /**
162      * @brief Set target device as active device.
163      *
164      * @param device The address of the peer bluetooth device.
165      * @return Returns <b>RET_NO_ERROR</b> Target device has already been active, or perform normal setting processing.
166      *         Returns <b>RET_BAD_PARAM</b> Input error.
167      *         Returns <b>RET_BAD_STATUS</b> Target device is not on connected, or set fails.
168      * @since 6.0
169      */
170     int SetActiveSinkDevice(const BluetoothRemoteDevice &device);
171 
172     /**
173      * @brief Get active device.
174      * @return Returns active device.
175      * @since 6.0
176      */
177     const BluetoothRemoteDevice &GetActiveSinkDevice() const;
178 
179     /**
180      * @brief Set connection strategy for peer bluetooth device.
181      *        If peer device is connected and the policy is set not allowed,then perform disconnect operation.
182      *        If peer device is disconnected and the policy is set allowed,then perform connect operation.
183      *
184      * @param device The address of the peer bluetooth device.
185      * @param strategy The device connect strategy.
186      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
187      *         Returns <b>BT_ERR_PERMISSION_FAILED</b> Permission denied.
188      *         Returns <b>BT_ERR_INVALID_PARAM</b> Input error.
189      *         Returns <b>BT_ERR_INVALID_STATE</b> BT_ERR_INVALID_STATE.
190      *         Returns <b>BT_ERR_INTERNAL_ERROR</b> Operation failed.
191      * @since 6.0
192      */
193     int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy);
194 
195     /**
196      * @brief Get connection strategy of peer bluetooth device.
197      *
198      * @param device The address of the peer bluetooth device.
199      * @param strategy The device connect strategy.
200      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
201      *         Returns <b>BT_ERR_PERMISSION_FAILED</b> Permission denied.
202      *         Returns <b>BT_ERR_INVALID_PARAM</b> Input error.
203      *         Returns <b>BT_ERR_INVALID_STATE</b> BT_ERR_INVALID_STATE.
204      *         Returns <b>BT_ERR_INTERNAL_ERROR</b> Operation failed.
205      * @since 6.0
206      */
207     int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const;
208 
209     /**
210      * @brief Get codec status information of connected device.
211      *
212      * @param device The address of the bluetooth device.
213      * @return Returns codec status information of connected device.
214      * @since 6.0
215      */
216     A2dpCodecStatus GetCodecStatus(const BluetoothRemoteDevice &device) const;
217 
218     /**
219      * @brief Set the codec encoding preferences of the specified device.
220      *
221      * @param device The address of the bluetooth device.
222      * @param info The codec encoding information.
223      * @return Return the result setted.
224      * @since 6.0
225      */
226     int SetCodecPreference(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info);
227 
228     /**
229      * @brief Set whether enables the optional codec.
230      *
231      * @param device The address of the bluetooth device.
232      * @param isEnable Set true if enables the optional codec and set optional codec's priority high.
233      *                 Set false if disables the optional codec and set optional codec's priority low.
234      * @since 6.0
235      */
236     void SwitchOptionalCodecs(const BluetoothRemoteDevice &device, bool isEnable);
237 
238     /**
239      * @brief Get whether the peer bluetooth device supports optional codec.
240      *
241      * @param device The address of the bluetooth device.
242      * @return Returns <b>A2DP_OPTIONAL_SUPPORT</b> The device supports optional codec.
243      *         Returns <b>A2DP_OPTIONAL_NOT_SUPPORT</b> The device dosn't support optional codec.
244      *         Returns <b>A2DP_OPTIONAL_SUPPORT_UNKNOWN</b> Don't know if the device support optional codec.
245      * @since 6.0
246      */
247     int GetOptionalCodecsSupportState(const BluetoothRemoteDevice &device) const;
248 
249     /**
250      * @brief Audio start streaming.
251      *
252      * @param device The address of the bluetooth device.
253      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
254      *         Returns <b>RET_BAD_PARAM</b> Input error.
255      *         Returns <b>RET_BAD_STATUS</b> if the operation fails.
256      * @since 6.0
257      */
258     int StartPlaying(const BluetoothRemoteDevice &device);
259 
260     /**
261      * @brief Audio suspend streaming.
262      *
263      * @param device The address of the bluetooth device.
264      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
265      *         Returns <b>RET_BAD_PARAM</b> Input error.
266      *         Returns <b>RET_BAD_STATUS</b> if the operation fails.
267      * @since 6.0
268      */
269     int SuspendPlaying(const BluetoothRemoteDevice &device);
270 
271     /**
272      * @brief Audio stop streaming.
273      *
274      * @param device The address of the bluetooth device.
275      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
276      *         Returns <b>RET_BAD_PARAM</b> Input error.
277      *         Returns <b>RET_BAD_STATUS</b> if the operation fails.
278      * @since 6.0
279      */
280     int StopPlaying(const BluetoothRemoteDevice &device);
281 
282     /**
283      * @brief Register callback function of framework.
284      *
285      * @param observer Reference to the a2dp source observer.
286      * @since 6.0
287      */
288     void RegisterObserver(A2dpSourceObserver *observer);
289 
290     /**
291      * @brief Deregister callback function of framework.
292      *
293      * @param observer Reference to the a2dp source observer.
294      * @since 6.0
295      */
296     void DeregisterObserver(A2dpSourceObserver *observer);
297 
298     /**
299      * @brief Write the pcm data to a2dp source profile.
300      *
301      * @param data Pointer of the data.
302      * @param size Size of the data
303      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
304      *         Returns <b>RET_BAD_PARAM</b> Input error.
305      *         Returns <b>RET_BAD_STATUS</b> if the operation fails.
306      *         Returns <b>RET_NO_SPACE</b> if the buffer of a2dp source profile is full.
307      * @since 6.0
308      */
309     int WriteFrame(const uint8_t *data, uint32_t size);
310 
311     /**
312      * @brief Get the information of the current rendered position.
313      * @param[out] dalayValue is the delayed time
314      * @param[out] sendDataSize is the data size that has been sent
315      * @param[out] timeStamp is the current time stamp
316      * @since 6.0
317      */
318     void GetRenderPosition(uint16_t &delayValue, uint16_t &sendDataSize, uint32_t &timeStamp);
319 
320     /**
321      * @brief The external process calls the A2dpSrc profile interface before the Bluetooth process starts. At this
322      * time, it needs to monitor the start of the Bluetooth process, and then call this interface to initialize the
323      * A2dpSrc proflie.
324      */
325     void Init();
326 
327 private:
328     /**
329      * @brief A constructor used to create a a2dp source instance.
330      *
331      * @since 6.0
332      */
333     A2dpSource(void);
334 
335     /**
336      * @brief A destructor used to delete the a2dp source instance.
337      *
338      * @since 6.0
339      */
340     ~A2dpSource(void);
341     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(A2dpSource);
342     BLUETOOTH_DECLARE_IMPL();
343 };
344 }  // namespace Bluetooth
345 }  // namespace OHOS
346 #endif  // BLUETOOTH_A2DP_SRC_H
347