diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index d42522500d0026aca96fca18acd437a9ae46e9a3..4e3a99b22c0ee6b95f0f05bb4c2d8a14ede8a55f 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 1ad2bb5305f110fec0940f0606d6b655be7dbcc8..db59fd1d823c281c5d0880920ae400597731903f 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 ebb67a478b7cd6e53f0dcd6f7b67b5bce0c58131..7243b7ff385cb340b67cf42008016f95c2998efc 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 dcd0e631b218d476de136f81785871d55bbc532d..6ba0a2e241d97b954a36a182ded64fa733ad80f5 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;