diff --git a/bluetooth_part.gni b/bluetooth_part.gni new file mode 100755 index 0000000000000000000000000000000000000000..baa9189c7c92c59f10713c485383b89a337c79d1 --- /dev/null +++ b/bluetooth_part.gni @@ -0,0 +1,19 @@ +# Copyright (c) 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. + +if (!defined(global_parts_info) || + defined(global_parts_info.communication_bluetooth)) { + bluetooth_part_enable = true +} else { + bluetooth_part_enable = false +} diff --git a/frameworks/native/bluetoothclient/BUILD.gn b/frameworks/native/bluetoothclient/BUILD.gn index 0eca740e70bd28ab66b4ea104424c8d17322a5b0..135842d3e379d646d33cb7fe989652f1ad599701 100644 --- a/frameworks/native/bluetoothclient/BUILD.gn +++ b/frameworks/native/bluetoothclient/BUILD.gn @@ -17,24 +17,17 @@ import("//foundation/multimedia/audio_framework/config.gni") ohos_shared_library("audio_bluetooth_client") { install_enable = true - sources = [ - "audio_bluetooth_manager.cpp", - "bluetooth_a2dp_audio_src_observer_stub.cpp", - "bluetooth_a2dp_src_observer.cpp", - ] + sources = [ "audio_bluetooth_manager.cpp" ] include_dirs = [ - "//foundation/communication/bluetooth/frameworks/inner/common", - "//foundation/communication/bluetooth/interfaces/inner_api/include", - "//foundation/communication/bluetooth/interfaces/inner_api/include/c_header", - "//foundation/communication/bluetooth/services/bluetooth/common/", - "//foundation/communication/bluetooth/services/bluetooth/ipc/include", "//foundation/multimedia/audio_framework/services/audio_policy/server/include/service/interface", "//foundation/multimedia/audio_framework/interfaces/inner_api/native/audiocommon/include", "//commonlibrary/c_utils/base/include", "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", "//drivers/peripheral/bluetooth/audio/interfaces/include", "//drivers/peripheral/bluetooth/audio/hal/hdi_binder/proxy/include", + "//foundation/communication/bluetooth/interfaces/inner_api/include", + "//foundation/communication/bluetooth/interfaces/inner_api/include/c_header", ] deps = [ @@ -44,12 +37,12 @@ ohos_shared_library("audio_bluetooth_client") { "$hdf_uhdf_path/utils:libhdf_utils", "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", "//foundation/communication/bluetooth/frameworks/inner:btframework", - "//foundation/communication/bluetooth/services/bluetooth/ipc:btipc_static", "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", "//foundation/multimedia/audio_framework/frameworks/native/hdiadapter/sink:bluetooth_renderer_sink", ] external_deps = [ + "bluetooth:btframework", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index 269087889b0920f121a769f2347043d2086cbd96..7d6e99daffa7b7ce59c4ae14090a87b8b024b72c 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp @@ -15,34 +15,32 @@ #include "audio_bluetooth_manager.h" -#include "audio_errors.h" #include "audio_info.h" -#include "bluetooth_a2dp_src_observer.h" +#include "audio_errors.h" +#include "audio_log.h" #include "bluetooth_def.h" -#include "bluetooth_host.h" #include "bluetooth_types.h" -#include "bt_def.h" -#include "i_bluetooth_a2dp_src.h" -#include "i_bluetooth_host.h" +#include "bluetooth_host.h" #include "iservice_registry.h" -#include "audio_log.h" #include "system_ability_definition.h" namespace OHOS { namespace Bluetooth { -using namespace OHOS::bluetooth; using namespace AudioStandard; -sptr g_proxy = nullptr; -static sptr g_btA2dpSrcObserverCallbacks = nullptr; -int g_playState = false; -RawAddress g_device; IDeviceStatusObserver *g_deviceObserver = nullptr; -HandsFreeAudioGateway *HandsFreeAudioGatewayManager::handsFreeAgInstance_; -HandsFreeGatewayListener HandsFreeAudioGatewayManager::hfpAgObserver_; -std::mutex observerMutex_; - -static bool GetAudioStreamInfo(BluetoothA2dpCodecInfo codecInfo, AudioStreamInfo &audioStreamInfo) +A2dpSource *AudioA2dpManager::a2dpInstance_ = nullptr; +AudioA2dpListener AudioA2dpManager::a2dpListener_; +HandsFreeAudioGateway *AudioHfpManager::hfpInstance_ = nullptr; +AudioHfpListener AudioHfpManager::hfpListener_; +int AudioA2dpManager::connectionState_ = STATE_TURN_OFF; +bool AudioA2dpManager::bluetoothSinkLoaded_ = false; +BluetoothRemoteDevice AudioA2dpManager::bluetoothRemoteDevice_; + +std::mutex g_deviceLock; +std::mutex g_a2dpInstanceLock; + +static bool GetAudioStreamInfo(A2dpCodecInfo codecInfo, AudioStreamInfo &audioStreamInfo) { switch (codecInfo.sampleRate) { case A2DP_SBC_SAMPLE_RATE_48000_USER: @@ -91,171 +89,169 @@ static bool GetAudioStreamInfo(BluetoothA2dpCodecInfo codecInfo, AudioStreamInfo return true; } -static void AudioOnPlayingStatusChanged(const RawAddress &device, int playingState, int error) +int32_t RegisterDeviceObserver(IDeviceStatusObserver &observer) { - AUDIO_INFO_LOG("AudioOnPlayingStatusChanged"); - g_playState = playingState; + std::lock_guard deviceLock(g_deviceLock); + g_deviceObserver = &observer; + return SUCCESS; } -static void AudioOnConfigurationChanged(const RawAddress &device, const BluetoothA2dpCodecInfo &info, int error) +void UnregisterDeviceObserver() { - AUDIO_INFO_LOG("AudioOnConfigurationChanged: sampleRate: %{public}d, channels: %{public}d, format: %{public}d", - info.sampleRate, info.channelMode, info.bitsPerSample); - AudioStreamInfo audioStreamInfo = {}; - if (!GetAudioStreamInfo(info, audioStreamInfo)) { - AUDIO_ERR_LOG("AudioOnConfigurationChanged: Unsupported a2dp codec info"); - return; - } + std::lock_guard deviceLock(g_deviceLock); + g_deviceObserver = nullptr; +} + +void AudioA2dpManager::RegisterBluetoothA2dpListener() +{ + AUDIO_INFO_LOG("Entered %{public}s", __func__); - std::string deviceName = BluetoothHost::GetDefaultHost().GetRemoteDevice(device.GetAddress(), - BT_TRANSPORT_BREDR).GetDeviceName(); + std::lock_guard a2dpLock(g_a2dpInstanceLock); - g_deviceObserver->OnDeviceConfigurationChanged(DEVICE_TYPE_BLUETOOTH_A2DP, device.GetAddress(), deviceName, - audioStreamInfo); + a2dpInstance_ = A2dpSource::GetProfile(); + CHECK_AND_RETURN_LOG(a2dpInstance_ != nullptr, "Failed to obtain A2DP profile instance"); + + a2dpInstance_->RegisterObserver(&a2dpListener_); } -static void AudioOnConnectionChanged(const RawAddress &device, int state) +void AudioA2dpManager::UnregisterBluetoothA2dpListener() { - std::lock_guard lock(observerMutex_); - AUDIO_INFO_LOG("AudioOnConnectionChanged: state: %{public}d", state); - g_device = RawAddress(device); + AUDIO_INFO_LOG("Entered %{public}s", __func__); - if (g_deviceObserver == nullptr) { - AUDIO_INFO_LOG("observer is null"); + std::lock_guard a2dpLock(g_a2dpInstanceLock); + + CHECK_AND_RETURN_LOG(a2dpInstance_ != nullptr, "A2DP profile instance unavailable"); + + a2dpInstance_->DeregisterObserver(&a2dpListener_); + a2dpInstance_ = nullptr; +} + +void AudioA2dpManager::ConnectBluetoothA2dpSink() +{ + // Update device when hdi service is available + if (connectionState_ != STATE_TURN_ON) { + AUDIO_ERR_LOG("bluetooth state is not on"); return; } - BluetoothA2dpCodecStatus codecStatus = g_proxy->GetCodecStatus(device); - AUDIO_DEBUG_LOG("BluetoothA2dpCodecStatus: sampleRate: %{public}d, channels: %{public}d, format: %{public}d", - codecStatus.codecInfo.sampleRate, codecStatus.codecInfo.channelMode, codecStatus.codecInfo.bitsPerSample); - - AudioStreamInfo streamInfo = {}; - std::string deviceName = BluetoothHost::GetDefaultHost().GetRemoteDevice(device.GetAddress(), - BT_TRANSPORT_BREDR).GetDeviceName(); - if (state == STATE_TURN_ON) { - if (!GetAudioStreamInfo(codecStatus.codecInfo, streamInfo)) { - AUDIO_ERR_LOG("AudioOnConnectionChanged: Unsupported a2dp codec info"); - return; - } + std::lock_guard deviceLock(g_deviceLock); - g_deviceObserver->OnDeviceStatusUpdated(DEVICE_TYPE_BLUETOOTH_A2DP, true, device.GetAddress(), deviceName, - streamInfo); - } else if (state == STATE_TURN_OFF) { - g_deviceObserver->OnDeviceStatusUpdated(DEVICE_TYPE_BLUETOOTH_A2DP, false, device.GetAddress(), deviceName, - streamInfo); + if (g_deviceObserver == nullptr) { + AUDIO_INFO_LOG("device observer is null"); + return; } -} -static BtA2dpAudioCallback g_hdiCallacks = { - .OnPlayingStatusChanged = AudioOnPlayingStatusChanged, - .OnConfigurationChanged = AudioOnConfigurationChanged, - .OnConnectionStateChanged = AudioOnConnectionChanged, -}; + AudioStreamInfo streamInfo = {}; + A2dpCodecStatus codecStatus = A2dpSource::GetProfile()->GetCodecStatus(bluetoothRemoteDevice_); + if (!GetAudioStreamInfo(codecStatus.codecInfo, streamInfo)) { + AUDIO_ERR_LOG("OnConnectionStateChanged: Unsupported a2dp codec info"); + return; + } -int GetPlayingState() -{ - return g_playState; + g_deviceObserver->OnDeviceStatusUpdated(DEVICE_TYPE_BLUETOOTH_A2DP, true, + bluetoothRemoteDevice_.GetDeviceAddr(), bluetoothRemoteDevice_.GetDeviceName(), streamInfo); + bluetoothSinkLoaded_ = true; } -RawAddress& GetDevice() +// Prepare for future optimization +void AudioA2dpManager::DisconnectBluetoothA2dpSink() { - return g_device; + if (bluetoothSinkLoaded_) { + AUDIO_WARNING_LOG("bluetooth sink still loaded, some error may occur!"); + } } -int32_t GetProxy() +void AudioA2dpListener::OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state) { - AUDIO_INFO_LOG("GetProxy start"); - sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!samgr) { - AUDIO_ERR_LOG("GetProxy error: no samgr"); - return ERROR; - } + AUDIO_INFO_LOG("OnConnectionStateChanged: state: %{public}d", state); - sptr hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID); - if (!hostRemote) { - AUDIO_ERR_LOG("A2dpSource::impl:GetProxy failed: no hostRemote"); - return ERROR; + // Record connection state and device for hdi start time to check + AudioA2dpManager::SetConnectionState(state); + if (state == STATE_TURN_ON) { + AudioA2dpManager::SetBluetoothRemoteDevice(device); } - sptr hostProxy = iface_cast(hostRemote); - if (!hostProxy) { - AUDIO_ERR_LOG("GetProxy error: host no proxy"); - return ERROR; - } + // Currently disconnect need to be done in OnConnectionStateChanged instead of hdi service stopped + if (state == STATE_TURN_OFF) { + std::lock_guard deviceLock(g_deviceLock); - sptr remote = hostProxy->GetProfile("A2dpSrcServer"); - if (!remote) { - AUDIO_ERR_LOG("GetProxy error: no remote"); - return ERROR; - } + if (g_deviceObserver == nullptr) { + AUDIO_INFO_LOG("device observer is null"); + return; + } - g_proxy = iface_cast(remote); - if (!g_proxy) { - AUDIO_ERR_LOG("GetProxy error: no proxy"); - return ERROR; + AudioStreamInfo streamInfo = {}; + g_deviceObserver->OnDeviceStatusUpdated(DEVICE_TYPE_BLUETOOTH_A2DP, false, + device.GetDeviceAddr(), device.GetDeviceName(), streamInfo); + AudioA2dpManager::SetBluetoothSinkLoaded(false); } - - return SUCCESS; } -int32_t RegisterObserver(IDeviceStatusObserver &observer) +void AudioA2dpListener::OnConfigurationChanged(const BluetoothRemoteDevice &device, const A2dpCodecInfo &codecInfo, + int error) { - std::lock_guard lock(observerMutex_); - AUDIO_INFO_LOG("RegisterObserver start"); - if (g_proxy == nullptr) { - if (GetProxy()) { - AUDIO_ERR_LOG("proxy is null"); - return ERROR; - } + AUDIO_INFO_LOG("OnConfigurationChanged: sampleRate: %{public}d, channels: %{public}d, format: %{public}d", + codecInfo.sampleRate, codecInfo.channelMode, codecInfo.bitsPerSample); + + std::lock_guard deviceLock(g_deviceLock); + + if (g_deviceObserver == nullptr) { + AUDIO_INFO_LOG("device observer is null"); + return; } - g_deviceObserver = &observer; - g_btA2dpSrcObserverCallbacks = new BluetoothA2dpSrcObserver(&g_hdiCallacks); - g_proxy->RegisterObserver(g_btA2dpSrcObserverCallbacks); - return SUCCESS; + AudioStreamInfo streamInfo = {}; + if (!GetAudioStreamInfo(codecInfo, streamInfo)) { + AUDIO_ERR_LOG("OnConfigurationChanged: Unsupported a2dp codec info"); + return; + } + + g_deviceObserver->OnDeviceConfigurationChanged(DEVICE_TYPE_BLUETOOTH_A2DP, device.GetDeviceAddr(), + device.GetDeviceName(), streamInfo); } -void DeRegisterObserver() +void AudioA2dpListener::OnPlayingStatusChanged(const BluetoothRemoteDevice &device, int playingState, int error) { - std::lock_guard lock(observerMutex_); - if ((g_deviceObserver != nullptr) && (g_btA2dpSrcObserverCallbacks != nullptr)) { - AUDIO_INFO_LOG("DeRegisterObserver start"); - g_deviceObserver = nullptr; - g_proxy->DeregisterObserver(g_btA2dpSrcObserverCallbacks); - } + AUDIO_INFO_LOG("OnPlayingStatusChanged, state: %{public}d, error: %{public}d", playingState, error); } -// Handsfree Gateway feature support -void HandsFreeAudioGatewayManager::RegisterBluetoothScoAgListener() +void AudioHfpManager::RegisterBluetoothScoListener() { AUDIO_INFO_LOG("Entered %{public}s", __func__); - handsFreeAgInstance_ = HandsFreeAudioGateway::GetProfile(); - CHECK_AND_RETURN_LOG(handsFreeAgInstance_ != nullptr, "Failed to obtain HFP AG profile instance"); + hfpInstance_ = HandsFreeAudioGateway::GetProfile(); + CHECK_AND_RETURN_LOG(hfpInstance_ != nullptr, "Failed to obtain HFP AG profile instance"); - handsFreeAgInstance_->RegisterObserver(&hfpAgObserver_); + hfpInstance_->RegisterObserver(&hfpListener_); } -void HandsFreeAudioGatewayManager::UnregisterBluetoothScoAgListener() +void AudioHfpManager::UnregisterBluetoothScoListener() { AUDIO_INFO_LOG("Entered %{public}s", __func__); - CHECK_AND_RETURN_LOG(handsFreeAgInstance_ != nullptr, "HFP AG profile instance unavailable"); + CHECK_AND_RETURN_LOG(hfpInstance_ != nullptr, "HFP AG profile instance unavailable"); - handsFreeAgInstance_->DeregisterObserver(&hfpAgObserver_); - handsFreeAgInstance_ = nullptr; + hfpInstance_->DeregisterObserver(&hfpListener_); + hfpInstance_ = nullptr; } -void HandsFreeGatewayListener::OnScoStateChanged(const BluetoothRemoteDevice &device, int state) +void AudioHfpListener::OnScoStateChanged(const BluetoothRemoteDevice &device, int state) { AUDIO_INFO_LOG("Entered %{public}s [%{public}d]", __func__, state); + + std::lock_guard deviceLock(g_deviceLock); + + if (g_deviceObserver == nullptr) { + AUDIO_INFO_LOG("device observer is null"); + return; + } + HfpScoConnectState scoState = static_cast(state); if (scoState == HfpScoConnectState::SCO_CONNECTED || scoState == HfpScoConnectState::SCO_DISCONNECTED) { - AudioStreamInfo info = {}; - std::string deviceName = BluetoothHost::GetDefaultHost().GetRemoteDevice(device.GetDeviceAddr(), - BT_TRANSPORT_BREDR).GetDeviceName(); + AudioStreamInfo streamInfo = {}; bool isConnected = (scoState == HfpScoConnectState::SCO_CONNECTED) ? true : false; g_deviceObserver->OnDeviceStatusUpdated(DEVICE_TYPE_BLUETOOTH_SCO, isConnected, device.GetDeviceAddr(), - deviceName, info); + device.GetDeviceName(), streamInfo); } } -} -} + +} // namespace Bluetooth +} // namespace OHOS diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h index 8f72399ed05e8a25fd0c43e11294c8403521744b..2f9f332e923d61114c351fb03a2a672d4637bd5e 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h @@ -16,50 +16,97 @@ #ifndef AUDIO_BLUETOOTH_MANAGERI_H #define AUDIO_BLUETOOTH_MANAGERI_H +#include "bluetooth_a2dp_src.h" #include "bluetooth_a2dp_codec.h" -#include "bluetooth_a2dp_a2dpCodecInfo.h" -#include "bluetooth_a2dp_a2dpCodecStatus.h" #include "bluetooth_hfp_ag.h" #include "idevice_status_observer.h" -#include "raw_address.h" namespace OHOS { namespace Bluetooth { -using namespace OHOS::bluetooth; -typedef struct BtA2dpAudioCallback { - void (*OnPlayingStatusChanged)(const RawAddress &device, int playingState, int error); - void (*OnConfigurationChanged)(const RawAddress &device, const BluetoothA2dpCodecInfo &info, int error); - void (*OnConnectionStateChanged)(const RawAddress &device, int state); -} BtA2dpAudioCallback; +int32_t RegisterDeviceObserver(AudioStandard::IDeviceStatusObserver &observer); +void UnregisterDeviceObserver(); -int GetPlayingState(); -RawAddress& GetDevice(); -int32_t GetProxy(); -int32_t RegisterObserver(AudioStandard::IDeviceStatusObserver &observer); -void DeRegisterObserver(); +// Audio bluetooth a2dp feature support +class AudioA2dpListener : public A2dpSourceObserver { +public: + AudioA2dpListener() = default; + virtual ~AudioA2dpListener() = default; + + virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state); + virtual void OnConfigurationChanged(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info, int error); + virtual void OnPlayingStatusChanged(const BluetoothRemoteDevice &device, int playingState, int error); + +private: + BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(AudioA2dpListener); +}; + +class AudioA2dpManager { +public: + AudioA2dpManager() = default; + virtual ~AudioA2dpManager() = default; + static void RegisterBluetoothA2dpListener(); + static void UnregisterBluetoothA2dpListener(); + static void ConnectBluetoothA2dpSink(); + static void DisconnectBluetoothA2dpSink(); + static void SetConnectionState(int state) + { + connectionState_ = state; + } + static int GetConnectionState() + { + return connectionState_; + } + static void SetBluetoothSinkLoaded(bool isLoaded) + { + bluetoothSinkLoaded_ = isLoaded; + } + static bool GetBluetoothSinkLoaded() + { + return bluetoothSinkLoaded_; + } + static void SetBluetoothRemoteDevice(BluetoothRemoteDevice device) + { + bluetoothRemoteDevice_ = device; + } + static BluetoothRemoteDevice GetBluetoothRemoteDevice() + { + return bluetoothRemoteDevice_; + } + +private: + static A2dpSource *a2dpInstance_; + static AudioA2dpListener a2dpListener_; + static int connectionState_; + static bool bluetoothSinkLoaded_; + static BluetoothRemoteDevice bluetoothRemoteDevice_; +}; -class HandsFreeGatewayListener : public HandsFreeAudioGatewayObserver { +// Audio bluetooth sco feature support +class AudioHfpListener : public HandsFreeAudioGatewayObserver { public: - HandsFreeGatewayListener() = default; - virtual ~HandsFreeGatewayListener() = default; + AudioHfpListener() = default; + virtual ~AudioHfpListener() = default; void OnScoStateChanged(const BluetoothRemoteDevice &device, int state); void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state) {} void OnActiveDeviceChanged(const BluetoothRemoteDevice &device) {} void OnHfEnhancedDriverSafetyChanged(const BluetoothRemoteDevice &device, int indValue) {} + +private: + BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(AudioHfpListener); }; -class HandsFreeAudioGatewayManager { +class AudioHfpManager { public: - HandsFreeAudioGatewayManager() = default; - virtual ~HandsFreeAudioGatewayManager() = default; - static void RegisterBluetoothScoAgListener(); - static void UnregisterBluetoothScoAgListener(); + AudioHfpManager() = default; + virtual ~AudioHfpManager() = default; + static void RegisterBluetoothScoListener(); + static void UnregisterBluetoothScoListener(); private: - static HandsFreeAudioGateway *handsFreeAgInstance_; - static HandsFreeGatewayListener hfpAgObserver_; + static HandsFreeAudioGateway *hfpInstance_; + static AudioHfpListener hfpListener_; }; } } diff --git a/frameworks/native/bluetoothclient/bluetooth_a2dp_audio_src_observer_stub.cpp b/frameworks/native/bluetoothclient/bluetooth_a2dp_audio_src_observer_stub.cpp deleted file mode 100644 index 17f4aa6e5fe8bda70e311f5e97db80ea2cac2d69..0000000000000000000000000000000000000000 --- a/frameworks/native/bluetoothclient/bluetooth_a2dp_audio_src_observer_stub.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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. - */ - -#include "bluetooth_a2dp_audio_src_observer_stub.h" -#include "bluetooth_log.h" -#include "raw_address.h" - -namespace OHOS { -namespace Bluetooth { -using namespace OHOS::bluetooth; -BluetoothA2dpAudioSrcObserverStub::BluetoothA2dpAudioSrcObserverStub() -{ - HILOGD("%{public}s start.", __func__); - memberFuncMap_[static_cast - (IBluetoothA2dpSourceObserver::Code::BT_A2DP_SRC_OBSERVER_CONNECTION_STATE_CHANGED)] = - &BluetoothA2dpAudioSrcObserverStub::OnConnectionStateChangedInner; - memberFuncMap_[static_cast - (IBluetoothA2dpSourceObserver::Code::BT_A2DP_SRC_OBSERVER_PLAYING_STATUS_CHANGED)] = - &BluetoothA2dpAudioSrcObserverStub::OnPlayingStatusChangedInner; - memberFuncMap_[static_cast - (IBluetoothA2dpSourceObserver::Code::BT_A2DP_SRC_OBSERVER_CONFIGURATION_CHANGED)] = - &BluetoothA2dpAudioSrcObserverStub::OnConfigurationChangedInner; -} - -BluetoothA2dpAudioSrcObserverStub::~BluetoothA2dpAudioSrcObserverStub() -{ - HILOGD("%{public}s start.", __func__); - memberFuncMap_.clear(); -} - -int BluetoothA2dpAudioSrcObserverStub::OnRemoteRequest( - uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) -{ - HILOGD("BluetoothA2dpAudioSrcObserverStub::OnRemoteRequest, cmd = %{public}d, flags = %{public}d", - code, option.GetFlags()); - if (data.ReadInterfaceToken() != BluetoothA2dpAudioSrcObserverStub::GetDescriptor()) { - HILOGE("BluetoothA2dpAudioSrcObserverStub: ReadInterfaceToken failed"); - return ERR_INVALID_STATE; - } - auto itFunc = memberFuncMap_.find(code); - if (itFunc != memberFuncMap_.end()) { - auto memberFunc = itFunc->second; - if (memberFunc != nullptr) { - return (this->*memberFunc)(data, reply); - } - } - HILOGW("BluetoothA2dpAudioSrcObserverStub::OnRemoteRequest, default case, need check."); - return IPCObjectStub::OnRemoteRequest(code, data, reply, option); -} - -ErrCode BluetoothA2dpAudioSrcObserverStub::OnConnectionStateChangedInner(MessageParcel &data, MessageParcel &reply) -{ - std::string addr = data.ReadString(); - int state = data.ReadInt32(); - HILOGD("BluetoothA2dpAudioSrcObserverStub::OnConnectionStateChangedInner, addr = %{public}s", addr.c_str()); - OnConnectionStateChanged(RawAddress(addr), state); - - return NO_ERROR; -} - -ErrCode BluetoothA2dpAudioSrcObserverStub::OnPlayingStatusChangedInner(MessageParcel &data, MessageParcel &reply) -{ - std::string addr = data.ReadString(); - int playingState = data.ReadInt32(); - int error = data.ReadInt32(); - - OnPlayingStatusChanged(RawAddress(addr), playingState, error); - - return NO_ERROR; -} - -ErrCode BluetoothA2dpAudioSrcObserverStub::OnConfigurationChangedInner(MessageParcel &data, MessageParcel &reply) -{ - std::string addr = data.ReadString(); - BluetoothA2dpCodecInfo info = *data.ReadParcelable(); - int error = data.ReadInt32(); - - OnConfigurationChanged(RawAddress(addr), info, error); - - return NO_ERROR; -} -} // namespace Bluetooth -} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/bluetoothclient/bluetooth_a2dp_audio_src_observer_stub.h b/frameworks/native/bluetoothclient/bluetooth_a2dp_audio_src_observer_stub.h deleted file mode 100644 index ac8f141f46c19e13fa2590d77d62d5528d6ba50d..0000000000000000000000000000000000000000 --- a/frameworks/native/bluetoothclient/bluetooth_a2dp_audio_src_observer_stub.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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. - */ - -#ifndef OHOS_BLUETOOTH_A2DP_AUDIO_SRC_OBSERVER_STUB_H -#define OHOS_BLUETOOTH_A2DP_AUDIO_SRC_OBSERVER_STUB_H - -#include - -#include "iremote_stub.h" -#include "i_bluetooth_a2dp_src_observer.h" - -namespace OHOS { -namespace Bluetooth { -class BluetoothA2dpAudioSrcObserverStub : public IRemoteStub { -public: - BluetoothA2dpAudioSrcObserverStub(); - virtual ~BluetoothA2dpAudioSrcObserverStub(); - - virtual int OnRemoteRequest( - uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; - -private: - ErrCode OnConnectionStateChangedInner(MessageParcel &data, MessageParcel &reply); - ErrCode OnPlayingStatusChangedInner(MessageParcel &data, MessageParcel &reply); - ErrCode OnConfigurationChangedInner(MessageParcel &data, MessageParcel &reply); - using BluetoothA2dpAudioSrcObserverFunc = ErrCode (BluetoothA2dpAudioSrcObserverStub::*)(MessageParcel &data, - MessageParcel &reply); - std::map memberFuncMap_; - - DISALLOW_COPY_AND_MOVE(BluetoothA2dpAudioSrcObserverStub); -}; -} // namespace Bluetooth -} // namespace OHOS -#endif // OHOS_BLUETOOTH_A2DP_AUDIO_SRC_OBSERVER_STUB_H diff --git a/frameworks/native/bluetoothclient/bluetooth_a2dp_src_observer.cpp b/frameworks/native/bluetoothclient/bluetooth_a2dp_src_observer.cpp deleted file mode 100644 index 72423200a8ec98c810d0ef83c29c00eb482cde9b..0000000000000000000000000000000000000000 --- a/frameworks/native/bluetoothclient/bluetooth_a2dp_src_observer.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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. - */ - -#include "bluetooth_a2dp_src_observer.h" -#include "audio_log.h" - -void BluetoothA2dpSrcObserver::OnConfigurationChanged(const OHOS::bluetooth::RawAddress &device, - const OHOS::Bluetooth::BluetoothA2dpCodecInfo &info, int error) -{ - AUDIO_INFO_LOG("[BluetoothA2dpSrcObserver] OnConfigurationChanged"); - if ((callbacks_ != nullptr) && (callbacks_->OnConfigurationChanged)) { - callbacks_->OnConfigurationChanged(device, info, error); - } -} - -void BluetoothA2dpSrcObserver::OnPlayingStatusChanged( - const OHOS::bluetooth::RawAddress &device, int playingState, int error) -{ - AUDIO_INFO_LOG("[BluetoothA2dpSrcObserver] OnPlayingStatusChanged, state: %{public}d, error: %{public}d", - playingState, error); - if ((callbacks_ != nullptr) && (callbacks_->OnPlayingStatusChanged)) { - callbacks_->OnPlayingStatusChanged(device, playingState, error); - } -} - -void BluetoothA2dpSrcObserver::OnConnectionStateChanged(const OHOS::bluetooth::RawAddress &device, int state) -{ - AUDIO_INFO_LOG("[BluetoothA2dpSrcObserver] OnConnectionStateChanged, state: %{public}d", state); - if ((callbacks_ != nullptr) && (callbacks_->OnConnectionStateChanged)) { - callbacks_->OnConnectionStateChanged(device, state); - } -} \ No newline at end of file diff --git a/frameworks/native/bluetoothclient/bluetooth_a2dp_src_observer.h b/frameworks/native/bluetoothclient/bluetooth_a2dp_src_observer.h deleted file mode 100644 index 4248143d01a109cac2591a50ed280237a18382a4..0000000000000000000000000000000000000000 --- a/frameworks/native/bluetoothclient/bluetooth_a2dp_src_observer.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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. - */ - -#ifndef BLUETOOTH_A2DP_SRC_OBSERVER_H -#define BLUETOOTH_A2DP_SRC_OBSERVER_H - -#include "bluetooth_a2dp_audio_src_observer_stub.h" -#include "audio_bluetooth_manager.h" - -class BluetoothA2dpSrcObserver : public OHOS::Bluetooth::BluetoothA2dpAudioSrcObserverStub { -public: - BluetoothA2dpSrcObserver(OHOS::Bluetooth::BtA2dpAudioCallback *callbacks) : callbacks_(callbacks) { }; - ~BluetoothA2dpSrcObserver() { }; - - void OnConnectionStateChanged(const OHOS::bluetooth::RawAddress &device, int state); - void OnPlayingStatusChanged(const OHOS::bluetooth::RawAddress &device, int playingState, int error); - void OnConfigurationChanged(const OHOS::bluetooth::RawAddress &device, - const OHOS::Bluetooth::BluetoothA2dpCodecInfo &info, int error); - -private: - OHOS::Bluetooth::BtA2dpAudioCallback *callbacks_; - BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothA2dpSrcObserver); -}; -#endif // BLUETOOTH_A2DP_SRC_OBSERVER_H diff --git a/services/audio_policy/BUILD.gn b/services/audio_policy/BUILD.gn index 304144160cb722e487418ac53866a3087d5e9a35..a19ba287c1804a61a411efad77edb17aee6833b0 100644 --- a/services/audio_policy/BUILD.gn +++ b/services/audio_policy/BUILD.gn @@ -13,6 +13,7 @@ import("//build/ohos.gni") import("//drivers/hdf_core/adapter/uhdf2/uhdf.gni") +import("//foundation/multimedia/audio_framework/bluetooth_part.gni") group("audio_policy_packages") { deps = [ @@ -81,6 +82,10 @@ config("audio_policy_public_config") { cflags += [ "-DBINDER_IPC_32BIT" ] } + if (bluetooth_part_enable == true) { + cflags += [ "-DBLUETOOTH_ENABLE" ] + } + ldflags = [ "-Wl" ] } @@ -134,12 +139,10 @@ ohos_shared_library("audio_policy_service") { "$hdf_uhdf_path/host:libhdf_host", "$hdf_uhdf_path/ipc:libhdf_ipc_adapter", "$hdf_uhdf_path/utils:libhdf_utils", - "//foundation/communication/bluetooth/services/bluetooth/ipc:btipc_static", "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata:distributeddata_inner", "//foundation/multimedia/audio_framework/frameworks/native/audioadapter:pulse_audio_service_adapter", - "//foundation/multimedia/audio_framework/frameworks/native/bluetoothclient:audio_bluetooth_client", "//foundation/multimedia/audio_framework/services/audio_policy:audio_dump", "//foundation/multimedia/audio_framework/services/audio_service:audio_client", "//third_party/libxml2:xml2", @@ -156,6 +159,12 @@ ohos_shared_library("audio_policy_service") { "safwk:system_ability_fwk", "samgr:samgr_proxy", ] + + if (bluetooth_part_enable == true) { + deps += [ "//foundation/multimedia/audio_framework/frameworks/native/bluetoothclient:audio_bluetooth_client" ] + + external_deps += [ "bluetooth:btframework" ] + } subsystem_name = "multimedia" part_name = "multimedia_audio_framework" } diff --git a/services/audio_policy/server/include/audio_policy_server.h b/services/audio_policy/server/include/audio_policy_server.h index dd078b430a33d67117f3764124169c648e850baf..d8e5609fb1807d35594278ab88206105c4dcec52 100644 --- a/services/audio_policy/server/include/audio_policy_server.h +++ b/services/audio_policy/server/include/audio_policy_server.h @@ -206,6 +206,7 @@ private: void SubscribeKeyEvents(); void InitKVStore(); void ConnectServiceAdapter(); + void RegisterBluetoothListener(); static float MapVolumeToHDI(int32_t volume); static int32_t ConvertVolumeToInt(float volume); diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index 3cf7bc0ddb3dfd0045ac28abcc4b842b7494cb37..6db6878309be086672780d305584ef0721a56783 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -168,6 +168,10 @@ public: void SetParameterCallback(const std::shared_ptr& callback); + void RegisterBluetoothListener(); + + void UnregisterBluetoothListener(); + private: AudioPolicyService() : mAudioPolicyManager(AudioPolicyManagerFactory::GetAudioPolicyManager()), @@ -252,6 +256,7 @@ private: bool interruptEnabled_ = true; bool isUpdateRouteSupported_ = true; bool isOpenRemoteDevice = false; + bool isBtListenerRegistered = false; static const int32_t G_UNKNOWN_PID = -1; int32_t dAudioClientUid = 3055; uint64_t audioLatencyInMsec_ = 50; diff --git a/services/audio_policy/server/src/audio_policy_server.cpp b/services/audio_policy/server/src/audio_policy_server.cpp index 28be799fd34de9cb0e4d9e80baabe6f317a94282..88761a316771b34ac0ac99d564ba47525f5673d3 100644 --- a/services/audio_policy/server/src/audio_policy_server.cpp +++ b/services/audio_policy/server/src/audio_policy_server.cpp @@ -78,6 +78,7 @@ void AudioPolicyServer::OnStart() AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID); AddSystemAbilityListener(MULTIMODAL_INPUT_SERVICE_ID); AddSystemAbilityListener(AUDIO_DISTRIBUTED_SERVICE_ID); + AddSystemAbilityListener(BLUETOOTH_HOST_SYS_ABILITY_ID); mPolicyService.Init(); RegisterAudioServerDeathRecipient(); @@ -92,23 +93,27 @@ void AudioPolicyServer::OnStop() void AudioPolicyServer::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) { - AUDIO_DEBUG_LOG("AudioPolicyServer::OnAddSystemAbility systemAbilityId:%{public}d", systemAbilityId); + AUDIO_DEBUG_LOG("OnAddSystemAbility systemAbilityId:%{public}d", systemAbilityId); switch (systemAbilityId) { case MULTIMODAL_INPUT_SERVICE_ID: - AUDIO_DEBUG_LOG("AudioPolicyServer::OnAddSystemAbility SubscribeKeyEvents"); + AUDIO_INFO_LOG("OnAddSystemAbility input service start"); SubscribeKeyEvents(); break; case DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID: - AUDIO_DEBUG_LOG("AudioPolicyServer::OnAddSystemAbility InitKVStore"); + AUDIO_INFO_LOG("OnAddSystemAbility kv data service start"); InitKVStore(); break; case AUDIO_DISTRIBUTED_SERVICE_ID: - AUDIO_DEBUG_LOG("AudioPolicyServer::OnAddSystemAbility ConnectServiceAdapter"); + AUDIO_INFO_LOG("OnAddSystemAbility audio service start"); ConnectServiceAdapter(); RegisterParamCallback(); break; + case BLUETOOTH_HOST_SYS_ABILITY_ID: + AUDIO_INFO_LOG("OnAddSystemAbility bluetooth service start"); + RegisterBluetoothListener(); + break; default: - AUDIO_DEBUG_LOG("AudioPolicyServer::OnAddSystemAbility unhandled sysabilityId:%{public}d", systemAbilityId); + AUDIO_ERR_LOG("OnAddSystemAbility unhandled sysabilityId:%{public}d", systemAbilityId); break; } } @@ -1537,5 +1542,11 @@ void AudioPolicyServer::RegisterParamCallback() remoteParameterCallback_ = std::make_shared(this); mPolicyService.SetParameterCallback(remoteParameterCallback_); } + +void AudioPolicyServer::RegisterBluetoothListener() +{ + AUDIO_INFO_LOG("AudioPolicyServer::RegisterBluetoothListener"); + mPolicyService.RegisterBluetoothListener(); +} } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index 09e4b8a6aa84a53ac942cb9c569a8cffc711f6a3..91b1d71d1e58ad09cab3ebe011bea4b78d49df0f 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -22,12 +22,17 @@ #include "system_ability_definition.h" #include "audio_manager_listener_stub.h" +#ifdef BLUETOOTH_ENABLE +#include "audio_bluetooth_manager.h" +#endif + #include "audio_policy_service.h" namespace OHOS { namespace AudioStandard { using namespace std; + const uint32_t PCM_8_BIT = 8; const uint32_t PCM_16_BIT = 16; const uint32_t PCM_24_BIT = 24; @@ -119,6 +124,10 @@ void AudioPolicyService::Deinit(void) mIOHandles.clear(); mDeviceStatusListener->UnRegisterDeviceStatusListener(); + + if (isBtListenerRegistered) { + UnregisterBluetoothListener(); + } return; } @@ -2007,5 +2016,27 @@ void AudioPolicyService::SetParameterCallback(const std::shared_ptrSetParameterCallback(object); } + +void AudioPolicyService::RegisterBluetoothListener() +{ +#ifdef BLUETOOTH_ENABLE + AUDIO_INFO_LOG("Enter AudioPolicyService::RegisterBluetoothListener"); + Bluetooth::RegisterDeviceObserver(mDeviceStatusListener->deviceObserver_); + Bluetooth::AudioA2dpManager::RegisterBluetoothA2dpListener(); + Bluetooth::AudioHfpManager::RegisterBluetoothScoListener(); + isBtListenerRegistered = true; +#endif +} + +void AudioPolicyService::UnregisterBluetoothListener() +{ +#ifdef BLUETOOTH_ENABLE + AUDIO_INFO_LOG("Enter AudioPolicyService::UnregisterBluetoothListener"); + Bluetooth::UnregisterDeviceObserver(); + Bluetooth::AudioA2dpManager::UnregisterBluetoothA2dpListener(); + Bluetooth::AudioHfpManager::UnregisterBluetoothScoListener(); + isBtListenerRegistered = false; +#endif +} } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_policy/server/src/service/listener/device_status_listener.cpp b/services/audio_policy/server/src/service/listener/device_status_listener.cpp index a302f590aa4d68b3ad108b144452f854c9917855..4959ea4b5701385ca015b80dd75e617ec02bc713 100644 --- a/services/audio_policy/server/src/service/listener/device_status_listener.cpp +++ b/services/audio_policy/server/src/service/listener/device_status_listener.cpp @@ -13,16 +13,19 @@ * limitations under the License. */ -#include "device_status_listener.h" - #include #include -#include "audio_bluetooth_manager.h" #include "audio_errors.h" #include "audio_events.h" #include "audio_log.h" +#ifdef BLUETOOTH_ENABLE +#include "audio_bluetooth_manager.h" +#endif + +#include "device_status_listener.h" + namespace OHOS { namespace AudioStandard { const std::string AUDIO_HDI_SERVICE_NAME = "audio_hdi_service"; @@ -100,15 +103,14 @@ static void OnServiceStatusReceived(struct ServiceStatusListener *listener, stru devListener->deviceObserver_.OnDeviceStatusUpdated(internalDevice, isConnected, "", "", streamInfo); } } else if (serviceStatus->serviceName == AUDIO_BLUETOOTH_HDI_SERVICE_NAME) { +#ifdef BLUETOOTH_ENABLE if (serviceStatus->status == SERVIE_STATUS_START) { AUDIO_INFO_LOG("Bluetooth hdi service started"); - Bluetooth::RegisterObserver(devListener->deviceObserver_); - Bluetooth::HandsFreeAudioGatewayManager::RegisterBluetoothScoAgListener(); + Bluetooth::AudioA2dpManager::ConnectBluetoothA2dpSink(); } else if (serviceStatus->status == SERVIE_STATUS_STOP) { AUDIO_INFO_LOG("Bluetooth hdi service stopped"); - OHOS::Bluetooth::DeRegisterObserver(); - Bluetooth::HandsFreeAudioGatewayManager::UnregisterBluetoothScoAgListener(); } +#endif } else if (serviceStatus->serviceName == DAUDIO_HDI_SERVICE_NAME) { ReceviceDistributedInfo(serviceStatus, info, devListener); } else {