From 6077814f1168da6be4f4e55adbdd171cdda3d988 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Mon, 5 Apr 2021 18:47:00 +0200 Subject: [PATCH] RTMP: avoid duplicate DTS --- internal/clientrtmp/client.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/clientrtmp/client.go b/internal/clientrtmp/client.go index ac40847e..58162c16 100644 --- a/internal/clientrtmp/client.go +++ b/internal/clientrtmp/client.go @@ -287,6 +287,7 @@ func (c *Client) runRead() { videoInitialized := false var videoBuf [][]byte var videoStartDTS time.Time + var videoLastDTS time.Duration for { data, ok := c.ringBuffer.Pull() @@ -325,7 +326,17 @@ func (c *Client) runRead() { marker := (pair.buf[1] >> 7 & 0x1) > 0 if marker { c.conn.NetConn().SetWriteDeadline(time.Now().Add(c.writeTimeout)) - err := c.conn.WriteH264(videoBuf, time.Since(videoStartDTS)) + + dts := time.Since(videoStartDTS) + + // avoid duplicate DTS + // (RTMP has a resolution of 1ms) + if int64(dts/time.Millisecond) <= (int64(videoLastDTS / time.Millisecond)) { + dts = videoLastDTS + time.Millisecond + } + videoLastDTS = dts + + err := c.conn.WriteH264(videoBuf, dts) if err != nil { return err }