From 3e0134f51fe174a1c68422b55147b2f2a2b07373 Mon Sep 17 00:00:00 2001 From: Vincent Penquerc'h Date: Tue, 16 Aug 2011 17:27:13 +0100 Subject: [PATCH] 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 --- ext/flac/gstflacdec.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/ext/flac/gstflacdec.c b/ext/flac/gstflacdec.c index 94f77899f3..061fa811f7 100644 --- a/ext/flac/gstflacdec.c +++ b/ext/flac/gstflacdec.c @@ -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"); }