Code refactoring for onvif device discovery #1991

This commit is contained in:
Alex X
2026-01-18 07:54:48 +03:00
parent bbb9466845
commit 66225973ae
2 changed files with 24 additions and 33 deletions
+5 -7
View File
@@ -7,7 +7,6 @@ import (
"net/url" "net/url"
"os" "os"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api"
@@ -186,12 +185,11 @@ func apiOnvif(w http.ResponseWriter, r *http.Request) {
u.Path = "" u.Path = ""
} }
var info string items = append(items, &api.Source{
info = strings.TrimSpace(device.Name + " " + device.Hardware) Name: u.Host,
if info == "" { URL: u.String(),
info = "ONVIF Device" Info: device.Name + " " + device.Hardware,
} })
items = append(items, &api.Source{Name: u.Host, URL: u.String(), Info: info})
} }
} else { } else {
client, err := onvif.NewClient(src) client, err := onvif.NewClient(src)
+19 -26
View File
@@ -33,7 +33,7 @@ func UUID() string {
return s[:8] + "-" + s[8:12] + "-" + s[12:16] + "-" + s[16:20] + "-" + s[20:] return s[:8] + "-" + s[8:12] + "-" + s[12:16] + "-" + s[16:20] + "-" + s[20:]
} }
// return list of tuple (onvif_url, name, hardware) // DiscoveryStreamingDevices return list of tuple (onvif_url, name, hardware)
func DiscoveryStreamingDevices() ([]DiscoveryDevice, error) { func DiscoveryStreamingDevices() ([]DiscoveryDevice, error) {
conn, err := net.ListenUDP("udp4", nil) conn, err := net.ListenUDP("udp4", nil)
if err != nil { if err != nil {
@@ -68,9 +68,7 @@ func DiscoveryStreamingDevices() ([]DiscoveryDevice, error) {
return nil, err return nil, err
} }
if err = conn.SetReadDeadline(time.Now().Add(time.Second * 3)); err != nil { _ = conn.SetReadDeadline(time.Now().Add(5 * time.Second))
return nil, err
}
var devices []DiscoveryDevice var devices []DiscoveryDevice
@@ -88,42 +86,37 @@ func DiscoveryStreamingDevices() ([]DiscoveryDevice, error) {
continue continue
} }
url := FindTagValue(b[:n], "XAddrs") device := DiscoveryDevice{
if url == "" { URL: FindTagValue(b[:n], "XAddrs"),
}
if device.URL == "" {
continue continue
} }
// fix some buggy cameras // fix some buggy cameras
// <wsdd:XAddrs>http://0.0.0.0:8080/onvif/device_service</wsdd:XAddrs> // <wsdd:XAddrs>http://0.0.0.0:8080/onvif/device_service</wsdd:XAddrs>
if strings.HasPrefix(url, "http://0.0.0.0") { if s, ok := strings.CutPrefix(device.URL, "http://0.0.0.0"); ok {
url = "http://" + addr.IP.String() + url[14:] device.URL = "http://" + addr.IP.String() + s
} }
// try to find the camera name and model (hardware) // try to find the camera name and model (hardware)
scopes := FindTagValue(b[:n], "Scopes") scopes := FindTagValue(b[:n], "Scopes")
var name, hardware string device.Name = findScope(scopes, "onvif://www.onvif.org/name/")
for _, scope := range strings.Split(scopes, " ") { device.Hardware = findScope(scopes, "onvif://www.onvif.org/hardware/")
if strings.HasPrefix(scope, "onvif://www.onvif.org/name/") {
name = strings.TrimPrefix(scope, "onvif://www.onvif.org/name/") devices = append(devices, device)
} else if strings.HasPrefix(scope, "onvif://www.onvif.org/hardware/") {
hardware = strings.TrimPrefix(scope, "onvif://www.onvif.org/hardware/")
}
}
// Some cameras has the hardware copied into the name field or vice versa
// this is to avoid duplication
if name != "" && hardware != "" {
if strings.Contains(hardware, name) {
name = ""
} else if strings.Contains(name, hardware) {
hardware = ""
}
}
devices = append(devices, DiscoveryDevice{URL: url, Name: name, Hardware: hardware})
} }
return devices, nil return devices, nil
} }
func findScope(s, prefix string) string {
s = core.Between(s, prefix, " ")
s, _ = url.QueryUnescape(s)
return s
}
func atoi(s string) int { func atoi(s string) int {
if s == "" { if s == "" {
return 0 return 0