/* * Copyright (C) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @addtogroup Bluetooth * @{ * * @brief The framework interface and callback function of a2dp source are defined. * * @since 6 */ #ifndef BLUETOOTH_A2DP_SRC_H #define BLUETOOTH_A2DP_SRC_H #include #include "bluetooth_def.h" #include "bluetooth_types.h" #include "bluetooth_remote_device.h" #include "bluetooth_a2dp_codec.h" #include "bluetooth_no_destructor.h" namespace OHOS { namespace Bluetooth { /** * @brief A2dp source API callback function. * * @since 6.0 */ class A2dpSourceObserver { public: /** * @brief A destructor used to delete the a2dp source Observer instance. * * @since 6.0 */ virtual ~A2dpSourceObserver() = default; /** * @brief The callback function after device's playing state changed. * * @param device the remote bluetooth device. * @param playingState the playing state after changing. * @param error the error information. * @since 6.0 */ virtual void OnPlayingStatusChanged(const BluetoothRemoteDevice &device, int playingState, int error) {} /** * @brief The callback function after device's codec information changed. * * @param device the remote bluetooth device. * @param info the device's codec information. * @param error the error information. * @since 6.0 */ virtual void OnConfigurationChanged(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info, int error) {} /** * @brief ConnectionState Changed observer. * @param device bluetooth device address. * @param state Connection state. * @param cause Connecton cause. * @since 12 */ virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause) {} /** * @brief Media Stack Changed observer. * @param device bluetooth device address. * @param Action on the device. * @since 11.0 */ virtual void OnMediaStackChanged(const BluetoothRemoteDevice &device, int action) {} /** * @brief The observer function to notify virtual device changed. * * @param Action on the device. * @param device bluetooth device address. * @since 12 */ virtual void OnVirtualDeviceChanged(int32_t action, std::string address) {} }; /** * @brief audio stream details. * * @since 11.0 */ struct A2dpStreamInfo { int32_t sessionId; int32_t streamType; int32_t sampleRate; bool isSpatialAudio; }; /** * @brief a2dp audio stream encode type. * * @since 11.0 */ enum A2dpStreamEncodeType : uint8_t { A2DP_STREAM_ENCODE_UNKNOWN = 0, A2DP_STREAM_ENCODE_SOFTWARE, A2DP_STREAM_ENCODE_HARDWARE, }; /** * @brief A2dp source API. * * @since 6.0 */ class BLUETOOTH_API A2dpSource { public: /** * @brief Get a2dp source instance. * * @return Returns an instance of a2dp source. * @since 6.0 */ static A2dpSource *GetProfile(); /** * @brief Get devices by connection states. * * @param states The connection states of the bluetooth device. * @return Returns devices that match the connection states. * @since 6.0 */ int GetDevicesByStates(const std::vector &states, std::vector &devices) const; /** * @brief Get device connection state by address. * * @param device The address of the peer bluetooth device. * @return Returns A2DP_DISCONNECTED if device connect state is disconnected; * Returns A2DP_DISCONNECTING if device connect state is disconnecting; * Returns A2DP_CONNECTED if device connect state is connected; * Returns A2DP_CONNECTING if device connect state is connecting; * Returns A2DP_INVALID_STATUS if can not find peer device. * @since 6.0 */ int GetDeviceState(const BluetoothRemoteDevice &device, int &state) const; /** * @brief Get device playing state by address when peer device is on connected. * * @param device The address of the peer bluetooth device. * @return Returns 1 if device is on playing; * Returns 0 if device is not on playing. * @since 6.0 */ int GetPlayingState(const BluetoothRemoteDevice &device) const; /** * @brief Get device playing state by address when peer device is on connected. * * @param device The address of the peer bluetooth device. * @param state The playing state of the peer bluetooth device. * @return Returns operation result; * @since 6.0 */ int GetPlayingState(const BluetoothRemoteDevice &device, int &state) const; /** * @brief Connect to the peer bluetooth device. * * @param device The address of the peer bluetooth device. * @return Returns true Perform normal connection processing. * Returns false Target device is on connected,or connecting, or device is not allowed to connect,or the connection fails. * @since 6.0 */ int32_t Connect(const BluetoothRemoteDevice &device); /** * @brief Disconnect with the peer bluetooth service. * * @param device The address of the peer bluetooth device. * @return Returns true if perform normal disconnection processing. * Returns false if target device is on disconnected,or disconnecting,or disconnection fails. * @since 6.0 */ int32_t Disconnect(const BluetoothRemoteDevice &device); /** * @brief Set target device as active device. * * @param device The address of the peer bluetooth device. * @return Returns RET_NO_ERROR Target device has already been active, or perform normal setting processing. * Returns RET_BAD_PARAM Input error. * Returns RET_BAD_STATUS Target device is not on connected, or set fails. * @since 6.0 */ int SetActiveSinkDevice(const BluetoothRemoteDevice &device); /** * @brief Get active device. * @return Returns active device. * @since 6.0 */ const BluetoothRemoteDevice &GetActiveSinkDevice() const; /** * @brief Set connection strategy for peer bluetooth device. * If peer device is connected and the policy is set not allowed,then perform disconnect operation. * If peer device is disconnected and the policy is set allowed,then perform connect operation. * * @param device The address of the peer bluetooth device. * @param strategy The device connect strategy. * @return Returns RET_NO_ERROR if the operation is successful. * Returns BT_ERR_PERMISSION_FAILED Permission denied. * Returns BT_ERR_INVALID_PARAM Input error. * Returns BT_ERR_INVALID_STATE BT_ERR_INVALID_STATE. * Returns BT_ERR_INTERNAL_ERROR Operation failed. * @since 6.0 */ int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy); /** * @brief Get connection strategy of peer bluetooth device. * * @param device The address of the peer bluetooth device. * @param strategy The device connect strategy. * @return Returns RET_NO_ERROR if the operation is successful. * Returns BT_ERR_PERMISSION_FAILED Permission denied. * Returns BT_ERR_INVALID_PARAM Input error. * Returns BT_ERR_INVALID_STATE BT_ERR_INVALID_STATE. * Returns BT_ERR_INTERNAL_ERROR Operation failed. * @since 6.0 */ int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const; /** * @brief Get codec status information of connected device. * * @param device The address of the bluetooth device. * @return Returns codec status information of connected device. * @since 6.0 */ A2dpCodecStatus GetCodecStatus(const BluetoothRemoteDevice &device) const; /** * @brief Get the codec encoding preferences of the specified device. * * @param device The address of the bluetooth device. * @param info The codec encoding information. * @return Return the result getted. * @since 6.0 */ int GetCodecPreference(const BluetoothRemoteDevice &device, A2dpCodecInfo &info); /** * @brief Set the codec encoding preferences of the specified device. * * @param device The address of the bluetooth device. * @param info The codec encoding information. * @return Return the result setted. * @since 6.0 */ int SetCodecPreference(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info); /** * @brief Set whether enables the optional codec. * * @param device The address of the bluetooth device. * @param isEnable Set true if enables the optional codec and set optional codec's priority high. * Set false if disables the optional codec and set optional codec's priority low. * @since 6.0 */ void SwitchOptionalCodecs(const BluetoothRemoteDevice &device, bool isEnable); /** * @brief Get whether the peer bluetooth device supports optional codec. * * @param device The address of the bluetooth device. * @return Returns A2DP_OPTIONAL_SUPPORT The device supports optional codec. * Returns A2DP_OPTIONAL_NOT_SUPPORT The device dosn't support optional codec. * Returns A2DP_OPTIONAL_SUPPORT_UNKNOWN Don't know if the device support optional codec. * @since 6.0 */ int GetOptionalCodecsSupportState(const BluetoothRemoteDevice &device) const; /** * @brief Audio start streaming. * * @param device The address of the bluetooth device. * @return Returns RET_NO_ERROR if the operation is successful. * Returns RET_BAD_PARAM Input error. * Returns RET_BAD_STATUS if the operation fails. * @since 6.0 */ int StartPlaying(const BluetoothRemoteDevice &device); /** * @brief Audio suspend streaming. * * @param device The address of the bluetooth device. * @return Returns RET_NO_ERROR if the operation is successful. * Returns RET_BAD_PARAM Input error. * Returns RET_BAD_STATUS if the operation fails. * @since 6.0 */ int SuspendPlaying(const BluetoothRemoteDevice &device); /** * @brief Audio stop streaming. * * @param device The address of the bluetooth device. * @return Returns RET_NO_ERROR if the operation is successful. * Returns RET_BAD_PARAM Input error. * Returns RET_BAD_STATUS if the operation fails. * @since 6.0 */ int StopPlaying(const BluetoothRemoteDevice &device); /** * @brief Register callback function of framework. * * @param observer Reference to the a2dp source observer. * @since 6.0 */ void RegisterObserver(std::shared_ptr observer); /** * @brief Deregister callback function of framework. * * @param observer Reference to the a2dp source observer. * @since 6.0 */ void DeregisterObserver(std::shared_ptr observer); /** * @brief Write the pcm data to a2dp source profile. * * @param data Pointer of the data. * @param size Size of the data * @return Returns RET_NO_ERROR if the operation is successful. * Returns RET_BAD_PARAM Input error. * Returns RET_BAD_STATUS if the operation fails. * Returns RET_NO_SPACE if the buffer of a2dp source profile is full. * @since 6.0 */ int WriteFrame(const uint8_t *data, uint32_t size); /** * @brief Get the information of the current rendered position. * @param device The address of the peer bluetooth device. * @param[out] dalayValue is the delayed time * @param[out] sendDataSize is the data size that has been sent * @param[out] timeStamp is the current time stamp * @since 6.0 */ int GetRenderPosition(const BluetoothRemoteDevice &device, uint32_t &delayValue, uint64_t &sendDataSize, uint32_t &timeStamp); /** * @brief Audio start offload streaming for hardware encoding datapath. * * @param device remote bluetooth sink device. * @return Returns general enum BtErrCode for the operation. * @since 6.0 */ int OffloadStartPlaying(const BluetoothRemoteDevice &device, const std::vector &sessionsId); /** * @brief Audio stop offload streaming for hardware encoding datapath. * * @param device remote bluetooth sink device. * @return Returns general enum BtErrCode for the operation. * @since 6.0 */ int OffloadStopPlaying(const BluetoothRemoteDevice &device, const std::vector &sessionsId); /** * brief Get a2dp encoding data path information of connected sink device. * * @param device remote bluetooth sink device. * @info streams detail information * @return Returns UNKNOWN_ENCODING_PATH: 0, bt unable to judge encoding data path * Returns SOFTWARE_ENCODING_PATH: 1, a2dp audio encoding path should select a2dp hdi. * Returns HARDWARE_ENCODING_PATH: 2, a2dp audio encoding path should select a2dp offload hdi. * Returns general enum BtErrCode for the operation. */ int A2dpOffloadSessionRequest(const BluetoothRemoteDevice &device, const std::vector &info); /** * @brief Get A2dp Offload codec status information of connected device. * * @param device remote bluetooth sink device. * @return a2dp offload configration information of connected device. * @since 6.0 */ A2dpOffloadCodecStatus GetOffloadCodecStatus(const BluetoothRemoteDevice &device) const; /** * Allow devices to automatically play music when connected. * * @param device Remote bluetooth sink device. * @return Returns general enum BtErrCode for the operation. * @since 12 */ int EnableAutoPlay(const BluetoothRemoteDevice &device); /** * Restriction devices to play music within {@code duration} milliseconds of connection. * * @param device Remote bluetooth sink device. * @param duration Restricted duration . * @return Returns general enum BtErrCode for the operation. * @since 12 */ int DisableAutoPlay(const BluetoothRemoteDevice &device, const int duration); /** * Obtains the duration for which automatic playback is disabled. * * @param device Remote bluetooth sink device. * @param duration Restricted duration . * @return Returns general enum BtErrCode for the operation. * @since 12 */ int GetAutoPlayDisabledDuration(const BluetoothRemoteDevice &device, int &duration); /** * get virtual device list. * * @param devices virtual device list. * @since 12 */ void GetVirtualDeviceList(std::vector &devices); private: /** * @brief A constructor used to create a a2dp source instance. * * @since 6.0 */ A2dpSource(void); /** * @brief A destructor used to delete the a2dp source instance. * * @since 6.0 */ ~A2dpSource(void); BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(A2dpSource); BLUETOOTH_DECLARE_IMPL(); #ifdef DTFUZZ_TEST friend class BluetoothNoDestructor; #endif }; } // namespace Bluetooth } // namespace OHOS #endif // BLUETOOTH_A2DP_SRC_H