Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Preload): Add detachAndSavePreload method #6630

Merged
merged 2 commits into from
May 20, 2024
Merged
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
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',

Expand Down
10 changes: 10 additions & 0 deletions test/player_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,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
Loading