1 /* 2 * Copyright (C) 2021 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 Connect to the peer bluetooth device. 132 * 133 * @param device The address of the peer bluetooth device. 134 * @return Returns <b>true</b> Perform normal connection processing. 135 * Returns <b>false</b> Target device is on connected,or connecting, 136 or device is not allowed to connect,or the connection fails. 137 * @since 6.0 138 */ 139 bool Connect(const BluetoothRemoteDevice &device); 140 141 /** 142 * @brief Disconnect with the peer bluetooth service. 143 * 144 * @param device The address of the peer bluetooth device. 145 * @return Returns <b>true</b> if perform normal disconnection processing. 146 * Returns <b>false</b> if target device is on disconnected,or disconnecting,or disconnection fails. 147 * @since 6.0 148 */ 149 bool Disconnect(const BluetoothRemoteDevice &device); 150 151 /** 152 * @brief Set target device as active device. 153 * 154 * @param device The address of the peer bluetooth device. 155 * @return Returns <b>RET_NO_ERROR</b> Target device has already been active, or perform normal setting processing. 156 * Returns <b>RET_BAD_PARAM</b> Input error. 157 * Returns <b>RET_BAD_STATUS</b> Target device is not on connected, or set fails. 158 * @since 6.0 159 */ 160 int SetActiveSinkDevice(const BluetoothRemoteDevice &device); 161 162 /** 163 * @brief Get active device. 164 * @return Returns active device. 165 * @since 6.0 166 */ 167 const BluetoothRemoteDevice &GetActiveSinkDevice() const; 168 169 /** 170 * @brief Set connection strategy for peer bluetooth device. 171 * If peer device is connected and the policy is set not allowed,then perform disconnect operation. 172 * If peer device is disconnected and the policy is set allowed,then perform connect operation. 173 * 174 * @param device The address of the peer bluetooth device. 175 * @param strategy The device connect strategy. 176 * @return Returns <b>true</b> if the operation is successful. 177 * Returns <b>false</b> if the operation fails. 178 * @since 6.0 179 */ 180 bool SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy); 181 182 /** 183 * @brief Get connection strategy of peer bluetooth device. 184 * 185 * @param device The address of the peer bluetooth device. 186 * @return Returns <b>CONNECTION_ALLOWED</b> if the peer device is allowed to connect. 187 * Returns <b>CONNECTION_FORBIDDEN</b> if the peer device is not allowed to connect. 188 * Returns <b>CONNECTION_UNKNOWN</b> if the connection policy is unknown. 189 * @since 6.0 190 */ 191 int GetConnectStrategy(const BluetoothRemoteDevice &device) const; 192 193 /** 194 * @brief Get codec status information of connected device. 195 * 196 * @param device The address of the bluetooth device. 197 * @return Returns codec status information of connected device. 198 * @since 6.0 199 */ 200 A2dpCodecStatus GetCodecStatus(const BluetoothRemoteDevice &device) const; 201 202 /** 203 * @brief Set the codec encoding preferences of the specified device. 204 * 205 * @param device The address of the bluetooth device. 206 * @param info The codec encoding information. 207 * @return Return the result setted. 208 * @since 6.0 209 */ 210 int SetCodecPreference(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info); 211 212 /** 213 * @brief Set whether enables the optional codec. 214 * 215 * @param device The address of the bluetooth device. 216 * @param isEnable Set true if enables the optional codec and set optional codec's priority high. 217 * Set false if disables the optional codec and set optional codec's priority low. 218 * @since 6.0 219 */ 220 void SwitchOptionalCodecs(const BluetoothRemoteDevice &device, bool isEnable); 221 222 /** 223 * @brief Get whether the peer bluetooth device supports optional codec. 224 * 225 * @param device The address of the bluetooth device. 226 * @return Returns <b>A2DP_OPTIONAL_SUPPORT</b> The device supports optional codec. 227 * Returns <b>A2DP_OPTIONAL_NOT_SUPPORT</b> The device dosn't support optional codec. 228 * Returns <b>A2DP_OPTIONAL_SUPPORT_UNKNOWN</b> Don't know if the device support optional codec. 229 * @since 6.0 230 */ 231 int GetOptionalCodecsSupportState(const BluetoothRemoteDevice &device) const; 232 233 /** 234 * @brief Audio start streaming. 235 * 236 * @param device The address of the bluetooth device. 237 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 238 * Returns <b>RET_BAD_PARAM</b> Input error. 239 * Returns <b>RET_BAD_STATUS</b> if the operation fails. 240 * @since 6.0 241 */ 242 int StartPlaying(const BluetoothRemoteDevice &device); 243 244 /** 245 * @brief Audio suspend streaming. 246 * 247 * @param device The address of the bluetooth device. 248 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 249 * Returns <b>RET_BAD_PARAM</b> Input error. 250 * Returns <b>RET_BAD_STATUS</b> if the operation fails. 251 * @since 6.0 252 */ 253 int SuspendPlaying(const BluetoothRemoteDevice &device); 254 255 /** 256 * @brief Audio stop streaming. 257 * 258 * @param device The address of the bluetooth device. 259 * @return Returns <b>RET_NO_ERROR</b> if the operation is successful. 260 * Returns <b>RET_BAD_PARAM</b> Input error. 261 * Returns <b>RET_BAD_STATUS</b> if the operation fails. 262 * @since 6.0 263 */ 264 int StopPlaying(const BluetoothRemoteDevice &device); 265 266 /** 267 * @brief Register callback function of framework. 268 * 269 * @param observer Reference to the a2dp source observer. 270 * @since 6.0 271 */ 272 void RegisterObserver(A2dpSourceObserver *observer); 273 274 /** 275 * @brief Deregister callback function of framework. 276 * 277 * @param observer Reference to the a2dp source observer. 278 * @since 6.0 279 */ 280 void DeregisterObserver(A2dpSourceObserver *observer); 281 282 /** 283 * @brief Set audio configure. 284 * @param[in] addr: The address of peer device 285 * @param[in] sampleRate: Audio pcm sample rate 286 * @param[in] bits: Audio pcm bits 287 * @param[in] channel: Number of audio pcm channel 288 * @since 6.0 289 */ 290 void SetAudioConfigure(const BluetoothRemoteDevice &addr, uint32_t sampleRate, uint32_t bits, uint8_t channel); 291 292 int WriteFrame(const uint8_t *data, uint32_t size); 293 294 void GetRenderPosition(uint16_t &delayValue, uint16_t &sendDataSize, uint32_t &timeStamp); 295 296 private: 297 /** 298 * @brief A constructor used to create a a2dp source instance. 299 * 300 * @since 6.0 301 */ 302 A2dpSource(void); 303 304 /** 305 * @brief A destructor used to delete the a2dp source instance. 306 * 307 * @since 6.0 308 */ 309 ~A2dpSource(void); 310 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(A2dpSource); 311 BLUETOOTH_DECLARE_IMPL(); 312 }; 313 } // namespace Bluetooth 314 } // namespace OHOS 315 #endif // BLUETOOTH_A2DP_SRC_H 316