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
// 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{}

View File

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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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{

View File

@@ -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,

View File

@@ -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{