Skip to content

Commit

Permalink
fix: Fix vanishing tracks while offline
Browse files Browse the repository at this point in the history
Introduced in shaka-project#4189, as a side-effect of restricting tracks when a
network failure occurs.  We should not trigger such restrictions when
the browser is known to be offline.

Closes shaka-project#4408
  • Loading branch information
joeyparrish committed Aug 18, 2022
1 parent 8376410 commit 4a43ea4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -5625,6 +5625,12 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
return false;
}

if (!navigator.onLine) {
// Don't restrict variants if we're completely offline, or else we end up
// rapidly restricting all of them.
return false;
}

let maxDisabledTime = this.config_.streaming.maxDisabledTime;
if (maxDisabledTime == 0) {
if (error.code == shaka.util.Error.Code.SEGMENT_MISSING) {
Expand Down
32 changes: 31 additions & 1 deletion test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ describe('Player', () => {
dispatchEventSpy.calls.reset();
player.configure({streaming: {maxDisabledTime}});
player.setMaxHardwareResolution(123, 456);
onErrorCallback(httpError);
});

afterEach(() => {
Expand All @@ -434,14 +433,18 @@ describe('Player', () => {
});

it('handles HTTP_ERROR', () => {
onErrorCallback(httpError);
expect(httpError.handled).toBeTruthy();
});

it('does not dispatch any error', () => {
onErrorCallback(httpError);
expect(dispatchEventSpy).not.toHaveBeenCalled();
});

it('disables the current variant and applies restrictions', () => {
onErrorCallback(httpError);

const foundDisabledVariant =
manifest.variants.some(({disabledUntilTime}) =>
disabledUntilTime == currentTime + maxDisabledTime);
Expand All @@ -453,11 +456,38 @@ describe('Player', () => {
});

it('switches the variant', () => {
onErrorCallback(httpError);

expect(chooseVariantSpy).toHaveBeenCalled();
expect(getBufferedInfoSpy).toHaveBeenCalled();
expect(switchVariantSpy)
.toHaveBeenCalledWith(chosenVariant, false, true, 14);
});

describe('but browser is truly offline', () => {
let navigatorOnLineDescriptor;
beforeEach(() => {
navigatorOnLineDescriptor = Object.getOwnPropertyDescriptor(
Navigator.prototype, 'onLine');

// Redefine the property, replacing only the getter.
Object.defineProperty(navigator, 'onLine',
Object.assign(navigatorOnLineDescriptor, {
get: () => false,
}));
});

afterEach(() => {
// Restore the original property definition.
Object.defineProperty(
navigator, 'onLine', navigatorOnLineDescriptor);
});

it('does not handle HTTP_ERROR', () => {
onErrorCallback(httpError);
expect(httpError.handled).toBe(false);
});
});
});
});
});
Expand Down

0 comments on commit 4a43ea4

Please sign in to comment.