web/remux: improve page <-> worker messaging

This commit is contained in:
wukko 2025-01-17 01:16:51 +06:00
parent 8e9347b4a0
commit 28eb9ebe5d
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
2 changed files with 33 additions and 21 deletions

View file

@ -2,16 +2,20 @@ import mime from "mime";
import LibAVWrapper from "$lib/libav"; import LibAVWrapper from "$lib/libav";
const error = (code: string) => { const error = (code: string) => {
return { self.postMessage({
error: `error.${code}`, cobaltRemuxWorker: {
} error: `error.${code}`,
}
})
} }
const ff = new LibAVWrapper((progress) => { const ff = new LibAVWrapper((progress) => {
self.postMessage({ self.postMessage({
progress: { cobaltRemuxWorker: {
durationProcessed: progress.out_time_sec, progress: {
speed: progress.speed, durationProcessed: progress.out_time_sec,
speed: progress.speed,
}
} }
}) })
}); });
@ -29,18 +33,21 @@ const remux = async (file: File) => {
console.error("uh oh! out of memory"); console.error("uh oh! out of memory");
console.error(e); console.error(e);
self.postMessage(error("remux.out_of_resources")); error("remux.out_of_resources");
self.close();
} }
}); });
if (!file_info?.format) { if (!file_info?.format) {
self.postMessage(error("remux.corrupted")); error("remux.corrupted");
return; return;
} }
self.postMessage({ self.postMessage({
progressInfo: { cobaltRemuxWorker: {
duration: Number(file_info.format.duration), progressInfo: {
duration: Number(file_info.format.duration),
}
} }
}); });
@ -59,7 +66,7 @@ const remux = async (file: File) => {
console.error("uh-oh! render error"); console.error("uh-oh! render error");
console.error(e); console.error(e);
self.postMessage(error("remux.out_of_resources")); error("remux.out_of_resources");
}); });
if (!render) { if (!render) {
@ -72,8 +79,10 @@ const remux = async (file: File) => {
const filename = `${filenameParts.join(".")} (remux).${filenameExt}`; const filename = `${filenameParts.join(".")} (remux).${filenameExt}`;
self.postMessage({ self.postMessage({
render, cobaltRemuxWorker: {
filename render,
filename
}
}); });
} catch (e) { } catch (e) {
console.log(e); console.log(e);

View file

@ -45,10 +45,13 @@
}; };
worker.onmessage = (event) => { worker.onmessage = (event) => {
console.log(event.data); const eventData = event.data.cobaltRemuxWorker;
if (!eventData) return;
if (event.data.progress) { console.log(eventData);
let eprogress = event.data.progress;
if (eventData.progress) {
let eprogress = eventData.progress;
if (eprogress?.speed) { if (eprogress?.speed) {
speed = eprogress.speed; speed = eprogress.speed;
@ -72,17 +75,17 @@
console.log(eprogress, progress, speed, currentProgress); console.log(eprogress, progress, speed, currentProgress);
} }
if (event.data.render) { if (eventData.render) {
processing = false; processing = false;
worker.terminate(); worker.terminate();
return downloadFile({ return downloadFile({
file: new File([event.data.render], event.data.filename, { file: new File([eventData.render], eventData.filename, {
type: event.data.render.type, type: eventData.render.type,
}), }),
}); });
} }
if (event.data.error) { if (eventData.error) {
processing = false; processing = false;
worker.terminate(); worker.terminate();
@ -90,7 +93,7 @@
id: "remux-error", id: "remux-error",
type: "small", type: "small",
meowbalt: "error", meowbalt: "error",
bodyText: $t(event.data.error), bodyText: $t(eventData.error),
buttons: [ buttons: [
{ {
text: $t("button.gotit"), text: $t("button.gotit"),