-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTrackExts.kt
More file actions
88 lines (79 loc) · 2.88 KB
/
TrackExts.kt
File metadata and controls
88 lines (79 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
}