-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrackSelectionParameters.kt
178 lines (161 loc) · 5.5 KB
/
TrackSelectionParameters.kt
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.player.extension
import android.content.Context
import android.os.Build
import android.view.accessibility.AccessibilityManager
import androidx.annotation.RequiresApi
import androidx.media3.common.C
import androidx.media3.common.C.TrackType
import androidx.media3.common.TrackGroup
import androidx.media3.common.TrackSelectionOverride
import androidx.media3.common.TrackSelectionParameters
/**
* Is text track disabled
*/
val TrackSelectionParameters.isTextTrackDisabled: Boolean
get() = disabledTrackTypes.contains(C.TRACK_TYPE_TEXT) ||
isForcedTextTrackActive
/**
* Is audio track disabled
*/
val TrackSelectionParameters.isAudioTrackDisabled: Boolean
get() = disabledTrackTypes.contains(C.TRACK_TYPE_AUDIO)
/**
* Get overrides for track type
*
* @param trackType The track type to filter.
* @return
*/
fun TrackSelectionParameters.getOverridesForTrackType(
trackType: @TrackType Int
): Map<TrackGroup, TrackSelectionOverride> {
return overrides.filterKeys { it.type == trackType }
}
/**
* Has track override
*
* @param trackType The track type to filter.
* @return
*/
fun TrackSelectionParameters.hasTrackOverride(trackType: @TrackType Int): Boolean {
if (overrides.isEmpty()) return false
return getOverridesForTrackType(trackType).isNotEmpty()
}
/**
* Disable text track
*
* @return
*/
fun TrackSelectionParameters.disableTextTrack(): TrackSelectionParameters {
return buildUpon()
.clearOverridesOfType(C.TRACK_TYPE_TEXT)
.setPreferredTextRoleFlags(0)
.setPreferredTextLanguage(null)
.setIgnoredTextSelectionFlags(C.SELECTION_FLAG_FORCED.inv())
.build()
}
/**
* Disable audio track
*
* @return
*/
fun TrackSelectionParameters.disableAudioTrack(): TrackSelectionParameters {
return disableTrackType(C.TRACK_TYPE_AUDIO)
}
/**
* Default text track parameters.
*
* @param context The context.
* @return
*/
fun TrackSelectionParameters.defaultTextTrack(context: Context): TrackSelectionParameters {
return buildUpon()
.clearOverridesOfType(C.TRACK_TYPE_TEXT)
.setIgnoredTextSelectionFlags(0)
.setPreferredTextLanguage(null)
.setPreferredTextRoleFlags(0)
.setPreferredTextLanguageAndRoleFlagsToCaptioningManagerSettings(context)
.setTrackTypeDisabled(C.TRACK_TYPE_TEXT, false)
.build()
}
/**
* Default audio track parameters.
*
* Reset [TrackSelectionParameters] for audio as Default.
*
* @param context The context.
* @return
*/
fun TrackSelectionParameters.defaultAudioTrack(context: Context): TrackSelectionParameters {
return buildUpon()
.clearOverridesOfType(C.TRACK_TYPE_AUDIO)
.setPreferredAudioLanguage(null)
.setPreferredAudioMimeType(null)
.setPreferredAudioRoleFlags(0)
.setTrackTypeDisabled(C.TRACK_TYPE_AUDIO, false)
.setPreferredAudioRoleFlagsToAccessibilityManagerSettings(context)
.build()
}
/**
* Set track selection override
*
* Audio track selection override also setups the preferred audio language to handle forced subtitles correctly.
*
* @param override The [TrackSelectionOverride] to apply.
* @return
*/
fun TrackSelectionParameters.setTrackOverride(override: TrackSelectionOverride): TrackSelectionParameters {
val builder = buildUpon()
.setOverrideForType(override)
.setTrackTypeDisabled(override.type, false)
// If audio and has tracks to select then set preferred language if applicable.
return when {
override.type == C.TRACK_TYPE_AUDIO && override.trackIndices.isNotEmpty() -> {
builder.setPreferredAudioLanguage(override.mediaTrackGroup.getFormat(0).language)
builder.build()
}
override.type == C.TRACK_TYPE_TEXT && override.trackIndices.isNotEmpty() -> {
builder.setIgnoredTextSelectionFlags(0)
builder.setPreferredTextLanguage(override.mediaTrackGroup.getFormat(0).language)
builder.setPreferredTextRoleFlags(override.mediaTrackGroup.getFormat(0).roleFlags)
builder.build()
}
else -> {
builder.build()
}
}
}
/**
* Set preferred audio role flags to accessibility manager settings.
*
* Dos nothing for api level < 33 or when audio description request is off.
* @param context The context.
*/
fun TrackSelectionParameters.Builder.setPreferredAudioRoleFlagsToAccessibilityManagerSettings(
context: Context
): TrackSelectionParameters.Builder {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
setPreferredAudioRoleFlagsToAccessibilityManagerSettingsV33(context)
}
return this
}
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
private fun TrackSelectionParameters.Builder.setPreferredAudioRoleFlagsToAccessibilityManagerSettingsV33(
context: Context
) {
val accessibilityManager = context.getSystemService(AccessibilityManager::class.java)
if (accessibilityManager.isAudioDescriptionRequested) {
setPreferredAudioRoleFlags(C.ROLE_FLAG_DESCRIBES_VIDEO or C.ROLE_FLAG_DESCRIBES_MUSIC_AND_SOUND)
}
}
private fun TrackSelectionParameters.disableTrackType(trackType: @TrackType Int): TrackSelectionParameters {
return buildUpon()
.clearOverridesOfType(trackType)
.setTrackTypeDisabled(trackType, true)
.build()
}
private val TrackSelectionParameters.isForcedTextTrackActive: Boolean
get() = ignoredTextSelectionFlags == C.SELECTION_FLAG_FORCED.inv()