librepods icon indicating copy to clipboard operation
librepods copied to clipboard

Head-tracked spatial audio: the head-tracking data plus two ways to render it (one no-root, one root)

Open IvanChanPing opened this issue 3 months ago • 5 comments

Splitting this out on its own because it's really two problems: getting the head-orientation data (already solved), and doing something with it (the hard part).

Getting the data is done. The buds stream head orientation over the same AACP channel LibrePods already talks to — opcode 0x17. A start packet turns the sensor on, and the sensor frames carry three int16 orientation values you can turn into pitch/yaw:

  • pitch = ((o2 + o3) / 2 / 32000) * 180
  • yaw = ((o2 - o3) / 2 / 32000) * 180 with a per-frame axis mapping. That's enough to build a live quaternion.

Turning that into audio that actually rotates when you turn your head is where it splits into two routes:

Route A — feed Android's system spatializer (needs root). Android's built-in Spatializer (the thing behind Dolby Atmos head tracking) only accepts head pose from a TYPE_HEAD_TRACKER sensor wired at the HAL level. A normal app can ask whether a head tracker exists (isHeadTrackerAvailable()), but it can't register one or push pose into it. The realistic way to feed it without OEM firmware is a rooted /dev/uhid bridge that presents the AirPods as a HID head-tracker: the kernel exposes a hidraw node, the dynamic sensor sub-HAL turns it into a TYPE_HEAD_TRACKER, and SpatializerHelper matches it to the current audio route. This only works on phones whose OEM spatializer effect already supports head tracking (you can check with dumpsys audio / getSpatializer), and it needs root for the uhid device.

Route B — our own renderer (no root, but app-scoped). Without root, LibrePods can spatialize audio itself: take the quaternion and run the audio through a binaural/HRTF renderer (Resonance Audio or libspatialaudio via Oboe/AAudio) so the soundfield stays put as you turn your head. The limitation is that it only spatializes audio the app itself plays — it can't rotate system-wide or other apps' audio, because that's exactly the job the system spatializer (Route A) does.

So there's a genuine no-root option (Route B) with a clear limitation, and a system-wide option (Route A) that needs root and a supportive phone. Wanted to lay both out so it's clear which is which.

AOSP references for Route A: frameworks/base/media/java/android/media/Spatializer.java, .../server/audio/SpatializerHelper.java, frameworks/av/services/audiopolicy/service/Spatializer.cpp and SpatializerPoseController.cpp, plus the kernel uhid docs.

IvanChanPing avatar Aug 11 '26 15:08 IvanChanPing

Small follow-up after digging into the RTBuddy work: head tracking, heart rate and sound-pressure-level all ride the same stream — BuddyCommand (0x0017) → RTBuddy SensorDataWX, one service per sensor (head motion = DEVMOTION6/16, HEARTRATE = 19, SPL = 17), each frame validated by descriptor 0x00100000. Two notes for whoever builds the head-tracking side:

  • Parse it protobuf-relative, not at fixed offsets. Locate the SensorDataWX payload first (motion service, payload at frame offset ~23 in a real capture), then read o1/o2/o3 at payload[20]/[22]/[24] and h/v-accel at payload[28]/[30]. The absolute offsets that float around (43/45/47) are header-relative and break if the framing shifts — tomppi's head-tracking-live-indicator branch already switched to this protobuf-relative approach.
  • Sound-pressure-level (service 17) comes down the same stream for free — worth exposing alongside head tracking once something is already parsing SensorDataWX.

IvanChanPing avatar Aug 11 '26 15:08 IvanChanPing

Route A works without root, using Shizuku (on my OnePlus)

I implemented the system route from #719 and #722 and it works on a OnePlus 11 (ColorOS 16 / Android 16) with AirPods Pro, without root. A Shizuku UserService (shell identity, Binder IPC) creates the /dev/uhid device. A raw socket from the app to a shell process is blocked by SELinux on this phone, which is why it uses Shizuku's Binder path.

