Rename multicast to stream forwarding

This commit is contained in:
Alexandre Iooss 2020-09-30 15:07:36 +02:00
parent 6ba297bf96
commit 8f7384ba35
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
9 changed files with 33 additions and 43 deletions

View File

@ -16,6 +16,7 @@ Features:
- SRT stream input, supported by FFMpeg, OBS and Gstreamer. - SRT stream input, supported by FFMpeg, OBS and Gstreamer.
- Low-latency streaming, sub-second with web player. - Low-latency streaming, sub-second with web player.
- Authentification of incoming stream using LDAP server. - Authentification of incoming stream using LDAP server.
- Possibility to forward stream to other streaming servers.
## Installation on Debian/Ubuntu ## Installation on Debian/Ubuntu

View File

@ -17,6 +17,13 @@ auth:
# uri: ldap://127.0.0.1:389 # uri: ldap://127.0.0.1:389
# userdn: cn=users,dc=example,dc=com # userdn: cn=users,dc=example,dc=com
# Configure stream forwarding
forwarding:
# Example to forward a stream named "demo" to Twitch and YouTube
#demo:
# - rtmp://live-cdg.twitch.tv/app/STREAM_KEY
# - rtmp://a.rtmp.youtube.com/live2/STREAM_KEY
# Prometheus monitoring endpoint # Prometheus monitoring endpoint
monitoring: monitoring:
listenAddress: 127.0.0.1:2112 listenAddress: 127.0.0.1:2112
@ -46,9 +53,3 @@ webrtc:
# STUN servers, you should host your own Coturn instance # STUN servers, you should host your own Coturn instance
STUNServers: STUNServers:
- stun:stun.l.google.com:19302 - stun:stun.l.google.com:19302
# Configuration for the multicast feature
multicast:
outputs:
# demo:
# - rtmp://localhost:1925

View File

@ -4,7 +4,6 @@ After=syslog.target
[Service] [Service]
User=www-data User=www-data
EnvironmentFile=-/etc/default/ghostream
ExecStart=/usr/bin/ghostream ExecStart=/usr/bin/ghostream
Restart=on-failure Restart=on-failure

12
main.go
View File

