Skip to content

Commit

Permalink
Fix init value for storeLastSettings (#4221)
Browse files Browse the repository at this point in the history
* Use `lastMediaSettingsCachingInfo` as default value of `storeLastSettings`

* Remove useless function param

* Add `noSettingsSave` flag to MediaPlayer.setCurrentTrack

* Add new setting `saveLastMediaSettingsForCurrentStreamingSession`

* Add test suites to cover new settings flag

* Add missing jsdoc

* Update index.d.ts
  • Loading branch information
minhui-foxtel committed Jul 14, 2023
1 parent ff6884e commit 3e1356f
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 8 deletions.
3 changes: 2 additions & 1 deletion index.d.ts
Expand Up @@ -1042,6 +1042,7 @@ declare namespace dashjs {
enabled?: boolean;
ttl?: number;
};
saveLastMediaSettingsForCurrentStreamingSession?: boolean;
cacheLoadThresholds?: {
video?: number;
audio?: number;
Expand Down Expand Up @@ -1344,7 +1345,7 @@ declare namespace dashjs {

getInitialMediaSettingsFor(type: MediaType): MediaSettings;

setCurrentTrack(track: MediaInfo): void;
setCurrentTrack(track: MediaInfo, noSettingsSave?: boolean): void;

addABRCustomRule(type: string, rulename: string, rule: object): void;

Expand Down
10 changes: 10 additions & 0 deletions samples/dash-if-reference-player/app/main.js
Expand Up @@ -302,6 +302,7 @@ app.controller('DashController', ['$scope', '$window', 'sources', 'contributors'
$scope.scheduleWhilePausedSelected = true;
$scope.calcSegmentAvailabilityRangeFromTimelineSelected = false;
$scope.reuseExistingSourceBuffersSelected = true;
$scope.saveLastMediaSettingsSelected = true;
$scope.localStorageSelected = true;
$scope.jumpGapsSelected = true;
$scope.fastSwitchSelected = true;
Expand Down Expand Up @@ -669,6 +670,14 @@ app.controller('DashController', ['$scope', '$window', 'sources', 'contributors'
});
};

$scope.toggleSaveLastMediaSettings = function () {
$scope.player.updateSettings({
'streaming': {
'saveLastMediaSettingsForCurrentStreamingSession': $scope.saveLastMediaSettingsSelected
}
});
};

$scope.toggleLocalStorage = function () {
$scope.player.updateSettings({
'streaming': {
Expand Down Expand Up @@ -2125,6 +2134,7 @@ app.controller('DashController', ['$scope', '$window', 'sources', 'contributors'
$scope.scheduleWhilePausedSelected = currentConfig.streaming.scheduling.scheduleWhilePaused;
$scope.calcSegmentAvailabilityRangeFromTimelineSelected = currentConfig.streaming.timeShiftBuffer.calcFromSegmentTimeline;
$scope.reuseExistingSourceBuffersSelected = currentConfig.streaming.buffer.reuseExistingSourceBuffers;
$scope.saveLastMediaSettingsSelected = currentConfig.streaming.saveLastMediaSettingsForCurrentStreamingSession;
$scope.localStorageSelected = currentConfig.streaming.lastBitrateCachingInfo.enabled;
$scope.jumpGapsSelected = currentConfig.streaming.gaps.jumpGaps;
}
Expand Down
6 changes: 6 additions & 0 deletions samples/dash-if-reference-player/index.html
Expand Up @@ -188,6 +188,12 @@
ng-checked="reuseExistingSourceBuffersSelected">
Reuse SourceBuffers
</label>
<label class="topcoat-checkbox" data-toggle="tooltip" data-placement="right"
title="Enable media settings from last selected track to be used for incoming track selection.">
<input type="checkbox" ng-model="saveLastMediaSettingsSelected"
ng-change="toggleSaveLastMediaSettings()" ng-checked="saveLastMediaSettingsSelected">
Save last media settings
</label>
<label class="topcoat-checkbox" data-toggle="tooltip" data-placement="right"
title="Enables local storage of player state (last bitrate, a/v or text track etc). This is then used when the next time media is played.">
<input type="checkbox" id="localStorageCB" ng-model="localStorageSelected"
Expand Down
8 changes: 8 additions & 0 deletions src/core/Settings.js
Expand Up @@ -154,6 +154,7 @@ import Events from './events/Events';
* },
* lastBitrateCachingInfo: { enabled: true, ttl: 360000 },
* lastMediaSettingsCachingInfo: { enabled: true, ttl: 360000 },
* saveLastMediaSettingsForCurrentStreamingSession: true,
* cacheLoadThresholds: { video: 50, audio: 5 },
* trackSwitchMode: {
* audio: Constants.TRACK_SWITCH_MODE_ALWAYS_REPLACE,
Expand Down Expand Up @@ -732,6 +733,12 @@ import Events from './events/Events';
* The default expiration is one hour, defined in milliseconds.
*
* If expired, the default initial bit rate (closest to 1000 kbps) will be used for that session and a new bit rate will be stored during that session.
* @property {module:Settings~CachingInfoSettings} [lastMediaSettingsCachingInfo={enabled: true, ttl: 360000}]
* Set to false if you would like to disable the last media settings from being stored to localStorage during playback and used to set the initial track for subsequent playback within the expiration window.
*
* The default expiration is one hour, defined in milliseconds.
* @property {boolean} [saveLastMediaSettingsForCurrentStreamingSession=true]
* Set to true if dash.js should save media settings from last selected track for incoming track selection during current streaming session.
* @property {module:Settings~AudioVideoSettings} [cacheLoadThresholds={video: 50, audio: 5}]
* For a given media type, the threshold which defines if the response to a fragment request is coming from browser cache or not.
* @property {module:Settings~AudioVideoSettings} [trackSwitchMode={video: "neverReplace", audio: "alwaysReplace"}]
Expand Down Expand Up @@ -924,6 +931,7 @@ function Settings() {
enabled: true,
ttl: 360000
},
saveLastMediaSettingsForCurrentStreamingSession: true,
cacheLoadThresholds: {
video: 50,
audio: 5
Expand Down
5 changes: 3 additions & 2 deletions src/streaming/MediaPlayer.js
Expand Up @@ -1614,15 +1614,16 @@ function MediaPlayer() {

/**
* @param {MediaInfo} track - instance of {@link MediaInfo}
* @param {boolean} [noSettingsSave] - specify if settings from the track must not be saved for incoming track selection
* @memberof module:MediaPlayer
* @throws {@link module:MediaPlayer~STREAMING_NOT_INITIALIZED_ERROR STREAMING_NOT_INITIALIZED_ERROR} if called before initializePlayback function
* @instance
*/
function setCurrentTrack(track) {
function setCurrentTrack(track, noSettingsSave = false) {
if (!streamingInitialized) {
throw STREAMING_NOT_INITIALIZED_ERROR;
}
mediaController.setTrack(track);
mediaController.setTrack(track, noSettingsSave);
}

/*
Expand Down
12 changes: 7 additions & 5 deletions src/streaming/controllers/MediaController.js
Expand Up @@ -89,7 +89,7 @@ function MediaController() {
setTrack(selectInitialTrack(type, tracksForType), true);
} else {
if (tracks.length > 1) {
setTrack(selectInitialTrack(type, tracks, !!lastSelectedTracks[type]));
setTrack(selectInitialTrack(type, tracks));
} else {
setTrack(tracks[0]);
}
Expand Down Expand Up @@ -601,25 +601,27 @@ function MediaController() {


function createTrackInfo() {
const storeLastSettings = settings.get().streaming.saveLastMediaSettingsForCurrentStreamingSession;

return {
audio: {
list: [],
storeLastSettings: true,
storeLastSettings,
current: null
},
video: {
list: [],
storeLastSettings: true,
storeLastSettings,
current: null
},
text: {
list: [],
storeLastSettings: true,
storeLastSettings,
current: null
},
image: {
list: [],
storeLastSettings: true,
storeLastSettings,
current: null
}
};
Expand Down
63 changes: 63 additions & 0 deletions test/unit/streaming.controllers.MediaController.js
Expand Up @@ -639,6 +639,69 @@ describe('MediaController', function () {
expect(objectUtils.areEqual(currentTrack, esTrack)).to.be.true;
});

it('should not check initial media settings to choose initial track when it has already selected a track', function () {
mediaController.addTrack(frTrack);
mediaController.addTrack(qtzTrack);

let trackList = mediaController.getTracksFor(trackType, streamInfo.id);
expect(trackList).to.have.lengthOf(2);
expect(objectUtils.areEqual(trackList[0], frTrack)).to.be.true;
expect(objectUtils.areEqual(trackList[1], qtzTrack)).to.be.true;

let currentTrack = mediaController.getCurrentTrackFor(trackType, streamInfo.id);
expect(objectUtils.areEqual(currentTrack, frTrack)).to.be.false;
expect(objectUtils.areEqual(currentTrack, qtzTrack)).to.be.false;

mediaController.setInitialSettings(trackType, {
lang: 'fr'
});
mediaController.setInitialMediaSettingsForType(trackType, streamInfo);

currentTrack = mediaController.getCurrentTrackFor(trackType, streamInfo.id);
expect(objectUtils.areEqual(currentTrack, frTrack)).to.be.true;

// pretend we're switching period, which will call setInitialMediaSettingsForType again
mediaController.setInitialSettings(trackType, {
lang: 'qtz'
});
mediaController.setInitialMediaSettingsForType(trackType, streamInfo);

currentTrack = mediaController.getCurrentTrackFor(trackType, streamInfo.id);
expect(objectUtils.areEqual(currentTrack, frTrack)).to.be.true;
});

it('should always check initial media settings to choose initial track when saveLastMediaSettingsForCurrentStreamingSession is disabled', function () {
settings.update({ streaming: { saveLastMediaSettingsForCurrentStreamingSession: false } });

mediaController.addTrack(frTrack);
mediaController.addTrack(qtzTrack);

let trackList = mediaController.getTracksFor(trackType, streamInfo.id);
expect(trackList).to.have.lengthOf(2);
expect(objectUtils.areEqual(trackList[0], frTrack)).to.be.true;
expect(objectUtils.areEqual(trackList[1], qtzTrack)).to.be.true;

let currentTrack = mediaController.getCurrentTrackFor(trackType, streamInfo.id);
expect(objectUtils.areEqual(currentTrack, frTrack)).to.be.false;
expect(objectUtils.areEqual(currentTrack, qtzTrack)).to.be.false;

mediaController.setInitialSettings(trackType, {
lang: 'fr'
});
mediaController.setInitialMediaSettingsForType(trackType, streamInfo);

currentTrack = mediaController.getCurrentTrackFor(trackType, streamInfo.id);
expect(objectUtils.areEqual(currentTrack, frTrack)).to.be.true;

// pretend we're switching period, which will call setInitialMediaSettingsForType again
mediaController.setInitialSettings(trackType, {
lang: 'qtz'
});
mediaController.setInitialMediaSettingsForType(trackType, streamInfo);

currentTrack = mediaController.getCurrentTrackFor(trackType, streamInfo.id);
expect(objectUtils.areEqual(currentTrack, qtzTrack)).to.be.true;
});
});

describe('Initial Track Selection', function () {
Expand Down

0 comments on commit 3e1356f

Please sign in to comment.