Procházet zdrojové kódy

ref: Calculate the local video stream height constraints based on the current state

dev1
Jaya Allamsetty před 5 roky
rodič
revize
10c6136bd5
2 změnil soubory, kde provedl 58 přidání a 61 odebrání
  1. 47
    48
      modules/RTC/TPCUtils.js
  2. 11
    13
      modules/RTC/TraceablePeerConnection.js

+ 47
- 48
modules/RTC/TPCUtils.js Zobrazit soubor

@@ -4,7 +4,6 @@ import transform from 'sdp-transform';
4 4
 import * as JitsiTrackEvents from '../../JitsiTrackEvents';
5 5
 import * as MediaType from '../../service/RTC/MediaType';
6 6
 import RTCEvents from '../../service/RTC/RTCEvents';
7
-import * as VideoType from '../../service/RTC/VideoType';
8 7
 import browser from '../browser';
9 8
 
10 9
 const logger = getLogger(__filename);
@@ -23,18 +22,27 @@ export class TPCUtils {
23 22
      * Creates a new instance for a given TraceablePeerConnection
24 23
      *
25 24
      * @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.
25
+     * @param videoBitrates - the bitrates to be configured on the video senders for
26
+     * different resolutions both in unicast and simulcast mode.
28 27
      */
29 28
     constructor(peerconnection, videoBitrates) {
30 29
         this.pc = peerconnection;
31 30
         this.videoBitrates = videoBitrates;
32 31
 
33 32
         /**
34
-         * The simulcast encodings that will be configured on the RTCRtpSender
35
-         * for the video tracks in the unified plan mode.
33
+         * The startup configuration for the stream encodings that are applicable to
34
+         * the video stream when a new sender is created on the peerconnection. The initial
35
+         * config takes into account the differences in browser's simulcast implementation.
36
+         *
37
+         * Encoding parameters:
38
+         * active - determine the on/off state of a particular encoding.
39
+         * maxBitrate - max. bitrate value to be applied to that particular encoding
40
+         *  based on the encoding's resolution and config.js videoQuality settings if applicable.
41
+         * rid - Rtp Stream ID that is configured for a particular simulcast stream.
42
+         * scaleResolutionDownBy - the factor by which the encoding is scaled down from the
43
+         *  original resolution of the captured video.
36 44
          */
37
-        this.simulcastEncodings = [
45
+        this.localStreamEncodingsConfig = [
38 46
             {
39 47
                 active: true,
40 48
                 maxBitrate: browser.isFirefox() ? this.videoBitrates.high : this.videoBitrates.low,
@@ -54,12 +62,6 @@ export class TPCUtils {
54 62
                 scaleResolutionDownBy: browser.isFirefox() ? 4.0 : 1.0
55 63
             }
56 64
         ];
57
-
58
-        /**
59
-         * Resolution height constraints for the simulcast encodings that
60
-         * are configured for the video tracks.
61
-         */
62
-        this.simulcastStreamConstraints = [];
63 65
     }
64 66
 
65 67
     /**
@@ -97,15 +99,21 @@ export class TPCUtils {
97 99
     }
98 100
 
99 101
     /**
100
-     * Obtains stream encodings that need to be configured on the given track.
102
+     * Obtains stream encodings that need to be configured on the given track based
103
+     * on the track media type and the simulcast setting.
101 104
      * @param {JitsiLocalTrack} localTrack
102 105
      */
103 106
     _getStreamEncodings(localTrack) {
104 107
         if (this.pc.isSimulcastOn() && localTrack.isVideoTrack()) {
105
-            return this.simulcastEncodings;
108
+            return this.localStreamEncodingsConfig;
106 109
         }
107 110
 
108
-        return [ { active: true } ];
111
+        return localTrack.isVideoTrack()
112
+            ? [ {
113
+                active: true,
114
+                maxBitrate: this.videoBitrates.high
115
+            } ]
116
+            : [ { active: true } ];
109 117
     }
110 118
 
111 119
     /**
@@ -181,29 +189,6 @@ export class TPCUtils {
181 189
         });
182 190
     }
183 191
 
184
-    /**
185
-     * Constructs resolution height constraints for the simulcast encodings that are
186
-     * created for a given local video track.
187
-     * @param {MediaStreamTrack} track - the local video track.
188
-     * @returns {void}
189
-     */
190
-    setSimulcastStreamConstraints(track) {
191
-        if (browser.isReactNative()) {
192
-            return;
193
-        }
194
-
195
-        const height = track.getSettings().height;
196
-
197
-        for (const encoding in this.simulcastEncodings) {
198
-            if (this.simulcastEncodings.hasOwnProperty(encoding)) {
199
-                this.simulcastStreamConstraints.push({
200
-                    height: height / this.simulcastEncodings[encoding].scaleResolutionDownBy,
201
-                    rid: this.simulcastEncodings[encoding].rid
202
-                });
203
-            }
204
-        }
205
-    }
206
-
207 192
     /**
208 193
     * Adds {@link JitsiLocalTrack} to the WebRTC peerconnection for the first time.
209 194
     * @param {JitsiLocalTrack} track - track to be added to the peerconnection.
@@ -231,11 +216,6 @@ export class TPCUtils {
231 216
             // unused "recv-only" transceiver.
232 217
             this.pc.peerconnection.addTrack(track);
233 218
         }
234
-
235
-        // Construct the stream constraints for the newly added track.
236
-        if (localTrack.isVideoTrack() && localTrack.videoType === VideoType.CAMERA) {
237
-            this.setSimulcastStreamConstraints(localTrack.getTrack());
238
-        }
239 219
     }
240 220
 
241 221
     /**
@@ -268,11 +248,6 @@ export class TPCUtils {
268 248
                 return this.setEncodings(localTrack).then(() => {
269 249
                     this.pc.localTracks.set(localTrack.rtcId, localTrack);
270 250
                     transceiver.direction = 'sendrecv';
271
-
272
-                    // Construct the stream constraints for the newly added track.
273
-                    if (localTrack.isVideoTrack() && localTrack.videoType === VideoType.CAMERA) {
274
-                        this.setSimulcastStreamConstraints(localTrack.getTrack());
275
-                    }
276 251
                 });
277 252
             }
278 253
 
@@ -285,6 +260,30 @@ export class TPCUtils {
285 260
             });
286 261
     }
287 262
 
263
+    /**
264
+     * Obtains the current local video track's height constraints based on the
265
+     * initial stream encodings configuration on the sender and the resolution
266
+     * of the current local track added to the peerconnection.
267
+     * @param {MediaStreamTrack} localTrack local video track
268
+     * @returns {Array[number]} an array containing the resolution heights of
269
+     * simulcast streams configured on the video sender.
270
+     */
271
+    getLocalStreamHeightConstraints(localTrack) {
272
+        // React-native hasn't implemented MediaStreamTrack getSettings yet.
273
+        if (browser.isReactNative()) {
274
+            return null;
275
+        }
276
+
277
+        const localVideoHeightConstraints = [];
278
+        const height = localTrack.getSettings().height;
279
+
280
+        for (const encoding of this.localStreamEncodingsConfig) {
281
+            localVideoHeightConstraints.push(height / encoding.scaleResolutionDownBy);
282
+        }
283
+
284
+        return localVideoHeightConstraints;
285
+    }
286
+
288 287
     /**
289 288
      * Removes the track from the RTCRtpSender as part of the mute operation.
290 289
      * @param {JitsiLocalTrack} localTrack - track to be removed.

+ 11
- 13
modules/RTC/TraceablePeerConnection.js Zobrazit soubor

@@ -1634,15 +1634,11 @@ TraceablePeerConnection.prototype.addTrack = function(track, isInitiator = false
1634 1634
 
1635 1635
     let promiseChain = Promise.resolve();
1636 1636
 
1637
-    if (browser.usesUnifiedPlan() && !browser.usesSdpMungingForSimulcast()) {
1637
+    // On Firefox, the encodings have to be configured on the sender only after the transceiver is created.
1638
+    if (browser.isFirefox()) {
1638 1639
         promiseChain = this.tpcUtils.setEncodings(track);
1639 1640
     }
1640 1641
 
1641
-    // Construct the stream constraints for the newly added track.
1642
-    if (track.isVideoTrack() && track.videoType === VideoType.CAMERA) {
1643
-        this.tpcUtils.setSimulcastStreamConstraints(track.getTrack());
1644
-    }
1645
-
1646 1642
     return promiseChain;
1647 1643
 };
1648 1644
 
@@ -2121,10 +2117,10 @@ TraceablePeerConnection.prototype.setMaxBitRate = function() {
2121 2117
                     && videoType === VideoType.DESKTOP
2122 2118
                     && this.options.capScreenshareBitrate
2123 2119
                     ? presenterEnabled ? this.videoBitrates.high : DESKSTOP_SHARE_RATE
2124
-                    : this.tpcUtils.simulcastEncodings[encoding].maxBitrate;
2120
+                    : this.tpcUtils.localStreamEncodingsConfig[encoding].maxBitrate;
2125 2121
 
2126 2122
                 logger.info(`${this} Setting a max bitrate of ${bitrate} bps on layer `
2127
-                    + `${this.tpcUtils.simulcastEncodings[encoding].rid}`);
2123
+                    + `${this.tpcUtils.localStreamEncodingsConfig[encoding].rid}`);
2128 2124
                 parameters.encodings[encoding].maxBitrate = bitrate;
2129 2125
             }
2130 2126
         }
@@ -2137,7 +2133,7 @@ TraceablePeerConnection.prototype.setMaxBitRate = function() {
2137 2133
             const scaleFactor = this.senderVideoMaxHeight
2138 2134
                 ? Math.floor(localVideoTrack.resolution / this.senderVideoMaxHeight)
2139 2135
                 : 1;
2140
-            const encoding = this.tpcUtils.simulcastEncodings
2136
+            const encoding = this.tpcUtils.localStreamEncodingsConfig
2141 2137
                 .find(layer => layer.scaleResolutionDownBy === scaleFactor);
2142 2138
 
2143 2139
             if (encoding) {
@@ -2263,8 +2259,9 @@ TraceablePeerConnection.prototype.setSenderVideoConstraint = function(frameHeigh
2263 2259
     if (this.isSimulcastOn()) {
2264 2260
         // Determine the encodings that need to stay enabled based on the
2265 2261
         // new frameHeight provided.
2266
-        const encodingsEnabledState = this.tpcUtils.simulcastStreamConstraints
2267
-            .map(constraint => constraint.height <= newHeight);
2262
+        const encodingsEnabledState
2263
+            = this.tpcUtils.getLocalStreamHeightConstraints(localVideoTrack.track)
2264
+                .map(height => height <= newHeight);
2268 2265
 
2269 2266
         for (const encoding in parameters.encodings) {
2270 2267
             if (parameters.encodings.hasOwnProperty(encoding)) {
@@ -2287,8 +2284,9 @@ TraceablePeerConnection.prototype.setSenderVideoConstraint = function(frameHeigh
2287 2284
         localVideoTrack.maxEnabledResolution = newHeight;
2288 2285
         this.eventEmitter.emit(RTCEvents.LOCAL_TRACK_MAX_ENABLED_RESOLUTION_CHANGED, localVideoTrack);
2289 2286
 
2290
-        // Max bitrate needs to be reconfigured on the p2p connection if needed when the send resolution changes.
2291
-        if (this.isP2P) {
2287
+        // Max bitrate needs to be reconfigured on the sender in p2p/non-simulcast case if needed when
2288
+        // the send resolution changes.
2289
+        if (this.isP2P || !this.isSimulcastOn()) {
2292 2290
             return this.setMaxBitRate();
2293 2291
         }
2294 2292
     });

Načítá se…
Zrušit
Uložit