HTML5 video crashes in Chrome, I think because of my response headers

337 views
Skip to first unread message

Marcus B

unread,
Aug 14, 2011, 8:46:17 AM8/14/11
to nodejs
Problem:
m4v files play and preload as normal at first, but when the browser
requests the video file a second time (this happens when I skip a
large part of the video in the seeker bar), the video element crashes
on the new data sent.
In the case of mp4 files, the second request is sent out immediately
and again the player crashes. Note that it's the HTML5 video element
that is giving the error, I'm not using any video.js or jplayer type
stuff.

Error:
The error given by the video element is a Code 3 MediaError: "An error
of some description occurred while decoding the media resource, after
the resource was established to be usable."

My guess:
I think it has something to do with the Content-Range / Content-Length
headers, but I can't figure out what I'm doing wrong.

My code:

var range = req.info.req.headers.range,
total = data.length,
parts = range.split('bytes=').join('').split('-'),
partialstart = parts[0],
partialend = parts[1],
start = parseInt(partialstart, 10),
end = partialend ? parseInt(partialend, 10) : total-1,
chunksize = (end-start)+1;

req.info.res.writeHead(206, { 'Transfer-Encoding':'chunked',
'Content-Range': 'bytes ' + start + '-'+ end + '/'
+ total,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type':
mime.lookup(req.uparams.join('/')),
'Connection':'keep-alive'});

req.info.res.end(data, 'binary'); // I have tried without the
'binary' parameter


Response Header Examples:

First request (seems to work fine):

Accept-Ranges:bytes
Connection:keep-alive // I have tried 'close'
Content-Length:3889885
Content-Range:bytes 0-3889884/3889885
Content-Type:video/x-m4v
Transfer-Encoding:chunked

Second request (makes the player crash):
Accept-Ranges:bytes
Connection:keep-alive
Content-Length:3874989
Content-Range:bytes 14896-3889884/3889885 // 3889885 - 3874989 =
14896 seems legit?
Content-Type:video/x-m4v
Transfer-Encoding:chunked

I have looked at the "Stream video with node.js" discussion at
http://groups.google.com/group/nodejs/browse_thread/thread/8339e0dc825c057f/e93c3aeaf3057275
(that's where I got my code).
Reply all
Reply to author
Forward
0 new messages