From 029ebd5dea8e59001fa2a7154df1e94ed49113b1 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Fri, 12 Nov 2021 16:44:16 +0100 Subject: [PATCH] client: remove OnPlay --- client.go | 6 -- .../client-read-h264-save-to-disk/main.go | 98 +++++++++++++------ examples/client-read-h264/main.go | 79 ++++++++++----- examples/client-read-partial/main.go | 15 ++- 4 files changed, 133 insertions(+), 65 deletions(-) diff --git a/client.go b/client.go index f3c890fe..84e9ca74 100644 --- a/client.go +++ b/client.go @@ -137,8 +137,6 @@ type Client struct { OnRequest func(*base.Request) // called after every response. OnResponse func(*base.Response) - // called before sending a PLAY request. - OnPlay func(*Client) // called when a RTP packet arrives. OnPacketRTP func(*Client, int, []byte) // called when a RTCP packet arrives. @@ -1528,10 +1526,6 @@ func (c *Client) doPlay(ra *headers.Range, isSwitchingProtocol bool) (*base.Resp return nil, err } - if c.OnPlay != nil { - c.OnPlay(c) - } - c.state = clientStatePlay // setup UDP communication before sending the request. diff --git a/examples/client-read-h264-save-to-disk/main.go b/examples/client-read-h264-save-to-disk/main.go index c5f0a0a1..e1aa1e11 100644 --- a/examples/client-read-h264-save-to-disk/main.go +++ b/examples/client-read-h264-save-to-disk/main.go @@ -8,7 +8,9 @@ import ( "time" "github.com/aler9/gortsplib" + "github.com/aler9/gortsplib/pkg/base" "github.com/aler9/gortsplib/pkg/h264" + "github.com/aler9/gortsplib/pkg/headers" "github.com/aler9/gortsplib/pkg/rtph264" "github.com/asticode/go-astits" "github.com/pion/rtp" @@ -19,14 +21,9 @@ import ( // 2. check whether there's a H264 track // 3. save the content of the H264 track to a file in MPEG-TS format -const ( - inputStream = "rtsp://localhost:8554/mystream" - outputFile = "mystream.ts" -) - func main() { // open output file - f, err := os.Create(outputFile) + f, err := os.Create("mystream.ts") if err != nil { panic(err) } @@ -40,6 +37,8 @@ func main() { dtsEst := h264.NewDTSEstimator() firstPacketWritten := false var startPTS time.Duration + var h264Track int + var h264Conf *gortsplib.TrackConfigH264 // add an H264 track to the MPEG-TS muxer mux.AddElementaryStream(astits.PMTElementaryStream{ @@ -48,30 +47,10 @@ func main() { }) mux.SetPCRPID(256) - var h264TrackID int = -1 - var h264Conf *gortsplib.TrackConfigH264 - c := gortsplib.Client{ - // called before sending a PLAY request - OnPlay: func(c *gortsplib.Client) { - // find the H264 track - for i, track := range c.Tracks() { - if track.IsH264() { - h264TrackID = i - var err error - h264Conf, err = track.ExtractConfigH264() - if err != nil { - panic(err) - } - } - } - if h264TrackID < 0 { - panic(fmt.Errorf("H264 track not found")) - } - }, // called when a RTP packet arrives OnPacketRTP: func(c *gortsplib.Client, trackID int, payload []byte) { - if trackID != h264TrackID { + if trackID != h264Track { return } @@ -162,6 +141,67 @@ func main() { }, } - // connect to the server and start reading all tracks - panic(c.StartReadingAndWait(inputStream)) + // parse URL + u, err := base.ParseURL("rtsp://localhost:8554/mystream") + if err != nil { + panic(err) + } + + // connect to the server + err = c.Start(u.Scheme, u.Host) + if err != nil { + panic(err) + } + + // get available methods + _, err = c.Options(u) + if err != nil { + panic(err) + } + + // find published tracks + tracks, baseURL, _, err := c.Describe(u) + if err != nil { + panic(err) + } + + // find the H264 track + h264Track = func() int { + for i, track := range tracks { + if track.IsH264() { + return i + } + } + return -1 + }() + if h264Track < 0 { + panic(fmt.Errorf("H264 track not found")) + } + fmt.Printf("H264 track is number %d\n", h264Track+1) + + // get track config + h264Conf, err = c.Tracks()[h264Track].ExtractConfigH264() + if err != nil { + panic(err) + } + + // instantiate a RTP/H264 decoder + dec = rtph264.NewDecoder() + + // setup all tracks + for _, t := range tracks { + _, err := c.Setup(headers.TransportModePlay, baseURL, t, 0, 0) + if err != nil { + panic(err) + } + } + + // start reading tracks + _, err = c.Play(nil) + if err != nil { + panic(err) + } + + // wait until a fatal error + panic(c.Wait()) } diff --git a/examples/client-read-h264/main.go b/examples/client-read-h264/main.go index 7ed4fdc7..c56e5885 100644 --- a/examples/client-read-h264/main.go +++ b/examples/client-read-h264/main.go @@ -4,6 +4,8 @@ import ( "fmt" "github.com/aler9/gortsplib" + "github.com/aler9/gortsplib/pkg/base" + "github.com/aler9/gortsplib/pkg/headers" "github.com/aler9/gortsplib/pkg/rtph264" "github.com/pion/rtp" ) @@ -15,28 +17,9 @@ import ( func main() { var h264Track int - var dec *rtph264.Decoder + dec := rtph264.NewDecoder() c := gortsplib.Client{ - // called before sending a PLAY request - OnPlay: func(c *gortsplib.Client) { - // find the H264 track - h264Track = func() int { - for i, track := range c.Tracks() { - if track.IsH264() { - return i - } - } - return -1 - }() - if h264Track < 0 { - panic(fmt.Errorf("H264 track not found")) - } - fmt.Printf("H264 track is number %d\n", h264Track+1) - - // instantiate a RTP/H264 decoder - dec = rtph264.NewDecoder() - }, // called when a RTP packet arrives OnPacketRTP: func(c *gortsplib.Client, trackID int, payload []byte) { if trackID != h264Track { @@ -63,6 +46,58 @@ func main() { }, } - // connect to the server and start reading all tracks - panic(c.StartReadingAndWait("rtsp://localhost:8554/mystream")) + // parse URL + u, err := base.ParseURL("rtsp://localhost:8554/mystream") + if err != nil { + panic(err) + } + + // connect to the server + err = c.Start(u.Scheme, u.Host) + if err != nil { + panic(err) + } + + // get available methods + _, err = c.Options(u) + if err != nil { + panic(err) + } + + // find published tracks + tracks, baseURL, _, err := c.Describe(u) + if err != nil { + panic(err) + } + + // find the H264 track + h264Track = func() int { + for i, track := range tracks { + if track.IsH264() { + return i + } + } + return -1 + }() + if h264Track < 0 { + panic(fmt.Errorf("H264 track not found")) + } + fmt.Printf("H264 track is number %d\n", h264Track+1) + + // setup all tracks + for _, t := range tracks { + _, err := c.Setup(headers.TransportModePlay, baseURL, t, 0, 0) + if err != nil { + panic(err) + } + } + + // start reading tracks + _, err = c.Play(nil) + if err != nil { + panic(err) + } + + // wait until a fatal error + panic(c.Wait()) } diff --git a/examples/client-read-partial/main.go b/examples/client-read-partial/main.go index d583db5d..595d0cd0 100644 --- a/examples/client-read-partial/main.go +++ b/examples/client-read-partial/main.go @@ -14,11 +14,6 @@ import ( // 3. read only selected tracks func main() { - u, err := base.ParseURL("rtsp://myserver/mypath") - if err != nil { - panic(err) - } - c := gortsplib.Client{ // called when a RTP packet arrives OnPacketRTP: func(c *gortsplib.Client, trackID int, payload []byte) { @@ -30,11 +25,15 @@ func main() { }, } + u, err := base.ParseURL("rtsp://myserver/mypath") + if err != nil { + panic(err) + } + err = c.Start(u.Scheme, u.Host) if err != nil { panic(err) } - defer c.Close() _, err = c.Options(u) if err != nil { @@ -46,7 +45,7 @@ func main() { panic(err) } - // start reading only video tracks, skipping audio or application tracks + // setup only video tracks, skipping audio or application tracks for _, t := range tracks { if t.Media.MediaName.Media == "video" { _, err := c.Setup(headers.TransportModePlay, baseURL, t, 0, 0) @@ -56,7 +55,7 @@ func main() { } } - // play setupped tracks + // start reading tracks _, err = c.Play(nil) if err != nil { panic(err)