Note that there are some explanatory texts on larger screens.

plurals
  1. POJava HTTP process for 'streaming' video file
    primarykey
    data
    text
    <p>I am creating a Java application which 'streams' a video file over http to a browser (currently Chrome v24.x). This video is sent to FFmpeg and the output of this is sent over HTTP. </p> <p>Now, once the file is completely encoded the file is served using chunked transfer, and responding to range requests. Example headers:</p> <h3>Request</h3> <pre><code>GET /file/9fe6b502-c127-47c2-b6d2-83ea58676a8d HTTP/1.1 : Host: localhost:1234 : Connection: keep-alive : Accept-Encoding: identity;q=1, *;q=0 : User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17 : Accept: */* : Referer: http://localhost:1234/media/9fe6b502-c127-47c2-b6d2-83ea58676a8d : Accept-Language: en-US,en;q=0.8 : Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 : Cookie: plushContainerWidth=100%25; plushNoTopMenu=0 : Range: bytes=0- : </code></pre> <h3>Response</h3> <pre><code>HTTP/1.1 206 Partial Content : Connection: close : Date: Mon, 28 Jan 2013 14:51:52 GMT : Content-Type: video/mp4 : Etag: "9fe6b502-c127-47c2-b6d2-83ea58676a8d" : Accept-Ranges: bytes : Content-Range: bytes 0-625825/625826 : Content-Length: 625826 : Transfer-Encoding: chunked : </code></pre> <p>Data sent is 625826 bytes, excluding header data and chunked overhead. </p> <p>Now, this works just fine!</p> <p>The trouble is when the GET request happens before the file encoding has completed. I have tried to start sending the file straight away, over HTTP, using just chunked transfer with no content length attributes or ranges because they are not currently known. This causes the browser to wait for the full file (and not start playing until the transfer has completed). In addition, when the file transfer is completed, the browser reports a video error that the file could not be played. Example headers:</p> <h3>Request</h3> <pre><code>Request : GET /file/9fe6b502-c127-47c2-b6d2-83ea58676a8d HTTP/1.1 : Host: localhost:1234 : Connection: keep-alive : Accept-Encoding: identity;q=1, *;q=0 : User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17 : Accept: */* : Referer: http://localhost:1234/media/9fe6b502-c127-47c2-b6d2-83ea58676a8d : Accept-Language: en-US,en;q=0.8 : Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 : Cookie: plushContainerWidth=100%25; plushNoTopMenu=0 : Range: bytes=0- </code></pre> <h3>Response</h3> <pre><code>HTTP/1.1 200 OK : Connection: close : Date: Mon, 28 Jan 2013 14:51:29 GMT : Content-Type: video/mp4 : Transfer-Encoding: chunked </code></pre> <p>Data sent is 625826 bytes, excluding header data and chunked overhead. </p> <p>Does anyone have any ideas what's going wrong, or how to start playing a video without knowing the full length of the file? </p> <p>Thanks for your time, </p> <p>James. </p> <p>//EDIT</p> <p>As the request for the incomplete file states 'Range: bytes=0-' - can I reply with a partial content of 'n' bytes (say, 1000 bytes) with Content-Range: bytes 0-999/* ?</p> <p>//EDIT 2 As requested, here is my code for outputting the file. This is condensed as the code actually spans several classes.</p> <pre><code>File f = new File(_filename); RandomAccessFile raf = new RandomAccessFile(f, "r"); ChunkedOutputStream cos = new ChunkedOutputStream(_out, 1024 * 100); byte[] bytes; while (true){ lBufferSizeMax = Math.min(lBufferSize, fc.length() - lCompleted); bytes = new byte[(int)lBufferSizeMax]; lCurrentRead = fc.read(bytes); if (lCurrentRead == 0){ break; } cos.write(bytes); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload