From bbfea63539cd35727f964bb50e0f02512c8fdcdb Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Mon, 22 Aug 2022 12:24:38 +0200 Subject: [PATCH] rtmp: read or write tracks only after interacting with the path --- internal/core/rtmp_conn.go | 50 ++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/internal/core/rtmp_conn.go b/internal/core/rtmp_conn.go index 8ec304cb..3791079e 100644 --- a/internal/core/rtmp_conn.go +++ b/internal/core/rtmp_conn.go @@ -302,12 +302,6 @@ func (c *rtmpConn) runRead(ctx context.Context, u *url.URL) error { c.path.Name(), sourceTrackInfo(res.stream.tracks())) - c.nconn.SetWriteDeadline(time.Now().Add(time.Duration(c.writeTimeout))) - err := c.conn.WriteTracks(videoTrack, audioTrack) - if err != nil { - return err - } - if c.path.Conf().RunOnRead != "" { c.log(logger.Info, "runOnRead command started") onReadCmd := externalcmd.NewCmd( @@ -324,6 +318,11 @@ func (c *rtmpConn) runRead(ctx context.Context, u *url.URL) error { }() } + err := c.conn.WriteTracks(videoTrack, audioTrack) + if err != nil { + return err + } + // disable read deadline c.nconn.SetReadDeadline(time.Time{}) @@ -461,26 +460,6 @@ func (c *rtmpConn) runRead(ctx context.Context, u *url.URL) error { } func (c *rtmpConn) runPublish(ctx context.Context, u *url.URL) error { - c.nconn.SetReadDeadline(time.Now().Add(time.Duration(c.readTimeout))) - videoTrack, audioTrack, err := c.conn.ReadTracks() - if err != nil { - return err - } - - var tracks gortsplib.Tracks - videoTrackID := -1 - audioTrackID := -1 - - if videoTrack != nil { - videoTrackID = len(tracks) - tracks = append(tracks, videoTrack) - } - - if audioTrack != nil { - audioTrackID = len(tracks) - tracks = append(tracks, audioTrack) - } - pathName, query, rawQuery := pathNameAndQuery(u) res := c.pathManager.publisherAdd(pathPublisherAddReq{ @@ -514,6 +493,25 @@ func (c *rtmpConn) runPublish(ctx context.Context, u *url.URL) error { c.state = rtmpConnStatePublish c.stateMutex.Unlock() + videoTrack, audioTrack, err := c.conn.ReadTracks() + if err != nil { + return err + } + + var tracks gortsplib.Tracks + videoTrackID := -1 + audioTrackID := -1 + + if videoTrack != nil { + videoTrackID = len(tracks) + tracks = append(tracks, videoTrack) + } + + if audioTrack != nil { + audioTrackID = len(tracks) + tracks = append(tracks, audioTrack) + } + rres := c.path.publisherStart(pathPublisherStartReq{ author: c, tracks: tracks,