Skip to content

Commit

Permalink
fix(StreamingEngine): clear lastInitSegmentReference when clearing bu…
Browse files Browse the repository at this point in the history
…ffer

When toggling between two text streams quickly(for example en -> fr ->
en), the mediaState's lastInitSegmentReference may not get updated to
'fr' before switching back to 'en'. That way, in initSourceBuffer_(),
the initSegmentReference is considered as fetched and appended, but the
buffer was in fact cleared.

Setting lastInitSegmentReference to null every time we clear the
buffer, and fetch and append the initSegmentReference with the new
update.

b/168253400

Change-Id: Id6378bdadd34f93a2a2f54ac84d82cf39b89716e
  • Loading branch information
michellezhuogg committed Jan 5, 2021
1 parent 1e74fc2 commit e40cdd0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/media/streaming_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,7 @@ shaka.media.StreamingEngine = class {
mediaState.clearBufferSafeMargin = 0;
mediaState.clearingBuffer = true;
mediaState.lastSegmentReference = null;
mediaState.lastInitSegmentReference = null;
mediaState.segmentIterator = null;

shaka.log.debug(logPrefix, 'clearing buffer');
Expand Down
38 changes: 36 additions & 2 deletions test/media/streaming_engine_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -897,9 +897,11 @@ describe('StreamingEngine', () => {
});
});
manifest.addTextStream(20, (stream) => {
stream.setInitSegmentReference(['text-20-init'], 0, null);
stream.useSegmentTemplate('text-20-%d.mp4', 10);
});
manifest.addTextStream(21, (stream) => {
stream.setInitSegmentReference(['text-21-init'], 0, null);
stream.useSegmentTemplate('text-21-%d.mp4', 10);
});
});
Expand Down Expand Up @@ -987,10 +989,9 @@ describe('StreamingEngine', () => {
});

// See https://github.com/google/shaka-player/issues/2956
it('works with fast stream switches during update', async () => {
it('works with fast variant switches during update', async () => {
// Delay the appendBuffer call until later so we are waiting for this to
// finish when we switch.
const Util = shaka.test.Util;
const p = new shaka.util.PublicPromise();
const old = mediaSourceEngine.appendBuffer;
// Replace the whole spy since we want to call the original.
Expand All @@ -1013,6 +1014,39 @@ describe('StreamingEngine', () => {

expect(Util.invokeSpy(mediaSourceEngine.bufferEnd, 'video')).toBe(10);
});

it('works with fast text stream switches during update', async () => {
// Delay the appendBuffer call until later so we are waiting for this to
// finish when we switch.
const p = new shaka.util.PublicPromise();

const old = mediaSourceEngine.appendBuffer;
// Replace the whole spy since we want to call the original.
mediaSourceEngine.appendBuffer =
jasmine.createSpy('appendBuffer')
.and.callFake(async (type, data, start, end) => {
await p;
return Util.invokeSpy(old, type, data, start, end);
});

await streamingEngine.start();
playing = true;

await Util.fakeEventLoop(3);
netEngine.request.calls.reset();

streamingEngine.switchTextStream(newTextStream);
streamingEngine.switchTextStream(initialTextStream);
p.resolve();

await Util.fakeEventLoop(5);

const segmentType = shaka.net.NetworkingEngine.RequestType.SEGMENT;
// Quickly switching back to text1, and text init segment should be
// fetched again.
netEngine.expectRequest('text-20-init', segmentType);
netEngine.expectNoRequest('text-21-init', segmentType);
});
});

describe('handles seeks (VOD)', () => {
Expand Down

0 comments on commit e40cdd0

Please sign in to comment.