From 7f799ffea8e68c7f5c50493c2abeaf451ce1ee9e Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sat, 19 Feb 2022 15:11:37 +0100 Subject: [PATCH] add client-read-republish example (#91) --- README.md | 1 + .../client-read-h264-convert-to-jpeg/main.go | 1 + examples/client-read-h264/main.go | 1 + examples/client-read-republish/main.go | 67 +++++++++++++++++++ examples/client-read/main.go | 4 +- 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 examples/client-read-republish/main.go diff --git a/README.md b/README.md index 429856e0..d7e07274 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Features: * [client-read-h264-convert-to-jpeg](examples/client-read-h264-convert-to-jpeg/main.go) * [client-read-h264-save-to-disk](examples/client-read-h264-save-to-disk/main.go) * [client-read-aac](examples/client-read-aac/main.go) +* [client-read-republish](examples/client-read-republish/main.go) * [client-publish-h264](examples/client-publish-h264/main.go) * [client-publish-aac](examples/client-publish-aac/main.go) * [client-publish-opus](examples/client-publish-opus/main.go) diff --git a/examples/client-read-h264-convert-to-jpeg/main.go b/examples/client-read-h264-convert-to-jpeg/main.go index bd435a14..5aa7855e 100644 --- a/examples/client-read-h264-convert-to-jpeg/main.go +++ b/examples/client-read-h264-convert-to-jpeg/main.go @@ -19,6 +19,7 @@ import ( // 2. check if there's a H264 track // 3. convert 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: // apt install -y libavformat-dev libswscale-dev gcc pkg-config diff --git a/examples/client-read-h264/main.go b/examples/client-read-h264/main.go index 6bc05fc8..4939e615 100644 --- a/examples/client-read-h264/main.go +++ b/examples/client-read-h264/main.go @@ -13,6 +13,7 @@ import ( // 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 + // This example requires the ffmpeg libraries, that can be installed in this way: // apt install -y libavformat-dev libswscale-dev gcc pkg-config diff --git a/examples/client-read-republish/main.go b/examples/client-read-republish/main.go new file mode 100644 index 00000000..abb52c0e --- /dev/null +++ b/examples/client-read-republish/main.go @@ -0,0 +1,67 @@ +package main + +import ( + "log" + + "github.com/aler9/gortsplib" + "github.com/aler9/gortsplib/pkg/base" + "github.com/pion/rtp" +) + +// This example shows how to +// 1. connect to a RTSP server and read all tracks on a path +// 2. re-publish all tracks on another path. + +func main() { + reader := gortsplib.Client{} + + // parse source URL + sourceURL, err := base.ParseURL("rtsp://localhost:8554/mystream") + if err != nil { + panic(err) + } + + // connect to the server + err = reader.Start(sourceURL.Scheme, sourceURL.Host) + if err != nil { + panic(err) + } + defer reader.Close() + + // get available methods + _, err = reader.Options(sourceURL) + if err != nil { + panic(err) + } + + // find published tracks + tracks, baseURL, _, err := reader.Describe(sourceURL) + if err != nil { + panic(err) + } + + log.Printf("republishing %d tracks", len(tracks)) + + publisher := gortsplib.Client{} + + // connect to the server and start publishing + err = publisher.StartPublishing("rtsp://localhost:8554/mystream2", tracks) + if err != nil { + panic(err) + } + defer publisher.Close() + + // called when a RTP packet arrives + reader.OnPacketRTP = func(trackID int, pkt *rtp.Packet) { + publisher.WritePacketRTP(trackID, pkt) + } + + // start reading tracks + err = reader.SetupAndPlay(tracks, baseURL) + if err != nil { + panic(err) + } + + // wait until a fatal error + panic(reader.Wait()) +} diff --git a/examples/client-read/main.go b/examples/client-read/main.go index 22f5fda7..a852bdbe 100644 --- a/examples/client-read/main.go +++ b/examples/client-read/main.go @@ -8,8 +8,8 @@ import ( "github.com/pion/rtp" ) -// This example shows how to -// 1. connect to a RTSP server and read all tracks on a path +// This example shows how to connect to a RTSP server +// and read all tracks on a path. func main() { c := gortsplib.Client{