From 8f9dabedc1364543515d394a15036651c3983ade Mon Sep 17 00:00:00 2001 From: "hezhiqiang19@huawei.com" Date: Tue, 20 Aug 2024 15:34:21 +0000 Subject: [PATCH] ndk for manager device info Signed-off-by: hezhiqiang19@huawei.com Change-Id: If5cb7f024dd42605fac8b80db6bb2e16b859dd12 --- .../napi_audio_routing_manager.cpp | 9 - frameworks/native/ohaudio/BUILD.gn | 1 + frameworks/native/ohaudio/OHAudioManager.cpp | 82 +++++++ frameworks/native/ohaudio/OHAudioManager.h | 38 ++++ .../native/ohaudio/OHAudioRoutingManager.cpp | 208 +++++++++++++++--- .../native/ohaudio/OHAudioRoutingManager.h | 6 + .../c/audio_manager/native_audio_manager.h | 83 +++++++ .../native_audio_routing_manager.h | 109 ++++++++- .../kits/c/common/native_audio_common.h | 54 ++++- .../kits/c/common/native_audio_device_base.h | 49 +++++ .../server/src/audio_policy_server.cpp | 9 +- .../src/service/audio_policy_service.cpp | 7 +- .../client/src/audio_routing_manager.cpp | 2 - 13 files changed, 593 insertions(+), 64 deletions(-) create mode 100644 frameworks/native/ohaudio/OHAudioManager.cpp create mode 100644 frameworks/native/ohaudio/OHAudioManager.h create mode 100644 interfaces/kits/c/audio_manager/native_audio_manager.h diff --git a/frameworks/js/napi/audiomanager/napi_audio_routing_manager.cpp b/frameworks/js/napi/audiomanager/napi_audio_routing_manager.cpp index 15f1620a70..535cc45340 100644 --- a/frameworks/js/napi/audiomanager/napi_audio_routing_manager.cpp +++ b/frameworks/js/napi/audiomanager/napi_audio_routing_manager.cpp @@ -826,11 +826,6 @@ napi_value NapiAudioRoutingManager::GetAvailableMicrophones(napi_env env, napi_c napi_value NapiAudioRoutingManager::GetAvailableDevices(napi_env env, napi_callback_info info) { - AUDIO_INFO_LOG("GetAvailableDevices"); - - CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySelfPermission(), - NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED), "No system permission"); - napi_value result = nullptr; size_t argc = ARGS_ONE; napi_value argv[ARGS_ONE] = {}; @@ -888,8 +883,6 @@ napi_value NapiAudioRoutingManager::RegisterCallback(napi_env env, napi_value js } else if (!cbName.compare(PREFERRED_INPUT_DEVICE_CALLBACK_NAME)) { RegisterPreferredInputDeviceChangeCallback(env, argc, args, cbName, napiRoutingMgr); } else if (!cbName.compare(AVAILABLE_DEVICE_CHANGE_CALLBACK_NAME)) { - CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySelfPermission(), - NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED), "No system permission"); RegisterAvaiableDeviceChangeCallback(env, argc, args, cbName, napiRoutingMgr); } else { AUDIO_ERR_LOG("NapiAudioRoutingManager::No such supported"); @@ -1105,8 +1098,6 @@ napi_value NapiAudioRoutingManager::UnregisterCallback(napi_env env, napi_value } else if (!callbackName.compare(PREFERRED_INPUT_DEVICE_CALLBACK_NAME)) { UnregisterPreferredInputDeviceChangeCallback(env, callback, napiRoutingMgr); } else if (!callbackName.compare(AVAILABLE_DEVICE_CHANGE_CALLBACK_NAME)) { - CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifySelfPermission(), - NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED), "No system permission"); UnregisterAvailableDeviceChangeCallback(env, callback, napiRoutingMgr); } else { AUDIO_ERR_LOG("off no such supported"); diff --git a/frameworks/native/ohaudio/BUILD.gn b/frameworks/native/ohaudio/BUILD.gn index 25448ed13c..75b840b49c 100644 --- a/frameworks/native/ohaudio/BUILD.gn +++ b/frameworks/native/ohaudio/BUILD.gn @@ -53,6 +53,7 @@ ohos_shared_library("ohaudio") { sources = [ "./OHAudioCapturer.cpp", "./OHAudioDeviceDescriptor.cpp", + "./OHAudioManager.cpp", "./OHAudioRenderer.cpp", "./OHAudioRoutingManager.cpp", "./OHAudioSessionManager.cpp", diff --git a/frameworks/native/ohaudio/OHAudioManager.cpp b/frameworks/native/ohaudio/OHAudioManager.cpp new file mode 100644 index 0000000000..6294bace6d --- /dev/null +++ b/frameworks/native/ohaudio/OHAudioManager.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "OHAudioManager.h" + +#include + +#include "audio_info.h" +#include "audio_common_log.h" +#include "audio_system_manager.h" + +namespace { +// should be same with OH_AudioScene in native_audio_common.h +const std::set INVALID_AUDIO_SCENES = { + OHOS::AudioStandard::AUDIO_SCENE_DEFAULT, + OHOS::AudioStandard::AUDIO_SCENE_RINGING, + OHOS::AudioStandard::AUDIO_SCENE_PHONE_CALL, + OHOS::AudioStandard::AUDIO_SCENE_PHONE_CHAT, + OHOS::AudioStandard::AUDIO_SCENE_VOICE_RINGING +}; +} +using OHOS::AudioStandard::OHAudioManager; +static OHOS::AudioStandard::OHAudioManager *convertManager(OH_AudioManager *audioManager) +{ + return (OHAudioManager*) audioManager; +} + +OH_AudioCommon_Result OH_GetAudioManager(OH_AudioManager **audioManager) +{ + if (audioManager == nullptr) { + AUDIO_ERR_LOG("invalid OH_AudioManager"); + return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM; + } + OHAudioManager *manager = OHAudioManager::GetInstance(); + *audioManager = (OH_AudioManager *)manager; + return AUDIOCOMMON_RESULT_SUCCESS; +} + +OH_AudioCommon_Result OH_GetAudioScene(OH_AudioManager* manager, OH_AudioScene *scene) +{ + if (manager == nullptr || scene == nullptr) { + AUDIO_ERR_LOG("invalid OH_AudioManager or scene"); + return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM; + } + *scene = static_cast(convertManager(manager)->GetAudioScene()); + return AUDIOCOMMON_RESULT_SUCCESS; +} + +namespace OHOS { +namespace AudioStandard { +OHAudioManager *OHAudioManager::GetInstance() +{ + static OHAudioManager manager; + return &manager; +} + +AudioScene OHAudioManager::GetAudioScene() +{ + AudioScene scene = AudioSystemManager::GetInstance()->GetAudioScene(); + if (!INVALID_AUDIO_SCENES.count(scene)) { + AUDIO_WARNING_LOG("Get scene:%{public}d that is not defined, return defalut!", scene); + return AUDIO_SCENE_DEFAULT; + } + if (scene == AUDIO_SCENE_VOICE_RINGING) { + return AUDIO_SCENE_RINGING; + } + return scene; +} +} // namespace AudioStandard +} // namespace OHOS diff --git a/frameworks/native/ohaudio/OHAudioManager.h b/frameworks/native/ohaudio/OHAudioManager.h new file mode 100644 index 0000000000..096aeda613 --- /dev/null +++ b/frameworks/native/ohaudio/OHAudioManager.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OH_AUDIO_MANAGER_H +#define OH_AUDIO_MANAGER_H + +#include "audio_info.h" +#include "native_audio_common.h" +#include "native_audio_manager.h" + +namespace OHOS { +namespace AudioStandard { + +class OHAudioManager { +public: + ~OHAudioManager() {}; + + static OHAudioManager *GetInstance(); + AudioScene GetAudioScene(); +private: + OHAudioManager() {}; +}; + +} // namespace AudioStandard +} // namespace OHOS +#endif // OH_AUDIO_MANAGER_H \ No newline at end of file diff --git a/frameworks/native/ohaudio/OHAudioRoutingManager.cpp b/frameworks/native/ohaudio/OHAudioRoutingManager.cpp index be3b3fbdf6..7c71f81d70 100644 --- a/frameworks/native/ohaudio/OHAudioRoutingManager.cpp +++ b/frameworks/native/ohaudio/OHAudioRoutingManager.cpp @@ -15,10 +15,53 @@ #include "OHAudioRoutingManager.h" +#include + +#include "audio_errors.h" +#include "audio_routing_manager.h" + +namespace { +const size_t MAX_VALID_SIZE = 128; // MAX AudioDevice size. +const std::set VALID_OH_AUDIO_DEVICE_UASGES = { + AUDIO_DEVICE_USAGE_MEDIA_OUTPUT, + AUDIO_DEVICE_USAGE_MEDIA_INPUT, + AUDIO_DEVICE_USAGE_MEDIA_ALL, + AUDIO_DEVICE_USAGE_CALL_OUTPUT, + AUDIO_DEVICE_USAGE_CALL_INPUT, + AUDIO_DEVICE_USAGE_CALL_ALL +}; +const std::set VALID_OH_STREAM_USAGES = { + AUDIOSTREAM_USAGE_UNKNOWN, + AUDIOSTREAM_USAGE_MUSIC, + AUDIOSTREAM_USAGE_VOICE_COMMUNICATION, + AUDIOSTREAM_USAGE_VOICE_ASSISTANT, + AUDIOSTREAM_USAGE_ALARM, + AUDIOSTREAM_USAGE_VOICE_MESSAGE, + AUDIOSTREAM_USAGE_RINGTONE, + AUDIOSTREAM_USAGE_NOTIFICATION, + AUDIOSTREAM_USAGE_ACCESSIBILITY, + AUDIOSTREAM_USAGE_MOVIE, + AUDIOSTREAM_USAGE_GAME, + AUDIOSTREAM_USAGE_AUDIOBOOK, + AUDIOSTREAM_USAGE_NAVIGATION, + AUDIOSTREAM_USAGE_VIDEO_COMMUNICATION +}; +const std::set VALID_OH_SOURCE_TYPES = { + AUDIOSTREAM_SOURCE_TYPE_MIC, + AUDIOSTREAM_SOURCE_TYPE_VOICE_RECOGNITION, + AUDIOSTREAM_SOURCE_TYPE_PLAYBACK_CAPTURE, + AUDIOSTREAM_SOURCE_TYPE_VOICE_CALL, + AUDIOSTREAM_SOURCE_TYPE_VOICE_COMMUNICATION +}; +} + using OHOS::AudioStandard::OHAudioRoutingManager; using OHOS::AudioStandard::OHAudioDeviceDescriptor; using OHOS::AudioStandard::AudioRoutingManager; using OHOS::AudioStandard::DeviceFlag; +using OHOS::AudioStandard::AudioDeviceUsage; +using OHOS::AudioStandard::StreamUsage; +using OHOS::AudioStandard::SourceType; static OHOS::AudioStandard::OHAudioRoutingManager *convertManager(OH_AudioRoutingManager* manager) { @@ -53,6 +96,63 @@ OH_AudioCommon_Result OH_AudioRoutingManager_GetDevices(OH_AudioRoutingManager * return AUDIOCOMMON_RESULT_SUCCESS; } +OH_AudioCommon_Result OH_AudioRoutingManager_GetAvailableDevices(OH_AudioRoutingManager *audioRoutingManager, + OH_AudioDevice_Usage deviceUsage, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray) +{ + if (audioRoutingManager == nullptr || !VALID_OH_AUDIO_DEVICE_UASGES.count(deviceUsage) || + audioDeviceDescriptorArray == nullptr) { + AUDIO_ERR_LOG("Invalid params!"); + } + OHAudioRoutingManager* ohAudioRoutingManager = convertManager(audioRoutingManager); + CHECK_AND_RETURN_RET_LOG(ohAudioRoutingManager != nullptr, + AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioRoutingManager is nullptr"); + + AudioDeviceUsage usage = static_cast(deviceUsage); + *audioDeviceDescriptorArray = ohAudioRoutingManager->GetAvailableDevices(usage); + CHECK_AND_RETURN_RET_LOG(*audioDeviceDescriptorArray != nullptr, + AUDIOCOMMON_RESULT_ERROR_NO_MEMORY, "*audioDeviceDescriptorArray is nullptr"); + + return AUDIOCOMMON_RESULT_SUCCESS; +} + +OH_AudioCommon_Result OH_AudioRoutingManager_GetPreferredOutputDevice(OH_AudioRoutingManager *audioRoutingManager, + OH_AudioStream_Usage streamUsage, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray) +{ + if (audioRoutingManager == nullptr || !VALID_OH_STREAM_USAGES.count(streamUsage) || + audioDeviceDescriptorArray == nullptr) { + AUDIO_ERR_LOG("Invalid params!"); + } + OHAudioRoutingManager* ohAudioRoutingManager = convertManager(audioRoutingManager); + CHECK_AND_RETURN_RET_LOG(ohAudioRoutingManager != nullptr, + AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioRoutingManager is nullptr"); + + StreamUsage usage = static_cast(streamUsage); + *audioDeviceDescriptorArray = ohAudioRoutingManager->GetPreferredOutputDevice(usage); + CHECK_AND_RETURN_RET_LOG(*audioDeviceDescriptorArray != nullptr, + AUDIOCOMMON_RESULT_ERROR_NO_MEMORY, "*audioDeviceDescriptorArray is nullptr"); + + return AUDIOCOMMON_RESULT_SUCCESS; +} + +OH_AudioCommon_Result OH_AudioRoutingManager_GetPreferredInputDevice(OH_AudioRoutingManager *audioRoutingManager, + OH_AudioStream_SourceType sourceType, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray) +{ + if (audioRoutingManager == nullptr || !VALID_OH_SOURCE_TYPES.count(sourceType) || + audioDeviceDescriptorArray == nullptr) { + AUDIO_ERR_LOG("Invalid params!"); + } + OHAudioRoutingManager* ohAudioRoutingManager = convertManager(audioRoutingManager); + CHECK_AND_RETURN_RET_LOG(ohAudioRoutingManager != nullptr, + AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioRoutingManager is nullptr"); + + SourceType type = static_cast(sourceType); + *audioDeviceDescriptorArray = ohAudioRoutingManager->GetPreferredInputDevice(type); + CHECK_AND_RETURN_RET_LOG(*audioDeviceDescriptorArray != nullptr, + AUDIOCOMMON_RESULT_ERROR_NO_MEMORY, "*audioDeviceDescriptorArray is nullptr"); + + return AUDIOCOMMON_RESULT_SUCCESS; +} + OH_AudioCommon_Result OH_AudioRoutingManager_RegisterDeviceChangeCallback( OH_AudioRoutingManager *audioRoutingManager, OH_AudioDevice_Flag deviceFlag, OH_AudioRoutingManager_OnDeviceChangedCallback callback) @@ -136,6 +236,45 @@ OHAudioRoutingManager::~OHAudioRoutingManager() AUDIO_INFO_LOG("OHAudioRoutingManager destroyed!"); } +OH_AudioDeviceDescriptorArray *OHAudioRoutingManager::ConvertDesc(std::vector> &desc) +{ + size_t size = desc.size(); + if (size == 0 || size >= MAX_VALID_SIZE) { + AUDIO_ERR_LOG("failed to convert device info, size is %{public}zu", size); + return nullptr; + } + + OH_AudioDeviceDescriptorArray *audioDeviceDescriptorArray = + (OH_AudioDeviceDescriptorArray *)malloc(sizeof(OH_AudioDeviceDescriptorArray)); + + if (audioDeviceDescriptorArray == nullptr) { + AUDIO_ERR_LOG("failed to malloc."); + return nullptr; + } + audioDeviceDescriptorArray->size = 0; + audioDeviceDescriptorArray->descriptors = + (OH_AudioDeviceDescriptor **)malloc(sizeof(OH_AudioDeviceDescriptor *) * size); + if (audioDeviceDescriptorArray->descriptors == nullptr) { + free(audioDeviceDescriptorArray); + audioDeviceDescriptorArray = nullptr; + AUDIO_ERR_LOG("failed to malloc descriptors."); + return nullptr; + } + + uint32_t index = 0; + for (auto deviceDescriptor : desc) { + audioDeviceDescriptorArray->descriptors[index] = + (OH_AudioDeviceDescriptor *)(new OHAudioDeviceDescriptor(deviceDescriptor)); + if (audioDeviceDescriptorArray->descriptors[index] == nullptr) { + DestroyAudioDeviceDescriptor(audioDeviceDescriptorArray); + return nullptr; + } + index++; + audioDeviceDescriptorArray->size = index; + } + return audioDeviceDescriptorArray; +} + OH_AudioDeviceDescriptorArray* OHAudioRoutingManager::GetDevices(DeviceFlag deviceFlag) { CHECK_AND_RETURN_RET_LOG(audioSystemManager_ != nullptr, @@ -146,32 +285,51 @@ OH_AudioDeviceDescriptorArray* OHAudioRoutingManager::GetDevices(DeviceFlag devi AUDIO_ERR_LOG("audioDeviceDescriptors is null"); return nullptr; } - OH_AudioDeviceDescriptorArray *audioDeviceDescriptorArray = - (OH_AudioDeviceDescriptorArray *)malloc(sizeof(OH_AudioDeviceDescriptorArray)); - - if (audioDeviceDescriptorArray) { - audioDeviceDescriptorArray->descriptors = - (OH_AudioDeviceDescriptor**)malloc(sizeof(OH_AudioDeviceDescriptor*) * size); - if (audioDeviceDescriptorArray->descriptors == nullptr) { - free(audioDeviceDescriptorArray); - audioDeviceDescriptorArray = nullptr; - AUDIO_ERR_LOG("failed to malloc descriptors."); - return nullptr; - } - audioDeviceDescriptorArray->size = size; - uint32_t index = 0; - for (auto deviceDescriptor : audioDeviceDescriptors) { - audioDeviceDescriptorArray->descriptors[index] = - (OH_AudioDeviceDescriptor *)(new OHAudioDeviceDescriptor(deviceDescriptor)); - if (audioDeviceDescriptorArray->descriptors[index] == nullptr) { - DestroyAudioDeviceDescriptor(audioDeviceDescriptorArray); - return nullptr; - } - index++; - } - return audioDeviceDescriptorArray; + return ConvertDesc(audioDeviceDescriptors); +} + +OH_AudioDeviceDescriptorArray *OHAudioRoutingManager::GetAvailableDevices(AudioDeviceUsage deviceUsage) +{ + std::vector> tempDesc = + AudioRoutingManager::GetInstance()->GetAvailableDevices(deviceUsage); + if (tempDesc.size() == 0) { + AUDIO_ERR_LOG("get no device"); + return nullptr; + } + std::vector> altaDesc = {}; + for (const auto &availableDesc : tempDesc) { + sptr dec = new(std::nothrow) AudioDeviceDescriptor(*availableDesc); + altaDesc.push_back(dec); + } + return ConvertDesc(altaDesc); +} + +OH_AudioDeviceDescriptorArray *OHAudioRoutingManager::GetPreferredOutputDevice(StreamUsage streamUsage) +{ + AudioRendererInfo rendererInfo = {}; + rendererInfo.streamUsage = streamUsage; + std::vector> desc = {}; + + int32_t ret = AudioRoutingManager::GetInstance()->GetPreferredOutputDeviceForRendererInfo(rendererInfo, desc); + if (ret != SUCCESS) { + AUDIO_ERR_LOG("call failed!"); + return nullptr; + } + return ConvertDesc(desc); +} + +OH_AudioDeviceDescriptorArray *OHAudioRoutingManager::GetPreferredInputDevice(SourceType sourceType) +{ + AudioCapturerInfo capturerInfo = {}; + capturerInfo.sourceType = sourceType; + std::vector> desc = {}; + + int32_t ret = AudioRoutingManager::GetInstance()->GetPreferredInputDeviceForCapturerInfo(capturerInfo, desc); + if (ret != SUCCESS) { + AUDIO_ERR_LOG("call failed!"); + return nullptr; } - return nullptr; + return ConvertDesc(desc); } OH_AudioCommon_Result OHAudioRoutingManager::SetDeviceChangeCallback(const DeviceFlag deviceFlag, diff --git a/frameworks/native/ohaudio/OHAudioRoutingManager.h b/frameworks/native/ohaudio/OHAudioRoutingManager.h index b545d2b266..dfa3d5e18e 100644 --- a/frameworks/native/ohaudio/OHAudioRoutingManager.h +++ b/frameworks/native/ohaudio/OHAudioRoutingManager.h @@ -64,6 +64,12 @@ public: return ohAudioRoutingManager_; } OH_AudioDeviceDescriptorArray* GetDevices(DeviceFlag deviceFlag); + + OH_AudioDeviceDescriptorArray *ConvertDesc(std::vector> &desc); + OH_AudioDeviceDescriptorArray *GetAvailableDevices(AudioDeviceUsage deviceUsage); + OH_AudioDeviceDescriptorArray *GetPreferredOutputDevice(StreamUsage streamUsage); + OH_AudioDeviceDescriptorArray *GetPreferredInputDevice(SourceType sourceType); + OH_AudioCommon_Result SetDeviceChangeCallback(const DeviceFlag flag, OH_AudioRoutingManager_OnDeviceChangedCallback callback); OH_AudioCommon_Result UnsetDeviceChangeCallback(DeviceFlag flag, diff --git a/interfaces/kits/c/audio_manager/native_audio_manager.h b/interfaces/kits/c/audio_manager/native_audio_manager.h new file mode 100644 index 0000000000..b25ae98d8c --- /dev/null +++ b/interfaces/kits/c/audio_manager/native_audio_manager.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup OHAudio + * @{ + * + * @brief Provide the definition of the C interface for the audio module. + * + * @syscap SystemCapability.Multimedia.Audio.Core + * + * @since 12 + * @version 1.0 + */ + +/** + * @file native_audio_manager.h + * + * @brief Declare audio manager related interfaces. + * + * @library libohaudio.so + * @syscap SystemCapability.Multimedia.Audio.Core + * @since 12 + * @version 1.0 + */ +#ifndef NATIVE_AUDIO_MANAGER_H +#define NATIVE_AUDIO_MANAGER_H + +#include "native_audio_common.h" +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Declare the audio manager. + * The handle of audio manager is used for audio management related functions. + * + * @since 12 + */ +typedef struct OH_AudioManager OH_AudioManager; + +/** + * @brief Get audio manager handle. + * + * @param audioManager the {@link OH_AudioManager} handle received from this function. + * @return Function result code: + * {@link AUDIOCOMMON_RESULT_SUCCESS} If the execution is successful. + * {@link AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM}: + * 1.The param of audioManager is nullptr; + * @since 12 + */ +OH_AudioCommon_Result OH_GetAudioManager(OH_AudioManager **audioManager); + +/** + * @brief Get audio scene. + * @param audioManager the {@link OH_AudioManager} handle received from {@link OH_GetAudioManager}. + * @param scene the {@link OH_AudioScene} pointer to receive the result. + * @return Function result code: + * {@link AUDIOCOMMON_RESULT_SUCCESS} If the execution is successful. + * {@link AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM}: + * 1.The param of audioManager is nullptr; + * 2.The param of scene is nullptr. + * @since 12 + */ +OH_AudioCommon_Result OH_GetAudioScene(OH_AudioManager* manager, OH_AudioScene *scene); + +#ifdef __cplusplus +} +#endif +/** @} */ +#endif // NATIVE_AUDIO_ROUTING_MANAGER_H \ No newline at end of file diff --git a/interfaces/kits/c/audio_manager/native_audio_routing_manager.h b/interfaces/kits/c/audio_manager/native_audio_routing_manager.h index e5c9a3938e..a75462df40 100644 --- a/interfaces/kits/c/audio_manager/native_audio_routing_manager.h +++ b/interfaces/kits/c/audio_manager/native_audio_routing_manager.h @@ -36,6 +36,7 @@ * * @library libohaudio.so * @syscap SystemCapability.Multimedia.Audio.Core + * @kit AudioKit * @since 12 * @version 1.0 */ @@ -50,7 +51,7 @@ extern "C" { /** * @brief Declaring the audio routing manager. - * The handle of audio routing manager used for routing and device related function. + * The handle of audio routing manager used for routing and device related functions. * * @since 12 */ @@ -65,7 +66,7 @@ typedef struct OH_AudioRoutingManager OH_AudioRoutingManager; * @param audioDeviceDescriptorArray the {@link OH_AudioDeviceDescriptorArray} * pointer variable which will be set the audio device descriptors value. * Do not release the audioDeviceDescriptorArray pointer separately - * instead call {@link OH_AudioRoutingManager_ReleaseDevices} to relase all the DeviceDescriptor array + * instead call {@link OH_AudioRoutingManager_ReleaseDevices} to release the DeviceDescriptor array * when it is no use anymore. * @since 12 */ @@ -80,7 +81,8 @@ typedef int32_t (*OH_AudioRoutingManager_OnDeviceChangedCallback) ( * * @param audioRoutingManager the {@link OH_AudioRoutingManager} * handle returned by {@link OH_AudioManager_GetAudioRoutingManager}. - * @return {@link #AUDIOCOMMON_RESULT_SUCCESS} + * @return Function result code: + * {@link AUDIOCOMMON_RESULT_SUCCESS} If the execution is successful. * @since 12 */ OH_AudioCommon_Result OH_AudioManager_GetAudioRoutingManager(OH_AudioRoutingManager **audioRoutingManager); @@ -95,10 +97,15 @@ OH_AudioCommon_Result OH_AudioManager_GetAudioRoutingManager(OH_AudioRoutingMana * @param audioDeviceDescriptorArray the {@link OH_AudioDeviceDescriptorArray} * pointer variable which will be set the audio device descriptors value * Do not release the audioDeviceDescriptorArray pointer separately - * instead call {@link OH_AudioRoutingManager_ReleaseDevices} to relase all the DeviceDescriptor array + * instead call {@link OH_AudioRoutingManager_ReleaseDevices} to release the DeviceDescriptor array * when it is no use anymore. - * @return {@link #AUDIOCOMMON_RESULT_SUCCESS} or {@link #AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM} - * or {@link #AUDIOCOMMON_RESULT_ERROR_NO_MEMORY}. + * @return Function result code: + * {@link AUDIOCOMMON_RESULT_SUCCESS} If the execution is successful. + * {@link AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM}: + * 1.The param of audioRoutingManager is nullptr; + * 2.The param of deviceFlag invalid; + * 3.The param of audioDeviceDescriptorArray is nullptr. + * {@link AUDIOCOMMON_RESULT_ERROR_NO_MEMORY} The param of audioDeviceDescriptorArray is nullptr. * @since 12 */ OH_AudioCommon_Result OH_AudioRoutingManager_GetDevices( @@ -106,6 +113,75 @@ OH_AudioCommon_Result OH_AudioRoutingManager_GetDevices( OH_AudioDevice_Flag deviceFlag, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray); +/** + * @brief Get available devices by device usage. + * + * @param audioRoutingManager the {@link OH_AudioRoutingManager} handle returned + * by {@link OH_AudioManager_GetAudioRoutingManager}. + * @param deviceUsage the device usage. + * @param audioDeviceDescriptorArray the {@link OH_AudioDeviceDescriptorArray} + * pointer variable which will be set the audio device descriptors value + * Do not release the audioDeviceDescriptorArray pointer separately + * instead call {@link OH_AudioRoutingManager_ReleaseDevices} to release the DeviceDescriptor array + * when it is no use anymore. + * @return Function result code: + * {@link AUDIOCOMMON_RESULT_SUCCESS} If the execution is successful. + * {@link AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM}: + * 1.The param of audioRoutingManager is nullptr; + * 2.The param of deviceUsage invalid; + * 3.The param of audioDeviceDescriptorArray is nullptr. + * {@link AUDIOCOMMON_RESULT_ERROR_NO_MEMORY} No memory error. + * @since 12 + */ +OH_AudioCommon_Result OH_AudioRoutingManager_GetAvailableDevices( + OH_AudioRoutingManager *audioRoutingManager, + OH_AudioDevice_Usage deviceUsage, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray); + +/** + * @brief Get preferred ouput devices by audio usage. + * + * @param audioRoutingManager the {@link OH_AudioRoutingManager} handle returned + * by {@link OH_AudioManager_GetAudioRoutingManager}. + * @param streamUsage the audio stream usage. + * @param audioDeviceDescriptorArray the {@link OH_AudioDeviceDescriptorArray} + * pointer variable which will be set the audio device descriptors value + * Do not release the audioDeviceDescriptorArray pointer separately + * instead call {@link OH_AudioRoutingManager_ReleaseDevices} to release the DeviceDescriptor array + * when it is no use anymore. + * @return Function result code: + * {@link AUDIOCOMMON_RESULT_SUCCESS} If the execution is successful. + * {@link AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM}: + * 1.The param of audioRoutingManager is nullptr; + * 2.The param of streamUsage invalid; + * 3.The param of audioDeviceDescriptorArray is nullptr. + * {@link AUDIOCOMMON_RESULT_ERROR_NO_MEMORY} No memory error. + * @since 12 + */ +OH_AudioCommon_Result OH_AudioRoutingManager_GetPreferredOutputDevice( + OH_AudioRoutingManager *audioRoutingManager, + OH_AudioStream_Usage streamUsage, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray); + +/** + * @brief Get preferred input devices by audio source type. + * @param audioRoutingManager the {@link OH_AudioRoutingManager} handle returned + * by {@link OH_AudioManager_GetAudioRoutingManager}. + * @param sourceType the audio source type. + * @param audioDeviceDescriptorArray the {@link OH_AudioDeviceDescriptorArray} + * pointer variable which will be set the audio device descriptors value + * Do not release the audioDeviceDescriptorArray pointer separately + * instead call {@link OH_AudioRoutingManager_ReleaseDevices} to release the DeviceDescriptor array + * when it is no use anymore. + * @return Function result code: + * {@link AUDIOCOMMON_RESULT_SUCCESS} If the execution is successful. + * {@link AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM}: + * 1.The param of audioRoutingManager is nullptr; + * 2.The param of sourceType invalid; + * 3.The param of audioDeviceDescriptorArray is nullptr. + * {@link AUDIOCOMMON_RESULT_ERROR_NO_MEMORY} No memory error. + * @since 12 + */ +OH_AudioCommon_Result OH_AudioRoutingManager_GetPreferredInputDevice(OH_AudioRoutingManager *audioRoutingManager, + OH_AudioStream_SourceType sourceType, OH_AudioDeviceDescriptorArray **audioDeviceDescriptorArray); /** * @brief Register the device change callback of the audio routing manager. * @@ -114,7 +190,12 @@ OH_AudioCommon_Result OH_AudioRoutingManager_GetDevices( * @param deviceFlag the {@link OH_AudioDevice_DeviceFlag} which is used to register callback. * @param callback the {@link OH_AudioRoutingManager_OnDeviceChangedCallback} * Callback function which will be called when devices changed. - * @return {@link #AUDIOCOMMON_RESULT_SUCCESS} or {@link #AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM}. + * @return Function result code: + * {@link AUDIOCOMMON_RESULT_SUCCESS} If the execution is successful. + * {@link AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM}: + * 1.The param of audioRoutingManager is nullptr; + * 2.The param of deviceFlag invalid; + * 3.The param of callback is nullptr. * @since 12 */ OH_AudioCommon_Result OH_AudioRoutingManager_RegisterDeviceChangeCallback( @@ -128,7 +209,11 @@ OH_AudioCommon_Result OH_AudioRoutingManager_RegisterDeviceChangeCallback( * handle returned by {@link OH_AudioManager_GetAudioRoutingManager}. * @param callback the {@link OH_AudioRoutingManager_OnDeviceChangedCallback} * Callback function which will be called when devices changed. - * @return {@link #AUDIOCOMMON_RESULT_SUCCESS} or {@link #AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM}. + * @return Function result code: + * {@link AUDIOCOMMON_RESULT_SUCCESS} If the execution is successful. + * {@link AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM}: + * 1.The param of audioRoutingManager is nullptr; + * 2.The param of callback is nullptr. * @since 12 */ OH_AudioCommon_Result OH_AudioRoutingManager_UnregisterDeviceChangeCallback( @@ -142,7 +227,11 @@ OH_AudioCommon_Result OH_AudioRoutingManager_UnregisterDeviceChangeCallback( * handle returned by {@link OH_AudioManager_GetAudioRoutingManager}. * @param audioDeviceDescriptorArray Audio device descriptors should be released. * and get from {@link OH_AudioRoutingManager_GetDevices} - * @return {@link #AUDIOCOMMON_RESULT_SUCCESS} or {@link #AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM}. + * @return Function result code: + * {@link AUDIOCOMMON_RESULT_SUCCESS} If the execution is successful. + * {@link AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM}: + * 1.The param of audioRoutingManager is nullptr; + * 2.The param of audioDeviceDescriptorArray is nullptr. * @since 12 */ OH_AudioCommon_Result OH_AudioRoutingManager_ReleaseDevices( @@ -152,4 +241,4 @@ OH_AudioCommon_Result OH_AudioRoutingManager_ReleaseDevices( } #endif /** @} */ -#endif // NATIVE_AUDIO_ROUTING_MANAGER_H \ No newline at end of file +#endif // NATIVE_AUDIO_ROUTING_MANAGER_H diff --git a/interfaces/kits/c/common/native_audio_common.h b/interfaces/kits/c/common/native_audio_common.h index 19eb382f6f..6a50df302a 100644 --- a/interfaces/kits/c/common/native_audio_common.h +++ b/interfaces/kits/c/common/native_audio_common.h @@ -34,6 +34,7 @@ * * @library libohaudio.so * @syscap SystemCapability.Multimedia.Audio.Core + * @kit AudioKit * @since 12 * @version 1.0 */ @@ -54,48 +55,83 @@ extern "C" { */ typedef enum { /** - * @brief The call was successful. + * @error The call was successful. */ AUDIOCOMMON_RESULT_SUCCESS = 0, /** - * @brief This means that the input parameter is invalid. + * @error This means that the input parameter is invalid. */ AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM = 6800101, /** - * @brief This means there is no memory left. + * @error This means there is no memory left. */ AUDIOCOMMON_RESULT_ERROR_NO_MEMORY = 6800102, /** - * @brief Execution status exception. + * @error Execution status exception. */ AUDIOCOMMON_RESULT_ERROR_ILLEGAL_STATE = 6800103, /** - * @brief This means the operation is unsupported. + * @error This means the operation is unsupported. */ AUDIOCOMMON_RESULT_ERROR_UNSUPPORTED = 6800104, /** - * @brief This means the operation is timeout. + * @error This means the operation is timeout. */ AUDIOCOMMON_RESULT_ERROR_TIMEOUT = 6800105, /** - * @brief This means reached stream limit. + * @error This means reached stream limit. */ AUDIOCOMMON_RESULT_ERROR_STREAM_LIMIT = 6800201, /** - * @brief An system error has occurred. + * @error An system error has occurred. */ AUDIOCOMMON_RESULT_ERROR_SYSTEM = 6800301, } OH_AudioCommon_Result; +/** + * @brief Defines the audio scene. + * + * @since 12 + */ +typedef enum { + /** + * Default audio scene. + * + * @since 12 + */ + AUDIO_SCENE_DEFAULT = 0, + + /** + * Ringing scene. + * + * @since 12 + */ + AUDIO_SCENE_RINGING = 1, + + /** + * Phone call scene. + * + * @since 12 + */ + AUDIO_SCENE_PHONE_CALL = 2, + + /** + * Voice chat scene. + * + * @since 12 + */ + AUDIO_SCENE_VOICE_CHAT = 3, +} OH_AudioScene; + #ifdef __cplusplus } #endif /** @} */ -#endif // NATIVE_AUDIO_COMMON_H \ No newline at end of file +#endif // NATIVE_AUDIO_COMMON_H diff --git a/interfaces/kits/c/common/native_audio_device_base.h b/interfaces/kits/c/common/native_audio_device_base.h index f109cb5b38..fc7436a39d 100644 --- a/interfaces/kits/c/common/native_audio_device_base.h +++ b/interfaces/kits/c/common/native_audio_device_base.h @@ -175,6 +175,55 @@ typedef enum { AUDIO_DEVICE_FLAG_ALL = 3, } OH_AudioDevice_Flag; +/** + * @brief Defines the audio device usage. + * + * @since 12 + */ +typedef enum { + /** + * @brief Device used for media ouput. + * + * @since 12 + */ + AUDIO_DEVICE_USAGE_MEDIA_OUTPUT = 1, + + /** + * @brief Device used for media input. + * + * @since 12 + */ + AUDIO_DEVICE_USAGE_MEDIA_INPUT = 2, + + /** + * @brief Device used for media, including input and output. + * + * @since 12 + */ + AUDIO_DEVICE_USAGE_MEDIA_ALL = 3, + + /** + * @brief Device used for call output. + * + * @since 12 + */ + AUDIO_DEVICE_USAGE_CALL_OUTPUT = 4, + + /** + * @brief Device used for call input. + * + * @since 12 + */ + AUDIO_DEVICE_USAGE_CALL_INPUT = 8, + + /** + * @brief Device used for call, including input and output. + * + * @since 12 + */ + AUDIO_DEVICE_USAGE_CALL_ALL = 12, +} OH_AudioDevice_Usage; + /** * @brief Declaring the audio device descriptor. * The instance is used to get more audio device detail attributes. diff --git a/services/audio_policy/server/src/audio_policy_server.cpp b/services/audio_policy/server/src/audio_policy_server.cpp index 289d127426..cf19b08eb0 100644 --- a/services/audio_policy/server/src/audio_policy_server.cpp +++ b/services/audio_policy/server/src/audio_policy_server.cpp @@ -2175,7 +2175,6 @@ int32_t AudioPolicyServer::SetAvailableDeviceChangeCallback(const int32_t /*clie { CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_INVALID_PARAM, "SetAvailableDeviceChangeCallback set listener object is nullptr"); - bool hasSystemPermission = PermissionUtil::VerifySystemPermission(); switch (usage) { case MEDIA_OUTPUT_DEVICES: case MEDIA_INPUT_DEVICES: @@ -2183,13 +2182,11 @@ int32_t AudioPolicyServer::SetAvailableDeviceChangeCallback(const int32_t /*clie case CALL_OUTPUT_DEVICES: case CALL_INPUT_DEVICES: case ALL_CALL_DEVICES: - if (!hasSystemPermission) { - AUDIO_ERR_LOG("SetAvailableDeviceChangeCallback: No system permission"); - return ERR_PERMISSION_DENIED; - } + case D_ALL_DEVICES: break; default: - break; + AUDIO_ERR_LOG("Invalid AudioDeviceUsage"); + return ERR_INVALID_PARAM; } int32_t clientPid = IPCSkeleton::GetCallingPid(); diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index 2fb9cb65bb..7e232d5536 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -1922,7 +1922,8 @@ std::vector> AudioPolicyService::GetPreferredOutputD { std::vector> deviceList = {}; if (rendererInfo.streamUsage <= STREAM_USAGE_UNKNOWN || - rendererInfo.streamUsage > STREAM_USAGE_VOICE_MODEM_COMMUNICATION) { + rendererInfo.streamUsage > STREAM_USAGE_MAX) { + AUDIO_WARNING_LOG("Invalid usage[%{public}d], return current device.", rendererInfo.streamUsage); sptr devDesc = new(std::nothrow) AudioDeviceDescriptor(currentActiveDevice_); deviceList.push_back(devDesc); return deviceList; @@ -3361,8 +3362,8 @@ AudioScene AudioPolicyService::GetAudioScene(bool hasSystemPermission) const AUDIO_DEBUG_LOG("return value: %{public}d", audioScene_); if (!hasSystemPermission) { switch (audioScene_) { - case AUDIO_SCENE_RINGING: - case AUDIO_SCENE_PHONE_CALL: + case AUDIO_SCENE_CALL_START: + case AUDIO_SCENE_CALL_END: return AUDIO_SCENE_DEFAULT; default: break; diff --git a/services/audio_service/client/src/audio_routing_manager.cpp b/services/audio_service/client/src/audio_routing_manager.cpp index 1252840023..e58b322166 100644 --- a/services/audio_service/client/src/audio_routing_manager.cpp +++ b/services/audio_service/client/src/audio_routing_manager.cpp @@ -60,8 +60,6 @@ int32_t AudioRoutingManager::GetPreferredOutputDeviceForRendererInfo(AudioRender int32_t AudioRoutingManager::GetPreferredInputDeviceForCapturerInfo(AudioCapturerInfo captureInfo, std::vector> &desc) { - AUDIO_INFO_LOG("Entered %{public}s", __func__); - desc = AudioPolicyManager::GetInstance().GetPreferredInputDeviceDescriptors(captureInfo); return SUCCESS; -- Gitee