-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathworker-getinputstream.js
319 lines (292 loc) · 9.89 KB
/
worker-getinputstream.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
'use strict';
/**
* Worker that produces a stream of VideoFrame.
*
* Produced stream represents a Nyan-Cat-like animation featuring the W3C logo.
* This is done by preparing individual frames in a canvas and by creating a
* VideoFrame object out of it.
*
* Frames are produced at the requested frame rate if possible. Backpressure
* signals received from downstream will slow generation and may mean that some
* frames cannot be generated in time or at all.
*/
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
let writer;
let intervalId;
let started = false;
// TEMP: VideoFrames sent through a TransformStream are serialized (and thus
// cloned) and not transferred for now. This means that they need to be closed
// on both ends, in particular when TransformStream sits across workers.
// Unfortunately, they cannot be closed right away on the sender's end because
// the receiver may not yet have received them. Workaround is to close them at
// the end of the processing.
// For additional context, see https://github.com/whatwg/streams/issues/1187
const framesToClose = {};
self.addEventListener('message', async function (e) {
if (e.data.type === 'start') {
if (started) return;
started = true;
// Configuration
const config = e.data.config;
const width = config.width;
const height = config.height;
const frameRate = config.frameRate || 25;
const frameDuration = Math.round(1000 / frameRate);
const writableStream = e.data.stream;
writer = writableStream.getWriter();
// Create the canvas onto which we'll generate our frame drawing
const inputCanvas = new OffscreenCanvas(width, height);
const inputCtx = inputCanvas.getContext('2d', { alpha: false });
// Rough animation speed control
// TODO: make animation independent of frame rate
const nbFramesBeforeNextMove = 2;
// Stars
const starRectWidth = 10;
const starRectHeight = 10;
const starPhases = [
{
rectangles: [
{ x: 0, y: 0 }
]
},
{
rectangles: [
{ x: 0 - starRectWidth, y: 0 },
{ x: starRectWidth, y: 0 },
{ x: 0, y: 0 - starRectHeight },
{ x: 0, y: starRectHeight }
]
},
{
rectangles: [
{ x: 0 - starRectWidth, y: 0 },
{ x: starRectWidth, y: 0 },
{ x: 0, y: 0 - starRectHeight },
{ x: 0, y: starRectHeight },
{ x: 0 - 2 * starRectWidth, y: 0 },
{ x: 2 * starRectWidth, y: 0 },
{ x: 0, y: 0 - 2 * starRectHeight },
{ x: 0, y: 2 * starRectHeight }
]
},
{
rectangles: [
{ x: 0, y: 0 },
{ x: -2 * starRectWidth, y: 0 },
{ x: 2 * starRectWidth, y: 0 },
{ x: 0, y: -2 * starRectHeight },
{ x: 0, y: 2 * starRectHeight },
{ x: -3 * starRectWidth, y: 0 },
{ x: 3 * starRectWidth, y: 0 },
{ x: 0, y: -3 * starRectHeight },
{ x: 0, y: 3 * starRectHeight }
]
},
{
rectangles: [
{ x: 0, y: 0 },
{ x: -3 * starRectWidth, y: 0 },
{ x: 3 * starRectWidth, y: 0 },
{ x: 0, y: -3 * starRectHeight },
{ x: 0, y: 3 * starRectHeight }
]
},
{
rectangles: [
{ x: -3 * starRectWidth, y: 0 },
{ x: 3 * starRectWidth, y: 0 },
{ x: 0, y: -3 * starRectHeight },
{ x: 0, y: 3 * starRectHeight },
{ x: -2.5 * starRectWidth, y: starRectHeight },
{ x: 2.5 * starRectWidth, y: starRectHeight },
{ x: -2.5 * starRectWidth, y: 0 - starRectHeight },
{ x: 2.5 * starRectWidth, y: 0 - starRectHeight },
{ x: starRectWidth, y: -2.5 * starRectHeight },
{ x: starRectWidth, y: 2.5 * starRectHeight },
{ x: 0 - starRectWidth, y: -2.5 * starRectHeight },
{ x: 0 - starRectWidth, y: 2.5 * starRectHeight }
]
},
{
rectangles: [
{ x: -3 * starRectWidth, y: 0 },
{ x: 3 * starRectWidth, y: 0 },
{ x: 0, y: -3 * starRectHeight },
{ x: 0, y: 3 * starRectHeight },
{ x: -2.5 * starRectWidth, y: starRectHeight },
{ x: 2.5 * starRectWidth, y: starRectHeight },
{ x: -2.5 * starRectWidth, y: 0 - starRectHeight },
{ x: 2.5 * starRectWidth, y: 0 - starRectHeight },
{ x: starRectWidth, y: -2.5 * starRectHeight },
{ x: starRectWidth, y: 2.5 * starRectHeight },
{ x: 0 - starRectWidth, y: -2.5 * starRectHeight },
{ x: 0 - starRectWidth, y: 2.5 * starRectHeight },
{ x: -2 * starRectWidth, y: -2 * starRectHeight },
{ x: -2 * starRectWidth, y: 2 * starRectHeight },
{ x: 2 * starRectWidth, y: -2 * starRectHeight },
{ x: 2 * starRectWidth, y: 2 * starRectHeight }
]
}
];
const nbStars = 20;
const nbStarRows = 4;
const nbStarsPerRow = nbStars / nbStarRows;
const stars = [];
for (let i = 0; i < nbStars; i++) {
const col = i % nbStarsPerRow;
let row = Math.floor(i / nbStarsPerRow);
const colWidth = width / nbStarsPerRow;
const rowHeight = height / nbStarRows;
stars[i] = {
x: col * (width + 3 * starRectWidth) / nbStarsPerRow +
colWidth / 2 + getRandomInt(-colWidth / 4, colWidth / 4),
y: row * height / nbStarRows +
rowHeight / 2 + getRandomInt(-rowHeight / 8, rowHeight / 8),
phase: getRandomInt(0, starPhases.length),
pause: 0
}
}
const starVelocity = -20;
// Rainbow
const rainbow = {
x: width / 3,
y: height / 2,
phase: 0,
pause: 0
};
const rainbowStripeWidth = 80;
const rainbowStripeHeight = 20;
const rainbowColors = [
'#f00',
'#f90',
'#ff0',
'#8fb',
'#09f',
'#63f'
];
function timestampToVideoFrame(timestamp) {
// W3C blue! No, green FTW!
//inputCtx.fillStyle = '#005a9c';
inputCtx.fillStyle = '#009c02';
inputCtx.fillRect(0, 0, width, height);
// Render timestamp
inputCtx.fillStyle = 'white';
inputCtx.font = '32px Arial';
inputCtx.fillText(`Timestamp: ${timestamp}`, 10, 42);
// Render the stars
stars.forEach(star => {
inputCtx.fillStyle = 'white';
starPhases[star.phase].rectangles.forEach(rect => {
inputCtx.fillRect(
star.x + rect.x,
star.y + rect.y,
starRectWidth,
starRectHeight
);
});
if (star.pause < nbFramesBeforeNextMove) {
star.pause += 1;
}
else {
star.phase = (star.phase + 1) % starPhases.length;
star.pause = 0;
}
star.x += starVelocity;
if (star.x < -3 * starRectWidth) {
star.x = width + 3 * starRectWidth;
}
});
// Render the rainbow
const stripe = {
x: rainbow.x - rainbowStripeWidth,
y: rainbow.y - 3 * rainbowStripeHeight,
phase: rainbow.phase
};
while (stripe.x > 0 - rainbowStripeWidth) {
rainbowColors.forEach(color => {
inputCtx.fillStyle = color;
inputCtx.fillRect(
stripe.x,
stripe.y + ((stripe.phase === 1) ? rainbowStripeHeight / 4 : 0),
rainbowStripeWidth,
rainbowStripeHeight
);
stripe.y += rainbowStripeHeight;
});
stripe.x -= rainbowStripeWidth;
stripe.y = rainbow.y - 3 * rainbowStripeHeight;
stripe.phase = (stripe.phase + 1) % 2;
}
if (rainbow.pause < 2 * nbFramesBeforeNextMove) {
rainbow.pause += 1;
}
else {
rainbow.phase = (rainbow.phase + 1) % 2;
rainbow.pause = 0;
}
// Render W3C icon next to rainbow
inputCtx.drawImage(config.icon, rainbow.x, rainbow.y - 192/2 + ((rainbow.phase === 1) ? rainbowStripeHeight / 4 : 0));
return new VideoFrame(inputCanvas, {
timestamp: timestamp * 1000,
alpha: 'discard'
});
}
// Produce VideoFrames roughly every frameDuration milliseconds, taking
// backpressure into account.
let startTimestamp = performance.now();
let previousTimestamp = 0;
async function writeVideoFrame() {
if (!started) return;
const writeStart = performance.now();
// Cater for backpressure
await writer.ready;
if (!started) return;
// Create and write next VideoFrame
// (unless function was called within 0ms of the previous call)
const timestamp = Math.round(performance.now() - startTimestamp);
if (timestamp > previousTimestamp) {
const frame = timestampToVideoFrame(timestamp);
// TEMP: VideoFrame close hack
if (config.closeHack) {
framesToClose[frame.timestamp] = frame;
}
writer.write(frame);
}
// Next VideoFrame is due in xx ms
let sleep = frameDuration - Math.round(performance.now() - writeStart);
// ... but if previous generation took a bit longer, let's compensate
// (not taking skipped frames into account)
if ((timestamp - previousTimestamp) > frameDuration) {
sleep -= (timestamp - previousTimestamp) % frameDuration;
}
// Back to the future is only for Doc'
if (sleep < 0) {
sleep = 0;
}
// Schedule next VideoFrame generation
setTimeout(writeVideoFrame, sleep);
previousTimestamp = timestamp;
}
writeVideoFrame();
}
else if (e.data.type === 'stop') {
if (!started) return;
started = false;
if (writer) {
writer.close();
writer = null;
}
}
// TEMP: VideoFrame close hack
else if (e.data.type === 'closeframe') {
const frame = framesToClose[e.data.timestamp];
if (frame) {
frame.close();
delete framesToClose[e.data.timestamp];
}
}
});