From 7afbef6d01b0f01b5af4f7f079ca4272ebf94349 Mon Sep 17 00:00:00 2001 From: zhanhang Date: Wed, 21 Jun 2023 15:39:18 +0800 Subject: [PATCH 1/3] ohaudio interface Signed-off-by: zhanhang --- .../c/audio_capturer/native_audiocapturer.h | 69 ++++- .../c/audio_renderer/native_audiorenderer.h | 69 ++++- .../kits/c/common/native_audiostream_base.h | 250 ++++++++++++++++-- .../kits/c/common/native_audiostreambuilder.h | 31 ++- 4 files changed, 381 insertions(+), 38 deletions(-) diff --git a/interfaces/kits/c/audio_capturer/native_audiocapturer.h b/interfaces/kits/c/audio_capturer/native_audiocapturer.h index 4529ab3c97..68d5796ca5 100644 --- a/interfaces/kits/c/audio_capturer/native_audiocapturer.h +++ b/interfaces/kits/c/audio_capturer/native_audiocapturer.h @@ -13,8 +13,30 @@ * limitations under the License. */ -#ifndef ST_NATIVE_AUDIOCAPTURER_H -#define ST_NATIVE_AUDIOCAPTURER_H +/** + * @addtogroup OHAudio + * @{ + * + * @brief Provide the definition of the C interface for the audio module. + * + * @syscap SystemCapability.Multimedia.Audio.Core + * + * @since 10 + * @version 1.0 + */ + +/** + * @file native_audiocapturer.h + * + * @brief Declare audio stream related interfaces for input type. + * + * @syscap SystemCapability.Multimedia.Audio.Core + * @since 10 + * @version 1.0 + */ + +#ifndef NATIVE_AUDIOCAPTURER_H +#define NATIVE_AUDIOCAPTURER_H #include #include "native_audiostream_base.h" @@ -162,18 +184,51 @@ OH_AudioStream_Result OH_AudioCapturer_GetEncodingType(OH_AudioCapturer* capture /* * Query the capturer info of the capturer client. * - * The rendere info includes {@link OH_AudioStream_Usage} value and {@link OH_AudioStream_Content} value. - * * @since 10 * * @param capturer Reference created by OH_AudioStreamBuilder_GenerateCapturer() - * @param usage Pointer to a variable that will be set for the stream usage. - * @param content Pointer to a variable that will be set for the stream content. + * @param sourceType Pointer to a variable that will be set for the stream sourceType. * @return {@link #AUDIOSTREAM_SUCCESS} or an undesired error. */ OH_AudioStream_Result OH_AudioCapturer_GetCapturerInfo(OH_AudioCapturer* capturer, OH_AudioStream_SourceType* sourceType); + +/* + * Query the frame size in callback, it is a fixed length of the buffer returned by each callback. + * + * @since 10 + * + * @param capturer Reference created by OH_AudioStreamBuilder_GenerateCapturer() + * @param frameSize Pointer to a variable that will be set for the frame size. + * @return {@link #AUDIOSTREAM_SUCCESS} or an undesired error. + */ +OH_AudioStream_Result OH_AudioCapturer_GetFrameSizeInCallback(OH_AudioCapturer* capturer, int32_t* frameSize); + +/* + * Query the the time at which a particular frame was presented + * + * @since 10 + * + * @param capturer Reference created by OH_AudioStreamBuilder_GenerateCapturer() + * @param clockId {@link #CLOCK_MONOTONIC} + * @param framePosition Pointer to a variable to receive the position + * @param timestamp Pointer to a variable to receive the timestamp + * @return {@link #AUDIOSTREAM_SUCCESS} or an undesired error. + */ +OH_AudioStream_Result OH_AudioCapturer_GetTimestamp(OH_AudioCapturer* capturer, + clockid_t clockId, int64_t* framePosition, int64_t* timestamp); + +/* + * Query the the number of frames that have been read since the stream was created. + * + * @since 10 + * + * @param capturer Reference created by OH_AudioStreamBuilder_GenerateCapturer() + * @param frames Pointer to a variable that will be set for the frame count number. + * @return {@link #AUDIOSTREAM_SUCCESS} or an undesired error. + */ +OH_AudioStream_Result OH_AudioCapturer_GetFramesRead(OH_AudioCapturer* capturer, int64_t* frames); #ifdef __cplusplus } #endif -#endif // ST_NATIVE_AUDIORENDERER_H +#endif // NATIVE_AUDIOCAPTURER_H diff --git a/interfaces/kits/c/audio_renderer/native_audiorenderer.h b/interfaces/kits/c/audio_renderer/native_audiorenderer.h index 6e96a42ad5..8b201d9789 100644 --- a/interfaces/kits/c/audio_renderer/native_audiorenderer.h +++ b/interfaces/kits/c/audio_renderer/native_audiorenderer.h @@ -13,8 +13,30 @@ * limitations under the License. */ -#ifndef ST_NATIVE_AUDIORENDERER_H -#define ST_NATIVE_AUDIORENDERER_H +/** + * @addtogroup OHAudio + * @{ + * + * @brief Provide the definition of the C interface for the audio module. + * + * @syscap SystemCapability.Multimedia.Audio.Core + * + * @since 10 + * @version 1.0 + */ + +/** + * @file native_audiorenderer.h + * + * @brief Declare audio stream related interfaces for output type. + * + * @syscap SystemCapability.Multimedia.Audio.Core + * @since 10 + * @version 1.0 + */ + +#ifndef NATIVE_AUDIORENDERER_H +#define NATIVE_AUDIORENDERER_H #include #include "native_audiostream_base.h" @@ -146,17 +168,16 @@ OH_AudioStream_Result OH_AudioRenderer_GetLatencyMode(OH_AudioRenderer* renderer /* * Query the renderer info of the renderer client. * - * The rendere info includes {@link OH_AudioStream_Usage} value and {@link OH_AudioStream_Content} value. + * The rendere info includes {@link OH_AudioStream_Usage} value. * * @since 10 * * @param renderer Reference created by OH_AudioStreamBuilder_GenerateRenderer() * @param usage Pointer to a variable that will be set for the stream usage. - * @param content Pointer to a variable that will be set for the stream content. * @return {@link #AUDIOSTREAM_SUCCESS} or an undesired error. */ OH_AudioStream_Result OH_AudioRenderer_GetRendererInfo(OH_AudioRenderer* renderer, - OH_AudioStream_Usage* usage, OH_AudioStream_Content* content); + OH_AudioStream_Usage* usage); /* * Query the encoding type of the renderer client. @@ -169,7 +190,43 @@ OH_AudioStream_Result OH_AudioRenderer_GetRendererInfo(OH_AudioRenderer* rendere */ OH_AudioStream_Result OH_AudioRenderer_GetEncodingType(OH_AudioRenderer* renderer, OH_AudioStream_EncodingType* encodingType); + +/* + * Query the the number of frames that have been written since the stream was created. + * + * @since 10 + * + * @param renderer Reference created by OH_AudioStreamBuilder_GenerateRenderer() + * @param frames Pointer to a variable that will be set for the frame count number. + * @return {@link #AUDIOSTREAM_SUCCESS} or an undesired error. + */ +OH_AudioStream_Result OH_AudioRenderer_GetFramesWritten(OH_AudioRenderer* renderer, int64_t* frames); + +/* + * Query the the time at which a particular frame was presented. + * + * @since 10 + * + * @param renderer Reference created by OH_AudioStreamBuilder_GenerateRenderer() + * @param clockId {@link #CLOCK_MONOTONIC} + * @param framePosition Pointer to a variable to receive the position + * @param timestamp Pointer to a variable to receive the timestamp + * @return {@link #AUDIOSTREAM_SUCCESS} or an undesired error. + */ +OH_AudioStream_Result OH_AudioRenderer_GetTimestamp(OH_AudioRenderer* renderer, + clockid_t clockId, int64_t* framePosition, int64_t* timestamp); + +/* + * Query the frame size in callback, it is a fixed length that the stream want to be filled for each callback. + * + * @since 10 + * + * @param renderer Reference created by OH_AudioStreamBuilder_GenerateRenderer() + * @param frameSize Pointer to a variable that will be set for the frame size. + * @return {@link #AUDIOSTREAM_SUCCESS} or an undesired error. + */ +OH_AudioStream_Result OH_AudioRenderer_GetFrameSizeInCallback(OH_AudioRenderer* renderer, int32_t* frameSize); #ifdef __cplusplus } #endif -#endif // ST_NATIVE_AUDIORENDERER_H +#endif // NATIVE_AUDIORENDERER_H diff --git a/interfaces/kits/c/common/native_audiostream_base.h b/interfaces/kits/c/common/native_audiostream_base.h index eee4a1eed5..989feba32a 100644 --- a/interfaces/kits/c/common/native_audiostream_base.h +++ b/interfaces/kits/c/common/native_audiostream_base.h @@ -13,6 +13,28 @@ * limitations under the License. */ +/** + * @addtogroup OHAudio + * @{ + * + * @brief Provide the definition of the C interface for the audio module. + * + * @syscap SystemCapability.Multimedia.Audio.Core + * + * @since 10 + * @version 1.0 + */ + +/** + * @file native_audiostream_base.h + * + * @brief Declare the underlying data structure. + * + * @syscap SystemCapability.Multimedia.Audio.Core + * @since 10 + * @version 1.0 + */ + #ifndef ST_NATIVE_AUDIOSTREAM_BASE_H #define ST_NATIVE_AUDIOSTREAM_BASE_H @@ -31,22 +53,22 @@ typedef enum { /** * The call was successful. */ - AUDIOSTREAM_SUCCESS, + AUDIOSTREAM_SUCCESS = 0, /** * This means that the function was executed with an invalid input parameter. */ - AUDIOSTREAM_ERROR_INVALID_PARAM, + AUDIOSTREAM_ERROR_INVALID_PARAM = 1, /** * Execution status exception. */ - AUDIOSTREAM_ERROR_ILLEGAL_STATE, + AUDIOSTREAM_ERROR_ILLEGAL_STATE = 2, /** * An system error has occurred. */ - AUDIOSTREAM_ERROR_SYSTEM + AUDIOSTREAM_ERROR_SYSTEM = 3 } OH_AudioStream_Result; /** @@ -72,11 +94,22 @@ typedef enum { * @since 10 */ typedef enum { + /** + * Unsigned 8 format. + */ AUDIOSTREAM_SAMPLE_U8 = 0, + /** + * Signed 16 bit integer, little endian. + */ AUDIOSTREAM_SAMPLE_S16LE = 1, + /** + * Signed 24 bit integer, little endian. + */ AUDIOSTREAM_SAMPLE_S24LE = 2, + /** + * Signed 32 bit integer, little endian. + */ AUDIOSTREAM_SAMPLE_S32LE = 3, - AUDIOSTREAM_SAMPLE_F32LE = 4, } OH_AudioStream_SampleFormat; /** @@ -85,6 +118,9 @@ typedef enum { * @since 10 */ typedef enum { + /** + * PCM encoding type. + */ AUDIOSTREAM_ENCODING_TYPE_RAW = 0, } OH_AudioStream_EncodingType; @@ -96,36 +132,87 @@ typedef enum { * @since 10 */ typedef enum { + /** + * Unknown usage. + */ AUDIOSTREAM_USAGE_UNKNOWN = 0, - AUDIOSTREAM_USAGE_MEDIA = 1, - AUDIOSTREAM_USAGE_COMMUNICATION = 2, + /** + * Music usage. + */ + AUDIOSTREAM_USAGE_MUSIC = 1, + /** + * Voice communication usage. + */ + AUDIOSTREAM_USAGE_VOICE_COMMUNICATION = 2, + /** + * Voice assistant usage. + */ + AUDIOSTREAM_USAGE_VOICE_ASSISTANT = 3, + /** + * Alarm usage. + */ + AUDIOSTREAM_USAGE_ALARM = 4, + /** + * Voice message usage. + */ + AUDIOSTREAM_USAGE_VOICE_MESSAGE = 5, + /** + * Ringtone usage. + */ + AUDIOSTREAM_USAGE_RINGTONE = 6, + /** + * Notification usage. + */ + AUDIOSTREAM_USAGE_NOTIFICATION = 7, + /** + * Accessibility usage, such as screen reader. + */ + AUDIOSTREAM_USAGE_ACCESSIBILITY = 8, + /** + * Movie or video usage. + */ + AUDIOSTREAM_USAGE_MOVIE = 10, + /** + * Game sound effect usage. + */ + AUDIOSTREAM_USAGE_GAME = 11, + /** + * Audiobook usage. + */ + AUDIOSTREAM_USAGE_AUDIOBOOK = 12, + /** + * Navigation usage. + */ + AUDIOSTREAM_USAGE_NAVIGATION = 13, } OH_AudioStream_Usage; /** - * Define the audio stream content. - * Audio stream content is used to describe the stream data - * type. + * Define the audio latency mode. * * @since 10 */ typedef enum { - AUDIOSTREAM_CONTENT_TYPE_UNKNOWN = 0, - AUDIOSTREAM_CONTENT_TYPE_SPEECH = 1, - AUDIOSTREAM_CONTENT_TYPE_MUSIC = 2, - AUDIOSTREAM_CONTENT_TYPE_MOVIE = 3, -} OH_AudioStream_Content; + /** + * This is a normal audio scene. + */ + AUDIOSTREAM_LATENCY_MODE_NORMAL = 0, + /** + * This is a low latency audio scene. + */ + AUDIOSTREAM_LATENCY_MODE_FAST = 1 +} OH_AudioStream_LatencyMode; /** - * Define the audio latency mode. + * Define the audio event. * * @since 10 */ typedef enum { /** - * This is a normal audio scene. + * The routing of the audio has changed. */ - AUDIOSTREAM_LATENCY_MODE_NORMAL, -} OH_AudioStream_LatencyMode; + AUDIOSTREAM_EVENT_ROUTING_CHANGED = 0 +} OH_AudioStream_Event; /** * The audio stream states @@ -137,6 +224,10 @@ typedef enum { * The invalid state. */ AUDIOSTREAM_STATE_INVALID = -1, + /** + * Create new instance state. + */ + AUDIOSTREAM_STATE_NEW = 0, /** * The prepared state. */ @@ -159,16 +250,79 @@ typedef enum { AUDIOSTREAM_STATE_PAUSED = 5, } OH_AudioStream_State; +/** + * Defines the audio interrupt type. + * + * @since 10 + */ +typedef enum { + /** + * Force type, system change audio state. + */ + AUDIOSTREAM_INTERRUPT_FORCE = 0, + /** + * Share type, application change audio state. + */ + AUDIOSTREAM_INTERRUPT_SHARE = 1 +} OH_AudioInterrupt_ForceType; + +/** + * Defines the audio interrupt hint type. + * + * @since 10 + */ +typedef enum { + /** + * None. + */ + AUDIOSTREAM_INTERRUPT_HINT_NONE = 0, + /** + * Resume the stream. + */ + AUDIOSTREAM_INTERRUPT_HINT_RESUME = 1, + /** + * Pause the stream. + */ + AUDIOSTREAM_INTERRUPT_HINT_PAUSE = 2, + /** + * Stop the stream. + */ + AUDIOSTREAM_INTERRUPT_HINT_STOP = 3, + /** + * Ducked the stream. + */ + AUDIOSTREAM_INTERRUPT_HINT_DUCK = 4, + /** + * Unducked the stream. + */ + AUDIOSTREAM_INTERRUPT_HINT_UNDUCK = 5 +} OH_AudioInterrupt_Hint; + /** * Defines the audio source type. * * @since 10 */ typedef enum { + /** + * Invalid type. + */ AUDIOSTREAM_SOURCE_TYPE_INVALID = -1, - AUDIOSTREAM_SOURCE_TYPE_MIC, + /** + * Mic source type. + */ + AUDIOSTREAM_SOURCE_TYPE_MIC = 0, + /** + * Voice recognition source type. + */ AUDIOSTREAM_SOURCE_TYPE_VOICE_RECOGNITION = 1, + /** + * Playback capture source type. + */ AUDIOSTREAM_SOURCE_TYPE_PLAYBACK_CAPTURE = 2, + /** + * Voice communication source type. + */ AUDIOSTREAM_SOURCE_TYPE_VOICE_COMMUNICATION = 7 } OH_AudioStream_SourceType; @@ -211,6 +365,34 @@ typedef struct OH_AudioRenderer_Callbacks_Struct { void* userData, void* buffer, int32_t lenth); + + /** + * This function pointer will point to the callback function that + * is used to handle audio renderer stream events. + */ + int32_t (*OH_AudioRenderer_OnStreamEvent)( + OH_AudioRenderer* renderer, + void* userData, + OH_AudioStream_Event event); + + /** + * This function pointer will point to the callback function that + * is used to handle audio interrupt events. + */ + int32_t (*OH_AudioRenderer_OnInterrptEvent)( + OH_AudioRenderer* renderer, + void* userData, + OH_AudioInterrupt_ForceType type, + OH_AudioInterrupt_Hint hint); + + /** + * This function pointer will point to the callback function that + * is used to handle audio error result. + */ + int32_t (*OH_AudioRenderer_OnError)( + OH_AudioRenderer* renderer, + void* userData, + OH_AudioStream_Result error); } OH_AudioRenderer_Callbacks; /** @@ -228,6 +410,34 @@ typedef struct OH_AudioCapturer_Callbacks_Struct { void* userData, void* buffer, int32_t lenth); + + /** + * This function pointer will point to the callback function that + * is used to handle audio capturer stream events. + */ + int32_t (*OH_AudioCapturer_OnStreamEvent)( + OH_AudioCapturer* capturer, + void* userData, + OH_AudioStream_Event event); + + /** + * This function pointer will point to the callback function that + * is used to handle audio interrupt events. + */ + int32_t (*OH_AudioCapturer_OnInterrptEvent)( + OH_AudioCapturer* renderer, + void* userData, + OH_AudioInterrupt_ForceType type, + OH_AudioInterrupt_Hint hint); + + /** + * This function pointer will point to the callback function that + * is used to handle audio error result. + */ + int32_t (*OH_AudioCapturer_OnError)( + OH_AudioCapturer* capturer, + void* userData, + OH_AudioStream_Result error); } OH_AudioCapturer_Callbacks; #ifdef __cplusplus } diff --git a/interfaces/kits/c/common/native_audiostreambuilder.h b/interfaces/kits/c/common/native_audiostreambuilder.h index 6a66bf075f..99d94288e4 100644 --- a/interfaces/kits/c/common/native_audiostreambuilder.h +++ b/interfaces/kits/c/common/native_audiostreambuilder.h @@ -13,8 +13,30 @@ * limitations under the License. */ -#ifndef ST_NATIVE_AUDIOSTREAM_BUILDER_H -#define ST_NATIVE_AUDIOSTREAM_BUILDER_H +/** + * @addtogroup OHAudio + * @{ + * + * @brief Provide the definition of the C interface for the audio module. + * + * @syscap SystemCapability.Multimedia.Audio.Core + * + * @since 10 + * @version 1.0 + */ + +/** + * @file native_audiostreambuilder.h + * + * @brief Declare audio stream builder related interfaces. + * + * @syscap SystemCapability.Multimedia.Audio.Core + * @since 10 + * @version 1.0 + */ + +#ifndef NATIVE_AUDIOSTREAM_BUILDER_H +#define NATIVE_AUDIOSTREAM_BUILDER_H #include "native_audiostream_base.h" #include "native_audiorenderer.h" @@ -112,11 +134,10 @@ OH_AudioStream_Result OH_AudioStreamBuilder_SetLatencyMode(OH_AudioStreamBuilder * * @param builder Reference provided by OH_AudioStreamBuilder_Create() * @param usage Set the stream usage for the renderer client. - * @param content Set the stream content for the renderer client. * @return {@link #AUDIOSTREAM_SUCCESS} or an undesired error. */ OH_AudioStream_Result OH_AudioStreamBuilder_SetRendererInfo(OH_AudioStreamBuilder* builder, - OH_AudioStream_Usage usage, OH_AudioStream_Content content); + OH_AudioStream_Usage usage); /* * Set the capturer information of the stream client @@ -182,4 +203,4 @@ OH_AudioStream_Result OH_AudioStreamBuilder_GenerateCapturer(OH_AudioStreamBuild } #endif -#endif // ST_NATIVE_AUDIOSTREAM_BUILDER_H +#endif // NATIVE_AUDIOSTREAM_BUILDER_H -- Gitee From 4e06b04d5978c7783a975a0ad0dab8f7e08a652a Mon Sep 17 00:00:00 2001 From: zhanhang Date: Wed, 5 Jul 2023 19:15:17 +0800 Subject: [PATCH 2/3] remove OH_AudioStream_Content Signed-off-by: zhanhang --- frameworks/native/ohaudio/OHAudioRenderer.cpp | 3 +-- .../native/ohaudio/OHAudioStreamBuilder.cpp | 4 ++-- .../src/oh_audio_render_unit_test.cpp | 3 +-- .../src/oh_audio_stream_builder_unit_test.cpp | 18 ++++++------------ 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/frameworks/native/ohaudio/OHAudioRenderer.cpp b/frameworks/native/ohaudio/OHAudioRenderer.cpp index 7d125ea922..e65ed882b7 100644 --- a/frameworks/native/ohaudio/OHAudioRenderer.cpp +++ b/frameworks/native/ohaudio/OHAudioRenderer.cpp @@ -138,7 +138,7 @@ OH_AudioStream_Result OH_AudioRenderer_GetLatencyMode(OH_AudioRenderer* renderer } OH_AudioStream_Result OH_AudioRenderer_GetRendererInfo(OH_AudioRenderer* renderer, - OH_AudioStream_Usage* usage, OH_AudioStream_Content* content) + OH_AudioStream_Usage* usage) { OHOS::AudioStandard::OHAudioRenderer *audioRenderer = convertRenderer(renderer); CHECK_AND_RETURN_RET_LOG(audioRenderer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert renderer failed"); @@ -146,7 +146,6 @@ OH_AudioStream_Result OH_AudioRenderer_GetRendererInfo(OH_AudioRenderer* rendere OHOS::AudioStandard::AudioRendererInfo rendererInfo; audioRenderer->GetRendererInfo(rendererInfo); *usage = (OH_AudioStream_Usage)rendererInfo.streamUsage; - *content = (OH_AudioStream_Content)rendererInfo.contentType; return AUDIOSTREAM_SUCCESS; } diff --git a/frameworks/native/ohaudio/OHAudioStreamBuilder.cpp b/frameworks/native/ohaudio/OHAudioStreamBuilder.cpp index 651c23b3b9..f8e56f145f 100644 --- a/frameworks/native/ohaudio/OHAudioStreamBuilder.cpp +++ b/frameworks/native/ohaudio/OHAudioStreamBuilder.cpp @@ -78,12 +78,12 @@ OH_AudioStream_Result OH_AudioStreamBuilder_SetLatencyMode(OH_AudioStreamBuilder } OH_AudioStream_Result OH_AudioStreamBuilder_SetRendererInfo(OH_AudioStreamBuilder* builder, - OH_AudioStream_Usage usage, OH_AudioStream_Content content) + OH_AudioStream_Usage usage) { OHAudioStreamBuilder *audioStreamBuilder = convertBuilder(builder); CHECK_AND_RETURN_RET_LOG(audioStreamBuilder != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert builder failed"); StreamUsage streamUsage = (StreamUsage)usage; - ContentType contentType = (ContentType)content; + ContentType contentType = ContentType::CONTENT_TYPE_MUSIC; return audioStreamBuilder->SetRendererInfo(streamUsage, contentType); } diff --git a/frameworks/native/ohaudio/test/unittest/oh_audio_render_test/src/oh_audio_render_unit_test.cpp b/frameworks/native/ohaudio/test/unittest/oh_audio_render_test/src/oh_audio_render_unit_test.cpp index ba4a69afc2..6ea1935271 100644 --- a/frameworks/native/ohaudio/test/unittest/oh_audio_render_test/src/oh_audio_render_unit_test.cpp +++ b/frameworks/native/ohaudio/test/unittest/oh_audio_render_test/src/oh_audio_render_unit_test.cpp @@ -477,8 +477,7 @@ HWTEST(OHAudioRenderUnitTest, OH_Audio_Render_GetRendererInfo_001, TestSize.Leve OH_AudioStream_Result result = OH_AudioStreamBuilder_GenerateRenderer(builder, &audioRenderer); OH_AudioStream_Usage usage; - OH_AudioStream_Content content; - result = OH_AudioRenderer_GetRendererInfo(audioRenderer, &usage, &content); + result = OH_AudioRenderer_GetRendererInfo(audioRenderer, &usage); EXPECT_TRUE(result == AUDIOSTREAM_SUCCESS); EXPECT_TRUE(usage == AUDIOSTREAM_USAGE_MEDIA); EXPECT_TRUE(content == AUDIOSTREAM_CONTENT_TYPE_MUSIC); diff --git a/frameworks/native/ohaudio/test/unittest/oh_audio_stream_builder_test/src/oh_audio_stream_builder_unit_test.cpp b/frameworks/native/ohaudio/test/unittest/oh_audio_stream_builder_test/src/oh_audio_stream_builder_unit_test.cpp index 9cd12eaa03..f15ad105c9 100644 --- a/frameworks/native/ohaudio/test/unittest/oh_audio_stream_builder_test/src/oh_audio_stream_builder_unit_test.cpp +++ b/frameworks/native/ohaudio/test/unittest/oh_audio_stream_builder_test/src/oh_audio_stream_builder_unit_test.cpp @@ -268,8 +268,7 @@ HWTEST(OHAudioStreamBuilderUnitTest, OH_AudioStreamBuilder_SetParameter_008, Tes EXPECT_EQ(result, AUDIOSTREAM_SUCCESS); OH_AudioStream_Usage usage = AUDIOSTREAM_USAGE_MEDIA; - OH_AudioStream_Content content = AUDIOSTREAM_CONTENT_TYPE_MUSIC; - result = OH_AudioStreamBuilder_SetRendererInfo(builder, usage, content); + result = OH_AudioStreamBuilder_SetRendererInfo(builder, usage); EXPECT_TRUE(result == AUDIOSTREAM_ERROR_INVALID_PARAM); result = OH_AudioStreamBuilder_Destroy(builder); @@ -289,8 +288,7 @@ HWTEST(OHAudioStreamBuilderUnitTest, OH_AudioStreamBuilder_SetParameter_009, Tes EXPECT_EQ(result, AUDIOSTREAM_SUCCESS); OH_AudioStream_Usage usage = AUDIOSTREAM_USAGE_COMMUNICATION; - OH_AudioStream_Content content = AUDIOSTREAM_CONTENT_TYPE_SPEECH; - result = OH_AudioStreamBuilder_SetRendererInfo(builder, usage, content); + result = OH_AudioStreamBuilder_SetRendererInfo(builder, usage); EXPECT_EQ(result, AUDIOSTREAM_SUCCESS); result = OH_AudioStreamBuilder_Destroy(builder); @@ -311,8 +309,7 @@ HWTEST(OHAudioStreamBuilderUnitTest, OH_AudioStreamBuilder_SetParameter_010, Tes EXPECT_EQ(result, AUDIOSTREAM_SUCCESS); OH_AudioStream_Usage usage = AUDIOSTREAM_USAGE_COMMUNICATION; - OH_AudioStream_Content content = AUDIOSTREAM_CONTENT_TYPE_SPEECH; - result = OH_AudioStreamBuilder_SetRendererInfo(builder, usage, content); + result = OH_AudioStreamBuilder_SetRendererInfo(builder, usage); EXPECT_TRUE(result == AUDIOSTREAM_ERROR_INVALID_PARAM); result = OH_AudioStreamBuilder_Destroy(builder); @@ -645,8 +642,7 @@ HWTEST(OHAudioStreamBuilderUnitTest, OH_AudioStreamBuilder_SetRendererInfo_001, EXPECT_EQ(result, AUDIOSTREAM_SUCCESS); OH_AudioStream_Usage usage = AUDIOSTREAM_USAGE_UNKNOWN; - OH_AudioStream_Content content = AUDIOSTREAM_CONTENT_TYPE_MUSIC; - result = OH_AudioStreamBuilder_SetRendererInfo(builder, usage, content); + result = OH_AudioStreamBuilder_SetRendererInfo(builder, usage); EXPECT_TRUE(result == AUDIOSTREAM_ERROR_INVALID_PARAM); result = OH_AudioStreamBuilder_Destroy(builder); @@ -667,8 +663,7 @@ HWTEST(OHAudioStreamBuilderUnitTest, OH_AudioStreamBuilder_SetRendererInfo_002, EXPECT_EQ(result, AUDIOSTREAM_SUCCESS); OH_AudioStream_Usage usage = AUDIOSTREAM_USAGE_MEDIA; - OH_AudioStream_Content content = AUDIOSTREAM_CONTENT_TYPE_UNKNOWN; - result = OH_AudioStreamBuilder_SetRendererInfo(builder, usage, content); + result = OH_AudioStreamBuilder_SetRendererInfo(builder, usage); EXPECT_TRUE(result == AUDIOSTREAM_ERROR_INVALID_PARAM); result = OH_AudioStreamBuilder_Destroy(builder); @@ -688,8 +683,7 @@ HWTEST(OHAudioStreamBuilderUnitTest, OH_AudioStreamBuilder_SetRendererInfo_003, EXPECT_EQ(result, AUDIOSTREAM_SUCCESS); OH_AudioStream_Usage usage = AUDIOSTREAM_USAGE_MEDIA; - OH_AudioStream_Content content = AUDIOSTREAM_CONTENT_TYPE_MOVIE; - result = OH_AudioStreamBuilder_SetRendererInfo(builder, usage, content); + result = OH_AudioStreamBuilder_SetRendererInfo(builder, usage); EXPECT_EQ(result, AUDIOSTREAM_SUCCESS); result = OH_AudioStreamBuilder_Destroy(builder); -- Gitee From 0f646bdd8d303bc246fd2beff910973fada0f5ef Mon Sep 17 00:00:00 2001 From: zhanhang Date: Mon, 26 Jun 2023 15:02:55 +0800 Subject: [PATCH 3/3] add interface Signed-off-by: zhanhang Change-Id: Id0c0d5c1144dd38f937f19fbcfc98055de0c1d7d --- .../include/audio_capturer_private.h | 1 + .../audiocapturer/src/audio_capturer.cpp | 5 + .../include/audio_renderer_private.h | 1 + .../audiorenderer/src/audio_renderer.cpp | 5 + .../native/audiostream/include/audio_stream.h | 2 + frameworks/native/ohaudio/OHAudioCapturer.cpp | 48 ++++++++ frameworks/native/ohaudio/OHAudioCapturer.h | 2 + frameworks/native/ohaudio/OHAudioRenderer.cpp | 42 ++++++- frameworks/native/ohaudio/OHAudioRenderer.h | 3 +- .../test/example/oh_audio_renderer_test.cpp | 13 ++- .../audiocapturer/include/audio_capturer.h | 8 ++ .../audiorenderer/include/audio_renderer.h | 7 ++ .../kits/c/common/native_audiostream_base.h | 110 ++++++++++++++++++ .../client/include/audio_service_client.h | 14 +++ .../client/src/audio_service_client.cpp | 18 +++ .../audio_service/client/src/audio_stream.cpp | 15 +++ 16 files changed, 289 insertions(+), 5 deletions(-) diff --git a/frameworks/native/audiocapturer/include/audio_capturer_private.h b/frameworks/native/audiocapturer/include/audio_capturer_private.h index b9a57418b1..7a2c7acdbe 100644 --- a/frameworks/native/audiocapturer/include/audio_capturer_private.h +++ b/frameworks/native/audiocapturer/include/audio_capturer_private.h @@ -59,6 +59,7 @@ public: int32_t GetBufQueueState(BufferQueueState &bufState)const override; void SetApplicationCachePath(const std::string cachePath) override; void SetValid(bool valid) override; + int64_t GetFramesRead() const override; std::shared_ptr audioStream_; AudioCapturerInfo capturerInfo_ = {}; diff --git a/frameworks/native/audiocapturer/src/audio_capturer.cpp b/frameworks/native/audiocapturer/src/audio_capturer.cpp index 61fad3ca31..005c449e1c 100644 --- a/frameworks/native/audiocapturer/src/audio_capturer.cpp +++ b/frameworks/native/audiocapturer/src/audio_capturer.cpp @@ -603,5 +603,10 @@ void AudioCapturerPrivate::SetValid(bool valid) std::lock_guard lock(lock_); isValid_ = valid; } + +int64_t AudioCapturerPrivate::GetFramesRead() const +{ + return audioStream_->GetFramesWritten(); +} } // namespace AudioStandard } // namespace OHOS diff --git a/frameworks/native/audiorenderer/include/audio_renderer_private.h b/frameworks/native/audiorenderer/include/audio_renderer_private.h index bfbdd621fe..a7dad16baa 100644 --- a/frameworks/native/audiorenderer/include/audio_renderer_private.h +++ b/frameworks/native/audiorenderer/include/audio_renderer_private.h @@ -87,6 +87,7 @@ public: int32_t UnregisterAudioPolicyServerDiedCb(const int32_t clientPid) override; void DestroyAudioRendererStateCallback() override; AudioEffectMode GetAudioEffectMode() const override; + int64_t GetFramesWritten() const override; int32_t SetAudioEffectMode(AudioEffectMode effectMode) const override; AudioRendererInfo rendererInfo_ = {}; diff --git a/frameworks/native/audiorenderer/src/audio_renderer.cpp b/frameworks/native/audiorenderer/src/audio_renderer.cpp index 4bb27ce928..caca021061 100644 --- a/frameworks/native/audiorenderer/src/audio_renderer.cpp +++ b/frameworks/native/audiorenderer/src/audio_renderer.cpp @@ -974,6 +974,11 @@ AudioEffectMode AudioRendererPrivate::GetAudioEffectMode() const return audioStream_->GetAudioEffectMode(); } +int64_t AudioRendererPrivate::GetFramesWritten() const +{ + return audioStream_->GetFramesWritten(); +} + int32_t AudioRendererPrivate::SetAudioEffectMode(AudioEffectMode effectMode) const { return audioStream_->SetAudioEffectMode(effectMode); diff --git a/frameworks/native/audiostream/include/audio_stream.h b/frameworks/native/audiostream/include/audio_stream.h index a311a7a837..1aa389df05 100644 --- a/frameworks/native/audiostream/include/audio_stream.h +++ b/frameworks/native/audiostream/include/audio_stream.h @@ -77,6 +77,8 @@ public: float GetLowPowerVolume(); float GetSingleStreamVolume(); AudioEffectMode GetAudioEffectMode(); + int64_t GetFramesWritten(); + int64_t GetFramesRead(); int32_t SetAudioEffectMode(AudioEffectMode effectMode); std::vector GetSupportedFormats() const; diff --git a/frameworks/native/ohaudio/OHAudioCapturer.cpp b/frameworks/native/ohaudio/OHAudioCapturer.cpp index 53bbfb9ec6..1cc2dc7b8a 100644 --- a/frameworks/native/ohaudio/OHAudioCapturer.cpp +++ b/frameworks/native/ohaudio/OHAudioCapturer.cpp @@ -157,6 +157,38 @@ OH_AudioStream_Result OH_AudioCapturer_GetCapturerInfo(OH_AudioCapturer* capture return AUDIOSTREAM_SUCCESS; } +OH_AudioStream_Result OH_AudioCapturer_GetFrameSizeInCallback(OH_AudioCapturer* capturer, int32_t* frameSize) +{ + OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer); + CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed"); + + *frameSize = audioCapturer->GetFrameSizeInCallback(); + return AUDIOSTREAM_SUCCESS; +} + +OH_AudioStream_Result OH_AudioCapturer_GetTimestamp(OH_AudioCapturer* capturer, + clockid_t clockId, int64_t* framePosition, int64_t* timestamp) +{ + OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer); + CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed"); + + Timestamp stamp; + Timestamp::Timestampbase base = Timestamp::Timestampbase::MONOTONIC; + audioCapturer->GetAudioTime(stamp, base); + *framePosition = stamp.framePosition; + *timestamp = stamp.time.tv_nsec; + return AUDIOSTREAM_SUCCESS; +} + +OH_AudioStream_Result OH_AudioCapturer_GetFramesRead(OH_AudioCapturer* capturer, int64_t* frames) +{ + OHOS::AudioStandard::OHAudioCapturer *audioCapturer = convertCapturer(capturer); + CHECK_AND_RETURN_RET_LOG(audioCapturer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert capturer failed"); + + *frames = audioCapturer->GetFramesRead(); + return AUDIOSTREAM_SUCCESS; +} + namespace OHOS { namespace AudioStandard { OHAudioCapturer::OHAudioCapturer() @@ -269,12 +301,28 @@ void OHAudioCapturer::SetCapturerReadCallback(OH_AudioCapturer_Callbacks callbac } } +int64_t OHAudioCapturer::GetFramesRead() +{ + CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr"); + uint32_t frameCount = 0; + audioCapturer_->GetFrameCount(frameCount); + return (int64_t)frameCount; +} + void OHAudioCapturer::GetAudioTime(Timestamp ×tamp, Timestamp::Timestampbase base) { CHECK_AND_RETURN_LOG(audioCapturer_ != nullptr, "capturer client is nullptr"); audioCapturer_->GetAudioTime(timestamp, base); } +int32_t OHAudioCapturer::GetFrameSizeInCallback() +{ + CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr"); + BufferDesc bufDesc; + audioCapturer_->GetBufferDesc(bufDesc); + return bufDesc.bufLength; +} + int32_t OHAudioCapturer::GetBufferDesc(BufferDesc &bufDesc) const { CHECK_AND_RETURN_RET_LOG(audioCapturer_ != nullptr, ERROR, "capturer client is nullptr"); diff --git a/frameworks/native/ohaudio/OHAudioCapturer.h b/frameworks/native/ohaudio/OHAudioCapturer.h index 39ab135561..d5fe2c6e8a 100644 --- a/frameworks/native/ohaudio/OHAudioCapturer.h +++ b/frameworks/native/ohaudio/OHAudioCapturer.h @@ -56,7 +56,9 @@ class OHAudioCapturer { AudioEncodingType GetEncodingType(); AudioSampleFormat GetSampleFormat(); void GetCapturerInfo(AudioCapturerInfo& capturerInfo); + int64_t GetFramesRead(); void GetAudioTime(Timestamp ×tamp, Timestamp::Timestampbase base); + int32_t GetFrameSizeInCallback(); int32_t GetBufferDesc(BufferDesc &bufDesc) const; int32_t Enqueue(const BufferDesc &bufDesc) const; diff --git a/frameworks/native/ohaudio/OHAudioRenderer.cpp b/frameworks/native/ohaudio/OHAudioRenderer.cpp index e65ed882b7..2279245abb 100644 --- a/frameworks/native/ohaudio/OHAudioRenderer.cpp +++ b/frameworks/native/ohaudio/OHAudioRenderer.cpp @@ -158,6 +158,35 @@ OH_AudioStream_Result OH_AudioRenderer_GetEncodingType(OH_AudioRenderer* rendere return AUDIOSTREAM_SUCCESS; } +OH_AudioStream_Result OH_AudioRenderer_GetFramesWritten(OH_AudioRenderer* renderer, int64_t* frames) +{ + OHOS::AudioStandard::OHAudioRenderer *audioRenderer = convertRenderer(renderer); + CHECK_AND_RETURN_RET_LOG(audioRenderer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert renderer failed"); + *frames = audioRenderer->GetFramesWritten(); + return AUDIOSTREAM_SUCCESS; +} + +OH_AudioStream_Result OH_AudioRenderer_GetTimestamp(OH_AudioRenderer* renderer, + clockid_t clockId, int64_t* framePosition, int64_t* timestamp) +{ + OHOS::AudioStandard::OHAudioRenderer *audioRenderer = convertRenderer(renderer); + CHECK_AND_RETURN_RET_LOG(audioRenderer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert renderer failed"); + Timestamp stamp; + Timestamp::Timestampbase base = Timestamp::Timestampbase::MONOTONIC; + audioRenderer->GetAudioTime(stamp, base); + *framePosition = stamp.framePosition; + *timestamp = stamp.time.tv_nsec; + return AUDIOSTREAM_SUCCESS; +} + +OH_AudioStream_Result OH_AudioRenderer_GetFrameSizeInCallback(OH_AudioRenderer* renderer, int32_t* frameSize) +{ + OHOS::AudioStandard::OHAudioRenderer *audioRenderer = convertRenderer(renderer); + CHECK_AND_RETURN_RET_LOG(audioRenderer != nullptr, AUDIOSTREAM_ERROR_INVALID_PARAM, "convert renderer failed"); + *frameSize = audioRenderer->GetFrameSizeInCallback(); + return AUDIOSTREAM_SUCCESS; +} + namespace OHOS { namespace AudioStandard { OHAudioRenderer::OHAudioRenderer() @@ -258,9 +287,10 @@ AudioEncodingType OHAudioRenderer::GetEncodingType() return params.encodingType; } -int32_t OHAudioRenderer::GetFramesWritten() +int64_t OHAudioRenderer::GetFramesWritten() { - return 0; + CHECK_AND_RETURN_RET_LOG(audioRenderer_ != nullptr, ERROR, "renderer client is nullptr"); + return audioRenderer_->GetFramesWritten(); } void OHAudioRenderer::GetAudioTime(Timestamp ×tamp, Timestamp::Timestampbase base) @@ -269,6 +299,14 @@ void OHAudioRenderer::GetAudioTime(Timestamp ×tamp, Timestamp::Timestampbas audioRenderer_->GetAudioTime(timestamp, base); } +int32_t OHAudioRenderer::GetFrameSizeInCallback() +{ + CHECK_AND_RETURN_RET_LOG(audioRenderer_ != nullptr, ERROR, "renderer client is nullptr"); + size_t bufSize; + audioRenderer_->GetBufferSize(bufSize); + return (int32_t)bufSize; +} + int32_t OHAudioRenderer::GetBufferDesc(BufferDesc &bufDesc) const { CHECK_AND_RETURN_RET_LOG(audioRenderer_ != nullptr, ERROR, "renderer client is nullptr"); diff --git a/frameworks/native/ohaudio/OHAudioRenderer.h b/frameworks/native/ohaudio/OHAudioRenderer.h index c1165f4a0e..25d9c44150 100644 --- a/frameworks/native/ohaudio/OHAudioRenderer.h +++ b/frameworks/native/ohaudio/OHAudioRenderer.h @@ -55,9 +55,10 @@ class OHAudioRenderer { int32_t GetSamplingRate(); AudioSampleFormat GetSampleFormat(); AudioEncodingType GetEncodingType(); - int32_t GetFramesWritten(); + int64_t GetFramesWritten(); void GetRendererInfo(AudioRendererInfo& rendererInfo); void GetAudioTime(Timestamp ×tamp, Timestamp::Timestampbase base); + int32_t GetFrameSizeInCallback(); int32_t GetBufferDesc(BufferDesc &bufDesc) const; int32_t Enqueue(const BufferDesc &bufDesc) const; diff --git a/frameworks/native/ohaudio/test/example/oh_audio_renderer_test.cpp b/frameworks/native/ohaudio/test/example/oh_audio_renderer_test.cpp index 5188f936f2..fc8a5cd3fe 100644 --- a/frameworks/native/ohaudio/test/example/oh_audio_renderer_test.cpp +++ b/frameworks/native/ohaudio/test/example/oh_audio_renderer_test.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "common/native_audiostreambuilder.h" #include "native_audiorenderer.h" @@ -83,13 +84,21 @@ void PlayerTest(char *argv[]) // 4. start ret = OH_AudioRenderer_Start(audioRenderer); printf("start ret: %d \n", ret); + int32_t frameSize; + OH_AudioRenderer_GetFrameSizeInCallback(audioRenderer, &frameSize); + printf("framesize: %d \n", frameSize); int timer = 0; while (!g_readEnd) { std::this_thread::sleep_for(std::chrono::milliseconds(AudioTestConstants::WAIT_INTERVAL)); - printf("Wait for the audio to finish playing.(..%d s)\n", ++timer); + int64_t frames; + OH_AudioRenderer_GetFramesWritten(audioRenderer, &frames); + printf("Wait for the audio to finish playing.(..%d s) frames:%lld\n", ++timer, frames); + int64_t framePosition; + int64_t timestamp; + OH_AudioRenderer_GetTimestamp(audioRenderer, CLOCK_MONOTONIC, &framePosition, ×tamp); + printf("framePosition %lld timestamp:%lld\n", framePosition, timestamp); } - // 5. stop and release client ret = OH_AudioRenderer_Stop(audioRenderer); printf("stop ret: %d \n", ret); diff --git a/interfaces/inner_api/native/audiocapturer/include/audio_capturer.h b/interfaces/inner_api/native/audiocapturer/include/audio_capturer.h index f33589f3ad..7392fa5cd3 100644 --- a/interfaces/inner_api/native/audiocapturer/include/audio_capturer.h +++ b/interfaces/inner_api/native/audiocapturer/include/audio_capturer.h @@ -511,6 +511,14 @@ public: */ virtual void SetValid(bool valid) = 0; + /** + * @brief Gets the audio frame size that has been read. + * + * @return Returns the audio frame size that has been read. + * @since 10 + */ + virtual int64_t GetFramesRead() const = 0; + virtual ~AudioCapturer(); }; } // namespace AudioStandard diff --git a/interfaces/inner_api/native/audiorenderer/include/audio_renderer.h b/interfaces/inner_api/native/audiorenderer/include/audio_renderer.h index 8a4f620ee7..bfcc9bc439 100644 --- a/interfaces/inner_api/native/audiorenderer/include/audio_renderer.h +++ b/interfaces/inner_api/native/audiorenderer/include/audio_renderer.h @@ -705,6 +705,13 @@ public: */ virtual AudioEffectMode GetAudioEffectMode() const = 0; + /** + * @brief Gets the audio frame size that has been written. + * + * @return Returns the audio frame size that has been written. + */ + virtual int64_t GetFramesWritten() const = 0; + /** * @brief Sets the audio effect mode. * diff --git a/interfaces/kits/c/common/native_audiostream_base.h b/interfaces/kits/c/common/native_audiostream_base.h index 989feba32a..9270d8437b 100644 --- a/interfaces/kits/c/common/native_audiostream_base.h +++ b/interfaces/kits/c/common/native_audiostream_base.h @@ -52,21 +52,29 @@ extern "C" { typedef enum { /** * The call was successful. + * + * @since 10 */ AUDIOSTREAM_SUCCESS = 0, /** * This means that the function was executed with an invalid input parameter. + * + * @since 10 */ AUDIOSTREAM_ERROR_INVALID_PARAM = 1, /** * Execution status exception. + * + * @since 10 */ AUDIOSTREAM_ERROR_ILLEGAL_STATE = 2, /** * An system error has occurred. + * + * @since 10 */ AUDIOSTREAM_ERROR_SYSTEM = 3 } OH_AudioStream_Result; @@ -79,11 +87,15 @@ typedef enum { typedef enum { /** * The type for audio stream is renderer. + * + * @since 10 */ AUDIOSTREAM_TYPE_RENDERER = 1, /** * The type for audio stream is capturer. + * + * @since 10 */ AUDIOSTREAM_TYPE_CAPTURER = 2 } OH_AudioStream_Type; @@ -96,18 +108,26 @@ typedef enum { typedef enum { /** * Unsigned 8 format. + * + * @since 10 */ AUDIOSTREAM_SAMPLE_U8 = 0, /** * Signed 16 bit integer, little endian. + * + * @since 10 */ AUDIOSTREAM_SAMPLE_S16LE = 1, /** * Signed 24 bit integer, little endian. + * + * @since 10 */ AUDIOSTREAM_SAMPLE_S24LE = 2, /** * Signed 32 bit integer, little endian. + * + * @since 10 */ AUDIOSTREAM_SAMPLE_S32LE = 3, } OH_AudioStream_SampleFormat; @@ -120,6 +140,8 @@ typedef enum { typedef enum { /** * PCM encoding type. + * + * @since 10 */ AUDIOSTREAM_ENCODING_TYPE_RAW = 0, } OH_AudioStream_EncodingType; @@ -134,54 +156,80 @@ typedef enum { typedef enum { /** * Unknown usage. + * + * @since 10 */ AUDIOSTREAM_USAGE_UNKNOWN = 0, /** * Music usage. + * + * @since 10 */ AUDIOSTREAM_USAGE_MUSIC = 1, /** * Voice communication usage. + * + * @since 10 */ AUDIOSTREAM_USAGE_VOICE_COMMUNICATION = 2, /** * Voice assistant usage. + * + * @since 10 */ AUDIOSTREAM_USAGE_VOICE_ASSISTANT = 3, /** * Alarm usage. + * + * @since 10 */ AUDIOSTREAM_USAGE_ALARM = 4, /** * Voice message usage. + * + * @since 10 */ AUDIOSTREAM_USAGE_VOICE_MESSAGE = 5, /** * Ringtone usage. + * + * @since 10 */ AUDIOSTREAM_USAGE_RINGTONE = 6, /** * Notification usage. + * + * @since 10 */ AUDIOSTREAM_USAGE_NOTIFICATION = 7, /** * Accessibility usage, such as screen reader. + * + * @since 10 */ AUDIOSTREAM_USAGE_ACCESSIBILITY = 8, /** * Movie or video usage. + * + * @since 10 */ AUDIOSTREAM_USAGE_MOVIE = 10, /** * Game sound effect usage. + * + * @since 10 */ AUDIOSTREAM_USAGE_GAME = 11, /** * Audiobook usage. + * + * @since 10 */ AUDIOSTREAM_USAGE_AUDIOBOOK = 12, /** * Navigation usage. + * + * @since 10 */ AUDIOSTREAM_USAGE_NAVIGATION = 13, } OH_AudioStream_Usage; @@ -194,10 +242,14 @@ typedef enum { typedef enum { /** * This is a normal audio scene. + * + * @since 10 */ AUDIOSTREAM_LATENCY_MODE_NORMAL = 0, /** * This is a low latency audio scene. + * + * @since 10 */ AUDIOSTREAM_LATENCY_MODE_FAST = 1 } OH_AudioStream_LatencyMode; @@ -210,6 +262,8 @@ typedef enum { typedef enum { /** * The routing of the audio has changed. + * + * @since 10 */ AUDIOSTREAM_EVENT_ROUTING_CHANGED = 0 } OH_AudioStream_Event; @@ -222,30 +276,44 @@ typedef enum { typedef enum { /** * The invalid state. + * + * @since 10 */ AUDIOSTREAM_STATE_INVALID = -1, /** * Create new instance state. + * + * @since 10 */ AUDIOSTREAM_STATE_NEW = 0, /** * The prepared state. + * + * @since 10 */ AUDIOSTREAM_STATE_PREPARED = 1, /** * The stream is running. + * + * @since 10 */ AUDIOSTREAM_STATE_RUNNING = 2, /** * The stream is stopped. + * + * @since 10 */ AUDIOSTREAM_STATE_STOPPED = 3, /** * The stream is released. + * + * @since 10 */ AUDIOSTREAM_STATE_RELEASED = 4, /** * The stream is paused. + * + * @since 10 */ AUDIOSTREAM_STATE_PAUSED = 5, } OH_AudioStream_State; @@ -258,10 +326,14 @@ typedef enum { typedef enum { /** * Force type, system change audio state. + * + * @since 10 */ AUDIOSTREAM_INTERRUPT_FORCE = 0, /** * Share type, application change audio state. + * + * @since 10 */ AUDIOSTREAM_INTERRUPT_SHARE = 1 } OH_AudioInterrupt_ForceType; @@ -274,26 +346,38 @@ typedef enum { typedef enum { /** * None. + * + * @since 10 */ AUDIOSTREAM_INTERRUPT_HINT_NONE = 0, /** * Resume the stream. + * + * @since 10 */ AUDIOSTREAM_INTERRUPT_HINT_RESUME = 1, /** * Pause the stream. + * + * @since 10 */ AUDIOSTREAM_INTERRUPT_HINT_PAUSE = 2, /** * Stop the stream. + * + * @since 10 */ AUDIOSTREAM_INTERRUPT_HINT_STOP = 3, /** * Ducked the stream. + * + * @since 10 */ AUDIOSTREAM_INTERRUPT_HINT_DUCK = 4, /** * Unducked the stream. + * + * @since 10 */ AUDIOSTREAM_INTERRUPT_HINT_UNDUCK = 5 } OH_AudioInterrupt_Hint; @@ -306,22 +390,32 @@ typedef enum { typedef enum { /** * Invalid type. + * + * @since 10 */ AUDIOSTREAM_SOURCE_TYPE_INVALID = -1, /** * Mic source type. + * + * @since 10 */ AUDIOSTREAM_SOURCE_TYPE_MIC = 0, /** * Voice recognition source type. + * + * @since 10 */ AUDIOSTREAM_SOURCE_TYPE_VOICE_RECOGNITION = 1, /** * Playback capture source type. + * + * @since 10 */ AUDIOSTREAM_SOURCE_TYPE_PLAYBACK_CAPTURE = 2, /** * Voice communication source type. + * + * @since 10 */ AUDIOSTREAM_SOURCE_TYPE_VOICE_COMMUNICATION = 7 } OH_AudioStream_SourceType; @@ -359,6 +453,8 @@ typedef struct OH_AudioRenderer_Callbacks_Struct { /** * This function pointer will point to the callback function that * is used to write audio data + * + * @since 10 */ int32_t (*OH_AudioRenderer_OnWriteData)( OH_AudioRenderer* renderer, @@ -369,6 +465,8 @@ typedef struct OH_AudioRenderer_Callbacks_Struct { /** * This function pointer will point to the callback function that * is used to handle audio renderer stream events. + * + * @since 10 */ int32_t (*OH_AudioRenderer_OnStreamEvent)( OH_AudioRenderer* renderer, @@ -378,6 +476,8 @@ typedef struct OH_AudioRenderer_Callbacks_Struct { /** * This function pointer will point to the callback function that * is used to handle audio interrupt events. + * + * @since 10 */ int32_t (*OH_AudioRenderer_OnInterrptEvent)( OH_AudioRenderer* renderer, @@ -388,6 +488,8 @@ typedef struct OH_AudioRenderer_Callbacks_Struct { /** * This function pointer will point to the callback function that * is used to handle audio error result. + * + * @since 10 */ int32_t (*OH_AudioRenderer_OnError)( OH_AudioRenderer* renderer, @@ -404,6 +506,8 @@ typedef struct OH_AudioCapturer_Callbacks_Struct { /** * This function pointer will point to the callback function that * is used to read audio data. + * + * @since 10 */ int32_t (*OH_AudioCapturer_OnReadData)( OH_AudioCapturer* capturer, @@ -414,6 +518,8 @@ typedef struct OH_AudioCapturer_Callbacks_Struct { /** * This function pointer will point to the callback function that * is used to handle audio capturer stream events. + * + * @since 10 */ int32_t (*OH_AudioCapturer_OnStreamEvent)( OH_AudioCapturer* capturer, @@ -423,6 +529,8 @@ typedef struct OH_AudioCapturer_Callbacks_Struct { /** * This function pointer will point to the callback function that * is used to handle audio interrupt events. + * + * @since 10 */ int32_t (*OH_AudioCapturer_OnInterrptEvent)( OH_AudioCapturer* renderer, @@ -433,6 +541,8 @@ typedef struct OH_AudioCapturer_Callbacks_Struct { /** * This function pointer will point to the callback function that * is used to handle audio error result. + * + * @since 10 */ int32_t (*OH_AudioCapturer_OnError)( OH_AudioCapturer* capturer, diff --git a/services/audio_service/client/include/audio_service_client.h b/services/audio_service/client/include/audio_service_client.h index 0094a33ddc..bde47e64d0 100644 --- a/services/audio_service/client/include/audio_service_client.h +++ b/services/audio_service/client/include/audio_service_client.h @@ -490,6 +490,20 @@ public: */ AudioEffectMode GetStreamAudioEffectMode(); + /** + * @brief Gets the audio frame size that has been written. + * + * @return Returns the audio frame size that has been written. + */ + int64_t GetStreamFramesWritten(); + + /** + * @brief Gets the audio frame size that has been read. + * + * @return Returns the audio frame size that has been read. + */ + int64_t GetStreamFramesRead(); + /** * @brief Sets the audio effect mode. * diff --git a/services/audio_service/client/src/audio_service_client.cpp b/services/audio_service/client/src/audio_service_client.cpp index 153e3a4fd4..62299752c3 100644 --- a/services/audio_service/client/src/audio_service_client.cpp +++ b/services/audio_service/client/src/audio_service_client.cpp @@ -2897,6 +2897,24 @@ AudioEffectMode AudioServiceClient::GetStreamAudioEffectMode() return effectMode; } +int64_t AudioServiceClient::GetStreamFramesWritten() +{ + if (mFrameSize == 0) { + AUDIO_ERR_LOG("Error frame size"); + return ERROR; + } + return mTotalBytesWritten / mFrameSize; +} + +int64_t AudioServiceClient::GetStreamFramesRead() +{ + if (mFrameSize == 0) { + AUDIO_ERR_LOG("Error frame size"); + return ERROR; + } + return mTotalBytesRead / mFrameSize; +} + int32_t AudioServiceClient::SetStreamAudioEffectMode(AudioEffectMode audioEffectMode) { AUDIO_INFO_LOG("SetStreamAudioEffectMode: %{public}d", audioEffectMode); diff --git a/services/audio_service/client/src/audio_stream.cpp b/services/audio_service/client/src/audio_stream.cpp index 77d7fed413..9fb1a84776 100644 --- a/services/audio_service/client/src/audio_stream.cpp +++ b/services/audio_service/client/src/audio_stream.cpp @@ -175,6 +175,11 @@ bool AudioStream::GetAudioTime(Timestamp ×tamp, Timestamp::Timestampbase ba resetTime_ = false; resetTimestamp_ = paTimeStamp; } + if (eMode_ == AUDIO_MODE_PLAYBACK) { + timestamp.framePosition = GetStreamFramesWritten(); + } else { + timestamp.framePosition = GetStreamFramesRead(); + } timestamp.time.tv_sec = static_cast((paTimeStamp - resetTimestamp_) / TIME_CONVERSION_US_S); timestamp.time.tv_nsec @@ -996,5 +1001,15 @@ void AudioStream::SetPrivacyType(AudioPrivacyType privacyType) { SetStreamPrivacyType(privacyType); } + +int64_t AudioStream::GetFramesWritten() +{ + return GetStreamFramesWritten(); +} + +int64_t AudioStream::GetFramesRead() +{ + return GetStreamFramesRead(); +} } // namespace AudioStandard } // namespace OHOS -- Gitee