@vestra-player/sdk 1.7.7 → 1.7.8

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.
@@ -270,7 +270,9 @@ export class PlayerService {
270
270
  const id = ++this.#operation;
271
271
  let result;
272
272
  try {
273
- result = await this.runtime.host.source.submit(intent, id);
273
+ result = await (options.send
274
+ ? options.send(id)
275
+ : this.runtime.host.source.submit(intent, id));
274
276
  }
275
277
  catch (error) {
276
278
  if (!uncertain(error))
@@ -1680,8 +1680,99 @@ export declare class TestHost implements Transport {
1680
1680
  }>;
1681
1681
  /** Snapshots taken with `media.capture`, by snapshot id. Wire-level. */
1682
1682
  readonly snapshots: Map<number, Json>;
1683
- /** Every `jobs.start` request (`kind`, `destination`, `quality`, …), newest last. */
1683
+ /**
1684
+ * Every `jobs.start` request (`kind`, `destination`, `quality`, …),
1685
+ * newest last: a snapshot's (`snapshotId`) or, since 1.7.8, an export of
1686
+ * remembered files' parts (`parts`), whose job runs half way through its
1687
+ * middle part (`progress.part`), then completes (a copy's destination gets
1688
+ * the extension of its container).
1689
+ */
1684
1690
  readonly exports: Record<string, Json>[];
1691
+ /**
1692
+ * Local files as the fake player knows them, by path, for
1693
+ * `plugin.media.sources` and exports of their parts. A file not listed is a 1080p 30 fps
1694
+ * H.264 file with AAC stereo 48 kHz sound, as long as the current
1695
+ * source. Set `status` to `missing` or `changed` to stand for a moved or
1696
+ * rewritten file; `video: null` or `audio: null` for a file without one.
1697
+ *
1698
+ * @example A second file that a copy cannot join to the first
1699
+ * The plugin's list gets a part of a 4K file, then one of a 1080p file:
1700
+ * the check then says `size` and the job has to re-encode.
1701
+ * ```ts
1702
+ * declare const host: TestHost;
1703
+ * // ---cut---
1704
+ * host.mediaFiles.set("/media/b.mp4", {
1705
+ * duration: 30,
1706
+ * video: { codec: "h264", width: 1920, height: 818, fps: 30, pixelFormat: "yuv420p" },
1707
+ * });
1708
+ * host.setSource({ phase: "ready", source: "/media/b.mp4", duration: 30 });
1709
+ * ```
1710
+ *
1711
+ * @since 1.7.8
1712
+ */
1713
+ readonly mediaFiles: Map<string, {
1714
+ /** Seconds; default the current source's duration (60 without one). */
1715
+ duration?: number;
1716
+ /** `ok` (default), `missing` or `changed`. */
1717
+ status?: "ok" | "missing" | "changed";
1718
+ /**
1719
+ * Stands for the file's size and modification time (default 0): a
1720
+ * reference made at another version reads `changed`, and
1721
+ * `remember` gives a new token for the file at a new version, as
1722
+ * the player does for a rewritten file.
1723
+ *
1724
+ * @since 1.7.8
1725
+ */
1726
+ version?: number;
1727
+ /** The video stream; `null` for none. */
1728
+ video?: {
1729
+ /** FFmpeg's codec name. */
1730
+ codec: string;
1731
+ /** Pixels. */
1732
+ width: number;
1733
+ /** Pixels. */
1734
+ height: number;
1735
+ /** Frames per second. */
1736
+ fps: number;
1737
+ /** FFmpeg's pixel format. */
1738
+ pixelFormat: string | null;
1739
+ } | null;
1740
+ /** The audio stream; `null` for none. */
1741
+ audio?: {
1742
+ /** FFmpeg's codec name. */
1743
+ codec: string;
1744
+ /** Samples per second. */
1745
+ sampleRate: number;
1746
+ /** Channel count. */
1747
+ channels: number;
1748
+ } | null;
1749
+ }>;
1750
+ /**
1751
+ * Every `plugin.media.sources.open` the plugin made (its token and
1752
+ * start, 0 for the beginning), newest last. Each opens the token's path
1753
+ * as an ordinary open: `source` becomes that path, `position` the start.
1754
+ *
1755
+ * @since 1.7.8
1756
+ */
1757
+ readonly sourceOpens: {
1758
+ /** The token opened. */
1759
+ token: string;
1760
+ /** Seconds it started at. */
1761
+ start: number;
1762
+ }[];
1763
+ /**
1764
+ * The plugin's source references (`plugin.media.sources`) by token: the
1765
+ * path each stands for. `locate` takes its answer from
1766
+ * {@link TestHost.dialogAnswers}. Wire-level.
1767
+ *
1768
+ * @since 1.7.8
1769
+ */
1770
+ readonly sourceReferences: Map<string, {
1771
+ /** The file's path (never shown to the plugin). */
1772
+ path: string;
1773
+ /** The file's `mediaFiles` version when it was remembered. */
1774
+ version?: number;
1775
+ }>;
1685
1776
  /**
1686
1777
  * Export jobs by job id. Each state query advances a job one step
1687
1778
  * (`queued` → `running` → `completed`; `cancelling` → `cancelled`).
@@ -3011,8 +3102,14 @@ export declare class TestHost implements Transport {
3011
3102
  *
3012
3103
  * The manifest id and capabilities come from `directory/manifest.json`
3013
3104
  * unless `capabilities` is given; then `id` (default `"test.plugin"`) and
3014
- * `capabilities` are used and no manifest is read. Calls that need an
3015
- * undeclared capability fail with `capability`, as in the player.
3105
+ * `capabilities` are used and no manifest is read. The test host does not
3106
+ * apply the player's capability table to every request: the members that
3107
+ * check a capability on the plugin's side (`plugin.media.sources` and its
3108
+ * `open`, which also needs `playback.control`, `plugin.secrets`, timeline
3109
+ * marks, video shaders and views, among others) fail with `capability` as in
3110
+ * the player, but a call the player refuses for an undeclared capability can
3111
+ * succeed here. `vestra-plugin check` (and the TypeScript plugin) reports
3112
+ * those calls from the sources.
3016
3113
  *
3017
3114
  * Rejects when `manifest.json` cannot be read or parsed, when `activate`
3018
3115
  * throws, or when a registration made during activation fails (for example
@@ -1238,8 +1238,53 @@ export class TestHost {
1238
1238
  uploads = new Map();
1239
1239
  /** Snapshots taken with `media.capture`, by snapshot id. Wire-level. */
1240
1240
  snapshots = new Map();
1241
- /** Every `jobs.start` request (`kind`, `destination`, `quality`, …), newest last. */
1241
+ /**
1242
+ * Every `jobs.start` request (`kind`, `destination`, `quality`, …),
1243
+ * newest last: a snapshot's (`snapshotId`) or, since 1.7.8, an export of
1244
+ * remembered files' parts (`parts`), whose job runs half way through its
1245
+ * middle part (`progress.part`), then completes (a copy's destination gets
1246
+ * the extension of its container).
1247
+ */
1242
1248
  exports = [];
1249
+ /**
1250
+ * Local files as the fake player knows them, by path, for
1251
+ * `plugin.media.sources` and exports of their parts. A file not listed is a 1080p 30 fps
1252
+ * H.264 file with AAC stereo 48 kHz sound, as long as the current
1253
+ * source. Set `status` to `missing` or `changed` to stand for a moved or
1254
+ * rewritten file; `video: null` or `audio: null` for a file without one.
1255
+ *
1256
+ * @example A second file that a copy cannot join to the first
1257
+ * The plugin's list gets a part of a 4K file, then one of a 1080p file:
1258
+ * the check then says `size` and the job has to re-encode.
1259
+ * ```ts
1260
+ * declare const host: TestHost;
1261
+ * // ---cut---
1262
+ * host.mediaFiles.set("/media/b.mp4", {
1263
+ * duration: 30,
1264
+ * video: { codec: "h264", width: 1920, height: 818, fps: 30, pixelFormat: "yuv420p" },
1265
+ * });
1266
+ * host.setSource({ phase: "ready", source: "/media/b.mp4", duration: 30 });
1267
+ * ```
1268
+ *
1269
+ * @since 1.7.8
1270
+ */
1271
+ mediaFiles = new Map();
1272
+ /**
1273
+ * Every `plugin.media.sources.open` the plugin made (its token and
1274
+ * start, 0 for the beginning), newest last. Each opens the token's path
1275
+ * as an ordinary open: `source` becomes that path, `position` the start.
1276
+ *
1277
+ * @since 1.7.8
1278
+ */
1279
+ sourceOpens = [];
1280
+ /**
1281
+ * The plugin's source references (`plugin.media.sources`) by token: the
1282
+ * path each stands for. `locate` takes its answer from
1283
+ * {@link TestHost.dialogAnswers}. Wire-level.
1284
+ *
1285
+ * @since 1.7.8
1286
+ */
1287
+ sourceReferences = new Map();
1243
1288
  /**
1244
1289
  * Export jobs by job id. Each state query advances a job one step
1245
1290
  * (`queued` → `running` → `completed`; `cancelling` → `cancelled`).
@@ -2640,8 +2685,14 @@ export class TestHost {
2640
2685
  *
2641
2686
  * The manifest id and capabilities come from `directory/manifest.json`
2642
2687
  * unless `capabilities` is given; then `id` (default `"test.plugin"`) and
2643
- * `capabilities` are used and no manifest is read. Calls that need an
2644
- * undeclared capability fail with `capability`, as in the player.
2688
+ * `capabilities` are used and no manifest is read. The test host does not
2689
+ * apply the player's capability table to every request: the members that
2690
+ * check a capability on the plugin's side (`plugin.media.sources` and its
2691
+ * `open`, which also needs `playback.control`, `plugin.secrets`, timeline
2692
+ * marks, video shaders and views, among others) fail with `capability` as in
2693
+ * the player, but a call the player refuses for an undeclared capability can
2694
+ * succeed here. `vestra-plugin check` (and the TypeScript plugin) reports
2695
+ * those calls from the sources.
2645
2696
  *
2646
2697
  * Rejects when `manifest.json` cannot be read or parsed, when `activate`
2647
2698
  * throws, or when a registration made during activation fails (for example
@@ -41,6 +41,19 @@ export declare class Internals {
41
41
  end: number;
42
42
  container: "mp4" | "mkv" | null;
43
43
  } | null;
44
+ /** An export of parts (`jobs.start { parts }`): its part count, length and container. */
45
+ join?: {
46
+ /** Each part's token (its file's status decides the outcome). */
47
+ tokens: string[];
48
+ parts: number;
49
+ total: number;
50
+ container: "mp4" | "mkv";
51
+ written: {
52
+ start: number;
53
+ end: number;
54
+ }[];
55
+ mode: "copy" | "mp4";
56
+ };
44
57
  }>;
45
58
  sentTypes: string;
46
59
  readonly overrides: Map<string, (params: Record<string, Json>) => unknown>;
@@ -11,6 +11,7 @@ import { randomBytes } from "node:crypto";
11
11
  import { HostError } from "../../wire.js";
12
12
  import { localSocketPath } from "../local.js";
13
13
  import { isCurrentProducer } from "./media.js";
14
+ import { MEDIA_SOURCE_LIMITS } from "../media.js";
14
15
  /** `jobs.start`'s `encoding`, checked against the player's sets. */
15
16
  export function validEncoding(value) {
16
17
  if (typeof value !== "object" || value === null || Array.isArray(value))
@@ -42,7 +43,158 @@ export function failStream(host, inner, producerId, message) {
42
43
  }
43
44
  /** The default handlers of this area's wire methods, by method name. */
44
45
  export function jobsHandlers(host, inner) {
45
- return {
46
+ /** A file's facts: `mediaFiles`, else a 1080p30 H.264 + AAC file. */
47
+ const facts = (path) => {
48
+ const file = host.mediaFiles.get(path) ?? {};
49
+ return {
50
+ duration: file.duration ?? host.source.duration ?? 60,
51
+ status: file.status ?? "ok",
52
+ video: file.video === undefined
53
+ ? {
54
+ codec: "h264",
55
+ width: 1920,
56
+ height: 1080,
57
+ fps: 30,
58
+ pixelFormat: "yuv420p",
59
+ }
60
+ : file.video,
61
+ audio: file.audio === undefined
62
+ ? { codec: "aac", sampleRate: 48000, channels: 2 }
63
+ : file.audio,
64
+ };
65
+ };
66
+ const known = (token) => {
67
+ const value = String(token);
68
+ if (!host.sourceReferences.has(value))
69
+ throw new HostError("missing", "no such source reference");
70
+ return value;
71
+ };
72
+ /** A file's version: what the player's size and modification time are. */
73
+ const version = (path) => host.mediaFiles.get(path)?.version ?? 0;
74
+ /** As the player: gone, rewritten since remembered, or as it was. */
75
+ const status = (token) => {
76
+ const { path, version: was } = host.sourceReferences.get(token);
77
+ const file = facts(path);
78
+ if (file.status === "missing")
79
+ return "missing";
80
+ return file.status === "changed" || version(path) !== (was ?? 0)
81
+ ? "changed"
82
+ : "ok";
83
+ };
84
+ const describe = (token) => {
85
+ const { path } = host.sourceReferences.get(token);
86
+ const file = facts(path);
87
+ return {
88
+ token,
89
+ name: path.split(/[/\\]/).pop() ?? path,
90
+ duration: file.duration,
91
+ status: status(token),
92
+ video: file.video,
93
+ audio: file.audio,
94
+ };
95
+ };
96
+ /** The player's `media_copy::fits_mp4`: codecs a copy writes into MP4. */
97
+ const fitsMp4 = (file) => ["h264", "hevc", "av1", "mpeg4"].includes(file?.video?.codec ?? "") &&
98
+ (!file?.audio ||
99
+ ["aac", "mp3", "ac3", "eac3", "alac"].includes(file.audio.codec));
100
+ // The player's check (`media_sources::check`) over the fake facts.
101
+ const exportCheck = (parts) => {
102
+ // The player's bounds (`media_sources::parts_valid`).
103
+ if (!Array.isArray(parts) ||
104
+ parts.length === 0 ||
105
+ parts.length > MEDIA_SOURCE_LIMITS.parts ||
106
+ parts.some((part) => typeof part?.source !== "string" ||
107
+ part.source === "" ||
108
+ !Number.isFinite(part.start) ||
109
+ !Number.isFinite(part.end) ||
110
+ part.start < 0 ||
111
+ part.end > MEDIA_SOURCE_LIMITS.partEnd) ||
112
+ parts.reduce((sum, part) => sum + Math.max(0, part.end - part.start), 0) >
113
+ MEDIA_SOURCE_LIMITS.totalSeconds)
114
+ throw new HostError("invalid", `1–${MEDIA_SOURCE_LIMITS.parts} parts with a token and finite times from 0 to ${MEDIA_SOURCE_LIMITS.partEnd} s (${MEDIA_SOURCE_LIMITS.totalSeconds} s in all) required`);
115
+ const blocking = [];
116
+ const files = parts.map((part, index) => {
117
+ const reference = host.sourceReferences.get(part.source);
118
+ if (!reference) {
119
+ blocking.push({ part: index, problem: "unknown" });
120
+ return null;
121
+ }
122
+ const file = facts(reference.path);
123
+ const now = status(part.source);
124
+ if (now !== "ok")
125
+ blocking.push({ part: index, problem: now });
126
+ if (!file.video)
127
+ blocking.push({ part: index, problem: "noVideo" });
128
+ else if (part.end <= part.start ||
129
+ part.end - part.start < 1 / file.video.fps - 1e-6 ||
130
+ part.end > file.duration + 0.001)
131
+ blocking.push({
132
+ part: index,
133
+ problem: "range",
134
+ duration: file.duration,
135
+ });
136
+ return file;
137
+ });
138
+ const reasons = [];
139
+ const first = files[0]?.video ? files[0] : null;
140
+ const audio = (a) => a ? `${a.codec} ${a.sampleRate} Hz ${a.channels} ch` : "none";
141
+ if (first?.video)
142
+ files.forEach((file, part) => {
143
+ if (part === 0 || !file?.video)
144
+ return;
145
+ const v = file.video;
146
+ const f = first.video;
147
+ if (v.codec !== f.codec)
148
+ reasons.push({
149
+ part,
150
+ field: "videoCodec",
151
+ value: v.codec,
152
+ expected: f.codec,
153
+ });
154
+ if (v.width !== f.width || v.height !== f.height)
155
+ reasons.push({
156
+ part,
157
+ field: "size",
158
+ value: `${v.width}×${v.height}`,
159
+ expected: `${f.width}×${f.height}`,
160
+ });
161
+ if (v.fps !== f.fps)
162
+ reasons.push({ part, field: "fps", value: v.fps, expected: f.fps });
163
+ if (v.pixelFormat !== f.pixelFormat)
164
+ reasons.push({
165
+ part,
166
+ field: "pixelFormat",
167
+ value: v.pixelFormat,
168
+ expected: f.pixelFormat,
169
+ });
170
+ if (audio(file.audio) !== audio(first.audio))
171
+ reasons.push({
172
+ part,
173
+ field: "audio",
174
+ value: audio(file.audio),
175
+ expected: audio(first.audio),
176
+ });
177
+ });
178
+ const mp4 = fitsMp4(first);
179
+ return {
180
+ blocking,
181
+ copy: reasons.length > 0 || !first
182
+ ? { ok: false, reasons }
183
+ : { ok: true, container: mp4 ? "mp4" : "mkv" },
184
+ encode: first?.video
185
+ ? {
186
+ width: first.video.width + (first.video.width % 2),
187
+ height: first.video.height + (first.video.height % 2),
188
+ fps: first.video.fps,
189
+ audio: files.some((file) => file?.audio)
190
+ ? { sampleRate: 48000, channels: 2 }
191
+ : null,
192
+ }
193
+ : null,
194
+ duration: parts.reduce((sum, part) => sum + Math.max(0, part.end - part.start), 0),
195
+ };
196
+ };
197
+ const handlers = {
46
198
  "snapshot.capture": (p) => {
47
199
  const expected = p.expected;
48
200
  if (expected.generation !== host.source.stamp.generation ||
@@ -70,6 +222,8 @@ export function jobsHandlers(host, inner) {
70
222
  "snapshot.state": (p) => host.snapshots.get(Number(p.snapshotId)) ?? null,
71
223
  "snapshot.release": (p) => void host.snapshots.delete(Number(p.snapshotId)),
72
224
  "jobs.start": (p) => {
225
+ if (p.parts !== undefined)
226
+ return startParts(p);
73
227
  if (!host.snapshots.has(Number(p.snapshotId)))
74
228
  throw new HostError("missing", "snapshot released");
75
229
  const kind = String(p.kind);
@@ -124,12 +278,57 @@ export function jobsHandlers(host, inner) {
124
278
  const current = structuredClone(job);
125
279
  const made = inner.exportJobs.get(job.jobId);
126
280
  const clip = made?.clip;
281
+ const join = made?.join;
127
282
  if (job.status === "queued") {
128
283
  job.status = "running";
129
284
  if (clip) {
130
285
  const total = clip.end - clip.start;
131
286
  job.progress = { done: total / 2, total };
132
287
  }
288
+ // A join is half written: in its middle part.
289
+ if (join)
290
+ job.progress = {
291
+ done: join.total / 2,
292
+ total: join.total,
293
+ part: Math.ceil(join.parts / 2),
294
+ parts: join.parts,
295
+ };
296
+ }
297
+ else if (job.status === "running" &&
298
+ join &&
299
+ !host.exportFailure &&
300
+ join.tokens.some((token) => status(token) !== "ok")) {
301
+ // As the player: a part whose file went or changed ends the job.
302
+ const index = join.tokens.findIndex((token) => status(token) !== "ok");
303
+ const gone = status(join.tokens[index]) === "missing";
304
+ Object.assign(job, {
305
+ status: "failed",
306
+ outcome: {
307
+ error: {
308
+ code: gone ? "source-missing" : "source-changed",
309
+ message: `part ${index + 1} ${gone ? "is missing" : "changed since it was added"}`,
310
+ },
311
+ },
312
+ });
313
+ }
314
+ else if (job.status === "running" && join && !host.exportFailure) {
315
+ const destination = join.mode === "copy"
316
+ ? made.destination.replace(/(\.(mp4|m4v|mov|mkv|webm))?$/i, `.${join.container}`)
317
+ : made.destination;
318
+ Object.assign(job, {
319
+ status: "completed",
320
+ publicationStarted: true,
321
+ facts: {
322
+ join: {
323
+ mode: join.mode,
324
+ container: join.container,
325
+ parts: join.written,
326
+ target: null,
327
+ },
328
+ },
329
+ outcome: { destination },
330
+ progress: { done: join.total, total: join.total },
331
+ });
133
332
  }
134
333
  else if (job.status === "running" && host.exportFailure)
135
334
  Object.assign(job, {
@@ -175,6 +374,154 @@ export function jobsHandlers(host, inner) {
175
374
  return { jobId: job.jobId, cancel: "requested" };
176
375
  },
177
376
  "jobs.operation": () => ({ status: "unknown" }),
377
+ "media.sources.remember": (p) => {
378
+ const path = host.source.source;
379
+ if (p.expected !== undefined &&
380
+ (p.expected.generation !==
381
+ host.source.stamp.generation ||
382
+ p.expected.intentVersion !==
383
+ host.source.stamp.intentVersion))
384
+ throw new HostError("stale", "source intent changed");
385
+ if (!path ||
386
+ path.includes("://") ||
387
+ !["ready", "ended"].includes(host.source.phase) ||
388
+ host.mediaFiles.get(path)?.status === "missing")
389
+ throw new HostError("unsupported", "only a local file can be referenced");
390
+ // The same file as it is (path, size and time: `version`): its token.
391
+ for (const [token, reference] of host.sourceReferences)
392
+ if (reference.path === path &&
393
+ (reference.version ?? 0) === version(path))
394
+ return describe(token);
395
+ if (host.sourceReferences.size >= MEDIA_SOURCE_LIMITS.references)
396
+ throw new HostError("limit", `a plugin holds at most ${MEDIA_SOURCE_LIMITS.references} references`);
397
+ const token = `src-${randomBytes(16).toString("hex")}`;
398
+ host.sourceReferences.set(token, { path, version: version(path) });
399
+ return describe(token);
400
+ },
401
+ "media.sources.get": (p) => describe(known(p.token)),
402
+ "media.sources.list": () => [...host.sourceReferences.keys()].map(describe),
403
+ "media.sources.forget": (p) => void host.sourceReferences.delete(String(p.token)),
404
+ // The player's own dialog: answered from `dialogAnswers` like a file
405
+ // dialog (a one-path array picks that file, `null` cancels).
406
+ "media.sources.locate": (p) => {
407
+ const token = known(p.token);
408
+ const answer = host.dialogAnswers.shift();
409
+ if (answer === undefined)
410
+ throw new Error("test host: no dialog answer queued");
411
+ if (answer !== null && !Array.isArray(answer))
412
+ throw new Error("test host: a confirm answer was queued");
413
+ if (answer === null)
414
+ return null;
415
+ host.sourceReferences.set(token, {
416
+ path: answer[0],
417
+ version: version(answer[0]),
418
+ });
419
+ return describe(token);
420
+ },
421
+ "jobs.check": (p) => {
422
+ // As the player: parts or a snapshot, one of them.
423
+ if ((p.snapshotId === undefined) === (p.parts === undefined))
424
+ throw new HostError("invalid", "jobs.check takes parts or a snapshotId, one of them");
425
+ if (p.snapshotId === undefined) {
426
+ // Parts carry their own ends; `end` is the snapshot form's.
427
+ if (p.end !== undefined && p.end !== null)
428
+ throw new HostError("invalid", "end belongs to a snapshotId check; parts carry their own end");
429
+ return exportCheck(p.parts);
430
+ }
431
+ // A snapshot's clip: one part from the current file's facts.
432
+ const snapshot = host.snapshots.get(Number(p.snapshotId));
433
+ if (!snapshot)
434
+ throw new HostError("unknown-expired", "snapshot unavailable");
435
+ const end = Number(p.end);
436
+ if (!(end > snapshot.time && end <= snapshot.duration))
437
+ throw new HostError("invalid", "end must be after the snapshot time and within its duration");
438
+ const file = facts(snapshot.source);
439
+ const fps = file.video?.fps ?? 30;
440
+ const processed = snapshot.kind === "processed";
441
+ return {
442
+ blocking: file.video ? [] : [{ part: 0, problem: "noVideo" }],
443
+ copy: processed
444
+ ? {
445
+ ok: false,
446
+ reasons: [
447
+ { part: 0, field: "effects", value: null, expected: null },
448
+ ],
449
+ }
450
+ : { ok: true, container: fitsMp4(file) ? "mp4" : "mkv" },
451
+ // The source's size (even), as the player's target; a processed
452
+ // snapshot's picture is the source's in the test host.
453
+ encode: file.video
454
+ ? {
455
+ width: file.video.width + (file.video.width % 2),
456
+ height: file.video.height + (file.video.height % 2),
457
+ fps,
458
+ audio: file.audio ? { sampleRate: 48000, channels: 2 } : null,
459
+ }
460
+ : null,
461
+ duration: end - snapshot.time,
462
+ };
463
+ },
464
+ };
465
+ /** `jobs.start { parts }`: an export of remembered files' parts. */
466
+ function startParts(p) {
467
+ const parts = p.parts;
468
+ const copy = p.kind === "rawCopy";
469
+ if (!Array.isArray(parts) ||
470
+ parts.length === 0 ||
471
+ parts.length > MEDIA_SOURCE_LIMITS.parts ||
472
+ parts.some((part) => !(part.end > part.start)) ||
473
+ parts.reduce((sum, part) => sum + (part.end - part.start), 0) >
474
+ MEDIA_SOURCE_LIMITS.totalSeconds ||
475
+ !["rawCopy", "rawMp4"].includes(String(p.kind)) ||
476
+ p.snapshotId !== undefined ||
477
+ p.end !== undefined ||
478
+ p.sampleTime !== undefined ||
479
+ p.quality !== undefined ||
480
+ (p.encoding !== undefined && (copy || !validEncoding(p.encoding))) ||
481
+ (p.container !== undefined && !copy))
482
+ throw new HostError("invalid", "invalid export of parts");
483
+ parts.forEach((part, index) => {
484
+ if (!host.sourceReferences.has(part.source))
485
+ throw new HostError("missing", `part ${index} names no source reference of this plugin`);
486
+ });
487
+ const checked = exportCheck(parts);
488
+ host.exports.push(structuredClone(p));
489
+ const jobId = ++inner.next.job;
490
+ host.jobs.set(jobId, {
491
+ jobId,
492
+ status: "queued",
493
+ publicationStarted: false,
494
+ facts: null,
495
+ outcome: null,
496
+ progress: null,
497
+ });
498
+ inner.exportJobs.set(jobId, {
499
+ destination: String(p.destination),
500
+ clip: null,
501
+ join: {
502
+ tokens: parts.map((part) => part.source),
503
+ parts: parts.length,
504
+ total: checked.duration,
505
+ mode: copy ? "copy" : "mp4",
506
+ container: !copy
507
+ ? "mp4"
508
+ : p.container === "mkv" ||
509
+ (p.container !== "mp4" &&
510
+ checked.copy.ok &&
511
+ checked.copy.container === "mkv")
512
+ ? "mkv"
513
+ : "mp4",
514
+ // A copy starts each part on a "keyframe" 1 s early, as clips do.
515
+ written: parts.map((part) => ({
516
+ start: copy ? Math.max(0, part.start - 1) : part.start,
517
+ end: part.end,
518
+ })),
519
+ },
520
+ });
521
+ return { jobId, status: "queued" };
522
+ }
523
+ return {
524
+ ...handlers,
178
525
  "producer.prepare": async () => {
179
526
  const id = ++inner.next.producer;
180
527
  const tcp = host.producerTransport === "tcp";
@@ -484,6 +484,35 @@ export function mediaHandlers(host, inner) {
484
484
  }
485
485
  return result;
486
486
  },
487
+ // The player's `media.sources.open`: the token's file opened as a
488
+ // `source.submit` open under the plugin's operation id, at `start`.
489
+ "media.sources.open": (p) => {
490
+ const reference = host.sourceReferences.get(String(p.token));
491
+ if (!reference)
492
+ throw new HostError("missing", "no such source reference");
493
+ const file = host.mediaFiles.get(reference.path) ?? {};
494
+ if (file.status === "missing")
495
+ throw new HostError("source-missing", "the file is missing");
496
+ if (file.status === "changed" ||
497
+ (file.version ?? 0) !== (reference.version ?? 0))
498
+ throw new HostError("source-changed", "the file changed since it was remembered");
499
+ const start = p.start === undefined ? 0 : Number(p.start);
500
+ const duration = file.duration ?? host.source.duration ?? 60;
501
+ if (!(start >= 0 && start < duration))
502
+ throw new HostError("invalid", "start must be before the file's end");
503
+ const intent = {
504
+ kind: "open",
505
+ source: reference.path,
506
+ startPaused: false,
507
+ };
508
+ host.sourceOpens.push({ token: String(p.token), start });
509
+ const ticket = sourceIntent(host, inner, intent);
510
+ // Loaded at once, at the start (the player's `start=`).
511
+ host.setSource({ phase: "ready", duration, position: start });
512
+ const result = { status: "completed", ...ticket };
513
+ inner.sourceBook.set(Number(p.operationId), result);
514
+ return result;
515
+ },
487
516
  "source.operation": (p) => {
488
517
  const result = inner.sourceBook.get(Number(p.operationId));
489
518
  if (!result)
@@ -6,7 +6,7 @@ import type {
6
6
  RenderingReasonCode,
7
7
  RenderingState,
8
8
  } from "./services.ts";
9
- import type { ExportJob, Snapshot } from "./snapshots.ts";
9
+ import type { ExportJob, MediaSources, Snapshot } from "./snapshots.ts";
10
10
  import type { Resolver } from "./sources.ts";
11
11
  import type { PropValue, View } from "./ui-content.ts";
12
12
  import type { Panel, Window } from "./ui-layout.ts";
@@ -123,7 +123,7 @@ export interface Disposable {
123
123
  * | `unknown`, `rejected`, `expired` | After a transport problem the SDK could not confirm the outcome (does not happen in normal operation). |
124
124
  * | `activation` | The plugin opened a tool window while its activation was still pending and the player could not track it. |
125
125
  * | `command` | The player could not run or update a command it no longer knows for this plugin (removed meanwhile, or another plugin's). |
126
- * | `export-failed` | An export job ({@link ExportJob}) failed: the code of its `outcome.error`, not a rejection; `message` has the worker's or FFmpeg's reason. |
126
+ * | `export-failed`, `source-missing`, `source-changed`, `no-space` | An export job ({@link ExportJob}) failed: the code of its `outcome.error`, not a rejection; `message` has the worker's or FFmpeg's reason. An export of parts ({@link MediaCapture.export}) ends `source-missing` when a part's file is gone and `source-changed` when it changed since it was remembered; {@link MediaSources.open} rejects with them. An export of parts ends `no-space` when the disk has less room than its estimate. |
127
127
  * | `input-overflow` | `openStream` input: the player's queue of physical key transitions was full; the input was revoked and its keys released. |
128
128
  * | `internal` | An internal step of the player failed (keeping an operation's result, a subtitle or media job); `message` has the reason. |
129
129
  * | `javascript`, `slint` | {@link Packages.validate}: the package's entry module does not pass `node --check` / one of its Slint files does not compile; `message` has the diagnostics. |