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";
const error = (code: string) => {
return {
error: `error.${code}`,
}
self.postMessage({
cobaltRemuxWorker: {
error: `error.${code}`,
}
})
}
const ff = new LibAVWrapper((progress) => {
self.postMessage({
progress: {
durationProcessed: progress.out_time_sec,
speed: progress.speed,
cobaltRemuxWorker: {
progress: {
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(e);
self.postMessage(error("remux.out_of_resources"));
error("remux.out_of_resources");
self.close();
}
});
if (!file_info?.format) {
self.postMessage(error("remux.corrupted"));
error("remux.corrupted");
return;
}
self.postMessage({
progressInfo: {
duration: Number(file_info.format.duration),
cobaltRemuxWorker: {
progressInfo: {
duration: Number(file_info.format.duration),
}
}
});
@ -59,7 +66,7 @@ const remux = async (file: File) => {
console.error("uh-oh! render error");
console.error(e);
self.postMessage(error("remux.out_of_resources"));
error("remux.out_of_resources");
});
if (!render) {
@ -72,8 +79,10 @@ const remux = async (file: File) => {
const filename = `${filenameParts.join(".")} (remux).${filenameExt}`;
self.postMessage({
render,
filename
cobaltRemuxWorker: {
render,
filename
}
});
} catch (e) {
console.log(e);

View file

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