Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sink / source API #7622

Merged
merged 10 commits into from
Jun 19, 2023
1 change: 1 addition & 0 deletions src/audio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if((NOT CONFIG_LIBRARY) OR CONFIG_LIBRARY_STATIC)
source_api_helper.c
sink_api_helper.c
sink_source_utils.c
audio_stream.c
channel_map.c
)
if(CONFIG_COMP_BLOB)
Expand Down
175 changes: 175 additions & 0 deletions src/audio/audio_stream.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2023 Intel Corporation. All rights reserved.
//

#include <sof/audio/audio_stream.h>
#include <sof/audio/buffer.h>

static size_t audio_stream_get_free_size(struct sof_sink __sparse_cache *sink)
{
struct audio_stream __sparse_cache *audio_stream =
attr_container_of(sink, struct audio_stream __sparse_cache,
sink_api, __sparse_cache);

return audio_stream_get_free_bytes(audio_stream);
}

static int audio_stream_get_buffer(struct sof_sink __sparse_cache *sink, size_t req_size,
void **data_ptr, void **buffer_start, size_t *buffer_size)
{
struct audio_stream __sparse_cache *audio_stream =
attr_container_of(sink, struct audio_stream __sparse_cache,
sink_api, __sparse_cache);

if (req_size > audio_stream_get_free_size(sink))
return -ENODATA;

/* get circular buffer parameters */
*data_ptr = audio_stream->w_ptr;
*buffer_start = audio_stream->addr;
*buffer_size = audio_stream->size;
return 0;
}

static int audio_stream_commit_buffer(struct sof_sink __sparse_cache *sink, size_t commit_size)
{
struct audio_stream __sparse_cache *audio_stream =
attr_container_of(sink, struct audio_stream __sparse_cache,
sink_api, __sparse_cache);

if (commit_size)
audio_stream_produce(audio_stream, commit_size);

return 0;
}

static size_t audio_stream_get_data_available(struct sof_source __sparse_cache *source)
{
struct audio_stream __sparse_cache *audio_stream =
attr_container_of(source, struct audio_stream __sparse_cache,
source_api, __sparse_cache);

return audio_stream_get_avail_bytes(audio_stream);
}

static int audio_stream_get_data(struct sof_source __sparse_cache *source, size_t req_size,
void **data_ptr, void **buffer_start, size_t *buffer_size)
{
struct audio_stream __sparse_cache *audio_stream =
attr_container_of(source, struct audio_stream __sparse_cache,
source_api, __sparse_cache);

if (req_size > audio_stream_get_data_available(source))
return -ENODATA;

/* get circular buffer parameters */
*data_ptr = audio_stream->r_ptr;
*buffer_start = audio_stream->addr;
*buffer_size = audio_stream->size;
return 0;
}

static int audio_stream_release_data(struct sof_source __sparse_cache *source, size_t free_size)
{
struct audio_stream __sparse_cache *audio_stream =
attr_container_of(source, struct audio_stream __sparse_cache,
source_api, __sparse_cache);

if (free_size)
audio_stream_consume(audio_stream, free_size);

return 0;
}

static int audio_stream_set_ipc_params_source(struct sof_source __sparse_cache *source,
struct sof_ipc_stream_params *params,
bool force_update)
{
struct audio_stream __sparse_cache *audio_stream =
attr_container_of(source, struct audio_stream __sparse_cache,
source_api, __sparse_cache);
struct comp_buffer __sparse_cache *buffer =
attr_container_of(audio_stream, struct comp_buffer __sparse_cache,
stream, __sparse_cache);

return buffer_set_params(buffer, params, force_update);
}

static int audio_stream_set_ipc_params_sink(struct sof_sink __sparse_cache *sink,
struct sof_ipc_stream_params *params,
bool force_update)
{
struct audio_stream __sparse_cache *audio_stream =
attr_container_of(sink, struct audio_stream __sparse_cache,
sink_api, __sparse_cache);
struct comp_buffer __sparse_cache *buffer =
attr_container_of(audio_stream, struct comp_buffer __sparse_cache,
stream, __sparse_cache);

return buffer_set_params(buffer, params, force_update);
}

