diff --git a/cmd/api/api.go b/cmd/api/api.go index 4fe29e08..b86baf9d 100644 --- a/cmd/api/api.go +++ b/cmd/api/api.go @@ -9,8 +9,6 @@ import ( "github.com/rs/zerolog" "net" "net/http" - "os" - "strconv" ) func Init() { @@ -39,9 +37,7 @@ func Init() { HandleFunc("/api/frame.mp4", frameHandler) HandleFunc("/api/frame.raw", frameHandler) - HandleFunc("/api/stack", stackHandler) HandleFunc("/api/streams", streamsHandler) - HandleFunc("/api/exit", exitHandler) HandleFunc("/api/ws", apiWS) // ensure we can listen without errors @@ -99,12 +95,6 @@ func streamsHandler(w http.ResponseWriter, r *http.Request) { } } -func exitHandler(w http.ResponseWriter, r *http.Request) { - s := r.URL.Query().Get("code") - code, _ := strconv.Atoi(s) - os.Exit(code) -} - func apiWS(w http.ResponseWriter, r *http.Request) { ctx := new(Context) if err := ctx.Upgrade(w, r); err != nil { diff --git a/cmd/debug/debug.go b/cmd/debug/debug.go new file mode 100644 index 00000000..22746cf9 --- /dev/null +++ b/cmd/debug/debug.go @@ -0,0 +1,27 @@ +package debug + +import ( + "github.com/AlexxIT/go2rtc/cmd/api" + "github.com/AlexxIT/go2rtc/cmd/streams" + "github.com/AlexxIT/go2rtc/pkg/streamer" + "net/http" + "os" + "strconv" +) + +func Init() { + api.HandleFunc("/api/stack", stackHandler) + api.HandleFunc("/api/exit", exitHandler) + + streams.HandleFunc("null", nullHandler) +} + +func exitHandler(_ http.ResponseWriter, r *http.Request) { + s := r.URL.Query().Get("code") + code, _ := strconv.Atoi(s) + os.Exit(code) +} + +func nullHandler(string) (streamer.Producer, error) { + return nil, nil +} diff --git a/cmd/api/stack.go b/cmd/debug/stack.go similarity index 98% rename from cmd/api/stack.go rename to cmd/debug/stack.go index edb7ddf8..97b0ec98 100644 --- a/cmd/api/stack.go +++ b/cmd/debug/stack.go @@ -1,4 +1,4 @@ -package api +package debug import ( "bytes" diff --git a/cmd/streams/producer.go b/cmd/streams/producer.go index 111cd960..2e6ae580 100644 --- a/cmd/streams/producer.go +++ b/cmd/streams/producer.go @@ -29,7 +29,7 @@ func (p *Producer) GetMedias() []*streamer.Media { var err error p.element, err = GetProducer(p.url) - if err != nil { + if err != nil || p.element == nil { log.Error().Err(err).Str("url", p.url).Msg("[streams] probe producer") return nil } diff --git a/cmd/streams/stream.go b/cmd/streams/stream.go index 6c8936bf..ce1fcb12 100644 --- a/cmd/streams/stream.go +++ b/cmd/streams/stream.go @@ -2,6 +2,7 @@ package streams import ( "encoding/json" + "errors" "github.com/AlexxIT/go2rtc/pkg/streamer" ) @@ -78,7 +79,7 @@ func (s *Stream) AddConsumer(cons streamer.Consumer) (err error) { // can't match tracks for consumer if len(consumer.tracks) == 0 { - return nil + return errors.New("couldn't find the matching tracks") } s.consumers = append(s.consumers, consumer) diff --git a/cmd/webrtc/webrtc.go b/cmd/webrtc/webrtc.go index eccd1b13..ac072f7f 100644 --- a/cmd/webrtc/webrtc.go +++ b/cmd/webrtc/webrtc.go @@ -108,6 +108,7 @@ func offerHandler(ctx *api.Context, msg *streamer.Message) { // 2. AddConsumer, so we get new tracks if err = stream.AddConsumer(conn); err != nil { log.Warn().Err(err).Msg("[api.webrtc] add consumer") + _ = conn.Conn.Close() ctx.Error(err) return } diff --git a/main.go b/main.go index d9a03e2d..0e49b2b0 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "github.com/AlexxIT/go2rtc/cmd/api" "github.com/AlexxIT/go2rtc/cmd/app" + "github.com/AlexxIT/go2rtc/cmd/debug" "github.com/AlexxIT/go2rtc/cmd/exec" "github.com/AlexxIT/go2rtc/cmd/ffmpeg" "github.com/AlexxIT/go2rtc/cmd/hass" @@ -33,6 +34,7 @@ func main() { mse.Init() ngrok.Init() + debug.Init() sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)