@vestra-player/sdk 1.7.6 → 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.
@@ -140,6 +140,8 @@ class JobHandle {
140
140
  await this.cancel().catch(() => { });
141
141
  }
142
142
  }
143
+ /** A live snapshot's id, for `exportCheck({ snapshot })`. */
144
+ const snapshotIds = new WeakMap();
143
145
  class SnapshotHandle {
144
146
  #scope;
145
147
  #service;
@@ -150,6 +152,11 @@ class SnapshotHandle {
150
152
  this.#service = service;
151
153
  this.#frozen = frozen;
152
154
  scope.add(this);
155
+ snapshotIds.set(this, () => {
156
+ if (this.#released)
157
+ throw failure("released", "snapshot already released");
158
+ return this.#frozen.snapshotId;
159
+ });
153
160
  }
154
161
  get kind() {
155
162
  return this.#frozen.kind;
@@ -311,5 +318,164 @@ export function mediaCapture(scope) {
311
318
  : await runtime.host.snapshot.state(captured.snapshotId);
312
319
  return new SnapshotHandle(scope, service, frozen);
313
320
  },
321
+ sources: mediaSources(scope),
322
+ async export(options) {
323
+ const runtime = scope.runtime;
324
+ const service = serviceOf(runtime);
325
+ if (typeof options !== "object" || options === null)
326
+ throw failure("invalid", "export options must be an object");
327
+ needSources(scope);
328
+ const parts = checkParts(options.parts);
329
+ let mode;
330
+ if (options.format === "copy") {
331
+ const container = options.container;
332
+ if (container !== undefined &&
333
+ !["auto", "mp4", "mkv"].includes(container))
334
+ throw failure("invalid", "container must be auto, mp4 or mkv");
335
+ if (options.encoding !== undefined)
336
+ throw failure("invalid", "a copy takes no encoding");
337
+ mode = {
338
+ kind: "rawCopy",
339
+ ...(container === undefined ? {} : { container }),
340
+ };
341
+ }
342
+ else if (options.format === "mp4") {
343
+ if (options.container !== undefined)
344
+ throw failure("invalid", "a re-encode takes no container");
345
+ mode = {
346
+ kind: "rawMp4",
347
+ ...(options.encoding === undefined
348
+ ? {}
349
+ : { encoding: checkEncoding(options.encoding) }),
350
+ };
351
+ }
352
+ else
353
+ throw failure("invalid", `an export of parts is format copy or mp4, not ${String(options.format)}`);
354
+ const result = await service.mutate((operationId) => runtime.host.jobs.startParts({
355
+ operationId,
356
+ parts,
357
+ destination: options.destination,
358
+ ...mode,
359
+ }));
360
+ return new JobHandle(scope, service, result.jobId);
361
+ },
362
+ async exportCheck(input, options) {
363
+ const encoding = options?.encoding === undefined
364
+ ? undefined
365
+ : checkEncoding(options.encoding);
366
+ if (!Array.isArray(input)) {
367
+ // A snapshot's clip: needs no media.sources.
368
+ const id = input?.snapshot
369
+ ? snapshotIds.get(input.snapshot)
370
+ : undefined;
371
+ if (!id)
372
+ throw failure("invalid", "exportCheck takes parts or { snapshot, end }");
373
+ if (!Number.isFinite(input.end))
374
+ throw failure("invalid", "end must be seconds");
375
+ return scope.runtime.host.jobs.checkSnapshot(id(), input.end, encoding);
376
+ }
377
+ // As the player: parts carry their own ends; `end` is a snapshot's.
378
+ if (options?.end !== undefined)
379
+ throw failure("invalid", "end belongs to a snapshot's check; parts carry their own end");
380
+ needSources(scope);
381
+ return scope.runtime.host.jobs.check(checkParts(input), encoding);
382
+ },
383
+ };
384
+ }
385
+ /**
386
+ * The player's bounds of remembered files and exports of their parts,
387
+ * documented in the limits reference. Exported from `./runtime` only, for
388
+ * the player's agreement test; TestHost and the SDK's own checks read
389
+ * these figures. The bound on references is a resource limit; tokens are
390
+ * an API convenience, not a security boundary.
391
+ *
392
+ * @internal
393
+ */
394
+ export const MEDIA_SOURCE_LIMITS = Object.freeze({
395
+ /** References one plugin may hold; one more fails with `limit` (500). */
396
+ references: 500,
397
+ /** Parts of one export or check, at least 1; more fail with `invalid` (100). */
398
+ parts: 100,
399
+ /** Seconds of a part's end in its file (24 hours). */
400
+ partEnd: 86400,
401
+ /** Seconds of all parts of one export together, as one clip (24 hours). */
402
+ totalSeconds: 86400,
403
+ /**
404
+ * `remember`, `get`, `list` and checks one plugin may have the
405
+ * player working on at the same time; one more fails with `busy` (4).
406
+ */
407
+ requests: 4,
408
+ /** Seconds between two `locate` dialogs of one plugin; sooner fails with `busy` (2). */
409
+ locateInterval: 2,
410
+ });
411
+ /** Refuse what the player would refuse of an export's parts, naming the part. */
412
+ function checkParts(parts) {
413
+ if (!Array.isArray(parts) ||
414
+ parts.length === 0 ||
415
+ parts.length > MEDIA_SOURCE_LIMITS.parts)
416
+ throw failure("invalid", `an export takes 1 to ${MEDIA_SOURCE_LIMITS.parts} parts`);
417
+ const checked = parts.map((part, index) => {
418
+ const { source, start, end } = part ?? {};
419
+ if (typeof source !== "string" || source === "")
420
+ throw failure("invalid", `part ${index}: source must be a token`);
421
+ if (!Number.isFinite(start) ||
422
+ !Number.isFinite(end) ||
423
+ start < 0 ||
424
+ end > MEDIA_SOURCE_LIMITS.partEnd ||
425
+ end <= start)
426
+ throw failure("invalid", `part ${index}: start and end must be seconds with 0 <= start < end <= ${MEDIA_SOURCE_LIMITS.partEnd}`);
427
+ return { source, start, end };
428
+ });
429
+ const total = checked.reduce((sum, part) => sum + part.end - part.start, 0);
430
+ if (total > MEDIA_SOURCE_LIMITS.totalSeconds)
431
+ throw failure("invalid", `the parts are ${total} s long; an export takes at most ${MEDIA_SOURCE_LIMITS.totalSeconds} s`);
432
+ return checked;
433
+ }
434
+ /** The parts' tokens are media.sources': refuse at once without it, as the player does. */
435
+ function needSources(scope) {
436
+ if (!scope.runtime.can("media.sources"))
437
+ throw failure("capability", "media.sources must be declared in manifest");
438
+ }
439
+ function mediaSources(scope) {
440
+ const host = () => {
441
+ needSources(scope);
442
+ return scope.runtime.host.mediaSources;
443
+ };
444
+ const token = (value) => {
445
+ if (typeof value !== "string" || value === "")
446
+ throw failure("invalid", "token must be a non-empty string");
447
+ return value;
448
+ };
449
+ return {
450
+ async remember(options) {
451
+ // Conditional like a capture: the token is for the file in `ifCurrent`.
452
+ const expected = options?.ifCurrent === undefined
453
+ ? undefined
454
+ : playerOf(scope.runtime).expected(options.ifCurrent).expected;
455
+ return host().remember(expected);
456
+ },
457
+ get: async (value) => host().get(token(value)),
458
+ locate: async (value) => host().locate(token(value)),
459
+ forget: async (value) => host().forget(token(value)),
460
+ list: async () => host().list(),
461
+ async open(value, options = {}) {
462
+ needSources(scope);
463
+ // An open like Player.open's: the player asks playback.control too.
464
+ if (!scope.runtime.can("playback.control"))
465
+ throw failure("capability", "playback.control must be declared in manifest");
466
+ const start = options?.start;
467
+ if (start !== undefined && !(Number.isFinite(start) && start >= 0))
468
+ throw failure("invalid", "start must be seconds from 0");
469
+ const checked = token(value);
470
+ // An ordinary open, followed like player.open's (the player opens the
471
+ // path the token stands for; the plugin never sees it).
472
+ await playerOf(scope.runtime).submit({ kind: "open", source: "", startPaused: false }, {
473
+ send: (operationId) => host().open({
474
+ token: checked,
475
+ operationId,
476
+ ...(start === undefined ? {} : { start }),
477
+ }),
478
+ });
479
+ },
314
480
  };
315
481
  }
@@ -1,4 +1,4 @@
1
- import type { SourceIntent, SourceStamp, SourceState, SourceTicket } from "../source.ts";
1
+ import type { SourceIntent, SourceOperation, SourceStamp, SourceState, SourceTicket } from "../source.ts";
2
2
  import { Latest } from "./latest.ts";
3
3
  import type { Runtime } from "./runtime.ts";
4
4
  import type { ScopeImpl } from "./scope.ts";
@@ -69,6 +69,12 @@ export declare class PlayerService implements StreamSource {
69
69
  submit(intent: SourceIntent, options?: {
70
70
  signal?: AbortSignal;
71
71
  accepted?: (ticket: SourceTicket) => void;
72
+ /**
73
+ * Send the submit another way under this service's operation id
74
+ * (`media.sources.open`, which the player turns into this intent's
75
+ * `source.submit`); followed with `source.operation` as any submit.
76
+ */
77
+ send?: (operationId: number) => Promise<SourceOperation>;
72
78
  }): Promise<SourceTicket>;
73
79
  expected(state: PlayerState | undefined): {
74
80
  expected?: SourceStamp;
@@ -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>;