Skip to content

linux-rust: add opt-in Linux/iPhone handoff prototype - #841

Open
vadika wants to merge 1 commit into
librepods-org:linux/rustfrom
vadika:feat/iphone-handoff
Open

vadika wants to merge 1 commit into
librepods-org:linux/rustfrom
vadika:feat/iphone-handoff

Conversation

@vadika

@vadika vadika commented Oct 14, 2026

Copy link
Copy Markdown

Linux/iPhone switching needs an explicit distinction between a new playback request and playback that was already running when the AirPods reconnected. Eager Linux reconnection after closing the case also left the iPhone playing through its speaker in hardware testing.

This adds an opt-in handoff prototype for the linux/rust branch. It establishes a playback baseline, requests Linux ownership on a new playback start only when wear/host/activity state permits it, and yields on playback stop or a remote ownership request. After an observed case disconnect, Linux waits for a new playback request before reconnecting, allowing the iPhone to connect first. Normal GUI startup remains the default.

Changes

  • Add interactive --handoff-probe <AIRPODS_MAC> and unattended --handoff-probe <AIRPODS_MAC> --handoff-auto modes. The unattended mode ignores stdin EOF and handles SIGTERM; neither mode starts the GUI or the normal playback listener.
  • Track ownership acknowledgements and timeouts, pause MPRIS players when yielding ownership requested by this controller, and reject takeover with missing state, unworn buds, or reported remote media/call/unknown activity.
  • Reconnect with a new control session, require fresh host/wear/audio-source notifications, clear stale state on EOF/error/disconnect, and discard playback samples from before the new baseline. Retry delays back off to eight seconds.
  • Correct the connected-device count offset and truncated-packet length checks; preserve unknown audio activity as unknown instead of treating it as idle.
  • Reuse the existing MPRIS polling and AACP packet helpers; add no dependencies.

Trying it

With already paired AirPods and a working Apple-compatible BlueZ setup, stop any other LibrePods instance and run from linux-rust:

cargo run --locked -- --handoff-probe <AIRPODS_MAC>
# Enter `auto` to enable switching, or use `status`, `take`, `release`, and `quit`.

# Unattended mode, suitable for running under a user service:
cargo run --locked -- --handoff-probe <AIRPODS_MAC> --handoff-auto

The first playback sample is a baseline: pause and start playback again to request a handoff. Machine-specific service/autostart files and Bluetooth configuration are not included.

Validation

  • cargo test --locked: seven tests pass, covering parsing/truncation, unknown activity, takeover guards, fresh-state readiness, disconnect invalidation, and case recovery cancellation.
  • cargo build --locked, rustfmt --edition 2024 --check src/handoff_probe.rs, and git diff --check pass. The build retains existing warnings.
  • Interactive hardware testing with AirPods Pro 3, an iPhone 17 Pro running iOS 27, and Linux with BlueZ 5.87 / PipeWire 1.6.8: confirmed switching in both directions with playback pausing, and audio returning to Linux after an iPhone call ended.
  • After a case cycle, confirmed automatic iPhone playback followed by Linux reconnection on a new Brave playback request, then switching back to iPhone.
  • Earlier interactive protocol stress: 30 silent take/release cycles, all 60 acknowledgements received. These were protocol checks, not 30 audible end-to-end handoffs.
  • Unattended mode was installed locally for continued testing; clean service stop/start was verified.

Remaining work — draft

This is an experimental controller, not seamless Apple-device parity. Linux can play through its speakers briefly while Bluetooth reconnects after a case cycle. Automatic media resume is not implemented. MPRIS state is aggregated across players and polled every 500 ms; it does not identify individual user intent. Takeover during an active iPhone call, local conferencing, and suspend/resume still need hardware validation. Continuous-use logs also show repeated control-connection failure retries; unattended recovery needs more investigation.

Related open PRs

