Skip to content

Commit

Permalink
Clean HASS Urls that come from UniFi Protect
Browse files Browse the repository at this point in the history
This automatically applies the recommended configuration for UniFi cameras.

Closes AlexxIT#81
  • Loading branch information
ViViDboarder committed Jan 9, 2024
1 parent ccec41a commit c8d4909
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion internal/hass/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import (
"encoding/json"
"net"
"net/http"
"regexp"
"strings"

"github.com/AlexxIT/go2rtc/internal/api"
"github.com/AlexxIT/go2rtc/internal/streams"
"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)
}
Expand All @@ -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)
Expand Down

0 comments on commit c8d4909

Please sign in to comment.