examples: print write errors (#810)

This commit is contained in:
Alessandro Ros
2025-06-28 12:42:37 +02:00
committed by GitHub
parent ebf9912f77
commit f4ef4ec23a
8 changed files with 43 additions and 20 deletions

View File

@@ -15,7 +15,7 @@ import (
// This example shows how to // This example shows how to
// 1. connect to a RTSP server. // 1. connect to a RTSP server.
// 2. check if there's a H264 stream and a MPEG-4 audio stream. // 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() { func main() {
c := gortsplib.Client{} c := gortsplib.Client{}

View File

@@ -14,7 +14,7 @@ import (
// This example shows how to // This example shows how to
// 1. connect to a RTSP server. // 1. connect to a RTSP server.
// 2. read all media streams on a path. // 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() { func main() {
c := gortsplib.Client{} c := gortsplib.Client{}
@@ -46,11 +46,11 @@ func main() {
// called when a RTP packet arrives // called when a RTP packet arrives
c.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) { 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) pts, ptsAvailable := c.PacketPTS2(medi, pkt)
log.Printf("PTS: available=%v, value=%v\n", ptsAvailable, pts) 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) ntp, ntpAvailable := c.PacketNTP(medi, pkt)
log.Printf("NTP: available=%v, value=%v\n", ntpAvailable, ntp) log.Printf("NTP: available=%v, value=%v\n", ntpAvailable, ntp)
}) })

View File

@@ -2,6 +2,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"github.com/bluenviron/gortsplib/v4" "github.com/bluenviron/gortsplib/v4"
@@ -57,7 +58,10 @@ func main() {
// read RTP packets from the reader and route them to the publisher // read RTP packets from the reader and route them to the publisher
reader.OnPacketRTPAny(func(_ *description.Media, _ format.Format, pkt *rtp.Packet) { 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 // start playing

View File

@@ -78,7 +78,10 @@ func (c *client) read() error {
} }
writeToClient := func(pkt *rtp.Packet) { 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 // setup all medias
@@ -98,7 +101,10 @@ func (c *client) read() error {
log.Printf("received RTP packet from the client, routing to readers") log.Printf("received RTP packet from the client, routing to readers")
// route incoming packets to the server stream // 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 // start playing

View File

@@ -71,7 +71,10 @@ func (c *client) read() error {
// called when a RTP packet arrives // called when a RTP packet arrives
rc.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) { rc.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) {
// route incoming packets to the server stream // 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 // start playing

View File

@@ -16,8 +16,8 @@ import (
// This example shows how to // This example shows how to
// 1. create a RTSP server which accepts plain connections. // 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. // 2. allow a single client to publish a stream, if it provides credentials.
// 3. allow multiple clients to read the stream with TCP, UDP or UDP-multicast, if they provide credentials. // 3. allow several clients to read the stream, if they provide credentials.
const ( const (
// credentials required to publish the stream // credentials required to publish the stream
@@ -188,7 +188,10 @@ func (sh *serverHandler) OnRecord(ctx *gortsplib.ServerHandlerOnRecordCtx) (*bas
// called when receiving a RTP packet // called when receiving a RTP packet
ctx.Session.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) { ctx.Session.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) {
// route the RTP packet to all readers // 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{ return &base.Response{

View File

@@ -15,9 +15,9 @@ import (
) )
// This example shows how to // This example shows how to
// 1. create a RTSP server which accepts only connections encrypted with TLS (RTSPS). // 1. create a RTSP server which uses secure protocols only (RTSPS, TLS).
// 2. allow a single client to publish a stream with TCP. // 2. allow a single client to publish a stream.
// 3. allow multiple clients to read the stream with TCP. // 3. allow several clients to read the stream.
type serverHandler struct { type serverHandler struct {
server *gortsplib.Server server *gortsplib.Server
@@ -151,7 +151,10 @@ func (sh *serverHandler) OnRecord(ctx *gortsplib.ServerHandlerOnRecordCtx) (*bas
// called when receiving a RTP packet // called when receiving a RTP packet
ctx.Session.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) { ctx.Session.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) {
// route the RTP packet to all readers // 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{ return &base.Response{
@@ -160,7 +163,7 @@ func (sh *serverHandler) OnRecord(ctx *gortsplib.ServerHandlerOnRecordCtx) (*bas
} }
func main() { func main() {
// setup certificates - they can be generated with // load certificates - they can be generated with
// openssl genrsa -out server.key 2048 // openssl genrsa -out server.key 2048
// openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650 // openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
cert, err := tls.LoadX509KeyPair("server.crt", "server.key") cert, err := tls.LoadX509KeyPair("server.crt", "server.key")
@@ -168,7 +171,8 @@ func main() {
panic(err) panic(err)
} }
// configure the server // configure the server.
// when TLSConfig is set, only secure protocols are used.
h := &serverHandler{} h := &serverHandler{}
h.server = &gortsplib.Server{ h.server = &gortsplib.Server{
Handler: h, Handler: h,

View File

@@ -15,8 +15,8 @@ import (
// This example shows how to // This example shows how to
// 1. create a RTSP server which accepts plain connections. // 1. create a RTSP server which accepts plain connections.
// 2. allow a single client to publish a stream with TCP or UDP. // 2. allow a single client to publish a stream.
// 3. allow multiple clients to read the stream with TCP, UDP or UDP-multicast. // 3. allow several clients to read the stream.
type serverHandler struct { type serverHandler struct {
server *gortsplib.Server server *gortsplib.Server
@@ -150,7 +150,10 @@ func (sh *serverHandler) OnRecord(ctx *gortsplib.ServerHandlerOnRecordCtx) (*bas
// called when receiving a RTP packet // called when receiving a RTP packet
ctx.Session.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) { ctx.Session.OnPacketRTPAny(func(medi *description.Media, _ format.Format, pkt *rtp.Packet) {
// route the RTP packet to all readers // 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{ return &base.Response{