diff --git a/Cargo.lock b/Cargo.lock index c4085febfb5..b0f9e1ea84f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2958,6 +2958,7 @@ dependencies = [ "tokio", "tracing", "url", + "uuid", "workspace-hack", ] @@ -5346,8 +5347,7 @@ dependencies = [ [[package]] name = "mp4-atom" version = "0.17.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "327faeb7d11fc3735194fdaeaa82af72167801bf39dd3d6b2684c8fc5cd7fc19" +source = "git+https://github.com/michaelstuurman/mp4-atom?rev=10490e39383294ea11e3d5827517ef1eb9d950a8#10490e39383294ea11e3d5827517ef1eb9d950a8" dependencies = [ "bytes", "derive_more 2.1.1", diff --git a/Cargo.toml b/Cargo.toml index 68db74e417a..fbbed15c3a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -250,6 +250,8 @@ zeroize = { version = "1", features = ["derive"] } # the member, and the next publish that reaches both fails to resolve a single # version (#3551). kio = { path = "rs/kio" } +# Common Encryption boxes (kixelated/mp4-atom#268), until a release ships them. +mp4-atom = { git = "https://github.com/michaelstuurman/mp4-atom", rev = "10490e39383294ea11e3d5827517ef1eb9d950a8" } [profile.dev] # Full DWARF is what makes a debug build enormous: the debug `libmoq_ffi.a` diff --git a/doc/bin/cli.md b/doc/bin/cli.md index 31ee5b908ee..77369b2c213 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -162,6 +162,24 @@ MP4 parser. `import fmp4 --cmaf` publishes each fragment whole as [`cmaf`](/concept/hang#container) instead, keeping the source's exact sample timing. +`import fmp4 --cmaf` also publishes Common Encryption (`cenc` and `cbcs`) media, +such as Shaka Packager's output, unchanged, and signals it in the catalog's +[`drm`](/concept/hang#content-protection) section. Name each DRM system's +license server with `--drm-license SYSTEM=URL`, and FairPlay's certificate with +`--drm-certificate SYSTEM=URL`, once per system; SYSTEM is `widevine`, +`playready`, `fairplay`, `clearkey`, or a system ID, and one the media carries +no `pssh` for is refused. A system ID may also be written as a +`urn:uuid:` URN. `SYSTEM:TYPE=URL` also gives the server's type, such as +`widevine:EME-1.0=…` or `fairplay:application/pkcs7-mime=…`. Encrypted media +without `--cmaf`, key rotation, and a clear lead are refused too. An export +leaves the protected renditions out, since nothing in `moq` decrypts them, and +fails when every rendition is protected. + +```bash +moq --connect https://relay.example.com/anon --broadcast movie.hang \ + import fmp4 --cmaf --drm-license clearkey=https://license.example.com/clearkey < cbcs.mp4 +``` + `import rtsp` pulls an IP camera's H.264 or H.265 video and AAC audio without re-encoding. RTP rides the RTSP connection, so run it on the camera's network and the camera needs no inbound port. Credentials go in the URL diff --git a/doc/concept/hang.md b/doc/concept/hang.md index a7b9e77fbd4..8114c46e42c 100644 --- a/doc/concept/hang.md +++ b/doc/concept/hang.md @@ -64,6 +64,7 @@ A few things the catalog can express beyond decoder config: - **Labels.** Any rendition may carry a human-readable `label` for a track picker. The map key stays the rendition's ID, so labels need not be unique and renaming one doesn't rename the track. - **Renditions in another broadcast.** A rendition may point at a relative broadcast path, so a transcoder can publish a ladder that adds low rungs and references the source's original rendition without re-publishing its bytes. The path resolves against where the consumer found the catalog, so a reference that escapes above the root names nothing and the catalog is rejected. `@moq/watch` and `moqsrc` follow a referenced path's announcements like their own, restarting the rendition on each instance that serves it; every other reader, such as `moq play`, the exporters, and the bindings, waits for the first one announced and keeps it, and an exporter ends the rendition with it. An optional `instance` beside the path names the [stream](/concept/moq-lite#publisher-instances) it expects: a route of another instance ends the rendition instead of falling back to it, while a route whose instance a session minted (one that crossed an older version) cannot prove one, so there it is best effort. Omit `instance` for a broadcast produced on demand, which has none until a worker answers; see [identity and recovery](/concept/identity). - **Whole contributions.** A root `contributions` map names whole broadcasts, such as each guest feed in a composed room, by the same relative path and optional `instance` as a rendition's reference. Listing one opens nothing, so a contribution produced on demand starts only once a viewer selects it, which `@moq/watch` does with `broadcast.contribution(effect, id)`. Its rendition IDs stay its own. +- **DRM.** A root `drm` map describes the DRM systems protecting Common Encryption (`cenc` or `cbcs`) CMAF renditions, one entry per system, scheme, and key: its `system` and `kid` UUIDs, the `pssh` box, and the `license` and `certificate` servers. A protected rendition lists its entries' IDs in its own `drm` field, keeps the encrypted sample entry in its `init`, and names the codec that entry wraps; a rendition listing an ID the root lacks refuses the catalog. Renditions under one key, with the same servers, share an entry. The MSF catalog carries the same entries as CMSF `contentProtections`. See [Content protection](#content-protection). - **Track names.** The map key is a rendition's ID and, by default, its track name. A rendition sets `track` only when the two must differ, such as two renditions of same-named tracks in different broadcasts, or a rendition whose media changed and so needs a new name; readers subscribe to `track` when present. An ID can't name both a video and an audio rendition. - **Jitter.** A rendition can say how much media it writes behind a frame's timestamp, in whole milliseconds rounded up: the reorder delay of B-frames and the span of a fragment or batch written together. It is measured from media timestamps alone, never the time a frame was written, so neither a constant encoder delay nor a capture clock counts. It describes the publisher, never the network, only grows over the life of a stream, and a player sizes its buffer to at least this much. A `0` is read as absent. - **Delay.** A rendition can also say how far its frames' timestamps trail those another rendition already wrote, so a video encoder running 200 ms behind audio advertises `delay: 200` on video. A rendition alone measures none. It follows the same rules as jitter. A player holds the largest `delay + jitter` among the renditions it subscribes to, and never subtracts one rendition's `delay` from another's. @@ -240,6 +241,41 @@ derived from the credential and a semantic name such as `foo.hang`; it carries no format marker. `moq-crypto` is the Rust implementation. Relays need no configuration to carry it. +## Content protection + +DRM is separate from moq-e2ee: the media itself is Common Encryption CMAF, +encrypted before it reaches MoQ by a packager such as Shaka Packager, and a +relay carries it like any other CMAF. `moq import fmp4 --cmaf` publishes such a +file unchanged and fills the catalog's `drm` section from each track's `schm`, +`tenc`, and `pssh` boxes. The license and certificate servers are not in the +media, so the publisher supplies them per DRM system, with `--drm-license` and +`--drm-certificate` or `fmp4::Import::with_drm`. + +```json +{ + "drm": { + "1": { + "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + "scheme": "cbcs", + "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90", + "pssh": "AAAANHBzc2gB...", + "license": { "url": "https://license.example.com/clearkey" } + } + }, + "video": { "renditions": { "video": { "codec": "avc1.64000c", "drm": ["1"], "container": { "kind": "cmaf", "init": "..." } } } } +} +``` + +WebCodecs cannot decrypt, so `@moq/watch` treats a protected rendition as +unplayable and never selects it. Playback through MSE and EME is not built yet. +Nothing in the Rust tools decrypts either: `moq play`, `moq export`, the HLS +gateway, recordings, transcoding, and the C and UniFFI bindings leave a +protected rendition out, as they would a codec they cannot decode, and `moq +export` fails when nothing else is left. They also count a CMAF rendition whose +`init` holds an encrypted sample entry as protected when it lists no `drm`. +Key rotation and a clear lead are refused on import, as is anything else the +republished media could not decrypt. + ## Your own format hang is a convention, not a requirement. If you control both ends, publish diff --git a/drafts/draft-lcurley-moq-hang.md b/drafts/draft-lcurley-moq-hang.md index c8cf35e9acd..1223b3bb67c 100644 --- a/drafts/draft-lcurley-moq-hang.md +++ b/drafts/draft-lcurley-moq-hang.md @@ -18,6 +18,14 @@ author: normative: moqmsf: I-D.ietf-moq-msf + moqcmsf: I-D.ietf-moq-cmsf + cenc: + title: "Information technology - MPEG systems technologies - Part 7: Common encryption in ISO base media file format files" + author: + - org: ISO/IEC + seriesinfo: + ISO/IEC: 23001-7 + date: false moql: I-D.lcurley-moq-lite moqflate: title: "DEFLATE Compressed Tracks for MoQ" @@ -160,6 +168,7 @@ type Catalog = { "broadcast": string, "instance": string | undefined, }> | undefined, + "drm": Record | undefined, // ... any custom fields ... } ~~~ @@ -172,7 +181,7 @@ For example, a chat entry should name a chat track, not carry individual chat me This way catalog updates are rare and a client MAY choose to not subscribe. The `json` and `binary` sections ({{data}}) are how a track like that is listed. -This specification defines audio, video, and text media tracks, an optional `media` section ({{media}}) holding the clock that maps content time to wall time and the interval a viewer can join on, the root `version` ({{catalog-version}}), the application data tracks in {{data}}, optional publisher statistics and solicited viewer feedback ({{media-stats}}), and whole broadcasts listed as `contributions` ({{contributions}}). +This specification defines audio, video, and text media tracks, an optional `media` section ({{media}}) holding the clock that maps content time to wall time and the interval a viewer can join on, the root `version` ({{catalog-version}}), the application data tracks in {{data}}, optional publisher statistics and solicited viewer feedback ({{media-stats}}), whole broadcasts listed as `contributions` ({{contributions}}), and the DRM systems protecting encrypted renditions ({{drm}}). ## Version {#catalog-version} The root `version` field is the oldest draft of this specification that reads the catalog correctly, by draft number: `4` needs a consumer of moq-hang-04 or later. @@ -188,7 +197,7 @@ This draft needs `version` 4 for: - a `track` ({{field-track}}) other than the entry's ID, which an older consumer subscribes to instead; - a video `warmup` ({{field-warmup}}) above zero, since an older consumer starts decoding a group at a picture that is not a keyframe. -Every other catalog change in this draft is safe to ignore: `enabled` ({{field-enabled}}) leaves an older consumer selecting a disabled rendition, which plays nothing; `media` ({{media}}) only offers the clock and join interval, which an older consumer goes without; `stats` and `echo` ({{media-stats}}) only offer optional tracks; `contributions` ({{contributions}}) only offer broadcasts to open on selection; video `reorder` leaves an older consumer inferring decode times from timestamps; `jitter`, `delay`, audio `warmup`, and the data track estimates are hints. +Every other catalog change in this draft is safe to ignore: `enabled` ({{field-enabled}}) leaves an older consumer selecting a disabled rendition, which plays nothing; `media` ({{media}}) only offers the clock and join interval, which an older consumer goes without; `stats` and `echo` ({{media-stats}}) only offer optional tracks; `contributions` ({{contributions}}) only offer broadcasts to open on selection; `drm` ({{drm}}) leaves an older consumer reading a rendition its init segment already marks as encrypted; video `reorder` leaves an older consumer inferring decode times from timestamps; `jitter`, `delay`, audio `warmup`, and the data track estimates are hints. ## Media {#media} The catalog's root `media` field holds what applies to every audio and video track: @@ -255,6 +264,34 @@ A contribution's rendition IDs belong to its own catalog and MUST NOT replace ID An unavailable contribution does not invalidate the listing catalog. A consumer does not expand a contribution's own `contributions`: each needs a selection of its own. +## DRM {#drm} +The optional root `drm` map describes the DRM systems protecting encrypted renditions. +Each key is an entry ID, and each entry is one DRM system for one Common Encryption scheme and one key: + +~~~ +type DrmSchema = { + "system": string, + "scheme": "cenc" | "cbcs", + "kid": string, + "pssh": string | undefined, + "license": { "url": string, "type": string | undefined } | undefined, + "certificate": { "url": string, "type": string | undefined } | undefined, +} +~~~ + +The `system` field is the DRM system ID and `kid` the default key ID, each a hyphenated UUID string, and `scheme` is the Common Encryption scheme {{cenc}}. +The `pssh` field is the system's whole `pssh` box, base64-encoded ({{!RFC4648, Section 4}}). +The `license` field names the server a consumer requests a license from, and `certificate` the server certificate a system such as FairPlay needs first; each `type` is the license protocol or the certificate's MIME type. +Renditions encrypted under the same system, scheme, and key MAY share an entry. + +A protected audio or video rendition lists the IDs of the entries that can decrypt it in its `drm` field, an array of strings; absent or empty means the rendition is clear. +Common Encryption is defined for CMAF, so a protected rendition is in practice a `cmaf` one ({{cmaf}}): its `init` keeps the encrypted sample entry with its `sinf`, and the `pssh` boxes, while its `codec` names the codec the encrypted entry wraps. +A consumer MUST reject a catalog with a malformed entry, or with a rendition listing an ID the root `drm` lacks. +A consumer that cannot decrypt a rendition MUST treat it as one it cannot decode. + +Each entry maps onto a CMSF `contentProtections` entry {{moqcmsf}}: the ID is its `refID`, `kid` its single `defaultKID`, `system`, `license`, `certificate`, and `pssh` its `drmSystem`'s `systemID`, `laURL`, `certURL`, and `pssh`, and a rendition's `drm` its `contentProtectionRefIDs`. +A CMSF entry naming several `defaultKID`s has no single hang entry, so a consumer converting it splits it into one entry per key. + ## Track Identity {#identity} A track name always means the same content, so a catalog update never redefines an audio or video track. A track's identity is the fields a consumer cannot change without reinitializing its decoder or demuxer: `codec`, `description`, and `container`, plus `displayAspectWidth`, `displayAspectHeight`, and `interlaced` for video and `sampleRate` and `numberOfChannels` for audio. @@ -1127,6 +1164,7 @@ This document has no IANA actions. - Added the optional video `preference` field: a consumer selects automatically among the highest preference it can decode. - Added the optional video `reorder` field: the most frames that can precede one in decode order and follow it in presentation, fixed for the track once declared. A consumer that authors decode times derives them from it and the current group alone, refusing deeper reordering; without it, a consumer infers them from timestamps. - Added an optional `instance` beside every rendition and data track `broadcast` reference ({{field-instance}}). A consumer reads each referenced path from the first broadcast it resolves for a catalog instance; a present `instance` fails a rendition that resolves another stream, and is best effort against an Instance a receiver minted. +- Added the optional root `drm` map ({{drm}}) and the audio and video `drm` field: the DRM systems, scheme, and key protecting a Common Encryption `cmaf` rendition, mapping onto CMSF `contentProtections`. A dangling entry ID refuses the catalog. - Added the optional root `contributions` map ({{contributions}}): whole broadcasts named by a relative `broadcast` and an optional `instance`, opened only on selection, whose rendition IDs stay their own. - An HLS master selects the contributions it lists by reading their catalogs, without demanding media, and segments them on the listing catalog's `media`; an on-demand publisher answers its catalog without starting media production. - Added optional `stats` and `echo` catalog sections, publisher/viewer snapshots keyed by rendition ID, and the `.echo` feedback broadcast convention. diff --git a/js/hang/src/catalog/audio.ts b/js/hang/src/catalog/audio.ts index c84397adf74..0e9891d79a3 100644 --- a/js/hang/src/catalog/audio.ts +++ b/js/hang/src/catalog/audio.ts @@ -39,6 +39,12 @@ const AudioConfigFields = z.object({ // The container format, used to decode the timestamp and more. container: ContainerSchema, + // The IDs of the root `drm` entries protecting this rendition, one per DRM system that can + // decrypt it. Absent or empty means clear. Common Encryption is defined for CMAF, so in practice + // it is a CMAF rendition, and `codec` names the codec its encrypted sample entry wraps. A consumer + // that can't decrypt skips it. + drm: z.optional(z.array(z.string())), + // The description is used for some codecs. // If provided, we can initialize the decoder based on the catalog alone. // Otherwise, the initialization information is in-band. diff --git a/js/hang/src/catalog/drm.ts b/js/hang/src/catalog/drm.ts new file mode 100644 index 00000000000..91b4bba38b0 --- /dev/null +++ b/js/hang/src/catalog/drm.ts @@ -0,0 +1,70 @@ +import * as z from "zod/mini"; + +/** + * Well-known DRM system IDs, as draft-ietf-moq-cmsf lists them. + */ +export const SYSTEM = { + widevine: "edef8ba9-79d6-4ace-a3c8-27dcd51d21ed", + playready: "9a04f079-9840-4286-ab92-e65be0885f95", + fairplay: "94ce86fb-07ff-4f43-adb8-93d2fa968ca2", + // The W3C Common System ID: for testing, since the key travels in the clear. + clearkey: "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", +} as const; + +/** A license or certificate server. */ +export const DrmServerSchema = z.object({ + url: z.url(), + // The protocol a license server speaks (e.g. `EME-1.0`), or the MIME type of a certificate. + type: z.optional(z.string()), +}); + +/** A license or certificate server. */ +export type DrmServer = z.infer; + +/** + * One DRM system protecting renditions under one Common Encryption scheme and one default key. + * + * Each protected video or audio rendition lists the IDs of its entries in its own `drm` field; + * each entry maps onto one of draft-ietf-moq-cmsf's `contentProtections`, and a CMSF entry naming + * several default keys splits into one entry per key on parse. + */ +export const DrmSchema = z.object({ + // The DRM system ID, as a UUID string (see `SYSTEM`). + system: z.guid(), + // How the samples are encrypted; draft-ietf-moq-cmsf allows only these two. + scheme: z.enum(["cenc", "cbcs"]), + // The default key ID (`tenc`'s `default_KID`), as a UUID string. + kid: z.guid(), + // The system's whole `pssh` box from the init segment, base64. + pssh: z.optional(z.base64()), + // Where to request a license. + license: z.optional(DrmServerSchema), + // Where to fetch the server certificate, which systems such as FairPlay need first. + certificate: z.optional(DrmServerSchema), +}); + +/** One DRM system protecting renditions. */ +export type Drm = z.infer; + +/** A protected rendition lists a `drm` entry the root `drm` section lacks. */ +export class MissingDrm extends Error { + /** The rendition listing the entry. */ + readonly rendition: string; + /** The entry ID the root lacks. */ + readonly id: string; + + constructor(rendition: string, id: string) { + super(`rendition ${rendition} references missing drm entry ${id}`); + this.name = "MissingDrm"; + this.rendition = rendition; + this.id = id; + } +} + +/** + * Whether a rendition is protected: it lists `drm` entries, so its samples are encrypted and only a + * consumer that can decrypt them may select it. + */ +export function isProtected(config: { drm?: string[] }): boolean { + return (config.drm?.length ?? 0) > 0; +} diff --git a/js/hang/src/catalog/index.ts b/js/hang/src/catalog/index.ts index e99954e9dbf..6a798ed4fa1 100644 --- a/js/hang/src/catalog/index.ts +++ b/js/hang/src/catalog/index.ts @@ -2,8 +2,8 @@ * JSON catalog schema and types describing a broadcast's tracks: audio/video decoder configs for * WebCodecs, container format, rendition layout, the `media` section (the broadcast's `clock` and * `join` interval), - * plus the `json` and `binary` data sections, optional `stats` and `echo` discovery, and whole - * `contributions` listed without opening them. + * plus the `json` and `binary` data sections, optional `stats` and `echo` discovery, whole + * `contributions` listed without opening them, and the `drm` systems protecting CMAF renditions. * * @module */ @@ -26,6 +26,7 @@ export * from "./binary"; export * from "./clock"; export * from "./container"; export * from "./contribution"; +export * from "./drm"; export * from "./echo"; export * from "./format"; export * from "./hex"; diff --git a/js/hang/src/catalog/root.ts b/js/hang/src/catalog/root.ts index b5c4a9fe29a..9e12b65a2cb 100644 --- a/js/hang/src/catalog/root.ts +++ b/js/hang/src/catalog/root.ts @@ -5,6 +5,7 @@ import * as z from "zod/mini"; import { AudioSchema } from "./audio"; import { BinarySchema } from "./binary"; import { ContributionSchema } from "./contribution"; +import { DrmSchema, MissingDrm } from "./drm"; import { EchoSchema } from "./echo"; import { TRACK } from "./format"; import { JsonSchema } from "./json"; @@ -86,6 +87,9 @@ export const RootSchema = z.looseObject({ binary: section(BinarySchema, "tracks"), // A new key, so a malformed entry fails the catalog, the same as Rust. contributions: z.optional(renditions(ContributionSchema, "contribution")), + // The DRM systems protecting the CMAF renditions, keyed by entry ID; each protected rendition + // lists its entries' IDs in its own `drm`. A new key, so a malformed entry fails the catalog. + drm: z.optional(renditions(DrmSchema, "drm entry")), }); /** The root catalog object: the media sections, `media`, the data track sections, plus any app extensions. */ @@ -125,11 +129,26 @@ export class DuplicateRendition extends Error { } } -/** Refuse an update with an ID naming both a video and an audio rendition. */ +/** + * Refuse an update with an ID naming both a video and an audio rendition, or with a rendition + * listing a `drm` entry the root lacks. + * + * Throws {@link DuplicateRendition} or {@link MissingDrm}. + */ export function checkRenditions(root: Root): Root { const audio = root.audio?.renditions ?? {}; const duplicate = Object.keys(root.video?.renditions ?? {}).find((id) => Object.hasOwn(audio, id)); if (duplicate !== undefined) throw new DuplicateRendition(duplicate); + + // A protected rendition whose entry is missing would read as clear, or name no system. + const drm = root.drm ?? {}; + const sections: (Record | undefined)[] = [root.video?.renditions, audio]; + for (const section of sections) { + for (const [rendition, config] of Object.entries(section ?? {})) { + const missing = config.drm?.find((id) => !Object.hasOwn(drm, id)); + if (missing !== undefined) throw new MissingDrm(rendition, missing); + } + } return root; } diff --git a/js/hang/src/catalog/video.ts b/js/hang/src/catalog/video.ts index adbb69f3bbc..e17a5ef5318 100644 --- a/js/hang/src/catalog/video.ts +++ b/js/hang/src/catalog/video.ts @@ -135,6 +135,12 @@ export const VideoConfigSchema = z.object({ // The container format, used to decode the timestamp and more. container: ContainerSchema, + // The IDs of the root `drm` entries protecting this rendition, one per DRM system that can + // decrypt it. Absent or empty means clear. Common Encryption is defined for CMAF, so in practice + // it is a CMAF rendition, and `codec` names the codec its encrypted sample entry wraps. A consumer + // that can't decrypt skips it. + drm: z.optional(z.array(z.string())), + // The description is used for some codecs. // If provided, we can initialize the decoder based on the catalog alone. // Otherwise, the initialization information is (repeated) before each key-frame. diff --git a/js/msf/src/catalog.test.ts b/js/msf/src/catalog.test.ts index 426192a9f52..690bd4a1c89 100644 --- a/js/msf/src/catalog.test.ts +++ b/js/msf/src/catalog.test.ts @@ -330,3 +330,41 @@ test("refuses malformed Unicode in catalogs", () => { const prefix = new TextEncoder().encode('{"version":1,"extension":"'); expect(() => decode(new Uint8Array([...prefix, 0xff, 0x22, 0x7d]))).toThrow(); }); + +// Mirrors moq-msf: CMSF content protection round-trips, and a dangling refID is refused. +test("content protection round-trips and a dangling refID is refused", () => { + const contentProtections = [ + { + refID: "1", + defaultKID: ["a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90"], + scheme: "cbcs", + drmSystem: { + systemID: "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + laURL: { url: "https://license.example.com/", type: "EME-1.0" }, + pssh: "AAAA", + }, + }, + ]; + const track = { name: "video", packaging: "cmaf", isLive: true, role: "video", codec: "avc1.64001f" }; + const catalog = decode( + encodeJson({ + version: "draft-01", + contentProtections, + tracks: [{ ...track, contentProtectionRefIDs: ["1"] }], + }), + ); + expect(catalog.contentProtections).toEqual(contentProtections); + expect(catalog.tracks[0].contentProtectionRefIDs).toEqual(["1"]); + expect(catalog.ext).toBeUndefined(); + expect(decode(encode(catalog))).toEqual(catalog); + + const dangling = encodeJson({ version: "draft-01", tracks: [{ ...track, contentProtectionRefIDs: ["2"] }] }); + expect(() => decode(dangling)).toThrow("missing content protection"); + + const repeated = encodeJson({ + version: "draft-01", + contentProtections: [...contentProtections, ...contentProtections], + tracks: [], + }); + expect(() => decode(repeated)).toThrow("is repeated"); +}); diff --git a/js/msf/src/catalog.ts b/js/msf/src/catalog.ts index bb9eb2ff6cb..fe64a599582 100644 --- a/js/msf/src/catalog.ts +++ b/js/msf/src/catalog.ts @@ -48,6 +48,8 @@ const trackShape = { altGroup: z.optional(z.number()), maxGrpSapStartingType: z.optional(z.number()), maxObjSapStartingType: z.optional(z.number()), + /** The `refID`s of the root `contentProtections` entries protecting this track (CMSF 4.1.2). */ + contentProtectionRefIDs: z.optional(z.array(z.string())), // Non-standard: maximum delay (ms) between consecutive frames on this track. // The player's buffer must be at least this large to avoid underruns. @@ -59,6 +61,40 @@ const trackShape = { delay: z.optional(z.number()), }; +/** A service URL in a {@link DrmSystem}, with an optional protocol or MIME type. */ +export const ProtectionUrlSchema = z.object({ + url: z.string(), + type: z.optional(z.string()), +}); + +/** + * A DRM system description (CMSF 4.1.1.4). draft-ietf-moq-cmsf-01 names no member for its + * authorization URL or robustness, so neither is carried. + */ +export const DrmSystemSchema = z.object({ + systemID: z.string(), + laURL: z.optional(ProtectionUrlSchema), + certURL: z.optional(ProtectionUrlSchema), + /** A base64 `pssh` box with the system's initialization data. */ + pssh: z.optional(z.string()), +}); + +/** A DRM system description within a {@link ContentProtection}. */ +export type DrmSystem = z.infer; + +/** One DRM system configuration protecting tracks of the broadcast (CMSF 4.1.1). */ +export const ContentProtectionSchema = z.object({ + refID: z.string(), + /** The default key IDs (`default_KID`) as UUID strings. */ + defaultKID: z.array(z.string()), + /** The Common Encryption scheme, `cenc` or `cbcs`. */ + scheme: z.string(), + drmSystem: DrmSystemSchema, +}); + +/** One DRM system configuration protecting tracks of the broadcast. */ +export type ContentProtection = z.infer; + /** Zod schema describing a single track entry in an MSF catalog. */ export const TrackSchema = z.object(trackShape); @@ -68,6 +104,8 @@ export type Track = z.infer; /** Zod schema for the top-level MSF catalog: a version-agnostic snapshot of tracks. */ export const CatalogSchema = z.object({ tracks: z.array(TrackSchema), + /** The DRM systems protecting the tracks, which reference them by `refID` (CMSF 4.1.1). */ + contentProtections: z.optional(z.array(ContentProtectionSchema)), // The application extension: root members beyond the ones MSF defines, kept // verbatim. An extension adds a section here, e.g. the `mpegts` section of // draft-lcurley-moq-mpegts. Mirrors `Catalog::ext` in `moq-msf`, which is typed; @@ -96,7 +134,7 @@ export const CatalogSchema = z.object({ export type Catalog = z.infer; /** Root member names MSF itself defines, which an extension section must not reuse. */ -const RESERVED_ROOT = ["version", "generatedAt", "isComplete", "tracks", "initDataList"]; +const RESERVED_ROOT = ["version", "generatedAt", "isComplete", "tracks", "initDataList", "contentProtections"]; /** The newest MSF draft version string this package emits on the wire. */ export const VERSION = "draft-01"; @@ -123,6 +161,7 @@ const WireCatalogSchema = z.object({ version: z.union([z.literal(1), z.string()]), tracks: z.optional(z.array(WireTrackSchema)), initDataList: z.optional(z.array(InitDataSchema)), + contentProtections: z.optional(z.array(ContentProtectionSchema)), }); /** Serialize a catalog to its JSON wire representation (draft-01). */ @@ -151,6 +190,7 @@ export function encode(catalog: Catalog): Uint8Array { // the member from the encoded catalog. const wire: Record = Object.assign(Object.create(null), { version: VERSION, tracks }); if (initDataList.length > 0) wire.initDataList = initDataList; + if (catalog.contentProtections?.length) wire.contentProtections = catalog.contentProtections; // Extension members are written flat, so one named after a field MSF defines would // emit a duplicate key and lose one of the two on re-parse. Refuse instead. The Rust @@ -187,8 +227,22 @@ export function decode(raw: Uint8Array): Catalog { byId.set(entry.id, entry); } + // A refID names one entry, so a repeated one leaves its tracks' protection ambiguous. + const refIDs = new Set(); + for (const cp of wire.contentProtections ?? []) { + if (refIDs.has(cp.refID)) throw new Error(`MSF content protection refID "${cp.refID}" is repeated`); + refIDs.add(cp.refID); + } + + // A track's protection refers to a root entry by refID; one that names none would claim + // encryption the catalog cannot describe, so it is refused like a missing initRef. + const tracks = (wire.tracks ?? []).map(({ initRef, ...track }) => { track.isLive ??= false; + const missing = track.contentProtectionRefIDs?.find((id) => !refIDs.has(id)); + if (missing !== undefined) { + throw new Error(`MSF track "${track.name}" references missing content protection "${missing}"`); + } // Resolve draft-01 initRef into inline initData so callers never see the // indirection. Inline initData (draft-00) is kept as-is, but a catalog that @@ -212,7 +266,10 @@ export function decode(raw: Uint8Array): Catalog { return track; }); - return Object.keys(ext).length > 0 ? { tracks, ext } : { tracks }; + const catalog: Catalog = { tracks }; + if (wire.contentProtections?.length) catalog.contentProtections = wire.contentProtections; + if (Object.keys(ext).length > 0) catalog.ext = ext; + return catalog; } /** Read and decode the next catalog frame from a track, or undefined if the track ended. */ diff --git a/js/watch/src/audio/config.ts b/js/watch/src/audio/config.ts index 4452effc5a4..51d2c186a7d 100644 --- a/js/watch/src/audio/config.ts +++ b/js/watch/src/audio/config.ts @@ -13,7 +13,7 @@ const MP3_MPEG1_MIN_SAMPLE_RATE = 32000; /** The catalog fields that determine the demuxer and WebCodecs decoder instance. */ export type DecoderConfig = Pick< Catalog.AudioConfig, - "codec" | "container" | "description" | "sampleRate" | "numberOfChannels" | "warmup" + "codec" | "container" | "description" | "sampleRate" | "numberOfChannels" | "warmup" | "drm" >; /** The routing and decoder state whose changes require a replacement subscription. */ @@ -33,6 +33,7 @@ export function decoderConfig(config: Catalog.AudioConfig): DecoderConfig { sampleRate: config.sampleRate, numberOfChannels: config.numberOfChannels, warmup: config.warmup, + drm: config.drm, }; } diff --git a/js/watch/src/audio/decoder.ts b/js/watch/src/audio/decoder.ts index 4dad4c81cb9..18195723bae 100644 --- a/js/watch/src/audio/decoder.ts +++ b/js/watch/src/audio/decoder.ts @@ -1020,6 +1020,12 @@ async function supported(config: Catalog.AudioConfig): Promise { return false; } + // WebCodecs cannot decrypt, so an encrypted rendition is unplayable here. + if (Catalog.isProtected(config)) { + console.warn(`audio: ignoring protected rendition ${config.codec}: playback cannot decrypt it`); + return false; + } + // Opus only runs at its native rates, so a catalog advertising anything else is wrong and Safari // refuses to decode it. Warn rather than reject: Chrome and Firefox ignore the configured rate and // play these streams fine, so rejecting would silence them for a publisher they handle today. diff --git a/js/watch/src/msf.test.ts b/js/watch/src/msf.test.ts index 3303c7564d1..74057018fea 100644 --- a/js/watch/src/msf.test.ts +++ b/js/watch/src/msf.test.ts @@ -1,4 +1,5 @@ import { expect, test } from "bun:test"; +import * as Catalog from "@moq/hang/catalog"; import { u53 } from "@moq/hang/catalog"; import * as Msf from "@moq/msf"; import { Path } from "@moq/net"; @@ -317,3 +318,90 @@ test("a later MSF track with a name replaces the earlier one, whatever its role" "vp09.00.10.08", ); }); + +// A protected MSF track lands as a hang rendition listing its `drm` entries, so `` +// knows it cannot play it; an entry with two keys splits into one hang entry per key. +test("maps CMSF content protection onto hang drm entries", () => { + const catalog: Msf.Catalog = { + contentProtections: [ + { + refID: "ck", + defaultKID: ["a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90"], + scheme: "cbcs", + drmSystem: { systemID: "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", laURL: { url: "https://l.example/" } }, + }, + { + refID: "wv", + defaultKID: ["01234567-89ab-cdef-0123-456789abcdef", "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90"], + scheme: "cbcs", + drmSystem: { systemID: "edef8ba9-79d6-4ace-a3c8-27dcd51d21ed" }, + }, + ], + tracks: [ + { name: "video", packaging: "loc", role: "video", codec: "vp8", contentProtectionRefIDs: ["ck", "wv"] }, + { name: "audio", packaging: "loc", role: "audio", codec: "opus" }, + ], + }; + const root = Catalog.checkRenditions(toHang(catalog)); + expect(Object.keys(root.drm ?? {}).sort()).toEqual([ + "ck", + "wv:01234567-89ab-cdef-0123-456789abcdef", + "wv:a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90", + ]); + expect(root.drm?.ck?.license?.url).toBe("https://l.example/"); + expect(root.video?.renditions.video?.drm).toHaveLength(3); + expect(Catalog.isProtected(root.video?.renditions.video ?? { drm: [] })).toBe(true); + expect(root.audio?.renditions.audio?.drm).toBeUndefined(); + + // An entry the hang catalog cannot carry is dropped, and with it the tracks it protects; the + // clear audio stays. + const cens = structuredClone(catalog); + if (cens.contentProtections?.[0]) cens.contentProtections[0].scheme = "cens"; + const noKid = structuredClone(catalog); + if (noKid.contentProtections?.[0]) noKid.contentProtections[0].defaultKID = []; + for (const malformed of [cens, noKid]) { + const root = Catalog.checkRenditions(toHang(malformed)); + expect(Object.keys(root.drm ?? {}).sort()).toEqual([ + "wv:01234567-89ab-cdef-0123-456789abcdef", + "wv:a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90", + ]); + expect(root.video).toBeUndefined(); + expect(root.audio?.renditions.audio).toBeDefined(); + } +}); + +// Mirrors moq-mux: a split entry's ID and key are lowercase hyphenated whatever form MSF used, and +// one that would take another entry's refID drops that entry rather than overwriting the other. +test("split CMSF entry IDs are normalised and never collide", () => { + const split: Msf.ContentProtection = { + refID: "ck", + defaultKID: ["0123456789ABCDEF0123456789ABCDEF", "{A1B2C3D4-E5F6-0718-293A-4B5C6D7E8F90}"], + scheme: "cenc", + drmSystem: { systemID: "1077EFEC-C0B2-4D02-ACE3-3C1E52E2FB4B" }, + }; + const track: Msf.Track = { + name: "video", + packaging: "loc", + role: "video", + codec: "vp8", + contentProtectionRefIDs: ["ck"], + }; + const root = Catalog.checkRenditions(toHang({ contentProtections: [split], tracks: [track] })); + expect(Object.keys(root.drm ?? {}).sort()).toEqual([ + "ck:01234567-89ab-cdef-0123-456789abcdef", + "ck:a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90", + ]); + expect(root.drm?.["ck:01234567-89ab-cdef-0123-456789abcdef"]?.kid).toBe("01234567-89ab-cdef-0123-456789abcdef"); + expect(root.drm?.["ck:01234567-89ab-cdef-0123-456789abcdef"]?.system).toBe(Catalog.SYSTEM.clearkey); + + const colliding: Msf.ContentProtection = { + refID: "ck:01234567-89ab-cdef-0123-456789abcdef", + defaultKID: ["01234567-89ab-cdef-0123-456789abcdef"], + scheme: "cbcs", + drmSystem: { systemID: Catalog.SYSTEM.widevine }, + }; + const collided = Catalog.checkRenditions(toHang({ contentProtections: [split, colliding], tracks: [track] })); + expect(Object.keys(collided.drm ?? {})).toEqual(["ck:01234567-89ab-cdef-0123-456789abcdef"]); + expect(collided.drm?.["ck:01234567-89ab-cdef-0123-456789abcdef"]?.scheme).toBe("cbcs"); + expect(collided.video).toBeUndefined(); +}); diff --git a/js/watch/src/msf.ts b/js/watch/src/msf.ts index 9d7219cdc4c..cd78ff7d048 100644 --- a/js/watch/src/msf.ts +++ b/js/watch/src/msf.ts @@ -1,5 +1,5 @@ import type * as Catalog from "@moq/hang/catalog"; -import { u53 } from "@moq/hang/catalog"; +import { DrmSchema, u53 } from "@moq/hang/catalog"; import * as Util from "@moq/hang/util"; import type * as Msf from "@moq/msf"; import { Path } from "@moq/net"; @@ -119,8 +119,107 @@ function namesCatalog(namespace: Path.Valid, base: Path.Valid): boolean { return endsWith(a, b) || endsWith(b, a); } +const HYPHENATED = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; +const SIMPLE = /^[0-9a-f]{32}$/i; + +/** + * A UUID in lowercase hyphenated form, from any form moq-mux's `uuid` parse accepts: simple, + * hyphenated, braced, or URN. `undefined` for anything else. + */ +function normalizeUuid(text: string): string | undefined { + let hyphenated: string; + if (text.length === 32) { + if (!SIMPLE.test(text)) return undefined; + const at = [0, 8, 12, 16, 20, 32]; + hyphenated = at + .slice(1) + .map((end, i) => text.slice(at[i], end)) + .join("-"); + } else if (text.length === 36) { + hyphenated = text; + } else if (text.length === 38 && text.startsWith("{") && text.endsWith("}")) { + hyphenated = text.slice(1, -1); + } else if (text.length === 45 && text.slice(0, 9).toLowerCase() === "urn:uuid:") { + hyphenated = text.slice(9); + } else { + return undefined; + } + return HYPHENATED.test(hyphenated) ? hyphenated.toLowerCase() : undefined; +} + +/** + * The hang `drm` entries one MSF content protection describes, by ID, or `undefined` when the hang + * catalog cannot carry it. + * + * Mirrors moq-mux: an entry naming several default keys becomes one hang entry per key, + * `{refID}:{kid}` with the key in lowercase hyphenated form, and a split ID that is another + * entry's refID drops the entry rather than overwriting the other. + */ +function splitEntry(cp: Msf.ContentProtection, refIDs: Set): [string, Catalog.Drm][] | undefined { + if (cp.defaultKID.length === 0) return undefined; + const system = normalizeUuid(cp.drmSystem.systemID); + if (system === undefined) return undefined; + const entries: [string, Catalog.Drm][] = []; + for (const raw of cp.defaultKID) { + const kid = normalizeUuid(raw); + if (kid === undefined) return undefined; + const id = cp.defaultKID.length === 1 ? cp.refID : `${cp.refID}:${kid}`; + if (entries.some(([other]) => other === id)) return undefined; + if (cp.defaultKID.length > 1 && refIDs.has(id)) return undefined; + // The scheme, the servers' URLs, and the pssh are checked the way the hang catalog reads them. + const entry = DrmSchema.safeParse({ + system, + scheme: cp.scheme, + kid, + pssh: cp.drmSystem.pssh, + license: cp.drmSystem.laURL, + certificate: cp.drmSystem.certURL, + }); + if (!entry.success) return undefined; + entries.push([id, entry.data]); + } + return entries; +} + +/** + * The hang `drm` entries an MSF catalog's `contentProtections` describe, and each `refID`'s entry + * IDs, `undefined` for an entry the hang catalog cannot carry. + * + * A track referencing it lists all of a split entry's IDs here, which is enough to know it is + * protected; moq-mux picks the one its init's `tenc` names. + */ +function toDrm(msf: Msf.Catalog): { drm: Record; byRef: Map } { + const drm: Record = Object.create(null); + const byRef = new Map(); + const refIDs = new Set((msf.contentProtections ?? []).map((cp) => cp.refID)); + for (const cp of msf.contentProtections ?? []) { + const entries = splitEntry(cp, refIDs); + byRef.set( + cp.refID, + entries?.map(([id]) => id), + ); + for (const [id, entry] of entries ?? []) drm[id] = entry; + } + return { drm, byRef }; +} + /** Convert an MSF catalog to a hang catalog Root. */ export function toHang(msf: Msf.Catalog, base?: Path.Valid): Catalog.Root { + const { drm, byRef } = toDrm(msf); + // The hang entry IDs a track's refIDs name; @moq/msf refuses a refID the root lacks. A track + // protected by an entry that was dropped is encrypted in a way the catalog cannot describe, so + // it is dropped too (`undefined`) rather than signalled as clear; the other tracks remain. + const protection = (track: Msf.Track): { drm?: string[] } | undefined => { + const ids: string[] = []; + for (const ref of track.contentProtectionRefIDs ?? []) { + if (!byRef.has(ref)) continue; + const split = byRef.get(ref); + if (split === undefined) return undefined; + ids.push(...split); + } + return ids.length > 0 ? { drm: ids } : {}; + }; + // Track names are untrusted keys; a null prototype keeps `__proto__` an ordinary rendition. const videoRenditions: Record = Object.create(null); const audioRenditions: Record = Object.create(null); @@ -151,10 +250,12 @@ export function toHang(msf: Msf.Catalog, base?: Path.Valid): Catalog.Root { } if (track.role === "video") { const config = toVideoConfig(track); - if (config) videoRenditions[track.name] = { ...config, broadcast }; + const drm = protection(track); + if (config && drm) videoRenditions[track.name] = { ...config, broadcast, ...drm }; } else if (track.role === "audio") { const config = toAudioConfig(track); - if (config) audioRenditions[track.name] = { ...config, broadcast }; + const drm = protection(track); + if (config && drm) audioRenditions[track.name] = { ...config, broadcast, ...drm }; } else if ( track.role === "caption" || track.role === "subtitle" || @@ -192,5 +293,7 @@ export function toHang(msf: Msf.Catalog, base?: Path.Valid): Catalog.Root { if (Object.keys(textRenditions).length > 0) root.text = { renditions: textRenditions }; + if (Object.keys(drm).length > 0) root.drm = drm; + return root; } diff --git a/js/watch/src/protected.test.ts b/js/watch/src/protected.test.ts new file mode 100644 index 00000000000..bc7c29fb14b --- /dev/null +++ b/js/watch/src/protected.test.ts @@ -0,0 +1,23 @@ +import { expect, mock, test } from "bun:test"; +import * as Catalog from "@moq/hang/catalog"; + +// Bun cannot load the blob-URL worklet import, so mock it before the decoder imports it. +mock.module("./audio/render-worklet.ts?worklet", () => ({ default: async () => "blob:fake-render" })); +const Audio = await import("./audio/decoder"); +const Video = await import("./video/decoder"); + +// WebCodecs cannot decrypt, so `` treats a protected rendition as unplayable before it +// asks the decoder, whatever the codec. +test("protected renditions are unplayable", async () => { + const cmaf = { kind: "cmaf", init: "AAAA" }; + const video = Catalog.VideoConfigSchema.parse({ codec: "avc1.64001f", container: cmaf, drm: ["1"] }); + const audio = Catalog.AudioConfigSchema.parse({ + codec: "mp4a.40.2", + container: cmaf, + sampleRate: 48000, + numberOfChannels: 2, + drm: ["1"], + }); + expect(await Video.Decoder.supported(video)).toBe(false); + expect(await Audio.Decoder.supported(audio)).toBe(false); +}); diff --git a/js/watch/src/video/config.ts b/js/watch/src/video/config.ts index fd5cd257781..0faade426c7 100644 --- a/js/watch/src/video/config.ts +++ b/js/watch/src/video/config.ts @@ -3,7 +3,7 @@ import type * as Catalog from "@moq/hang/catalog"; /** The catalog fields that determine the demuxer and WebCodecs decoder instance. */ export type DecoderConfig = Pick< Catalog.VideoConfig, - "codec" | "container" | "description" | "displayAspectWidth" | "displayAspectHeight" + "codec" | "container" | "description" | "displayAspectWidth" | "displayAspectHeight" | "drm" > & { optimizeForLatency: boolean; interlaced: boolean; @@ -29,6 +29,7 @@ export function playbackIdentity(config: Catalog.VideoConfig): PlaybackIdentity description: config.description, displayAspectWidth: config.displayAspectWidth, displayAspectHeight: config.displayAspectHeight, + drm: config.drm, optimizeForLatency: config.optimizeForLatency ?? true, interlaced: config.interlaced ?? false, }, diff --git a/js/watch/src/video/decoder.ts b/js/watch/src/video/decoder.ts index 38503bb77bc..7f4de364ed0 100644 --- a/js/watch/src/video/decoder.ts +++ b/js/watch/src/video/decoder.ts @@ -1272,6 +1272,12 @@ async function supported(config: Catalog.VideoConfig): Promise { return false; } + // WebCodecs cannot decrypt, so an encrypted rendition is unplayable here. + if (Catalog.isProtected(config)) { + console.warn(`video: ignoring protected rendition ${config.codec}: playback cannot decrypt it`); + return false; + } + let description: Uint8Array | undefined; if (config.description) { description = Util.Hex.toBytes(config.description); diff --git a/rs/hang/Cargo.toml b/rs/hang/Cargo.toml index cba18cab5eb..ee2201d1a5f 100644 --- a/rs/hang/Cargo.toml +++ b/rs/hang/Cargo.toml @@ -23,6 +23,7 @@ serde_json = { workspace = true } serde_with = { workspace = true, features = ["base64"] } thiserror = { workspace = true } url = { workspace = true, features = ["serde"] } +uuid = { workspace = true, features = ["serde"] } [dependencies.derive_more] version = "2" diff --git a/rs/hang/fixtures/catalog-parity.json b/rs/hang/fixtures/catalog-parity.json index 92cf1a0f078..59d91f41004 100644 --- a/rs/hang/fixtures/catalog-parity.json +++ b/rs/hang/fixtures/catalog-parity.json @@ -3759,5 +3759,367 @@ } }, "refused": true + }, + { + "name": "drm entries protect video and audio", + "catalog": { + "drm": { + "1": { + "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + "scheme": "cbcs", + "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90", + "pssh": "AAAA", + "license": { + "url": "https://license.example.com/", + "type": "EME-1.0" + } + }, + "2": { + "system": "edef8ba9-79d6-4ace-a3c8-27dcd51d21ed", + "scheme": "cbcs", + "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90", + "certificate": { + "url": "https://cert.example.com/" + } + } + }, + "video": { + "renditions": { + "v": { + "codec": "avc1.64001f", + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1", "2"] + } + } + }, + "audio": { + "renditions": { + "a": { + "codec": "mp4a.40.2", + "sampleRate": 48000, + "numberOfChannels": 2, + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1"] + } + } + } + }, + "video": ["v"], + "audio": ["a"] + }, + { + "name": "drm unknown entry members are ignored", + "catalog": { + "drm": { + "1": { + "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + "scheme": "cbcs", + "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90", + "future": true + } + }, + "video": { + "renditions": { + "v": { + "codec": "avc1.64001f", + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1"] + } + } + }, + "audio": { + "renditions": { + "a": { + "codec": "mp4a.40.2", + "sampleRate": 48000, + "numberOfChannels": 2, + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1"] + } + } + } + }, + "video": ["v"], + "audio": ["a"] + }, + { + "name": "drm unreferenced entry is accepted", + "catalog": { + "drm": { + "1": { + "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + "scheme": "cbcs", + "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90" + } + } + } + }, + { + "name": "drm dangling video reference refuses the catalog", + "catalog": { + "drm": { + "1": { + "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + "scheme": "cbcs", + "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90" + } + }, + "video": { + "renditions": { + "v": { + "codec": "avc1.64001f", + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["2"] + } + } + }, + "audio": { + "renditions": { + "a": { + "codec": "mp4a.40.2", + "sampleRate": 48000, + "numberOfChannels": 2, + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1"] + } + } + } + }, + "refused": true + }, + { + "name": "drm dangling audio reference refuses the catalog", + "catalog": { + "drm": {}, + "video": { + "renditions": { + "v": { + "codec": "avc1.64001f", + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": [] + } + } + }, + "audio": { + "renditions": { + "a": { + "codec": "mp4a.40.2", + "sampleRate": 48000, + "numberOfChannels": 2, + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1"] + } + } + } + }, + "refused": true + }, + { + "name": "drm scheme cens refuses the catalog", + "catalog": { + "drm": { + "1": { + "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + "scheme": "cens", + "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90" + } + }, + "video": { + "renditions": { + "v": { + "codec": "avc1.64001f", + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1"] + } + } + }, + "audio": { + "renditions": { + "a": { + "codec": "mp4a.40.2", + "sampleRate": 48000, + "numberOfChannels": 2, + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1"] + } + } + } + }, + "refused": true + }, + { + "name": "drm malformed system refuses the catalog", + "catalog": { + "drm": { + "1": { + "system": "widevine", + "scheme": "cbcs", + "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90" + } + }, + "video": { + "renditions": { + "v": { + "codec": "avc1.64001f", + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1"] + } + } + }, + "audio": { + "renditions": { + "a": { + "codec": "mp4a.40.2", + "sampleRate": 48000, + "numberOfChannels": 2, + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1"] + } + } + } + }, + "refused": true + }, + { + "name": "drm entry without kid refuses the catalog", + "catalog": { + "drm": { + "1": { + "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + "scheme": "cenc" + } + } + }, + "refused": true + }, + { + "name": "drm license without url refuses the catalog", + "catalog": { + "drm": { + "1": { + "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + "scheme": "cbcs", + "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90", + "license": { + "type": "EME-1.0" + } + } + }, + "video": { + "renditions": { + "v": { + "codec": "avc1.64001f", + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1"] + } + } + }, + "audio": { + "renditions": { + "a": { + "codec": "mp4a.40.2", + "sampleRate": 48000, + "numberOfChannels": 2, + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1"] + } + } + } + }, + "refused": true + }, + { + "name": "drm references must be a list", + "catalog": { + "drm": { + "1": { + "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + "scheme": "cbcs", + "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90" + } + }, + "video": { + "renditions": { + "v": { + "codec": "avc1.64001f", + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": "1" + } + } + }, + "audio": { + "renditions": { + "a": { + "codec": "mp4a.40.2", + "sampleRate": 48000, + "numberOfChannels": 2, + "container": { + "kind": "cmaf", + "init": "AAAA" + }, + "drm": ["1"] + } + } + } + }, + "refused": true + }, + { + "name": "drm unhyphenated kid refuses the catalog", + "catalog": { + "drm": { + "1": { + "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + "scheme": "cenc", + "kid": "a1b2c3d4e5f60718293a4b5c6d7e8f90" + } + } + }, + "refused": true } ] diff --git a/rs/hang/src/catalog/audio/mod.rs b/rs/hang/src/catalog/audio/mod.rs index 177afe90eea..5bacf93f4c7 100644 --- a/rs/hang/src/catalog/audio/mod.rs +++ b/rs/hang/src/catalog/audio/mod.rs @@ -175,6 +175,16 @@ pub struct AudioConfig { #[serde(default)] pub container: Container, + /// The IDs of the root `drm` entries protecting this rendition, one per DRM system that can + /// decrypt it. Empty means the rendition is clear, and is left off the wire. + /// + /// Common Encryption is defined for CMAF, so a protected rendition is a [`Cmaf`](Container::Cmaf) + /// one in practice: its init segment carries the encrypted sample entry (`encv`/`enca`), while + /// `codec` names the codec it wraps. A consumer that cannot decrypt treats a protected + /// rendition as undecodable. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub drm: Vec, + /// The maximum delay between a frame being ready and the publisher flushing it. /// The player's jitter buffer should be larger than this value. /// If not provided, the player should assume each frame is flushed immediately. @@ -240,6 +250,7 @@ impl AudioConfig { enabled: true, description: None, container: Container::default(), + drm: Vec::new(), jitter: None, warmup: None, delay: None, diff --git a/rs/hang/src/catalog/drm.rs b/rs/hang/src/catalog/drm.rs new file mode 100644 index 00000000000..1d60c6ff736 --- /dev/null +++ b/rs/hang/src/catalog/drm.rs @@ -0,0 +1,253 @@ +//! Content protection: the DRM systems that encrypt a broadcast's CMAF renditions. +//! +//! The catalog's root `drm` section maps an entry ID to an [`Entry`], and each protected video or +//! audio rendition lists the IDs of the entries that protect it in its `drm` field. A rendition +//! that lists none is clear. Every entry names one DRM system, one Common Encryption +//! [`Scheme`], and one default key, so each maps onto one of draft-ietf-moq-cmsf's +//! `contentProtections` and a rendition's IDs onto its `contentProtectionRefIDs`; a CMSF entry +//! naming several default keys splits into one entry per key on parse. +//! +//! ```json +//! { +//! "drm": { +//! "1": { +//! "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", +//! "scheme": "cbcs", +//! "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90", +//! "pssh": "AAAANHBzc2gB...", +//! "license": { "url": "https://license.example.com/clearkey" } +//! } +//! }, +//! "video": { "renditions": { "hd": { "codec": "avc1.64001f", "drm": ["1"], "...": "..." } } } +//! } +//! ``` + +use bytes::Bytes; +use serde::{Deserialize, Serialize}; +use serde_with::{base64::Base64, serde_as}; + +/// Re-export of the [`uuid`] crate's UUID, which system and key IDs are. +pub use uuid::Uuid; + +/// Well-known DRM system IDs, as draft-ietf-moq-cmsf lists them. +pub mod system { + /// Google Widevine. + pub const WIDEVINE: uuid::Uuid = uuid::uuid!("edef8ba9-79d6-4ace-a3c8-27dcd51d21ed"); + /// Microsoft PlayReady. + pub const PLAYREADY: uuid::Uuid = uuid::uuid!("9a04f079-9840-4286-ab92-e65be0885f95"); + /// Apple FairPlay Streaming. + pub const FAIRPLAY: uuid::Uuid = uuid::uuid!("94ce86fb-07ff-4f43-adb8-93d2fa968ca2"); + /// W3C ClearKey, the Common System ID: for testing, since the key travels in the clear. + pub const CLEARKEY: uuid::Uuid = uuid::uuid!("1077efec-c0b2-4d02-ace3-3c1e52e2fb4b"); + + /// The system ID a short name (`widevine`, `playready`, `fairplay`, `clearkey`) stands for. + pub fn named(name: &str) -> Option { + Some(match name.to_ascii_lowercase().as_str() { + "widevine" => WIDEVINE, + "playready" => PLAYREADY, + "fairplay" => FAIRPLAY, + "clearkey" => CLEARKEY, + _ => return None, + }) + } +} + +/// One DRM system protecting renditions under one scheme and one default key. +/// +/// The scheme, key, system, and `pssh` are the media's own (the init segment's `schm`, `tenc`, +/// and `pssh` boxes); the license and certificate servers are the publisher's, which the media +/// does not carry. +/// +/// Marked `#[non_exhaustive]` so optional fields can be added later. External callers build one +/// with [`Entry::new`]. +#[serde_as] +#[serde_with::skip_serializing_none] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +#[non_exhaustive] +pub struct Entry { + /// The DRM system ID, as a UUID string (see [`system`]). + #[serde_as(as = "Hyphenated")] + pub system: uuid::Uuid, + + /// How the samples are encrypted. + pub scheme: Scheme, + + /// The default key ID (`tenc`'s `default_KID`), as a UUID string. + #[serde_as(as = "Hyphenated")] + pub kid: uuid::Uuid, + + /// The system's whole `pssh` box from the init segment, base64: what the player hands its + /// DRM system to request a license. + #[serde(default)] + #[serde_as(as = "Option")] + pub pssh: Option, + + /// Where to request a license. + #[serde(default)] + pub license: Option, + + /// Where to fetch the server certificate, which systems such as FairPlay need first. + #[serde(default)] + pub certificate: Option, +} + +impl Entry { + /// An entry with no `pssh` and no servers. + pub fn new(system: uuid::Uuid, scheme: Scheme, kid: uuid::Uuid) -> Self { + Self { + system, + scheme, + kid, + pssh: None, + license: None, + certificate: None, + } + } +} + +/// A UUID in its hyphenated form only, as draft-ietf-moq-cmsf writes one, refusing the simple, +/// braced, and URN forms [`uuid::Uuid`]'s own decoder also takes. +struct Hyphenated; + +impl serde_with::SerializeAs for Hyphenated { + fn serialize_as(source: &uuid::Uuid, serializer: S) -> Result { + source.hyphenated().serialize(serializer) + } +} + +impl<'de> serde_with::DeserializeAs<'de, uuid::Uuid> for Hyphenated { + fn deserialize_as>(deserializer: D) -> Result { + let text = String::deserialize(deserializer)?; + match text.len() { + 36 => uuid::Uuid::try_parse(&text).map_err(serde::de::Error::custom), + _ => Err(serde::de::Error::custom(format_args!( + "expected a hyphenated UUID: {text:?}" + ))), + } + } +} + +/// The Common Encryption scheme (ISO/IEC 23001-7) of a protected rendition. +/// +/// draft-ietf-moq-cmsf allows only these two; a catalog naming another is refused. +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[serde(rename_all = "lowercase")] +pub enum Scheme { + /// AES-CTR, full sample or subsample. + Cenc, + /// AES-CBC pattern encryption with a constant IV, which FairPlay and most hardware decode. + Cbcs, +} + +impl Scheme { + /// The scheme's four-character code, as `schm` carries it. + pub fn as_str(&self) -> &'static str { + match self { + Self::Cenc => "cenc", + Self::Cbcs => "cbcs", + } + } +} + +impl std::fmt::Display for Scheme { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + +impl std::str::FromStr for Scheme { + type Err = crate::Error; + + fn from_str(s: &str) -> crate::Result { + match s { + "cenc" => Ok(Self::Cenc), + "cbcs" => Ok(Self::Cbcs), + other => Err(crate::Error::UnsupportedScheme(other.to_string())), + } + } +} + +/// The servers a publisher supplies for one DRM system, which the media does not carry. +/// +/// An importer copies them into each [`Entry`] of that system. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +#[non_exhaustive] +pub struct Servers { + /// Where to request a license. + pub license: Option, + /// Where to fetch the server certificate. + pub certificate: Option, +} + +impl Servers { + /// A license server and no certificate server. + pub fn license(url: url::Url) -> Self { + Self { + license: Some(Server::new(url)), + certificate: None, + } + } +} + +/// A license or certificate server. +/// +/// Marked `#[non_exhaustive]` so optional fields can be added later. External callers build one +/// with [`Server::new`]. +#[serde_with::skip_serializing_none] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] +pub struct Server { + /// The server's URL. + pub url: url::Url, + + /// The protocol a license server speaks (e.g. `EME-1.0`), or the MIME type of a certificate + /// (e.g. `application/pkcs7-mime`). + #[serde(default, rename = "type")] + pub kind: Option, +} + +impl Server { + /// A server at `url`, of no declared type. + pub fn new(url: url::Url) -> Self { + Self { url, kind: None } + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn an_entry_round_trips_with_hang_member_names() { + let json = serde_json::json!({ + "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + "scheme": "cbcs", + "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90", + "pssh": "AAAA", + "license": { "url": "https://license.example.com/", "type": "EME-1.0" }, + }); + let entry: Entry = serde_json::from_value(json.clone()).unwrap(); + assert_eq!(entry.system, system::CLEARKEY); + assert_eq!(entry.scheme, Scheme::Cbcs); + assert_eq!(entry.pssh.as_deref(), Some(&[0u8, 0, 0][..])); + assert_eq!(entry.license.as_ref().unwrap().kind.as_deref(), Some("EME-1.0")); + assert_eq!(serde_json::to_value(&entry).unwrap(), json); + } + + #[test] + fn another_scheme_is_refused() { + let json = serde_json::json!({ + "system": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", + "scheme": "cens", + "kid": "a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90", + }); + assert!(serde_json::from_value::(json).is_err()); + } + + #[test] + fn short_system_names_resolve() { + assert_eq!(system::named("Widevine"), Some(system::WIDEVINE)); + assert_eq!(system::named("primetime"), None); + } +} diff --git a/rs/hang/src/catalog/mod.rs b/rs/hang/src/catalog/mod.rs index 8d792d033dc..e3a6c2b78cb 100644 --- a/rs/hang/src/catalog/mod.rs +++ b/rs/hang/src/catalog/mod.rs @@ -11,6 +11,7 @@ mod clock; mod consumer; mod container; mod contribution; +pub mod drm; mod echo; mod hex; mod json; diff --git a/rs/hang/src/catalog/root.rs b/rs/hang/src/catalog/root.rs index 73b752b1819..8dd5b3718b6 100644 --- a/rs/hang/src/catalog/root.rs +++ b/rs/hang/src/catalog/root.rs @@ -91,6 +91,14 @@ pub struct Catalog { /// Omitted from the wire when empty. A malformed entry refuses the catalog. pub contributions: std::collections::BTreeMap, + /// The DRM systems protecting the broadcast's CMAF renditions, keyed by entry ID. + /// + /// A protected video or audio rendition lists the IDs of its entries in its own `drm` field, + /// and one listing an ID missing here is refused by [`check_renditions`](Self::check_renditions). + /// Omitted from the wire when empty. A malformed entry refuses the catalog. See + /// [`drm`](crate::catalog::drm). + pub drm: std::collections::BTreeMap, + /// Application-defined root sections, flattened alongside the base catalog sections. /// /// The root `version` is reserved: serializing computes it from the base sections, and fails if @@ -168,6 +176,9 @@ struct Wire { #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")] contributions: std::collections::BTreeMap, + #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")] + drm: std::collections::BTreeMap, + #[serde(flatten, serialize_with = "serialize_ext")] ext: E, } @@ -203,6 +214,7 @@ impl From> for Catalog { stats, echo, contributions, + drm, ext, } = wire; Self { @@ -216,6 +228,7 @@ impl From> for Catalog { stats, echo, contributions, + drm, ext, } } @@ -270,10 +283,11 @@ impl Catalog<()> { } impl Catalog { - /// Refuse a catalog with an ID naming both a video and an audio rendition, or with a - /// rendition field out of range, such as a zero sample rate or a projection out of bounds. + /// Refuse a catalog with an ID naming both a video and an audio rendition, with a rendition + /// field out of range, such as a zero sample rate or a projection out of bounds, or with a + /// rendition listing a `drm` entry the root lacks. /// - /// Decoding already refuses an out-of-range rendition, so this catches a catalog built by hand. + /// Decoding already refuses an out-of-range rendition, so that catches a catalog built by hand. pub fn check_renditions(&self) -> Result<()> { use crate::catalog::Rendition; @@ -291,6 +305,18 @@ impl Catalog { for config in self.audio.renditions.values() { config.check()?; } + + // A protected rendition whose entry is missing would read as clear, or name no system. + let drm = (self.video.renditions.iter().map(|(id, config)| (id, &config.drm))) + .chain(self.audio.renditions.iter().map(|(id, config)| (id, &config.drm))); + for (rendition, ids) in drm { + if let Some(id) = ids.iter().find(|id| !self.drm.contains_key(*id)) { + return Err(crate::Error::MissingDrm { + rendition: rendition.clone(), + id: id.clone(), + }); + } + } Ok(()) } @@ -318,13 +344,15 @@ impl Catalog { (renamed || warmup).then_some(4) } - /// Copy the media sections into a catalog with no application extension. + /// Copy the media sections, and the `drm` entries they reference, into a catalog with no + /// application extension. pub fn media(&self) -> Catalog<()> { Catalog { video: self.video.clone(), audio: self.audio.clone(), media: self.media, text: self.text.clone(), + drm: self.drm.clone(), ..Default::default() } } @@ -485,6 +513,44 @@ mod test { assert!(failures.is_empty(), "{}", failures.join("\n")); } + /// A protected rendition references a root `drm` entry, which survives a round trip and the + /// media copy; a clear catalog writes no `drm` at all, and a dangling reference is refused. + #[test] + fn drm_entries_round_trip_and_dangling_references_are_refused() { + use crate::catalog::drm; + + let mut catalog = Catalog::<()>::default(); + assert!(!catalog.to_json().unwrap().contains("drm")); + + let kid = uuid::uuid!("a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90"); + catalog.drm.insert( + "1".into(), + drm::Entry::new(drm::system::CLEARKEY, drm::Scheme::Cbcs, kid), + ); + let mut video = VideoConfig::new(H264 { + profile: 0x64, + constraints: 0, + level: 0x1f, + inline: false, + }); + video.container = Container::Cmaf { + init: bytes::Bytes::from_static(b"init"), + }; + video.drm = vec!["1".into()]; + catalog.video.renditions.insert("hd".into(), video); + catalog.check_renditions().unwrap(); + + let decoded = Catalog::<()>::from_str(&catalog.to_json().unwrap()).unwrap(); + assert_eq!(decoded, catalog); + assert_eq!(catalog.media().drm, catalog.drm); + + catalog.drm.clear(); + assert!(matches!( + catalog.check_renditions(), + Err(crate::Error::MissingDrm { rendition, id }) if rendition == "hd" && id == "1" + )); + } + /// The rendition count is the publisher's to choose: a catalog listing many is accepted. #[test] fn many_renditions_are_accepted() { @@ -785,6 +851,7 @@ mod test { reorder: None, optimize_for_latency: None, container: Container::Legacy, + drm: Vec::new(), warmup: None, jitter: Some(std::time::Duration::from_millis(100)), delay: Some(std::time::Duration::from_millis(200)), @@ -807,6 +874,7 @@ mod test { enabled: true, description: None, container: Container::Legacy, + drm: Vec::new(), warmup: None, jitter: Some(std::time::Duration::from_millis(40)), delay: None, diff --git a/rs/hang/src/catalog/video/mod.rs b/rs/hang/src/catalog/video/mod.rs index db21279c4d2..3bef6a49ac3 100644 --- a/rs/hang/src/catalog/video/mod.rs +++ b/rs/hang/src/catalog/video/mod.rs @@ -317,6 +317,16 @@ pub struct VideoConfig { #[serde(default)] pub container: Container, + /// The IDs of the root `drm` entries protecting this rendition, one per DRM system that can + /// decrypt it. Empty means the rendition is clear, and is left off the wire. + /// + /// Common Encryption is defined for CMAF, so a protected rendition is a [`Cmaf`](Container::Cmaf) + /// one in practice: its init segment carries the encrypted sample entry (`encv`/`enca`), while + /// `codec` names the codec it wraps. A consumer that cannot decrypt treats a protected + /// rendition as undecodable. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub drm: Vec, + /// The maximum delay between a frame being ready and the publisher flushing it. /// The player's jitter buffer should be larger than this value. /// If not provided, the player should assume each frame is flushed immediately. @@ -394,6 +404,7 @@ impl VideoConfig { reorder: None, optimize_for_latency: None, container: Container::default(), + drm: Vec::new(), jitter: None, warmup: None, delay: None, diff --git a/rs/hang/src/error.rs b/rs/hang/src/error.rs index ce4a66b5a2e..a811eee2517 100644 --- a/rs/hang/src/error.rs +++ b/rs/hang/src/error.rs @@ -17,6 +17,19 @@ pub enum Error { #[error("catalog rendition ID is both video and audio: {0}")] DuplicateRendition(String), + /// A protected rendition lists a `drm` entry the catalog's root `drm` section lacks. + #[error("rendition {rendition} references missing drm entry {id}")] + MissingDrm { + /// The rendition listing the entry. + rendition: String, + /// The entry ID the root lacks. + id: String, + }, + + /// A Common Encryption scheme other than `cenc` and `cbcs`. + #[error("unsupported encryption scheme: {0}")] + UnsupportedScheme(String), + /// A catalog needs a newer moq-hang draft than this reader implements. #[error("unsupported catalog version {0}: upgrade to read it")] UnsupportedVersion(u64), diff --git a/rs/moq-audio/src/decode/decoder.rs b/rs/moq-audio/src/decode/decoder.rs index 462981480fa..c2c954868dd 100644 --- a/rs/moq-audio/src/decode/decoder.rs +++ b/rs/moq-audio/src/decode/decoder.rs @@ -54,7 +54,15 @@ impl Decoder { /// catalog fields directly and requires an absent `description`. AAC reads /// its AudioSpecificConfig; without one, legacy and LOC packets carry ADTS headers, /// while demuxed CMAF samples remain raw. + /// + /// A protected rendition (one listing `drm` entries) is refused, since its samples are + /// encrypted and nothing here decrypts them. pub fn new(catalog: &hang::catalog::AudioConfig, config: &Config) -> Result { + if !catalog.drm.is_empty() { + return Err(Error::Unsupported( + "the rendition is encrypted, and decryption is not supported".into(), + )); + } let backend = backend::open(catalog, config)?; let delay = backend.delay(); Ok(Self { @@ -406,6 +414,17 @@ pub(crate) mod tests { } } + /// A protected rendition's samples are encrypted, so it is refused before any backend opens. + #[test] + fn a_protected_rendition_is_unsupported() { + let mut catalog = hang::catalog::AudioConfig::new(hang::catalog::AudioCodec::Opus, 48_000, 2); + catalog.drm = vec!["1".into()]; + assert!(matches!( + Decoder::new(&catalog, &Config::default()), + Err(Error::Unsupported(_)) + )); + } + /// Without a description the catalog shapes the stream, which has no pre-skip. #[test] fn opus_decodes_without_a_description() { diff --git a/rs/moq-c/MIGRATE.md b/rs/moq-c/MIGRATE.md index 143245df61b..01f08155363 100644 --- a/rs/moq-c/MIGRATE.md +++ b/rs/moq-c/MIGRATE.md @@ -4,6 +4,10 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Protected renditions are hidden + +A catalog snapshot leaves out every protected video and audio rendition: one listing `drm` entries, or a CMAF one whose init holds an encrypted (`encv`/`enca`) sample entry. Its samples are encrypted and nothing here decrypts them, so `moq_consume_video_config`, `moq_consume_audio_config`, and the media readers no longer reach it, rather than handing out ciphertext that has lost its IVs. The C ABI is unchanged; renditions after a hidden one move down an index. + ### Separate datagram tracks Create a separate producer with `moq_publish_datagrams` and `moq_datagram_info`; replace `moq_publish_track_datagram` with `moq_publish_datagrams_append` on that handle, or `moq_publish_datagrams_insert` for an explicit sequence. `moq_consume_datagrams` now takes a subscriber priority before its callback and never subscribes to a stream track. Use `moq_publish_datagrams_demand` and `moq_datagram_demand_priority` for aggregate priority, and `moq_publish_datagrams_requests` / `moq_datagram_request_accept` for on-demand publishing. Finish or abort datagram producers separately. A zeroed datagram info is untimed, unlike the microsecond default of stream-track info. `moq_consume_datagrams` returns a negative code at once when the broadcast has neither the datagram track nor a datagram request handler, and a remote name without one ends its callback with a not-found error. diff --git a/rs/moq-c/src/consume.rs b/rs/moq-c/src/consume.rs index e43df123482..a122e27d0ad 100644 --- a/rs/moq-c/src/consume.rs +++ b/rs/moq-c/src/consume.rs @@ -311,7 +311,7 @@ impl Consume { // `biased` so a pending close always wins over a ready update: a hot // stream must not be able to starve the close signal, and we must not // deliver another update once close has been requested. - let update = tokio::select! { + let mut update = tokio::select! { biased; _ = &mut close => return Ok(()), next = catalog.next() => match next? { @@ -319,6 +319,9 @@ impl Consume { None => return Ok(()), }, }; + // A protected rendition's samples are encrypted, and nothing here decrypts them or + // exposes the `drm` section, so the bindings treat it as one they cannot decode. + moq_mux::catalog::hang::retain_clear(&mut update); // Unfortunately we need to store the codec information on the heap. let audio_codec = update diff --git a/rs/moq-c/src/test.rs b/rs/moq-c/src/test.rs index dc416e220b2..69d4be9b9af 100644 --- a/rs/moq-c/src/test.rs +++ b/rs/moq-c/src/test.rs @@ -853,6 +853,70 @@ fn publish_video_config_replaces_its_own_rendition() { assert_eq!(moq_origin_close(origin), 0); } +/// The `ftyp` and `moov` of an encrypted (ClearKey `cenc`) fMP4 file, ahead of its first fragment. +fn encrypted_init() -> &'static [u8] { + const FILE: &[u8] = include_bytes!("../../moq-mux/src/container/fmp4/test_data/cenc-clearkey.mp4"); + let mut end = 0; + while &FILE[end + 4..end + 8] != b"moof" { + end += u32::from_be_bytes(FILE[end..end + 4].try_into().unwrap()) as usize; + } + &FILE[..end] +} + +/// A protected rendition (here a CMAF one whose init is encrypted) is absent from the catalog the +/// bindings see: they cannot decrypt it, so they treat it as one they cannot decode. +#[test] +fn consume_catalog_hides_protected_renditions() { + let origin = id(moq_origin_create()); + let path = b"catalog-protected"; + let broadcast = publish_broadcast(origin, path); + + let video = |name: &'static str, codec: &'static str, container: moq_container| moq_video_config { + preference: 0, + name: name.as_ptr().cast(), + name_len: name.len(), + label: std::ptr::null(), + label_len: 0, + codec: codec.as_ptr().cast(), + codec_len: codec.len(), + description: std::ptr::null(), + description_len: 0, + coded_width: 640, + coded_height: 360, + container, + }; + let init = encrypted_init(); + let protected = video( + "protected", + "avc1.64001f", + moq_container { + kind: moq_container_kind::MOQ_CONTAINER_KIND_CMAF as u32, + init: init.as_ptr(), + init_len: init.len(), + }, + ); + assert_eq!(unsafe { moq_publish_video_config(broadcast, &protected) }, 0); + let clear = video("clear", "vp8", moq_container::default()); + assert_eq!(unsafe { moq_publish_video_config(broadcast, &clear) }, 0); + + let consume = request_broadcast(origin, path); + let catalog_cb = Callback::new(); + let catalog_task = id(unsafe { moq_consume_catalog(consume, Some(channel_callback), catalog_cb.ptr) }); + let catalog = id(catalog_cb.recv()); + + let mut config = video("", "", moq_container::default()); + assert_eq!(unsafe { moq_consume_video_config(catalog, 0, &mut config) }, 0); + assert_eq!(borrowed_string(config.name, config.name_len).as_deref(), Some("clear")); + assert!(unsafe { moq_consume_video_config(catalog, 1, &mut config) } < 0); + + assert_eq!(moq_consume_catalog_free(catalog), 0); + assert_eq!(moq_consume_catalog_cancel(catalog_task), 0); + assert_eq!(catalog_cb.recv_terminal(), 0); + assert_eq!(moq_consume_close(consume), 0); + assert_eq!(moq_publish_close(broadcast), 0); + assert_eq!(moq_origin_close(origin), 0); +} + /// Manual catalog metadata cannot redefine the decoder identity of an existing raw track. #[test] fn manual_video_identity_change_is_refused() { diff --git a/rs/moq-cli/src/args.rs b/rs/moq-cli/src/args.rs index 8677c5e9330..f6db897437e 100644 --- a/rs/moq-cli/src/args.rs +++ b/rs/moq-cli/src/args.rs @@ -327,6 +327,11 @@ impl Invocation { { capture.validate()?; } + if let Command::Import(import) = command + && let ImportSource::Fmp4(fmp4) = &import.source + { + fmp4.drm()?; + } if let Command::Import(import) = command && import.report && let Some(reason) = import.source.catalog_refusal() @@ -882,11 +887,15 @@ pub enum ImportSource { impl ImportSource { /// The stdin container format, when this source is one of the container formats. + /// + /// Call it only on an [`Invocation::validate`]d invocation, which refuses the flags it cannot + /// turn into a format. pub fn stdin_format(&self) -> Option { Some(match self { Self::Avc3 => PublishFormat::Avc3, Self::Fmp4(args) => PublishFormat::Fmp4 { passthrough: args.passthrough, + drm: (args.drm()).expect("Invocation::validate refuses a system named twice"), }, Self::Ts(args) if args.passthrough => PublishFormat::TsPassthrough { pcr_pid: args.pcr_pid.map(|pid| pid.0), @@ -939,14 +948,96 @@ impl ImportSource { } } -/// The fMP4 stdin container: whether to unwrap its fragments. +/// The fMP4 stdin container: whether to unwrap its fragments, and where a player gets the +/// licenses for encrypted ones. #[derive(usage_rs::Args, Clone)] #[usage(unknown_flags = "error", args_override_self = false)] pub struct Fmp4Import { /// Publish each fragment whole as CMAF instead of splitting it into one frame per sample. /// Keeps the source's exact sample timing, but every player has to parse ISO BMFF. + /// Encrypted (Common Encryption) media needs it. #[usage(long = "cmaf")] pub passthrough: bool, + + /// The license server of a DRM system the media is encrypted for, as `SYSTEM[:TYPE]=URL`. + /// SYSTEM is `widevine`, `playready`, `fairplay`, `clearkey`, or a system ID; TYPE is the + /// protocol the server speaks, such as `EME-1.0`. Once per system; a system the media carries + /// no `pssh` for is refused. + #[usage(long = "drm-license", value_name = "SYSTEM[:TYPE]=URL", requires = "--cmaf")] + pub drm_license: Vec, + + /// The certificate server of a DRM system, such as FairPlay's, as `SYSTEM[:TYPE]=URL`, where + /// TYPE is the certificate's MIME type. Once per system. + #[usage(long = "drm-certificate", value_name = "SYSTEM[:TYPE]=URL", requires = "--cmaf")] + pub drm_certificate: Vec, +} + +impl Fmp4Import { + /// The servers of each DRM system the flags name, refusing a system named twice by one flag. + fn drm(&self) -> anyhow::Result> { + let mut drm = std::collections::BTreeMap::<_, hang::catalog::drm::Servers>::new(); + for (flag, servers) in [ + ("--drm-license", &self.drm_license), + ("--drm-certificate", &self.drm_certificate), + ] { + for server in servers { + let entry = drm.entry(server.system).or_default(); + let slot = match flag { + "--drm-license" => &mut entry.license, + _ => &mut entry.certificate, + }; + anyhow::ensure!(slot.is_none(), "{flag} names DRM system {} twice", server.system); + let mut value = hang::catalog::drm::Server::new(server.url.clone()); + value.kind = server.kind.clone(); + *slot = Some(value); + } + } + Ok(drm) + } +} + +/// An `import fmp4 --drm-license` or `--drm-certificate` value: a DRM system, optionally the +/// server's type, and its URL. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct DrmServer { + /// The DRM system ID. + pub system: hang::catalog::drm::Uuid, + /// The server's protocol, or the certificate's MIME type. + pub kind: Option, + /// The server's URL. + pub url: url::Url, +} + +impl std::str::FromStr for DrmServer { + type Err = String; + + fn from_str(arg: &str) -> Result { + let (system, url) = arg + .split_once('=') + .ok_or_else(|| format!("expected SYSTEM[:TYPE]=URL, got `{arg}`"))?; + // A system ID may be a `urn:uuid:` URN, as DASH writes them. Past that prefix no system ID + // or name holds a colon, so the first one starts the type. + const URN: &str = "urn:uuid:"; + let (urn, rest) = match system.get(..URN.len()) { + Some(prefix) if prefix.eq_ignore_ascii_case(URN) => (true, &system[URN.len()..]), + _ => (false, system), + }; + let (system, kind) = match rest.split_once(':') { + Some((_, "")) => return Err(format!("empty TYPE in `{arg}`")), + Some((system, kind)) => (system, Some(kind.to_string())), + None => (rest, None), + }; + let named = (!urn).then(|| hang::catalog::drm::system::named(system)).flatten(); + let system = named + .or_else(|| hang::catalog::drm::Uuid::try_parse(system).ok()) + .ok_or_else(|| { + format!( + "unknown DRM system `{system}`: expected widevine, playready, fairplay, clearkey, or a system ID" + ) + })?; + let url = url.parse().map_err(|err| format!("invalid URL `{url}`: {err}"))?; + Ok(Self { system, kind, url }) + } } /// The MPEG-TS stdin container: which programs of a multiplex to publish. @@ -1988,6 +2079,83 @@ mod tests { assert!(matches!(format(&[]), Some(PublishFormat::Ts { program: None }))); } + /// `import fmp4 --drm-license` and `--drm-certificate` name a system and need `--cmaf`. + #[test] + fn import_fmp4_takes_drm_servers() { + use hang::catalog::drm; + + let format = |args: &[&str]| { + let argv = ["moq", "import", "fmp4"].into_iter().chain(args.iter().copied()); + let cli = Invocation::try_parse_from(argv).ok()?; + cli.validate().ok()?; + let Command::Import(import) = &cli.stages[0] else { + panic!("an import stage"); + }; + import.source.stdin_format() + }; + let Some(PublishFormat::Fmp4 { passthrough: true, drm }) = format(&[ + "--cmaf", + "--drm-license", + "clearkey=https://license.example.com/clearkey", + "--drm-license", + "edef8ba9-79d6-4ace-a3c8-27dcd51d21ed=https://wv.example.com/", + "--drm-certificate", + "FairPlay:application/pkcs7-mime=https://fps.example.com/cert", + "--drm-license", + "fairplay=https://fps.example.com/license", + "--drm-license", + "URN:UUID:9a04f079-9840-4286-ab92-e65be0885f95:EME-1.0=https://pr.example.com/", + "--drm-certificate", + "urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed=https://wv.example.com/cert", + ]) else { + panic!("an fmp4 import"); + }; + assert_eq!(drm.len(), 4); + let playready = drm[&drm::system::PLAYREADY].license.as_ref().unwrap(); + assert_eq!(playready.kind.as_deref(), Some("EME-1.0")); + assert_eq!(playready.url.as_str(), "https://pr.example.com/"); + assert_eq!( + drm[&drm::system::WIDEVINE].certificate.as_ref().unwrap().url.as_str(), + "https://wv.example.com/cert" + ); + assert!(drm[&drm::system::WIDEVINE].certificate.as_ref().unwrap().kind.is_none()); + assert_eq!( + drm[&drm::system::CLEARKEY].license.as_ref().unwrap().url.as_str(), + "https://license.example.com/clearkey" + ); + assert!(drm[&drm::system::FAIRPLAY].license.is_some()); + let certificate = drm[&drm::system::FAIRPLAY].certificate.as_ref().unwrap(); + assert_eq!(certificate.kind.as_deref(), Some("application/pkcs7-mime")); + assert_eq!(certificate.url.as_str(), "https://fps.example.com/cert"); + assert!(drm[&drm::system::CLEARKEY].license.as_ref().unwrap().kind.is_none()); + + // One server per system and role: a repeat is refused rather than replacing the first. + assert!( + format(&[ + "--cmaf", + "--drm-license", + "clearkey=https://a.example/", + "--drm-license", + "ClearKey:EME-1.0=https://b.example/", + ]) + .is_none(), + "a repeated system" + ); + assert!(format(&["--cmaf", "--drm-license", "clearkey:=https://a.example/"]).is_none()); + + assert!( + format(&["--drm-license", "clearkey=https://a.example/"]).is_none(), + "needs --cmaf" + ); + assert!(format(&["--cmaf", "--drm-license", "primetime=https://a.example/"]).is_none()); + assert!( + format(&["--cmaf", "--drm-license", "urn:uuid:clearkey=https://a.example/"]).is_none(), + "a URN holds a system ID, not a name" + ); + assert!(format(&["--cmaf", "--drm-license", "clearkey"]).is_none()); + assert!(format(&["--cmaf", "--drm-license", "clearkey=not a url"]).is_none()); + } + /// `import srt` takes the same `--program` as `import ts`; `export srt` has no program to pick. #[test] fn import_srt_takes_a_program() { diff --git a/rs/moq-cli/src/publish.rs b/rs/moq-cli/src/publish.rs index 39393d5621e..5766f7c546a 100644 --- a/rs/moq-cli/src/publish.rs +++ b/rs/moq-cli/src/publish.rs @@ -3,12 +3,16 @@ use hang::moq_net; use moq_mux::container::{flv, fmp4, ts}; /// Container format read from stdin on the import (source) side. -#[derive(Clone, Copy)] +#[derive(Clone)] pub enum PublishFormat { /// Raw AVC (H.264) Annex B elementary stream. Avc3, - /// Fragmented MP4 (CMAF), republished whole as CMAF when `passthrough`. - Fmp4 { passthrough: bool }, + /// Fragmented MP4 (CMAF), republished whole as CMAF when `passthrough`, with the license and + /// certificate servers of each DRM system encrypted media is for. + Fmp4 { + passthrough: bool, + drm: std::collections::BTreeMap, + }, /// MPEG-TS (transport stream), limited to one `program` of a multiplex when given. Ts { program: Option }, /// MPEG-TS published whole, paced on `pcr_pid` when given rather than the PMT's clock. @@ -433,9 +437,12 @@ impl Publish { import: Box::new(import), } } - PublishFormat::Fmp4 { passthrough } => { + PublishFormat::Fmp4 { passthrough, drm } => { let timebase = catalog.timebase(); - let fmp4 = fmp4::Import::new(broadcast.clone(), timebase.reserve()).with_passthrough(*passthrough); + let mut fmp4 = fmp4::Import::new(broadcast.clone(), timebase.reserve()).with_passthrough(*passthrough); + for (system, servers) in drm { + fmp4 = fmp4.with_drm(*system, servers.clone()); + } PublishDecoder::Fmp4(Box::new(fmp4)) } PublishFormat::Ts { .. } => unreachable!("TS is handled above with the mpegts catalog extension"), @@ -2135,8 +2142,20 @@ pub(crate) mod tests { for (format, input) in [ (PublishFormat::Ts { program: None }, TS), (PublishFormat::TsPassthrough { pcr_pid: None }, TS), - (PublishFormat::Fmp4 { passthrough: false }, FMP4), - (PublishFormat::Fmp4 { passthrough: true }, FMP4), + ( + PublishFormat::Fmp4 { + passthrough: false, + drm: Default::default(), + }, + FMP4, + ), + ( + PublishFormat::Fmp4 { + passthrough: true, + drm: Default::default(), + }, + FMP4, + ), ] { let origin = moq_tokio::origin::spawn(); let consumer = origin.consume(); diff --git a/rs/moq-ffi/MIGRATE.md b/rs/moq-ffi/MIGRATE.md index d5927e2007d..2fb442635af 100644 --- a/rs/moq-ffi/MIGRATE.md +++ b/rs/moq-ffi/MIGRATE.md @@ -5,6 +5,10 @@ These are the UniFFI names; each language wrapper has its own `MIGRATE.md` in it ## Unreleased +### Protected renditions are hidden + +`MoqMediaCatalogConsumer::next` leaves out every protected video and audio rendition: one listing `drm` entries, or a CMAF one whose init holds an encrypted (`encv`/`enca`) sample entry. Its samples are encrypted and nothing here decrypts them, so a binding no longer subscribes to it and reads ciphertext that has lost its IVs. The API is unchanged. + ### Catalog renditions carry their warmup ([#7864](https://github.com/moq-dev/moq/pull/7864)) `MoqVideo` and `MoqAudio` gain `warmup`, the catalog's decoder warmup, null when absent. `MoqMediaTrackConsumer::subscribe` reads it from the rendition: it starts that much further back than the subscription's min delay and asks the publisher for its max delay plus the warmup, while it still skips at the max delay alone. `MoqAudioConsumer::subscribe` and `MoqVideoConsumer::subscribe` now honor it too. diff --git a/rs/moq-ffi/src/media.rs b/rs/moq-ffi/src/media.rs index 242e2e776b0..33f9a891eda 100644 --- a/rs/moq-ffi/src/media.rs +++ b/rs/moq-ffi/src/media.rs @@ -603,11 +603,17 @@ impl MoqVideoInit { } } +/// Convert a catalog snapshot for the bindings, leaving out the protected renditions. +/// +/// A protected rendition (see [`moq_mux::catalog::hang::protected`]) carries encrypted samples, and +/// nothing here decrypts them or exposes the `drm` section, so the bindings treat it as one they +/// cannot decode. pub(crate) fn convert_catalog(catalog: &moq_mux::catalog::hang::Catalog) -> MoqCatalog { let video = catalog .video .renditions .iter() + .filter(|(_, config)| !moq_mux::catalog::hang::protected(&config.drm, &config.container)) .filter_map(|(name, config)| { Some(( name.clone(), @@ -641,6 +647,7 @@ pub(crate) fn convert_catalog(catalog: &moq_mux::catalog::hang::Catalog>(), ["clear"]); + assert!(converted.audio.is_empty()); + } + #[test] fn catalog_exposes_rendition_labels() { let mut catalog = moq_mux::catalog::hang::Catalog::default(); diff --git a/rs/moq-gst/src/source/imp.rs b/rs/moq-gst/src/source/imp.rs index c42c9782852..a72d301fec8 100644 --- a/rs/moq-gst/src/source/imp.rs +++ b/rs/moq-gst/src/source/imp.rs @@ -1049,6 +1049,9 @@ fn reconcile( } } let mut playable = catalog.clone(); + // Encrypted samples would reach the decoders as ciphertext; this source does not decrypt. Drop + // them before selecting, so a clear rendition is never passed over for a protected one. + moq_mux::catalog::hang::retain_clear(&mut playable); moq_mux::select::Broadcast::default() .video(Default::default()) .audio(Default::default()) diff --git a/rs/moq-hls/src/export/mod.rs b/rs/moq-hls/src/export/mod.rs index c3df8030db7..676029c5439 100644 --- a/rs/moq-hls/src/export/mod.rs +++ b/rs/moq-hls/src/export/mod.rs @@ -813,6 +813,35 @@ mod tests { assert!(renditions.get(Kind::Video, "video").is_none()); } + /// A protected rendition is not served: its segments would lose the `pssh`, `senc`, `saiz`, + /// and `saio` a decrypting player needs. Its clear sibling is. + #[tokio::test(start_paused = true)] + async fn protected_rendition_is_not_advertised() { + let broadcast = moq_net::broadcast::Info::new().produce(); + let source = moq_mux::Source::new(produce_origin().consume(), broadcast.consume()); + let mut catalog = joined(); + let mut protected = hang::catalog::VideoConfig::new(hang::catalog::VideoCodec::VP8); + protected.drm = vec!["1".into()]; + catalog.drm.insert( + "1".into(), + hang::catalog::drm::Entry::new( + hang::catalog::drm::system::CLEARKEY, + hang::catalog::drm::Scheme::Cenc, + hang::catalog::drm::Uuid::nil(), + ), + ); + catalog.video.renditions.insert("protected".to_string(), protected); + catalog.video.renditions.insert( + "clear".to_string(), + hang::catalog::VideoConfig::new(hang::catalog::VideoCodec::VP8), + ); + + let renditions = renditions::Producer::spawned(Config::default().window); + renditions.sync(&source, &catalog); + assert!(renditions.get(Kind::Video, "protected").is_none()); + assert!(renditions.get(Kind::Video, "clear").is_some()); + } + #[tokio::test(start_paused = true)] async fn audio_renditions_follow_catalog_quality_order() { let broadcast = moq_net::broadcast::Info::new().produce(); diff --git a/rs/moq-hls/src/export/renditions.rs b/rs/moq-hls/src/export/renditions.rs index 84a293945cf..45c28e88be6 100644 --- a/rs/moq-hls/src/export/renditions.rs +++ b/rs/moq-hls/src/export/renditions.rs @@ -661,6 +661,11 @@ impl Producer { } pub fn sync(&self, source: &moq_mux::Source, catalog: &Catalog) { + // A protected rendition is neither listed nor held: the segments would lose the `pssh`, + // `senc`, `saiz`, and `saio` a decrypting player needs. + let mut catalog = catalog.clone(); + moq_mux::catalog::hang::retain_clear(&mut catalog); + let catalog = &catalog; let clock = catalog.media.clock; let interval = catalog.media.interval(); diff --git a/rs/moq-msf/src/lib.rs b/rs/moq-msf/src/lib.rs index 0da55733d83..3576aa9ffd9 100644 --- a/rs/moq-msf/src/lib.rs +++ b/rs/moq-msf/src/lib.rs @@ -56,6 +56,12 @@ pub struct Catalog { /// The tracks in this catalog snapshot. pub tracks: Vec, + /// The DRM systems protecting this broadcast's tracks (CMSF 4.1.1). + /// + /// Tracks point at these entries by [`ContentProtection::ref_id`] through + /// [`Track::content_protection_ref_ids`]; an entry is never repeated on a track. + pub content_protections: Vec, + /// The application extension: root members beyond the ones MSF defines. pub ext: E, } @@ -89,7 +95,8 @@ pub struct Catalog { /// `serde_json::Map`, which keeps them verbatim. /// /// An extension MUST NOT name a member after one MSF defines (`version`, `generatedAt`, -/// `isComplete`, `tracks`, `initDataList`); the wire JSON would carry a duplicate key. +/// `isComplete`, `tracks`, `initDataList`, `contentProtections`); the wire JSON would carry a +/// duplicate key. pub trait CatalogExt: Serialize + serde::de::DeserializeOwned + Default + Clone + Send + Unpin + 'static {} // Blanket, so a caller writes no impl: naming the trait is only about readable bounds. @@ -185,6 +192,11 @@ pub struct Track { #[serde(rename = "maxObjSapStartingType")] pub max_obj_sap_starting_type: Option, + /// The [`ContentProtection::ref_id`]s of the catalog's entries that protect this track + /// (CMSF 4.1.2). Empty means the track is not encrypted with Common Encryption. + #[serde(default, rename = "contentProtectionRefIDs", skip_serializing_if = "Vec::is_empty")] + pub content_protection_ref_ids: Vec, + /// Jitter (non-standard extension; not in the MSF/CMSF drafts). /// /// Serialized as a JSON number of milliseconds, matching the hang catalog. @@ -217,6 +229,7 @@ impl Catalog { generated_at: None, is_complete: false, tracks, + content_protections: Vec::new(), ext, } } @@ -261,7 +274,7 @@ fn colliding_member(ext: &E) -> Option { pub fn reserved_root(name: &str) -> bool { matches!( name, - "version" | "generatedAt" | "isComplete" | "tracks" | "initDataList" + "version" | "generatedAt" | "isComplete" | "tracks" | "initDataList" | "contentProtections" ) } @@ -312,6 +325,7 @@ impl Serialize for Catalog { is_complete: self.is_complete, tracks, init_data_list: (!init_data_list.is_empty()).then_some(init_data_list), + content_protections: self.content_protections.clone(), ext: self.ext.clone(), } .serialize(serializer) @@ -338,6 +352,30 @@ impl<'de, E: CatalogExt> Deserialize<'de> for Catalog { } } + // A refID names one entry, so a repeated one leaves its tracks' protection ambiguous. + let mut ref_ids = std::collections::HashSet::new(); + if let Some(repeated) = (wire.content_protections.iter()).find(|cp| !ref_ids.insert(cp.ref_id.as_str())) { + return Err(serde::de::Error::custom(format!( + "MSF content protection refID {:?} is repeated", + repeated.ref_id + ))); + } + + // A track's protection refers to a root entry by refID; one that names none would claim + // encryption the catalog cannot describe, so it is refused like a missing initRef. + for track in &wire.tracks { + if let Some(missing) = track + .content_protection_ref_ids + .iter() + .find(|id| !wire.content_protections.iter().any(|cp| &cp.ref_id == *id)) + { + return Err(serde::de::Error::custom(format!( + "MSF track {:?} references missing content protection {:?}", + track.name, missing + ))); + } + } + let tracks = wire .tracks .into_iter() @@ -380,6 +418,7 @@ impl<'de, E: CatalogExt> Deserialize<'de> for Catalog { generated_at: wire.generated_at, is_complete: wire.is_complete, tracks, + content_protections: wire.content_protections, ext: wire.ext, }) } @@ -402,6 +441,8 @@ struct Wire { #[serde(default)] tracks: Vec, init_data_list: Option>, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + content_protections: Vec, /// The extension's own root members, written flat alongside the ones above. #[serde(flatten)] @@ -508,12 +549,111 @@ impl Track { alt_group: None, max_grp_sap_starting_type: None, max_obj_sap_starting_type: None, + content_protection_ref_ids: Vec::new(), jitter: None, delay: None, } } } +/// One DRM system configuration protecting tracks of the broadcast (CMSF 4.1.1). +/// +/// The samples are encrypted with ISO Common Encryption; the track's initialization data +/// carries the matching `sinf`/`tenc` (CMSF 4.2). draft-ietf-moq-cmsf-01 names no member for +/// its authorization URL (4.1.1.4.4) or robustness (4.1.1.4.6), so neither is carried. +#[serde_with::skip_serializing_none] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] +pub struct ContentProtection { + /// The identifier tracks use to reference this entry. + #[serde(rename = "refID")] + pub ref_id: String, + + /// The default key IDs (`default_KID`) as UUID strings. + #[serde(rename = "defaultKID")] + pub default_kid: Vec, + + /// The Common Encryption scheme, `cenc` or `cbcs`. + pub scheme: String, + + /// The DRM system and where to obtain its license. + #[serde(rename = "drmSystem")] + pub drm_system: DrmSystem, +} + +impl ContentProtection { + /// An entry with the required fields; the DRM system's optional fields start empty. + pub fn new( + ref_id: impl Into, + default_kid: Vec, + scheme: impl Into, + system_id: impl Into, + ) -> Self { + Self { + ref_id: ref_id.into(), + default_kid, + scheme: scheme.into(), + drm_system: DrmSystem::new(system_id), + } + } +} + +/// A DRM system description within a [`ContentProtection`] (CMSF 4.1.1.4). +#[serde_with::skip_serializing_none] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] +pub struct DrmSystem { + /// The DRM system ID as a UUID string, e.g. Widevine's `edef8ba9-79d6-4ace-a3c8-27dcd51d21ed`. + #[serde(rename = "systemID")] + pub system_id: String, + + /// The license acquisition service. + #[serde(rename = "laURL")] + pub la_url: Option, + + /// The certificate service, required by systems such as FairPlay Streaming. + #[serde(rename = "certURL")] + pub cert_url: Option, + + /// A base64 `pssh` box with the system's initialization data. + pub pssh: Option, +} + +impl DrmSystem { + /// A DRM system with only its ID set. + pub fn new(system_id: impl Into) -> Self { + Self { + system_id: system_id.into(), + la_url: None, + cert_url: None, + pssh: None, + } + } +} + +/// A service URL in a [`DrmSystem`], with an optional protocol or MIME type. +#[serde_with::skip_serializing_none] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] +pub struct ProtectionUrl { + /// The service URL. + pub url: String, + + /// The protocol or MIME type, e.g. `EME-1.0`. + #[serde(rename = "type")] + pub kind: Option, +} + +impl ProtectionUrl { + /// A URL without a type. + pub fn new(url: impl Into) -> Self { + Self { + url: url.into(), + kind: None, + } + } +} + /// Packaging mode for an MSF track. #[derive(Debug, Clone, PartialEq, Eq)] pub enum Packaging { @@ -638,6 +778,91 @@ impl<'de> Deserialize<'de> for Role { #[cfg(test)] mod test { + /// The protected-video example of draft-ietf-moq-cmsf-01 section 5.2, trimmed to two DRM + /// systems, survives a parse and a serialize unchanged. + #[test] + fn content_protection_round_trips() { + let json = r#"{ + "version": "draft-01", + "contentProtections": [ + { + "refID": "1", + "defaultKID": ["01234567-89ab-cdef-0123-456789abcdef"], + "scheme": "cbcs", + "drmSystem": { + "systemID": "edef8ba9-79d6-4ace-a3c8-27dcd51d21ed", + "laURL": { "url": "https://widevine-license.example.com/proxy" }, + "pssh": "AAAAP3Bzc2gAAAAA7e+LqXnWSs6jy" + } + }, + { + "refID": "3", + "defaultKID": ["01234567-89ab-cdef-0123-456789abcdef"], + "scheme": "cbcs", + "drmSystem": { + "systemID": "94ce86fb-07ff-4f43-adb8-93d2fa968ca2", + "laURL": { "url": "https://fps-license.example.com/api/licenses" }, + "certURL": { "url": "https://fps-license.example.com/cert", "type": "application/pkcs7-mime" } + } + } + ], + "tracks": [ + { "name": "video_protected", "packaging": "cmaf", "isLive": true, "codec": "avc3.4D401F", "contentProtectionRefIDs": ["1", "3"] }, + { "name": "audio", "packaging": "cmaf", "isLive": true, "codec": "mp4a.40.2" } + ] + }"#; + let catalog: super::Catalog = json.parse().unwrap(); + assert_eq!(catalog.content_protections.len(), 2); + let fairplay = &catalog.content_protections[1].drm_system; + assert_eq!( + fairplay.cert_url.as_ref().unwrap().kind.as_deref(), + Some("application/pkcs7-mime") + ); + assert_eq!(catalog.tracks[0].content_protection_ref_ids, ["1", "3"]); + assert!(catalog.tracks[1].content_protection_ref_ids.is_empty()); + + let again: super::Catalog = catalog.to_json().unwrap().parse().unwrap(); + assert_eq!(again, catalog); + let wire: serde_json::Value = serde_json::from_str(&catalog.to_json().unwrap()).unwrap(); + assert!( + wire["tracks"][1].get("contentProtectionRefIDs").is_none(), + "an unprotected track carries no refs" + ); + assert_eq!( + wire["contentProtections"][0]["drmSystem"]["systemID"], + "edef8ba9-79d6-4ace-a3c8-27dcd51d21ed" + ); + } + + /// A catalog without protection writes no `contentProtections` member at all. + #[test] + fn no_protection_writes_nothing() { + let catalog = super::Catalog::new(vec![super::Track::new("video", super::Packaging::Cmaf)]); + let wire: serde_json::Value = serde_json::from_str(&catalog.to_json().unwrap()).unwrap(); + assert!(wire.get("contentProtections").is_none()); + assert!(wire["tracks"][0].get("contentProtectionRefIDs").is_none()); + } + + /// A track that references an entry the root lacks is refused, like a missing initRef. + #[test] + fn a_dangling_protection_ref_is_refused() { + let json = r#"{ "version": "draft-01", "tracks": [ + { "name": "video", "packaging": "cmaf", "isLive": true, "contentProtectionRefIDs": ["9"] } + ] }"#; + let err = json.parse::().unwrap_err().to_string(); + assert!(err.contains("missing content protection"), "{err}"); + } + + /// A refID names one entry, so a catalog repeating one is refused. + #[test] + fn a_repeated_protection_ref_id_is_refused() { + let entry = r#"{ "refID": "1", "defaultKID": ["01234567-89ab-cdef-0123-456789abcdef"], "scheme": "cbcs", + "drmSystem": { "systemID": "edef8ba9-79d6-4ace-a3c8-27dcd51d21ed" } }"#; + let json = format!(r#"{{ "version": "draft-01", "contentProtections": [{entry}, {entry}], "tracks": [] }}"#); + let err = json.parse::().unwrap_err().to_string(); + assert!(err.contains("is repeated"), "{err}"); + } + #[test] fn track_namespace_round_trips() { for version in ["1", "\"draft-01\""] { @@ -674,6 +899,7 @@ mod test { alt_group: None, max_grp_sap_starting_type: None, max_obj_sap_starting_type: None, + content_protection_ref_ids: Vec::new(), jitter: None, delay: None, } @@ -701,6 +927,7 @@ mod test { alt_group: None, max_grp_sap_starting_type: None, max_obj_sap_starting_type: None, + content_protection_ref_ids: Vec::new(), jitter: None, delay: None, } @@ -728,6 +955,7 @@ mod test { alt_group: None, max_grp_sap_starting_type: Some(1), max_obj_sap_starting_type: Some(2), + content_protection_ref_ids: Vec::new(), jitter: Some(Duration::from_millis(15)), delay: None, } diff --git a/rs/moq-mux/MIGRATE.md b/rs/moq-mux/MIGRATE.md index 922ddcf04fa..ff19f7cb065 100644 --- a/rs/moq-mux/MIGRATE.md +++ b/rs/moq-mux/MIGRATE.md @@ -4,6 +4,21 @@ How to adopt new APIs and update for breaking changes, newest first. ## Unreleased +### Protected CMAF import and the mp4-atom bump + +`fmp4::Import` with passthrough publishes Common Encryption (`cenc` and `cbcs`) fMP4 instead of refusing its `encv`/`enca` entries, filling the catalog's `drm` section; set license and certificate servers with `Import::with_drm`. +Without passthrough, an encrypted file now fails with `fmp4::Error::ProtectedNeedsPassthrough` rather than `UnsupportedCodec`. + +Every export (fMP4, MP4, MKV, MPEG-TS, FLV, Annex-B) leaves out a protected rendition as it does a disabled one: nothing here decrypts, and a re-encoded file would lose its IVs. +A rendition is protected when its `drm` lists entries, or when it is CMAF and its init holds an encrypted (`encv`/`enca`) sample entry, so a publisher that encrypts without signalling it is never read as clear. +A consumer of your own can do the same with `catalog::hang::retain_clear`, or test one rendition with `catalog::hang::protected`. +`catalog::Select` drops protected renditions before it selects, and fails a snapshot whose video and audio are all protected with the new `Error::Protected`, so an export of such a broadcast fails instead of writing nothing. +An MSF CMAF track whose init is encrypted but that lists no `contentProtectionRefIDs` is dropped on parse rather than read as clear. +`RenditionConfig` gains `drm_mut`, defaulting to `None`; an implementation of your own needs no change. + +The re-exported `fmp4::mp4_atom` moves to the release with Common Encryption's boxes: `Moov` and `Moof` gain `pssh: Vec`, and `Codec` gains `Encv` and `Enca`. +Build a `Moov` or `Moof` literal with `pssh: Vec::new()`, and match the new `Codec` variants. + ### `container::Playout` replaces `with_warmup` ([#7864](https://github.com/moq-dev/moq/pull/7864)) `container::Consumer::with_warmup` is gone. Build a `container::Playout::new(delay, warmup)` from the `Delay` and the catalog rendition's `warmup`, subscribe with `playout.subscription(subscription)`, which widens the max delay by the warmup, and build the consumer with `playout.consumer(track, format)`, which skips at the max delay alone. The order no longer matters, and a track subscribed without the widening is widened when the consumer is built. diff --git a/rs/moq-mux/benches/containers.rs b/rs/moq-mux/benches/containers.rs index 6e15f8ab0b6..c9a7dd33988 100644 --- a/rs/moq-mux/benches/containers.rs +++ b/rs/moq-mux/benches/containers.rs @@ -201,6 +201,7 @@ fn interleaved(init: &[u8], tracks: usize, runs: usize, fragments: usize, payloa let mut out = BytesMut::from(&init[..cursor.position() as usize]); for fragment in 0..fragments { let mut moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: fragment as u32 + 1, }, @@ -331,6 +332,7 @@ fn cmaf_receive(c: &mut Criterion) { for samples in [1, 32, 1024] { for size in [1, 4096] { let mut moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 1 }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { diff --git a/rs/moq-mux/src/catalog/hang/container.rs b/rs/moq-mux/src/catalog/hang/container.rs index 0186bd8e140..d7b8481b90c 100644 --- a/rs/moq-mux/src/catalog/hang/container.rs +++ b/rs/moq-mux/src/catalog/hang/container.rs @@ -73,6 +73,69 @@ pub(crate) fn supported(rendition: &str, container: &hang::catalog::Container) - } } +/// Drop the protected video and audio renditions, logging each one (see [`protected`]). +/// +/// Their samples are encrypted, and the hang spec has a consumer that cannot decrypt a rendition +/// treat it as one it cannot decode. Nothing here decrypts, and a re-encoded file would also lose +/// the `pssh`, `senc`, `saiz`, and `saio` a decrypting player needs, so every export, player, and +/// binding in this workspace runs this on each catalog snapshot instead of handing out +/// undecryptable media. A rendition is logged as a warning the first time and at debug level after. +pub fn retain_clear(catalog: &mut hang::Catalog) { + catalog + .video + .renditions + .retain(|name, config| clear("video", name, &config.drm, &config.container)); + catalog + .audio + .renditions + .retain(|name, config| clear("audio", name, &config.drm, &config.container)); +} + +/// Whether a rendition's samples are encrypted: it lists `drm` entries, or its CMAF init holds an +/// encrypted (`encv`/`enca`) sample entry. +/// +/// The init check catches a publisher that encrypts without signalling it, so a consumer never +/// reads such a rendition as clear and decodes ciphertext. +pub fn protected(drm: &[String], container: &hang::catalog::Container) -> bool { + if !drm.is_empty() { + return true; + } + match container { + hang::catalog::Container::Cmaf { init, .. } => fmp4::cenc::init_sinf(init).is_some(), + _ => false, + } +} + +fn clear(kind: &'static str, rendition: &str, drm: &[String], container: &hang::catalog::Container) -> bool { + if !protected(drm, container) { + return true; + } + // Every catalog snapshot runs this, so warn only the first time a rendition is dropped. + static WARNED: std::sync::Mutex> = + std::sync::Mutex::new(std::collections::BTreeSet::new()); + let first = { + let mut warned = WARNED.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + // Bounded, so a publisher minting fresh names cannot grow it without limit. + if warned.len() >= 1024 { + warned.clear(); + } + warned.insert((kind, rendition.to_string())) + }; + match first { + true => tracing::warn!( + rendition, + kind, + "ignoring encrypted rendition: decryption is not supported" + ), + false => tracing::debug!( + rendition, + kind, + "ignoring encrypted rendition: decryption is not supported" + ), + } + false +} + impl ContainerTrait for Container { type Error = crate::Error; diff --git a/rs/moq-mux/src/catalog/hang/mod.rs b/rs/moq-mux/src/catalog/hang/mod.rs index ac0c170d728..24e952e983c 100644 --- a/rs/moq-mux/src/catalog/hang/mod.rs +++ b/rs/moq-mux/src/catalog/hang/mod.rs @@ -11,7 +11,7 @@ mod container; mod ext; pub use consumer::Consumer; -pub use container::Container; pub(crate) use container::supported; +pub use container::{Container, protected, retain_clear}; pub use ext::{CatalogExt, Extra}; pub use hang::Catalog; diff --git a/rs/moq-mux/src/catalog/msf/consumer.rs b/rs/moq-mux/src/catalog/msf/consumer.rs index 6092a2e570a..61b0587a888 100644 --- a/rs/moq-mux/src/catalog/msf/consumer.rs +++ b/rs/moq-mux/src/catalog/msf/consumer.rs @@ -111,9 +111,21 @@ impl From for Consumer { /// Fields with no representation in `hang::Catalog` (`generated_at`, `is_complete`, `is_live`, /// `render_group`, `alt_group`, `max_grp_sap_starting_type`, `max_obj_sap_starting_type`) are /// dropped. +/// +/// `contentProtections` become the `drm` section, each `refID` an entry ID, and a track's +/// `contentProtectionRefIDs` its rendition's `drm`. An entry naming several default keys becomes +/// one hang entry per key, `{refID}:{kid}` with the key in lowercase hyphenated form, and a track +/// lists the one its init's `tenc` uses (all of them when the init names none). An entry the hang +/// catalog cannot carry (a malformed ID, URL, or `pssh`, a scheme other than `cenc` and `cbcs`, no +/// default key, or a split ID that is another entry's `refID`) is dropped with a warning, and so is +/// every video and audio track it protects; the other tracks remain. A CMAF track whose init is +/// encrypted (`encv`/`enca`) but that lists no `contentProtectionRefIDs` is dropped the same way, +/// rather than read as clear. pub(crate) fn from_msf(msf: &moq_msf::Catalog, base: &moq_net::Path) -> Result> { let mut catalog = Catalog::default(); catalog.ext = msf.ext.clone(); + let protections = Protections::from_msf(msf); + catalog.drm = protections.entries.clone(); for track in &msf.tracks { // The track name is the rendition ID, so a later track with the name replaces the earlier @@ -145,7 +157,12 @@ pub(crate) fn from_msf(msf: &moq_msf::Catalog, base: &moq_net: match role { moq_msf::Role::Video => match video_config_from_msf(track)? { Some(mut config) => { + let Some(drm) = protections.track(track, &config.container) else { + tracing::warn!(track = %track.name, "skipping MSF video track whose protection the catalog cannot signal"); + continue; + }; config.broadcast = broadcast; + config.drm = drm; catalog.video.renditions.insert(track.name.clone(), config); } None => { @@ -158,7 +175,12 @@ pub(crate) fn from_msf(msf: &moq_msf::Catalog, base: &moq_net: }, moq_msf::Role::Audio => match audio_config_from_msf(track)? { Some(mut config) => { + let Some(drm) = protections.track(track, &config.container) else { + tracing::warn!(track = %track.name, "skipping MSF audio track whose protection the catalog cannot signal"); + continue; + }; config.broadcast = broadcast; + config.drm = drm; catalog.audio.renditions.insert(track.name.clone(), config); } None => { @@ -188,6 +210,134 @@ pub(crate) fn from_msf(msf: &moq_msf::Catalog, base: &moq_net: Ok(catalog) } +/// An MSF catalog's `contentProtections` as hang `drm` entries. +struct Protections { + /// The hang entries, by ID. + entries: std::collections::BTreeMap, + /// Each usable `refID`'s hang entries, with the default key each one is for, or `None` for one + /// the hang catalog cannot carry. + by_ref: std::collections::HashMap>>, +} + +impl Protections { + /// Map each entry, dropping with a warning one the hang catalog cannot carry, so a bad entry + /// costs only the renditions it protects (see [`track`](Self::track)), not the whole catalog. + fn from_msf(msf: &moq_msf::Catalog) -> Self { + // moq-msf refuses a repeated refID. A split entry's `{refID}:{kid}` must not take another + // entry's refID, whichever comes first. + let ref_ids: std::collections::HashSet<&str> = + msf.content_protections.iter().map(|cp| cp.ref_id.as_str()).collect(); + let mut protections = Self { + entries: Default::default(), + by_ref: Default::default(), + }; + for cp in &msf.content_protections { + let split = match protections.split(cp, &ref_ids) { + Ok(split) => Some(split), + Err(reason) => { + tracing::warn!(ref_id = %cp.ref_id, reason, "ignoring MSF content protection the hang catalog cannot carry"); + None + } + }; + protections.by_ref.insert(cp.ref_id.clone(), split); + } + protections + } + + /// Add `cp`'s hang entries, one per default key, returning their IDs. + fn split( + &mut self, + cp: &moq_msf::ContentProtection, + ref_ids: &std::collections::HashSet<&str>, + ) -> std::result::Result, &'static str> { + use hang::catalog::drm; + + let system = drm::Uuid::try_parse(&cp.drm_system.system_id).map_err(|_| "malformed systemID")?; + let scheme: drm::Scheme = cp.scheme.parse().map_err(|_| "unsupported scheme")?; + let server = |url: &moq_msf::ProtectionUrl| -> std::result::Result { + let mut server = drm::Server::new(url.url.parse().map_err(|_| "malformed URL")?); + server.kind = url.kind.clone(); + Ok(server) + }; + let license = cp.drm_system.la_url.as_ref().map(server).transpose()?; + let certificate = cp.drm_system.cert_url.as_ref().map(server).transpose()?; + let pssh = (cp.drm_system.pssh.as_ref()) + .map(|pssh| base64::engine::general_purpose::STANDARD.decode(pssh)) + .transpose() + .map_err(|_| "malformed pssh")?; + if cp.default_kid.is_empty() { + return Err("no defaultKID"); + } + + let mut split = Vec::with_capacity(cp.default_kid.len()); + for kid in &cp.default_kid { + let kid = drm::Uuid::try_parse(kid).map_err(|_| "malformed defaultKID")?; + let id = match cp.default_kid.len() { + 1 => cp.ref_id.clone(), + _ => format!("{}:{}", cp.ref_id, kid.hyphenated()), + }; + if split.iter().any(|(other, _)| *other == id) { + return Err("repeated defaultKID"); + } + if cp.default_kid.len() > 1 && ref_ids.contains(id.as_str()) { + return Err("a split entry ID is another entry's refID"); + } + split.push((id, kid)); + } + for (id, kid) in &split { + let mut entry = drm::Entry::new(system, scheme, *kid); + entry.license = license.clone(); + entry.certificate = certificate.clone(); + entry.pssh = pssh.clone().map(Into::into); + self.entries.insert(id.clone(), entry); + } + Ok(split) + } + + /// The hang entry IDs protecting `track`, whose CMAF init is in `container`, or `None` when it + /// references an entry that was dropped, or its init is encrypted (`encv`/`enca`) and it + /// references none: its samples are encrypted in a way the catalog cannot describe, so the + /// rendition is left out rather than signalled as clear. + fn track(&self, track: &moq_msf::Track, container: &Container) -> Option> { + let (encrypted, kid) = match container { + Container::Cmaf { init } => match crate::container::fmp4::cenc::init_sinf(init) { + Some(_) => (true, init_kid(init)), + None => (false, None), + }, + _ => (false, None), + }; + // An encrypted init no content protection signals would read as clear, and every consumer + // would then decode ciphertext. + if encrypted && track.content_protection_ref_ids.is_empty() { + tracing::warn!(track = %track.name, "MSF track has an encrypted init but no contentProtectionRefIDs"); + return None; + } + let mut ids = Vec::new(); + // moq-msf refuses a refID the root lacks. + for split in track + .content_protection_ref_ids + .iter() + .filter_map(|id| self.by_ref.get(id)) + { + let split = split.as_ref()?; + let matching: Vec<_> = split.iter().filter(|(_, other)| Some(*other) == kid).collect(); + let chosen = match (split.len(), matching.is_empty()) { + (1, _) | (_, true) => split.iter().collect(), + _ => matching, + }; + ids.extend(chosen.into_iter().map(|(id, _)| id.clone())); + } + Some(ids) + } +} + +/// The default key ID an encrypted CMAF init's `tenc` names, if any. +fn init_kid(init: &[u8]) -> Option { + let sinf = crate::container::fmp4::cenc::init_sinf(init)?; + let tenc = sinf.schi.as_ref()?.tenc.as_ref()?; + Some(hang::catalog::drm::Uuid::from_bytes(tenc.default_kid)) +} + /// Decode the [`Container`] for a track based on its packaging and `init_data`. /// /// Returns `Ok(None)` when the packaging is unsupported (e.g. `MediaTimeline`, @@ -433,7 +583,9 @@ fn cmaf_moov(track: &moq_msf::Track, init: &[u8]) -> Result { fn cmaf_video_description(track: &moq_msf::Track, init: &[u8], codec: &VideoCodec) -> Result> { use mp4_atom::{Atom, Codec}; let moov = cmaf_moov(track, init)?; - for sample in moov.trak.iter().flat_map(|trak| &trak.mdia.minf.stbl.stsd.codecs) { + let samples = moov.trak.iter().flat_map(|trak| &trak.mdia.minf.stbl.stsd.codecs); + // A protected entry wraps the codec the catalog names. + for sample in samples.map(crate::container::fmp4::cenc::clear) { let mut description = bytes::BytesMut::new(); match (codec, sample) { ( @@ -463,7 +615,7 @@ fn derive_from_cmaf_moov(track: &moq_msf::Track, init: bytes::Bytes) -> Result { return Ok(DerivedAudio { @@ -486,6 +638,119 @@ fn derive_from_cmaf_moov(track: &moq_msf::Track, init: bytes::Bytes) -> Result>(), ["wv"]); + assert!(catalog.video.renditions.is_empty()); + assert!(catalog.audio.renditions.contains_key("audio")); + } + } + + /// A split entry's ID writes its key in lowercase hyphenated form, whatever form the MSF + /// catalog used, as `@moq/watch` does; one that would take another entry's `refID` drops that + /// entry rather than overwriting the other. + #[test] + fn split_entry_ids_are_normalised_and_never_collide() { + let json = r#"{ + "version": "draft-01", + "contentProtections": [ + { "refID": "ck", "defaultKID": ["0123456789ABCDEF0123456789ABCDEF", "{A1B2C3D4-E5F6-0718-293A-4B5C6D7E8F90}"], + "scheme": "cenc", "drmSystem": { "systemID": "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b" } } + ], + "tracks": [ + { "name": "video", "packaging": "legacy", "isLive": true, "role": "video", "codec": "vp8", + "contentProtectionRefIDs": ["ck"] } + ] + }"#; + let msf: moq_msf::Catalog = json.parse().unwrap(); + let catalog = from_msf(&msf, &moq_net::Path::empty()).unwrap(); + assert_eq!( + catalog.drm.keys().collect::>(), + [ + "ck:01234567-89ab-cdef-0123-456789abcdef", + "ck:a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90" + ] + ); + assert_eq!( + catalog.drm["ck:01234567-89ab-cdef-0123-456789abcdef"].kid, + hang::catalog::drm::Uuid::from_u128(0x01234567_89ab_cdef_0123_456789abcdef) + ); + + // Another entry whose refID is one of the split IDs keeps it; the split entry is dropped. + let colliding = json.replace( + "\"contentProtections\": [", + r#""contentProtections": [ + { "refID": "ck:01234567-89ab-cdef-0123-456789abcdef", "defaultKID": ["01234567-89ab-cdef-0123-456789abcdef"], + "scheme": "cbcs", "drmSystem": { "systemID": "edef8ba9-79d6-4ace-a3c8-27dcd51d21ed" } },"#, + ); + let msf: moq_msf::Catalog = colliding.parse().unwrap(); + let catalog = from_msf(&msf, &moq_net::Path::empty()).unwrap(); + assert_eq!( + catalog.drm.keys().collect::>(), + ["ck:01234567-89ab-cdef-0123-456789abcdef"] + ); + assert_eq!( + catalog.drm["ck:01234567-89ab-cdef-0123-456789abcdef"].scheme, + hang::catalog::drm::Scheme::Cbcs + ); + assert!(catalog.video.renditions.is_empty()); + } + #[test] fn received_metadata_is_bounded() { let track = moq_msf::Track::new("video", moq_msf::Packaging::Cmaf); diff --git a/rs/moq-mux/src/catalog/producer.rs b/rs/moq-mux/src/catalog/producer.rs index 8a289303fd0..e1ee18891da 100644 --- a/rs/moq-mux/src/catalog/producer.rs +++ b/rs/moq-mux/src/catalog/producer.rs @@ -1368,6 +1368,7 @@ fn to_msf_media(catalog: &hang::Catalog, base: &moq_net::Path) -> track.alt_group = if has_multiple_video { Some(1) } else { None }; track.max_grp_sap_starting_type = sap_type; track.max_obj_sap_starting_type = sap_type; + track.content_protection_ref_ids = config.drm.clone(); track.jitter = config.jitter; track.delay = config.delay.filter(|delay| !delay.is_zero()); tracks.push(track); @@ -1401,6 +1402,7 @@ fn to_msf_media(catalog: &hang::Catalog, base: &moq_net::Path) -> track.alt_group = if has_multiple_audio { Some(1) } else { None }; track.max_grp_sap_starting_type = Some(1); track.max_obj_sap_starting_type = Some(1); + track.content_protection_ref_ids = config.drm.clone(); track.jitter = config.jitter; track.delay = config.delay.filter(|delay| !delay.is_zero()); tracks.push(track); @@ -1438,7 +1440,33 @@ fn to_msf_media(catalog: &hang::Catalog, base: &moq_net::Path) -> } } } - moq_msf::Catalog::with_ext(unique, E::default()) + let mut msf = moq_msf::Catalog::with_ext(unique, E::default()); + msf.content_protections = catalog + .drm + .iter() + .map(|(id, entry)| content_protection(id, entry)) + .collect(); + msf +} + +/// A hang `drm` entry as the CMSF `contentProtections` entry it mirrors (draft-ietf-moq-cmsf +/// section 4.1.1), its ID becoming the `refID` tracks reference. +fn content_protection(id: &str, entry: &hang::catalog::drm::Entry) -> moq_msf::ContentProtection { + let server = |server: &hang::catalog::drm::Server| { + let mut url = moq_msf::ProtectionUrl::new(server.url.as_str()); + url.kind = server.kind.clone(); + url + }; + let mut msf = moq_msf::ContentProtection::new( + id, + vec![entry.kid.hyphenated().to_string()], + entry.scheme.as_str(), + entry.system.hyphenated().to_string(), + ); + msf.drm_system.la_url = entry.license.as_ref().map(server); + msf.drm_system.cert_url = entry.certificate.as_ref().map(server); + msf.drm_system.pssh = (entry.pssh.as_ref()).map(|pssh| base64::engine::general_purpose::STANDARD.encode(pssh)); + msf } #[cfg(test)] diff --git a/rs/moq-mux/src/catalog/select.rs b/rs/moq-mux/src/catalog/select.rs index 5193d0b9b1a..adf2ab2efe7 100644 --- a/rs/moq-mux/src/catalog/select.rs +++ b/rs/moq-mux/src/catalog/select.rs @@ -4,6 +4,10 @@ //! to each snapshot, dropping renditions that aren't selected before handing the //! catalog on. Each snapshot holds what the previous one selected, so a rendition its //! publisher disables stays while nothing else of its role is enabled. +//! +//! Protected renditions (see [`retain_clear`](super::hang::retain_clear)) are dropped before +//! selecting, since nothing that consumes a selection decrypts, and a snapshot whose video and +//! audio are all protected fails with [`Error::Protected`](crate::Error::Protected). use std::task::{Poll, ready}; @@ -39,12 +43,22 @@ impl Stream for Select { type Ext = S::Ext; fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll>>> { - let next = ready!(self.inner.poll_next(waiter))?; - Poll::Ready(Ok(next.map(|mut catalog| { - self.selection.retain(&mut catalog, &self.selected); - self.selected = select::Selected::of(&catalog); - catalog - }))) + let Some(mut catalog) = ready!(self.inner.poll_next(waiter))? else { + return Poll::Ready(Ok(None)); + }; + // Nothing downstream of a selection decrypts, so a protected rendition is absent before it + // is chosen from: otherwise one could displace a clear rendition, then be dropped. + let media = |catalog: &Catalog| catalog.video.renditions.len() + catalog.audio.renditions.len(); + let listed = media(&catalog); + super::hang::retain_clear(&mut catalog); + // A broadcast whose media is all protected can never export anything; say so rather than + // export nothing and succeed. + if listed > 0 && media(&catalog) == 0 { + return Poll::Ready(Err(crate::Error::Protected)); + } + self.selection.retain(&mut catalog, &self.selected); + self.selected = select::Selected::of(&catalog); + Poll::Ready(Ok(Some(catalog))) } } @@ -92,6 +106,48 @@ mod tests { (name.to_string(), config) } + /// A protected rendition is dropped before selecting, so a held clear rendition the publisher + /// disabled is not passed over for an enabled protected one that is then removed too. + #[test] + fn protected_renditions_never_displace_a_clear_one() { + let snapshot = |enabled: bool| { + let mut catalog = Catalog::default(); + let (name, mut clear) = h264("clear"); + clear.enabled = enabled; + catalog.video.renditions.insert(name, clear); + let (name, mut protected) = h264("protected"); + protected.drm = vec!["1".to_string()]; + catalog.video.renditions.insert(name, protected); + catalog + }; + let selection = select::Broadcast::default().video(select::Video::default()); + let mut stream = Snapshots::new([snapshot(true), snapshot(false)]).select(selection); + assert_eq!(next(&mut stream).video.renditions.keys().collect::>(), ["clear"]); + assert_eq!( + next(&mut stream).video.renditions.keys().collect::>(), + ["clear"], + "held while disabled" + ); + } + + /// A broadcast whose media is all protected fails rather than exporting nothing; one with no + /// media yet just waits for it. + #[test] + fn only_protected_media_is_an_error() { + let (name, mut protected) = h264("protected"); + protected.drm = vec!["1".to_string()]; + let mut catalog = Catalog::default(); + catalog.video.renditions.insert(name, protected); + + let selection = select::Broadcast::default().video(select::Video::default()); + let mut stream = Snapshots::new([Catalog::default(), catalog]).select(selection); + assert!(next(&mut stream).video.renditions.is_empty()); + assert!(matches!( + stream.poll_next(&kio::Waiter::noop()), + Poll::Ready(Err(crate::Error::Protected)) + )); + } + #[test] fn narrows_each_snapshot() { let mut catalog = Catalog::default(); diff --git a/rs/moq-mux/src/catalog/tracks.rs b/rs/moq-mux/src/catalog/tracks.rs index 06c9d4d3341..0ca4f982f11 100644 --- a/rs/moq-mux/src/catalog/tracks.rs +++ b/rs/moq-mux/src/catalog/tracks.rs @@ -111,6 +111,12 @@ pub trait RenditionConfig: Clone + Send + 'static { /// Replace the config's [`Estimate`] fields. Defaults to discarding them. fn set_estimate(&mut self, _estimate: Estimate) {} + + /// The IDs of the root `drm` entries the config lists, for a config that can be protected. + /// Defaults to `None`: only video and audio renditions carry protected media. + fn drm_mut(&mut self) -> Option<&mut Vec> { + None + } } impl RenditionConfig for hang::catalog::JsonConfig { @@ -294,6 +300,45 @@ impl VideoHint { } } +/// Add `entries` to the catalog's `drm` section, returning their IDs for a rendition to list. +/// +/// An entry identical to one already present (the same system, scheme, key, `pssh`, and servers) +/// is shared, so video and audio under one key list the same IDs. Any difference, such as another +/// importer's servers for the same key, takes a new entry with the lowest free numeric ID, so no +/// rendition is signalled with another's servers. +fn register_drm(catalog: &mut Catalog, entries: Vec) -> Vec { + let mut ids = Vec::with_capacity(entries.len()); + for entry in entries { + let existing = catalog.drm.iter().find(|(_, other)| **other == entry); + let id = match existing { + Some((id, _)) => id.clone(), + None => { + let id = (1..) + .map(|n: u64| n.to_string()) + .find(|id| !catalog.drm.contains_key(id)) + .expect("a free entry ID"); + catalog.drm.insert(id.clone(), entry); + id + } + }; + ids.push(id); + } + ids +} + +/// Remove the `drm` entries among `ids` that no video or audio rendition lists any more, so an +/// entry leaves the catalog with the last rendition it protects. +fn release_drm(catalog: &mut Catalog, ids: &[String]) { + for id in ids { + let listed = (catalog.video.renditions.values().map(|config| &config.drm)) + .chain(catalog.audio.renditions.values().map(|config| &config.drm)) + .any(|drm| drm.contains(id)); + if !listed { + catalog.drm.remove(id); + } + } +} + impl RenditionConfig for hang::catalog::VideoConfig { fn detects() -> bool { true @@ -306,7 +351,9 @@ impl RenditionConfig for hang::catalog::VideoConfig { catalog.video.renditions.get_mut(name) } fn remove(catalog: &mut Catalog, name: &str) { - catalog.video.renditions.remove(name); + if let Some(config) = catalog.video.renditions.remove(name) { + release_drm(catalog, &config.drm); + } } fn estimate(&self) -> Estimate { @@ -322,6 +369,9 @@ impl RenditionConfig for hang::catalog::VideoConfig { self.delay = estimate.delay; self.framerate = estimate.framerate; } + fn drm_mut(&mut self) -> Option<&mut Vec> { + Some(&mut self.drm) + } } impl RenditionConfig for hang::catalog::AudioConfig { @@ -336,7 +386,9 @@ impl RenditionConfig for hang::catalog::AudioConfig { catalog.audio.renditions.get_mut(name) } fn remove(catalog: &mut Catalog, name: &str) { - catalog.audio.renditions.remove(name); + if let Some(config) = catalog.audio.renditions.remove(name) { + release_drm(catalog, &config.drm); + } } fn estimate(&self) -> Estimate { @@ -350,6 +402,9 @@ impl RenditionConfig for hang::catalog::AudioConfig { self.bitrate = estimate.bitrate; self.delay = estimate.delay; } + fn drm_mut(&mut self) -> Option<&mut Vec> { + Some(&mut self.drm) + } } impl RenditionConfig for hang::catalog::TextConfig { @@ -668,7 +723,21 @@ impl> Rendition { /// the MSF catalog. A caller who wants to pre-empt detection sets the field on the config, or (for /// a config an importer builds out of the bitstream) hands the importer a hint like [`VideoHint`]. /// Unset jitter and delay keep the published floor across reconfiguration. - pub(crate) fn set(&mut self, mut config: C) -> crate::Result<()> { + pub(crate) fn set(&mut self, config: C) -> crate::Result<()> { + self.set_protected(config, Vec::new()) + } + + /// [`set`](Self::set) a protected rendition, adding the `drm` entries protecting it in the same + /// catalog edit and listing their IDs in its config. + /// + /// One edit adds the entries and the rendition listing them, so no catalog holds one without the + /// other: an entry another rendition's removal released in between would dangle and fail every + /// later catalog publish, and one added for a rendition that then failed would be left behind. + pub(crate) fn set_protected( + &mut self, + mut config: C, + entries: Vec, + ) -> crate::Result<()> { let supplied = config.estimate(); let mut resolved = Self::resolved(&supplied, &self.detected); if self.present { @@ -684,7 +753,15 @@ impl> Rendition { config.set_estimate(resolved.clone()); { let mut guard = self.catalog.modify()?; - write(&mut guard, &self.name, config)?; + let ids = register_drm(&mut guard, entries); + if !ids.is_empty() { + let drm = config.drm_mut().expect("only video and audio renditions are protected"); + drm.clone_from(&ids); + } + if let Err(err) = write(&mut guard, &self.name, config) { + release_drm(&mut guard, &ids); + return Err(err); + } self.present = true; guard.list::(&self.name); if C::detects() { diff --git a/rs/moq-mux/src/container/fmp4/cenc.rs b/rs/moq-mux/src/container/fmp4/cenc.rs new file mode 100644 index 00000000000..456327f193a --- /dev/null +++ b/rs/moq-mux/src/container/fmp4/cenc.rs @@ -0,0 +1,375 @@ +//! Common Encryption (ISO/IEC 23001-7) passthrough: what an encrypted track's init declares, and +//! the checks that keep a republished fragment decryptable. + +use hang::catalog::drm; +use mp4_atom::{Atom, Encode, FourCC}; + +use super::Error; +use crate::Result; + +/// The `seig` sample group: per-sample key IDs, which is key rotation. +const SEIG: FourCC = FourCC::new(b"seig"); + +/// What an encrypted track's sample entry and its moov declare. +#[derive(Debug, Clone)] +pub(crate) struct Protection { + /// The scheme `schm` names. + pub scheme: drm::Scheme, + /// `tenc`'s default key ID. + pub kid: [u8; 16], + /// The moov's `pssh` boxes that apply to this track, in their order there. + pub pssh: Vec, + /// How each fragment's encryption information is laid out. + aux: Aux, +} + +/// How an encrypted track's `senc` lays out each sample's encryption information. +#[derive(Debug, Clone, Copy)] +pub(crate) struct Aux { + /// The size of each sample's IV, 0 under a constant IV. + iv_size: u8, + /// Every fragment must carry a `senc`: the samples have per-sample IVs, or they are video, + /// which Common Encryption only protects with subsample maps. + senc: bool, +} + +impl Protection { + /// The protection of `trak`'s sample entry, or `None` when it is clear. + /// + /// Refuses a clear lead (an encrypted entry beside another), a scheme other than `cenc` or + /// `cbcs`, key rotation declared in the sample table, and an encrypted track that no `pssh` + /// applies to, since the catalog could name no DRM system for it. + pub fn of(trak: &mp4_atom::Trak, moov: &mp4_atom::Moov) -> Result> { + let stbl = &trak.mdia.minf.stbl; + let codecs = &stbl.stsd.codecs; + let Some(sinf) = codecs.iter().find_map(sinf) else { + return Ok(None); + }; + // A clear lead: an encrypted entry beside a clear one, refused as several codecs are. + if codecs.len() > 1 { + return Err(Error::MultipleCodecs.into()); + } + + let schm = sinf + .schm + .as_ref() + .ok_or(Error::InvalidProtection("protected sample entry without schm"))?; + let scheme = match &<[u8; 4]>::from(schm.scheme_type) { + b"cenc" => drm::Scheme::Cenc, + b"cbcs" => drm::Scheme::Cbcs, + _ => return Err(Error::UnsupportedScheme(schm.scheme_type).into()), + }; + // mp4-atom refuses a cenc or cbcs sinf without a tenc. + let tenc = sinf + .schi + .as_ref() + .and_then(|schi| schi.tenc.as_ref()) + .ok_or(Error::InvalidProtection("protected sample entry without tenc"))?; + + if rotates(&stbl.sgpd, &stbl.sbgp) { + return Err(Error::KeyRotation.into()); + } + + let kid = tenc.default_kid; + // A version 0 pssh applies to every key; a version 1 one names the keys it covers. + let pssh: Vec<_> = (moov.pssh.iter()) + .filter(|pssh| { + pssh.kid + .as_ref() + .is_none_or(|kids| kids.is_empty() || kids.contains(&kid)) + }) + .cloned() + .collect(); + if pssh.is_empty() { + return Err(Error::MissingPssh.into()); + } + + let iv_size = tenc.default_per_sample_iv_size; + let video = matches!(codecs[..], [mp4_atom::Codec::Encv(_)]); + let aux = Aux { + iv_size, + // A clear-by-default track (no seig, refused above) has no encryption information. + senc: tenc.default_is_protected && (iv_size > 0 || video), + }; + Ok(Some(Self { scheme, kid, pssh, aux })) + } + + /// How the track's fragments lay out their encryption information. + pub fn aux(&self) -> Aux { + self.aux + } + + /// One catalog entry per DRM system, with that system's first `pssh` box. + pub fn entries(&self) -> Result> { + let mut entries: Vec = Vec::new(); + for pssh in &self.pssh { + let system = drm::Uuid::from_bytes(pssh.system_id); + if entries.iter().any(|entry| entry.system == system) { + continue; + } + let mut entry = drm::Entry::new(system, self.scheme, drm::Uuid::from_bytes(self.kid)); + let mut buf = Vec::new(); + pssh.encode(&mut buf).map_err(Error::from)?; + entry.pssh = Some(buf.into()); + entries.push(entry); + } + Ok(entries) + } +} + +impl Aux { + /// Refuse a fragment that lacks the `senc` its samples need to be decrypted. + /// + /// mp4-atom reads only the ISO `senc`, so this also refuses one that carries its information + /// only in PIFF's `uuid` box, which the rebuilt fragment would drop. + /// + /// A fragment kept whole passes through as the source wrote it, so its `senc` and `saiz` sample + /// counts are not held to its `trun`: ffmpeg's `cenc` audio (as of Lavf 63) carries every earlier + /// fragment's entries too, and refusing that would refuse ffmpeg's own output. Only a fragment + /// the importer drops samples from is held to it, by [`restate`](Self::restate). + pub fn check(&self, traf: &mp4_atom::Traf) -> Result<()> { + if self.senc && traf.senc.is_none() { + return Err(Error::InvalidProtection("encrypted fragment without senc").into()); + } + Ok(()) + } + + /// Restate `source`'s encryption information in `kept`, the same fragment holding only the + /// samples at `indices`, ascending positions among `source`'s samples. + /// + /// The importer drops samples an edit list skips, such as AAC's priming frame, and those before + /// the first sync sample of a stream joined mid-GOP; the samples that remain still need their + /// IVs and subsample maps. + pub fn restate(&self, source: &mp4_atom::Traf, kept: &mut mp4_atom::Traf, indices: &[usize]) -> Result<()> { + let Some(senc) = &source.senc else { + // Nothing per sample to keep: a constant IV over whole samples. Information elsewhere + // is refused, as it is for any fragment. + if source.saio.iter().any(|saio| is_cenc(saio.aux_info.as_ref())) { + return Err(Error::InvalidProtection("encryption auxiliary information outside senc").into()); + } + return Ok(()); + }; + if senc.version != mp4_atom::SencBoxVersion::V0 { + return Err(Error::InvalidProtection("unsupported senc version").into()); + } + + let entries = self.entries(senc)?; + if entries.len() as u64 != super::sample::count(source) { + return Err(Error::InvalidProtection("senc and trun disagree on the sample count").into()); + } + let mut data = Vec::with_capacity(senc.data.len()); + data.extend_from_slice(&(indices.len() as u32).to_be_bytes()); + for &index in indices { + let entry = entries + .get(index) + .ok_or(Error::InvalidProtection("a kept sample past senc"))?; + data.extend_from_slice(entry); + } + kept.senc = Some(mp4_atom::Senc { + version: senc.version.clone(), + use_subsamples: senc.use_subsamples, + data, + }); + + let mut saiz = source.saiz.iter().filter(|saiz| is_cenc(saiz.aux_info.as_ref())); + if let Some(saiz) = saiz.next() { + if saiz.sample_count as u64 != super::sample::count(source) { + return Err(Error::InvalidProtection("saiz and trun disagree on the sample count").into()); + } + if saiz.default_sample_info_size == 0 { + let sizes = (indices.iter()) + .map(|&index| saiz.sample_info_size.get(index).copied()) + .collect::>>() + .ok_or(Error::InvalidProtection("a kept sample past saiz"))?; + kept.saiz.push(mp4_atom::Saiz { + aux_info: saiz.aux_info.clone(), + default_sample_info_size: 0, + sample_count: indices.len() as u32, + sample_info_size: sizes, + }); + } else { + kept.saiz.push(mp4_atom::Saiz { + aux_info: saiz.aux_info.clone(), + default_sample_info_size: saiz.default_sample_info_size, + sample_count: indices.len() as u32, + sample_info_size: Vec::new(), + }); + } + } + // Several would be refused by `reserve_saio` on the source's own fragments too. + if saiz.next().is_some() { + return Err(Error::InvalidProtection("encryption auxiliary information split over several saiz").into()); + } + kept.saio = (source.saio.iter()) + .filter(|saio| is_cenc(saio.aux_info.as_ref())) + .cloned() + .collect(); + Ok(()) + } + + /// Each sample's encryption information in `senc`: its IV, then its subsample map when + /// `senc` carries them. + fn entries<'a>(&self, senc: &'a mp4_atom::Senc) -> Result> { + let malformed = || Error::InvalidProtection("malformed senc"); + let data = senc.data.as_slice(); + let count = data.get(..4).ok_or_else(malformed)?; + let count = u32::from_be_bytes(count.try_into().expect("four bytes")) as usize; + // A constant IV over whole samples leaves every entry empty, so the count is bounded by + // what a fragment may hold rather than by the box's size. + if count as u64 > super::MAX_FRAGMENT_SAMPLES { + return Err(malformed().into()); + } + let mut entries = Vec::with_capacity(count); + let mut position = 4; + for _ in 0..count { + let start = position; + position += self.iv_size as usize; + if senc.use_subsamples { + let subsamples = data.get(position..position + 2).ok_or_else(malformed)?; + let subsamples = u16::from_be_bytes(subsamples.try_into().expect("two bytes")) as usize; + // Each subsample is a 16-bit clear and a 32-bit protected byte count. + position += 2 + subsamples * 6; + } + entries.push(data.get(start..position).ok_or_else(malformed)?); + } + if position != data.len() { + return Err(malformed().into()); + } + Ok(entries) + } +} + +/// The first `sinf` of an encrypted sample entry in a CMAF init's `moov`, `None` for a clear init +/// or one that does not decode. +pub(crate) fn init_sinf(init: &[u8]) -> Option { + use mp4_atom::{Any, DecodeMaybe}; + + let mut cursor = std::io::Cursor::new(init); + while let Ok(Some(atom)) = Any::decode_maybe(&mut cursor) { + if let Any::Moov(moov) = atom { + return (moov.trak.iter()) + .flat_map(|trak| &trak.mdia.minf.stbl.stsd.codecs) + .find_map(sinf) + .cloned(); + } + } + None +} + +/// The `sinf` of an encrypted sample entry, `None` for a clear one. +pub(crate) fn sinf(codec: &mp4_atom::Codec) -> Option<&mp4_atom::Sinf> { + match codec { + mp4_atom::Codec::Encv(encv) => Some(&encv.sinf), + mp4_atom::Codec::Enca(enca) => Some(&enca.sinf), + _ => None, + } +} + +/// The sample entry an encrypted one wraps, or `codec` itself when it is clear. +/// +/// The catalog names a protected rendition by this codec, as MSE and EME expect. +pub(crate) fn clear(codec: &mp4_atom::Codec) -> &mp4_atom::Codec { + match codec { + mp4_atom::Codec::Encv(encv) => &encv.codec, + mp4_atom::Codec::Enca(enca) => &enca.codec, + codec => codec, + } +} + +/// Whether sample groups assign keys per sample (`seig`), which is key rotation. +/// +/// Other groups, such as AAC's `roll`, say nothing about keys and pass through. +fn rotates(sgpd: &[mp4_atom::Sgpd], sbgp: &[mp4_atom::Sbgp]) -> bool { + sgpd.iter().any(|sgpd| sgpd.grouping_type == SEIG) || sbgp.iter().any(|sbgp| sbgp.grouping_type == SEIG) +} + +/// Refuse a fragment of an encrypted track whose keys rotate: a `pssh` in its `moof`, or a `seig` +/// sample group in its `traf`. +pub(crate) fn check_moof(moof: &mp4_atom::Moof, traf: &mp4_atom::Traf) -> Result<()> { + if !moof.pssh.is_empty() || rotates(&traf.sgpd, &traf.sbgp) { + return Err(Error::KeyRotation.into()); + } + Ok(()) +} + +/// Whether `saio` addresses Common Encryption's auxiliary information: an untyped one (which an +/// encrypted track's scheme types) or one typed by a Common Encryption scheme. +fn is_cenc(aux: Option<&mp4_atom::AuxInfo>) -> bool { + aux.is_none_or(|aux| { + matches!( + &<[u8; 4]>::from(aux.aux_info_type), + b"cenc" | b"cbc1" | b"cens" | b"cbcs" + ) + }) +} + +/// Prepare an encrypted track's fragment for re-encoding: refuse auxiliary information the +/// rebuilt fragment would lose, and reserve the one `saio` offset [`point_saio`] fills in. +/// +/// The per-sample IVs and subsample maps must sit in the fragment's `senc`, which the rebuilt +/// fragment carries; ones only in the source's `mdat`, or split over several `saio` boxes, would be +/// lost or misaddressed. +pub(crate) fn reserve_saio(traf: &mut mp4_atom::Traf) -> Result<()> { + let count = traf.saio.iter().filter(|saio| is_cenc(saio.aux_info.as_ref())).count(); + match (traf.senc.is_some(), count) { + (_, 0) => Ok(()), + (false, _) => Err(Error::InvalidProtection("encryption auxiliary information outside senc").into()), + (true, 1) => { + for saio in traf.saio.iter_mut().filter(|saio| is_cenc(saio.aux_info.as_ref())) { + // The rebuilt senc holds every sample's information back to back, so one offset + // addresses all of it, however many runs the source split it over. + saio.offsets = vec![0]; + } + Ok(()) + } + (true, _) => Err(Error::InvalidProtection("encryption auxiliary information split over several saio").into()), + } +} + +/// Point the Common Encryption `saio` of the encoded per-track fragment `moof` at its `senc`'s +/// first sample, relative to the `moof` as `default-base-is-moof` addresses it. +/// +/// `moof` must be the encoding of `traf`'s fragment with the offset [`reserve_saio`] reserved, so +/// the offset this fills in keeps its size. +pub(crate) fn point_saio(traf: &mut mp4_atom::Traf, moof: &[u8]) -> Result<()> { + if !traf.saio.iter().any(|saio| is_cenc(saio.aux_info.as_ref())) { + return Ok(()); + } + let senc = find( + moof, + &[mp4_atom::Moof::KIND, mp4_atom::Traf::KIND, mp4_atom::Senc::KIND], + ) + .ok_or(Error::InvalidProtection("rebuilt fragment has no senc"))?; + // senc's version and flags, then its sample count, precede the first sample's information. + let offset = senc + 4 + 4; + for saio in traf.saio.iter_mut().filter(|saio| is_cenc(saio.aux_info.as_ref())) { + saio.offsets = vec![offset as u64]; + } + Ok(()) +} + +/// The offset of the body of the box at `path`, walking from the start of `buf`. +fn find(buf: &[u8], path: &[FourCC]) -> Option { + let (first, rest) = path.split_first()?; + let mut position = 0; + while position < buf.len() { + let mut cursor = &buf[position..]; + let header = ::decode(&mut cursor).ok()?; + let header_size = buf.len() - position - cursor.len(); + let size = header + .size + .map(|size| size + header_size) + .unwrap_or(buf.len() - position); + let body = position + header_size; + if header.kind == *first { + if rest.is_empty() { + return Some(body); + } + // A box's children start right after its header; moof and traf carry no fields. + let end = (position + size).min(buf.len()); + return find(&buf[body..end], rest).map(|offset| body + offset); + } + position = position.checked_add(size)?; + } + None +} diff --git a/rs/moq-mux/src/container/fmp4/export_test.rs b/rs/moq-mux/src/container/fmp4/export_test.rs index 02b42f6dae7..c4d3e7fdaf3 100644 --- a/rs/moq-mux/src/container/fmp4/export_test.rs +++ b/rs/moq-mux/src/container/fmp4/export_test.rs @@ -457,6 +457,77 @@ async fn cmaf_source_to_cmaf_export_passthrough() { moov.encode(&mut buf).expect("encode merged moov"); } +/// A protected rendition is left out of the export: re-encoded fragments would lose its IVs, and +/// nothing here decrypts it. The clear renditions beside it are exported as usual. +#[tokio::test(start_paused = true)] +async fn a_protected_rendition_is_left_out() { + let broadcast = moq_net::broadcast::Info::new(); + let mut producer = broadcast.produce(); + let consumer = producer.consume(); + + let catalog = crate::catalog::Producer::new(&mut producer, crate::catalog::Config::default()).unwrap(); + let mut clear = crate::container::fmp4::Import::new(producer.clone(), catalog.reserve()); + let _ = clear.decode(&BytesMut::from(include_bytes!("test_data/bbb.mp4").as_slice())); + let mut protected = crate::container::fmp4::Import::new(producer, catalog.reserve()).with_passthrough(true); + protected + .decode(include_bytes!("test_data/cbcs-video.mp4")) + .expect("protected import"); + assert_eq!(catalog.snapshot().video.renditions.len(), 2); + + let catalog_stream = crate::catalog::Consumer::<()>::new(&consumer, crate::catalog::CatalogFormat::Hang) + .await + .expect("catalog consumer"); + let mut exporter = crate::container::fmp4::Export::new(crate::source::announced(&consumer).await, catalog_stream); + let init = kio::clock::timeout(std::time::Duration::from_secs(1), exporter.next()) + .await + .expect("exporter timed out") + .expect("exporter result") + .expect("expected init bytes"); + + let mut cursor = Cursor::new(init.as_ref()); + let mut moov = None; + while let Some(atom) = mp4_atom::Any::decode_maybe(&mut cursor).expect("decode init") { + if let mp4_atom::Any::Moov(m) = atom { + moov = Some(m); + } + } + let moov = moov.expect("moov"); + assert_eq!(moov.trak.len(), 2, "only bbb's clear video and audio"); + let codecs = moov.trak.iter().flat_map(|trak| &trak.mdia.minf.stbl.stsd.codecs); + assert!( + !codecs + .into_iter() + .any(|codec| matches!(codec, mp4_atom::Codec::Encv(_))) + ); +} + +/// A selected export of a broadcast whose media is all protected fails, rather than writing +/// nothing and ending as if it succeeded. +#[tokio::test(start_paused = true)] +async fn an_export_of_only_protected_media_fails() { + use crate::catalog::Stream as _; + + let broadcast = moq_net::broadcast::Info::new(); + let mut producer = broadcast.produce(); + let consumer = producer.consume(); + + let catalog = crate::catalog::Producer::new(&mut producer, crate::catalog::Config::default()).unwrap(); + let mut protected = crate::container::fmp4::Import::new(producer, catalog.reserve()).with_passthrough(true); + protected + .decode(include_bytes!("test_data/cenc-clearkey.mp4")) + .expect("protected import"); + + let catalog_stream = crate::catalog::Consumer::<()>::new(&consumer, crate::catalog::CatalogFormat::Hang) + .await + .expect("catalog consumer"); + let selected = catalog_stream.select(crate::select::Broadcast::default()); + let mut exporter = crate::container::fmp4::Export::new(crate::source::announced(&consumer).await, selected); + let result = kio::clock::timeout(std::time::Duration::from_secs(1), exporter.next()) + .await + .expect("exporter timed out"); + assert!(matches!(result, Err(crate::Error::Protected)), "{result:?}"); +} + /// Per-rendition export (a single non-first source track, e.g. moq-hls exporting /// the audio rendition alone) must give the init moov the SAME track id its /// re-encoded fragments carry. diff --git a/rs/moq-mux/src/container/fmp4/import.rs b/rs/moq-mux/src/container/fmp4/import.rs index 7552c86dcc3..b40c4723df1 100644 --- a/rs/moq-mux/src/container/fmp4/import.rs +++ b/rs/moq-mux/src/container/fmp4/import.rs @@ -57,6 +57,9 @@ pub struct Import { // Republish each fragment whole as CMAF instead of unwrapping its samples. passthrough: bool, + // The license and certificate servers of each DRM system the media is encrypted for. + drm: std::collections::BTreeMap, + // A lookup to tracks in the broadcast tracks: HashMap>, text: HashMap, @@ -138,6 +141,9 @@ struct Fmp4Track { // group, which is what keeps audio on the same boundaries as video. segment: Option, + // The samples are encrypted, so each fragment's encryption information must survive. + protected: Option, + output: Output, } @@ -248,6 +254,8 @@ struct Sample { sync: bool, /// The sample's bytes within the `mdat` payload. range: std::ops::Range, + /// The sample's position among its `traf`'s, which its encryption information follows. + index: usize, } /// The samples one `traf` places in its `mdat`. @@ -272,6 +280,7 @@ impl Import { initial_reservation: Some(reserved), select: None, passthrough: false, + drm: Default::default(), tracks: HashMap::default(), text: HashMap::default(), skipped: HashSet::default(), @@ -336,6 +345,18 @@ impl Import { self } + /// Set the license and certificate servers of a DRM system the media is encrypted for. + /// + /// Common Encryption media names its DRM systems (each `pssh` box's system ID) but not where a + /// player gets a license, so the publisher supplies that here; the catalog's `drm` entries for + /// `system` carry it. A later call for the same system replaces the earlier one. Once the + /// `moov` arrives, a system no imported encrypted track carries a `pssh` for is refused with + /// [`Error::UnusedDrmSystem`]. + pub fn with_drm(mut self, system: hang::catalog::drm::Uuid, servers: hang::catalog::drm::Servers) -> Self { + self.drm.insert(system, servers); + self + } + /// Whether `kind` is selected for import (every role when unset). fn selects(&self, kind: &Kind) -> bool { match (&self.select, kind) { @@ -529,6 +550,9 @@ impl Import { // Only `finish()` releases the reservation before a moov, since a fragment needs one. let reserved = self.initial_reservation.clone().ok_or(Error::MoovAfterFinish)?; + // Refuse undecryptable or unsignalled protection before any track is published. + let protections = self.protections(&moov)?; + for trak in &moov.trak { let track_id = trak.tkhd.track_id; let handler = &trak.mdia.hdlr.handler; @@ -553,9 +577,10 @@ impl Import { let timescale = moq_net::Timescale::new(trak.mdia.mdhd.timescale as u64)?; let edit = Edit::new(trak, moov.mvhd.timescale)?; + let protection = protections.get(&track_id); let output = match self.passthrough { true => Output::Passthrough(Box::new( - self.init_passthrough(trak, &moov, &reserved, kind, timescale)?, + self.init_passthrough(trak, &moov, &reserved, kind, timescale, protection)?, )), false => self.init_unwrap(trak, &moov, &reserved, kind)?, }; @@ -572,6 +597,7 @@ impl Import { last_decode_time: None, sample_duration: None, segment: None, + protected: protection.map(super::cenc::Protection::aux), output, }, ); @@ -588,6 +614,51 @@ impl Import { Ok(()) } + /// The protection of every imported track that is encrypted, keyed by track ID. + /// + /// Refuses encrypted media without passthrough, whose unwrapped samples would lose their + /// per-sample IVs, every protection [`Protection::of`](super::cenc::Protection::of) refuses, and a + /// DRM system [`with_drm`](Self::with_drm) configured that no encrypted track carries. + fn protections(&self, moov: &Moov) -> Result> { + let mut protections = HashMap::new(); + for trak in &moov.trak { + let imported = Kind::from_handler(trak.mdia.hdlr.handler).is_ok_and(|kind| self.selects(&kind)); + if !imported { + continue; + } + let Some(protection) = super::cenc::Protection::of(trak, moov)? else { + continue; + }; + if !self.passthrough { + return Err(Error::ProtectedNeedsPassthrough.into()); + } + protections.insert(trak.tkhd.track_id, protection); + } + + let carried = |system: &hang::catalog::drm::Uuid| { + (protections.values()) + .flat_map(|protection| &protection.pssh) + .any(|pssh| pssh.system_id == *system.as_bytes()) + }; + if let Some(system) = self.drm.keys().find(|system| !carried(system)) { + return Err(Error::UnusedDrmSystem(*system).into()); + } + Ok(protections) + } + + /// The catalog's `drm` entries for `protection`, one per DRM system, each with the servers + /// [`with_drm`](Self::with_drm) set for it. + fn drm_entries(&self, protection: &super::cenc::Protection) -> Result> { + let mut entries = protection.entries()?; + for entry in &mut entries { + if let Some(servers) = self.drm.get(&entry.system) { + entry.license = servers.license.clone(); + entry.certificate = servers.certificate.clone(); + } + } + Ok(entries) + } + fn init_passthrough( &mut self, trak: &Trak, @@ -595,6 +666,7 @@ impl Import { reserved: &crate::catalog::Reserved, kind: Kind, timescale: moq_net::Timescale, + protection: Option<&super::cenc::Protection>, ) -> Result> { // Declare the track at the fMP4's native timescale, which the moq-hang draft requires // of a CMAF track: frame timestamps are emitted at this same scale (see `extract`), so @@ -609,17 +681,22 @@ impl Import { // Whatever the descriptor declared (a bitrate) is authoritative; the rest is filled by // `estimate` as fragments arrive. + // The entries go in with the rendition listing them, in one catalog edit. + let drm = match protection { + Some(protection) => self.drm_entries(protection)?, + None => Vec::new(), + }; let rendition = match kind { Kind::Video => { - let config = self.init_video(trak, moov)?; + let config = self.init_video(trak, moov, protection)?; let mut rendition = reserved.init(track.name())?; - rendition.set(config)?; + rendition.set_protected(config, drm)?; Rendition::Video(rendition) } Kind::Audio => { - let config = self.init_audio(trak, moov)?; + let config = self.init_audio(trak, moov, protection)?; let mut rendition = reserved.init(track.name())?; - rendition.set(config)?; + rendition.set_protected(config, drm)?; Rendition::Audio(rendition) } }; @@ -655,8 +732,8 @@ impl Import { )?; let wire = crate::catalog::hang::Container::new(&Container::Legacy, wire_kind)?; Ok(match kind { - Kind::Video => Output::Video(reserved.video(track, wire, self.init_video(trak, moov)?)?), - Kind::Audio => Output::Audio(reserved.audio(track, wire, self.init_audio(trak, moov)?)?), + Kind::Video => Output::Video(reserved.video(track, wire, self.init_video(trak, moov, None)?)?), + Kind::Audio => Output::Audio(reserved.audio(track, wire, self.init_audio(trak, moov, None)?)?), }) } @@ -704,7 +781,10 @@ impl Import { /// The container a rendition advertises: unwrapped samples are Legacy frames, while a /// passthrough track carries its own single-track init segment. - fn container(&self, trak: &Trak, moov: &Moov) -> Result { + /// + /// An encrypted track's init keeps its `encv`/`enca` entry and the `pssh` boxes that apply to + /// it, which a decrypting player needs (CMSF section 4.2). + fn container(&self, trak: &Trak, moov: &Moov, protection: Option<&super::cenc::Protection>) -> Result { if !self.passthrough { return Ok(Container::Legacy); } @@ -736,6 +816,7 @@ impl Import { trak.edts = None; let single_moov = Moov { + pssh: protection.map(|protection| protection.pssh.clone()).unwrap_or_default(), mvhd: moov.mvhd.clone(), trak: vec![trak], mvex: Some(mp4_atom::Mvex { @@ -755,8 +836,13 @@ impl Import { } } - fn init_video(&mut self, trak: &Trak, moov: &Moov) -> Result { - let container = self.container(trak, moov)?; + fn init_video( + &mut self, + trak: &Trak, + moov: &Moov, + protection: Option<&super::cenc::Protection>, + ) -> Result { + let container = self.container(trak, moov, protection)?; let stsd = &trak.mdia.minf.stbl.stsd; let codec = match stsd.codecs.len() { @@ -764,6 +850,8 @@ impl Import { 1 => &stsd.codecs[0], _ => return Err(Error::MultipleCodecs.into()), }; + // A protected rendition is described by the codec its encrypted entry wraps. + let codec = super::cenc::clear(codec); let (mut config, colr, pasp, spatial) = match codec { mp4_atom::Codec::Avc1(avc1) => ( @@ -931,8 +1019,13 @@ impl Import { Ok(config) } - fn init_audio(&mut self, trak: &Trak, moov: &Moov) -> Result { - let container = self.container(trak, moov)?; + fn init_audio( + &mut self, + trak: &Trak, + moov: &Moov, + protection: Option<&super::cenc::Protection>, + ) -> Result { + let container = self.container(trak, moov, protection)?; let stsd = &trak.mdia.minf.stbl.stsd; let codec = match stsd.codecs.len() { @@ -940,6 +1033,8 @@ impl Import { 1 => &stsd.codecs[0], _ => return Err(Error::MultipleCodecs.into()), }; + // A protected rendition is described by the codec its encrypted entry wraps. + let codec = super::cenc::clear(codec); let config = match codec { mp4_atom::Codec::Mp4a(mp4a) => { @@ -1152,7 +1247,7 @@ impl Import { new_segment: track.segment != Some(self.segment), }; let prefix = match track.output { - Output::Passthrough(_) => Some(fragment(traf, &moof, &run, &track.defaults)?), + Output::Passthrough(_) => Some(fragment(traf, &moof, &run, &track.defaults, track.protected)?), _ => None, }; let index = mdat.tracks.len(); @@ -1396,7 +1491,7 @@ impl Fmp4Track { let mut end = None; let mut samples = Vec::with_capacity(sample::count(traf) as usize); let mut dropped = false; - for resolved in resolver.resolve(traf, self.defaults)? { + for (index, resolved) in resolver.resolve(traf, self.defaults)?.enumerate() { let sample::Resolved { sample, cts, range } = resolved?; let duration = sample.duration; @@ -1430,6 +1525,7 @@ impl Fmp4Track { flags: sample.flags, sync: sample.is_sync(), range, + index, }); } else { dropped = true; @@ -1459,16 +1555,37 @@ impl Fmp4Track { /// /// The mdat holds `run`'s samples back to back in decode order, whatever separated them in the /// source (padding, another track's runs), so each rewritten `data_offset` is the sizes before it. -fn fragment(traf: &mp4_atom::Traf, moof: &Moof, run: &Run, defaults: &sample::Defaults) -> Result { +/// +/// An encrypted track's fragment keeps its `senc`, `saiz`, and `saio`, with `saio` pointed at the +/// rebuilt `senc`, and samples dropped by an edit list or a mid-GOP start have their entries +/// restated for the kept ones. One whose keys rotate, that lacks a `senc` its samples need, or +/// whose encryption information is outside `senc` is refused. +fn fragment( + traf: &mp4_atom::Traf, + moof: &Moof, + run: &Run, + defaults: &sample::Defaults, + protected: Option, +) -> Result { + if let Some(aux) = protected { + super::cenc::check_moof(moof, traf)?; + aux.check(traf)?; + } + // Build a per-track moof containing only this traf, and a per-track mdat // with only the samples belonging to this track. let mut adjusted_moof = Moof { + pssh: Vec::new(), mfhd: moof.mfhd.clone(), traf: vec![match run.dropped { - true => kept(traf, run)?, + true => kept(traf, run, protected)?, false => traf.clone(), }], }; + let protected = protected.is_some(); + if protected { + super::cenc::reserve_saio(&mut adjusted_moof.traf[0])?; + } // Apply structural (flag-presence) changes BEFORE measuring the encoded // size, otherwise the rewritten data_offset values are computed from a @@ -1497,6 +1614,10 @@ fn fragment(traf: &mp4_atom::Traf, moof: &Moof, run: &Run, defaults: &sample::De let mut moof_buf = Vec::new(); adjusted_moof.encode(&mut moof_buf)?; let new_moof_size = moof_buf.len(); + // Rewriting the reserved offsets below keeps every box's size, so senc stays where it is now. + if protected { + super::cenc::point_saio(&mut adjusted_moof.traf[0], &moof_buf)?; + } // A payload past 4 GiB takes the 16-byte largesize header. let mut mdat = Vec::new(); @@ -1536,9 +1657,10 @@ fn fragment(traf: &mp4_atom::Traf, moof: &Moof, run: &Run, defaults: &sample::De /// /// Each sample keeps its decode and composition time: `tfdt` starts at the first kept sample, and /// each kept sample lasts until the next one decodes. The per-sample tables (`sbgp`, -/// `subs`, sample auxiliary information) would index the wrong samples, and no consumer reads -/// them, so they go. -fn kept(traf: &mp4_atom::Traf, run: &Run) -> Result { +/// `subs`, sample auxiliary information) would index the wrong samples, and no consumer of a clear +/// track reads them, so they go. An encrypted track's encryption information is restated for the +/// kept samples instead, since a player cannot decrypt without it. +fn kept(traf: &mp4_atom::Traf, run: &Run, protected: Option) -> Result { let mut entries = Vec::with_capacity(run.samples.len()); for (index, sample) in run.samples.iter().enumerate() { let duration = match run.samples.get(index + 1) { @@ -1554,7 +1676,7 @@ fn kept(traf: &mp4_atom::Traf, run: &Run) -> Result { }); } let first = run.samples.first().ok_or(Error::MissingTrun)?; - Ok(mp4_atom::Traf { + let mut kept = mp4_atom::Traf { tfhd: traf.tfhd.clone(), tfdt: Some(mp4_atom::Tfdt { base_media_decode_time: first.dts, @@ -1564,7 +1686,12 @@ fn kept(traf: &mp4_atom::Traf, run: &Run) -> Result { samples: entries.into(), }], ..Default::default() - }) + }; + if let Some(aux) = protected { + let indices: Vec = run.samples.iter().map(|sample| sample.index).collect(); + aux.restate(traf, &mut kept, &indices)?; + } + Ok(kept) } impl Passthrough { @@ -1908,6 +2035,7 @@ mod tests { // Only the headers are supplied. The declared mdat may never finish. fn media_header(id: u32, decode_time: u64, offsets: &[usize], size: u32, len: Option) -> Vec { let mut moof = Moof { + pssh: Vec::new(), mfhd: Default::default(), traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { @@ -2042,6 +2170,7 @@ mod tests { // A moof of one traf on track `id` carrying `trun`, whose samples are a byte and 1024 ticks each. fn runs_moof(id: u32, trun: Vec) -> Vec { let moof = Moof { + pssh: Vec::new(), mfhd: Default::default(), traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { diff --git a/rs/moq-mux/src/container/fmp4/import_test.rs b/rs/moq-mux/src/container/fmp4/import_test.rs index da326f51d4e..cda324d2e62 100644 --- a/rs/moq-mux/src/container/fmp4/import_test.rs +++ b/rs/moq-mux/src/container/fmp4/import_test.rs @@ -765,6 +765,7 @@ async fn separated_runs_keep_their_samples() { ..Default::default() }; let mut moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 1 }, traf: vec![ mp4_atom::Traf { @@ -868,6 +869,7 @@ fn empty_samples_are_refused() { let (init, (video_id, timescale), _) = bbb_init(); for explicit in [false, true] { let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 1 }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { @@ -922,6 +924,7 @@ fn absolute_sample_bases_are_refused() { let (init, (video_id, timescale), _) = bbb_init(); for base in [0, 1024, u64::MAX] { let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 1 }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { @@ -995,6 +998,7 @@ async fn shared_sample_bytes_are_refused() { ]; for trafs in cases { let mut moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 1 }, traf: trafs.iter().map(|(traf, _)| traf.clone()).collect(), }; @@ -1050,6 +1054,7 @@ async fn unwrapped_samples_present_at_their_composition_time() { let non_sync = 0x0001_0000; let entries = [(tick, 0, 0xA), (2 * tick, non_sync, 0xB), (0, non_sync, 0xC)]; let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 1 }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { @@ -1142,6 +1147,7 @@ fn fieldless_runs_do_not_stall_the_segment_scan() { // repeating the traf can't multiply it. for (trafs, runs, mdat) in [(1, 1000, 6), (1000, 1, 1 << 16)] { let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 2 }, traf: vec![traf(runs); trafs], }; @@ -1246,6 +1252,7 @@ async fn audio_presents_from_its_edit() { /// One video fragment decoding from `dts`, a sample per `(cts, flags, byte)` lasting `tick`. fn video_fragment(track_id: u32, dts: u64, tick: u64, entries: &[(u64, u32, u8)]) -> Vec { let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 1 }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { @@ -2323,6 +2330,7 @@ async fn a_mid_gop_join_keeps_tracks_on_the_same_groups_in(passthrough: bool) { /// the importer needs to slice them back out. fn audio_fragment(base_media_decode_time: u64, duration: u32, size: u32) -> Vec { let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 0 }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { @@ -2411,6 +2419,7 @@ fn rejected_fragment_keeps_the_first_error() { /// Like [`audio_fragment`] but carrying `samples` samples, the shape a real segmenter emits. fn audio_fragment_samples(base_media_decode_time: u64, duration: u32, size: u32, samples: usize) -> Vec { let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 0 }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { @@ -2516,6 +2525,7 @@ fn fragment_jitter_uses_sample_endpoints() { let mut import = crate::container::fmp4::Import::new(broadcast, catalog.reserve()); import.decode(&init).unwrap(); let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 0 }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { @@ -3069,6 +3079,7 @@ fn webvtt_init(header: &str) -> Vec { /// One fragment carrying `samples` back to back, each lasting `duration` ms from `decode_time`. fn webvtt_fragment(decode_time: u64, samples: &[Vec], duration: u32) -> Vec { let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 1 }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { @@ -3199,6 +3210,7 @@ fn empty_and_multiple_webvtt_cues_are_supported() { fn oversized_text_sample_is_refused_before_staging() { let size = super::MAX_HELD_BYTES + 1; let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 1 }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { @@ -3888,3 +3900,644 @@ async fn declared_boundaries_do_not_split_passthrough_fragments() { ); } } + +const CENC: &[u8] = include_bytes!("test_data/cenc-clearkey.mp4"); +const CBCS_VIDEO: &[u8] = include_bytes!("test_data/cbcs-video.mp4"); +const CBCS_AUDIO: &[u8] = include_bytes!("test_data/cbcs-audio.mp4"); +const CLEARKEY_KID: hang::catalog::drm::Uuid = + hang::catalog::drm::Uuid::from_u128(0xa1b2c3d4_e5f6_0718_293a_4b5c6d7e8f90); + +/// Import each of `files` in passthrough into one broadcast and catalog, configuring each importer +/// with `configure`, and return the first error alongside what was published. +/// +/// The importers come back too: dropping one retires its renditions. +#[allow(clippy::type_complexity, reason = "a test helper returning what each test reads")] +fn import_protected( + files: &[&[u8]], + configure: impl Fn(super::Import) -> super::Import, +) -> ( + crate::Result<()>, + crate::catalog::Producer, + moq_net::broadcast::Consumer, + Vec, +) { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let consumer = broadcast.consume(); + let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); + let mut importers: Vec<_> = (files.iter()) + .map(|_| configure(super::Import::new(broadcast.clone(), catalog.reserve()).with_passthrough(true))) + .collect(); + let mut result = Ok(()); + for (fmp4, data) in importers.iter_mut().zip(files) { + result = result.and(fmp4.decode(data)); + } + for fmp4 in &mut importers { + result = result.and(fmp4.finish()); + } + (result, catalog, consumer, importers) +} + +fn protected_error(files: &[&[u8]], configure: impl Fn(super::Import) -> super::Import) -> super::Error { + match import_protected(files, configure).0 { + Err(crate::Error::Cmaf(err)) => err, + other => panic!("expected a refusal, got {other:?}"), + } +} + +/// The file's top-level boxes with `edit` applied, re-encoded. A changed `moof` keeps addressing +/// its samples: every run's `data_offset` shifts by how much the `moof` grew. +fn rewrite(data: &[u8], mut edit: impl FnMut(&mut mp4_atom::Any)) -> Vec { + use mp4_atom::DecodeMaybe; + + let mut cursor = std::io::Cursor::new(data); + let mut out = Vec::new(); + loop { + let start = cursor.position(); + let Some(mut atom) = mp4_atom::Any::decode_maybe(&mut cursor).unwrap() else { + break; + }; + let before = cursor.position() - start; + edit(&mut atom); + if let mp4_atom::Any::Moof(moof) = &mut atom { + let mut after = Vec::new(); + moof.encode(&mut after).unwrap(); + let grew = after.len() as i32 - before as i32; + for trun in moof.traf.iter_mut().flat_map(|traf| &mut traf.trun) { + trun.data_offset = trun.data_offset.map(|offset| offset + grew); + } + } + atom.encode(&mut out).unwrap(); + } + out +} + +/// The first `moof` of `data`. +fn first_moof(data: &[u8]) -> mp4_atom::Moof { + use mp4_atom::DecodeMaybe; + + let mut cursor = std::io::Cursor::new(data); + while let Some(atom) = mp4_atom::Any::decode_maybe(&mut cursor).unwrap() { + if let mp4_atom::Any::Moof(moof) = atom { + return moof; + } + } + panic!("no moof"); +} + +/// Every fragment the broadcast published on `name`, in order. +async fn fragments(consumer: &moq_net::broadcast::Consumer, name: &str) -> Vec { + let mut track = consumer.track(name).unwrap().subscribe(replay()).await.unwrap(); + let mut out = Vec::new(); + while let Some(mut group) = track.recv_group().now_or_never().and_then(|r| r.ok().flatten()) { + while let Some(frame) = group.read_frame().now_or_never().and_then(|r| r.ok().flatten()) { + out.push(frame.payload); + } + } + out +} + +/// The `senc` of a published fragment, checking its `saio` (if any) points at its first sample's +/// information, relative to the `moof`. +fn checked_senc(fragment: &[u8]) -> mp4_atom::Senc { + let moof = mp4_atom::Moof::decode(&mut &fragment[..]).unwrap(); + let traf = &moof.traf[0]; + let senc = traf.senc.clone().expect("the fragment keeps its senc"); + if let Some(saio) = traf.saio.first() { + let [offset] = saio.offsets[..] else { + panic!("one saio offset, got {:?}", saio.offsets); + }; + let offset = offset as usize; + // The senc header's type, its version and flags, then the sample count the data starts with. + assert_eq!( + &fragment[offset - 12..offset - 8], + b"senc", + "saio points past a senc header" + ); + assert_eq!(&fragment[offset - 4..offset - 4 + senc.data.len()], &senc.data[..]); + } + senc +} + +/// The ClearKey `cenc` file's two tracks share one key and system, so one `drm` entry protects +/// both; each init keeps its encrypted entry and the `pssh`, and the codec is the one it wraps. +#[test] +fn cenc_passthrough_signals_drm() { + use hang::catalog::drm; + + let (result, catalog, _, _importers) = import_protected(&[CENC], |fmp4| fmp4); + result.unwrap(); + let catalog = catalog.snapshot(); + + let entry = &catalog.drm["1"]; + assert_eq!(catalog.drm.len(), 1, "{:?}", catalog.drm); + assert_eq!(entry.system, drm::system::CLEARKEY); + assert_eq!(entry.scheme, drm::Scheme::Cenc); + assert_eq!(entry.kid, CLEARKEY_KID); + let pssh = mp4_atom::Pssh::decode(&mut entry.pssh.as_deref().unwrap()).unwrap(); + assert_eq!(pssh.kid.as_deref(), Some(&[*CLEARKEY_KID.as_bytes()][..])); + + let video = catalog.video.renditions.values().next().unwrap(); + let audio = catalog.audio.renditions.values().next().unwrap(); + assert!(video.codec.to_string().starts_with("avc1"), "{}", video.codec); + assert_eq!(audio.codec.to_string(), "mp4a.40.2"); + assert_eq!( + (&video.drm[..], &audio.drm[..]), + (&["1".to_string()][..], &["1".to_string()][..]) + ); + + for container in [&video.container, &audio.container] { + let Container::Cmaf { init } = container else { + panic!("passthrough publishes CMAF"); + }; + let (_, moov) = decode_init(init); + assert!( + matches!( + moov.trak[0].mdia.minf.stbl.stsd.codecs[..], + [mp4_atom::Codec::Encv(_) | mp4_atom::Codec::Enca(_)] + ), + "the init keeps the encrypted entry" + ); + assert_eq!(moov.pssh, std::slice::from_ref(&pssh), "the init keeps the pssh"); + } +} + +/// Shaka's `cbcs` video and audio, imported side by side, share the entry for their one key; the +/// audio's `roll` sample groups are not key rotation. +#[test] +fn cbcs_video_and_audio_share_an_entry() { + let (result, catalog, _, _importers) = import_protected(&[CBCS_VIDEO, CBCS_AUDIO], |fmp4| fmp4); + result.unwrap(); + let catalog = catalog.snapshot(); + assert_eq!(catalog.drm.len(), 1, "{:?}", catalog.drm); + assert_eq!(catalog.drm["1"].scheme, hang::catalog::drm::Scheme::Cbcs); + assert_eq!(catalog.drm["1"].kid, CLEARKEY_KID); + let video = catalog.video.renditions.values().next().unwrap(); + let audio = catalog.audio.renditions.values().next().unwrap(); + assert_eq!(video.drm, ["1"]); + assert_eq!(audio.drm, ["1"]); + assert!(video.codec.to_string().starts_with("avc1"), "{}", video.codec); + assert_eq!(audio.codec.to_string(), "mp4a.40.2"); +} + +/// The MSF catalog renders the entries as CMSF `contentProtections`, with the publisher's servers, +/// and parsing it maps them back to the same `drm` section. +#[test] +fn drm_renders_as_cmsf_content_protection_and_back() { + use hang::catalog::drm; + + let mut certificate = drm::Server::new("https://cert.example.com/".parse().unwrap()); + certificate.kind = Some("application/pkcs7-mime".into()); + let mut servers = drm::Servers::license("https://license.example.com/clearkey".parse().unwrap()); + servers.certificate = Some(certificate); + let (result, catalog, _, _importers) = import_protected(&[CBCS_VIDEO, CBCS_AUDIO], |fmp4| { + fmp4.with_drm(drm::system::CLEARKEY, servers.clone()) + }); + result.unwrap(); + let catalog = catalog.snapshot(); + assert_eq!(catalog.drm["1"].license, servers.license); + assert_eq!(catalog.drm["1"].certificate, servers.certificate); + + let msf = crate::catalog::to_msf(&catalog, &moq_net::Path::empty()); + let [entry] = &msf.content_protections[..] else { + panic!("one entry, got {:?}", msf.content_protections); + }; + assert_eq!(entry.ref_id, "1"); + assert_eq!(entry.scheme, "cbcs"); + assert_eq!(entry.default_kid, ["a1b2c3d4-e5f6-0718-293a-4b5c6d7e8f90"]); + assert_eq!(entry.drm_system.system_id, "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b"); + assert_eq!( + entry.drm_system.la_url.as_ref().unwrap().url, + "https://license.example.com/clearkey" + ); + assert_eq!( + entry.drm_system.cert_url.as_ref().unwrap().kind.as_deref(), + Some("application/pkcs7-mime") + ); + assert!(entry.drm_system.pssh.is_some()); + for track in &msf.tracks { + assert_eq!(track.content_protection_ref_ids, ["1"], "{}", track.name); + } + + let wire: moq_msf::Catalog = msf.to_json().unwrap().parse().unwrap(); + let back = crate::catalog::msf::from_msf(&wire, &moq_net::Path::empty()).unwrap(); + assert_eq!(back.drm, catalog.drm); + assert_eq!(back.video.renditions.values().next().unwrap().drm, ["1"]); + assert_eq!(back.audio.renditions.values().next().unwrap().drm, ["1"]); +} + +/// An MSF CMAF track whose init is encrypted but that no `contentProtections` entry signals must not +/// read as clear: parsing drops it, and a hang catalog listing such a rendition without `drm` has +/// it removed by `retain_clear` all the same. +#[test] +fn an_encrypted_init_without_protection_is_never_clear() { + let (result, catalog, _, _importers) = import_protected(&[CENC], |fmp4| fmp4); + result.unwrap(); + let catalog = catalog.snapshot(); + + let mut msf = crate::catalog::to_msf(&catalog, &moq_net::Path::empty()); + msf.content_protections.clear(); + for track in &mut msf.tracks { + track.content_protection_ref_ids.clear(); + } + let wire: moq_msf::Catalog = msf.to_json().unwrap().parse().unwrap(); + let back = crate::catalog::msf::from_msf(&wire, &moq_net::Path::empty()).unwrap(); + assert!(back.video.renditions.is_empty(), "{:?}", back.video.renditions.keys()); + assert!(back.audio.renditions.is_empty(), "{:?}", back.audio.renditions.keys()); + + let mut unsignalled = catalog.clone(); + unsignalled.drm.clear(); + for config in unsignalled.video.renditions.values_mut() { + config.drm.clear(); + } + for config in unsignalled.audio.renditions.values_mut() { + config.drm.clear(); + } + assert!(!unsignalled.video.renditions.is_empty()); + crate::catalog::hang::retain_clear(&mut unsignalled); + assert!(unsignalled.video.renditions.is_empty()); + assert!(unsignalled.audio.renditions.is_empty()); +} + +/// A license server for a DRM system the media carries no `pssh` for is a configuration mistake. +#[test] +fn a_drm_system_the_media_lacks_is_refused() { + use hang::catalog::drm; + + let servers = drm::Servers::license("https://license.example.com/".parse().unwrap()); + let err = protected_error(&[CENC], |fmp4| fmp4.with_drm(drm::system::WIDEVINE, servers.clone())); + assert!( + matches!(err, super::Error::UnusedDrmSystem(system) if system == drm::system::WIDEVINE), + "{err:?}" + ); +} + +/// Without passthrough the importer would unwrap samples and lose their per-sample IVs, so an +/// encrypted file is refused rather than published undecryptable. +#[test] +fn encrypted_unwrapped_import_is_refused() { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); + let mut fmp4 = super::Import::new(broadcast, catalog.reserve()); + let err = fmp4.decode(CBCS_VIDEO).unwrap_err(); + assert!( + matches!(err, crate::Error::Cmaf(super::Error::ProtectedNeedsPassthrough)), + "{err:?}" + ); +} + +/// Passthrough re-encodes each fragment, which moves `senc`; every published fragment's `saio` +/// still points at its IVs, which are the source's. +#[tokio::test] +async fn encrypted_fragments_keep_saio_on_their_ivs() { + for data in [CENC, CBCS_VIDEO] { + let (result, catalog, consumer, _importers) = import_protected(&[data], |fmp4| fmp4); + result.unwrap(); + let snapshot = catalog.snapshot(); + let source = first_moof(data); + for (name, config) in &snapshot.video.renditions { + let Container::Cmaf { init } = &config.container else { + unreachable!() + }; + let (_, moov) = decode_init(init); + let track_id = moov.trak[0].tkhd.track_id; + let published = fragments(&consumer, name).await; + assert!(!published.is_empty(), "{name}"); + let senc = checked_senc(&published[0]); + let traf = source.traf.iter().find(|traf| traf.tfhd.track_id == track_id).unwrap(); + assert_eq!(Some(senc), traf.senc, "the IVs are the source's"); + for fragment in &published[1..] { + checked_senc(fragment); + } + } + } +} + +/// Shaka starts AAC on an edit that skips its priming frame. The edit list drops that sample, so +/// the first fragment's `senc` is restated for the kept samples rather than refused. +#[tokio::test] +async fn an_edit_restates_the_kept_samples_encryption() { + let (result, catalog, consumer, _importers) = import_protected(&[CBCS_AUDIO], |fmp4| fmp4); + result.unwrap(); + let name = catalog.snapshot().audio.renditions.keys().next().unwrap().clone(); + let published = fragments(&consumer, &name).await; + let first = mp4_atom::Moof::decode(&mut &published[0][..]).unwrap(); + let samples = super::sample::count(&first.traf[0]); + assert_eq!(samples, super::sample::count(&first_moof(CBCS_AUDIO).traf[0]) - 1); + let senc = checked_senc(&published[0]); + assert_eq!( + senc.data, + (samples as u32).to_be_bytes(), + "one empty entry per kept sample" + ); +} + +/// The same with per-sample IVs and subsamples: an edit dropping the `cenc` file's first audio +/// frame drops that frame's IV and size, and keeps the rest byte for byte. +#[tokio::test] +async fn an_edit_drops_the_skipped_samples_ivs() { + let data = rewrite(CENC, |atom| { + if let mp4_atom::Any::Moov(moov) = atom { + let audio = moov + .trak + .iter_mut() + .find(|trak| trak.mdia.hdlr.handler == b"soun".into()) + .unwrap(); + let frame = 1024; + audio.edts = Some(mp4_atom::Edts { + elst: Some(mp4_atom::Elst { + entries: vec![mp4_atom::ElstEntry { + segment_duration: 0, + media_time: Some(frame), + media_rate: mp4_atom::FixedPoint::from(1), + }], + }), + }); + } + }); + let (result, catalog, consumer, _importers) = import_protected(&[&data], |fmp4| fmp4); + result.unwrap(); + let (name, config) = catalog.snapshot().audio.renditions.into_iter().next().unwrap(); + let Container::Cmaf { init } = &config.container else { + unreachable!() + }; + let track_id = decode_init(init).1.trak[0].tkhd.track_id; + let source = first_moof(&data); + let source = source.traf.iter().find(|traf| traf.tfhd.track_id == track_id).unwrap(); + + let published = fragments(&consumer, &name).await; + let senc = checked_senc(&published[0]); + let kept = super::sample::count(source) as u32 - 1; + // 8-byte IVs and no subsamples in ffmpeg's audio: the first IV goes. + let source_senc = source.senc.as_ref().unwrap(); + assert!(!source_senc.use_subsamples); + let mut expected = kept.to_be_bytes().to_vec(); + expected.extend_from_slice(&source_senc.data[4 + 8..]); + assert_eq!(senc.data, expected); + let moof = mp4_atom::Moof::decode(&mut &published[0][..]).unwrap(); + assert_eq!(moof.traf[0].saiz[0].sample_count, kept); +} + +/// Keys that rotate are refused: a `pssh` in a `moof`, or a `seig` sample group. +#[test] +fn key_rotation_is_refused() { + let pssh = rewrite(CBCS_VIDEO, |atom| { + if let mp4_atom::Any::Moof(moof) = atom { + moof.pssh.push(mp4_atom::Pssh { + system_id: *hang::catalog::drm::system::CLEARKEY.as_bytes(), + kid: Some(vec![*CLEARKEY_KID.as_bytes()]), + data: Vec::new(), + }); + } + }); + let seig = rewrite(CBCS_VIDEO, |atom| { + if let mp4_atom::Any::Moof(moof) = atom { + moof.traf[0].sbgp.push(mp4_atom::Sbgp { + grouping_type: b"seig".into(), + grouping_type_parameter: None, + entries: vec![mp4_atom::SbgpEntry { + sample_count: 1, + group_description_index: 1, + }], + }); + } + }); + for data in [pssh, seig] { + let err = protected_error(&[&data], |fmp4| fmp4); + assert!(matches!(err, super::Error::KeyRotation), "{err:?}"); + } +} + +/// Without a `pssh` the catalog could name no DRM system, and the track would read as clear. +#[test] +fn an_encrypted_track_without_pssh_is_refused() { + let data = rewrite(CBCS_VIDEO, |atom| { + if let mp4_atom::Any::Moov(moov) = atom { + moov.pssh.clear(); + } + }); + let err = protected_error(&[&data], |fmp4| fmp4); + assert!(matches!(err, super::Error::MissingPssh), "{err:?}"); +} + +/// IVs the rebuilt fragment would not carry are refused: ones outside `senc`, or split over +/// several `saio`. +#[test] +fn encryption_information_outside_senc_is_refused() { + let outside = rewrite(CBCS_VIDEO, |atom| { + if let mp4_atom::Any::Moof(moof) = atom { + moof.traf[0].senc = None; + } + }); + let split = rewrite(CBCS_VIDEO, |atom| { + if let mp4_atom::Any::Moof(moof) = atom { + let saio = moof.traf[0].saio[0].clone(); + moof.traf[0].saio.push(saio); + } + }); + for data in [outside, split] { + let err = protected_error(&[&data], |fmp4| fmp4); + assert!(matches!(err, super::Error::InvalidProtection(_)), "{err:?}"); + } +} + +/// A clear lead (an encrypted entry beside a clear one) stays refused as several codecs. +#[test] +fn a_clear_lead_is_refused() { + let data = rewrite(CBCS_VIDEO, |atom| { + if let mp4_atom::Any::Moov(moov) = atom { + let codecs = &mut moov.trak[0].mdia.minf.stbl.stsd.codecs; + let mp4_atom::Codec::Encv(encv) = &codecs[0] else { + unreachable!() + }; + let clear = (*encv.codec).clone(); + codecs.push(clear); + } + }); + let err = protected_error(&[&data], |fmp4| fmp4); + assert!(matches!(err, super::Error::MultipleCodecs), "{err:?}"); +} + +/// A fragment of an encrypted track without the `senc` its per-sample IVs (or, for video, its +/// subsample maps) need is refused, rather than republished undecryptable. +#[test] +fn an_encrypted_fragment_without_senc_is_refused() { + for data in [CENC, CBCS_VIDEO] { + let data = rewrite(data, |atom| { + if let mp4_atom::Any::Moof(moof) = atom { + for traf in &mut moof.traf { + traf.senc = None; + traf.saio.clear(); + traf.saiz.clear(); + } + } + }); + let err = protected_error(&[&data], |fmp4| fmp4); + assert!( + matches!(err, super::Error::InvalidProtection("encrypted fragment without senc")), + "{err:?}" + ); + } +} + +/// Only `cenc` and `cbcs` are carried, the two schemes CMSF allows. +#[test] +fn another_scheme_is_refused() { + let data = rewrite(CBCS_VIDEO, |atom| { + if let mp4_atom::Any::Moov(moov) = atom { + let mp4_atom::Codec::Encv(encv) = &mut moov.trak[0].mdia.minf.stbl.stsd.codecs[0] else { + unreachable!() + }; + encv.sinf.schm.as_mut().unwrap().scheme_type = b"cens".into(); + } + }); + let err = protected_error(&[&data], |fmp4| fmp4); + assert!( + matches!(err, super::Error::UnsupportedScheme(scheme) if scheme == b"cens".into()), + "{err:?}" + ); +} + +/// The video track's ID and its `tenc`'s per-sample IV size. +fn protected_video(data: &[u8]) -> (u32, u8) { + use mp4_atom::DecodeMaybe; + + let mut cursor = std::io::Cursor::new(data); + while let Some(atom) = mp4_atom::Any::decode_maybe(&mut cursor).unwrap() { + if let mp4_atom::Any::Moov(moov) = atom { + let trak = (moov.trak.iter()) + .find(|trak| trak.mdia.hdlr.handler == b"vide".into()) + .unwrap(); + let mp4_atom::Codec::Encv(encv) = &trak.mdia.minf.stbl.stsd.codecs[0] else { + panic!("expected encv"); + }; + let tenc = encv.sinf.schi.as_ref().unwrap().tenc.as_ref().unwrap(); + return (trak.tkhd.track_id, tenc.default_per_sample_iv_size); + } + } + panic!("no moov"); +} + +/// Each sample's entry in `senc`: its IV, then its subsample map. +fn senc_entries(senc: &mp4_atom::Senc, iv_size: usize) -> Vec> { + let data = &senc.data[..]; + let count = u32::from_be_bytes(data[..4].try_into().unwrap()); + let mut position = 4; + let mut entries = Vec::new(); + for _ in 0..count { + let start = position; + position += iv_size; + if senc.use_subsamples { + let subsamples = u16::from_be_bytes(data[position..position + 2].try_into().unwrap()) as usize; + position += 2 + 6 * subsamples; + } + entries.push(data[start..position].to_vec()); + } + assert_eq!(position, data.len()); + entries +} + +/// A stream joined mid-GOP drops the samples before its first sync sample, the same as an edit +/// list does: the kept samples' IVs, subsample maps, and `saiz` sizes are restated, with the +/// per-sample IVs of the `cenc` file and the constant IV of Shaka's `cbcs` video alike. +#[tokio::test] +async fn a_mid_gop_start_restates_the_kept_samples_encryption() { + const NON_SYNC: u32 = 0x0101_0000; + const SYNC: u32 = 0x0200_0000; + const SKIPPED: usize = 2; + + for source in [CENC, CBCS_VIDEO] { + let (video, iv_size) = protected_video(source); + // The first fragment's first two video samples become non-sync and its third a sync sample. + let mut first = true; + let data = rewrite(source, |atom| { + let mp4_atom::Any::Moof(moof) = atom else { + return; + }; + if !std::mem::take(&mut first) { + return; + } + let traf = moof.traf.iter_mut().find(|traf| traf.tfhd.track_id == video).unwrap(); + let mut index = 0; + for trun in &mut traf.trun { + let mut entries: Vec<_> = trun.samples.iter().collect(); + for entry in &mut entries { + entry.flags = Some(if index == SKIPPED { SYNC } else { NON_SYNC }); + index += 1; + } + trun.samples = mp4_atom::TrunSamples::Explicit(entries); + } + }); + + let (result, catalog, consumer, _importers) = import_protected(&[&data], |fmp4| fmp4); + result.unwrap(); + let name = catalog.snapshot().video.renditions.keys().next().unwrap().clone(); + let published = fragments(&consumer, &name).await; + + let source = first_moof(&data); + let source = source.traf.iter().find(|traf| traf.tfhd.track_id == video).unwrap(); + let total = super::sample::count(source) as usize; + let moof = mp4_atom::Moof::decode(&mut &published[0][..]).unwrap(); + assert_eq!(super::sample::count(&moof.traf[0]) as usize, total - SKIPPED); + + let senc = checked_senc(&published[0]); + let source_senc = source.senc.as_ref().unwrap(); + assert!(source_senc.use_subsamples, "encrypted video carries subsample maps"); + let kept = senc_entries(&senc, iv_size as usize); + assert_eq!(kept, senc_entries(source_senc, iv_size as usize)[SKIPPED..]); + + let saiz = &moof.traf[0].saiz[0]; + assert_eq!(saiz.sample_count as usize, total - SKIPPED); + let sizes: Vec<_> = kept.iter().map(|entry| entry.len()).collect(); + match saiz.default_sample_info_size { + 0 => assert_eq!( + saiz.sample_info_size + .iter() + .map(|&size| size as usize) + .collect::>(), + sizes + ), + size => assert!(sizes.iter().all(|&entry| entry == size as usize)), + } + } +} + +/// Importers sharing a key but not their servers get an entry each, so neither rendition is +/// signalled with the other's license server. +#[test] +fn different_servers_for_one_key_take_their_own_entries() { + use hang::catalog::drm; + + let servers = [ + drm::Servers::license("https://a.example.com/".parse().unwrap()), + drm::Servers::license("https://b.example.com/".parse().unwrap()), + ]; + let index = std::cell::Cell::new(0); + let (result, catalog, _, _importers) = import_protected(&[CBCS_VIDEO, CBCS_AUDIO], |fmp4| { + let servers = servers[index.replace(index.get() + 1)].clone(); + fmp4.with_drm(drm::system::CLEARKEY, servers) + }); + result.unwrap(); + let catalog = catalog.snapshot(); + assert_eq!(catalog.drm.len(), 2, "{:?}", catalog.drm); + let video = &catalog.video.renditions.values().next().unwrap().drm; + let audio = &catalog.audio.renditions.values().next().unwrap().drm; + assert_ne!(video, audio); + assert_eq!(catalog.drm[&video[0]].license, servers[0].license); + assert_eq!(catalog.drm[&audio[0]].license, servers[1].license); +} + +/// A shared entry stays while any rendition lists it and leaves with the last one. +#[test] +fn a_drm_entry_leaves_with_its_last_rendition() { + let (result, catalog, _, mut importers) = import_protected(&[CBCS_VIDEO, CBCS_AUDIO], |fmp4| fmp4); + result.unwrap(); + assert_eq!(catalog.snapshot().drm.len(), 1); + + drop(importers.remove(0)); + let snapshot = catalog.snapshot(); + assert!(snapshot.video.renditions.is_empty()); + assert_eq!(snapshot.drm.len(), 1, "the audio still lists it"); + + drop(importers); + assert!(catalog.snapshot().drm.is_empty()); +} diff --git a/rs/moq-mux/src/container/fmp4/mod.rs b/rs/moq-mux/src/container/fmp4/mod.rs index 53d6d7f108b..f1f1c7f32d7 100644 --- a/rs/moq-mux/src/container/fmp4/mod.rs +++ b/rs/moq-mux/src/container/fmp4/mod.rs @@ -6,6 +6,7 @@ //! container; [`Import`] parses external fMP4 streams and [`Export`] //! produces them. +pub(crate) mod cenc; mod edit; mod export; pub mod fragment; @@ -171,6 +172,32 @@ pub enum Error { #[error("unknown codec: {0:?}")] UnknownCodec(mp4_atom::FourCC), + /// An encrypted (`encv`/`enca`) track whose protection boxes, or whose fragments' encryption + /// information, the republished media could not carry intact. + #[error("invalid protected track: {0}")] + InvalidProtection(&'static str), + + /// An encrypted track under a Common Encryption scheme other than `cenc` and `cbcs`. + #[error("unsupported encryption scheme: {0:?}")] + UnsupportedScheme(mp4_atom::FourCC), + + /// Encrypted samples carry their per-sample IVs in the fragment's `senc`, which only + /// passthrough keeps; unwrapped samples would be undecryptable. + #[error("encrypted fMP4 needs passthrough (CMAF) import")] + ProtectedNeedsPassthrough, + + /// An encrypted track without a `pssh` box, so the catalog could name no DRM system for it. + #[error("encrypted track without a pssh box")] + MissingPssh, + + /// Keys that rotate within the track: a `pssh` box in a `moof`, or a `seig` sample group. + #[error("key rotation is not supported")] + KeyRotation, + + /// A license or certificate server for a DRM system no encrypted track carries a `pssh` for. + #[error("DRM system {0} protects no track")] + UnusedDrmSystem(hang::catalog::drm::Uuid), + /// A `colr` box code point past H.273's 255. #[error("invalid colr code point: {0}")] InvalidColr(u16), @@ -781,6 +808,7 @@ pub(super) fn encode_at(info: FragmentInfo, base_dts: u64, frames: &[Frame]) -> let mdat_data: Vec = frames.iter().flat_map(|f| f.payload.iter().copied()).collect(); let build_moof = |data_offset| mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { @@ -1821,6 +1849,7 @@ mod tests { // hand-built moof+mdat with explicit per-sample durations is enough. let timescale = moq_net::Timescale::MICRO; let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 0 }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { @@ -2244,6 +2273,7 @@ mod tests { let timescale = moq_net::Timescale::new(24_000).unwrap(); let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 0 }, traf: vec![mp4_atom::Traf { tfhd: mp4_atom::Tfhd { diff --git a/rs/moq-mux/src/container/fmp4/sample.rs b/rs/moq-mux/src/container/fmp4/sample.rs index 5f18533c995..0927513a2b4 100644 --- a/rs/moq-mux/src/container/fmp4/sample.rs +++ b/rs/moq-mux/src/container/fmp4/sample.rs @@ -378,6 +378,7 @@ mod tests { run.samples = samples.clone(); } let moof = mp4_atom::Moof { + pssh: Vec::new(), mfhd: mp4_atom::Mfhd { sequence_number: 1 }, traf: vec![traf], }; diff --git a/rs/moq-mux/src/container/fmp4/test_data/cbcs-audio.mp4 b/rs/moq-mux/src/container/fmp4/test_data/cbcs-audio.mp4 new file mode 100644 index 00000000000..afa0e008be8 Binary files /dev/null and b/rs/moq-mux/src/container/fmp4/test_data/cbcs-audio.mp4 differ diff --git a/rs/moq-mux/src/container/fmp4/test_data/cbcs-video.mp4 b/rs/moq-mux/src/container/fmp4/test_data/cbcs-video.mp4 new file mode 100644 index 00000000000..f87c54e3f82 Binary files /dev/null and b/rs/moq-mux/src/container/fmp4/test_data/cbcs-video.mp4 differ diff --git a/rs/moq-mux/src/container/fmp4/test_data/cenc-clearkey.mp4 b/rs/moq-mux/src/container/fmp4/test_data/cenc-clearkey.mp4 new file mode 100644 index 00000000000..edb6e9c1eee Binary files /dev/null and b/rs/moq-mux/src/container/fmp4/test_data/cenc-clearkey.mp4 differ diff --git a/rs/moq-mux/src/error.rs b/rs/moq-mux/src/error.rs index f4f916e2e23..19c1482d91c 100644 --- a/rs/moq-mux/src/error.rs +++ b/rs/moq-mux/src/error.rs @@ -282,6 +282,11 @@ pub enum Error { #[error("rendition `{0}` changed identity; publish it under a new track name")] IdentityChanged(String), + /// Every video and audio rendition of a catalog is protected, so a selection has nothing it can + /// export: nothing here decrypts (see [`retain_clear`](crate::catalog::hang::retain_clear)). + #[error("no rendition can be exported: every one is protected")] + Protected, + /// A rendition moved to a track whose media goes back before what the export already read for it. #[error( "rendition {rendition} moved to a track that goes back in time: a group starts at {start:?}, not past {read:?}" diff --git a/rs/moq-mux/src/source.rs b/rs/moq-mux/src/source.rs index f7673a46ee6..cd1e12baef8 100644 --- a/rs/moq-mux/src/source.rs +++ b/rs/moq-mux/src/source.rs @@ -264,15 +264,18 @@ impl Source { /// /// Every export runs this on each catalog snapshot, so a disabled rendition reads as /// absent: one that is disabled later leaves the snapshot, and one that re-enables - /// returns to it. Every section carrying renditions must be listed here; one left out - /// silently exempts its renditions from the containment check, exactly as on the - /// consumer side. + /// returns to it. A protected one reads as absent as well, since no export decrypts it + /// (see [`retain_clear`](crate::catalog::hang::retain_clear)). Every section carrying + /// renditions must be listed here; one left out silently exempts its renditions from the + /// containment check, exactly as on the consumer side. pub(crate) fn retain_selectable( &self, catalog: &mut crate::catalog::hang::Catalog, ) { catalog.video.renditions.retain(|_, config| config.enabled); catalog.audio.renditions.retain(|_, config| config.enabled); + // Nothing exported is decrypted, so a protected rendition reads as absent too. + crate::catalog::hang::retain_clear(catalog); self.retain_valid_references("video", &mut catalog.video.renditions); self.retain_valid_references("audio", &mut catalog.audio.renditions); self.retain_valid_references("text", &mut catalog.text.renditions); @@ -1046,6 +1049,33 @@ mod tests { assert!(catalog.audio.renditions.is_empty()); } + /// A protected rendition is never exported, since nothing exported is decrypted, while its + /// clear sibling is. + #[test] + fn protected_rendition_is_removed_while_clear_sibling_remains() { + let origin = produce_origin(); + let source = created(&origin); + let clear = VideoConfig::new(H264 { + profile: 0x42, + constraints: 0, + level: 0x1e, + inline: false, + }); + let mut protected = clear.clone(); + protected.drm = vec!["1".into()]; + let mut audio = hang::catalog::AudioConfig::new(hang::catalog::AudioCodec::Opus, 48_000, 2); + audio.drm = vec!["1".into()]; + + let mut catalog = hang::Catalog::<()>::default(); + catalog.video.renditions.insert("protected".to_string(), protected); + catalog.video.renditions.insert("clear".to_string(), clear); + catalog.audio.renditions.insert("audio".to_string(), audio); + source.retain_selectable(&mut catalog); + + assert_eq!(catalog.video.renditions.keys().collect::>(), vec!["clear"]); + assert!(catalog.audio.renditions.is_empty()); + } + /// The filter covers every section carrying renditions, not just the media ones. Text /// is the section that only exists on one side of the containment check by default, so /// it is the one that silently goes unchecked when the two sides drift. diff --git a/rs/moq-rtc/src/egress.rs b/rs/moq-rtc/src/egress.rs index 25327f5dacf..4011f3932c7 100644 --- a/rs/moq-rtc/src/egress.rs +++ b/rs/moq-rtc/src/egress.rs @@ -199,7 +199,9 @@ async fn follow_catalog(mut consumer: moq_mux::catalog::Consumer<()>, tx: watch: loop { tokio::select! { next = consumer.next() => match next { - Ok(Some(catalog)) => { + Ok(Some(mut catalog)) => { + // WebRTC egress would forward ciphertext as media; it does not decrypt. + moq_mux::catalog::hang::retain_clear(&mut catalog); tx.send_replace(catalog); } Ok(None) => return Ok(()), diff --git a/rs/moq-transcode/src/catalog.rs b/rs/moq-transcode/src/catalog.rs index a1f77512202..1be66d9a510 100644 --- a/rs/moq-transcode/src/catalog.rs +++ b/rs/moq-transcode/src/catalog.rs @@ -148,6 +148,7 @@ impl Decoders { let mut config = rendition.clone(); config.description = None; config.container = Container::Legacy; + config.drm.clear(); match &mut config.codec { VideoCodec::H264(h264) => h264.inline = true, VideoCodec::H265(h265) => h265.in_band = true, @@ -172,6 +173,8 @@ pub(crate) async fn choose_source(video: &Video, decoders: &mut Decoders) -> Res // A rendition that itself lives in another broadcast can't be subscribed // through this one; composing relative references is a follow-up. .filter(|(_, config)| config.broadcast.is_none()) + // A protected rendition's samples are encrypted, and nothing here decrypts them. + .filter(|(_, config)| config.drm.is_empty()) .filter_map(|(name, config)| Some((name, config, codec(config)?))); let mut refused = None; @@ -220,6 +223,7 @@ pub(crate) async fn follow_source( let mut kept = None; if let Some(config) = video.renditions.get(current) && config.broadcast.is_none() + && config.drm.is_empty() && dimensions(config).is_some() && let Some(codec) = codec(config) // The name can stay while the codec changes under it. @@ -359,6 +363,8 @@ pub(crate) fn populate( ) -> Result<(), Error> { out.video = Video::default(); out.audio = hang::catalog::Audio::default(); + // The rungs are clear; only the source renditions listed below may reference an entry. + out.drm.clear(); // The derivative republishes the child's timeline, so its timestamps only mean something under // the child's wall mapping. out.media.clock = source.media.clock; @@ -381,6 +387,8 @@ pub(crate) fn populate( if out.media.join.is_none() { out.media.join = source.media.join; } + // A protected source rendition keeps its `drm` IDs, so the entries come along. + out.drm = source.drm.clone(); for (name, config) in &source.video.renditions { if config.broadcast.is_some() { @@ -1076,4 +1084,37 @@ mod tests { }; assert_eq!(frame.timestamp.unwrap().as_millis(), adopted.now().as_millis()); } + + /// A protected rendition's samples are encrypted, so it is never the source, however well it + /// ranks; the derivative still lists it, with the `drm` entries it references. + #[tokio::test] + async fn a_protected_rendition_is_not_transcoded() { + use hang::catalog::drm; + + let mut child = moq_mux::catalog::hang::Catalog::<()>::default(); + child.drm.insert( + "1".into(), + drm::Entry::new(drm::system::CLEARKEY, drm::Scheme::Cbcs, drm::Uuid::nil()), + ); + let mut protected = source(1920, 1080, None); + protected.drm = vec!["1".into()]; + child.video.insert("protected", protected).unwrap(); + child.video.insert("clear", source(640, 360, None)).unwrap(); + + let (name, _) = choose_source(&child.video, &mut decodes_all()).await.unwrap(); + assert_eq!(name, "clear"); + let (name, _) = follow_source(&child.video, "protected", &mut decodes_all()) + .await + .unwrap(); + assert_eq!(name, "clear"); + + let rel = Relative::new(".").unwrap(); + let mut out = moq_mux::catalog::hang::Catalog::<()>::default(); + populate(&mut out, &child, &[], Some(&rel)).unwrap(); + assert_eq!(out.drm, child.drm); + out.check_renditions().unwrap(); + + populate(&mut out, &child, &[], None).unwrap(); + assert!(out.drm.is_empty(), "only the rungs, which are clear"); + } } diff --git a/rs/moq-video/src/decode/decoder.rs b/rs/moq-video/src/decode/decoder.rs index d03151c89e4..3b1669d1288 100644 --- a/rs/moq-video/src/decode/decoder.rs +++ b/rs/moq-video/src/decode/decoder.rs @@ -172,11 +172,17 @@ impl Decoder { /// Build a decoder for the catalog's video config. /// /// A codec or description this build cannot decode is - /// [`Error::UnsupportedCodec`], and so is a stream every decoder for its codec - /// refused because its device cannot decode it. When any of them failed to open - /// for another reason, such as a missing driver, the result is - /// [`Error::NoDecoder`], a fault rather than a property of the track. + /// [`Error::UnsupportedCodec`], as is a protected rendition (one listing `drm` entries), whose + /// samples are encrypted, and a stream every decoder for its codec refused because its + /// device cannot decode it. When any of them failed to open for another reason, such as + /// a missing driver, the result is [`Error::NoDecoder`], a fault rather than a property + /// of the track. pub fn new(catalog: &VideoConfig, config: &Config) -> Result { + if !catalog.drm.is_empty() { + return Err(Error::UnsupportedCodec( + "the rendition is encrypted, and decryption is not supported".into(), + )); + } // CMAF samples are length-prefixed whatever the sample entry; Legacy and LOC // carry Annex-B exactly when the parameter sets are in band. let cmaf = matches!(catalog.container, Container::Cmaf { .. }); @@ -523,6 +529,23 @@ mod tests { crate::Size::new(320, 240) } + /// A protected rendition's samples are encrypted, so it is refused like a codec this build + /// cannot decode, before any backend opens. + #[test] + fn a_protected_rendition_is_unsupported() { + let mut catalog = hang::catalog::VideoConfig::new(hang::catalog::H264 { + inline: true, + profile: 0x42, + constraints: 0, + level: 0x1e, + }); + catalog.drm = vec!["1".into()]; + assert!(matches!( + super::Decoder::new(&catalog, &super::Config::new()), + Err(crate::Error::UnsupportedCodec(_)) + )); + } + #[test] #[cfg(feature = "openh264")] fn openh264_round_trip() { diff --git a/rs/workspace-hack/Cargo.toml b/rs/workspace-hack/Cargo.toml index b0e290810ca..8597ac57bd6 100644 --- a/rs/workspace-hack/Cargo.toml +++ b/rs/workspace-hack/Cargo.toml @@ -128,7 +128,7 @@ typenum = { version = "1", default-features = false, features = ["const-generics url = { version = "2", features = ["serde"] } usage-argv = { version = "7", default-features = false, features = ["complete", "diagnostics", "help-advanced"] } usage-rs = { version = "7", features = ["completions", "config"] } -uuid = { version = "1", features = ["v4", "v7"] } +uuid = { version = "1", features = ["serde", "v4", "v7"] } winnow-dff4ba8e3ae991db = { package = "winnow", version = "1", features = ["simd"] } zeroize = { version = "1", features = ["derive"] } @@ -244,7 +244,7 @@ typenum = { version = "1", default-features = false, features = ["const-generics url = { version = "2", features = ["serde"] } usage-argv = { version = "7", default-features = false, features = ["complete", "diagnostics", "help-advanced"] } usage-rs = { version = "7", features = ["completions", "config"] } -uuid = { version = "1", features = ["v4", "v7"] } +uuid = { version = "1", features = ["serde", "v4", "v7"] } winnow-ca01ad9e24f5d932 = { package = "winnow", version = "0.7" } winnow-dff4ba8e3ae991db = { package = "winnow", version = "1", features = ["simd"] } zeroize = { version = "1", features = ["derive"] } @@ -343,7 +343,6 @@ tokio = { version = "1", default-features = false, features = ["tracing"] } tokio-rustls = { version = "0.26", default-features = false, features = ["aws-lc-rs", "tls12"] } tower = { version = "0.5", default-features = false, features = ["retry", "timeout"] } tower-http = { version = "0.6", default-features = false, features = ["decompression-gzip", "follow-redirect"] } -uuid = { version = "1", default-features = false, features = ["serde"] } zerocopy = { version = "0.8", default-features = false, features = ["derive", "simd"] } [target.aarch64-unknown-linux-gnu.build-dependencies] @@ -373,7 +372,6 @@ tokio = { version = "1", default-features = false, features = ["tracing"] } tokio-rustls = { version = "0.26", default-features = false, features = ["aws-lc-rs", "tls12"] } tower = { version = "0.5", default-features = false, features = ["retry", "timeout"] } tower-http = { version = "0.6", default-features = false, features = ["decompression-gzip", "follow-redirect"] } -uuid = { version = "1", default-features = false, features = ["serde"] } zerocopy = { version = "0.8", default-features = false, features = ["derive", "simd"] } [target.x86_64-pc-windows-msvc.dependencies] @@ -448,7 +446,6 @@ tokio = { version = "1", default-features = false, features = ["tracing"] } tokio-rustls = { version = "0.26", default-features = false, features = ["aws-lc-rs", "tls12"] } tower = { version = "0.5", default-features = false, features = ["retry", "timeout"] } tower-http = { version = "0.6", default-features = false, features = ["decompression-gzip", "follow-redirect"] } -uuid = { version = "1", default-features = false, features = ["serde"] } zerocopy = { version = "0.8", default-features = false, features = ["derive", "simd"] } [target.x86_64-unknown-linux-gnu.build-dependencies] @@ -478,7 +475,6 @@ tokio = { version = "1", default-features = false, features = ["tracing"] } tokio-rustls = { version = "0.26", default-features = false, features = ["aws-lc-rs", "tls12"] } tower = { version = "0.5", default-features = false, features = ["retry", "timeout"] } tower-http = { version = "0.6", default-features = false, features = ["decompression-gzip", "follow-redirect"] } -uuid = { version = "1", default-features = false, features = ["serde"] } zerocopy = { version = "0.8", default-features = false, features = ["derive", "simd"] } ### END HAKARI SECTION