client: remove OnPlay

This commit is contained in:
aler9
2021-11-12 16:44:16 +01:00
committed by Alessandro Ros
parent 3ba7c373b9
commit 029ebd5dea
4 changed files with 133 additions and 65 deletions

View File

@@ -137,8 +137,6 @@ type Client struct {
OnRequest func(*base.Request) OnRequest func(*base.Request)
// called after every response. // called after every response.
OnResponse func(*base.Response) OnResponse func(*base.Response)
// called before sending a PLAY request.
OnPlay func(*Client)
// called when a RTP packet arrives. // called when a RTP packet arrives.
OnPacketRTP func(*Client, int, []byte) OnPacketRTP func(*Client, int, []byte)
// called when a RTCP packet arrives. // called when a RTCP packet arrives.
@@ -1528,10 +1526,6 @@ func (c *Client) doPlay(ra *headers.Range, isSwitchingProtocol bool) (*base.Resp
return nil, err return nil, err
} }
if c.OnPlay != nil {
c.OnPlay(c)
}
c.state = clientStatePlay c.state = clientStatePlay
// setup UDP communication before sending the request. // setup UDP communication before sending the request.

View File

@@ -8,7 +8,9 @@ import (
"time" "time"
"github.com/aler9/gortsplib" "github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/base"
"github.com/aler9/gortsplib/pkg/h264" "github.com/aler9/gortsplib/pkg/h264"
"github.com/aler9/gortsplib/pkg/headers"
"github.com/aler9/gortsplib/pkg/rtph264" "github.com/aler9/gortsplib/pkg/rtph264"
"github.com/asticode/go-astits" "github.com/asticode/go-astits"
"github.com/pion/rtp" "github.com/pion/rtp"
@@ -19,14 +21,9 @@ import (
// 2. check whether there's a H264 track // 2. check whether there's a H264 track
// 3. save the content of the H264 track to a file in MPEG-TS format // 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() { func main() {
// open output file // open output file
f, err := os.Create(outputFile) f, err := os.Create("mystream.ts")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -40,6 +37,8 @@ func main() {
dtsEst := h264.NewDTSEstimator() dtsEst := h264.NewDTSEstimator()
firstPacketWritten := false firstPacketWritten := false
var startPTS time.Duration var startPTS time.Duration
var h264Track int
var h264Conf *gortsplib.TrackConfigH264
// add an H264 track to the MPEG-TS muxer // add an H264 track to the MPEG-TS muxer
mux.AddElementaryStream(astits.PMTElementaryStream{ mux.AddElementaryStream(astits.PMTElementaryStream{
@@ -48,30 +47,10 @@ func main() {
}) })
mux.SetPCRPID(256) mux.SetPCRPID(256)
var h264TrackID int = -1
var h264Conf *gortsplib.TrackConfigH264
c := gortsplib.Client{ 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 // called when a RTP packet arrives
OnPacketRTP: func(c *gortsplib.Client, trackID int, payload []byte) { OnPacketRTP: func(c *gortsplib.Client, trackID int, payload []byte) {
if trackID != h264TrackID { if trackID != h264Track {
return return
} }
@@ -162,6 +141,67 @@ func main() {
}, },
} }
// connect to the server and start reading all tracks // parse URL
panic(c.StartReadingAndWait(inputStream)) 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())
} }

View File

@@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"github.com/aler9/gortsplib" "github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/base"
"github.com/aler9/gortsplib/pkg/headers"
"github.com/aler9/gortsplib/pkg/rtph264" "github.com/aler9/gortsplib/pkg/rtph264"
"github.com/pion/rtp" "github.com/pion/rtp"
) )
@@ -15,28 +17,9 @@ import (
func main() { func main() {
var h264Track int var h264Track int
var dec *rtph264.Decoder dec := rtph264.NewDecoder()
c := gortsplib.Client{ 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 // called when a RTP packet arrives
OnPacketRTP: func(c *gortsplib.Client, trackID int, payload []byte) { OnPacketRTP: func(c *gortsplib.Client, trackID int, payload []byte) {
if trackID != h264Track { if trackID != h264Track {
@@ -63,6 +46,58 @@ func main() {
}, },
} }
// connect to the server and start reading all tracks // parse URL
panic(c.StartReadingAndWait("rtsp://localhost:8554/mystream")) 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())
} }

View File

@@ -14,11 +14,6 @@ import (
// 3. read only selected tracks // 3. read only selected tracks
func main() { func main() {
u, err := base.ParseURL("rtsp://myserver/mypath")
if err != nil {
panic(err)
}
c := gortsplib.Client{ c := gortsplib.Client{
// called when a RTP packet arrives // called when a RTP packet arrives
OnPacketRTP: func(c *gortsplib.Client, trackID int, payload []byte) { 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) err = c.Start(u.Scheme, u.Host)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer c.Close()
_, err = c.Options(u) _, err = c.Options(u)
if err != nil { if err != nil {
@@ -46,7 +45,7 @@ func main() {
panic(err) 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 { for _, t := range tracks {
if t.Media.MediaName.Media == "video" { if t.Media.MediaName.Media == "video" {
_, err := c.Setup(headers.TransportModePlay, baseURL, t, 0, 0) _, 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) _, err = c.Play(nil)
if err != nil { if err != nil {
panic(err) panic(err)