/
testbench.js
193 lines (179 loc) · 5.33 KB
/
testbench.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* eslint-disable no-var, no-console */
// Browser environment state
var video;
var logString = '';
var hls;
function setupConsoleLogRedirection() {
var log = document.getElementById('log');
var inner = log.getElementsByClassName('inner')[0];
// append log message
function append(methodName, msg) {
var a =
new Date().toISOString().replace('T', ' ').replace('Z', '') + ': ' + msg;
var text = document.createTextNode(a);
var line = document.createElement('pre');
line.className = 'line line-' + methodName;
line.appendChild(text);
inner.appendChild(line);
// The empty log line at the beginning comes from a test in `enableLogs`.
self.logString = logString += a + '\n';
}
// overload global window console methods
var methods = ['log', 'debug', 'info', 'warn', 'error'];
methods.forEach(function (methodName) {
var original = self.console[methodName];
if (!original) {
return;
}
self.console[methodName] = function () {
append(
methodName,
Array.prototype.slice
.call(arguments)
.map(function (arg) {
try {
return JSON.stringify(arg);
} catch (err) {
return 'Unserializable (reason: ' + err.message + ')';
}
})
.join(' ')
);
return original.apply(this, arguments);
};
});
}
// Object.assign polyfill
function objectAssign(target, firstSource) {
if (target === undefined || target === null) {
throw new TypeError('Cannot convert first argument to object');
}
var to = Object(target);
for (var i = 1; i < arguments.length; i++) {
var nextSource = arguments[i];
if (nextSource === undefined || nextSource === null) {
continue;
}
var keysArray = Object.keys(Object(nextSource));
for (
var nextIndex = 0, len = keysArray.length;
nextIndex < len;
nextIndex++
) {
var nextKey = keysArray[nextIndex];
var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
if (desc !== undefined && desc.enumerable) {
to[nextKey] = nextSource[nextKey];
}
}
}
return to;
}
function startStream(streamUrl, config, callback, autoplay) {
var Hls = self.Hls;
if (!Hls) {
throw new Error('Hls not installed');
}
if (!Hls.isSupported()) {
callback({ code: 'notSupported', logs: logString });
return;
}
if (hls) {
callback({ code: 'hlsjsAlreadyInitialised', logs: logString });
return;
}
self.video = video = document.getElementById('video');
try {
self.hls = hls = new Hls(
objectAssign({}, config, {
// debug: true
debug: {
debug: function () {},
log: console.log.bind(console),
info: console.info.bind(console, '[info]'),
warn: console.warn.bind(console, '[warn]'),
error: console.error.bind(console, '[error]'),
},
})
);
console.log('[test] > userAgent:', navigator.userAgent);
if (autoplay !== false) {
hls.on(Hls.Events.MANIFEST_PARSED, function () {
console.log('[test] > Manifest parsed. Calling video.play()');
var playPromise = video.play();
if (playPromise) {
playPromise.catch(function (error) {
console.log(
'[test] > video.play() failed with error: ' +
error.name +
' ' +
error.message
);
if (error.name === 'NotAllowedError') {
console.log('[test] > Attempting to play with video muted');
video.muted = true;
return video.play();
}
});
}
});
}
hls.on(Hls.Events.ERROR, function (event, data) {
if (data.fatal) {
console.log('[test] > hlsjs fatal error :' + data.details);
if (data.details === Hls.ErrorDetails.INTERNAL_EXCEPTION) {
console.log('[test] > exception in :' + data.event);
console.log(
data.error.stack
? JSON.stringify(data.error.stack)
: data.error.message
);
}
callback({ code: data.details, logs: logString });
}
});
video.onerror = function (event) {
console.log('[test] > video error, code :' + video.error.code);
callback({ code: 'video_error_' + video.error.code, logs: logString });
};
hls.loadSource(streamUrl);
hls.attachMedia(video);
} catch (err) {
callback({ code: 'exception', logs: logString });
}
}
function switchToLowestLevel(mode) {
console.log('[test] > switch to lowest level', mode);
switch (mode) {
case 'current':
hls.currentLevel = 0;
break;
case 'next':
hls.nextLevel = 0;
break;
case 'load':
default:
hls.loadLevel = 0;
break;
}
}
function switchToHighestLevel(mode) {
var highestLevel = hls.levels.length - 1;
console.log('[test] > switch to highest level', highestLevel, mode);
switch (mode) {
case 'current':
hls.currentLevel = highestLevel;
break;
case 'next':
hls.nextLevel = highestLevel;
break;
case 'load':
default:
hls.loadLevel = highestLevel;
break;
}
}
self.setupConsoleLogRedirection = setupConsoleLogRedirection;
self.startStream = startStream;
self.switchToHighestLevel = switchToHighestLevel;
self.switchToLowestLevel = switchToLowestLevel;