What I found along the way, which may help the spec in #722:

  • The sensor only registered as a real TYPE_HEAD_TRACKER when SENSOR_DESCRIPTION was exactly #AndroidHeadTracker#1.0 (23 bytes) with the Bluetooth persistent ID from the AOSP spec. The generic #ANDROID#… tag only produced a generic custom sensor.
  • This ROM's HAL never sent SET_REPORT when enabling the sensor. Gating output on it meant no data ever flowed, so I send unconditionally.
  • The AACP DEVMOTION6 payload is a unit quaternion (x/y/z as int16/32768 at payload offsets 20/22/24, w implied), not angles. A linear pitch/yaw formula can't give a correct 3-axis rotation vector. The gyro at [26:32] also matches the quaternion's rate of change (R² 0.999 on a 65 s capture), so I send real angular velocity, which the platform uses for prediction.
  • The descriptor must advertise the spec's 10–100 ms interval range. A copy with a fixed 40 ms interval caps audioserver at 25 Hz. With the spec range, audioserver subscribes at 10 ms, and the AirPods streamed unique samples at 100 Hz with exact 10 ms timestamps.
  • The Spatializer ends up in mActualHeadTrackingMode: RELATIVE_WORLD, and head turns, nods and body rotation audibly anchor the sound.

Limits: tested on OnePlus 11 on OxygenOS 16 (no root) and pair of AirPods Pro 1 and 3 only. It needs an OEM spatializer with head tracking (this one has Dolby's). Yaw can drift slowly, and the platform's own auto-recenter handles most of it.

https://github.com/DroidThug/librepods/blob/shizuku-head-tracked-spatial-audio/HEADTRACKING_FORK_NOTES.md

Code: https://github.com/DroidThug/librepods/tree/shizuku-head-tracked-spatial-audio

System Sound effects: head tracking on, Apple Music Atmos on AirPods Pro Diagnostics screen: virtual head-tracker sensor running at about 94 Hz

DroidThug avatar Nov 05 '26 19:11 DroidThug

@DroidThug @xygn Awesome work!

I've added support of head tracking for my Apple AirPods Max 2:

https://github.com/FluorescentHallucinogen/librepods/tree/shizuku-head-tracked-spatial-audio-airpods-max-2-support

It works on Google Pixel 7 Pro with the latest stable Android without root like a charm!

@DroidThug @xygn Would you accept my pull request if I submit it to your branch https://github.com/DroidThug/librepods/tree/shizuku-head-tracked-spatial-audio?

I've also prepared a sound sample for tests (play it using any Android player based on system's ExoPlayer):

5.1_center_only_sample.wav

@kavishdevar Please take another look at this idea!

I understand that writing a spatializer is outside the scope of the project, but head tracking is already within its scope (at least for head gestures). So why not add head-tracked spatial audio, especially since all the necessary building blocks are already in place?

The spatializer is already built into Android itself. And the idea of using a Shizuku service is pretty cool! Shizuku is available on Google Play, and no root access is required.

After all, isn't LibrePods' main mission to provide Android users with an experience that's just as good as iOS?

We could prepare a pull request for this. ;)

What do you think?

FluorescentHallucinogen avatar Nov 12 '26 13:11 FluorescentHallucinogen

Please take another look at this idea!

I understand that writing a spatializer is outside the scope of the project, but head tracking is already within its scope (at least for head gestures). So why not add head-tracked spatial audio, especially since all the necessary building blocks are already in place?

The spatializer is already built into Android itself. And the idea of using a Shizuku service is pretty cool! Shizuku is available on Google Play, and no root access is required.

After all, isn't LibrePods' main mission to provide Android users with an experience that's just as good as iOS?

I totally want to add this, I just closed this issue for being slop.

Although I can't guarantee I'll be straight up merging a PR for this. v2 is still very much a WIP, and there's already a bounty (and a few submissions? for it) for a shell/privileged service. If I do use any of your code, I'll definitely be adding co-authors.

kavishdevar avatar Nov 12 '26 13:11 kavishdevar

@kavishdevar Let's reopen this issue?

FluorescentHallucinogen avatar Nov 12 '26 14:11 FluorescentHallucinogen