Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1a056d5
OPTI-1528: extract language localisation logic into a separate file, …
adadukin-dolby Mar 16, 2026
55f3620
OPTI-1528: add util to conditionally check player version
adadukin-dolby Mar 16, 2026
c111205
OPTI-1528: add CEA label formatting util
adadukin-dolby Mar 16, 2026
9c78eaf
OPTI-1528: add a CEA formatting checker with unit tests
adadukin-dolby Mar 16, 2026
9cddc51
OPTI-1528: substitute label with channelNumber for CEA tracks
adadukin-dolby Mar 16, 2026
41b48c3
OPTI-1528: add a DASH stream example with CEA text tracks
adadukin-dolby Mar 16, 2026
b8b3d7d
OPTI-1528: check whether the label is null or blank
adadukin-dolby Mar 16, 2026
2aea86a
OPTI-1528: address the review feedback
adadukin-dolby Mar 16, 2026
04d0ef4
Read `Track.channelNumber` using reflection
MattiasBuelens Mar 16, 2026
db459c5
Fix indentation
MattiasBuelens Mar 16, 2026
9e0d8c1
Simplify
MattiasBuelens Mar 16, 2026
5f33a33
Tweak checks
MattiasBuelens Mar 16, 2026
933465d
OPTI-1528: return locale full display name in its own locale
adadukin-dolby Mar 16, 2026
b7c75ef
OPTI-1528: fix reflection call
adadukin-dolby Mar 17, 2026
124e19f
Add `Version` class
MattiasBuelens Mar 17, 2026
597050f
Cache parsed THEOplayer version
MattiasBuelens Mar 17, 2026
cc6717e
Throw if player version cannot be parsed
MattiasBuelens Mar 17, 2026
0de6af2
Rename
MattiasBuelens Mar 17, 2026
8f77f2c
Test the whole version
MattiasBuelens Mar 17, 2026
c76d6ac
Improve test titles
MattiasBuelens Mar 17, 2026
6e1bd64
Make `Version` comparable
MattiasBuelens Mar 17, 2026
3800c21
Move to `Compat`
MattiasBuelens Mar 17, 2026
d8c6bfd
Use helper class instead of reflection
MattiasBuelens Mar 17, 2026
8c3901b
Link to AndroidX docs
MattiasBuelens Mar 18, 2026
a89ddbe
Simplify mocking THEOplayer version
MattiasBuelens Mar 18, 2026
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
9 changes: 8 additions & 1 deletion app/src/main/java/com/theoplayer/android/ui/demo/Streams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ val streams by lazy {
TypedSource.Builder("https://livesim.dashif.org/livesim/testpic_2s/Manifest.mpd")
.build()
).build()
)
),
Stream(
title = "Test card (with CEA tracks)",
source = SourceDescription.Builder(
TypedSource.Builder("https://livesim2.dashif.org/vod/testpic_2s/cea608.mpd")
.build()
).build()
),
)
}

Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ activity-compose = "1.10.1"
appcompat = "1.7.1"
compose-bom = "2025.08.01"
junit4 = "4.13.2"
mockk = "1.14.9"
playServices-castFramework = "21.5.0"
ui-test-junit4 = "1.9.0" # ...not in BOM for some reason?
androidx-junit = "1.3.0"
androidx-espresso = "3.7.0"
androidx-mediarouter = "1.8.1"
dokka = "2.0.0"
theoplayer = { prefer="10.11.0", strictly = "[7.6.0, 11.0)" }
theoplayer = { prefer="10.13.0", strictly = "[7.6.0, 11.0)" }

