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 c847f17715707c58db3611a7ba06113e97af4c00..46f4b6ac67ec4d3db2495d4ed4bed150861190bd 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 28687d797e425b6744970014f31fdc357577e2a2..77b013b57bc37c4d6c0510c24447106910492716 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"); + + aptr 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. Your AudioRendererFilter is NULL."); + CHECK_AND_RETURN_RET_LOG(audioRendererFilter->rendererInfo.streamUsage != StreamUsage::STREAM_USAGE_INVALID, + NapiAudioError::ThrowError(env, NAPI_ERR_INVALID_PARAM, + "Parameter verification failed. Your usage in AudioRendererInfo is invalid."), "invalid streamUsage"); + std::shared_ptr cb = + std::make_shared(env); + CHECK_AND_RETURN_LOG(cb != nullptr, "Memory allocation failed!!"); + + cb->SaveCallbackReference(args[PARAM2]); + cb->CreatePreferredOutTsfn(env); + + 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 085019c4a17027cda60c542bb41ec24baefa6a53..a95ea1a9c1a6a55e7fe4441d8cca8c2da01966fd 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 e7ed0c67730d907be41eef9c354c8eca5d86f9dd..cf355b3964e2826dd665f06e9fa70b200a31d40a 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); @@ -344,7 +347,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); @@ -703,7 +706,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 6486fcb4b01714979c4c710ca8651c6be3622a96..7a01527b5f4ca73fe2a1e3f9883835f9f8354b9f 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 5dd388ab651078d3e505bbfb098187ef597eda69..931f3746512253516d39ad34f86b9081663d094f 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, 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 344265be60d6a60e219a69a6bd33e19cf65c8f2e..d755e1385379040d340bbfbc1ab499c346530c1e 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,18 @@ public: * @since 9 */ int32_t SelectOutputDevice(sptr audioRendererFilter, - std::vector> audioDeviceDescriptors) const; + std::vector> audioDeviceDescriptors, + const int32_t audioDeviceSelectMode = 0) const; + + /** + * @brief Restore the audio output device according to the filter conditions. + * + * @param audioRendererFilter filter conditions. + * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined + * in {@link audio_errors.h} otherwise. + * @since 21 + */ + int32_t RestoreOutputDevice(sptr audioRendererFilter) 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 ac9bdab7e62db8e187cb645fbb14a8d209b86949..a59d159a62f2bfb8b5baf1dbce56515f93469a04 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 09dcfb30f0621e10b5e557288ffa778dadc0c573..70225ed8f96b756121bec78c58604fd4ffc685ca 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) @@ -288,7 +296,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"); @@ -306,7 +314,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 62b3c23018eb9ba89cac21d24f5bc75efa4539c4..5cb39d2a8420ac56b00fa91bf81d6be839dcd196 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 GetPreferredOutputStreamType([in] AudioRendererInfo rendererInfo, [out] int streamType); @@ -243,7 +243,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); @@ -299,5 +299,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 67ecbb72becd05ecc0d6d1e1757f9cea97a53f55..9bc2d02016d3010cfbb1e79a5b6fd1089b2f1aa3 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 2e298fe93cbd5a3810ccea580499d4e66ae90809..452f5577941594c1f21cf1516cfe6c245ea02abd 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 e44ad7e105bd79568353f2ee6c9da29229eaced4..d73363b4172b2af1e0acb38ef7785e50d99cde82 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 8454c811ada35840c2867c4011f8c2d8abdc04a2..01e79484528aa11953f7fe0cb0fc378498343312 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,15 @@ 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 preferredDev_ = + AudioStateManager::GetAudioStateManager().GetPreferredCallRenderDeviceForUid(uid); + if (preferredDev_->deviceId_ != 0) { + deviceList.push_back(preferredDev_); + 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 4d697ebc5478fc346dffa2df01a48c84007ee6d2..b17e030c8c7f7fa9c52765de6846f3a051b0be8a 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,13 @@ 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()); + 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 +205,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 +218,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,8 +228,6 @@ 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; } @@ -276,7 +278,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 +293,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 3a46f9c06664ebfa7b08010250dcf1b2191a25bf..b864a05ce920f34434326fb053a84131ba2a64da 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 @@ -187,6 +187,20 @@ shared_ptr AudioStateManager::GetPreferredCallRenderDevic return std::make_shared(); } +shared_ptr AudioStateManager::GetPreferredCallRenderDeviceForUid(const int32_t clientUid) +{ + std::lock_guard lock(mutex_); + if (clientUid >= 0 && !forcedDeviceMapList_.empty()) { + for (auto it = forcedDeviceMapList_.begin(); it != forcedDeviceMapList_.end(); ++it) { + if (clientUid == it->begin()->first) { + AUDIO_INFO_LOG("deviceType: %{public}d, clientUid: %{public}d", it->begin()->second->deviceType_, clientUid); + return make_shared(std::move(it->begin()->second)); + } + } + } + return std::make_shared(); +} + shared_ptr AudioStateManager::GetPreferredCallCaptureDevice() { std::lock_guard lock(mutex_); diff --git a/services/audio_policy/server/infra/async_action_handler/include/audio_policy_server_handler.h b/services/audio_policy/server/infra/async_action_handler/include/audio_policy_server_handler.h index 1a94a5eb4039d55acde2f40e7f100f673cf033cb..6582ef0737cabbfbacc2c7675cf867d0c5ffb6a8 100644 --- a/services/audio_policy/server/infra/async_action_handler/include/audio_policy_server_handler.h +++ b/services/audio_policy/server/infra/async_action_handler/include/audio_policy_server_handler.h @@ -199,7 +199,7 @@ public: bool SendHeadTrackingEnabledChangeForAnyDeviceEvent( const std::shared_ptr &selectedAudioDevice, const bool &enabled); int32_t SetClientCallbacksEnable(const CallbackChange &callbackchange, const bool &enable); - int32_t SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo); + int32_t SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo, const int32_t uid = -1); int32_t SetCallbackCapturerInfo(const AudioCapturerInfo &capturerInfo); bool SendAudioSceneChangeEvent(const AudioScene &audioScene); bool SendAudioSessionDeactiveCallback(const std::pair &sessionDeactivePair); @@ -263,7 +263,7 @@ private: void HandleVolumeKeyEventToRssWhenAccountsChange(std::shared_ptr &eventContextObj); - std::vector GetCallbackRendererInfoList(int32_t clientPid); + std::vector GetCallbackRendererInfoList(int32_t clientPid); std::vector GetCallbackCapturerInfoList(int32_t clientPid); std::mutex runnerMutex_; @@ -283,7 +283,7 @@ private: std::unordered_map> distributedRoutingRoleChangeCbsMap_; std::unordered_map> clientCallbacksMap_; int32_t pidOfRss_ = -1; - std::unordered_map> clientCbRendererInfoMap_; + std::unordered_map> clientCbRendererInfoMap_; std::unordered_map> clientCbCapturerInfoMap_; std::unordered_map> clientCbStreamUsageMap_; }; diff --git a/services/audio_policy/server/infra/async_action_handler/src/audio_policy_server_handler.cpp b/services/audio_policy/server/infra/async_action_handler/src/audio_policy_server_handler.cpp index a188951ab3398e5d47791c8cffee26ffd0073c92..fceec3502b2b00f946a53c3202febde7438addcd 100644 --- a/services/audio_policy/server/infra/async_action_handler/src/audio_policy_server_handler.cpp +++ b/services/audio_policy/server/infra/async_action_handler/src/audio_policy_server_handler.cpp @@ -1084,10 +1084,10 @@ void AudioPolicyServerHandler::HandlePreferredOutputDeviceUpdated() std::lock_guard lock(handleMapMutex_); for (auto it = audioPolicyClientProxyAPSCbsMap_.begin(); it != audioPolicyClientProxyAPSCbsMap_.end(); ++it) { int32_t clientPid = it->first; - std::vector rendererInfoList = GetCallbackRendererInfoList(clientPid); - for (auto rendererInfo : rendererInfoList) { + std::vector rendererFilterList = GetCallbackRendererInfoList(clientPid); + for (auto rendererFilter : rendererFilterList) { auto deviceDescs = AudioPolicyService::GetAudioPolicyService(). - GetPreferredOutputDeviceDescInner(rendererInfo); + GetPreferredOutputDeviceDescInner(rendererFilter.rendererInfo, LOCAL_NETWORK_ID, rendererFilter.uid); if (!(it->second->hasBTPermission_)) { AudioPolicyService::GetAudioPolicyService().UpdateDescWhenNoBTPermission(deviceDescs); } @@ -1098,7 +1098,7 @@ void AudioPolicyServerHandler::HandlePreferredOutputDeviceUpdated() AUDIO_INFO_LOG("Send PreferredOutputDevice deviceType[%{public}d] deviceId[%{public}d]" \ "change to clientPid[%{public}d]", deviceDescs[0]->deviceType_, deviceDescs[0]->deviceId_, clientPid); - it->second->OnPreferredOutputDeviceUpdated(rendererInfo, deviceDescs); + it->second->OnPreferredOutputDeviceUpdated(rendererFilter.rendererInfo, deviceDescs); } } } @@ -1653,22 +1653,26 @@ int32_t AudioPolicyServerHandler::SetClientCallbacksEnable(const CallbackChange return AUDIO_OK; } -int32_t AudioPolicyServerHandler::SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo) +int32_t AudioPolicyServerHandler::SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo, const int32_t uid) { int32_t clientPid = IPCSkeleton::GetCallingPid(); lock_guard lock(clientCbRendererInfoMapMutex_); auto &rendererList = clientCbRendererInfoMap_[clientPid]; auto it = std::find_if(rendererList.begin(), rendererList.end(), - [&rendererInfo](const AudioRendererInfo &existingRenderer) { - return existingRenderer.streamUsage == rendererInfo.streamUsage; + [&rendererInfo, uid](const AudioRendererFilter &existingFilter) { + return existingFilter.uid == uid && + existingFilter.rendererInfo.streamUsage == rendererInfo.streamUsage; }); if (it == rendererList.end()) { - rendererList.push_back(rendererInfo); + AudioRendererFilter newFilter; + newFilter.uid = uid; + newFilter.rendererInfo = rendererInfo; + rendererList.push_back(newFilter); } return AUDIO_OK; } -std::vector AudioPolicyServerHandler::GetCallbackRendererInfoList(int32_t clientPid) +std::vector AudioPolicyServerHandler::GetCallbackRendererInfoList(int32_t clientPid) { lock_guard lock(clientCbRendererInfoMapMutex_); auto it = clientCbRendererInfoMap_.find(clientPid); diff --git a/services/audio_policy/server/service/service_main/include/audio_core_service.h b/services/audio_policy/server/service/service_main/include/audio_core_service.h index dd623410ae7fcf7945c1669b96ddf39a4615f859..6600d36a2732648e09a6bd5219c39307b61f8144 100644 --- a/services/audio_policy/server/service/service_main/include/audio_core_service.h +++ b/services/audio_policy/server/service/service_main/include/audio_core_service.h @@ -128,7 +128,7 @@ public: bool ConnectServiceAdapter(); void OnReceiveUpdateDeviceNameEvent(const std::string macAddress, const std::string deviceName); int32_t SelectOutputDevice(sptr audioRendererFilter, - std::vector> selectedDesc); + std::vector> selectedDesc, const int32_t audioDeviceSelectMode = 0); int32_t SelectInputDevice(sptr audioCapturerFilter, std::vector> selectedDesc); void NotifyRemoteRenderState(std::string networkId, std::string condition, std::string value); @@ -240,7 +240,7 @@ private: bool ConnectServiceAdapter(); void OnReceiveUpdateDeviceNameEvent(const std::string macAddress, const std::string deviceName); int32_t SelectOutputDevice(sptr audioRendererFilter, - std::vector> selectedDesc); + std::vector> selectedDesc, const int32_t audioDeviceSelectMode = 0); void NotifyDistributedOutputChange(const AudioDeviceDescriptor &deviceDesc); int32_t SelectInputDevice(sptr audioCapturerFilter, std::vector> selectedDesc); diff --git a/services/audio_policy/server/service/service_main/include/audio_policy_server.h b/services/audio_policy/server/service/service_main/include/audio_policy_server.h index 73b8147036197aaa4fa4c917394a89fd88f88cd9..65c94efbb40f42e66d334956c6530e3a4845496d 100644 --- a/services/audio_policy/server/service/service_main/include/audio_policy_server.h +++ b/services/audio_policy/server/service/service_main/include/audio_policy_server.h @@ -156,7 +156,10 @@ public: void MapExternalToInternalDeviceType(AudioDeviceDescriptor &desc); int32_t SelectOutputDevice(const sptr &audioRendererFilter, - const std::vector> &audioDeviceDescriptors) override; + const std::vector> &audioDeviceDescriptors, + const int32_t audioDeviceSelectMode = 0) override; + + int32_t RestoreOutputDevice(const sptr &audioRendererFilter); int32_t GetSelectedDeviceInfo(int32_t uid, int32_t pid, int32_t streamType, std::string &info) override; @@ -347,7 +350,7 @@ public: int32_t SetClientCallbacksEnable(int32_t callbackchange, bool enable) override; - int32_t SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo) override; + int32_t SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo, const int32_t uid = -1) override; int32_t SetCallbackCapturerInfo(const AudioCapturerInfo &capturerInfo) override; diff --git a/services/audio_policy/server/service/service_main/include/audio_policy_service.h b/services/audio_policy/server/service/service_main/include/audio_policy_service.h index b634e93d3e7dac7c3f9a11120e1cd020752cfb3a..fdf38b9113210db03d085454aa641b5b2b110b38 100644 --- a/services/audio_policy/server/service/service_main/include/audio_policy_service.h +++ b/services/audio_policy/server/service/service_main/include/audio_policy_service.h @@ -185,7 +185,7 @@ public: AudioCapturerInfo &captureInfo, std::string networkId = LOCAL_NETWORK_ID); 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); diff --git a/services/audio_policy/server/service/service_main/src/audio_core_service.cpp b/services/audio_policy/server/service/service_main/src/audio_core_service.cpp index 060909bc5371b3fc479fb9ca95df7d99a1f34a87..fd723fc0ce10a4e8c07023bbff4c9de1a93cf041 100644 --- a/services/audio_policy/server/service/service_main/src/audio_core_service.cpp +++ b/services/audio_policy/server/service/service_main/src/audio_core_service.cpp @@ -1041,7 +1041,7 @@ void AudioCoreService::RecordSelectDevice(const std::string &selectHistory) } int32_t AudioCoreService::SelectOutputDevice(sptr audioRendererFilter, - std::vector> selectedDesc) + std::vector> selectedDesc, const int32_t audioDeviceSelectMode) { if (!selectedDesc.empty() && selectedDesc[0] != nullptr) { // eg. 2025-06-22-21:12:07:666|Uid: 6700 select output device: LOCAL_DEVICE type:2 @@ -1051,7 +1051,7 @@ int32_t AudioCoreService::SelectOutputDevice(sptr audioRend RecordSelectDevice(selectHistory); } - return audioRecoveryDevice_.SelectOutputDevice(audioRendererFilter, selectedDesc); + return audioRecoveryDevice_.SelectOutputDevice(audioRendererFilter, selectedDesc, audioDeviceSelectMode); } void AudioCoreService::NotifyDistributedOutputChange(const AudioDeviceDescriptor &deviceDesc) diff --git a/services/audio_policy/server/service/service_main/src/audio_core_service_entry.cpp b/services/audio_policy/server/service/service_main/src/audio_core_service_entry.cpp index c26c4486f3e42d15816f41d10be95d76289fa84b..31e1c985f76e852c4153bcaac369e74932d00f31 100644 --- a/services/audio_policy/server/service/service_main/src/audio_core_service_entry.cpp +++ b/services/audio_policy/server/service/service_main/src/audio_core_service_entry.cpp @@ -382,7 +382,7 @@ void AudioCoreService::EventEntry::OnReceiveUpdateDeviceNameEvent(const std::str } int32_t AudioCoreService::EventEntry::SelectOutputDevice(sptr audioRendererFilter, - std::vector> selectedDesc) + std::vector> selectedDesc, const int32_t audioDeviceSelectMode) { Trace trace("KeyAction AudioCoreService::SelectOutputDevice"); if (!selectedDesc.empty() && selectedDesc[0] && coreService_ && @@ -390,7 +390,7 @@ int32_t AudioCoreService::EventEntry::SelectOutputDevice(sptrNotifyDistributedOutputChange(selectedDesc[0]); } std::lock_guard lock(eventMutex_); - return coreService_->SelectOutputDevice(audioRendererFilter, selectedDesc); + return coreService_->SelectOutputDevice(audioRendererFilter, selectedDesc, audioDeviceSelectMode); } int32_t AudioCoreService::EventEntry::SelectInputDevice(sptr audioCapturerFilter, diff --git a/services/audio_policy/server/service/service_main/src/audio_policy_server.cpp b/services/audio_policy/server/service/service_main/src/audio_policy_server.cpp index 6b030c7772cb53ffc6e1848cd2859ab0bd49a86d..ea0ae8349c6bf7111acf75fc4a85fa867ec21f21 100644 --- a/services/audio_policy/server/service/service_main/src/audio_policy_server.cpp +++ b/services/audio_policy/server/service/service_main/src/audio_policy_server.cpp @@ -1932,7 +1932,8 @@ void AudioPolicyServer::MapExternalToInternalDeviceType(AudioDeviceDescriptor &d // LCOV_EXCL_START int32_t AudioPolicyServer::SelectOutputDevice(const sptr &audioRendererFilter, - const std::vector> &audioDeviceDescriptors) + const std::vector> &audioDeviceDescriptors, + const int32_t audioDeviceSelectMode) { CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySystemPermission(), ERR_PERMISSION_DENIED, "SelectOutputDevice: No system permission"); @@ -1946,7 +1947,19 @@ int32_t AudioPolicyServer::SelectOutputDevice(const sptr &a targetOutputDevice.push_back(newDeviceDescriptor); } - return eventEntry_->SelectOutputDevice(audioRendererFilter, targetOutputDevice); + return eventEntry_->SelectOutputDevice(audioRendererFilter, targetOutputDevice, audioDeviceSelectMode); +} + +int32_t AudioPolicyServer::RestoreOutputDevice(const sptr &audioRendererFilter) +{ + CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySystemPermission(), ERR_PERMISSION_DENIED, + "SelectOutputDevice: No system permission"); + + std::vector> restoreDescs; + restoreDescs.push_back(std::make_shared(DeviceType::DEVICE_TYPE_NONE, + DeviceRole::OUTPUT_DEVICE)); + + return eventEntry_->SelectOutputDevice(audioRendererFilter, restoreDescs); } int32_t AudioPolicyServer::GetSelectedDeviceInfo(int32_t uid, int32_t pid, int32_t streamTypeIn, @@ -2082,10 +2095,6 @@ int32_t AudioPolicyServer::GetDevicesInner(int32_t deviceFlagIn, int32_t AudioPolicyServer::GetOutputDevice(const sptr &audioRendererFilter, std::vector> &deviceDescs) { - if (!PermissionUtil::VerifySystemPermission()) { - AUDIO_ERR_LOG("only for system app"); - return ERR_INVALID_OPERATION; - } deviceDescs = audioPolicyService_.GetOutputDevice(audioRendererFilter); int32_t apiVersion = HasUsbDevice(deviceDescs) ? GetApiTargetVersion() : 0; @@ -2188,10 +2197,10 @@ int32_t AudioPolicyServer::SetClientCallbacksEnable(int32_t callbackchangeIn, bo } } -int32_t AudioPolicyServer::SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo) +int32_t AudioPolicyServer::SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo, const int32_t uid) { if (audioPolicyServerHandler_ != nullptr) { - audioPolicyServerHandler_->SetCallbackRendererInfo(rendererInfo); + audioPolicyServerHandler_->SetCallbackRendererInfo(rendererInfo, uid); } return SUCCESS; } diff --git a/services/audio_policy/server/service/service_main/src/audio_policy_service.cpp b/services/audio_policy/server/service/service_main/src/audio_policy_service.cpp index 5fc0f49e59ffd5ed8946e8a88117dbbb81fc2528..fe919a5f9b9c8ec60e8e282827193131183be599 100644 --- a/services/audio_policy/server/service/service_main/src/audio_policy_service.cpp +++ b/services/audio_policy/server/service/service_main/src/audio_policy_service.cpp @@ -294,9 +294,9 @@ std::vector> AudioPolicyService::GetPrefe } std::vector> AudioPolicyService::GetPreferredOutputDeviceDescInner( - AudioRendererInfo &rendererInfo, std::string networkId) + AudioRendererInfo &rendererInfo, std::string networkId, const int32_t uid) { - return audioDeviceCommon_.GetPreferredOutputDeviceDescInner(rendererInfo, networkId); + return audioDeviceCommon_.GetPreferredOutputDeviceDescInner(rendererInfo, networkId, uid); } std::vector> AudioPolicyService::GetPreferredInputDeviceDescInner( @@ -313,9 +313,13 @@ std::vector> AudioPolicyService::GetOutpu shared_ptr preferredDesc = audioAffinityManager_.GetRendererDevice(audioRendererFilter->uid); std::shared_ptr devDesc = std::make_shared(*preferredDesc); - deviceList.push_back(devDesc); + if (devDesc->deviceId_ != 0) { + deviceList.push_back(devDesc); + return deviceList; + } } - return deviceList; + return audioDeviceCommon_.GetPreferredOutputDeviceDescInner(audioRendererFilter->rendererInfo, + LOCAL_NETWORK_ID, audioRendererFilter->uid); } std::vector> AudioPolicyService::GetInputDevice( diff --git a/services/audio_policy/test/unittest/audio_policy_server_handler_unit_test/src/audio_policy_server_handler_unit_test.cpp b/services/audio_policy/test/unittest/audio_policy_server_handler_unit_test/src/audio_policy_server_handler_unit_test.cpp index 3e2409c3f2b23909219b136e6d58a6b73ffa359b..62585b38e1cb35e6af9182f60a79e008e5f82773 100644 --- a/services/audio_policy/test/unittest/audio_policy_server_handler_unit_test/src/audio_policy_server_handler_unit_test.cpp +++ b/services/audio_policy/test/unittest/audio_policy_server_handler_unit_test/src/audio_policy_server_handler_unit_test.cpp @@ -958,8 +958,8 @@ HWTEST(AudioPolicyServerHandlerUnitTest, GetCallbackRendererInfoList_002, TestSi auto audioPolicyServerHandler_ = std::make_shared(); EXPECT_NE(audioPolicyServerHandler_, nullptr); int32_t clientPid = 123; - std::vector infoList = {AudioRendererInfo()}; - audioPolicyServerHandler_->clientCbRendererInfoMap_[clientPid] = infoList; + std::vector filterList = {AudioRendererFilter()}; + audioPolicyServerHandler_->clientCbRendererInfoMap_[clientPid] = filterList; audioPolicyServerHandler_->GetCallbackRendererInfoList(clientPid); EXPECT_EQ(audioPolicyServerHandler_->audioPolicyClientProxyAPSCbsMap_.size(), 0); } diff --git a/services/audio_service/client/src/audio_routing_manager.cpp b/services/audio_service/client/src/audio_routing_manager.cpp index 6d1d2493695bc4dd7ec9b181b0be26798762d919..1c5ed539ce6c820b47746fc777844db2b349f211 100644 --- a/services/audio_service/client/src/audio_routing_manager.cpp +++ b/services/audio_service/client/src/audio_routing_manager.cpp @@ -65,12 +65,12 @@ int32_t AudioRoutingManager::GetPreferredInputDeviceForCapturerInfo(AudioCapture } int32_t AudioRoutingManager::SetPreferredOutputDeviceChangeCallback(AudioRendererInfo rendererInfo, - const std::shared_ptr& callback) + const std::shared_ptr& callback, const int32_t uid) { AUDIO_INFO_LOG("Entered %{public}s", __func__); CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_INVALID_PARAM, "callback is nullptr"); - return AudioPolicyManager::GetInstance().SetPreferredOutputDeviceChangeCallback(rendererInfo, callback); + return AudioPolicyManager::GetInstance().SetPreferredOutputDeviceChangeCallback(rendererInfo, callback, uid); } int32_t AudioRoutingManager::SetPreferredInputDeviceChangeCallback(AudioCapturerInfo capturerInfo, diff --git a/services/audio_service/client/src/audio_system_manager.cpp b/services/audio_service/client/src/audio_system_manager.cpp index 05f3c2f9043151fe1eea648070fc1843735bd51e..31324c52d742e3ce7f4c1590574fb5d388ab1dac 100644 --- a/services/audio_service/client/src/audio_system_manager.cpp +++ b/services/audio_service/client/src/audio_system_manager.cpp @@ -1030,7 +1030,8 @@ std::string AudioSystemManager::GetSelectedDeviceInfo(int32_t uid, int32_t pid, } int32_t AudioSystemManager::SelectOutputDevice(sptr audioRendererFilter, - std::vector> audioDeviceDescriptors) const + std::vector> audioDeviceDescriptors, + const int32_t audioDeviceSelectMode) const { // basic check CHECK_AND_RETURN_RET_LOG(audioRendererFilter != nullptr && audioDeviceDescriptors.size() != 0, @@ -1051,11 +1052,32 @@ int32_t AudioSystemManager::SelectOutputDevice(sptr audioRe CHECK_AND_RETURN_RET_LOG(audioRendererFilter->uid >= 0 || (audioRendererFilter->uid == -1), ERR_INVALID_PARAM, "invalid uid."); - AUDIO_DEBUG_LOG("[%{public}d] SelectOutputDevice: uid<%{public}d> streamType<%{public}d> device", - getpid(), audioRendererFilter->uid, static_cast(audioRendererFilter->streamType), - (audioDeviceDescriptors[0]->networkId_.c_str())); + CHECK_AND_RETURN_RET_LOG(audioDeviceSelectMode == 0 || audioDeviceSelectMode == 1, + ERR_INVALID_PARAM, "invalid audioDeviceSelectMode."); + + AUDIO_DEBUG_LOG("[%{public}d] SelectOutputDevice: uid<%{public}d> streamType<%{public}d> device " \ + " audioDeviceSelectMode<%{public}d>", getpid(), audioRendererFilter->uid, + static_cast(audioRendererFilter->streamType), (audioDeviceDescriptors[0]->networkId_.c_str()), + audioDeviceSelectMode); + + return AudioPolicyManager::GetInstance().SelectOutputDevice(audioRendererFilter, audioDeviceDescriptors, + audioDeviceSelectMode); +} + +int32_t AudioSystemManager::RestoreOutputDevice(sptr audioRendererFilter) const +{ + CHECK_AND_RETURN_RET_LOG(audioRendererFilter != nullptr, ERR_INVALID_PARAM, "invalid parameter"); + + audioRendererFilter->streamType = AudioSystemManager::GetStreamType(audioRendererFilter->rendererInfo.contentType, + audioRendererFilter->rendererInfo.streamUsage); + + CHECK_AND_RETURN_RET_LOG(audioRendererFilter->uid >= 0 || (audioRendererFilter->uid == -1), + ERR_INVALID_PARAM, "invalid uid."); + + AUDIO_DEBUG_LOG("[%{public}d] RestoreOutputDevice: uid<%{public}d> streamType<%{public}d>", + getpid(), audioRendererFilter->uid, static_cast(audioRendererFilter->streamType)); - return AudioPolicyManager::GetInstance().SelectOutputDevice(audioRendererFilter, audioDeviceDescriptors); + return AudioPolicyManager::GetInstance().RestoreOutputDevice(audioRendererFilter); } int32_t AudioSystemManager::SelectInputDevice(sptr audioCapturerFilter,