1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-24 16:02:20 +00:00

Use random UDP ports to able to have multiple concurrent streams

This commit is contained in:
Yohann D'ANELLO 2020-10-29 13:11:30 +01:00
parent 9fc3d37e72
commit 5b40aa886f

View File

@ -3,7 +3,9 @@ package webrtc
import ( import (
"bufio" "bufio"
"fmt"
"log" "log"
"math/rand"
"net" "net"
"os/exec" "os/exec"
@ -17,20 +19,23 @@ func ingest(name string, q *messaging.Quality) {
videoInput := make(chan []byte, 1024) videoInput := make(chan []byte, 1024)
q.Register(videoInput) q.Register(videoInput)
// Open a UDP Listener for RTP Packets on port 5004 // FIXME Mux into RTP without having multiple UDP listeners
audioListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 5004}) firstPort := int(rand.Int31n(63535)) + 2000
// Open UDP listeners for RTP Packets
audioListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: firstPort})
if err != nil { if err != nil {
log.Printf("Faited to open UDP listener %s", err) log.Printf("Faited to open UDP listener %s", err)
return return
} }
videoListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 5005}) videoListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: firstPort + 1})
if err != nil { if err != nil {
log.Printf("Faited to open UDP listener %s", err) log.Printf("Faited to open UDP listener %s", err)
return return
} }
// Start ffmpag to convert videoInput to video and audio UDP // Start ffmpag to convert videoInput to video and audio UDP
ffmpeg, err := startFFmpeg(videoInput) ffmpeg, err := startFFmpeg(videoInput, firstPort)
if err != nil { if err != nil {
log.Printf("Error while starting ffmpeg: %s", err) log.Printf("Error while starting ffmpeg: %s", err)
return return
@ -115,14 +120,14 @@ func ingest(name string, q *messaging.Quality) {
q.Unregister(videoInput) q.Unregister(videoInput)
} }
func startFFmpeg(in <-chan []byte) (ffmpeg *exec.Cmd, err error) { func startFFmpeg(in <-chan []byte, listeningPort int) (ffmpeg *exec.Cmd, err error) {
ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0", ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0",
// Audio // Audio
"-vn", "-c:a", "libopus", "-b:a", "160k", "-vn", "-c:a", "libopus", "-b:a", "160k",
"-f", "rtp", "rtp://127.0.0.1:5004", "-f", "rtp", fmt.Sprintf("rtp://127.0.0.1:%d", listeningPort),
// Source // Source
"-an", "-c:v", "copy", "-b:v", "3000k", "-maxrate", "5000k", "-bufsize", "5000k", "-an", "-c:v", "copy", "-b:v", "3000k", "-maxrate", "5000k", "-bufsize", "5000k",
"-f", "rtp", "rtp://127.0.0.1:5005"} "-f", "rtp", fmt.Sprintf("rtp://127.0.0.1:%d", listeningPort+1)}
ffmpeg = exec.Command("ffmpeg", ffmpegArgs...) ffmpeg = exec.Command("ffmpeg", ffmpegArgs...)
// Handle errors output // Handle errors output