-
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
Open
adadukin-dolby
wants to merge
25
commits into
develop
Choose a base branch
from
bugfix/OPTI-1528-add-cc-label-if-language-or-label-unknown
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 55f3620
OPTI-1528: add util to conditionally check player version
adadukin-dolby c111205
OPTI-1528: add CEA label formatting util
adadukin-dolby 9c78eaf
OPTI-1528: add a CEA formatting checker with unit tests
adadukin-dolby 9cddc51
OPTI-1528: substitute label with channelNumber for CEA tracks
adadukin-dolby 41b48c3
OPTI-1528: add a DASH stream example with CEA text tracks
adadukin-dolby b8b3d7d
OPTI-1528: check whether the label is null or blank
adadukin-dolby 2aea86a
OPTI-1528: address the review feedback
adadukin-dolby 04d0ef4
Read `Track.channelNumber` using reflection
MattiasBuelens db459c5
Fix indentation
MattiasBuelens 9e0d8c1
Simplify
MattiasBuelens 5f33a33
Tweak checks
MattiasBuelens 933465d
OPTI-1528: return locale full display name in its own locale
adadukin-dolby b7c75ef
OPTI-1528: fix reflection call
adadukin-dolby 124e19f
Add `Version` class
MattiasBuelens 597050f
Cache parsed THEOplayer version
MattiasBuelens cc6717e
Throw if player version cannot be parsed
MattiasBuelens 0de6af2
Rename
MattiasBuelens 8f77f2c
Test the whole version
MattiasBuelens c76d6ac
Improve test titles
MattiasBuelens 6e1bd64
Make `Version` comparable
MattiasBuelens 3800c21
Move to `Compat`
MattiasBuelens d8c6bfd
Use helper class instead of reflection
MattiasBuelens 8c3901b
Link to AndroidX docs
MattiasBuelens a89ddbe
Simplify mocking THEOplayer version
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
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 | ||
| } | ||
| } |
88 changes: 88 additions & 0 deletions
88
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,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
62
ui/src/main/java/com/theoplayer/android/ui/util/VersionUtil.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,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
17
ui/src/test/java/com/theoplayer/android/ui/ExampleUnitTest.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.