Skip to content

Commit 0a68e93

Browse files
authored
feat(Preload): Add detachAndSavePreload method (shaka-project#6630)
1 parent 3c2fd55 commit 0a68e93

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

lib/player.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,34 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
16691669
*/
16701670
async unloadAndSavePreload(
16711671
initializeMediaSource = true, keepAdManager = false) {
1672+
const preloadManager = await this.savePreload_();
1673+
await this.unload(initializeMediaSource, keepAdManager);
1674+
return preloadManager;
1675+
}
1676+
1677+
/**
1678+
* Detach the player from the current media element, if any, and returns a
1679+
* PreloadManager that contains the loaded manifest of that asset, if any.
1680+
* Allows for the asset to be re-loaded by this player faster, in the future.
1681+
* When in src= mode, this detach but does not make a PreloadManager.
1682+
* Leaves the player in a state where it cannot play media, until it has been
1683+
* attached to something else.
1684+
*
1685+
* @param {boolean=} keepAdManager
1686+
* @return {!Promise.<?shaka.media.PreloadManager>}
1687+
* @export
1688+
*/
1689+
async detachAndSavePreload(keepAdManager = false) {
1690+
const preloadManager = await this.savePreload_();
1691+
await this.detach(keepAdManager);
1692+
return preloadManager;
1693+
}
1694+
1695+
/**
1696+
* @return {!Promise.<?shaka.media.PreloadManager>}
1697+
* @private
1698+
*/
1699+
async savePreload_() {
16721700
let preloadManager = null;
16731701
if (this.manifest_ && this.parser_ && this.parserFactory_ &&
16741702
this.assetUri_) {
@@ -1703,7 +1731,6 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
17031731
this.abrManager_ = null;
17041732
this.abrManagerFactory_ = null;
17051733
}
1706-
await this.unload(initializeMediaSource, keepAdManager);
17071734
return preloadManager;
17081735
}
17091736

test/cast/cast_utils_unit.js

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('CastUtils', () => {
2828
'setVideoContainer',
2929
'getActiveSessionsMetadata',
3030
'releaseAllMutexes', // Very specific to the inner workings of the player.
31+
'detachAndSavePreload',
3132
'unloadAndSavePreload',
3233
'preload',
3334
'getNonDefaultConfiguration',

test/player_integration.js

+10
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,16 @@ describe('Player', () => {
13971397
await waiter.waitUntilPlayheadReachesOrFailOnTimeout(video, 1, 10);
13981398
});
13991399

1400+
it('detachAndSavePreload', async () => {
1401+
await player.load('test:sintel_compiled');
1402+
await video.play();
1403+
const preloadManager = await player.detachAndSavePreload();
1404+
await player.attach(video);
1405+
await player.load(preloadManager);
1406+
await video.play();
1407+
await waiter.waitUntilPlayheadReachesOrFailOnTimeout(video, 1, 10);
1408+
});
1409+
14001410
it('unloadAndSavePreload', async () => {
14011411
await player.load('test:sintel_compiled');
14021412
await video.play();

0 commit comments

Comments
 (0)