From ff83aa6573890ccc193ee489cbb58b378bae6f8c Mon Sep 17 00:00:00 2001 From: Gary Katsevman Date: Tue, 19 Apr 2016 15:18:21 -0400 Subject: [PATCH] @gkatsev made the first emulated text track enabled by default. closes #3248 --- CHANGELOG.md | 1 + src/js/tracks/text-track-display.js | 28 +++++++++++++++ src/js/tracks/text-track.js | 8 +++++ test/unit/tracks/tracks.test.js | 54 +++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index da558ca4d..1eadd2a39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG ## HEAD (Unreleased) * @benjipott updated IS_CHROME to not be true on MS Edge ([view](https://github.com/videojs/video.js/pull/3232)) * @mister-ben blacklisted Chrome for Android for playback rate support ([view](https://github.com/videojs/video.js/pull/3246)) +* @gkatsev made the first emulated text track enabled by default ([view](https://github.com/videojs/video.js/pull/3248)) -------------------- diff --git a/src/js/tracks/text-track-display.js b/src/js/tracks/text-track-display.js index bee096b6e..40ca875b3 100644 --- a/src/js/tracks/text-track-display.js +++ b/src/js/tracks/text-track-display.js @@ -58,6 +58,34 @@ class TextTrackDisplay extends Component { let track = tracks[i]; this.player_.addRemoteTextTrack(track); } + + let modes = {'captions': 1, 'subtitles': 1}; + let trackList = this.player_.textTracks(); + let firstDesc; + let firstCaptions; + + if (trackList) { + for (let i = 0; i < trackList.length; i++) { + let track = trackList[i]; + if (track.default) { + if (track.kind === 'descriptions' && !firstDesc) { + firstDesc = track; + } else if (track.kind in modes && !firstCaptions) { + firstCaptions = track; + } + } + } + + // We want to show the first default track but captions and subtitles + // take precedence over descriptions. + // So, display the first default captions or subtitles track + // and otherwise the first default descriptions track. + if (firstCaptions) { + firstCaptions.mode = 'showing'; + } else if (firstDesc) { + firstDesc.mode = 'showing'; + } + } })); } diff --git a/src/js/tracks/text-track.js b/src/js/tracks/text-track.js index c08fe9074..1b510dc60 100644 --- a/src/js/tracks/text-track.js +++ b/src/js/tracks/text-track.js @@ -136,6 +136,7 @@ class TextTrack extends EventTarget { let mode = TextTrackEnum.TextTrackMode[options.mode] || 'disabled'; let kind = TextTrackEnum.TextTrackKind[options.kind] || 'subtitles'; + let default_ = options.default; let label = options.label || ''; let language = options.language || options.srclang || ''; let id = options.id || 'vjs_text_track_' + Guid.newGUID(); @@ -190,6 +191,13 @@ class TextTrack extends EventTarget { set() {} }); + Object.defineProperty(tt, 'default', { + get() { + return default_; + }, + set() {} + }); + Object.defineProperty(tt, 'mode', { get() { return mode; diff --git a/test/unit/tracks/tracks.test.js b/test/unit/tracks/tracks.test.js index db1678bc5..4a92e6bc2 100644 --- a/test/unit/tracks/tracks.test.js +++ b/test/unit/tracks/tracks.test.js @@ -475,3 +475,57 @@ test('should uniformly create html track element when adding text track', functi player.dispose(); }); + +test('default text tracks should show by default', function() { + let tag = TestHelpers.makeTag(); + let capt = document.createElement('track'); + + capt.kind = 'captions'; + capt.setAttribute('default', 'default'); + + tag.appendChild(capt); + + let player = TestHelpers.makePlayer({ + html5: { + nativeTextTracks: false + } + }, tag); + + // native tracks are initialized after the player is ready + this.clock.tick(1); + + let tracks = player.textTracks(); + + equal(tracks[0].kind, 'captions', 'the captions track is present'); + equal(tracks[0].mode, 'showing', 'the captions track is showing'); +}); + +test('default captions take precedence over default descriptions', function() { + let tag = TestHelpers.makeTag(); + let desc = document.createElement('track'); + let capt = document.createElement('track'); + + desc.kind = 'descriptions'; + desc.setAttribute('default', 'default'); + capt.kind = 'captions'; + capt.setAttribute('default', 'default'); + + tag.appendChild(desc); + tag.appendChild(capt); + + let player = TestHelpers.makePlayer({ + html5: { + nativeTextTracks: false + } + }, tag); + + // native tracks are initialized after the player is ready + this.clock.tick(1); + + let tracks = player.textTracks(); + + equal(tracks[0].kind, 'descriptions', 'the descriptions track is first'); + equal(tracks[0].mode, 'disabled', 'the descriptions track is disabled'); + equal(tracks[1].kind, 'captions', 'the captions track is second'); + equal(tracks[1].mode, 'showing', 'the captions track is showing'); +});