How to proxy HTTP video stream for any number of clients through Spring web server

Provided that I have an HTTP video stream broadcast on a server that is on the same network as my Spring web server, for example, in some URL, for example:

http: // localhost: 9090 / httpstream

How can I proxy this video stream to any number of clients using Spring? The following example demonstrates the desired stream:

This is necessary because HTTPStream is an insecure and not authenticated host, and I would like to wrap it around Spring Webserverso that I can use some form of security, for example Basic Auth.


I tried to request some form of matching, but I could not find which object to return, and how to make a connection, but the expected behavior should be something like this:

@Controller
public class HttpStreamProxyController {

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

       return   //What should be returned?
       }
   }
}
+7
source share
3 answers

, ( YouTube ...)

, StreamingResponseBody ( Spring 4.2+). .

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() .

, - Spring - ...

@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;
       }

   }
}

StreamingResponseBody, , HTML5, - .

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

- kurento. http://doc-kurento.readthedocs.io/en/latest/tutorials/java/tutorial-one2many.html. WEB-RTC .

:

Java, - Kurento. , Java EE , SIP Servlets, Play, Vertex .. Spring Boot for .

0

The restTemplate exchange solution does not work properly for video streams (in my case mp4), because the request freezes and the client does not receive any data. Take a look at the execute method. Example:

import org.apache.poi.util.IOUtils;

@GetMapping("/video")    
public void process(HttpServletResponse response) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.execute(            
        URI.create("https://some-url"),            
        HttpMethod.GET,            
        (ClientHttpRequest request) -> {},            
        responseExtractor -> {                
            response.setContentType("video/mp4");                
            IOUtils.copy(responseExtractor.getBody(), response.getOutputStream());                
            return null;            
        }
    );    
}

0
source

All Articles