Skip to content

Commit

Permalink
stream-controller: isolate start position adjustement logic
Browse files Browse the repository at this point in the history
ensure that media.currentTime is adjusted to expected start position as soon as possible
related to https://github.com/dailymotion/hls.js/issues/499
  • Loading branch information
mangui committed Jun 14, 2016
1 parent 568bbb7 commit 203dc09
Showing 1 changed file with 26 additions and 38 deletions.
64 changes: 26 additions & 38 deletions src/controller/stream-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1067,29 +1067,32 @@ class StreamController extends EventHandler {

_checkBuffer() {
var media = this.media;
if(media) {
// compare readyState
let readyState = media.readyState;
// if ready state different from HAVE_NOTHING (numeric value 0), we are allowed to seek
if(readyState) {
let targetSeekPosition,
currentTime = media.currentTime,
loadedmetadata = this.loadedmetadata;

// adjust currentTime to start position on loaded metadata
if(!loadedmetadata && media.buffered.length) {
this.loadedmetadata = true;
// only adjust currentTime if not equal to 0
if (!currentTime && currentTime !== this.startPosition) {
targetSeekPosition = this.startPosition;
// if ready state different from HAVE_NOTHING (numeric value 0), we are allowed to seek
if(media && media.readyState) {
let currentTime = media.currentTime;
// adjust currentTime to start position on loaded metadata
if(!this.loadedmetadata && media.buffered.length) {
this.loadedmetadata = true;
// only adjust currentTime if startPosition not equal to 0
let startPosition = this.startPosition;
if (!currentTime && currentTime !== startPosition) {
if (startPosition) {
logger.log(`target start position:${startPosition}`);
let bufInfo = BufferHelper.bufferInfo(media,startPosition,0);
if(bufInfo.len === 0) {
let nextStart = bufInfo.nextStart;
if (nextStart !== undefined &&
(nextStart - startPosition) < this.config.maxSeekHole) {
startPosition = nextStart;
logger.log(`target start position not buffered, seek to next buffered ${startPosition}`);
}
}
logger.log(`adjust currentTime from ${currentTime} to ${startPosition}`);
media.currentTime = startPosition;
}
}

if (targetSeekPosition) {
currentTime = targetSeekPosition;
logger.log(`target seek position:${targetSeekPosition}`);
}
var bufferInfo = BufferHelper.bufferInfo(media,currentTime,0),
} else {
let bufferInfo = BufferHelper.bufferInfo(media,currentTime,0),
expectedPlaying = !(media.paused || // not playing when media is paused
media.ended || // not playing when media is ended
media.buffered.length === 0), // not playing if nothing buffered
Expand All @@ -1101,8 +1104,7 @@ _checkBuffer() {
logger.log(`playback not stuck anymore @${currentTime}`);
}
// check buffer upfront
// if less than jumpThreshold second is buffered, and media is expected to play but playhead is not moving,
// and we have a new buffer range available upfront, let's seek to that one
// if less than jumpThreshold second is buffered, let's check in more details
if(expectedPlaying && bufferInfo.len <= jumpThreshold) {
if(playheadMoving) {
// playhead moving
Expand All @@ -1119,7 +1121,7 @@ _checkBuffer() {
this.seekHoleNudgeDuration += this.config.seekHoleNudgeDuration;
}
}
// if we are below threshold, try to jump if next buffer range is close
// if we are below threshold, try to jump to start of next buffer range if close
if(bufferInfo.len <= jumpThreshold) {
// no buffer available @ currentTime, check if next buffer is close (within a config.maxSeekHole second range)
var nextBufferStart = bufferInfo.nextStart, delta = nextBufferStart-currentTime;
Expand All @@ -1134,20 +1136,6 @@ _checkBuffer() {
this.hls.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_SEEK_OVER_HOLE, fatal: false, hole : hole});
}
}
} else {
let currentTime = media.currentTime;
if (targetSeekPosition && currentTime !== targetSeekPosition) {
if(bufferInfo.len === 0) {
let nextStart = bufferInfo.nextStart;
if (nextStart !== undefined &&
(nextStart - targetSeekPosition) < this.config.maxSeekHole) {
targetSeekPosition = nextStart;
logger.log(`target seek position not buffered, seek to next buffered ${targetSeekPosition}`);
}
}
logger.log(`adjust currentTime from ${currentTime} to ${targetSeekPosition}`);
media.currentTime = targetSeekPosition;
}
}
}
}
Expand Down

0 comments on commit 203dc09

Please sign in to comment.