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
@@ -0,0 +1,123 @@
package org.siloserver.silo.common.player

import org.siloserver.silo.model.playback.PlaybackDelivery
import org.siloserver.silo.model.playback.PlaybackExecutionPlan
import org.siloserver.silo.model.playback.PlaybackTimeline
import org.siloserver.silo.model.playback.PlayerSubtitleInfo
import org.siloserver.silo.model.playback.SubtitleIdentity
import org.siloserver.silo.model.playback.SubtitleValidationClaims
import org.siloserver.silo.model.playback.activeOriginalHttpClaims
import org.siloserver.silo.model.playback.carriesHlsSubtitleRendition

/**
* Whether the player may show [target] on the stream it is playing before the
* server answers the pick, instead of waiting for a replan and a remount.
*
* True for a track the media already carries and the server said the player
* may switch to: a native track of an `original_http` stream
* ([PlayerSubtitleInfo.nativeContainerTrackId] on a row other than the
* selected one), or a rendition of an HLS stream that lists every embedded
* text track ([PlayerSubtitleInfo.hlsRendition] with an index). Off qualifies
* on the same streams. The pick still goes to the server as a track change;
* its answer is applied in place when it keeps the transport
* ([org.siloserver.silo.common.player.VideoSessionStartV3.Ready.inPlace]).
*
* Phone and TV both ask this before taking the local-first path, and then
* whether Media3 lists the track.
*/
fun canSwitchSubtitleInPlace(
plan: PlaybackExecutionPlan?,
rows: List<PlayerSubtitleInfo>,
committed: SubtitleIdentity,
target: SubtitleIdentity,
): Boolean {
if (plan == null || target == committed || committed is SubtitleIdentity.ServerBurnIn) return false
return when (target) {
SubtitleIdentity.Off -> when {
plan.delivery == PlaybackDelivery.ORIGINAL_HTTP -> true
plan.delivery.carriesHlsSubtitleRendition() -> rows.any { it.hlsRendition?.index != null }
else -> false
}
is SubtitleIdentity.Embedded -> {
val row = rows.singleOrNull { it.index == target.serverIndex && it.serverTrackId != null }
?: return false
val nativeTrackId = target.containerTrackId
val rendition = target.hlsRendition
when {
nativeTrackId != null -> plan.delivery == PlaybackDelivery.ORIGINAL_HTTP &&
row.nativeContainerTrackId == nativeTrackId
rendition != null -> plan.delivery.carriesHlsSubtitleRendition() &&
rendition.index != null &&
row.hlsRendition == rendition
else -> false
}
}
is SubtitleIdentity.ServerSidecar,
is SubtitleIdentity.ServerBurnIn,
is SubtitleIdentity.Downloaded,
is SubtitleIdentity.LocalMedia3,
-> false
}
}

/**
* The part of [this] plan a mounted MediaItem is built from. A plan applied in
* place differs from the playing one only in its subtitle decision, its plan
* identity and its replan-time position, so it has the same key and leaves
* the mounted item alone. Every other plan change also comes with a new mount
* token, so keying a mount on this rather than on the whole plan only ever
* skips an in-place adoption.
*/
fun PlaybackExecutionPlan.mediaMountKey(): PlaybackExecutionPlan = copy(
planId = "",
timeline = PlaybackTimeline(
streamOriginSeconds = timeline.streamOriginSeconds,
timelineOffsetSeconds = timeline.timelineOffsetSeconds,
),
selectedTracks = selectedTracks.copy(subtitleIndex = null),
source = source.copy(subtitleCodec = null),
claims = claims.copy(subtitles = SubtitleValidationClaims()),
// Compared as sets, like the server's own same-route check.
transformations = transformations.sortedBy { "${it.executor}:${it.name}:${it.recipeVersion}" },
appliedQuirks = appliedQuirks.sortedBy { it.id },
runtimeCorrections = runtimeCorrections.sorted(),
availableQualities = emptyList(),
degradationWarnings = emptyList(),
// Only the claim a decision reason names reaches the renderer.
decisionTrace = activeOriginalHttpClaims(),
bandwidthLimit = null,
activeQuality = null,
activeQualityReported = false,
)

