Browse Source

feat: Apply max bitrates on video sender (#1275)

* feat: Apply max bitrates on video sender
Add the ability to control the max bitrates on the video sender through a config.js videoQuality setting.

* ref: Use '_' prefix only for internal methods
master
Jaya Allamsetty 4 years ago
parent
commit
f74cd0abe9
No account linked to committer's email address

+ 14
- 9
modules/RTC/TPCUtils.js View File

@@ -20,10 +20,15 @@ export const SIM_LAYER_RIDS = [ SIM_LAYER_1_RID, SIM_LAYER_2_RID, SIM_LAYER_3_RI
20 20
  */
21 21
 export class TPCUtils {
22 22
     /**
23
-     * @constructor
23
+     * Creates a new instance for a given TraceablePeerConnection
24
+     *
25
+     * @param peerconnection - the tpc instance for which we have utility functions.
26
+     * @param videoBitrates - the bitrates to be configured on the video senders when
27
+     * simulcast is enabled.
24 28
      */
25
-    constructor(peerconnection) {
29
+    constructor(peerconnection, videoBitrates) {
26 30
         this.pc = peerconnection;
31
+        this.videoBitrates = videoBitrates;
27 32
 
28 33
         /**
29 34
          * The simulcast encodings that will be configured on the RTCRtpSender
@@ -32,19 +37,19 @@ export class TPCUtils {
32 37
         this.simulcastEncodings = [
33 38
             {
34 39
                 active: true,
35
-                maxBitrate: browser.isFirefox() ? 2500000 : 200000,
40
+                maxBitrate: browser.isFirefox() ? this.videoBitrates.high : this.videoBitrates.low,
36 41
                 rid: SIM_LAYER_1_RID,
37 42
                 scaleResolutionDownBy: browser.isFirefox() ? 1.0 : 4.0
38 43
             },
39 44
             {
40 45
                 active: true,
41
-                maxBitrate: 700000,
46
+                maxBitrate: this.videoBitrates.standard,
42 47
                 rid: SIM_LAYER_2_RID,
43 48
                 scaleResolutionDownBy: 2.0
44 49
             },
45 50
             {
46 51
                 active: true,
47
-                maxBitrate: browser.isFirefox() ? 200000 : 2500000,
52
+                maxBitrate: browser.isFirefox() ? this.videoBitrates.low : this.videoBitrates.high,
48 53
                 rid: SIM_LAYER_3_RID,
49 54
                 scaleResolutionDownBy: browser.isFirefox() ? 4.0 : 1.0
50 55
             }
@@ -65,7 +70,7 @@ export class TPCUtils {
65 70
      * description.
66 71
      * @private
67 72
      */
68
-    _ensureCorrectOrderOfSsrcs(description) {
73
+    ensureCorrectOrderOfSsrcs(description) {
69 74
         const parsedSdp = transform.parse(description.sdp);
70 75
 
71 76
         parsedSdp.media.forEach(mLine => {
@@ -113,7 +118,7 @@ export class TPCUtils {
113 118
      * @return {Object} A session description (same format as above) object
114 119
      * with its sdp field modified to advertise simulcast receive support
115 120
      */
116
-    _insertUnifiedPlanSimulcastReceive(desc) {
121
+    insertUnifiedPlanSimulcastReceive(desc) {
117 122
         // a=simulcast line is not needed on browsers where
118 123
         // we munge SDP for turning on simulcast. Remove this check
119 124
         // when we move to RID/MID based simulcast on all browsers.
@@ -182,7 +187,7 @@ export class TPCUtils {
182 187
      * @param {MediaStreamTrack} track - the local video track.
183 188
      * @returns {void}
184 189
      */
185
-    _setSimulcastStreamConstraints(track) {
190
+    setSimulcastStreamConstraints(track) {
186 191
         if (browser.isReactNative()) {
187 192
             return;
188 193
         }
@@ -230,7 +235,7 @@ export class TPCUtils {
230 235
 
231 236
         // Construct the simulcast stream constraints for the newly added track.
232 237
         if (localTrack.isVideoTrack() && localTrack.videoType === VideoType.CAMERA && this.pc.isSimulcastOn()) {
233
-            this._setSimulcastStreamConstraints(localTrack.getTrack());
238
+            this.setSimulcastStreamConstraints(localTrack.getTrack());
234 239
         }
235 240
     }
236 241
 

+ 37
- 12
modules/RTC/TraceablePeerConnection.js View File

@@ -25,10 +25,13 @@ import { SIM_LAYER_RIDS, TPCUtils } from './TPCUtils';
25 25
 // FIXME SDP tools should end up in some kind of util module
26 26
 
27 27
 const logger = getLogger(__filename);
28
-const MAX_BITRATE = 2500000;
29
-const DESKSTOP_SHARE_RATE = 500000;
30 28
 const DEGRADATION_PREFERENCE_CAMERA = 'maintain-framerate';
31 29
 const DEGRADATION_PREFERENCE_DESKTOP = 'maintain-resolution';
30
+const DESKSTOP_SHARE_RATE = 500000;
31
+const HD_BITRATE = 2500000;
32
+const LD_BITRATE = 200000;
33
+const SD_BITRATE = 700000;
34
+
32 35
 /* eslint-disable max-params */
33 36
 
34 37
 /**
@@ -209,7 +212,23 @@ export default function TraceablePeerConnection(
209 212
 
210 213
     this.peerconnection
211 214
         = new RTCUtils.RTCPeerConnectionType(iceConfig, constraints);
212
-    this.tpcUtils = new TPCUtils(this);
215
+
216
+    // The standard video bitrates are used in Unified plan when switching
217
+    // between camera/desktop tracks on the same sender.
218
+    const standardVideoBitrates = {
219
+        low: LD_BITRATE,
220
+        standard: SD_BITRATE,
221
+        high: HD_BITRATE
222
+    };
223
+
224
+    // Check if the max. bitrates for video are specified through config.js
225
+    // videoQuality settings. These bitrates will be applied on all browsers
226
+    // for camera sources in simulcast mode.
227
+    const videoBitrates = this.options.videoQuality
228
+        ? this.options.videoQuality.maxBitratesVideo
229
+        : standardVideoBitrates;
230
+
231
+    this.tpcUtils = new TPCUtils(this, videoBitrates);
213 232
     this.updateLog = [];
214 233
     this.stats = {};
215 234
     this.statsinterval = null;
@@ -1526,7 +1545,7 @@ TraceablePeerConnection.prototype.addTrack = function(track, isInitiator = false
1526 1545
 
1527 1546
     // Construct the simulcast stream constraints for the newly added track.
1528 1547
     if (track.isVideoTrack() && track.videoType === VideoType.CAMERA && this.isSimulcastOn()) {
1529
-        this.tpcUtils._setSimulcastStreamConstraints(track.getTrack());
1548
+        this.tpcUtils.setSimulcastStreamConstraints(track.getTrack());
1530 1549
     }
1531 1550
 };
1532 1551
 
@@ -1982,15 +2001,21 @@ TraceablePeerConnection.prototype.setSenderVideoDegradationPreference = function
1982 2001
  * @param {JitsiLocalTrack} localTrack - the local track whose
1983 2002
  * max bitrate is to be configured.
1984 2003
  */
1985
-TraceablePeerConnection.prototype.setMaxBitRate = function(localTrack) {
2004
+TraceablePeerConnection.prototype.setMaxBitRate = function(localTrack = null) {
2005
+    if (!localTrack) {
2006
+        // eslint-disable-next-line no-param-reassign
2007
+        localTrack = Array.from(this.localTracks.values()).find(t => t.isVideoTrack());
2008
+    }
1986 2009
     const trackId = localTrack.track.id;
1987 2010
     const videoType = localTrack.videoType;
1988 2011
 
1989 2012
     // No need to set max bitrates on the streams in the following cases.
1990 2013
     // 1. When a 'camera' track is replaced in plan-b mode, since its a new sender.
1991 2014
     // 2. When the config.js option for capping the SS bitrate is not enabled.
1992
-    if ((browser.usesPlanB() && !this.options.capScreenshareBitrate)
1993
-        || (browser.usesPlanB() && videoType === VideoType.CAMERA)) {
2015
+    // The above two conditions are ignored When max video bitrates are specified through config.js.
2016
+    if (((browser.usesPlanB() && !this.options.capScreenshareBitrate)
2017
+        || (browser.usesPlanB() && videoType === VideoType.CAMERA))
2018
+        && !(this.options.videoQuality && this.options.videoQuality.maxBitratesVideo)) {
1994 2019
         return;
1995 2020
     }
1996 2021
     if (!this.peerconnection.getSenders) {
@@ -2017,15 +2042,15 @@ TraceablePeerConnection.prototype.setMaxBitRate = function(localTrack) {
2017 2042
                         // capScreenshareBitrate is enabled through config.js and presenter
2018 2043
                         // is not turned on.
2019 2044
                         parameters.encodings[encoding].maxBitrate
2020
-                            = browser.usesPlanB()
2021
-                                ? presenterEnabled ? MAX_BITRATE : DESKSTOP_SHARE_RATE
2045
+                            = browser.usesPlanB() && videoType === VideoType.DESKTOP
2046
+                                ? presenterEnabled ? HD_BITRATE : DESKSTOP_SHARE_RATE
2022 2047
 
2023 2048
                                 // In unified plan, simulcast for SS is on by default.
2024 2049
                                 // When simulcast is disabled through a config.js option,
2025 2050
                                 // we cap the bitrate on desktop and camera tracks to 2500 Kbps.
2026 2051
                                 : this.isSimulcastOn()
2027 2052
                                     ? this.tpcUtils.simulcastEncodings[encoding].maxBitrate
2028
-                                    : MAX_BITRATE;
2053
+                                    : HD_BITRATE;
2029 2054
                     }
2030 2055
                 }
2031 2056
                 sender.setParameters(parameters);
@@ -2078,13 +2103,13 @@ TraceablePeerConnection.prototype.setRemoteDescription = function(description) {
2078 2103
             description = this.simulcast.mungeRemoteDescription(description);
2079 2104
 
2080 2105
             // eslint-disable-next-line no-param-reassign
2081
-            description = this.tpcUtils._insertUnifiedPlanSimulcastReceive(description);
2106
+            description = this.tpcUtils.insertUnifiedPlanSimulcastReceive(description);
2082 2107
             this.trace(
2083 2108
                 'setRemoteDescription::postTransform (sim receive)',
2084 2109
                 dumpSDP(description));
2085 2110
 
2086 2111
             // eslint-disable-next-line no-param-reassign
2087
-            description = this.tpcUtils._ensureCorrectOrderOfSsrcs(description);
2112
+            description = this.tpcUtils.ensureCorrectOrderOfSsrcs(description);
2088 2113
         }
2089 2114
     }
2090 2115
 

+ 3
- 0
modules/qualitycontrol/QualityController.js View File

@@ -46,6 +46,9 @@ export class QualityController {
46 46
 
47 47
         // Set the degradation preference on the local video track.
48 48
         mediaSession.setSenderVideoDegradationPreference();
49
+
50
+        // Set the max bitrates on video sender if they are specified in config.js videoQuality settings.
51
+        mediaSession.setSenderMaxBitrates();
49 52
     }
50 53
 
51 54
     /**

+ 17
- 3
modules/xmpp/JingleSessionPC.js View File

@@ -329,6 +329,7 @@ export default class JingleSessionPC extends JingleSession {
329 329
         }
330 330
         pcOptions.capScreenshareBitrate = false;
331 331
         pcOptions.enableInsertableStreams = options.enableInsertableStreams;
332
+        pcOptions.videoQuality = options.videoQuality;
332 333
 
333 334
         if (this.isP2P) {
334 335
             // simulcast needs to be disabled for P2P (121) calls
@@ -1394,6 +1395,18 @@ export default class JingleSessionPC extends JingleSession {
1394 1395
             IQ_TIMEOUT);
1395 1396
     }
1396 1397
 
1398
+    /**
1399
+     * Sets the maximum bitrates on the local video track if the current
1400
+     * session is a JVB session. Bitrate values from videoQuality settings
1401
+     * in config.js will be used for configuring the sender.
1402
+     * @returns {void}
1403
+     */
1404
+    setSenderMaxBitrates() {
1405
+        if (this._assertNotEnded() && !this.isP2P) {
1406
+            return this.peerconnection.setMaxBitRate();
1407
+        }
1408
+    }
1409
+
1397 1410
     /**
1398 1411
      * Sets the resolution constraint on the local camera track.
1399 1412
      * @param {number} maxFrameHeight - The user preferred max frame height.
@@ -2071,11 +2084,12 @@ export default class JingleSessionPC extends JingleSession {
2071 2084
         return this._addRemoveTrackAsMuteUnmute(
2072 2085
             false /* add as unmute */, track)
2073 2086
             .then(() => {
2074
-                // Apply the video constraints and degradation preference on
2087
+                // Apply the video constraints, max bitrates and degradation preference on
2075 2088
                 // the video sender if needed.
2076 2089
                 if (track.isVideoTrack() && browser.doesVideoMuteByStreamRemove()) {
2077
-                    this.peerconnection.setSenderVideoDegradationPreference();
2078
-                    this.peerconnection.setSenderVideoConstraint();
2090
+                    this.setSenderMaxBitrates();
2091
+                    this.setSenderVideoDegradationPreference();
2092
+                    this.setSenderVideoConstraint();
2079 2093
                 }
2080 2094
             });
2081 2095
     }

Loading…
Cancel
Save