f50583c9 by David LaPalomento

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.
1 parent b75d3f37
......@@ -8,7 +8,18 @@
(function(window, videojs, document, undefined) {
videojs.hls = {};
videojs.hls = {
/**
* Whether the browser has built-in HLS support.
*/
supportsNativeHls: (function() {
var
video = document.createElement('video'),
xMpegUrl = video.canPlayType('application/x-mpegURL'),
vndMpeg = video.canPlayType('application/vnd.apple.mpegURL');
return /probably|maybe/.test(xMpegUrl) || /probably|maybe/.test(vndMpeg);
})()
};
var
// the desired length of video to maintain in the buffer, in seconds
......@@ -125,6 +136,7 @@ var
mediaSource = new videojs.MediaSource(),
segmentParser = new videojs.hls.SegmentParser(),
player = this,
currentSrc,
extname,
srcUrl,
......@@ -132,14 +144,27 @@ var
downloadPlaylist,
fillBuffer;
extname = (/[^#?]*(?:\/[^#?]*\.([^#?]*))/).exec(player.currentSrc());
// if the video element supports HLS natively, do nothing
if (videojs.hls.supportsNativeHls) {
return;
}
currentSrc = player.currentSrc();
// when the video element is initializing, currentSrc may be undefined
// grab the src from the video element because video.js doesn't currently
// expose it
if (!currentSrc) {
currentSrc = player.el().querySelector('.vjs-tech').src;
}
extname = (/[^#?]*(?:\/[^#?]*\.([^#?]*))/).exec(currentSrc);
if (typeof options === 'string') {
srcUrl = options;
} else if (options) {
srcUrl = options.url;
} else if (extname && extname[1] === 'm3u8') {
// if the currentSrc looks like an m3u8, attempt to use it
srcUrl = player.currentSrc();
srcUrl = currentSrc;
} else {
// do nothing until the plugin is initialized with a valid URL
videojs.log('hls: no valid playlist URL specified');
......
......@@ -605,6 +605,18 @@ test('segment 500 should trigger MEDIA_ERR_ABORTED', function () {
equal(4, player.hls.error.code, 'Player error code should be set to MediaError.MEDIA_ERR_ABORTED');
});
test('has no effect if native HLS is available', function() {
var supported = videojs.hls.supportsNativeHls;
videojs.hls.supportsNativeHls = true;
player.hls('manifest/master.m3u8');
ok(!(player.currentSrc() in videojs.mediaSources),
'no media source was opened');
// clean up
videojs.hls.supportsNativeHls = supported;
});
module('segment controller', {
setup: function() {
segmentController = new window.videojs.hls.SegmentController();
......