From c5b08521e49ea76171e2a82dfd4ec0ddb86bef84 Mon Sep 17 00:00:00 2001 From: Bobie Date: Fri, 25 Aug 2023 17:16:07 +0800 Subject: [PATCH] unify interface with idl file of audio. Signed-off-by: Bobie --- .../audio/v1_0/AudioTypes.idl | 501 ++++++++++++------ .../audio/v1_0/IAudioAdapter.idl | 23 +- .../audio/v1_0/IAudioCallback.idl | 4 +- .../audio/v1_0/IAudioCapture.idl | 10 +- .../audio/v1_0/IAudioManager.idl | 10 +- .../audio/v1_0/IAudioRender.idl | 9 +- .../include/audio_adapter_interface_impl.h | 20 +- .../include/audio_capture_interface_impl.h | 2 +- .../v1_0/src/audio_adapter_interface_impl.cpp | 319 ++++++++--- .../v1_0/src/audio_capture_interface_impl.cpp | 2 +- .../createcapture_fuzzer.cpp | 5 +- .../createrender_fuzzer.cpp | 5 +- .../destroycapture_fuzzer.cpp | 11 +- .../destroyrender_fuzzer.cpp | 11 +- .../src/audio_adapter_interface_impl_test.cpp | 66 +-- .../audio_test_utils/audio_test_utils.h | 6 +- .../include/daudio_adapter_internal.h | 6 +- .../src/daudio_adapter_internal.cpp | 22 +- .../src/daudio_param_callback_internal.cpp | 6 +- .../src/daudio_render_callback_internal.cpp | 6 +- .../audio_adapter_internal_test.cpp | 2 +- .../audio_adapter_internal_test.h | 10 +- 22 files changed, 727 insertions(+), 329 deletions(-) diff --git a/hdf_interfaces/distributed_audio/audio/v1_0/AudioTypes.idl b/hdf_interfaces/distributed_audio/audio/v1_0/AudioTypes.idl index 38cb481b..830a0c5d 100644 --- a/hdf_interfaces/distributed_audio/audio/v1_0/AudioTypes.idl +++ b/hdf_interfaces/distributed_audio/audio/v1_0/AudioTypes.idl @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -13,13 +13,35 @@ * limitations under the License. */ +/** + * @addtogroup HdiAudio + * @{ + * + * @brief Provides unified APIs for audio services to access audio drivers. + * + * An audio service can obtain an audio driver object or agent and then call APIs provided by this object or agent to + * access different types of audio devices based on the audio IDs, thereby obtaining audio information, + * subscribing to or unsubscribing from audio data, enabling or disabling an audio, + * setting the audio data reporting mode, and setting audio options such as the accuracy and measurement range. + * + * @since 4.0 + * @version 1.0 + */ + package ohos.hdi.distributed_audio.audio.v1_0; +/** + * @brief Enumerates the audio port type. + */ enum AudioPortDirection { PORT_OUT = 1, /**< Output port */ PORT_IN = 2, /**< Input port */ PORT_OUT_IN = 3, /**< Input/output port, supporting both audio input and output */ }; + +/** + * @brief Enumerates the pin of an audio adapter. + */ enum AudioPortPin { PIN_NONE = 0, /**< Invalid pin */ PIN_OUT_SPEAKER = 1 << 0, /**< Speaker output pin */ @@ -30,21 +52,32 @@ enum AudioPortPin { PIN_OUT_USB_EXT = 1 << 5, /**< Extended USB output pin*/ PIN_OUT_EARPIECE = 1 << 5 | 1 << 4, /**< Earpiece output pin */ PIN_OUT_BLUETOOTH_SCO = 1 << 6, /**< Bluetooth SCO output pin */ - PIN_OUT_DAUDIO_DEFAULT = 1 << 7, + PIN_OUT_DAUDIO_DEFAULT = 1 << 7, /**< Daudio default output pin */ + PIN_OUT_HEADPHONE = 1 << 8, /**< Wired headphone output pin*/ + PIN_OUT_USB_HEADSET = 1 << 9, /**< ARM USB out pin */ PIN_IN_MIC = 1 << 27 | 1 << 0, /**< Microphone input pin */ PIN_IN_HS_MIC = 1 << 27 | 1 << 1, /**< Wired headset microphone pin for input */ PIN_IN_LINEIN = 1 << 27 | 1 << 2, /**< Line-in pin */ PIN_IN_USB_EXT = 1 << 27 | 1 << 3, /**< Extended USB input pin*/ PIN_IN_BLUETOOTH_SCO_HEADSET = 1 << 27 | 1 << 4, /**< Bluetooth SCO headset input pin */ - PIN_IN_DAUDIO_DEFAULT = 1 << 27 | 1 << 5, + PIN_IN_DAUDIO_DEFAULT = 1 << 27 | 1 << 5, /**< Daudio default input pin */ + PIN_IN_USB_HEADSET = 1 << 27 | 1 << 6, /**< ARM USB input pin */ }; + +/** + * @brief Enumerates the audio category. + */ enum AudioCategory { - AUDIO_IN_MEDIA = 0, - AUDIO_IN_COMMUNICATION = 1, - AUDIO_IN_RINGTONE = 2, - AUDIO_IN_CALL = 3, - AUDIO_MMAP_NOIRQ = 4, + AUDIO_IN_MEDIA = 0, /**< Media */ + AUDIO_IN_COMMUNICATION = 1, /**< Communications */ + AUDIO_IN_RINGTONE = 2, /**< Ringtone */ + AUDIO_IN_CALL = 3, /**< Call */ + AUDIO_MMAP_NOIRQ = 4, /**< Mmap mode */ }; + +/** + * @brief Enumerates the audio format. + */ enum AudioFormat { AUDIO_FORMAT_TYPE_PCM_8_BIT = 1 << 0, /**< 8-bit PCM */ AUDIO_FORMAT_TYPE_PCM_16_BIT = 1 << 1, /**< 16-bit PCM */ @@ -60,12 +93,22 @@ enum AudioFormat { AUDIO_FORMAT_TYPE_G711U = 1 << 25 | 1 << 1, /**< G711u */ AUDIO_FORMAT_TYPE_G726 = 1 << 25 | 1 << 1 | 1 << 0, /**< G726 */ }; + +/** + * @brief Enumerates the audio channel mask. + * + * A mask describes an audio channel position. + */ enum AudioChannelMask { AUDIO_CHANNEL_FRONT_LEFT = 1, /**< Front left channel */ AUDIO_CHANNEL_FRONT_RIGHT = 2, /**< Front right channel */ AUDIO_CHANNEL_MONO = 1, /**< Mono channel */ AUDIO_CHANNEL_STEREO = 3, /**< Stereo channel, consisting of front left and front right channels */ }; + +/** + * @brief Enumerates masks of audio sampling rates. + */ enum AudioSampleRatesMask { AUDIO_SAMPLE_RATE_MASK_8000 = 1 << 0, /**< 8 kHz */ AUDIO_SAMPLE_RATE_MASK_12000 = 1 << 1, /**< 12 kHz */ @@ -80,206 +123,362 @@ enum AudioSampleRatesMask { AUDIO_SAMPLE_RATE_MASK_96000 = 1 << 10, /**< 96 kHz */ AUDIO_SAMPLE_RATE_MASK_INVALID = 4294967295, /**< Invalid sampling rate */ }; + +/** + * @brief Enumerates the passthrough data transmission mode of an audio port. + */ enum AudioPortPassthroughMode { - PORT_PASSTHROUGH_LPCM = 1, - PORT_PASSTHROUGH_RAW = 2, - PORT_PASSTHROUGH_HBR2LBR = 4, - PORT_PASSTHROUGH_AUTO = 8, + PORT_PASSTHROUGH_LPCM = 1 << 0, /**< Stereo PCM */ + PORT_PASSTHROUGH_RAW = 1 << 1, /**< HDMI passthrough */ + PORT_PASSTHROUGH_HBR2LBR = 1 << 2, /**< Blu-ray next-generation audio output with reduced specifications */ + PORT_PASSTHROUGH_AUTO = 1 << 3, /**< Mode automatically matched based on the HDMI EDID */ }; + +/** + * @brief Defines formats of raw audio samples. + */ enum AudioSampleFormat { - AUDIO_SAMPLE_FORMAT_S8 = 0, - AUDIO_SAMPLE_FORMAT_S8P = 1, - AUDIO_SAMPLE_FORMAT_U8 = 2, - AUDIO_SAMPLE_FORMAT_U8P = 3, - AUDIO_SAMPLE_FORMAT_S16 = 4, - AUDIO_SAMPLE_FORMAT_S16P = 5, - AUDIO_SAMPLE_FORMAT_U16 = 6, - AUDIO_SAMPLE_FORMAT_U16P = 7, - AUDIO_SAMPLE_FORMAT_S24 = 8, - AUDIO_SAMPLE_FORMAT_S24P = 9, - AUDIO_SAMPLE_FORMAT_U24 = 10, - AUDIO_SAMPLE_FORMAT_U24P = 11, - AUDIO_SAMPLE_FORMAT_S32 = 12, - AUDIO_SAMPLE_FORMAT_S32P = 13, - AUDIO_SAMPLE_FORMAT_U32 = 14, - AUDIO_SAMPLE_FORMAT_U32P = 15, - AUDIO_SAMPLE_FORMAT_S64 = 16, - AUDIO_SAMPLE_FORMAT_S64P = 17, - AUDIO_SAMPLE_FORMAT_U64 = 18, - AUDIO_SAMPLE_FORMAT_U64P = 19, - AUDIO_SAMPLE_FORMAT_F32 = 20, - AUDIO_SAMPLE_FORMAT_F32P = 21, - AUDIO_SAMPLE_FORMAT_F64 = 22, - AUDIO_SAMPLE_FORMAT_F64P = 23, + /* 8 bits */ + AUDIO_SAMPLE_FORMAT_S8 = 0, /**< signed 8 bit sample */ + AUDIO_SAMPLE_FORMAT_S8P = 1, /**< signed 8 bit planar sample */ + AUDIO_SAMPLE_FORMAT_U8 = 2, /**< unsigned 8 bit sample */ + AUDIO_SAMPLE_FORMAT_U8P = 3, /**< unsigned 8 bit planar sample */ + /* 16 bits */ + AUDIO_SAMPLE_FORMAT_S16 = 4, /**< signed 16 bit sample */ + AUDIO_SAMPLE_FORMAT_S16P = 5, /**< signed 16 bit planar sample */ + AUDIO_SAMPLE_FORMAT_U16 = 6, /**< unsigned 16 bit sample */ + AUDIO_SAMPLE_FORMAT_U16P = 7, /**< unsigned 16 bit planar sample */ + /* 24 bits */ + AUDIO_SAMPLE_FORMAT_S24 = 8, /**< signed 24 bit sample */ + AUDIO_SAMPLE_FORMAT_S24P = 9, /**< signed 24 bit planar sample */ + AUDIO_SAMPLE_FORMAT_U24 = 10, /**< unsigned 24 bit sample */ + AUDIO_SAMPLE_FORMAT_U24P = 11, /**< unsigned 24 bit planar sample */ + /* 32 bits */ + AUDIO_SAMPLE_FORMAT_S32 = 12, /**< signed 32 bit sample */ + AUDIO_SAMPLE_FORMAT_S32P = 13, /**< signed 32 bit planar sample */ + AUDIO_SAMPLE_FORMAT_U32 = 14, /**< unsigned 32 bit sample */ + AUDIO_SAMPLE_FORMAT_U32P = 15, /**< unsigned 32 bit planar sample */ + /* 64 bits */ + AUDIO_SAMPLE_FORMAT_S64 = 16, /**< signed 64 bit sample */ + AUDIO_SAMPLE_FORMAT_S64P = 17, /**< signed 64 bit planar sample */ + AUDIO_SAMPLE_FORMAT_U64 = 18, /**< unsigned 64 bit sample */ + AUDIO_SAMPLE_FORMAT_U64P = 19, /**< unsigned 64 bit planar sample */ + /* float double */ + AUDIO_SAMPLE_FORMAT_F32 = 20, /**< float 32 bit sample */ + AUDIO_SAMPLE_FORMAT_F32P = 21, /**< float 32 bit planar sample */ + AUDIO_SAMPLE_FORMAT_F64 = 22, /**< double 64 bit sample */ + AUDIO_SAMPLE_FORMAT_F64P = 23, /**< double 64 bit planar sample */ }; + +/** + * @brief Enumerates channel modes for audio rendering. + * + * @attention The following modes are set for rendering dual-channel audios. Others are not supported. + */ enum AudioChannelMode { - AUDIO_CHANNEL_NORMAL = 0, - AUDIO_CHANNEL_BOTH_LEFT = 1, - AUDIO_CHANNEL_BOTH_RIGHT = 2, - AUDIO_CHANNEL_EXCHANGE = 3, - AUDIO_CHANNEL_MIX = 4, - AUDIO_CHANNEL_LEFT_MUTE = 5, - AUDIO_CHANNEL_RIGHT_MUTE = 6, - AUDIO_CHANNEL_BOTH_MUTE = 7, + AUDIO_CHANNEL_NORMAL = 0, /**< Normal mode. No processing is required. */ + AUDIO_CHANNEL_BOTH_LEFT = 1, /**< Two left channels */ + AUDIO_CHANNEL_BOTH_RIGHT = 2, /**< Two right channels */ + AUDIO_CHANNEL_EXCHANGE = 3, /**< Data exchange between the left and right channels. The left channel takes the audio + * stream of the right channel, and the right channel takes that of the left channel. + */ + AUDIO_CHANNEL_MIX = 4, /**< Mix of streams of the left and right channels */ + AUDIO_CHANNEL_LEFT_MUTE = 5, /**< Left channel muted. The stream of the right channel is output. */ + AUDIO_CHANNEL_RIGHT_MUTE = 6, /**< Right channel muted. The stream of the left channel is output. */ + AUDIO_CHANNEL_BOTH_MUTE = 7, /**< Both left and right channels are muted */ }; + +/** + * @brief Enumerates the execution types of the DrainBuffer function. + */ enum AudioDrainNotifyType { - AUDIO_DRAIN_NORMAL_MODE = 0, - AUDIO_DRAIN_EARLY_MODE = 1, + AUDIO_DRAIN_NORMAL_MODE = 0, /**< The DrainBuffer function returns after all data finishes playback. */ + AUDIO_DRAIN_EARLY_MODE = 1, /**< The DrainBuffer function returns before all the data of the current track + * finishes playback to reserve time for a smooth track switch by the audio service. + */ + }; + +/** + * @brief Enumerates callback notification events. + */ enum AudioCallbackType { - AUDIO_NONBLOCK_WRITE_COMPELETED = 0, - AUDIO_DRAIN_COMPELETED = 1, - AUDIO_FLUSH_COMPLETED = 2, - AUDIO_RENDER_FULL = 3, - AUDIO_ERROR_OCCUR = 4, + AUDIO_NONBLOCK_WRITE_COMPLETED = 0, /**< The non-block write is complete. */ + AUDIO_DRAIN_COMPLETED = 1, /**< The draining is complete. */ + AUDIO_FLUSH_COMPLETED = 2, /**< The flush is complete. */ + AUDIO_RENDER_FULL = 3, /**< The render buffer is full.*/ + AUDIO_ERROR_OCCUR = 4, /**< An error occurs.*/ }; + +/** + * @brief Describes AudioPortRole. + */ enum AudioPortRole { - AUDIO_PORT_UNASSIGNED_ROLE = 0, - AUDIO_PORT_SOURCE_ROLE = 1, - AUDIO_PORT_SINK_ROLE = 2, + AUDIO_PORT_UNASSIGNED_ROLE = 0, /**< Unassigned port role */ + AUDIO_PORT_SOURCE_ROLE = 1, /**< Assigned source role */ + AUDIO_PORT_SINK_ROLE = 2, /**< Assigned sink role */ }; + +/** + * @brief Describes AudioPortType. + */ enum AudioPortType { - AUDIO_PORT_UNASSIGNED_TYPE = 0, - AUDIO_PORT_DEVICE_TYPE = 1, - AUDIO_PORT_MIX_TYPE = 2, - AUDIO_PORT_SESSION_TYPE = 3, + AUDIO_PORT_UNASSIGNED_TYPE = 0, /**< Unassigned port type */ + AUDIO_PORT_DEVICE_TYPE = 1, /**< Assigned device type */ + AUDIO_PORT_MIX_TYPE = 2, /**< Assigned mix type */ + AUDIO_PORT_SESSION_TYPE = 3, /**< Assigned session type */ }; + +/** + * @brief Describes AudioSessionType. + */ enum AudioSessionType { - AUDIO_OUTPUT_STAGE_SESSION = 0, - AUDIO_OUTPUT_MIX_SESSION = 1, - AUDIO_ALLOCATE_SESSION = 2, - AUDIO_INVALID_SESSION = 3, + AUDIO_OUTPUT_STAGE_SESSION = 0, /**< Assigned output stage session */ + AUDIO_OUTPUT_MIX_SESSION = 1, /**< Assigned output mix session */ + AUDIO_ALLOCATE_SESSION = 2, /**< Assigned allocate session */ + AUDIO_INVALID_SESSION = 3, /**< Assigned invalid session */ }; + +/** + * @brief Describes AudioDeviceType. + */ enum AudioDeviceType { - AUDIO_LINEOUT = 1 << 0, - AUDIO_HEADPHONE = 1 << 1, - AUDIO_HEADSET = 1 << 2, - AUDIO_USB_HEADSET = 1 << 3, - AUDIO_USB_HEADPHONE = 1 << 4, - AUDIO_USBA_HEADSET = 1 << 5, - AUDIO_USBA_HEADPHONE = 1 << 6, - AUDIO_PRIMARY_DEVICE = 1 << 7, - AUDIO_USB_DEVICE = 1 << 8, - AUDIO_A2DP_DEVICE = 1 << 9, - AUDIO_DEVICE_UNKNOWN, + AUDIO_LINEOUT = 1 << 0, /**< Assigned lineout device type */ + AUDIO_HEADPHONE = 1 << 1, /**< Assigned headphone device type */ + AUDIO_HEADSET = 1 << 2, /**< Assigned headset device type */ + AUDIO_USB_HEADSET = 1 << 3, /**< Assigned usb headset device type */ + AUDIO_USB_HEADPHONE = 1 << 4, /**< Assigned usb headphone device type */ + AUDIO_USBA_HEADSET = 1 << 5, /**< Assigned usba headset device type */ + AUDIO_USBA_HEADPHONE = 1 << 6, /**< Assigned usba headphone device type */ + AUDIO_PRIMARY_DEVICE = 1 << 7, /**< Assigned primary device type */ + AUDIO_USB_DEVICE = 1 << 8, /**< Assigned usb device type */ + AUDIO_A2DP_DEVICE = 1 << 9, /**< Assigned a2dp device type */ + AUDIO_HDMI_DEVICE = 1 << 10, /**< Assigned hdmi device type */ + AUDIO_ADAPTER_DEVICE = 1 << 11, /**< Assigned adapter device type */ + AUDIO_DEVICE_UNKNOWN, /**< Assigned unknown device type */ }; + +/** + * @brief Describes AudioEventType. + */ enum AudioEventType { - AUDIO_DEVICE_ADD = 1, - AUDIO_DEVICE_REMOVE = 2, - AUDIO_LOAD_SUCCESS = 3, - AUDIO_LOAD_FAILURE = 4, - AUDIO_UNLOAD = 5, - AUDIO_SERVICE_VALID = 7, - AUDIO_SERVICE_INVALID = 8, - AUDIO_CAPTURE_THRESHOLD = 9, - AUDIO_EVENT_UNKNOWN = 10, + AUDIO_DEVICE_ADD = 1, /**< Assigned add device event type */ + AUDIO_DEVICE_REMOVE = 2, /**< Assigned remove device event type */ + AUDIO_LOAD_SUCCESS = 3, /**< Assigned load sucess event type */ + AUDIO_LOAD_FAILURE = 4, /**< Assigned load failure event type */ + AUDIO_UNLOAD = 5, /**< Assigned unload event type */ + AUDIO_SERVICE_VALID = 7, /**< Assigned valid service event type */ + AUDIO_SERVICE_INVALID = 8, /**< Assigned invalid service event type */ + AUDIO_CAPTURE_THRESHOLD = 9, /**< Assigned threshold capture event type */ + AUDIO_EVENT_UNKNOWN = 10, /**< Assigned unknown event type */ }; + +/** + * @brief Enumerates the restricted key type of the parameters + */ enum AudioExtParamKey { - AUDIO_EXT_PARAM_KEY_NONE = 0, /**< Distributed audio extra param key none */ - AUDIO_EXT_PARAM_KEY_VOLUME = 1, /**< Distributed audio extra param key volume event */ - AUDIO_EXT_PARAM_KEY_FOCUS = 2, /**< Distributed audio extra param key focus event */ - AUDIO_EXT_PARAM_KEY_BUTTON = 3, /**< Distributed audio extra param key media button event */ - AUDIO_EXT_PARAM_KEY_EFFECT = 4, /**< Distributed audio extra param key audio effect event */ - AUDIO_EXT_PARAM_KEY_STATUS = 5, /**< Distributed audio extra param key device status event */ + AUDIO_EXT_PARAM_KEY_NONE = 0, /**< Distributed audio extra param key none */ + AUDIO_EXT_PARAM_KEY_VOLUME = 1, /**< Distributed audio extra param key volume event */ + AUDIO_EXT_PARAM_KEY_FOCUS = 2, /**< Distributed audio extra param key focus event */ + AUDIO_EXT_PARAM_KEY_BUTTON = 3, /**< Distributed audio extra param key media button event */ + AUDIO_EXT_PARAM_KEY_EFFECT = 4, /**< Distributed audio extra param key audio effect event */ + AUDIO_EXT_PARAM_KEY_STATUS = 5, /**< Distributed audio extra param key device status event */ + AUDIO_EXT_PARAM_KEY_USB_DEVICE = 101, /**< Check USB device type ARM or HIFI */ AUDIO_EXT_PARAM_KEY_LOWPOWER = 1000, /**< Low power event type */ }; + +/** + * @brief Describes status of audio deivce.@link enum AudioDeviceType + */ struct AudioDeviceStatus { - unsigned int pnpStatus; + unsigned int pnpStatus; /**< Audio pnp status */ }; + +/** + * @brief Describes the audio scene. + */ union SceneDesc { - unsigned int id; + unsigned int id; /**< Audio scene ID */ }; + +/** + * @brief Defines the audio port. + */ struct AudioPort { - enum AudioPortDirection dir; - unsigned int portId; - String portName; + enum AudioPortDirection dir; /**< Audio port type. For details, see {@link AudioPortDirection} */ + unsigned int portId; /**< Audio port ID */ + String portName; /**< Audio port name */ }; + +/** + * @brief Defines the audio adapter descriptor. + * + * An audio adapter is a set of port drivers for a sound card, including the output and input ports. + * One port corresponds to multiple pins, and each pin belongs to a physical component (such as a + * speaker or a wired headset). + */ struct AudioAdapterDescriptor { - String adapterName; - struct AudioPort[] ports; + String adapterName; /**< Name of the audio adapter */ + struct AudioPort[] ports; /**< List of ports supported by an audio adapter */ }; + +/** + * @brief Defines the audio device descriptor. + */ struct AudioDeviceDescriptor { - unsigned int portId; - enum AudioPortPin pins; - String desc; + unsigned int portId; /**< Audio port ID */ + enum AudioPortPin pins; /**< Pins of audio ports (input and output). For details, see {@link AudioPortPin}. */ + String desc; /**< Audio device name */ }; + +/** + * @brief Defines the audio scene descriptor. + */ struct AudioSceneDescriptor { - union SceneDesc scene; - struct AudioDeviceDescriptor desc; + union SceneDesc scene; /**< Describes the audio scene */ + struct AudioDeviceDescriptor desc; /**< Audio device descriptor */ +}; + +/** + * @brief Defines audio input type. + */ +enum AudioInputType { + AUDIO_INPUT_DEFAULT_TYPE = 0, /**< Assigned default input type */ + AUDIO_INPUT_MIC_TYPE = 1 << 0, /**< Assigned mic input type */ + AUDIO_INPUT_SPEECH_WAKEUP_TYPE = 1 << 1, /**< Assigned speech wakeup input type */ + AUDIO_INPUT_VOICE_COMMUNICATION_TYPE = 1 << 2, /**< Assigned voice communication input type */ + AUDIO_INPUT_VOICE_RECOGNITION_TYPE = 1 << 3, /**< Assigned voice recognition input type */ }; + +/** + * @brief Defines audio sampling attributes. + */ struct AudioSampleAttributes { - enum AudioCategory type; - boolean interleaved; - enum AudioFormat format; - unsigned int sampleRate; - unsigned int channelCount; - unsigned int period; - unsigned int frameSize; - boolean isBigEndian; - boolean isSignedData; - unsigned int startThreshold; - unsigned int stopThreshold; - unsigned int silenceThreshold; - int streamId; + enum AudioCategory type; /**< Audio type. For details, see {@link AudioCategory} */ + boolean interleaved; /**< Interleaving flag of audio data */ + enum AudioFormat format; /**< Audio data format. For details, see {@link AudioFormat}. */ + unsigned int sampleRate; /**< Audio sampling rate */ + unsigned int channelCount; /**< Number of audio channels. For example, for the mono channel, the value is 1, + * and for the stereo channel, the value is 2. + */ + unsigned int period; /**< Audio sampling period */ + unsigned int frameSize; /**< Frame size of the audio data */ + boolean isBigEndian; /**< Big endian flag of audio data */ + boolean isSignedData; /**< Signed or unsigned flag of audio data */ + unsigned int startThreshold; /**< Audio render start threshold. */ + unsigned int stopThreshold; /**< Audio render stop threshold. */ + unsigned int silenceThreshold; /**< Audio capture buffer threshold. */ + int streamId; /**< Audio Identifier of render or capture */ + int sourceType; /**< Audio sourceType of render or capture */ }; + +/** + * @brief Defines the audio timestamp, which is a substitute for POSIX timespec. + */ struct AudioTimeStamp { - long tvSec; - long tvNSec; + long tvSec; /**< Seconds */ + long tvNSec; /**< Nanoseconds */ }; + +/** + * @brief Defines the sub-port capability. + */ struct AudioSubPortCapability { - unsigned int portId; - String desc; - enum AudioPortPassthroughMode mask; + unsigned int portId; /**< Sub-port ID */ + String desc; /**< Sub-port name */ + enum AudioPortPassthroughMode mask; /**< Passthrough mode of data transmission. For details, + * see {@link AudioPortPassthroughMode}. + */ }; + +/** + * @brief Defines the audio port capability. + */ struct AudioPortCapability { - unsigned int deviceType; - unsigned int deviceId; - boolean hardwareMode; - unsigned int formatNum; - enum AudioFormat[] formats; - unsigned int sampleRateMasks; - enum AudioChannelMask channelMasks; - unsigned int channelCount; - struct AudioSubPortCapability[] subPorts; - enum AudioSampleFormat[] supportSampleFormats; + unsigned int deviceType; /**< Device type (output or input) */ + unsigned int deviceId; /**< Device ID used for device binding */ + boolean hardwareMode; /**< Whether to support device binding */ + unsigned int formatNum; /**< Number of the supported audio formats */ + enum AudioFormat[] formats; /**< Supported audio formats. For details, see {@link AudioFormat}. */ + unsigned int sampleRateMasks; /**< Supported audio sampling rates (8 kHz, 16 kHz, 32 kHz, and 48 kHz) */ + enum AudioChannelMask channelMasks; /**< Audio channel layout mask of the device. For details, + * see {@link AudioChannelMask}. + */ + unsigned int channelCount; /**< Supported maximum number of audio channels */ + struct AudioSubPortCapability[] subPorts; /**< List of supported sub-ports */ + enum AudioSampleFormat[] supportSampleFormats; /**< Supported audio sample formats. For details, + * see {@link AudioSampleFormat}. + */ }; + +/** + * @brief Describes a mmap buffer. + */ struct AudioMmapBufferDescriptor { - byte[] memoryAddress; - FileDescriptor memoryFd; - int totalBufferFrames; - int transferFrameSize; - int isShareable; - unsigned int offset; - String filePath; + byte[] memoryAddress; /**< Pointer to the mmap buffer */ + FileDescriptor memoryFd; /**< File descriptor of the mmap buffer */ + int totalBufferFrames; /**< Total size of the mmap buffer (unit: frame )*/ + int transferFrameSize; /**< Transfer size (unit: frame) */ + int isShareable; /**< Whether the mmap buffer can be shared among processes */ + unsigned int offset; /**< off set */ + String filePath; /**< file path */ }; + +/** + * @brief Describes AudioDevExtInfo. + */ struct AudioDevExtInfo { - int moduleId; - enum AudioPortPin type; - String desc; + int moduleId; /**< Identifier of the module stream is attached to */ + enum AudioPortPin type; /**< Device type For details, see {@link AudioPortPin}. */ + String desc; /**< Address */ }; + +/** + * @brief Describes AudioMixInfo. + */ struct AudioMixExtInfo { - int moduleId; - int streamId; + int moduleId; /**< Identifier of the module stream is attached to */ + int streamId; /**< Identifier of the capture or render passed by caller */ }; + +/** + * @brief Describes AudioSessionExtInfo. + */ struct AudioSessionExtInfo { - enum AudioSessionType sessionType; + enum AudioSessionType sessionType; /**< Audio session type */ }; + +/** + * @brief Describes AudioInfo. + */ struct AudioInfo { - struct AudioDevExtInfo device; - struct AudioMixExtInfo mix; - struct AudioSessionExtInfo session; + struct AudioDevExtInfo device; /* Specific Device Ext info */ + struct AudioMixExtInfo mix; /* Specific mix info */ + struct AudioSessionExtInfo session; /* session specific info */ }; + +/** + * @brief Describes AudioRouteNode. + */ struct AudioRouteNode { - int portId; - enum AudioPortRole role; - enum AudioPortType type; - struct AudioInfo ext; + int portId; /**< Audio port ID */ + enum AudioPortRole role; /**< Audio port as a sink or a source */ + enum AudioPortType type; /**< device, mix ... */ + struct AudioInfo ext; /**< The ext object */ }; + +/** + * @brief Describes AudioRoute. + */ struct AudioRoute { - struct AudioRouteNode[] sources; - struct AudioRouteNode[] sinks; + struct AudioRouteNode[] sources; /**< List of sources */ + struct AudioRouteNode[] sinks; /**< List of sinks */ }; + +/** + * @brief Describes AudioEvent. + */ struct AudioEvent { - unsigned int eventType; /**< @link enum AudioEventType */ + unsigned int eventType; /**< @link enum AudioEventType */ unsigned int deviceType; /**< @link enum AudioDeviceType */ }; diff --git a/hdf_interfaces/distributed_audio/audio/v1_0/IAudioAdapter.idl b/hdf_interfaces/distributed_audio/audio/v1_0/IAudioAdapter.idl index 39f5ff59..739a4fa3 100644 --- a/hdf_interfaces/distributed_audio/audio/v1_0/IAudioAdapter.idl +++ b/hdf_interfaces/distributed_audio/audio/v1_0/IAudioAdapter.idl @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -20,12 +20,23 @@ import ohos.hdi.distributed_audio.audio.v1_0.IAudioCapture; import ohos.hdi.distributed_audio.audio.v1_0.IAudioRender; import ohos.hdi.distributed_audio.audio.v1_0.IAudioCallback; +/** + * @brief Provides audio adapter capabilities, including initializing ports, creating rendering and capturing tasks, + * and obtaining the port capability set. + * + * @see IAudioRender + * @see IAudioCapture + * @since 4.0 + * @version 1.0 + */ interface IAudioAdapter { InitAllPorts(); - CreateRender([in] struct AudioDeviceDescriptor desc, [in] struct AudioSampleAttributes attrs, [out] IAudioRender render); - DestroyRender([in] struct AudioDeviceDescriptor desc); - CreateCapture([in] struct AudioDeviceDescriptor desc, [in] struct AudioSampleAttributes attrs, [out] IAudioCapture capture); - DestroyCapture([in] struct AudioDeviceDescriptor desc); + CreateRender([in] struct AudioDeviceDescriptor desc, [in] struct AudioSampleAttributes attrs, + [out] IAudioRender render, [out] unsigned int renderId); + DestroyRender([in] unsigned int renderId); + CreateCapture([in] struct AudioDeviceDescriptor desc, [in] struct AudioSampleAttributes attrs, + [out] IAudioCapture capture, [out] unsigned int captureId); + DestroyCapture([in] unsigned int captureId); GetPortCapability([in] struct AudioPort port, [out] struct AudioPortCapability capability); SetPassthroughMode([in] struct AudioPort port, [in] enum AudioPortPassthroughMode mode); GetPassthroughMode([in] struct AudioPort port, [out] enum AudioPortPassthroughMode mode); @@ -38,4 +49,4 @@ interface IAudioAdapter { SetExtraParams([in] enum AudioExtParamKey key, [in] String condition, [in] String value); GetExtraParams([in] enum AudioExtParamKey key, [in] String condition, [out] String value); RegExtraParamObserver([in] IAudioCallback audioCallback, [in] byte cookie); -} +} \ No newline at end of file diff --git a/hdf_interfaces/distributed_audio/audio/v1_0/IAudioCallback.idl b/hdf_interfaces/distributed_audio/audio/v1_0/IAudioCallback.idl index f73ff31b..7e29d458 100644 --- a/hdf_interfaces/distributed_audio/audio/v1_0/IAudioCallback.idl +++ b/hdf_interfaces/distributed_audio/audio/v1_0/IAudioCallback.idl @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -42,5 +42,5 @@ import ohos.hdi.distributed_audio.audio.v1_0.AudioTypes; */ [callback] interface IAudioCallback { RenderCallback([in] enum AudioCallbackType type, [out] byte reserved, [out] byte cookie); - ParamCallback([in] enum AudioExtParamKey key, [in] String condition, [in] String value, [out] byte reserved, [out] byte cookie); + ParamCallback([in] enum AudioExtParamKey key, [in] String condition, [in] String value, [out] byte reserved, [in] byte cookie); } diff --git a/hdf_interfaces/distributed_audio/audio/v1_0/IAudioCapture.idl b/hdf_interfaces/distributed_audio/audio/v1_0/IAudioCapture.idl index c93ae011..51b6f0d3 100644 --- a/hdf_interfaces/distributed_audio/audio/v1_0/IAudioCapture.idl +++ b/hdf_interfaces/distributed_audio/audio/v1_0/IAudioCapture.idl @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -17,8 +17,14 @@ package ohos.hdi.distributed_audio.audio.v1_0; import ohos.hdi.distributed_audio.audio.v1_0.AudioTypes; +/** + * @brief Provides capabilities for audio capturing, including controlling the capturing, setting audio attributes, + * scenes, and volume, and capturing audio frames. + * @since 4.0 + * @version 1.0 + */ interface IAudioCapture { - CaptureFrame([out] byte[] frame, [in] unsigned long requestBytes); + CaptureFrame([out] byte[] frame, [out] unsigned long replyBytes); GetCapturePosition([out] unsigned long frames, [out] struct AudioTimeStamp time); CheckSceneCapability([in] struct AudioSceneDescriptor scene, [out] boolean supported); SelectScene([in] struct AudioSceneDescriptor scene); diff --git a/hdf_interfaces/distributed_audio/audio/v1_0/IAudioManager.idl b/hdf_interfaces/distributed_audio/audio/v1_0/IAudioManager.idl index beef5caf..16061bf1 100644 --- a/hdf_interfaces/distributed_audio/audio/v1_0/IAudioManager.idl +++ b/hdf_interfaces/distributed_audio/audio/v1_0/IAudioManager.idl @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -18,6 +18,14 @@ package ohos.hdi.distributed_audio.audio.v1_0; import ohos.hdi.distributed_audio.audio.v1_0.AudioTypes; import ohos.hdi.distributed_audio.audio.v1_0.IAudioAdapter; +/** + * @brief Manages audio adapters through a specific adapter driver program loaded based on the given audio + * adapter descriptor. + * + * @see IAudioAdapter + * @since 4.0 + * @version 1.0 + */ interface IAudioManager { GetAllAdapters([out] struct AudioAdapterDescriptor[] descs); LoadAdapter([in] struct AudioAdapterDescriptor desc, [out] IAudioAdapter adapter); diff --git a/hdf_interfaces/distributed_audio/audio/v1_0/IAudioRender.idl b/hdf_interfaces/distributed_audio/audio/v1_0/IAudioRender.idl index 4e24e431..c4a898bf 100644 --- a/hdf_interfaces/distributed_audio/audio/v1_0/IAudioRender.idl +++ b/hdf_interfaces/distributed_audio/audio/v1_0/IAudioRender.idl @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -18,6 +18,13 @@ package ohos.hdi.distributed_audio.audio.v1_0; import ohos.hdi.distributed_audio.audio.v1_0.AudioTypes; import ohos.hdi.distributed_audio.audio.v1_0.IAudioCallback; +/** + * @brief Provides capabilities for audio rendering, including controlling the rendering, setting audio attributes, + * scenes, and volume, obtaining hardware latency, and rendering audio frames. + * + * @since 4.0 + * @version 1.0 + */ interface IAudioRender { GetLatency([out] unsigned int ms); RenderFrame([in] byte[] frame, [out] unsigned long replyBytes); diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_adapter_interface_impl.h b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_adapter_interface_impl.h index cee31f40..7a6018df 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_adapter_interface_impl.h +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_adapter_interface_impl.h @@ -73,11 +73,11 @@ public: int32_t InitAllPorts() override; int32_t CreateRender(const AudioDeviceDescriptor &desc, const AudioSampleAttributes &attrs, - sptr &render) override; - int32_t DestroyRender(const AudioDeviceDescriptor &desc) override; + sptr &render, uint32_t &renderId) override; + int32_t DestroyRender(uint32_t renderId) override; int32_t CreateCapture(const AudioDeviceDescriptor &desc, const AudioSampleAttributes &attrs, - sptr &capture) override; - int32_t DestroyCapture(const AudioDeviceDescriptor &desc) override; + sptr &capture, uint32_t &captureId) override; + int32_t DestroyCapture(uint32_t captureId) override; int32_t GetPortCapability(const AudioPort &port, AudioPortCapability &capability) override; int32_t SetPassthroughMode(const AudioPort &port, AudioPortPassthroughMode mode) override; int32_t GetPassthroughMode(const AudioPort &port, AudioPortPassthroughMode &mode) override; @@ -119,6 +119,12 @@ private: int32_t WaitForSANotify(const AudioDeviceEvent &event); int32_t HandleDeviceClosed(const DAudioEvent &event); int32_t getEventTypeFromCondition(const std::string& condition); + int32_t InsertRenderImpl(const sptr &audioRender, uint32_t &renderId); + int32_t InsertCapImpl(const sptr &audioCapture, uint32_t &captureId); + inline bool IsIdValid(const uint32_t id); + bool CheckRendersValid(); + bool CheckCapsValid(); + void SetDumpFlag(bool isRender); private: static constexpr uint8_t WAIT_SECONDS = 10; @@ -129,9 +135,11 @@ private: sptr extSpkCallback_ = nullptr; sptr extMicCallback_ = nullptr; sptr paramCallback_ = nullptr; - sptr audioRender_ = nullptr; + std::mutex renderDevMtx_; + std::vector> renderDevs_; AudioParameter renderParam_; - sptr audioCapture_ = nullptr; + std::mutex capDevMtx_; + std::vector> captureDevs_; AudioParameter captureParam_; std::mutex devMapMtx_; diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_capture_interface_impl.h b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_capture_interface_impl.h index cd9e25ba..7056d9b3 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_capture_interface_impl.h +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_capture_interface_impl.h @@ -41,7 +41,7 @@ public: const AudioSampleAttributes &attrs, const sptr &callback); ~AudioCaptureInterfaceImpl() override; - int32_t CaptureFrame(std::vector &frame, uint64_t requestBytes) override; + int32_t CaptureFrame(std::vector &frame, uint64_t &replyBytes) override; int32_t GetCapturePosition(uint64_t &frames, AudioTimeStamp &time) override; int32_t CheckSceneCapability(const AudioSceneDescriptor &scene, bool &supported) override; int32_t SelectScene(const AudioSceneDescriptor &scene) override; diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_adapter_interface_impl.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_adapter_interface_impl.cpp index e5583255..1be35bac 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_adapter_interface_impl.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_adapter_interface_impl.cpp @@ -34,11 +34,18 @@ namespace HDI { namespace DistributedAudio { namespace Audio { namespace V1_0 { +static constexpr uint32_t MAX_AUDIO_STREAM_NUM = 10; + AudioAdapterInterfaceImpl::AudioAdapterInterfaceImpl(const AudioAdapterDescriptor &desc) : adpDescriptor_(desc) { + renderDevs_ = std::vector>( + MAX_AUDIO_STREAM_NUM, sptr(nullptr)); + captureDevs_ = std::vector>( + MAX_AUDIO_STREAM_NUM, sptr(nullptr)); renderParam_ = { 0, 0, 0, 0, 0, 0 }; captureParam_ = { 0, 0, 0, 0, 0, 0 }; + DHLOGD("Distributed audio adapter constructed, name(%s).", GetAnonyString(desc.adapterName).c_str()); } @@ -72,11 +79,12 @@ int32_t AudioAdapterInterfaceImpl::InitAllPorts() } int32_t AudioAdapterInterfaceImpl::CreateRender(const AudioDeviceDescriptor &desc, - const AudioSampleAttributes &attrs, sptr &render) + const AudioSampleAttributes &attrs, sptr &render, uint32_t &renderId) { DHLOGI("Create distributed audio render, {pin: %zu, sampleRate: %zu, channel: %zu, formats: %zu, type: %d}.", desc.pins, attrs.sampleRate, attrs.channelCount, attrs.format, static_cast(attrs.type)); render = nullptr; + sptr audioRender = nullptr; { std::lock_guard devLck(devMapMtx_); if (mapAudioDevice_.find(desc.pins) == mapAudioDevice_.end()) { @@ -84,64 +92,100 @@ int32_t AudioAdapterInterfaceImpl::CreateRender(const AudioDeviceDescriptor &des return HDF_FAILURE; } } + #ifdef DAUDIO_SUPPORT_EXTENSION if (attrs.type == AUDIO_MMAP_NOIRQ) { DHLOGI("Try to mmap mode."); renderFlags_ = Audioext::V1_0::MMAP_MODE; - audioRender_ = new AudioRenderExtImpl(); - audioRender_->SetAttrs(adpDescriptor_.adapterName, desc, attrs, extSpkCallback_); + audioRender = new AudioRenderExtImpl(); + audioRender->SetAttrs(adpDescriptor_.adapterName, desc, attrs, extSpkCallback_); } else { DHLOGI("Try to normal mode."); renderFlags_ = Audioext::V1_0::NORMAL_MODE; - audioRender_ = new AudioRenderInterfaceImpl(adpDescriptor_.adapterName, desc, attrs, extSpkCallback_); + audioRender = new AudioRenderInterfaceImpl(adpDescriptor_.adapterName, desc, attrs, extSpkCallback_); } #else renderFlags_ = Audioext::V1_0::NORMAL_MODE; - audioRender_ = new AudioRenderInterfaceImpl(adpDescriptor_.adapterName, desc, attrs, extSpkCallback_); + audioRender = new AudioRenderInterfaceImpl(adpDescriptor_.adapterName, desc, attrs, extSpkCallback_); #endif - if (audioRender_ == nullptr) { + if (audioRender == nullptr) { DHLOGE("Create render failed."); return HDF_FAILURE; } - int32_t ret = OpenRenderDevice(desc, attrs); if (ret != DH_SUCCESS) { DHLOGE("Open render device failed."); - audioRender_ = nullptr; + audioRender = nullptr; return ret == ERR_DH_AUDIO_HDF_WAIT_TIMEOUT ? HDF_ERR_TIMEOUT : HDF_FAILURE; } - render = audioRender_; - DHLOGI("Create render success."); + render = audioRender; + if (InsertRenderImpl(audioRender, renderId) != HDF_SUCCESS) { + DHLOGE("Generrate render ID and insert render failed."); + return HDF_FAILURE; + } + DHLOGI("Create render success, render ID is %u.", renderId); return HDF_SUCCESS; } -int32_t AudioAdapterInterfaceImpl::DestroyRender(const AudioDeviceDescriptor &desc) +int32_t AudioAdapterInterfaceImpl::InsertRenderImpl(const sptr &audioRender, + uint32_t &renderId) { - DHLOGI("Destroy distributed audio render, {pin: %zu}.", desc.pins); - if (audioRender_ == nullptr) { - DHLOGD("Render has not been created, do not need destroy."); - return HDF_SUCCESS; + std::lock_guard devLck(renderDevMtx_); + if (renderDevs_.size() != MAX_AUDIO_STREAM_NUM) { + DHLOGE("Render device contain check error."); + return HDF_FAILURE; + } + renderId = MAX_AUDIO_STREAM_NUM; + for (uint32_t i = 0; i < MAX_AUDIO_STREAM_NUM; i++) { + if (renderDevs_[i] == nullptr) { + renderId = i; + break; + } } - if (desc.pins != audioRender_->GetRenderDesc().pins) { - DHLOGE("Render pin is wrong, destroy render failed."); + if (renderId == MAX_AUDIO_STREAM_NUM) { + DHLOGE("The device is busy, can't create render anymore."); return HDF_FAILURE; } + renderDevs_[renderId] = audioRender; + return HDF_SUCCESS; +} + +int32_t AudioAdapterInterfaceImpl::DestroyRender(uint32_t renderId) +{ + DHLOGI("Destroy distributed audio render, ID: %u.", renderId); + if (!IsIdValid(renderId)) { + DHLOGE("The input render ID is invalid."); + return HDF_FAILURE; + } + sptr audioRender(nullptr); + { + std::lock_guard devLck(renderDevMtx_); + audioRender = renderDevs_[renderId]; + } + if (audioRender == nullptr) { + DHLOGD("Render has not been created, do not need destroy."); + return HDF_SUCCESS; + } - int32_t ret = CloseRenderDevice(desc); + int32_t ret = CloseRenderDevice(audioRender->GetRenderDesc()); if (ret != DH_SUCCESS) { DHLOGE("Close render device failed."); return HDF_FAILURE; } - audioRender_ = nullptr; + { + std::lock_guard devLck(renderDevMtx_); + renderDevs_[renderId] = nullptr; + } return HDF_SUCCESS; } int32_t AudioAdapterInterfaceImpl::CreateCapture(const AudioDeviceDescriptor &desc, - const AudioSampleAttributes &attrs, sptr &capture) + const AudioSampleAttributes &attrs, sptr &capture, uint32_t &captureId) { DHLOGI("Create distributed audio capture, {pin: %zu, sampleRate: %zu, channel: %zu, formats: %zu}.", desc.pins, attrs.sampleRate, attrs.channelCount, attrs.format); capture = nullptr; + sptr audioCapture(nullptr); { std::lock_guard devLck(devMapMtx_); if (mapAudioDevice_.find(desc.pins) == mapAudioDevice_.end()) { @@ -149,55 +193,90 @@ int32_t AudioAdapterInterfaceImpl::CreateCapture(const AudioDeviceDescriptor &de return HDF_FAILURE; } } + #ifdef DAUDIO_SUPPORT_EXTENSION if (attrs.type == AUDIO_MMAP_NOIRQ) { DHLOGI("Try to mmap mode."); capturerFlags_ = Audioext::V1_0::MMAP_MODE; - audioCapture_ = new AudioCaptureExtImpl(); - audioCapture_->SetAttrs(adpDescriptor_.adapterName, desc, attrs, extMicCallback_); + audioCapture = new AudioCaptureExtImpl(); + audioCapture->SetAttrs(adpDescriptor_.adapterName, desc, attrs, extMicCallback_); } else { DHLOGI("Try to normal mode."); capturerFlags_ = Audioext::V1_0::NORMAL_MODE; - audioCapture_ = new AudioCaptureInterfaceImpl(adpDescriptor_.adapterName, desc, attrs, extMicCallback_); + audioCapture = new AudioCaptureInterfaceImpl(adpDescriptor_.adapterName, desc, attrs, extMicCallback_); } #else capturerFlags_ = Audioext::V1_0::NORMAL_MODE; - audioCapture_ = new AudioCaptureInterfaceImpl(adpDescriptor_.adapterName, desc, attrs, extMicCallback_); + audioCapture = new AudioCaptureInterfaceImpl(adpDescriptor_.adapterName, desc, attrs, extMicCallback_); #endif - if (audioCapture_ == nullptr) { + if (audioCapture == nullptr) { DHLOGE("Create capture failed."); return HDF_FAILURE; } - int32_t ret = OpenCaptureDevice(desc, attrs); if (ret != DH_SUCCESS) { DHLOGE("Open capture device failed."); - audioCapture_ = nullptr; + audioCapture = nullptr; return ret == ERR_DH_AUDIO_HDF_WAIT_TIMEOUT ? HDF_ERR_TIMEOUT : HDF_FAILURE; } - capture = audioCapture_; - DHLOGI("Create capture success."); + capture = audioCapture; + if (InsertCapImpl(audioCapture, captureId) != HDF_SUCCESS) { + DHLOGE("Generrate capture ID and insert capture failed."); + return HDF_FAILURE; + } + DHLOGI("Create capture success, capture ID is %u.", captureId); return HDF_SUCCESS; } -int32_t AudioAdapterInterfaceImpl::DestroyCapture(const AudioDeviceDescriptor &desc) +int32_t AudioAdapterInterfaceImpl::InsertCapImpl(const sptr &audioCapture, + uint32_t &captureId) { - DHLOGI("Destroy distributed audio capture, {pin: %zu}.", desc.pins); - if (audioCapture_ == nullptr) { - DHLOGD("Capture has not been created, do not need destroy."); - return HDF_SUCCESS; + std::lock_guard devLck(capDevMtx_); + if (captureDevs_.size() != MAX_AUDIO_STREAM_NUM) { + DHLOGE("Capture device's size check error."); + return HDF_FAILURE; + } + captureId = MAX_AUDIO_STREAM_NUM; + for (uint32_t i = 0; i < MAX_AUDIO_STREAM_NUM; i++) { + if (captureDevs_[i] == nullptr) { + captureId = i; + break; + } + } + if (captureId == MAX_AUDIO_STREAM_NUM) { + DHLOGE("The device is busy, can't create capture anymore."); + return HDF_FAILURE; } - if (desc.pins != audioCapture_->GetCaptureDesc().pins) { - DHLOGE("Capture pin is wrong, destroy capture failed."); + captureDevs_[captureId] = audioCapture; + return HDF_SUCCESS; +} + +int32_t AudioAdapterInterfaceImpl::DestroyCapture(uint32_t captureId) +{ + DHLOGI("Destroy distributed audio capture, ID: %u.", captureId); + if (!IsIdValid(captureId)) { + DHLOGE("The input capture ID is invalid."); return HDF_FAILURE; } + sptr audioCapture(nullptr); + { + std::lock_guard devLck(capDevMtx_); + audioCapture = captureDevs_[captureId]; + } + if (audioCapture == nullptr) { + DHLOGD("Capture has not been created, do not need destroy."); + return HDF_SUCCESS; + } - int32_t ret = CloseCaptureDevice(desc); + int32_t ret = CloseCaptureDevice(audioCapture->GetCaptureDesc()); if (ret != DH_SUCCESS) { DHLOGE("Close capture device failed."); return HDF_FAILURE; } - audioCapture_ = nullptr; + { + std::lock_guard devLck(capDevMtx_); + captureDevs_[captureId] = nullptr; + } return HDF_SUCCESS; } @@ -348,7 +427,7 @@ int32_t AudioAdapterInterfaceImpl::AdapterLoad() int32_t AudioAdapterInterfaceImpl::AdapterUnload() { - if (audioRender_ != nullptr || audioCapture_ != nullptr) { + if (CheckRendersValid() || CheckCapsValid()) { DHLOGE("Adapter is busy, audio render or capture is not null."); return HDF_ERR_DEVICE_BUSY; } @@ -410,14 +489,15 @@ int32_t AudioAdapterInterfaceImpl::RemoveAudioDevice(const uint32_t devId) } mapAudioDevice_.erase(devId); } - AudioDeviceDescriptor dec; if (devId == spkPinInUse_) { - dec.pins = static_cast(spkPinInUse_); - DestroyRender(dec); + for (uint32_t i = 0; i < MAX_AUDIO_STREAM_NUM; i++) { + DestroyRender(i); + } } if (devId == micPinInUse_) { - dec.pins = static_cast(micPinInUse_); - DestroyCapture(dec); + for (uint32_t capId = 0; capId < MAX_AUDIO_STREAM_NUM; capId++) { + DestroyCapture(capId); + } } DHLOGI("Remove audio device success."); @@ -590,10 +670,6 @@ int32_t AudioAdapterInterfaceImpl::SetAudioVolume(const std::string& condition, DHLOGE("Callback is nullptr."); return ERR_DH_AUDIO_HDF_NULLPTR; } - if (audioRender_ == nullptr) { - DHLOGE("Render has not been created."); - return ERR_DH_AUDIO_HDF_NULLPTR; - } std::string content = condition; int32_t type = getEventTypeFromCondition(content); EXT_PARAM_EVENT eventType; @@ -615,18 +691,32 @@ int32_t AudioAdapterInterfaceImpl::SetAudioVolume(const std::string& condition, SetAudioParamStr(content, VOLUME_LEVEL, param); } DAudioEvent event = { eventType, content }; - int32_t ret = - extSpkCallback_->NotifyEvent(adpDescriptor_.adapterName, audioRender_->GetRenderDesc().pins, event); - if (ret != HDF_SUCCESS) { - DHLOGE("NotifyEvent failed."); - return ERR_DH_AUDIO_HDF_FAIL; + + { + std::lock_guard devLck(renderDevMtx_); + for (auto render : renderDevs_) { + if (render == nullptr) { + continue; + } + if (extSpkCallback_->NotifyEvent(adpDescriptor_.adapterName, + render->GetRenderDesc().pins, event) != HDF_SUCCESS) { + DHLOGE("NotifyEvent failed."); + return ERR_DH_AUDIO_HDF_FAIL; + } + } } return DH_SUCCESS; } int32_t AudioAdapterInterfaceImpl::GetAudioVolume(const std::string& condition, std::string ¶m) { - if (audioRender_ == nullptr) { + int renderId = 0; // fromaudio framwork + sptr audioRender(nullptr); + { + std::lock_guard devLck(renderDevMtx_); + audioRender = renderDevs_[renderId]; + } + if (audioRender == nullptr) { DHLOGE("Render has not been created."); return ERR_DH_AUDIO_HDF_NULLPTR; } @@ -634,13 +724,13 @@ int32_t AudioAdapterInterfaceImpl::GetAudioVolume(const std::string& condition, uint32_t vol; switch (type) { case VolumeEventType::EVENT_GET_VOLUME: - vol = audioRender_->GetVolumeInner(); + vol = audioRender->GetVolumeInner(); break; case VolumeEventType::EVENT_GET_MAX_VOLUME: - vol = audioRender_->GetMaxVolumeInner(); + vol = audioRender->GetMaxVolumeInner(); break; case VolumeEventType::EVENT_GET_MIN_VOLUME: - vol = audioRender_->GetMinVolumeInner(); + vol = audioRender->GetMinVolumeInner(); break; case VolumeEventType::EVENT_IS_STREAM_MUTE: vol = streamMuteStatus_; @@ -663,7 +753,13 @@ int32_t AudioAdapterInterfaceImpl::getEventTypeFromCondition(const std::string & int32_t AudioAdapterInterfaceImpl::HandleVolumeChangeEvent(const DAudioEvent &event) { DHLOGI("Vol change (%s).", event.content.c_str()); - if (audioRender_ == nullptr) { + int renderId = 0; // daudio SA + sptr audioRender(nullptr); + { + std::lock_guard devLck(renderDevMtx_); + audioRender = renderDevs_[renderId]; + } + if (audioRender == nullptr) { DHLOGE("Render has not been created."); return ERR_DH_AUDIO_HDF_NULLPTR; } @@ -685,18 +781,18 @@ int32_t AudioAdapterInterfaceImpl::HandleVolumeChangeEvent(const DAudioEvent &ev if (ret != DH_SUCCESS) { DHLOGE("Get min volume value failed, use defult min volume."); } - audioRender_->SetVolumeInner(vol); - audioRender_->SetVolumeRangeInner(maxVol, minVol); + audioRender->SetVolumeInner(vol); + audioRender->SetVolumeRangeInner(maxVol, minVol); return DH_SUCCESS; } - audioRender_->SetVolumeInner(vol); + audioRender->SetVolumeInner(vol); if (paramCallback_ == nullptr) { DHLOGE("Audio param observer is null."); return ERR_DH_AUDIO_HDF_NULLPTR; } - int8_t reserved; - int8_t cookie; + int8_t reserved = 0; + int8_t cookie = 0; ret = paramCallback_->ParamCallback(AUDIO_EXT_PARAM_KEY_VOLUME, event.content, std::to_string(vol), reserved, cookie); if (ret != DH_SUCCESS) { @@ -713,8 +809,8 @@ int32_t AudioAdapterInterfaceImpl::HandleFocusChangeEvent(const DAudioEvent &eve DHLOGE("Audio param observer is null."); return ERR_DH_AUDIO_HDF_NULLPTR; } - int8_t reserved; - int8_t cookie; + int8_t reserved = 0; + int8_t cookie = 0; int32_t ret = paramCallback_->ParamCallback(AUDIO_EXT_PARAM_KEY_FOCUS, event.content, "", reserved, cookie); if (ret != DH_SUCCESS) { DHLOGE("Notify Focus failed."); @@ -730,8 +826,8 @@ int32_t AudioAdapterInterfaceImpl::HandleRenderStateChangeEvent(const DAudioEven DHLOGE("Audio param observer is null."); return ERR_DH_AUDIO_HDF_NULLPTR; } - int8_t reserved; - int8_t cookie; + int8_t reserved = 0; + int8_t cookie = 0; int32_t ret = paramCallback_->ParamCallback(AUDIO_EXT_PARAM_KEY_STATUS, event.content, "", reserved, cookie); if (ret != DH_SUCCESS) { DHLOGE("Notify render state failed."); @@ -773,14 +869,10 @@ int32_t AudioAdapterInterfaceImpl::HandleSANotifyEvent(const DAudioEvent &event) micWaitCond_.notify_all(); break; case HDF_AUDIO_EVENT_SPK_DUMP: - if (audioRender_ != nullptr) { - audioRender_->SetDumpFlagInner(); - } + SetDumpFlag(true); break; case HDF_AUDIO_EVENT_MIC_DUMP: - if (audioCapture_ != nullptr) { - audioCapture_->SetDumpFlagInner(); - } + SetDumpFlag(false); break; default: DHLOGE("Notify not support event type %d, event content: %s.", event.type, event.content.c_str()); @@ -842,8 +934,8 @@ int32_t AudioAdapterInterfaceImpl::HandleDeviceClosed(const DAudioEvent &event) std::stringstream ss; ss << "ERR_EVENT;DEVICE_TYPE=" << (event.type == HDF_AUDIO_EVENT_SPK_CLOSED ? AUDIO_DEVICE_TYPE_SPEAKER : AUDIO_DEVICE_TYPE_MIC) << ";"; - int8_t reserved; - int8_t cookie; + int8_t reserved = 0; + int8_t cookie = 0; int32_t ret = paramCallback_->ParamCallback(AUDIO_EXT_PARAM_KEY_STATUS, ss.str(), std::to_string(EVENT_DEV_CLOSED), reserved, cookie); if (ret != DH_SUCCESS) { @@ -851,15 +943,24 @@ int32_t AudioAdapterInterfaceImpl::HandleDeviceClosed(const DAudioEvent &event) } } - AudioDeviceDescriptor dec; if (isSpkOpened_ && event.type == HDF_AUDIO_EVENT_SPK_CLOSED) { DHLOGE("Render device status error, close render."); - dec.pins = static_cast(spkPinInUse_); - return DestroyRender(dec); + bool destroyStatus = true; + for (uint32_t i = 0; i < MAX_AUDIO_STREAM_NUM; i++) { + if (DestroyRender(i) != DH_SUCCESS) { + destroyStatus = false; + } + } + return destroyStatus ? DH_SUCCESS : ERR_DH_AUDIO_HDF_FAIL; } else if (isMicOpened_ && event.type == HDF_AUDIO_EVENT_MIC_CLOSED) { DHLOGE("Capture device status error, close capture."); - dec.pins = static_cast(micPinInUse_); - return DestroyCapture(dec); + bool capCloseStatus = true; + for (uint32_t i = 0; i < MAX_AUDIO_STREAM_NUM; i++) { + if (DestroyCapture(i) != DH_SUCCESS) { + capCloseStatus = false; + } + } + return capCloseStatus ? DH_SUCCESS : ERR_DH_AUDIO_HDF_FAIL; } DHLOGI("Handle device closed success."); return DH_SUCCESS; @@ -870,6 +971,64 @@ bool AudioAdapterInterfaceImpl::IsPortsNoReg() std::lock_guard devLck(devMapMtx_); return mapAudioDevice_.empty(); } + +inline bool AudioAdapterInterfaceImpl::IsIdValid(const uint32_t id) +{ + if (id >= MAX_AUDIO_STREAM_NUM) { + DHLOGE("Current id:%u is not supported.", id); + return false; + } + return true; +} + +bool AudioAdapterInterfaceImpl::CheckRendersValid() +{ + { + std::lock_guard devLck(renderDevMtx_); + for (uint32_t i = 0; i < MAX_AUDIO_STREAM_NUM; i++) { + if (renderDevs_[i] != nullptr) { + DHLOGI("Containing active render."); + return true; + } + } + } + return false; +} + +bool AudioAdapterInterfaceImpl::CheckCapsValid() +{ + { + std::lock_guard devLck(capDevMtx_); + for (uint32_t i = 0; i < MAX_AUDIO_STREAM_NUM; i++) { + if (captureDevs_[i] != nullptr) { + DHLOGI("Containing active capture."); + return true; + } + } + } + return false; +} + +void AudioAdapterInterfaceImpl::SetDumpFlag(bool isRender) +{ + if (isRender) { + std::lock_guard renderLck(renderDevMtx_); + for (auto render : renderDevs_) { + if (render == nullptr) { + continue; + } + render->SetDumpFlagInner(); + } + } else { + std::lock_guard capLck(capDevMtx_); + for (auto capture : captureDevs_) { + if (capture == nullptr) { + continue; + } + capture->SetDumpFlagInner(); + } + } +} } // V1_0 } // Audio } // Distributedaudio diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_capture_interface_impl.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_capture_interface_impl.cpp index 083282ca..a3722366 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_capture_interface_impl.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_capture_interface_impl.cpp @@ -60,7 +60,7 @@ int32_t AudioCaptureInterfaceImpl::GetCapturePosition(uint64_t &frames, AudioTim return HDF_SUCCESS; } -int32_t AudioCaptureInterfaceImpl::CaptureFrame(std::vector &frame, uint64_t requestBytes) +int32_t AudioCaptureInterfaceImpl::CaptureFrame(std::vector &frame, uint64_t &replyBytes) { DHLOGD("Capture frame[sampleRate: %d, channelCount: %d, format: %d, frameSize: %d].", devAttrs_.sampleRate, devAttrs_.channelCount, devAttrs_.format, devAttrs_.frameSize); diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/createcapture_fuzzer/createcapture_fuzzer.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/createcapture_fuzzer/createcapture_fuzzer.cpp index 87f0fe8a..d49cd09a 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/createcapture_fuzzer/createcapture_fuzzer.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/createcapture_fuzzer/createcapture_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -59,7 +59,8 @@ void CreateCaptureFuzzTest(const uint8_t* data, size_t size) }; sptr capture; - audioAdapter->CreateCapture(deviceDes, sampleAttr, capture); + uint32_t capId; + audioAdapter->CreateCapture(deviceDes, sampleAttr, capture, capId); } } // V1_0 } // Audio diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/createrender_fuzzer/createrender_fuzzer.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/createrender_fuzzer/createrender_fuzzer.cpp index d3ba28c7..9b0289cd 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/createrender_fuzzer/createrender_fuzzer.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/createrender_fuzzer/createrender_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -59,7 +59,8 @@ void CreateRenderFuzzTest(const uint8_t* data, size_t size) }; sptr render; - audioAdapter->CreateRender(deviceDes, sampleAttr, render); + uint32_t renderId; + audioAdapter->CreateRender(deviceDes, sampleAttr, render, renderId); } } // V1_0 } // Audio diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/destroycapture_fuzzer/destroycapture_fuzzer.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/destroycapture_fuzzer/destroycapture_fuzzer.cpp index fcf2144b..3cd06868 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/destroycapture_fuzzer/destroycapture_fuzzer.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/destroycapture_fuzzer/destroycapture_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -36,13 +36,8 @@ void DestroyCaptureFuzzTest(const uint8_t* data, size_t size) AudioAdapterDescriptor desc; auto audioAdapter = std::make_shared(desc); - AudioDeviceDescriptor deviceDes = { - .portId = *(reinterpret_cast(data)), - .pins = *(reinterpret_cast(data)), - .desc = std::string(reinterpret_cast(data), size), - }; - - audioAdapter->DestroyCapture(deviceDes); + uint32_t capId = *(reinterpret_cast(data)); + audioAdapter->DestroyCapture(capId); } } // V1_0 } // Audio diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/destroyrender_fuzzer/destroyrender_fuzzer.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/destroyrender_fuzzer/destroyrender_fuzzer.cpp index c4c39172..1ca043aa 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/destroyrender_fuzzer/destroyrender_fuzzer.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/fuzztest/destroyrender_fuzzer/destroyrender_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -36,13 +36,8 @@ void DestroyRenderFuzzTest(const uint8_t* data, size_t size) AudioAdapterDescriptor desc; auto audioAdapter = std::make_shared(desc); - AudioDeviceDescriptor deviceDes = { - .portId = *(reinterpret_cast(data)), - .pins = *(reinterpret_cast(data)), - .desc = std::string(reinterpret_cast(data), size), - }; - - audioAdapter->DestroyRender(deviceDes); + uint32_t renderId = *(reinterpret_cast(data)); + audioAdapter->DestroyRender(renderId); } } // V1_0 } // Audio diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_adapter_interface/src/audio_adapter_interface_impl_test.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_adapter_interface/src/audio_adapter_interface_impl_test.cpp index a1e5d6c1..c9554175 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_adapter_interface/src/audio_adapter_interface_impl_test.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_adapter_interface/src/audio_adapter_interface_impl_test.cpp @@ -73,14 +73,15 @@ HWTEST_F(AudioAdapterInterfaceImpTest, CreateRender_001, TestSize.Level1) AudioDeviceDescriptor devDesc; AudioSampleAttributes attrs; sptr render = nullptr; + uint32_t renderId = 10; AdapterTest_->extSpkCallback_ = new MockIDAudioCallback(); - EXPECT_NE(HDF_SUCCESS, AdapterTest_->CreateRender(devDesc, attrs, render)); - EXPECT_EQ(HDF_SUCCESS, AdapterTest_->DestroyRender(devDesc)); + EXPECT_NE(HDF_SUCCESS, AdapterTest_->CreateRender(devDesc, attrs, render, renderId)); + EXPECT_EQ(HDF_SUCCESS, AdapterTest_->DestroyRender(renderId)); AdapterTest_->mapAudioDevice_.insert(std::make_pair(PIN_OUT_DAUDIO_DEFAULT, "hello")); devDesc.pins = PIN_OUT_DAUDIO_DEFAULT; - EXPECT_NE(HDF_SUCCESS, AdapterTest_->CreateRender(devDesc, attrs, render)); - EXPECT_EQ(HDF_SUCCESS, AdapterTest_->DestroyRender(devDesc)); + EXPECT_NE(HDF_SUCCESS, AdapterTest_->CreateRender(devDesc, attrs, render, renderId)); + EXPECT_EQ(HDF_SUCCESS, AdapterTest_->DestroyRender(renderId)); } /** @@ -98,12 +99,13 @@ HWTEST_F(AudioAdapterInterfaceImpTest, DestroyRender_001, TestSize.Level1) AdapterTest_->extSpkCallback_ = new MockRevertIDAudioCallback(); devDesc.pins = PIN_OUT_DAUDIO_DEFAULT; - AdapterTest_->audioRender_ = new AudioRenderInterfaceImpl(adpterName, devDesc, attrs, callback); + uint32_t renderId = 0; + AdapterTest_->renderDevs_[renderId] = new AudioRenderInterfaceImpl(adpterName, devDesc, attrs, callback); AdapterTest_->spkPinInUse_ = 0; - EXPECT_EQ(HDF_SUCCESS, AdapterTest_->DestroyRender(devDesc)); - AdapterTest_->spkPinInUse_ = 1; - EXPECT_NE(HDF_FAILURE, AdapterTest_->DestroyRender(devDesc)); + EXPECT_EQ(HDF_SUCCESS, AdapterTest_->DestroyRender(renderId)); + renderId = 10; + EXPECT_NE(HDF_FAILURE, AdapterTest_->DestroyRender(renderId)); } /** @@ -117,14 +119,15 @@ HWTEST_F(AudioAdapterInterfaceImpTest, CreateCapture_001, TestSize.Level1) AudioDeviceDescriptor devDesc; AudioSampleAttributes attrs; sptr capture = nullptr; + uint32_t capId = 10; AdapterTest_->extMicCallback_ = new MockIDAudioCallback(); - EXPECT_NE(HDF_SUCCESS, AdapterTest_->CreateCapture(devDesc, attrs, capture)); - EXPECT_EQ(HDF_SUCCESS, AdapterTest_->DestroyCapture(devDesc)); + EXPECT_NE(HDF_SUCCESS, AdapterTest_->CreateCapture(devDesc, attrs, capture, capId)); + EXPECT_EQ(HDF_SUCCESS, AdapterTest_->DestroyCapture(capId)); AdapterTest_->mapAudioDevice_.insert(std::make_pair(PIN_OUT_DAUDIO_DEFAULT, "hello")); devDesc.pins = PIN_OUT_DAUDIO_DEFAULT; - EXPECT_NE(HDF_SUCCESS, AdapterTest_->CreateCapture(devDesc, attrs, capture)); - EXPECT_EQ(HDF_SUCCESS, AdapterTest_->DestroyCapture(devDesc)); + EXPECT_NE(HDF_SUCCESS, AdapterTest_->CreateCapture(devDesc, attrs, capture, capId)); + EXPECT_EQ(HDF_SUCCESS, AdapterTest_->DestroyCapture(capId)); } /** @@ -142,11 +145,12 @@ HWTEST_F(AudioAdapterInterfaceImpTest, DestroyCapture_001, TestSize.Level1) AdapterTest_->extMicCallback_ = new MockRevertIDAudioCallback(); devDesc.pins = PIN_OUT_DAUDIO_DEFAULT; - AdapterTest_->audioCapture_ = new AudioCaptureInterfaceImpl(adpterName, devDesc, attrs, callback); + uint32_t capId = 0; + AdapterTest_->captureDevs_[capId] = new AudioCaptureInterfaceImpl(adpterName, devDesc, attrs, callback); - EXPECT_EQ(HDF_SUCCESS, AdapterTest_->DestroyCapture(devDesc)); - AdapterTest_->micPinInUse_ = 1; - EXPECT_NE(HDF_FAILURE, AdapterTest_->DestroyCapture(devDesc)); + EXPECT_EQ(HDF_SUCCESS, AdapterTest_->DestroyCapture(capId)); + capId = 10; + EXPECT_NE(HDF_FAILURE, AdapterTest_->DestroyCapture(capId)); } /** @@ -423,19 +427,19 @@ HWTEST_F(AudioAdapterInterfaceImpTest, AdapterUnload_001, TestSize.Level1) AudioDeviceDescriptor descSpk; AudioSampleAttributes attrsSpk; sptr callbackSpk = new MockIDAudioCallback(); - AdapterTest_->audioRender_ = new AudioRenderInterfaceImpl(adpterName, descSpk, attrsSpk, callbackSpk); + AdapterTest_->renderDevs_[0] = new AudioRenderInterfaceImpl(adpterName, descSpk, attrsSpk, callbackSpk); EXPECT_EQ(HDF_ERR_DEVICE_BUSY, AdapterTest_->AdapterUnload()); AudioDeviceDescriptor devDescMic; AudioSampleAttributes attrsMic; sptr callbackMic = new MockIDAudioCallback(); - AdapterTest_->audioCapture_ = new AudioCaptureInterfaceImpl(adpterName, devDescMic, attrsMic, callbackMic); + AdapterTest_->captureDevs_[0] = new AudioCaptureInterfaceImpl(adpterName, devDescMic, attrsMic, callbackMic); EXPECT_EQ(HDF_ERR_DEVICE_BUSY, AdapterTest_->AdapterUnload()); - AdapterTest_->audioRender_ = nullptr; + AdapterTest_->renderDevs_[0] = nullptr; EXPECT_EQ(HDF_ERR_DEVICE_BUSY, AdapterTest_->AdapterUnload()); - AdapterTest_->audioCapture_ = nullptr; + AdapterTest_->captureDevs_[0] = nullptr; EXPECT_EQ(HDF_SUCCESS, AdapterTest_->AdapterUnload()); } @@ -675,7 +679,7 @@ HWTEST_F(AudioAdapterInterfaceImpTest, SetAudioVolume_001, TestSize.Level1) AudioSampleAttributes attrs; sptr callback = new MockIDAudioCallback(); - AdapterTest_->audioRender_ = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); + AdapterTest_->renderDevs_[0] = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); EXPECT_EQ(HDF_SUCCESS, AdapterTest_->SetAudioVolume(condition, param)); param = "0"; EXPECT_EQ(HDF_SUCCESS, AdapterTest_->SetAudioVolume(condition, param)); @@ -698,7 +702,7 @@ HWTEST_F(AudioAdapterInterfaceImpTest, GetAudioVolume_001, TestSize.Level1) AudioSampleAttributes attrs; sptr callback = new MockIDAudioCallback(); - AdapterTest_->audioRender_ = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); + AdapterTest_->renderDevs_[0] = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); std::string condition = "EVENT_TYPE=1;VOLUME_GROUP_ID=2;AUDIO_VOLUME_TYPE=1;"; std::string param = "1"; @@ -718,7 +722,7 @@ HWTEST_F(AudioAdapterInterfaceImpTest, GetAudioVolume_002, TestSize.Level1) AudioSampleAttributes attrs; sptr callback = new MockIDAudioCallback(); - AdapterTest_->audioRender_ = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); + AdapterTest_->renderDevs_[0] = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); std::string condition = "EVENT_TYPE=3;VOLUME_GROUP_ID=2;AUDIO_VOLUME_TYPE=1;"; std::string param = "1"; @@ -738,7 +742,7 @@ HWTEST_F(AudioAdapterInterfaceImpTest, GetAudioVolume_003, TestSize.Level1) AudioSampleAttributes attrs; sptr callback = new MockIDAudioCallback(); - AdapterTest_->audioRender_ = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); + AdapterTest_->renderDevs_[0] = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); std::string condition = "EVENT_TYPE=2;VOLUME_GROUP_ID=2;AUDIO_VOLUME_TYPE=1;"; std::string param = "1"; @@ -758,7 +762,7 @@ HWTEST_F(AudioAdapterInterfaceImpTest, GetAudioVolume_004, TestSize.Level1) AudioSampleAttributes attrs; sptr callback = new MockIDAudioCallback(); - AdapterTest_->audioRender_ = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); + AdapterTest_->renderDevs_[0] = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); std::string condition = "EVENT_TYPE=4;VOLUME_GROUP_ID=2;AUDIO_VOLUME_TYPE=1;"; std::string param = "1"; @@ -779,7 +783,7 @@ HWTEST_F(AudioAdapterInterfaceImpTest, GetAudioVolume_005, TestSize.Level1) AudioSampleAttributes attrs; sptr callback = new MockIDAudioCallback(); - AdapterTest_->audioRender_ = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); + AdapterTest_->renderDevs_[0] = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); std::string condition = "EVENT_TYPE=66;VOLUME_GROUP_ID=2;AUDIO_VOLUME_TYPE=1;"; std::string param = "1"; @@ -795,7 +799,7 @@ HWTEST_F(AudioAdapterInterfaceImpTest, GetAudioVolume_005, TestSize.Level1) */ HWTEST_F(AudioAdapterInterfaceImpTest, GetAudioVolume_006, TestSize.Level1) { - AdapterTest_->audioRender_ = nullptr; + AdapterTest_->renderDevs_[0] = nullptr; std::string condition = "EVENT_TYPE=1;VOLUME_GROUP_ID=2;AUDIO_VOLUME_TYPE=1;"; std::string param = "1"; @@ -844,10 +848,10 @@ HWTEST_F(AudioAdapterInterfaceImpTest, HandleVolumeChangeEvent_001, TestSize.Lev AudioSampleAttributes attrs; sptr callback = new MockIDAudioCallback(); - AdapterTest_->audioRender_ = nullptr; + AdapterTest_->renderDevs_[0] = nullptr; EXPECT_NE(HDF_SUCCESS, AdapterTest_->HandleVolumeChangeEvent(event)); - AdapterTest_->audioRender_ = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); + AdapterTest_->renderDevs_[0] = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); EXPECT_NE(HDF_SUCCESS, AdapterTest_->HandleVolumeChangeEvent(event)); AdapterTest_->paramCallback_ = new MockIAudioParamCallback(); EXPECT_EQ(HDF_SUCCESS, AdapterTest_->HandleVolumeChangeEvent(event)); @@ -867,7 +871,7 @@ HWTEST_F(AudioAdapterInterfaceImpTest, HandleVolumeChangeEvent_002, TestSize.Lev AudioSampleAttributes attrs; sptr callback = new MockIDAudioCallback(); - AdapterTest_->audioRender_ = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); + AdapterTest_->renderDevs_[0] = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); EXPECT_NE(HDF_SUCCESS, AdapterTest_->HandleVolumeChangeEvent(event)); } @@ -885,7 +889,7 @@ HWTEST_F(AudioAdapterInterfaceImpTest, HandleVolumeChangeEvent_003, TestSize.Lev AudioSampleAttributes attrs; sptr callback = new MockIDAudioCallback(); - AdapterTest_->audioRender_ = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); + AdapterTest_->renderDevs_[0] = new AudioRenderInterfaceImpl(adpterName, desc, attrs, callback); EXPECT_NE(HDF_SUCCESS, AdapterTest_->HandleVolumeChangeEvent(event)); } diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_test_utils/audio_test_utils.h b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_test_utils/audio_test_utils.h index bc57b7db..c67d9582 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_test_utils/audio_test_utils.h +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_test_utils/audio_test_utils.h @@ -291,7 +291,7 @@ public: MockIAudioCapture() {} ~MockIAudioCapture() {} - int32_t CaptureFrame(std::vector &frame, uint64_t requestBytes) override + int32_t CaptureFrame(std::vector &frame, uint64_t &replyBytes) override { return DistributedHardware::DH_SUCCESS; } @@ -458,7 +458,7 @@ public: } int32_t ParamCallback(AudioExtParamKey key, const std::string& condition, const std::string& value, - int8_t &reserved, int8_t &cookie) override + int8_t &reserved, int8_t cookie) override { return DistributedHardware::DH_SUCCESS; } @@ -475,7 +475,7 @@ public: } int32_t ParamCallback(AudioExtParamKey key, const std::string& condition, const std::string& value, - int8_t &reserved, int8_t &cookie) override + int8_t &reserved, int8_t cookie) override { return DistributedHardware::ERR_DH_AUDIO_HDF_FAIL; } diff --git a/services/hdfaudioclient/include/daudio_adapter_internal.h b/services/hdfaudioclient/include/daudio_adapter_internal.h index 0f5eaebb..78d0ebdf 100644 --- a/services/hdfaudioclient/include/daudio_adapter_internal.h +++ b/services/hdfaudioclient/include/daudio_adapter_internal.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -45,8 +45,8 @@ struct AudioAdapterContext { std::unique_ptr callbackInternal_ = nullptr; ParamCallback callback_ = nullptr; - std::vector> captures_; - std::vector> renders_; + std::vector>> captures_; + std::vector>> renders_; std::map> caps_; }; } // namespace DistributedHardware diff --git a/services/hdfaudioclient/src/daudio_adapter_internal.cpp b/services/hdfaudioclient/src/daudio_adapter_internal.cpp index 030daea2..1258b84d 100644 --- a/services/hdfaudioclient/src/daudio_adapter_internal.cpp +++ b/services/hdfaudioclient/src/daudio_adapter_internal.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -87,7 +87,8 @@ static int32_t CreateRenderInternal(struct AudioAdapter *adapter, const struct : AudioSampleAttributes attrsHal; SetAudioSampleAttributesHAL(attrs, attrsHal); sptr renderProxy = nullptr; - int32_t ret = context->proxy_->CreateRender(descHal, attrsHal, renderProxy); + uint32_t renderId; + int32_t ret = context->proxy_->CreateRender(descHal, attrsHal, renderProxy, renderId); if (ret != DH_SUCCESS) { *render = nullptr; return ret; @@ -96,9 +97,10 @@ static int32_t CreateRenderInternal(struct AudioAdapter *adapter, const struct : *render = &renderContext->instance_; renderContext->proxy_ = renderProxy; renderContext->descHal_ = descHal; + DHLOGI("The render ID: %u.", renderId); { std::lock_guard lock(context->mtx_); - context->renders_.push_back(std::move(renderContext)); + context->renders_.push_back(std::make_pair(renderId, std::move(renderContext))); } return DH_SUCCESS; } @@ -120,8 +122,8 @@ static int32_t DestroyRenderInternal(struct AudioAdapter *adapter, struct AudioR std::lock_guard lock(adapterContext->mtx_); for (auto it = adapterContext->renders_.begin(); it != adapterContext->renders_.end(); ++it) { - if ((*it).get() == renderContext) { - int32_t ret = adapterContext->proxy_->DestroyRender(renderContext->descHal_); + if ((it->second).get() == renderContext) { + int32_t ret = adapterContext->proxy_->DestroyRender(it->first); if (ret != DH_SUCCESS) { return ret; } @@ -154,7 +156,8 @@ static int32_t CreateCaptureInternal(struct AudioAdapter *adapter, const struct AudioSampleAttributes attrsHal; SetAudioSampleAttributesHAL(attrs, attrsHal); sptr captureProxy = nullptr; - int32_t ret = context->proxy_->CreateCapture(descHal, attrsHal, captureProxy); + uint32_t captureId; + int32_t ret = context->proxy_->CreateCapture(descHal, attrsHal, captureProxy, captureId); if (ret != DH_SUCCESS) { *capture = nullptr; return ret; @@ -164,9 +167,10 @@ static int32_t CreateCaptureInternal(struct AudioAdapter *adapter, const struct *capture = &captureContext->instance_; captureContext->proxy_ = captureProxy; captureContext->descHal_ = descHal; + DHLOGI("The capture ID: %u.", captureId); { std::lock_guard lock(context->mtx_); - context->captures_.push_back(std::move(captureContext)); + context->captures_.push_back(std::make_pair(captureId, std::move(captureContext))); } return DH_SUCCESS; } @@ -188,8 +192,8 @@ static int32_t DestroyCaptureInternal(struct AudioAdapter *adapter, struct Audio std::lock_guard lock(adapterContext->mtx_); for (auto it = adapterContext->captures_.begin(); it != adapterContext->captures_.end(); ++it) { - if ((*it).get() == captureContext) { - int32_t ret = adapterContext->proxy_->DestroyCapture(captureContext->descHal_); + if ((it->second).get() == captureContext) { + int32_t ret = adapterContext->proxy_->DestroyCapture(it->first); if (ret != DH_SUCCESS) { return ret; } diff --git a/services/hdfaudioclient/src/daudio_param_callback_internal.cpp b/services/hdfaudioclient/src/daudio_param_callback_internal.cpp index ea11fa67..cb89b82c 100644 --- a/services/hdfaudioclient/src/daudio_param_callback_internal.cpp +++ b/services/hdfaudioclient/src/daudio_param_callback_internal.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -36,7 +36,7 @@ public: int32_t RenderCallback(AudioCallbackType type, int8_t &reserved, int8_t &cookie) override; int32_t ParamCallback(AudioExtParamKey key, const std::string& condition, const std::string& value, - int8_t &reserved, int8_t &cookie) override; + int8_t &reserved, int8_t cookie) override; private: ::ParamCallback callback_ = nullptr; void *cookie_ = nullptr; @@ -55,7 +55,7 @@ int32_t AudioParamCallbackImpl::RenderCallback(AudioCallbackType type, int8_t &r } int32_t AudioParamCallbackImpl::ParamCallback(AudioExtParamKey key, const std::string& condition, - const std::string& value, int8_t &reserved, int8_t &cookie) + const std::string& value, int8_t &reserved, int8_t cookie) { (void) cookie; if (callback_ != nullptr) { diff --git a/services/hdfaudioclient/src/daudio_render_callback_internal.cpp b/services/hdfaudioclient/src/daudio_render_callback_internal.cpp index 0e103915..9facc92b 100644 --- a/services/hdfaudioclient/src/daudio_render_callback_internal.cpp +++ b/services/hdfaudioclient/src/daudio_render_callback_internal.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -32,7 +32,7 @@ public: int32_t RenderCallback(AudioCallbackType type, int8_t &reserved, int8_t &cookie) override; int32_t ParamCallback(AudioExtParamKey key, const std::string& condition, const std::string& value, - int8_t &reserved, int8_t &cookie) override; + int8_t &reserved, int8_t cookie) override; private: ::RenderCallback callback_ = nullptr; @@ -57,7 +57,7 @@ int32_t AudioRenderCallbackImpl::RenderCallback(AudioCallbackType type, int8_t & } int32_t AudioRenderCallbackImpl::ParamCallback(AudioExtParamKey key, const std::string& condition, - const std::string& value, int8_t &reserved, int8_t &cookie) + const std::string& value, int8_t &reserved, int8_t cookie) { (void) reserved; (void) cookie; diff --git a/services/hdfaudioclient/test/unittest/audio_adapter_internal/audio_adapter_internal_test.cpp b/services/hdfaudioclient/test/unittest/audio_adapter_internal/audio_adapter_internal_test.cpp index a1f167a0..a0f22c09 100644 --- a/services/hdfaudioclient/test/unittest/audio_adapter_internal/audio_adapter_internal_test.cpp +++ b/services/hdfaudioclient/test/unittest/audio_adapter_internal/audio_adapter_internal_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 diff --git a/services/hdfaudioclient/test/unittest/audio_adapter_internal/audio_adapter_internal_test.h b/services/hdfaudioclient/test/unittest/audio_adapter_internal/audio_adapter_internal_test.h index 03567108..e083a165 100644 --- a/services/hdfaudioclient/test/unittest/audio_adapter_internal/audio_adapter_internal_test.h +++ b/services/hdfaudioclient/test/unittest/audio_adapter_internal/audio_adapter_internal_test.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -54,23 +54,23 @@ public: } int32_t CreateRender(const AudioDeviceDescriptor& desc, const AudioSampleAttributes& attrs, - sptr& render) override + sptr& render, uint32_t &renderId) override { return DH_SUCCESS; } - int32_t DestroyRender(const AudioDeviceDescriptor& desc) override + int32_t DestroyRender(uint32_t renderId) override { return DH_SUCCESS; } int32_t CreateCapture(const AudioDeviceDescriptor& desc, const AudioSampleAttributes& attrs, - sptr& capture) override + sptr& capture, uint32_t &captureId) override { return DH_SUCCESS; } - int32_t DestroyCapture(const AudioDeviceDescriptor& desc) override + int32_t DestroyCapture(uint32_t captureId) override { return DH_SUCCESS; } -- Gitee