Skip to content

Commit

Permalink
fix: Fix vanishing tracks while offline (shaka-project#4426)
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 d945084 commit c935cc1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/player.js
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
40 changes: 39 additions & 1 deletion test/player_unit.js
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,46 @@ 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', () => {
/** @type {!Object} */
let navigatorOnLineDescriptor;

// eslint-disable-next-line no-restricted-syntax
const navigatorPrototype = Navigator.prototype;

beforeAll(() => {
navigatorOnLineDescriptor =
/** @type {!Object} */(Object.getOwnPropertyDescriptor(
navigatorPrototype, 'onLine'));
});

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

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

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

0 comments on commit c935cc1

Please sign in to comment.