From 98a3889f670aeb5f2dac3482206bbf2872defacb Mon Sep 17 00:00:00 2001 From: zhuchaochao Date: Fri, 15 Dec 2023 06:18:54 +0000 Subject: [PATCH] fix bug of bluetooth Signed-off-by: zhuchaochao Change-Id: Ib21234444d48b5fbca5a836f6c7ec4090e1aacb2 --- .../audio_bluetooth_manager.cpp | 21 ++++++++++++++++- .../bluetoothclient/audio_bluetooth_manager.h | 2 ++ .../src/service/audio_policy_service.cpp | 23 +++++++++++-------- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index 1ba04dceee..d42522500d 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp @@ -33,8 +33,9 @@ HandsFreeAudioGateway *AudioHfpManager::hfpInstance_ = nullptr; std::shared_ptr AudioHfpManager::hfpListener_ = std::make_shared(); AudioScene AudioHfpManager::scene_ = AUDIO_SCENE_DEFAULT; BluetoothRemoteDevice AudioHfpManager::activeHfpDevice_; -std::mutex g_hfpInstanceLock; +std::mutex g_activehfpDeviceLock; std::mutex g_audioSceneLock; +std::mutex g_hfpInstanceLock; static bool GetAudioStreamInfo(A2dpCodecInfo codecInfo, AudioStreamInfo &audioStreamInfo) { @@ -256,6 +257,7 @@ int32_t AudioHfpManager::SetActiveHfpDevice(const std::string &macAddress) AUDIO_ERR_LOG("SetActiveHfpDevice failed for the HFP device does not exist."); return ERROR; } + std::lock_guard hfpDeviceLock(g_activehfpDeviceLock); if (macAddress != activeHfpDevice_.GetDeviceAddr()) { AUDIO_INFO_LOG("Active hfp device is changed, need to DisconnectSco for current activeHfpDevice."); int32_t ret = DisconnectSco(); @@ -347,6 +349,18 @@ void AudioHfpManager::DisconnectBluetoothHfpSink() HfpBluetoothDeviceManager::ClearAllHfpBluetoothDevice(); } +void AudioHfpManager::SetActiveHfpDevice(BluetoothRemoteDevice &device) +{ + std::lock_guard hfpDeviceLock(g_activehfpDeviceLock); + activeHfpDevice_ = device; +} + +void AudioHfpManager::SetAudioScene(AudioScene scene) +{ + std::lock_guard sceneLock(g_audioSceneLock); + scene_ = scene; +} + void AudioHfpListener::OnScoStateChanged(const BluetoothRemoteDevice &device, int state) { AUDIO_INFO_LOG("Entered %{public}s [%{public}d]", __func__, state); @@ -364,6 +378,11 @@ void AudioHfpListener::OnConnectionStateChanged(const BluetoothRemoteDevice &dev HfpBluetoothDeviceManager::SetHfpStack(device, BluetoothDeviceAction::CONNECT_ACTION); } if (state == static_cast(BTConnectState::DISCONNECTED)) { + if (device.GetDeviceAddr() == AudioHfpManager::GetActiveHfpDevice()) { + BluetoothRemoteDevice defaultDevice; + AudioHfpManager::SetActiveHfpDevice(defaultDevice); + AudioHfpManager::SetAudioScene(AUDIO_SCENE_DEFAULT); + } HfpBluetoothDeviceManager::SetHfpStack(device, BluetoothDeviceAction::DISCONNECT_ACTION); } } diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h index 37984060ef..1ad2bb5305 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h @@ -105,6 +105,8 @@ public: static int32_t DisconnectSco(); static int8_t GetScoCategoryFromScene(AudioStandard::AudioScene scene); static void DisconnectBluetoothHfpSink(); + static void SetActiveHfpDevice(BluetoothRemoteDevice &device); + static void SetAudioScene(AudioStandard::AudioScene scene); private: static HandsFreeAudioGateway *hfpInstance_; 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 3fe1398647..4147c8a870 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -3452,18 +3452,23 @@ void AudioPolicyService::OnServiceDisconnected(AudioServiceIndex serviceIndex) void AudioPolicyService::OnForcedDeviceSelected(DeviceType devType, const std::string &macAddress) { if (macAddress.empty()) { - sptr audioDescriptor = new(std::nothrow) AudioDeviceDescriptor(); - audioStateManager_.SetPerferredMediaRenderDevice(audioDescriptor); - } else { - std::vector> bluetoothDevices = - audioDeviceManager_.GetAvailableBluetoothDevice(devType, macAddress); - std::vector> audioDeviceDescriptors; - for (auto &dec : bluetoothDevices) { + AUDIO_ERR_LOG("OnForcedDeviceSelected failed as the macAddress is empty!"); + return; + } + std::vector> bluetoothDevices = + audioDeviceManager_.GetAvailableBluetoothDevice(devType, macAddress); + std::vector> audioDeviceDescriptors; + for (auto &dec : bluetoothDevices) { + if (dec->deviceRole_ == DeviceRole::OUTPUT_DEVICE) { sptr tempDec = new(std::nothrow) AudioDeviceDescriptor(*dec); audioDeviceDescriptors.push_back(move(tempDec)); } - int32_t res = DeviceParamsCheck(DeviceRole::OUTPUT_DEVICE, audioDeviceDescriptors); - CHECK_AND_RETURN_LOG(res == SUCCESS, "OnForcedDeviceSelected DeviceParamsCheck no success"); + } + int32_t res = DeviceParamsCheck(DeviceRole::OUTPUT_DEVICE, audioDeviceDescriptors); + CHECK_AND_RETURN_LOG(res == SUCCESS, "OnForcedDeviceSelected DeviceParamsCheck no success"); + if (devType == DEVICE_TYPE_BLUETOOTH_SCO) { + audioStateManager_.SetPerferredCallRenderDevice(audioDeviceDescriptors[0]); + } else { audioStateManager_.SetPerferredMediaRenderDevice(audioDeviceDescriptors[0]); } FetchDevice(true); -- Gitee