fix(qconnect): WS-authoritative shuffle/repeat gating, button reflection, pause-to-iOS

Shuffle and Repeat now go to the CLOUD whenever connected (renderer OR controller),
gating on transport_connected like Tauri's v2_toggle_shuffle / v2_set_repeat_mode,
instead of the old peer-active gate that fell through to a LOCAL reshuffle when QBZ
was the active renderer — that did nothing visible AND risked a divergent local
order. QBZ sends only the seed + pivot; the cloud owns the order; the toggle reads
the cloud-authoritative queue.shuffle_mode. The local shuffle path is now reachable
only when NOT connected.

- shuffle/repeat button reflects the cloud queue flag (queue.shuffle_mode for the
  controller branch, core queue state via refresh_transport_modes for the renderer
  branch), not the per-renderer field the cloud never populates for a peer — fixes
  the iOS-reflects / WebPlayer-doesn't asymmetry.
- pause/play to a peer renderer: send a BARE SetPlayerState (playing_state only).
  iOS reports current_queue_item_id=null while playing, so resolving the qid landed
  on queue_item_id 0 — the protocol's "unknown" placeholder — and iOS could not
  anchor the pause, auto-resuming. A bare transport toggle is spec-sanctioned: the
  renderer pauses/resumes its own current item in place.
- skip_serializing_if on the SetPlayerState request structs so a None field is
  OMITTED, not serialized as null; and the mapper now treats a null current_queue_item
  as absent. Without this the bare command failed to encode and was never sent.
- log the outbound SetPlayerState payload for controller diagnostics.

cc
This commit is contained in:
vicrodh committed 2026-06-05 17:44:11 -06:00
1 parent 1072195740
commit b4c3bab91b
6 files changed
+204 -66

No files matched your search

