From 68f7a0eb2a8574645592e069c63813e0c97c5e66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Thu, 29 Jun 2023 21:59:45 +0200 Subject: [PATCH] feat: Optimize appendBuffer operations for init segments (#5377) --- lib/media/media_source_engine.js | 59 ++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/lib/media/media_source_engine.js b/lib/media/media_source_engine.js index 94e915b377..b83795d6d9 100644 --- a/lib/media/media_source_engine.js +++ b/lib/media/media_source_engine.js @@ -133,6 +133,9 @@ shaka.media.MediaSourceEngine = class { /** @private {boolean} */ this.ignoreManifestTimestampsInSegmentsMode_ = false; + /** @private {boolean} */ + this.attemptTimestampOffsetCalculation_ = false; + /** @private {!shaka.util.PublicPromise.} */ this.textSequenceModeOffset_ = new shaka.util.PublicPromise(); } @@ -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); @@ -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) {