fix client-publish-udp example

This commit is contained in:
aler9
2020-11-07 16:35:05 +01:00
parent c7c3e00388
commit e711b2925f
3 changed files with 59 additions and 33 deletions

View File

@@ -15,6 +15,13 @@ import (
// the frames with the UDP protocol.
func main() {
var writerDone chan struct{}
defer func() {
if writerDone != nil {
<-writerDone
}
}()
// open a listener to receive RTP/H264 frames
pc, err := net.ListenPacket("udp4", "127.0.0.1:9000")
if err != nil {
@@ -48,20 +55,26 @@ func main() {
}
defer conn.Close()
buf := make([]byte, 2048)
for {
// read frames from the source
n, _, err := pc.ReadFrom(buf)
if err != nil {
break
}
writerDone = make(chan struct{})
// write frames to the server
err = conn.WriteFrameUDP(track.Id, gortsplib.StreamTypeRtp, buf[:n])
if err != nil {
break
go func() {
defer close(writerDone)
buf := make([]byte, 2048)
for {
// read frames from the source
n, _, err := pc.ReadFrom(buf)
if err != nil {
break
}
// write frames to the server
err = conn.WriteFrameUDP(track.Id, gortsplib.StreamTypeRtp, buf[:n])
if err != nil {
break
}
}
}
}()
// wait until the connection is closed
err = conn.LoopUDP()