1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-23 01:32:19 +00:00

Refractor streamKey to name

This commit is contained in:
Alexandre Iooss 2020-09-30 15:10:13 +02:00
parent 8f7384ba35
commit 2550d75c57
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02

View File

@ -24,13 +24,13 @@ 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(name string) {
if len(options[streamKey]) == 0 { if len(options[name]) == 0 {
return return
} }
params := []string{"-re", "-i", "pipe:0"} params := []string{"-re", "-i", "pipe:0"}
for _, stream := range options[streamKey] { for _, stream := range options[name] {
params = append(params, "-f", "flv", "-preset", "ultrafast", "-tune", "zerolatency", params = append(params, "-f", "flv", "-preset", "ultrafast", "-tune", "zerolatency",
"-c", "copy", stream) "-c", "copy", stream)
} }
@ -50,8 +50,8 @@ func RegisterStream(streamKey string) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
ffmpegInstances[streamKey] = ffmpeg ffmpegInstances[name] = ffmpeg
ffmpegInputStreams[streamKey] = &input ffmpegInputStreams[name] = &input
if err := ffmpeg.Start(); err != nil { if err := ffmpeg.Start(); err != nil {
panic(err) panic(err)
@ -61,33 +61,34 @@ func RegisterStream(streamKey string) {
go func() { go func() {
scanner := bufio.NewScanner(output) scanner := bufio.NewScanner(output)
for scanner.Scan() { for scanner.Scan() {
log.Println("[FFMPEG " + streamKey + "] " + scanner.Text()) log.Println("[FFMPEG " + name + "] " + scanner.Text())
} }
}() }()
// Log also error output // Log also error output
go func() { go func() {
scanner := bufio.NewScanner(errOutput) scanner := bufio.NewScanner(errOutput)
for scanner.Scan() { for scanner.Scan() {
log.Println("[FFMPEG ERROR " + streamKey + "] " + scanner.Text()) log.Println("[FFMPEG ERROR " + name + "] " + scanner.Text())
} }
}() }()
} }
// 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(name string, data []byte) {
stdin := ffmpegInputStreams[streamKey] stdin := ffmpegInputStreams[name]
_, 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.Printf("Error while sending a packet to external streaming server for key %s: %s", name, 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(name string) {
ffmpeg := ffmpegInstances[streamKey] ffmpeg := ffmpegInstances[name]
if err := ffmpeg.Process.Kill(); err != nil { if err := ffmpeg.Process.Kill(); err != nil {
panic(err) panic(err)
} }
delete(ffmpegInstances, streamKey) delete(ffmpegInstances, name)
delete(ffmpegInputStreams, streamKey) delete(ffmpegInputStreams, name)
} }