mirror of
https://gitlab.crans.org/nounous/ghostream.git
synced 2024-12-23 00:22:19 +00:00
Use only one instance of FFPMEG per SRT stream instead of one per additional cast
This commit is contained in:
parent
351870e92a
commit
9049935ee4
@ -13,8 +13,8 @@ type Options struct {
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
options Options
|
options Options
|
||||||
ffmpegInstances = make(map[string][]*exec.Cmd)
|
ffmpegInstances = make(map[string]*exec.Cmd)
|
||||||
ffmpegInputStreams = make(map[string][]*io.WriteCloser)
|
ffmpegInputStreams = make(map[string]*io.WriteCloser)
|
||||||
)
|
)
|
||||||
|
|
||||||
// New Load configuration
|
// New Load configuration
|
||||||
@ -25,16 +25,19 @@ func New(cfg *Options) error {
|
|||||||
|
|
||||||
// RegisterStream Declare a new open stream and create ffmpeg instances
|
// RegisterStream Declare a new open stream and create ffmpeg instances
|
||||||
func RegisterStream(streamKey string) {
|
func RegisterStream(streamKey string) {
|
||||||
ffmpegInstances[streamKey] = []*exec.Cmd{}
|
if len(options.Outputs[streamKey]) == 0 {
|
||||||
ffmpegInputStreams[streamKey] = []*io.WriteCloser{}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// TODO Export the list of multicasts
|
params := []string{"-re", "-i", "pipe:0"}
|
||||||
for _, stream := range options.Outputs[streamKey] {
|
for _, stream := range options.Outputs[streamKey] {
|
||||||
// Launch FFMPEG instance
|
|
||||||
// TODO Set optimal parameters
|
// TODO Set optimal parameters
|
||||||
ffmpeg := exec.Command("ffmpeg", "-re", "-i", "pipe:0", "-f", "flv", "-c:v", "libx264", "-preset",
|
params = append(params, "-f", "flv", "-c:v", "libx264", "-preset",
|
||||||
"veryfast", "-maxrate", "3000k", "-bufsize", "6000k", "-pix_fmt", "yuv420p", "-g", "50", "-c:a", "aac",
|
"veryfast", "-maxrate", "3000k", "-bufsize", "6000k", "-pix_fmt", "yuv420p", "-g", "50", "-c:a", "aac",
|
||||||
"-b:a", "160k", "-ac", "2", "-ar", "44100", stream)
|
"-b:a", "160k", "-ac", "2", "-ar", "44100", stream)
|
||||||
|
}
|
||||||
|
// Launch FFMPEG instance
|
||||||
|
ffmpeg := exec.Command("ffmpeg", params...)
|
||||||
|
|
||||||
// Open pipes
|
// Open pipes
|
||||||
input, err := ffmpeg.StdinPipe()
|
input, err := ffmpeg.StdinPipe()
|
||||||
@ -49,8 +52,8 @@ func RegisterStream(streamKey string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
ffmpegInstances[streamKey] = append(ffmpegInstances[streamKey], ffmpeg)
|
ffmpegInstances[streamKey] = ffmpeg
|
||||||
ffmpegInputStreams[streamKey] = append(ffmpegInputStreams[streamKey], &input)
|
ffmpegInputStreams[streamKey] = &input
|
||||||
|
|
||||||
if err := ffmpeg.Start(); err != nil {
|
if err := ffmpeg.Start(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -71,26 +74,22 @@ func RegisterStream(streamKey string) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// SendPacket When a SRT packet is received, transmit it to all FFMPEG instances related to the stream key
|
// SendPacket When a SRT packet is received, transmit it to all FFMPEG instances related to the stream key
|
||||||
func SendPacket(streamKey string, data []byte) {
|
func SendPacket(streamKey string, data []byte) {
|
||||||
for _, stdin := range ffmpegInputStreams[streamKey] {
|
stdin := ffmpegInputStreams[streamKey]
|
||||||
_, err := (*stdin).Write(data)
|
_, err := (*stdin).Write(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error while sending a packet to external streaming server for key "+streamKey, err)
|
log.Println("Error while sending a packet to external streaming server for key "+streamKey, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// CloseConnection When the stream is ended, close FFMPEG instances
|
// CloseConnection When the stream is ended, close FFMPEG instances
|
||||||
func CloseConnection(streamKey string) {
|
func CloseConnection(streamKey string) {
|
||||||
for _, ffmpeg := range ffmpegInstances[streamKey] {
|
ffmpeg := ffmpegInstances[streamKey]
|
||||||
if err := ffmpeg.Process.Kill(); err != nil {
|
if err := ffmpeg.Process.Kill(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
delete(ffmpegInstances, streamKey)
|
delete(ffmpegInstances, streamKey)
|
||||||
delete(ffmpegInputStreams, streamKey)
|
delete(ffmpegInputStreams, streamKey)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user