2020-10-09 20:36:02 +00:00
|
|
|
// Package forwarding forwards incoming stream to other streaming services
|
2020-09-30 13:07:36 +00:00
|
|
|
package forwarding
|
2020-09-29 19:31:53 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-18 18:36:36 +00:00
|
|
|
"bufio"
|
2020-09-29 19:31:53 +00:00
|
|
|
"log"
|
2020-10-18 18:36:36 +00:00
|
|
|
"os/exec"
|
2020-10-18 14:05:28 +00:00
|
|
|
"time"
|
2020-10-04 18:16:29 +00:00
|
|
|
|
2020-10-18 14:05:28 +00:00
|
|
|
"gitlab.crans.org/nounous/ghostream/stream"
|
2020-09-29 19:31:53 +00:00
|
|
|
)
|
|
|
|
|
2020-09-30 13:07:36 +00:00
|
|
|
// Options to configure the stream forwarding.
|
|
|
|
// For each stream name, user can provide several URL to forward stream to
|
|
|
|
type Options map[string][]string
|
2020-09-29 20:40:49 +00:00
|
|
|
|
2020-10-04 18:16:29 +00:00
|
|
|
// Serve handles incoming packets from SRT and forward them to other external services
|
2020-10-18 14:05:28 +00:00
|
|
|
func Serve(streams map[string]*stream.Stream, cfg Options) {
|
2020-10-11 19:35:43 +00:00
|
|
|
if len(cfg) < 1 {
|
|
|
|
// No forwarding, ignore
|
2020-10-18 14:05:28 +00:00
|
|
|
return
|
2020-10-11 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 13:28:19 +00:00
|
|
|
log.Printf("Stream forwarding initialized")
|
2020-10-18 14:05:28 +00:00
|
|
|
for {
|
|
|
|
for name, st := range streams {
|
|
|
|
fwdCfg, ok := cfg[name]
|
|
|
|
if !ok {
|
|
|
|
// Not configured
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start forwarding
|
|
|
|
log.Printf("Starting forwarding for '%s'", name)
|
|
|
|
go forward(st, fwdCfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regulary pull stream list,
|
|
|
|
// it may be better to tweak the messaging system
|
|
|
|
// to get an event on a new stream.
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 18:36:36 +00:00
|
|
|
// Start a FFMPEG instance and redirect stream output to forwarded streams
|
2020-10-18 14:05:28 +00:00
|
|
|
func forward(st *stream.Stream, fwdCfg []string) {
|
2020-10-18 18:36:36 +00:00
|
|
|
output := make(chan []byte, 1024)
|
|
|
|
st.Register(output)
|
2020-09-29 19:31:53 +00:00
|
|
|
|
2020-09-30 13:28:19 +00:00
|
|
|
// Launch FFMPEG instance
|
2020-10-04 18:42:16 +00:00
|
|
|
params := []string{"-hide_banner", "-loglevel", "error", "-re", "-i", "pipe:0"}
|
2020-10-18 18:36:36 +00:00
|
|
|
for _, url := range fwdCfg {
|
2020-09-29 22:36:58 +00:00
|
|
|
params = append(params, "-f", "flv", "-preset", "ultrafast", "-tune", "zerolatency",
|
2020-10-18 18:36:36 +00:00
|
|
|
"-c", "copy", url)
|
2020-09-29 21:43:19 +00:00
|
|
|
}
|
|
|
|
ffmpeg := exec.Command("ffmpeg", params...)
|
2020-09-29 20:40:49 +00:00
|
|
|
|
2020-09-29 21:43:19 +00:00
|
|
|
// Open pipes
|
|
|
|
input, err := ffmpeg.StdinPipe()
|
|
|
|
if err != nil {
|
2020-10-18 18:36:36 +00:00
|
|
|
log.Printf("Error while opening forwarding ffmpeg input pipe: %s", err)
|
|
|
|
return
|
2020-09-29 21:43:19 +00:00
|
|
|
}
|
|
|
|
errOutput, err := ffmpeg.StderrPipe()
|
|
|
|
if err != nil {
|
2020-10-18 18:36:36 +00:00
|
|
|
log.Printf("Error while opening forwarding ffmpeg output pipe: %s", err)
|
|
|
|
return
|
2020-09-29 21:43:19 +00:00
|
|
|
}
|
2020-09-29 19:31:53 +00:00
|
|
|
|
2020-09-30 13:28:19 +00:00
|
|
|
// Start FFMpeg
|
2020-09-29 21:43:19 +00:00
|
|
|
if err := ffmpeg.Start(); err != nil {
|
2020-10-18 18:36:36 +00:00
|
|
|
log.Printf("Error while starting forwarding ffmpeg instance: %s", err)
|
2020-09-29 19:31:53 +00:00
|
|
|
}
|
2020-09-29 21:43:19 +00:00
|
|
|
|
2020-10-18 18:36:36 +00:00
|
|
|
// Kill FFMPEG when stream is ended
|
|
|
|
defer func() {
|
|
|
|
_ = input.Close()
|
|
|
|
_ = errOutput.Close()
|
|
|
|
_ = ffmpeg.Process.Kill()
|
|
|
|
st.Unregister(output)
|
|
|
|
}()
|
|
|
|
|
2020-10-04 18:47:05 +00:00
|
|
|
// Log standard error output
|
2020-09-29 21:43:19 +00:00
|
|
|
go func() {
|
|
|
|
scanner := bufio.NewScanner(errOutput)
|
|
|
|
for scanner.Scan() {
|
2020-10-18 18:36:36 +00:00
|
|
|
log.Printf("[FORWARDING FFMPEG] %s", scanner.Text())
|
2020-09-29 21:43:19 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-09-30 13:28:19 +00:00
|
|
|
|
2020-10-18 18:36:36 +00:00
|
|
|
// Read stream output and redirect immediately to ffmpeg
|
|
|
|
for data := range output {
|
|
|
|
_, err := input.Write(data)
|
2020-09-29 19:31:53 +00:00
|
|
|
|
2020-10-18 18:36:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error while writing to forwarded stream: %s", err)
|
|
|
|
break
|
|
|
|
}
|
2020-09-29 19:31:53 +00:00
|
|
|
}
|
|
|
|
}
|