diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index d81c0fcf7c96028835684429121d405be353324d..5a0551cc11c60faa4f578421f4c5a3f77aa2aae0 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp @@ -125,6 +125,15 @@ int32_t AudioA2dpManager::SetActiveA2dpDevice(const std::string& macAddress) return SUCCESS; } +std::string AudioA2dpManager::GetActiveA2dpDevice() +{ + std::lock_guard a2dpLock(g_a2dpInstanceLock); + a2dpInstance_ = A2dpSource::GetProfile(); + CHECK_AND_RETURN_RET_LOG(a2dpInstance_ != nullptr, "", "Failed to obtain A2DP profile instance"); + BluetoothRemoteDevice device = a2dpInstance_->GetActiveSinkDevice(); + return device.GetDeviceAddr(); +} + int32_t AudioA2dpManager::SetDeviceAbsVolume(const std::string& macAddress, int32_t volume) { BluetoothRemoteDevice device; @@ -260,6 +269,14 @@ int32_t AudioHfpManager::SetActiveHfpDevice(const std::string &macAddress) return SUCCESS; } +std::string AudioHfpManager::GetActiveHfpDevice() +{ + std::lock_guard hfpLock(g_hfpInstanceLock); + CHECK_AND_RETURN_RET_LOG(hfpInstance_ != nullptr, "", "HFP AG profile instance unavailable"); + BluetoothRemoteDevice device = hfpInstance_->GetActiveDevice(); + return device.GetDeviceAddr(); +} + int32_t AudioHfpManager::ConnectScoWithAudioScene(AudioScene scene) { AUDIO_INFO_LOG("Entered %{public}s,\ @@ -284,6 +301,7 @@ int32_t AudioHfpManager::ConnectScoWithAudioScene(AudioScene scene) } int8_t newScoCategory = GetScoCategoryFromScene(scene); if (newScoCategory != ScoCategory::SCO_DEFAULT) { + AUDIO_INFO_LOG("Entered to connectSco."); ret = hfpInstance_->ConnectSco(static_cast(newScoCategory)); CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "ConnectScoWithAudioScene failed, result: %{public}d", ret); } @@ -314,6 +332,7 @@ int8_t AudioHfpManager::GetScoCategoryFromScene(AudioScene scene) case AUDIO_SCENE_PHONE_CALL: return ScoCategory::SCO_CALLULAR; case AUDIO_SCENE_PHONE_CHAT: + case AUDIO_SCENE_RINGING: return ScoCategory::SCO_VIRTUAL; default: return ScoCategory::SCO_DEFAULT; diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h index f98c7a16c1da18e2fe89aea8f557f86f08ea7dba..37984060ef6fb49f386ad221d0b1c4f6cfee2d1f 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h @@ -48,6 +48,7 @@ public: static void UnregisterBluetoothA2dpListener(); static void DisconnectBluetoothA2dpSink(); static int32_t SetActiveA2dpDevice(const std::string& macAddress); + static std::string GetActiveA2dpDevice(); static int32_t SetDeviceAbsVolume(const std::string& macAddress, int32_t volume); static int32_t GetA2dpDeviceStreamInfo(const std::string& macAddress, AudioStandard::AudioStreamInfo &streamInfo); @@ -99,6 +100,7 @@ public: static void RegisterBluetoothScoListener(); static void UnregisterBluetoothScoListener(); static int32_t SetActiveHfpDevice(const std::string &macAddress); + static std::string GetActiveHfpDevice(); static int32_t ConnectScoWithAudioScene(AudioStandard::AudioScene scene); static int32_t DisconnectSco(); static int8_t GetScoCategoryFromScene(AudioStandard::AudioScene scene); diff --git a/frameworks/native/bluetoothclient/bluetooth_device_manager.cpp b/frameworks/native/bluetoothclient/bluetooth_device_manager.cpp index 92509172d9931cc369325b49459ae99aedd5de6b..34257406fc54410f720c28fdce11ee6aacde91f0 100644 --- a/frameworks/native/bluetoothclient/bluetooth_device_manager.cpp +++ b/frameworks/native/bluetoothclient/bluetooth_device_manager.cpp @@ -210,8 +210,13 @@ void MediaBluetoothDeviceManager::HandleWearDevice(const BluetoothRemoteDevice & wearDetectionStateMap_[device.GetDeviceAddr()] = BluetoothDeviceAction::WEAR_ACTION; } AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP; + desc.macAddress_ = device.GetDeviceAddr(); desc.deviceCategory_ = BT_HEADPHONE; - NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD); + std::lock_guard observerLock(g_observerLock); + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE); + } } void MediaBluetoothDeviceManager::HandleUnwearDevice(const BluetoothRemoteDevice &device) @@ -228,8 +233,13 @@ void MediaBluetoothDeviceManager::HandleUnwearDevice(const BluetoothRemoteDevice wearDetectionStateMap_[device.GetDeviceAddr()] = BluetoothDeviceAction::UNWEAR_ACTION; } AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP; + desc.macAddress_ = device.GetDeviceAddr(); desc.deviceCategory_ = BT_UNWEAR_HEADPHONE; - NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD); + std::lock_guard observerLock(g_observerLock); + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE); + } } void MediaBluetoothDeviceManager::HandleEnableDevice(const BluetoothRemoteDevice &device) @@ -239,29 +249,13 @@ void MediaBluetoothDeviceManager::HandleEnableDevice(const BluetoothRemoteDevice return; } AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP; + desc.macAddress_ = device.GetDeviceAddr(); desc.isEnable_ = true; - int cod = DEFAULT_COD; - int majorClass = DEFAULT_MAJOR_CLASS; - int majorMinorClass = DEFAULT_MAJOR_MINOR_CLASS; - device.GetDeviceProductType(cod, majorClass, majorMinorClass); - DeviceCategory bluetoothCategory = CATEGORY_DEFAULT; - auto pos = bluetoothDeviceCategoryMap_.find(std::make_pair(majorClass, majorMinorClass)); - if (pos != bluetoothDeviceCategoryMap_.end()) { - bluetoothCategory = pos->second; - } - if (bluetoothCategory == BT_HEADPHONE) { - std::lock_guard wearStateMapLock(g_a2dpWearStateMapLock); - auto wearStateIter = wearDetectionStateMap_.find(device.GetDeviceAddr()); - if (wearStateIter != wearDetectionStateMap_.end() && - wearStateIter->second == BluetoothDeviceAction::WEAR_ACTION) { - desc.deviceCategory_ = BT_HEADPHONE; - } else { - desc.deviceCategory_ = BT_UNWEAR_HEADPHONE; - } - } else { - desc.deviceCategory_ = bluetoothCategory; + std::lock_guard observerLock(g_observerLock); + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::ENABLE_UPDATE); } - NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD); } void MediaBluetoothDeviceManager::HandleDisableDevice(const BluetoothRemoteDevice &device) @@ -271,29 +265,13 @@ void MediaBluetoothDeviceManager::HandleDisableDevice(const BluetoothRemoteDevic return; } AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP; + desc.macAddress_ = device.GetDeviceAddr(); desc.isEnable_ = false; - int cod = DEFAULT_COD; - int majorClass = DEFAULT_MAJOR_CLASS; - int majorMinorClass = DEFAULT_MAJOR_MINOR_CLASS; - device.GetDeviceProductType(cod, majorClass, majorMinorClass); - DeviceCategory bluetoothCategory = CATEGORY_DEFAULT; - auto pos = bluetoothDeviceCategoryMap_.find(std::make_pair(majorClass, majorMinorClass)); - if (pos != bluetoothDeviceCategoryMap_.end()) { - bluetoothCategory = pos->second; - } - if (bluetoothCategory == BT_HEADPHONE) { - std::lock_guard wearStateMapLock(g_a2dpWearStateMapLock); - auto wearStateIter = wearDetectionStateMap_.find(device.GetDeviceAddr()); - if (wearStateIter != wearDetectionStateMap_.end() && - wearStateIter->second == BluetoothDeviceAction::WEAR_ACTION) { - desc.deviceCategory_ = BT_HEADPHONE; - } else { - desc.deviceCategory_ = BT_UNWEAR_HEADPHONE; - } - } else { - desc.deviceCategory_ = bluetoothCategory; + std::lock_guard observerLock(g_observerLock); + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::ENABLE_UPDATE); } - NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD); } void MediaBluetoothDeviceManager::HandleWearEnable(const BluetoothRemoteDevice &device) @@ -306,6 +284,8 @@ void MediaBluetoothDeviceManager::HandleWearEnable(const BluetoothRemoteDevice & RemoveDeviceInConfigVector(device, privacyDevices_); std::lock_guard wearStateMapLock(g_a2dpWearStateMapLock); AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP; + desc.macAddress_ = device.GetDeviceAddr(); auto wearStateIter = wearDetectionStateMap_.find(device.GetDeviceAddr()); if (wearStateIter != wearDetectionStateMap_.end() && wearStateIter->second == BluetoothDeviceAction::WEAR_ACTION) { @@ -315,7 +295,10 @@ void MediaBluetoothDeviceManager::HandleWearEnable(const BluetoothRemoteDevice & AddDeviceInConfigVector(device, negativeDevices_); desc.deviceCategory_ = BT_UNWEAR_HEADPHONE; } - NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD); + std::lock_guard observerLock(g_observerLock); + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE); + } } void MediaBluetoothDeviceManager::HandleWearDisable(const BluetoothRemoteDevice &device) @@ -328,8 +311,13 @@ void MediaBluetoothDeviceManager::HandleWearDisable(const BluetoothRemoteDevice RemoveDeviceInConfigVector(device, negativeDevices_); AddDeviceInConfigVector(device, privacyDevices_); AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP; + desc.macAddress_ = device.GetDeviceAddr(); desc.deviceCategory_ = BT_HEADPHONE; - NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD); + std::lock_guard observerLock(g_observerLock); + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE); + } } void MediaBluetoothDeviceManager::HandleUserSelection(const BluetoothRemoteDevice &device) @@ -562,8 +550,13 @@ void HfpBluetoothDeviceManager::HandleWearDevice(const BluetoothRemoteDevice &de wearDetectionStateMap_[device.GetDeviceAddr()] = BluetoothDeviceAction::WEAR_ACTION; } AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO; + desc.macAddress_ = device.GetDeviceAddr(); desc.deviceCategory_ = BT_HEADPHONE; - NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD); + std::lock_guard observerLock(g_observerLock); + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE); + } } void HfpBluetoothDeviceManager::HandleUnwearDevice(const BluetoothRemoteDevice &device) @@ -580,8 +573,13 @@ void HfpBluetoothDeviceManager::HandleUnwearDevice(const BluetoothRemoteDevice & wearDetectionStateMap_[device.GetDeviceAddr()] = BluetoothDeviceAction::UNWEAR_ACTION; } AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO; + desc.macAddress_ = device.GetDeviceAddr(); desc.deviceCategory_ = BT_UNWEAR_HEADPHONE; - NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD); + std::lock_guard observerLock(g_observerLock); + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE); + } } void HfpBluetoothDeviceManager::HandleEnableDevice(const BluetoothRemoteDevice &device) @@ -591,29 +589,13 @@ void HfpBluetoothDeviceManager::HandleEnableDevice(const BluetoothRemoteDevice & return; } AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO; + desc.macAddress_ = device.GetDeviceAddr(); desc.isEnable_ = true; - int cod = DEFAULT_COD; - int majorClass = DEFAULT_MAJOR_CLASS; - int majorMinorClass = DEFAULT_MAJOR_MINOR_CLASS; - device.GetDeviceProductType(cod, majorClass, majorMinorClass); - DeviceCategory bluetoothCategory = CATEGORY_DEFAULT; - auto pos = bluetoothDeviceCategoryMap_.find(std::make_pair(majorClass, majorMinorClass)); - if (pos != bluetoothDeviceCategoryMap_.end()) { - bluetoothCategory = pos->second; - } - if (bluetoothCategory == BT_HEADPHONE) { - std::lock_guard wearStateMapLock(g_hfpWearStateMapLock); - auto wearStateIter = wearDetectionStateMap_.find(device.GetDeviceAddr()); - if (wearStateIter != wearDetectionStateMap_.end() && - wearStateIter->second == BluetoothDeviceAction::WEAR_ACTION) { - desc.deviceCategory_ = BT_HEADPHONE; - } else { - desc.deviceCategory_ = BT_UNWEAR_HEADPHONE; - } - } else { - desc.deviceCategory_ = bluetoothCategory; + std::lock_guard observerLock(g_observerLock); + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::ENABLE_UPDATE); } - NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD); } void HfpBluetoothDeviceManager::HandleDisableDevice(const BluetoothRemoteDevice &device) @@ -623,29 +605,13 @@ void HfpBluetoothDeviceManager::HandleDisableDevice(const BluetoothRemoteDevice return; } AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO; + desc.macAddress_ = device.GetDeviceAddr(); desc.isEnable_ = false; - int cod = DEFAULT_COD; - int majorClass = DEFAULT_MAJOR_CLASS; - int majorMinorClass = DEFAULT_MAJOR_MINOR_CLASS; - device.GetDeviceProductType(cod, majorClass, majorMinorClass); - DeviceCategory bluetoothCategory = CATEGORY_DEFAULT; - auto pos = bluetoothDeviceCategoryMap_.find(std::make_pair(majorClass, majorMinorClass)); - if (pos != bluetoothDeviceCategoryMap_.end()) { - bluetoothCategory = pos->second; - } - if (bluetoothCategory == BT_HEADPHONE) { - std::lock_guard wearStateMapLock(g_hfpWearStateMapLock); - auto wearStateIter = wearDetectionStateMap_.find(device.GetDeviceAddr()); - if (wearStateIter != wearDetectionStateMap_.end() && - wearStateIter->second == BluetoothDeviceAction::WEAR_ACTION) { - desc.deviceCategory_ = BT_HEADPHONE; - } else { - desc.deviceCategory_ = BT_UNWEAR_HEADPHONE; - } - } else { - desc.deviceCategory_ = bluetoothCategory; + std::lock_guard observerLock(g_observerLock); + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::ENABLE_UPDATE); } - NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD); } void HfpBluetoothDeviceManager::HandleWearEnable(const BluetoothRemoteDevice &device) @@ -658,6 +624,8 @@ void HfpBluetoothDeviceManager::HandleWearEnable(const BluetoothRemoteDevice &de RemoveDeviceInConfigVector(device, privacyDevices_); std::lock_guard wearStateMapLock(g_hfpWearStateMapLock); AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO; + desc.macAddress_ = device.GetDeviceAddr(); auto wearStateIter = wearDetectionStateMap_.find(device.GetDeviceAddr()); if (wearStateIter != wearDetectionStateMap_.end() && wearStateIter->second == BluetoothDeviceAction::WEAR_ACTION) { @@ -667,7 +635,10 @@ void HfpBluetoothDeviceManager::HandleWearEnable(const BluetoothRemoteDevice &de AddDeviceInConfigVector(device, negativeDevices_); desc.deviceCategory_ = BT_UNWEAR_HEADPHONE; } - NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD); + std::lock_guard observerLock(g_observerLock); + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE); + } } void HfpBluetoothDeviceManager::HandleWearDisable(const BluetoothRemoteDevice &device) @@ -680,8 +651,13 @@ void HfpBluetoothDeviceManager::HandleWearDisable(const BluetoothRemoteDevice &d RemoveDeviceInConfigVector(device, negativeDevices_); AddDeviceInConfigVector(device, privacyDevices_); AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO; + desc.macAddress_ = device.GetDeviceAddr(); desc.deviceCategory_ = BT_HEADPHONE; - NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD); + std::lock_guard observerLock(g_observerLock); + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE); + } } void HfpBluetoothDeviceManager::HandleUserSelection(const BluetoothRemoteDevice &device) @@ -784,12 +760,14 @@ void HfpBluetoothDeviceManager::ClearAllHfpBluetoothDevice() void HfpBluetoothDeviceManager::OnScoStateChanged(const BluetoothRemoteDevice &device, bool isConnected) { + AudioDeviceDescriptor desc; + desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO; + desc.macAddress_ = device.GetDeviceAddr(); + desc.connectState_ = isConnected == true ? ConnectState::CONNECTED : ConnectState::DEACTIVE_CONNECTED; std::lock_guard observerLock(g_observerLock); - if (g_deviceObserver == nullptr) { - AUDIO_ERR_LOG("OnScoStateChanged, device observer is null"); - return; + if (g_deviceObserver != nullptr) { + g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CONNECTSTATE_UPDATE); } - g_deviceObserver->OnScoStateChanged(device.GetDeviceAddr(), isConnected); } } // namespace Bluetooth } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/native/audiocommon/include/audio_device_info.h b/interfaces/inner_api/native/audiocommon/include/audio_device_info.h index 129423922960d0c960b21cf33ad961086e9f022d..dd1dc660d373a803b180b55bb0a473d94dfede60 100644 --- a/interfaces/inner_api/native/audiocommon/include/audio_device_info.h +++ b/interfaces/inner_api/native/audiocommon/include/audio_device_info.h @@ -210,9 +210,8 @@ enum DeviceUsage { }; enum DeviceInfoUpdateCommand { - CATEGORY_UPDATE, + CATEGORY_UPDATE = 1, CONNECTSTATE_UPDATE, - SCO_REAL_CONNECT_UPDATE, ENABLE_UPDATE, }; diff --git a/services/audio_policy/server/include/service/audio_device_manager.h b/services/audio_policy/server/include/service/audio_device_manager.h index 50082a47763f6e6053f86286c6d4581d7bd5ce83..4384e783858b4a7df190e256379be01feab1b972 100644 --- a/services/audio_policy/server/include/service/audio_device_manager.h +++ b/services/audio_policy/server/include/service/audio_device_manager.h @@ -43,7 +43,7 @@ public: int32_t GetDeviceUsageFromType(const DeviceType devType) const; void ParseDeviceXml(); void UpdateDevicesListInfo(const sptr &deviceDescriptor, - const DeviceInfoUpdateCommand &updateCommand); + const DeviceInfoUpdateCommand updateCommand); vector> GetRemoteRenderDevices(); vector> GetRemoteCaptureDevices(); @@ -114,6 +114,7 @@ private: void RemoveCaptureDevices(const AudioDeviceDescriptor &devDesc); void UpdateConnectState(const shared_ptr &devDesc); void UpdateDeviceCategory(const sptr &deviceDescriptor); + void UpdateEnableState(const shared_ptr &deviceDescriptor); list privacyDeviceList_; list publicDeviceList_; 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 eae63c904e9125577b4f3b3b61d281afacf48e05..49f5a6a5a71baa10f711e8a1e1068a69c0f11af2 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -201,6 +201,9 @@ public: void HandleOfflineDistributedDevice(); + int32_t HandleDistributedDeviceUpdate(DStatusInfo &statusInfo, + std::vector> &deviceChangeDescriptor); + void OnServiceConnected(AudioServiceIndex serviceIndex); void OnServiceDisconnected(AudioServiceIndex serviceIndex); @@ -388,6 +391,8 @@ public: void OnScoStateChanged(const std::string &macAddress, bool isConnnected); + void OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand updateCommand); + void UpdateA2dpOffloadFlagBySpatialService(const std::string& macAddress); private: diff --git a/services/audio_policy/server/include/service/interface/idevice_status_observer.h b/services/audio_policy/server/include/service/interface/idevice_status_observer.h index 49ca02501dd5b59e36e0a40b3f32366925ae2a05..ffe87dd8393d3bb1ef985a7ef789b5440fb41c37 100644 --- a/services/audio_policy/server/include/service/interface/idevice_status_observer.h +++ b/services/audio_policy/server/include/service/interface/idevice_status_observer.h @@ -36,6 +36,7 @@ public: virtual void OnForcedDeviceSelected(DeviceType devType, const std::string &macAddress) = 0; virtual void OnDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected) = 0; virtual void OnScoStateChanged(const std::string &macAddress, bool isConnected) = 0; + virtual void OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand updateCommand) = 0; }; } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_policy/server/include/service/routers/router_base.h b/services/audio_policy/server/include/service/routers/router_base.h index 7f238266cb1fde9c294c2dae9558437a7bf586ae..8e9f0e142e74c1581469f90e14e0899ef292093a 100644 --- a/services/audio_policy/server/include/service/routers/router_base.h +++ b/services/audio_policy/server/include/service/routers/router_base.h @@ -44,7 +44,7 @@ public: { // remove abnormal device for (size_t i = 0; i < descs.size(); i++) { - if (descs[i]->exceptionFlag_) { + if (descs[i]->exceptionFlag_ || descs[i]->connectState_ == SUSPEND_CONNECTED) { descs.erase(descs.begin() + i); i--; } @@ -64,7 +64,7 @@ public: std::vector> &captureDescs) { for (auto &captureDesc : captureDescs) { - if (captureDesc->deviceId_ == desc->deviceId_) { + if (captureDesc->deviceId_ == desc->deviceId_ && captureDesc->connectState_ != SUSPEND_CONNECTED) { return std::move(captureDesc); } } diff --git a/services/audio_policy/server/src/service/audio_device_manager.cpp b/services/audio_policy/server/src/service/audio_device_manager.cpp index eac0f21dd123c6ee6ab638212ff947aba0b5639e..6e0d0b83e6cc5f53793a5e1030150d131da173c3 100644 --- a/services/audio_policy/server/src/service/audio_device_manager.cpp +++ b/services/audio_policy/server/src/service/audio_device_manager.cpp @@ -646,13 +646,21 @@ void AudioDeviceManager::UpdateScoState(const std::string &macAddress, bool isCo } void AudioDeviceManager::UpdateDevicesListInfo(const sptr &deviceDescriptor, - const DeviceInfoUpdateCommand &updateCommand) + const DeviceInfoUpdateCommand updateCommand) { shared_ptr devDesc = make_shared(deviceDescriptor); - if (updateCommand == CATEGORY_UPDATE) { - UpdateDeviceCategory(deviceDescriptor); - } else if (updateCommand == CONNECTSTATE_UPDATE) { - UpdateConnectState(devDesc); + switch (updateCommand) { + case CATEGORY_UPDATE: + UpdateDeviceCategory(deviceDescriptor); + break; + case CONNECTSTATE_UPDATE: + UpdateConnectState(devDesc); + break; + case ENABLE_UPDATE: + UpdateEnableState(devDesc); + break; + default: + break; } } @@ -680,24 +688,47 @@ void AudioDeviceManager::UpdateConnectState(const shared_ptrdeviceType_ == DEVICE_TYPE_BLUETOOTH_SCO; for (auto &desc : connectedDevices_) { - if (desc->networkId_ == devDesc->networkId_ && - desc->macAddress_ == devDesc->macAddress_) { - if (desc->deviceType_ == devDesc->deviceType_) { - desc->connectState_ = devDesc->connectState_; - } else if (isScoDevice && desc->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP && + if (desc->networkId_ != devDesc->networkId_ || + desc->macAddress_ != devDesc->macAddress_) { + continue; + } + if (desc->deviceType_ == devDesc->deviceType_) { + desc->connectState_ = devDesc->connectState_; + continue; + } + // a2dp connectState needs to be updated simultaneously when connectState of sco is updated + if (isScoDevice) { + if (desc->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP && devDesc->connectState_ == CONNECTED) { - // sco connected,a2dp suspend + // sco connected, suspend a2dp desc->connectState_ = SUSPEND_CONNECTED; - } else if (isScoDevice && desc->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP && - devDesc->connectState_ == DEACTIVE_CONNECTED && - desc->connectState_ == SUSPEND_CONNECTED) { - // sco deactive,a2dp CONNECTED + } else if (desc->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP && + desc->connectState_ == SUSPEND_CONNECTED && + devDesc->connectState_ == DEACTIVE_CONNECTED) { + // sco deactive, a2dp CONNECTED desc->connectState_ = CONNECTED; } } } } +void AudioDeviceManager::UpdateEnableState(const shared_ptr &devDesc) +{ + for (auto &desc : connectedDevices_) { + if (devDesc->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP || + devDesc->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO) { + if (desc->macAddress_ == devDesc->macAddress_ && + desc->isEnable_ != devDesc->isEnable_) { + desc->isEnable_ = devDesc->isEnable_; + } + } else if (desc->deviceType_ == devDesc->deviceType_ && + desc->networkId_ == devDesc->networkId_ && + desc->isEnable_ != devDesc->isEnable_) { + desc->isEnable_ = devDesc->isEnable_; + } + } +} + void AudioDeviceManager::AddBtToOtherList(const shared_ptr &devDesc) { if (devDesc->networkId_ != LOCAL_NETWORK_ID) { 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 7660d295a97f609d1a16a04a328d400ab9cf74e2..d6d27b3435fbf9da916b528fe17de8baa4de67ae 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -777,9 +777,9 @@ int32_t AudioPolicyService::SelectOutputDevice(sptr audioRe return SelectFastOutputDevice(audioRendererFilter, audioDeviceDescriptors[0]); } if (audioDeviceDescriptors[0]->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP || - audioDeviceDescriptors[0]->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO) { + audioDeviceDescriptors[0]->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO) { audioDeviceDescriptors[0]->isEnable_ = true; - audioDeviceManager_.AddNewDevice(audioDeviceDescriptors[0]); + audioDeviceManager_.UpdateDevicesListInfo(audioDeviceDescriptors[0], ENABLE_UPDATE); } StreamUsage strUsage = audioRendererFilter->rendererInfo.streamUsage; if (strUsage == STREAM_USAGE_VOICE_COMMUNICATION || strUsage == STREAM_USAGE_VOICE_MODEM_COMMUNICATION) { @@ -1661,7 +1661,7 @@ int32_t AudioPolicyService::HandleScoDeviceFetched(unique_ptrisScoRealConnected_) { + if (desc->connectState_ == DEACTIVE_CONNECTED) { Bluetooth::AudioHfpManager::ConnectScoWithAudioScene(audioScene_); return ERROR; } @@ -1779,7 +1779,7 @@ void AudioPolicyService::FetchInputDevice(vectorisScoRealConnected_) { + if (desc->connectState_ == DEACTIVE_CONNECTED) { Bluetooth::AudioHfpManager::ConnectScoWithAudioScene(audioScene_); return; } @@ -1994,6 +1994,10 @@ int32_t AudioPolicyService::SwitchActiveA2dpDevice(const sptrmacAddress_) { + AUDIO_INFO_LOG("a2dp device [%{public}s] is already active", deviceDescriptor->macAddress_.c_str()); + return SUCCESS; + } AUDIO_INFO_LOG("SelectNewDevice::a2dp device name [%{public}s]", (deviceDescriptor->deviceName_).c_str()); result = Bluetooth::AudioA2dpManager::SetActiveA2dpDevice(deviceDescriptor->macAddress_); CHECK_AND_RETURN_RET_LOG(result == SUCCESS, result, "SetActiveA2dpDevice failed %{public}d", result); @@ -3240,18 +3244,15 @@ void AudioPolicyService::HandleOfflineDistributedDevice() } } } + FetchDevice(true); + FetchDevice(false); TriggerDeviceChangedCallback(deviceChangeDescriptor, false); TriggerAvailableDeviceChangedCallback(deviceChangeDescriptor, false); } -void AudioPolicyService::OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isStop) +int32_t AudioPolicyService::HandleDistributedDeviceUpdate(DStatusInfo &statusInfo, + std::vector> &deviceChangeDescriptor) { - AUDIO_INFO_LOG("Device connection updated | HDI_PIN[%{public}d] CONNECT_STATUS[%{public}d] NETWORKID[%{public}s]", - statusInfo.hdiPin, statusInfo.isConnected, statusInfo.networkId); - if (isStop) { - HandleOfflineDistributedDevice(); - return; - } std::lock_guard lock(deviceStatusUpdateSharedMutex_); DeviceType devType = GetDeviceTypeFromPin(statusInfo.hdiPin); const std::string networkId = statusInfo.networkId; @@ -3263,16 +3264,15 @@ void AudioPolicyService::OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isSt statusInfo.mappingVolumeId); UpdateGroupInfo(INTERRUPT_TYPE, GROUP_NAME_DEFAULT, deviceDesc.interruptGroupId_, networkId, statusInfo.isConnected, statusInfo.mappingInterruptId); - std::vector> deviceChangeDescriptor = {}; if (statusInfo.isConnected) { for (auto devDes : connectedDevices_) { if (devDes->deviceType_ == devType && devDes->networkId_ == networkId) { - return; + return ERROR; } } int32_t ret = ActivateNewDevice(statusInfo.networkId, devType, statusInfo.connectType == ConnectType::CONNECT_TYPE_DISTRIBUTED); - CHECK_AND_RETURN_LOG(ret == SUCCESS, "DEVICE online but open audio device failed."); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR, "DEVICE online but open audio device failed."); UpdateConnectedDevicesWhenConnecting(deviceDesc, deviceChangeDescriptor); const sptr gsp = GetAudioServerProxy(); @@ -3289,8 +3289,25 @@ void AudioPolicyService::OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isSt RemoveDeviceInRouterMap(moduleName); RemoveDeviceInFastRouterMap(networkId); } + return SUCCESS; +} + +void AudioPolicyService::OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isStop) +{ + AUDIO_INFO_LOG("Device connection updated | HDI_PIN[%{public}d] CONNECT_STATUS[%{public}d] NETWORKID[%{public}s]", + statusInfo.hdiPin, statusInfo.isConnected, statusInfo.networkId); + if (isStop) { + HandleOfflineDistributedDevice(); + return; + } + std::vector> deviceChangeDescriptor = {}; + int32_t ret = HandleDistributedDeviceUpdate(statusInfo, deviceChangeDescriptor); + CHECK_AND_RETURN_LOG(ret == SUCCESS, "HandleDistributedDeviceUpdate return directly."); + FetchDevice(true); + FetchDevice(false); TriggerDeviceChangedCallback(deviceChangeDescriptor, statusInfo.isConnected); TriggerAvailableDeviceChangedCallback(deviceChangeDescriptor, statusInfo.isConnected); + DeviceType devType = GetDeviceTypeFromPin(statusInfo.hdiPin); if (GetDeviceRole(devType) == DeviceRole::INPUT_DEVICE) { remoteCapturerSwitch_ = true; } @@ -5504,5 +5521,27 @@ void AudioPolicyService::OnScoStateChanged(const std::string &macAddress, bool i FetchDevice(true); FetchDevice(false); } + +void AudioPolicyService::OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand updateCommand) +{ + AUDIO_INFO_LOG("[OnDeviceInfoUpdated] updateCommand: %{public}d", updateCommand); + if (updateCommand == ENABLE_UPDATE && desc.isEnable_ == true) { + unique_ptr userSelectMediaDevice = + AudioStateManager::GetAudioStateManager().GetPerferredMediaRenderDevice(); + unique_ptr userSelectCallDevice = + AudioStateManager::GetAudioStateManager().GetPerferredCallRenderDevice(); + if ((userSelectMediaDevice->deviceType_ == desc.deviceType_ && + userSelectMediaDevice->macAddress_ == desc.macAddress_) || + (userSelectCallDevice->deviceType_ == desc.deviceType_ && + userSelectCallDevice->macAddress_ == desc.macAddress_)) { + AUDIO_INFO_LOG("Current enable state has been set true during user selection, no need to be set again."); + return; + } + } + sptr audioDescriptor = new(std::nothrow) AudioDeviceDescriptor(desc); + audioDeviceManager_.UpdateDevicesListInfo(audioDescriptor, updateCommand); + FetchDevice(true); + FetchDevice(false); +} } // namespace AudioStandard } // namespace OHOS