Init media sources in src method.
Moved initializations to init. Rename and repurpose original init method to initSource which gets called from #src.
Showing
1 changed file
with
29 additions
and
88 deletions
... | @@ -174,82 +174,15 @@ var | ... | @@ -174,82 +174,15 @@ var |
174 | 174 | ||
175 | resolveUrl, | 175 | resolveUrl, |
176 | 176 | ||
177 | /** | 177 | initSource = function(player, mediaSource, srcUrl) { |
178 | * Initializes the HLS plugin. | ||
179 | * @param options {mixed} the URL to an HLS playlist | ||
180 | */ | ||
181 | init = function(options, mediaSource) { | ||
182 | var | 178 | var |
183 | segmentParser = new videojs.Hls.SegmentParser(), | 179 | segmentParser = new videojs.Hls.SegmentParser(), |
184 | player = this, | ||
185 | srcUrl, | ||
186 | 180 | ||
187 | segmentXhr, | 181 | segmentXhr, |
188 | settings, | 182 | settings = videojs.util.mergeOptions({}, player.options().hls), |
189 | fillBuffer, | 183 | fillBuffer, |
190 | updateDuration; | 184 | updateDuration; |
191 | 185 | ||
192 | // if the video element supports HLS natively, do nothing | ||
193 | if (videojs.Hls.supportsNativeHls) { | ||
194 | return; | ||
195 | } | ||
196 | |||
197 | settings = videojs.util.mergeOptions({}, options); | ||
198 | |||
199 | srcUrl = (function() { | ||
200 | var | ||
201 | extname, | ||
202 | i = 0, | ||
203 | j = 0, | ||
204 | sources = player.options().sources, | ||
205 | techName, | ||
206 | length = sources.length; | ||
207 | |||
208 | // use the URL specified in options if one was provided | ||
209 | if (typeof options === 'string') { | ||
210 | return options; | ||
211 | } else if (options && options.url) { | ||
212 | return options.url; | ||
213 | } | ||
214 | |||
215 | // find the first playable source | ||
216 | for (; i < length; i++) { | ||
217 | |||
218 | // ignore sources without a specified type | ||
219 | if (!sources[i].type) { | ||
220 | continue; | ||
221 | } | ||
222 | |||
223 | // do nothing if the source is handled by one of the standard techs | ||
224 | //for (j in player.options().techOrder) { | ||
225 | //techname = player.options().techorder[j]; | ||
226 | //techname = techname[0].touppercase() + techname.substring(1); | ||
227 | //if (videojs[techname].canplaysource({ type: sources[i].type })) { | ||
228 | //return; | ||
229 | //} | ||
230 | //} | ||
231 | |||
232 | // use the plugin if the MIME type specifies HLS | ||
233 | if ((/application\/x-mpegURL/).test(sources[i].type) || | ||
234 | (/application\/vnd\.apple\.mpegURL/).test(sources[i].type)) { | ||
235 | return sources[i].src; | ||
236 | } | ||
237 | } | ||
238 | })(); | ||
239 | |||
240 | if (!srcUrl) { | ||
241 | // do nothing until the plugin is initialized with a valid URL | ||
242 | videojs.log('hls: no valid playlist URL specified'); | ||
243 | return; | ||
244 | } | ||
245 | |||
246 | // expose the HLS plugin state | ||
247 | player.hls.readyState = function() { | ||
248 | if (!player.hls.media) { | ||
249 | return 0; // HAVE_NOTHING | ||
250 | } | ||
251 | return 1; // HAVE_METADATA | ||
252 | }; | ||
253 | 186 | ||
254 | player.on('seeking', function() { | 187 | player.on('seeking', function() { |
255 | var currentTime = player.currentTime(); | 188 | var currentTime = player.currentTime(); |
... | @@ -513,37 +446,45 @@ var | ... | @@ -513,37 +446,45 @@ var |
513 | oldMediaPlaylist = updatedPlaylist; | 446 | oldMediaPlaylist = updatedPlaylist; |
514 | }); | 447 | }); |
515 | }); | 448 | }); |
516 | |||
517 | //player.src([{ | ||
518 | //src: videojs.URL.createObjectURL(mediaSource), | ||
519 | //type: "video/flv" | ||
520 | //}]); | ||
521 | |||
522 | //if (player.options().autoplay) { | ||
523 | //player.play(); | ||
524 | //} | ||
525 | |||
526 | }; | 449 | }; |
527 | 450 | ||
528 | var mpegurlRE = /^application\/(?:x-|vnd\.apple\.)mpegurl/i; | 451 | var mpegurlRE = /^application\/(?:x-|vnd\.apple\.)mpegurl/i; |
529 | 452 | ||
530 | videojs.Hls = videojs.Flash.extend({ | 453 | videojs.Hls = videojs.Flash.extend({ |
531 | init: function(player, options, ready) { | 454 | init: function(player, options, ready) { |
532 | var mediaSource = new videojs.MediaSource(); | 455 | var |
533 | var source = options.source; | 456 | source = options.source, |
534 | var settings = player.options(); | 457 | settings = player.options(); |
458 | |||
459 | delete options.source; | ||
535 | options.swf = settings.flash.swf; | 460 | options.swf = settings.flash.swf; |
536 | options.source = { | ||
537 | src: videojs.URL.createObjectURL(mediaSource), | ||
538 | type: "video/flv" | ||
539 | }; | ||
540 | videojs.Flash.call(this, player, options, ready); | 461 | videojs.Flash.call(this, player, options, ready); |
541 | player.hls = {}; | 462 | player.hls = {}; |
542 | options.source = source; | 463 | options.source = source; |
543 | init.call(player, options, mediaSource); | 464 | videojs.Hls.prototype.src.call(player, options.source && options.source.src); |
544 | } | 465 | } |
545 | }); | 466 | }); |
546 | 467 | ||
468 | videojs.Hls.prototype.src = function(src) { | ||
469 | var | ||
470 | player = this.player(), | ||
471 | mediaSource, | ||
472 | source; | ||
473 | |||
474 | if (src) { | ||
475 | mediaSource = new videojs.MediaSource(), | ||
476 | source = { | ||
477 | src: videojs.URL.createObjectURL(mediaSource), | ||
478 | type: "video/flv" | ||
479 | }; | ||
480 | player.hls.mediaSource = mediaSource; | ||
481 | initSource(player, mediaSource, src); | ||
482 | this.ready(function() { | ||
483 | this.el().querySelector('.vjs-tech').vjs_src(source.src); | ||
484 | }); | ||
485 | } | ||
486 | } | ||
487 | |||
547 | //for (var prop in videojs.Flash) { | 488 | //for (var prop in videojs.Flash) { |
548 | //videojs.Hls[prop] = videojs.Flash[prop]; | 489 | //videojs.Hls[prop] = videojs.Flash[prop]; |
549 | //} | 490 | //} |
... | @@ -553,7 +494,7 @@ videojs.Hls = videojs.Flash.extend({ | ... | @@ -553,7 +494,7 @@ videojs.Hls = videojs.Flash.extend({ |
553 | 494 | ||
554 | videojs.Hls.isSupported = function() { | 495 | videojs.Hls.isSupported = function() { |
555 | return videojs.Flash.isSupported() && videojs.MediaSource; | 496 | return videojs.Flash.isSupported() && videojs.MediaSource; |
556 | } | 497 | }; |
557 | 498 | ||
558 | videojs.Hls.canPlaySource = function(srcObj) { | 499 | videojs.Hls.canPlaySource = function(srcObj) { |
559 | return mpegurlRE.test(srcObj.type) || videojs.Flash.canPlaySource.call(this, srcObj); | 500 | return mpegurlRE.test(srcObj.type) || videojs.Flash.canPlaySource.call(this, srcObj); | ... | ... |
-
Please register or sign in to post a comment