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(DASH): Fix playback of some DASH with multiple periods #7519

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions build/types/core
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
+../../lib/util/public_promise.js
+../../lib/util/state_history.js
+../../lib/util/stats.js
+../../lib/util/segment_index.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already lib/media/segment_index, 2 same file names in different spaces might be confusing

+../../lib/util/stream_utils.js
+../../lib/util/string_utils.js
+../../lib/util/switch_history.js
Expand Down
7 changes: 7 additions & 0 deletions externs/shaka/manifest_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ shaka.extern.ManifestParser = class {
* @exportDoc
*/
setMediaElement(mediaElement) {}

/**
* provide CloseSegmentIndexRegister
* @param {shaka.util.CloseSegmentIndexRegister} register
* @exportDoc
*/
setCloseSegmentIndexRegister(register) {}
Comment on lines +124 to +129
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to add new method to publicly exposed interface, it will need to be documented better.
Preferably though, we shouldn't add new method but try to look for other solutions (maybe callback in shaka.extern.ManifestParser.PlayerInterface?)

};


Expand Down
23 changes: 23 additions & 0 deletions lib/dash/dash_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ shaka.dash.DashParser = class {
}
}

/**
* @override
* @exportInterface
*/
setCloseSegmentIndexRegister(register) {
const parser = this;
const closeSegmentIndexes = () => {
parser.handleDeferredCloseSegmentIndexes();
};
register.add(closeSegmentIndexes);
}

/**
* @override
* @exportInterface
Expand Down Expand Up @@ -1491,6 +1503,17 @@ shaka.dash.DashParser = class {
}
}

/**
* Handles deferred releases of old SegmentIndexes
* content type from a previous update.
* @export
Comment on lines +1507 to +1509
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a part of public interface, don't export it

*/
handleDeferredCloseSegmentIndexes() {
if (this.periodCombiner_) {
this.periodCombiner_.handleDeferredCloseSegmentIndexes();
}
}

/**
* Clean StreamMap Object to remove reference of deleted Stream Object
* @private
Expand Down
11 changes: 11 additions & 0 deletions lib/dash/segment_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,17 @@ shaka.dash.TimelineSegmentIndex = class extends shaka.media.SegmentIndex {
}
}

/**
* @override
*/
evictAll() {
if (this.templateInfo_ && this.templateInfo_.timeline.length) {
this.numEvicted_ += this.templateInfo_.timeline.length;
this.templateInfo_.timeline = [];
}
this.references = [];
}

/**
* Merge new template info
* @param {shaka.dash.SegmentTemplate.SegmentTemplateInfo} info
Expand Down
5 changes: 5 additions & 0 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ shaka.hls.HlsParser = class {
}
}

/** @override */
setCloseSegmentIndexRegister(register) {
// No-op
}

/**
* @override
* @exportInterface
Expand Down
11 changes: 11 additions & 0 deletions lib/media/segment_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ shaka.media.SegmentIndex = class {
this.numEvicted_ += diff;
}

/**
* Removes all SegmentReferences that end before the given time.
* @export
*/
evictAll() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it was exported, it should be defined in shaka.extern.SegmentIndex.

if (this.references.length) {
this.numEvicted_ += this.references.length;
this.references = [];
}
}


/**
* Drops references that start after windowEnd, or end before windowStart,
Expand Down
17 changes: 15 additions & 2 deletions lib/media/streaming_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ goog.require('shaka.util.MimeUtils');
goog.require('shaka.util.Mp4BoxParsers');
goog.require('shaka.util.Mp4Parser');
goog.require('shaka.util.Networking');

goog.require('shaka.util.CloseSegmentIndexRegister');

/**
* @summary Creates a Streaming Engine.
Expand Down Expand Up @@ -148,6 +148,12 @@ shaka.media.StreamingEngine = class {

/** @private {?shaka.media.StreamingEngine.MediaState_} */
this.lastTextMediaStateBeforeUnload_ = null;

/**
* @private {shaka.util.CloseSegmentIndexRegister}
*/
this.closeSegmentIndexRegister_ =
new shaka.util.CloseSegmentIndexRegister();
}

/** @override */
Expand Down Expand Up @@ -182,6 +188,7 @@ shaka.media.StreamingEngine = class {
this.playerInterface_ = null;
this.manifest_ = null;
this.config_ = null;
this.closeSegmentIndexRegister_ = null;
}

/**
Expand Down Expand Up @@ -269,7 +276,6 @@ shaka.media.StreamingEngine = class {
}
}


/**
* Applies a playback range. This will only affect non-live content.
*
Expand Down Expand Up @@ -530,8 +536,15 @@ shaka.media.StreamingEngine = class {
this.deferredCloseSegmentIndex_.delete(streamId);
}
}
this.closeSegmentIndexRegister_.closeSegmentIndexes();
}

/**
* @return {shaka.util.CloseSegmentIndexRegister}
*/
getCloseSegmentIndexRegister() {
return this.closeSegmentIndexRegister_;
}

/**
* Switches to the given Stream. |stream| may be from any Variant.
Expand Down
6 changes: 6 additions & 0 deletions lib/mss/mss_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ shaka.mss.MssParser = class {
}
}

/**
* @override
* @exportInterface
*/
setCloseSegmentIndexRegister(register) {}

