From 1762267d356a04217340ec792c51ceadb842cd6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Mon, 9 Jan 2023 19:13:40 +0100 Subject: [PATCH] fix: Fix media source duration when using sequence mode (#4848) Tested with https://pub-e06bd0d1422649108618e7121f2af8b5.r2.dev/finite.m3u8 --- lib/media/media_source_engine.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/media/media_source_engine.js b/lib/media/media_source_engine.js index e8ce3aebed..1487990093 100644 --- a/lib/media/media_source_engine.js +++ b/lib/media/media_source_engine.js @@ -658,6 +658,8 @@ shaka.media.MediaSourceEngine = class { // into extracting a timestamp for us to align text segments in sequence // mode. + const duration = this.mediaSource_.duration; + // 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. @@ -701,7 +703,7 @@ shaka.media.MediaSourceEngine = class { // Clear the buffer. await this.enqueueOperation_( contentType, - () => this.remove_(contentType, 0, this.mediaSource_.duration)); + () => this.remove_(contentType, 0, duration)); // Finally, flush the buffer in case of choppy video start on HLS fMP4. if (contentType == ContentType.VIDEO) { @@ -713,6 +715,10 @@ shaka.media.MediaSourceEngine = class { // Now switch to sequence mode and fall through to our normal operations. sourceBuffer.mode = SEQUENCE; + + // When we change the buffer mode the duration is lost, so we need to set + // it explicitly. + await this.setDuration(duration); } if (reference && this.sequenceMode_ && contentType != ContentType.TEXT) {