Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1392,8 +1392,8 @@ class Preview extends EventEmitter {

/**
* If the current viewer is a video preload (jpg) and the file now has a playable rep (dash/mp4),
* either upgrade the same viewer to that rep or switch viewer (e.g. Dash → MP4). Returns true
* if this path was taken so the caller can return.
* either upgrade the same viewer to that rep or switch viewer (e.g. Dash → MP4, Dash → Video360).
* Returns true if this path was taken so the caller can return.
*
* @private
* @param {Object} file - File from server
Expand All @@ -1410,19 +1410,23 @@ class Preview extends EventEmitter {
return false;
}

const viewerName = getProp(this.viewer, 'options.viewer.NAME', '');
const loader = this.getLoader(this.file);
const representation = loader.determineRepresentation(this.file, this.viewer.options.viewer);
const expectedRep = viewerName === 'Dash' ? 'dash' : 'mp4';
const canUpgradeInPlace = representation?.representation === expectedRep;

if (canUpgradeInPlace) {
this.viewer.options.file = this.file;
this.viewer.options.representation = representation;
this.viewer.load();
if (!loader) {
this.loadViewer();
return true;
}
this.loadViewer();

// The preload viewer was chosen from file info without dash/mp4, so the playable file may need
// a different viewer (e.g. Video360, which requires dash).
const viewer = loader.determineViewer(this.file, Object.keys(this.disabledViewers), this.options.viewers);
if (viewer?.NAME !== getProp(this.viewer, 'options.viewer.NAME')) {
this.loadViewer();
return true;
}

this.viewer.options.file = this.file;
this.viewer.options.representation = loader.determineRepresentation(this.file, viewer);
this.viewer.load();
return true;
}

Expand Down
104 changes: 102 additions & 2 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,7 @@ describe('lib/Preview', () => {
expect(stubs.loadViewer).not.toHaveBeenCalled();
});

test('should upgrade video preload to playable rep when file has matching rep (MP4)', () => {
test('should upgrade the MP4 preload viewer in place when the playable file still uses MP4', () => {
const viewerLoad = jest.fn();
preview.viewer = {
options: {
Expand All @@ -2516,6 +2516,7 @@ describe('lib/Preview', () => {
const mp4Rep = { representation: 'mp4', content: { url_template: 'https://example.com/video.mp4' } };
stubs.getLoader = jest.spyOn(preview, 'getLoader').mockReturnValue({
determineRepresentation: jest.fn().mockReturnValue(mp4Rep),
determineViewer: jest.fn().mockReturnValue({ NAME: 'MP4' }),
});

preview.handleFileInfoResponse(stubs.file);
Expand All @@ -2526,7 +2527,7 @@ describe('lib/Preview', () => {
expect(stubs.loadViewer).not.toHaveBeenCalled();
});

test('should call loadViewer when video preload viewer (Dash) and file has only non-matching playable rep (mp4)', () => {
test("should reload the viewer when the Dash preload viewer's file only has mp4 (switches to MP4)", () => {
stubs.loadViewer.mockImplementation(() => {});
preview.viewer = {
options: {
Expand All @@ -2538,13 +2539,112 @@ describe('lib/Preview', () => {
stubs.getCachedFile.mockReturnValue({ file_version: { sha1: 2 } });
stubs.getLoader = jest.spyOn(preview, 'getLoader').mockReturnValue({
determineRepresentation: jest.fn().mockReturnValue({ representation: 'mp4' }),
determineViewer: jest.fn().mockReturnValue({ NAME: 'MP4' }),
});

preview.handleFileInfoResponse(stubs.file);

expect(stubs.loadViewer).toHaveBeenCalled();
});

test('should reload the viewer instead of upgrading in place when the playable file needs a different viewer (Video360)', () => {
stubs.loadViewer.mockImplementation(() => {});
const viewerLoad = jest.fn();
preview.viewer = {
options: {
viewer: { NAME: 'Dash' },
representation: { representation: PRELOAD_REP_NAME },
},
load: viewerLoad,
};
stubs.file.name = 'video.360.mp4';
stubs.file.representations.entries = [{ representation: PRELOAD_REP_NAME }, { representation: 'dash' }];
stubs.getCachedFile.mockReturnValue({ file_version: { sha1: 2 } });
const determineRepresentation = jest.fn().mockReturnValue({ representation: 'dash' });
const determineViewer = jest.fn().mockReturnValue({ NAME: 'Video360' });
stubs.getLoader = jest.spyOn(preview, 'getLoader').mockReturnValue({
determineRepresentation,
determineViewer,
});

preview.handleFileInfoResponse(stubs.file);

expect(determineViewer).toHaveBeenCalledWith(stubs.file, expect.any(Array), preview.options.viewers);
expect(determineRepresentation).not.toHaveBeenCalled();
expect(viewerLoad).not.toHaveBeenCalled();
expect(stubs.loadViewer).toHaveBeenCalled();
});

test('should pick Video360 with the real loaders when a .360 Dash preload gets dash', () => {
stubs.loadViewer.mockImplementation(() => {});
jest.spyOn(Browser, 'getName').mockReturnValue('Chrome');
jest.spyOn(Browser, 'isIOS').mockReturnValue(false);
jest.spyOn(Browser, 'hasWebGL').mockReturnValue(true);
const viewerLoad = jest.fn();
preview.viewer = {
options: {
viewer: { NAME: 'Dash', REP: 'dash' },
representation: { representation: PRELOAD_REP_NAME },
},
load: viewerLoad,
};
stubs.file.name = 'video.360.mp4';
stubs.file.extension = 'mp4';
stubs.file.representations.entries = [{ representation: PRELOAD_REP_NAME }, { representation: 'dash' }];
stubs.getCachedFile.mockReturnValue({ file_version: { sha1: 2 } });

preview.handleFileInfoResponse(stubs.file);

expect(preview.getLoader(stubs.file).determineViewer(stubs.file).NAME).toBe('Video360');
expect(viewerLoad).not.toHaveBeenCalled();
expect(stubs.loadViewer).toHaveBeenCalled();
});

test('should reload the viewer when no loader can play the file so loadViewer reports the real error', () => {
stubs.loadViewer.mockImplementation(() => {});
const viewerLoad = jest.fn();
preview.viewer = {
options: {
viewer: { NAME: 'MP4', REP: 'mp4' },
representation: { representation: PRELOAD_REP_NAME },
},
load: viewerLoad,
};
stubs.file.representations.entries = [{ representation: PRELOAD_REP_NAME }, { representation: 'dash' }];
stubs.getCachedFile.mockReturnValue({ file_version: { sha1: 2 } });
jest.spyOn(preview, 'getLoader').mockReturnValue(undefined);

preview.handleFileInfoResponse(stubs.file);

expect(stubs.triggerError).not.toHaveBeenCalled();
expect(viewerLoad).not.toHaveBeenCalled();
expect(stubs.loadViewer).toHaveBeenCalled();
});

test('should upgrade the Dash preload viewer in place when the playable file still uses Dash', () => {
const viewerLoad = jest.fn();
preview.viewer = {
options: {
viewer: { NAME: 'Dash' },
representation: { representation: PRELOAD_REP_NAME },
},
load: viewerLoad,
};
stubs.file.representations.entries = [{ representation: PRELOAD_REP_NAME }, { representation: 'dash' }];
stubs.getCachedFile.mockReturnValue({ file_version: { sha1: 2 } });
const dashRep = { representation: 'dash' };
stubs.getLoader = jest.spyOn(preview, 'getLoader').mockReturnValue({
determineRepresentation: jest.fn().mockReturnValue(dashRep),
determineViewer: jest.fn().mockReturnValue({ NAME: 'Dash' }),
});

preview.handleFileInfoResponse(stubs.file);

expect(preview.viewer.options.representation).toBe(dashRep);
expect(viewerLoad).toHaveBeenCalled();
expect(stubs.loadViewer).not.toHaveBeenCalled();
});

test('should trigger account error when video file has only preload rep and no playable reps', () => {
stubs.file.extension = 'mp4';
stubs.file.representations.entries = [{ representation: PRELOAD_REP_NAME }];
Expand Down
Loading