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 cb7b9ac04a160b893311253e46917996788945a5..901b55f0d771f4125a382d5503f61f92377f5840 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -397,6 +397,8 @@ private: int32_t HandleFileDevice(DeviceType deviceType); + int32_t ActivateNormalNewDevice(DeviceType deviceType, bool isSceneActivation); + int32_t ActivateNewDevice(DeviceType deviceType, bool isSceneActivation); DeviceRole GetDeviceRole(AudioPin pin) const; 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 62b85d4963dee368f8f326c9eb3db7688f20978a..a4d2b32a060e38def1cac8580328593237d06cd7 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -1726,11 +1726,42 @@ int32_t AudioPolicyService::HandleFileDevice(DeviceType 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; - if (currentActiveDevice_.deviceType_ == deviceType) { if (deviceType != DEVICE_TYPE_BLUETOOTH_A2DP || currentActiveDevice_.macAddress_ == activeBTDevice_) { return result; @@ -1740,11 +1771,11 @@ int32_t AudioPolicyService::ActivateNewDevice(DeviceType deviceType, bool isScen result = HandleA2dpDevice(deviceType); return result; } - if (isArmUsbDevice_ && deviceType == DEVICE_TYPE_USB_HEADSET ) { + if (isArmUsbDevice_ && deviceType == DEVICE_TYPE_USB_HEADSET) { result = HandleArmUsbDevice(deviceType); return result; } - if (deviceType == DEVICE_TYPE_FILE_SINK ) { + if (deviceType == DEVICE_TYPE_FILE_SINK) { result = HandleFileDevice(deviceType); return result; } @@ -1760,38 +1791,7 @@ int32_t AudioPolicyService::ActivateNewDevice(DeviceType deviceType, bool isScen result = HandleFileDevice(deviceType); return result; } - - 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; + return ActivateNormalNewDevice(deviceType, isSceneActivation); } void AudioPolicyService::KeepPortMute(int32_t muteDuration, std::string portName, DeviceType deviceType)