From add0bb100dc26b2a1c562b75e413fe66a44f9368 Mon Sep 17 00:00:00 2001 From: huyue57 Date: Fri, 24 Nov 2023 17:31:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=AC=E7=AD=92=E4=BF=AE=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=B8=B8=E9=A9=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huyue57 Change-Id: I0c803ef6d68ef0b38ffbd609e0113ed0ea38022f --- .../sink/primary/audio_renderer_sink.cpp | 20 ++++-- .../include/service/audio_policy_service.h | 4 +- .../src/service/audio_policy_service.cpp | 66 ++++--------------- 3 files changed, 26 insertions(+), 64 deletions(-) diff --git a/frameworks/native/hdiadapter/sink/primary/audio_renderer_sink.cpp b/frameworks/native/hdiadapter/sink/primary/audio_renderer_sink.cpp index b11819b33c..ba538616f1 100644 --- a/frameworks/native/hdiadapter/sink/primary/audio_renderer_sink.cpp +++ b/frameworks/native/hdiadapter/sink/primary/audio_renderer_sink.cpp @@ -683,16 +683,16 @@ static int32_t SetOutputPortPin(DeviceType outputDevice, AudioRouteNode &sink) int32_t AudioRendererSinkInner::SetOutputRoute(DeviceType outputDevice) { + if (outputDevice == currentActiveDevice_) { + AUDIO_INFO_LOG("SetOutputRoute output device not change"); + return SUCCESS; + } AudioPortPin outputPortPin = PIN_OUT_SPEAKER; return SetOutputRoute(outputDevice, outputPortPin); } int32_t AudioRendererSinkInner::SetOutputRoute(DeviceType outputDevice, AudioPortPin &outputPortPin) { - if (outputDevice == currentActiveDevice_) { - AUDIO_INFO_LOG("SetOutputRoute output device not change"); - return SUCCESS; - } currentActiveDevice_ = outputDevice; AudioRouteNode source = {}; @@ -764,6 +764,7 @@ int32_t AudioRendererSinkInner::SetAudioScene(AudioScene audioScene, DeviceType AUDIO_DEBUG_LOG("OUTPUT port is %{public}d", audioSceneOutPort); int32_t ret = SUCCESS; + bool isAudioSceneUpdate = false; if (audioScene != currentAudioScene_) { struct AudioSceneDescriptor scene; scene.scene.id = GetAudioCategory(audioScene); @@ -776,11 +777,16 @@ int32_t AudioRendererSinkInner::SetAudioScene(AudioScene audioScene, DeviceType return ERR_OPERATION_FAILED; } currentAudioScene_ = audioScene; + isAudioSceneUpdate = true; } - ret = SetOutputRoute(activeDevice, audioSceneOutPort); - if (ret < 0) { - AUDIO_ERR_LOG("Update route FAILED: %{public}d", ret); + if (activeDevice != currentActiveDevice_ || + (isAudioSceneUpdate && + (currentAudioScene_ == AUDIO_SCENE_PHONE_CALL || currentAudioScene_ == AUDIO_SCENE_PHONE_CHAT))) { + ret = SetOutputRoute(activeDevice, audioSceneOutPort); + if (ret < 0) { + AUDIO_ERR_LOG("Update route FAILED: %{public}d", ret); + } } } return SUCCESS; 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 4c55509250..ae8fb52040 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -497,8 +497,6 @@ private: std::vector> GetDevicesForGroup(GroupType type, int32_t groupId); - void SetEarpieceState(); - void SetVolumeForSwitchDevice(DeviceType deviceType); void SetVoiceCallVolume(int32_t volume); @@ -562,6 +560,8 @@ private: std::vector> DeviceFilterByUsage(AudioDeviceUsage usage, const std::vector>& descs); + void AddEarpiece(); + bool interruptEnabled_ = true; bool isUpdateRouteSupported_ = true; bool isCurrentRemoteRenderer = false; 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 cd1ae9be2b..e3439edecd 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -64,7 +64,6 @@ const uint32_t PCM_32_BIT = 32; const uint32_t BT_BUFFER_ADJUSTMENT_FACTOR = 50; const uint32_t ABS_VOLUME_SUPPORT_RETRY_INTERVAL_IN_MICROSECONDS = 10000; const std::string AUDIO_SERVICE_PKG = "audio_manager_service"; -const uint32_t PRIORITY_LIST_OFFSET_POSTION = 1; std::shared_ptr g_dataShareHelper = nullptr; static sptr g_adProxy = nullptr; #ifdef BLUETOOTH_ENABLE @@ -2053,10 +2052,6 @@ int32_t AudioPolicyService::SetAudioScene(AudioScene audioScene) CHECK_AND_RETURN_RET_LOG(gsp != nullptr, ERR_OPERATION_FAILED, "Service proxy unavailable"); audioScene_ = audioScene; - if (isUpdateRouteSupported_) { - SetEarpieceState(); - } - int32_t result = gsp->SetAudioScene(audioScene, currentActiveDevice_.deviceType_); CHECK_AND_RETURN_RET_LOG(result == SUCCESS, ERR_OPERATION_FAILED, "SetAudioScene failed [%{public}d]", result); @@ -2072,58 +2067,18 @@ int32_t AudioPolicyService::SetAudioScene(AudioScene audioScene) return SUCCESS; } -void AudioPolicyService::SetEarpieceState() +void AudioPolicyService::AddEarpiece() { - if (audioScene_ == AUDIO_SCENE_PHONE_CALL) { - AUDIO_INFO_LOG("SetEarpieceState: add earpiece device only for [phone], localDevicesType [%{public}s]", - localDevicesType_.c_str()); - if (localDevicesType_.compare("phone") != 0) { - return; - } - - // add earpiece to connectedDevices_ - auto isPresent = [](const sptr &desc) { - CHECK_AND_RETURN_RET_LOG(desc != nullptr, false, "Invalid device descriptor"); - return desc->deviceType_ == DEVICE_TYPE_EARPIECE; - }; - auto itr = std::find_if(connectedDevices_.begin(), connectedDevices_.end(), isPresent); - if (itr == connectedDevices_.end()) { - sptr audioDescriptor = - new (std::nothrow) AudioDeviceDescriptor(DEVICE_TYPE_EARPIECE, OUTPUT_DEVICE); - UpdateDisplayName(audioDescriptor); - audioDeviceManager_.AddNewDevice(audioDescriptor); - connectedDevices_.insert(connectedDevices_.begin(), audioDescriptor); - AUDIO_INFO_LOG("SetAudioScene: Add earpiece to connectedDevices_"); - } - - // add earpiece to outputPriorityList_ - auto earpiecePos = find(outputPriorityList_.begin(), outputPriorityList_.end(), DEVICE_TYPE_EARPIECE); - if (earpiecePos == outputPriorityList_.end()) { - outputPriorityList_.insert(outputPriorityList_.end() - PRIORITY_LIST_OFFSET_POSTION, - DEVICE_TYPE_EARPIECE); - AUDIO_INFO_LOG("SetAudioScene: Add earpiece to outputPriorityList_"); - } - } else { - // remove earpiece from connectedDevices_ - auto isPresent = [] (const sptr &desc) { - CHECK_AND_RETURN_RET_LOG(desc != nullptr, false, "Invalid device descriptor"); - return desc->deviceType_ == DEVICE_TYPE_EARPIECE; - }; - - std::lock_guard lock(deviceStatusUpdateSharedMutex_); - auto itr = std::find_if(connectedDevices_.begin(), connectedDevices_.end(), isPresent); - if (itr != connectedDevices_.end()) { - audioDeviceManager_.RemoveNewDevice(*itr); - connectedDevices_.erase(itr); - AUDIO_INFO_LOG("SetAudioScene: Remove earpiece from connectedDevices_"); - } - // remove earpiece from outputPriorityList_ - auto earpiecePos = find(outputPriorityList_.begin(), outputPriorityList_.end(), DEVICE_TYPE_EARPIECE); - if (earpiecePos != outputPriorityList_.end()) { - outputPriorityList_.erase(earpiecePos); - AUDIO_INFO_LOG("SetAudioScene: Remove earpiece from outputPriorityList_"); - } + if (localDevicesType_.compare("phone") != 0) { + return; } + sptr audioDescriptor = + new (std::nothrow) AudioDeviceDescriptor(DEVICE_TYPE_EARPIECE, OUTPUT_DEVICE); + audioDescriptor->deviceId_ = startDeviceId++; + UpdateDisplayName(audioDescriptor); + audioDeviceManager_.AddNewDevice(audioDescriptor); + connectedDevices_.insert(connectedDevices_.begin(), audioDescriptor); + AUDIO_INFO_LOG("Add earpiece to device list"); } std::string AudioPolicyService::GetLocalDevicesType() @@ -2877,6 +2832,7 @@ void AudioPolicyService::OnServiceConnected(AudioServiceIndex serviceIndex) currentActiveInputDevice_ = AudioDeviceDescriptor(*inDevice); SetVolumeForSwitchDevice(currentActiveDevice_.deviceType_); OnPreferredDeviceUpdated(currentActiveDevice_, currentActiveInputDevice_.deviceType_); + AddEarpiece(); for (auto it = pnpDeviceList_.begin(); it != pnpDeviceList_.end(); ++it) { OnPnpDeviceStatusUpdated((*it).first, (*it).second); } -- Gitee