@ -3,13 +3,13 @@
package main package main
import ( import (
"gitlab.crans.org/nounous/ghostream/stream/multicast"
"log" "log"
"strings" "strings"
"github.com/spf13/viper" "github.com/spf13/viper"
"gitlab.crans.org/nounous/ghostream/auth" "gitlab.crans.org/nounous/ghostream/auth"
"gitlab.crans.org/nounous/ghostream/internal/monitoring" "gitlab.crans.org/nounous/ghostream/internal/monitoring"
"gitlab.crans.org/nounous/ghostream/stream/forwarding"
"gitlab.crans.org/nounous/ghostream/stream/srt" "gitlab.crans.org/nounous/ghostream/stream/srt"
"gitlab.crans.org/nounous/ghostream/stream/webrtc" "gitlab.crans.org/nounous/ghostream/stream/webrtc"
"gitlab.crans.org/nounous/ghostream/web" "gitlab.crans.org/nounous/ghostream/web"
@ -58,7 +58,7 @@ func loadConfiguration() {
viper.SetDefault("WebRTC.MinPortUDP", 10000) viper.SetDefault("WebRTC.MinPortUDP", 10000)
viper.SetDefault("WebRTC.MaxPortUDP", 10005) viper.SetDefault("WebRTC.MaxPortUDP", 10005)
viper.SetDefault("WebRTC.STUNServers", []string{"stun:stun.l.google.com:19302"}) viper.SetDefault("WebRTC.STUNServers", []string{"stun:stun.l.google.com:19302"})
viper.SetDefault("Multicast.Outputs", make(map[string][]string)) viper.SetDefault("Forwarding", make(map[string][]string))
// Copy STUN configuration to clients // Copy STUN configuration to clients
viper.Set("Web.STUNServers", viper.Get("WebRTC.STUNServers")) viper.Set("Web.STUNServers", viper.Get("WebRTC.STUNServers"))
@ -69,8 +69,8 @@ func main() {
loadConfiguration() loadConfiguration()
cfg := struct { cfg := struct {
Auth auth.Options Auth auth.Options
Forwarding forwarding.Options
Monitoring monitoring.Options Monitoring monitoring.Options
Multicast multicast.Options
Srt srt.Options Srt srt.Options
Web web.Options Web web.Options
WebRTC webrtc.Options WebRTC webrtc.Options
@ -96,10 +96,10 @@ func main() {
go web.Serve(remoteSdpChan, localSdpChan, &cfg.Web) go web.Serve(remoteSdpChan, localSdpChan, &cfg.Web)
go webrtc.Serve(remoteSdpChan, localSdpChan, &cfg.WebRTC) go webrtc.Serve(remoteSdpChan, localSdpChan, &cfg.WebRTC)
// Init multicast // Init stream forwarding
err = multicast.New(&cfg.Multicast) err = forwarding.New(&cfg.Forwarding)
if err != nil { if err != nil {
log.Fatalln("Failed to load multicast app:", err) log.Fatalln("Failed to init stream forwarding:", err)
} }
// Wait for routines // Wait for routines

View File

@ -1,14 +1,15 @@
package main package main
import ( import (
"testing"
"github.com/spf13/viper" "github.com/spf13/viper"
"gitlab.crans.org/nounous/ghostream/auth" "gitlab.crans.org/nounous/ghostream/auth"
"gitlab.crans.org/nounous/ghostream/internal/monitoring" "gitlab.crans.org/nounous/ghostream/internal/monitoring"
"gitlab.crans.org/nounous/ghostream/stream/multicast" "gitlab.crans.org/nounous/ghostream/stream/forwarding"
"gitlab.crans.org/nounous/ghostream/stream/srt" "gitlab.crans.org/nounous/ghostream/stream/srt"
"gitlab.crans.org/nounous/ghostream/stream/webrtc" "gitlab.crans.org/nounous/ghostream/stream/webrtc"
"gitlab.crans.org/nounous/ghostream/web" "gitlab.crans.org/nounous/ghostream/web"
"testing"
) )
// TestLoadConfiguration tests the configuration file loading and the init of some parameters // TestLoadConfiguration tests the configuration file loading and the init of some parameters
@ -16,8 +17,8 @@ func TestLoadConfiguration(t *testing.T) {
loadConfiguration() loadConfiguration()
cfg := struct { cfg := struct {
Auth auth.Options Auth auth.Options
Forwarding forwarding.Options
Monitoring monitoring.Options Monitoring monitoring.Options
Multicast multicast.Options
Srt srt.Options Srt srt.Options
Web web.Options Web web.Options
WebRTC webrtc.Options WebRTC webrtc.Options

View File

@ -1,4 +1,4 @@
package multicast package forwarding
import ( import (
"bufio" "bufio"
@ -7,11 +7,9 @@ import (
"os/exec" "os/exec"
) )
// Options to configure the multicast: // Options to configure the stream forwarding.
//for each stream key, we can have several additional stream URL where the main stream is redirected to // For each stream name, user can provide several URL to forward stream to
type Options struct { type Options map[string][]string
Outputs map[string][]string
}
var ( var (
options Options options Options
@ -27,12 +25,12 @@ func New(cfg *Options) error {
// RegisterStream Declare a new open stream and create ffmpeg instances // RegisterStream Declare a new open stream and create ffmpeg instances
func RegisterStream(streamKey string) { func RegisterStream(streamKey string) {
if len(options.Outputs[streamKey]) == 0 { if len(options[streamKey]) == 0 {
return return
} }
params := []string{"-re", "-i", "pipe:0"} params := []string{"-re", "-i", "pipe:0"}
for _, stream := range options.Outputs[streamKey] { for _, stream := range options[streamKey] {
params = append(params, "-f", "flv", "-preset", "ultrafast", "-tune", "zerolatency", params = append(params, "-f", "flv", "-preset", "ultrafast", "-tune", "zerolatency",
"-c", "copy", stream) "-c", "copy", stream)
} }

View File

@ -0,0 +1 @@
package forwarding

View File

@ -1 +0,0 @@
package multicast

View File

@ -1,13 +1,12 @@
package srt package srt
import ( import (
"gitlab.crans.org/nounous/ghostream/stream/multicast"
"log" "log"
"net" "net"
"strconv" "strconv"
"github.com/haivision/srtgo" "github.com/haivision/srtgo"
"github.com/pion/rtp" "gitlab.crans.org/nounous/ghostream/stream/forwarding"
) )
// Options holds web package configuration // Options holds web package configuration
@ -57,42 +56,33 @@ func Serve(cfg *Options) {
// Create a new buffer // Create a new buffer
buff := make([]byte, 2048) buff := make([]byte, 2048)
// Setup linked multicasts // Setup stream forwarding
multicast.RegisterStream("demo") // FIXME Replace with real stream key forwarding.RegisterStream("demo") // FIXME Replace with real stream key
// Read RTP packets forever and send them to the WebRTC Client // Read RTP packets forever and send them to the WebRTC Client
for { for {
n, err := s.Read(buff, 10000) n, err := s.Read(buff, 10000)
if err != nil { if err != nil {
log.Println("Error occured while reading SRT socket:", err) log.Println("Error occured while reading SRT socket:", err)
multicast.CloseConnection("demo") forwarding.CloseConnection("demo")
break break
} }
if n == 0 { if n == 0 {
// End of stream // End of stream
log.Printf("Received no bytes, stopping stream.") log.Printf("Received no bytes, stopping stream.")
multicast.CloseConnection("demo") forwarding.CloseConnection("demo")
break break
} }
log.Printf("Received %d bytes", n) log.Printf("Received %d bytes", n)
// Send raw packet to other streams // Send raw packet to other streams
multicast.SendPacket("demo", buff[:n]) forwarding.SendPacket("demo", buff[:n])
// Unmarshal incoming packet
packet := &rtp.Packet{}
if err := packet.Unmarshal(buff[:n]); err != nil {
log.Println("Error occured while unmarshaling SRT:", err)
multicast.CloseConnection("demo")
break
}
// TODO: Send to WebRTC // TODO: Send to WebRTC
//payloadType := uint8(22) // FIXME put vp8 payload // See https://github.com/ebml-go/webm/blob/master/reader.go
//packet.Header.PayloadType = payloadType //err := videoTrack.WriteSample(media.Sample{Data: data, Samples: uint32(sampleCount)})
//err := videoTrack.WriteRTP(packet)
} }
} }
} }