Skip to content

Commit 34a7ba2

Browse files
authored
fix: proper timestamp interpretation in playback transfer (#247)
1 parent 8ca49aa commit 34a7ba2

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

cmd/daemon/state.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,29 @@ func (s *State) reset() {
6464
}
6565

6666
func (s *State) trackPosition() int64 {
67-
if s.player.IsPaused {
67+
// If paused or not actually playing, use raw position value
68+
if s.player.IsPaused || !s.player.IsPlaying {
69+
return s.player.PositionAsOfTimestamp
70+
}
71+
72+
// Calculate dynamic position only if playback is actually active
73+
now := time.Now().UnixMilli()
74+
elapsed := now - s.player.Timestamp
75+
76+
// Validate timestamp freshness: if elapsed time exceeds 10 minutes (600000ms),
77+
// timestamp is likely stale (e.g., from a previous session), use raw position
78+
const maxReasonableElapsed = 10 * 60 * 1000 // 10 minutes in milliseconds
79+
if elapsed > maxReasonableElapsed || elapsed < 0 {
6880
return s.player.PositionAsOfTimestamp
69-
} else {
70-
return time.Now().UnixMilli() - s.player.Timestamp + s.player.PositionAsOfTimestamp
7181
}
82+
83+
calculated := s.player.PositionAsOfTimestamp + elapsed
84+
// Ensure position is non-negative (shouldn't happen, but defensive)
85+
if calculated < 0 {
86+
return s.player.PositionAsOfTimestamp
87+
}
88+
89+
return calculated
7290
}
7391

7492
// Update timestamp, and updating the player position timestamp according to how

0 commit comments

Comments
 (0)