fix(scrobble): stamp started_at and time the threshold by audible position
The delayed scrobble used unix_now() when it fired (up to 240 s late on a long track) and slept on wall-clock, so a track paused two minutes in still scrobbled. Capture started_at on the track edge and wait on the player's own position while it is playing: pause holds the scrobble, a stop drops it, a newer track edge still self-cancels via the generation. Same rule qbzd's scrobble_engine already applies. cc
This commit is contained in:
1 file changed
+44
-10
@@ -769,9 +769,18 @@ pub fn meta_from_queue_track(track: &QueueTrack) -> ScrobbleMeta {
|
|||||||
|
|
||||||
/// Monotonic generation, bumped on every track change so a delayed scrobble
|
/// Monotonic generation, bumped on every track change so a delayed scrobble
|
||||||
/// timer that fires after the user skipped is dropped (the Svelte
|
/// timer that fires after the user skipped is dropped (the Svelte
|
||||||
/// `clearTimeout` equivalent). Like Tauri, pause/stop do NOT cancel it.
|
/// `clearTimeout` equivalent).
|
||||||
|
///
|
||||||
|
/// The wait itself is NOT wall-clock any more: it is timed by the player's
|
||||||
|
/// own position while it is playing, so a pause holds the scrobble and a
|
||||||
|
/// stop drops it (`qbzd/src/scrobble_engine.rs` — `Playing{started_at,..}`
|
||||||
|
/// + `PositionUpdated` — is the reference; the old `sleep(wait)` scrobbled a
|
||||||
|
/// track the user had paused two minutes in).
|
||||||
static SCROBBLE_GEN: AtomicU64 = AtomicU64::new(0);
|
static SCROBBLE_GEN: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
/// How often the delayed-scrobble task samples the player while waiting.
|
||||||
|
const SCROBBLE_TICK: Duration = Duration::from_secs(1);
|
||||||
|
|
||||||
/// Track-change entry point. Fires now-playing immediately for each enabled +
|
/// Track-change entry point. Fires now-playing immediately for each enabled +
|
||||||
/// authed service, then arms a delayed scrobble at `min(50% of duration,
|
/// authed service, then arms a delayed scrobble at `min(50% of duration,
|
||||||
/// 240s)`. No-op when no service is active (opt-in gate).
|
/// 240s)`. No-op when no service is active (opt-in gate).
|
||||||
@@ -785,6 +794,10 @@ pub fn on_track_changed(meta: ScrobbleMeta) {
|
|||||||
if !cfg.lastfm_active() && !cfg.listenbrainz_active() {
|
if !cfg.lastfm_active() && !cfg.listenbrainz_active() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Last.fm wants the time the track STARTED, captured here on the edge —
|
||||||
|
// never the time the threshold fired (a 240 s wait on a long track put
|
||||||
|
// every scrobble four minutes late).
|
||||||
|
let started_at = unix_now();
|
||||||
crate::spawn(async move {
|
crate::spawn(async move {
|
||||||
// Now-playing is never queued. It may use the independent network in
|
// Now-playing is never queued. It may use the independent network in
|
||||||
// manual-offline immediate mode or an opted-in logged-out session.
|
// manual-offline immediate mode or an opted-in logged-out session.
|
||||||
@@ -801,14 +814,32 @@ pub fn on_track_changed(meta: ScrobbleMeta) {
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if wait > 0 {
|
// Wait for the PLAYER to reach the threshold, not the clock: sample
|
||||||
tokio::time::sleep(Duration::from_secs(wait)).await;
|
// its position once a second and only count it while playing. A
|
||||||
|
// pause simply keeps waiting; a stop (no track loaded) ends the wait
|
||||||
|
// without a scrobble; a newer track edge self-cancels via the
|
||||||
|
// generation, as before. Seeks move the position and therefore the
|
||||||
|
// moment this fires — the same rule the daemon applies.
|
||||||
|
let rt = crate::app();
|
||||||
|
let mut ticker = tokio::time::interval(SCROBBLE_TICK);
|
||||||
|
loop {
|
||||||
|
ticker.tick().await;
|
||||||
|
if SCROBBLE_GEN.load(Ordering::SeqCst) != my_gen {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let ev = rt.core().player().get_playback_event();
|
||||||
|
if ev.track_id == 0 {
|
||||||
|
log::debug!(
|
||||||
|
"[qbz-qt] scrobble: stopped before the threshold, dropping '{}'",
|
||||||
|
meta.track
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ev.is_playing && ev.position >= wait {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Self-cancel if a newer track change superseded us.
|
send_scrobble(&meta, started_at).await;
|
||||||
if SCROBBLE_GEN.load(Ordering::SeqCst) != my_gen {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
send_scrobble(&meta).await;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -909,10 +940,13 @@ async fn send_now_playing(meta: &ScrobbleMeta, cfg: &ScrobblerSettings) {
|
|||||||
/// Fire, retain or drop the actual scrobble according to the restored offline
|
/// Fire, retain or drop the actual scrobble according to the restored offline
|
||||||
/// policy plus the logged-out opt-out. Settings are re-read in case the user
|
/// policy plus the logged-out opt-out. Settings are re-read in case the user
|
||||||
/// changed a policy or disconnected while the timer waited.
|
/// changed a policy or disconnected while the timer waited.
|
||||||
async fn send_scrobble(meta: &ScrobbleMeta) {
|
///
|
||||||
|
/// `started_at` is the unix time the track STARTED (captured on the track
|
||||||
|
/// edge), which is what both services define as the listen's timestamp.
|
||||||
|
async fn send_scrobble(meta: &ScrobbleMeta, started_at: i64) {
|
||||||
let cfg = scrobble_settings();
|
let cfg = scrobble_settings();
|
||||||
let album = meta.album.as_deref();
|
let album = meta.album.as_deref();
|
||||||
let timestamp = unix_now();
|
let timestamp = started_at;
|
||||||
let policy = current_scrobble_policy(&cfg).await;
|
let policy = current_scrobble_policy(&cfg).await;
|
||||||
|
|
||||||
if cfg.lastfm_active() {
|
if cfg.lastfm_active() {
|
||||||
|
|||||||
Reference in new issue
Block a user