From b4990b1e90e61e2e4312037c25dfa75d025755df Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Fri, 2 Sep 2022 17:57:56 +0300 Subject: [PATCH] Fix support RTSPtoWebRTC API --- cmd/hass/hass.go | 28 +++++++++++++++++++--------- cmd/streams/streams.go | 21 +++++++++++++-------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/cmd/hass/hass.go b/cmd/hass/hass.go index e8f70605..62a6e852 100644 --- a/cmd/hass/hass.go +++ b/cmd/hass/hass.go @@ -6,12 +6,12 @@ import ( "fmt" "github.com/AlexxIT/go2rtc/cmd/api" "github.com/AlexxIT/go2rtc/cmd/app" - "github.com/AlexxIT/go2rtc/cmd/rtsp" "github.com/AlexxIT/go2rtc/cmd/streams" "github.com/AlexxIT/go2rtc/cmd/webrtc" "github.com/AlexxIT/go2rtc/pkg/streamer" "github.com/rs/zerolog" "net/http" + "net/url" "os" "path" "strings" @@ -90,7 +90,13 @@ func handler(w http.ResponseWriter, r *http.Request) { return } - url := r.FormValue("url") + src := r.FormValue("url") + src, err := url.QueryUnescape(src) + if err != nil { + log.Error().Err(err).Msg("[api.hass] query unescape") + return + } + str := r.FormValue("sdp64") offer, err := base64.StdEncoding.DecodeString(str) @@ -99,16 +105,20 @@ func handler(w http.ResponseWriter, r *http.Request) { return } - // TODO: fixme - if strings.HasPrefix(url, "rtsp://") { - port := ":" + rtsp.Port + "/" - i := strings.Index(url, port) - if i > 0 { - url = url[i+len(port):] + // check if stream links to our rtsp server + if strings.HasPrefix(src, "rtsp://") { + i := strings.IndexByte(src[7:], '/') + if i > 0 && streams.Has(src[8+i:]) { + src = src[8+i:] } } - stream := streams.Get(url) + stream := streams.Get(src) + if stream == nil { + log.Error().Str("url", src).Msg("[api.hass] unsupported source") + return + } + str, err = webrtc.ExchangeSDP(stream, string(offer), r.UserAgent()) if err != nil { log.Error().Err(err).Msg("[api.hass] exchange SDP") diff --git a/cmd/streams/streams.go b/cmd/streams/streams.go index fb3d151e..65f39455 100644 --- a/cmd/streams/streams.go +++ b/cmd/streams/streams.go @@ -24,19 +24,24 @@ func Init() { } } -func Get(name string) *Stream { - if stream, ok := streams[name]; ok { +func Get(src string) *Stream { + if stream, ok := streams[src]; ok { return stream + } - if HasProducer(name) { - log.Info().Str("url", name).Msg("[streams] create new stream") - stream := NewStream(name) - streams[name] = stream - return stream + if !HasProducer(src) { + return nil } - return nil + log.Info().Str("url", src).Msg("[streams] create new stream") + stream := NewStream(src) + streams[src] = stream + return stream +} + +func Has(src string) bool { + return streams[src] != nil } func New(name string, source interface{}) {