Skip to content

Commit

Permalink
feat: Optimize appendBuffer operations for init segments (#5377)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored Jun 29, 2023
1 parent ddbc249 commit 68f7a0e
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions lib/media/media_source_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ shaka.media.MediaSourceEngine = class {
/** @private {boolean} */
this.ignoreManifestTimestampsInSegmentsMode_ = false;

/** @private {boolean} */
this.attemptTimestampOffsetCalculation_ = false;

/** @private {!shaka.util.PublicPromise.<number>} */
this.textSequenceModeOffset_ = new shaka.util.PublicPromise();
}
Expand Down Expand Up @@ -383,6 +386,10 @@ shaka.media.MediaSourceEngine = class {
this.ignoreManifestTimestampsInSegmentsMode_ =
ignoreManifestTimestampsInSegmentsMode;

this.attemptTimestampOffsetCalculation_ = !this.sequenceMode_ &&
this.manifestType_ == shaka.media.ManifestParser.HLS &&
!this.ignoreManifestTimestampsInSegmentsMode_;

for (const contentType of streamsByType.keys()) {
const stream = streamsByType.get(contentType);
this.initSourceBuffer_(contentType, stream);
Expand Down Expand Up @@ -751,39 +758,39 @@ shaka.media.MediaSourceEngine = class {
return;
}

const attemptTimestampOffsetCalculation = !this.sequenceMode_ &&
this.manifestType_ == shaka.media.ManifestParser.HLS &&
!this.ignoreManifestTimestampsInSegmentsMode_;

let timestampOffset = this.sourceBuffers_[contentType].timestampOffset;

let mimeType = this.sourceBufferTypes_[contentType];
if (this.transmuxers_[contentType]) {
mimeType = this.transmuxers_[contentType].getOrginalMimeType();
}
const timestamp = this.getTimestampAndDispatchMetadata_(
contentType, data, reference, mimeType, timestampOffset);
if (timestamp != null && reference) {
const calculatedTimestampOffset = reference.startTime - timestamp;
const timestampOffsetDifference =
Math.abs(timestampOffset - calculatedTimestampOffset);
if (timestampOffsetDifference >= 0.1 || seeked || adaptation) {
timestampOffset = calculatedTimestampOffset;
if (attemptTimestampOffsetCalculation) {
this.enqueueOperation_(contentType, () => this.abort_(contentType));
this.enqueueOperation_(
contentType,
() => this.setTimestampOffset_(contentType, timestampOffset));
if (reference) {
const timestamp = this.getTimestampAndDispatchMetadata_(
contentType, data, reference, mimeType, timestampOffset);
if (timestamp != null) {
const calculatedTimestampOffset = reference.startTime - timestamp;
const timestampOffsetDifference =
Math.abs(timestampOffset - calculatedTimestampOffset);
if (timestampOffsetDifference >= 0.1 || seeked || adaptation) {
timestampOffset = calculatedTimestampOffset;
if (this.attemptTimestampOffsetCalculation_) {
this.enqueueOperation_(
contentType,
() => this.abort_(contentType));
this.enqueueOperation_(
contentType,
() => this.setTimestampOffset_(contentType, timestampOffset));
}
}
// Timestamps can only be reliably extracted from video, not audio.
// Packed audio formats do not have internal timestamps at all.
// Prefer video for this when available.
const isBestSourceBufferForTimestamps =
contentType == ContentType.VIDEO ||
!(ContentType.VIDEO in this.sourceBuffers_);
if (this.sequenceMode_ && isBestSourceBufferForTimestamps) {
this.textSequenceModeOffset_.resolve(timestampOffset);
}
}
// Timestamps can only be reliably extracted from video, not audio.
// Packed audio formats do not have internal timestamps at all.
// Prefer video for this when available.
const isBestSourceBufferForTimestamps =
contentType == ContentType.VIDEO ||
!(ContentType.VIDEO in this.sourceBuffers_);
if (this.sequenceMode_ && isBestSourceBufferForTimestamps) {
this.textSequenceModeOffset_.resolve(timestampOffset);
}
}
if (hasClosedCaptions && contentType == ContentType.VIDEO) {
Expand Down

0 comments on commit 68f7a0e

Please sign in to comment.