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..86cf58275bc8a2f1b07c4e2df2df3fad7669177d 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 |
@@ -59,19 +60,21 @@ static int textTrackSelectionScore(const TextTrack& track) |
AutomaticTrackSelection::AutomaticTrackSelection(const Configuration& configuration) |
: m_configuration(configuration) |
+ , m_fallbackCaptionOrSubtitleTrack(nullptr) |
{ |
} |
-void AutomaticTrackSelection::performAutomaticTextTrackSelection(const TrackGroup& group) |
+PassRefPtrWillBeRawPtr<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; |
+ |
for (size_t i = 0; i < group.tracks.size(); ++i) { |
RefPtrWillBeRawPtr<TextTrack> textTrack = group.tracks[i]; |
@@ -80,24 +83,19 @@ 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; |
+ m_fallbackCaptionOrSubtitleTrack = textTrack; |
} |
- |
- if (!defaultTrack && textTrack->isDefault()) |
+ if (textTrack->isDefault() && !defaultTrack) |
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 +104,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 +145,8 @@ void AutomaticTrackSelection::enableDefaultMetadataTextTracks(const TrackGroup& |
void AutomaticTrackSelection::perform(TextTrackList& textTracks) |
{ |
- TrackGroup captionAndSubtitleTracks(TrackGroup::CaptionsAndSubtitles); |
philipj_slow
2015/06/24 09:54:20
I see that you have discussed this with fs, but I
fs
2015/06/24 10:23:26
FWIW, this is one of the "further improvements" th
philipj_slow
2015/06/24 10:26:16
That sounds good if it's possible. srivats@, did y
|
+ TrackGroup captionTracks(TrackGroup::Captions); |
+ TrackGroup subtitleTracks(TrackGroup::Subtitles); |
TrackGroup descriptionTracks(TrackGroup::Description); |
TrackGroup chapterTracks(TrackGroup::Chapter); |
TrackGroup metadataTracks(TrackGroup::Metadata); |
@@ -162,8 +158,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 +190,62 @@ void AutomaticTrackSelection::perform(TextTrackList& textTracks) |
currentGroup->tracks.append(textTrack); |
} |
- if (captionAndSubtitleTracks.tracks.size()) |
- performAutomaticTextTrackSelection(captionAndSubtitleTracks); |
+ RefPtrWillBeRawPtr<TextTrack> preferredCaptionTrack = nullptr; |
+ if (captionTracks.tracks.size()) |
+ preferredCaptionTrack = performAutomaticTextTrackSelection(captionTracks); |
+ |
+ RefPtrWillBeRawPtr<TextTrack> preferredSubtitleTrack = nullptr; |
+ if (subtitleTracks.tracks.size()) |
+ preferredSubtitleTrack = performAutomaticTextTrackSelection(subtitleTracks); |
+ |
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) { |
+ trackToEnable = m_fallbackCaptionOrSubtitleTrack; |
+ if (captionTracks.tracks.size() && !trackToEnable) |
+ trackToEnable = captionTracks.tracks[0]; |
+ else if (subtitleTracks.tracks.size() && !trackToEnable) |
+ trackToEnable = subtitleTracks.tracks[0]; |
+ } |
+ |
+ if (trackToEnable) |
+ trackToEnable->setMode(TextTrack::showingKeyword()); |
+} |
+ |
+PassRefPtrWillBeRawPtr<TextTrack> AutomaticTrackSelection::enableTrackBasedOnUserPreference( |
+ PassRefPtrWillBeRawPtr<TextTrack> preferredCaptionTrack, PassRefPtrWillBeRawPtr<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 |