Stop initialization if the browser natively supports HLS
Check the video element for HLS mime-type support and make the plugin a no-op if the browser already has built-in support. Also, if the HLs plugin is initialized before the resource selection algorithm has run on the video element and currentSrc is not set, check for a src attribute on the tech.
Showing
2 changed files
with
40 additions
and
3 deletions
... | @@ -8,7 +8,18 @@ | ... | @@ -8,7 +8,18 @@ |
8 | 8 | ||
9 | (function(window, videojs, document, undefined) { | 9 | (function(window, videojs, document, undefined) { |
10 | 10 | ||
11 | videojs.hls = {}; | 11 | videojs.hls = { |
12 | /** | ||
13 | * Whether the browser has built-in HLS support. | ||
14 | */ | ||
15 | supportsNativeHls: (function() { | ||
16 | var | ||
17 | video = document.createElement('video'), | ||
18 | xMpegUrl = video.canPlayType('application/x-mpegURL'), | ||
19 | vndMpeg = video.canPlayType('application/vnd.apple.mpegURL'); | ||
20 | return /probably|maybe/.test(xMpegUrl) || /probably|maybe/.test(vndMpeg); | ||
21 | })() | ||
22 | }; | ||
12 | 23 | ||
13 | var | 24 | var |
14 | // the desired length of video to maintain in the buffer, in seconds | 25 | // the desired length of video to maintain in the buffer, in seconds |
... | @@ -125,6 +136,7 @@ var | ... | @@ -125,6 +136,7 @@ var |
125 | mediaSource = new videojs.MediaSource(), | 136 | mediaSource = new videojs.MediaSource(), |
126 | segmentParser = new videojs.hls.SegmentParser(), | 137 | segmentParser = new videojs.hls.SegmentParser(), |
127 | player = this, | 138 | player = this, |
139 | currentSrc, | ||
128 | extname, | 140 | extname, |
129 | srcUrl, | 141 | srcUrl, |
130 | 142 | ||
... | @@ -132,14 +144,27 @@ var | ... | @@ -132,14 +144,27 @@ var |
132 | downloadPlaylist, | 144 | downloadPlaylist, |
133 | fillBuffer; | 145 | fillBuffer; |
134 | 146 | ||
135 | extname = (/[^#?]*(?:\/[^#?]*\.([^#?]*))/).exec(player.currentSrc()); | 147 | // if the video element supports HLS natively, do nothing |
148 | if (videojs.hls.supportsNativeHls) { | ||
149 | return; | ||
150 | } | ||
151 | |||
152 | currentSrc = player.currentSrc(); | ||
153 | // when the video element is initializing, currentSrc may be undefined | ||
154 | // grab the src from the video element because video.js doesn't currently | ||
155 | // expose it | ||
156 | if (!currentSrc) { | ||
157 | currentSrc = player.el().querySelector('.vjs-tech').src; | ||
158 | } | ||
159 | |||
160 | extname = (/[^#?]*(?:\/[^#?]*\.([^#?]*))/).exec(currentSrc); | ||
136 | if (typeof options === 'string') { | 161 | if (typeof options === 'string') { |
137 | srcUrl = options; | 162 | srcUrl = options; |
138 | } else if (options) { | 163 | } else if (options) { |
139 | srcUrl = options.url; | 164 | srcUrl = options.url; |
140 | } else if (extname && extname[1] === 'm3u8') { | 165 | } else if (extname && extname[1] === 'm3u8') { |
141 | // if the currentSrc looks like an m3u8, attempt to use it | 166 | // if the currentSrc looks like an m3u8, attempt to use it |
142 | srcUrl = player.currentSrc(); | 167 | srcUrl = currentSrc; |
143 | } else { | 168 | } else { |
144 | // do nothing until the plugin is initialized with a valid URL | 169 | // do nothing until the plugin is initialized with a valid URL |
145 | videojs.log('hls: no valid playlist URL specified'); | 170 | videojs.log('hls: no valid playlist URL specified'); | ... | ... |
... | @@ -605,6 +605,18 @@ test('segment 500 should trigger MEDIA_ERR_ABORTED', function () { | ... | @@ -605,6 +605,18 @@ test('segment 500 should trigger MEDIA_ERR_ABORTED', function () { |
605 | equal(4, player.hls.error.code, 'Player error code should be set to MediaError.MEDIA_ERR_ABORTED'); | 605 | equal(4, player.hls.error.code, 'Player error code should be set to MediaError.MEDIA_ERR_ABORTED'); |
606 | }); | 606 | }); |
607 | 607 | ||
608 | test('has no effect if native HLS is available', function() { | ||
609 | var supported = videojs.hls.supportsNativeHls; | ||
610 | videojs.hls.supportsNativeHls = true; | ||
611 | player.hls('manifest/master.m3u8'); | ||
612 | |||
613 | ok(!(player.currentSrc() in videojs.mediaSources), | ||
614 | 'no media source was opened'); | ||
615 | |||
616 | // clean up | ||
617 | videojs.hls.supportsNativeHls = supported; | ||
618 | }); | ||
619 | |||
608 | module('segment controller', { | 620 | module('segment controller', { |
609 | setup: function() { | 621 | setup: function() { |
610 | segmentController = new window.videojs.hls.SegmentController(); | 622 | segmentController = new window.videojs.hls.SegmentController(); | ... | ... |
-
Please register or sign in to post a comment