简体   繁体   English

如何通过servlet服务内部IP摄像机mjpeg流?

[英]How to serve internal ip camera mjpeg stream through servlet?

I'm working on a project where I have a simple ip camera that has an mjpg stream with an internal address and a web server. 我正在一个项目中,我有一个简单的IP摄像机,该摄像机具有一个带有内部地址和Web服务器的mjpg流。 I would like to serve the camera's feed through my servlet so that the camera does not require port forwarding for each installation. 我想通过我的servlet服务摄像机的提要,以便摄像机不需要为每个安装都进行端口转发。 My ideal situation would be that the user would only enter the URL for the mjpg's location (located on their network) and the servlet would handle the rest. 我的理想情况是用户仅输入mjpg位置(位于其网络上)的URL,而servlet将处理其余的位置。

I'm pretty clueless on how to begin implementing this or if it's even possible as I'm not actually creating the mjpg stream myself, I'm using the one that already exists. 我对如何开始实施此方法一无所知,或者甚至有可能,因为我不是自己亲自创建mjpg流,而是使用已经存在的mjpg流。 Is there a way to do this without making an obscene amount of requests to my web server? 有没有一种方法可以在不向我的Web服务器发出大量请求的情况下进行此操作?

An additional thorn in my side is that I'm restricted to libraries that would work in J2ME. 我这边的另一个麻烦是,我仅限于可以在J2ME中使用的库。

In a servlet, you can simply inspect the HttpRequestObject for the URL, method, and probably accept header, then create an HttpURLConnection with the ip address of the appropriate camera, make the connection. 在servlet中,您可以简单地检查HttpRequestObject的URL,方法,并可能接受标头,然后使用适当摄像机的ip地址创建HttpURLConnection,进行连接。 When you get a response back from the camera, copy the appropriate entries from the response and set them in the ServletResponse object, and then get the camera response input stream and the output stream for the client's request and copy the bytes from one stream to another, and close everything up when you're done. 当您从摄像头获得响应时,请从响应中复制适当的条目并将其设置在ServletResponse对象中,然后获取摄像头响应输入流和客户端请求的输出流,并将字节从一个流复制到另一个,并在完成后关闭所有内容。

If you can deploy something like Jersey, the JAX-RS reference implementation, this would be even easier. 如果您可以部署诸如Jersey,JAX-RS参考实现之类的东西,这将更加容易。

Ok. 好。 So you basically want to proxy several cameras to be connected to via a single servlet? 因此,您基本上想代理几个通过单个servlet连接的摄像机?

It shouldn't be too hard. 应该不难。 You just need to open up a socket connection to the requested URL in your servlet. 您只需要在Servlet中打开与请求的URL的套接字连接即可。

Assuming you're using HTTP, if you had a request for "http://myservlet/servlet?url=http://camera_url_here", you should just be able to open up a connection to the camera URL and write the bytes received to the HttpServletResponse's Outputstream. 假设您使用的是HTTP,如果您请求“ http:// myservlet / servlet?url = http:// camera_url_here”,则应该能够打开与相机URL的连接并写入接收到的字节到HttpServletResponse的Outputstream。 You might want to stick your socket connections in a Map, so that you don't needlessly connect to the same camera twice. 您可能希望将套接字连接保留在Map中,这样就不必不必要地两次连接同一台摄像机。 Don't forget to copy/set the correct MIME types in the HttpServletResponse object. 不要忘记在HttpServletResponse对象中复制/设置正确的MIME类型。

Have you looked at http://www.videolan.org/vlc/streaming.html ? 您是否看过http://www.videolan.org/vlc/streaming.html I'm pretty sure it can already do this. 我很确定它已经可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM