From 6c23082a5308f6e24f9f23f0cbfbaa606256d572 Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Tue, 9 Jan 2024 15:31:23 -0800 Subject: [PATCH] Clean HASS Urls that come from UniFi Protect This automatically applies the recommended configuration for UniFi cameras. Closes #81 --- internal/hass/api.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/hass/api.go b/internal/hass/api.go index e3de23b3e..964ef88fc 100644 --- a/internal/hass/api.go +++ b/internal/hass/api.go @@ -5,6 +5,7 @@ import ( "encoding/json" "net" "net/http" + "regexp" "strings" "github.com/AlexxIT/go2rtc/internal/api" @@ -12,6 +13,10 @@ import ( "github.com/AlexxIT/go2rtc/internal/webrtc" ) +// UniFi streams will start with rtsps and end with ?enableSrtp +// this is not compatible with go2rtc and requires some manipulation +var UnifiMatch = regexp.MustCompile(`rtsps://.+[?]enableSrtp`) + func apiOK(w http.ResponseWriter, r *http.Request) { api.Response(w, `{"status":1,"payload":{}}`, api.MimeJSON) } @@ -30,7 +35,15 @@ func apiStream(w http.ResponseWriter, r *http.Request) { // 1. link to go2rtc stream: rtsp://...:8554/{stream_name} // 2. static link to Hass camera // 3. dynamic link to Hass camera - if streams.Patch(v.Name, v.Channels.First.Url) != nil { + + // Mutate incompatible UniFi URLs + streamURL := v.Channels.First.Url + if UnifiMatch.MatchString(streamURL) { + streamURL = strings.Replace(streamURL, "rtsps", "rtspx", 1) + streamURL = strings.Replace(streamURL, "?enableSrtp", "", 1) + log.Debug().Msgf("Cleaning UniFi stream URL: %v", streamURL) + } + if streams.Patch(v.Name, streamURL) != nil { apiOK(w, r) } else { http.Error(w, "", http.StatusBadRequest)