diff --git a/cmd/api/api.go b/cmd/api/api.go index ccd88530..61f2d688 100644 --- a/cmd/api/api.go +++ b/cmd/api/api.go @@ -144,3 +144,23 @@ func exitHandler(w http.ResponseWriter, r *http.Request) { code, _ := strconv.Atoi(s) os.Exit(code) } + +type Stream struct { + Name string `json:"name"` + URL string `json:"url"` +} + +func ResponseStreams(w http.ResponseWriter, streams []Stream) { + if len(streams) == 0 { + http.Error(w, "no streams", http.StatusNotFound) + return + } + + var response struct { + Streams []Stream `json:"streams"` + } + response.Streams = streams + if err := json.NewEncoder(w).Encode(response); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} diff --git a/cmd/ffmpeg/device/devices.go b/cmd/ffmpeg/device/devices.go index 1a886f7e..33610c10 100644 --- a/cmd/ffmpeg/device/devices.go +++ b/cmd/ffmpeg/device/devices.go @@ -1,7 +1,6 @@ package device import ( - "encoding/json" "github.com/AlexxIT/go2rtc/cmd/api" "github.com/AlexxIT/go2rtc/cmd/app" "github.com/AlexxIT/go2rtc/pkg/core" @@ -72,12 +71,21 @@ func handle(w http.ResponseWriter, r *http.Request) { loadMedias() } - data, err := json.Marshal(medias) - if err != nil { - log.Error().Err(err).Msg("[api.ffmpeg]") - return - } - if _, err = w.Write(data); err != nil { - log.Error().Err(err).Msg("[api.ffmpeg]") + var items []api.Stream + var iv, ia int + + for _, media := range medias { + var source string + switch media.Kind { + case core.KindVideo: + source = "ffmpeg:device?video=" + strconv.Itoa(iv) + iv++ + case core.KindAudio: + source = "ffmpeg:device?audio=" + strconv.Itoa(ia) + ia++ + } + items = append(items, api.Stream{Name: media.ID, URL: source}) } + + api.ResponseStreams(w, items) } diff --git a/cmd/hass/hass.go b/cmd/hass/hass.go index 38c34aec..dc29b979 100644 --- a/cmd/hass/hass.go +++ b/cmd/hass/hass.go @@ -3,11 +3,13 @@ package hass import ( "encoding/json" "fmt" + "github.com/AlexxIT/go2rtc/cmd/api" "github.com/AlexxIT/go2rtc/cmd/app" "github.com/AlexxIT/go2rtc/cmd/roborock" "github.com/AlexxIT/go2rtc/cmd/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/rs/zerolog" + "net/http" "os" "path" ) @@ -39,6 +41,14 @@ func Init() { urls := map[string]string{} + api.HandleFunc("api/hass", func(w http.ResponseWriter, r *http.Request) { + var items []api.Stream + for name, url := range urls { + items = append(items, api.Stream{Name: name, URL: url}) + } + api.ResponseStreams(w, items) + }) + streams.HandleFunc("hass", func(url string) (core.Producer, error) { if hurl := urls[url[5:]]; hurl != "" { return streams.GetProducer(hurl) diff --git a/cmd/roborock/roborock.go b/cmd/roborock/roborock.go index 1dcc8355..7e719c8f 100644 --- a/cmd/roborock/roborock.go +++ b/cmd/roborock/roborock.go @@ -1,7 +1,6 @@ package roborock import ( - "encoding/json" "fmt" "github.com/AlexxIT/go2rtc/cmd/api" "github.com/AlexxIT/go2rtc/cmd/streams" @@ -85,17 +84,7 @@ func apiHandle(w http.ResponseWriter, r *http.Request) { return } - if len(devices) == 0 { - http.Error(w, "no devices in the account", http.StatusNotFound) - return - } - - var response struct { - Devices []struct { - Name string `json:"name"` - Source string `json:"source"` - } `json:"devices"` - } + var items []api.Stream for _, device := range devices { source := fmt.Sprintf( @@ -104,17 +93,8 @@ func apiHandle(w http.ResponseWriter, r *http.Request) { Auth.UserData.IoT.User, Auth.UserData.IoT.Pass, Auth.UserData.IoT.Domain, device.DID, device.Key, ) - - response.Devices = append(response.Devices, struct { - Name string `json:"name"` - Source string `json:"source"` - }{ - Name: device.Name, - Source: source, - }) + items = append(items, api.Stream{Name: device.Name, URL: source}) } - if err = json.NewEncoder(w).Encode(response); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } + api.ResponseStreams(w, items) } diff --git a/cmd/streams/streams.go b/cmd/streams/init.go similarity index 100% rename from cmd/streams/streams.go rename to cmd/streams/init.go diff --git a/www/add.html b/www/add.html new file mode 100644 index 00000000..f452fccd --- /dev/null +++ b/www/add.html @@ -0,0 +1,222 @@ + + + + Add Stream + + + + + + + + + + +
+
+ + + +
+
+ + + + +
+
+ + +
+ + + + + + + + + + + +
NameAddressModelCommands
+
+ + + + +
+
+
+ + + + +
+
+ + + +
+ +
+
+ + + + +
+ +
+
+ + + + diff --git a/www/devices.html b/www/devices.html deleted file mode 100644 index 59e84942..00000000 --- a/www/devices.html +++ /dev/null @@ -1,75 +0,0 @@ - - - - - - - - go2rtc - - - - - - - - - - - - - - -
KindName
- - - \ No newline at end of file diff --git a/www/homekit.html b/www/homekit.html deleted file mode 100644 index 7d9281c2..00000000 --- a/www/homekit.html +++ /dev/null @@ -1,111 +0,0 @@ - - - - - - - - go2rtc - - - - - -
- - -
- - - - - - - - - - - - -
NameAddressModelCommands
- - - \ No newline at end of file diff --git a/www/index.html b/www/index.html index 86497bae..9746f752 100644 --- a/www/index.html +++ b/www/index.html @@ -63,10 +63,6 @@
-
- - add -
@@ -93,14 +89,6 @@ 'delete', ]; - document.querySelector("#add") - .addEventListener("click", () => { - const src = document.querySelector("#src"); - const url = new URL("api/streams", location.href); - url.searchParams.set("src", src.value); - fetch(url, {method: "PUT"}).then(reload); - }); - document.querySelector(".controls > button") .addEventListener("click", () => { const url = new URL("stream.html", location.href); diff --git a/www/main.js b/www/main.js index 297fd6df..9ec2fecf 100644 --- a/www/main.js +++ b/www/main.js @@ -45,8 +45,7 @@ nav li {