2020-09-29 19:31:53 +00:00
|
|
|
package multicast
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
2020-09-30 06:50:54 +00:00
|
|
|
// Options to configure the multicast:
|
|
|
|
//for each stream key, we can have several additional stream URL where the main stream is redirected to
|
2020-09-29 20:40:49 +00:00
|
|
|
type Options struct {
|
|
|
|
Outputs map[string][]string
|
|
|
|
}
|
|
|
|
|
2020-09-29 19:31:53 +00:00
|
|
|
var (
|
2020-09-29 20:40:49 +00:00
|
|
|
options 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
|
|
|
|
func New(cfg *Options) error {
|
|
|
|
options = *cfg
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterStream Declare a new open stream and create ffmpeg instances
|
2020-09-29 19:31:53 +00:00
|
|
|
func RegisterStream(streamKey string) {
|
2020-09-29 21:43:19 +00:00
|
|
|
if len(options.Outputs[streamKey]) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-09-29 19:31:53 +00:00
|
|
|
|
2020-09-29 21:43:19 +00:00
|
|
|
params := []string{"-re", "-i", "pipe:0"}
|
2020-09-29 20:40:49 +00:00
|
|
|
for _, stream := range options.Outputs[streamKey] {
|
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
|
|
|
}
|
|
|
|
// Launch FFMPEG instance
|
|
|
|
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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
output, err := ffmpeg.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
errOutput, err := ffmpeg.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ffmpegInstances[streamKey] = ffmpeg
|
|
|
|
ffmpegInputStreams[streamKey] = &input
|
2020-09-29 19:31:53 +00:00
|
|
|
|
2020-09-29 21:43:19 +00:00
|
|
|
if err := ffmpeg.Start(); err != nil {
|
|
|
|
panic(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() {
|
|
|
|
log.Println("[FFMPEG " + streamKey + "] " + scanner.Text())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// Log also error output
|
|
|
|
go func() {
|
|
|
|
scanner := bufio.NewScanner(errOutput)
|
|
|
|
for scanner.Scan() {
|
|
|
|
log.Println("[FFMPEG ERROR " + streamKey + "] " + scanner.Text())
|
|
|
|
}
|
|
|
|
}()
|
2020-09-29 19:31:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 20:40:49 +00:00
|
|
|
// SendPacket When a SRT packet is received, transmit it to all FFMPEG instances related to the stream key
|
2020-09-29 19:31:53 +00:00
|
|
|
func SendPacket(streamKey string, data []byte) {
|
2020-09-29 21:43:19 +00:00
|
|
|
stdin := ffmpegInputStreams[streamKey]
|
|
|
|
_, err := (*stdin).Write(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error while sending a packet to external streaming server for key "+streamKey, 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-29 19:31:53 +00:00
|
|
|
func CloseConnection(streamKey string) {
|
2020-09-29 21:43:19 +00:00
|
|
|
ffmpeg := ffmpegInstances[streamKey]
|
|
|
|
if err := ffmpeg.Process.Kill(); err != nil {
|
|
|
|
panic(err)
|
2020-09-29 19:31:53 +00:00
|
|
|
}
|
|
|
|
delete(ffmpegInstances, streamKey)
|
|
|
|
delete(ffmpegInputStreams, streamKey)
|
|
|
|
}
|