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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/* Partytown 0.5.4 - MIT builder.io */
(self => {
const [getter, setter, callMethod, constructGlobal, definePrototypePropertyDescriptor, randomId, WinIdKey, InstanceIdKey, ApplyPathKey] = self.$bridgeToMedia$;
delete self.$bridgeToMedia$;
const ContextKey = Symbol();
const MediaSourceKey = Symbol();
const ReadyStateKey = Symbol();
const SourceBuffersKey = Symbol();
const SourceBufferTasksKey = Symbol();
const TimeRangesKey = Symbol();
const EMPTY_ARRAY = [];
const defineCstr = (win, cstrName, Cstr) => win[cstrName] = defineCstrName(cstrName, Cstr);
const defineCstrName = (cstrName, Cstr) => Object.defineProperty(Cstr, "name", {
value: cstrName
});
const initCanvas = (WorkerBase, win) => {
const HTMLCanvasDescriptorMap = {
getContext: {
value(contextType, contextAttributes) {
this[ContextKey] || (this[ContextKey] = (contextType.includes("webgl") ? createContextWebGL : createContext2D)(this, contextType, contextAttributes));
return this[ContextKey];
}
}
};
const WorkerCanvasGradient = defineCstr(win, "CanvasGradient", class extends WorkerBase {
addColorStop(...args) {
callMethod(this, [ "addColorStop" ], args, 2);
}
});
const WorkerCanvasPattern = defineCstr(win, "CanvasPattern", class extends WorkerBase {
setTransform(...args) {
callMethod(this, [ "setTransform" ], args, 2);
}
});
const createContext2D = (canvasInstance, contextType, contextAttributes) => {
const winId = canvasInstance[WinIdKey];
const ctxInstanceId = randomId();
const ctxInstance = {
[WinIdKey]: winId,
[InstanceIdKey]: ctxInstanceId,
[ApplyPathKey]: []
};
const ctx = callMethod(canvasInstance, [ "getContext" ], [ contextType, contextAttributes ], 1, ctxInstanceId);
const ctx2dGetterMethods = "getContextAttributes,getImageData,getLineDash,getTransform,isPointInPath,isPointInStroke,measureText".split(",");
const CanvasRenderingContext2D = {
get: (target, propName) => "string" == typeof propName && propName in ctx ? "function" == typeof ctx[propName] ? (...args) => {
if (propName.startsWith("create")) {
const instanceId = randomId();
callMethod(ctxInstance, [ propName ], args, 2, instanceId);
if ("createImageData" === propName || "createPattern" === propName) {
(api => {
console.warn(`${api} not implemented`);
})(`${propName}()`);
return {
setTransform: () => {}
};
}
return new WorkerCanvasGradient(winId, instanceId);
}
const methodCallType = ctx2dGetterMethods.includes(propName) ? 1 : 2;
return callMethod(ctxInstance, [ propName ], args, methodCallType);
} : ctx[propName] : target[propName],
set(target, propName, value) {
if ("string" == typeof propName && propName in ctx) {
ctx[propName] !== value && "function" != typeof value && setter(ctxInstance, [ propName ], value);
ctx[propName] = value;
} else {
target[propName] = value;
}
return true;
}
};
return new Proxy(ctx, CanvasRenderingContext2D);
};
const createContextWebGL = (canvasInstance, contextType, contextAttributes) => {
const winId = canvasInstance[WinIdKey];
const ctxInstanceId = randomId();
const ctxInstance = {
[WinIdKey]: winId,
[InstanceIdKey]: ctxInstanceId,
[ApplyPathKey]: []
};
const ctx = callMethod(canvasInstance, [ "getContext" ], [ contextType, contextAttributes ], 1, ctxInstanceId);
const WebGLRenderingContextHandler = {
get: (target, propName) => "string" == typeof propName ? "function" != typeof ctx[propName] ? ctx[propName] : (...args) => callMethod(ctxInstance, [ propName ], args, getWebGlMethodCallType(propName)) : target[propName],
set(target, propName, value) {
if ("string" == typeof propName && propName in ctx) {
ctx[propName] !== value && "function" != typeof value && setter(ctxInstance, [ propName ], value);
ctx[propName] = value;
} else {
target[propName] = value;
}
return true;
}
};
return new Proxy(ctx, WebGLRenderingContextHandler);
};
const ctxWebGLGetterMethods = "checkFramebufferStatus,makeXRCompatible".split(",");
const getWebGlMethodCallType = methodName => methodName.startsWith("create") || methodName.startsWith("get") || methodName.startsWith("is") || ctxWebGLGetterMethods.includes(methodName) ? 1 : 2;
defineCstr(win, "CanvasGradient", WorkerCanvasGradient);
defineCstr(win, "CanvasPattern", WorkerCanvasPattern);
definePrototypePropertyDescriptor(win.HTMLCanvasElement, HTMLCanvasDescriptorMap);
};
const initMedia = (WorkerBase, WorkerEventTargetProxy, env, win) => {
var _a, _b;
win.Audio = defineCstrName("HTMLAudioElement", class {
constructor(src) {
const audio = env.$createNode$("audio", randomId());
audio.src = src;
return audio;
}
});
const WorkerAudioTrack = class extends WorkerBase {
get enabled() {
return getter(this, [ "enabled" ]);
}
set enabled(value) {
setter(this, [ "enabled" ], value);
}
get id() {
return getter(this, [ "id" ]);
}
get kind() {
return getter(this, [ "kind" ]);
}
get label() {
return getter(this, [ "label" ]);
}
get language() {
return getter(this, [ "language" ]);
}
get sourceBuffer() {
return new WorkerSourceBuffer(this);
}
};
const WorkerAudioTrackList = class {
constructor(mediaElm) {
const winId = mediaElm[WinIdKey];
const instanceId = mediaElm[InstanceIdKey];
const instance = {
addEventListener(...args) {
callMethod(mediaElm, [ "audioTracks", "addEventListener" ], args, 3);
},
getTrackById: (...args) => callMethod(mediaElm, [ "audioTracks", "getTrackById" ], args),
get length() {
return getter(mediaElm, [ "audioTracks", "length" ]);
},
removeEventListener(...args) {
callMethod(mediaElm, [ "audioTracks", "removeEventListener" ], args, 3);
}
};
return new Proxy(instance, {
get: (target, propName) => "number" == typeof propName ? new WorkerAudioTrack(winId, instanceId, [ "audioTracks", propName ]) : target[propName]
});
}
};
const WorkerSourceBufferList = defineCstr(win, "SourceBufferList", class extends Array {
constructor(mediaSource) {
super();
this[MediaSourceKey] = mediaSource;
}
addEventListener(...args) {
callMethod(this[MediaSourceKey], [ "sourceBuffers", "addEventListener" ], args, 3);
}
removeEventListener(...args) {
callMethod(this[MediaSourceKey], [ "sourceBuffers", "removeEventListener" ], args, 3);
}
});
const WorkerSourceBuffer = defineCstr(win, "SourceBuffer", (_b = class extends WorkerEventTargetProxy {
constructor(mediaSource) {
super(mediaSource[WinIdKey], mediaSource[InstanceIdKey], [ "sourceBuffers" ]);
this[_a] = [];
this[MediaSourceKey] = mediaSource;
}
abort() {
const sbIndex = getSourceBufferIndex(this);
callMethod(this, [ sbIndex, "appendWindowStart" ], EMPTY_ARRAY, 1);
}
addEventListener(...args) {
const sbIndex = getSourceBufferIndex(this);
callMethod(this, [ sbIndex, "addEventListener" ], args, 3);
}
appendBuffer(buf) {
this[SourceBufferTasksKey].push([ "appendBuffer", [ buf ], buf ]);
drainSourceBufferQueue(this);
}
get appendWindowStart() {
const sbIndex = getSourceBufferIndex(this);
return getter(this, [ sbIndex, "appendWindowStart" ]);
}
set appendWindowStart(value) {
const sbIndex = getSourceBufferIndex(this);
setter(this, [ sbIndex, "appendWindowStart" ], value);
}
get appendWindowEnd() {
const sbIndex = getSourceBufferIndex(this);
return getter(this, [ sbIndex, "appendWindowEnd" ]);
}
set appendWindowEnd(value) {
const sbIndex = getSourceBufferIndex(this);
setter(this, [ sbIndex, "appendWindowEnd" ], value);
}
get buffered() {
const mediaSource = this[MediaSourceKey];
const sbIndex = getSourceBufferIndex(this);
const timeRanges = new WorkerTimeRanges(mediaSource[WinIdKey], mediaSource[InstanceIdKey], [ "sourceBuffers", sbIndex, "buffered" ]);
return timeRanges;
}
changeType(mimeType) {
const sbIndex = getSourceBufferIndex(this);
callMethod(this, [ sbIndex, "changeType" ], [ mimeType ], 2);
}
get mode() {
const sbIndex = getSourceBufferIndex(this);
return getter(this, [ sbIndex, "mode" ]);
}
set mode(value) {
const sbIndex = getSourceBufferIndex(this);
setter(this, [ sbIndex, "mode" ], value);
}
remove(start, end) {
this[SourceBufferTasksKey].push([ "remove", [ start, end ] ]);
drainSourceBufferQueue(this);
}
removeEventListener(...args) {
const sbIndex = getSourceBufferIndex(this);
callMethod(this, [ sbIndex, "removeEventListener" ], args, 3);
}
get timestampOffset() {
const sbIndex = getSourceBufferIndex(this);
return getter(this, [ sbIndex, "timestampOffset" ]);
}
set timestampOffset(value) {
const sbIndex = getSourceBufferIndex(this);
setter(this, [ sbIndex, "timestampOffset" ], value);
}
get updating() {
const sbIndex = getSourceBufferIndex(this);
return getter(this, [ sbIndex, "updating" ]);
}
}, _a = SourceBufferTasksKey, _b));
const WorkerTimeRanges = defineCstr(win, "TimeRanges", class extends WorkerBase {
start(...args) {
return callMethod(this, [ "start" ], args);
}
end(...args) {
return callMethod(this, [ "end" ], args);
}
get length() {
return getter(this, [ "length" ]);
}
});
const getSourceBufferIndex = sourceBuffer => {
if (sourceBuffer) {
const mediaSource = sourceBuffer[MediaSourceKey];
const sourceBufferList = mediaSource[SourceBuffersKey];
return sourceBufferList.indexOf(sourceBuffer);
}
return -1;
};
const drainSourceBufferQueue = sourceBuffer => {
if (sourceBuffer[SourceBufferTasksKey].length) {
if (!sourceBuffer.updating) {
const task = sourceBuffer[SourceBufferTasksKey].shift();
if (task) {
const sbIndex = getSourceBufferIndex(sourceBuffer);
callMethod(sourceBuffer, [ sbIndex, task[0] ], task[1], 3, void 0, task[2]);
}
}
setTimeout((() => drainSourceBufferQueue(sourceBuffer)), 50);
}
};
const HTMLMediaDescriptorMap = {
buffered: {
get() {
if (!this[TimeRangesKey]) {
this[TimeRangesKey] = new WorkerTimeRanges(this[WinIdKey], this[InstanceIdKey], [ "buffered" ]);
setTimeout((() => {
this[TimeRangesKey] = void 0;
}), 5e3);
}
return this[TimeRangesKey];
}
},
readyState: {
get() {
if (4 === this[ReadyStateKey]) {
return 4;
}
if ("number" != typeof this[ReadyStateKey]) {
this[ReadyStateKey] = getter(this, [ "readyState" ]);
setTimeout((() => {
this[ReadyStateKey] = void 0;
}), 1e3);
}
return this[ReadyStateKey];
}
}
};
defineCstr(win, "MediaSource", class extends WorkerEventTargetProxy {
constructor() {
super(env.$winId$);
this[SourceBuffersKey] = new WorkerSourceBufferList(this);
constructGlobal(this, "MediaSource", EMPTY_ARRAY);
}
get activeSourceBuffers() {
return [];
}
addSourceBuffer(mimeType) {
const sourceBuffer = new WorkerSourceBuffer(this);
this[SourceBuffersKey].push(sourceBuffer);
callMethod(this, [ "addSourceBuffer" ], [ mimeType ]);
return sourceBuffer;
}
clearLiveSeekableRange() {
callMethod(this, [ "clearLiveSeekableRange" ], EMPTY_ARRAY, 2);
}
get duration() {
return getter(this, [ "duration" ]);
}
set duration(value) {
setter(this, [ "duration" ], value);
}
endOfStream(endOfStreamError) {
callMethod(this, [ "endOfStream" ], [ endOfStreamError ], 3);
}
get readyState() {
return getter(this, [ "readyState" ]);
}
removeSourceBuffer(sourceBuffer) {
const index = getSourceBufferIndex(sourceBuffer);
if (index > -1) {
this[SourceBuffersKey].splice(index, 1);
callMethod(this, [ "removeSourceBuffer" ], [ index ], 1);
}
}
setLiveSeekableRange(start, end) {
callMethod(this, [ "setLiveSeekableRange" ], [ start, end ], 2);
}
get sourceBuffers() {
return this[SourceBuffersKey];
}
static isTypeSupported(mimeType) {
if (!isStaticTypeSupported.has(mimeType)) {
const isSupported = callMethod(win, [ "MediaSource", "isTypeSupported" ], [ mimeType ]);
isStaticTypeSupported.set(mimeType, isSupported);
}
return isStaticTypeSupported.get(mimeType);
}
});
const winURL = win.URL = defineCstrName("URL", class extends URL {});
const hasAudioTracks = "audioTracks" in win.HTMLMediaElement.prototype;
if (hasAudioTracks) {
defineCstr(win, "AudioTrackList", WorkerAudioTrackList);
defineCstr(win, "AudioTrack", WorkerAudioTrack);
HTMLMediaDescriptorMap.audioTracks = {
get() {
return new WorkerAudioTrackList(this);
}
};
}
definePrototypePropertyDescriptor(win.HTMLMediaElement, HTMLMediaDescriptorMap);
winURL.createObjectURL = obj => callMethod(win, [ "URL", "createObjectURL" ], [ obj ]);
winURL.revokeObjectURL = obj => callMethod(win, [ "URL", "revokeObjectURL" ], [ obj ]);
};
const isStaticTypeSupported = new Map;
self.$bridgeFromMedia$ = (WorkerBase, WorkerEventTargetProxy, env, win, windowMediaConstructors) => {
windowMediaConstructors.map((mediaCstrName => {
delete win[mediaCstrName];
}));
initCanvas(WorkerBase, win);
initMedia(WorkerBase, WorkerEventTargetProxy, env, win);
};
})(self);
|