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