Skip to content

Commit

Permalink
feat: Add getNonDefaultConfiguration (#6620)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored May 17, 2024
1 parent 02c5b43 commit 907e6ba
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ assignees: ''
**What configuration are you using? What is the output of `player.getConfiguration()`?**
<!-- NOTE:
You can censor URLs to keep them private, but include them in the email. You
can also use JSON.stringify(obj, null, 2) to print nicely on platforms that
don't print objects well in the console. DON'T SEND '[object Object]'!
can also use JSON.stringify(player.getNonDefaultConfiguration(), null, 2) to
print nicely on platforms that don't print objects well in the console.
DON'T SEND '[object Object]'!
-->


Expand Down
18 changes: 18 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -3596,6 +3596,24 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
return ret;
}

/**
* Return a copy of the current non default configuration. Modifications of
* the returned value will not affect the Player's active configuration.
* You must call <code>player.configure()</code> to make changes.
*
* @return {!Object}
* @export
*/
getNonDefaultConfiguration() {
goog.asserts.assert(this.config_, 'Config must not be null!');

const ret = this.defaultConfig_();
shaka.util.PlayerConfiguration.mergeConfigObjects(
ret, this.config_, this.defaultConfig_());
return shaka.util.ConfigUtils.getDifferenceFromConfigObjects(
this.config_, this.defaultConfig_());
}

/**
* Return a reference to the current configuration. Modifications to the
* returned value will affect the Player's active configuration. This method
Expand Down
53 changes: 53 additions & 0 deletions lib/util/config_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,57 @@ shaka.util.ConfigUtils = class {
static referenceParametersAndReturn(parameters, returnValue) {
return parameters && returnValue;
}

/**
* @param {!Object} object
* @param {!Object} base
* @return {!Object}
* @export
*/
static getDifferenceFromConfigObjects(object, base) {
const isObject = (obj) => {
return obj && typeof obj === 'object' && !Array.isArray(obj);
};

const isArrayEmpty = (array) => {
return Array.isArray(array) && array.length === 0;
};

const changes = (object, base) => {
return Object.fromEntries(
Object.entries(object).reduce((acc, [key, value]) => {
// eslint-disable-next-line no-prototype-builtins
if (!base.hasOwnProperty(key)) {
acc.push([key, value]);
} else if (isObject(value) && isObject(base[key])) {
const diff = changes(value, base[key]);
if (Object.keys(diff).length > 0 || !isObject(diff)) {
acc.push([key, diff]);
}
} else if (isArrayEmpty(value) && isArrayEmpty(base[key])) {
// Do nothing if both are empty arrays
} else if (value !== base[key]) {
acc.push([key, value]);
}
return acc;
}, []));
};

const diff = changes(object, base);

const removeEmpty = (obj) => {
for (const key of Object.keys(obj)) {
if (isObject(obj[key]) && Object.keys(obj[key]).length === 0) {
delete obj[key];
} else if (isArrayEmpty(obj[key])) {
delete obj[key];
} else if (isObject(obj[key])) {
removeEmpty(obj[key]);
}
}
};

removeEmpty(diff);
return diff;
}
};
1 change: 1 addition & 0 deletions test/cast/cast_utils_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('CastUtils', () => {
'releaseAllMutexes', // Very specific to the inner workings of the player.
'unloadAndSavePreload',
'preload',
'getNonDefaultConfiguration',

// Test helper methods (not @export'd)
'createDrmEngine',
Expand Down
11 changes: 11 additions & 0 deletions test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,17 @@ describe('Player', () => {
});
});

it('getNonDefaultConfiguration', () => {
player.configure({
drm: {
retryParameters: {backoffFactor: 5},
},
});
const nonDefaultConfiguration = player.getNonDefaultConfiguration();
const config = player.getConfiguration();
expect(nonDefaultConfiguration).not.toBe(config);
});

describe('configure', () => {
it('overwrites defaults', () => {
const defaultConfig = player.getConfiguration();
Expand Down

0 comments on commit 907e6ba

Please sign in to comment.