Skip to content

Commit

Permalink
Don't clear buffer if switching to the same stream.
Browse files Browse the repository at this point in the history
Several variants may have the same audio or video stream.
When switching variants we will only update a stream if it actually
changed. This allows to keep previously buffered content if
only one of the variant's streams changed, but not the other.

Closes #693.

Change-Id: If63c9a3a95c5c48007023224457b291814f14c0c
  • Loading branch information
ismena committed Mar 31, 2017
1 parent 9a05e26 commit 1b3015a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
26 changes: 23 additions & 3 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,7 @@ shaka.Player.prototype.selectVariantTrack = function(track, opt_clearBuffer) {

var period = this.streamingEngine_.getCurrentPeriod();
var variant = StreamUtils.findVariantForTrack(period, track);
var activeStreams = this.streamingEngine_.getActiveStreams();
if (variant) {
// Double check that the track is allowed to be played.
// The track list should only contain playable variants,
Expand All @@ -1165,8 +1166,19 @@ shaka.Player.prototype.selectVariantTrack = function(track, opt_clearBuffer) {
'" because it is restricted.');
return;
}
if (variant.audio) streamsToSwitch[ContentType.AUDIO] = variant.audio;
if (variant.video) streamsToSwitch[ContentType.VIDEO] = variant.video;

if (variant.audio) {
// Update active streams even if we're not switching
// in case active streams haven't been set up yet.
this.updateActiveStreams_(variant.audio);
if (variant.audio != activeStreams[ContentType.AUDIO])
streamsToSwitch[ContentType.AUDIO] = variant.audio;
}
if (variant.video) {
this.updateActiveStreams_(variant.video);
if (variant.video != activeStreams[ContentType.VIDEO])
streamsToSwitch[ContentType.VIDEO] = variant.video;
}
}

// Add entries to the history.
Expand All @@ -1176,7 +1188,6 @@ shaka.Player.prototype.selectVariantTrack = function(track, opt_clearBuffer) {

// Save current text stream to ensure that it doesn't get overridden
// by a default one inside shaka.Player.configure()
var activeStreams = this.streamingEngine_.getActiveStreams();
var currentTextStream = activeStreams[ContentType.TEXT];

if (currentTextStream) {
Expand Down Expand Up @@ -1519,6 +1530,15 @@ shaka.Player.prototype.addToSwitchHistory_ = function(stream, fromAdaptation) {
fromAdaptation: fromAdaptation
});

this.updateActiveStreams_(stream);
};


/**
* @param {!shakaExtern.Stream} stream
* @private
*/
shaka.Player.prototype.updateActiveStreams_ = function(stream) {
goog.asserts.assert(this.manifest_, 'Must not be destroyed');
var periodIndex =
shaka.util.StreamUtils.findPeriodContainingStream(this.manifest_, stream);
Expand Down
54 changes: 48 additions & 6 deletions test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,48 @@ describe('Player', function() {
.toHaveBeenCalledWith(ContentType.VIDEO, variant.video, false);
});

it('doesn\'t switch audio if old and new variants ' +
'have the same audio track', function() {
chooseStreams();
canSwitch();

var period = manifest.periods[0];
var variant1 = period.variants[0];
var variant2 = period.variants[1];
expect(variant1.audio).toEqual(variant2.audio);

player.selectVariantTrack(variantTracks[0]);
streamingEngine.switch.calls.reset();

player.selectVariantTrack(variantTracks[1]);

expect(streamingEngine.switch).toHaveBeenCalledWith(
ContentType.VIDEO, variant2.video, false);
expect(streamingEngine.switch).not.toHaveBeenCalledWith(
ContentType.AUDIO, variant2.audio, false);
});

it('doesn\'t switch video if old and new variants ' +
'have the same video track', function() {
chooseStreams();
canSwitch();

var period = manifest.periods[0];
var variant1 = period.variants[0];
var variant2 = period.variants[2];
expect(variant1.video).toEqual(variant2.video);

player.selectVariantTrack(variantTracks[0]);
streamingEngine.switch.calls.reset();

player.selectVariantTrack(variantTracks[2]);

expect(streamingEngine.switch).toHaveBeenCalledWith(
ContentType.AUDIO, variant2.audio, false);
expect(streamingEngine.switch).not.toHaveBeenCalledWith(
ContentType.VIDEO, variant2.video, false);
});

it('still switches streams if called during startup', function() {
player.selectVariantTrack(variantTracks[1]);
expect(streamingEngine.switch).not.toHaveBeenCalled();
Expand All @@ -896,13 +938,13 @@ describe('Player', function() {
it('still switches streams if called while switching Periods', function() {
chooseStreams();

player.selectVariantTrack(variantTracks[1]);
player.selectVariantTrack(variantTracks[3]);
expect(streamingEngine.switch).not.toHaveBeenCalled();

canSwitch();

var period = manifest.periods[0];
var variant = period.variants[1];
var variant = period.variants[3];
expect(streamingEngine.switch)
.toHaveBeenCalledWith(ContentType.AUDIO, variant.audio, false);
expect(streamingEngine.switch)
Expand All @@ -927,9 +969,9 @@ describe('Player', function() {

streamingEngine.switch.calls.reset();

var variant = period.variants[1];
expect(variantTracks[1].id).toBe(variant.id);
player.selectVariantTrack(variantTracks[1]);
var variant = period.variants[2];
expect(variantTracks[2].id).toBe(variant.id);
player.selectVariantTrack(variantTracks[2]);
expect(streamingEngine.switch)
.toHaveBeenCalledWith(ContentType.TEXT, textStream, true);
expect(streamingEngine.switch)
Expand Down Expand Up @@ -1254,7 +1296,7 @@ describe('Player', function() {
});

it('includes selectVariantTrack choices', function() {
var track = player.getVariantTracks()[1];
var track = player.getVariantTracks()[3];
player.selectVariantTrack(track);

var period = manifest.periods[0];
Expand Down

0 comments on commit 1b3015a

Please sign in to comment.