static int audio_stream_source_set_alignment_constants(struct sof_source __sparse_cache *source,
const uint32_t byte_align,
const uint32_t frame_align_req)
{
struct audio_stream __sparse_cache *audio_stream =
attr_container_of(source, struct audio_stream __sparse_cache,
source_api, __sparse_cache);

audio_stream_init_alignment_constants(byte_align, frame_align_req, audio_stream);

return 0;
}

static int audio_stream_sink_set_alignment_constants(struct sof_sink __sparse_cache *sink,
const uint32_t byte_align,
const uint32_t frame_align_req)
{
struct audio_stream __sparse_cache *audio_stream =
attr_container_of(sink, struct audio_stream __sparse_cache,
sink_api, __sparse_cache);

audio_stream_init_alignment_constants(byte_align, frame_align_req, audio_stream);

return 0;
}

static const struct source_ops audio_stream_source_ops = {
.get_data_available = audio_stream_get_data_available,
.get_data = audio_stream_get_data,
.release_data = audio_stream_release_data,
.audio_set_ipc_params = audio_stream_set_ipc_params_source,
.set_alignment_constants = audio_stream_source_set_alignment_constants
};

static const struct sink_ops audio_stream_sink_ops = {
.get_free_size = audio_stream_get_free_size,
.get_buffer = audio_stream_get_buffer,
.commit_buffer = audio_stream_commit_buffer,
.audio_set_ipc_params = audio_stream_set_ipc_params_sink,
.set_alignment_constants = audio_stream_sink_set_alignment_constants
};

void audio_stream_init(struct audio_stream __sparse_cache *audio_stream,
void *buff_addr, uint32_t size)
{
audio_stream->size = size;
audio_stream->addr = buff_addr;
audio_stream->end_addr = (char *)audio_stream->addr + size;

/* set the default alignment info.
* set byte_align as 1 means no alignment limit on byte.
* set frame_align as 1 means no alignment limit on frame.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea what this means either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a limited calculation frame number function audio_stream_avail_frames_aligned, it use shift to replace division, the byte_align and frame_align is for shift index calculation.

*/
audio_stream_init_alignment_constants(1, 1, audio_stream);

source_init(audio_stream_get_source(audio_stream), &audio_stream_source_ops,
(__sparse_force struct sof_audio_stream_params *)
&audio_stream->runtime_stream_params);
sink_init(audio_stream_get_sink(audio_stream), &audio_stream_sink_ops,
(__sparse_force struct sof_audio_stream_params *)
&audio_stream->runtime_stream_params);
audio_stream_reset(audio_stream);
}
43 changes: 28 additions & 15 deletions src/include/sof/audio/audio_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#define __SOF_AUDIO_AUDIO_STREAM_H__

