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 std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states) 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) 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>true</b> if the operation is successful. 187 * Returns <b>false</b> if the operation fails. 188 * @since 6.0 189 */ 190 bool SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy); 191 192 /** 193 * @brief Get connection strategy of peer bluetooth device. 194 * 195 * @param device The address of the peer bluetooth device. 196 * @return Returns <b>CONNECTION_ALLOWED</b> if the peer device is allowed to connect. 197 * Returns <b>CONNECTION_FORBIDDEN</b> if the peer device is not allowed to connect. 198 * Returns <b>CONNECTION_UNKNOWN</b> if the connection policy is unknown. 199 * @since 6.0 200 */ 201 int GetConnectStrategy(const BluetoothRemoteDevice &device) const; 202 203 /** 204 * @brief Get codec status information of connected device. 205 * 206 * @param device The address of the bluetooth device. 207 * @return Returns codec status information of connected device. 208 * @since 6.0 209 */ 210 A2dpCodecStatus GetCodecStatus(const BluetoothRemoteDevice &device) const; 211 212 /** 213 * @brief Set the codec encoding preferences of the specified device. 214 * 215 * @param device The address of the bluetooth device. 216 * @param info The codec encoding information. 217 * @return Return the result setted. 218 * @since 6.0 219 */ 220 int SetCodecPreference(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info); 221 222 /** 223 * @brief Set whether enables the optional codec. 224 * 225 * @param device The address of the bluetooth device. 226 * @param isEnable Set true if enables the optional codec and set optional codec's priority high. 227 * Set false if disables the optional codec and set optional codec's priority low. 228 * @since 6.0 229 */ 230 void SwitchOptionalCodecs(const BluetoothRemoteDevice &device, bool isEnable); 231 232 /** 233 * @brief Get whether the peer bluetooth device supports optional codec. 234 * 235 * @param device The address of the bluetooth device. 236 * @return Returns <b>A2DP_OPTIONAL_SUPPORT</b> The device supports optional codec. 237 * Returns <b>A2DP_OPTIONAL_NOT_SUPPORT</b> The device dosn't support optional codec. 238 * Returns <b>A2DP_OPTIONAL_SUPPORT_UNKNOWN</b> Don't know if the device support optional codec. 239 * @since 6.0 240 */ 241 int GetOptionalCodecsSupportState(const BluetoothRemoteDevice &device) const; 242 243 /** 244 * @brief Audio start streaming. 245 * 246 * @param device The address of the bluetooth device. 247 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 248 * Returns <b>RET_BAD_PARAM</b> Input error. 249 * Returns <b>RET_BAD_STATUS</b> if the operation fails. 250 * @since 6.0 251 */ 252 int StartPlaying(const BluetoothRemoteDevice &device); 253 254 /** 255 * @brief Audio suspend streaming. 256 * 257 * @param device The address of the bluetooth device. 258 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 259 * Returns <b>RET_BAD_PARAM</b> Input error. 260 * Returns <b>RET_BAD_STATUS</b> if the operation fails. 261 * @since 6.0 262 */ 263 int SuspendPlaying(const BluetoothRemoteDevice &device); 264 265 /** 266 * @brief Audio stop streaming. 267 * 268 * @param device The address of the bluetooth device. 269 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 270 * Returns <b>RET_BAD_PARAM</b> Input error. 271 * Returns <b>RET_BAD_STATUS</b> if the operation fails. 272 * @since 6.0 273 */ 274 int StopPlaying(const BluetoothRemoteDevice &device); 275 276 /** 277 * @brief Register callback function of framework. 278 * 279 * @param observer Reference to the a2dp source observer. 280 * @since 6.0 281 */ 282 void RegisterObserver(A2dpSourceObserver *observer); 283 284 /** 285 * @brief Deregister callback function of framework. 286 * 287 * @param observer Reference to the a2dp source observer. 288 * @since 6.0 289 */ 290 void DeregisterObserver(A2dpSourceObserver *observer); 291 292 /** 293 * @brief Write the pcm data to a2dp source profile. 294 * 295 * @param data Pointer of the data. 296 * @param size Size of the data 297 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 298 * Returns <b>RET_BAD_PARAM</b> Input error. 299 * Returns <b>RET_BAD_STATUS</b> if the operation fails. 300 * Returns <b>RET_NO_SPACE</b> if the buffer of a2dp source profile is full. 301 * @since 6.0 302 */ 303 int WriteFrame(const uint8_t *data, uint32_t size); 304 305 /** 306 * @brief Get the information of the current rendered position. 307 * @param[out] dalayValue is the delayed time 308 * @param[out] sendDataSize is the data size that has been sent 309 * @param[out] timeStamp is the current time stamp 310 * @since 6.0 311 */ 312 void GetRenderPosition(uint16_t &delayValue, uint16_t &sendDataSize, uint32_t &timeStamp); 313 314 private: 315 /** 316 * @brief A constructor used to create a a2dp source instance. 317 * 318 * @since 6.0 319 */ 320 A2dpSource(void); 321 322 /** 323 * @brief A destructor used to delete the a2dp source instance. 324 * 325 * @since 6.0 326 */ 327 ~A2dpSource(void); 328 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(A2dpSource); 329 BLUETOOTH_DECLARE_IMPL(); 330 }; 331 } // namespace Bluetooth 332 } // namespace OHOS 333 #endif // BLUETOOTH_A2DP_SRC_H 334