From 9646ab105f007319b23c0e13ec4512e5284f6941 Mon Sep 17 00:00:00 2001 From: marcovidonis Date: Wed, 22 Jan 2025 12:48:44 +0100 Subject: [PATCH 1/2] add support for encodings priority option when adding tracks to a peer connection --- lib/RTCSession.js | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/RTCSession.js b/lib/RTCSession.js index 7a1dfd63..7c7dd713 100644 --- a/lib/RTCSession.js +++ b/lib/RTCSession.js @@ -278,6 +278,7 @@ module.exports = class RTCSession extends EventEmitter const pcConfig = Utils.cloneObject(options.pcConfig, { iceServers: [] }); const rtcConstraints = options.rtcConstraints || null; const rtcOfferConstraints = options.rtcOfferConstraints || null; + const encodingsPriority = options.encodingsPriority || 'low'; this._rtcOfferConstraints = rtcOfferConstraints; this._rtcAnswerConstraints = options.rtcAnswerConstraints || null; @@ -395,7 +396,7 @@ module.exports = class RTCSession extends EventEmitter this._newRTCSession('local', this._request); - this._sendInitialRequest(mediaConstraints, rtcOfferConstraints, mediaStream); + this._sendInitialRequest(mediaConstraints, rtcOfferConstraints, mediaStream, encodingsPriority); } init_incoming(request, initCallback) @@ -503,6 +504,24 @@ module.exports = class RTCSession extends EventEmitter this._progress('local', null); } + addStreamTracksWithPriority(stream, priority) { + logger.debug('Adding stream tracks with priority: %s', priority); + stream.getTracks().forEach((track) => { + let sender = this._connection.addTrack(track, stream); + const params = sender.getParameters(); + params.encodings = params.encodings.map((e) => { + if (e.priority) { + e.priority = priority; + } + if (e.networkPriority) { + e.networkPriority = priority; + } + return e; + }); + sender.setParameters(params); + }); + } + /** * Answer the call. */ @@ -518,6 +537,7 @@ module.exports = class RTCSession extends EventEmitter const rtcConstraints = options.rtcConstraints || null; const rtcAnswerConstraints = options.rtcAnswerConstraints || null; const rtcOfferConstraints = Utils.cloneObject(options.rtcOfferConstraints); + const encodingsPriority = options.encodingsPriority || 'low'; let tracks; let peerHasAudioLine = false; @@ -695,10 +715,7 @@ module.exports = class RTCSession extends EventEmitter this._localMediaStream = stream; if (stream) { - stream.getTracks().forEach((track) => - { - this._connection.addTrack(track, stream); - }); + this.addStreamTracksWithPriority(stream, encodingsPriority); } }) // Set remote description. @@ -2594,7 +2611,7 @@ module.exports = class RTCSession extends EventEmitter /** * Initial Request Sender */ - _sendInitialRequest(mediaConstraints, rtcOfferConstraints, mediaStream) + _sendInitialRequest(mediaConstraints, rtcOfferConstraints, mediaStream, encodingsPriority) { const request_sender = new RequestSender(this._ua, this._request, { onRequestTimeout : () => @@ -2661,10 +2678,7 @@ module.exports = class RTCSession extends EventEmitter if (stream) { - stream.getTracks().forEach((track) => - { - this._connection.addTrack(track, stream); - }); + this.addStreamTracksWithPriority(stream, encodingsPriority); } // TODO: should this be triggered here? From 800960750c007a82cd95d326d2688139866b161f Mon Sep 17 00:00:00 2001 From: marcovidonis Date: Wed, 22 Jan 2025 14:36:57 +0100 Subject: [PATCH 2/2] check priority value before setting encodings --- lib/RTCSession.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/RTCSession.js b/lib/RTCSession.js index 7c7dd713..ceedd5d8 100644 --- a/lib/RTCSession.js +++ b/lib/RTCSession.js @@ -278,7 +278,7 @@ module.exports = class RTCSession extends EventEmitter const pcConfig = Utils.cloneObject(options.pcConfig, { iceServers: [] }); const rtcConstraints = options.rtcConstraints || null; const rtcOfferConstraints = options.rtcOfferConstraints || null; - const encodingsPriority = options.encodingsPriority || 'low'; + const encodingsPriority = options.encodingsPriority; this._rtcOfferConstraints = rtcOfferConstraints; this._rtcAnswerConstraints = options.rtcAnswerConstraints || null; @@ -508,17 +508,19 @@ module.exports = class RTCSession extends EventEmitter logger.debug('Adding stream tracks with priority: %s', priority); stream.getTracks().forEach((track) => { let sender = this._connection.addTrack(track, stream); - const params = sender.getParameters(); - params.encodings = params.encodings.map((e) => { - if (e.priority) { - e.priority = priority; - } - if (e.networkPriority) { - e.networkPriority = priority; - } - return e; - }); - sender.setParameters(params); + if (priority && ['very-low', 'low', 'medium', 'high'].includes(priority)) { + const params = sender.getParameters(); + params.encodings = params.encodings.map((e) => { + if (e.priority) { + e.priority = priority; + } + if (e.networkPriority) { + e.networkPriority = priority; + } + return e; + }); + sender.setParameters(params); + } }); } @@ -537,7 +539,7 @@ module.exports = class RTCSession extends EventEmitter const rtcConstraints = options.rtcConstraints || null; const rtcAnswerConstraints = options.rtcAnswerConstraints || null; const rtcOfferConstraints = Utils.cloneObject(options.rtcOfferConstraints); - const encodingsPriority = options.encodingsPriority || 'low'; + const encodingsPriority = options.encodingsPriority; let tracks; let peerHasAudioLine = false;