mhijazi1 added a commit to mhijazi1/librepods that referenced this pull request Nov 8, 2026
Import upstream handoff prototype (librepods-org#841) as an opt-in mode

This cherry-picks upstream librepods-org/librepods PR librepods-org#841 (commit 7e125c8, by vadik likholetov) without changing its semantics:
- a new `linux-rust/src/handoff_probe.rs`, run with `--handoff-probe <MAC> [--handoff-auto]`, which returns early right after `Args::parse()`;
- AACP hardening in aacp.rs: `disconnect`, `clear_connection`, `AudioSourceType::Unknown`, the connected-devices count read at payload[4], and short-packet guards;
- `check_if_playing` in media_controller.rs becomes `pub(crate)`.

Without the flag, startup behaves as before. The aacp.rs fixes apply to the default path too (listed in the PR). It also adds `cli_` parse tests. No Cargo changes.

Validated at 1c76153: all 4 criteria pass. cargo test --locked shows 23 passed, 0 failed, and nix flake check passes. Approved by all 3 reviewers.

Closes #6

Swarm-Agent: swarm-planner
Swarm-Agent-Id: 6752788e-b893-4289-968d-4ed5a8d7c8db
Swarm-Model: claude-opus-5.5
Swarm-Session: 48ba9be7-4f4c-45e7-b38d-d7ae0cbf283c
Swarm-Ticket: #6
mhijazi1 added a commit to mhijazi1/librepods that referenced this pull request Nov 8, 2026
Handoff state machine: pure core with event-stream tests (#8)

- New pure, synchronous core `linux-rust/src/handoff.rs` (`Handoff::handle(now, Input) -> Vec<Command>`). It owns the probe's decision state: pending/requested take, playback baseline, case recovery and session liveness. 18 event-stream tests assert the exact command lists.
- `decide_takeover(TakeoverInputs)` is the only takeover policy. The librepods-org#841 guards (case recovery, live session, two hosts, idle or local audio) moved into it through `trigger` + `handoff: Option<HandoffGuards>`. The legacy `playback_listener_loop` passes `handoff: None` and decides as before.
- `handoff_probe::run` is now a thin I/O shell that feeds AACP events, playback samples, ticks, reconnect results and REPL commands into the core and runs the commands it returns.
- Fixes the stop that was lost during a pending take, including after a reconnect take. Also handles the mirror case (a restart during a pending release) with a pause when the retake is blocked, and session loss between ticks.

Validated at 5a2a28d: c1–c9 pass. The check command compiled this worktree (98 tests, 76 → 98) and `nix flake check` passes. Hardware validation by mo is pending and is not acceptance.

swarm-dissent: swarm-review-gpt's major finding (a queued initialization ownership report [0] is replayed after the reconnect take, pauses playback and clears `requested_take`, so the reconnect-stop release is lost on the real shell event stream) is accepted as valid. It is tracked as #43 and does not block #8. The old probe already retained these reports. The ticket's c5 sequence passes. The fix needs the shell to settle the init queue before the reconnect take, which is a separate change to the probe's I/O ordering. claude's minor finding (REPL `take` during case recovery prints BLOCKED, not WAIT) is #42.

Closes #8

Swarm-Agent: swarm-planner
Swarm-Agent-Id: a780d1c9-fa2b-4252-914e-2f0ba2aa8ad3
Swarm-Model: claude-opus-5.5
Swarm-Session: 0c3275c6-fe17-4709-a2ce-11262b126bed
Swarm-Ticket: #8
mhijazi1 added a commit to mhijazi1/librepods that referenced this pull request Nov 8, 2026
…#43) (#47)

Handoff: ignore stale init ownership report during the reconnect take (#43)

This is a core-only fix in `linux-rust/src/handoff.rs`. A take sent on reconnect now sets `Handoff::reconnect_take_pending`. While that marker is set, an `OwnsConnection=0` report updates `owner` but neither pauses playback nor drops `requested_take`, so a stop during the take still releases. The marker clears at the take's `[1]` ACK, at the ACK timeout, on `Disconnected` and on any new `request()`. Non-reconnect takes keep upstream librepods-org#841 semantics.

New tests: `reconnect_take_ignores_stale_init_ownership` and `genuine_loss_after_reconnect_take_pauses`. Results: 100 passed, 0 failed (98 before), and `nix flake check --no-build` passed. Validated at 4f583bb with a fresh compile of this worktree. Reviews: 3/3 approve.

Follow-up: #49 (a genuine `[0]` inside the reconnect-take window is also ignored).

Hardware validation by mo; not acceptance: run `--handoff-probe <MAC> --handoff-auto`, put the buds in the case, take them out, then play on Linux. Playback must not pause after RECONNECTED, and stopping it must release ownership.

Closes #43

Swarm-Agent: swarm-planner
Swarm-Agent-Id: 5e40d3d9-6ccb-412d-a08b-d089b6843ead
Swarm-Model: claude-opus-5.5
Swarm-Session: 4568eb3b-4ec3-4325-9d1b-9437602c7306
Swarm-Ticket: #43
mhijazi1 added a commit to mhijazi1/librepods that referenced this pull request Nov 8, 2026
An OwnsConnection [0] that acknowledges no release of Linux's, while
Linux holds or is taking the AirPods, is now one clean yield
(`yield_take`): pause in automatic mode, forget the take and any pending
take request (no later TIMEOUT or reconcile), and print a YIELD line.

The [0] that acknowledges Linux's own playback-stop or remote-request
release no longer pauses a second time; only the deferred pause of a
blocked retake remains. User and quit releases keep upstream librepods-org#841's
pause. During the reconnect take a [0] still only updates the owner
(#43).

Five existing release-ACK assertions move from [PauseMedia] to [], each
with a #39 comment. New tests: yield_on_owns_connection_zero,
yield_on_set_ownership_false, no_double_yield_remote_then_ack,
no_double_yield_loss_then_remote,
owns_connection_zero_after_own_release_is_noop.

Swarm-Agent: swarm-coder-strong
Swarm-Agent-Id: 21d49ab5-0955-458b-b724-81f4e414ccd9
Swarm-Model: claude-opus-5.5
Swarm-Session: a6d45f10-769e-4a41-bbd5-c30aff15887d
Swarm-Ticket: #39
mhijazi1 added a commit to mhijazi1/librepods that referenced this pull request Nov 8, 2026
handoff: yield once on OwnsConnection=0 (#39)

In the pure handoff core (`handoff.rs`, `Handoff::ownership`), an OwnsConnection [0] that doesn't acknowledge one of Linux's own releases now counts as a genuine loss while Linux holds or is taking the AirPods. The new `yield_take` handles it: it pauses once in automatic mode (manual mode pauses nothing), forgets the take and its pending request, so no TIMEOUT or reconcile fires later, and prints a `YIELD` line.

- The [0] that acknowledges a playback-stop release or a remote-request release no longer pauses a second time.
- The [0] that acknowledges a user or quit release keeps upstream librepods-org#841's pause.
- The deferred pause of a blocked retake is kept.
- While `reconnect_take_pending` is set, a [0] still only updates `owner` (DECISIONS #43).
- No Command or Input variants changed. Only `linux-rust/src/handoff.rs` changed.

Tests: 5 new (`yield_on_owns_connection_zero`, `yield_on_set_ownership_false`, `no_double_yield_remote_then_ack`, `no_double_yield_loss_then_remote`, `owns_connection_zero_after_own_release_is_noop`). Five existing release-ACK assertions changed from [PauseMedia] to [], each with a #39 comment. 136 passed, `nix flake check --no-build` passes, and all three reviewers approve.

Hardware validation by mo; not acceptance: run `--handoff-probe <MAC> --handoff-auto` with Peaches playing, then press play on the Mac. Expect one YIELD line and Peaches to pause.

Closes #39

Swarm-Agent: swarm-planner
Swarm-Agent-Id: b2396f1c-8014-43f7-bf5d-950152807316
Swarm-Model: claude-opus-5.5
Swarm-Session: a6d45f10-769e-4a41-bbd5-c30aff15887d
Swarm-Ticket: #39
mhijazi1 added a commit to mhijazi1/librepods that referenced this pull request Nov 8, 2026
Handoff: allow takeover with three or more connected hosts.

The librepods-org#841 host guard in `handoff_block` (media_controller.rs) now takes when the AirPods report this machine plus at least one remote host (2, 3, 4... hosts). Before, it needed exactly two. It still skips when the host list is empty, when Linux is the only host, and when Linux is not in the list. `TakeoverSkip::NotTwoHosts` is renamed `NoRemoteHost`. The handoff core and the probe's `take_allowed` go through the same policy, and the `Input::Take` BLOCKED line now reads "BLOCKED take needs Linux plus a remote host, ...".

Validation at 6f40bd8: all 5 criteria pass. New tests: `takeover_host_count_table`, `takes_with_three_or_four_hosts`, `no_take_when_only_linux_hosts` and `take_allowed_with_three_hosts`. A fresh crate compile of the check command gives 298 passed (base 294 + 4), and `nix flake check --no-build` passes. Reviews: 3/3 approve.

Hardware validation by mo; not acceptance: with the iPhone and the Mac connected, Peaches takes the AirPods when its playback starts.

Closes #38

Swarm-Agent: swarm-planner
Swarm-Agent-Id: 06499492-2bce-40b5-a260-d16a652becf3
Swarm-Model: claude-opus-5.5
Swarm-Session: 07304332-7b83-49a1-91c4-d04205e65028
Swarm-Ticket: #38
@vadika
vadika marked this pull request as ready for review November 28, 2026 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant