From 3650dcf901ddb21ea2c9d358d776b662255336ba Mon Sep 17 00:00:00 2001 From: wangtao Date: Sat, 10 Aug 2024 13:30:13 +0000 Subject: [PATCH] fix pop Signed-off-by: wangtao Change-Id: Ib73fba96d2c284d026db68ff6b41096baf17a893 --- .../audio_bluetooth_manager.cpp | 1 + .../bluetooth/bluetooth_renderer_sink.cpp | 35 ++- .../sink/primary/audio_renderer_sink.cpp | 15 +- .../include/service/audio_policy_service.h | 23 +- .../service/common/audio_module_info.h | 1 + .../interface/idevice_status_observer.h | 1 - .../src/service/audio_policy_service.cpp | 211 +++++++----------- .../audio_service/server/src/audio_server.cpp | 18 +- 8 files changed, 144 insertions(+), 161 deletions(-) diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index 68be394c38..36f59de454 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp @@ -509,6 +509,7 @@ AudioStandard::AudioScene AudioHfpManager::GetPolicyAudioScene() void AudioHfpListener::OnScoStateChanged(const BluetoothRemoteDevice &device, int state, int reason) { AUDIO_INFO_LOG("AudioHfpListener::OnScoStateChanged: state: [%{public}d] reason: [%{public}d]", state, reason); + // SCO_DISCONNECTED = 3, SCO_CONNECTING = 4, SCO_DISCONNECTING = 5, SCO_CONNECTED = 6 HfpScoConnectState scoState = static_cast(state); if (scoState == HfpScoConnectState::SCO_CONNECTED || scoState == HfpScoConnectState::SCO_DISCONNECTED) { if (device.GetDeviceAddr() == AudioHfpManager::GetCurrentActiveHfpDevice() && diff --git a/frameworks/native/hdiadapter/sink/bluetooth/bluetooth_renderer_sink.cpp b/frameworks/native/hdiadapter/sink/bluetooth/bluetooth_renderer_sink.cpp index 55f94d65ec..7d196b721a 100644 --- a/frameworks/native/hdiadapter/sink/bluetooth/bluetooth_renderer_sink.cpp +++ b/frameworks/native/hdiadapter/sink/bluetooth/bluetooth_renderer_sink.cpp @@ -70,6 +70,8 @@ const uint32_t PCM_32_BIT = 32; const uint32_t STEREO_CHANNEL_COUNT = 2; constexpr uint32_t BIT_TO_BYTES = 8; constexpr int64_t STAMP_THRESHOLD_MS = 20; +const unsigned int BUFFER_CALC_20MS = 20; +const unsigned int BUFFER_CALC_1000MS = 1000; #ifdef FEATURE_POWER_MANAGER constexpr int32_t RUNNINGLOCK_LOCK_TIMEOUTMS_LASTING = -1; #endif @@ -123,6 +125,8 @@ public: const size_t size) final; int32_t UpdateAppsUid(const std::vector &appsUid) final; + int32_t SetRenderEmpty(int32_t durationUs) final; + explicit BluetoothRendererSinkInner(bool isBluetoothLowLatency = false); ~BluetoothRendererSinkInner(); private: @@ -146,6 +150,9 @@ private: int32_t logMode_ = 0; AudioSampleFormat audioSampleFormat_ = SAMPLE_S16LE; + // for device switch + std::atomic renderEmptyFrameCount_ = 0; + // Low latency int32_t PrepareMmapBuffer(); int32_t GetMmapBufferInfo(int &fd, uint32_t &totalSizeInframe, uint32_t &spanSizeInframe, @@ -498,12 +505,8 @@ int32_t BluetoothRendererSinkInner::RenderFrame(char &data, uint64_t len, uint64 int32_t ret = SUCCESS; CHECK_AND_RETURN_RET_LOG(audioRender_ != nullptr, ERR_INVALID_HANDLE, "Bluetooth Render Handle is nullptr!"); - if (audioMonoState_) { - AdjustStereoToMono(&data, len); - } - if (audioBalanceState_) { - AdjustAudioBalance(&data, len); - } + if (audioMonoState_) { AdjustStereoToMono(&data, len); } + if (audioBalanceState_) { AdjustAudioBalance(&data, len); } CheckLatencySignal(reinterpret_cast(&data), len); DumpFileUtil::WriteDumpFile(dumpFile_, static_cast(&data), len); @@ -514,11 +517,17 @@ int32_t BluetoothRendererSinkInner::RenderFrame(char &data, uint64_t len, uint64 static_cast(&data), len); } CheckUpdateState(&data, len); - if (suspend_) { - return ret; - } + if (suspend_) { return ret; } Trace trace("BluetoothRendererSinkInner::RenderFrame"); + if (renderEmptyFrameCount_ > 0) { + Trace traceEmpty("BluetoothRendererSinkInner::RenderFrame::renderEmpty"); + if (memset_s(reinterpret_cast(&data), static_cast(len), 0, + static_cast(len)) != EOK) { + AUDIO_WARNING_LOG("call memset_s failed"); + } + renderEmptyFrameCount_--; + } while (true) { Trace::CountVolume("BluetoothRendererSinkInner::RenderFrame", static_cast(data)); Trace trace("audioRender_->RenderFrame"); @@ -1137,5 +1146,13 @@ int32_t BluetoothRendererSinkInner::UpdateAppsUid(const std::vector &ap return SUCCESS; } + +int32_t BluetoothRendererSinkInner::SetRenderEmpty(int32_t durationUs) +{ + int32_t emptyCount = durationUs / BUFFER_CALC_1000MS / BUFFER_CALC_20MS; // 1000 us->ms + AUDIO_INFO_LOG("a2dp render %{public}d empty", emptyCount); + CasWithCompare(renderEmptyFrameCount_, emptyCount, std::less()); + return SUCCESS; +} } // namespace AudioStandard } // namespace OHOS diff --git a/frameworks/native/hdiadapter/sink/primary/audio_renderer_sink.cpp b/frameworks/native/hdiadapter/sink/primary/audio_renderer_sink.cpp index a3b8879b84..32103929f2 100644 --- a/frameworks/native/hdiadapter/sink/primary/audio_renderer_sink.cpp +++ b/frameworks/native/hdiadapter/sink/primary/audio_renderer_sink.cpp @@ -230,6 +230,9 @@ private: #endif std::string audioAttrInfo_ = ""; + // for device switch + std::atomic renderEmptyFrameCount_ = 0; + private: int32_t CreateRender(const struct AudioPort &renderPort); int32_t InitAudioManager(); @@ -721,6 +724,15 @@ int32_t AudioRendererSinkInner::RenderFrame(char &data, uint64_t len, uint64_t & } CheckUpdateState(&data, len); + if (renderEmptyFrameCount_ > 0) { + Trace traceEmpty("AudioRendererSinkInner::RenderFrame::renderEmpty"); + if (memset_s(reinterpret_cast(&data), static_cast(len), 0, + static_cast(len)) != EOK) { + AUDIO_WARNING_LOG("call memset_s failed"); + } + renderEmptyFrameCount_--; + } + Trace::CountVolume("AudioRendererSinkInner::RenderFrame", static_cast(data)); CheckLatencySignal(reinterpret_cast(&data), len); @@ -1586,7 +1598,8 @@ int32_t AudioRendererSinkInner::UpdateAppsUid(const std::vector &appsUi int32_t AudioRendererSinkInner::SetRenderEmpty(int32_t durationUs) { int32_t emptyCount = durationUs / 1000 / BUFFER_CALC_20MS; // 1000 us->ms - AUDIO_INFO_LOG("render %{public}d empty", emptyCount); + AUDIO_INFO_LOG("%{public}s render %{public}d empty", halName_.c_str(), emptyCount); + CasWithCompare(renderEmptyFrameCount_, emptyCount, std::less()); return SUCCESS; } } // namespace AudioStandard 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 fc8c3f4eef..385b7219ce 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -456,9 +456,8 @@ public: DistributedRoutingInfo& GetDistributedRoutingRoleInfo(); - void OnScoStateChanged(const std::string &macAddress, bool isConnnected); - - void OnPreferredStateUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand updateCommand); + void OnPreferredStateUpdated(AudioDeviceDescriptor &desc, + const DeviceInfoUpdateCommand updateCommand, AudioStreamDeviceChangeReasonExt &reason); void OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand command); @@ -636,10 +635,6 @@ private: int32_t GetModuleInfo(ClassType classType, std::string &moduleInfoStr); - int32_t HandleFileDevice(DeviceType deviceType); - - int32_t ActivateNormalNewDevice(DeviceType deviceType, bool isSceneActivation); - void MoveToNewOutputDevice(unique_ptr &rendererChangeInfo, vector> &outputDevices, const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN); @@ -653,7 +648,7 @@ private: DeviceRole GetDeviceRole(AudioPin pin) const; - void KeepPortMute(int32_t muteDuration, std::string portName, DeviceType deviceType); + void UnmutePortAfterMuteDuration(int32_t muteDuration, std::string portName, DeviceType deviceType); int32_t ActivateNewDevice(std::string networkId, DeviceType deviceType, bool isRemote); @@ -837,13 +832,20 @@ private: bool NeedRehandleA2DPDevice(unique_ptr &desc); - void MuteSinkPort(DeviceType deviceType, int32_t duration, bool isSync = false); - void MuteSinkPort(const std::string &portName, int32_t duration, bool isSync); void MuteSinkPort(const std::string &oldSinkname, const std::string &newSinkName, AudioStreamDeviceChangeReasonExt reason); + void MuteDefaultSinkPort(); + + void MuteSinkPortForSwtichDevice(unique_ptr& rendererChangeInfo, + vector>& outputDevices, const AudioStreamDeviceChangeReasonExt reason); + + std::string GetSinkName(const DeviceInfo& desc, int32_t sessionId); + + std::string GetSinkName(const AudioDeviceDescriptor& desc, int32_t sessionId); + void RectifyModuleInfo(AudioModuleInfo &moduleInfo, std::list &moduleInfoList, SourceInfo &targetInfo); @@ -1112,6 +1114,7 @@ private: SOURCE_TYPE_REMOTE_CAST }; + static std::map sinkPortStrToClassStrMap_; static std::map formatStrToEnum; static std::map classStrToEnum; static std::map portStrToEnum; diff --git a/services/audio_policy/server/include/service/common/audio_module_info.h b/services/audio_policy/server/include/service/common/audio_module_info.h index ab3904ac41..871557677f 100644 --- a/services/audio_policy/server/include/service/common/audio_module_info.h +++ b/services/audio_policy/server/include/service/common/audio_module_info.h @@ -31,6 +31,7 @@ static const std::string USB_CLASS = "usb"; static const std::string DP_CLASS = "dp"; static const std::string FILE_CLASS = "file_io"; static const std::string REMOTE_CLASS = "remote"; +static const std::string OFFLOAD_CLASS = "offload"; static const std::string INVALID_CLASS = "invalid"; static const std::string BLUETOOTH_SPEAKER = "Bt_Speaker"; static const std::string PRIMARY_SPEAKER = "Speaker"; 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 7503b329f5..8510780323 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 @@ -37,7 +37,6 @@ public: virtual void OnServiceDisconnected(AudioServiceIndex serviceIndex) = 0; 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 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 e23c6bebfb..c9500d57d4 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -61,8 +61,6 @@ static const int64_t SELECT_DEVICE_MUTE_MS = 200000; // 200ms static const int64_t SELECT_OFFLOAD_DEVICE_MUTE_MS = 600000; // 600ms static const int64_t NEW_DEVICE_AVALIABLE_MUTE_MS = 300000; // 300ms static const int64_t NEW_DEVICE_AVALIABLE_OFFLOAD_MUTE_MS = 1000000; // 1s -static const int64_t ARM_USB_DEVICE_MUTE_MS = 40000; // 40ms -static const int64_t DEVICE_TYPE_REMOTE_CAST_MS = 40000; // 40ms static const int64_t SET_BT_ABS_SCENE_DELAY_MS = 120000; // 120ms static const int64_t NEW_DEVICE_REMOTE_CAST_AVALIABLE_MUTE_MS = 300000; // 300ms static const unsigned int BUFFER_CALC_20MS = 20; @@ -118,6 +116,13 @@ std::map AudioPolicyService::portStrToEnum = { {REMOTE_CLASS, TYPE_REMOTE_AUDIO}, }; +std::map AudioPolicyService::sinkPortStrToClassStrMap_ = { + {PRIMARY_SPEAKER, PRIMARY_CLASS}, + {BLUETOOTH_SPEAKER, A2DP_CLASS}, + {USB_SPEAKER, USB_CLASS}, + {DP_SINK, DP_CLASS}, +}; + static const std::string SETTINGS_DATA_BASE_URI = "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true"; static const std::string SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; @@ -1679,6 +1684,7 @@ AudioModuleInfo AudioPolicyService::ConstructRemoteAudioModuleInfo(std::string n int32_t AudioPolicyService::MoveToOutputDevice(uint32_t sessionId, std::string portName) { AUDIO_INFO_LOG("move for session [%{public}d], portName %{public}s", sessionId, portName.c_str()); + MuteSinkPort(portName, SELECT_DEVICE_MUTE_MS, true); std::vector sinkInputIds = FilterSinkInputs(sessionId); // start move. uint32_t sinkId = -1; // invalid sink id, use sink name instead. @@ -2043,6 +2049,40 @@ void AudioPolicyService::UpdateDualToneState(const bool &enable, const int32_t & CHECK_AND_RETURN_LOG(ret == SUCCESS, "Failed to update the dual tone state for sessionId:%{public}d", sessionId); } +std::string AudioPolicyService::GetSinkName(const DeviceInfo& desc, int32_t sessionId) +{ + if (desc.networkId == LOCAL_NETWORK_ID) { + AudioPipeType pipeType = PIPE_TYPE_UNKNOWN; + streamCollector_.GetPipeType(sessionId, pipeType); + return GetSinkPortName(desc.deviceType, pipeType); + } else { + return GetRemoteModuleName(desc.networkId, desc.deviceRole); + } +} + +std::string AudioPolicyService::GetSinkName(const AudioDeviceDescriptor &desc, int32_t sessionId) +{ + if (desc.networkId_ == LOCAL_NETWORK_ID) { + AudioPipeType pipeType = PIPE_TYPE_UNKNOWN; + streamCollector_.GetPipeType(sessionId, pipeType); + return GetSinkPortName(desc.deviceType_, pipeType); + } else { + return GetRemoteModuleName(desc.networkId_, desc.deviceRole_); + } +} + +void AudioPolicyService::MuteSinkPortForSwtichDevice(unique_ptr& rendererChangeInfo, + vector>& outputDevices, const AudioStreamDeviceChangeReasonExt reason) +{ + if (outputDevices.size() != 1) return; + if (outputDevices.front()->isSameDevice(rendererChangeInfo->outputDeviceInfo)) return; + + std::string oldSinkName = GetSinkName(rendererChangeInfo->outputDeviceInfo, rendererChangeInfo->sessionId); + std::string newSinkName = GetSinkName(*outputDevices.front(), rendererChangeInfo->sessionId); + AUDIO_INFO_LOG("mute sink old:[%{public}s] new:[%{public}s]", oldSinkName.c_str(), newSinkName.c_str()); + MuteSinkPort(oldSinkName, newSinkName, reason); +} + void AudioPolicyService::MoveToNewOutputDevice(unique_ptr &rendererChangeInfo, vector> &outputDevices, const AudioStreamDeviceChangeReasonExt reason) { @@ -2059,11 +2099,6 @@ void AudioPolicyService::MoveToNewOutputDevice(unique_ptr(reason)); DeviceType oldDevice = rendererChangeInfo->outputDeviceInfo.deviceType; - DeviceType newDevice = outputDevices.front()->deviceType_; - AudioPipeType pipeType = PIPE_TYPE_UNKNOWN; - streamCollector_.GetPipeType(rendererChangeInfo->sessionId, pipeType); - std::string oldSinkname = GetSinkPortName(oldDevice, pipeType); - std::string newSinkName = GetSinkPortName(newDevice, pipeType); UpdateDeviceInfo(rendererChangeInfo->outputDeviceInfo, new AudioDeviceDescriptor(*outputDevices.front()), true, true); @@ -2071,7 +2106,6 @@ void AudioPolicyService::MoveToNewOutputDevice(unique_ptrSendRendererDeviceChangeEvent(rendererChangeInfo->callerPid, rendererChangeInfo->sessionId, rendererChangeInfo->outputDeviceInfo, reason); - if (outputDevices.size() == 1) {MuteSinkPort(oldSinkname, newSinkName, reason);} } UpdateEffectDefaultSink(outputDevices.front()->deviceType_); @@ -2085,6 +2119,7 @@ void AudioPolicyService::MoveToNewOutputDevice(unique_ptrsessionId, outputDevices.front()->deviceType_); return; } + std::string newSinkName = GetSinkName(*outputDevices.front(), rendererChangeInfo->sessionId); SetVolumeForSwitchDevice(outputDevices.front()->deviceType_, newSinkName); if (isUpdateRouteSupported_ && outputDevices.front()->networkId_ == LOCAL_NETWORK_ID) { @@ -2217,32 +2252,26 @@ bool AudioPolicyService::NeedRehandleA2DPDevice(unique_ptrGetIAudioServerInnerCall()->SetSinkRenderEmpty(PRIMARY_CLASS, duration); - } - audioPolicyManager_.SetSinkMute(portName, true, isSync); - thread switchThread(&AudioPolicyService::KeepPortMute, this, duration, portName, deviceType); - switchThread.detach(); // add another sleep before switch local can avoid pop in some case -} - void AudioPolicyService::MuteSinkPort(const std::string &portName, int32_t duration, bool isSync) { - if (g_adProxy != nullptr) { - std::string identity = IPCSkeleton::ResetCallingIdentity(); - if (portName == PRIMARY_SPEAKER) { - g_adProxy->SetSinkMuteForSwitchDevice("primary", duration, true); - } else if (portName == OFFLOAD_PRIMARY_SPEAKER) { - g_adProxy->SetSinkMuteForSwitchDevice("offload", duration, true); - } - IPCSkeleton::SetCallingIdentity(identity); - } + CHECK_AND_RETURN_LOG(g_adProxy != nullptr, "Audio Server Proxy is null"); - audioPolicyManager_.SetSinkMute(portName, true, isSync); - thread switchThread(&AudioPolicyService::KeepPortMute, this, duration, portName, DEVICE_TYPE_NONE); + std::string identity = IPCSkeleton::ResetCallingIdentity(); + if (sinkPortStrToClassStrMap_.count(portName) > 0) { + g_adProxy->SetSinkMuteForSwitchDevice(sinkPortStrToClassStrMap_.at(portName), duration, true); + // Mute sink, unmute after empty frame is written, so return. + return; + } else if (portName == OFFLOAD_PRIMARY_SPEAKER) { + // Mute offload sink. + g_adProxy->SetSinkMuteForSwitchDevice(OFFLOAD_CLASS, duration, true); + } else { + // Mute by pa. + audioPolicyManager_.SetSinkMute(portName, true, isSync); + } + IPCSkeleton::SetCallingIdentity(identity); + + // Muted and then unmute. + thread switchThread(&AudioPolicyService::UnmutePortAfterMuteDuration, this, duration, portName, DEVICE_TYPE_NONE); switchThread.detach(); } @@ -2256,15 +2285,6 @@ void AudioPolicyService::MuteSinkPort(const std::string &oldSinkname, const std: } MuteSinkPort(newSinkName, SELECT_DEVICE_MUTE_MS, true); MuteSinkPort(oldSinkname, muteTime, true); - - if (oldSinkname == PRIMARY_SPEAKER && - (newSinkName == BLUETOOTH_SPEAKER || (newSinkName == USB_SPEAKER))) { - usleep(ARM_USB_DEVICE_MUTE_MS); // spk->arm_usb or spk->a2dp sleep 40 ms fix pop - } - - if (oldSinkname == REMOTE_CAST_INNER_CAPTURER_SINK_NAME) { - usleep(DEVICE_TYPE_REMOTE_CAST_MS); // remote cast -> spk or other device 40 ms fix pop - } } else if (reason == AudioStreamDeviceChangeReason::NEW_DEVICE_AVAILABLE) { int64_t muteTime = NEW_DEVICE_AVALIABLE_MUTE_MS; if (newSinkName == OFFLOAD_PRIMARY_SPEAKER || oldSinkname == OFFLOAD_PRIMARY_SPEAKER) { @@ -2272,15 +2292,8 @@ void AudioPolicyService::MuteSinkPort(const std::string &oldSinkname, const std: } MuteSinkPort(newSinkName, NEW_DEVICE_AVALIABLE_MUTE_MS, true); MuteSinkPort(oldSinkname, muteTime, true); - - if (oldSinkname == USB_SPEAKER || newSinkName == USB_SPEAKER) { - usleep(ARM_USB_DEVICE_MUTE_MS); // ->arm_usb or arm_usb-> sleep 40 ms fix pop - } } else if (reason.IsOldDeviceUnavaliable() && audioScene_ == AUDIO_SCENE_DEFAULT) { MuteSinkPort(newSinkName, OLD_DEVICE_UNAVALIABLE_MUTE_MS, true); - if (oldSinkname == REMOTE_CAST_INNER_CAPTURER_SINK_NAME) { - usleep(DEVICE_TYPE_REMOTE_CAST_MS); // remote cast -> spk or other device 40 ms fix pop - } } else if (reason == AudioStreamDeviceChangeReason::UNKNOWN && oldSinkname == REMOTE_CAST_INNER_CAPTURER_SINK_NAME) { // remote cast -> earpiece 300ms fix sound leak @@ -2288,6 +2301,14 @@ void AudioPolicyService::MuteSinkPort(const std::string &oldSinkname, const std: } } +void AudioPolicyService::MuteDefaultSinkPort() +{ + if (GetSinkPortName(currentActiveDevice_.deviceType_) != PRIMARY_CLASS) { + // PA may move the sink to default when unloading module. + MuteSinkPort(PRIMARY_SPEAKER, OLD_DEVICE_UNAVALIABLE_MUTE_MS, true); + } +} + int32_t AudioPolicyService::HandleDeviceChangeForFetchOutputDevice(unique_ptr &desc, unique_ptr &rendererChangeInfo) { @@ -2340,6 +2361,7 @@ void AudioPolicyService::FetchOutputDevice(vectorrendererInfo.streamUsage)) { continue; } + MuteSinkPortForSwtichDevice(rendererChangeInfo, descs, reason); std::string encryptMacAddr = GetEncryptAddr(descs.front()->macAddress_); if (descs.front()->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) { if (IsFastFromA2dpToA2dp(descs.front(), rendererChangeInfo, reason)) { continue; } @@ -2852,6 +2874,7 @@ int32_t AudioPolicyService::SwitchActiveA2dpDevice(const sptrKeepPortMute(muteDuration, sinkPortName, deviceType); - }; - std::thread switchThread(switchFirThread); - switchThread.detach(); - int32_t beforSwitchDelay = 300000; // 300 ms - usleep(beforSwitchDelay); - UpdateActiveDeviceRoute(deviceType, DeviceFlag::OUTPUT_DEVICES_FLAG); - if (GetVolumeGroupType(currentActiveDevice_.deviceType_) != GetVolumeGroupType(deviceType)) { - SetVolumeForSwitchDevice(deviceType); - } - isVolumeSwitched = true; - } else { - UpdateActiveDeviceRoute(deviceType, DeviceFlag::INPUT_DEVICES_FLAG); - } - } - if (GetDeviceRole(deviceType) == OUTPUT_DEVICE && !isVolumeSwitched && - GetVolumeGroupType(currentActiveDevice_.deviceType_) != GetVolumeGroupType(deviceType)) { - SetVolumeForSwitchDevice(deviceType); - } - UpdateInputDeviceInfo(deviceType); - return SUCCESS; -} - -void AudioPolicyService::KeepPortMute(int32_t muteDuration, std::string portName, DeviceType deviceType) +void AudioPolicyService::UnmutePortAfterMuteDuration(int32_t muteDuration, std::string portName, DeviceType deviceType) { - Trace trace("AudioPolicyService::KeepPortMute:" + portName + " for " + std::to_string(muteDuration) + "us"); + Trace trace("UnmutePortAfterMuteDuration:" + portName + " for " + std::to_string(muteDuration) + "us"); AUDIO_INFO_LOG("%{public}d us for device type[%{public}s]", muteDuration, portName.c_str()); usleep(muteDuration); if (portName == OFFLOAD_PRIMARY_SPEAKER) { std::string identity = IPCSkeleton::ResetCallingIdentity(); - g_adProxy->SetSinkMuteForSwitchDevice("offload", muteDuration, false); + g_adProxy->SetSinkMuteForSwitchDevice(OFFLOAD_CLASS, muteDuration, false); IPCSkeleton::SetCallingIdentity(identity); + } else { + audioPolicyManager_.SetSinkMute(portName, false); } - - audioPolicyManager_.SetSinkMute(portName, false); } int32_t AudioPolicyService::ActivateNewDevice(std::string networkId, DeviceType deviceType, bool isRemote) @@ -7220,17 +7180,6 @@ void AudioPolicyService::GetAllRunningStreamSession(std::vector &allSes #endif } -void AudioPolicyService::OnScoStateChanged(const std::string &macAddress, bool isConnnected) -{ - std::lock_guard deviceLock(deviceStatusUpdateSharedMutex_); - - AUDIO_INFO_LOG("macAddress: %{public}s, isConnnected: %{public}d", GetEncryptAddr(macAddress).c_str(), - isConnnected); - audioDeviceManager_.UpdateScoState(macAddress, isConnnected); - FetchDevice(true); - FetchDevice(false); -} - void AudioPolicyService::UpdateAllUserSelectDevice(vector> &userSelectDeviceMap, AudioDeviceDescriptor &desc, const sptr &selectDesc) { @@ -7253,14 +7202,13 @@ void AudioPolicyService::UpdateAllUserSelectDevice(vector userSelectMediaRenderDevice = stateManager.GetPreferredMediaRenderDevice(); unique_ptr userSelectCallRenderDevice = stateManager.GetPreferredCallRenderDevice(); unique_ptr userSelectCallCaptureDevice = stateManager.GetPreferredCallCaptureDevice(); unique_ptr userSelectRecordCaptureDevice = stateManager.GetPreferredRecordCaptureDevice(); - AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN; vector> userSelectDeviceMap; userSelectDeviceMap.push_back(make_unique(*userSelectMediaRenderDevice)); userSelectDeviceMap.push_back(make_unique(*userSelectCallRenderDevice)); @@ -7298,7 +7246,6 @@ void AudioPolicyService::OnPreferredStateUpdated(AudioDeviceDescriptor &desc, reason = desc.isEnable_ ? AudioStreamDeviceChangeReason::NEW_DEVICE_AVAILABLE : AudioStreamDeviceChangeReason::OLD_DEVICE_UNAVALIABLE; } - FetchDevice(true, reason); } #ifdef BLUETOOTH_ENABLE @@ -7342,7 +7289,9 @@ void AudioPolicyService::OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const audioDeviceManager_.UpdateDevicesListInfo(audioDescriptor, command); CheckForA2dpSuspend(desc); - OnPreferredStateUpdated(desc, command); + AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN; + OnPreferredStateUpdated(desc, command, reason); + FetchDevice(true, reason); FetchDevice(false); UpdateA2dpOffloadFlagForAllStream(); } diff --git a/services/audio_service/server/src/audio_server.cpp b/services/audio_service/server/src/audio_server.cpp index f17547a587..acb386750c 100644 --- a/services/audio_service/server/src/audio_server.cpp +++ b/services/audio_service/server/src/audio_server.cpp @@ -2062,19 +2062,19 @@ int32_t AudioServer::SetSinkMuteForSwitchDevice(const std::string &devceClass, i int32_t callingUid = IPCSkeleton::GetCallingUid(); CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED, "refused for %{public}d", callingUid); - if (devceClass == "primary") { - if (durationUs <= 0) { - return SUCCESS; - } - IAudioRendererSink *audioRendererSinkInstance = IAudioRendererSink::GetInstance("primary", ""); - CHECK_AND_RETURN_RET_LOG(audioRendererSinkInstance != nullptr, ERROR, "has no valid sink"); - return audioRendererSinkInstance->SetRenderEmpty(durationUs); - } else if (devceClass == "offload") { + + if (durationUs <= 0) { + return SUCCESS; + } + if (devceClass == "offload") { IAudioRendererSink *audioRendererSinkInstance = IAudioRendererSink::GetInstance("offload", ""); CHECK_AND_RETURN_RET_LOG(audioRendererSinkInstance != nullptr, ERROR, "has no valid sink"); return audioRendererSinkInstance->SetSinkMuteForSwitchDevice(mute); } - return SUCCESS; + + IAudioRendererSink *audioRendererSinkInstance = IAudioRendererSink::GetInstance(devceClass.c_str(), ""); + CHECK_AND_RETURN_RET_LOG(audioRendererSinkInstance != nullptr, ERROR, "has no valid sink"); + return audioRendererSinkInstance->SetRenderEmpty(durationUs); } void AudioServer::LoadHdiEffectModel() -- Gitee