ghostream/stream/webrtc/ingest.go

261 lines
6.9 KiB
Go

// Package webrtc provides the backend to simulate a WebRTC client to send stream
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"
)
func ingest(name string, q *messaging.Quality) {
// Register to get stream
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()
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")
if err != nil {
log.Printf("Error while searching opus codec: %s", err)
}
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")
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)
}
// Receive video
go func() {
h264, _ := h264reader.NewReader(&b)
var spsAndPpsCache []byte
for {
nal, h264Err := h264.NextNAL()
if h264Err == io.EOF {
fmt.Printf("All video frames parsed and sent")
return
}
if h264Err != nil {
log.Printf("Failed to read from H264: %s", h264Err)
break
}
if videoTracks[name] == nil {
videoTracks[name] = make([]*webrtc.Track, 0)
}
nal.Data = append([]byte{0x00, 0x00, 0x00, 0x01}, nal.Data...)
if nal.UnitType == h264reader.NalUnitTypeSPS || nal.UnitType == h264reader.NalUnitTypePPS {
spsAndPpsCache = append(spsAndPpsCache, nal.Data...)
continue
} else if nal.UnitType == h264reader.NalUnitTypeCodedSliceIdr {
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 {
panic(h264Err)
}
}
}
}()
// Receive audio
go func() {
inboundRTPPacket := make([]byte, 1500) // UDP MTU
for {
n, _, err := audioListener.ReadFromUDP(inboundRTPPacket)
if err != nil {
log.Printf("Failed to read from UDP: %s", err)
break
}
packet := &rtp.Packet{}
if err := packet.Unmarshal(inboundRTPPacket[:n]); err != nil {
log.Printf("Failed to unmarshal RTP srtPacket: %s", err)
continue
}
if audioTracks[name] == nil {
audioTracks[name] = make([]*webrtc.Track, 0)
}
// Write RTP srtPacket to all audio tracks
// Adapt payload and SSRC to match destination
for _, audioTrack := range audioTracks[name] {
packet.Header.PayloadType = audioTrack.PayloadType()
packet.Header.SSRC = audioTrack.SSRC()
if writeErr := audioTrack.WriteRTP(packet); writeErr != nil {
log.Printf("Failed to write to audio track: %s", err)
continue
}
}
}
}()
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)
}
// 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) {
ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0",
// Vidéo
"-an", "-c:v", "copy", "-bsf", "h264_mp4toannexb",
"-f", "h264", "pipe:1",
// Audio
"-vn", "-c:a", "libopus", "-b:a", "96k",
"-f", "rtp", fmt.Sprintf("rtp://127.0.0.1:%d", listeningPort)}
ffmpeg = exec.Command("ffmpeg", ffmpegArgs...)
// Handle errors output
errOutput, err := ffmpeg.StderrPipe()
if err != nil {
return nil, nil, err
}
go func() {
scanner := bufio.NewScanner(errOutput)
for scanner.Scan() {
log.Printf("[WEBRTC FFMPEG %s] %s", "demo", scanner.Text())
}
}()
// Handle stream input
input, err := ffmpeg.StdinPipe()
if err != nil {
return nil, nil, err
}
go func() {
for data := range in {
if _, err := input.Write(data); err != nil {
log.Printf("Failed to write data to ffmpeg input: %s", err)
}
}
// End of stream
ffmpeg.Process.Kill()
}()
output, err := ffmpeg.StdoutPipe()
if err != nil {
return nil, nil, err
}
// Start process
err = ffmpeg.Start()
return ffmpeg, &output, err
}