Skip to content

Commit 20bcfdd

Browse files
srikieonlineSrikanth Peddibhotlaaveladtheodab
authored
fix: Handle updateend event on duration reduction (#8403)
https://www.w3.org/TR/media-source-2/#duration-change-algorithm "Duration reductions that would truncate currently buffered media are disallowed. When truncation is necessary, use remove() to reduce the buffered range before updating duration." Introduced a new config durationReductionEmitsUpdateEnd. When set indicates media source duration change can truncate buffer, hence updateend event is expected on setDuration operation if new duration is smaller than existing value. Disabled by default to match W3C specs. --------- Co-authored-by: Srikanth Peddibhotla <[email protected]> Co-authored-by: Álvaro Velad Galván <[email protected]> Co-authored-by: theodab <[email protected]>
1 parent 1eea1e6 commit 20bcfdd

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

demo/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,9 @@ shakaDemo.Config = class {
702702
.addBoolInput_('Dispatch all emsg boxes',
703703
'mediaSource.dispatchAllEmsgBoxes')
704704
.addBoolInput_('Uses source elements',
705-
'mediaSource.useSourceElements');
705+
'mediaSource.useSourceElements')
706+
.addBoolInput_('Expect updateEnd when duration is truncated',
707+
'mediaSource.durationReductionEmitsUpdateEnd');
706708
}
707709

708710
/** @private */

externs/shaka/player.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2185,7 +2185,8 @@ shaka.extern.NetworkingConfiguration;
21852185
* insertFakeEncryptionInInit: boolean,
21862186
* modifyCueCallback: shaka.extern.TextParser.ModifyCueCallback,
21872187
* dispatchAllEmsgBoxes: boolean,
2188-
* useSourceElements: boolean
2188+
* useSourceElements: boolean,
2189+
* durationReductionEmitsUpdateEnd: boolean
21892190
* }}
21902191
*
21912192
* @description
@@ -2234,6 +2235,17 @@ shaka.extern.NetworkingConfiguration;
22342235
* Disabling it will prevent using AirPlay on MSE.
22352236
* <br>
22362237
* Defaults to <code>true</code>.
2238+
* @property {boolean} durationReductionEmitsUpdateEnd
2239+
* https://www.w3.org/TR/media-source-2/#duration-change-algorithm
2240+
* "Duration reductions that would truncate currently buffered media are
2241+
* disallowed.
2242+
* When truncation is necessary, use remove() to reduce the buffered range
2243+
* before updating duration."
2244+
* When set indicates media source duration change can truncate buffer, hence
2245+
* updateend event is expected on setDuration operation if new duration is
2246+
* smaller than existing value.
2247+
* <br>
2248+
* Defaults to <code>true</code>.
22372249
* @exportDoc
22382250
*/
22392251
shaka.extern.MediaSourceConfiguration;

lib/media/media_source_engine.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,11 +1678,20 @@ shaka.media.MediaSourceEngine = class {
16781678
*/
16791679
async setDuration(duration) {
16801680
await this.enqueueBlockingOperation_(() => {
1681-
// Reducing the duration causes the MSE removal algorithm to run, which
1682-
// triggers an 'updateend' event to fire. To handle this scenario, we
1683-
// have to insert a dummy operation into the beginning of each queue,
1684-
// which the 'updateend' handler will remove.
1685-
if (duration < this.mediaSource_.duration) {
1681+
// https://www.w3.org/TR/media-source-2/#duration-change-algorithm
1682+
// "Duration reductions that would truncate currently buffered media
1683+
// are disallowed.
1684+
// When truncation is necessary, use remove() to reduce the buffered
1685+
// range before updating duration."
1686+
// But in some platforms, truncating the duration causes the
1687+
// buffer range removal algorithm to run which triggers an
1688+
// 'updateend' event to fire.
1689+
// To handle this scenario, we have to insert a dummy operation into
1690+
// the beginning of each queue, which the 'updateend' handler will remove.
1691+
// Using config to disable it by default and enable only
1692+
// on relevant platforms.
1693+
if (this.config_.durationReductionEmitsUpdateEnd &&
1694+
duration < this.mediaSource_.duration) {
16861695
for (const contentType of this.sourceBuffers_.keys()) {
16871696
const dummyOperation = {
16881697
start: () => {},

lib/util/player_configuration.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ shaka.util.PlayerConfiguration = class {
431431
},
432432
dispatchAllEmsgBoxes: false,
433433
useSourceElements: true,
434+
durationReductionEmitsUpdateEnd: true,
434435
};
435436

436437
let customPlayheadTracker = false;

0 commit comments

Comments
 (0)