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-10-21 09:47:06 +00:00
|
|
|
"fmt"
|
2020-09-29 19:31:53 +00:00
|
|
|
"log"
|
2020-10-18 18:36:36 +00:00
|
|
|
"os/exec"
|
2020-10-21 09:47:06 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2020-10-04 18:16:29 +00:00
|
|
|
|
2020-10-19 17:28:30 +00:00
|
|
|
"gitlab.crans.org/nounous/ghostream/messaging"
|
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-19 17:28:30 +00:00
|
|
|
func Serve(streams *messaging.Streams, 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-10-19 17:28:30 +00:00
|
|
|
// Subscribe to new stream event
|
|
|
|
event := make(chan string, 8)
|
|
|
|
streams.Subscribe(event)
|
2020-09-30 13:28:19 +00:00
|
|
|
log.Printf("Stream forwarding initialized")
|
2020-10-19 17:28:30 +00:00
|
|
|
|
|
|
|
// For each new stream
|
|
|
|
for name := range event {
|
|
|
|
streamCfg, ok := cfg[name]
|
|
|
|
if !ok {
|
|
|
|
// Not configured
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get stream
|
|
|
|
stream, err := streams.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to get stream '%s'", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get specific quality
|
|
|
|
// FIXME: make it possible to forward other qualities
|
|
|
|
qualityName := "source"
|
|
|
|
quality, err := stream.GetQuality(qualityName)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to get quality '%s'", qualityName)
|
2020-10-18 14:05:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 17:28:30 +00:00
|
|
|
// Start forwarding
|
|
|
|
log.Printf("Starting forwarding for '%s' quality '%s'", name, qualityName)
|
2020-10-21 09:47:06 +00:00
|
|
|
go forward(name, quality, streamCfg)
|
2020-10-18 14:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 18:36:36 +00:00
|
|
|
// Start a FFMPEG instance and redirect stream output to forwarded streams
|
2020-10-21 09:47:06 +00:00
|
|
|
func forward(streamName string, q *messaging.Quality, fwdCfg []string) {
|
2020-10-18 18:36:36 +00:00
|
|
|
output := make(chan []byte, 1024)
|
2020-10-19 17:28:30 +00:00
|
|
|
q.Register(output)
|
2020-09-29 19:31:53 +00:00
|
|
|
|
2020-09-30 13:28:19 +00:00
|
|
|
// Launch FFMPEG instance
|
2020-10-21 11:34:39 +00:00
|
|
|
params := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0"}
|
2020-10-18 18:36:36 +00:00
|
|
|
for _, url := range fwdCfg {
|
2020-10-21 09:47:06 +00:00
|
|
|
// If the url should be date-formatted, replace special characters with the current time information
|
|
|
|
now := time.Now()
|
|
|
|
formattedURL := strings.ReplaceAll(url, "%Y", fmt.Sprintf("%04d", now.Year()))
|
|
|
|
formattedURL = strings.ReplaceAll(formattedURL, "%m", fmt.Sprintf("%02d", now.Month()))
|
|
|
|
formattedURL = strings.ReplaceAll(formattedURL, "%d", fmt.Sprintf("%02d", now.Day()))
|
|
|
|
formattedURL = strings.ReplaceAll(formattedURL, "%H", fmt.Sprintf("%02d", now.Hour()))
|
|
|
|
formattedURL = strings.ReplaceAll(formattedURL, "%M", fmt.Sprintf("%02d", now.Minute()))
|
|
|
|
formattedURL = strings.ReplaceAll(formattedURL, "%S", fmt.Sprintf("%02d", now.Second()))
|
|
|
|
formattedURL = strings.ReplaceAll(formattedURL, "%name", streamName)
|
|
|
|
|
2020-09-29 22:36:58 +00:00
|
|
|
params = append(params, "-f", "flv", "-preset", "ultrafast", "-tune", "zerolatency",
|
2020-10-21 09:47:06 +00:00
|
|
|
"-c", "copy", formattedURL)
|
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()
|
2020-10-19 17:28:30 +00:00
|
|
|
q.Unregister(output)
|
2020-10-18 18:36:36 +00:00
|
|
|
}()
|
|
|
|
|
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-21 09:47:06 +00:00
|
|
|
log.Printf("[FORWARDING FFMPEG %s] %s", streamName, 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
|
|
|
}
|
|
|
|
}
|