diff --git a/go.mod b/go.mod index aab8f7f..7411214 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module gitlab.crans.org/nounous/ghostream go 1.15 require ( - github.com/3d0c/gmf v0.0.0-20200614092945-e58d8d5a6035 github.com/go-ldap/ldap/v3 v3.2.3 github.com/gorilla/websocket v1.4.0 github.com/haivision/srtgo v0.0.0-20201025191851-67964e8f497a diff --git a/go.sum b/go.sum index 2374a19..6628b7c 100644 --- a/go.sum +++ b/go.sum @@ -7,8 +7,6 @@ dmitri.shuralyov.com/html/belt v0.0.0-20180602232347-f7d459c86be0/go.mod h1:JLBr dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1:a1inKt/atXimZ4Mv927x+r7UpyzRUf4emIoiiSC2TN4= dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU= git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg= -github.com/3d0c/gmf v0.0.0-20200614092945-e58d8d5a6035 h1:QZb1aMKxiYdGGieyIDmXuw9I9YcGWGViTrpQ6vcZX7Q= -github.com/3d0c/gmf v0.0.0-20200614092945-e58d8d5a6035/go.mod h1:0QMRcUq2JsDECeAq7bj4h79k7XbhtTsrPUQf6G7qfPs= github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c h1:/IBSNwUN8+eKzUzbJPqhK839ygXJ82sde8x3ogr6R28= github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= diff --git a/stream/webrtc/ingest.go b/stream/webrtc/ingest.go index 8a3cb95..ff85f90 100644 --- a/stream/webrtc/ingest.go +++ b/stream/webrtc/ingest.go @@ -3,20 +3,17 @@ package webrtc import ( "bufio" - "bytes" "fmt" - "io" - "log" - "math/rand" - "net" - "os/exec" - - "github.com/3d0c/gmf" "github.com/pion/rtp" "github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3/pkg/media" "github.com/pion/webrtc/v3/pkg/media/h264reader" "gitlab.crans.org/nounous/ghostream/messaging" + "io" + "log" + "math/rand" + "net" + "os/exec" ) func ingest(name string, q *messaging.Quality) { @@ -24,98 +21,26 @@ func ingest(name string, q *messaging.Quality) { videoInput := make(chan []byte, 1024) q.Register(videoInput) - inputCtx := gmf.NewCtx() - avioInputCtx, _ := gmf.NewAVIOContext(inputCtx, &gmf.AVIOHandlers{ReadPacket: func() ([]byte, int) { - data := <-videoInput - return data, len(data) - }}) - log.Println("Open input") - inputCtx.SetPb(avioInputCtx).OpenInput("") - log.Println("Opened") - defer inputCtx.CloseInput() - defer avioInputCtx.Release() + // FIXME Mux into RTP without having multiple UDP listeners + firstPort := int(rand.Int31n(63535)) + 2000 - if audioTracks[name] == nil { - audioTracks[name] = make([]*webrtc.Track, 0) - } - - port := rand.Int()%64355 + 2000 - audioListener, _ := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: port}) - - b := bytes.Buffer{} - videoOutputCtx, _ := gmf.NewOutputCtxWithFormatName("/dev/null", "h264") - avioOutputCtx, _ := gmf.NewAVIOContext(videoOutputCtx, &gmf.AVIOHandlers{WritePacket: func(data []byte) int { - n, _ := b.Write(data) - return n - }}) - - videoOutputCtx.SetPb(avioOutputCtx) - defer videoOutputCtx.CloseOutput() - defer avioOutputCtx.Release() - - audioOutputCtx, _ := gmf.NewOutputCtxWithFormatName(fmt.Sprintf("rtp://127.0.0.1:%d", port), "rtp") - defer audioOutputCtx.CloseOutput() - - log.Printf("%d streams", inputCtx.StreamsCnt()) - - c, err := gmf.FindEncoder("libopus") + // Open UDP listener for RTP Packets + audioListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: firstPort}) if err != nil { - log.Printf("Error while searching opus codec: %s", err) + log.Printf("Faited to open UDP listener %s", err) + return } - audioStream, _ := inputCtx.GetBestStream(gmf.AVMEDIA_TYPE_AUDIO) - ctx := gmf.NewCodecCtx(c, []*gmf.Option{ - {Key: "time_base", Val: audioStream.CodecCtx().TimeBase().AVR()}, - {Key: "ar", Val: audioStream.CodecCtx().SampleRate()}, - {Key: "ac", Val: audioStream.CodecCtx().Channels()}, - }) - par := gmf.NewCodecParameters() - _ = par.FromContext(audioStream.CodecCtx()) - defer par.Free() - _, _ = audioOutputCtx.AddStreamWithCodeCtx(ctx) - //c, err = gmf.FindEncoder("libx264") - - videoStream, _ := inputCtx.GetBestStream(gmf.AVMEDIA_TYPE_VIDEO) - c, err = gmf.FindEncoder("libx264") + // Start ffmpag to convert videoInput to audio UDP + ffmpeg, ffmpegOut, err := startFFmpeg(videoInput, firstPort) if err != nil { - log.Printf("Error while searching x264 codec: %s", err) - } - - ctx = gmf.NewCodecCtx(c, []*gmf.Option{ - {Key: "time_base", Val: gmf.AVR{Num: 1, Den: 25}}, - {Key: "pixel_format", Val: gmf.AV_PIX_FMT_YUV420P}, - // Save original - {Key: "video_size", Val: videoStream.CodecCtx().GetVideoSize()}, - {Key: "b", Val: 500000}, - }) - par = gmf.NewCodecParameters() - _ = par.FromContext(videoStream.CodecCtx()) - defer par.Free() - _, _ = videoOutputCtx.AddStreamWithCodeCtx(ctx) - - for i := 0; i < inputCtx.StreamsCnt(); i++ { - srcStream, err := inputCtx.GetStream(i) - if err != nil { - log.Println("GetStream error") - continue - } - - log.Println(srcStream.CodecCtx()) - } - videoOutputCtx.Dump() - audioOutputCtx.Dump() - - if err := videoOutputCtx.WriteHeader(); err != nil { - log.Printf("Unable to write video header: %s", err) - } - - if err := audioOutputCtx.WriteHeader(); err != nil { - log.Printf("Unable to write audio header: %s", err) + log.Printf("Error while starting ffmpeg: %s", err) + return } // Receive video go func() { - h264, _ := h264reader.NewReader(&b) + h264, _ := h264reader.NewReader(*ffmpegOut) var spsAndPpsCache []byte for { @@ -142,7 +67,6 @@ func ingest(name string, q *messaging.Quality) { nal.Data = append(spsAndPpsCache, nal.Data...) spsAndPpsCache = []byte{} } - log.Println(len(nal.Data)) for _, videoTrack := range videoTracks[name] { if h264Err = videoTrack.WriteSample(media.Sample{Data: nal.Data, Samples: 90000}); h264Err != nil { @@ -184,31 +108,16 @@ func ingest(name string, q *messaging.Quality) { } }() - for packet := range inputCtx.GetNewPackets() { - if packet.StreamIndex() == 0 { - if err := videoOutputCtx.WritePacketNoBuffer(packet); err != nil { - log.Printf("Error while writing packet: %s", err) - } - } else if packet.StreamIndex() == 1 { - packet = packet.Clone() - packet.SetStreamIndex(0) - if err := audioOutputCtx.WritePacketNoBuffer(packet); err != nil { - log.Printf("Error while writing packet: %s", err) - } - } - packet.Free() + // Wait for stopped ffmpeg + if err = ffmpeg.Wait(); err != nil { + log.Printf("Faited to wait for ffmpeg: %s", err) } - // Wait for stopped ffmpeg - /* if err = ffmpeg.Wait(); err != nil { - log.Printf("Faited to wait for ffmpeg: %s", err) - } - - // Close UDP listener - if err = audioListener.Close(); err != nil { - log.Printf("Faited to close UDP listener: %s", err) - } - q.Unregister(videoInput)*/ + // Close UDP listener + if err = audioListener.Close(); err != nil { + log.Printf("Faited to close UDP listener: %s", err) + } + q.Unregister(videoInput) } func startFFmpeg(in <-chan []byte, listeningPort int) (ffmpeg *exec.Cmd, stdout *io.ReadCloser, err error) {