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
|
* scrobbles would not fire, and system-playlist rotation would not
|
||||||
* advance.
|
* 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`
|
* [PlayerController.uiState]. The client always calls `playEnded`
|
||||||
* with the actual position played to; the server applies its
|
* with the actual position played to; the server applies its
|
||||||
* configurable skip rule (`skip_max_completion_ratio` AND
|
* configurable skip rule (`skip_max_completion_ratio` AND
|
||||||
@@ -81,55 +81,67 @@ class PlayEventsReporter @Inject constructor(
|
|||||||
// orphan on the next play_started instead.
|
// orphan on the next play_started instead.
|
||||||
private var startInFlight: Boolean = false
|
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 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 {
|
init {
|
||||||
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
|
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
|
||||||
scope.launch {
|
scope.launch {
|
||||||
playerController.uiState.collect { state ->
|
playerController.uiState.collect { state -> evaluate(state) }
|
||||||
evaluate(
|
|
||||||
trackId = state.currentTrack?.id,
|
|
||||||
playing = state.isPlaying,
|
|
||||||
positionMs = state.positionMs,
|
|
||||||
durationMs = state.durationMs,
|
|
||||||
source = state.currentSource,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun evaluate(
|
private fun evaluate(state: PlayerUiState) {
|
||||||
trackId: String?,
|
val trackId = state.currentTrack?.id
|
||||||
playing: Boolean,
|
val listenSeq = state.listenSeq
|
||||||
positionMs: Long,
|
val playing = state.isPlaying
|
||||||
durationMs: Long,
|
val durationMs = state.durationMs
|
||||||
source: String?,
|
val listenChanged = trackId != prevTrackId || listenSeq != prevListenSeq
|
||||||
) {
|
// Listen change (a new track, or the same one again) → close the
|
||||||
// Track-change → close the prior tracked play.
|
// prior tracked play.
|
||||||
if (trackId != prevTrackId && curTrackId != null) {
|
if (listenChanged && curTrackId != null) {
|
||||||
closeCurrent()
|
closeCurrent()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entered playing for a new track → begin tracking it.
|
// Entered playing for a new listen → begin tracking it.
|
||||||
if (trackId != null && playing && curTrackId != trackId) {
|
val tracked = curTrackId == trackId && curListenSeq == listenSeq
|
||||||
beginTrack(trackId = trackId, source = source, durationMs = durationMs)
|
if (trackId != null && playing && !tracked) {
|
||||||
|
beginTrack(
|
||||||
|
trackId = trackId,
|
||||||
|
listenSeq = listenSeq,
|
||||||
|
source = state.currentSource,
|
||||||
|
durationMs = durationMs,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Advance the tracked play's progress. The gate `trackId ==
|
// Advance the tracked play's progress. The gate on the tracked
|
||||||
// curTrackId` keeps a finishing track's last-known position
|
// listen keeps a finishing track's last-known position intact
|
||||||
// intact across the moment the listener fires for a track
|
// across the moment the listener fires for a track change or a
|
||||||
// change (where currentTrack is briefly the new one).
|
// repeat (where the position has already gone back to 0).
|
||||||
if (curTrackId != null && trackId == curTrackId) {
|
if (curTrackId != null && trackId == curTrackId && listenSeq == curListenSeq) {
|
||||||
curLastPositionMs = positionMs
|
curLastPositionMs = state.positionMs
|
||||||
if (durationMs > 0) curDurationMs = durationMs
|
if (durationMs > 0) curDurationMs = durationMs
|
||||||
curPlaying = playing
|
curPlaying = playing
|
||||||
}
|
}
|
||||||
|
|
||||||
prevTrackId = trackId
|
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
|
curTrackId = trackId
|
||||||
|
curListenSeq = listenSeq
|
||||||
curStartedAt = Clock.System.now()
|
curStartedAt = Clock.System.now()
|
||||||
curSource = source
|
curSource = source
|
||||||
curLastPositionMs = 0
|
curLastPositionMs = 0
|
||||||
@@ -137,10 +149,11 @@ class PlayEventsReporter @Inject constructor(
|
|||||||
curPlaying = true
|
curPlaying = true
|
||||||
openPlayEventId = null
|
openPlayEventId = null
|
||||||
startInFlight = true
|
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()
|
val cid = resolveClientId()
|
||||||
if (cid == null) {
|
if (cid == null) {
|
||||||
startInFlight = false
|
startInFlight = false
|
||||||
@@ -156,7 +169,7 @@ class PlayEventsReporter @Inject constructor(
|
|||||||
deviceClass = DEVICE_CLASS,
|
deviceClass = DEVICE_CLASS,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
if (curTrackId == trackId) {
|
if (curPlayToken == token) {
|
||||||
openPlayEventId = response.playEventId
|
openPlayEventId = response.playEventId
|
||||||
}
|
}
|
||||||
} catch (
|
} catch (
|
||||||
@@ -165,7 +178,7 @@ class PlayEventsReporter @Inject constructor(
|
|||||||
// Offline / flaky — openPlayEventId stays null; close
|
// Offline / flaky — openPlayEventId stays null; close
|
||||||
// path will enqueue the completed play for replay.
|
// path will enqueue the completed play for replay.
|
||||||
} finally {
|
} finally {
|
||||||
startInFlight = false
|
if (curPlayToken == token) startInFlight = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -223,6 +236,7 @@ class PlayEventsReporter @Inject constructor(
|
|||||||
|
|
||||||
private fun resetCurrent() {
|
private fun resetCurrent() {
|
||||||
curTrackId = null
|
curTrackId = null
|
||||||
|
curListenSeq = -1
|
||||||
curStartedAt = null
|
curStartedAt = null
|
||||||
curSource = null
|
curSource = null
|
||||||
curLastPositionMs = 0
|
curLastPositionMs = 0
|
||||||
|
|||||||
@@ -121,6 +121,14 @@ class PlayerController @Inject constructor(
|
|||||||
*/
|
*/
|
||||||
private var lastEvaluatedItemIndex: Int = -1
|
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
|
* Self-heal budget — re-pulls of a stale refreshable queue since
|
||||||
* the last successful play. Capped so a still-broken refresh can't loop;
|
* 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?,
|
mediaItem: androidx.media3.common.MediaItem?,
|
||||||
reason: Int,
|
reason: Int,
|
||||||
) {
|
) {
|
||||||
|
// A new listen begins, the same item on repeat included.
|
||||||
|
listenSeq++
|
||||||
// Reset the per-item evaluation guard so the new
|
// Reset the per-item evaluation guard so the new
|
||||||
// item's STATE_READY transition gets a fresh check.
|
// item's STATE_READY transition gets a fresh check.
|
||||||
lastEvaluatedItemIndex = -1
|
lastEvaluatedItemIndex = -1
|
||||||
@@ -636,6 +646,7 @@ class PlayerController @Inject constructor(
|
|||||||
else -> RepeatMode.OFF
|
else -> RepeatMode.OFF
|
||||||
},
|
},
|
||||||
isUpnpLoading = isUpnpLoading,
|
isUpnpLoading = isUpnpLoading,
|
||||||
|
listenSeq = listenSeq,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -29,4 +29,10 @@ data class PlayerUiState(
|
|||||||
val repeatMode: RepeatMode = RepeatMode.OFF,
|
val repeatMode: RepeatMode = RepeatMode.OFF,
|
||||||
/** True while the UPnP initial-batch load is in progress (target set, active not yet wired). */
|
/** True while the UPnP initial-batch load is in progress (target set, active not yet wired). */
|
||||||
val isUpnpLoading: Boolean = false,
|
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