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:
@ -4,9 +4,9 @@ package audio
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/3d0c/gmf"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
@ -63,15 +63,15 @@ func transcode(input, output *stream.Stream, cfg *Options) {
|
||||
// Start ffmpeg to transcode video to audio
|
||||
videoInput := make(chan []byte, 1024)
|
||||
input.Register(videoInput)
|
||||
ffmpeg, rawvideo, err := startFFmpeg(videoInput, cfg)
|
||||
ffmpeg, audio, err := startFFmpeg(videoInput, cfg)
|
||||
if err != nil {
|
||||
log.Printf("Error while starting ffmpeg: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
dataBuff := make([]byte, 1316) // UDP MTU
|
||||
dataBuff := make([]byte, gmf.IO_BUFFER_SIZE) // UDP MTU
|
||||
for {
|
||||
n, err := (*rawvideo).Read(dataBuff)
|
||||
n, err := (*audio).Read(dataBuff)
|
||||
if err != nil {
|
||||
log.Printf("An error occurred while reading input: %s", err)
|
||||
break
|
||||
@ -86,36 +86,36 @@ func transcode(input, output *stream.Stream, cfg *Options) {
|
||||
|
||||
// Stop transcode
|
||||
_ = ffmpeg.Process.Kill()
|
||||
_ = rawvideo.Close()
|
||||
_ = (*audio).Close()
|
||||
}
|
||||
|
||||
// Start a ffmpeg instance to convert stream into audio
|
||||
func startFFmpeg(in <-chan []byte, cfg *Options) (*exec.Cmd, *net.UDPConn, error) {
|
||||
func startFFmpeg(in <-chan []byte, cfg *Options) (*exec.Cmd, *io.ReadCloser, 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
|
||||
/* 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
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
break
|
||||
}
|
||||
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", fmt.Sprintf("rtp://127.0.0.1:%d", port)}
|
||||
"-vn", "-c:a", "copy", "-b:a", bitrate, "-f", "mpegts", "pipe:1"}
|
||||
ffmpeg := exec.Command("ffmpeg", ffmpegArgs...)
|
||||
|
||||
// Handle errors output
|
||||
@ -130,6 +130,12 @@ func startFFmpeg(in <-chan []byte, cfg *Options) (*exec.Cmd, *net.UDPConn, error
|
||||
}
|
||||
}()
|
||||
|
||||
// Handle text output
|
||||
output, err := ffmpeg.StdoutPipe()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// Handle stream input
|
||||
input, err := ffmpeg.StdinPipe()
|
||||
if err != nil {
|
||||
@ -143,5 +149,5 @@ func startFFmpeg(in <-chan []byte, cfg *Options) (*exec.Cmd, *net.UDPConn, error
|
||||
|
||||
// Start process
|
||||
err = ffmpeg.Start()
|
||||
return ffmpeg, udpListener, err
|
||||
return ffmpeg, &output, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user