From 9145533ebe79e59ff0570cd2a88a164c803fd912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E8=80=80=E6=96=87?= Date: Fri, 29 Aug 2025 14:35:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B8=85=E9=99=A4=E9=80=9A=E8=AF=9D?= =?UTF-8?q?=E5=BC=BA=E9=80=89=E9=9C=80=E6=B1=82=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈耀文 --- .../napi_audio_routing_manager_callbacks.h | 1 + .../napi_audio_routing_manager.cpp | 47 ++++++++++++++++++- .../audiomanager/napi_audio_routing_manager.h | 3 ++ .../include/audio_policy_manager.h | 9 ++-- .../audiocommon/include/audio_device_info.h | 5 ++ .../include/audio_routing_manager.h | 3 +- .../include/audio_system_manager.h | 3 +- .../service/src/audio_policy_manager.cpp | 6 +-- .../src/audio_policy_manager_device.cpp | 16 +++++-- services/audio_policy/idl/IAudioPolicy.idl | 7 +-- .../device/include/audio_device_common.h | 2 +- .../device/include/audio_recovery_device.h | 6 ++- .../device/include/audio_state_manager.h | 1 + .../domain/device/src/audio_device_common.cpp | 12 ++++- .../device/src/audio_recovery_device.cpp | 32 +++++++------ .../domain/device/src/audio_state_manager.cpp | 26 +++++++++- .../domain/router/user_select_router.cpp | 2 +- .../server/domain/router/user_select_router.h | 3 +- .../include/audio_policy_server_handler.h | 6 +-- .../src/audio_policy_server_handler.cpp | 22 +++++---- .../service_main/include/audio_core_service.h | 4 +- .../include/audio_policy_server.h | 7 ++- .../include/audio_policy_service.h | 2 +- .../service_main/src/audio_core_service.cpp | 4 +- .../src/audio_core_service_entry.cpp | 4 +- .../service_main/src/audio_policy_server.cpp | 21 +++++++-- .../service_main/src/audio_policy_service.cpp | 13 +++-- .../audio_policy_server_handler_unit_test.cpp | 4 +- .../client/src/audio_routing_manager.cpp | 19 +++++++- .../client/src/audio_system_manager.cpp | 16 +++++-- 30 files changed, 230 insertions(+), 76 deletions(-) diff --git a/frameworks/js/napi/audiomanager/callback/napi_audio_routing_manager_callbacks.h b/frameworks/js/napi/audiomanager/callback/napi_audio_routing_manager_callbacks.h index c847f17715..46f4b6ac67 100644 --- a/frameworks/js/napi/audiomanager/callback/napi_audio_routing_manager_callbacks.h +++ b/frameworks/js/napi/audiomanager/callback/napi_audio_routing_manager_callbacks.h @@ -26,6 +26,7 @@ namespace AudioStandard { const std::string PREFERRED_OUTPUT_DEVICE_CALLBACK_NAME = "preferredOutputDeviceChangeForRendererInfo"; const std::string PREFER_OUTPUT_DEVICE_CALLBACK_NAME = "preferOutputDeviceChangeForRendererInfo"; +const std::string PREFER_OUTPUT_DEVICE_BY_FILTER_CALLBACK_NAME = "preferredOutputDeviceChangeByFilter"; const std::string PREFERRED_INPUT_DEVICE_CALLBACK_NAME = "preferredInputDeviceChangeForCapturerInfo"; class NapiAudioPreferredOutputDeviceChangeCallback : public AudioPreferredOutputDeviceChangeCallback { diff --git a/frameworks/js/napi/audiomanager/napi_audio_routing_manager.cpp b/frameworks/js/napi/audiomanager/napi_audio_routing_manager.cpp index 28687d797e..f324af57c9 100644 --- a/frameworks/js/napi/audiomanager/napi_audio_routing_manager.cpp +++ b/frameworks/js/napi/audiomanager/napi_audio_routing_manager.cpp @@ -318,6 +318,9 @@ napi_value NapiAudioRoutingManager::SelectOutputDeviceByFilter(napi_env env, nap context->bArgTransFlag, argv[PARAM0]); NapiParamUtils::GetAudioDeviceDescriptorVector(env, context->deviceDescriptors, context->bArgTransFlag, argv[PARAM1]); + if (argc == ARGS_THREE) { + NapiParamUtils::GetValueInt32(env, context->audioDeviceSelectMode, argv[PARAM2]); + } }; context->GetCbInfo(env, info, inputParser); @@ -332,7 +335,7 @@ napi_value NapiAudioRoutingManager::SelectOutputDeviceByFilter(napi_env env, nap context->SignError(NAPI_ERR_UNSUPPORTED); } context->intValue = napiAudioRoutingManager->audioMngr_->SelectOutputDevice(context->audioRendererFilter, - context->deviceDescriptors); + context->deviceDescriptors, context->audioDeviceSelectMode); NAPI_CHECK_ARGS_RETURN_VOID(context, context->intValue == SUCCESS, "SelectOutputDeviceByFilter failed", NAPI_ERR_SYSTEM); }; @@ -984,6 +987,8 @@ napi_value NapiAudioRoutingManager::RegisterCallback(napi_env env, napi_value js } else if (!cbName.compare(PREFERRED_OUTPUT_DEVICE_CALLBACK_NAME) || !cbName.compare(PREFER_OUTPUT_DEVICE_CALLBACK_NAME)) { RegisterPreferredOutputDeviceChangeCallback(env, argc, args, cbName, napiRoutingMgr); + } else if (!cbName.compare(PREFER_OUTPUT_DEVICE_BY_FILTER_CALLBACK_NAME)) { + RegisterPreferredOutputDeviceByFilterChangeCallback(env, argc, args, cbName, napiRoutingMgr); } else if (!cbName.compare(PREFERRED_INPUT_DEVICE_CALLBACK_NAME)) { RegisterPreferredInputDeviceChangeCallback(env, argc, args, cbName, napiRoutingMgr); } else if (!cbName.compare(AVAILABLE_DEVICE_CHANGE_CALLBACK_NAME)) { @@ -1100,6 +1105,43 @@ void NapiAudioRoutingManager::RegisterPreferredOutputDeviceChangeCallback(napi_e AddPreferredOutputDeviceChangeCallback(napiRoutingMgr, cb); } +void NapiAudioRoutingManager::RegisterPreferredOutputDeviceByFilterChangeCallback(napi_env env, size_t argc, + napi_value *args, const std::string &cbName, NapiAudioRoutingManager *napiRoutingMgr) +{ + CHECK_AND_RETURN_RET_LOG(argc == ARGS_THREE, NapiAudioError::ThrowError(env, NAPI_ERR_INPUT_INVALID, + "incorrect number of parameters: expected at least 3 parameters"), "argc invalid"); + + CHECK_AND_RETURN_RET_LOG(NapiParamUtils::CheckArgType(env, args[PARAM2], napi_function), + NapiAudioError::ThrowError(env, NAPI_ERR_INPUT_INVALID, + "incorrect parameter types: The type of callback must be function"), "callback invalid"); + + CHECK_AND_RETURN_LOG(GetNapiPrefOutputDeviceChangeCb(args[PARAM2], napiRoutingMgr) == nullptr, + "Do not allow duplicate registration of the same callback"); + + sptr audioRendererFilter = nullptr; + bool argTransFlag = false; + napi_status status = NapiParamUtils::GetAudioRendererFilter(env, audioRendererFilter, argTransFlag, args[PARAM1]); + CHECK_AND_RETURN_LOG(status == napi_ok, "Parameter verification failed."); + CHECK_AND_RETURN_LOG(audioRendererFilter != nullptr, + "Parameter verification failed. The audioRendererFilter obj is NULL."); + std::shared_ptr cb = + std::make_shared(env); + CHECK_AND_RETURN_LOG(cb != nullptr, "Memory allocation failed!!"); + + cb->SaveCallbackReference(args[PARAM2]); + cb->CreatePreferredOutTsfn(env); + + CHECK_AND_RETURN_LOG(napiRoutingMgr != nullptr, "NapiRoutingMgr is NULL."); + CHECK_AND_RETURN_LOG(napiRoutingMgr->audioRoutingMngr_ != nullptr, + "napiRoutingMgr->audioRoutingMngr_ is NULL."); + int32_t ret = napiRoutingMgr->audioRoutingMngr_->SetPreferredOutputDeviceChangeCallback( + audioRendererFilter->rendererInfo, cb, audioRendererFilter->uid); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, NapiAudioError::ThrowError(env, ret), + "Registering Preferred Output Device By Filter Change Callback Failed %{public}d", ret); + + AddPreferredOutputDeviceChangeCallback(napiRoutingMgr, cb); +} + std::shared_ptr NapiAudioRoutingManager::GetNapiPrefInputDeviceChangeCb( napi_value args, NapiAudioRoutingManager *napiRoutingMgr) { @@ -1278,7 +1320,8 @@ napi_value NapiAudioRoutingManager::UnregisterCallback(napi_env env, napi_value if (!callbackName.compare(DEVICE_CHANGE_CALLBACK_NAME)) { UnregisterDeviceChangeCallback(env, callback, napiRoutingMgr); } else if (!callbackName.compare(PREFERRED_OUTPUT_DEVICE_CALLBACK_NAME) || - !callbackName.compare(PREFER_OUTPUT_DEVICE_CALLBACK_NAME)) { + !callbackName.compare(PREFER_OUTPUT_DEVICE_CALLBACK_NAME) || + !callbackName.compare(PREFER_OUTPUT_DEVICE_BY_FILTER_CALLBACK_NAME)) { UnregisterPreferredOutputDeviceChangeCallback(env, callback, napiRoutingMgr); } else if (!callbackName.compare(PREFERRED_INPUT_DEVICE_CALLBACK_NAME)) { UnregisterPreferredInputDeviceChangeCallback(env, callback, napiRoutingMgr); diff --git a/frameworks/js/napi/audiomanager/napi_audio_routing_manager.h b/frameworks/js/napi/audiomanager/napi_audio_routing_manager.h index 085019c4a1..a95ea1a9c1 100644 --- a/frameworks/js/napi/audiomanager/napi_audio_routing_manager.h +++ b/frameworks/js/napi/audiomanager/napi_audio_routing_manager.h @@ -40,6 +40,7 @@ private: int32_t deviceFlag; int32_t deviceType; int32_t intValue; + int32_t audioDeviceSelectMode = 0; bool isActive; bool isTrue; bool bArgTransFlag = true; @@ -88,6 +89,8 @@ private: NapiAudioRoutingManager *napiRoutingMgr); static void RegisterPreferredOutputDeviceChangeCallback(napi_env env, size_t argc, napi_value *args, const std::string &cbName, NapiAudioRoutingManager *napiRoutingMgr); + static void RegisterPreferredOutputDeviceByFilterChangeCallback(napi_env env, size_t argc, napi_value *args, + const std::string &cbName, NapiAudioRoutingManager *napiRoutingMgr); static void RegisterPreferredInputDeviceChangeCallback(napi_env env, size_t argc, napi_value *args, const std::string &cbName, NapiAudioRoutingManager *napiRoutingMgr); static void RegisterAvaiableDeviceChangeCallback(napi_env env, size_t argc, napi_value *args, diff --git a/frameworks/native/audiopolicy/include/audio_policy_manager.h b/frameworks/native/audiopolicy/include/audio_policy_manager.h index 7eaba530e8..96850da52c 100644 --- a/frameworks/native/audiopolicy/include/audio_policy_manager.h +++ b/frameworks/native/audiopolicy/include/audio_policy_manager.h @@ -109,7 +109,10 @@ public: bool IsFastRecordingSupported(AudioStreamInfo &streamInfo, SourceType source); int32_t SelectOutputDevice(sptr audioRendererFilter, - std::vector> audioDeviceDescriptors); + std::vector> audioDeviceDescriptors, + const int32_t audioDeviceSelectMode = 0); + + int32_t RestoreOutputDevice(sptr audioRendererFilter); std::string GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType); @@ -346,7 +349,7 @@ public: AudioCapturerInfo &captureInfo); int32_t SetPreferredOutputDeviceChangeCallback(const AudioRendererInfo &rendererInfo, - const std::shared_ptr &callback); + const std::shared_ptr &callback, const int32_t uid = -1); int32_t SetPreferredInputDeviceChangeCallback(const AudioCapturerInfo &capturerInfo, const std::shared_ptr &callback); @@ -710,7 +713,7 @@ private: int32_t RegisterPolicyCallbackClientFunc(const sptr &gsp); int32_t SetClientCallbacksEnable(const CallbackChange &callbackchange, const bool &enable, bool block = true); int32_t SetCallbackStreamInfo(const CallbackChange &callbackChange); - int32_t SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo); + int32_t SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo, const int32_t uid = -1); int32_t SetCallbackCapturerInfo(const AudioCapturerInfo &capturerInfo); std::mutex listenerStubMutex_; 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 6486fcb4b0..7a01527b5f 100644 --- a/interfaces/inner_api/native/audiocommon/include/audio_device_info.h +++ b/interfaces/inner_api/native/audiocommon/include/audio_device_info.h @@ -235,6 +235,11 @@ inline bool IsInputDevice(DeviceType deviceType, DeviceRole deviceRole = DEVICE_ } } +enum AudioDeviceSelectMode { + SELECT_STRATEGY_DEFAULT = 0, + SELECT_STRATEGY_INDEPENDENT = 1, +}; + enum DmDeviceType { DM_DEVICE_TYPE_DEFAULT = 0, DM_DEVICE_TYPE_PENCIL = 0xA07, diff --git a/interfaces/inner_api/native/audiomanager/include/audio_routing_manager.h b/interfaces/inner_api/native/audiomanager/include/audio_routing_manager.h index 5dd388ab65..a93b249ee0 100644 --- a/interfaces/inner_api/native/audiomanager/include/audio_routing_manager.h +++ b/interfaces/inner_api/native/audiomanager/include/audio_routing_manager.h @@ -40,7 +40,7 @@ public: int32_t GetPreferredOutputDeviceForRendererInfo(AudioRendererInfo rendererInfo, std::vector> &desc); int32_t SetPreferredOutputDeviceChangeCallback(AudioRendererInfo rendererInfo, - const std::shared_ptr &callback); + const std::shared_ptr &callback, const int32_t uid = -1); int32_t UnsetPreferredOutputDeviceChangeCallback( const std::shared_ptr &callback = nullptr); int32_t GetPreferredInputDeviceForCapturerInfo(AudioCapturerInfo captureInfo, @@ -56,6 +56,7 @@ public: int32_t UnsetAudioDeviceRefinerCallback(); int32_t SetPreferredDevice(const PreferredType preferredType, const std::shared_ptr &desc, const int32_t uid = INVALID_UID); + int32_t RestoreOutputDevice(sptr audioRendererFilter); int32_t SetDeviceVolumeBehavior(const std::string &networkId, DeviceType deviceType, VolumeBehavior volumeBehavior); int32_t SetDeviceConnectionStatus(const std::shared_ptr &desc, const bool isConnected); private: diff --git a/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h b/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h index 678d9a3851..6c819a5708 100644 --- a/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h +++ b/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h @@ -371,7 +371,8 @@ public: * @since 9 */ int32_t SelectOutputDevice(sptr audioRendererFilter, - std::vector> audioDeviceDescriptors) const; + std::vector> audioDeviceDescriptors, + const int32_t audioDeviceSelectMode = 0) const; /** * @brief Select the audio input device according to the filter conditions. diff --git a/services/audio_policy/client/service/src/audio_policy_manager.cpp b/services/audio_policy/client/service/src/audio_policy_manager.cpp index b248b63264..d279daeea2 100644 --- a/services/audio_policy/client/service/src/audio_policy_manager.cpp +++ b/services/audio_policy/client/service/src/audio_policy_manager.cpp @@ -213,7 +213,7 @@ int32_t AudioPolicyManager::SetCallbackStreamInfo(const CallbackChange &callback int32_t ret = SUCCESS; if (callbackChange == CALLBACK_PREFERRED_OUTPUT_DEVICE_CHANGE) { for (auto &rendererInfo : rendererInfos_) { - ret = gsp->SetCallbackRendererInfo(rendererInfo); + ret = gsp->SetCallbackRendererInfo(rendererInfo, -1); } } else if (callbackChange == CALLBACK_PREFERRED_INPUT_DEVICE_CHANGE) { for (auto &capturerInfo : capturerInfos_) { @@ -639,11 +639,11 @@ int32_t AudioPolicyManager::SetClientCallbacksEnable(const CallbackChange &callb return gsp->SetClientCallbacksEnable(callbackchange, enable); } -int32_t AudioPolicyManager::SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo) +int32_t AudioPolicyManager::SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo, const int32_t uid) { const sptr gsp = GetAudioPolicyManagerProxy(); CHECK_AND_RETURN_RET_LOG(gsp != nullptr, -1, "audio policy manager proxy is NULL."); - return gsp->SetCallbackRendererInfo(rendererInfo); + return gsp->SetCallbackRendererInfo(rendererInfo, uid); } int32_t AudioPolicyManager::SetCallbackCapturerInfo(const AudioCapturerInfo &capturerInfo) diff --git a/services/audio_policy/client/service/src/audio_policy_manager_device.cpp b/services/audio_policy/client/service/src/audio_policy_manager_device.cpp index 93059dc0b2..fd9b29cda4 100644 --- a/services/audio_policy/client/service/src/audio_policy_manager_device.cpp +++ b/services/audio_policy/client/service/src/audio_policy_manager_device.cpp @@ -31,7 +31,7 @@ using namespace std; const unsigned int TIME_OUT_SECONDS = 10; int32_t AudioPolicyManager::SelectOutputDevice(sptr audioRendererFilter, - std::vector> audioDeviceDescriptors) + std::vector> audioDeviceDescriptors, const int32_t audioDeviceSelectMode) { const sptr gsp = GetAudioPolicyManagerProxy(); CHECK_AND_RETURN_RET_LOG(gsp != nullptr, -1, "audio policy manager proxy is NULL."); @@ -43,7 +43,15 @@ int32_t AudioPolicyManager::SelectOutputDevice(sptr audioRe return -1; } - return gsp->SelectOutputDevice(audioRendererFilter, audioDeviceDescriptors); + return gsp->SelectOutputDevice(audioRendererFilter, audioDeviceDescriptors, audioDeviceSelectMode); +} + +int32_t AudioPolicyManager::RestoreOutputDevice(sptr audioRendererFilter) +{ + const sptr gsp = GetAudioPolicyManagerProxy(); + CHECK_AND_RETURN_RET_LOG(gsp != nullptr, -1, "audio policy manager proxy is NULL."); + + return gsp->SelectOutputDevice(audioRendererFilter); } std::string AudioPolicyManager::GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType) @@ -297,7 +305,7 @@ int32_t AudioPolicyManager::UnsetDeviceChangeCallback(const int32_t clientId, De } int32_t AudioPolicyManager::SetPreferredOutputDeviceChangeCallback(const AudioRendererInfo &rendererInfo, - const std::shared_ptr &callback) + const std::shared_ptr &callback, const int32_t uid) { AUDIO_DEBUG_LOG("AudioPolicyManager::SetPreferredOutputDeviceChangeCallback"); CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_INVALID_PARAM, "callback is nullptr"); @@ -315,7 +323,7 @@ int32_t AudioPolicyManager::SetPreferredOutputDeviceChangeCallback(const AudioRe if (audioPolicyClientStubCB_ != nullptr) { audioPolicyClientStubCB_->AddPreferredOutputDeviceChangeCallback(rendererInfo, callback); rendererInfos_.push_back(rendererInfo); - SetCallbackRendererInfo(rendererInfo); + SetCallbackRendererInfo(rendererInfo, uid); size_t callbackSize = audioPolicyClientStubCB_->GetPreferredOutputDeviceChangeCallbackSize(); if (callbackSize == 1) { callbackChangeInfos_[CALLBACK_PREFERRED_OUTPUT_DEVICE_CHANGE].isEnable = true; diff --git a/services/audio_policy/idl/IAudioPolicy.idl b/services/audio_policy/idl/IAudioPolicy.idl index 99bcc26400..5a1c7362f1 100644 --- a/services/audio_policy/idl/IAudioPolicy.idl +++ b/services/audio_policy/idl/IAudioPolicy.idl @@ -66,7 +66,7 @@ interface IAudioPolicy { [out] int volumeLevel); void GetAppVolumeLevel([in] int appUid, [out] int volumeLevel); void GetSelfAppVolumeLevel([out] int volumeLevel); - void SetStreamMuteLegacy([in] int volumeType, [in] boolean mute, [in] int deviceType); /*deviceType default DEVICE_TYPE_NONE, AudioVolumeType volumeType*/ + void SetStreamMuteLegacy([in] int volumeType, [in] boolean mute, [in] int deviceType); /*deviceType default DEVICE_TYPE_NONE, AudioVolumeType volumeType*/ void SetStreamMute([in] int volumeType, [in] boolean mute, [in] int deviceType); /*deviceType default DEVICE_TYPE_NONE*/ void GetStreamMute([in] int volumeType, [out] boolean retMute); void IsStreamActive([in] int volumeType, [out] boolean retActive); @@ -106,7 +106,7 @@ interface IAudioPolicy { [out] AudioInterrupt audioInterrupt, [in] int zoneId /* zoneID default value: 0 -- local device */); void GetDevices([in] int deviceFlag, [out] List> retDevicesList); - void SelectOutputDevice([in] sptr audioRendererFilter, [in] List> audioDeviceDescriptors); + void SelectOutputDevice([in] sptr audioRendererFilter, [in] List> audioDeviceDescriptors, [in] int audioDeviceSelectMode); void GetSelectedDeviceInfo([in] int uid, [in] int pid, [in] int streamType, [out] String retStr); void SelectInputDevice([in] sptr audioCapturerFilter, [in] List> audioDeviceDescriptors); void SelectInputDevice([in] sharedptr audioDeviceDescriptor); @@ -246,7 +246,7 @@ interface IAudioPolicy { void IsAllowedPlayback([in] int uid, [in] int pid, [out] boolean isAllowed); void SetVoiceRingtoneMute([in] boolean isMute); void SetCallbackRendererInfo([in] AudioRendererInfo rendererInfo); - void SetCallbackCapturerInfo([in] AudioCapturerInfo capturerInfo); + void SetCallbackCapturerInfo([in] AudioCapturerInfo capturerInfo, [in] int uid); void GetStreamInFocusByUid([in] int uid, [in] int zoneId, [out] int retStreamType); /* zoneID default value: 0 -- local device */ void SetPreferredDevice([in] int preferredType, [in] sharedptr desc, [in] int uid); void SetDeviceVolumeBehavior([in] String networkId, [in] int deviceType, [in] VolumeBehavior volumeBehavior); @@ -303,5 +303,6 @@ interface IAudioPolicy { void GetAudioZoneByName([in] String name, [out] int zoneId); void AddStreamsToAudioZone([in] int zoneId, [in] List streams); void RemoveStreamsFromAudioZone([in] int zoneId, [in] List streams); + void RestoreOutputDevice([in] sptr audioRendererFilter); } diff --git a/services/audio_policy/server/domain/device/include/audio_device_common.h b/services/audio_policy/server/domain/device/include/audio_device_common.h index 67ecbb72be..9bc2d02016 100644 --- a/services/audio_policy/server/domain/device/include/audio_device_common.h +++ b/services/audio_policy/server/domain/device/include/audio_device_common.h @@ -65,7 +65,7 @@ public: const AudioStreamDeviceChangeReason reason); void OnPreferredInputDeviceUpdated(DeviceType deviceType, std::string networkId); std::vector> GetPreferredOutputDeviceDescInner( - AudioRendererInfo &rendererInfo, std::string networkId = LOCAL_NETWORK_ID); + AudioRendererInfo &rendererInfo, std::string networkId = LOCAL_NETWORK_ID, const int32_t uid = -1); std::vector> GetPreferredInputDeviceDescInner( AudioCapturerInfo &captureInfo, std::string networkId = LOCAL_NETWORK_ID); int32_t GetPreferredOutputStreamTypeInner(StreamUsage streamUsage, DeviceType deviceType, int32_t flags, diff --git a/services/audio_policy/server/domain/device/include/audio_recovery_device.h b/services/audio_policy/server/domain/device/include/audio_recovery_device.h index 2e298fe93c..452f557794 100644 --- a/services/audio_policy/server/domain/device/include/audio_recovery_device.h +++ b/services/audio_policy/server/domain/device/include/audio_recovery_device.h @@ -53,7 +53,8 @@ public: void RecoverExcludedOutputDevices(); int32_t SelectOutputDevice(sptr audioRendererFilter, - std::vector> audioDeviceDescriptors); + std::vector> audioDeviceDescriptors, + const int32_t audioDeviceSelectMode = 0); int32_t SelectInputDevice(sptr audioCapturerFilter, std::vector> audioDeviceDescriptors); int32_t ExcludeOutputDevices(AudioDeviceUsage audioDevUsage, @@ -82,7 +83,8 @@ private: // selectoutputdevice int32_t SelectOutputDeviceForFastInner(sptr audioRendererFilter, std::vector> selectedDesc); - int32_t SetRenderDeviceForUsage(StreamUsage streamUsage, std::shared_ptr desc); + int32_t SetRenderDeviceForUsage(StreamUsage streamUsage, std::shared_ptr desc, + const int32_t uid = -1); int32_t ConnectVirtualDevice(std::shared_ptr &desc); void HandleFetchDeviceChange(const AudioStreamDeviceChangeReason &reason, const std::string &caller); void WriteSelectOutputSysEvents(const std::vector> &selectedDesc, diff --git a/services/audio_policy/server/domain/device/include/audio_state_manager.h b/services/audio_policy/server/domain/device/include/audio_state_manager.h index e44ad7e105..d73363b417 100644 --- a/services/audio_policy/server/domain/device/include/audio_state_manager.h +++ b/services/audio_policy/server/domain/device/include/audio_state_manager.h @@ -63,6 +63,7 @@ public: // Get call render device selected by the user shared_ptr GetPreferredCallRenderDevice(); + shared_ptr GetPreferredCallRenderDeviceForUid(const int32_t clientUid); // Get call capture device selected by the user shared_ptr GetPreferredCallCaptureDevice(); diff --git a/services/audio_policy/server/domain/device/src/audio_device_common.cpp b/services/audio_policy/server/domain/device/src/audio_device_common.cpp index 8454c811ad..e8d6eb1ef4 100644 --- a/services/audio_policy/server/domain/device/src/audio_device_common.cpp +++ b/services/audio_policy/server/domain/device/src/audio_device_common.cpp @@ -156,7 +156,7 @@ void AudioDeviceCommon::OnPreferredInputDeviceUpdated(DeviceType deviceType, std } std::vector> AudioDeviceCommon::GetPreferredOutputDeviceDescInner( - AudioRendererInfo &rendererInfo, std::string networkId) + AudioRendererInfo &rendererInfo, std::string networkId, const int32_t uid) { std::vector> deviceList = {}; if (rendererInfo.streamUsage <= STREAM_USAGE_UNKNOWN || @@ -168,6 +168,16 @@ std::vector> AudioDeviceCommon::GetPrefer return deviceList; } if (networkId == LOCAL_NETWORK_ID) { + auto preferredType = AudioPolicyUtils::GetInstance().GetPreferredTypeByStreamUsage(rendererInfo.streamUsage); + if (preferredType == AUDIO_CALL_RENDER) { + shared_ptr preferredDevice = + AudioStateManager::GetAudioStateManager().GetPreferredCallRenderDeviceForUid(uid); + CHECK_AND_RETURN_RET_LOG(preferredDevice != nullptr, deviceList, "preferredDevice is nullptr."); + if (preferredDevice->deviceId_ != 0) { + deviceList.push_back(preferredDevice); + return deviceList; + } + } vector> descs = audioRouterCenter_.FetchOutputDevices(rendererInfo.streamUsage, -1, "GetPreferredOutputDeviceDescInner"); diff --git a/services/audio_policy/server/domain/device/src/audio_recovery_device.cpp b/services/audio_policy/server/domain/device/src/audio_recovery_device.cpp index 4d697ebc54..9362ddcdc3 100644 --- a/services/audio_policy/server/domain/device/src/audio_recovery_device.cpp +++ b/services/audio_policy/server/domain/device/src/audio_recovery_device.cpp @@ -179,12 +179,14 @@ void AudioRecoveryDevice::SetDeviceEnableAndUsage(const std::shared_ptr audioRendererFilter, - std::vector> selectedDesc) + std::vector> selectedDesc, const int32_t audioDeviceSelectMode) { - AUDIO_WARNING_LOG("[ADeviceEvent] uid[%{public}d] type[%{public}d] islocal [%{public}d] mac[%{public}s] " - "streamUsage[%{public}d] callerUid[%{public}d]", audioRendererFilter->uid, selectedDesc[0]->deviceType_, - selectedDesc[0]->networkId_ == LOCAL_NETWORK_ID, GetEncryptAddr(selectedDesc[0]->macAddress_).c_str(), - audioRendererFilter->rendererInfo.streamUsage, IPCSkeleton::GetCallingUid()); + CHECK_AND_RETURN_RET_LOG(audioRendererFilter != nullptr, ERR_INVALID_PARAM, "audioRendererFilter is nullptr"); + AUDIO_WARNING_LOG("[ADeviceEvent] uid[%{public}d] type[%{public}d] islocal [%{public}d] mac[%{public}s] " \ + "streamUsage[%{public}d] callerUid[%{public}d] audioDeviceSelectMode[%{public}d]", + audioRendererFilter->uid, selectedDesc[0]->deviceType_, selectedDesc[0]->networkId_ == LOCAL_NETWORK_ID, + GetEncryptAddr(selectedDesc[0]->macAddress_).c_str(), audioRendererFilter->rendererInfo.streamUsage, + IPCSkeleton::GetCallingUid(), audioDeviceSelectMode); CHECK_AND_RETURN_RET_LOG(selectedDesc.size() == 1 && selectedDesc[0] && selectedDesc[0]->deviceRole_ == DeviceRole::OUTPUT_DEVICE, ERR_INVALID_OPERATION, "DeviceCheck no success"); @@ -204,7 +206,10 @@ int32_t AudioRecoveryDevice::SelectOutputDevice(sptr audioR SetDeviceEnableAndUsage(selectedDesc[0]); - if (audioRendererFilter->uid != -1) { + if (selectedDesc[0]->deviceType_ == DEVICE_TYPE_NONE && audioRendererFilter->uid >= 0) { + audioAffinityManager_.DelSelectRendererDevice(audioRendererFilter->uid); + } + if (audioDeviceSelectMode == SELECT_STRATEGY_INDEPENDENT && audioRendererFilter->uid >= 0) { return SelectOutputDeviceByFilterInner(audioRendererFilter, selectedDesc); } if (audioRendererFilter->rendererInfo.rendererFlags == STREAM_FLAG_FAST) { @@ -214,7 +219,7 @@ int32_t AudioRecoveryDevice::SelectOutputDevice(sptr audioR if (selectedDesc[0]->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO) { AudioPolicyUtils::GetInstance().ClearScoDeviceSuspendState(selectedDesc[0]->macAddress_); } - SetRenderDeviceForUsage(strUsage, selectedDesc[0]); + SetRenderDeviceForUsage(strUsage, selectedDesc[0], audioRendererFilter->uid); CheckAndWriteDeviceChangeExceptionEvent(res == SUCCESS, AudioStreamDeviceChangeReason::OVERRODE, selectedDesc[0]->deviceType_, selectedDesc[0]->deviceRole_, res, "SetRenderDeviceForUsage fail"); CHECK_AND_RETURN_RET_LOG(res == SUCCESS, res, "SetRenderDeviceForUsage fail"); @@ -224,9 +229,7 @@ int32_t AudioRecoveryDevice::SelectOutputDevice(sptr audioR int32_t ret = ConnectVirtualDevice(selectedDesc[0]); CheckAndWriteDeviceChangeExceptionEvent(ret == SUCCESS, AudioStreamDeviceChangeReason::OVERRODE, selectedDesc[0]->deviceType_, selectedDesc[0]->deviceRole_, ret, "Connect virtual device fail"); - CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Connect device [%{public}s] failed", - GetEncryptStr(selectedDesc[0]->macAddress_).c_str()); - return SUCCESS; + return ret; } audioActiveDevice_.NotifyUserSelectionEventToBt(selectedDesc[0], strUsage); @@ -276,7 +279,7 @@ int32_t AudioRecoveryDevice::SelectOutputDeviceForFastInner(sptr desc) + std::shared_ptr desc, const int32_t uid) { // get deviceUsage and preferredType auto deviceUsage = AudioPolicyUtils::GetInstance().GetAudioDeviceUsageByStreamUsage(streamUsage); @@ -291,15 +294,16 @@ int32_t AudioRecoveryDevice::SetRenderDeviceForUsage(StreamUsage streamUsage, (desc->networkId_ == device->networkId_) && (!IsUsb(desc->deviceType_) || desc->deviceRole_ == device->deviceRole_); }); - CHECK_AND_RETURN_RET_LOG(itr != devices.end(), ERR_INVALID_OPERATION, + CHECK_AND_RETURN_RET_LOG(itr != devices.end() || desc->deviceType_ == DEVICE_TYPE_NONE, ERR_INVALID_OPERATION, "device not available type:%{public}d macAddress:%{public}s id:%{public}d networkId:%{public}s", desc->deviceType_, GetEncryptAddr(desc->macAddress_).c_str(), tempId, GetEncryptStr(desc->networkId_).c_str()); // set preferred device - std::shared_ptr descriptor = std::make_shared(**itr); + std::shared_ptr descriptor = (desc->deviceType_ == DEVICE_TYPE_NONE ? + desc : std::make_shared(**itr)); CHECK_AND_RETURN_RET_LOG(descriptor != nullptr, ERR_INVALID_OPERATION, "Create device descriptor failed"); - auto callerUid = IPCSkeleton::GetCallingUid(); + auto callerUid = uid == -1 ? IPCSkeleton::GetCallingUid() : uid; if (preferredType == AUDIO_CALL_RENDER) { AudioPolicyUtils::GetInstance().SetPreferredDevice(preferredType, descriptor, callerUid, "SelectOutputDevice"); } else { diff --git a/services/audio_policy/server/domain/device/src/audio_state_manager.cpp b/services/audio_policy/server/domain/device/src/audio_state_manager.cpp index 3a46f9c066..0b622f6c9b 100644 --- a/services/audio_policy/server/domain/device/src/audio_state_manager.cpp +++ b/services/audio_policy/server/domain/device/src/audio_state_manager.cpp @@ -73,9 +73,11 @@ void AudioStateManager::SetPreferredCallRenderDevice(const std::shared_ptr