Skip to content

Commit 58f5d26

Browse files
fixup: audio: mux: factor out circular buffer frame count helper
Signed-off-by: Piotr Hoppe <piotr.hoppe@intel.com>
1 parent c7cb65c commit 58f5d26

6 files changed

Lines changed: 198 additions & 95 deletions

File tree

src/audio/buffers/comp_buffer.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ static int comp_buffer_sink_get_state(struct sof_sink *sink)
107107
return comp_get_state(comp_buffer_get_sink_component(buffer));
108108
}
109109

110+
static int comp_buffer_source_get_state(struct sof_source *source)
111+
{
112+
struct comp_buffer *buffer = comp_buffer_get_from_source(source);
113+
114+
return comp_get_state(comp_buffer_get_source_component(buffer));
115+
}
116+
110117
static int comp_buffer_set_ipc_params(struct sof_audio_buffer *audio_buffer,
111118
struct sof_ipc_stream_params *params,
112119
bool force_update)
@@ -186,6 +193,7 @@ APP_TASK_DATA static const struct source_ops comp_buffer_source_ops = {
186193
.audio_set_ipc_params = audio_buffer_source_set_ipc_params,
187194
.on_audio_format_set = audio_buffer_source_on_audio_format_set,
188195
.set_alignment_constants = audio_buffer_source_set_alignment_constants,
196+
.get_state = comp_buffer_source_get_state,
189197
};
190198

191199
APP_TASK_DATA static const struct sink_ops comp_buffer_sink_ops = {

src/audio/mux/mux.c

Lines changed: 75 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ static int get_stream_index(struct comp_dev *dev, struct comp_data *cd, uint32_t
156156
}
157157

158158
static void mux_prepare_active_look_up(struct comp_data *cd,
159-
struct audio_stream *sink,
160-
const struct audio_stream **sources)
159+
struct sof_sink *sink,
160+
struct sof_source **sources)
161161
{
162-
const struct audio_stream *source;
162+
struct sof_source *source;
163163
int elem;
164164
int active_elem = 0;
165165

@@ -169,8 +169,8 @@ static void mux_prepare_active_look_up(struct comp_data *cd,
169169
if (!source)
170170
continue;
171171

172-
if (cd->lookup[0].copy_elem[elem].in_ch >= audio_stream_get_channels(source) ||
173-
cd->lookup[0].copy_elem[elem].out_ch >= audio_stream_get_channels(sink))
172+
if (cd->lookup[0].copy_elem[elem].in_ch >= source_get_channels(source) ||
173+
cd->lookup[0].copy_elem[elem].out_ch >= sink_get_channels(sink))
174174
continue;
175175

176176
cd->active_lookup.copy_elem[active_elem] = cd->lookup[0].copy_elem[elem];
@@ -327,58 +327,88 @@ static int demux_trigger(struct processing_module *mod, int cmd)
327327

328328
/* process and copy stream data from source to sink buffers */
329329
static int mux_process(struct processing_module *mod,
330-
struct input_stream_buffer *input_buffers, int num_input_buffers,
331-
struct output_stream_buffer *output_buffers, int num_output_buffers)
330+
struct sof_source **sources, int num_of_sources,
331+
struct sof_sink **sinks, int num_of_sinks)
332332
{
333333
struct comp_data *cd = module_get_private_data(mod);
334334
struct comp_dev *dev = mod->dev;
335-
struct comp_buffer *source;
336-
const struct audio_stream *sources_stream[MUX_MAX_STREAMS] = { NULL };
337-
int frames = 0;
338-
int sink_bytes;
339-
int i, j;
335+
struct sof_sink *sink = sinks[0];
336+
struct sof_source *active_sources[MUX_MAX_STREAMS] = { NULL };
337+
struct cir_buf_source source_bufs[MUX_MAX_STREAMS] = { 0 };
338+
size_t source_bytes[MUX_MAX_STREAMS] = { 0 };
339+
struct cir_buf_sink sink_buf;
340+
size_t sink_bytes, size;
341+
uint32_t frames;
342+
int i, idx, ret;
340343

341344
comp_dbg(dev, "entry");
342345

343-
/* align source streams with their respective configurations */
344-
j = 0;
345-
comp_dev_for_each_producer(dev, source) {
346-
if (comp_buffer_get_source_state(source) == dev->state) {
347-
if (frames)
348-
frames = MIN(frames, input_buffers[j].size);
349-
else
350-
frames = input_buffers[j].size;
351-
352-
i = get_stream_index(dev, cd, buffer_pipeline_id(source));
353-
/* return if index wrong */
354-
if (i < 0) {
355-
return i;
356-
}
346+
/* if there are no sources or sinks active, then there is nothing to do */
347+
if (num_of_sinks == 0 || num_of_sources == 0)
348+
return 0;
357349

358-
sources_stream[i] = &source->stream;
359-
}
360-
j++;
350+
/* the same number of frames is taken from every active source and
351+
* written to the single sink, so it is limited by both the sink free
352+
* space and every active source's availability
353+
*/
354+
frames = sink_get_free_frames(sink);
355+
for (i = 0; i < num_of_sources; i++) {
356+
struct sof_source *source = sources[i];
357+
358+
/* skip sources that are not in the same state as the component */
359+
if (source_get_comp_state(source) != dev->state)
360+
continue;
361+
362+
/* map the source to its configured stream index */
363+
idx = get_stream_index(dev, cd, source_get_pipeline_id(source));
364+
if (idx < 0)
365+
return idx;
366+
367+
active_sources[idx] = source;
368+
frames = MIN(frames, source_get_data_frames_available(source));
361369
}
362370

363-
/* check if there are any sources active */
364-
if (num_input_buffers == 0)
371+
if (!frames)
365372
return 0;
366373

367-
sink_bytes = frames * audio_stream_frame_bytes(mod->output_buffers[0].data);
368-
mux_prepare_active_look_up(cd, output_buffers[0].data, &sources_stream[0]);
374+
/* build the active routing table for the current connections */
375+
mux_prepare_active_look_up(cd, sink, active_sources);
369376

370-
/* produce output */
371-
cd->mux(dev, output_buffers[0].data, &sources_stream[0], frames, &cd->active_lookup);
372-
373-
/* Update consumed per source using each source's own frame size */
374-
j = 0;
375-
comp_dev_for_each_producer(dev, source) {
376-
if (comp_buffer_get_source_state(source) == dev->state)
377-
mod->input_buffers[j].consumed =
378-
frames * audio_stream_frame_bytes(mod->input_buffers[j].data);
379-
j++;
377+
/* acquire the single sink circular buffer for the whole period */
378+
sink_bytes = frames * sink_get_frame_bytes(sink);
379+
ret = sink_get_buffer(sink, sink_bytes, &sink_buf.ptr, &sink_buf.buf_start, &size);
380+
if (ret)
381+
return ret;
382+
sink_buf.buf_end = (char *)sink_buf.buf_start + size;
383+
384+
/* acquire every active source circular buffer */
385+
for (i = 0; i < MUX_MAX_STREAMS; i++) {
386+
if (!active_sources[i])
387+
continue;
388+
389+
source_bytes[i] = frames * source_get_frame_bytes(active_sources[i]);
390+
ret = source_get_data(active_sources[i], source_bytes[i], &source_bufs[i].ptr,
391+
&source_bufs[i].buf_start, &size);
392+
if (ret) {
393+
/* release the sources acquired so far and the sink */
394+
for (--i; i >= 0; i--)
395+
if (active_sources[i])
396+
source_release_data(active_sources[i], 0);
397+
sink_commit_buffer(sink, 0);
398+
return ret;
399+
}
400+
source_bufs[i].buf_end = (const char *)source_bufs[i].buf_start + size;
380401
}
381-
mod->output_buffers[0].size = sink_bytes;
402+
403+
/* produce output */
404+
cd->mux(dev, sink, &sink_buf, active_sources, source_bufs, frames, &cd->active_lookup);
405+
406+
/* commit the produced data and consume from every active source */
407+
sink_commit_buffer(sink, sink_bytes);
408+
for (i = 0; i < MUX_MAX_STREAMS; i++)
409+
if (active_sources[i])
410+
source_release_data(active_sources[i], source_bytes[i]);
411+
382412
return 0;
383413
}
384414

@@ -465,7 +495,7 @@ static const struct module_interface mux_interface = {
465495
.set_configuration = mux_set_config,
466496
.get_configuration = mux_get_config,
467497
.prepare = mux_prepare,
468-
.process_audio_stream = mux_process,
498+
.process = mux_process,
469499
.reset = mux_reset,
470500
.free = mux_free,
471501
};

src/audio/mux/mux.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
#endif
2929
struct comp_buffer;
3030
struct comp_dev;
31+
struct sof_source;
32+
struct sof_sink;
33+
struct cir_buf_source;
34+
struct cir_buf_sink;
3135

3236
/** \brief Supported streams count. */
3337
#if CONFIG_IPC_MAJOR_3
@@ -70,8 +74,9 @@ typedef int(*demux_func)(struct comp_dev *dev, struct sof_sink *sink,
7074
struct sof_source *source, const void *source_data,
7175
const void *source_start, size_t source_size,
7276
uint32_t frames, struct mux_look_up *look_up);
73-
typedef void(*mux_func)(struct comp_dev *dev, struct audio_stream *sink,
74-
const struct audio_stream **sources, uint32_t frames,
77+
typedef void(*mux_func)(struct comp_dev *dev, struct sof_sink *sink,
78+
struct cir_buf_sink *sink_buf, struct sof_source **sources,
79+
struct cir_buf_source *source_bufs, uint32_t frames,
7580
struct mux_look_up *look_up);
7681

7782
/**

0 commit comments

Comments
 (0)