From 664cfa635c6c502fde1dae929ead2be64031d6cb Mon Sep 17 00:00:00 2001 From: Casey Occhialini <1508707+littlespex@users.noreply.github.com> Date: Mon, 27 Apr 2026 17:05:19 -0700 Subject: [PATCH 01/64] docs(cmcd): Add design spec for CMCD CML refactor Spec the migration from shaka's custom CMCD integration to @svta/cml-cmcd via a vendored closure-port in third_party/cml-cmcd/. CmcdManager becomes a thin adapter over CmcdReporter; encoding, validation, and state tracking move upstream to CML as the single source of truth. Three-phase plan: vendor + delegate encoding, dedupe constants, then swap state machine for CmcdReporter. Vendored port is transitional and deletes when shaka adopts TypeScript (#8262). Co-Authored-By: Claude Opus 4.7 (1M context) --- plans/cmcd-cml-refactor/spec.md | 573 ++++++++++++++++++++++++++++++++ 1 file changed, 573 insertions(+) create mode 100644 plans/cmcd-cml-refactor/spec.md diff --git a/plans/cmcd-cml-refactor/spec.md b/plans/cmcd-cml-refactor/spec.md new file mode 100644 index 0000000000..9b0638cbf5 --- /dev/null +++ b/plans/cmcd-cml-refactor/spec.md @@ -0,0 +1,573 @@ +# CMCD refactor: replace shaka-player's custom integration with `@svta/cml-cmcd` + +Date: 2026-04-27 +Status: Approved design, ready for implementation planning +Tracking: [shaka-player#3619](https://github.com/shaka-project/shaka-player/issues/3619), [common-media-library#40](https://github.com/streaming-video-technology-alliance/common-media-library/issues/40) + +## Context + +Shaka Player ships a custom CMCD (Common Media Client Data, CTA-5004) integration: + +- [`lib/util/cmcd_manager.js`](lib/util/cmcd_manager.js) — 1704 lines, supports CMCD v1 and v2 in request/response/event modes. +- [`test/util/cmcd_manager_unit.js`](test/util/cmcd_manager_unit.js) — 4317 lines, mostly testing CMCD wire-format correctness. +- [`externs/cmcd.js`](externs/cmcd.js) — `CmcdData` typedef. +- [`externs/shaka/player.js`](externs/shaka/player.js) — `shaka.extern.CmcdConfiguration` and `shaka.extern.CmcdTarget` typedefs. + +Other major adaptive players (hls.js, dash.js) have already migrated to the +[`@svta/cml-cmcd`](https://github.com/streaming-video-technology-alliance/common-media-library/tree/main/libs/cmcd) +package as the shared reference implementation. See +[hls.js PR #7725](https://github.com/video-dev/hls.js/pull/7725) for the pattern +this design follows. + +## Motivation + +This refactor pursues three explicit goals: + +1. **Standards conformance / single source of truth.** CML is the shared + reference implementation for CMCD across players. Tracking it gets shaka's + CMCD output spec-aligned automatically as v2 finalizes. +2. **Reduce maintenance burden.** ~6000 lines of CMCD code (manager + tests) + carry the full encoding, validation, and state-machine surface. Most of that + logic is identical in CML. Maintenance can move upstream where multiple + players amortize it. +3. **Submit upstream PR-friendly to shaka maintainers.** No new runtime npm + dependency (preserves shaka's hard-won "zero runtime deps" property), no + build-system upheaval, incremental phasing for review. + +## Constraints + +These are non-negotiable inputs to the design: + +1. **Zero runtime npm dependencies.** [AGENTS.md](AGENTS.md) calls this out as + a "hard-won property... do not introduce any." Direct npm consumption of + `@svta/cml-cmcd` is not on the table. +2. **Closure Compiler ADVANCED_OPTIMIZATIONS.** All source uses + `goog.provide`/`goog.require` with JSDoc type annotations. Externs define + public types. +3. **TypeScript migration is on the horizon.** + [shaka-player#8262](https://github.com/shaka-project/shaka-player/issues/8262) + tracks shaka's planned migration to TypeScript (P2, Backlog as of + 2026-04). Whatever closure-era infrastructure we build is **transitional** + and gets deleted on migration day. Avoid building durable infrastructure + that exists only to bridge closure to a TS package. +4. **Public API back-compat.** `shaka.extern.CmcdConfiguration`, + `shaka.extern.CmcdTarget`, and the `CmcdData` typedef are consumed by + shaka users. The `shaka.util.CmcdManager.StreamingFormat` enum is + `@export`ed (see [`cmcd_manager.js:1610`](lib/util/cmcd_manager.js:1610)). + v1 fields and v2 fields outside the experimental flag must keep their + existing names. v2-only fields flagged experimental may be renamed. + +## Decision summary + +- **Strategy: Manual vendored port** of `@svta/cml-cmcd` into + `third_party/cml-cmcd/`, following the + [`third_party/closure-uri/`](third_party/closure-uri/) precedent. +- **Scope: Full replacement.** `shaka.util.CmcdManager` becomes a thin + shaka-specific adapter wrapping `cml.cmcd.CmcdReporter`. State tracking, + encoding, key filtering, sequence numbers, and event timing all move to the + reporter. +- **Phasing: Three sequential PRs** (vendor + delegate encoding → dedupe + constants → adapter rewrite). Each phase is independently reviewable and + bisectable. +- **Test ownership boundary:** CMCD wire-format tests move to / live in CML + upstream. Shaka keeps shaka-specific glue tests + a small end-to-end smoke + set. + +Rejected alternatives: + +- **CML emits a closure-friendly distribution** (option A in brainstorm). + Rejected because shaka's TS migration would orphan the closure target in + CML; not worth the upstream burden. +- **Build-time TS-to-Closure transpiler in shaka** (option B). Rejected + because tsickle is archived (last push 2024-05) and a custom transformer + would cost ~500-1500 LoC of throwaway tooling. CML's TS uses `as const` + enum patterns, type-only imports, intersections, and `ValueOf<>` generics + that aren't trivial to mechanically translate. +- **Wait for shaka's TS migration before doing CMCD work.** Rejected because + the migration is in Backlog with no concrete timeline; CMCD work shouldn't + block on it. + +## Architecture + +Three layers, each with one job: + +``` +┌────────────────────────────────────────────────────────────────┐ +│ shaka public API (UNCHANGED except experimental v2 renames) │ +│ shaka.extern.CmcdConfiguration shaka.extern.CmcdTarget │ +│ externs/cmcd.js (CmcdData typedef) │ +│ shaka.util.CmcdManager.StreamingFormat (the only @export) │ +└────────────────────────────────────────────────────────────────┘ + ▲ + │ (back-compat preserved) +┌────────────────────────────────────────────────────────────────┐ +│ lib/util/cmcd_manager.js (REFACTORED, ~250 lines target) │ +│ Thin shaka-specific adapter: │ +│ - NetworkingEngine integration (applyRequestData / │ +│ applyResponseData) — public method signatures unchanged │ +│ -