Change response sources API

This commit is contained in:
Alexey Khit
2023-09-01 22:27:33 +03:00
parent 22787b979d
commit 0621b82aff
14 changed files with 29 additions and 29 deletions
+2 -2
View File
@@ -220,14 +220,14 @@ type Source struct {
Location string `json:"location,omitempty"` Location string `json:"location,omitempty"`
} }
func ResponseSources(w http.ResponseWriter, sources []Source) { func ResponseSources(w http.ResponseWriter, sources []*Source) {
if len(sources) == 0 { if len(sources) == 0 {
http.Error(w, "no sources", http.StatusNotFound) http.Error(w, "no sources", http.StatusNotFound)
return return
} }
var response = struct { var response = struct {
Sources []Source `json:"sources"` Sources []*Source `json:"sources"`
}{ }{
Sources: sources, Sources: sources,
} }
+3 -3
View File
@@ -48,7 +48,7 @@ func apiDvrip(w http.ResponseWriter, r *http.Request) {
api.ResponseSources(w, items) api.ResponseSources(w, items)
} }
func discover() ([]api.Source, error) { func discover() ([]*api.Source, error) {
addr := &net.UDPAddr{ addr := &net.UDPAddr{
Port: Port, Port: Port,
IP: net.IP{239, 255, 255, 250}, IP: net.IP{239, 255, 255, 250},
@@ -63,7 +63,7 @@ func discover() ([]api.Source, error) {
go sendBroadcasts(conn) go sendBroadcasts(conn)
var items []api.Source var items []*api.Source
for _, info := range getResponses(conn) { for _, info := range getResponses(conn) {
if info.HostIP == "" || info.HostName == "" { if info.HostIP == "" || info.HostName == "" {
@@ -75,7 +75,7 @@ func discover() ([]api.Source, error) {
continue continue
} }
items = append(items, api.Source{ items = append(items, &api.Source{
Name: info.HostName, Name: info.HostName,
URL: "dvrip://user:pass@" + host + "?channel=0&subtype=0", URL: "dvrip://user:pass@" + host + "?channel=0&subtype=0",
}) })
+1 -1
View File
@@ -79,7 +79,7 @@ func initDevices() {
audios = append(audios, name) audios = append(audios, name)
} }
streams = append(streams, api.Source{ streams = append(streams, &api.Source{
Name: name, URL: "ffmpeg:device?" + kind + "=" + name, Name: name, URL: "ffmpeg:device?" + kind + "=" + name,
}) })
} }
+2 -2
View File
@@ -70,7 +70,7 @@ func initDevices() {
m := re.FindAllStringSubmatch(string(b), -1) m := re.FindAllStringSubmatch(string(b), -1)
for _, i := range m { for _, i := range m {
size, _, _ := strings.Cut(i[4], " ") size, _, _ := strings.Cut(i[4], " ")
stream := api.Source{ stream := &api.Source{
Name: i[3] + " | " + i[4], Name: i[3] + " | " + i[4],
URL: "ffmpeg:device?video=" + name + "&input_format=" + i[2] + "&video_size=" + size, URL: "ffmpeg:device?video=" + name + "&input_format=" + i[2] + "&video_size=" + size,
} }
@@ -86,7 +86,7 @@ func initDevices() {
err = exec.Command(Bin, "-f", "alsa", "-i", "default", "-t", "1", "-f", "null", "-").Run() err = exec.Command(Bin, "-f", "alsa", "-i", "default", "-t", "1", "-f", "null", "-").Run()
if err == nil { if err == nil {
stream := api.Source{ stream := &api.Source{
Name: "ALSA default", Name: "ALSA default",
URL: "ffmpeg:device?audio=default&channels=1&sample_rate=16000&#audio=opus", URL: "ffmpeg:device?audio=default&channels=1&sample_rate=16000&#audio=opus",
} }
+1 -1
View File
@@ -80,7 +80,7 @@ func initDevices() {
name := m[1] name := m[1]
kind := m[2] kind := m[2]
stream := api.Source{ stream := &api.Source{
Name: name, URL: "ffmpeg:device?" + kind + "=" + name, Name: name, URL: "ffmpeg:device?" + kind + "=" + name,
} }
+1 -1
View File
@@ -40,7 +40,7 @@ func GetInput(src string) (string, error) {
var Bin string var Bin string
var videos, audios []string var videos, audios []string
var streams []api.Source var streams []*api.Source
var runonce sync.Once var runonce sync.Once
func apiDevices(w http.ResponseWriter, r *http.Request) { func apiDevices(w http.ResponseWriter, r *http.Request) {
+2 -2
View File
@@ -7,8 +7,8 @@ import (
const ProbeVideoToolboxH264 = "-f lavfi -i testsrc2=size=svga -t 1 -c h264_videotoolbox -f null -" const ProbeVideoToolboxH264 = "-f lavfi -i testsrc2=size=svga -t 1 -c h264_videotoolbox -f null -"
const ProbeVideoToolboxH265 = "-f lavfi -i testsrc2=size=svga -t 1 -c hevc_videotoolbox -f null -" const ProbeVideoToolboxH265 = "-f lavfi -i testsrc2=size=svga -t 1 -c hevc_videotoolbox -f null -"
func ProbeAll(bin string) []api.Source { func ProbeAll(bin string) []*api.Source {
return []api.Source{ return []*api.Source{
{ {
Name: runToString(bin, ProbeVideoToolboxH264), Name: runToString(bin, ProbeVideoToolboxH264),
URL: "ffmpeg:...#video=h264#hardware=" + EngineVideoToolbox, URL: "ffmpeg:...#video=h264#hardware=" + EngineVideoToolbox,
+3 -3
View File
@@ -14,9 +14,9 @@ const ProbeVAAPIJPEG = "-init_hw_device vaapi -f lavfi -i testsrc2 -t 1 -vf form
const ProbeCUDAH264 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c h264_nvenc -f null -" const ProbeCUDAH264 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c h264_nvenc -f null -"
const ProbeCUDAH265 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c hevc_nvenc -f null -" const ProbeCUDAH265 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c hevc_nvenc -f null -"
func ProbeAll(bin string) []api.Source { func ProbeAll(bin string) []*api.Source {
if runtime.GOARCH == "arm64" || runtime.GOARCH == "arm" { if runtime.GOARCH == "arm64" || runtime.GOARCH == "arm" {
return []api.Source{ return []*api.Source{
{ {
Name: runToString(bin, ProbeV4L2M2MH264), Name: runToString(bin, ProbeV4L2M2MH264),
URL: "ffmpeg:...#video=h264#hardware=" + EngineV4L2M2M, URL: "ffmpeg:...#video=h264#hardware=" + EngineV4L2M2M,
@@ -28,7 +28,7 @@ func ProbeAll(bin string) []api.Source {
} }
} }
return []api.Source{ return []*api.Source{
{ {
Name: runToString(bin, ProbeVAAPIH264), Name: runToString(bin, ProbeVAAPIH264),
URL: "ffmpeg:...#video=h264#hardware=" + EngineVAAPI, URL: "ffmpeg:...#video=h264#hardware=" + EngineVAAPI,
+2 -2
View File
@@ -8,8 +8,8 @@ const ProbeDXVA2JPEG = "-init_hw_device dxva2 -f lavfi -i testsrc2 -t 1 -c mjpeg
const ProbeCUDAH264 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c h264_nvenc -f null -" const ProbeCUDAH264 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c h264_nvenc -f null -"
const ProbeCUDAH265 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c hevc_nvenc -f null -" const ProbeCUDAH265 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c hevc_nvenc -f null -"
func ProbeAll(bin string) []api.Source { func ProbeAll(bin string) []*api.Source {
return []api.Source{ return []*api.Source{
{ {
Name: runToString(bin, ProbeDXVA2H264), Name: runToString(bin, ProbeDXVA2H264),
URL: "ffmpeg:...#video=h264#hardware=" + EngineDXVA2, URL: "ffmpeg:...#video=h264#hardware=" + EngineDXVA2,
+2 -2
View File
@@ -75,9 +75,9 @@ func Init() {
} }
}) })
var items []api.Source var items []*api.Source
for name, url := range entities { for name, url := range entities {
items = append(items, api.Source{ items = append(items, &api.Source{
Name: name, URL: "hass:" + name, Location: url, Name: name, URL: "hass:" + name, Location: url,
}) })
} }
+2 -2
View File
@@ -42,12 +42,12 @@ func apiNest(w http.ResponseWriter, r *http.Request) {
return return
} }
var items []api.Source var items []*api.Source
for name, deviceID := range devices { for name, deviceID := range devices {
query.Set("device_id", deviceID) query.Set("device_id", deviceID)
items = append(items, api.Source{ items = append(items, &api.Source{
Name: name, URL: "nest:?" + query.Encode(), Name: name, URL: "nest:?" + query.Encode(),
}) })
} }
+4 -4
View File
@@ -122,7 +122,7 @@ func onvifDeviceService(w http.ResponseWriter, r *http.Request) {
func apiOnvif(w http.ResponseWriter, r *http.Request) { func apiOnvif(w http.ResponseWriter, r *http.Request) {
src := r.URL.Query().Get("src") src := r.URL.Query().Get("src")
var items []api.Source var items []*api.Source
if src == "" { if src == "" {
urls, err := onvif.DiscoveryStreamingURLs() urls, err := onvif.DiscoveryStreamingURLs()
@@ -150,7 +150,7 @@ func apiOnvif(w http.ResponseWriter, r *http.Request) {
u.Path = "" u.Path = ""
} }
items = append(items, api.Source{Name: u.Host, URL: u.String()}) items = append(items, &api.Source{Name: u.Host, URL: u.String()})
} }
} else { } else {
client, err := onvif.NewClient(src) client, err := onvif.NewClient(src)
@@ -177,14 +177,14 @@ func apiOnvif(w http.ResponseWriter, r *http.Request) {
} }
for i, token := range tokens { for i, token := range tokens {
items = append(items, api.Source{ items = append(items, &api.Source{
Name: name + " stream" + strconv.Itoa(i), Name: name + " stream" + strconv.Itoa(i),
URL: src + "?subtype=" + token, URL: src + "?subtype=" + token,
}) })
} }
if len(tokens) > 0 && client.HasSnapshots() { if len(tokens) > 0 && client.HasSnapshots() {
items = append(items, api.Source{ items = append(items, &api.Source{
Name: name + " snapshot", Name: name + " snapshot",
URL: src + "?subtype=" + tokens[0] + "&snapshot", URL: src + "?subtype=" + tokens[0] + "&snapshot",
}) })
+2 -2
View File
@@ -85,7 +85,7 @@ func apiHandle(w http.ResponseWriter, r *http.Request) {
return return
} }
var items []api.Source var items []*api.Source
for _, device := range devices { for _, device := range devices {
source := fmt.Sprintf( source := fmt.Sprintf(
@@ -94,7 +94,7 @@ func apiHandle(w http.ResponseWriter, r *http.Request) {
Auth.UserData.IoT.User, Auth.UserData.IoT.Pass, Auth.UserData.IoT.Domain, Auth.UserData.IoT.User, Auth.UserData.IoT.Pass, Auth.UserData.IoT.Domain,
device.DID, device.Key, device.DID, device.Key,
) )
items = append(items, api.Source{Name: device.Name, URL: source}) items = append(items, &api.Source{Name: device.Name, URL: source})
} }
api.ResponseSources(w, items) api.ResponseSources(w, items)
+2 -2
View File
@@ -111,11 +111,11 @@ func apiHandle(w http.ResponseWriter, r *http.Request) {
} }
} else { } else {
// response all shares // response all shares
var items []api.Source var items []*api.Source
for src, share := range shares { for src, share := range shares {
pwd := srv.GetSharePwd(share) pwd := srv.GetSharePwd(share)
source := fmt.Sprintf("webtorrent:?share=%s&pwd=%s", share, pwd) source := fmt.Sprintf("webtorrent:?share=%s&pwd=%s", share, pwd)
items = append(items, api.Source{ID: src, URL: source}) items = append(items, &api.Source{ID: src, URL: source})
} }
api.ResponseSources(w, items) api.ResponseSources(w, items)
} }