forked from Yahweasel/libav.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextern-post.js
98 lines (87 loc) · 3.45 KB
/
extern-post.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
/*
* Copyright (C) 2019-2023 Yahweasel
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
function LibAVGlobe() {
if (typeof globalThis !== "undefined") return globalThis;
else if (typeof self !== "undefined") return self;
return window;
}
if (typeof LibAVFactory !== "undefined")
LibAVGlobe().LibAVFactory = LibAVFactory;
if (/* We're in a worker */
typeof importScripts !== "undefined" &&
/* We haven't explicitly been requested not to load */
(typeof LibAV === "undefined" || !LibAV.nolibavworker) &&
/* We're not being loaded as a thread */
typeof Module === "undefined"
) (function() {
var globe = LibAVGlobe();
var libav;
Promise.all([]).then(function() {
/* We're the primary code for this worker. The host should ask us to
* load immediately. */
return new Promise(function(res, rej) {
onmessage = function(e) {
if (e && e.data && e.data.config) {
if (e.data.config.wasmurl) {
if (!globe.LibAV) globe.LibAV = {};
globe.LibAV.wasmurl = e.data.config.wasmurl;
}
LibAVFactory().then(res).catch(rej);
}
};
});
}).then(function(lib) {
libav = lib;
// Now we're ready for normal messages
onmessage = function(e) {
var id = e.data[0];
var fun = e.data[1];
var args = e.data.slice(2);
var ret = void 0;
var succ = true;
try {
ret = libav[fun].apply(libav, args);
} catch (ex) {
succ = false;
ret = ex.toString() + "\n" + ex.stack;
}
if (succ && typeof ret === "object" && ret !== null && ret.then) {
// Let the promise resolve
ret.then(function(res) {
ret = res;
}).catch(function(ex) {
succ = false;
ret = ex.toString() + "\n" + ex.stack;
}).then(function() {
postMessage([id, fun, succ, ret]);
});
} else {
postMessage([id, fun, succ, ret]);
}
};
libav.onwrite = function(name, pos, buf) {
/* We have to buf.slice(0) so we don't duplicate the entire heap just
* to get one part of it in postMessage */
buf = buf.slice(0);
postMessage(["onwrite", "onwrite", true, [name, pos, buf]], [buf.buffer]);
};
libav.onblockread = function(name, pos, len) {
postMessage(["onblockread", "onblockread", true, [name, pos, len]]);
};
postMessage(["onready", "onready", true, null]);
}).catch(function(ex) {
console.log("Loading LibAV failed\n" + ex + "\n" + ex.stack);
});
})();