1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2025-06-27 22:42:15 +02:00

Try to mux into RTP with FFMPEG bindings

This commit is contained in:
Yohann D'ANELLO
2020-10-19 10:18:45 +02:00
parent 698b83fe6f
commit fa281e6b31
4 changed files with 100 additions and 25 deletions

View File

@ -2,13 +2,14 @@
package webrtc
import (
"github.com/3d0c/gmf"
"github.com/pion/rtp"
"github.com/pion/webrtc/v3"
"gitlab.crans.org/nounous/ghostream/stream"
"log"
"net"
"strings"
"time"
"gitlab.crans.org/nounous/ghostream/stream"
)
var (
@ -52,11 +53,63 @@ func ingest(name string, input *stream.Stream, audio *stream.Stream) {
audio.Register(audioInput)
activeStream[name] = struct{}{}
inputCtx := gmf.NewCtx()
avioInputCtx, _ := gmf.NewAVIOContext(inputCtx, &gmf.AVIOHandlers{ReadPacket: func() ([]byte, int) {
data := <-audioInput
return data, len(data)
}})
log.Println("Open input")
inputCtx.SetPb(avioInputCtx).OpenInput("")
log.Println("Opened")
defer inputCtx.CloseInput()
defer avioInputCtx.Release()
if audioTracks[name] == nil {
audioTracks[name] = make([]*webrtc.Track, 0)
}
udpListener, _ := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 1234})
outputCtx, _ := gmf.NewOutputCtxWithFormatName("rtp://127.0.0.1:1234", "rtp")
avioOutputCtx, _ := gmf.NewAVIOContext(outputCtx, &gmf.AVIOHandlers{WritePacket: func(data []byte) int {
n := len(data)
log.Printf("Read %d bytes", n)
return n
}})
// FIXME DON'T RAN AN UDP LISTENER, PLIZ GET DIRECTLY UDP PACKETS, WHY IS IT SO COMPLICATED????
// outputCtx.SetPb(avioOutputCtx)
defer outputCtx.CloseOutput()
defer avioOutputCtx.Release()
log.Printf("%d streams", inputCtx.StreamsCnt())
for i := 0; i < inputCtx.StreamsCnt(); i++ {
srcStream, err := inputCtx.GetStream(i)
if err != nil {
log.Println("GetStream error")
}
outputCtx.AddStreamWithCodeCtx(srcStream.CodecCtx())
}
outputCtx.Dump()
if err := outputCtx.WriteHeader(); err != nil {
log.Printf("Unable to write RTP header: %s", err)
}
// Receive audio data
go func() {
buff := make([]byte, 1500)
for {
n, _ := udpListener.Read(buff)
if n == 0 {
return
}
packet := &rtp.Packet{}
if err := packet.Unmarshal(<-audioInput); err != nil {
if err := packet.Unmarshal(buff[:n]); err != nil {
log.Printf("Failed to unmarshal RTP srtPacket: %s", err)
continue
}
@ -78,6 +131,19 @@ func ingest(name string, input *stream.Stream, audio *stream.Stream) {
}
}()
first := false
for packet := range inputCtx.GetNewPackets() {
if first { //if read from rtsp ,the first packets is wrong.
if err := outputCtx.WritePacketNoBuffer(packet); err != nil {
log.Printf("Error while writing packet: %s", err)
}
}
first = true
packet.Free()
}
select {}
// TODO Register to all substreams and make RTP packets. Don't transcode in this package.