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-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-09-29 19:31:53 +00:00
|
|
|
var (
|
2020-09-30 13:28:19 +00:00
|
|
|
cfg Options
|
2020-09-29 21:43:19 +00:00
|
|
|
ffmpegInstances = make(map[string]*exec.Cmd)
|
|
|
|
ffmpegInputStreams = make(map[string]*io.WriteCloser)
|
2020-09-29 19:31:53 +00:00
|
|
|
)
|
|
|
|
|
2020-09-29 20:40:49 +00:00
|
|
|
// New Load configuration
|
2020-09-30 13:28:19 +00:00
|
|
|
func New(c Options) {
|
|
|
|
cfg = c
|
|
|
|
log.Printf("Stream forwarding initialized")
|
2020-09-29 20:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterStream Declare a new open stream and create ffmpeg instances
|
2020-09-30 13:28:19 +00:00
|
|
|
func RegisterStream(name string) error {
|
|
|
|
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-09-29 21:43:19 +00:00
|
|
|
params := []string{"-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
|
|
|
}
|
|
|
|
output, err := ffmpeg.StdoutPipe()
|
|
|
|
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
|
|
|
|
|
|
|
// Log ffmpeg output
|
|
|
|
go func() {
|
|
|
|
scanner := bufio.NewScanner(output)
|
|
|
|
for scanner.Scan() {
|
2020-09-30 13:28:19 +00:00
|
|
|
log.Printf("[FFMPEG %s] %s", name, scanner.Text())
|
2020-09-29 21:43:19 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-09-30 13:10:13 +00:00
|
|
|
|
2020-09-29 21:43:19 +00:00
|
|
|
// Log also error output
|
|
|
|
go func() {
|
|
|
|
scanner := bufio.NewScanner(errOutput)
|
|
|
|
for scanner.Scan() {
|
2020-09-30 13:28:19 +00:00
|
|
|
log.Printf("[FFMPEG ERR %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-09-30 13:28:19 +00:00
|
|
|
// SendPacket forward data to all FFMpeg instances related to the stream name
|
2020-09-30 13:10:13 +00:00
|
|
|
func SendPacket(name string, data []byte) {
|
|
|
|
stdin := ffmpegInputStreams[name]
|
2020-09-30 14:53:15 +00:00
|
|
|
if stdin == nil {
|
|
|
|
// Don't need to forward stream
|
|
|
|
return
|
|
|
|
}
|
2020-09-29 21:43:19 +00:00
|
|
|
_, err := (*stdin).Write(data)
|
|
|
|
if err != nil {
|
2020-09-30 13:10:13 +00:00
|
|
|
log.Printf("Error while sending a packet to external streaming server for key %s: %s", name, err)
|
2020-09-29 19:31:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:40:49 +00:00
|
|
|
// CloseConnection When the stream is ended, close FFMPEG instances
|
2020-09-30 13:28:19 +00:00
|
|
|
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
|
|
|
|
}
|
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
|
|
|
}
|