@@ -795,8 +795,24 @@ shaka.media.StreamingEngine.prototype.update_ = function(mediaState) {
795795
796796 // Compute how far we've buffered ahead of the playhead.
797797 var playheadTime = this . playhead_ . getTime ( ) ;
798- var bufferedAhead = this . mediaSourceEngine_ . bufferedAheadOf (
799- mediaState . type , playheadTime , 0.1 ) ;
798+
799+ // Get the next timestamp we need.
800+ var bufferEnd = this . mediaSourceEngine_ . bufferEnd ( mediaState . type ) ;
801+ var timeNeeded = this . getTimeNeeded_ ( mediaState , playheadTime ) ;
802+ shaka . log . v2 ( logPrefix , 'timeNeeded=' + timeNeeded ) ;
803+
804+ var needPeriodIndex = this . findPeriodContainingTime_ ( timeNeeded ) ;
805+ var needPeriod = this . manifest_ . periods [ needPeriodIndex ] ;
806+ var reference = this . getSegmentReferenceNeeded_ (
807+ mediaState , playheadTime , bufferEnd , needPeriodIndex ) ;
808+
809+ var bufferedAhead ;
810+ if ( reference ) {
811+ bufferedAhead = needPeriod . startTime + reference . startTime - playheadTime ;
812+ } else {
813+ bufferedAhead = this . mediaSourceEngine_ . bufferedAheadOf (
814+ mediaState . type , playheadTime , 0.1 ) ;
815+ }
800816
801817 goog . asserts . assert ( this . manifest_ , 'manifest_ should not be null' ) ;
802818 goog . asserts . assert ( this . config_ , 'config_ should not be null' ) ;
@@ -822,12 +838,6 @@ shaka.media.StreamingEngine.prototype.update_ = function(mediaState) {
822838 return 0.5 ;
823839 }
824840
825- // Get the next timestamp we need.
826- var bufferEnd = this . mediaSourceEngine_ . bufferEnd ( mediaState . type ) ;
827- var timeNeeded = this . getTimeNeeded_ (
828- mediaState , playheadTime , bufferedAhead , bufferEnd ) ;
829- shaka . log . v2 ( logPrefix , 'timeNeeded=' + timeNeeded ) ;
830-
831841 // Check if we've buffered to the end of the presentation.
832842 if ( timeNeeded >= this . manifest_ . presentationTimeline . getDuration ( ) ) {
833843 // We shouldn't rebuffer if the playhead is close to the end of the
@@ -855,7 +865,6 @@ shaka.media.StreamingEngine.prototype.update_ = function(mediaState) {
855865 // available once it's switched to. Note that we don't use the non-existence
856866 // of SegmentReferences as an indicator to determine Period boundaries
857867 // because SegmentIndexes can provide SegmentReferences outside its Period.
858- var needPeriodIndex = this . findPeriodContainingTime_ ( timeNeeded ) ;
859868 if ( needPeriodIndex != currentPeriodIndex ) {
860869 shaka . log . debug ( logPrefix ,
861870 'need Period ' + needPeriodIndex ,
@@ -866,7 +875,7 @@ shaka.media.StreamingEngine.prototype.update_ = function(mediaState) {
866875 return null ;
867876 }
868877
869- var reference = this . getSegmentReferenceNeeded_ (
878+ reference = this . getSegmentReferenceNeeded_ (
870879 mediaState , playheadTime , bufferEnd , currentPeriodIndex ) ;
871880 if ( ! reference ) {
872881 // The segment could not be found, does not exist, or is not available. In
@@ -888,15 +897,13 @@ shaka.media.StreamingEngine.prototype.update_ = function(mediaState) {
888897 *
889898 * @param {shaka.media.StreamingEngine.MediaState_ } mediaState
890899 * @param {number } playheadTime
891- * @param {number } bufferedAhead
892- * @param {?number } bufferEnd
893900 * @return {number } The next timestamp needed.
894901 * @throws {!shaka.util.Error } if the buffer is inconsistent with our
895902 * expectations.
896903 * @private
897904 */
898905shaka . media . StreamingEngine . prototype . getTimeNeeded_ = function (
899- mediaState , playheadTime , bufferedAhead , bufferEnd ) {
906+ mediaState , playheadTime ) {
900907 // Get the next timestamp we need. We must use |lastSegmentReference|
901908 // to determine this and not the actual buffer for two reasons:
902909 // 1. actual segments end slightly before their advertised end times, so
0 commit comments