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

WebRTC is registering to the audio-transcoded stream

This commit is contained in:
Yohann D'ANELLO
2020-10-18 22:07:11 +02:00
parent 20776d897c
commit 698b83fe6f
2 changed files with 135 additions and 94 deletions

View File

@ -4,8 +4,9 @@ package audio
import (
"bufio"
"fmt"
"io"
"log"
"math/rand"
"net"
"os/exec"
"strings"
"time"
@ -84,20 +85,37 @@ func transcode(input, output *stream.Stream, cfg *Options) {
}
// Stop transcode
ffmpeg.Process.Kill()
_ = ffmpeg.Process.Kill()
_ = rawvideo.Close()
}
// Start a ffmpeg instance to convert stream into audio
func startFFmpeg(in <-chan []byte, cfg *Options) (*exec.Cmd, *io.ReadCloser, error) {
func startFFmpeg(in <-chan []byte, cfg *Options) (*exec.Cmd, *net.UDPConn, error) {
// TODO in a future release: remove FFMPEG dependency and transcode directly using the libopus API
// FIXME It seems impossible to get a RTP Packet from standard output.
// We need to find a clean solution, without waiting on UDP listeners.
// FIXME We should also not build RTP packets here.
port := 0
var udpListener *net.UDPConn
var err error
for {
port = rand.Intn(65535)
udpListener, err = net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: port})
if err != nil {
if strings.Contains(fmt.Sprintf("%s", err), "address already in use") {
continue
}
return nil, nil, err
}
break
}
bitrate := fmt.Sprintf("%dk", cfg.Bitrate)
// Use copy audio codec, assume for now that libopus is used by the streamer
ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0",
"-vn", "-c:a", "copy", "-b:a", bitrate, "-f", "rtp", "pipe:1"}
"-vn", "-c:a", "copy", "-b:a", bitrate, "-f", "rtp", fmt.Sprintf("rtp://127.0.0.1:%d", port)}
ffmpeg := exec.Command("ffmpeg", ffmpegArgs...)
// Handle errors output
@ -112,12 +130,6 @@ func startFFmpeg(in <-chan []byte, cfg *Options) (*exec.Cmd, *io.ReadCloser, err
}
}()
// Handle audio output
output, err := ffmpeg.StdoutPipe()
if err != nil {
return nil, nil, err
}
// Handle stream input
input, err := ffmpeg.StdinPipe()
if err != nil {
@ -125,11 +137,11 @@ func startFFmpeg(in <-chan []byte, cfg *Options) (*exec.Cmd, *io.ReadCloser, err
}
go func() {
for data := range in {
input.Write(data)
_, _ = input.Write(data)
}
}()
// Start process
err = ffmpeg.Start()
return ffmpeg, &output, err
return ffmpeg, udpListener, err
}