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..50875f7938c4d961b08307dc6f53ea22ce7a6d44 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_FOR_UID_CALLBACK_NAME = "preferOutputDeviceChangeForRendererInfoForUid"; 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..4f3c98bb64e56b272d80a5cbbdbd8d68fd95e4fb 100644 --- a/frameworks/js/napi/audiomanager/napi_audio_routing_manager.cpp +++ b/frameworks/js/napi/audiomanager/napi_audio_routing_manager.cpp @@ -93,6 +93,7 @@ napi_value NapiAudioRoutingManager::Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getDevicesSync", GetDevicesSync), DECLARE_NAPI_FUNCTION("selectOutputDevice", SelectOutputDevice), DECLARE_NAPI_FUNCTION("selectOutputDeviceByFilter", SelectOutputDeviceByFilter), + DECLARE_NAPI_FUNCTION("restoreOutputDeviceByFilter", RestoreOutputDeviceByFilter), DECLARE_NAPI_FUNCTION("selectInputDevice", SelectInputDevice), DECLARE_NAPI_FUNCTION("selectInputDeviceByFilter", SelectInputDeviceByFilter), DECLARE_NAPI_FUNCTION("excludeOutputDevices", ExcludeOutputDevices), @@ -103,6 +104,7 @@ napi_value NapiAudioRoutingManager::Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getActiveOutputDeviceDescriptors", GetActiveOutputDeviceDescriptors), DECLARE_NAPI_FUNCTION("getPreferredOutputDeviceForRendererInfo", GetPreferredOutputDeviceForRendererInfo), DECLARE_NAPI_FUNCTION("getPreferOutputDeviceForRendererInfo", GetPreferOutputDeviceForRendererInfo), + DECLARE_NAPI_FUNCTION("getPreferOutputDeviceForRendererInfoForUid", GetPreferOutputDeviceForRendererInfoForUid), DECLARE_NAPI_FUNCTION("getPreferredOutputDeviceForRendererInfoSync", GetPreferredOutputDeviceForRendererInfoSync), DECLARE_NAPI_FUNCTION("getPreferredOutputDeviceByFilter", GetPreferredOutputDeviceByFilter), @@ -318,6 +320,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 +337,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); }; @@ -343,6 +348,47 @@ napi_value NapiAudioRoutingManager::SelectOutputDeviceByFilter(napi_env env, nap return NapiAsyncWork::Enqueue(env, context, "SelectOutputDeviceByFilter", executor, complete); } +napi_value NapiAudioRoutingManager::RestoreOutputDeviceByFilter(napi_env env, napi_callback_info info) +{ + auto context = std::make_shared(); + if (context == nullptr) { + AUDIO_ERR_LOG("RestoreOutputDeviceByFilter failed : no memory"); + NapiAudioError::ThrowError(env, NAPI_ERR_NO_MEMORY); + return NapiParamUtils::GetUndefinedValue(env); + } + + auto inputParser = [env, context](size_t argc, napi_value *argv) { + NAPI_CHECK_ARGS_RETURN_VOID(context, argc >= ARGS_ONE, "invalid arguments", + NAPI_ERR_INVALID_PARAM); + NapiParamUtils::GetAudioRendererFilter(env, context->audioRendererFilter, + context->bArgTransFlag, argv[PARAM0]); + }; + context->GetCbInfo(env, info, inputParser); + context->deviceDescriptors.push_back(std::make_shared(DeviceType::DEVICE_TYPE_NONE, + DeviceRole::OUTPUT_DEVICE)); + + auto executor = [context]() { + CHECK_AND_RETURN_LOG(CheckContextStatus(context), "context object state is error."); + auto obj = reinterpret_cast(context->native); + ObjectRefMap objectGuard(obj); + auto *napiAudioRoutingManager = objectGuard.GetPtr(); + CHECK_AND_RETURN_LOG(CheckAudioRoutingManagerStatus(napiAudioRoutingManager, context), + "context object state is error."); + if (!context->bArgTransFlag) { + context->SignError(NAPI_ERR_UNSUPPORTED); + } + context->intValue = napiAudioRoutingManager->audioMngr_->SelectOutputDevice(context->audioRendererFilter, + context->deviceDescriptors); + NAPI_CHECK_ARGS_RETURN_VOID(context, context->intValue == SUCCESS, "RestoreOutputDeviceByFilter failed", + NAPI_ERR_SYSTEM); + }; + + auto complete = [env](napi_value &output) { + output = NapiParamUtils::GetUndefinedValue(env); + }; + return NapiAsyncWork::Enqueue(env, context, "RestoreOutputDeviceByFilter", executor, complete); +} + napi_value NapiAudioRoutingManager::SelectInputDevice(napi_env env, napi_callback_info info) { auto context = std::make_shared(); @@ -699,6 +745,57 @@ napi_value NapiAudioRoutingManager::GetPreferOutputDeviceForRendererInfo(napi_en return GetPreferredOutputDeviceForRendererInfo(env, info); } +napi_value NapiAudioRoutingManager::GetPreferOutputDeviceForRendererInfoForUid(napi_env env, napi_callback_info info) +{ + auto context = std::make_shared(); + if (context == nullptr) { + AUDIO_ERR_LOG("GetPreferOutputDeviceForRendererInfoForUid failed : no memory"); + NapiAudioError::ThrowError(env, NAPI_ERR_NO_MEMORY); + return NapiParamUtils::GetUndefinedValue(env); + } + + auto inputParser = [env, context](size_t argc, napi_value *argv) { + NAPI_CHECK_ARGS_RETURN_VOID(context, argc >= ARGS_TWO, "mandatory parameters are left unspecified", + NAPI_ERR_INPUT_INVALID); + context->status = NapiParamUtils::GetValueInt32(env, context->uid, argv[PARAM0]); + NAPI_CHECK_ARGS_RETURN_VOID(context, context->status == napi_ok, + "incorrect parameter types: The type of uid must be number", + NAPI_ERR_INPUT_INVALID); + context->status = NapiParamUtils::GetRendererInfo(env, &(context->rendererInfo), argv[PARAM1]); + NAPI_CHECK_ARGS_RETURN_VOID(context, context->status == napi_ok, + "incorrect parameter types: The type of rendererInfo must be interface AudioRendererInfo", + NAPI_ERR_INPUT_INVALID); + }; + context->GetCbInfo(env, info, inputParser); + if (context->status != napi_ok) { + NapiAudioError::ThrowError(env, context->errCode, context->errMessage); + return NapiParamUtils::GetUndefinedValue(env); + } + + auto executor = [context]() { + CHECK_AND_RETURN_LOG(CheckContextStatus(context), "context object state is error."); + auto obj = reinterpret_cast(context->native); + ObjectRefMap objectGuard(obj); + auto *napiAudioRoutingManager = objectGuard.GetPtr(); + CHECK_AND_RETURN_LOG(CheckAudioRoutingManagerStatus(napiAudioRoutingManager, context), + "context object state is error."); + if (context->rendererInfo.streamUsage == StreamUsage::STREAM_USAGE_INVALID) { + context->SignError(NAPI_ERR_INVALID_PARAM, + "Parameter verification failed. Your usage in AudioRendererInfo is invalid."); + } else { + context->intValue = napiAudioRoutingManager->audioRoutingMngr_->GetPreferredOutputDeviceForRendererInfo( + context->rendererInfo, context->outDeviceDescriptors, context->uid); + NAPI_CHECK_ARGS_RETURN_VOID(context, context->intValue == SUCCESS, + "GetPreferOutputDeviceForRendererInfoForUid failed", NAPI_ERR_SYSTEM); + } + }; + + auto complete = [env, context](napi_value &output) { + NapiParamUtils::SetDeviceDescriptors(env, context->outDeviceDescriptors, output); + }; + return NapiAsyncWork::Enqueue(env, context, "GetPreferOutputDeviceForRendererInfoForUid", executor, complete); +} + napi_value NapiAudioRoutingManager::GetPreferredOutputDeviceForRendererInfoSync(napi_env env, napi_callback_info info) { AUDIO_INFO_LOG("GetPreferredOutputDeviceForRendererInfoSync"); @@ -984,6 +1081,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_FOR_UID_CALLBACK_NAME)) { + RegisterPreferredOutputDeviceForUidChangeCallback(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)) { @@ -1068,7 +1167,7 @@ void NapiAudioRoutingManager::RegisterPreferredOutputDeviceChangeCallback(napi_e { 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[PARAM1], napi_object), NapiAudioError::ThrowError(env, NAPI_ERR_INPUT_INVALID, "incorrect parameter types: The type of rendererInfo must be object"), "rendererInfo invalid"); @@ -1100,6 +1199,57 @@ void NapiAudioRoutingManager::RegisterPreferredOutputDeviceChangeCallback(napi_e AddPreferredOutputDeviceChangeCallback(napiRoutingMgr, cb); } +void NapiAudioRoutingManager::RegisterPreferredOutputDeviceForUidChangeCallback(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"); + + int32_t optionalArgsCount = ARGS_ZERO; + int32_t uid = -1; + if (argc == ARGS_FOUR) { + optionalArgsCount = ARGS_ONE; + + CHECK_AND_RETURN_RET_LOG(NapiParamUtils::CheckArgType(env, args[PARAM1], napi_number), + NapiAudioError::ThrowError(env, NAPI_ERR_INPUT_INVALID, + "incorrect parameter types: The type of uid must be number"), "uid invalid"); + + NapiParamUtils::GetValueInt32(env, uid, args[PARAM1]); + CHECK_AND_RETURN_RET_LOG(uid >= -1, NapiAudioError::ThrowError(env, NAPI_ERR_INVALID_PARAM, + "parameter verification failed: Your uid is invalid."), "invalid uid"); + } + + CHECK_AND_RETURN_RET_LOG(NapiParamUtils::CheckArgType(env, args[PARAM1 + optionalArgsCount], napi_object), + NapiAudioError::ThrowError(env, NAPI_ERR_INPUT_INVALID, + "incorrect parameter types: The type of rendererInfo must be object"), "rendererInfo invalid"); + + CHECK_AND_RETURN_RET_LOG(NapiParamUtils::CheckArgType(env, args[PARAM2 + optionalArgsCount], 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 + optionalArgsCount], napiRoutingMgr) == nullptr, + "Do not allow duplicate registration of the same callback"); + + AudioRendererInfo rendererInfo; + NapiParamUtils::GetRendererInfo(env, &rendererInfo, args[PARAM1 + optionalArgsCount]); + CHECK_AND_RETURN_RET_LOG(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 + optionalArgsCount]); + cb->CreatePreferredOutTsfn(env); + + int32_t ret = napiRoutingMgr->audioRoutingMngr_->SetPreferredOutputDeviceChangeCallback( + rendererInfo, cb, uid); + CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, NapiAudioError::ThrowError(env, ret), + "Registering Preferred Output Device For Uid Change Callback Failed %{public}d", ret); + + AddPreferredOutputDeviceChangeCallback(napiRoutingMgr, cb); +} + std::shared_ptr NapiAudioRoutingManager::GetNapiPrefInputDeviceChangeCb( napi_value args, NapiAudioRoutingManager *napiRoutingMgr) { @@ -1231,16 +1381,17 @@ void NapiAudioRoutingManager::RegisterMicrophoneBlockedCallback(napi_env env, si napi_value NapiAudioRoutingManager::On(napi_env env, napi_callback_info info) { const size_t requireArgc = ARGS_TWO; - const size_t maxArgc = ARGS_THREE; - size_t argc = ARGS_THREE; + const size_t availableArgc = ARGS_THREE; + const size_t maxArgc = ARGS_FOUR; + size_t argc = ARGS_FOUR; napi_value undefinedResult = nullptr; napi_get_undefined(env, &undefinedResult); - napi_value args[requireArgc + PARAM1] = { nullptr, nullptr, nullptr }; + napi_value args[requireArgc + PARAM2] = { nullptr, nullptr, nullptr, nullptr }; napi_value jsThis = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr); - bool isArgcCountRight = argc == requireArgc || argc == maxArgc; + bool isArgcCountRight = argc == requireArgc || argc == availableArgc || argc == maxArgc; CHECK_AND_RETURN_RET_LOG(status == napi_ok && isArgcCountRight, NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID, "mandatory parameters are left unspecified"), "status or isArgcCountRight error"); diff --git a/frameworks/js/napi/audiomanager/napi_audio_routing_manager.h b/frameworks/js/napi/audiomanager/napi_audio_routing_manager.h index 085019c4a17027cda60c542bb41ec24baefa6a53..e7738db86c89b073162700458cb5c6c40aa2d0ae 100644 --- a/frameworks/js/napi/audiomanager/napi_audio_routing_manager.h +++ b/frameworks/js/napi/audiomanager/napi_audio_routing_manager.h @@ -40,6 +40,8 @@ private: int32_t deviceFlag; int32_t deviceType; int32_t intValue; + int32_t audioDeviceSelectMode = 0; + int32_t uid = -1; bool isActive; bool isTrue; bool bArgTransFlag = true; @@ -64,6 +66,7 @@ private: static napi_value Off(napi_env env, napi_callback_info info); static napi_value SelectOutputDevice(napi_env env, napi_callback_info info); static napi_value SelectOutputDeviceByFilter(napi_env env, napi_callback_info info); + static napi_value RestoreOutputDeviceByFilter(napi_env env, napi_callback_info info); static napi_value SelectInputDevice(napi_env env, napi_callback_info info); static napi_value SelectInputDeviceByFilter(napi_env env, napi_callback_info info); static napi_value ExcludeOutputDevices(napi_env env, napi_callback_info info); @@ -74,6 +77,7 @@ private: static napi_value GetActiveOutputDeviceDescriptors(napi_env env, napi_callback_info info); static napi_value GetPreferredOutputDeviceForRendererInfo(napi_env env, napi_callback_info info); static napi_value GetPreferOutputDeviceForRendererInfo(napi_env env, napi_callback_info info); + static napi_value GetPreferOutputDeviceForRendererInfoForUid(napi_env env, napi_callback_info info); static napi_value GetPreferredOutputDeviceForRendererInfoSync(napi_env env, napi_callback_info info); static napi_value GetPreferredOutputDeviceByFilter(napi_env env, napi_callback_info info); static napi_value GetPreferredInputDeviceForCapturerInfo(napi_env env, napi_callback_info info); @@ -88,6 +92,8 @@ private: NapiAudioRoutingManager *napiRoutingMgr); static void RegisterPreferredOutputDeviceChangeCallback(napi_env env, size_t argc, napi_value *args, const std::string &cbName, NapiAudioRoutingManager *napiRoutingMgr); + static void RegisterPreferredOutputDeviceForUidChangeCallback(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/js/napi/common/napi_audio_enum.h b/frameworks/js/napi/common/napi_audio_enum.h index 0d8a7b5e2fbc0d51bc42b51e91481316c324544a..4245ef6faa7b2aa59208d6bc742354ea42d823fe 100644 --- a/frameworks/js/napi/common/napi_audio_enum.h +++ b/frameworks/js/napi/common/napi_audio_enum.h @@ -123,6 +123,11 @@ public: LOOPBACK_MODE_HARDWARE = 0 }; + enum AudioDeviceSelectMode { + DEFAULT = 0, + INDEPENDENT = 1 + }; + static napi_value Init(napi_env env, napi_value exports); static bool IsLegalInputArgumentInterruptMode(int32_t interruptMode); static bool IsLegalInputArgumentAudioEffectMode(int32_t audioEffectMode); diff --git a/frameworks/js/napi/common/napi_param_utils.h b/frameworks/js/napi/common/napi_param_utils.h index 5c9b0a1aec358ddc85278382d1a2243510b8b23b..15b4791d8c23156864bab6001f41cdbd6ca27ac7 100644 --- a/frameworks/js/napi/common/napi_param_utils.h +++ b/frameworks/js/napi/common/napi_param_utils.h @@ -38,6 +38,7 @@ const int ARGS_ZERO = 0; const int ARGS_ONE = 1; const int ARGS_TWO = 2; const int ARGS_THREE = 3; +const int ARGS_FOUR = 4; const int PARAM0 = 0; const int PARAM1 = 1; diff --git a/frameworks/native/audiopolicy/include/audio_policy_manager.h b/frameworks/native/audiopolicy/include/audio_policy_manager.h index 5b66996db43d025f0049d3a94c1a4c30c7725ed0..314685c7c00692db9b604b658df610880f1dbcc1 100644 --- a/frameworks/native/audiopolicy/include/audio_policy_manager.h +++ b/frameworks/native/audiopolicy/include/audio_policy_manager.h @@ -109,7 +109,7 @@ public: bool IsFastRecordingSupported(AudioStreamInfo &streamInfo, SourceType source); int32_t SelectOutputDevice(sptr audioRendererFilter, - std::vector> audioDeviceDescriptors); + std::vector> audioDeviceDescriptors, const int32_t audioDeviceSelectMode = 0); std::string GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType); @@ -338,13 +338,13 @@ public: int32_t GetNetworkIdByGroupId(int32_t groupId, std::string &networkId); std::vector> GetPreferredOutputDeviceDescriptors( - AudioRendererInfo &rendererInfo, bool forceNoBTPermission = false); + AudioRendererInfo &rendererInfo, bool forceNoBTPermission = false, const int32_t uid = -1); std::vector> GetPreferredInputDeviceDescriptors( 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); @@ -697,7 +697,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/audiomanager/include/audio_routing_manager.h b/interfaces/inner_api/native/audiomanager/include/audio_routing_manager.h index 5dd388ab651078d3e505bbfb098187ef597eda69..276d912d7fcb8f3e3ff3166942fe2493d33a14a3 100644 --- a/interfaces/inner_api/native/audiomanager/include/audio_routing_manager.h +++ b/interfaces/inner_api/native/audiomanager/include/audio_routing_manager.h @@ -38,9 +38,9 @@ public: static AudioRoutingManager *GetInstance(); int32_t SetMicStateChangeCallback(const std::shared_ptr &callback); int32_t GetPreferredOutputDeviceForRendererInfo(AudioRendererInfo rendererInfo, - std::vector> &desc); + std::vector> &desc, const int32_t uid = -1); 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 24837a1d8f88738a181453d598ca1ded525624d7..e63891e233b37427af8123d2fb59efeb3464a4d4 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,7 @@ 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 77d5c7c43dda7fda0226e9b921a8bef14be7798e..b9c5a10c9378b88f5a407b807624153f683a4e7c 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_) { @@ -643,7 +643,7 @@ int32_t AudioPolicyManager::SetCallbackRendererInfo(const AudioRendererInfo &ren { 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 6521e5d803b485fbb40ace4db98365efe9f225f8..e4a79f2d50e15b72b6877836a9d8c99cd658c4d2 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,7 @@ int32_t AudioPolicyManager::SelectOutputDevice(sptr audioRe return -1; } - return gsp->SelectOutputDevice(audioRendererFilter, audioDeviceDescriptors); + return gsp->SelectOutputDevice(audioRendererFilter, audioDeviceDescriptors, audioDeviceSelectMode); } std::string AudioPolicyManager::GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType) @@ -130,7 +130,7 @@ std::vector> AudioPolicyManager::GetDevic } std::vector> AudioPolicyManager::GetPreferredOutputDeviceDescriptors( - AudioRendererInfo &rendererInfo, bool forceNoBTPermission) + AudioRendererInfo &rendererInfo, bool forceNoBTPermission, const int32_t uid) { AudioXCollie audioXCollie("AudioPolicyManager::GetPreferredOutputDeviceDescriptors", TIME_OUT_SECONDS, nullptr, nullptr, AUDIO_XCOLLIE_FLAG_LOG); @@ -140,7 +140,7 @@ std::vector> AudioPolicyManager::GetPrefe AUDIO_ERR_LOG("GetPreferredOutputDeviceDescriptors: audio policy manager proxy is NULL."); return deviceInfo; } - gsp->GetPreferredOutputDeviceDescriptors(rendererInfo, forceNoBTPermission, deviceInfo); + gsp->GetPreferredOutputDeviceDescriptors(rendererInfo, forceNoBTPermission, deviceInfo, uid); return deviceInfo; } @@ -288,7 +288,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 +306,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 455902669eeaff863af0eab118d464be1b2ca94f..f8e7098d3db4287fe5f09440d730d82ef4f6406b 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); /* audioDeviceSelectMode default value: 0 */ 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); @@ -126,7 +126,7 @@ interface IAudioPolicy { void GetNetworkIdByGroupId([in] int groupId, [out] String networkId); void GetToneConfig([in] int ltonetype, [in] String countryCode, [out] sharedptr config); // [macrodef FEATURE_DTMF_TONE] void GetSupportedTones([in] String countryCode, [out] List tones); // [macrodef FEATURE_DTMF_TONE] - void GetPreferredOutputDeviceDescriptors([in] AudioRendererInfo rendererInfo, [in] boolean forceNoBTPermission, [out] List> retDevList); + void GetPreferredOutputDeviceDescriptors([in] AudioRendererInfo rendererInfo, [in] boolean forceNoBTPermission, [out] List> retDevList, [in] int uid); /*uid default value: -1*/ void GetPreferredInputDeviceDescriptors([in] AudioCapturerInfo captureInfo, [out] List> retDevList); void SetClientCallbacksEnable([in] int callbackchange, [in] boolean enable); void GetAudioFocusInfoList([out] List> focusInfoList, [in] int zoneId); /* zoneID default value: 0 -- local device */ @@ -246,7 +246,7 @@ interface IAudioPolicy { void UnsetAudioDeviceAnahsCallback(); void IsAllowedPlayback([in] int uid, [in] int pid, [out] boolean isAllowed); void SetVoiceRingtoneMute([in] boolean isMute); - void SetCallbackRendererInfo([in] AudioRendererInfo rendererInfo); + void SetCallbackRendererInfo([in] AudioRendererInfo rendererInfo, [in] int uid);/* uid default value: -1 */ void SetCallbackCapturerInfo([in] AudioCapturerInfo capturerInfo); 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); 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 57e34d3232d955e1a4037227a1bb8116b844641d..ba516763e569c30d500744e09894d03e54c3fa44 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_device_lock.h b/services/audio_policy/server/domain/device/include/audio_device_lock.h index bc875c2cbbd9247406ca696585cb11a5160c14f6..b502520a0ba3cad30a53125a04a96570d0649988 100644 --- a/services/audio_policy/server/domain/device/include/audio_device_lock.h +++ b/services/audio_policy/server/domain/device/include/audio_device_lock.h @@ -60,7 +60,7 @@ public: void DeInit(); std::vector> GetDevices(DeviceFlag deviceFlag); std::vector> GetPreferredOutputDeviceDescriptors( - AudioRendererInfo &rendererInfo, std::string networkId = LOCAL_NETWORK_ID); + AudioRendererInfo &rendererInfo, std::string networkId = LOCAL_NETWORK_ID, const int32_t uid = -1); std::vector> GetPreferredInputDeviceDescriptors( AudioCapturerInfo &captureInfo, std::string networkId = LOCAL_NETWORK_ID); int32_t UnexcludeOutputDevices(AudioDeviceUsage audioDevUsage, 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..19db6c08dd70134998491a0e084899bee2586048 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,7 @@ 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, 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..c99a491bd34458846f8cae6bd74cdcf4f7cb0523 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 @@ -62,7 +62,7 @@ public: shared_ptr GetPreferredMediaRenderDevice(); // Get call render device selected by the user - shared_ptr GetPreferredCallRenderDevice(); + shared_ptr GetPreferredCallRenderDevice(const int32_t clientUID = -1); // 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 c61bb71191f2db4f55d0ae72cbef5739ea510f6d..803e2a6b7a1655a8891d4ef38e80303938418fa9 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 @@ -155,7 +155,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 || @@ -169,7 +169,7 @@ std::vector> AudioDeviceCommon::GetPrefer if (networkId == LOCAL_NETWORK_ID) { vector> descs = audioRouterCenter_.FetchOutputDevices(rendererInfo.streamUsage, - -1, "GetPreferredOutputDeviceDescInner"); + uid, "GetPreferredOutputDeviceDescInner"); for (size_t i = 0; i < descs.size(); i++) { std::shared_ptr devDesc = std::make_shared(*descs[i]); deviceList.push_back(devDesc); diff --git a/services/audio_policy/server/domain/device/src/audio_device_lock.cpp b/services/audio_policy/server/domain/device/src/audio_device_lock.cpp index 5183e5f9f727e9327f52b574bc1ac871c19ad961..1b35b0678a102f13df8f676d9140d943789c0f92 100644 --- a/services/audio_policy/server/domain/device/src/audio_device_lock.cpp +++ b/services/audio_policy/server/domain/device/src/audio_device_lock.cpp @@ -46,10 +46,10 @@ std::vector> AudioDeviceLock::GetDevices( } std::vector> AudioDeviceLock::GetPreferredOutputDeviceDescriptors( - AudioRendererInfo &rendererInfo, std::string networkId) + AudioRendererInfo &rendererInfo, std::string networkId, const int32_t uid) { std::shared_lock deviceLock(deviceStatusUpdateSharedMutex_); - return audioDeviceCommon_.GetPreferredOutputDeviceDescInner(rendererInfo, networkId); + return audioDeviceCommon_.GetPreferredOutputDeviceDescInner(rendererInfo, networkId, uid); } std::vector> AudioDeviceLock::GetPreferredInputDeviceDescriptors( 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 f8045d0164c40a175ff3d8f18e0ab17f776b9919..87b5962923c8904a722bb497d05fb3d3ed16ef52 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,9 +205,12 @@ int32_t AudioRecoveryDevice::SelectOutputDevice(sptr audioR SetDeviceEnableAndUsage(selectedDesc[0]); - if (audioRendererFilter->uid != -1) { + if (audioDeviceSelectMode == 1 && audioRendererFilter->uid >= 0) { return SelectOutputDeviceByFilterInner(audioRendererFilter, selectedDesc); } + if (selectedDesc[0]->deviceType_ == DEVICE_TYPE_NONE && audioRendererFilter->uid >= 0){ + audioAffinityManager_.DelSelectRendererDevice(audioRendererFilter->uid); + } if (audioRendererFilter->rendererInfo.rendererFlags == STREAM_FLAG_FAST) { return SelectOutputDeviceForFastInner(audioRendererFilter, selectedDesc); } @@ -291,12 +295,13 @@ 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(); 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..d3a878b7196a4092b7eabf596d8e1b5c9ef96268 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 @@ -158,9 +158,21 @@ shared_ptr AudioStateManager::GetPreferredMediaRenderDevi return devDesc; } -shared_ptr AudioStateManager::GetPreferredCallRenderDevice() +shared_ptr AudioStateManager::GetPreferredCallRenderDevice(const int32_t clientUid) { std::lock_guard lock(mutex_); + if (clientUid != -1) { + if (!forcedDeviceMapList_.empty()) { + for (auto it = forcedDeviceMapList_.begin(); it != forcedDeviceMapList_.end(); ++it) { + if (SYSTEM_UID == it->begin()->first) { + AUDIO_INFO_LOG("deviceType: %{public}d", + it->begin()->second->deviceType_); + return make_shared(it->begin()->second); + } + } + } + return std::make_shared(); + } if (ownerUid_ == 0) { if (!forcedDeviceMapList_.empty()) { AUDIO_INFO_LOG("ownerUid_: 0, deviceType: %{public}d, Uid: %{public}d", diff --git a/services/audio_policy/server/domain/router/user_select_router.cpp b/services/audio_policy/server/domain/router/user_select_router.cpp index 57dec10da3501670985f7f0c0e50173d8e6ed0f8..82db1893ecdf9be5c3acc762e990f5e61bbf7129 100644 --- a/services/audio_policy/server/domain/router/user_select_router.cpp +++ b/services/audio_policy/server/domain/router/user_select_router.cpp @@ -46,7 +46,7 @@ shared_ptr UserSelectRouter::GetMediaRenderDevice(StreamU shared_ptr UserSelectRouter::GetCallRenderDevice(StreamUsage streamUsage, int32_t clientUID) { shared_ptr perDev_ = - AudioStateManager::GetAudioStateManager().GetPreferredCallRenderDevice(); + AudioStateManager::GetAudioStateManager().GetPreferredCallRenderDevice(clientUID); CHECK_AND_RETURN_RET_LOG(perDev_ != nullptr, make_shared(), "perDev is null"); vector> callDevices = AudioDeviceManager::GetAudioDeviceManager().GetAvailableDevicesByUsage(CALL_OUTPUT_DEVICES); 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 810bba8633fca14bd0e1d76573f34fcff8a9c3f8..664704eafabfed4b5e3fa270bf5ac41d1cab84d9 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 @@ -201,7 +201,7 @@ public: const std::shared_ptr &selectedAudioDevice, const bool &enabled); bool SendPipeStreamCleanEvent(AudioPipeType pipeType); 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); @@ -266,7 +266,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_; @@ -286,7 +286,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 83b2461039a6ac5fd38236781948f8ee863f2d96..cf69abb61bce2e9fafabecdb60c93f94fcce7ea3 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 @@ -1089,10 +1089,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) { + auto rendererPairs = GetCallbackRendererInfoList(clientPid); + for (auto rendererPair : rendererPairs) { auto deviceDescs = AudioPolicyService::GetAudioPolicyService(). - GetPreferredOutputDeviceDescInner(rendererInfo); + GetPreferredOutputDeviceDescInner(rendererPairs.first, {}, rendererPair.second); if (!(it->second->hasBTPermission_)) { AudioPolicyService::GetAudioPolicyService().UpdateDescWhenNoBTPermission(deviceDescs); } @@ -1103,7 +1103,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(rendererPair.first, deviceDescs); } } } @@ -1669,22 +1669,23 @@ 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; + + auto &rendererPairs = clientCbRendererInfoMap_[clientPid]; + auto it = std::find_if(rendererPairs.begin(), rendererPairs.end(), + [&rendererInfo, uid](const std::pair &existingPair) { + return existingPair.first.streamUsage == rendererInfo.streamUsage && existingPair.second == uid; }); - if (it == rendererList.end()) { - rendererList.push_back(rendererInfo); + if (it == rendererPairs.end()) { + rendererPairs.push_back({rendererInfo, uid}); } 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 e2015f952ddd632e38f82635c541aff41ec20ed4..b86b186abeb52750266fdbbbdd5e0f68222ccf18 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 @@ -127,7 +127,7 @@ public: bool ConnectServiceAdapter(); void OnReceiveBluetoothEvent(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); @@ -237,7 +237,7 @@ private: bool ConnectServiceAdapter(); void OnReceiveBluetoothEvent(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 56b600ec1f17a98dcdb94f2286eea4ef05a2d017..8a3d069a11ec4b5c93ffa88a8e2623dac706c420 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,7 @@ 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 GetSelectedDeviceInfo(int32_t uid, int32_t pid, int32_t streamType, std::string &info) override; @@ -334,7 +334,7 @@ public: int32_t GetNetworkIdByGroupId(int32_t groupId, std::string &networkId) override; int32_t GetPreferredOutputDeviceDescriptors(const AudioRendererInfo &rendererInfo, bool forceNoBTPermission, - std::vector> &deviceDescs) override; + std::vector> &deviceDescs, const int32_t uid = -1) override; int32_t GetPreferredInputDeviceDescriptors(const AudioCapturerInfo &captureInfo, std::vector> &deviceDescs) override; @@ -347,7 +347,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 6f2f18f69229d0124cbd7d796e355cbd9936d5d2..63971b749b74a8c02d2fc647fdd51cd7620cdcba 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 f56533a4267244540cc78093b4d8e0b026cc25b8..9bd070ed4cd44cbce7039a3d1bce05d7219edadc 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 @@ -1010,7 +1010,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 @@ -1020,7 +1020,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 cda570d4ae2d0dd86d4c8c0a3cea8a110db7c0d8..967c9bfa5d4fa15fdf9b79ed0b8e2abbd067d727 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 @@ -380,7 +380,7 @@ void AudioCoreService::EventEntry::OnReceiveBluetoothEvent(const std::string mac } 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_ && @@ -388,7 +388,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 9e2ece09558ac5dcd362cf643166639c166358a5..36e8de89701942b0573c85d114c4e4496ccecdc5 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 @@ -1747,7 +1747,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"); @@ -1761,7 +1762,7 @@ 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::GetSelectedDeviceInfo(int32_t uid, int32_t pid, int32_t streamTypeIn, @@ -1942,10 +1943,10 @@ int32_t AudioPolicyServer::VerifyVoiceCallPermission( // LCOV_EXCL_STOP int32_t AudioPolicyServer::GetPreferredOutputDeviceDescriptors(const AudioRendererInfo &rendererInfo, - bool forceNoBTPermission, std::vector> &deviceDescs) + bool forceNoBTPermission, std::vector> &deviceDescs, const int32_t uid) { AudioRendererInfo newRendererInfo = rendererInfo; - deviceDescs = audioDeviceLock_.GetPreferredOutputDeviceDescriptors(newRendererInfo, LOCAL_NETWORK_ID); + deviceDescs = audioDeviceLock_.GetPreferredOutputDeviceDescriptors(newRendererInfo, LOCAL_NETWORK_ID, uid); bool hasBTPermission = false; if (!forceNoBTPermission) { @@ -1992,10 +1993,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 5c306c38bffdc9ec92e05db887ffee7fe6eeacc4..470cbb1ae3701c792d1a3326c48a5e75c57e46ce 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 @@ -293,9 +293,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( diff --git a/services/audio_service/client/src/audio_routing_manager.cpp b/services/audio_service/client/src/audio_routing_manager.cpp index 6d1d2493695bc4dd7ec9b181b0be26798762d919..4e90528bfb5b0abc5cb7c9fc9cc6de134b9f52f0 100644 --- a/services/audio_service/client/src/audio_routing_manager.cpp +++ b/services/audio_service/client/src/audio_routing_manager.cpp @@ -49,9 +49,9 @@ int32_t AudioRoutingManager::SetMicStateChangeCallback( } int32_t AudioRoutingManager::GetPreferredOutputDeviceForRendererInfo(AudioRendererInfo rendererInfo, - std::vector> &desc) + std::vector> &desc, const int32_t uid) { - desc = AudioPolicyManager::GetInstance().GetPreferredOutputDeviceDescriptors(rendererInfo); + desc = AudioPolicyManager::GetInstance().GetPreferredOutputDeviceDescriptors(rendererInfo, false, uid); return SUCCESS; } @@ -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 0d0556d54fa59fdee50bbc5060afdbada5a9dbeb..891771fe97204fa9ac67f6ea3a284b4f10f9e4d3 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,16 @@ 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); + return AudioPolicyManager::GetInstance().SelectOutputDevice(audioRendererFilter, audioDeviceDescriptors, + audioDeviceSelectMode); } int32_t AudioSystemManager::SelectInputDevice(sptr audioCapturerFilter,