0.95.2: reopen the sound at the same point when the box's audio clock goes wrong (spurious audio timestamps), instead of drifting
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 2m8s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m25s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-03 21:57:20 +00:00
co-authored by Claude Opus 5.5
parent e10afd41d5
commit dcee125b93
6 changed files with 85 additions and 7 deletions
@@ -66,7 +66,7 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI
connection.connectTimeout = 10_000
connection.readTimeout = request.readTimeoutMs ?: if (request.path == "/Sessions/Logout") 5_000 else 15_000
connection.setRequestProperty("Accept", "application/json")
connection.setRequestProperty("X-Emby-Authorization", "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.95.1\"")
connection.setRequestProperty("X-Emby-Authorization", "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.95.2\"")
request.token?.let { connection.setRequestProperty("X-Emby-Token", it) }
if (request.method == "POST") {
val bytes = request.body.orEmpty().toByteArray(Charsets.UTF_8)
@@ -38,6 +38,25 @@ import kotlinx.coroutines.withContext
/** A decoder fault normally repeats on the same file, so one more try is enough. */
private const val MAX_PLAYER_RETRIES = 1
/**
* The box's audio clock can go wrong deep into a long film (seen 3 November on Godzilla's TrueHD passthrough, about
* 1 hour 40 in): every position report the sound hardware gives back is nonsense, ExoPlayer rejects them twice a second
* ("Spurious audio timestamp"), and the sound drifts out of sync with the picture until the track is reopened.
* While a scheduled programme has been playing steadily, Orbit asks ExoPlayer to stop on that instead, and reopens the
* sound at the same point: a one-second blip rather than a drifting film. Not during the first moments of a programme,
* where a single stray report is normal, and only a few times a programme, after which the old tolerant behaviour returns.
*/
internal const val AUDIO_CLOCK_SETTLE_MS = 30_000L
internal const val MAX_AUDIO_CLOCK_RESETS = 3
/** True when a playback error is ExoPlayer giving up on the sound hardware's position reports. */
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
internal fun isAudioClockFault(error: Throwable): Boolean =
generateSequence(error) { it.cause }.any { it is androidx.media3.exoplayer.audio.DefaultAudioSink.InvalidAudioTrackTimestampException }
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
internal fun watchAudioClock(on: Boolean) { androidx.media3.exoplayer.audio.DefaultAudioSink.failOnSpuriousAudioTimestamp = on }
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
@Composable
internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPlan, onClose: () -> Unit, scheduled: ScheduledPlayback? = null,
@@ -64,6 +83,7 @@ internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPl
var handedBack by remember { mutableStateOf(false) }
var playerRetries by remember { mutableIntStateOf(0) }
var audioRetries by remember { mutableIntStateOf(0) }
var audioClockResets by remember { mutableIntStateOf(0) }
var audioChosen by remember { mutableStateOf(false) }
var trackProblem by remember { mutableStateOf<String?>(null) }
val notes = remember { java.io.File(context.noBackupFilesDir, "schedule") }
@@ -118,9 +138,9 @@ internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPl
else numberProblem = "Channel ${entry.number} isn't available"
}
LaunchedEffect(trackProblem) { if (trackProblem != null) { delay(6_000); trackProblem = null } }
LaunchedEffect(playerRetries, audioRetries) {
if ((playerRetries > 0 || audioRetries > 0) && scheduled != null) {
delay(800)
LaunchedEffect(playerRetries, audioRetries, audioClockResets) {
if ((playerRetries > 0 || audioRetries > 0 || audioClockResets > 0) && scheduled != null) {
delay(if (audioClockResets > 0 && playerRetries == 0 && audioRetries == 0) 200 else 800)
player.seekTo(scheduled.programme.offsetAt(scheduled.nowMs()))
player.prepare()
}
@@ -154,6 +174,15 @@ internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPl
playing = player.isPlaying
val listener = object : Player.Listener {
override fun onPlayerError(error: PlaybackException) {
// The sound hardware's clock went wrong: reopen at the same point, not a retry and never a failure.
if (scheduled != null && isAudioClockFault(error) && audioClockResets < MAX_AUDIO_CLOCK_RESETS) {
android.util.Log.w("OrbitSchedule", "channel=${scheduled.channel.number} audioClockReset=${audioClockResets + 1} positionMs=${player.currentPosition}")
PlaybackNotes.add(notes, "channel=${scheduled.channel.number} item=${plan.item.id} audioClockReset=${audioClockResets + 1} positionMs=${player.currentPosition}")
watchAudioClock(false)
ready = false
audioClockResets++
return
}
android.util.Log.w("OrbitSchedule", "channel=${scheduled?.channel?.number} playerError=${error.errorCodeName} retry=$playerRetries")
PlaybackNotes.add(notes, "channel=${scheduled?.channel?.number} item=${plan.item.id} error=${error.errorCodeName} audioChosen=$audioChosen")
// The viewer's choice of sound failed, not the programme. Back to the sound that was working, and the file keeps its good name.
@@ -230,6 +259,15 @@ internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPl
LaunchedEffect(player, ready) {
if (ready) { delay(2_000); tuningNumberVisible = false }
}
// Watch the audio clock only once the programme has played steadily for a while, and never under its ident.
LaunchedEffect(player, ready, ident != null, audioClockResets) {
watchAudioClock(false)
if (scheduled != null && ready && ident == null && audioClockResets < MAX_AUDIO_CLOCK_RESETS) {
delay(AUDIO_CLOCK_SETTLE_MS)
watchAudioClock(true)
}
}
DisposableEffect(player) { onDispose { watchAudioClock(false) } }
val covered = ident != null
LaunchedEffect(player, intro?.showing) { if (intro != null) player.volume = if (intro.showing) 0f else 1f }
LaunchedEffect(player, covered) {
@@ -33,7 +33,7 @@ internal class SecureMediaDataSource(private val session: EmbySampleSession) : B
next.instanceFollowRedirects = false
next.connectTimeout = 10_000
next.readTimeout = 20_000
next.setRequestProperty("User-Agent", "OrbitTV/0.95.1")
next.setRequestProperty("User-Agent", "OrbitTV/0.95.2")
next.setRequestProperty("Accept-Encoding", "identity")
session.mediaHeaders(address).forEach { (key, value) -> next.setRequestProperty(key, value) }
if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) {