Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions internal/homekit/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"

"github.com/AlexxIT/go2rtc/internal/api"
Expand All @@ -23,7 +22,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
return
}

urls := findHomeKitURLs()
urls := streams.FindPrefixURLs("homekit")
for id, u := range urls {
deviceID := u.Query().Get("device_id")
for _, source := range sources {
Expand Down Expand Up @@ -112,7 +111,7 @@ func apiUnpair(id string) error {
return errors.New(api.StreamNotFound)
}

rawURL := findHomeKitURL(stream.Sources())
rawURL := streams.FindPrefixURL("homekit", stream.Sources())
if rawURL == "" {
return errors.New("not homekit source")
}
Expand All @@ -125,15 +124,3 @@ func apiUnpair(id string) error {

return app.PatchConfig([]string{"streams", id}, nil)
}

func findHomeKitURLs() map[string]*url.URL {
urls := map[string]*url.URL{}
for name, sources := range streams.GetAllSources() {
if rawURL := findHomeKitURL(sources); rawURL != "" {
if u, err := url.Parse(rawURL); err == nil {
urls[name] = u
}
}
}
return urls
}
22 changes: 1 addition & 21 deletions internal/homekit/homekit.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func Init() {
Handler: homekit.ServerHandler(srv),
}

if url := findHomeKitURL(stream.Sources()); url != "" {
if url := streams.FindPrefixURL("homekit", stream.Sources()); url != "" {
// 1. Act as transparent proxy for HomeKit camera
dial := func() (net.Conn, error) {
client, err := homekit.Dial(url, srtp.Server)
Expand Down Expand Up @@ -190,26 +190,6 @@ func hapHandler(w http.ResponseWriter, r *http.Request) {
}
}

func findHomeKitURL(sources []string) string {
if len(sources) == 0 {
return ""
}

url := sources[0]
if strings.HasPrefix(url, "homekit") {
return url
}

if strings.HasPrefix(url, "hass") {
location, _ := streams.Location(url)
if strings.HasPrefix(location, "homekit") {
return url
}
}

return ""
}

func parseBitrate(s string) int {
n := len(s)
if n == 0 {
Expand Down
8 changes: 8 additions & 0 deletions internal/streams/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/AlexxIT/go2rtc/internal/api"
"github.com/AlexxIT/go2rtc/internal/app"
"github.com/AlexxIT/go2rtc/pkg/hap"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this dependency in this module. The streams module does not need to know about the existence of any particular source.

"github.com/AlexxIT/go2rtc/pkg/probe"
)

Expand Down Expand Up @@ -94,6 +95,13 @@ func apiStreams(w http.ResponseWriter, r *http.Request) {
}

case "DELETE":
stream := Get(src)
if stream != nil {
if rawURL := FindPrefixURL("homekit", stream.Sources()); rawURL != "" {
_ = hap.Unpair(rawURL)
}
}

delete(streams, src)

if err := app.PatchConfig([]string{"streams", src}, nil); err != nil {
Expand Down
34 changes: 34 additions & 0 deletions internal/streams/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,37 @@ func ParseQuery(s string) url.Values {
}
return params
}

func FindPrefixURL(prefix string, sources []string) string {
if len(sources) == 0 {
return ""
}

url := sources[0]
if strings.HasPrefix(url, prefix) {
return url
}

if prefix == "homekit" {
if strings.HasPrefix(url, "hass") {
location, _ := Location(url)
if strings.HasPrefix(location, prefix) {
return url
}
}
}

return ""
}

func FindPrefixURLs(prefix string) map[string]*url.URL {
urls := map[string]*url.URL{}
for name, sources := range GetAllSources() {
if rawURL := FindPrefixURL(prefix, sources); rawURL != "" {
if u, err := url.Parse(rawURL); err == nil {
urls[name] = u
}
}
}
return urls
}