From 7182b5b4a11ec34414d37e19569a195c773b7ace Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 20 Mar 2022 11:46:33 +0100 Subject: [PATCH] simplify h264 publish examples --- examples/client-publish-h264/main.go | 27 +++++++--------- examples/client-publish-options/main.go | 27 +++++++--------- examples/client-publish-pause/main.go | 31 +++++++------------ .../client-read-h264-convert-to-jpeg/main.go | 2 +- .../client-read-h264-save-to-disk/main.go | 2 +- examples/client-read-h264/main.go | 2 +- 6 files changed, 39 insertions(+), 52 deletions(-) diff --git a/examples/client-publish-h264/main.go b/examples/client-publish-h264/main.go index f6639ba2..949c02b2 100644 --- a/examples/client-publish-h264/main.go +++ b/examples/client-publish-h264/main.go @@ -5,7 +5,6 @@ import ( "net" "github.com/aler9/gortsplib" - "github.com/aler9/gortsplib/pkg/rtph264" "github.com/pion/rtp/v2" ) @@ -27,17 +26,16 @@ func main() { " ! x264enc speed-preset=veryfast tune=zerolatency bitrate=600000" + " ! rtph264pay ! udpsink host=127.0.0.1 port=9000") - // get SPS and PPS - decoder := &rtph264.Decoder{} - decoder.Init() - sps, pps, err := decoder.ReadSPSPPS(rtph264.PacketConnReader{pc}) + // wait for first packet + buf := make([]byte, 2048) + n, _, err := pc.ReadFrom(buf) if err != nil { panic(err) } log.Println("stream connected") // create an H264 track - track, err := gortsplib.NewTrackH264(96, sps, pps, nil) + track, err := gortsplib.NewTrackH264(96, nil, nil, nil) if err != nil { panic(err) } @@ -51,25 +49,24 @@ func main() { } defer c.Close() - buf := make([]byte, 2048) var pkt rtp.Packet for { - // read RTP packets from the source - n, _, err := pc.ReadFrom(buf) - if err != nil { - panic(err) - } - - // parse RTP packets + // parse RTP packet err = pkt.Unmarshal(buf[:n]) if err != nil { panic(err) } - // route RTP packets to the server + // route RTP packet to the server err = c.WritePacketRTP(0, &pkt) if err != nil { panic(err) } + + // read another RTP packet from source + n, _, err = pc.ReadFrom(buf) + if err != nil { + panic(err) + } } } diff --git a/examples/client-publish-options/main.go b/examples/client-publish-options/main.go index e3333f00..cba37934 100644 --- a/examples/client-publish-options/main.go +++ b/examples/client-publish-options/main.go @@ -6,7 +6,6 @@ import ( "time" "github.com/aler9/gortsplib" - "github.com/aler9/gortsplib/pkg/rtph264" "github.com/pion/rtp/v2" ) @@ -28,17 +27,16 @@ func main() { "gst-launch-1.0 filesrc location=video.mp4 ! qtdemux ! video/x-h264" + " ! h264parse config-interval=1 ! rtph264pay ! udpsink host=127.0.0.1 port=9000") - // get SPS and PPS - decoder := &rtph264.Decoder{} - decoder.Init() - sps, pps, err := decoder.ReadSPSPPS(rtph264.PacketConnReader{pc}) + // wait for first packet + buf := make([]byte, 2048) + n, _, err := pc.ReadFrom(buf) if err != nil { panic(err) } log.Println("stream connected") // create an H264 track - track, err := gortsplib.NewTrackH264(96, sps, pps, nil) + track, err := gortsplib.NewTrackH264(96, nil, nil, nil) if err != nil { panic(err) } @@ -61,25 +59,24 @@ func main() { } defer c.Close() - buf := make([]byte, 2048) var pkt rtp.Packet for { - // read RTP packets from the source - n, _, err := pc.ReadFrom(buf) - if err != nil { - panic(err) - } - - // parse RTP packets + // parse RTP packet err = pkt.Unmarshal(buf[:n]) if err != nil { panic(err) } - // route RTP packets to the server + // route RTP packet to the server err = c.WritePacketRTP(0, &pkt) if err != nil { panic(err) } + + // read another RTP packet from source + n, _, err = pc.ReadFrom(buf) + if err != nil { + panic(err) + } } } diff --git a/examples/client-publish-pause/main.go b/examples/client-publish-pause/main.go index 5902e826..4755db25 100644 --- a/examples/client-publish-pause/main.go +++ b/examples/client-publish-pause/main.go @@ -6,7 +6,6 @@ import ( "time" "github.com/aler9/gortsplib" - "github.com/aler9/gortsplib/pkg/rtph264" "github.com/pion/rtp/v2" ) @@ -29,24 +28,22 @@ func main() { "gst-launch-1.0 filesrc location=video.mp4 ! qtdemux ! video/x-h264" + " ! h264parse config-interval=1 ! rtph264pay ! udpsink host=127.0.0.1 port=9000") - // get SPS and PPS - decoder := &rtph264.Decoder{} - decoder.Init() - sps, pps, err := decoder.ReadSPSPPS(rtph264.PacketConnReader{pc}) + // wait for first packet + buf := make([]byte, 2048) + n, _, err := pc.ReadFrom(buf) if err != nil { panic(err) } log.Println("stream connected") // create an H264 track - track, err := gortsplib.NewTrackH264(96, sps, pps, nil) + track, err := gortsplib.NewTrackH264(96, nil, nil, nil) if err != nil { panic(err) } - c := gortsplib.Client{} - // connect to the server and start publishing the track + c := gortsplib.Client{} err = c.StartPublishing("rtsp://localhost:8554/mystream", gortsplib.Tracks{track}) if err != nil { @@ -56,25 +53,21 @@ func main() { for { go func() { - buf := make([]byte, 2048) var pkt rtp.Packet for { - // read RTP packets from the source - n, _, err := pc.ReadFrom(buf) - if err != nil { - break - } - - // parse RTP packets + // parse RTP packet err = pkt.Unmarshal(buf[:n]) if err != nil { panic(err) } - // route RTP packets to the server - err = c.WritePacketRTP(0, &pkt) + // route RTP packet to the server + c.WritePacketRTP(0, &pkt) + + // read another RTP packet from source + n, _, err = pc.ReadFrom(buf) if err != nil { - break + panic(err) } } }() diff --git a/examples/client-read-h264-convert-to-jpeg/main.go b/examples/client-read-h264-convert-to-jpeg/main.go index a4901496..04c1798d 100644 --- a/examples/client-read-h264-convert-to-jpeg/main.go +++ b/examples/client-read-h264-convert-to-jpeg/main.go @@ -17,7 +17,7 @@ import ( // This example shows how to // 1. connect to a RTSP server and read all tracks on a path // 2. check if there's a H264 track -// 3. convert H264 into raw frames +// 3. decode H264 into raw frames // 4. encode the frames into JPEG images and save them on disk // This example requires the ffmpeg libraries, that can be installed in this way: diff --git a/examples/client-read-h264-save-to-disk/main.go b/examples/client-read-h264-save-to-disk/main.go index ccadcb90..8c0441bf 100644 --- a/examples/client-read-h264-save-to-disk/main.go +++ b/examples/client-read-h264-save-to-disk/main.go @@ -10,7 +10,7 @@ import ( // This example shows how to // 1. connect to a RTSP server and read all tracks on a path // 2. check if 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 into a file in MPEG-TS format func main() { c := gortsplib.Client{} diff --git a/examples/client-read-h264/main.go b/examples/client-read-h264/main.go index 949d8024..0de09ff0 100644 --- a/examples/client-read-h264/main.go +++ b/examples/client-read-h264/main.go @@ -12,7 +12,7 @@ import ( // This example shows how to // 1. connect to a RTSP server and read all tracks on a path // 2. check if there's an H264 track -// 3. convert H264 into raw frames +// 3. decode H264 into raw frames // This example requires the ffmpeg libraries, that can be installed in this way: // apt install -y libavformat-dev libswscale-dev gcc pkg-config