diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index 1ba04dceeea9a94a6bba84ce0387a6282e2198df..d42522500d0026aca96fca18acd437a9ae46e9a3 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 37984060ef6fb49f386ad221d0b1c4f6cfee2d1f..1ad2bb5305f110fec0940f0606d6b655be7dbcc8 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 3fe139864731267fd5ab03309a179d614b16c3e3..4147c8a8703075fdd8988cede4dcadc58cbd4d1a 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);