Rename buffer status to make it more symetrical

Only try to pull once in a timeout.
We can reuse the last returned state as our internal state in
audiomixer.
This commit is contained in:
Wim Taymans 2017-04-28 11:49:13 +02:00
parent 430b749d53
commit cc8a5da9c7
11 changed files with 58 additions and 65 deletions

View file

@ -1207,7 +1207,7 @@ pinos_stream_send_buffer (PinosStream *stream,
bid->used = true;
spa_list_remove (&bid->link);
impl->trans->outputs[0].buffer_id = id;
impl->trans->outputs[0].status = SPA_RESULT_HAVE_OUTPUT;
impl->trans->outputs[0].status = SPA_RESULT_HAVE_BUFFER;
pinos_log_trace ("stream %p: send buffer %d", stream, id);
if (!impl->in_need_buffer)
send_have_output (stream);

View file

@ -855,7 +855,7 @@ spa_proxy_node_process_output (SpaNode *node)
pinos_log_trace ("%d %d %d %d", io->status, io->buffer_id, tmp.status, tmp.buffer_id);
if (io->status == SPA_RESULT_NEED_INPUT)
if (io->status == SPA_RESULT_NEED_BUFFER)
send_need = true;
*io = tmp;
@ -865,7 +865,7 @@ spa_proxy_node_process_output (SpaNode *node)
else if (flush)
do_flush (this);
return SPA_RESULT_HAVE_OUTPUT;
return SPA_RESULT_HAVE_BUFFER;
}
static SpaResult

View file

@ -273,7 +273,7 @@ do_pull (PinosNode *this)
pinos_log_trace ("node %p: need input port %d, %d %d", this,
inport->port_id, pi->buffer_id, pi->status);
if (pi->status != SPA_RESULT_NEED_INPUT)
if (pi->status != SPA_RESULT_NEED_BUFFER)
continue;
spa_list_for_each (link, &inport->rt.links, rt.input_link) {
@ -291,15 +291,15 @@ do_pull (PinosNode *this)
res = spa_node_process_output (outport->node->node);
if (res == SPA_RESULT_NEED_INPUT) {
if (res == SPA_RESULT_NEED_BUFFER) {
res = do_pull (outport->node);
pinos_log_trace ("node %p: pull return %d", outport->node, res);
}
else if (res < 0 && res != SPA_RESULT_HAVE_OUTPUT) {
else if (res < 0 && res != SPA_RESULT_HAVE_BUFFER) {
pinos_log_warn ("node %p: got process output %d", outport->node, res);
}
if (res == SPA_RESULT_HAVE_OUTPUT) {
if (res == SPA_RESULT_HAVE_BUFFER) {
*pi = *po;
pinos_log_trace ("node %p: have output %d %d", this, pi->status, pi->buffer_id);
have_output = true;
@ -360,7 +360,7 @@ on_node_event (SpaNode *node, SpaEvent *event, void *user_data)
}
}
res = spa_node_process_output (this->node);
if (res < 0 && res != SPA_RESULT_HAVE_OUTPUT)
if (res < 0 && res != SPA_RESULT_HAVE_BUFFER)
pinos_log_warn ("node %p: got process output %d", this, res);
}

View file

@ -41,8 +41,8 @@ typedef enum {
SPA_RESULT_NO_FORMAT = -4,
SPA_RESULT_INVALID_COMMAND = -5,
SPA_RESULT_INVALID_PORT = -6,
SPA_RESULT_HAVE_OUTPUT = -7,
SPA_RESULT_NEED_INPUT = -8,
SPA_RESULT_HAVE_BUFFER = -7,
SPA_RESULT_NEED_BUFFER = -8,
SPA_RESULT_PORTS_CHANGED = -9,
SPA_RESULT_FORMAT_CHANGED = -10,
SPA_RESULT_PROPERTIES_CHANGED = -11,

View file

@ -329,15 +329,16 @@ static inline snd_pcm_uframes_t
pull_frames (SpaALSAState *state,
const snd_pcm_channel_area_t *my_areas,
snd_pcm_uframes_t offset,
snd_pcm_uframes_t frames)
snd_pcm_uframes_t frames,
bool do_pull)
{
snd_pcm_uframes_t total_frames = 0, to_write = frames;
SpaPortIO *io = state->io;
if (spa_list_is_empty (&state->ready)) {
if (spa_list_is_empty (&state->ready) && do_pull) {
SpaEvent event = SPA_EVENT_INIT (state->type.event_node.NeedInput);
io->status = SPA_RESULT_NEED_INPUT;
io->status = SPA_RESULT_NEED_BUFFER;
io->range.offset = state->sample_count * state->frame_size;
io->range.min_size = state->threshold * state->frame_size;
io->range.max_size = frames * state->frame_size;
@ -401,9 +402,9 @@ pull_frames (SpaALSAState *state,
total_frames += n_frames;
to_write -= n_frames;
}
if (total_frames == 0) {
if (total_frames == 0 && do_pull) {
total_frames = SPA_MIN (frames, state->threshold);
spa_log_warn (state->log, "underrun, want %zd frames", total_frames);
spa_log_trace (state->log, "underrun, want %zd frames", total_frames);
snd_pcm_areas_silence (my_areas, offset, state->channels, total_frames, state->format);
}
return total_frames;
@ -419,7 +420,7 @@ push_frames (SpaALSAState *state,
SpaPortIO *io = state->io;
if (spa_list_is_empty (&state->free)) {
spa_log_warn (state->log, "no more buffers");
spa_log_trace (state->log, "no more buffers");
}
else {
uint8_t *src;
@ -529,6 +530,9 @@ alsa_on_playback_timeout_event (SpaSource *source)
state->last_ticks = state->sample_count - filled;
state->last_monotonic = (int64_t)htstamp.tv_sec * SPA_NSEC_PER_SEC + (int64_t)htstamp.tv_nsec;
spa_log_trace (state->log, "timeout %ld %d %ld %ld %ld", filled, state->threshold,
state->sample_count, htstamp.tv_sec, htstamp.tv_nsec);
if (filled > state->threshold) {
if (snd_pcm_state (hndl) == SND_PCM_STATE_SUSPENDED) {
spa_log_error (state->log, "suspended: try resume");
@ -538,6 +542,7 @@ alsa_on_playback_timeout_event (SpaSource *source)
}
else {
snd_pcm_uframes_t to_write = state->buffer_frames - filled;
bool do_pull = true;
while (total_written < to_write) {
snd_pcm_uframes_t written, frames, offset;
@ -547,9 +552,7 @@ alsa_on_playback_timeout_event (SpaSource *source)
spa_log_error (state->log, "snd_pcm_mmap_begin error: %s", snd_strerror(res));
return;
}
written = pull_frames (state, my_areas, offset, frames);
written = pull_frames (state, my_areas, offset, frames, do_pull);
if (written < frames)
to_write = 0;
@ -559,6 +562,7 @@ alsa_on_playback_timeout_event (SpaSource *source)
return;
}
total_written += written;
do_pull = false;
}
state->sample_count += total_written;
}
@ -573,10 +577,6 @@ alsa_on_playback_timeout_event (SpaSource *source)
calc_timeout (total_written + filled, state->threshold, state->rate, &htstamp, &ts.it_value);
spa_log_trace (state->log, "timeout %ld %ld %ld %ld %ld", total_written, filled,
ts.it_value.tv_sec, ts.it_value.tv_nsec,
ts.it_value.tv_nsec - htstamp.tv_nsec);
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;
timerfd_settime (state->timerfd, TFD_TIMER_ABSTIME, &ts, NULL);

View file

@ -101,10 +101,7 @@ struct _SpaAudioMixer {
uint8_t format_buffer[4096];
bool started;
#define STATE_ERROR 0
#define STATE_IN 1
#define STATE_OUT 2
int state;
SpaResult state;
};
#define CHECK_PORT_NUM(this,d,p) (((d) == SPA_DIRECTION_INPUT && (p) < MAX_PORTS) || \
@ -666,16 +663,14 @@ mix_output (SpaAudioMixer *this, size_t n_bytes)
add_port_data (this, outbuf, port, layer++);
}
output->buffer_id = outbuf->outbuf->id;
output->status = SPA_RESULT_HAVE_OUTPUT;
this->state = STATE_OUT;
output->status = SPA_RESULT_HAVE_BUFFER;
return SPA_RESULT_HAVE_OUTPUT;
return SPA_RESULT_HAVE_BUFFER;
}
static SpaResult
spa_audiomixer_node_process_input (SpaNode *node)
{
SpaResult res;
SpaAudioMixer *this;
uint32_t i;
SpaAudioMixerPort *outport;
@ -690,8 +685,8 @@ spa_audiomixer_node_process_input (SpaNode *node)
output = outport->io;
spa_return_val_if_fail (output != NULL, SPA_RESULT_ERROR);
if (this->state == STATE_OUT)
return SPA_RESULT_HAVE_OUTPUT;
if (this->state == SPA_RESULT_HAVE_BUFFER)
return SPA_RESULT_HAVE_BUFFER;
for (i = 0; i < MAX_PORTS; i++) {
SpaAudioMixerPort *port = &this->in_ports[i];
@ -701,7 +696,7 @@ spa_audiomixer_node_process_input (SpaNode *node)
continue;
if (port->queued_bytes == 0 &&
input->status == SPA_RESULT_HAVE_OUTPUT &&
input->status == SPA_RESULT_HAVE_BUFFER &&
input->buffer_id != SPA_ID_INVALID) {
MixerBuffer *b = &port->buffers[input->buffer_id];
@ -724,17 +719,16 @@ spa_audiomixer_node_process_input (SpaNode *node)
}
if (min_queued != SIZE_MAX && min_queued > 0) {
res = mix_output (this, min_queued);
this->state = mix_output (this, min_queued);
} else {
res = SPA_RESULT_NEED_INPUT;
this->state = SPA_RESULT_NEED_BUFFER;
}
return res;
return this->state;
}
static SpaResult
spa_audiomixer_node_process_output (SpaNode *node)
{
SpaResult res;
SpaAudioMixer *this;
SpaAudioMixerPort *port;
SpaPortIO *output;
@ -753,9 +747,8 @@ spa_audiomixer_node_process_output (SpaNode *node)
recycle_buffer (this, output->buffer_id);
output->buffer_id = SPA_ID_INVALID;
}
res = SPA_RESULT_NEED_INPUT;
/* produce more output if possible */
if (this->state == STATE_OUT) {
if (this->state == SPA_RESULT_HAVE_BUFFER) {
size_t min_queued = SIZE_MAX;
for (i = 0; i < MAX_PORTS; i++) {
@ -768,13 +761,13 @@ spa_audiomixer_node_process_output (SpaNode *node)
min_queued = port->queued_bytes;
}
if (min_queued != SIZE_MAX && min_queued > 0) {
res = mix_output (this, min_queued);
this->state = mix_output (this, min_queued);
} else {
this->state = STATE_IN;
this->state = SPA_RESULT_NEED_BUFFER;
}
}
/* take requested output range and apply to input */
if (this->state == STATE_IN) {
if (this->state == SPA_RESULT_NEED_BUFFER) {
for (i = 0; i < MAX_PORTS; i++) {
SpaAudioMixerPort *port = &this->in_ports[i];
SpaPortIO *input;
@ -784,7 +777,7 @@ spa_audiomixer_node_process_output (SpaNode *node)
if (port->queued_bytes == 0) {
input->range = output->range;
input->status = SPA_RESULT_NEED_INPUT;
input->status = SPA_RESULT_NEED_BUFFER;
}
else {
input->status = SPA_RESULT_OK;
@ -793,7 +786,7 @@ spa_audiomixer_node_process_output (SpaNode *node)
i, output->range.min_size, port->queued_bytes, input->status);
}
}
return res;
return this->state;
}
static const SpaNode audiomixer_node = {
@ -879,7 +872,7 @@ spa_audiomixer_init (const SpaHandleFactory *factory,
init_type (&this->type, this->map);
this->node = audiomixer_node;
this->state = STATE_IN;
this->state = SPA_RESULT_NEED_BUFFER;
this->out_ports[0].io = NULL;
this->out_ports[0].info.flags = SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS |

View file

@ -348,9 +348,9 @@ audiotestsrc_make_buffer (SpaAudioTestSrc *this)
set_timer (this, true);
io->buffer_id = b->outbuf->id;
io->status = SPA_RESULT_HAVE_OUTPUT;
io->status = SPA_RESULT_HAVE_BUFFER;
return SPA_RESULT_HAVE_OUTPUT;
return SPA_RESULT_HAVE_BUFFER;
}
static void
@ -361,7 +361,7 @@ audiotestsrc_on_output (SpaSource *source)
res = audiotestsrc_make_buffer (this);
if (res == SPA_RESULT_HAVE_OUTPUT)
if (res == SPA_RESULT_HAVE_BUFFER)
send_have_output (this);
}
@ -850,15 +850,15 @@ spa_audiotestsrc_node_process_output (SpaNode *node)
io = this->io;
spa_return_val_if_fail (io != NULL, SPA_RESULT_WRONG_STATE);
if (io->status == SPA_RESULT_HAVE_OUTPUT)
return SPA_RESULT_HAVE_OUTPUT;
if (io->status == SPA_RESULT_HAVE_BUFFER)
return SPA_RESULT_HAVE_BUFFER;
if (io->buffer_id != SPA_ID_INVALID) {
reuse_buffer (this, this->io->buffer_id);
this->io->buffer_id = SPA_ID_INVALID;
}
if (!this->async && (io->status == SPA_RESULT_NEED_INPUT))
if (!this->async && (io->status == SPA_RESULT_NEED_BUFFER))
return audiotestsrc_make_buffer (this);
else
return SPA_RESULT_OK;

View file

@ -310,9 +310,9 @@ videotestsrc_make_buffer (SpaVideoTestSrc *this)
set_timer (this, true);
io->buffer_id = b->outbuf->id;
io->status = SPA_RESULT_HAVE_OUTPUT;
io->status = SPA_RESULT_HAVE_BUFFER;
return SPA_RESULT_HAVE_OUTPUT;
return SPA_RESULT_HAVE_BUFFER;
}
static void
@ -323,7 +323,7 @@ videotestsrc_on_output (SpaSource *source)
res = videotestsrc_make_buffer (this);
if (res == SPA_RESULT_HAVE_OUTPUT)
if (res == SPA_RESULT_HAVE_BUFFER)
send_have_output (this);
}

View file

@ -725,13 +725,13 @@ spa_volume_node_process_input (SpaNode *node)
if (sbuf != dbuf)
release_buffer (this, sbuf);
return SPA_RESULT_HAVE_OUTPUT;
return SPA_RESULT_HAVE_BUFFER;
}
static SpaResult
spa_volume_node_process_output (SpaNode *node)
{
return SPA_RESULT_NEED_INPUT;
return SPA_RESULT_NEED_BUFFER;
}
static const SpaNode volume_node = {

View file

@ -225,27 +225,27 @@ on_sink_event (SpaNode *node, SpaEvent *event, void *user_data)
res = spa_node_process_output (data->mix);
if (res == SPA_RESULT_NEED_INPUT) {
if (res == SPA_RESULT_NEED_BUFFER) {
if (data->source1_mix_io[0].status == SPA_RESULT_NEED_INPUT) {
if (data->source1_mix_io[0].status == SPA_RESULT_NEED_BUFFER) {
res = spa_node_process_output (data->source1);
if (res != SPA_RESULT_HAVE_OUTPUT)
if (res != SPA_RESULT_HAVE_BUFFER)
printf ("got process_output error from source1 %d\n", res);
}
if (data->source2_mix_io[0].status == SPA_RESULT_NEED_INPUT) {
if (data->source2_mix_io[0].status == SPA_RESULT_NEED_BUFFER) {
res = spa_node_process_output (data->source2);
if (res != SPA_RESULT_HAVE_OUTPUT)
if (res != SPA_RESULT_HAVE_BUFFER)
printf ("got process_output error from source2 %d\n", res);
}
res = spa_node_process_input (data->mix);
if (res == SPA_RESULT_HAVE_OUTPUT)
if (res == SPA_RESULT_HAVE_BUFFER)
goto push;
else
printf ("got process_input error from mixer %d\n", res);
} else if (res == SPA_RESULT_HAVE_OUTPUT) {
} else if (res == SPA_RESULT_HAVE_BUFFER) {
push:
if ((res = spa_node_process_input (data->sink)) < 0)
printf ("got process_input error from sink %d\n", res);

View file

@ -215,7 +215,7 @@ on_sink_event (SpaNode *node, SpaEvent *event, void *user_data)
if (SPA_EVENT_TYPE (event) == data->type.event_node.NeedInput) {
res = spa_node_process_output (data->source);
if (res != SPA_RESULT_HAVE_OUTPUT)
if (res != SPA_RESULT_HAVE_BUFFER)
printf ("got process_output error from source %d\n", res);
if ((res = spa_node_process_input (data->sink)) < 0)