Skip to content
Merged
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 @@ -32,6 +32,8 @@ import org.siloserver.silo.model.playback.CLIENT_DV7_TO_DV81
import org.siloserver.silo.model.playback.CLIENT_DV7_TO_HDR10
import org.siloserver.silo.model.playback.CLIENT_DV_TRANSFORM_RECIPE_VERSION
import org.siloserver.silo.model.playback.NATIVE_HLS_PLAYBACK_V1_FEATURE
import org.siloserver.silo.model.playback.NATIVE_EMBEDDED_SUBTITLE_CODECS
import org.siloserver.silo.model.playback.NativeEmbeddedSubtitleCapability
import org.siloserver.silo.model.playback.CLIENT_SELECTED_AUDIO_TRACK_V1_CLAIM
import org.siloserver.silo.model.playback.CLIENT_DV8_BASE_LAYER_FALLBACK_V1_CLAIM
import org.siloserver.silo.model.playback.HdrCapabilities
Expand Down Expand Up @@ -528,11 +530,22 @@ class PlaybackCapabilityDetector(
maxChannels = passthrough?.maxChannels,
hdrDetails = caps.hdrDetails,
subtitles = DeliverySubtitleCapabilities(
nativeEmbedded = listOf(org.siloserver.silo.model.playback.NativeEmbeddedSubtitleCapability(
container = "mp4",
codecs = listOf("mov_text"),
trackIdentity = "container_track_id",
)),
// MP4 and MatroskaExtractor both set each track's
// Format.id to the container track ID the server
// records. libass's Matroska extractor subclass
// forwards formats unchanged, so ASS keeps the same
// identity; its flags tell the planner whether the
// stream path keeps styling and attached fonts.
nativeEmbedded = NATIVE_EMBEDDED_SUBTITLE_CODECS.map { (container, codecs) ->
val ass = "ass" in codecs
NativeEmbeddedSubtitleCapability(
container = container,
codecs = codecs,
trackIdentity = "container_track_id",
assStyling = ass && libassDirectFidelity,
fontAttachments = ass && libassEmbeddedFonts,
)
},
embeddedText = true,
sidecarText = true,
assStyling = libassDirectFidelity,
Expand Down
34 changes: 27 additions & 7 deletions docs/playback/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,38 @@ feature token has the same name.
## Native embedded subtitles

Android advertises `embedded_subtitles_v1` only when the original-HTTP delivery
includes a `native_embedded` capability. The first supported pair is MP4 with
`mov_text`, using `container_track_id`. Media3 1.11.0 sets a timed-text format's
ID from the MP4 `tkhd` track ID; FFmpeg stream indexes are not Media3 track IDs.
See the pinned [Media3 MP4 parser](https://github.com/androidx/media/blob/1.11.0/libraries/extractor/src/main/java/androidx/media3/extractor/mp4/BoxParser.java).
includes a `native_embedded` capability. Both supported entries use
`container_track_id`; FFmpeg stream indexes are not Media3 track IDs.

- MP4 with `mov_text`: Media3 1.11.0 sets a timed-text format's ID from the MP4
`tkhd` track ID. See the pinned [Media3 MP4 parser](https://github.com/androidx/media/blob/1.11.0/libraries/extractor/src/main/java/androidx/media3/extractor/mp4/BoxParser.java).
- MKV with `subrip` and `ass`: `MatroskaExtractor` sets each track's ID to its
Matroska `TrackNumber`. See the pinned [Media3 Matroska extractor](https://github.com/androidx/media/blob/1.11.0/libraries/extractor/src/main/java/androidx/media3/extractor/mkv/MatroskaExtractor.java).
Remuxed files keep their original track numbers, so numbers can have gaps
and do not equal stream index + 1. The `ass-media` extractor subclass passes
formats through unchanged, so ASS keeps the same ID. The MKV entry's
`ass_styling` and `font_attachments` report whether libass renders the stream
with its attached fonts; when they are false and the user prefers preserved
styling, the server keeps ASS on its sidecar path.

The server must record each Matroska subtitle's `TrackNumber` as
`container_track_id` (proposed in Silo-Server/silo-server#1890). Tracks without
a recorded ID keep using sidecars. MKV WebVTT (`S_TEXT/WEBVTT`) is not
advertised because FFmpeg does not recognize that codec, so the server cannot
identify those tracks. Media3 drops tracks with the legacy `S_ASS` and `S_SSA`
codec IDs, which FFmpeg still reports as `ass`; the server records no ID for
them, so they stay on sidecars too.

Known cost of the stream path: after a seek or resume, a cue that began before
the cluster Media3 resumes from stays hidden until the next cue.

A plan's `subtitle.embedded` selects the exact container track. Inventory URLs
remain fallback descriptions and are not mounted alongside that selection.
Phone and TV commit the preference only after the native track is mounted.
Missing, ambiguous, or unsupported identities produce `subtitle_embedded_failed`
and let the server replan with its sidecar path. Matroska and tracks without a
probed container ID keep using sidecars; metadata similarity is not exact
identity evidence.
and let the server replan with its sidecar path. Tracks without a probed
container ID keep using sidecars; metadata similarity is not exact identity
evidence.

Sidecar cues use absolute source timestamps. Android subtracts the plan's
`timeline_offset_seconds` once, alongside the user's subtitle delay. Embedded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,36 @@ sealed interface PlaybackV3Validation {
data class ReplanRequired(val reason: String, val plan: PlaybackPlanV3, val sessionId: String) : PlaybackV3Validation
}

/**
* Subtitle codecs, by source container, that Android selects natively from an
* original-HTTP stream by `container_track_id`. MP4 `mov_text` maps to the
* `tkhd` track ID and MKV SubRip/ASS to the Matroska TrackNumber, both of which
* Media3 sets as `Format.id`. The capability detector advertises these pairs
* and [validateForMedia3] accepts them, so the two share one container/codec
* list. Both also assume every entry is identified by `container_track_id`:
* only add a pair whose Media3 track ID is the container's own track ID.
*/
val NATIVE_EMBEDDED_SUBTITLE_CODECS: Map<String, List<String>> = mapOf(
"mp4" to listOf("mov_text"),
"mkv" to listOf("subrip", "ass"),
)

/**
* Normalizes a subtitle codec name the way the server does when it matches a
* native capability, so a plan it issues for an alias is not rejected here.
*/
private fun nativeEmbeddedSubtitleCodec(codec: String?): String? =
when (val normalized = codec?.trim()?.lowercase()) {
"srt" -> "subrip"
"tx3g" -> "mov_text"
else -> normalized
}

private fun isNativeEmbeddedSubtitlePair(container: String?, codec: String?): Boolean {
val codecs = NATIVE_EMBEDDED_SUBTITLE_CODECS[container?.trim()?.lowercase()] ?: return false
return nativeEmbeddedSubtitleCodec(codec) in codecs
}

fun PlaybackDecisionResponseV3.validateForMedia3(): PlaybackV3Validation {
if (protocolVersion != PLAYBACK_PROTOCOL_V3 ||
PLAYBACK_PLAN_V3_FEATURE !in serverFeatures ||
Expand Down Expand Up @@ -564,8 +594,8 @@ fun PlaybackDecisionResponseV3.validateForMedia3(): PlaybackV3Validation {
if (plan.delivery != PlaybackDelivery.ORIGINAL_HTTP || plan.subtitle.mode != PlaybackSubtitleModeV3.RENDER ||
plan.subtitle.artifact != null || native.streamIndex < 0 ||
nativeId == null || nativeId !in 1..Int.MAX_VALUE.toLong() || native.containerTrackId != nativeId.toString() ||
selectedSubtitle?.source != "embedded" || selectedSubtitle.codec != "mov_text" ||
plan.source.container?.lowercase() !in setOf("mp4", "mov", "m4v") ||
selectedSubtitle?.source != "embedded" ||
!isNativeEmbeddedSubtitlePair(plan.source.container, selectedSubtitle.codec) ||
plan.subtitle.trackId != plan.selectedTracks.subtitle?.id
) return PlaybackV3Validation.ReplanRequired("subtitle_embedded_failed", plan, resolvedSessionId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ class NativeEmbeddedSubtitleProtocolTest {
assertIs<PlaybackV3Validation.ReplanRequired>(validate(plan.copy(source = PlaybackSourceDescriptorV3(container = "mkv"))))
}

@Test fun matroskaTextTracksArePlayableOnlyForAdvertisedCodecs() {
fun mkv(codec: String) = plan.copy(
source = PlaybackSourceDescriptorV3(container = "mkv"),
subtitle = plan.subtitle.copy(inventory = listOf(track.copy(codec = codec))),
)
assertIs<PlaybackV3Validation.Playable>(validate(mkv("subrip")))
assertIs<PlaybackV3Validation.Playable>(validate(mkv("ass")))
assertIs<PlaybackV3Validation.Playable>(validate(mkv("srt")))
assertIs<PlaybackV3Validation.ReplanRequired>(validate(mkv("webvtt")))
assertIs<PlaybackV3Validation.ReplanRequired>(validate(plan.copy(
subtitle = plan.subtitle.copy(inventory = listOf(track.copy(codec = "subrip"))),
)))
}

@Test fun nativeCapabilitiesHaveExactWireNames() {
val capability = DeliverySubtitleCapabilities(nativeEmbedded = listOf(NativeEmbeddedSubtitleCapability("mp4", listOf("mov_text"), "container_track_id")))
val json = SiloJson.encodeToString(capability)
Expand Down