From d4531d5ba438f26bfd8d4d181380c04f47e36fca Mon Sep 17 00:00:00 2001 From: zhuchaochao Date: Mon, 18 Dec 2023 02:25:06 +0000 Subject: [PATCH] fix code Signed-off-by: zhuchaochao Change-Id: I249b2e25eb1564f8643945c20acd9c5bebd7e3ca --- .../audio_bluetooth_manager.cpp | 22 ++++++-- .../bluetoothclient/audio_bluetooth_manager.h | 5 +- .../include/service/audio_policy_service.h | 2 + .../src/service/audio_policy_service.cpp | 55 ++++++++++++------- 4 files changed, 56 insertions(+), 28 deletions(-) diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index d42522500..4e3a99b22 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp @@ -349,13 +349,19 @@ void AudioHfpManager::DisconnectBluetoothHfpSink() HfpBluetoothDeviceManager::ClearAllHfpBluetoothDevice(); } -void AudioHfpManager::SetActiveHfpDevice(BluetoothRemoteDevice &device) +void AudioHfpManager::UpdateCurrentActiveHfpDevice(BluetoothRemoteDevice &device) { std::lock_guard hfpDeviceLock(g_activehfpDeviceLock); activeHfpDevice_ = device; } -void AudioHfpManager::SetAudioScene(AudioScene scene) +std::string AudioHfpManager::GetCurrentActiveHfpDevice() +{ + std::lock_guard hfpDeviceLock(g_activehfpDeviceLock); + return activeHfpDevice_.GetDeviceAddr(); +} + +void AudioHfpManager::UpdateAudioScene(AudioScene scene) { std::lock_guard sceneLock(g_audioSceneLock); scene_ = scene; @@ -368,6 +374,11 @@ void AudioHfpListener::OnScoStateChanged(const BluetoothRemoteDevice &device, in if (scoState == HfpScoConnectState::SCO_CONNECTED || scoState == HfpScoConnectState::SCO_DISCONNECTED) { bool isConnected = (scoState == HfpScoConnectState::SCO_CONNECTED) ? true : false; HfpBluetoothDeviceManager::OnScoStateChanged(device, isConnected); + if (device.GetDeviceAddr() == AudioHfpManager::GetCurrentActiveHfpDevice() && + scoState == HfpScoConnectState::SCO_DISCONNECTED) { + AUDIO_INFO_LOG("Sco disconnect, need set audio scene as default."); + AudioHfpManager::UpdateAudioScene(AUDIO_SCENE_DEFAULT); + } } } @@ -378,10 +389,11 @@ void AudioHfpListener::OnConnectionStateChanged(const BluetoothRemoteDevice &dev HfpBluetoothDeviceManager::SetHfpStack(device, BluetoothDeviceAction::CONNECT_ACTION); } if (state == static_cast(BTConnectState::DISCONNECTED)) { - if (device.GetDeviceAddr() == AudioHfpManager::GetActiveHfpDevice()) { + if (device.GetDeviceAddr() == AudioHfpManager::GetCurrentActiveHfpDevice()) { BluetoothRemoteDevice defaultDevice; - AudioHfpManager::SetActiveHfpDevice(defaultDevice); - AudioHfpManager::SetAudioScene(AUDIO_SCENE_DEFAULT); + AudioHfpManager::UpdateCurrentActiveHfpDevice(defaultDevice); + AUDIO_INFO_LOG("Current active hfp device diconnect, need set audio scene as default."); + AudioHfpManager::UpdateAudioScene(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 1ad2bb530..db59fd1d8 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h @@ -105,8 +105,9 @@ 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); + static void UpdateCurrentActiveHfpDevice(BluetoothRemoteDevice &device); + static std::string GetCurrentActiveHfpDevice(); + static void UpdateAudioScene(AudioStandard::AudioScene scene); private: static HandsFreeAudioGateway *hfpInstance_; 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 ebb67a478..7243b7ff3 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -107,6 +107,8 @@ public: void NotifyRemoteRenderState(std::string networkId, std::string condition, std::string value); + void NotifyUserSelectionEventToBt(sptr audioDeviceDescriptor); + int32_t SelectOutputDevice(sptr audioRendererFilter, std::vector> audioDeviceDescriptors); int32_t SelectFastOutputDevice(sptr audioRendererFilter, 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 dcd0e631b..6ba0a2e24 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -766,6 +766,35 @@ int32_t AudioPolicyService::DeviceParamsCheck(DeviceRole targetRole, return SUCCESS; } +void AudioPolicyService::NotifyUserSelectionEventToBt(sptr audioDeviceDescriptor) +{ + if (audioDeviceDescriptor == nullptr) { + return; + } +#ifdef BLUETOOTH_ENABLE + if (currentActiveDevice_.deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO && + audioDeviceDescriptor->deviceType_ != DEVICE_TYPE_BLUETOOTH_SCO) { + Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_SCO, + currentActiveDevice_.macAddress_, USER_NOT_SELECT_BT); + } + if (currentActiveDevice_.deviceType_ != DEVICE_TYPE_BLUETOOTH_SCO && + audioDeviceDescriptor->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO) { + Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_SCO, + audioDeviceDescriptor->macAddress_, USER_SELECT_BT); + } + if (currentActiveDevice_.deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP && + audioDeviceDescriptor->deviceType_ != DEVICE_TYPE_BLUETOOTH_A2DP) { + Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_A2DP, + currentActiveDevice_.macAddress_, USER_NOT_SELECT_BT); + } + if (currentActiveDevice_.deviceType_ != DEVICE_TYPE_BLUETOOTH_A2DP && + audioDeviceDescriptor->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) { + Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_A2DP, + audioDeviceDescriptor->macAddress_, USER_SELECT_BT); + } +#endif +} + int32_t AudioPolicyService::SelectOutputDevice(sptr audioRendererFilter, std::vector> audioDeviceDescriptors) { @@ -787,26 +816,7 @@ int32_t AudioPolicyService::SelectOutputDevice(sptr audioRe } else { audioStateManager_.SetPerferredMediaRenderDevice(audioDeviceDescriptors[0]); } - if (currentActiveDevice_.deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO && - audioDeviceDescriptors[0]->deviceType_ != DEVICE_TYPE_BLUETOOTH_SCO) { - Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_SCO, - currentActiveDevice_.macAddress_, USER_NOT_SELECT_BT); - } - if (currentActiveDevice_.deviceType_ != DEVICE_TYPE_BLUETOOTH_SCO && - audioDeviceDescriptors[0]->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO) { - Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_SCO, - audioDeviceDescriptors[0]->macAddress_, USER_SELECT_BT); - } - if (currentActiveDevice_.deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP && - audioDeviceDescriptors[0]->deviceType_ != DEVICE_TYPE_BLUETOOTH_A2DP) { - Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_A2DP, - currentActiveDevice_.macAddress_, USER_NOT_SELECT_BT); - } - if (currentActiveDevice_.deviceType_ != DEVICE_TYPE_BLUETOOTH_A2DP && - audioDeviceDescriptors[0]->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) { - Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_A2DP, - audioDeviceDescriptors[0]->macAddress_, USER_SELECT_BT); - } + NotifyUserSelectionEventToBt(audioDeviceDescriptors[0]); std::string networkId = audioDeviceDescriptors[0]->networkId_; DeviceType deviceType = audioDeviceDescriptors[0]->deviceType_; if ((deviceType != DEVICE_TYPE_BLUETOOTH_A2DP) || (networkId != LOCAL_NETWORK_ID)) { @@ -2357,16 +2367,18 @@ int32_t AudioPolicyService::SetDeviceActive(InternalDeviceType deviceType, bool auto itr = std::find_if(deviceList.begin(), deviceList.end(), isPresent); CHECK_AND_RETURN_RET_LOG(itr != deviceList.end(), ERR_OPERATION_FAILED, "Requested device not available %{public}d ", deviceType); - if (!active) { audioStateManager_.SetPerferredCallRenderDevice(new(std::nothrow) AudioDeviceDescriptor()); +#ifdef BLUETOOTH_ENABLE if (currentActiveDevice_.deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO && deviceType == DEVICE_TYPE_BLUETOOTH_SCO) { Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_SCO, currentActiveDevice_.macAddress_, USER_NOT_SELECT_BT); } +#endif } else { audioStateManager_.SetPerferredCallRenderDevice(*itr); +#ifdef BLUETOOTH_ENABLE if (currentActiveDevice_.deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO && deviceType != DEVICE_TYPE_BLUETOOTH_SCO) { Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_SCO, @@ -2377,6 +2389,7 @@ int32_t AudioPolicyService::SetDeviceActive(InternalDeviceType deviceType, bool Bluetooth::SendUserSelectionEvent(DEVICE_TYPE_BLUETOOTH_SCO, (*itr)->macAddress_, USER_SELECT_BT); } +#endif } FetchDevice(true); return SUCCESS; -- Gitee