From 0a68e934fde7016286ed40e23eb5f41df59c63bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Mon, 20 May 2024 09:13:57 +0200 Subject: [PATCH] feat(Preload): Add detachAndSavePreload method (#6630) --- lib/player.js | 29 ++++++++++++++++++++++++++++- test/cast/cast_utils_unit.js | 1 + test/player_integration.js | 10 ++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/player.js b/lib/player.js index d2c23afef4..fb39978d0c 100644 --- a/lib/player.js +++ b/lib/player.js @@ -1669,6 +1669,34 @@ shaka.Player = class extends shaka.util.FakeEventTarget { */ async unloadAndSavePreload( initializeMediaSource = true, keepAdManager = false) { + const preloadManager = await this.savePreload_(); + await this.unload(initializeMediaSource, keepAdManager); + return preloadManager; + } + + /** + * Detach the player from the current media element, if any, and returns a + * PreloadManager that contains the loaded manifest of that asset, if any. + * Allows for the asset to be re-loaded by this player faster, in the future. + * When in src= mode, this detach but does not make a PreloadManager. + * Leaves the player in a state where it cannot play media, until it has been + * attached to something else. + * + * @param {boolean=} keepAdManager + * @return {!Promise.} + * @export + */ + async detachAndSavePreload(keepAdManager = false) { + const preloadManager = await this.savePreload_(); + await this.detach(keepAdManager); + return preloadManager; + } + + /** + * @return {!Promise.} + * @private + */ + async savePreload_() { let preloadManager = null; if (this.manifest_ && this.parser_ && this.parserFactory_ && this.assetUri_) { @@ -1703,7 +1731,6 @@ shaka.Player = class extends shaka.util.FakeEventTarget { this.abrManager_ = null; this.abrManagerFactory_ = null; } - await this.unload(initializeMediaSource, keepAdManager); return preloadManager; } diff --git a/test/cast/cast_utils_unit.js b/test/cast/cast_utils_unit.js index d675ef87e3..b18615fb91 100644 --- a/test/cast/cast_utils_unit.js +++ b/test/cast/cast_utils_unit.js @@ -28,6 +28,7 @@ describe('CastUtils', () => { 'setVideoContainer', 'getActiveSessionsMetadata', 'releaseAllMutexes', // Very specific to the inner workings of the player. + 'detachAndSavePreload', 'unloadAndSavePreload', 'preload', 'getNonDefaultConfiguration', diff --git a/test/player_integration.js b/test/player_integration.js index 28ff3a4b14..62c7b8a74c 100644 --- a/test/player_integration.js +++ b/test/player_integration.js @@ -1397,6 +1397,16 @@ describe('Player', () => { await waiter.waitUntilPlayheadReachesOrFailOnTimeout(video, 1, 10); }); + it('detachAndSavePreload', async () => { + await player.load('test:sintel_compiled'); + await video.play(); + const preloadManager = await player.detachAndSavePreload(); + await player.attach(video); + await player.load(preloadManager); + await video.play(); + await waiter.waitUntilPlayheadReachesOrFailOnTimeout(video, 1, 10); + }); + it('unloadAndSavePreload', async () => { await player.load('test:sintel_compiled'); await video.play();