Repository navigation
fix(mpris): Fix position tracking and seeking - #85
Conversation
- Fix relative seek calculation (was treating offset as absolute position) - Add SetPosition support for widgets using absolute positioning - Sync MPRIS position with player events via set_position() - Emit Seeked signal so external clients update their displays - Update song_progress_ms on seek so UI updates even when paused
There was a problem hiding this comment.
Pull request overview
Fixes broken MPRIS position reporting/seeking so external MPRIS clients (e.g., playerctl, widgets) correctly track playback position and can seek reliably, including absolute seeking via SetPosition.
Changes:
- Correct relative seek handling by computing new absolute position from current position + offset, and add support for absolute
SetPosition. - Synchronize MPRIS-reported position with native player
PositionChangedevents and emitSeekedon jumps. - Update in-app progress state on MPRIS seeks so the UI refreshes even while paused.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/mpris.rs | Adds SetPosition handling and introduces a Seeked command path to emit the MPRIS Seeked signal. |
| src/main.rs | Tracks current position via an atomic, recalculates relative seeks properly, updates UI/app state on seek, and syncs MPRIS position continuously. |
|
|
||
| // Calculate new position, clamping to prevent going negative | ||
| let new_position_ms = (current_ms + offset_ms).max(0) as u32; | ||
|
|
||
| // Seek the player | ||
| player.seek(new_position_ms); | ||
|
|
||
| // Update shared position immediately so UI reflects the change | ||
| shared_position.store(new_position_ms as u64, Ordering::Relaxed); | ||
|
|
||
| // Update app's song_progress_ms so UI updates even when paused | ||
| if let Ok(mut app_lock) = app.try_lock() { | ||
| app_lock.song_progress_ms = new_position_ms as u128; | ||
| } | ||
|
|
||
| // Emit Seeked signal so external clients know position jumped | ||
| mpris_manager.emit_seeked(new_position_ms as u64); | ||
| } | ||
| MprisEvent::SetPosition(position_micros) => { | ||
| // MPRIS SetPosition sends absolute position in microseconds | ||
| // Convert to milliseconds and seek directly | ||
| let new_position_ms = (position_micros / 1000).max(0) as u32; | ||
|
|
There was a problem hiding this comment.
new_position_ms is computed as i64 and then cast with as u32. If current_ms + offset_ms (or position_micros/1000) exceeds u32::MAX, the cast will wrap/truncate and can seek to an unintended position. Clamp to an upper bound before converting (e.g., clamp to u32::MAX or track duration if available), or keep the value as u64 until the final validated conversion to u32 for player.seek().
| MprisCommand::Position(position_ms) => { | ||
| // Silent position update (for regular playback progress) | ||
| player.set_position(Time::from_millis(position_ms as i64)); | ||
| } | ||
| MprisCommand::Seeked(position_ms) => { | ||
| // Update position AND emit Seeked signal so clients know to refresh | ||
| let time = Time::from_millis(position_ms as i64); | ||
| player.set_position(time); | ||
| if let Err(e) = player.seeked(time).await { |
There was a problem hiding this comment.
Time::from_millis(position_ms as i64) casts from u64 to i64 with as, which can wrap for large values. Even if current callers pass small positions, this is an API footgun (and Seeked uses the same pattern). Consider changing Position/Seeked to carry u32 milliseconds (matching track durations elsewhere), or use a checked conversion/clamp before building Time so invalid/oversized positions can’t become negative/incorrect.
# Summary Fixes #808. spotatui's MPRIS metadata had no `mpris:trackid`, so clients refused `SetPosition` (`playerctl position 60`: "Could not get track id to set position"). Relative seeks worked. - `track_object_path()`: `/org/spotatui/track/<16 hex>` from a hash of title, artists, album and length (`DefaultHasher::new()` has fixed keys, so it is stable). The same track gets the same id from every path that publishes it: the native player's track change and the driver's sync read the same `native_track_info` fields; a remote device or a local file only has one path. - `build_metadata()` sets it as `mpris:trackid` next to the existing fields. - `set_position_targets_current()`: a `SetPosition` with spotatui's id of *another* track is ignored, as the spec asks, so a seek sent right before a track change does not land in the next song. `NoTrack` and ids of a client's own still go through: since #85, widgets and KDE Connect seek without a real id, and I didn't want to break them. All in `src/infra/mpris.rs`, plus a CHANGELOG entry; no caller changes. macOS and Windows have no track-id concept and are untouched. # Testing - `cargo fmt --all -- --check`: clean. - `cargo clippy -- -D warnings` and with `--no-default-features` for `telemetry,tui`, `telemetry`, `telemetry,streaming` and the `gui` leg: clean. - `cargo test`: 2037 passed, 0 failed, 7 ignored. `cargo test --no-default-features --features telemetry,tui`: 1555 passed. `gui` leg: 1741 passed. - `tools/check_gates_ratchet.sh origin/main`: ok. - 8 new tests in `mpris.rs` (stable id, different fields give different ids, no field run-together, valid id outside `/org/mpris`, the metadata carries it, the three `SetPosition` cases). Without `.trackid(…)` in `build_metadata` the metadata test fails. - Manually on Linux (NixOS, Ghostty), with `playerctl` and `busctl --user call … SetPosition`, on the local player and on a Sonos room (#795): - The metadata carries `mpris:trackid` (`/org/spotatui/track/…`), and `playerctl position 60` jumps to 1:00 (on the room: `PUT /v1/me/player/seek?position_ms=60000`). - After `next`, a `SetPosition` with the previous track's id is ignored (no seek in the log), one with `NoTrack` and one with the current id both seek. - On `main` without the fix, `playerctl position 60` still fails with "Could not get track id to set position". # Additional notes - The id comes from the metadata rather than the URI because that is what every caller of `set_metadata` already passes. Two different tracks with identical title, artists, album and length would share an id; then a stale seek is simply not filtered, which is the old behavior. - Right after an end of track the native snapshot can fall back to the polled context for a moment, so the id may briefly change for the same song if the context reports a different album or length. - Not changed: a `SetPosition` outside `0..length` is still clamped rather than ignored. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * MPRIS track metadata now includes a track ID, enabling media clients to identify the track associated with a seek request. * Clients that do not provide a track ID remain supported. * **Bug Fixes** * Seek requests targeting a track that has changed are ignored, preventing playback from jumping to an unintended position. Other clients’ track IDs remain accepted. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Summary
Fix the broken MPRIS progression sync and make the modification of the song progress work.
Testing
Manual testing:
Additional notes
What was broken
What's fixed