From f4ef4ec23a9b18fef00e188d2f4054b34971f2bb Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Sat, 28 Jun 2025 12:42:37 +0200 Subject: [PATCH] examples: print write errors (#810) --- .../main.go | 2 +- examples/client-play-timestamp/main.go | 6 +++--- examples/client-play-to-record/main.go | 6 +++++- examples/proxy-backchannel/client.go | 10 ++++++++-- examples/proxy/client.go | 5 ++++- examples/server-auth/main.go | 9 ++++++--- examples/server-tls/main.go | 16 ++++++++++------ examples/server/main.go | 9 ++++++--- 8 files changed, 43 insertions(+), 20 deletions(-) diff --git a/examples/client-play-format-h264-mpeg4audio-to-disk/main.go b/examples/client-play-format-h264-mpeg4audio-to-disk/main.go index 470cdab8..86edf3c2 100644 --- a/examples/client-play-format-h264-mpeg4audio-to-disk/main.go +++ b/examples/client-play-format-h264-mpeg4audio-to-disk/main.go @@ -15,7 +15,7 @@ import ( // This example shows how to // 1. connect to a RTSP server. // 2. check if there's a H264 stream and a MPEG-4 audio stream. -// 3. save the content of those formats in a file in MPEG-TS format. +// 3. save the content of these streams in a file in MPEG-TS format. func main() { c := gortsplib.Client{} diff --git a/examples/client-play-timestamp/main.go b/examples/client-play-timestamp/main.go index 0950b79e..fd28abfc 100644 --- a/examples/client-play-timestamp/main.go +++ b/examples/client-play-timestamp/main.go @@ -14,7 +14,7 @@ import ( // This example shows how to // 1. connect to a RTSP server. // 2. read all media streams on a path. -// 3. Get the PTS and NTP timestamp of incoming RTP packets. +// 3. Get PTS and NTP of incoming RTP packets. func main() { c := gortsplib.Client{} @@ -46,11 +46,11 @@ func main() { // called when a RTP packet arrives c.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) { - // get the PTS timestamp of the packet, i.e. timestamp relative to the start of the session + // get the PTS (presentation timestamp) of the packet pts, ptsAvailable := c.PacketPTS2(medi, pkt) log.Printf("PTS: available=%v, value=%v\n", ptsAvailable, pts) - // get the NTP timestamp of the packet, i.e. the absolute timestamp + // get the NTP (absolute timestamp) of the packet ntp, ntpAvailable := c.PacketNTP(medi, pkt) log.Printf("NTP: available=%v, value=%v\n", ntpAvailable, ntp) }) diff --git a/examples/client-play-to-record/main.go b/examples/client-play-to-record/main.go index b7777a6e..c2cf6d26 100644 --- a/examples/client-play-to-record/main.go +++ b/examples/client-play-to-record/main.go @@ -2,6 +2,7 @@ package main import ( + "fmt" "log" "github.com/bluenviron/gortsplib/v4" @@ -57,7 +58,10 @@ func main() { // read RTP packets from the reader and route them to the publisher reader.OnPacketRTPAny(func(_ *description.Media, _ format.Format, pkt *rtp.Packet) { - publisher.WritePacketRTP(desc.Medias[0], pkt) //nolint:errcheck + err2 := publisher.WritePacketRTP(desc.Medias[0], pkt) + if err2 != nil { + fmt.Printf("ERR: %v", err2) + } }) // start playing diff --git a/examples/proxy-backchannel/client.go b/examples/proxy-backchannel/client.go index e23e2f79..c100408a 100644 --- a/examples/proxy-backchannel/client.go +++ b/examples/proxy-backchannel/client.go @@ -78,7 +78,10 @@ func (c *client) read() error { } writeToClient := func(pkt *rtp.Packet) { - rc.WritePacketRTP(backChannelMedia, pkt) //nolint:errcheck + err2 := rc.WritePacketRTP(backChannelMedia, pkt) + if err2 != nil { + log.Printf("ERR: %v", err2) + } } // setup all medias @@ -98,7 +101,10 @@ func (c *client) read() error { log.Printf("received RTP packet from the client, routing to readers") // route incoming packets to the server stream - stream.WritePacketRTP(medi, pkt) //nolint:errcheck + err2 := stream.WritePacketRTP(medi, pkt) + if err2 != nil { + log.Printf("ERR: %v", err2) + } }) // start playing diff --git a/examples/proxy/client.go b/examples/proxy/client.go index 5b6c0d5c..0b4d521d 100644 --- a/examples/proxy/client.go +++ b/examples/proxy/client.go @@ -71,7 +71,10 @@ func (c *client) read() error { // called when a RTP packet arrives rc.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) { // route incoming packets to the server stream - stream.WritePacketRTP(medi, pkt) //nolint:errcheck + err2 := stream.WritePacketRTP(medi, pkt) + if err2 != nil { + log.Printf("ERR: %v", err2) + } }) // start playing diff --git a/examples/server-auth/main.go b/examples/server-auth/main.go index acd8f869..43fac880 100644 --- a/examples/server-auth/main.go +++ b/examples/server-auth/main.go @@ -16,8 +16,8 @@ import ( // This example shows how to // 1. create a RTSP server which accepts plain connections. -// 2. allow a single client to publish a stream with TCP or UDP, if it provides credentials. -// 3. allow multiple clients to read the stream with TCP, UDP or UDP-multicast, if they provide credentials. +// 2. allow a single client to publish a stream, if it provides credentials. +// 3. allow several clients to read the stream, if they provide credentials. const ( // credentials required to publish the stream @@ -188,7 +188,10 @@ func (sh *serverHandler) OnRecord(ctx *gortsplib.ServerHandlerOnRecordCtx) (*bas // called when receiving a RTP packet ctx.Session.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) { // route the RTP packet to all readers - sh.stream.WritePacketRTP(medi, pkt) //nolint:errcheck + err := sh.stream.WritePacketRTP(medi, pkt) + if err != nil { + log.Printf("ERR: %v", err) + } }) return &base.Response{ diff --git a/examples/server-tls/main.go b/examples/server-tls/main.go index 8d3c423e..36577244 100644 --- a/examples/server-tls/main.go +++ b/examples/server-tls/main.go @@ -15,9 +15,9 @@ import ( ) // This example shows how to -// 1. create a RTSP server which accepts only connections encrypted with TLS (RTSPS). -// 2. allow a single client to publish a stream with TCP. -// 3. allow multiple clients to read the stream with TCP. +// 1. create a RTSP server which uses secure protocols only (RTSPS, TLS). +// 2. allow a single client to publish a stream. +// 3. allow several clients to read the stream. type serverHandler struct { server *gortsplib.Server @@ -151,7 +151,10 @@ func (sh *serverHandler) OnRecord(ctx *gortsplib.ServerHandlerOnRecordCtx) (*bas // called when receiving a RTP packet ctx.Session.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) { // route the RTP packet to all readers - sh.stream.WritePacketRTP(medi, pkt) //nolint:errcheck + err := sh.stream.WritePacketRTP(medi, pkt) + if err != nil { + log.Printf("ERR: %v", err) + } }) return &base.Response{ @@ -160,7 +163,7 @@ func (sh *serverHandler) OnRecord(ctx *gortsplib.ServerHandlerOnRecordCtx) (*bas } func main() { - // setup certificates - they can be generated with + // load certificates - they can be generated with // openssl genrsa -out server.key 2048 // openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650 cert, err := tls.LoadX509KeyPair("server.crt", "server.key") @@ -168,7 +171,8 @@ func main() { panic(err) } - // configure the server + // configure the server. + // when TLSConfig is set, only secure protocols are used. h := &serverHandler{} h.server = &gortsplib.Server{ Handler: h, diff --git a/examples/server/main.go b/examples/server/main.go index dde9bbbb..10d57ede 100644 --- a/examples/server/main.go +++ b/examples/server/main.go @@ -15,8 +15,8 @@ import ( // This example shows how to // 1. create a RTSP server which accepts plain connections. -// 2. allow a single client to publish a stream with TCP or UDP. -// 3. allow multiple clients to read the stream with TCP, UDP or UDP-multicast. +// 2. allow a single client to publish a stream. +// 3. allow several clients to read the stream. type serverHandler struct { server *gortsplib.Server @@ -150,7 +150,10 @@ func (sh *serverHandler) OnRecord(ctx *gortsplib.ServerHandlerOnRecordCtx) (*bas // called when receiving a RTP packet ctx.Session.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) { // route the RTP packet to all readers - sh.stream.WritePacketRTP(medi, pkt) //nolint:errcheck + err := sh.stream.WritePacketRTP(medi, pkt) + if err != nil { + log.Printf("ERR: %v", err) + } }) return &base.Response{