Skip to content

fix(ios): keep small AAC packets alive while the AudioConverter reads them - #10

Merged
andreialecu merged 2 commits into
mainfrom
fix/ios-audio-small-packets
Nov 25, 2026
Merged

andreialecu merged 2 commits into
mainfrom
fix/ios-audio-small-packets

Conversation

@andreialecu

@andreialecu andreialecu commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

The iOS AudioDecoder put out nothing for AAC packets of 14 bytes or less, and reported no error. This is why "starts an AudioDecoder over after a flush (iOS)" has failed on every CI run on main.

Cause

provide() in ios/Audio.swift passed AudioToolbox a pointer from Data.withUnsafeMutableBytes, which is only valid inside the closure. A Data of 14 bytes or less keeps its bytes inline, so the pointer pointed at a temporary copy. The converter reads the packet after provide() returns, so it read whatever was left there. The silent packets in the test clip are 4 bytes each. On a local Mac the memory happened to still hold the packet, so the test passed there.

Fix

  • The packet and its AudioStreamPacketDescription now live in memory the decoder allocates. They stay valid until the next input call and are freed then, or in deinit. The encoder already did this.
  • pull() checks for a converter error before it treats 0 frames as the end of the stream. A failure is now reported instead of being swallowed.

How it was found

A diagnostic run on CI (run 45649412237, branch diag/audio-flush-ci, not for merging) showed:

  • all 24 packets between 3 s and 3.5 s are 4 bytes;
  • a new decoder, with no flush before it, put out 0 frames for them;
  • the tone decoded normally before and after a flush, and nothing arrived late.

So flush() was not the problem.

Simplification

flush() used to make a new AudioConverter instead of calling AudioConverterReset, because the samples after a flush were missing on CI. Those samples were the 4-byte silent packets, so the reset was never the cause. flush() now calls AudioConverterReset again, and the three fields that were kept only to rebuild the converter are gone.

Tests

  • New e2e test, "decodes AAC packets of a few bytes": a new decoder gets only the 4-byte silent packets and must put out silent frames with no error. It runs on iOS and Android. It has not been run on Android, and CI has no Android job.
  • CI on the fix alone (run 45650526117): the flush test and the new test pass. Three video export tests timed out after 30 s; they don't use the audio decoder.
  • CI with the reset (run 45652601652): 65 of 66 pass, the flush test included. The one failure is "handles the app going to the background", which also fails on some main runs.
  • yarn tsc passes.

… them

provide() passed AudioToolbox a pointer from Data.withUnsafeMutableBytes,
which is only valid inside the closure. A Data of 14 bytes or less is
stored inline, so for silent AAC packets (4 bytes) the converter read a
temporary copy and put out nothing, with no error. The packet and its
description now live in memory the decoder owns until the next call.

pull() also checks for a converter error before treating 0 frames as the
end, so such a failure is reported instead of swallowed.
A new converter on flush was a workaround for samples missing after a
flush on CI. The real cause was the small packets fixed in the previous
commit: the decode after the flush was of silent, 4-byte packets.
AudioConverterReset is enough.
@andreialecu
andreialecu merged commit 3a453d2 into main Nov 25, 2026
1 of 2 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 25, 2026
## [0.1.3](v0.1.2...v0.1.3) (2026-11-25)

### Bug Fixes

* **android:** put out a GOP that goes back in time after the one before it ([e0eb929](e0eb929))
* **example:** load the export's sound in Android release builds ([0cd1af8](0cd1af8))
* **ios:** decode AAC from its AudioSpecificConfig, ADTS, and restart the AudioDecoder on flush ([b78c863](b78c863)), closes [#3](#3) [#2](#2) [#10](#10) [#2](#2)
* **ios:** decode from a CRA or recovery point, drain the reorder buffer when a key frame goes back in time ([5c3e02a](5c3e02a))
* **ios:** forward bitrateMode, refuse quantizer, and tag the range the encoder writes ([05e9063](05e9063))
* **ios:** keep small AAC packets alive while the AudioConverter reads them ([d8f2742](d8f2742))
* **ios:** pin a wrapped surface's use count until the frame is closed ([99d3aa6](99d3aa6))
* **ios:** start an AudioDecoder over on flush with a new AudioConverter ([9cb6fd4](9cb6fd4))
@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