-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia_source2.js
343 lines (308 loc) · 9.47 KB
/
media_source2.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const SourceBufferOperationTypes = {
CONFIGURE: 0,
APPEND: 2,
REMOVE: 3,
CHANGE_TYPE: 4,
};
class SourceBuffer2 {
#currentOperation;
#errorEvent;
#sourceBuffer;
#pendingOperations;
constructor(sb) {
this.#sourceBuffer = sb;
this.#sourceBuffer.addEventListener('error', e => {
this.#errorEvent = e;
this.#runEventLoop();
});
this.#pendingOperations = [];
}
// Would be nice to hide this, but it's needed for removeSourceBuffer().
get sourceBuffer() {
return this.#sourceBuffer;
}
#runEventLoop() {
while (this.#pendingOperations.length > 0) {
if (this.#errorEvent) {
if (this.#currentOperation)
this.currentOperation.reject(this.#errorEvent);
let op = this.#pendingOperations.shift();
op.reject(this.#errorEvent);
continue;
}
if (this.#currentOperation) {
return;
}
if (this.#sourceBuffer.updating) {
let op = this.#pendingOperations.shift();
op.reject(new DOMException(
'External source of updates detected.', 'InvalidStateError'));
return;
}
// If readyState is closed or ended, the operations below will throw.
let op = this.#pendingOperations.shift();
try {
switch (op.operationType) {
case SourceBufferOperationTypes.APPEND: {
this.#sourceBuffer.addEventListener('updateend', _ => {
this.#currentOperation = null;
op.resolve();
this.#runEventLoop();
}, {once: true});
this.#sourceBuffer.appendBuffer(op.buffer);
this.#currentOperation = op;
break;
}
case SourceBufferOperationTypes.REMOVE: {
this.#sourceBuffer.addEventListener('updateend', _ => {
this.#currentOperation = null;
op.resolve();
this.#runEventLoop();
}, {once: true});
this.#sourceBuffer.remove(op.start, op.end);
this.#currentOperation = op;
break;
}
case SourceBufferOperationTypes.CONFIGURE: {
if ('timestampOffset' in op.options) {
this.#sourceBuffer.timestampOffset =
op.options['timestampOffset'];
}
if ('appendWindowStart' in op.options) {
this.#sourceBuffer.appendWindowStart =
op.options['appendWindowStart'];
}
if ('appendWindowEnd' in op.options) {
this.#sourceBuffer.appendWindowEnd =
op.options['appendWindowEnd'];
}
if ('mode' in op.options) {
this.#sourceBuffer.mode = op.options['mode'];
}
op.resolve();
break;
}
case SourceBufferOperationTypes.CHANGE_TYPE: {
this.#sourceBuffer.changeType(op.sourceBufferType);
op.resolve();
break;
}
}
} catch (e) {
op.reject(e);
}
}
}
// {timestampOffset:, mode:, appendWindowStart:, appendWindowEnd:}
configure(configureOptions) {
return new Promise((resolvePromise, rejectPromise) => {
this.#pendingOperations.push({
resolve: resolvePromise,
reject: rejectPromise,
operationType: SourceBufferOperationTypes.CONFIGURE,
options: configureOptions,
});
this.#runEventLoop();
});
}
appendBuffer(data) {
return new Promise((resolvePromise, rejectPromise) => {
this.#pendingOperations.push({
resolve: resolvePromise,
reject: rejectPromise,
operationType: SourceBufferOperationTypes.APPEND,
buffer: data,
});
this.#runEventLoop();
});
}
remove(startRange, endRange) {
return new Promise((resolvePromise, rejectPromise) => {
this.#pendingOperations.push({
resolve: resolvePromise,
reject: rejectPromise,
operationType: SourceBufferOperationTypes.REMOVE,
start: startRange,
end: endRange,
});
this.#runEventLoop();
});
}
changeType(type) {
return new Promise((resolvePromise, rejectPromise) => {
this.#pendingOperations.push({
resolve: resolvePromise,
reject: rejectPromise,
operationType: SourceBufferOperationTypes.CHANGE_TYPE,
sourceBufferType: type,
});
this.#runEventLoop();
});
}
async abort() {
// Invalid state for calling abort(), so let underlying SB throw.
if (this.#errorEvent) {
this.#sourceBuffer.abort();
return;
}
this.#errorEvent = new DOMException('abort() called', 'AbortError');
this.#runEventLoop();
this.#sourceBuffer.abort();
this.#errorEvent = null;
}
// Simple passthrough methods.
get audioTracks() {
return this.#sourceBuffer.audioTracks;
}
get videoTracks() {
return this.#sourceBuffer.videoTracks;
}
get buffered() {
return this.#sourceBuffer.buffered;
}
}
const MediaSourceOperationTypes = {
ADD: 0,
REMOVE: 1,
MARK_EOS: 2,
SET_DURATION: 3,
};
class MediaSource2 {
#opened;
#source;
#pendingOperations;
constructor() {
this.#opened = false;
this.#source = new MediaSource();
this.#source.addEventListener('sourceopen', _ => {
this.#opened = true;
this.#runEventLoop();
}, {once: true});
this.#source.addEventListener('sourceclose', _ => {
this.#runEventLoop();
});
this.#pendingOperations = [];
}
// It'd be better if we could only expose only `handle()`, but unfortunately
// that doesn't yet work with non-worker mse.
get mediaSource() {
return this.#source;
}
#runEventLoop() {
while (this.#pendingOperations.length > 0) {
if (this.#source.readyState == 'closed' && !this.#opened) {
// Waiting for the source to be connected to an element.
return;
}
// If readyState is closed or ended, the operations below will throw.
let op = this.#pendingOperations[0];
try {
switch (op.operationType) {
case MediaSourceOperationTypes.ADD: {
let sb = this.#source.addSourceBuffer(op.sourceBufferType);
op.resolve(new SourceBuffer2(sb));
break;
}
case MediaSourceOperationTypes.REMOVE: {
let sb = op.sourceBuffer.sourceBuffer;
this.#source.removeSourceBuffer(sb);
op.resolve();
break;
}
case MediaSourceOperationTypes.MARK_EOS: {
for (let i = 0; i < this.#source.sourceBuffers.length; ++i) {
let sb = this.#source.sourceBuffers[i];
if (sb.updating) {
sb.addEventListener('updateend', _ => {
this.#runEventLoop();
}, {once: true});
return;
}
}
this.#source.addEventListener('sourceended', _ => {
op.resolve();
}, {once: true});
this.#pendingOperations.shift();
this.#source.endOfStream(op.error);
break;
}
case MediaSourceOperationTypes.SET_DURATION: {
// FIXME: This can trigger `updateend` events if duration triggers
// the removal algorithm in some UA (though no longer allowed by
// spec). A UA provided MediaSource2 API wouldn't allow this though,
// so for now this polyfill just has code above which explodes if
// `updating` is unexpectedly detected in each SourceBuffer.
this.#source.duration = op.duration;
op.resolve();
break;
}
}
} catch (e) {
op.reject(e);
}
this.#pendingOperations.shift();
}
}
addSourceBuffer(type) {
return new Promise((resolvePromise, rejectPromise) => {
this.#pendingOperations.push({
resolve: resolvePromise,
reject: rejectPromise,
operationType: MediaSourceOperationTypes.ADD,
sourceBufferType: type,
});
this.#runEventLoop();
});
}
removeSourceBuffer(sb) {
return new Promise((resolvePromise, rejectPromise) => {
this.#pendingOperations.push({
resolve: resolvePromise,
reject: rejectPromise,
operationType: MediaSourceOperationTypes.REMOVE,
sourceBuffer: sb,
});
this.#runEventLoop();
});
}
endOfStream(error) {
return new Promise((resolvePromise, rejectPromise) => {
this.#pendingOperations.push({
resolve: resolvePromise,
reject: rejectPromise,
operationType: MediaSourceOperationTypes.MARK_EOS,
error: error,
});
this.#runEventLoop();
});
}
setDuration(dur) {
return new Promise((resolvePromise, rejectPromise) => {
this.#pendingOperations.push({
resolve: resolvePromise,
reject: rejectPromise,
operationType: MediaSourceOperationTypes.SET_DURATION,
duration: dur,
});
this.#runEventLoop();
});
}
get duration() {
return this.#source.duration;
}
// Simple passthrough methods.
setLiveSeekableRange(start, end) {
return this.setLiveSeekableRange(start, end);
}
clearLiveSeekableRange() {
return this.clearLiveSeekableRange();
}
static isTypeSupported(type) {
return MediaSource.isTypeSupported(type);
}
static canConstructInDedicatedWorker =
MediaSource.canConstructInDedicatedWorker;
};