From 92f44c981db9fd9de3deae2a7ce1b34d3f8a238a Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Mon, 28 Sep 2020 16:32:35 +0200 Subject: [PATCH] More code for SRT handling --- go.mod | 1 + stream/srt/srt.go | 57 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index c0fa2ee..cfcef65 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.13 require ( github.com/go-ldap/ldap/v3 v3.2.3 github.com/haivision/srtgo v0.0.0-20200731151239-e00427ae473a + github.com/pion/rtp v1.6.0 github.com/pion/webrtc/v3 v3.0.0-beta.5 github.com/prometheus/client_golang v1.7.1 github.com/spf13/viper v1.7.1 diff --git a/stream/srt/srt.go b/stream/srt/srt.go index 3a0d5ff..1c75370 100644 --- a/stream/srt/srt.go +++ b/stream/srt/srt.go @@ -4,6 +4,7 @@ import ( "log" "github.com/haivision/srtgo" + "github.com/pion/rtp" ) // Options holds web package configuration @@ -16,7 +17,7 @@ func Serve(cfg *Options) { log.Printf("SRT server listening on %s", cfg.ListenAddress) options := make(map[string]string) - options["transtype"] = "file" + options["transtype"] = "live" // FIXME: cfg.ListenAddress -> host and port sck := srtgo.NewSrtSocket("0.0.0.0", 9710, options) @@ -25,24 +26,48 @@ func Serve(cfg *Options) { for { s, err := sck.Accept() if err != nil { - log.Println("Error occured while accepting request:", err) + log.Println("Error occurred while accepting request:", err) continue } - go func(s *srtgo.SrtSocket) { - buff := make([]byte, 2048) - for { - n, err := s.Read(buff, 10000) - if err != nil { - log.Println("Error occured while reading SRT socket:", err) - break - } - if n == 0 { - // End of stream - break - } - log.Printf("Received %d bytes", n) + buff := make([]byte, 2048) + n, err := s.Read(buff, 10000) + if err != nil { + log.Println("Error occurred while reading SRT socket:", err) + break + } + if n == 0 { + // End of stream + break + } + + // Unmarshal the incoming packet + packet := &rtp.Packet{} + if err = packet.Unmarshal(buff[:n]); err != nil { + log.Println("Error occured while unmarshaling SRT:", err) + break + } + + // videoTrack, err := peerConnection.NewTrack(payloadType, packet.SSRC, "video", "pion") + + // Read RTP packets forever and send them to the WebRTC Client + for { + n, err := s.Read(buff, 10000) + if err != nil { + log.Println("Error occured while reading SRT socket:", err) + break } - }(s) + + log.Printf("Received %d bytes", n) + + packet := &rtp.Packet{} + if err := packet.Unmarshal(buff[:n]); err != nil { + panic(err) + } + payloadType := uint8(22) // FIXME put vp8 payload + packet.Header.PayloadType = payloadType + + //err := videoTrack.WriteRTP(packet) + } } }