我正在创建一个Java应用程序,将视频文件通过http流传输到浏览器(目前是Chrome v24.x)。该视频被发送到FFmpeg,其输出通过HTTP发送。
现在,一旦文件完全编码,该文件将使用分块传输进行服务,并响应范围请求。示例标头:
请求
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- :
响应
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 :
发送的数据量为625826字节,不包括头部数据和分块开销。
现在,这个工作非常正常!
问题是当GET请求发生在文件编码完成之前。我尝试立即开始使用HTTP发送文件,仅使用分块传输,没有内容长度属性或范围,因为它们目前未知。这会导致浏览器等待完整的文件(并且在传输完成之前不开始播放)。此外,当文件传输完成时,浏览器报告视频错误,无法播放该文件。示例标头:
请求
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-
响应
HTTP/1.1 200 OK : Connection: close : Date: Mon, 28 Jan 2013 14:51:29 GMT : Content-Type: video/mp4 : Transfer-Encoding: chunked
发送的数据为625826字节,不包括头部数据和分块开销。
有没有人有任何想法是什么出了问题,或如何开始播放一个视频而不知道文件的完整长度?
感谢您的时间,
詹姆斯。
//编辑
由于请求不完整文件状态“范围:字节= 0-”,我可以回复部分内容的“n”字节(比如1000字节)与Content-Range:字节0-999 / *?
//编辑2 根据要求,这里是输出文件的代码。由于代码实际上跨越了几个类,因此这是压缩的。
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);
}
content-type
等的完整代码。 - Andremoniy