Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ internal fun PlaybackPlanV3.toSessionResponse(
label = item.label,
source = item.source,
forced = item.forced,
hearingImpaired = item.hearingImpaired,
url = if (
rendition == null &&
subtitle.mode != PlaybackSubtitleModeV3.BURN_IN &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.siloserver.silo.common.player.video

import org.siloserver.silo.model.playback.SubtitleIdentity

/**
* The viewer's track picks waiting for playback to take them. A pick is
* remembered for the series only once it lands: a staged pick can be refused
* and a committed one rolled back, and either leaves the earlier track
* playing. Each pick is handed back at most once.
*
* An audio pick the player switches to itself ([local]) lands when the player
* is on the track; one the server must deliver lands when its replan commits.
* Called from the player and from the track transaction's persistence, which
* may run on another thread.
*/
class PendingSeriesTrackPicks {
private var subtitle: SubtitleIdentity? = null
private var audio: Pair<Int, Boolean>? = null

@Synchronized
fun pickSubtitle(identity: SubtitleIdentity) {
subtitle = identity
}

@Synchronized
fun pickAudio(catalogOrdinal: Int, local: Boolean) {
audio = catalogOrdinal to local
}

/** Whether [committed] is the subtitle the viewer picked. */
@Synchronized
fun takeSubtitle(committed: SubtitleIdentity): Boolean {
if (subtitle?.isSameTrackAs(committed) != true) return false
subtitle = null
return true
}

/** Whether [catalogOrdinal], landing [local]ly or through a replan, is the audio the viewer picked. */
@Synchronized
fun takeAudio(catalogOrdinal: Int?, local: Boolean): Boolean {
if (catalogOrdinal == null || audio != (catalogOrdinal to local)) return false
Comment on lines +40 to +41

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kody code-review Bug medium

Audio preference persistence fails on mobile when playback exhausts local selection attempts and falls back to replanForDesiredAudio: its successful transaction calls takeAudio with local=false, which cannot consume the pending local=true entry, so the series never remembers the successfully applied pick. Track pending intent independently of its initial delivery path or update that path when initiating fallback.

Prompt for LLM

File android-shared/src/androidMain/kotlin/org/siloserver/silo/common/player/video/PendingSeriesTrackPicks.kt:

Line 40 to 41:

Audio preference persistence fails on mobile when playback exhausts local selection attempts and falls back to replanForDesiredAudio: its successful transaction calls takeAudio with local=false, which cannot consume the pending local=true entry, so the series never remembers the successfully applied pick. Track pending intent independently of its initial delivery path or update that path when initiating fallback.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

​

​

audio = null
return true
}

@Synchronized
fun clear() {
subtitle = null
audio = null
}
}

/** The same track, though playback may deliver it another way (an embedded track as a sidecar). */
private fun SubtitleIdentity.isSameTrackAs(other: SubtitleIdentity): Boolean =
this == other || serverIndexOrNull()?.let { it == other.serverIndexOrNull() } == true ||
this is SubtitleIdentity.Downloaded && other is SubtitleIdentity.Downloaded && downloadId == other.downloadId

private fun SubtitleIdentity.serverIndexOrNull(): Int? = when (this) {
is SubtitleIdentity.ServerSidecar -> serverIndex
is SubtitleIdentity.ServerBurnIn -> serverIndex
is SubtitleIdentity.Embedded -> serverIndex
else -> null
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class VideoPlaybackSessionCoordinator(
seasonNumber = result.seasonNumber,
episodeNumber = result.episodeNumber,
resolvedEpisodeSelection = result.resolvedEpisodeSelection,
trackPreferenceRestore = result.trackPreferenceRestore,
)
}
is VideoPlaybackStartResult.Error -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ data class VideoPlaybackStartRequest(
val roomId: String?,
val resumePositionOverride: Double?,
val audioTrackIndex: Int? = null,
/** [audioTrackIndex] is the detail row's automatic choice, not the viewer's pick. */
val audioTrackIndexAutoResolved: Boolean = false,
val subtitleTrackIndex: Int? = null,
/**
* [subtitleTrackIndex] is the detail row's Auto preview, not the viewer's
* pick, so the starter may resolve Auto again with the title's own
* preferences.
*/
val subtitleTrackIndexAutoResolved: Boolean = false,
/** Selects the preferred source file version and supplies the default output-quality ceiling. */
val preferredQualityOverride: String? = null,
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ import org.siloserver.silo.model.playback.PlaybackExecutionPlan
import org.siloserver.silo.model.playback.PlaybackPlanV3
import org.siloserver.silo.model.playback.PlayerSubtitleInfo

/**
* How a start applied the title's remembered tracks, for the player's restore
* decisions once its tracks land.
*/
data class TrackPreferenceRestore(
/** The server resolved a subtitle preference, so a per-file choice must not override it. */
val subtitlePreferenceResolved: Boolean = false,
/** The series' remembered subtitle track (`effective_subtitle_track_signature`). */
val subtitleTrackSignature: org.siloserver.silo.model.playback.SubtitleTrackSignature? = null,
/** The start requested the series' saved audio pick, so a per-file choice must not override it. */
val seriesAudioApplied: Boolean = false,
/** The detail row's Auto subtitle re-resolved with this title's own preferences; null when not re-resolved. */
val autoSubtitleTrackIndex: Int? = null,
)

sealed interface VideoPlaybackStartResult {
data class Ready(
val contentId: String,
Expand Down Expand Up @@ -55,6 +70,7 @@ sealed interface VideoPlaybackStartResult {
val episodeNumber: Int? = null,
/** TV's target-catalog resolution of [VideoPlaybackStartRequest.episodeSelectionHandoff]. */
val resolvedEpisodeSelection: ResolvedEpisodeSelection? = null,
val trackPreferenceRestore: TrackPreferenceRestore = TrackPreferenceRestore(),
val seriesTitle: String? = null,
/** Release year, for the player's "MOVIE · 2026" line. */
val year: Int? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ sealed interface VideoPlayerUiState {
val episodeNumber: Int? = null,
/** Target-catalog decision for the one-shot episode-selection handoff. */
val resolvedEpisodeSelection: ResolvedEpisodeSelection? = null,
val trackPreferenceRestore: TrackPreferenceRestore = TrackPreferenceRestore(),
val seriesTitle: String? = null,
/** Release year, for the player's "MOVIE · 2026" line. */
val year: Int? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ val androidModule = module {
reachabilityMonitor = get(),
userItemStatePort = get(),
networkEvidenceProvider = get(),
trackPreferences = get(),
)
}
factory {
Expand Down Expand Up @@ -493,6 +494,7 @@ val androidModule = module {
shuffleFeatureStore = get(),
networkEvidenceProvider = get(),
throughputMeter = get(),
trackPreferenceWriter = get(),
)
}
viewModel { HomeViewModel(get(), get(), get(), get(), getOrNull(), get(), get(), catalogLanguage = get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import org.siloserver.silo.model.catalog.SubtitleTrack
import org.siloserver.silo.model.playback.AutoSubtitleCandidate
import org.siloserver.silo.model.playback.PlayerSubtitleInfo
import org.siloserver.silo.model.playback.SubtitleIdentity
import org.siloserver.silo.model.playback.SubtitleTrackSignature
import org.siloserver.silo.model.playback.autoSubtitlePreferenceOrder
import org.siloserver.silo.model.playback.bestSignatureAutoSubtitleCandidate
import org.siloserver.silo.model.playback.toAutoSubtitleCandidate
import org.siloserver.silo.playback.canonicalSubtitleLanguage
import org.siloserver.silo.playback.hasPositiveSubtitleDiscriminator
Expand Down Expand Up @@ -91,12 +93,20 @@ internal fun resolveMobileAutoSubtitleSelection(
preferredLanguage: String?,
subtitleMode: String?,
showForcedSubtitles: Boolean,
/** The series' remembered track (`effective_subtitle_track_signature`); it outranks the language cascade. */
trackSignature: SubtitleTrackSignature? = null,
): MobileSubtitleAutoSelection {
if (subtitles.isEmpty()) return MobileSubtitleAutoSelection.NoChange

val mode = subtitleMode?.trim()?.lowercase()?.takeIf { it.isNotBlank() } ?: "auto"
if (mode == "off") return MobileSubtitleAutoSelection.Disable

if (trackSignature != null) {
val candidates = subtitles.map(PlayerSubtitleInfo::toAutoSubtitleCandidate)
bestSignatureAutoSubtitleCandidate(trackSignature, candidates)
?.let { return MobileSubtitleAutoSelection.Select(candidates.indexOf(it)) }
}

if (preferredLanguage != null && preferredLanguage.isBlank()) {
return MobileSubtitleAutoSelection.Disable
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.siloserver.silo.common.player.TrackSelectionPresets
import org.siloserver.silo.common.player.VideoSessionStartV3
import org.siloserver.silo.common.player.qualityPair
import org.siloserver.silo.common.player.usesCellularQuality
import org.siloserver.silo.common.player.video.TrackPreferenceRestore
import org.siloserver.silo.common.player.video.VideoPlaybackStartRequest
import org.siloserver.silo.common.player.video.VideoPlaybackStartResult
import org.siloserver.silo.common.player.video.VideoPlaybackStarter
Expand All @@ -35,6 +36,10 @@ import org.siloserver.silo.model.playback.resolvedSelectedSubtitleIndex
import org.siloserver.silo.model.playback.videoQualityPreference
import org.siloserver.silo.network.ApiResult
import org.siloserver.silo.playback.orNullIfBlank
import org.siloserver.silo.model.playback.hasSubtitlePreference
import org.siloserver.silo.model.playback.trackPreferenceKey
import org.siloserver.silo.network.apiv2.TrackPreferencesV2Api
import org.siloserver.silo.network.apiv2.savedAudioTrackIndex
import org.siloserver.silo.playback.resolveAudioTrackOrdinal
import org.siloserver.silo.playback.resolveCatalogSubtitlePreferenceOrdinal
import org.siloserver.silo.playback.selectPlaybackVersion
Expand Down Expand Up @@ -92,13 +97,16 @@ internal data class MobileInitialTrackSelection(
internal fun resolveMobileInitialTrackSelection(
explicitAudioTrackIndex: Int?,
explicitSubtitleTrackIndex: Int?,
/** The series' saved audio pick resolved onto this file; see [savedAudioTrackIndex]. */
seriesAudioTrackIndex: Int? = null,
audioTracks: List<org.siloserver.silo.model.catalog.AudioTrack>,
subtitleTracks: List<SubtitleTrack>,
persisted: LocalTrackSelection?,
preferredAudioLanguage: String? = null,
capabilities: ClientCodecCapabilities,
): MobileInitialTrackSelection {
val audioTrackIndex = explicitAudioTrackIndex
?: seriesAudioTrackIndex
?: resolveAudioTrackOrdinal(audioTracks, persisted?.audioFingerprint)
?: preferredAudioLanguage?.let { language ->
TrackSelectionPresets.selectBestCompatibleAudioTrackOrdinal(
Expand Down Expand Up @@ -141,6 +149,7 @@ internal class MobileVideoPlaybackStarter(
private val sessionAdopter: MobileVideoSessionAdopter? = null,
/** Picks the cellular quality pair on mobile data or a metered network. */
private val networkEvidenceProvider: PlaybackNetworkEvidenceProvider = PlaybackNetworkEvidenceProvider.None,
private val trackPreferences: TrackPreferencesV2Api? = null,
) : VideoPlaybackStarter {

override suspend fun start(request: VideoPlaybackStartRequest): VideoPlaybackStartResult {
Expand Down Expand Up @@ -250,11 +259,26 @@ internal class MobileVideoPlaybackStarter(
}
}
val allowAlternateVersions = request.allowAlternateVersions()
val seriesAudioTrackIndex = if (request.audioTrackIndex == null) {
trackPreferences?.savedAudioTrackIndex(
owner = expectedMetadataOwner,
key = trackPreferenceKey(watchDetail.type, watchDetail.contentId, watchDetail.seriesId),
version = version,
)
} else {
null
}
// A choice remembered on this device for this file only fills in
// where the server resolved no series preference.
val persistedTrackSelection = if (
userItemStatePort != null &&
(request.audioTrackIndex == null || request.subtitleTrackIndex == null)
) {
userItemStatePort.localTrackSelection(request.contentId, version.fileId)
?.let { local ->
val serverDecides = watchDetail.hasSubtitlePreference()
local.copy(subtitleFingerprint = local.subtitleFingerprint.takeUnless { serverDecides })
}
} else {
null
}
Expand Down Expand Up @@ -286,6 +310,7 @@ internal class MobileVideoPlaybackStarter(
val initialTracks = resolveMobileInitialTrackSelection(
explicitAudioTrackIndex = request.audioTrackIndex,
explicitSubtitleTrackIndex = request.subtitleTrackIndex,
seriesAudioTrackIndex = seriesAudioTrackIndex,
audioTracks = version.audioTracks.orEmpty(),
subtitleTracks = version.subtitleTracks.orEmpty(),
persisted = persistedTrackSelection,
Expand Down Expand Up @@ -520,6 +545,12 @@ internal class MobileVideoPlaybackStarter(
showForcedSubtitles = watchDetail.effectiveShowForcedSubtitles
?: activeProfile?.showForcedSubtitles
?: true,
// The player reads the watch detail again once tracks mount; a
// failed read there must not undo what this start decided.
trackPreferenceRestore = TrackPreferenceRestore(
subtitlePreferenceResolved = watchDetail.hasSubtitlePreference(),
subtitleTrackSignature = watchDetail.effectiveSubtitleTrackSignature,
),
intro = effectiveVersion?.intro,
credits = effectiveVersion?.credits,
recap = effectiveVersion?.recap,
Expand Down
Loading
Loading