9840c6ee by Gaurav Saxena Committed by David LaPalomento

Add canPlayType to Hls source handler

If someone calls player.canPlayType() with HLS registered as a source handler, it would fail at runtime. Related to videojs/video.js#2709.
1 parent 8d4ab4b4
...@@ -95,13 +95,7 @@ videojs.Hls.canPlaySource = function() { ...@@ -95,13 +95,7 @@ videojs.Hls.canPlaySource = function() {
95 videojs.HlsSourceHandler = function(mode) { 95 videojs.HlsSourceHandler = function(mode) {
96 return { 96 return {
97 canHandleSource: function(srcObj) { 97 canHandleSource: function(srcObj) {
98 var mpegurlRE = /^application\/(?:x-|vnd\.apple\.)mpegurl/i; 98 return videojs.HlsSourceHandler.canPlayType(srcObj.type);
99
100 // favor native HLS support if it's available
101 if (videojs.Hls.supportsNativeHls) {
102 return false;
103 }
104 return mpegurlRE.test(srcObj.type);
105 }, 99 },
106 handleSource: function(source, tech) { 100 handleSource: function(source, tech) {
107 if (mode === 'flash') { 101 if (mode === 'flash') {
...@@ -117,10 +111,23 @@ videojs.HlsSourceHandler = function(mode) { ...@@ -117,10 +111,23 @@ videojs.HlsSourceHandler = function(mode) {
117 }); 111 });
118 tech.hls.src(source.src); 112 tech.hls.src(source.src);
119 return tech.hls; 113 return tech.hls;
114 },
115 canPlayType: function(type) {
116 return videojs.HlsSourceHandler.canPlayType(type);
120 } 117 }
121 }; 118 };
122 }; 119 };
123 120
121 videojs.HlsSourceHandler.canPlayType = function(type) {
122 var mpegurlRE = /^application\/(?:x-|vnd\.apple\.)mpegurl/i;
123
124 // favor native HLS support if it's available
125 if (videojs.Hls.supportsNativeHls) {
126 return false;
127 }
128 return mpegurlRE.test(type);
129 };
130
124 // register source handlers with the appropriate techs 131 // register source handlers with the appropriate techs
125 if (videojs.MediaSource.supportsNativeMediaSources()) { 132 if (videojs.MediaSource.supportsNativeMediaSources()) {
126 videojs.getComponent('Html5').registerSourceHandler(videojs.HlsSourceHandler('html5')); 133 videojs.getComponent('Html5').registerSourceHandler(videojs.HlsSourceHandler('html5'));
......
...@@ -1917,6 +1917,8 @@ test('the source handler supports HLS mime types', function() { ...@@ -1917,6 +1917,8 @@ test('the source handler supports HLS mime types', function() {
1917 ok(videojs.HlsSourceHandler(techName).canHandleSource({ 1917 ok(videojs.HlsSourceHandler(techName).canHandleSource({
1918 type: 'aPplicatiOn/VnD.aPPle.MpEgUrL' 1918 type: 'aPplicatiOn/VnD.aPPle.MpEgUrL'
1919 }), 'supports vnd.apple.mpegurl'); 1919 }), 'supports vnd.apple.mpegurl');
1920 ok(videojs.HlsSourceHandler(techName).canPlayType('aPplicatiOn/VnD.aPPle.MpEgUrL'), 'supports vnd.apple.mpegurl');
1921 ok(videojs.HlsSourceHandler(techName).canPlayType('aPplicatiOn/x-MPegUrl'), 'supports x-mpegurl');
1920 1922
1921 ok(!(videojs.HlsSourceHandler(techName).canHandleSource({ 1923 ok(!(videojs.HlsSourceHandler(techName).canHandleSource({
1922 type: 'video/mp4' 1924 type: 'video/mp4'
...@@ -1924,6 +1926,8 @@ test('the source handler supports HLS mime types', function() { ...@@ -1924,6 +1926,8 @@ test('the source handler supports HLS mime types', function() {
1924 ok(!(videojs.HlsSourceHandler(techName).canHandleSource({ 1926 ok(!(videojs.HlsSourceHandler(techName).canHandleSource({
1925 type: 'video/x-flv' 1927 type: 'video/x-flv'
1926 }) instanceof videojs.HlsHandler), 'does not support flv'); 1928 }) instanceof videojs.HlsHandler), 'does not support flv');
1929 ok(!(videojs.HlsSourceHandler(techName).canPlayType('video/mp4')), 'does not support mp4');
1930 ok(!(videojs.HlsSourceHandler(techName).canPlayType('video/x-flv')), 'does not support flv');
1927 }); 1931 });
1928 }); 1932 });
1929 1933
......