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

@@ -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())
}