From f9f63443e7259f17af9d366091058882e762b84a Mon Sep 17 00:00:00 2001 From: huyue57 Date: Wed, 22 Nov 2023 23:36:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=80=9A=E8=AF=9D?= =?UTF-8?q?=E6=8E=A5=E9=80=9A=E5=90=8E=EF=BC=8C=E5=BC=80=E5=90=AF=E5=85=8D?= =?UTF-8?q?=E6=8F=90=E5=A3=B0=E9=9F=B3=E5=A4=A7=E5=B0=8F=E4=B8=8D=E5=8F=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huyue57 Change-Id: Icd3dc4d6f0802bc3a616cd20c7c85abf605625a8 --- .../include/service/audio_policy_service.h | 4 + .../src/service/audio_policy_service.cpp | 98 ++++++++++++++++++- 2 files changed, 97 insertions(+), 5 deletions(-) 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 bd49ade4d1..30d0eb9448 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -442,6 +442,10 @@ private: int32_t HandleArmUsbDevice(DeviceType deviceType); + int32_t HandleFileDevice(DeviceType deviceType); + + int32_t ActivateNormalNewDevice(DeviceType deviceType, bool isSceneActivation); + int32_t ActivateNewDevice(DeviceType deviceType, bool isSceneActivation); void SelectNewOutputDevice(unique_ptr &rendererChangeInfo, 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 cfc437cf90..9a9a9648b8 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -2029,17 +2029,103 @@ int32_t AudioPolicyService::HandleArmUsbDevice(DeviceType deviceType) return SUCCESS; } +int32_t AudioPolicyService::HandleFileDevice(DeviceType deviceType) +{ + AUDIO_INFO_LOG("HandleFileDevice"); + + std::string sinkPortName = GetSinkPortName(deviceType); + std::string sourcePortName = GetSourcePortName(deviceType); + if (sinkPortName == PORT_NONE && sourcePortName == PORT_NONE) { + AUDIO_ERR_LOG("HandleFileDevice failed for sinkPortName and sourcePortName are none"); + return ERR_OPERATION_FAILED; + } + if (sinkPortName != PORT_NONE) { + AudioIOHandle ioHandle = GetSinkIOHandle(deviceType); + audioPolicyManager_.SetDeviceActive(ioHandle, deviceType, sinkPortName, true); + audioPolicyManager_.SuspendAudioDevice(sinkPortName, false); + } + if (sourcePortName != PORT_NONE) { + AudioIOHandle ioHandle = GetSourceIOHandle(deviceType); + audioPolicyManager_.SetDeviceActive(ioHandle, deviceType, sourcePortName, true); + audioPolicyManager_.SuspendAudioDevice(sourcePortName, false); + } + if (isUpdateRouteSupported_) { + UpdateActiveDeviceRoute(deviceType); + } + + UpdateInputDeviceInfo(deviceType); + return SUCCESS; +} + +int32_t AudioPolicyService::ActivateNormalNewDevice(DeviceType deviceType, bool isSceneActivation = false) +{ + bool isVolumeSwitched = false; + if (isUpdateRouteSupported_ && !isSceneActivation) { + if (GetDeviceRole(deviceType) == OUTPUT_DEVICE) { + int32_t muteDuration = 1200000; // us + std::string sinkPortName = GetSinkPortName(deviceType); + CHECK_AND_RETURN_RET_LOG(sinkPortName != PORT_NONE, + ERR_OPERATION_FAILED, + "Invalid port %{public}s", + sinkPortName.c_str()); + std::thread switchThread(&AudioPolicyService::KeepPortMute, this, muteDuration, sinkPortName, deviceType); + switchThread.detach(); + int32_t beforSwitchDelay = 300000; // 300 ms + usleep(beforSwitchDelay); + } + UpdateActiveDeviceRoute(deviceType); + if (GetDeviceRole(deviceType) == OUTPUT_DEVICE) { + if (GetVolumeGroupType(currentActiveDevice_.deviceType_) != GetVolumeGroupType(deviceType)) { + SetVolumeForSwitchDevice(deviceType); + } + isVolumeSwitched = true; + } + } + if (GetDeviceRole(deviceType) == OUTPUT_DEVICE && !isVolumeSwitched && + GetVolumeGroupType(currentActiveDevice_.deviceType_) != GetVolumeGroupType(deviceType)) { + SetVolumeForSwitchDevice(deviceType); + } + UpdateInputDeviceInfo(deviceType); + return SUCCESS; +} + int32_t AudioPolicyService::ActivateNewDevice(DeviceType deviceType, bool isSceneActivation = false) { + AUDIO_INFO_LOG("Switch device: [%{public}d]-->[%{public}d]", currentActiveDevice_.deviceType_, deviceType); int32_t result = SUCCESS; + ResetOffloadMode(); + if (currentActiveDevice_.deviceType_ == deviceType) { + if (deviceType != DEVICE_TYPE_BLUETOOTH_A2DP || currentActiveDevice_.macAddress_ == activeBTDevice_) { + return result; + } + } + if (deviceType == DEVICE_TYPE_BLUETOOTH_A2DP) { + result = HandleA2dpDevice(deviceType); + return result; + } if (isArmUsbDevice_ && deviceType == DEVICE_TYPE_USB_HEADSET) { result = HandleArmUsbDevice(deviceType); return result; } - - return SUCCESS; + if (deviceType == DEVICE_TYPE_FILE_SINK) { + result = HandleFileDevice(deviceType); + return result; + } + if (currentActiveDevice_.deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) { + result = HandleA2dpDevice(deviceType); + return result; + } + if (isArmUsbDevice_ && currentActiveDevice_.deviceType_ == DEVICE_TYPE_USB_HEADSET) { + result = HandleArmUsbDevice(deviceType); + return result; + } + if (currentActiveDevice_.deviceType_ == DEVICE_TYPE_FILE_SINK) { + result = HandleFileDevice(deviceType); + return result; + } + return ActivateNormalNewDevice(deviceType, isSceneActivation); } void AudioPolicyService::KeepPortMute(int32_t muteDuration, std::string portName, DeviceType deviceType) @@ -2434,11 +2520,13 @@ int32_t AudioPolicyService::HandleLocalDeviceConnected(DeviceType devType, const } } - // new device found. If connected, add into active device list - int32_t result = ActivateNewDevice(devType); - CHECK_AND_RETURN_RET_LOG(result == SUCCESS, result, "Failed to activate new device %{public}d", devType); ResetOffloadMode(); + if (isArmUsbDevice_ && deviceType == DEVICE_TYPE_USB_HEADSET) { + int32_t result = HandleArmUsbDevice(deviceType); + return result; + } + return SUCCESS; } -- Gitee From f10cbe251dfcf77b39c6452b95670bab565b3e20 Mon Sep 17 00:00:00 2001 From: huyue57 Date: Wed, 22 Nov 2023 23:39:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=80=9A=E8=AF=9D?= =?UTF-8?q?=E6=8E=A5=E9=80=9A=E5=90=8E=EF=BC=8C=E5=BC=80=E5=90=AF=E5=85=8D?= =?UTF-8?q?=E6=8F=90=E5=A3=B0=E9=9F=B3=E5=A4=A7=E5=B0=8F=E4=B8=8D=E5=8F=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huyue57 Change-Id: Ib118fb6ea083cf64d70aab289613a504a1b3e2db --- .../audio_policy/server/src/service/audio_policy_service.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 9a9a9648b8..25a1339adb 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -2522,8 +2522,8 @@ int32_t AudioPolicyService::HandleLocalDeviceConnected(DeviceType devType, const ResetOffloadMode(); - if (isArmUsbDevice_ && deviceType == DEVICE_TYPE_USB_HEADSET) { - int32_t result = HandleArmUsbDevice(deviceType); + if (isArmUsbDevice_ && devType == DEVICE_TYPE_USB_HEADSET) { + int32_t result = HandleArmUsbDevice(devType); return result; } -- Gitee