From fea0d0b1a4757ab199f2b57b4ff3ffefac1034e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Cr=C3=AAte?= Date: Sat, 27 Oct 2018 19:27:12 +0100 Subject: [PATCH] flvmux: Force timestamps to always be increasing https://bugzilla.gnome.org/show_bug.cgi?id=796382 --- gst/flv/gstflvmux.c | 15 +++++++++++++++ gst/flv/gstflvmux.h | 1 + 2 files changed, 16 insertions(+) diff --git a/gst/flv/gstflvmux.c b/gst/flv/gstflvmux.c index c094016224..a37acd4bf7 100644 --- a/gst/flv/gstflvmux.c +++ b/gst/flv/gstflvmux.c @@ -322,6 +322,7 @@ gst_flv_mux_reset (GstElement * element) mux->duration = GST_CLOCK_TIME_NONE; mux->new_tags = FALSE; mux->first_timestamp = GST_CLOCK_STIME_NONE; + mux->last_dts = 0; mux->state = GST_FLV_MUX_STATE_HEADER; mux->sent_header = FALSE; @@ -1145,6 +1146,19 @@ gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer, dts = pad->dts / GST_MSECOND; } + /* We prevent backwards timestamps because they confuse librtmp, + * it expects timestamps to go forward not only inside one stream, but + * also between the audio & video streams. + */ + if (dts < mux->last_dts) { + GST_WARNING_OBJECT (pad, "Got backwards dts! (%" GST_TIME_FORMAT + " < %" GST_TIME_FORMAT ")", GST_TIME_ARGS (dts), + GST_TIME_ARGS (mux->last_dts)); + dts = mux->last_dts; + } + mux->last_dts = dts; + + /* Be safe in case TS are buggy */ if (pts > dts) cts = pts - dts; @@ -1190,6 +1204,7 @@ gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer, data[2] = ((size - 11 - 4) >> 8) & 0xff; data[3] = ((size - 11 - 4) >> 0) & 0xff; + GST_WRITE_UINT24_BE (data + 4, dts); data[7] = (((guint) dts) >> 24) & 0xff; diff --git a/gst/flv/gstflvmux.h b/gst/flv/gstflvmux.h index e116d4caab..d2ad8da3cf 100644 --- a/gst/flv/gstflvmux.h +++ b/gst/flv/gstflvmux.h @@ -98,6 +98,7 @@ typedef struct _GstFlvMux { guint64 byte_count; guint64 duration; gint64 first_timestamp; + GstClockTime last_dts; gboolean sent_header; } GstFlvMux;