[libraries]
androidx-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "ktx" }
Expand All @@ -35,6 +36,7 @@ androidx-compose-ui-toolingPreview = { group = "androidx.compose.ui", name = "ui
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-junit" }
androidx-espresso = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidx-espresso" }
androidx-mediarouter = { group = "androidx.mediarouter", name = "mediarouter", version.ref = "androidx-mediarouter" }
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
playServices-castFramework = { group = "com.google.android.gms", name = "play-services-cast-framework", version.ref = "playServices-castFramework" }
gradle-plugin = { group = "com.android.tools.build", name = "gradle", version.ref = "gradle" }
dokka-base = { group = "org.jetbrains.dokka", name = "dokka-base", version.ref = "dokka" }
Expand Down
2 changes: 2 additions & 0 deletions ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ dependencies {
implementation(libs.androidx.compose.ui.toolingPreview)
implementation(libs.androidx.compose.material3)
implementation(libs.androidx.compose.material.iconsExtended)

testImplementation(libs.junit4)
testImplementation(libs.mockk)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso)
androidTestImplementation(libs.androidx.compose.ui.testJunit4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fun AudioTrackList(
) {
val audioTrack = audioTracks[it]
ListItem(
headlineContent = { Text(text = formatTrackLabel(audioTrack)) },
headlineContent = { Text(text = rememberTrackLabel(audioTrack)) },
leadingContent = {
RadioButton(
selected = (activeAudioTrack == audioTrack),
Expand Down
28 changes: 10 additions & 18 deletions ui/src/main/java/com/theoplayer/android/ui/Helper.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.theoplayer.android.ui

import android.content.res.Resources
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalResources
import com.theoplayer.android.api.player.track.Track
import java.util.Locale
import com.theoplayer.android.ui.util.constructLabel
import kotlin.math.absoluteValue

/**
Expand Down Expand Up @@ -63,19 +65,9 @@ fun formatTime(time: Double, guide: Double = 0.0, preferNegative: Boolean = fals
* @param track the media track or text track
*/
@Composable
fun formatTrackLabel(track: Track): String {
val label = track.label
if (!label.isNullOrEmpty()) {
return label
}
val languageCode = track.language
if (!languageCode.isNullOrEmpty()) {
val locale = Locale.forLanguageTag(languageCode)
val languageName = locale.getDisplayName(locale)
if (languageName.isNotEmpty()) {
return languageName
}
return languageCode
}
return stringResource(R.string.theoplayer_ui_track_unknown)
}
fun rememberTrackLabel(
track: Track,
resources: Resources = LocalResources.current,
): String = remember(key1 = track.id, key2 = track.uid) {
constructLabel(track) ?: resources.getString(R.string.theoplayer_ui_track_unknown)
}
4 changes: 2 additions & 2 deletions ui/src/main/java/com/theoplayer/android/ui/LanguageMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fun MenuScope.LanguageMenuCompact() {
) {
Text(
modifier = Modifier.weight(1f),
text = player?.activeAudioTrack?.let { formatTrackLabel(it) }
text = player?.activeAudioTrack?.let { rememberTrackLabel(it) }
?: stringResource(
R.string.theoplayer_ui_audio_none
),
Expand Down Expand Up @@ -115,7 +115,7 @@ fun MenuScope.LanguageMenuCompact() {
) {
Text(
modifier = Modifier.weight(1f),
text = player?.activeSubtitleTrack?.let { formatTrackLabel(it) } ?: stringResource(
text = player?.activeSubtitleTrack?.let { rememberTrackLabel(it) } ?: stringResource(
R.string.theoplayer_ui_subtitles_off
),
textAlign = TextAlign.Center
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fun SubtitleTrackList(
) {
val audioTrack = subtitleTracks[it]
ListItem(
headlineContent = { Text(text = formatTrackLabel(audioTrack)) },
headlineContent = { Text(text = rememberTrackLabel(audioTrack)) },
leadingContent = {
RadioButton(
selected = (activeSubtitleTrack == audioTrack),
Expand Down
45 changes: 45 additions & 0 deletions ui/src/main/java/com/theoplayer/android/ui/util/CeaUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.theoplayer.android.ui.util

import androidx.annotation.CheckResult
import androidx.annotation.IntRange

private val CEA_FORMATTING_REGEX = "^CC(\\d+)$".toRegex()

/**
* Checks whether a provided label is CEA-608 or CEA-708 formed.
*/
@CheckResult
internal fun isLabelCeaFormatted(label: String?): Boolean {
if (label.isNullOrEmpty()) {
return false
}

val matchResult = CEA_FORMATTING_REGEX.find(label) ?: return false
val groupValues = matchResult.groupValues
// There is one group we want to match with the channel number.
if (groupValues.size != 2) {
return false
}

val rawChannelNumber = groupValues[1]
val channelNumber = rawChannelNumber.toIntOrNull()
return !rawChannelNumber.startsWith("0") && channelNumber in 1..63
}

/**
* Creates a text track label for CEA-608 and CEA-708 formats.
*
* @return an optional string composed of a [channelNumber] and a prepended
* "CC" suffix, or `null` if the channel number is invalid.
*/
@CheckResult
internal fun getLabelForChannelNumber(
@IntRange(from = 0L, to = 63L) channelNumber: Int,
): String? {
// CEA-608 only supports channel numbers in [1, 4],
// while CEA-708 support service numbers in [1, 63].
if (channelNumber !in 1..63) {
return null
}
return "CC${channelNumber}"
}
32 changes: 32 additions & 0 deletions ui/src/main/java/com/theoplayer/android/ui/util/Compat.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.theoplayer.android.ui.util

import androidx.annotation.DoNotInline
import com.theoplayer.android.api.player.track.texttrack.TextTrack

/**
* Returns [TextTrack.getCaptionChannel], if available.
*/
internal val TextTrack.captionChannelCompat: Int?
get() {
// TextTrack.getCaptionChannel was added in THEOplayer 10.13.0.
return if (THEOplayerGlobalExt.version >= version1013) {
TheoPlayer1013Impl.getTextTrackCaptionChannel(this)
} else null
}

private val version1013 = Version(major = 10, minor = 13, patchAndPrerelease = "0")

/**
* This class must be loaded **only** with THEOplayer 10.13.0 or higher.
*
* This uses the same pattern as AndroidX AppCompat,
* see e.g. [androidx.appcompat.app.AppCompatDelegate.Api33Impl]
* and the docs about [API-specific implementations](https://github.com/androidx/androidx/blob/androidx-main/docs/api_guidelines/compat.md#delegating-to-api-specific-implementations-delegating-to-api-specific-implementations)
* and [static shims](https://github.com/androidx/androidx/blob/androidx-main/docs/api_guidelines/platform_compat.md#static-shims-ex-viewcompat-static-shim).
*/
private class TheoPlayer1013Impl private constructor() {
companion object {
@DoNotInline
fun getTextTrackCaptionChannel(track: TextTrack): Int? = track.captionChannel
}
}
88 changes: 88 additions & 0 deletions ui/src/main/java/com/theoplayer/android/ui/util/TrackExts.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.theoplayer.android.ui.util

import androidx.annotation.CheckResult
import com.theoplayer.android.api.player.track.Track
import com.theoplayer.android.api.player.track.texttrack.TextTrack
import com.theoplayer.android.api.player.track.texttrack.TextTrackType
import java.util.Locale

private const val LANGUAGE_UNDEFINED = "und"

/**
* Returns a name for the [Track.language] in the
* [Locale.Category.DISPLAY] locale that is appropriate
* for display to the user.
* If such conversion is not possible, for instance
* when [Track.language] is `null`, blank, or `"und"`,
* returns `null`.
*/
@get:CheckResult
internal val Track.localizedLanguageName: String?
get() {
val languageCode = this.language
?.takeUnless { it.isBlank() || it == LANGUAGE_UNDEFINED }
?: return null
val locale = Locale.forLanguageTag(languageCode)
val localisedLanguage: String? = locale.getDisplayName(locale)
return localisedLanguage?.takeUnless { it.isBlank() }
}

/**
* Constructs a label for the given [Track] instance.
* The method works slightly different for different player version.
*
* On version 10 and below the logic checks the following and condition
* and the first not `null` entry from the list:
* 1. Track label if is not a language code
* or a CEA-prefixed string.
* 2. Track language display name
* 3. Track caption channel if a text CEA-608 track
* 4. Track label if was either a language code or a CEA-prefixed string
*
* If none of the above is satisfied, returns `null`.
*
* On version 11 and later the logic has slightly changed as
* the player no longer constructs the [Track.getLabel] internally:
* 1. Track label
* 2. Track language display name
* 3. Track caption channel
*/
internal fun constructLabel(
track: Track,
): String? {
val label: String? = if (
(track is TextTrack) &&
THEOplayerGlobalExt.version.major < 11 &&
(isLabelCeaFormatted(track.label) || (track.label != null && track.language == track.label))
) {
// If we are below 11th major release
// and the label is CEA-formatted we
// can safely assume it was the last resort
// option to produce a meaningful label, given
// we cannot localize the language code in the player.
null
} else {
// With 11 release, the player will no longer
// prefix text tracks with "CC" for CEA-608 and CEA-708,
// if [Track.label] is `null`.
track.label
}

if (!label.isNullOrBlank()) {
return label
}

track.localizedLanguageName?.let { return it }

if ((track is TextTrack) && track.type == TextTrackType.CEA608) {
track.captionChannelCompat
?.let { getLabelForChannelNumber(it) }
?.let { return it }

track.label
?.takeUnless { it.isBlank() }
?.let { return it }
}

return null
}
62 changes: 62 additions & 0 deletions ui/src/main/java/com/theoplayer/android/ui/util/VersionUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.theoplayer.android.ui.util

import com.theoplayer.android.api.THEOplayerGlobal

private const val VERSION_DELIMITER = '.'

/**
* A [semver](https://semver.org/) version.
*/
internal data class Version(
/**
* The major version.
*/
val major: Int,
/**
* The minor version.
*/
val minor: Int,
/**
* The patch (and prerelease) version.
*/
val patchAndPrerelease: String,
) : Comparable<Version> {
override fun toString() = buildString {
append(major)
append(VERSION_DELIMITER)
append(minor)
append(VERSION_DELIMITER)
append(patchAndPrerelease)
}

override fun compareTo(other: Version): Int {
return compareBy<Version> { it.major }
.thenBy { it.minor }
.thenBy { it.patchAndPrerelease }
.compare(this, other)
}

companion object {
fun parse(version: String): Version {
try {
val versionParts = version.split(VERSION_DELIMITER, limit = 3)
require(versionParts.size == 3)
val (major, minor, patchAndPrerelease) = versionParts
return Version(
major = major.toInt(),
minor = minor.toInt(),
patchAndPrerelease = patchAndPrerelease
)
} catch (e: IllegalArgumentException) {
throw IllegalArgumentException("Invalid version", e)
}
}
}
}

internal object THEOplayerGlobalExt {
/**
* Returns the version of THEOplayer, as a [Version].
*/
val version: Version by lazy { Version.parse(THEOplayerGlobal.getVersion()) }
}
17 changes: 0 additions & 17 deletions ui/src/test/java/com/theoplayer/android/ui/ExampleUnitTest.kt

This file was deleted.

Loading
Loading