Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setMediaDuration MSE duration override #79

Merged
merged 6 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,8 @@ declare namespace dashjs {

getInitialMediaSettingsFor(type: MediaType): MediaSettings;

setMediaDuration(duration: number): void;

setCurrentTrack(track: MediaInfo): void;

addABRCustomRule(type: string, rulename: string, rule: object): void;
Expand Down Expand Up @@ -2290,6 +2292,8 @@ declare namespace dashjs {
reset(): void;

getStreams(): any[];

setMediaDuration(duration: number): void;
}

export interface TimeSyncController {
Expand Down
17 changes: 17 additions & 0 deletions src/streaming/MediaPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,22 @@ function MediaPlayer() {
return _getAsUTC(duration());
}

/**
* Use this method to override the duration of the current MediaSource object.
*
* @throws {@link module:MediaPlayer~PLAYBACK_NOT_INITIALIZED_ERROR PLAYBACK_NOT_INITIALIZED_ERROR} if called before initializePlayback function
* @param {number} duration
* @memberof module:MediaPlayer
* @instance
*/
function setMediaDuration (duration) {
if (!playbackInitialized) {
throw PLAYBACK_NOT_INITIALIZED_ERROR;
}

streamController.setMediaDuration(duration)
}

/*
---------------------------------------------------------------------------

Expand Down Expand Up @@ -2502,6 +2518,7 @@ function MediaPlayer() {
getCurrentTrackFor,
setInitialMediaSettingsFor,
getInitialMediaSettingsFor,
setMediaDuration,
setCurrentTrack,
addABRCustomRule,
removeABRCustomRule,
Expand Down
7 changes: 4 additions & 3 deletions src/streaming/controllers/StreamController.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ function StreamController() {
mediaSource.removeEventListener('sourceopen', _onMediaSourceOpen);
mediaSource.removeEventListener('webkitsourceopen', _onMediaSourceOpen);

_setMediaDuration();
setMediaDuration();
const dvrInfo = dashMetrics.getCurrentDVRInfo();
mediaSourceController.setSeekable(dvrInfo.range.start, dvrInfo.range.end);
if (streamActivated) {
Expand Down Expand Up @@ -1037,7 +1037,7 @@ function StreamController() {
* @param {number} duration
* @private
*/
function _setMediaDuration(duration) {
function setMediaDuration(duration) {
const manifestDuration = duration ? duration : getActiveStreamInfo().manifestInfo.duration;
mediaSourceController.setDuration(manifestDuration);
}
Expand Down Expand Up @@ -1440,7 +1440,7 @@ function StreamController() {

function _onManifestValidityChanged(e) {
if (!isNaN(e.newDuration)) {
_setMediaDuration(e.newDuration);
setMediaDuration(e.newDuration);
}
}

Expand Down Expand Up @@ -1623,6 +1623,7 @@ function StreamController() {
getActiveStream,
getInitialPlayback,
getAutoPlay,
setMediaDuration,
reset
};

Expand Down
2 changes: 2 additions & 0 deletions test/unit/mocks/StreamControllerMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class StreamControllerMock {
return true;
}

setMediaDuration() {}

}

export default StreamControllerMock;
14 changes: 14 additions & 0 deletions test/unit/streaming.MediaPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Settings from '../../src/core/Settings';
import ABRRulesCollection from '../../src/streaming/rules/abr/ABRRulesCollection';
import CustomParametersModel from '../../src/streaming/models/CustomParametersModel';

const sinon = require('sinon');
const expect = require('chai').expect;
const ELEMENT_NOT_ATTACHED_ERROR = 'You must first call attachView() to set the video element before calling this method';
const PLAYBACK_NOT_INITIALIZED_ERROR = 'You must first call initialize() and set a valid source and view before calling this method';
Expand Down Expand Up @@ -184,6 +185,9 @@ describe('MediaPlayer', function () {
expect(player.durationAsUTC).to.throw(PLAYBACK_NOT_INITIALIZED_ERROR);
});

it('Method setMediaDuration should throw an exception', function () {
expect(player.setMediaDuration).to.throw(PLAYBACK_NOT_INITIALIZED_ERROR);
});
});

describe('When it is initialized', function () {
Expand Down Expand Up @@ -376,6 +380,16 @@ describe('MediaPlayer', function () {
duration = player.duration();
expect(duration).to.equal(4);
});

it('Method setMediaDuration should call through to streamController', function () {
sinon.spy(streamControllerMock, 'setMediaDuration');

player.setMediaDuration(15);

expect(streamControllerMock.setMediaDuration.calledWith(15)).to.be.true;

streamControllerMock.setMediaDuration.restore();
})
});
});

Expand Down