Skip to content

Commit

Permalink
Fix setting MediaSource's duration (#3532)
Browse files Browse the repository at this point in the history
* Check if no SourceBuffer is updating when setting MediaSource duration

* Check if no SourceBuffer is updating when setting MediaSource duration

* fix MediaSourceController unit tests
  • Loading branch information
bbert authored Feb 8, 2021
1 parent ef29480 commit ecb4fc4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
21 changes: 18 additions & 3 deletions src/streaming/controllers/MediaSourceController.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@ function MediaSourceController() {
}

function setDuration(source, value) {
if (!source || source.readyState !== 'open') return;
if (value === null && isNaN(value)) return;
if (source.duration === value) return;

if (source.duration != value)
if (!isBufferUpdating(source)) {
logger.info('Set MediaSource duration:' + value);
source.duration = value;

return source.duration;
} else {
setTimeout(setDuration.bind(null, source, value), 50);
}
}

function setSeekable(source, start, end) {
Expand Down Expand Up @@ -104,6 +109,16 @@ function MediaSourceController() {
source.endOfStream();
}

function isBufferUpdating(source) {
let buffers = source.sourceBuffers;
for (let i = 0; i < buffers.length; i++) {
if (buffers[i].updating) {
return true;
}
}
return false;
}

instance = {
createMediaSource: createMediaSource,
attachMediaSource: attachMediaSource,
Expand Down
6 changes: 1 addition & 5 deletions src/streaming/controllers/StreamController.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,7 @@ function StreamController() {

function setMediaDuration(duration) {
const manifestDuration = duration ? duration : getActiveStreamInfo().manifestInfo.duration;

if (manifestDuration && !isNaN(manifestDuration)) {
const mediaDuration = mediaSourceController.setDuration(mediaSource, manifestDuration);
logger.debug('Duration successfully set to: ' + mediaDuration);
}
mediaSourceController.setDuration(mediaSource, manifestDuration);
}

function getComposedStream(streamInfo) {
Expand Down
10 changes: 7 additions & 3 deletions test/unit/streaming.controllers.MediaSourceController.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ describe('MediaSourceController', function () {

it('should update source duration', function () {

let source = {};
let duration = mediaSourceController.setDuration(source, 'duration');
expect(duration).to.equal('duration');
let source = {
readyState: 'open',
sourceBuffers: [],
duration: NaN
};
mediaSourceController.setDuration(source, 'duration');
expect(source.duration).to.equal('duration');

});

Expand Down

0 comments on commit ecb4fc4

Please sign in to comment.