-
Notifications
You must be signed in to change notification settings - Fork 2
OPTI-1528: handle CC prefix for CEA text tracks on Player API below 11th release
#84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
adadukin-dolby
merged 37 commits into
develop
from
bugfix/OPTI-1528-add-cc-label-if-language-or-label-unknown
Mar 30, 2026
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
91d3c18
OPTI-1528: extract language localisation logic into a separate file, …
adadukin-dolby 39120bb
OPTI-1528: add util to conditionally check player version
adadukin-dolby c06453b
OPTI-1528: add CEA label formatting util
adadukin-dolby 2905302
OPTI-1528: add a CEA formatting checker with unit tests
adadukin-dolby 4c9b896
OPTI-1528: substitute label with channelNumber for CEA tracks
adadukin-dolby 8f724f7
OPTI-1528: add a DASH stream example with CEA text tracks
adadukin-dolby 96b4176
OPTI-1528: check whether the label is null or blank
adadukin-dolby db63be8
OPTI-1528: address the review feedback
adadukin-dolby ec229de
Read `Track.channelNumber` using reflection
MattiasBuelens 8f5d777
Fix indentation
MattiasBuelens b6a03dc
Simplify
MattiasBuelens 6a74318
Tweak checks
MattiasBuelens 5b9fe03
OPTI-1528: return locale full display name in its own locale
adadukin-dolby 9940a86
OPTI-1528: fix reflection call
adadukin-dolby e35cec6
Add `Version` class
MattiasBuelens e61f50f
Cache parsed THEOplayer version
MattiasBuelens ba3da70
Throw if player version cannot be parsed
MattiasBuelens 1dc7466
Rename
MattiasBuelens 1fdfcfc
Test the whole version
MattiasBuelens 63bba78
Improve test titles
MattiasBuelens 6502447
Make `Version` comparable
MattiasBuelens 785cb6d
Move to `Compat`
MattiasBuelens 834a608
Use helper class instead of reflection
MattiasBuelens 1fa3e79
Link to AndroidX docs
MattiasBuelens 9316f4e
Simplify mocking THEOplayer version
MattiasBuelens c07dbb2
Require THEOplayer 10.13.0 for compiling
MattiasBuelens 88121b4
Prefer latest THEOplayer 10.x version
MattiasBuelens 52e5efc
Don't test with null `Locale.getDisplayName()`
MattiasBuelens f601bcb
Rename
MattiasBuelens 475dd6e
Fix tests
MattiasBuelens 38a83be
Update changelog
MattiasBuelens 2a818cf
Tweak
MattiasBuelens d575f55
Add overloads for MediaTrack and TextTrack
MattiasBuelens d08ac12
Rename
MattiasBuelens 010c53f
Simplify
MattiasBuelens ba3170e
Rename back to formatTrackLabel
MattiasBuelens 2c389a2
Tweak changelog
MattiasBuelens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
ui/src/main/java/com/theoplayer/android/ui/util/CeaUtil.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
adadukin-dolby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * 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}" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } |
98 changes: 98 additions & 0 deletions
98
ui/src/main/java/com/theoplayer/android/ui/util/TrackExts.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| 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.mediatrack.MediaTrack | ||
| 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 = locale.getDisplayName(locale).orEmpty() | ||
| return localisedLanguage.takeUnless { it.isBlank() } | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a label for the given [MediaTrack] instance. | ||
| * | ||
| * This returns the first non-empty entry from the list: | ||
| * 1. Track label | ||
| * 2. Track language display name | ||
| */ | ||
| internal fun constructLabel(track: MediaTrack<*>): String? { | ||
| return track.label?.takeUnless { it.isBlank() } | ||
| ?: track.localizedLanguageName | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a label for the given [TextTrack] 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: TextTrack): String? { | ||
| val label: String? = if ( | ||
| 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.type == TextTrackType.CEA608) { | ||
| track.captionChannelCompat | ||
| ?.let { getLabelForChannelNumber(it) } | ||
| ?.let { return it } | ||
|
|
||
| track.label | ||
| ?.takeUnless { it.isBlank() } | ||
| ?.let { return it } | ||
| } | ||
|
|
||
| return null | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.