diff --git a/src/lib/Preview.js b/src/lib/Preview.js index e294f02be..4876c6007 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -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 @@ -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; } diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index ff7d45c88..378d4d45c 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -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: { @@ -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); @@ -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: { @@ -2538,6 +2539,7 @@ 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); @@ -2545,6 +2547,104 @@ describe('lib/Preview', () => { 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 }];