Java onvif协议通用协议获取rtsp地址
Java onvif协议通用协议获取rtsp地址
吐槽下可搜索的资源真的是少,好多都是4 5年前的资料
onvif通用协议就不说了,大多摄像头都可用,利用onvif获取实时流和历史流转换成h264编码推送到前端,用h5新标签直接播放
onvif协议大部分是c或者android 资料 java的比较少 感谢大佬留下的宝贵资源:
1. https://github.com/RootSoft/ONVIF-Java (功能不够完善,没有使用例子)
2. https://github.com/fpompermaier/onvif (这个是基于上面的项目封装,有简单的例子,这里使用的这个)
实际使用:
前言:因为例子有限,没有历史流rtsp地址的使用,在参考例子和源码下,走了很多坑
后续更新利用javacv 使用rtsp地址拉取流推送出去。
上代码:
将第二个项目拉下来打包到本地仓库 下面的
pom 中的onvif-ws-client就是打包到本地仓库的
项目地址:https://github.com/xsjzf/onvif_java
核心的地方是:
这里说明下例子中只有实时流rtsp地址的方法获取
在onvif-ws-client的源码中有很多功能例子中只有media 也就是实时流的
要使用历史流则需要自行添加 就是replay和recording 其他功能详见onvif的官网文档
Capabilities capabilities = this.device.getCapabilities(Arrays.asList(CapabilityCategory.ALL));
@javax.xml.bind.annotation.XmlElement(name = “Analytics”)
protected org.onvif.ver10.schema.AnalyticsCapabilities analytics;
@javax.xml.bind.annotation.XmlElement(name = “Device”)
protected org.onvif.ver10.schema.DeviceCapabilities device;
@javax.xml.bind.annotation.XmlElement(name = “Events”)
protected org.onvif.ver10.schema.EventCapabilities events;
@javax.xml.bind.annotation.XmlElement(name = “Imaging”)
protected org.onvif.ver10.schema.ImagingCapabilities imaging;
@javax.xml.bind.annotation.XmlElement(name = “Media”)
protected org.onvif.ver10.schema.MediaCapabilities media;
@javax.xml.bind.annotation.XmlElement(name = “PTZ”)
protected org.onvif.ver10.schema.PTZCapabilities ptz;
@javax.xml.bind.annotation.XmlElement(name = “Extension”)
protected org.onvif.ver10.schema.CapabilitiesExtension extension;
OnvifDevice 的init方法中
Capabilities对象里面除了基本的功能大多在Extension里面
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = “CapabilitiesExtension”, propOrder = {
“any”,
“deviceIO”,
“display”,
“recording”,
“search”,
“replay”,
“receiver”,
“analyticsDevice”,
“extensions”
})
public class CapabilitiesExtension {
@XmlAnyElement(lax = true)
protected List any;
@XmlElement(name = “DeviceIO”)
protected DeviceIOCapabilities deviceIO;
@XmlElement(name = “Display”)
protected DisplayCapabilities display;
@XmlElement(name = “Recording”)
protected RecordingCapabilities recording;
@XmlElement(name = “Search”)
protected SearchCapabilities search;
@XmlElement(name = “Replay”)
protected ReplayCapabilities replay;
@XmlElement(name = “Receiver”)
protected ReceiverCapabilities receiver;
@XmlElement(name = “AnalyticsDevice”)
protected AnalyticsDeviceCapabilities analyticsDevice;
@XmlElement(name = “Extensions”)
protected CapabilitiesExtension2 extensions;
例如我这里需要历史流的功能那么我在init方法中初始化添加上
if (capabilities.getExtension().getReplay() != null && capabilities.getExtension().getReplay().getXAddr() != null) {
this.replayPort = new ReplayService().getReplayPort();
this.replayPort =
getServiceProxy((BindingProvider) replayPort, capabilities.getExtension().getReplay().getXAddr())
.create(ReplayPort.class);
}
if (capabilities.getExtension().getRecording() != null && capabilities.getExtension().getRecording().getXAddr() != null) {
this.recordingPort = new RecordingService().getRecordingPort();
this.recordingPort =
getServiceProxy((BindingProvider) recordingPort, capabilities.getExtension().getRecording().getXAddr())
.create(RecordingPort.class);
}
if (capabilities.getExtension().getSearch() != null && capabilities.getExtension().getSearch().getXAddr() != null) {
this.searchPort = new SearchService().getSearchPort();
this.searchPort =
getServiceProxy((BindingProvider) searchPort, capabilities.getExtension().getSearch().getXAddr())
.create(SearchPort.class);
}
具体使用:
/**
* 获取到OnvifDevice对象
* @param host 摄像头地址 92.168.xx.yy, or http://host[:port]
* @param username 用户名
* @param password 密码
* @param profileToken “MediaProfile000” If empty, will use first profile.
* @return
*/
public static OnvifDevice getOnvifCredentials(String host, String username, String password, String profileToken){
try {
OnvifCredentials credentials = new OnvifCredentials(host, username, password, profileToken);
//补全host
URL u = credentials.getHost().startsWith(“http”)
? new URL(credentials.getHost())
: new URL(“http://” + credentials.getHost());
return new OnvifDevice(u, credentials.getUser(), credentials.getPassword());
} catch (MalformedURLException | ConnectException | SOAPException e) {
e.printStackTrace();
throw new RrException(e.getMessage());
}
}
/**
* 获取实时rtsp地址
* @param onvifDevice 设备
* @return
* @throws Exception
*/
public static String getRtspUrl(OnvifDevice onvifDevice) throws Exception {
List profiles = onvifDevice.getMedia().getProfiles();
for (Profile profile : profiles) {
String profileToken = profile.getToken();
String rtsp = onvifDevice.getStreamUri(profileToken, TransportProtocol.RTSP);
String uri = “rtsp://” + onvifDevice.getUser() + “:” + onvifDevice.getPassword() + “@” + rtsp.replace(“rtsp://”, “”);
}
return “”;
}
/**
* 获取历史rtsp地址
* @param onvifDevice 设备
* @return
*/
public static String getReplayUrl(OnvifDevice onvifDevice){
List recordings = onvifDevice.getRecordingPort().getRecordings();
for (GetRecordingsResponseItem recording : recordings) {
String recordingToken = recording.getRecordingToken();
String rtsp = onvifDevice.getReplayUri(onvifDevice, recordingToken, TransportProtocol.RTSP);
String uri = “rtsp://” + onvifDevice.getUser() + “:” + onvifDevice.getPassword() + “@” + rtsp.replace(“rtsp://”, “”);
}
return “”;
}
/**
* 转换 时间段为rtsp时间格式
* @param start 开始
* @param end 结束
* @return ?starttime=20200908t093812z&endtime=20200908t104816z
*/
public static String getRtspTimeSpace(LocalDateTime start, LocalDateTime end){
Long st = start.toInstant(ZoneOffset.of(“+8”)).toEpochMilli();
Long ed = end.toInstant(ZoneOffset.of(“+8”)).toEpochMilli();
String ios8601St = getDate(st);
String ios8601Ed = getDate(st);
return “?starttime=” + ios8601St + “&endtime=” + ios8601Ed;
}
/**
* 时间戳转换成IOS8601格式
* @param beginTime
* @return
*/
public static String getDate(Long beginTime) {
SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String bt = format.format(beginTime);
Date date = null;
try {
date = format.parse(bt);
} catch (Exception e) {
e.printStackTrace();
}
SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMdd’T’HHmmss’z'”);
return sdf.format(date);
}
这里就会获取到实时流和历史流的rtsp地址 后续文章会使用这个地址实现视频推流
****持续更新****
Java onvif协议通用协议获取rtsp地址相关教程
今天的文章Java onvif协议,Java onvif协议通用协议获取rtsp地址分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/7630.html