2020-10-09 20:36:02 +00:00
|
|
|
// Package webrtc provides the backend to simulate a WebRTC client to send stream
|
2020-10-05 08:11:30 +00:00
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-10-29 12:11:30 +00:00
|
|
|
"fmt"
|
2020-11-07 23:08:52 +00:00
|
|
|
"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"
|
2020-11-08 13:21:36 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"os/exec"
|
2020-10-05 08:11:30 +00:00
|
|
|
)
|
|
|
|
|
2020-10-19 17:48:44 +00:00
|
|
|
func ingest(name string, q *messaging.Quality) {
|
2020-10-18 14:05:28 +00:00
|
|
|
// Register to get stream
|
|
|
|
videoInput := make(chan []byte, 1024)
|
2020-10-19 17:48:44 +00:00
|
|
|
q.Register(videoInput)
|
2020-10-05 08:11:30 +00:00
|
|
|
|
2020-11-08 13:21:36 +00:00
|
|
|
// FIXME Mux into RTP without having multiple UDP listeners
|
|
|
|
firstPort := int(rand.Int31n(63535)) + 2000
|
2020-10-29 12:11:30 +00:00
|
|
|
|
2020-11-08 13:21:36 +00:00
|
|
|
// Open UDP listener for RTP Packets
|
|
|
|
audioListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: firstPort})
|
2020-10-13 07:50:46 +00:00
|
|
|
if err != nil {
|
2020-11-08 13:21:36 +00:00
|
|
|
log.Printf("Faited to open UDP listener %s", err)
|
|
|
|
return
|
2020-10-13 07:50:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 14:29:18 +00:00
|
|
|
// Start ffmpeg to convert videoInput to audio UDP
|
2020-11-08 13:21:36 +00:00
|
|
|
ffmpeg, ffmpegOut, err := startFFmpeg(videoInput, firstPort)
|
2020-10-13 07:50:46 +00:00
|
|
|
if err != nil {
|
2020-11-08 13:21:36 +00:00
|
|
|
log.Printf("Error while starting ffmpeg: %s", err)
|
|
|
|
return
|
2020-10-13 07:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Receive video
|
|
|
|
go func() {
|
2020-11-08 14:29:18 +00:00
|
|
|
h264, _ := h264reader.NewReader(bufio.NewReader(*ffmpegOut))
|
2020-11-07 19:36:33 +00:00
|
|
|
var spsAndPpsCache []byte
|
2020-11-07 20:45:18 +00:00
|
|
|
|
2020-10-13 07:50:46 +00:00
|
|
|
for {
|
2020-11-07 19:36:33 +00:00
|
|
|
nal, h264Err := h264.NextNAL()
|
2020-11-07 15:58:59 +00:00
|
|
|
if h264Err == io.EOF {
|
|
|
|
fmt.Printf("All video frames parsed and sent")
|
|
|
|
return
|
2020-10-05 08:11:30 +00:00
|
|
|
}
|
2020-11-07 15:58:59 +00:00
|
|
|
if h264Err != nil {
|
|
|
|
log.Printf("Failed to read from H264: %s", h264Err)
|
|
|
|
break
|
2020-10-13 07:50:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 14:05:28 +00:00
|
|
|
if videoTracks[name] == nil {
|
|
|
|
videoTracks[name] = make([]*webrtc.Track, 0)
|
2020-10-05 08:11:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:36:33 +00:00
|
|
|
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{}
|
|
|
|
}
|
|
|
|
|
2020-10-18 14:05:28 +00:00
|
|
|
for _, videoTrack := range videoTracks[name] {
|
2020-11-07 19:36:33 +00:00
|
|
|
if h264Err = videoTrack.WriteSample(media.Sample{Data: nal.Data, Samples: 90000}); h264Err != nil {
|
|
|
|
panic(h264Err)
|
2020-10-12 22:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-13 07:50:46 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-10-12 22:52:08 +00:00
|
|
|
|
2020-10-13 07:50:46 +00:00
|
|
|
// Receive audio
|
|
|
|
go func() {
|
2020-10-13 08:10:25 +00:00
|
|
|
inboundRTPPacket := make([]byte, 1500) // UDP MTU
|
2020-10-13 07:50:46 +00:00
|
|
|
for {
|
|
|
|
n, _, err := audioListener.ReadFromUDP(inboundRTPPacket)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to read from UDP: %s", err)
|
2020-10-18 14:05:28 +00:00
|
|
|
break
|
2020-10-13 07:50:46 +00:00
|
|
|
}
|
|
|
|
packet := &rtp.Packet{}
|
|
|
|
if err := packet.Unmarshal(inboundRTPPacket[:n]); err != nil {
|
|
|
|
log.Printf("Failed to unmarshal RTP srtPacket: %s", err)
|
|
|
|
continue
|
2020-10-05 08:11:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 14:05:28 +00:00
|
|
|
if audioTracks[name] == nil {
|
|
|
|
audioTracks[name] = make([]*webrtc.Track, 0)
|
2020-10-13 07:50:46 +00:00
|
|
|
}
|
2020-10-05 08:11:30 +00:00
|
|
|
|
2020-10-13 07:50:46 +00:00
|
|
|
// Write RTP srtPacket to all audio tracks
|
|
|
|
// Adapt payload and SSRC to match destination
|
2020-10-18 14:05:28 +00:00
|
|
|
for _, audioTrack := range audioTracks[name] {
|
2020-10-13 07:50:46 +00:00
|
|
|
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
|
2020-10-05 08:11:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 07:50:46 +00:00
|
|
|
}()
|
|
|
|
|
2020-11-07 23:08:52 +00:00
|
|
|
// Wait for stopped ffmpeg
|
2020-11-08 13:21:36 +00:00
|
|
|
if err = ffmpeg.Wait(); err != nil {
|
|
|
|
log.Printf("Faited to wait for ffmpeg: %s", err)
|
|
|
|
}
|
2020-11-07 23:08:52 +00:00
|
|
|
|
2020-11-08 13:21:36 +00:00
|
|
|
// Close UDP listener
|
|
|
|
if err = audioListener.Close(); err != nil {
|
|
|
|
log.Printf("Faited to close UDP listener: %s", err)
|
|
|
|
}
|
|
|
|
q.Unregister(videoInput)
|
2020-10-18 14:05:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 21:11:21 +00:00
|
|
|
func startFFmpeg(in <-chan []byte, listeningPort int) (ffmpeg *exec.Cmd, stdout *io.ReadCloser, err error) {
|
2020-10-18 14:05:28 +00:00
|
|
|
ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0",
|
2020-11-07 20:45:18 +00:00
|
|
|
// Vidéo
|
2020-11-07 21:11:21 +00:00
|
|
|
"-an", "-c:v", "copy", "-bsf", "h264_mp4toannexb",
|
|
|
|
"-f", "h264", "pipe:1",
|
2020-10-27 18:32:23 +00:00
|
|
|
// Audio
|
2020-11-07 13:58:15 +00:00
|
|
|
"-vn", "-c:a", "libopus", "-b:a", "96k",
|
2020-11-07 15:58:59 +00:00
|
|
|
"-f", "rtp", fmt.Sprintf("rtp://127.0.0.1:%d", listeningPort)}
|
2020-10-18 14:05:28 +00:00
|
|
|
ffmpeg = exec.Command("ffmpeg", ffmpegArgs...)
|
|
|
|
|
|
|
|
// Handle errors output
|
|
|
|
errOutput, err := ffmpeg.StderrPipe()
|
|
|
|
if err != nil {
|
2020-11-07 21:11:21 +00:00
|
|
|
return nil, nil, err
|
2020-10-18 14:05:28 +00:00
|
|
|
}
|
2020-10-13 07:50:46 +00:00
|
|
|
go func() {
|
|
|
|
scanner := bufio.NewScanner(errOutput)
|
|
|
|
for scanner.Scan() {
|
|
|
|
log.Printf("[WEBRTC FFMPEG %s] %s", "demo", scanner.Text())
|
2020-10-05 08:11:30 +00:00
|
|
|
}
|
2020-10-13 07:50:46 +00:00
|
|
|
}()
|
2020-10-18 14:05:28 +00:00
|
|
|
|
|
|
|
// Handle stream input
|
|
|
|
input, err := ffmpeg.StdinPipe()
|
|
|
|
if err != nil {
|
2020-11-07 21:11:21 +00:00
|
|
|
return nil, nil, err
|
2020-10-18 14:05:28 +00:00
|
|
|
}
|
|
|
|
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()
|
|
|
|
}()
|
|
|
|
|
2020-11-07 21:11:21 +00:00
|
|
|
output, err := ffmpeg.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-18 14:05:28 +00:00
|
|
|
// Start process
|
|
|
|
err = ffmpeg.Start()
|
2020-11-07 21:11:21 +00:00
|
|
|
return ffmpeg, &output, err
|
2020-10-05 08:11:30 +00:00
|
|
|
}
|