Index: Source/core/html/track/AutomaticTrackSelection.cpp |
diff --git a/Source/core/html/track/AutomaticTrackSelection.cpp b/Source/core/html/track/AutomaticTrackSelection.cpp |
index 925a3bea2e6ab542bdcb36d668a97a59d13ab080..e912e671d65c36b796f9c63981a45ebff07882bc 100644 |
--- a/Source/core/html/track/AutomaticTrackSelection.cpp |
+++ b/Source/core/html/track/AutomaticTrackSelection.cpp |
@@ -15,7 +15,8 @@ class TrackGroup { |
STACK_ALLOCATED(); |
public: |
enum GroupKind { |
- CaptionsAndSubtitles, |
+ Captions, |
+ Subtitles, |
Description, |
Chapter, |
Metadata |
@@ -60,18 +61,21 @@ static int textTrackSelectionScore(const TextTrack& track) |
AutomaticTrackSelection::AutomaticTrackSelection(const Configuration& configuration) |
: m_configuration(configuration) |
{ |
+ fallbackCaptionOrSubtitleTrack = nullptr; |
fs
2015/06/23 09:23:58
Do this in the initializer list instead.
srivats
2015/06/24 01:40:19
Done.
|
} |
-void AutomaticTrackSelection::performAutomaticTextTrackSelection(const TrackGroup& group) |
+RefPtrWillBeRawPtr<TextTrack> AutomaticTrackSelection::performAutomaticTextTrackSelection(const TrackGroup& group) |
{ |
ASSERT(group.tracks.size()); |
// First, find the track in the group that should be enabled (if any). |
WillBeHeapVector<RefPtrWillBeMember<TextTrack>> currentlyEnabledTracks; |
- RefPtrWillBeRawPtr<TextTrack> trackToEnable = nullptr; |
+ RefPtrWillBeRawPtr<TextTrack> preferredTrack = nullptr; |
RefPtrWillBeRawPtr<TextTrack> defaultTrack = nullptr; |
- RefPtrWillBeRawPtr<TextTrack> fallbackTrack = nullptr; |
+ |
int highestTrackScore = 0; |
+ int highestDefaultTrackScore = 0; |
+ |
for (size_t i = 0; i < group.tracks.size(); ++i) { |
RefPtrWillBeRawPtr<TextTrack> textTrack = group.tracks[i]; |
@@ -80,24 +84,22 @@ void AutomaticTrackSelection::performAutomaticTextTrackSelection(const TrackGrou |
int trackScore = textTrackSelectionScore(*textTrack); |
if (trackScore) { |
- // * If the text track kind is { [subtitles or captions] [descriptions] } and the user has indicated an interest in having a |
+ // * If the text track kind is subtitles or captions and the user has indicated an interest in having a |
// track with this text track kind, text track language, and text track label enabled, and there is no |
// other text track in the media element's list of text tracks with a text track kind of either subtitles |
// or captions whose text track mode is showing |
- // ... |
- // * If the text track kind is chapters and the text track language is one that the user agent has reason |
- // to believe is appropriate for the user, and there is no other text track in the media element's list of |
- // text tracks with a text track kind of chapters whose text track mode is showing |
// Let the text track mode be showing. |
+ |
if (trackScore > highestTrackScore) { |
+ preferredTrack = textTrack; |
highestTrackScore = trackScore; |
- trackToEnable = textTrack; |
+ fallbackCaptionOrSubtitleTrack = textTrack; |
} |
- |
- if (!defaultTrack && textTrack->isDefault()) |
+ // Keep track of the highest scoring default track |
+ 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.
|
+ highestDefaultTrackScore = trackScore; |
defaultTrack = textTrack; |
- if (!defaultTrack && !fallbackTrack) |
- fallbackTrack = textTrack; |
+ } |
} else if (!group.visibleTrack && !defaultTrack && textTrack->isDefault()) { |
// * If the track element has a default attribute specified, and there is no other text track in the media |
// element's list of text tracks whose text track mode is showing or showing by default |
@@ -106,27 +108,24 @@ void AutomaticTrackSelection::performAutomaticTextTrackSelection(const TrackGrou |
} |
} |
- if (!trackToEnable && defaultTrack) |
- trackToEnable = defaultTrack; |
- |
- // If no track matches the user's preferred language and non was marked 'default', enable the first track |
- // because the user has explicitly stated a preference for this kind of track. |
- if (!fallbackTrack && m_configuration.forceEnableSubtitleOrCaptionTrack && group.kind == TrackGroup::CaptionsAndSubtitles) |
- fallbackTrack = group.tracks[0]; |
- |
- if (!trackToEnable && fallbackTrack) |
- trackToEnable = fallbackTrack; |
+ if (!preferredTrack && defaultTrack) |
+ preferredTrack = defaultTrack; |
if (currentlyEnabledTracks.size()) { |
for (size_t i = 0; i < currentlyEnabledTracks.size(); ++i) { |
RefPtrWillBeRawPtr<TextTrack> textTrack = currentlyEnabledTracks[i]; |
- if (textTrack != trackToEnable) |
- textTrack->setMode(TextTrack::disabledKeyword()); |
+ textTrack->setMode(TextTrack::disabledKeyword()); |
} |
} |
- if (trackToEnable) |
- trackToEnable->setMode(TextTrack::showingKeyword()); |
+ // Set the default track to showing if group kind is not subtitles or captions. |
+ if (defaultTrack && group.kind != TrackGroup::Captions && group.kind != TrackGroup::Subtitles) |
+ defaultTrack->setMode(TextTrack::showingKeyword()); |
+ |
+ if (m_configuration.textTrackKindUserPreference == TextTrackKindUserPreference::Default) |
+ return defaultTrack; |
+ |
+ return preferredTrack; |
} |
void AutomaticTrackSelection::enableDefaultMetadataTextTracks(const TrackGroup& group) |
@@ -150,7 +149,8 @@ void AutomaticTrackSelection::enableDefaultMetadataTextTracks(const TrackGroup& |
void AutomaticTrackSelection::perform(TextTrackList& textTracks) |
{ |
- TrackGroup captionAndSubtitleTracks(TrackGroup::CaptionsAndSubtitles); |
+ TrackGroup captionTracks(TrackGroup::Captions); |
+ TrackGroup subtitleTracks(TrackGroup::Subtitles); |
TrackGroup descriptionTracks(TrackGroup::Description); |
TrackGroup chapterTracks(TrackGroup::Chapter); |
TrackGroup metadataTracks(TrackGroup::Metadata); |
@@ -162,8 +162,10 @@ void AutomaticTrackSelection::perform(TextTrackList& textTracks) |
String kind = textTrack->kind(); |
TrackGroup* currentGroup; |
- if (kind == TextTrack::subtitlesKeyword() || kind == TextTrack::captionsKeyword()) { |
- currentGroup = &captionAndSubtitleTracks; |
+ if (kind == TextTrack::captionsKeyword()) { |
+ currentGroup = &captionTracks; |
+ } else if (kind == TextTrack::subtitlesKeyword()) { |
+ currentGroup = &subtitleTracks; |
} else if (kind == TextTrack::descriptionsKeyword()) { |
currentGroup = &descriptionTracks; |
} else if (kind == TextTrack::chaptersKeyword()) { |
@@ -192,14 +194,61 @@ void AutomaticTrackSelection::perform(TextTrackList& textTracks) |
currentGroup->tracks.append(textTrack); |
} |
- if (captionAndSubtitleTracks.tracks.size()) |
- performAutomaticTextTrackSelection(captionAndSubtitleTracks); |
+ 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.
|
+ RefPtrWillBeRawPtr<TextTrack> preferredCaptionTrack = nullptr; |
+ if (captionTracks.tracks.size()) { |
+ preferredCaptionTrack = performAutomaticTextTrackSelection(captionTracks); |
+ 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.
|
+ fallbackCaptionOrSubtitleTrack = captionTracks.tracks[0]; |
+ } |
+ if (subtitleTracks.tracks.size()) { |
+ preferredSubtitleTrack = performAutomaticTextTrackSelection(subtitleTracks); |
+ if (!fallbackCaptionOrSubtitleTrack) |
+ fallbackCaptionOrSubtitleTrack = subtitleTracks.tracks[0]; |
+ } |
if (descriptionTracks.tracks.size()) |
performAutomaticTextTrackSelection(descriptionTracks); |
if (chapterTracks.tracks.size()) |
performAutomaticTextTrackSelection(chapterTracks); |
if (metadataTracks.tracks.size()) |
enableDefaultMetadataTextTracks(metadataTracks); |
+ |
+ RefPtrWillBeRawPtr<TextTrack> trackToEnable = |
+ enableTrackBasedOnUserPreference(preferredCaptionTrack, preferredSubtitleTrack); |
+ if (m_configuration.forceEnableSubtitleOrCaptionTrack && !trackToEnable && fallbackCaptionOrSubtitleTrack) |
+ trackToEnable = fallbackCaptionOrSubtitleTrack; |
+ |
+ if (trackToEnable) |
+ trackToEnable->setMode(TextTrack::showingKeyword()); |
+} |
+ |
+RefPtrWillBeRawPtr<TextTrack> AutomaticTrackSelection::enableTrackBasedOnUserPreference( |
+ RefPtrWillBeRawPtr<TextTrack> preferredCaptionTrack, RefPtrWillBeRawPtr<TextTrack> preferredSubtitleTrack) |
+{ |
+ RefPtrWillBeRawPtr<TextTrack> trackToEnable = nullptr; |
+ switch (m_configuration.textTrackKindUserPreference) { |
+ case TextTrackKindUserPreference::Subtitles: |
+ if (preferredSubtitleTrack) |
+ trackToEnable = preferredSubtitleTrack; |
+ break; |
+ case TextTrackKindUserPreference::Default: |
+ case TextTrackKindUserPreference::Captions: |
+ // When the user prefers captions, display a caption track if present in user's preferred language. |
+ // If no captions track exists in the user's preferred language or a subtitle track |
+ // exists in a language with a higher score, display the subtitle track. |
+ int captionTrackScore = 0; |
+ int subtitleTrackScore = 0; |
+ if (preferredCaptionTrack) |
+ captionTrackScore = textTrackSelectionScore(*preferredCaptionTrack); |
+ if (preferredSubtitleTrack) |
+ subtitleTrackScore = textTrackSelectionScore(*preferredSubtitleTrack); |
+ if (captionTrackScore >= subtitleTrackScore && preferredCaptionTrack) |
+ trackToEnable = preferredCaptionTrack; |
+ else if (preferredSubtitleTrack) |
+ trackToEnable = preferredSubtitleTrack; |
+ break; |
+ } |
+ return trackToEnable; |
} |
} // namespace blink |