/**
* @override
* @exportInterface
Expand Down
5 changes: 5 additions & 0 deletions lib/offline/offline_manifest_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ shaka.offline.OfflineManifestParser = class {
// No-op
}

/** @override */
setCloseSegmentIndexRegister(register) {
// No-op
}

/** @override */
async start(uriString, playerInterface) {
/** @type {shaka.offline.OfflineUri} */
Expand Down
2 changes: 2 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2570,6 +2570,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {

this.streamingEngine_ = this.createStreamingEngine();
this.streamingEngine_.configure(this.config_.streaming);
this.parser_.setCloseSegmentIndexRegister(
this.streamingEngine_.getCloseSegmentIndexRegister());

// Set the load mode to "loaded with media source" as late as possible so
// that public methods won't try to access internal components until
Expand Down
38 changes: 35 additions & 3 deletions lib/util/periods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ shaka.util.PeriodCombiner = class {
* @private {!Set.<string>}
*/
this.usedPeriodIds_ = new Set();

/**
* Retains a reference to the function used to close SegmentIndex objects
* for streams which were switched away from during an ongoing update_()
* just after the period removal.
*
* @private {!Map.<number, !function()>}
*/
this.deferredCloseSegmentIndex_ = new Map();
}

/** @override */
Expand All @@ -69,7 +78,7 @@ shaka.util.PeriodCombiner = class {
stream.segmentIndex.release();
}
}

this.handleDeferredCloseSegmentIndexes();
this.audioStreams_ = [];
this.videoStreams_ = [];
this.textStreams_ = [];
Expand Down Expand Up @@ -110,6 +119,21 @@ shaka.util.PeriodCombiner = class {
return this.imageStreams_;
}


/**
* Handles deferred releases of old SegmentIndexes
* content type from a previous update.
* @export
*/
handleDeferredCloseSegmentIndexes() {
for (const [key, value] of this.deferredCloseSegmentIndex_.entries()) {
const streamId = /** @type {number} */ (key);
const closeSegmentIndex = /** @type {function()} */ (value);
closeSegmentIndex();
this.deferredCloseSegmentIndex_.delete(streamId);
}
}

/**
* Deletes a stream from matchedStreams because it is no longer needed
*
Expand Down Expand Up @@ -154,9 +178,17 @@ shaka.util.PeriodCombiner = class {
});
}
}
if (stream.segmentIndex) {
stream.closeSegmentIndex();
if (stream.segmentIndex &&
!this.deferredCloseSegmentIndex_.has(stream.id)) {
const deferedCloseSegmentIndex = () => {
if (stream.segmentIndex) {
stream.segmentIndex.evictAll();
stream.closeSegmentIndex();
}
};
this.deferredCloseSegmentIndex_.set(stream.id, deferedCloseSegmentIndex);
}

this.usedPeriodIds_.delete(periodId);
}

Expand Down
36 changes: 36 additions & 0 deletions lib/util/segment_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
goog.provide('shaka.util.CloseSegmentIndexRegister');

/**
* @summary a register which provides an interface
* to close shaka.media.SegmentIndex
* @export
*/
shaka.util.CloseSegmentIndexRegister = class {
/** */
constructor() {
/** @private {!Array.<Function>} */
this.register_ = [];
}

/**
* add function which closes shaka.media.SegmentIndex
* from the stream
* @param {Function} closeSegmentIndexFunction
*/
add(closeSegmentIndexFunction) {
if (closeSegmentIndexFunction) {
this.register_.push(closeSegmentIndexFunction);
}
}

/**
* close shaka.media.SegmentIndex from register
*/
closeSegmentIndexes() {
for (const closeSegmentIndexFunction of this.register_) {
if (closeSegmentIndexFunction) {
closeSegmentIndexFunction();
}
}
}
};
5 changes: 5 additions & 0 deletions test/offline/storage_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,11 @@ filterDescribe('Storage', storageSupport, () => {
/** @override */
configure(params) {}

/** @override */
setCloseSegmentIndexRegister(register) {
// No-op
}

/** @override */
start(uri, player) {
return Promise.resolve(this.map_[uri]);
Expand Down
8 changes: 8 additions & 0 deletions test/test/util/simple_fakes.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ shaka.test.FakeStreamingEngine = class {
jasmine.createSpy('switchTextStream').and.callFake((textStream) => {
activeText = textStream;
});

/** @type {!jasmine.Spy} */
this.getCloseSegmentIndexRegister =
jasmine.createSpy('getCloseSegmentIndexRegister');
}
};

Expand Down Expand Up @@ -149,6 +153,10 @@ shaka.test.FakeManifestParser = class {

/** @type {!jasmine.Spy} */
this.onExpirationUpdated = jasmine.createSpy('onExpirationUpdated');

/** @type {!jasmine.Spy} */
this.setCloseSegmentIndexRegister =
jasmine.createSpy('setCloseSegmentIndexRegister');
}
};

Expand Down
5 changes: 5 additions & 0 deletions test/test/util/test_scheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,11 @@ shaka.test.TestScheme.ManifestParser = class {
return new shaka.test.TestScheme.ManifestParser();
}

/** @override */
setCloseSegmentIndexRegister(register) {
// No-op
}

/** @override */
start(uri, playerInterface) {
const re = /^test:([^/]+)$/;
Expand Down
Loading