From 238d80658b8c91bdccd7fad9658130b658790773 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sun, 10 Jan 2021 12:04:16 -0500 Subject: [PATCH] Cleanup incoming stream handler Cleaned up the incoming RTMP handler to close connections on error. The code aquireing the RTMP channel was also rewritten to be more idomatic and easier to follow. --- handlers.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/handlers.go b/handlers.go index e2c30dd..3c80f1a 100644 --- a/handlers.go +++ b/handlers.go @@ -364,37 +364,39 @@ func handlePublish(conn *rtmp.Conn) { if len(urlParts) > 2 { common.LogErrorln("Extra garbage after stream key") l.Unlock() + conn.Close() return } if len(urlParts) != 2 { common.LogErrorln("Missing stream key") l.Unlock() + conn.Close() return } if urlParts[1] != settings.GetStreamKey() { common.LogErrorln("Stream key is incorrect. Denying stream.") l.Unlock() + conn.Close() return //If key not match, deny stream } streamPath := urlParts[0] - ch := channels[streamPath] - if ch == nil { - ch = &Channel{} - ch.que = pubsub.NewQueue() - ch.que.WriteHeader(streams) - channels[streamPath] = ch - } else { - ch = nil - } - l.Unlock() - if ch == nil { - common.LogErrorln("Unable to start stream, channel is nil.") + ch, exists := channels[streamPath] + if exists { + common.LogErrorln("Stream already running. Denying publish.") + conn.Close() + l.Unlock() return } + ch = &Channel{} + ch.que = pubsub.NewQueue() + ch.que.WriteHeader(streams) + channels[streamPath] = ch + l.Unlock() + stats.startStream() common.LogInfoln("Stream started")