~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/hydraulophone

An unofficial YouTube Music app for iPhone, iPad, and Mac.

0
0
main
106 lines · 6.5 KB Raw History
# Native Opus playback feasibility

Status: implemented; native High and the media card confirmed on iPhone. Updated
2026-09-23. Native Opus is the default High backend. The previous WebKit
backend remains available only with the `-HydraulophoneWebKitOpus` diagnostic
launch argument.

## Why investigate

The authenticated InnerTube High path returns playable WebM/Opus media. On the
physical iPhone, Hydraulophone's `WKWebView` decodes it and plays reliably when
the host `AVAudioSession` is deactivated. The iOS 27 Lock Screen then shows
transport controls and progress without title or artwork. Standard AAC uses
`AVPlayer`, activates the host playback session, and does show metadata and
artwork. A nonmixable host session interrupted WebKit playback; a mixable one
did not gain system-primary ownership. WebKit's `MediaMetadata` assignment
also did not populate the card. See [native media evidence](../protocol/native-media.md).

InnerTube supplies player metadata and expiring media URLs; it does not decide
which iOS process owns the Lock Screen card. The pinned AuraMusic reference
([commit `185a77d`](https://github.com/TeamAuraMusic/AuraMusic/tree/185a77d31733047de06cbec619d40731755a0ec1),
[player utilities](https://github.com/TeamAuraMusic/AuraMusic/blob/185a77d31733047de06cbec619d40731755a0ec1/app/src/main/kotlin/com/auramusic/app/utils/YTPlayerUtils.kt))
is an Android Media3 playback implementation, so its InnerTube request shapes
transfer but its audio-session behavior does not. Metrolist's
[playback service](https://github.com/MetrolistGroup/Metrolist/blob/main/app/src/main/kotlin/com/metrolist/music/playback/MusicService.kt)
is another Android reference. Its [stream URL cache proposal](https://github.com/MetrolistGroup/Metrolist/pull/4132)
concerns expiry and recovery, not iOS media-session ownership.

## Native implementation

`WebMOpusReader` extracts individual Opus packets and timestamps from bounded
HTTP range reads. It is an original Foundation-only Swift implementation based
on the public Matroska/EBML and Opus format specifications, including Cues,
SeekHead, all three lacing modes, CodecDelay, SeekPreRoll, and signed
DiscardPadding. Startup stops at the first Cluster. Missing cluster indexes
are built on demand for seeking; media payloads are skipped while indexing.
The supported track profile is mapping-family-0 mono/stereo Opus. See the
[reader brief and specification references](../test-briefs/native-webm-reader.md).

[`alta/swift-opus`](https://github.com/alta/swift-opus/tree/6f3cb6bd3ffed1fe5f06d00a962d5c191a50daf8)
is pinned at `6f3cb6bd3ffed1fe5f06d00a962d5c191a50daf8` in `project.yml`.
Its `Copus` product builds libopus revision
`101a71e03bbf860aaafb7090a0e440675cb27660` from source. Hydraulophone uses that
public C decoder through `NativeOpusDecoder`, which validates packet sizes,
decodes at 48 kHz, applies OpusHead output gain, and produces noninterleaved
Float32 PCM. This is an app-owned `AVAudioEngine`/`AVAudioPlayerNode` pipeline;
it does not register a codec with `AVPlayer`. The package and codec notices
ship in `OPUS_LICENSES.txt` in the application bundle.

`NativeOpusByteSource` keeps eight 64 KiB chunks in an ephemeral in-memory LRU.
It reuses the existing media transport's credential-free, bounded-response,
redirect-checked URLSession. It requires HTTP 206 and matching Content-Range,
and never writes URLs or audio to disk. This packet reader uses smaller reads
than the AAC AVAssetResourceLoader's 512 KiB policy because the consumer is a
sequential demuxer, not AVPlayer's resource-loading callbacks.

`NativeOpusPCMSource` confines the decoder and demuxer to an actor. It decodes
preroll and trims samples before the seek target and at source padding. The
main-actor output engine schedules about six seconds of PCM, reports actual
render timing, freezes the timeline on underrun, and refills before resuming.
Cancelled seeks join earlier work before resetting decoder state. The existing
controller retains queue, repeat/shuffle, Last.fm progress, retry/stream refresh,
and remote-command policy. Standard AAC continues through AVPlayer.

Native High activates the same nonmixable `AVAudioSession` as Standard.
On iOS 27 it uses the existing NowPlaying MediaSession; older supported iOS
versions use the native MediaPlayer publisher. The InnerTube request, proof,
format selection, timestamp alignment, and expiring URL resolution are unchanged.

The native baseline prepared only the selected track. The subsequent
[gapless implementation](0005-gapless-playback.md) adds a bounded prepared
successor and adjacent PCM scheduling on the same output node.

## Recorded validation

- `just simulator` built and launched the native path on iPhone 18 Pro / iOS 27.
- `just simulator-smoke-high` passed with an authenticated 690.241-second
  stereo Opus stream: continuous playback, unbuffered 65%/90% seeks,
  pause/resume, and High-to-Standard-to-High switching, with no retry.
- Cold native readiness took 236 ms after stream resolution; verified progress
  arrived at 3.319 seconds from selection. Returning to High took 92 ms to
  become ready after resolution and 1.997 seconds total including a seek back
  to the previous position. These are simulator timings, not iPhone claims.
- `just lint` and `git diff --check` passed. Automated tests remain deferred
  at the owner's request; briefs are on disk.
- `just device-install` built, installed, and launched on the paired physical
  iPhone. The owner confirmed faster High startup and a working Lock Screen
  card. Device first-progress timings were 3.716 seconds cold, then 2.504,
  2.731, and 2.491 seconds; native readiness added 109–280 ms after resolution.
  Sustained background and route/interruption validation remain follow-ups.

## Remaining acceptance

1. When automated tests resume, verify stereo PCM against a known signal and
   fixture decoding across supported iOS versions.
2. Extract packets and timestamps from a small, sanitized WebM/Opus fixture,
   then decode them into PCM with correct channel count, sample rate, codec
   delay, and duration. Preserve source license and provenance if a demuxer is
   adapted or vendored.
3. On device, verify long background playback, headphone/remote commands,
   interruptions, route changes, rapid cancelled seeks, and automatic queue
   advancement. Startup and Lock Screen operation are confirmed; dedicated
   coverage for those additional cases remains on the test brief.

The native path has passed the live simulator check and physical media-card
confirmation. Gapless listening acceptance is tracked in the subsequent ADR.