flacdec: avoid timestamp/offset tracking going out of sync

The libFLAC API is callback based, and we must only call it to
output data when we know we have enough input data. For this
reason, a single processing step is done when receiving a buffer.
However, if there were metadata buffers still pending, a step
intended for the first audio frame might end up writing that
leftover metadata. Since a single step is done per buffer, this
will cause every buffer to be written one step late.

This would add some latency (a bufferfull's worth), possibly
lose a buffer when seeking or the like, and also cause timestamp
and offset to be applied to the wrong buffer, as updates to
the "current" segment last_stop (from incoming buffer timestamp)
will be applied to an output buffer originating from the previous
incoming buffer.

This fixes the issue by ensuring that, upon receiving the first
audio frame, processing is done till all metadata is processed,
so the next "single step" done will be for the audio frame. After
this, we should keep to 1 input buffer -> 1 output buffer and so
avoid getting out of sync.

https://bugzilla.gnome.org/show_bug.cgi?id=650960
This commit is contained in:
Vincent Penquerc'h 2011-08-16 17:27:13 +01:00 committed by Tim-Philipp Müller
parent e09eb95a5f
commit 3e0134f51f

View file

@ -1391,8 +1391,10 @@ gst_flac_dec_chain (GstPad * pad, GstBuffer * buf)
dec = GST_FLAC_DEC (GST_PAD_PARENT (pad));
GST_LOG_OBJECT (dec, "buffer with ts=%" GST_TIME_FORMAT ", end_offset=%"
G_GINT64_FORMAT ", size=%u", GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
GST_LOG_OBJECT (dec,
"buffer with ts=%" GST_TIME_FORMAT ", offset=%" G_GINT64_FORMAT
", end_offset=%" G_GINT64_FORMAT ", size=%u",
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), GST_BUFFER_OFFSET (buf),
GST_BUFFER_OFFSET_END (buf), GST_BUFFER_SIZE (buf));
if (dec->init) {
@ -1483,6 +1485,19 @@ gst_flac_dec_chain (GstPad * pad, GstBuffer * buf)
/* framed - there should always be enough data to decode something */
GST_LOG_OBJECT (dec, "%u bytes available",
gst_adapter_available (dec->adapter));
if (G_UNLIKELY (!dec->got_headers)) {
/* The first time we get audio data, we know we got all the headers.
* We then loop until all the metadata is processed, then do an extra
* "process_single" step for the audio frame. */
GST_DEBUG_OBJECT (dec,
"First audio frame, ensuring all metadata is processed");
if (!FLAC__stream_decoder_process_until_end_of_metadata (dec->decoder)) {
GST_DEBUG_OBJECT (dec, "process_until_end_of_metadata failed");
}
GST_DEBUG_OBJECT (dec,
"All metadata is now processed, reading to process audio data");
dec->got_headers = TRUE;
}
if (!FLAC__stream_decoder_process_single (dec->decoder)) {
GST_DEBUG_OBJECT (dec, "process_single failed");
}