Skip to content

Commit

Permalink
Feat(player): Add updateStartTime method to play (shaka-project#3491)
Browse files Browse the repository at this point in the history
This provides a way to decide on a start time after the manifestparsed event,
but before the load process ends.
  • Loading branch information
caridley committed Aug 10, 2021
1 parent be8dce4 commit 16c1810
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/cast/cast_utils.js
Expand Up @@ -388,6 +388,7 @@ shaka.cast.CastUtils.PlayerVoidMethods = [
'selectVariantsByLabel',
'setTextTrackVisibility',
'trickPlay',
'updateStartTime',
'goToLive',
];

Expand Down
31 changes: 31 additions & 0 deletions lib/player.js
Expand Up @@ -533,6 +533,15 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
/** @private {!Array.<function():(!Promise|undefined)>} */
this.cleanupOnUnload_ = [];

/**
* This playback start position will be used when
* <code>updateStartTime()</code> has been called to provide an updated
* start position during the media loading process.
*
* @private {?number}
*/
this.updatedStartTime_ = null;

if (dependencyInjector) {
dependencyInjector(this);
}
Expand Down Expand Up @@ -1046,6 +1055,19 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
return this.wrapWalkerListenersWithPromise_(events);
}

/**
* Provides a way to update the stream start position during the media loading
* process. Can for example be called from the <code>manifestparsed</code>
* event handler to update the start position based on information in the
* manifest.
*
* @param {number} startTime
* @export
*/
updateStartTime(startTime) {
this.updatedStartTime_ = startTime;
}

/**
* Tell the player to load the content at <code>assetUri</code> and start
* playback at <code>startTime</code>. Before calling <code>load</code>,
Expand All @@ -1066,6 +1088,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
* @export
*/
load(assetUri, startTime, mimeType) {
this.updatedStartTime_ = null;

// Do not allow the player to be used after |destroy| is called.
if (this.loadMode_ == shaka.Player.LoadMode.DESTROYED) {
return Promise.reject(this.createAbortLoadError_());
Expand Down Expand Up @@ -1804,6 +1828,13 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
// something we are now depending on.
has.startTime = wants.startTime;

// If updateStartTime() has been called since load() was invoked use the
// requested startTime
if (this.updatedStartTime_ != null) {
has.startTime = this.updatedStartTime_;
this.updatedStartTime_ = null;
}

// Store a reference to values in |has| after asserting so that closure will
// know that they will still be non-null between calls to await.
const mediaElement = has.mediaElement;
Expand Down
35 changes: 35 additions & 0 deletions test/player_integration.js
Expand Up @@ -87,6 +87,41 @@ describe('Player', () => {
});
}); // describe('attach')

describe('updateStartTime() in manifestparsed event handler', () => {
it('does not get segments prior to startTime', async () => {
player.addEventListener('manifestparsed', () => {
player.updateStartTime(24);
});
const results = {
requestedVideoSegment0: false,
requestedVideoSegment1: false,
requestedVideoSegment2: false,
};
const expected = {
requestedVideoSegment0: false,
requestedVideoSegment1: false,
requestedVideoSegment2: true,
};
player.getNetworkingEngine().registerRequestFilter(
(type, request) => {
if (request.uris[0] == 'test:sintel/video/0') {
results.requestedVideoSegment0 = true;
}
if (request.uris[0] == 'test:sintel/video/1') {
results.requestedVideoSegment1 = true;
}
if (request.uris[0] == 'test:sintel/video/2') {
results.requestedVideoSegment2 = true;
}
});
await player.load('test:sintel_compiled');
video.play();
await waiter.waitUntilPlayheadReachesOrFailOnTimeout(video, 25, 30);
expect(results).toEqual(expected);
});
});


describe('getStats', () => {
it('gives stats about current stream', async () => {
// This is tested more in player_unit.js. This is here to test the public
Expand Down

0 comments on commit 16c1810

Please sign in to comment.