fix examples

This commit is contained in:
aler9
2020-07-13 09:42:16 +02:00
parent 025fa58edd
commit 643dce1dd7
2 changed files with 24 additions and 3 deletions

View File

@@ -47,11 +47,13 @@ func main() {
panic(err) panic(err)
} }
frame := &gortsplib.InterleavedFrame{Content: make([]byte, 512*1024)} frame := &gortsplib.InterleavedFrame{Content: make([]byte, 0, 512*1024)}
for { for {
frame.Content = frame.Content[:cap(frame.Content)]
err := rconn.ReadFrame(frame) err := rconn.ReadFrame(frame)
if err != nil { if err != nil {
panic(err) fmt.Println("connection is closed")
break
} }
fmt.Printf("packet from track %d, type %v: %v\n", fmt.Printf("packet from track %d, type %v: %v\n",

View File

@@ -45,6 +45,7 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer rtpl.Close()
rtpListeners = append(rtpListeners, rtpl) rtpListeners = append(rtpListeners, rtpl)
rtcpPort := 9001 + i*2 rtcpPort := 9001 + i*2
@@ -52,6 +53,7 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer rtcpl.Close()
rtcpListeners = append(rtcpListeners, rtcpl) rtcpListeners = append(rtcpListeners, rtcpl)
_, _, _, err = rconn.SetupUdp(u, media, rtpPort, rtcpPort) _, _, _, err = rconn.SetupUdp(u, media, rtpPort, rtcpPort)
@@ -65,6 +67,22 @@ func main() {
panic(err) panic(err)
} }
done := make(chan struct{})
// send periodic keepalive
go func() {
keepaliveTicker := time.NewTicker(30 * time.Second)
for range keepaliveTicker.C {
_, err = rconn.Options(u)
if err != nil {
fmt.Println("connection is closed")
close(done)
break
}
}
}()
// receive RTP packets
for trackId, l := range rtpListeners { for trackId, l := range rtpListeners {
go func(trackId int, l net.PacketConn) { go func(trackId int, l net.PacketConn) {
buf := make([]byte, 2048) buf := make([]byte, 2048)
@@ -79,6 +97,7 @@ func main() {
}(trackId, l) }(trackId, l)
} }
// receive RTCP packets
for trackId, l := range rtcpListeners { for trackId, l := range rtcpListeners {
go func(trackId int, l net.PacketConn) { go func(trackId int, l net.PacketConn) {
buf := make([]byte, 2048) buf := make([]byte, 2048)
@@ -93,5 +112,5 @@ func main() {
}(trackId, l) }(trackId, l)
} }
select {} <-done
} }