OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "core/html/track/AutomaticTrackSelection.h" | 6 #include "core/html/track/AutomaticTrackSelection.h" |
7 | 7 |
8 #include "core/html/track/TextTrack.h" | 8 #include "core/html/track/TextTrack.h" |
9 #include "core/html/track/TextTrackList.h" | 9 #include "core/html/track/TextTrackList.h" |
10 #include "platform/Language.h" | 10 #include "platform/Language.h" |
11 | 11 |
12 namespace blink { | 12 namespace blink { |
13 | 13 |
14 class TrackGroup { | 14 class TrackGroup { |
15 STACK_ALLOCATED(); | 15 STACK_ALLOCATED(); |
16 public: | 16 public: |
17 enum GroupKind { | 17 enum GroupKind { |
18 CaptionsAndSubtitles, | 18 Captions, |
19 Subtitles, | |
19 Description, | 20 Description, |
20 Chapter, | 21 Chapter, |
21 Metadata | 22 Metadata |
22 }; | 23 }; |
23 | 24 |
24 explicit TrackGroup(GroupKind kind) | 25 explicit TrackGroup(GroupKind kind) |
25 : visibleTrack(nullptr) | 26 : visibleTrack(nullptr) |
26 , defaultTrack(nullptr) | 27 , defaultTrack(nullptr) |
27 , kind(kind) | 28 , kind(kind) |
28 , hasSrcLang(false) | 29 , hasSrcLang(false) |
(...skipping 24 matching lines...) Expand all Loading... | |
53 { | 54 { |
54 if (track.kind() != TextTrack::captionsKeyword() && track.kind() != TextTrac k::subtitlesKeyword()) | 55 if (track.kind() != TextTrack::captionsKeyword() && track.kind() != TextTrac k::subtitlesKeyword()) |
55 return 0; | 56 return 0; |
56 | 57 |
57 return textTrackLanguageSelectionScore(track); | 58 return textTrackLanguageSelectionScore(track); |
58 } | 59 } |
59 | 60 |
60 AutomaticTrackSelection::AutomaticTrackSelection(const Configuration& configurat ion) | 61 AutomaticTrackSelection::AutomaticTrackSelection(const Configuration& configurat ion) |
61 : m_configuration(configuration) | 62 : m_configuration(configuration) |
62 { | 63 { |
64 fallbackCaptionOrSubtitleTrack = nullptr; | |
fs
2015/06/23 09:23:58
Do this in the initializer list instead.
srivats
2015/06/24 01:40:19
Done.
| |
63 } | 65 } |
64 | 66 |
65 void AutomaticTrackSelection::performAutomaticTextTrackSelection(const TrackGrou p& group) | 67 RefPtrWillBeRawPtr<TextTrack> AutomaticTrackSelection::performAutomaticTextTrack Selection(const TrackGroup& group) |
66 { | 68 { |
67 ASSERT(group.tracks.size()); | 69 ASSERT(group.tracks.size()); |
68 | 70 |
69 // First, find the track in the group that should be enabled (if any). | 71 // First, find the track in the group that should be enabled (if any). |
70 WillBeHeapVector<RefPtrWillBeMember<TextTrack>> currentlyEnabledTracks; | 72 WillBeHeapVector<RefPtrWillBeMember<TextTrack>> currentlyEnabledTracks; |
71 RefPtrWillBeRawPtr<TextTrack> trackToEnable = nullptr; | 73 RefPtrWillBeRawPtr<TextTrack> preferredTrack = nullptr; |
72 RefPtrWillBeRawPtr<TextTrack> defaultTrack = nullptr; | 74 RefPtrWillBeRawPtr<TextTrack> defaultTrack = nullptr; |
73 RefPtrWillBeRawPtr<TextTrack> fallbackTrack = nullptr; | 75 |
74 int highestTrackScore = 0; | 76 int highestTrackScore = 0; |
77 int highestDefaultTrackScore = 0; | |
78 | |
75 for (size_t i = 0; i < group.tracks.size(); ++i) { | 79 for (size_t i = 0; i < group.tracks.size(); ++i) { |
76 RefPtrWillBeRawPtr<TextTrack> textTrack = group.tracks[i]; | 80 RefPtrWillBeRawPtr<TextTrack> textTrack = group.tracks[i]; |
77 | 81 |
78 if (m_configuration.disableCurrentlyEnabledTracks && textTrack->mode() = = TextTrack::showingKeyword()) | 82 if (m_configuration.disableCurrentlyEnabledTracks && textTrack->mode() = = TextTrack::showingKeyword()) |
79 currentlyEnabledTracks.append(textTrack); | 83 currentlyEnabledTracks.append(textTrack); |
80 | 84 |
81 int trackScore = textTrackSelectionScore(*textTrack); | 85 int trackScore = textTrackSelectionScore(*textTrack); |
82 if (trackScore) { | 86 if (trackScore) { |
83 // * If the text track kind is { [subtitles or captions] [descriptio ns] } and the user has indicated an interest in having a | 87 // * If the text track kind is subtitles or captions and the user ha s indicated an interest in having a |
84 // track with this text track kind, text track language, and text tr ack label enabled, and there is no | 88 // track with this text track kind, text track language, and text tr ack label enabled, and there is no |
85 // other text track in the media element's list of text tracks with a text track kind of either subtitles | 89 // other text track in the media element's list of text tracks with a text track kind of either subtitles |
86 // or captions whose text track mode is showing | 90 // or captions whose text track mode is showing |
87 // ... | |
88 // * If the text track kind is chapters and the text track language is one that the user agent has reason | |
89 // to believe is appropriate for the user, and there is no other tex t track in the media element's list of | |
90 // text tracks with a text track kind of chapters whose text track m ode is showing | |
91 // Let the text track mode be showing. | 91 // Let the text track mode be showing. |
92 | |
92 if (trackScore > highestTrackScore) { | 93 if (trackScore > highestTrackScore) { |
94 preferredTrack = textTrack; | |
93 highestTrackScore = trackScore; | 95 highestTrackScore = trackScore; |
94 trackToEnable = textTrack; | 96 fallbackCaptionOrSubtitleTrack = textTrack; |
95 } | 97 } |
96 | 98 // Keep track of the highest scoring default track |
97 if (!defaultTrack && textTrack->isDefault()) | 99 if (trackScore > highestDefaultTrackScore && textTrack->isDefault()) { |
fs
2015/06/23 09:23:58
I don't think there's a need to track score for 'd
srivats
2015/06/24 01:40:19
Done.
| |
100 highestDefaultTrackScore = trackScore; | |
98 defaultTrack = textTrack; | 101 defaultTrack = textTrack; |
99 if (!defaultTrack && !fallbackTrack) | 102 } |
100 fallbackTrack = textTrack; | |
101 } else if (!group.visibleTrack && !defaultTrack && textTrack->isDefault( )) { | 103 } else if (!group.visibleTrack && !defaultTrack && textTrack->isDefault( )) { |
102 // * If the track element has a default attribute specified, and the re is no other text track in the media | 104 // * If the track element has a default attribute specified, and the re is no other text track in the media |
103 // element's list of text tracks whose text track mode is showing or showing by default | 105 // element's list of text tracks whose text track mode is showing or showing by default |
104 // Let the text track mode be showing by default. | 106 // Let the text track mode be showing by default. |
105 defaultTrack = textTrack; | 107 defaultTrack = textTrack; |
106 } | 108 } |
107 } | 109 } |
108 | 110 |
109 if (!trackToEnable && defaultTrack) | 111 if (!preferredTrack && defaultTrack) |
110 trackToEnable = defaultTrack; | 112 preferredTrack = defaultTrack; |
111 | |
112 // If no track matches the user's preferred language and non was marked 'def ault', enable the first track | |
113 // because the user has explicitly stated a preference for this kind of trac k. | |
114 if (!fallbackTrack && m_configuration.forceEnableSubtitleOrCaptionTrack && g roup.kind == TrackGroup::CaptionsAndSubtitles) | |
115 fallbackTrack = group.tracks[0]; | |
116 | |
117 if (!trackToEnable && fallbackTrack) | |
118 trackToEnable = fallbackTrack; | |
119 | 113 |
120 if (currentlyEnabledTracks.size()) { | 114 if (currentlyEnabledTracks.size()) { |
121 for (size_t i = 0; i < currentlyEnabledTracks.size(); ++i) { | 115 for (size_t i = 0; i < currentlyEnabledTracks.size(); ++i) { |
122 RefPtrWillBeRawPtr<TextTrack> textTrack = currentlyEnabledTracks[i]; | 116 RefPtrWillBeRawPtr<TextTrack> textTrack = currentlyEnabledTracks[i]; |
123 if (textTrack != trackToEnable) | 117 textTrack->setMode(TextTrack::disabledKeyword()); |
124 textTrack->setMode(TextTrack::disabledKeyword()); | |
125 } | 118 } |
126 } | 119 } |
127 | 120 |
128 if (trackToEnable) | 121 // Set the default track to showing if group kind is not subtitles or captio ns. |
129 trackToEnable->setMode(TextTrack::showingKeyword()); | 122 if (defaultTrack && group.kind != TrackGroup::Captions && group.kind != Trac kGroup::Subtitles) |
123 defaultTrack->setMode(TextTrack::showingKeyword()); | |
124 | |
125 if (m_configuration.textTrackKindUserPreference == TextTrackKindUserPreferen ce::Default) | |
126 return defaultTrack; | |
127 | |
128 return preferredTrack; | |
130 } | 129 } |
131 | 130 |
132 void AutomaticTrackSelection::enableDefaultMetadataTextTracks(const TrackGroup& group) | 131 void AutomaticTrackSelection::enableDefaultMetadataTextTracks(const TrackGroup& group) |
133 { | 132 { |
134 ASSERT(group.tracks.size()); | 133 ASSERT(group.tracks.size()); |
135 | 134 |
136 // https://html.spec.whatwg.org/multipage/embedded-content.html#honor-user-p references-for-automatic-text-track-selection | 135 // https://html.spec.whatwg.org/multipage/embedded-content.html#honor-user-p references-for-automatic-text-track-selection |
137 | 136 |
138 // 4. If there are any text tracks in the media element's list of text | 137 // 4. If there are any text tracks in the media element's list of text |
139 // tracks whose text track kind is metadata that correspond to track | 138 // tracks whose text track kind is metadata that correspond to track |
140 // elements with a default attribute set whose text track mode is set to | 139 // elements with a default attribute set whose text track mode is set to |
141 // disabled, then set the text track mode of all such tracks to hidden | 140 // disabled, then set the text track mode of all such tracks to hidden |
142 for (auto& textTrack : group.tracks) { | 141 for (auto& textTrack : group.tracks) { |
143 if (textTrack->mode() != TextTrack::disabledKeyword()) | 142 if (textTrack->mode() != TextTrack::disabledKeyword()) |
144 continue; | 143 continue; |
145 if (!textTrack->isDefault()) | 144 if (!textTrack->isDefault()) |
146 continue; | 145 continue; |
147 textTrack->setMode(TextTrack::hiddenKeyword()); | 146 textTrack->setMode(TextTrack::hiddenKeyword()); |
148 } | 147 } |
149 } | 148 } |
150 | 149 |
151 void AutomaticTrackSelection::perform(TextTrackList& textTracks) | 150 void AutomaticTrackSelection::perform(TextTrackList& textTracks) |
152 { | 151 { |
153 TrackGroup captionAndSubtitleTracks(TrackGroup::CaptionsAndSubtitles); | 152 TrackGroup captionTracks(TrackGroup::Captions); |
153 TrackGroup subtitleTracks(TrackGroup::Subtitles); | |
154 TrackGroup descriptionTracks(TrackGroup::Description); | 154 TrackGroup descriptionTracks(TrackGroup::Description); |
155 TrackGroup chapterTracks(TrackGroup::Chapter); | 155 TrackGroup chapterTracks(TrackGroup::Chapter); |
156 TrackGroup metadataTracks(TrackGroup::Metadata); | 156 TrackGroup metadataTracks(TrackGroup::Metadata); |
157 | 157 |
158 for (size_t i = 0; i < textTracks.length(); ++i) { | 158 for (size_t i = 0; i < textTracks.length(); ++i) { |
159 RefPtrWillBeRawPtr<TextTrack> textTrack = textTracks.item(i); | 159 RefPtrWillBeRawPtr<TextTrack> textTrack = textTracks.item(i); |
160 if (!textTrack) | 160 if (!textTrack) |
161 continue; | 161 continue; |
162 | 162 |
163 String kind = textTrack->kind(); | 163 String kind = textTrack->kind(); |
164 TrackGroup* currentGroup; | 164 TrackGroup* currentGroup; |
165 if (kind == TextTrack::subtitlesKeyword() || kind == TextTrack::captions Keyword()) { | 165 if (kind == TextTrack::captionsKeyword()) { |
166 currentGroup = &captionAndSubtitleTracks; | 166 currentGroup = &captionTracks; |
167 } else if (kind == TextTrack::subtitlesKeyword()) { | |
168 currentGroup = &subtitleTracks; | |
167 } else if (kind == TextTrack::descriptionsKeyword()) { | 169 } else if (kind == TextTrack::descriptionsKeyword()) { |
168 currentGroup = &descriptionTracks; | 170 currentGroup = &descriptionTracks; |
169 } else if (kind == TextTrack::chaptersKeyword()) { | 171 } else if (kind == TextTrack::chaptersKeyword()) { |
170 currentGroup = &chapterTracks; | 172 currentGroup = &chapterTracks; |
171 } else { | 173 } else { |
172 ASSERT(kind == TextTrack::metadataKeyword()); | 174 ASSERT(kind == TextTrack::metadataKeyword()); |
173 currentGroup = &metadataTracks; | 175 currentGroup = &metadataTracks; |
174 } | 176 } |
175 | 177 |
176 if (!currentGroup->visibleTrack && textTrack->mode() == TextTrack::showi ngKeyword()) | 178 if (!currentGroup->visibleTrack && textTrack->mode() == TextTrack::showi ngKeyword()) |
177 currentGroup->visibleTrack = textTrack; | 179 currentGroup->visibleTrack = textTrack; |
178 if (!currentGroup->defaultTrack && textTrack->isDefault()) | 180 if (!currentGroup->defaultTrack && textTrack->isDefault()) |
179 currentGroup->defaultTrack = textTrack; | 181 currentGroup->defaultTrack = textTrack; |
180 | 182 |
181 // Do not add this track to the group if it has already been automatical ly configured | 183 // Do not add this track to the group if it has already been automatical ly configured |
182 // as we only want to perform selection once per track so that adding an other track | 184 // as we only want to perform selection once per track so that adding an other track |
183 // after the initial configuration doesn't reconfigure every track - onl y those that | 185 // after the initial configuration doesn't reconfigure every track - onl y those that |
184 // should be changed by the new addition. For example all metadata track s are | 186 // should be changed by the new addition. For example all metadata track s are |
185 // disabled by default, and we don't want a track that has been enabled by script | 187 // disabled by default, and we don't want a track that has been enabled by script |
186 // to be disabled automatically when a new metadata track is added later . | 188 // to be disabled automatically when a new metadata track is added later . |
187 if (textTrack->hasBeenConfigured()) | 189 if (textTrack->hasBeenConfigured()) |
188 continue; | 190 continue; |
189 | 191 |
190 if (textTrack->language().length()) | 192 if (textTrack->language().length()) |
191 currentGroup->hasSrcLang = true; | 193 currentGroup->hasSrcLang = true; |
192 currentGroup->tracks.append(textTrack); | 194 currentGroup->tracks.append(textTrack); |
193 } | 195 } |
194 | 196 |
195 if (captionAndSubtitleTracks.tracks.size()) | 197 RefPtrWillBeRawPtr<TextTrack> preferredSubtitleTrack = nullptr; |
fs
2015/06/23 09:23:58
Move this down to just before the "if (subtitleTra
srivats
2015/06/24 01:40:19
Done.
| |
196 performAutomaticTextTrackSelection(captionAndSubtitleTracks); | 198 RefPtrWillBeRawPtr<TextTrack> preferredCaptionTrack = nullptr; |
199 if (captionTracks.tracks.size()) { | |
200 preferredCaptionTrack = performAutomaticTextTrackSelection(captionTracks ); | |
201 if (!fallbackCaptionOrSubtitleTrack) | |
fs
2015/06/23 09:23:58
Looks like we could do this when actually needed -
srivats
2015/06/24 01:40:19
Done.
| |
202 fallbackCaptionOrSubtitleTrack = captionTracks.tracks[0]; | |
203 } | |
204 if (subtitleTracks.tracks.size()) { | |
205 preferredSubtitleTrack = performAutomaticTextTrackSelection(subtitleTrac ks); | |
206 if (!fallbackCaptionOrSubtitleTrack) | |
207 fallbackCaptionOrSubtitleTrack = subtitleTracks.tracks[0]; | |
208 } | |
197 if (descriptionTracks.tracks.size()) | 209 if (descriptionTracks.tracks.size()) |
198 performAutomaticTextTrackSelection(descriptionTracks); | 210 performAutomaticTextTrackSelection(descriptionTracks); |
199 if (chapterTracks.tracks.size()) | 211 if (chapterTracks.tracks.size()) |
200 performAutomaticTextTrackSelection(chapterTracks); | 212 performAutomaticTextTrackSelection(chapterTracks); |
201 if (metadataTracks.tracks.size()) | 213 if (metadataTracks.tracks.size()) |
202 enableDefaultMetadataTextTracks(metadataTracks); | 214 enableDefaultMetadataTextTracks(metadataTracks); |
215 | |
216 RefPtrWillBeRawPtr<TextTrack> trackToEnable = | |
217 enableTrackBasedOnUserPreference(preferredCaptionTrack, preferredSubtitl eTrack); | |
218 if (m_configuration.forceEnableSubtitleOrCaptionTrack && !trackToEnable && f allbackCaptionOrSubtitleTrack) | |
219 trackToEnable = fallbackCaptionOrSubtitleTrack; | |
220 | |
221 if (trackToEnable) | |
222 trackToEnable->setMode(TextTrack::showingKeyword()); | |
223 } | |
224 | |
225 RefPtrWillBeRawPtr<TextTrack> AutomaticTrackSelection::enableTrackBasedOnUserPre ference( | |
226 RefPtrWillBeRawPtr<TextTrack> preferredCaptionTrack, RefPtrWillBeRawPtr<Text Track> preferredSubtitleTrack) | |
227 { | |
228 RefPtrWillBeRawPtr<TextTrack> trackToEnable = nullptr; | |
229 switch (m_configuration.textTrackKindUserPreference) { | |
230 case TextTrackKindUserPreference::Subtitles: | |
231 if (preferredSubtitleTrack) | |
232 trackToEnable = preferredSubtitleTrack; | |
233 break; | |
234 case TextTrackKindUserPreference::Default: | |
235 case TextTrackKindUserPreference::Captions: | |
236 // When the user prefers captions, display a caption track if present in user's preferred language. | |
237 // If no captions track exists in the user's preferred language or a sub title track | |
238 // exists in a language with a higher score, display the subtitle track. | |
239 int captionTrackScore = 0; | |
240 int subtitleTrackScore = 0; | |
241 if (preferredCaptionTrack) | |
242 captionTrackScore = textTrackSelectionScore(*preferredCaptionTrack); | |
243 if (preferredSubtitleTrack) | |
244 subtitleTrackScore = textTrackSelectionScore(*preferredSubtitleTrack ); | |
245 if (captionTrackScore >= subtitleTrackScore && preferredCaptionTrack) | |
246 trackToEnable = preferredCaptionTrack; | |
247 else if (preferredSubtitleTrack) | |
248 trackToEnable = preferredSubtitleTrack; | |
249 break; | |
250 } | |
251 return trackToEnable; | |
203 } | 252 } |
204 | 253 |
205 } // namespace blink | 254 } // namespace blink |
OLD | NEW |