Skip to content

feat(demuxer): add async iteration and demuxAsync method#17

Merged
Brooooooklyn merged 1 commit into
mainfrom
12-28-polish_muxer_api
Dec 30, 2025
Merged

feat(demuxer): add async iteration and demuxAsync method#17
Brooooooklyn merged 1 commit into
mainfrom
12-28-polish_muxer_api

Conversation

@Brooooooklyn

@Brooooooklyn Brooooooklyn commented Dec 30, 2025

Copy link
Copy Markdown
Owner

Add for await...of loop support and demuxAsync() method to all demuxers (Mp4Demuxer, WebMDemuxer, MkvDemuxer).

Changes:

  • Implement AsyncGenerator trait via NAPI-RS for all demuxers
  • Add read_next_chunk() method for single-packet reading
  • Add demuxAsync() as awaitable alternative to sync demux()
  • Add DemuxerChunk type with discriminated union pattern
  • Update TypeScript types with declaration merging for Symbol.asyncIterator
  • Add 10 tests covering async iteration and demuxAsync

The async iterator provides a cleaner streaming API:

  for await (const chunk of demuxer) {
    if (chunk.chunkType === 'video') {
      videoDecoder.decode(chunk.videoChunk!)
    }
  }

The demuxAsync() method eliminates the polling pattern:

  // Before
  while (demuxer.state !== 'ended') {
    demuxer.demux(100)
    await new Promise(r => setTimeout(r, 1))
  }

After

  await demuxer.demuxAsync()

Note

Introduces an async, stream-friendly demuxing API and awaitable demux calls.

  • Async iteration: Implemented [Symbol.asyncIterator] on Mp4Demuxer, WebMDemuxer, MkvDemuxer via #[napi(async_iterator)], yielding DemuxerChunk from new DemuxerInner::read_next_chunk()
  • Awaitable demux: Added demuxAsync(count?) to all demuxers (awaitable version of demux())
  • TypeScript updates: Declared DemuxerChunk and iterator signatures; enhanced JSDoc on demuxers in index.d.ts/dts-header.d.ts
  • Benchmarks: Replaced polling loops with await demuxer.demuxAsync()
  • Tests: Added async iterator and demuxAsync coverage for MP4/WebM/MKV, including audio+video paths
  • Build: Bumped napi feature to napi5 in Cargo.toml

Written by Cursor Bugbot for commit 4c18171. This will update automatically on new commits. Configure here.

Add `for await...of` loop support and `demuxAsync()` method to all
demuxers (Mp4Demuxer, WebMDemuxer, MkvDemuxer).

Changes:
- Implement AsyncGenerator trait via NAPI-RS for all demuxers
- Add `read_next_chunk()` method for single-packet reading
- Add `demuxAsync()` as awaitable alternative to sync `demux()`
- Add `DemuxerChunk` type with discriminated union pattern
- Update TypeScript types with declaration merging for Symbol.asyncIterator
- Add 10 tests covering async iteration and demuxAsync

The async iterator provides a cleaner streaming API:

  for await (const chunk of demuxer) {
    if (chunk.chunkType === 'video') {
      videoDecoder.decode(chunk.videoChunk!)
    }
  }

The demuxAsync() method eliminates the polling pattern:

  // Before
  while (demuxer.state !== 'ended') {
    demuxer.demux(100)
    await new Promise(r => setTimeout(r, 1))
  }

  // After
  await demuxer.demuxAsync()
Copilot AI review requested due to automatic review settings December 30, 2025 08:52

Copy link
Copy Markdown
Owner Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copilot AI 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.

Pull request overview

This PR adds async iteration support and a demuxAsync() method to all three demuxers (Mp4Demuxer, WebMDemuxer, MkvDemuxer), providing a more idiomatic JavaScript API for consuming demuxed chunks.

Key changes:

  • Implements AsyncGenerator trait for all demuxers to enable for await...of loops
  • Adds demuxAsync() method as an awaitable alternative to the existing synchronous demux() method
  • Introduces DemuxerChunk type with discriminated union pattern for yielding video/audio chunks

Reviewed changes

Copilot reviewed 9 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/webcodecs/demuxer_base.rs Adds DemuxerChunk struct and read_next_chunk() method for single-packet async iteration
src/webcodecs/mp4_demuxer.rs Implements AsyncGenerator trait and demuxAsync() method for Mp4Demuxer
src/webcodecs/webm_demuxer.rs Implements AsyncGenerator trait and demuxAsync() method for WebMDemuxer
src/webcodecs/mkv_demuxer.rs Implements AsyncGenerator trait and demuxAsync() method for MkvDemuxer
src/webcodecs/mod.rs Exports the new DemuxerChunk type
Cargo.toml Updates napi feature from napi4 to napi5 to support async iterators
dts-header.d.ts Adds TypeScript types for DemuxerChunk and async iterator interfaces using declaration merging
index.d.ts Generated TypeScript definitions including new async methods and documentation
test/demuxer.spec.ts Adds 10 comprehensive tests covering async iteration and demuxAsync for all demuxers
benchmark/bench-napi.ts Updates benchmark to use simplified demuxAsync() API
benchmark/bench-polyfill.ts Updates benchmark to use simplified demuxAsync() API

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/webcodecs/mp4_demuxer.rs
@Brooooooklyn Brooooooklyn merged commit 1a29354 into main Dec 30, 2025
40 checks passed
@Brooooooklyn Brooooooklyn deleted the 12-28-polish_muxer_api branch December 30, 2025 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants