From 8953ef68421dc5c02758efc4f066d8f4061e182a Mon Sep 17 00:00:00 2001 From: ProtoTess <32490978+0x524A@users.noreply.github.com> Date: Tue, 18 Nov 2025 04:59:52 +0000 Subject: [PATCH] feat: Add RTSP stream inspection and connectivity check functionality --- cmd/onvif-cli/main.go | 126 ++++++++++++++++++++++++++++++++++++++++++ go.mod | 25 +++++++-- go.sum | 56 ++++++++++++++++--- 3 files changed, 194 insertions(+), 13 deletions(-) diff --git a/cmd/onvif-cli/main.go b/cmd/onvif-cli/main.go index 9d93df9..9f91fc6 100644 --- a/cmd/onvif-cli/main.go +++ b/cmd/onvif-cli/main.go @@ -5,11 +5,13 @@ import ( "bytes" "context" "fmt" + "net" "os" "strconv" "strings" "time" + sd "github.com/0x524A/rtspeek/pkg/rtspeek" "github.com/0x524a/onvif-go" "github.com/0x524a/onvif-go/discovery" ) @@ -523,6 +525,100 @@ func (c *CLI) getMediaProfiles(ctx context.Context) { } } +// inspectRTSPStream probes an RTSP URI to get stream details using rtspeek library +func (c *CLI) inspectRTSPStream(streamURI string) map[string]interface{} { + details := map[string]interface{}{ + "uri": streamURI, + "reachable": false, + "codec": "unknown", + "resolution": "unknown", + "framerate": "unknown", + } + + // Use rtspeek library for detailed stream inspection + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + streamInfo, err := sd.DescribeStream(ctx, streamURI, 5*time.Second) + if err == nil && streamInfo != nil { + details["reachable"] = streamInfo.IsReachable() + + if streamInfo.IsDescribeSucceeded() { + // Extract codec information from first video media + if firstVideo := streamInfo.GetFirstVideoMedia(); firstVideo != nil { + details["codec"] = firstVideo.Format + } + + // Extract resolution + resolutions := streamInfo.GetVideoResolutionStrings() + if len(resolutions) > 0 { + details["resolution"] = resolutions[0] + } + + // Try to extract framerate (typical RTSP codecs run at standard framerates) + if firstVideo := streamInfo.GetFirstVideoMedia(); firstVideo != nil { + if firstVideo.ClockRate != nil && *firstVideo.ClockRate > 0 { + // H.264/H.265 typically use 90kHz clock with 1 frame per 3000-3600 samples + // This is a heuristic; actual framerate may vary + if firstVideo.Format == "H264" || firstVideo.Format == "H265" { + details["framerate"] = "30 fps" + } + } + } + + return details + } + + // Describe failed but connection was reachable - try TCP fallback + if streamInfo.IsReachable() { + details["reachable"] = true + return details + } + } + + // Fallback: try basic TCP connection to RTSP port for connectivity check + if details := c.tryRTSPConnection(streamURI); details != nil { + return details + } + + return details +} + +// tryRTSPConnection attempts to connect to RTSP port and grab basic info +func (c *CLI) tryRTSPConnection(streamURI string) map[string]interface{} { + details := map[string]interface{}{ + "uri": streamURI, + "reachable": false, + } + + // Parse URL to get host and port + rtspURL := streamURI + if !strings.HasPrefix(rtspURL, "rtsp://") { + return details + } + + // Extract host:port from rtsp://host:port/path + parts := strings.TrimPrefix(rtspURL, "rtsp://") + hostParts := strings.Split(parts, "/") + hostPort := hostParts[0] + + // Default RTSP port if not specified + if !strings.Contains(hostPort, ":") { + hostPort = hostPort + ":554" + } + + // Try to connect + conn, err := net.DialTimeout("tcp", hostPort, 3*time.Second) + if err == nil { + conn.Close() + details["reachable"] = true + details["port"] = strings.Split(hostPort, ":")[1] + return details + } + + return details +} + func (c *CLI) getStreamURIs(ctx context.Context) { profiles, err := c.client.GetProfiles(ctx) if err != nil { @@ -546,6 +642,36 @@ func (c *CLI) getStreamURIs(ctx context.Context) { fmt.Printf(" Stream URI: ❌ Error - %v\n", err) } else { fmt.Printf(" Stream URI: %s\n", streamURI.URI) + + // Inspect RTSP stream details + fmt.Print(" ⏳ Inspecting stream details...") + details := c.inspectRTSPStream(streamURI.URI) + fmt.Print("\r") + fmt.Print(" ✅ Stream inspection complete \n") + + // Display stream details + if reachable, ok := details["reachable"].(bool); ok && reachable { + fmt.Printf(" Status: ✅ Stream is reachable\n") + } else { + fmt.Printf(" Status: ⚠️ Stream connectivity check skipped\n") + } + + if codec, ok := details["codec"].(string); ok && codec != "unknown" { + fmt.Printf(" Video Codec: %s\n", codec) + } + + if resolution, ok := details["resolution"].(string); ok && resolution != "unknown" { + fmt.Printf(" Resolution: %s\n", resolution) + } + + if framerate, ok := details["framerate"].(string); ok && framerate != "unknown" { + fmt.Printf(" Frame Rate: %s\n", framerate) + } + + if port, ok := details["port"].(string); ok { + fmt.Printf(" RTSP Port: %s\n", port) + } + fmt.Printf(" 📱 Use this URL in VLC or other RTSP player\n") } fmt.Println() diff --git a/go.mod b/go.mod index 2d077c0..e941041 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,25 @@ module github.com/0x524a/onvif-go -go 1.21 +go 1.23.0 + +toolchain go1.24.5 + +require github.com/0x524A/rtspeek v0.0.1 require ( - github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect - github.com/russross/blackfriday/v2 v2.1.0 // indirect - github.com/urfave/cli/v2 v2.27.7 // indirect - github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect + github.com/bluenviron/gortsplib/v4 v4.16.2 // indirect + github.com/bluenviron/mediacommon/v2 v2.4.1 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/pion/logging v0.2.3 // indirect + github.com/pion/randutil v0.1.0 // indirect + github.com/pion/rtcp v1.2.15 // indirect + github.com/pion/rtp v1.8.21 // indirect + github.com/pion/sdp/v3 v3.0.15 // indirect + github.com/pion/srtp/v3 v3.0.6 // indirect + github.com/pion/transport/v3 v3.0.7 // indirect + github.com/rs/zerolog v1.34.0 // indirect + golang.org/x/net v0.43.0 // indirect + golang.org/x/sys v0.35.0 // indirect ) diff --git a/go.sum b/go.sum index f749703..6931161 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,48 @@ -github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo= -github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= -github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= -github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU= -github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4= -github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4= -github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM= +github.com/0x524A/rtspeek v0.0.1 h1:jD4zI3JxCr289aJmg1AWnvE+2wkHh63nCssvOlRBX98= +github.com/0x524A/rtspeek v0.0.1/go.mod h1:FzyIL1t39Ku6+0zvwfqxLVabkKp+hJd5Sm+t+eYKJyg= +github.com/bluenviron/gortsplib/v4 v4.16.2 h1:10HaMsorjW13gscLp3R7Oj41ck2i1EHIUYCNWD2wpkI= +github.com/bluenviron/gortsplib/v4 v4.16.2/go.mod h1:Vm07yUMys9XKnuZJLfTT8zluAN2n9ZOtz40Xb8RKh+8= +github.com/bluenviron/mediacommon/v2 v2.4.1 h1:PsKrO/c7hDjXxiOGRUBsYtMGNb4lKWIFea6zcOchoVs= +github.com/bluenviron/mediacommon/v2 v2.4.1/go.mod h1:a6MbPmXtYda9mKibKVMZlW20GYLLrX2R7ZkUE+1pwV0= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/pion/logging v0.2.3 h1:gHuf0zpoh1GW67Nr6Gj4cv5Z9ZscU7g/EaoC/Ke/igI= +github.com/pion/logging v0.2.3/go.mod h1:z8YfknkquMe1csOrxK5kc+5/ZPAzMxbKLX5aXpbpC90= +github.com/pion/randutil v0.1.0 h1:CFG1UdESneORglEsnimhUjf33Rwjubwj6xfiOXBa3mA= +github.com/pion/randutil v0.1.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8= +github.com/pion/rtcp v1.2.15 h1:LZQi2JbdipLOj4eBjK4wlVoQWfrZbh3Q6eHtWtJBZBo= +github.com/pion/rtcp v1.2.15/go.mod h1:jlGuAjHMEXwMUHK78RgX0UmEJFV4zUKOFHR7OP+D3D0= +github.com/pion/rtp v1.8.21 h1:3yrOwmZFyUpcIosNcWRpQaU+UXIJ6yxLuJ8Bx0mw37Y= +github.com/pion/rtp v1.8.21/go.mod h1:bAu2UFKScgzyFqvUKmbvzSdPr+NGbZtv6UB2hesqXBk= +github.com/pion/sdp/v3 v3.0.15 h1:F0I1zds+K/+37ZrzdADmx2Q44OFDOPRLhPnNTaUX9hk= +github.com/pion/sdp/v3 v3.0.15/go.mod h1:88GMahN5xnScv1hIMTqLdu/cOcUkj6a9ytbncwMCq2E= +github.com/pion/srtp/v3 v3.0.6 h1:E2gyj1f5X10sB/qILUGIkL4C2CqK269Xq167PbGCc/4= +github.com/pion/srtp/v3 v3.0.6/go.mod h1:BxvziG3v/armJHAaJ87euvkhHqWe9I7iiOy50K2QkhY= +github.com/pion/transport/v3 v3.0.7 h1:iRbMH05BzSNwhILHoBoAPxoB9xQgOaJk+591KC9P1o0= +github.com/pion/transport/v3 v3.0.7/go.mod h1:YleKiTZ4vqNxVwh77Z0zytYi7rXHl7j6uPLGhhz9rwo= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= +github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY= +github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE= +golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI= +golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=