@muhkoo/theater-transcoder 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/client.js +10 -4
- package/src/worker.js +20 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@muhkoo/theater-transcoder",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Drain a Muhkoo Theater transcode queue with NATIVE ffmpeg. Run it on any machine signed in as you to add transcoding power to your library.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"access": "public"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@muhkoo/connect": "0.10.
|
|
28
|
+
"@muhkoo/connect": "0.10.2-alpha.0",
|
|
29
29
|
"ffmpeg-static": "^5.2.0",
|
|
30
30
|
"node-datachannel": "^0.12.0",
|
|
31
31
|
"snarkjs": "^0.7.5",
|
package/src/client.js
CHANGED
|
@@ -55,10 +55,16 @@ export async function connect({ baseUrl, appKey, username, password }) {
|
|
|
55
55
|
apiKey: appKey,
|
|
56
56
|
circuits,
|
|
57
57
|
offline: { enabled: false },
|
|
58
|
-
// P2P on: fetch raw shards from a browser peer over WebRTC
|
|
59
|
-
//
|
|
60
|
-
//
|
|
61
|
-
|
|
58
|
+
// P2P on: fetch raw shards from a browser peer over WebRTC instead of
|
|
59
|
+
// round-tripping R2. We must let the default STUN server apply (do NOT pass
|
|
60
|
+
// an empty iceServers — that disables STUN entirely, so WebRTC can't gather
|
|
61
|
+
// server-reflexive candidates and the browser↔Node connection never forms).
|
|
62
|
+
// Origin stays the fallback; the block engine runs main-thread in Node.
|
|
63
|
+
p2p: {
|
|
64
|
+
enabled: true,
|
|
65
|
+
...(process.env.MUHKOO_STUN ? { iceServers: [{ urls: process.env.MUHKOO_STUN }] } : {}),
|
|
66
|
+
debug: process.env.MUHKOO_P2P_DEBUG === "1",
|
|
67
|
+
},
|
|
62
68
|
});
|
|
63
69
|
const user = await client.auth.zk.login(username, password);
|
|
64
70
|
return { client, user };
|
package/src/worker.js
CHANGED
|
@@ -45,11 +45,11 @@ export function startWorker({ client, space, appKey, baseUrl }) {
|
|
|
45
45
|
limit: 100,
|
|
46
46
|
});
|
|
47
47
|
const now = Date.now();
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
));
|
|
48
|
+
const isStale = (j) => j.status === "processing" && (!j.heartbeat_at || now - j.heartbeat_at > STALE_MS);
|
|
49
|
+
// Prefer an unclaimed queued job over reclaiming a stale one, so multiple
|
|
50
|
+
// workers spread across DIFFERENT jobs instead of piling onto the same movie.
|
|
51
|
+
const cand = rows.find((j) => j.input_manifest && j.status === "queued")
|
|
52
|
+
|| rows.find((j) => j.input_manifest && isStale(j));
|
|
53
53
|
if (!cand) return null;
|
|
54
54
|
await patch(cand._id, { status: "processing", worker_id: WORKER_ID, worker_label: WORKER_LABEL, claimed_at: now, heartbeat_at: now });
|
|
55
55
|
const fresh = await jobs().get(cand._id);
|
|
@@ -61,6 +61,10 @@ export function startWorker({ client, space, appKey, baseUrl }) {
|
|
|
61
61
|
async function process(job) {
|
|
62
62
|
activeJobId = job._id;
|
|
63
63
|
abort = new AbortController();
|
|
64
|
+
// Keep the lease alive for the WHOLE job — including the raw download, which
|
|
65
|
+
// for a big file can exceed STALE_MS with no progress events. Without this a
|
|
66
|
+
// peer treats us as dead mid-download and steals the job (double-transcode).
|
|
67
|
+
const beat = setInterval(() => void patch(job._id, { heartbeat_at: Date.now() }).catch(() => {}), 12_000);
|
|
64
68
|
let lastBeat = 0;
|
|
65
69
|
const onProgress = ({ fraction, label }) => {
|
|
66
70
|
const now = Date.now();
|
|
@@ -71,11 +75,18 @@ export function startWorker({ client, space, appKey, baseUrl }) {
|
|
|
71
75
|
}
|
|
72
76
|
};
|
|
73
77
|
try {
|
|
74
|
-
log(`claim job ${job._id} "${job.title}" —
|
|
78
|
+
log(`claim job ${job._id} "${job.title}" — fetching raw (P2P → origin)…`);
|
|
79
|
+
// Surface the fetch stage so the uploader's UI shows "Fetching raw… on the
|
|
80
|
+
// transcoder" instead of a silent stall while the bytes transfer.
|
|
81
|
+
await patch(job._id, { phase: "Fetching raw…", progress: 0 }).catch(() => {});
|
|
82
|
+
await postSignal({ kind: "progress", jobId: job._id, progress: 0, phase: "Fetching raw…", worker: WORKER_LABEL });
|
|
75
83
|
// The DB json column can hand back the manifest as a STRING — parse it, or
|
|
76
|
-
//
|
|
84
|
+
// getFile chokes ("manifest.chunks is not iterable").
|
|
77
85
|
const manifest = typeof job.input_manifest === "string" ? JSON.parse(job.input_manifest) : job.input_manifest;
|
|
78
|
-
|
|
86
|
+
// Read through the Space's getFile (NOT client.storage.readByManifest):
|
|
87
|
+
// only the Space file surface has the P2P peer source wired, so this pulls
|
|
88
|
+
// shards from the uploader's browser over WebRTC first, origin as fallback.
|
|
89
|
+
const { data } = await space.getFile(manifest);
|
|
79
90
|
log(`transcoding ${(data.length / 1e6).toFixed(1)}MB…`);
|
|
80
91
|
const { hls_index, duration, size } = await transcodeToHls({
|
|
81
92
|
rawBytes: data, filename: job.filename || "input.mp4", space, onProgress, signal: abort.signal,
|
|
@@ -92,6 +103,7 @@ export function startWorker({ client, space, appKey, baseUrl }) {
|
|
|
92
103
|
if (abort.signal.aborted) { log(`job ${job._id} stopped remotely`); }
|
|
93
104
|
else { await patch(job._id, { status: "error", error: String(e?.message || e) }).catch(() => {}); log(`✗ job ${job._id} failed:`, e?.message || e); }
|
|
94
105
|
} finally {
|
|
106
|
+
clearInterval(beat);
|
|
95
107
|
activeJobId = null; abort = null;
|
|
96
108
|
}
|
|
97
109
|
}
|