-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
avif-sw.js
160 lines (140 loc) · 4 KB
/
avif-sw.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
import dav1d from "dav1d.js";
import {rgba2bmp} from "./bmp";
import {avif2obu, avif2mov} from "./mov";
// Wait for client to become ready.
const waitForClient = {};
// Pending tasks.
const taskById = {};
let taskCounter = 0;
// AV1 decoder context.
let dCtx = null;
function initPolyfill(opts) {
if (!opts.usePolyfill) return Promise.resolve();
return dav1d.create({wasmURL: opts.wasmURL}).then(d => {
dCtx = d;
});
}
function setClientReady(cid) {
if (waitForClient[cid]) {
waitForClient[cid].resolve();
} else {
waitForClient[cid] = {ready: Promise.resolve(), resolve: null};
}
}
function setClientWaiting(cid) {
if (!waitForClient[cid]) {
let resolve = null;
let ready = new Promise(res => { resolve = res; });
waitForClient[cid] = {ready, resolve};
}
}
function resolveTask(taskId, cb) {
const task = taskById[taskId];
if (task) {
task.resolve(cb(task.toBlob));
}
}
function rejectTask(taskId, err) {
const task = taskById[taskId];
if (task) {
task.reject(err);
}
}
function arr2blob(bmpArr) {
return new Blob([bmpArr], {type: "image/bmp"});
}
function nativeDecodeAvif(client, id, avifArr) {
const movArr = avif2mov(avifArr);
client.postMessage({id, type: "avif-mov", data: movArr}, [movArr]);
}
// Synchronous but that should be ok.
function polyfillDecodeAvif(client, id, avifArr) {
const obuArr = avif2obu(avifArr).data;
resolveTask(id, toBlob => {
if (toBlob) {
// console.time("dav1d "+id);
const bmpArr = dCtx.unsafeDecodeFrameAsBMP(obuArr);
// console.timeEnd("dav1d "+id);
const blob = arr2blob(bmpArr);
dCtx.unsafeCleanup();
return blob;
} else {
// Will be transfered so ok to copy.
return dCtx.decodeFrameAsBMP(obuArr);
}
});
}
function decodeAvif(client, id, avifArr) {
return waitForClient[client.id].ready.then(() => {
return dCtx ? polyfillDecodeAvif(client, id, avifArr)
: nativeDecodeAvif(client, id, avifArr);
});
}
// Handle job messages.
self.addEventListener("message", e => {
const msg = e.data;
if (!msg) return;
switch (msg.type) {
// Client asks for our update
case "avif-update":
skipWaiting();
break;
// Client asks to activate us right away
case "avif-claim":
clients.claim();
break;
// Client is ready
case "avif-ready":
initPolyfill(msg.data).then(() => setClientReady(e.source.id));
break;
// Client sent task result
case "avif-rgba":
const bmpArr = rgba2bmp(msg.data, msg.width, msg.height);
resolveTask(msg.id, toBlob => toBlob ? arr2blob(bmpArr) : bmpArr);
break;
// Client sent task error
case "avif-error":
rejectTask(msg.id, new Error(msg.data));
break;
// Client sent task request
case "avif-task":
const client = e.source;
const id = msg.id;
new Promise((resolve, reject) => {
taskById[id] = {resolve, reject, toBlob: false};
decodeAvif(client, id, msg.data);
}).then(bmpArr => {
delete taskById[id];
client.postMessage({id, type: "avif-task", data: bmpArr}, [bmpArr]);
}, err => {
delete taskById[id];
client.postMessage({id, type: "avif-error", data: err.message});
});
break;
}
});
// Handle AVIF requests.
// TODO(Kagami): Error reporting?
self.addEventListener("fetch", e => {
// TODO(Kagami): Better check for AVIF. HTTP headers?
if (e.request.url.match(/\.avif$/i)) {
const id = taskCounter++;
setClientWaiting(e.clientId);
e.respondWith(new Promise((resolve, reject) => {
taskById[id] = {resolve, reject, toBlob: true};
clients.get(e.clientId).then(client =>
// TODO(Kagami): Apply request headers?
fetch(e.request.url, {credentials: "same-origin"})
.then(res => res.arrayBuffer())
.then(avifArr => decodeAvif(client, id, avifArr))
).catch(reject);
}).then(blob => {
delete taskById[id];
// TODO(Kagami): Apply response metadata?
return new Response(blob);
}, err => {
delete taskById[id];
throw err;
}));
}
});