Skip to content

feat(media): choose HLS/DASH quality, merge separate audio and decrypt AES-128 HLS - #584

Merged
linroid merged 4 commits into
mainfrom
feat/media-formats
Oct 18, 2026
Merged

linroid merged 4 commits into
mainfrom
feat/media-formats

Conversation

@linroid

@linroid linroid commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Implements the roadmap item "More HLS and DASH formats".

What changes

  • Quality choice: HLS variants and DASH video representations resolve as ResolvedSource.files with FileSelectionMode.SINGLE, best first (video before audio-only, then by declared bandwidth; FFmpeg's masters list the audio-only stream as a higher-bandwidth variant). Both sources implement planSelection and list download.fileSelection / download.awaitFileSelection. A selection made while downloading restarts the transfer with that variant (MediaDownloadHelper.downloadFollowingSelection). A download whose manifest no longer lists the chosen ID's bandwidth fails instead of downloading another quality.
  • Separate audio: an HLS variant's AUDIO rendition and a DASH presentation's main audio adaptation set are merged with the video into one fragmented MP4 when both are fMP4 (mergeTracks, Mp4Tracks.kt). The two init segments become one moov (video track 1, audio track 2, audio durations rescaled). Each fragment streams through FragmentRewriter, which keeps moof/mdat, renumbers sequences and track IDs and moves absolute base data offsets. Fragments are interleaved by start time. Samples are never decoded.
  • AES-128 HLS: EXT-X-KEY:METHOD=AES-128 (identity key format; an IV, else the media sequence number) is decrypted as bytes arrive by a pure Kotlin Aes128CbcDecryptor, so it works on every engine target. SAMPLE-AES and other DRM are still refused.
  • Add sheet: single-choice sources are no longer shown as torrent-style checkboxes (which could send several IDs and be refused). The sheet downloads the best quality, and the inspector's Files tab switches. A quality picker in the sheet is a follow-up.
  • Docs: docs/media.md, all READMEs (roadmap item removed), CLI, extension and API docs, AGENTS.md.

Still unsupported: separate audio for MPEG-TS/AAC/MP3 HLS, picking an audio language, live streams, DRM.

Testing

  • Core: AES against NIST SP 800-38A and OpenSSL vectors; the init merge and fragment rewriter against synthetic boxes in any chunk size; helper end to end (decrypt, merge, selection restart, planSelection).
  • HLS/DASH parser and source tests: variants, renditions, keys/IVs, DRM refusal, stale variants.
  • New FFmpeg fixtures (library/ktor/src/jvmTest/resources/media/{dash-tracks,hls-tracks}, regeneration steps in their README): DASH with two video representations and separate AAC, and HLS fMP4 with a separate audio rendition whose segments are AES-128 encrypted with OpenSSL. MediaDownloadIntegrationTest checks the merged file holds both tracks and every fragment's decrypted media in order. Locally the merged outputs decode with ffmpeg (H.264 + AAC; audio frame counts match the sources: 16 and 17).
  • Ran: core/hls/dash on JVM, JS, WasmWasi and Android host; iOS test compile; :library:ktor:jvmTest, :app:shared:jvmTest, :cli:test, :library:server:test, :library:mcp:test.
  • Core's full wasmWasiNodeTest fails in AwaitFileSelectionTest. That failure reproduces on a clean main and is tracked separately.

…t AES-128 HLS

- HLS variants and DASH video representations resolve as a single choice
  (FileSelectionMode.SINGLE), best first with video before audio-only; a
  selection made while downloading restarts with that variant
- Separate fragmented MP4 audio (an HLS AUDIO rendition, the main DASH audio
  adaptation set) is merged with the video into one MP4 by rewriting box
  headers, fragments interleaved by start time
- AES-128 HLS segments are decrypted as they arrive with a pure Kotlin
  AES-128-CBC decryptor; SAMPLE-AES and other DRM stay refused
- The add sheet downloads the best variant instead of offering stream
  variants as torrent-style checkboxes
- Real ffmpeg fixtures (DASH with separate tracks, encrypted HLS fMP4 with a
  separate audio rendition) back an end-to-end integration test
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-17T16:34:42.887907Z b98feed PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b98feede2d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…ep boxes before mdat

- Variant IDs are hashes of each variant's manifest identity rather than
  positions, so a refreshed or re-signed manifest cannot map a resumed task's
  chosen ID to another quality
- A variant whose container differs from the output name Ketch chose fails
  instead of writing TS bytes under .mp4; a file destination the user named
  is left alone
- Boxes between a moof and its mdat are kept, as trun data offsets count them
- HLS IVs of any width up to 32 hex digits are left-padded, per RFC 8216
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 2 060 files  + 12   2 088 suites  +12   32m 56s ⏱️ -40s
13 835 tests +119  13 571 ✅ +119  264 💤 ±0  0 ❌ ±0 
16 826 runs  +119  16 562 ✅ +119  264 💤 ±0  0 ❌ ±0 

Results for commit 6e4f9a1. ± Comparison against base commit 2462373.

@linroid
linroid merged commit 3626340 into main Oct 18, 2026
10 of 11 checks passed
@linroid
linroid deleted the feat/media-formats branch October 18, 2026 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant