7c6736ca by jrivera

Move updateEndHandler out of the addEventListener call to make things cleaner

1 parent 9c47ba5b
...@@ -142,7 +142,6 @@ ...@@ -142,7 +142,6 @@
142 start = intervalDuration(playlist, playlist.mediaSequence); 142 start = intervalDuration(playlist, playlist.mediaSequence);
143 end = intervalDuration(playlist, 143 end = intervalDuration(playlist,
144 playlist.mediaSequence + Math.max(0, playlist.segments.length - 3)); 144 playlist.mediaSequence + Math.max(0, playlist.segments.length - 3));
145 end = Math.max(0, end);
146 return videojs.createTimeRange(start, end); 145 return videojs.createTimeRange(start, end);
147 }; 146 };
148 147
......
...@@ -397,73 +397,7 @@ videojs.HlsHandler.prototype.setupSourceBuffer_ = function() { ...@@ -397,73 +397,7 @@ videojs.HlsHandler.prototype.setupSourceBuffer_ = function() {
397 397
398 // transition the sourcebuffer to the ended state if we've hit the end of 398 // transition the sourcebuffer to the ended state if we've hit the end of
399 // the playlist 399 // the playlist
400 this.sourceBuffer.addEventListener('updateend', function updateEndHandler() { 400 this.sourceBuffer.addEventListener('updateend', this.updateEndHandler_.bind(this));
401 var
402 segmentInfo = this.pendingSegment_,
403 segment,
404 segments,
405 playlist,
406 currentMediaIndex,
407 currentBuffered,
408 timelineUpdates;
409
410 this.pendingSegment_ = null;
411
412 // stop here if the update errored or was aborted
413 if (!segmentInfo) {
414 return;
415 }
416
417 playlist = this.playlists.media();
418 segments = playlist.segments;
419 currentMediaIndex = segmentInfo.mediaIndex + (segmentInfo.mediaSequence - playlist.mediaSequence);
420 currentBuffered = this.findCurrentBuffered_();
421
422 // if we switched renditions don't try to add segment timeline
423 // information to the playlist
424 if (segmentInfo.playlist.uri !== this.playlists.media().uri) {
425 return this.fillBuffer();
426 }
427
428 // annotate the segment with any start and end time information
429 // added by the media processing
430 segment = playlist.segments[currentMediaIndex];
431
432 timelineUpdates = videojs.Hls.bufferedAdditions_(segmentInfo.buffered,
433 this.tech_.buffered());
434
435 timelineUpdates.forEach(function (update) {
436 if (segment) {
437 if (update.end !== undefined) {
438 segment.end = update.end;
439 }
440 }
441 });
442
443 // if we've buffered to the end of the video, let the MediaSource know
444 if (this.playlists.media().endList &&
445 currentBuffered.length &&
446 segments[segments.length - 1].end <= currentBuffered.end(0) &&
447 this.mediaSource.readyState === 'open') {
448 this.mediaSource.endOfStream();
449 return;
450 }
451
452 if (timelineUpdates.length ||
453 segmentInfo.buffered.length !== this.tech_.buffered().length) {
454 this.updateDuration(playlist);
455 // check if it's time to download the next segment
456 this.fillBuffer();
457 return;
458 }
459
460 // the last segment append must have been entirely in the
461 // already buffered time ranges. just buffer forward until we
462 // find a segment that adds to the buffered time ranges and
463 // improves subsequent media index calculations.
464 this.fillBuffer(currentMediaIndex + 1);
465 return;
466 }.bind(this));
467 }; 401 };
468 402
469 /** 403 /**
...@@ -1151,6 +1085,74 @@ videojs.HlsHandler.prototype.drainBuffer = function(event) { ...@@ -1151,6 +1085,74 @@ videojs.HlsHandler.prototype.drainBuffer = function(event) {
1151 this.sourceBuffer.appendBuffer(bytes); 1085 this.sourceBuffer.appendBuffer(bytes);
1152 }; 1086 };
1153 1087
1088 videojs.HlsHandler.prototype.updateEndHandler_ = function () {
1089 var
1090 segmentInfo = this.pendingSegment_,
1091 segment,
1092 segments,
1093 playlist,
1094 currentMediaIndex,
1095 currentBuffered,
1096 timelineUpdates;
1097
1098 this.pendingSegment_ = null;
1099
1100 // stop here if the update errored or was aborted
1101 if (!segmentInfo) {
1102 return;
1103 }
1104
1105 playlist = this.playlists.media();
1106 segments = playlist.segments;
1107 currentMediaIndex = segmentInfo.mediaIndex + (segmentInfo.mediaSequence - playlist.mediaSequence);
1108 currentBuffered = this.findCurrentBuffered_();
1109
1110 // if we switched renditions don't try to add segment timeline
1111 // information to the playlist
1112 if (segmentInfo.playlist.uri !== this.playlists.media().uri) {
1113 return this.fillBuffer();
1114 }
1115
1116 // annotate the segment with any start and end time information
1117 // added by the media processing
1118 segment = playlist.segments[currentMediaIndex];
1119
1120 timelineUpdates = videojs.Hls.bufferedAdditions_(segmentInfo.buffered,
1121 this.tech_.buffered());
1122
1123 timelineUpdates.forEach(function (update) {
1124 if (segment) {
1125 if (update.end !== undefined) {
1126 segment.end = update.end;
1127 }
1128 }
1129 });
1130
1131 // if we've buffered to the end of the video, let the MediaSource know
1132 if (this.playlists.media().endList &&
1133 currentBuffered.length &&
1134 segments[segments.length - 1].end <= currentBuffered.end(0) &&
1135 this.mediaSource.readyState === 'open') {
1136 this.mediaSource.endOfStream();
1137 return;
1138 }
1139
1140 if (timelineUpdates.length ||
1141 segmentInfo.buffered.length !== this.tech_.buffered().length) {
1142 this.updateDuration(playlist);
1143 // check if it's time to download the next segment
1144 this.fillBuffer();
1145 return;
1146 }
1147
1148 // the last segment append must have been entirely in the
1149 // already buffered time ranges. just buffer forward until we
1150 // find a segment that adds to the buffered time ranges and
1151 // improves subsequent media index calculations.
1152 this.fillBuffer(currentMediaIndex + 1);
1153 return;
1154 };
1155
1154 /** 1156 /**
1155 * Attempt to retrieve the key for a particular media segment. 1157 * Attempt to retrieve the key for a particular media segment.
1156 */ 1158 */
......