Skip to content

fix(ios): decoder entry at CRA, reorder drain, AAC config and ADTS, bitrateMode, range and surface pinning - #4

Merged
andreialecu merged 12 commits into
mainfrom
fix/ios-top-items
Nov 25, 2026
Merged

andreialecu merged 12 commits into
mainfrom
fix/ios-top-items

Conversation

@andreialecu

@andreialecu andreialecu commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

This fixes 8 items from issue 2 (iOS problems that other projects ran into with VideoToolbox, AudioToolbox and IOSurface). Each fix comes with an e2e test that fails on main on the iOS Simulator and passes here.

Video decoder (ios/VideoDecoder.swift)

  • Item 1: decoding from a CRA or a recovery point. kVTVideoDecoderReferenceMissingErr (-17694) now drops the frame instead of failing the decoder. The first key sample after configure, flush or a backward jump gets ResetDecoderBeforeDecoding. For HEVC, RASL pictures after a CRA/BLA entry are skipped until the next IRAP.
  • Item 16: the reorder buffer. A key frame earlier than the frames before it drains the held frames first, so GOPs come out whole and in order. depth goes back to the SPS value on flush.
  • Item 19: the failed flag is now written under the reorder lock.

Audio (ios/Audio.swift, src/lib/audio.ts)

  • Item 2: ADTS. AAC with no description is read as ADTS on both platforms. JS builds the AudioSpecificConfig and strips the headers.
  • Item 3: the description is used. The decoder sets the ESDS magic cookie and takes the format from kAudioFormatProperty_FormatInfo, so HE-AAC and HE-AACv2 decode at the right rate and length.
  • Item 10: flush. AudioDecoder.flush() ends the stream and starts the next one on a new AudioConverter, made from the same formats and magic cookie. A reset converter put out no audio after the end of the stream on the CI Simulator.

Video encoder (src/lib/video.ts, ios/VideoEncoder.swift, ios/Surface.swift)

  • Item 4: bitrateMode. It is now passed to native. "quantizer" is reported as unsupported, so Mediabunny's Quality presets fall back to a bitrate.
  • Item 7: fullRange. decoderConfig.colorSpace.fullRange reports what the encoder wrote. RGB output frames from the decoder are tagged correctly.
  • Item 6: wrapped surfaces. A wrapped IOSurface keeps its use count raised until the frame is closed, so the producer's pool can't reuse it.

Tests

  • iOS Simulator: the 9 new tests fail on main and pass here. The full e2e suite passes 65/65. It also passes 65/65 on a physical iPhone (iOS 27.2.1), and the audio and key-frame tests pass again there after the flush change.
  • Android emulator: this branch's JS against an app built from main passes 65/65. The branch changes no Android code; it only adds an optional bitrateMode field to the shared config struct, which Android ignores.
    • One new test is relaxed on Android: "outputs a GOP that goes back in time". Android puts out the earlier GOP in decode order, as it already does on main, so the Android run only checks that every frame comes out. A separate fix for that is in progress.
  • yarn tsc, yarn lint and yarn test:unit pass.

Not covered by tests

  • Item 19: would need TSAN.
  • Item 6: needs a device with a pool producer.
  • The H.264 recovery-point path: has no test.
  • The HEVC open-GOP test returns early on the emulator, which has no HEVC decoder.

Nothing was tried on a physical iPhone.

…e encoder writes

VideoToolbox gets bitrateMode: "quantizer" is refused (no per-frame QP
outside the low-latency H.264 mode), so Mediabunny's Quality presets fall
back to a bitrate instead of VT's default rate; "constant" uses
ConstantBitRate on iOS 16+ when the encoder has it; "variable" caps each
second at 1.5x the bitrate with DataRateLimits.

The decoder config's colorSpace says limited range, as the encoders write,
instead of copying the frames' fullRange (Mediabunny wrote a full-range colr
over a video-range SPS). Decoded RGB frames are tagged full range.
A CVPixelBufferPool reuses an IOSurface once its use count is zero, whatever
its retain count, so a producer (camera, AVAssetWriter) could write a newer
image into a wrapped frame held before encode().
…er when a key frame goes back in time

Drop kVTVideoDecoderReferenceMissingErr frames instead of failing, set
ResetDecoderBeforeDecoding on a stream's first key frame, and skip HEVC
RASL pictures after a CRA/BLA entry until the next IRAP. A key frame
earlier than what was decoded since the last flush drains the held frames
first; flush() and that drain restore the stream's reorder depth. failed
is written under the reorder lock.
…the AudioDecoder on flush

- AudioDecoder wraps config.description in an esds magic cookie, takes the
  format from it (kAudioFormatProperty_FormatList: HE-AAC and HE-AAC v2 at
  their full rate and channels, 2048 frames a packet) and reports the
  converter's output rate and channels (#3).
- AAC without a description is ADTS: JS builds the AudioSpecificConfig and
  strips each chunk's header, on both platforms; mp4a.40.5/29 decode (#2).
- flush() ends the converter's stream and resets it, so what is decoded
  after it does not overlap what came before (#10).

Refs #2
… in time comes out

The emulator's c2 decoders put the earlier GOP out in decode order and hold
the later GOP's last frames until the end of the stream; iOS keeps the strict
order check.
@andreialecu andreialecu changed the title fix(ios): decoder entry at CRA, reorder drain, AAC config and ADTS, bitrateMode, range and surface pinning (#2) fix(ios): decoder entry at CRA, reorder drain, AAC config and ADTS, bitrateMode, range and surface pinning Nov 25, 2026
After the end of the stream, a reset AudioConverter put out no more audio on
the CI Simulator, so the decode after a flush gave no samples. flush() now
makes a new converter from the same formats and magic cookie.
…scratch

- Audio: makeConverter moves to file scope, and the AudioEncoder uses it
  instead of its own AudioConverterNew call.
- Audio: the AudioDecoder's pull() allocates its sample buffer once per
  call rather than once per pass of the drain loop.
- VideoDecoder: flush() and a key frame that goes back in time share one
  endStream() helper.
- VideoDecoder: the HEVC NAL length size comes from the format
  description VideoToolbox made of the hvcC record, rather than reading
  the record a second time.
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.1.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant