Skip to content

Commit

Permalink
unify API - the API
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinszkudlinski committed Oct 16, 2024
1 parent 2403bbc commit aff0ac6
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 18 deletions.
122 changes: 122 additions & 0 deletions src/audio/buffers/audio_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,125 @@ void audio_buffer_free(struct sof_audio_buffer *buffer)
buffer->ops->free(buffer);
rfree(buffer);
}

static
int audio_buffer_source_set_ipc_params_default(struct sof_audio_buffer *buffer,
struct sof_ipc_stream_params *params,
bool force_update)
{
CORE_CHECK_STRUCT(buffer);

if (audio_buffer_hw_params_configured(buffer) && !force_update)
return 0;

struct sof_audio_stream_params *audio_stream_params =
audio_buffer_get_stream_params(buffer);

audio_stream_params->frame_fmt = params->frame_fmt;
audio_stream_params->rate = params->rate;
audio_stream_params->channels = params->channels;
audio_stream_params->buffer_fmt = params->buffer_fmt;

audio_buffer_set_hw_params_configured(buffer);

if (buffer->ops->on_audio_format_set)
return buffer->ops->on_audio_format_set(buffer);
return 0;
}

static
int audio_buffer_sink_set_ipc_params(struct sof_sink *sink, struct sof_ipc_stream_params *params,
bool force_update)
{
struct sof_audio_buffer *buffer = sof_audio_buffer_from_sink(sink);

if (buffer->ops->audio_set_ipc_params)
return buffer->ops->audio_set_ipc_params(buffer, params, force_update);
return audio_buffer_source_set_ipc_params_default(buffer, params, force_update);
}

static
int audio_buffer_sink_on_audio_format_set(struct sof_sink *sink)
{
struct sof_audio_buffer *buffer = sof_audio_buffer_from_sink(sink);

if (buffer->ops->on_audio_format_set)
return buffer->ops->on_audio_format_set(buffer);
return 0;
}

static
int audio_buffer_sink_set_alignment_constants(struct sof_sink *sink,
const uint32_t byte_align,
const uint32_t frame_align_req)
{
struct sof_audio_buffer *buffer = sof_audio_buffer_from_sink(sink);

if (buffer->ops->set_alignment_constants)
return buffer->ops->set_alignment_constants(buffer, byte_align, frame_align_req);
return 0;
}

static
int audio_buffer_source_set_ipc_params(struct sof_source *source,
struct sof_ipc_stream_params *params, bool force_update)
{
struct sof_audio_buffer *buffer = sof_audio_buffer_from_source(source);

if (buffer->ops->audio_set_ipc_params)
return buffer->ops->audio_set_ipc_params(buffer, params, force_update);
return audio_buffer_source_set_ipc_params_default(buffer, params, force_update);
}

static
int audio_buffer_source_on_audio_format_set(struct sof_source *source)
{
struct sof_audio_buffer *buffer = sof_audio_buffer_from_source(source);

if (buffer->ops->on_audio_format_set)
return buffer->ops->on_audio_format_set(buffer);
return 0;
}

static
int audio_buffer_source_set_alignment_constants(struct sof_source *source,
const uint32_t byte_align,
const uint32_t frame_align_req)
{
struct sof_audio_buffer *buffer = sof_audio_buffer_from_source(source);

if (buffer->ops->set_alignment_constants)
return buffer->ops->set_alignment_constants(buffer, byte_align, frame_align_req);
return 0;
}

void audio_buffer_init(struct sof_audio_buffer *buffer, uint32_t buffer_type, bool is_shared,
struct source_ops *source_ops, struct sink_ops *sink_ops,
const struct audio_buffer_ops *audio_buffer_ops,
struct sof_audio_stream_params *audio_stream_params)
{
CORE_CHECK_STRUCT_INIT(&buffer, is_shared);
buffer->buffer_type = buffer_type;
buffer->ops = audio_buffer_ops;
buffer->audio_stream_params = audio_stream_params;
buffer->is_shared = is_shared;

if (!sink_ops->audio_set_ipc_params)
sink_ops->audio_set_ipc_params = audio_buffer_sink_set_ipc_params;
if (!sink_ops->on_audio_format_set && buffer->ops->on_audio_format_set)
sink_ops->on_audio_format_set = audio_buffer_sink_on_audio_format_set;
if (!sink_ops->set_alignment_constants && buffer->ops->set_alignment_constants)
sink_ops->set_alignment_constants = audio_buffer_sink_set_alignment_constants;

if (!source_ops->audio_set_ipc_params)
source_ops->audio_set_ipc_params = audio_buffer_source_set_ipc_params;
if (!source_ops->on_audio_format_set && buffer->ops->on_audio_format_set)
source_ops->on_audio_format_set = audio_buffer_source_on_audio_format_set;
if (!source_ops->set_alignment_constants && buffer->ops->set_alignment_constants)
source_ops->set_alignment_constants = audio_buffer_source_set_alignment_constants;

source_init(audio_buffer_get_source(buffer), source_ops,
audio_buffer_get_stream_params(buffer));
sink_init(audio_buffer_get_sink(buffer), sink_ops,
audio_buffer_get_stream_params(buffer));
}
4 changes: 2 additions & 2 deletions src/audio/buffers/comp_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ static void comp_buffer_free(struct sof_audio_buffer *audio_buffer)
rfree(buffer->stream.addr);
}

static const struct source_ops comp_buffer_source_ops = {
static struct source_ops comp_buffer_source_ops = {
.get_data_available = comp_buffer_get_data_available,
.get_data = comp_buffer_get_data,
.release_data = comp_buffer_release_data,
Expand All @@ -199,7 +199,7 @@ static const struct source_ops comp_buffer_source_ops = {
.set_alignment_constants = comp_buffer_source_set_alignment_constants
};

static const struct sink_ops comp_buffer_sink_ops = {
static struct sink_ops comp_buffer_sink_ops = {
.get_free_size = comp_buffer_get_free_size,
.get_buffer = comp_buffer_get_buffer,
.commit_buffer = comp_buffer_commit_buffer,
Expand Down
4 changes: 2 additions & 2 deletions src/audio/buffers/ring_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,14 @@ static int ring_buffer_set_ipc_params_sink(struct sof_sink *sink,
return ring_buffer_set_ipc_params(ring_buffer, params, force_update);
}

static const struct source_ops ring_buffer_source_ops = {
static struct source_ops ring_buffer_source_ops = {
.get_data_available = ring_buffer_get_data_available,
.get_data = ring_buffer_get_data,
.release_data = ring_buffer_release_data,
.audio_set_ipc_params = ring_buffer_set_ipc_params_source,
};

static const struct sink_ops ring_buffer_sink_ops = {
static struct sink_ops ring_buffer_sink_ops = {
.get_free_size = ring_buffer_get_free_size,
.get_buffer = ring_buffer_get_buffer,
.commit_buffer = ring_buffer_commit_buffer,
Expand Down
43 changes: 29 additions & 14 deletions src/include/sof/audio/audio_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ struct audio_buffer_ops {
* OPTIONAL
*/
void (*clean)(struct sof_audio_buffer *buffer);


/**
* OPTIONAL: Notification to the sink implementation about changes in audio format
*
* Once any of *audio_stream_params elements changes, the implementation of
* sink may need to perform some extra operations.
* This callback will be called immediately after any change
*
* @retval 0 if success, negative if new parameters are not supported
*/
int (*on_audio_format_set)(struct sof_audio_buffer *buffer);

/**
* OPTIONAL
* see sink_set_params comments
*/
int (*audio_set_ipc_params)(struct sof_audio_buffer *buffer,
struct sof_ipc_stream_params *params, bool force_update);

/**
* OPTIONAL
* see comment for sink_set_alignment_constants
*/
int (*set_alignment_constants)(struct sof_audio_buffer *buffer,
const uint32_t byte_align,
const uint32_t frame_align_req);
};

/* base class for all buffers, all buffers must inherit from it */
Expand Down Expand Up @@ -278,22 +305,10 @@ static inline struct sof_audio_buffer *sof_audio_buffer_from_source(struct sof_s
* @param audio_buffer_ops pointer to required buffer virtual methods implementation
* @param audio_stream_params pointer to audio stream (currently kept in buffer implementation)
*/
static inline
void audio_buffer_init(struct sof_audio_buffer *buffer, uint32_t buffer_type, bool is_shared,
const struct source_ops *source_ops, const struct sink_ops *sink_ops,
struct source_ops *source_ops, struct sink_ops *sink_ops,
const struct audio_buffer_ops *audio_buffer_ops,
struct sof_audio_stream_params *audio_stream_params)
{
CORE_CHECK_STRUCT_INIT(&buffer, is_shared);
buffer->buffer_type = buffer_type;
buffer->ops = audio_buffer_ops;
buffer->audio_stream_params = audio_stream_params;
buffer->is_shared = is_shared;
source_init(audio_buffer_get_source(buffer), source_ops,
audio_buffer_get_stream_params(buffer));
sink_init(audio_buffer_get_sink(buffer), sink_ops,
audio_buffer_get_stream_params(buffer));
}
struct sof_audio_stream_params *audio_stream_params);

/**
* @brief free buffer and all allocated resources
Expand Down

0 comments on commit aff0ac6

Please sign in to comment.