From 69da67139f8d24996c63591d2b90e6d4e4adbcaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B9=BF?= Date: Fri, 8 Nov 2024 00:17:42 +0800 Subject: [PATCH] add support for other protocol. add user/pass for media uri --- device.go | 17 +++++++++++++++-- onvif/Device.go | 10 +++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/device.go b/device.go index 9356c60..b8d76f1 100644 --- a/device.go +++ b/device.go @@ -5,6 +5,7 @@ import ( "errors" "net" "net/http" + "net/url" "strings" "time" @@ -187,7 +188,13 @@ func (d *Device) GetReplayUri(token string, stream onvifmodel.StreamSetup) (stri } // 返回获取到的URI - return string(resp.Uri), nil + // return string(resp.Uri), nil + if err != nil { + return "", err + } + urlobj, _ := url.Parse(string(resp.Uri)) + urlobj.User = url.UserPassword(d.Username, d.Password) + return urlobj.String(), nil } func (d *Device) GetStreamUri(stream onvifmodel.StreamSetup, profileToken string) (string, error) { @@ -201,7 +208,13 @@ func (d *Device) GetStreamUri(stream onvifmodel.StreamSetup, profileToken string } resp, rerr := media_sdk.Call_GetStreamUri(d.ctx, d.device, getStreamUri) // resp, rerr := media_sdk.Call_GetStreamUri(d.ctx, d.device, media.GetStreamUri{}) - return string(resp.MediaUri.Uri), rerr + // return string(resp.MediaUri.Uri), rerr + if rerr != nil { + return "", rerr + } + urlobj, _ := url.Parse(string(resp.MediaUri.Uri)) + urlobj.User = url.UserPassword(d.Username, d.Password) + return urlobj.String(), nil } func (d *Device) GetStreamUriTcp(profileToken string) (string, error) { diff --git a/onvif/Device.go b/onvif/Device.go index e1b7ec8..6bc3eaf 100644 --- a/onvif/Device.go +++ b/onvif/Device.go @@ -167,7 +167,15 @@ func NewDevice(params DeviceParams) (*Device, error) { dev := new(Device) dev.params = params dev.endpoints = make(map[string]string) - dev.addEndpoint("Device", "http://"+dev.params.Xaddr+"/onvif/device_service") + var urlstr string + if strings.HasPrefix(params.Xaddr, "http") { + urlObj, _ := url.Parse(params.Xaddr) + urlObj.Path = "/onvif/device_service" + urlstr = urlObj.String() + } else { + urlstr = "http://" + params.Xaddr + "/onvif/device_service" + } + dev.addEndpoint("Device", urlstr) if dev.params.HttpClient == nil { dev.params.HttpClient = new(http.Client)