Skip to content

Commit

Permalink
feat(Preload): Add detachAndSavePreload method (shaka-project#6630)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad committed May 20, 2024
1 parent 3c2fd55 commit 0a68e93
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<?shaka.media.PreloadManager>}
* @export
*/
async detachAndSavePreload(keepAdManager = false) {
const preloadManager = await this.savePreload_();
await this.detach(keepAdManager);
return preloadManager;
}

/**
* @return {!Promise.<?shaka.media.PreloadManager>}
* @private
*/
async savePreload_() {
let preloadManager = null;
if (this.manifest_ && this.parser_ && this.parserFactory_ &&
this.assetUri_) {
Expand Down Expand Up @@ -1703,7 +1731,6 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
this.abrManager_ = null;
this.abrManagerFactory_ = null;
}
await this.unload(initializeMediaSource, keepAdManager);
return preloadManager;
}

Expand Down
1 change: 1 addition & 0 deletions test/cast/cast_utils_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('CastUtils', () => {
'setVideoContainer',
'getActiveSessionsMetadata',
'releaseAllMutexes', // Very specific to the inner workings of the player.
'detachAndSavePreload',
'unloadAndSavePreload',
'preload',
'getNonDefaultConfiguration',
Expand Down
10 changes: 10 additions & 0 deletions test/player_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 0a68e93

Please sign in to comment.