#include <sof/audio/format.h>
#include <sof/audio/sink_api.h>
#include <sof/audio/sink_api_implementation.h>
#include <sof/audio/source_api.h>
#include <sof/audio/source_api_implementation.h>
marcinszkudlinski marked this conversation as resolved.
Show resolved Hide resolved
#include <sof/compiler_attributes.h>
#include <rtos/panic.h>
#include <sof/math/numbers.h>
Expand Down Expand Up @@ -73,8 +77,13 @@ struct sof_audio_stream_params {
* consumption/production and update the buffer state by calling
* audio_stream_consume()/audio_stream_produce() (just a single call following
* series of reads/writes).
*
* Audio stream implements pipeline2.0 sink and source API
*/
struct audio_stream {
struct sof_source source_api; /**< source API, don't modify, use helper functions only */
struct sof_sink sink_api; /**< sink API, don't modify, use helper functions only */

/* runtime data */
uint32_t size; /**< Runtime buffer size in bytes (period multiple) */
uint32_t avail; /**< Available bytes for reading */
Expand Down Expand Up @@ -714,24 +723,12 @@ static inline void audio_stream_reset(struct audio_stream __sparse_cache *buffer

/**
* Initializes the buffer with specified memory block and size.
* @param buffer Buffer to initialize.
* @param audio_stream the audio_stream a to initialize.
* @param buff_addr Address of the memory block to assign.
* @param size Size of the memory block in bytes.
*/
static inline void audio_stream_init(struct audio_stream __sparse_cache *buffer,
void *buff_addr, uint32_t size)
{
buffer->size = size;
buffer->addr = buff_addr;
buffer->end_addr = (char *)buffer->addr + size;

/* set the default alignment info.
* set byte_align as 1 means no alignment limit on byte.
* set frame_align as 1 means no alignment limit on frame.
*/
audio_stream_init_alignment_constants(1, 1, buffer);
audio_stream_reset(buffer);
}
void audio_stream_init(struct audio_stream __sparse_cache *audio_stream,
void *buff_addr, uint32_t size);

/**
* Invalidates (in DSP d-cache) the buffer in range [r_ptr, r_ptr+bytes],
Expand Down Expand Up @@ -969,6 +966,22 @@ static inline void audio_stream_fmt_conversion(enum ipc4_bit_depth depth,
*valid_fmt = SOF_IPC_FRAME_FLOAT;
}
}

/** get a handler to source API
* NOTE! to use the handlers the buffer must be acquired by buffer_acquire
*/
static inline struct sof_source __sparse_cache *
audio_stream_get_source(struct audio_stream __sparse_cache *audio_stream)
{
return &audio_stream->source_api;
}

static inline struct sof_sink __sparse_cache *
audio_stream_get_sink(struct audio_stream __sparse_cache *audio_stream)
{
return &audio_stream->sink_api;
}
marcinszkudlinski marked this conversation as resolved.
Show resolved Hide resolved

/** @}*/

#endif /* __SOF_AUDIO_AUDIO_STREAM_H__ */
4 changes: 4 additions & 0 deletions test/cmocka/src/audio/buffer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cmocka_test(buffer_copy
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c

${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c
Expand All @@ -28,6 +29,7 @@ cmocka_test(buffer_new
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c
Expand All @@ -48,6 +50,7 @@ cmocka_test(buffer_wrap
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c
Expand All @@ -68,6 +71,7 @@ cmocka_test(buffer_write
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c
Expand Down
1 change: 1 addition & 0 deletions test/cmocka/src/audio/component/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cmocka_test(comp_set_state
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c
Expand Down
1 change: 1 addition & 0 deletions test/cmocka/src/audio/eq_fir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_library(audio_for_eq_fir STATIC
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/src/audio/component.c
${PROJECT_SOURCE_DIR}/src/audio/data_blob.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
Expand Down
1 change: 1 addition & 0 deletions test/cmocka/src/audio/eq_iir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_library(audio_for_eq_iir STATIC
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/src/audio/component.c
${PROJECT_SOURCE_DIR}/src/audio/data_blob.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
Expand Down
1 change: 1 addition & 0 deletions test/cmocka/src/audio/mixer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cmocka_test(mixer
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/src/audio/component.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c
Expand Down
1 change: 1 addition & 0 deletions test/cmocka/src/audio/mux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_library(
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/src/math/numbers.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c
Expand Down
1 change: 1 addition & 0 deletions test/cmocka/src/audio/pcm_converter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ if(CONFIG_FORMAT_FLOAT)
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/src/audio/component.c
${PROJECT_SOURCE_DIR}/src/audio/data_blob.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
Expand Down
3 changes: 3 additions & 0 deletions test/cmocka/src/audio/pipeline/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cmocka_test(pipeline_new
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c
Expand All @@ -49,6 +50,7 @@ cmocka_test(pipeline_connect_upstream
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c
Expand All @@ -70,6 +72,7 @@ cmocka_test(pipeline_free
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c
Expand Down
1 change: 1 addition & 0 deletions test/cmocka/src/audio/selector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ add_library(audio_for_selector STATIC
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c
Expand Down
1 change: 1 addition & 0 deletions test/cmocka/src/audio/volume/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ add_library(audio_for_volume STATIC
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c
Expand Down
1 change: 1 addition & 0 deletions test/cmocka/src/math/fft/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cmocka_test(fft
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c
Expand Down
1 change: 1 addition & 0 deletions zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ zephyr_library_sources(
${SOF_AUDIO_PATH}/source_api_helper.c
${SOF_AUDIO_PATH}/sink_api_helper.c
${SOF_AUDIO_PATH}/sink_source_utils.c
${SOF_AUDIO_PATH}/audio_stream.c
${SOF_AUDIO_PATH}/component.c
${SOF_AUDIO_PATH}/pipeline/pipeline-graph.c
${SOF_AUDIO_PATH}/pipeline/pipeline-params.c
Expand Down