Go语言实现Onvif客户端:6、获取rtsp流地址
1、代码
获取并选择Profile token后获取rtsp流地址也是调用接口即可:
/**
* @Description: 获取rtsp流媒体信息
* @time: 2021-03-30 11:22:06
* @receiver client
* @return returnInfo:结果Code码和Info信息,Code码为0则成功,否则失败,通过获取Info查看失败原因
*/
func (client *GoOnvifClient) GetStreamUri() returnInfo {
if client.localSelectProfileToken == "" {
return returnInfo{GetStreamUriErr, "profile token is nil."}
}
return client.getStreamUri()
}
/**
* @Description: 根据选中的Profile token获取码流rtsp地址等
* @time: 2021-03-25 14:28:46
* @receiver client
* @return returnInfo
*/
func (client *GoOnvifClient) getStreamUri() returnInfo {
mediaUrlReq := media.GetStreamUri{}
mediaUrlReq.ProfileToken = onvif.ReferenceToken(client.localSelectProfileToken)
return client.sendReqGetResp(GetStreamUriErr, mediaUrlReq)
}
2、结果:
获取到的xml信息如下:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:soapenc="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tt="http://www.onvif.org/ver10/schema" xmlns:tds="http://www.onvif.org/ver10/device/wsdl" xmlns:trt="http://www.onvif.org/ver10/media/wsdl" xmlns:timg="http://www.onvif.org/ver20/imaging/wsdl" xmlns:tev="http://www.onvif.org/ver10/events/wsdl" xmlns:tptz="http://www.onvif.org/ver20/ptz/wsdl" xmlns:tan="http://www.onvif.org/ver20/analytics/wsdl" xmlns:tst="http://www.onvif.org/ver10/storage/wsdl" xmlns:ter="http://www.onvif.org/ver10/error" xmlns:dn="http://www.onvif.org/ver10/network/wsdl" xmlns:tns1="http://www.onvif.org/ver10/topics" xmlns:tmd="http://www.onvif.org/ver10/deviceIO/wsdl" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl" xmlns:wsoap12="http://schemas.xmlsoap.org/wsdl/soap12" xmlns:http="http://schemas.xmlsoap.org/wsdl/http" xmlns:d="http://schemas.xmlsoap.org/ws/2005/04/discovery" xmlns:wsadis="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wstop="http://docs.oasis-open.org/wsn/t-1" xmlns:wsrf-bf="http://docs.oasis-open.org/wsrf/bf-2" xmlns:wsntw="http://docs.oasis-open.org/wsn/bw-2" xmlns:wsrf-rw="http://docs.oasis-open.org/wsrf/rw-2" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:wsrf-r="http://docs.oasis-open.org/wsrf/r-2" xmlns:trc="http://www.onvif.org/ver10/recording/wsdl" xmlns:tse="http://www.onvif.org/ver10/search/wsdl" xmlns:trp="http://www.onvif.org/ver10/replay/wsdl" xmlns:tnshik="http://www.hikvision.com/2011/event/topics" xmlns:hikwsd="http://www.onvifext.com/onvif/ext/ver10/wsdl" xmlns:hikxsd="http://www.onvifext.com/onvif/ext/ver10/schema" xmlns:tas="http://www.onvif.org/ver10/advancedsecurity/wsdl" xmlns:tr2="http://www.onvif.org/ver20/media/wsdl" xmlns:axt="http://www.onvif.org/ver20/analytics">
<env:Body>
<trt:GetStreamUriResponse>
<trt:MediaUri>
<tt:Uri>rtsp://40.40.40.101:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1</tt:Uri>
<tt:InvalidAfterConnect>false</tt:InvalidAfterConnect>
<tt:InvalidAfterReboot>false</tt:InvalidAfterReboot>
<tt:Timeout>PT60S</tt:Timeout>
</trt:MediaUri>
</trt:GetStreamUriResponse>
</env:Body>
</env:Envelope>
3、查看
使用vlc播放器播放即可,比如我们上面获取的url为:rtsp://40.40.40.101:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
则通过打开网络串流播放:
如果url正确则播放正常,这里显示的码流和你选择的Profile token相关: