Created
June 20, 2014 08:32
-
-
Save acgrid/2062c2495aeb5116aa3d to your computer and use it in GitHub Desktop.
Unsuccessful XHR Library on OGG over HTTP Live Stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vim: set et sw=2 ts=2 sts=2 ff=unix fenc=utf8: | |
// Author: Binux<i@binux.me> | |
// http://binux.me | |
// acgrid<acgrid@gmail.com> | |
// Created on 2013-04-20 20:17:45 | |
define(['underscore', 'utils', 'jquery'], function(__,utils,$) { | |
return { | |
choice_piece_size: function() { | |
var psize = 1 << 19; // 1M, 4M for production | |
return psize; | |
}, | |
choice_block_size: function(piece_size) { | |
var bsize = piece_size; | |
while (bsize > 1 << 18) { // 512K, 1M for production | |
bsize >>= 1; | |
} | |
return bsize; | |
}, | |
build: function(stream_url) { | |
var piece_size = this.choice_piece_size(); | |
var block_size = this.choice_block_size(piece_size); | |
var result = { | |
type: utils.source_mimetype(stream_url), | |
piece_size: piece_size, | |
piece_cnt: 0, | |
block_size: block_size, | |
}; | |
var builder = { | |
'result': result | |
}; | |
return builder; | |
}, | |
loop: function(stream_url, piece_size, onrecv, ondata) { | |
var last_size = 0; | |
var first = 2; | |
try{ | |
var xhr = new XMLHttpRequest(); | |
xhr.responseType = 'stream'; | |
//xhr.overrideMimeType('text/plain; charset=x-user-define'); | |
xhr.onerror = function() { console.log(e); }; | |
xhr.addEventListener("progress", function(evt) { | |
//$("#J_ajax_size").text(utils.format_size(evt.loaded)); // for test, also console.log(evt.loaded) | |
}, false); | |
xhr.onreadystatechange = function() { | |
try{ | |
if (xhr.readyState == xhr.LOADING){ | |
if(first > 0){ | |
console.log(this.response); | |
console.log(typeof(this.response)); // result: string | |
console.log(this.response.length); | |
console.log(this.responseText.length); | |
first--; | |
} | |
var blob = new Blob([this.response],{type: "video/ogg"}); | |
var total_size = blob.size; | |
var offset = last_size; | |
if (_.isFunction(onrecv)) { | |
onrecv(total_size); | |
} | |
if(total_size > last_size && last_size + piece_size < total_size){ // new data arrive and can fill up a new block, do write add callback update | |
last_size = total_size; // Consider possible disorder: late blocks is written before early blocks | |
if (_.isFunction(ondata)) { | |
var thisblob = blob.slice(offset,blob.size); | |
ondata(thisblob,offset,last_size); | |
} | |
} | |
} | |
}catch (e){ | |
console.log(e); | |
} | |
}; | |
xhr.open("GET", stream_url, true); | |
xhr.send(); | |
}catch (e){ | |
console.log(e); | |
} | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment