From 66225973ae469033ad58c8f3add481b7521418e3 Mon Sep 17 00:00:00 2001 From: Alex X Date: Sun, 18 Jan 2026 07:54:48 +0300 Subject: [PATCH] Code refactoring for onvif device discovery #1991 --- internal/onvif/onvif.go | 12 +++++------ pkg/onvif/helpers.go | 45 +++++++++++++++++------------------------ 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/internal/onvif/onvif.go b/internal/onvif/onvif.go index 6d9e2262..38ba6581 100644 --- a/internal/onvif/onvif.go +++ b/internal/onvif/onvif.go @@ -7,7 +7,6 @@ import ( "net/url" "os" "strconv" - "strings" "time" "github.com/AlexxIT/go2rtc/internal/api" @@ -186,12 +185,11 @@ func apiOnvif(w http.ResponseWriter, r *http.Request) { u.Path = "" } - var info string - info = strings.TrimSpace(device.Name + " " + device.Hardware) - if info == "" { - info = "ONVIF Device" - } - items = append(items, &api.Source{Name: u.Host, URL: u.String(), Info: info}) + items = append(items, &api.Source{ + Name: u.Host, + URL: u.String(), + Info: device.Name + " " + device.Hardware, + }) } } else { client, err := onvif.NewClient(src) diff --git a/pkg/onvif/helpers.go b/pkg/onvif/helpers.go index d4fa92f6..8fac9ac4 100644 --- a/pkg/onvif/helpers.go +++ b/pkg/onvif/helpers.go @@ -33,7 +33,7 @@ func UUID() string { 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) { conn, err := net.ListenUDP("udp4", nil) if err != nil { @@ -68,9 +68,7 @@ func DiscoveryStreamingDevices() ([]DiscoveryDevice, error) { return nil, err } - if err = conn.SetReadDeadline(time.Now().Add(time.Second * 3)); err != nil { - return nil, err - } + _ = conn.SetReadDeadline(time.Now().Add(5 * time.Second)) var devices []DiscoveryDevice @@ -88,42 +86,37 @@ func DiscoveryStreamingDevices() ([]DiscoveryDevice, error) { continue } - url := FindTagValue(b[:n], "XAddrs") - if url == "" { + device := DiscoveryDevice{ + URL: FindTagValue(b[:n], "XAddrs"), + } + + if device.URL == "" { continue } // fix some buggy cameras // http://0.0.0.0:8080/onvif/device_service - if strings.HasPrefix(url, "http://0.0.0.0") { - url = "http://" + addr.IP.String() + url[14:] + if s, ok := strings.CutPrefix(device.URL, "http://0.0.0.0"); ok { + device.URL = "http://" + addr.IP.String() + s } // try to find the camera name and model (hardware) scopes := FindTagValue(b[:n], "Scopes") - var name, hardware string - for _, scope := range strings.Split(scopes, " ") { - if strings.HasPrefix(scope, "onvif://www.onvif.org/name/") { - name = strings.TrimPrefix(scope, "onvif://www.onvif.org/name/") - } 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}) + device.Name = findScope(scopes, "onvif://www.onvif.org/name/") + device.Hardware = findScope(scopes, "onvif://www.onvif.org/hardware/") + + devices = append(devices, device) } 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 { if s == "" { return 0