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

fix(media): Fix region checking in livestreams #5361

Merged
merged 3 commits into from
Jun 28, 2023
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
20 changes: 19 additions & 1 deletion lib/media/region_observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,24 @@ shaka.media.RegionObserver = class extends shaka.util.FakeEventTarget {
* destroy the timeline.
*
* @param {!shaka.media.RegionTimeline} timeline
* @param {boolean} startsPastZero
*/
constructor(timeline) {
constructor(timeline, startsPastZero) {
super();

/** @private {shaka.media.RegionTimeline} */
this.timeline_ = timeline;

/**
* Whether the asset is expected to start at a time beyond 0 seconds.
* For example, if the asset is a live stream.
* If true, we will not start polling for regions until the playhead has
* moved past 0 seconds, to avoid bad behaviors where the current time is
* briefly 0 before we have enough data to play.
* @private {boolean}
*/
this.startsPastZero_ = startsPastZero;

/**
* A mapping between a region and where we previously were relative to it.
* When the value here differs from what we calculate, it means we moved and
Expand Down Expand Up @@ -121,6 +132,13 @@ shaka.media.RegionObserver = class extends shaka.util.FakeEventTarget {
/** @override */
poll(positionInSeconds, wasSeeking) {
const RegionObserver = shaka.media.RegionObserver;
if (this.startsPastZero_ && positionInSeconds == 0) {
// Don't start checking regions until the timeline has begun moving.
return;
}
// Now that we have seen the playhead go past 0, it's okay if it goes
// back there (e.g. seeking back to the start).
this.startsPastZero_ = false;

for (const region of this.timeline_.regions()) {
const previousPosition = this.oldPosition_.get(region);
Expand Down
11 changes: 8 additions & 3 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2188,7 +2188,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
}

this.playhead_ = this.createPlayhead(has.startTime);
this.playheadObservers_ = this.createPlayheadObserversForMSE_();
this.playheadObservers_ =
this.createPlayheadObserversForMSE_(has.startTimeOfLoad);

// We need to start the buffer management code near the end because it will
// set the initial buffering state and that depends on other components
Expand Down Expand Up @@ -2930,17 +2931,21 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
* Create the observers for MSE playback. These observers are responsible for
* notifying the app and player of specific events during MSE playback.
*
* @param {number} startTimeOfLoad
* @return {!shaka.media.PlayheadObserverManager}
* @private
*/
createPlayheadObserversForMSE_() {
createPlayheadObserversForMSE_(startTimeOfLoad) {
goog.asserts.assert(this.manifest_, 'Must have manifest');
goog.asserts.assert(this.regionTimeline_, 'Must have region timeline');
goog.asserts.assert(this.video_, 'Must have video element');

const startsPastZero = this.isLive() || startTimeOfLoad > 0;

// Create the region observer. This will allow us to notify the app when we
// move in and out of timeline regions.
const regionObserver = new shaka.media.RegionObserver(this.regionTimeline_);
const regionObserver = new shaka.media.RegionObserver(
this.regionTimeline_, startsPastZero);

regionObserver.addEventListener('enter', (event) => {
/** @type {shaka.extern.TimelineRegionInfo} */
Expand Down
3 changes: 2 additions & 1 deletion test/media/region_observer_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ describe('RegionObserver', () => {
timeline = new shaka.media.RegionTimeline(
shaka.test.Util.spyFunc(onSeekRange));

observer = new shaka.media.RegionObserver(timeline);
observer = new shaka.media.RegionObserver(
timeline, /* startsPastZero= */ false);
observer.addEventListener('enter', (event) => {
shaka.test.Util.spyFunc(onEnterRegion)(event['region'], event['seeking']);
});
Expand Down