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 (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
2020-10-04 18:16:29 +00:00
|
|
|
|
|
|
|
"gitlab.crans.org/nounous/ghostream/stream/srt"
|
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
|
|
|
|
func Serve(inputChannel chan srt.Packet, cfg Options) {
|
2020-10-11 19:35:43 +00:00
|
|
|
if len(cfg) < 1 {
|
|
|
|
// No forwarding, ignore
|
2020-10-13 07:38:44 +00:00
|
|
|
for {
|
|
|
|
<-inputChannel // Clear input channel
|
|
|
|
}
|
2020-10-11 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 13:28:19 +00:00
|
|
|
log.Printf("Stream forwarding initialized")
|
2020-10-04 18:42:16 +00:00
|
|
|
ffmpegInstances := make(map[string]*exec.Cmd)
|
|
|
|
ffmpegInputStreams := make(map[string]*io.WriteCloser)
|
2020-10-01 10:00:59 +00:00
|
|
|
for {
|
|
|
|
var err error = nil
|
2020-10-04 18:16:29 +00:00
|
|
|
// Wait for packets
|
|
|
|
packet := <-inputChannel
|
2020-10-01 10:00:59 +00:00
|
|
|
switch packet.PacketType {
|
|
|
|
case "register":
|
2020-10-04 18:42:16 +00:00
|
|
|
err = registerStream(packet.StreamName, ffmpegInstances, ffmpegInputStreams, cfg)
|
2020-10-01 10:00:59 +00:00
|
|
|
break
|
|
|
|
case "sendData":
|
2020-10-04 18:42:16 +00:00
|
|
|
err = sendPacket(packet.StreamName, ffmpegInputStreams, packet.Data)
|
2020-10-01 10:00:59 +00:00
|
|
|
break
|
|
|
|
case "close":
|
2020-10-04 18:42:16 +00:00
|
|
|
err = close(packet.StreamName, ffmpegInstances, ffmpegInputStreams)
|
2020-10-01 10:00:59 +00:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
log.Println("Unknown SRT packet type:", packet.PacketType)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-10-14 19:36:12 +00:00
|
|
|
log.Printf("Error occurred while receiving SRT packet of type %s: %s", packet.PacketType, err)
|
2020-10-01 10:00:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 18:42:16 +00:00
|
|
|
// registerStream creates ffmpeg instance associated with newly created stream
|
|
|
|
func registerStream(name string, ffmpegInstances map[string]*exec.Cmd, ffmpegInputStreams map[string]*io.WriteCloser, cfg Options) error {
|
2020-09-30 13:28:19 +00:00
|
|
|
streams, exist := cfg[name]
|
|
|
|
if !exist || len(streams) == 0 {
|
|
|
|
// Nothing to do, not configured
|
|
|
|
return nil
|
2020-09-29 21:43:19 +00:00
|
|
|
}
|
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-09-30 13:28:19 +00:00
|
|
|
for _, stream := range streams {
|
2020-09-29 22:36:58 +00:00
|
|
|
params = append(params, "-f", "flv", "-preset", "ultrafast", "-tune", "zerolatency",
|
|
|
|
"-c", "copy", stream)
|
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-09-30 13:28:19 +00:00
|
|
|
return err
|
2020-09-29 21:43:19 +00:00
|
|
|
}
|
|
|
|
errOutput, err := ffmpeg.StderrPipe()
|
|
|
|
if err != nil {
|
2020-09-30 13:28:19 +00:00
|
|
|
return err
|
2020-09-29 21:43:19 +00:00
|
|
|
}
|
2020-09-30 13:10:13 +00:00
|
|
|
ffmpegInstances[name] = ffmpeg
|
|
|
|
ffmpegInputStreams[name] = &input
|
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-09-30 13:28:19 +00:00
|
|
|
return err
|
2020-09-29 19:31:53 +00:00
|
|
|
}
|
2020-09-29 21:43:19 +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-04 18:47:05 +00:00
|
|
|
log.Printf("[FFMPEG %s] %s", name, scanner.Text())
|
2020-09-29 21:43:19 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-09-30 13:28:19 +00:00
|
|
|
|
|
|
|
return nil
|
2020-09-29 19:31:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 18:42:16 +00:00
|
|
|
// sendPacket forwards data to the ffmpeg instance related to the stream name
|
|
|
|
func sendPacket(name string, ffmpegInputStreams map[string]*io.WriteCloser, data []byte) error {
|
2020-09-30 13:10:13 +00:00
|
|
|
stdin := ffmpegInputStreams[name]
|
2020-09-30 14:53:15 +00:00
|
|
|
if stdin == nil {
|
|
|
|
// Don't need to forward stream
|
2020-10-01 10:00:59 +00:00
|
|
|
return nil
|
2020-09-30 14:53:15 +00:00
|
|
|
}
|
2020-09-29 21:43:19 +00:00
|
|
|
_, err := (*stdin).Write(data)
|
2020-10-01 10:00:59 +00:00
|
|
|
return err
|
2020-09-29 19:31:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 18:42:16 +00:00
|
|
|
// close ffmpeg instance associated with stream name
|
|
|
|
func close(name string, ffmpegInstances map[string]*exec.Cmd, ffmpegInputStreams map[string]*io.WriteCloser) error {
|
2020-09-30 13:10:13 +00:00
|
|
|
ffmpeg := ffmpegInstances[name]
|
2020-09-30 14:53:15 +00:00
|
|
|
if ffmpeg == nil {
|
|
|
|
// No stream to close
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-29 21:43:19 +00:00
|
|
|
if err := ffmpeg.Process.Kill(); err != nil {
|
2020-09-30 13:28:19 +00:00
|
|
|
return err
|
2020-09-29 19:31:53 +00:00
|
|
|
}
|
2020-09-30 13:10:13 +00:00
|
|
|
delete(ffmpegInstances, name)
|
|
|
|
delete(ffmpegInputStreams, name)
|
2020-09-30 13:28:19 +00:00
|
|
|
return nil
|
2020-09-29 19:31:53 +00:00
|
|
|
}
|