fix(android): a song on repeat is a play per listen, and a late start reply can't make a quick skip record twice (#8071, #8073)
The play reporter keyed a play on the current track id, which repeat-one and the same song twice in a row never change, so any number of listens became one play closed at the last loop's position. PlayerController now counts media-item transitions (Media3 reports a repeat as one) as PlayerUiState.listenSeq, and the reporter keys a play on (track, listen). A play_started reply cleared the in-flight flag whichever play it was for. After a quick skip, song A's late reply cleared the flag song B's start had set, so skipping B before its reply recorded it offline as well as live. Each start now carries a token, and only its own play's reply adopts the id or clears the flag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -28,7 +28,7 @@ import javax.inject.Singleton
|
||||
* scrobbles would not fire, and system-playlist rotation would not
|
||||
* advance.
|
||||
*
|
||||
* State machine over (current track id, playing) observed against
|
||||
* State machine over (current listen, playing) observed against
|
||||
* [PlayerController.uiState]. The client always calls `playEnded`
|
||||
* with the actual position played to; the server applies its
|
||||
* configurable skip rule (`skip_max_completion_ratio` AND
|
||||
@@ -81,55 +81,67 @@ class PlayEventsReporter @Inject constructor(
|
||||
// orphan on the next play_started instead.
|
||||
private var startInFlight: Boolean = false
|
||||
|
||||
// The listen the tracked play belongs to (PlayerUiState.listenSeq), and
|
||||
// the one last seen. A repeat keeps the track id but moves the listen,
|
||||
// so a song on repeat is a play per time through.
|
||||
private var curListenSeq: Long = -1
|
||||
private var prevTrackId: String? = null
|
||||
private var prevListenSeq: Long = -1
|
||||
|
||||
// Identifies the tracked play to its own play_started call. A reply that
|
||||
// lands after the play it was for has closed must neither adopt its id
|
||||
// nor clear the in-flight flag the NEXT play's start has set: clearing it
|
||||
// would make a quick skip of that next play record it offline as well as
|
||||
// live.
|
||||
private var curPlayToken: Long = 0
|
||||
|
||||
init {
|
||||
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
|
||||
scope.launch {
|
||||
playerController.uiState.collect { state ->
|
||||
evaluate(
|
||||
trackId = state.currentTrack?.id,
|
||||
playing = state.isPlaying,
|
||||
positionMs = state.positionMs,
|
||||
durationMs = state.durationMs,
|
||||
source = state.currentSource,
|
||||
)
|
||||
}
|
||||
playerController.uiState.collect { state -> evaluate(state) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun evaluate(
|
||||
trackId: String?,
|
||||
playing: Boolean,
|
||||
positionMs: Long,
|
||||
durationMs: Long,
|
||||
source: String?,
|
||||
) {
|
||||
// Track-change → close the prior tracked play.
|
||||
if (trackId != prevTrackId && curTrackId != null) {
|
||||
private fun evaluate(state: PlayerUiState) {
|
||||
val trackId = state.currentTrack?.id
|
||||
val listenSeq = state.listenSeq
|
||||
val playing = state.isPlaying
|
||||
val durationMs = state.durationMs
|
||||
val listenChanged = trackId != prevTrackId || listenSeq != prevListenSeq
|
||||
// Listen change (a new track, or the same one again) → close the
|
||||
// prior tracked play.
|
||||
if (listenChanged && curTrackId != null) {
|
||||
closeCurrent()
|
||||
}
|
||||
|
||||
// Entered playing for a new track → begin tracking it.
|
||||
if (trackId != null && playing && curTrackId != trackId) {
|
||||
beginTrack(trackId = trackId, source = source, durationMs = durationMs)
|
||||
// Entered playing for a new listen → begin tracking it.
|
||||
val tracked = curTrackId == trackId && curListenSeq == listenSeq
|
||||
if (trackId != null && playing && !tracked) {
|
||||
beginTrack(
|
||||
trackId = trackId,
|
||||
listenSeq = listenSeq,
|
||||
source = state.currentSource,
|
||||
durationMs = durationMs,
|
||||
)
|
||||
}
|
||||
|
||||
// Advance the tracked play's progress. The gate `trackId ==
|
||||
// curTrackId` keeps a finishing track's last-known position
|
||||
// intact across the moment the listener fires for a track
|
||||
// change (where currentTrack is briefly the new one).
|
||||
if (curTrackId != null && trackId == curTrackId) {
|
||||
curLastPositionMs = positionMs
|
||||
// Advance the tracked play's progress. The gate on the tracked
|
||||
// listen keeps a finishing track's last-known position intact
|
||||
// across the moment the listener fires for a track change or a
|
||||
// repeat (where the position has already gone back to 0).
|
||||
if (curTrackId != null && trackId == curTrackId && listenSeq == curListenSeq) {
|
||||
curLastPositionMs = state.positionMs
|
||||
if (durationMs > 0) curDurationMs = durationMs
|
||||
curPlaying = playing
|
||||
}
|
||||
|
||||
prevTrackId = trackId
|
||||
prevListenSeq = listenSeq
|
||||
}
|
||||
|
||||
private fun beginTrack(trackId: String, source: String?, durationMs: Long) {
|
||||
private fun beginTrack(trackId: String, listenSeq: Long, source: String?, durationMs: Long) {
|
||||
curTrackId = trackId
|
||||
curListenSeq = listenSeq
|
||||
curStartedAt = Clock.System.now()
|
||||
curSource = source
|
||||
curLastPositionMs = 0
|
||||
@@ -137,10 +149,11 @@ class PlayEventsReporter @Inject constructor(
|
||||
curPlaying = true
|
||||
openPlayEventId = null
|
||||
startInFlight = true
|
||||
startLive(trackId = trackId, source = source)
|
||||
curPlayToken++
|
||||
startLive(trackId = trackId, source = source, token = curPlayToken)
|
||||
}
|
||||
|
||||
private fun startLive(trackId: String, source: String?) {
|
||||
private fun startLive(trackId: String, source: String?, token: Long) {
|
||||
val cid = resolveClientId()
|
||||
if (cid == null) {
|
||||
startInFlight = false
|
||||
@@ -156,7 +169,7 @@ class PlayEventsReporter @Inject constructor(
|
||||
deviceClass = DEVICE_CLASS,
|
||||
),
|
||||
)
|
||||
if (curTrackId == trackId) {
|
||||
if (curPlayToken == token) {
|
||||
openPlayEventId = response.playEventId
|
||||
}
|
||||
} catch (
|
||||
@@ -165,7 +178,7 @@ class PlayEventsReporter @Inject constructor(
|
||||
// Offline / flaky — openPlayEventId stays null; close
|
||||
// path will enqueue the completed play for replay.
|
||||
} finally {
|
||||
startInFlight = false
|
||||
if (curPlayToken == token) startInFlight = false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -223,6 +236,7 @@ class PlayEventsReporter @Inject constructor(
|
||||
|
||||
private fun resetCurrent() {
|
||||
curTrackId = null
|
||||
curListenSeq = -1
|
||||
curStartedAt = null
|
||||
curSource = null
|
||||
curLastPositionMs = 0
|
||||
|
||||
@@ -121,6 +121,14 @@ class PlayerController @Inject constructor(
|
||||
*/
|
||||
private var lastEvaluatedItemIndex: Int = -1
|
||||
|
||||
/**
|
||||
* Listens begun, published as [PlayerUiState.listenSeq]. Bumped on every
|
||||
* onMediaItemTransition: Media3 reports a repeat of the same item as a
|
||||
* transition too (MEDIA_ITEM_TRANSITION_REASON_REPEAT), though the
|
||||
* current track id doesn't change.
|
||||
*/
|
||||
private var listenSeq: Long = 0
|
||||
|
||||
/**
|
||||
* Self-heal budget — re-pulls of a stale refreshable queue since
|
||||
* the last successful play. Capped so a still-broken refresh can't loop;
|
||||
@@ -569,6 +577,8 @@ class PlayerController @Inject constructor(
|
||||
mediaItem: androidx.media3.common.MediaItem?,
|
||||
reason: Int,
|
||||
) {
|
||||
// A new listen begins, the same item on repeat included.
|
||||
listenSeq++
|
||||
// Reset the per-item evaluation guard so the new
|
||||
// item's STATE_READY transition gets a fresh check.
|
||||
lastEvaluatedItemIndex = -1
|
||||
@@ -636,6 +646,7 @@ class PlayerController @Inject constructor(
|
||||
else -> RepeatMode.OFF
|
||||
},
|
||||
isUpnpLoading = isUpnpLoading,
|
||||
listenSeq = listenSeq,
|
||||
)
|
||||
}
|
||||
},
|
||||
|
||||
@@ -29,4 +29,10 @@ data class PlayerUiState(
|
||||
val repeatMode: RepeatMode = RepeatMode.OFF,
|
||||
/** True while the UPnP initial-batch load is in progress (target set, active not yet wired). */
|
||||
val isUpnpLoading: Boolean = false,
|
||||
/**
|
||||
* Counts listens begun: moves on every media-item transition, a repeat
|
||||
* included. The play reporter keys a play on (track, listen), so a song
|
||||
* on repeat-one, or the same song twice in a row, is a play per listen.
|
||||
*/
|
||||
val listenSeq: Long = 0,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user