/**
* The plan to show once [adopted], a plan applied in place, replaces this
* mounted one: [adopted]'s subtitle decision, plan identity and menus over
* everything the MediaItem was built from. That keeps what the client changed
* on the mount itself (a PCM retry's audio claims and decision trace, the
* mount's timeline), so the media mount key and the stall detectors' key stay
* the same and nothing remounts.
*/
fun PlaybackExecutionPlan.withInPlaceAdoption(adopted: PlaybackExecutionPlan): PlaybackExecutionPlan = copy(
planId = adopted.planId,
selectedTracks = selectedTracks.copy(subtitleIndex = adopted.selectedTracks.subtitleIndex),
source = source.copy(subtitleCodec = adopted.source.subtitleCodec),
claims = claims.copy(subtitles = adopted.claims.subtitles),
availableQualities = adopted.availableQualities,
degradationWarnings = adopted.degradationWarnings,
bandwidthLimit = adopted.bandwidthLimit,
activeQuality = adopted.activeQuality,
activeQualityReported = adopted.activeQualityReported,
)

/**
* The stall detectors' key for one mounted MediaItem. It leaves out the plan
* id, which a plan applied in place changes while the same item keeps
* playing; [mountToken] changes with every remount.
*/
fun mountedPlaybackKey(
sessionId: String?,
streamUrl: String?,
plan: PlaybackExecutionPlan?,
mountToken: Long,
): String = "$sessionId:$streamUrl:${plan?.decisionTrace?.size ?: 0}:$mountToken"
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import org.siloserver.silo.model.playback.LEARNED_LIMIT_IGNORE
import org.siloserver.silo.model.playback.PLAYBACK_PLAN_SUMMARY_V1_FEATURE
import org.siloserver.silo.model.playback.PlaybackDecisionResponseV3
import org.siloserver.silo.model.playback.gatedForBufferingQuality
import org.siloserver.silo.model.playback.appliesInPlaceOver
import org.siloserver.silo.model.playback.withPlayerSwitchedAudio
import org.siloserver.silo.model.playback.carriesHlsSubtitleRendition
import org.siloserver.silo.model.playback.ClientCodecCapabilities
import org.siloserver.silo.model.playback.ClientPlaybackContext
Expand Down Expand Up @@ -234,6 +236,8 @@ open class PlaybackSessionManager(
val replacement: ActiveVideoAttempt,
val predecessor: ActiveVideoAttempt?,
val settled: CompletableDeferred<Unit> = CompletableDeferred(),
/** Applied in place: the audio sink never left the predecessor's attempt. */
val inPlace: Boolean = false,
)

