// THE MODULE CONTRACT (release 39, the video-modules toolkit). // // Every piece of a report video's chrome -- the deck's shell, its subtitle, // QR, rail and counter, the stamps, the posts, the finale, the intro, the // teaser, the effects -- is a MODULE: one file under modules/ that exports // `defineModule({...})` and is registered in modules/index.mjs. A module says // what it is (`kind`, `slot`, `z`), where its settings live in the manifest // (`configKey`, `defaults`), how they are checked (`validate`) and filled // (`resolve`), what it reads of other modules (`deps`, by NAME -- never by // reaching into their slice), how it is drawn (`page` for a HyperFrames page, // `render` for an ffmpeg/IM one), where its frames land (`region`), what its // render cache keys on (`frameKeys`: the static part and the frame events), // and how umtool shows it (`ui`, `preview`, `play`, `docs`). // // PLAY (`play`): an overlay module's HyperFrames page IS its live preview. // The build captures the page frame by frame; Play the cut and the clip // editor's live preview load the SAME page (composed with `preview: true`, // no render) in an iframe at the module's region and seek it by message on // the clock it declares -- `{ clock: "cut", ready: ":ready" }`: the // page takes `deck:seek` in the cut's seconds and posts `ready` once it can // be seeked. A whole-cut module is up for the whole cut; a window module // (`overlay-window`) only inside each of its regions' windows, as the build // lays it. A module that declares `play` needs nothing else to appear there // (onscreen.mjs composePlayLayers, OnscreenSection.tsx ModuleLayers). // // A PART (`host` set) is drawn inside its host module's page -- the subtitle, // the QR, the rail and the counter are all inside the deck's one page, so // their frames are the shell's. It owns its settings, validation, defaults, // docs and UI; its pixels are the host's region. // // Pure: no fs, no ffmpeg, no network. The registry and every module file are // imported by the build, the composer and umtool alike. /** What a module is: how its pixels reach the cut. */ import { fullFrameTraitProblem } from "../full-frame.mjs"; export const MODULE_KINDS = Object.freeze([ // A page over the whole cut (or a window of it), laid by its region at frame 1. "overlay-whole", // Pages laid at their own second: short sequences over a stretch of the cut. "overlay-window", // A segment inserted into the timeline (the intro, a teaser). "segment", // A filter on the footage itself (a clip's blur). "input", // After the master (a trailer, the variant ladder). "post", // Drawn inside another module's page (`host`): settings, checks and docs of its own. "part", ]); /** * The layout slots a module may ask for (layout.mjs `slotRect`): the deck's * band at the foot of the frame, the posts column, the stamp's box in the * footage, the footage box itself, the side panel beside it, a popup column * over the footage, the measure's instruments' column at the frame's right * (instruments.mjs), the shelf beside the footage (`deck.shelf`: deckGeometry's * `shelf`), the whole frame, or `inline` (a part, in its host's slot). */ export const MODULE_SLOTS = Object.freeze(["footer", "column", "popup", "stamp", "panel", "footage", "side", "instruments", "shelf", "full", "inline"]); /** * The clocks a module's live page is seeked on (`play.clock`): the cut's * seconds -- the one clock every page answers `deck:seek` on today. */ export const PLAY_CLOCKS = Object.freeze(["cut"]); /** * Where a module stands in the STACKED vertical deck (vertical-deck.mjs): the * whole frame (the shell's page), a cell of the instruments' panel, over the * picture (as it stands over the footage box), on the picture's lower-left * shelf, the finale's instrument column, inside its host's page (a part), a * segment of the cut (its own frame), or a filter on the footage itself. */ export const VERTICAL_SLOTS = Object.freeze(["frame", "panel", "picture", "shelf", "finale", "inline", "segment", "input"]); /** How a region is laid over the cut: a whole-cut sequence from frame 1, or a window at its own second. */ export const REGION_PLACEMENTS = Object.freeze(["whole", "window"]); const ID_RE = /^[a-z][a-z0-9-]{0,31}$/; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const isFn = (v) => typeof v === "function"; /** The keys a module definition may carry, each with what it must be. */ const SPEC = Object.freeze({ id: "an id: lower-case letters, digits and dashes", version: "a whole number from 1: bumped when what it draws changes", z: "a number: its stack order, lowest first", kind: `one of ${MODULE_KINDS.join(", ")}`, slot: `one of ${MODULE_SLOTS.join(", ")}`, title: "words: what the module is called in umtool and the docs", summary: "a sentence: what it draws", host: "a part's host module id", region: "the name its frames and project are filed under (chrome/-frames)", configKey: "a dotted manifest path its settings live at (render.chrome.deck.qr)", configKeys: "more dotted paths it owns beside configKey", defaults: "an object: every setting and its default", validate: "a function (config, ctx) => string[]", resolve: "a function (render) => its settings with the defaults filled", deps: "the named outputs of other modules it reads (counter.totals)", provides: "the named outputs it gives (counter.totals)", on: "a function (render, manifest?) => boolean: is it drawn at all", schedule: "a function (doc, ctx) => its slice of the deck schedule", page: "an async function (ctx) => the HTML of its page", render: "an async function (ctx) => what it renders without a page", regions: "a function (schedule, ctx) => its overlay regions", composes: "a function (schedule, ctx) => the compose requests a build makes for it", frameKeys: "its frame-key model: { data, omit, offset?, eases?, events?, items? }", ui: "a field descriptor list for umtool's generic panel", preview: "a function (slice, ctx) => a single-layer preview request", play: "its live-preview contract: { clock: \"cut\", ready: \":ready\" } -- its page seeked in an iframe by Play the cut and the live preview", docs: "its knobs.md section: { heading, intro?, rows: [[knob, range, terminal]], after? }", workers: "\"wide\" or \"narrow\": how many renderer workers its page takes", vertical: "its form in the stacked vertical deck (vertical-deck.mjs): { slot: one of VERTICAL_SLOTS }; absent, it has none and is left out of one", noun: "how a message names its sequence (\"the stamps'\")", cached: "false for a page the render cache does not key (the legacy chart band)", check: "a function (schedule, render) => the sentence why this schedule cannot draw it, or null", window: "a function (schedule, { window, segment, fps, total }) => the window it composes", names: "a function ({ preview, window, entry, suffix }) => { project, stem }", entry: "a function (manifest, segment) => the timeline entry a segment module draws", seconds: "a function (entry, transition, fps) => how long a segment module's page is", fullFrame: "how its region is laid over a full-frame item (full-frame.mjs): { followsDeck } alone, or { ownFade?, drawsMarks? } (together: drawsMarks wins over a teaser with marks, ownFade shapes the rest) -- each true; absent, it is off over every one", }); /** * One module, checked and frozen. Throws a sentence naming the key on a * definition no registry should hold: a module that mis-declares itself is a * bug in the build, not a manifest problem. * * @template T * @param {T & { id: string, kind: string }} def * @returns {Readonly} */ export function defineModule(def) { if (!isObj(def)) throw new Error("defineModule: a module is an object"); const where = `module ${def.id ?? "(no id)"}`; for (const k of Object.keys(def)) { if (!(k in SPEC)) throw new Error(`${where}: ${k} is not a module key (${Object.keys(SPEC).join(", ")})`); } if (typeof def.id !== "string" || !ID_RE.test(def.id)) throw new Error(`${where}: id must be ${SPEC.id}`); if (!(Number.isInteger(def.version) && def.version >= 1)) throw new Error(`${where}: version must be ${SPEC.version}`); if (typeof def.z !== "number" || !Number.isFinite(def.z)) throw new Error(`${where}: z must be ${SPEC.z}`); if (!MODULE_KINDS.includes(def.kind)) throw new Error(`${where}: kind must be ${SPEC.kind}`); if (!MODULE_SLOTS.includes(def.slot)) throw new Error(`${where}: slot must be ${SPEC.slot}`); if (typeof def.title !== "string" || !def.title.trim()) throw new Error(`${where}: title must be ${SPEC.title}`); if (def.kind === "part") { if (typeof def.host !== "string" || !ID_RE.test(def.host)) throw new Error(`${where}: a part names its host`); if (def.slot !== "inline") throw new Error(`${where}: a part's slot is inline (it draws in its host's)`); if (def.region !== undefined || def.page !== undefined) throw new Error(`${where}: a part has no region or page of its own`); } else if (def.host !== undefined) { throw new Error(`${where}: only a part has a host`); } if ((def.kind === "overlay-whole" || def.kind === "overlay-window") && typeof def.region !== "string") { throw new Error(`${where}: an overlay names its region`); } for (const k of ["validate", "resolve", "on", "schedule", "page", "render", "regions", "composes", "preview", "check", "window", "names", "entry", "seconds"]) { if (def[k] !== undefined && !isFn(def[k])) throw new Error(`${where}: ${k} must be ${SPEC[k]}`); } if (def.configKey !== undefined && !/^render(\.[A-Za-z]+)+$/.test(def.configKey)) { throw new Error(`${where}: configKey must be ${SPEC.configKey}`); } if (def.configKeys !== undefined && !(Array.isArray(def.configKeys) && def.configKeys.every((k) => /^[a-z]+(\.[A-Za-z]+)*$/.test(k)))) { throw new Error(`${where}: configKeys must be ${SPEC.configKeys}`); } for (const k of ["deps", "provides"]) { if (def[k] !== undefined && !(Array.isArray(def[k]) && def[k].every((n) => /^[a-z][a-z0-9-]*\.[a-zA-Z]+$/.test(n)))) { throw new Error(`${where}: ${k} must be ${SPEC[k]}`); } } for (const p of def.provides ?? []) { if (!p.startsWith(`${def.id}.`)) throw new Error(`${where}: it provides ${p}, which is not its own (${def.id}.)`); } if (def.defaults !== undefined && !isObj(def.defaults)) throw new Error(`${where}: defaults must be ${SPEC.defaults}`); if (def.frameKeys !== undefined && def.frameKeys !== null) { const f = def.frameKeys; if (!isObj(f) || typeof f.data !== "string" || !Array.isArray(f.omit)) throw new Error(`${where}: frameKeys must be ${SPEC.frameKeys}`); } if (def.ui !== undefined && !Array.isArray(def.ui)) throw new Error(`${where}: ui must be ${SPEC.ui}`); if (def.play !== undefined) { const p = def.play; if (!isObj(p) || !PLAY_CLOCKS.includes(p.clock) || typeof p.ready !== "string" || !/^[a-z][a-z0-9-]*:ready$/.test(p.ready)) { throw new Error(`${where}: play must be ${SPEC.play}`); } if (!(def.kind === "overlay-whole" || def.kind === "overlay-window") || !isFn(def.page) || !isFn(def.regions)) { throw new Error(`${where}: only an overlay with a page and regions plays live`); } } if (def.docs !== undefined && !(isObj(def.docs) && typeof def.docs.heading === "string")) throw new Error(`${where}: docs must be ${SPEC.docs}`); if (def.vertical !== undefined && !(isObj(def.vertical) && VERTICAL_SLOTS.includes(def.vertical.slot))) throw new Error(`${where}: vertical must be ${SPEC.vertical}`); if (def.workers !== undefined && !["wide", "narrow"].includes(def.workers)) throw new Error(`${where}: workers must be ${SPEC.workers}`); if (def.fullFrame !== undefined) { const f = def.fullFrame; const why = fullFrameTraitProblem(f); if (why) throw new Error(`${where}: fullFrame must be ${SPEC.fullFrame} (${why})`); if (!(def.kind === "overlay-whole" || def.kind === "overlay-window")) throw new Error(`${where}: only an overlay is laid over a full-frame item`); } return Object.freeze({ ...def }); } // --------------------------------------------------------------------------- // Regions: where a module's frames land in the cut. // --------------------------------------------------------------------------- const num = (v) => typeof v === "number" && Number.isFinite(v); /** * An overlay region, checked: `{ name, frames, x, y, width, height }`, and * for a window `offset` (the cut second its frame 1 lands on) and * `frameCount`; a chunk of the final encode reads a whole-cut sequence from * `startNumber`. What chromeOverlayChain and the chunked encode take. Returns * the region; throws a sentence on a malformed one. * * @template {{ name: string, frames: string, x: number, y: number, width: number, height: number, * offset?: number, frameCount?: number, startNumber?: number }} R * @param {R} r * @param {"whole" | "window" | null} [placement] * @returns {R} */ export function assertRegion(r, placement = null) { const where = `region ${r?.name ?? "(no name)"}`; if (!isObj(r) || typeof r.name !== "string" || !r.name) throw new Error(`${where}: a region has a name`); if (typeof r.frames !== "string" || !r.frames) throw new Error(`${where}: a region names its frames' directory`); for (const k of ["x", "y", "width", "height"]) if (!num(r[k])) throw new Error(`${where}: ${k} must be a number`); if (placement === "window") { if (!num(r.offset)) throw new Error(`${where}: a window region has an offset (the cut second its frame 1 lands on)`); if (!(Number.isInteger(r.frameCount) && r.frameCount > 0)) throw new Error(`${where}: a window region has a frame count`); } if (r.startNumber !== undefined && !(Number.isInteger(r.startNumber) && r.startNumber >= 1)) { throw new Error(`${where}: startNumber must be a frame number from 1`); } return r; }