ghostream/stream/forwarding/forwarding.go

135 lines
3.0 KiB
Go
Raw Normal View History

2020-09-30 13:07:36 +00:00
package forwarding
2020-09-29 19:31:53 +00:00
import (
"bufio"
"gitlab.crans.org/nounous/ghostream/stream/srt"
2020-09-29 19:31:53 +00:00
"io"
"log"
"os/exec"
)
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 19:31:53 +00:00
var (
cfg Options
forwardingChannel chan srt.Packet
ffmpegInstances = make(map[string]*exec.Cmd)
ffmpegInputStreams = make(map[string]*io.WriteCloser)
2020-09-29 19:31:53 +00:00
)
// Serve Load configuration and initialize SRT channel
func Serve(c Options, channel chan srt.Packet) {
cfg = c
forwardingChannel = channel
log.Printf("Stream forwarding initialized")
waitForPackets()
}
func waitForPackets() {
for {
var err error = nil
packet := <-forwardingChannel
switch packet.PacketType {
case "register":
err = RegisterStream(packet.StreamName)
break
case "sendData":
err = SendPacket(packet.StreamName, packet.Data)
break
case "close":
err = CloseConnection(packet.StreamName)
break
default:
log.Println("Unknown SRT packet type:", packet.PacketType)
break
}
if err != nil {
log.Printf("Error occured while receiving SRT packet of type %s: %s", packet.PacketType, err)
}
}
}
// RegisterStream Declare a new open stream and create ffmpeg instances
func RegisterStream(name string) error {
streams, exist := cfg[name]
if !exist || len(streams) == 0 {
// Nothing to do, not configured
return nil
}
2020-09-29 19:31:53 +00:00
// Launch FFMPEG instance
params := []string{"-re", "-i", "pipe:0"}
for _, stream := range streams {
params = append(params, "-f", "flv", "-preset", "ultrafast", "-tune", "zerolatency",
"-c", "copy", stream)
}
ffmpeg := exec.Command("ffmpeg", params...)
// Open pipes
input, err := ffmpeg.StdinPipe()
if err != nil {
return err
}
output, err := ffmpeg.StdoutPipe()
if err != nil {
return err
}
errOutput, err := ffmpeg.StderrPipe()
if err != nil {
return err
}
2020-09-30 13:10:13 +00:00
ffmpegInstances[name] = ffmpeg
ffmpegInputStreams[name] = &input
2020-09-29 19:31:53 +00:00
// Start FFMpeg
if err := ffmpeg.Start(); err != nil {
return err
2020-09-29 19:31:53 +00:00
}
// Log ffmpeg output
go func() {
scanner := bufio.NewScanner(output)
for scanner.Scan() {
log.Printf("[FFMPEG %s] %s", name, scanner.Text())
}
}()
2020-09-30 13:10:13 +00:00
// Log also error output
go func() {
scanner := bufio.NewScanner(errOutput)
for scanner.Scan() {
log.Printf("[FFMPEG ERR %s] %s", name, scanner.Text())
}
}()
return nil
2020-09-29 19:31:53 +00:00
}
// SendPacket forward data to all FFMpeg instances related to the stream name
func SendPacket(name string, 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
return nil
2020-09-30 14:53:15 +00:00
}
_, err := (*stdin).Write(data)
return err
2020-09-29 19:31:53 +00:00
}
// CloseConnection When the stream is ended, close FFMPEG instances
func CloseConnection(name string) 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
}
if err := ffmpeg.Process.Kill(); err != nil {
return err
2020-09-29 19:31:53 +00:00
}
2020-09-30 13:10:13 +00:00
delete(ffmpegInstances, name)
delete(ffmpegInputStreams, name)
return nil
2020-09-29 19:31:53 +00:00
}