private data class PendingPublicationRollback(
Expand Down Expand Up @@ -1331,6 +1335,19 @@ open class PlaybackSessionManager(
planAttemptKey = nextKey,
capabilities = currentCapabilities,
clientPlaybackContext = currentContext,
// Only a subtitle pick: a quality or output change the
// server happens to answer on the same transport still
// goes through the adoption its own bookkeeping expects.
// Compared against the plan the player renders: that is
// the media a same-transport answer must match, including
// audio the player switched on its own.
inPlace = classification == "subtitle_track_changed" &&
bufferingQuality == null &&
validated.plan.appliesInPlaceOver(
current = active.plan.withPlayerSwitchedAudio(request.selectedTracks.audio, validated.plan),
sessionId = validated.sessionId,
currentSessionId = active.sessionId,
),
)
val staged = StagedVideoReplan(
basePlaybackAttemptId = active.playbackAttemptId,
Expand Down Expand Up @@ -1469,8 +1486,18 @@ open class PlaybackSessionManager(
return@withLock stagedVideoReplanUnavailable()
}

val next = prepared.nextAttempt
passthroughSuppression.beginAttempt(next.planAttemptKey)
// A plan applied in place keeps the playing transport: its first
// frame has been shown and reported, its audio sink and the local
// fixes made to it carry on, so none of that starts over.
val next = if (staged.candidate.inPlace) {
prepared.nextAttempt.copy(
firstFrameReported = active.firstFrameReported,
startedAtElapsedRealtimeMs = active.startedAtElapsedRealtimeMs,
localMutations = active.localMutations,
)
} else {
prepared.nextAttempt.also { passthroughSuppression.beginAttempt(it.planAttemptKey) }
}
val routeEvent = PlaybackRouteEventV3(
playbackAttemptId = next.playbackAttemptId,
sessionId = next.sessionId,
Expand All @@ -1495,6 +1522,7 @@ open class PlaybackSessionManager(
pendingVideoPublication = PendingVideoPublication(
replacement = next,
predecessor = active,
inPlace = staged.candidate.inPlace,
)
}
runCatching { emitRouteEvent(routeEvent) }
Expand Down Expand Up @@ -1689,7 +1717,7 @@ open class PlaybackSessionManager(

pendingVideoPublication = null
revertRenderedPlanKeepingCursor(pending.predecessor)
pending.predecessor?.let {
pending.predecessor?.takeUnless { pending.inPlace }?.let {
passthroughSuppression.beginAttempt(it.planAttemptKey)
}
val protectedSessionIds = setOfNotNull(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import org.siloserver.silo.model.playback.SubtitleHlsRendition
import org.siloserver.silo.model.playback.SUBTITLE_DELIVERY_BURN_IN_ONLY
import org.siloserver.silo.model.playback.SUBTITLE_DELIVERY_SIDECAR
import org.siloserver.silo.model.playback.carriesHlsSubtitleRendition
import org.siloserver.silo.model.playback.nativeSwitchTrackId
import org.siloserver.silo.model.playback.switchableHlsRendition
import org.siloserver.silo.model.playback.resolvedSelectedSubtitleIndex
import org.siloserver.silo.playback.isBitmapSubtitleCodecFamily

Expand All @@ -31,6 +33,13 @@ sealed interface VideoSessionStartV3 {
val capabilities: org.siloserver.silo.model.playback.ClientCodecCapabilities,
/** Exact output/delivery context used to negotiate this plan. */
val clientPlaybackContext: org.siloserver.silo.model.playback.ClientPlaybackContext,
/**
* The plan keeps the transport the player is already playing and
* changes only a subtitle it selects in that media
* ([org.siloserver.silo.model.playback.appliesInPlaceOver]), so its
* adoption must not remount the MediaItem. Only a replan sets it.
*/
val inPlace: Boolean = false,
) : VideoSessionStartV3

data class Terminal(
Expand Down Expand Up @@ -83,7 +92,7 @@ internal fun PlaybackPlanV3.toSessionResponse(
val selectedHlsRendition = subtitle.hlsRendition?.takeIf {
delivery.carriesHlsSubtitleRendition() &&
(subtitle.mode == PlaybackSubtitleModeV3.CONVERT || subtitle.mode == PlaybackSubtitleModeV3.RENDER)
}?.let { SubtitleHlsRendition(groupId = it.groupId, name = it.name) }
}?.let { SubtitleHlsRendition(groupId = it.groupId, name = it.name, index = it.index) }
// Neutral v3 publishes the complete subtitle inventory on every plan,
// including plans with subtitles off. Project it into native phone/TV UI
// state so both menus retain every authoritative ordinal. The player mount
Expand All @@ -98,11 +107,21 @@ internal fun PlaybackPlanV3.toSessionResponse(
it.delivery == SUBTITLE_DELIVERY_BURN_IN_ONLY
}
.map { item ->
// The stream itself carries a rendition-selected track. Its
// inventory URL is a whole-file extraction, the slow path the
// rendition replaces, so this row publishes no URL to mount.
val rendition = selectedHlsRendition.takeIf {
item.trackId == subtitle.trackId && item.combinedIndex == selectedSubtitleIndex
// The selected row follows the plan's own decision. Every other
// row says how the player can switch to it on the stream it
// already plays: a rendition that stream lists, or a native track
// of the original file. Those rows are exact identities, never
// sidecars, so a pick shows at once while the replan confirms it.
val selected = item.trackId == subtitle.trackId && item.combinedIndex == selectedSubtitleIndex
// The stream itself carries a rendition track. Its inventory URL
// is a whole-file extraction, the slow path the rendition
// replaces, so this row publishes no URL to mount.
val rendition = if (selected) {
selectedHlsRendition
} else {
switchableHlsRendition(item)?.let {
SubtitleHlsRendition(groupId = it.groupId, name = it.name, index = it.index)
}
}
PlayerSubtitleInfo(
index = item.combinedIndex,
Expand All @@ -120,8 +139,10 @@ internal fun PlaybackPlanV3.toSessionResponse(
catalogLabel = item.label,
catalogSource = item.source,
isDefault = item.isDefault,
nativeContainerTrackId = subtitle.embedded?.containerTrackId.takeIf {
item.trackId == subtitle.trackId && item.combinedIndex == selectedSubtitleIndex
nativeContainerTrackId = if (selected) {
subtitle.embedded?.containerTrackId
} else {
nativeSwitchTrackId(item)
},
hlsRendition = rendition,
serverTrackId = item.trackId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,17 @@ fun isSubtitleSelected(tracks: Tracks, identity: SubtitleIdentity): Boolean {
?.isTrackSelected(selection.trackIndex) == true
}

/**
* Whether the player shows what [identity] asks for: the exact track, or for
* Off no text track at all. A local switch confirms itself with this.
*/
fun isSubtitleSelectionApplied(tracks: Tracks, identity: SubtitleIdentity): Boolean =
if (identity == SubtitleIdentity.Off) {
tracks.groups.none { it.type == C.TRACK_TYPE_TEXT && it.isSelected }
} else {
isSubtitleSelected(tracks, identity)
}

/**
* Every text track in a snapshot that this device can render, in snapshot
* order, described the way subtitle resolution sees it.
Expand Down
Loading
Loading