av-merge: Use fetchRange promise properly
This commit is contained in:
parent
55a8e50d6a
commit
284024433b
@ -204,6 +204,7 @@ Stream.prototype.setup = async function(){
|
|||||||
this.url,
|
this.url,
|
||||||
this.initRange.start,
|
this.initRange.start,
|
||||||
this.indexRange.end,
|
this.indexRange.end,
|
||||||
|
).then(
|
||||||
(buffer) => {
|
(buffer) => {
|
||||||
let init_end = this.initRange.end - this.initRange.start + 1;
|
let init_end = this.initRange.end - this.initRange.start + 1;
|
||||||
let index_start = this.indexRange.start - this.initRange.start;
|
let index_start = this.indexRange.start - this.initRange.start;
|
||||||
@ -211,22 +212,21 @@ Stream.prototype.setup = async function(){
|
|||||||
this.setupInitSegment(buffer.slice(0, init_end));
|
this.setupInitSegment(buffer.slice(0, init_end));
|
||||||
this.setupSegmentIndex(buffer.slice(index_start, index_end));
|
this.setupSegmentIndex(buffer.slice(index_start, index_end));
|
||||||
}
|
}
|
||||||
)
|
);
|
||||||
} else {
|
} else {
|
||||||
// initialization data
|
// initialization data
|
||||||
await fetchRange(
|
await fetchRange(
|
||||||
this.url,
|
this.url,
|
||||||
this.initRange.start,
|
this.initRange.start,
|
||||||
this.initRange.end,
|
this.initRange.end,
|
||||||
this.setupInitSegment.bind(this),
|
).then(this.setupInitSegment.bind(this));
|
||||||
);
|
|
||||||
// sidx (segment index) table
|
// sidx (segment index) table
|
||||||
fetchRange(
|
fetchRange(
|
||||||
this.url,
|
this.url,
|
||||||
this.indexRange.start,
|
this.indexRange.start,
|
||||||
this.indexRange.end,
|
this.indexRange.end,
|
||||||
this.setupSegmentIndex.bind(this)
|
).then(this.setupSegmentIndex.bind(this));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stream.prototype.setupInitSegment = function(initSegment) {
|
Stream.prototype.setupInitSegment = function(initSegment) {
|
||||||
@ -388,7 +388,7 @@ Stream.prototype.getSegmentIdx = function(videoTime) {
|
|||||||
}
|
}
|
||||||
index = index + increment;
|
index = index + increment;
|
||||||
}
|
}
|
||||||
this.reportInfo('Could not find segment index for time', videoTime);
|
this.reportError('Could not find segment index for time', videoTime);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Stream.prototype.checkBuffer = async function() {
|
Stream.prototype.checkBuffer = async function() {
|
||||||
@ -485,8 +485,7 @@ Stream.prototype.fetchSegment = function(segmentIdx) {
|
|||||||
this.url,
|
this.url,
|
||||||
entry.start,
|
entry.start,
|
||||||
entry.end,
|
entry.end,
|
||||||
this.appendSegment.bind(this, segmentIdx),
|
).then(this.appendSegment.bind(this, segmentIdx));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
Stream.prototype.fetchSegmentIfNeeded = function(segmentIdx) {
|
Stream.prototype.fetchSegmentIfNeeded = function(segmentIdx) {
|
||||||
if (segmentIdx < 0 || segmentIdx >= this.sidx.entries.length){
|
if (segmentIdx < 0 || segmentIdx >= this.sidx.entries.length){
|
||||||
@ -518,22 +517,33 @@ Stream.prototype.reportWarning = function(...args) {
|
|||||||
Stream.prototype.reportError = function(...args) {
|
Stream.prototype.reportError = function(...args) {
|
||||||
reportError(String(this.streamType) + ':', ...args);
|
reportError(String(this.streamType) + ':', ...args);
|
||||||
}
|
}
|
||||||
Stream.prototype.reportInfo = function(...args) {
|
|
||||||
reportInfo(String(this.streamType) + ':', ...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Utility functions
|
// Utility functions
|
||||||
|
|
||||||
function fetchRange(url, start, end, cb) {
|
// https://gomakethings.com/promise-based-xhr/
|
||||||
|
// https://stackoverflow.com/a/30008115
|
||||||
|
function fetchRange(url, start, end) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let xhr = new XMLHttpRequest();
|
let xhr = new XMLHttpRequest();
|
||||||
xhr.open('get', url);
|
xhr.open('get', url);
|
||||||
xhr.responseType = 'arraybuffer';
|
xhr.responseType = 'arraybuffer';
|
||||||
xhr.setRequestHeader('Range', 'bytes=' + start + '-' + end);
|
xhr.setRequestHeader('Range', 'bytes=' + start + '-' + end);
|
||||||
xhr.onload = function() {
|
xhr.onload = function () {
|
||||||
//bytesFetched += end - start + 1;
|
if (xhr.status >= 200 && xhr.status < 300) {
|
||||||
resolve(cb(xhr.response));
|
resolve(xhr.response);
|
||||||
|
} else {
|
||||||
|
reject({
|
||||||
|
status: xhr.status,
|
||||||
|
statusText: xhr.statusText
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhr.onerror = function () {
|
||||||
|
reject({
|
||||||
|
status: xhr.status,
|
||||||
|
statusText: xhr.statusText
|
||||||
|
});
|
||||||
};
|
};
|
||||||
xhr.send();
|
xhr.send();
|
||||||
});
|
});
|
||||||
@ -573,9 +583,6 @@ function addEvent(obj, eventName, func) {
|
|||||||
return new RegisteredEvent(obj, eventName, func);
|
return new RegisteredEvent(obj, eventName, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
function reportInfo(...args){
|
|
||||||
console.info(...args);
|
|
||||||
}
|
|
||||||
function reportWarning(...args){
|
function reportWarning(...args){
|
||||||
console.warn(...args);
|
console.warn(...args);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user