1d816d1e by David LaPalomento

Merge pull request #66 from videojs/feature/tech-seeking

Convert seeking implementation to currentTime
2 parents 50a78066 bc1df9e2
......@@ -183,20 +183,36 @@ var
initSource = function(player, mediaSource, srcUrl) {
var
segmentParser = new videojs.Hls.SegmentParser(),
settings = videojs.util.mergeOptions({}, player.options().hls),
lastSeekedTime,
segmentXhr,
settings = videojs.util.mergeOptions({}, player.options().hls),
fillBuffer,
updateDuration;
player.on('seeking', function() {
var currentTime = player.currentTime();
player.hls.mediaIndex = getMediaIndexByTime(player.hls.playlists.media(),
currentTime);
player.hls.currentTime = function() {
if (lastSeekedTime) {
return lastSeekedTime;
}
return this.el().vjs_getProperty('currentTime');
};
player.hls.setCurrentTime = function(currentTime) {
if (!(this.playlists && this.playlists.media())) {
// return immediately if the metadata is not ready yet
return 0;
}
// save the seek target so currentTime can report it correctly
// while the seek is pending
lastSeekedTime = currentTime;
// determine the requested segment
this.mediaIndex =
getMediaIndexByTime(this.playlists.media(), currentTime);
// abort any segments still being decoded
player.hls.sourceBuffer.abort();
this.sourceBuffer.abort();
// cancel outstanding requests and buffer appends
if (segmentXhr) {
......@@ -205,7 +221,7 @@ var
// begin filling the buffer at the new position
fillBuffer(currentTime * 1000);
});
};
/**
* Update the player duration
......@@ -324,8 +340,9 @@ var
bufferedTime = player.buffered().end(0) - player.currentTime();
}
// if there is plenty of content in the buffer, relax for awhile
if (bufferedTime >= goalBufferLength) {
// if there is plenty of content in the buffer and we're not
// seeking, relax for awhile
if (typeof offset !== 'number' && bufferedTime >= goalBufferLength) {
return;
}
......@@ -376,10 +393,18 @@ var
// if we're refilling the buffer after a seek, scan through the muxed
// FLV tags until we find the one that is closest to the desired
// playback time
if (offset !== undefined && typeof offset === "number") {
while (segmentParser.getTags()[0].pts < offset) {
if (typeof offset === 'number') {
(function() {
var tag = segmentParser.getTags()[0];
for (; tag.pts < offset; tag = segmentParser.getTags()[0]) {
segmentParser.getNextTag();
}
// tell the SWF where we will be seeking to
player.hls.el().vjs_setProperty('currentTime', tag.pts * 0.001);
lastSeekedTime = null;
})();
}
while (segmentParser.tagsAvailable()) {
......@@ -467,6 +492,7 @@ videojs.Hls = videojs.Flash.extend({
videojs.Hls.prototype.src = function(src) {
var
player = this.player(),
self = this,
mediaSource,
source;
......@@ -478,8 +504,8 @@ videojs.Hls.prototype.src = function(src) {
};
this.mediaSource = mediaSource;
initSource(player, mediaSource, src);
this.ready(function() {
this.el().vjs_src(source.src);
this.player().ready(function() {
self.el().vjs_src(source.src);
});
}
};
......
......@@ -49,6 +49,7 @@ var
tech = player.el().querySelector('.vjs-tech');
tech.vjs_getProperty = function() {};
tech.vjs_setProperty = function() {};
tech.vjs_src = function() {};
videojs.Flash.onReady(tech.id);
......@@ -754,7 +755,7 @@ test('cancels outstanding XHRs when seeking', function() {
// trigger a segment download request
player.trigger('timeupdate');
// attempt to seek while the download is in progress
player.trigger('seeking');
player.currentTime(7);
ok(requests[1].aborted, 'XHR aborted');
strictEqual(requests.length, 3, 'opened new XHR');
......@@ -830,10 +831,7 @@ test('drops tags before the target timestamp when seeking', function() {
bytes: i
});
}
player.currentTime = function() {
return 7;
};
player.trigger('seeking');
player.currentTime(7);
standardXHRResponse(requests[2]);
while (callbacks.length) {
......@@ -879,10 +877,7 @@ test('clears pending buffer updates when seeking', function() {
// seek to 7s
tags.push({ pts: 7000, bytes: 7 });
player.currentTime = function() {
return 7;
};
player.trigger('seeking');
player.currentTime(7);
standardXHRResponse(requests[2]);
while (callbacks.length) {
......