+1 -1
View File
@@ -5231,7 +5231,7 @@ dependencies = [
"anyhow",
"base64",
"chrono",
"dirs",
"dirs 6.0.0",
"futures-util",
"log",
"md-5",
+9
View File
@@ -2138,6 +2138,13 @@ pub fn start_poll_loop(
.volume
.map(|v| (v as f32 / 100.0).clamp(0.0, 1.0))
.unwrap_or(0.5);
// Reflect the PEER's shuffle/repeat state on the bar buttons. As
// controller these were never pushed (only the local toggle paths
// set them), so the buttons looked dead even when the remote toggle
// worked. Pure UI reflection of the cloud's reported state — no
// local order is generated (WS-authoritative for shuffle order).
let shuffle_on = remote.shuffle_mode;
let repeat_mode = remote.repeat_mode;
let _ = weak.upgrade_in_event_loop(move |w| {
let np = w.global::<NowPlayingState>();
np.set_position_secs(position_secs as i32);
@@ -2150,6 +2157,8 @@ pub fn start_poll_loop(
np.set_remaining(remaining.into());
np.set_playing(playing);
np.set_volume(remote_volume);
np.set_shuffle(shuffle_on);
np.set_repeat_mode(repeat_mode);
});
// Reset the LOCAL edge trackers so when control returns to QBZ
// the end-of-track / gapless / transition logic re-detects from a
+33 -3
View File
@@ -241,6 +241,30 @@ impl SlintQconnectEventSink {
async fn refresh_local_ui(&self) {
crate::playback::refresh_now_playing_meta(&self.runtime, &self.window).await;
crate::playback::refresh_sidebar(true);
self.refresh_transport_modes().await;
}
/// Reflect the cloud-authoritative shuffle/repeat on the bar buttons from the
/// materialized CORE queue. The inbound SetShuffleMode / QueueUpdated update
/// the CORE queue (the cloud's order + flags), but never the UI button — this
/// pushes them. Lightweight (no card/art refresh) so it is safe to call on a
/// standalone shuffle/loop command without resetting the now-playing card.
/// Matches Tauri, whose button reads `queue.shuffle` (not a per-renderer
/// field the cloud never populates for a peer).
async fn refresh_transport_modes(&self) {
let qs = self.runtime.core().get_queue_state().await;
let shuffle_on = qs.shuffle;
let repeat_mode = match qs.repeat {
qbz_models::RepeatMode::Off => 0,
qbz_models::RepeatMode::All => 1,
qbz_models::RepeatMode::One => 2,
};
let _ = self.window.upgrade_in_event_loop(move |w| {
use slint::ComponentHandle;
let np = w.global::<crate::NowPlayingState>();
np.set_shuffle(shuffle_on);
np.set_repeat_mode(repeat_mode);
});
}
/// Wire the owning app after construction. Idempotent (OnceLock).
@@ -485,11 +509,17 @@ impl QconnectEventSink for SlintQconnectEventSink {
self.report_active_renderer_ready().await;
}
// A SetState changes the current track / play-state — reflect it
// in the QBZ now-playing card + queue cursor highlight. (Other
// commands — volume/mute/shuffle/loop — don't move the track, so
// skip the card/art refresh.)
// in the QBZ now-playing card + queue cursor highlight. A
// standalone SetShuffleMode / SetLoopMode does NOT move the track,
// so only refresh the lightweight shuffle/repeat button state (a
// full refresh would reset the now-playing card position/art).
if matches!(command, RendererCommand::SetState { .. }) {
self.refresh_local_ui().await;
} else if matches!(
command,
RendererCommand::SetShuffleMode { .. } | RendererCommand::SetLoopMode { .. }
) {
self.refresh_transport_modes().await;
}
}
QconnectAppEvent::RendererUnreachable { renderer_id } => {
+110 -62
View File
@@ -81,6 +81,13 @@ pub struct RemoteNowPlaying {
/// change against its last-seen value to refresh the bar/queue meta when
/// the peer advances a track on its own.
pub track_id: u64,
/// The peer's shuffle flag, so the controller bar's shuffle button reflects
/// the REMOTE state (the poll loop only updated this for local playback).
pub shuffle_mode: bool,
/// The peer's repeat mode, already mapped to the UI's `repeat-mode`
/// (0=off, 1=all, 2=one) from the QConnect wire loop_mode (1=off, 3=all,
/// 2=one), so the controller bar's repeat button reflects the REMOTE state.
pub repeat_mode: i32,
}
/// Process-wide QConnect service singleton (one per app, like the playback
@@ -1051,6 +1058,16 @@ impl SlintQconnectService {
}
}
// Dev diagnostic: dump the EXACT outbound payload for the player-state
// command (pause/resume/seek/skip) so a test can diff QBZ's command
// field-for-field against a working controller (e.g. WebPlayer pausing an
// iOS renderer). Gated to SetPlayerState so a volume drag never spams it.
// Answers "do we log who sends what when QBZ != renderer": yes, now.
if matches!(command_type, QueueCommandType::CtrlSrvrSetPlayerState) {
log::info!("[QConnect] --> outbound SetPlayerState payload={payload}");
dev_push_event(&self.window, format!("-> SetPlayerState {payload}"));
}
let command = app.build_queue_command(command_type, payload).await;
app.send_queue_command(command)
.await
@@ -1174,7 +1191,7 @@ impl SlintQconnectService {
/// snapshot (`playing_state == PLAYING`). Title/artist/art come from the
/// materialized local core queue, so only these three fields are needed here.
pub async fn remote_now_playing(&self) -> Option<RemoteNowPlaying> {
let (renderer, _queue, _session) =
let (renderer, queue, _session) =
self.effective_remote_renderer_snapshot().await.ok().flatten()?;
Some(RemoteNowPlaying {
position_ms: renderer.current_position_ms.unwrap_or(0),
@@ -1186,6 +1203,20 @@ impl SlintQconnectService {
.as_ref()
.map(|item| item.track_id)
.unwrap_or(0),
// The shuffle BUTTON reflects the cloud-authoritative QUEUE shuffle
// flag, NOT the per-renderer `renderer.shuffle_mode` — the cloud never
// populates the per-renderer shuffle field for a peer (it stays None),
// so reading it lit the button only by luck (worked for one peer type,
// not the other). `QConnectQueueState.shuffle_mode` is always present.
// Matches Tauri (queueStore `isShuffle = queueState.shuffle`).
shuffle_mode: queue.shuffle_mode,
// QConnect wire loop_mode (1=off, 3=all, 2=one) -> UI repeat-mode
// (0=off, 1=all, 2=one). Unknown / off -> 0.
repeat_mode: match renderer.loop_mode {
Some(3) => 1,
Some(2) => 2,
_ => 0,
},
})
}
@@ -1344,7 +1375,7 @@ impl SlintQconnectService {
/// `toggle_remote_renderer_playback_if_active`.
pub async fn toggle_remote_renderer_playback_if_active(&self) -> Result<bool, String> {
let remote_context = self.effective_remote_renderer_snapshot().await?;
let Some((renderer, queue, session)) = remote_context else {
let Some((renderer, _queue, session)) = remote_context else {
let reason = {
let guard = self.inner.lock().await;
let Some(runtime) = guard.runtime.as_ref() else {
@@ -1368,46 +1399,22 @@ impl SlintQconnectService {
Some(PLAYING_STATE_PLAYING) => PLAYING_STATE_PAUSED,
_ => PLAYING_STATE_PLAYING,
};
let current_position = renderer
.current_position_ms
.and_then(|value| i32::try_from(value).ok());
// Resolve the current queue item from the CORE-aligned cursor, NOT the
// renderer snapshot. `align_queue_cursor` keeps the core on the peer's
// ACTUAL track even when the peer reports a null `current_queue_item_id`
// (iOS does this continuously while playing). The renderer snapshot's
// `current_track`, by contrast, goes stale on a null-qid report
// (`build_effective_renderer_snapshot` only refreshes it when the qid is
// Some), and sending that stale qid made iOS REJECT the pause (the
// official Qobuz client CAN pause iOS, so this was our bug, not an iOS
// limitation — only remote VOLUME is genuinely refused by iOS). WebPlayer
// reports its qid, so the core and the snapshot agree there: no change.
// Falls back to omitting the item (a bare play/pause the renderer applies
// to whatever it is currently on) rather than ever sending a stale qid.
let current_queue_item = self
.runtime
.core()
.current_track()
.await
.and_then(|core_track| {
queue
.queue_items
.iter()
.find(|item| item.track_id == core_track.id)
.map(|item| item.queue_item_id)
})
.and_then(|queue_item_id| i32::try_from(queue_item_id).ok())
.map(|queue_item_id| QconnectSetPlayerStateQueueItemPayload {
queue_version: Some(QconnectQueueVersionPayload {
major: queue.version.major,
minor: queue.version.minor,
}),
id: Some(queue_item_id),
});
// BARE play/pause: send ONLY `playing_state` — no `current_position`, no
// `current_queue_item`. Evidence (controller-of-iOS log 2026-06-05,
// 23:07:59): iOS ACCEPTS the pause (it reports playing_state=PAUSED) but
// then AUTO-RESUMES to PLAYING within the same second, with NO play command
// from QBZ in between — even though the qid (0) and queue_version (12.4) QBZ
// sent were CORRECT (verified against iOS's own SetState). Attaching a
// (possibly-stale) `current_position` + a `current_queue_item` makes iOS
// treat the command as a SEEK / set-state and bounce back to playing; a
// pure transport toggle needs neither — the renderer pauses/resumes its own
// current item in place. WebPlayer-as-renderer (verified working) pauses
// fine on a bare command too, so this does not regress it. (Only remote
// VOLUME is genuinely refused by iOS.)
let payload = serde_json::to_value(QconnectSetPlayerStateRequest {
playing_state: Some(next_playing_state),
current_position,
current_queue_item,
current_position: None,
current_queue_item: None,
})
.map_err(|err| format!("serialize toggle_play request: {err}"))?;
@@ -1541,24 +1548,61 @@ impl SlintQconnectService {
))
}
/// Toggle shuffle on the active PEER renderer. Mirrors the Tauri
/// `toggle_shuffle_if_remote`.
/// True whenever a QConnect transport/session is established (renderer OR
/// controller). Mirrors the Tauri `status().transport_connected` gate that
/// `v2_toggle_shuffle` / `v2_set_repeat_mode` use: shuffle/repeat are
/// QUEUE-state operations the cloud OWNS, so they go to the cloud whenever
/// connected, regardless of who is the active renderer.
async fn transport_connected(&self) -> bool {
let app = {
let guard = self.inner.lock().await;
match guard.runtime.as_ref() {
Some(runtime) => Arc::clone(&runtime.app),
None => return false,
}
};
app.state_handle().lock().await.transport_connected
}
/// Toggle shuffle through the CLOUD whenever connected (renderer OR
/// controller) — exactly like Tauri `v2_toggle_shuffle`, which gates on
/// `transport_connected`, NOT on a peer being active.
///
/// WS-AUTHORITATIVE (load-bearing): QBZ sends ONLY `{shuffle_mode,
/// shuffle_seed, shuffle_pivot_queue_item_id}` — never a local order. The
/// cloud generates the order and echoes it; QBZ applies ONLY that echoed
/// order (inbound SetShuffleMode is flag-only + materialize applies the
/// cloud's `shuffled_track_indexes`). The local `playback::toggle_shuffle`
/// path (which DOES invent a local random order — the documented failure
/// mode) is reachable ONLY when NOT connected (this returns `Ok(false)` then,
/// so the caller runs it offline). The previous peer-only gate let that local
/// path run while connected-as-renderer, which both did nothing visible AND
/// risked the divergent-order bug.
pub async fn toggle_shuffle_if_remote(&self) -> Result<bool, String> {
let remote_context = self.effective_remote_renderer_snapshot().await?;
let Some((renderer, queue, session)) = remote_context else {
if !self.transport_connected().await {
// Offline: caller runs the local shuffle path.
return Ok(false);
}
// UN-gated snapshot: returns Some even when QBZ ITSELF is the active
// renderer (effective_remote_* returns None then). Mirrors Tauri
// queue_snapshot()/renderer_snapshot().
let Some((renderer, queue, session)) = self.effective_active_renderer_snapshot().await?
else {
// Connected but no active renderer yet — do NOT fall through to the
// local reshuffle (it would diverge from the cloud). Handled no-op.
return Ok(true);
};
let current_shuffle = renderer.shuffle_mode.unwrap_or(false);
let next_shuffle = !current_shuffle;
// Toggle from the cloud-authoritative QUEUE shuffle flag (matches Tauri
// `!queue.shuffle_mode`), not the per-renderer field which the cloud
// never populates for a peer.
let next_shuffle = !queue.shuffle_mode;
// The cloud REQUIRES a `shuffle_seed` when enabling shuffle ("shuffleSeed
// is undefined" error otherwise) and uses it to generate the authoritative
// order — WS-authoritative is preserved: QBZ supplies only a seed + pivot,
// never a local order, and applies only the cloud's echoed order. No `rand`
// crate here (unlike Tauri), so seed from the wall clock; mask to i32::MAX
// so it fits the wire `fixed32` as a positive value. The pivot keeps the
// currently-playing track at the front of the shuffled order. Mirrors the
// The cloud REQUIRES a `shuffle_seed` when enabling ("shuffleSeed is
// undefined" otherwise) and uses it to GENERATE the order — QBZ supplies
// only the seed + pivot, never an order. No `rand` crate here (unlike
// Tauri); seed from the wall clock, masked to i32::MAX for the wire
// `fixed32`. Pivot keeps the current track at the front. Mirrors the
// Tauri `apply_qconnect_shuffle_mode` payload.
let shuffle_seed: Option<u32> = next_shuffle.then(|| {
let nanos = std::time::SystemTime::now()
@@ -1582,22 +1626,26 @@ impl SlintQconnectService {
self.send_command(QueueCommandType::CtrlSrvrSetShuffleMode, payload)
.await?;
log::info!("[QConnect] toggle_shuffle handoff -> {next_shuffle}");
log::info!("[QConnect] shuffle -> {next_shuffle} (cloud, active={:?})", session.active_renderer_id);
dev_push_event(
&self.window,
format!("controller shuffle -> {next_shuffle} (active={:?})", session.active_renderer_id),
format!("shuffle -> {next_shuffle} (active={:?})", session.active_renderer_id),
);
Ok(true)
}
/// Cycle repeat mode on the active PEER renderer. QConnect loop wire values:
/// 1=off, 3=all, 2=one; cycle off->all->one->off. Mirrors the Tauri
/// `cycle_repeat_if_remote`.
/// Cycle repeat through the CLOUD whenever connected (renderer OR
/// controller) — like Tauri `v2_set_repeat_mode` (gate = transport_connected).
/// QConnect loop wire values: 1=off, 3=all, 2=one; cycle off->all->one->off.
/// Returns `Ok(false)` ONLY when NOT connected so the caller runs local.
pub async fn cycle_repeat_if_remote(&self) -> Result<bool, String> {
let remote_context = self.effective_remote_renderer_snapshot().await?;
let Some((renderer, _queue, session)) = remote_context else {
if !self.transport_connected().await {
return Ok(false);
}
let Some((renderer, _queue, session)) = self.effective_active_renderer_snapshot().await?
else {
return Ok(true);
};
let current_loop = renderer.loop_mode.unwrap_or(1);
@@ -1611,10 +1659,10 @@ impl SlintQconnectService {
self.send_command(QueueCommandType::CtrlSrvrSetLoopMode, payload)
.await?;
log::info!("[QConnect] cycle_repeat handoff -> {next_loop}");
log::info!("[QConnect] repeat -> {next_loop} (cloud, active={:?})", session.active_renderer_id);
dev_push_event(
&self.window,
format!("controller repeat -> {next_loop} (active={:?})", session.active_renderer_id),
format!("repeat -> {next_loop} (active={:?})", session.active_renderer_id),
);
Ok(true)
@@ -162,14 +162,25 @@ pub struct QconnectQueueVersionPayload {
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct QconnectSetPlayerStateQueueItemPayload {
#[serde(skip_serializing_if = "Option::is_none")]
pub queue_version: Option<QconnectQueueVersionPayload>,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<i32>,
}
// `skip_serializing_if` is LOAD-BEARING here: a `None` field must be OMITTED, not
// serialized as JSON `null`. The protocol mapper rejects a `current_queue_item`
// that is present-but-null ("must be an object") and aborts the whole send, so a
// bare play/pause toggle (which sends only `playing_state`) would never reach the
// wire without these. Each SetPlayerState field is independently optional per the
// protocol, so omitting them is the intended "do one or several" behavior.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct QconnectSetPlayerStateRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub playing_state: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub current_position: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub current_queue_item: Option<QconnectSetPlayerStateQueueItemPayload>,
}
+40
View File
@@ -882,6 +882,13 @@ fn parse_set_player_state_queue_item(
let nested = payload
.get("current_queue_item")
.or_else(|| payload.get("currentQueueItem"));
// A present-but-null `current_queue_item` means "no item" — a bare play/pause
// toggle. Treat it as ABSENT rather than hard-failing the whole command encode
// (the old behavior aborted the send before it reached the wire). Defense in
// depth alongside `skip_serializing_if` on the request struct.
if matches!(nested, Some(Value::Null)) {
return Ok(None);
}
let source = match nested {
Some(value) => value,
None => payload,
@@ -1096,6 +1103,39 @@ mod tests {
);
}
#[test]
fn encodes_bare_set_player_state_pause_without_queue_item() {
// A bare play/pause toggle sends ONLY playing_state — the renderer
// pauses/resumes its current item in place. Two shapes must both encode
// cleanly with NO current_queue_item: the key omitted (via
// skip_serializing_if), and present-but-null (defense in depth). The old
// mapper rejected the null shape ("must be an object") and aborted the
// whole send, which is why pause-to-an-iOS-renderer silently failed.
for payload in [
json!({ "playing_state": 3 }),
json!({ "playing_state": 3, "current_position": null, "current_queue_item": null }),
] {
let command = QueueCommand::new(
QueueCommandType::CtrlSrvrSetPlayerState,
"132db70b-d293-4576-bfd6-747c40ab764f",
QueueVersion::new(5, 3),
payload,
);
let encoded =
encode_queue_command_batch(&command).expect("bare pause must encode (not abort)");
let decoded = QConnectMessages::decode(encoded.as_slice()).expect("decode batch");
let player_state = decoded.messages[0]
.ctrl_srvr_set_player_state
.as_ref()
.expect("player state payload");
assert_eq!(player_state.playing_state, Some(3));
assert!(
player_state.current_queue_item.is_none(),
"bare pause must carry NO current_queue_item (qid 0 is the placeholder iOS rejects)"
);
}
}
#[test]
fn encodes_queue_load_tracks_with_android_like_fields() {
let command = QueueCommand::new(