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 * @brief Media Stack Changed observer. 84 * @param device bluetooth device address. 85 * @param Action on the device. 86 * @since 11.0 87 */ OnMediaStackChanged(const BluetoothRemoteDevice & device,int action)88 virtual void OnMediaStackChanged(const BluetoothRemoteDevice &device, int action) 89 {} 90 }; 91 92 /** 93 * @brief audio stream details. 94 * 95 * @since 11.0 96 */ 97 struct A2dpStreamInfo { 98 int32_t sessionId; 99 int32_t streamType; 100 int32_t sampleRate; 101 bool isSpatialAudio; 102 }; 103 104 /** 105 * @brief a2dp audio stream encode type. 106 * 107 * @since 11.0 108 */ 109 enum A2dpStreamEncodeType : uint8_t { 110 A2DP_STREAM_ENCODE_UNKNOWN = 0, 111 A2DP_STREAM_ENCODE_SOFTWARE, 112 A2DP_STREAM_ENCODE_HARDWARE, 113 }; 114 115 /** 116 * @brief A2dp source API. 117 * 118 * @since 6.0 119 */ 120 class BLUETOOTH_API A2dpSource { 121 public: 122 /** 123 * @brief Get a2dp source instance. 124 * 125 * @return Returns an instance of a2dp source. 126 * @since 6.0 127 */ 128 static A2dpSource *GetProfile(); 129 130 /** 131 * @brief Get devices by connection states. 132 * 133 * @param states The connection states of the bluetooth device. 134 * @return Returns devices that match the connection states. 135 * @since 6.0 136 */ 137 int GetDevicesByStates(const std::vector<int> &states, std::vector<BluetoothRemoteDevice> &devices) const; 138 139 /** 140 * @brief Get device connection state by address. 141 * 142 * @param device The address of the peer bluetooth device. 143 * @return Returns <b>A2DP_DISCONNECTED</b> if device connect state is disconnected; 144 * Returns <b>A2DP_DISCONNECTING</b> if device connect state is disconnecting; 145 * Returns <b>A2DP_CONNECTED</b> if device connect state is connected; 146 * Returns <b>A2DP_CONNECTING</b> if device connect state is connecting; 147 * Returns <b>A2DP_INVALID_STATUS</b> if can not find peer device. 148 * @since 6.0 149 */ 150 int GetDeviceState(const BluetoothRemoteDevice &device, int &state) const; 151 152 /** 153 * @brief Get device playing state by address when peer device is on connected. 154 * 155 * @param device The address of the peer bluetooth device. 156 * @return Returns <b>1</b> if device is on playing; 157 * Returns <b>0</b> if device is not on playing. 158 * @since 6.0 159 */ 160 int GetPlayingState(const BluetoothRemoteDevice &device) const; 161 162 /** 163 * @brief Get device playing state by address when peer device is on connected. 164 * 165 * @param device The address of the peer bluetooth device. 166 * @param state The playing state of the peer bluetooth device. 167 * @return Returns operation result; 168 * @since 6.0 169 */ 170 int GetPlayingState(const BluetoothRemoteDevice &device, int &state) const; 171 172 /** 173 * @brief Connect to the peer bluetooth device. 174 * 175 * @param device The address of the peer bluetooth device. 176 * @return Returns <b>true</b> Perform normal connection processing. 177 * Returns <b>false</b> Target device is on connected,or connecting, 178 or device is not allowed to connect,or the connection fails. 179 * @since 6.0 180 */ 181 int32_t Connect(const BluetoothRemoteDevice &device); 182 183 /** 184 * @brief Disconnect with the peer bluetooth service. 185 * 186 * @param device The address of the peer bluetooth device. 187 * @return Returns <b>true</b> if perform normal disconnection processing. 188 * Returns <b>false</b> if target device is on disconnected,or disconnecting,or disconnection fails. 189 * @since 6.0 190 */ 191 int32_t Disconnect(const BluetoothRemoteDevice &device); 192 193 /** 194 * @brief Set target device as active device. 195 * 196 * @param device The address of the peer bluetooth device. 197 * @return Returns <b>RET_NO_ERROR</b> Target device has already been active, or perform normal setting processing. 198 * Returns <b>RET_BAD_PARAM</b> Input error. 199 * Returns <b>RET_BAD_STATUS</b> Target device is not on connected, or set fails. 200 * @since 6.0 201 */ 202 int SetActiveSinkDevice(const BluetoothRemoteDevice &device); 203 204 /** 205 * @brief Get active device. 206 * @return Returns active device. 207 * @since 6.0 208 */ 209 const BluetoothRemoteDevice &GetActiveSinkDevice() const; 210 211 /** 212 * @brief Set connection strategy for peer bluetooth device. 213 * If peer device is connected and the policy is set not allowed,then perform disconnect operation. 214 * If peer device is disconnected and the policy is set allowed,then perform connect operation. 215 * 216 * @param device The address of the peer bluetooth device. 217 * @param strategy The device connect strategy. 218 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 219 * Returns <b>BT_ERR_PERMISSION_FAILED</b> Permission denied. 220 * Returns <b>BT_ERR_INVALID_PARAM</b> Input error. 221 * Returns <b>BT_ERR_INVALID_STATE</b> BT_ERR_INVALID_STATE. 222 * Returns <b>BT_ERR_INTERNAL_ERROR</b> Operation failed. 223 * @since 6.0 224 */ 225 int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy); 226 227 /** 228 * @brief Get connection strategy of peer bluetooth device. 229 * 230 * @param device The address of the peer bluetooth device. 231 * @param strategy The device connect strategy. 232 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 233 * Returns <b>BT_ERR_PERMISSION_FAILED</b> Permission denied. 234 * Returns <b>BT_ERR_INVALID_PARAM</b> Input error. 235 * Returns <b>BT_ERR_INVALID_STATE</b> BT_ERR_INVALID_STATE. 236 * Returns <b>BT_ERR_INTERNAL_ERROR</b> Operation failed. 237 * @since 6.0 238 */ 239 int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const; 240 241 /** 242 * @brief Get codec status information of connected device. 243 * 244 * @param device The address of the bluetooth device. 245 * @return Returns codec status information of connected device. 246 * @since 6.0 247 */ 248 A2dpCodecStatus GetCodecStatus(const BluetoothRemoteDevice &device) const; 249 250 /** 251 * @brief Get the codec encoding preferences of the specified device. 252 * 253 * @param device The address of the bluetooth device. 254 * @param info The codec encoding information. 255 * @return Return the result getted. 256 * @since 6.0 257 */ 258 int GetCodecPreference(const BluetoothRemoteDevice &device, A2dpCodecInfo &info); 259 260 /** 261 * @brief Set the codec encoding preferences of the specified device. 262 * 263 * @param device The address of the bluetooth device. 264 * @param info The codec encoding information. 265 * @return Return the result setted. 266 * @since 6.0 267 */ 268 int SetCodecPreference(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info); 269 270 /** 271 * @brief Set whether enables the optional codec. 272 * 273 * @param device The address of the bluetooth device. 274 * @param isEnable Set true if enables the optional codec and set optional codec's priority high. 275 * Set false if disables the optional codec and set optional codec's priority low. 276 * @since 6.0 277 */ 278 void SwitchOptionalCodecs(const BluetoothRemoteDevice &device, bool isEnable); 279 280 /** 281 * @brief Get whether the peer bluetooth device supports optional codec. 282 * 283 * @param device The address of the bluetooth device. 284 * @return Returns <b>A2DP_OPTIONAL_SUPPORT</b> The device supports optional codec. 285 * Returns <b>A2DP_OPTIONAL_NOT_SUPPORT</b> The device dosn't support optional codec. 286 * Returns <b>A2DP_OPTIONAL_SUPPORT_UNKNOWN</b> Don't know if the device support optional codec. 287 * @since 6.0 288 */ 289 int GetOptionalCodecsSupportState(const BluetoothRemoteDevice &device) const; 290 291 /** 292 * @brief Audio start streaming. 293 * 294 * @param device The address of the bluetooth device. 295 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 296 * Returns <b>RET_BAD_PARAM</b> Input error. 297 * Returns <b>RET_BAD_STATUS</b> if the operation fails. 298 * @since 6.0 299 */ 300 int StartPlaying(const BluetoothRemoteDevice &device); 301 302 /** 303 * @brief Audio suspend streaming. 304 * 305 * @param device The address of the bluetooth device. 306 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 307 * Returns <b>RET_BAD_PARAM</b> Input error. 308 * Returns <b>RET_BAD_STATUS</b> if the operation fails. 309 * @since 6.0 310 */ 311 int SuspendPlaying(const BluetoothRemoteDevice &device); 312 313 /** 314 * @brief Audio stop streaming. 315 * 316 * @param device The address of the bluetooth device. 317 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 318 * Returns <b>RET_BAD_PARAM</b> Input error. 319 * Returns <b>RET_BAD_STATUS</b> if the operation fails. 320 * @since 6.0 321 */ 322 int StopPlaying(const BluetoothRemoteDevice &device); 323 324 /** 325 * @brief Register callback function of framework. 326 * 327 * @param observer Reference to the a2dp source observer. 328 * @since 6.0 329 */ 330 void RegisterObserver(std::shared_ptr<A2dpSourceObserver> observer); 331 332 /** 333 * @brief Deregister callback function of framework. 334 * 335 * @param observer Reference to the a2dp source observer. 336 * @since 6.0 337 */ 338 void DeregisterObserver(std::shared_ptr<A2dpSourceObserver> observer); 339 340 /** 341 * @brief Write the pcm data to a2dp source profile. 342 * 343 * @param data Pointer of the data. 344 * @param size Size of the data 345 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 346 * Returns <b>RET_BAD_PARAM</b> Input error. 347 * Returns <b>RET_BAD_STATUS</b> if the operation fails. 348 * Returns <b>RET_NO_SPACE</b> if the buffer of a2dp source profile is full. 349 * @since 6.0 350 */ 351 int WriteFrame(const uint8_t *data, uint32_t size); 352 353 /** 354 * @brief Get the information of the current rendered position. 355 * @param[out] dalayValue is the delayed time 356 * @param[out] sendDataSize is the data size that has been sent 357 * @param[out] timeStamp is the current time stamp 358 * @since 6.0 359 */ 360 void GetRenderPosition(uint16_t &delayValue, uint16_t &sendDataSize, uint32_t &timeStamp); 361 362 /** 363 * @brief Audio start offload streaming for hardware encoding datapath. 364 * 365 * @param device remote bluetooth sink device. 366 * @return Returns general <b>enum BtErrCode</b> for the operation. 367 * @since 6.0 368 */ 369 int OffloadStartPlaying(const BluetoothRemoteDevice &device, const std::vector<int32_t> &sessionsId); 370 371 /** 372 * @brief Audio stop offload streaming for hardware encoding datapath. 373 * 374 * @param device remote bluetooth sink device. 375 * @return Returns general <b>enum BtErrCode</b> for the operation. 376 * @since 6.0 377 */ 378 int OffloadStopPlaying(const BluetoothRemoteDevice &device, const std::vector<int32_t> &sessionsId); 379 380 /** 381 * brief Get a2dp encoding data path information of connected sink device. 382 * 383 * @param device remote bluetooth sink device. 384 * @info streams detail information 385 * @return Returns <b>UNKNOWN_ENCODING_PATH: 0</b>, bt unable to judge encoding data path 386 * Returns <b>SOFTWARE_ENCODING_PATH: 1</b>, a2dp audio encoding path should select a2dp hdi. 387 * Returns <b>HARDWARE_ENCODING_PATH: 2</b>, a2dp audio encoding path should select a2dp offload hdi. 388 * Returns general <b>enum BtErrCode</b> for the operation. 389 */ 390 int A2dpOffloadSessionRequest(const BluetoothRemoteDevice &device, const std::vector<A2dpStreamInfo> &info); 391 392 /** 393 * @brief Get A2dp Offload codec status information of connected device. 394 * 395 * @param device remote bluetooth sink device. 396 * @return a2dp offload configration information of connected device. 397 * @since 6.0 398 */ 399 A2dpOffloadCodecStatus GetOffloadCodecStatus(const BluetoothRemoteDevice &device) const; 400 401 /** 402 * @brief The external process calls the A2dpSrc profile interface before the Bluetooth process starts. At this 403 * time, it needs to monitor the start of the Bluetooth process, and then call this interface to initialize the 404 * A2dpSrc proflie. 405 */ 406 void Init(); 407 408 private: 409 /** 410 * @brief A constructor used to create a a2dp source instance. 411 * 412 * @since 6.0 413 */ 414 A2dpSource(void); 415 416 /** 417 * @brief A destructor used to delete the a2dp source instance. 418 * 419 * @since 6.0 420 */ 421 ~A2dpSource(void); 422 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(A2dpSource); 423 BLUETOOTH_DECLARE_IMPL(); 424 }; 425 } // namespace Bluetooth 426 } // namespace OHOS 427 #endif // BLUETOOTH_A2DP_SRC_H 428