如何通过 Spring Webserver 将 HTTP 视频流代理到任意数量的客户端 [英] How to proxy a HTTP video stream to any amount of clients through a Spring Webserver

查看:48
本文介绍了如何通过 Spring Webserver 将 HTTP 视频流代理到任意数量的客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前提是我在与 Spring Webserver 位于同一网络的服务器上广播了一个视频 HTTP 流,例如在某些 url 中,例如:

Provided that I have a video HTTP stream broadcasted on a server that is on the same network as my Spring Webserver is, for instance in some url such as:

http://localhost:9090/httpstream

如何使用 Spring 将此视频流代理到任意数量的客户端?以下示例演示了所需的流程:

How can I proxy this video stream to any amount of clients, using Spring? The following example demonstrates the wanted flow:

  1. Spring 网络服务器位于 http://localhost:9091/spring
  2. 客户端想要访问视频流,因此他将他的视频流播放器连接到 http://localhost:9091/spring(spring 网络服务器)
  3. Spring WebServer 应该将在 http://localhost:9090/httpstream 上找到的流重定向到客户端,后者永远不知道他访问了 httpstream 主机.连接是由 Spring 建立的,而不是由客户端建立的
  1. Spring webserver can be found at http://localhost:9091/spring
  2. A client wants to access a video stream, so he connects his video-stream-player to http://localhost:9091/spring (the spring webserver)
  3. The Spring WebServer should redirect the stream found on http://localhost:9090/httpstream to the client, with the latter never knowing that he accessed the httpstream host. The connection is made by Spring, not by the client

这是必需的,因为 HTTPStream 是一个不安全且未经过身份验证的主机,我想将它包装在 Spring Webserver 周围,以便我可以使用某种形式的安全性,例如 基本身份验证.

This is needed because the HTTPStream is an unsecured and not authenticated host, and I wanted to wrap it around a Spring Webserver, so that I could use some form of Security, such as a Basic Auth.

我尝试请求某种形式的映射,但找不到要返回的对象类型,也找不到如何建立连接,但预期的行为应该是这样的:

I tried requesting some form of mapping, but I couldn't find what kind of object to return, nor how to make the connection, but the expected behaviour should be something like this:

@Controller
public class HttpStreamProxyController {

    @RequestMapping("/spring") {
    public /*Stream Object?*/ getSecuredHttpStream() {
       if (clientIsSecured) {
       //... Security information

       return   //What should be returned?
       }
   }
}

推荐答案

我不确定您使用哪种来源来生成视频流(实时摄像机或视频文件或 youtube 视频或 ..)

I am not sure what kind of source you are using for generating your video stream (live camera or a video file or youtube video or ..)

您可能可以使用 StreamingResponseBody(需要 Spring 4.2+).参考以下链接

You can probably use StreamingResponseBody (requires Spring 4.2+). Refer following links

http://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/streaming-response-body/

http://shazsterblog.blogspot.in/2016/02/asynchronous-streaming-request.html

试试这个 -

    @GetMapping("/stream1")
        @ResponseBody
        public StreamingResponseBody getVidoeStream1(@RequestParam String any) throws IOException {
            /* do security check before connecting to stream hosting server */ 
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<Resource> responseEntity = restTemplate.exchange( "http://localhost:8080/stream", HttpMethod.GET, null, Resource.class );
            InputStream st = responseEntity.getBody().getInputStream();
            return (os) -> {
                readAndWrite(st, os);
            };


    }

private void readAndWrite(final InputStream is, OutputStream os)
            throws IOException {
        byte[] data = new byte[2048];
        int read = 0;
        while ((read = is.read(data)) > 0) {
            os.write(data, 0, read);
        }
        os.flush();
    }

它应该可以工作.您可以根据自己的要求编写自己的 readAndWrite() 实现.

It should work. You can write your own implementation of readAndWrite() depending on your requirements.

所以,你的 spring 代理控制器可能是这样的......

So, your spring proxy controller could be something like this...

@Controller
public class HttpStreamProxyController {

    @RequestMapping("/spring") 
    @ResponseBody
    public StreamingResponseBody  getSecuredHttpStream() {
       if (clientIsSecured) {
       //... Security information

    RestTemplate restTemplate = new RestTemplate();
    // get video stream by connecting to stream hosting server  like this
            ResponseEntity<Resource> responseEntity = restTemplate.exchange( "https://ur-to-stream", HttpMethod.GET, null, Resource.class );
            InputStream st = responseEntity.getBody().getInputStream();
    // Or if there is any other preferred way of getting the video stream use that. The idea is to get the video input stream    

    // now return a StreamingResponseBody  object created by following lambda 
            return (os) -> {
                readAndWrite(st, os);
            };

       } else {
          return null;
       }

   }
}

您的 rest 端点返回的 StreamingResponseBody 可以在 HTML5 中正常工作,这可能类似于 ..

The StreamingResponseBody returned by your rest endpoint will work fine with HTML5, which could be something like ..

<video width="320" height="240" controls>
  <source src="/spring" type="video/mp4">
  Your browser does not support the video tag
</video>

这篇关于如何通过 Spring Webserver 将 HTTP 视频流代理到任意数量的客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