Skip to content

Commit

Permalink
RSDK-9925: POC adding mDNS entries for discovered IP cameras.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgottlieb committed Feb 11, 2025
1 parent c81ce10 commit a7ba38d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions viamonvif/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"net/url"
"regexp"
"slices"
"strings"
"sync"

"github.com/edaniels/zeroconf"
"github.com/viam-modules/viamrtsp/viamonvif/device"
"github.com/viam-modules/viamrtsp/viamonvif/xsd/onvif"
"go.viam.com/rdk/logging"
Expand Down Expand Up @@ -179,6 +181,35 @@ func GetCameraInfo(dev OnvifDevice, xaddr *url.URL, creds device.Credentials, lo
return zero, fmt.Errorf("failed to get RTSP URLs: %w", err)
}

hostNameWithLocal := fmt.Sprintf("%s.local", resp.SerialNumber)

Check failure on line 184 in viamonvif/discovery.go

View workflow job for this annotation

GitHub Actions / quality-checks

fmt.Sprintf can be replaced with string concatenation (perfsprint)
for idx := range rtspURLs {
// We expect all of the `rtspURLs` to contain the `xaddr.Host` as a substring. But we'll
// warn in case that assumption is not always true.
if strings.Contains(rtspURLs[idx], xaddr.Host) {
rtspURLs[idx] = strings.Replace(rtspURLs[idx], xaddr.Host, hostNameWithLocal, 1)
} else {
logger.Warnf("RTSP URL did not contain expected hostname. URL: %v HostName: %v", rtspURLs[idx], xaddr.Host)
}
}

mdnsServer, err := zeroconf.RegisterProxy(
resp.SerialNumber, // Dan: As far as I can tell, just a name.
"_rtsp._tcp", // Dan: The mDNS "service" to register. Doesn't make a difference?
"local", // the domain
8080, // The service's port is ignored here

Check failure on line 199 in viamonvif/discovery.go

View workflow job for this annotation

GitHub Actions / quality-checks

Magic number: 8080, in <argument> detected (mnd)
resp.SerialNumber, // actual mDNS hostname, without the .local domain
[]string{xaddr.Host}, // ip to use
[]string{}, // txt fields, not needed
nil, // resolve this name for requests from all network interfaces
// RSDK-8205: logger.Desugar().Sugar() is necessary to massage a ZapCompatibleLogger into a
// *zap.SugaredLogger to match zeroconf function signatures.
logger.Desugar().Sugar(),
)
_ = mdnsServer
if err != nil {
logger.Warnf("Did not make hostname mapping for camera. Err:", err)
}

cameraInfo := CameraInfo{
Host: xaddr.Host,
RTSPURLs: rtspURLs,
Expand Down

0 comments on commit a7ba38d

Please sign in to comment.