-
Notifications
You must be signed in to change notification settings - Fork 1
/
mediasource.js
executable file
·138 lines (116 loc) · 3.75 KB
/
mediasource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Helpers for Media Source Extensions tests
var _logger;
function Logger(id) {
this.el = document.getElementById(id);
_logger = this;
}
var log_start = Date.now();
Logger.prototype.log = function(msg) {
var fragment = document.createDocumentFragment();
fragment.appendChild(document.createTextNode((Date.now() - log_start) + ': ' + msg));
fragment.appendChild(document.createElement('br'));
this.el.appendChild(fragment);
};
Logger.prototype.clear = function() {
this.el.textContent = '';
};
function info(message) {
_logger.log(message);
}
function ok(value, message) {
_logger.log("ok" + (value ? "(true) " : "(false) ") + message);
}
function is(value1, value2, message) {
var result = value1 == value2;
_logger.log("is(" + value1 + " == " + value2 + "): " + result + " (" + message + ")");
}
function isfuzzy(value1, value2, fuzz, message) {
var result = (value1 >= value2 - fuzz && value1 <= value2 + fuzz);
_logger.log("" + value1 + " == " + value2 + " ~" + fuzz + ": " + result + " (" + message + ")");
}
function runWithMSE(testFunction, id = 'log') {
window.onload = function() {
var ms = new MediaSource();
var el = document.createElement("video");
el.src = URL.createObjectURL(ms);
el.preload = "auto";
document.body.appendChild(el);
var log = document.createElement("pre");
log.setAttribute("id", id);
document.body.appendChild(log);
var logger = new Logger(id);
testFunction(ms, el);
};
}
function fetchWithXHR(uri, onLoadFunction) {
var p = new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open("GET", uri, true);
xhr.responseType = "arraybuffer";
xhr.addEventListener("load", function () {
is(xhr.status, 200, "fetchWithXHR load uri='" + uri + "' status=" + xhr.status);
resolve(xhr.response);
});
xhr.send();
});
if (onLoadFunction) {
p.then(onLoadFunction);
}
return p;
};
function range(start, end) {
var rv = [];
for (var i = start; i <= end; ++i) {
rv.push(i);
}
return rv;
}
function once(target, name, cb) {
var p = new Promise(function(resolve, reject) {
target.addEventListener(name, function() {
target.removeEventListener(name, arguments.callee);
resolve();
});
});
if (cb) {
p.then(cb);
}
return p;
}
function timeRangeToString(r) {
var str = "TimeRanges: ";
for (var i = 0; i < r.length; i++) {
str += "[" + r.start(i) + ", " + r.end(i) + ")";
}
return str;
}
function loadSegment(sb, typedArrayOrArrayBuffer) {
var typedArray = (typedArrayOrArrayBuffer instanceof ArrayBuffer) ? new Uint8Array(typedArrayOrArrayBuffer)
: typedArrayOrArrayBuffer;
info(`Loading buffer: [${typedArray.byteOffset}, ${typedArray.byteOffset + typedArray.byteLength})`);
var beforeBuffered = timeRangeToString(sb.buffered);
return new Promise(function(resolve, reject) {
once(sb, 'update').then(function() {
var afterBuffered = timeRangeToString(sb.buffered);
info(`SourceBuffer buffered ranges grew from ${beforeBuffered} to ${afterBuffered}`);
resolve();
});
sb.appendBuffer(typedArray);
});
}
function fetchAndLoad(sb, prefix, chunks, suffix) {
// Fetch the buffers in parallel.
var buffers = {};
var fetches = [];
for (var chunk of chunks) {
fetches.push(fetchWithXHR(prefix + chunk + suffix).then(((c, x) => buffers[c] = x).bind(null, chunk)));
}
// Load them in series, as required per spec.
return Promise.all(fetches).then(function() {
var rv = Promise.resolve();
for (var chunk of chunks) {
rv = rv.then(loadSegment.bind(null, sb, buffers[chunk]));
}
return rv;
});
}