From 237fbf23a12a84eec9d8aaf8e16484707abd3247 Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Sun, 18 Sep 2022 01:48:29 +0300 Subject: [PATCH] FIx backward support for RTSPtoWebRTC API --- cmd/hass/api.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/cmd/hass/api.go b/cmd/hass/api.go index 776a71da..61b0a8cc 100644 --- a/cmd/hass/api.go +++ b/cmd/hass/api.go @@ -7,6 +7,7 @@ import ( "github.com/AlexxIT/go2rtc/cmd/streams" "github.com/AlexxIT/go2rtc/cmd/webrtc" "net/http" + "net/url" "strings" ) @@ -84,6 +85,48 @@ func initAPI() { _, _ = w.Write([]byte(s)) } }) + + // api from RTSPtoWebRTC + api.HandleFunc("/stream", func(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + return + } + + str := r.FormValue("sdp64") + offer, err := base64.StdEncoding.DecodeString(str) + if err != nil { + return + } + + src := r.FormValue("url") + src, err = url.QueryUnescape(src) + if err != nil { + return + } + + stream := streams.Get(src) + if stream == nil { + if stream = rtspStream(src); stream != nil { + streams.New(src, stream) + } else { + stream = streams.New(src, src) + } + } + + str, err = webrtc.ExchangeSDP(stream, string(offer), r.UserAgent()) + if err != nil { + return + } + + v := struct { + Answer string `json:"sdp64"` + }{ + Answer: base64.StdEncoding.EncodeToString([]byte(str)), + } + + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(v) + }) } func rtspStream(url string) *streams.Stream {