Skip to content

Commit

Permalink
feat(mediasoup: add configurable nice for media type workers
Browse files Browse the repository at this point in the history
Similar in spirit to 66d443d, but
targeted at mediasoup workers. The idea here is allow prioritization of
workers by media type (eg audio should be higher priority than
main/content workers).

Also adds a couple of fixes to priority config parsing.
  • Loading branch information
prlanzarin committed Aug 15, 2022
1 parent 66d443d commit 3e24512
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 3 deletions.
10 changes: 10 additions & 0 deletions config/custom-environment-variables.yml
Expand Up @@ -204,6 +204,16 @@ mediasoup:
content:
__name: MS_CONTENT_WORKERS
__format: yaml
workerPriorities:
audio:
__name: MS_AUDIO_PRIO
__format: yaml
main:
__name: MS_MAIN_PRIO
__format: yaml
content:
__name: MS_CONTENT_PRIO
__format: yaml
worker:
rtcMinPort:
__name: MS_RTC_MIN
Expand Down
12 changes: 12 additions & 0 deletions config/default.example.yml
Expand Up @@ -312,6 +312,18 @@ mediasoup:
audio: 0
main: 0
content: 0
# mediasoup.workerPriorities:
# audio: <os.constants.priority> | [19,-20]
# main: <os.constants.priority> | [19,-20]
# content: <os.constants.priority> | [19,-20]
#
# * Sets the NICE level for each worker type. The app needs the correct user
# caps to be effective
#
#workerPriorities:
# audio: PRIORITY_HIGH
# main: PRIORITY_NORMAL
# content: PRIORITY_LOW
worker:
# "debug", "warn", "error", "none"
logLevel: "error"
Expand Down
4 changes: 3 additions & 1 deletion lib/common/utils.js
Expand Up @@ -29,7 +29,9 @@ const addBwToSpecContentType = (spec, bitrate) => {

const _getNormalizedPriority = (prio) => {
if (typeof prio === 'number') return prio;
if (typeof prio === 'string' && os.constants.priority[prio]) return os.constants.priority[prio];
if (typeof prio === 'string' && typeof os.constants.priority[prio] === 'number') {
return os.constants.priority[prio];
}

throw new TypeError('Invalid priority')
}
Expand Down
2 changes: 1 addition & 1 deletion lib/main/process-wrapper.js
Expand Up @@ -81,7 +81,7 @@ module.exports = class ProcessWrapper {
this.process.on('message', this._onMessage);
this.process.on('error', this.onError.bind(this));

if (this.priority) setProcessPriority(this.process.pid, this.priority);
if (this.priority != null) setProcessPriority(this.process.pid, this.priority);

PrometheusAgent.set(SFUM_NAMES.MODULE_STATUS, 1, {
module: this.name,
Expand Down
2 changes: 2 additions & 0 deletions lib/mcs-core/lib/adapters/mediasoup/configs.js
Expand Up @@ -12,6 +12,7 @@ const {
[C.MEDIA_PROFILE.CONTENT]: 0,
[C.MEDIA_PROFILE.AUDIO]: 0,
},
workerPriorities: WORKER_PRIORITIES = {},
promExportWorkerResourceUsage: WORKER_EXPORT_RESOURCE_USAGE,
worker: WORKER_SETTINGS,
router: ROUTER_SETTINGS = { mediaCodecs: mediasoup.getSupportedRtpCapabilities().codecs },
Expand Down Expand Up @@ -75,6 +76,7 @@ module.exports = {
DEFAULT_MAX_BW: 0,
SHARED_POOL_WORKERS,
DEDICATED_MEDIA_TYPE_WORKERS,
WORKER_PRIORITIES,
WORKER_SETTINGS,
WORKER_EXPORT_RESOURCE_USAGE,
ROUTER_SETTINGS,
Expand Down
2 changes: 2 additions & 0 deletions lib/mcs-core/lib/adapters/mediasoup/utils.js
Expand Up @@ -2,6 +2,7 @@ const { MEDIA_TYPE, MEDIA_PROFILE } = require('../../constants/constants');
const { MS_KINDS } = require('./constants.js');
const { extractCodecsListFromSDP } = require('./sdp-translator.js');
const Logger = require('../../utils/logger');
const { setProcessPriority } = require('../../../../common/utils.js');

const getMappedTransportType = (mcsCoreType) => {
switch (mcsCoreType) {
Expand Down Expand Up @@ -108,4 +109,5 @@ module.exports = {
filterValidMediaTypes,
mapConnectionTypeToKind,
replaceRouterCodecsWithSdpCodecs,
setProcessPriority,
}
7 changes: 6 additions & 1 deletion lib/mcs-core/lib/adapters/mediasoup/workers.js
Expand Up @@ -6,11 +6,12 @@ const C = require('../../constants/constants');
const { handleError } = require('./errors.js');
const Logger = require('../../utils/logger');
const {
WORKER_SETTINGS, WORKER_EXPORT_RESOURCE_USAGE,
WORKER_SETTINGS, WORKER_EXPORT_RESOURCE_USAGE, WORKER_PRIORITIES,
} = require('./configs.js');
const {
PrometheusAgent, MS_METRIC_NAMES, exportWorkerResourceUsageMetrics
} = require('./prom-metrics.js');
const { setProcessPriority } = require('./utils.js');

const SHARED_POOL_ID = 'shared';

Expand Down Expand Up @@ -216,6 +217,10 @@ const createDedicatedMediaTypeWorkers = (
for (let i = 0; i < numberOfWorkers; i++) {
createWorker({ workerSettings, mediaType }).then(worker => {
storeWorker(worker);

if (WORKER_PRIORITIES[mediaType] != null) {
setProcessPriority(worker.pid, WORKER_PRIORITIES[mediaType]);
}
}).catch(error => {
Logger.error(`mediasoup: worker creation failed - ${mediaType}`,
{ mediaType, errorMessage: error.message, errorCode: error.code });
Expand Down

0 comments on commit 3e24512

Please sign in to comment.