最后一个字节失败的部分范围请求

时间:2018-03-05 11:54:04

标签: java html5 video partial-response

在尝试使用chrome作为视频播放工具来完成部分范围请求时,视频播放开始播放但在到达中途时,它会冻结,就像客户端仍在等待更多数据一样。此时服务器已经发送了整个请求。请遵守以下请求和响应以及用于发送所请求范围的代码:

Range:bytes=0-

Accept-Ranges:bytes
Content-Length:546827
Content-Range:bytes 0-546827/546828
Content-Type:video/webm

Accept-Ranges:bytes
Content-Length:6155
Content-Range:bytes 540672-546827/546828
Content-Type:video/webm


Accept-Ranges:bytes
Content-Length:1
Content-Range:bytes 546827-546827/546828
Content-Type:video/webm

第二个响应是否正确处理?导致它冻结此请求并再次开始发出多个请求,直到请求超时。

处理请求的代码:

private static void copyStream (PullBufferStream inputStream, OutputStream outputStream, BytesStreamObject bytesWrittenObj, int from, int to) throws IOException, CodecNotAvailableException {
    //export media stream
    BufferedOutputStream bout = new BufferedOutputStream (outputStream);
    Buffer b = new Buffer();
    long skipped =0;
    int byteLength = 0;

    do {
        inputStream.read(b);

        int len    = b.getLength();
        byte[] data   = (byte[]) b.getData();
        int  offset = b.getOffset();
        if (len > 0) {
            if(skipped < from) {             
              // skip bytes until reach from position in inputstream
              if(skipped + len <= from) {     
                  // skip all bytes just read
                  skipped += len;
              } else {                       
                  // skip only some of bytes read 
                  offset += (from - skipped);
                  len -= (from - skipped);
                  skipped = from;

              }
            } 
            if(to >= 0) {
                if(from + byteLength + len <= to) {
                    // use the full buffer
                } else {
                    // trim len to needed bytes to be read to prevent exceeding the "to" input parameter
                    len = (to + 1) - (from + byteLength);
                }
            } 

            byteLength+= len;
            bytesWrittenObj.bytesWritten = byteLength;
            bout.write(data, offset, len);
            //bout.write(data, from, end);
        } 
    } while (!b.isEOM());
    //fileProperties.setBytesLength(byteLength);

    //bout.close(); 

}

1 个答案:

答案 0 :(得分:0)

我需要确保刷新输出流,但也要对实际文件大小进行一些更改,而不是使用stream.available()。