Explorar el Código

core: refactor initialization not to return a Promise (continued)

1. The example was using the Promise return value of JitsiMeetJS.init
which is no longer possible/correct after commit "core: refactor
initialization not to return a Promise".

2. We went back and forth with the value returned by JitsiMeetJS.init:
we initially didn't return a value, then we started returning a Promise,
and now we're not returning a value. Whether we'll go back to returning
a value is up in the air. Anyway, the return value is practically
determined by the last in a chain of function calls: JitsiMeetJS, RTC,
RTCUtil. Since the chain is not really documented, it will not hurt much
to make it easier to refactor the chain by "composing" the functions.
dev1
Lyubo Marinov hace 7 años
padre
commit
05b7ae60dc
Se han modificado 4 ficheros con 71 adiciones y 75 borrados
  1. 6
    4
      JitsiMeetJS.js
  2. 25
    26
      doc/example/example.js
  3. 6
    4
      modules/RTC/RTC.js
  4. 34
    41
      modules/RTC/RTCUtils.js

+ 6
- 4
JitsiMeetJS.js Ver fichero

@@ -198,7 +198,7 @@ export default _mergeNamespaceAndModule({
198 198
             Statistics.sendLog(JSON.stringify(logObject));
199 199
         }
200 200
 
201
-        RTC.init(options);
201
+        return RTC.init(options);
202 202
     },
203 203
 
204 204
     /**
@@ -211,10 +211,12 @@ export default _mergeNamespaceAndModule({
211 211
     },
212 212
 
213 213
     /**
214
-     * Returns wether the current environment supported WebRTC (for use with
215
-     * this library) or not.
214
+     * Returns whether the current execution environment supports WebRTC (for
215
+     * use within this library).
216 216
      *
217
-     * @returns {boolean} true if supported, false otherwise.
217
+     * @returns {boolean} {@code true} if WebRTC is supported in the current
218
+     * execution environment (for use within this library); {@code false},
219
+     * otherwise.
218 220
      */
219 221
     isWebRtcSupported() {
220 222
         return RTC.isWebRtcSupported();

+ 25
- 26
doc/example/example.js Ver fichero

@@ -257,32 +257,31 @@ const initOptions = {
257 257
     desktopSharingFirefoxDisabled: true
258 258
 };
259 259
 
260
-JitsiMeetJS.init(initOptions)
261
-    .then(() => {
262
-        connection = new JitsiMeetJS.JitsiConnection(null, null, options);
263
-
264
-        connection.addEventListener(
265
-            JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED,
266
-            onConnectionSuccess);
267
-        connection.addEventListener(
268
-            JitsiMeetJS.events.connection.CONNECTION_FAILED,
269
-            onConnectionFailed);
270
-        connection.addEventListener(
271
-            JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED,
272
-            disconnect);
273
-
274
-        JitsiMeetJS.mediaDevices.addEventListener(
275
-            JitsiMeetJS.events.mediaDevices.DEVICE_LIST_CHANGED,
276
-            onDeviceListChanged);
277
-
278
-        connection.connect();
279
-        JitsiMeetJS.createLocalTracks({ devices: [ 'audio', 'video' ] })
280
-            .then(onLocalTracks)
281
-            .catch(error => {
282
-                throw error;
283
-            });
284
-    })
285
-    .catch(error => console.log(error));
260
+JitsiMeetJS.init(initOptions);
261
+
262
+connection = new JitsiMeetJS.JitsiConnection(null, null, options);
263
+
264
+connection.addEventListener(
265
+    JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED,
266
+    onConnectionSuccess);
267
+connection.addEventListener(
268
+    JitsiMeetJS.events.connection.CONNECTION_FAILED,
269
+    onConnectionFailed);
270
+connection.addEventListener(
271
+    JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED,
272
+    disconnect);
273
+
274
+JitsiMeetJS.mediaDevices.addEventListener(
275
+    JitsiMeetJS.events.mediaDevices.DEVICE_LIST_CHANGED,
276
+    onDeviceListChanged);
277
+
278
+connection.connect();
279
+
280
+JitsiMeetJS.createLocalTracks({ devices: [ 'audio', 'video' ] })
281
+    .then(onLocalTracks)
282
+    .catch(error => {
283
+        throw error;
284
+    });
286 285
 
287 286
 if (JitsiMeetJS.mediaDevices.isDeviceChangeAvailable('output')) {
288 287
     JitsiMeetJS.mediaDevices.enumerateDevices(devices => {

+ 6
- 4
modules/RTC/RTC.js Ver fichero

@@ -398,7 +398,7 @@ export default class RTC extends Listenable {
398 398
     static init(options = {}) {
399 399
         this.options = options;
400 400
 
401
-        RTCUtils.init(this.options);
401
+        return RTCUtils.init(this.options);
402 402
     }
403 403
 
404 404
     /**
@@ -673,10 +673,12 @@ export default class RTC extends Listenable {
673 673
     }
674 674
 
675 675
     /**
676
-     * Returns wether the current environment supported WebRTC (for use with
677
-     * this library) or not.
676
+     * Returns whether the current execution environment supports WebRTC (for
677
+     * use within this library).
678 678
      *
679
-     * @returns {boolean} true if supported, false otherwise.
679
+     * @returns {boolean} {@code true} if WebRTC is supported in the current
680
+     * execution environment (for use within this library); {@code false},
681
+     * otherwise.
680 682
      */
681 683
     static isWebRtcSupported() {
682 684
         return browser.isSupported();

+ 34
- 41
modules/RTC/RTCUtils.js Ver fichero

@@ -8,6 +8,7 @@
8 8
           webkitRTCPeerConnection,
9 9
           webkitURL
10 10
 */
11
+
11 12
 import { AVAILABLE_DEVICE } from '../../service/statistics/AnalyticsEvents';
12 13
 import CameraFacingMode from '../../service/RTC/CameraFacingMode';
13 14
 import EventEmitter from 'events';
@@ -130,8 +131,7 @@ function initRawEnumerateDevicesWithCallback() {
130 131
             : function(callback) {
131 132
                 MediaStreamTrack.getSources(
132 133
                     sources =>
133
-                        callback(
134
-                            sources.map(convertMediaStreamTrackSource)));
134
+                        callback(sources.map(convertMediaStreamTrackSource)));
135 135
             };
136 136
 }
137 137
 
@@ -870,7 +870,7 @@ class RTCUtils extends Listenable {
870 870
 
871 871
     /**
872 872
      * Depending on the browser, sets difference instance methods for
873
-     * interacting with user media and adds methods to native webrtc related
873
+     * interacting with user media and adds methods to native WebRTC-related
874 874
      * objects. Also creates an instance variable for peer connection
875 875
      * constraints.
876 876
      *
@@ -907,8 +907,7 @@ class RTCUtils extends Listenable {
907 907
 
908 908
             this.getUserMedia
909 909
                 = (constraints, successCallback, errorCallback) =>
910
-                    window.navigator.mediaDevices
911
-                        .getUserMedia(constraints)
910
+                    window.navigator.mediaDevices.getUserMedia(constraints)
912 911
                         .then(stream => {
913 912
                             successCallback && successCallback(stream);
914 913
 
@@ -942,8 +941,8 @@ class RTCUtils extends Listenable {
942 941
                     }
943 942
                 });
944 943
 
945
-            this.getStreamID = stream => stream.id;
946
-            this.getTrackID = track => track.id;
944
+            this.getStreamID = ({ id }) => id;
945
+            this.getTrackID = ({ id }) => id;
947 946
         } else if (browser.isChrome() // this is chrome < 61
948 947
                 || browser.isOpera()
949 948
                 || browser.isNWJS()
@@ -951,10 +950,11 @@ class RTCUtils extends Listenable {
951 950
                 || browser.isReactNative()) {
952 951
 
953 952
             this.RTCPeerConnectionType = webkitRTCPeerConnection;
954
-            const getUserMedia
955
-                = navigator.webkitGetUserMedia.bind(navigator);
953
+
954
+            const getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
956 955
 
957 956
             this.getUserMedia = wrapGetUserMedia(getUserMedia);
957
+
958 958
             this.enumerateDevices = rawEnumerateDevicesWithCallback;
959 959
 
960 960
             this.attachMediaStream
@@ -963,26 +963,20 @@ class RTCUtils extends Listenable {
963 963
 
964 964
                     return element;
965 965
                 });
966
-            this.getStreamID = function(stream) {
967
-                // A. MediaStreams from FF endpoints have the characters '{'
968
-                // and '}' that make jQuery choke.
969
-                // B. The react-native-webrtc implementation that we use on
970
-                // React Native at the time of this writing returns a number
971
-                // for the id of MediaStream. Let's just say that a number
972
-                // contains no special characters.
973
-                const id = stream.id;
974
-
975
-                // XXX The return statement is affected by automatic
976
-                // semicolon insertion (ASI). No line terminator is allowed
977
-                // between the return keyword and the expression.
966
+
967
+            this.getStreamID = function({ id }) {
968
+                // A. MediaStreams from FF endpoints have the characters '{' and
969
+                // '}' that make jQuery choke.
970
+                // B. The react-native-webrtc implementation that we use at the
971
+                // time of this writing returns a number for the id of
972
+                // MediaStream. Let's just say that a number contains no special
973
+                // characters.
978 974
                 return (
979 975
                     typeof id === 'number'
980 976
                         ? id
981 977
                         : SDPUtil.filterSpecialChars(id));
982 978
             };
983
-            this.getTrackID = function(track) {
984
-                return track.id;
985
-            };
979
+            this.getTrackID = ({ id }) => id;
986 980
 
987 981
             if (!webkitMediaStream.prototype.getVideoTracks) {
988 982
                 webkitMediaStream.prototype.getVideoTracks = function() {
@@ -1019,39 +1013,38 @@ class RTCUtils extends Listenable {
1019 1013
             };
1020 1014
 
1021 1015
             // Remote MediaStreamTracks generated by ORTC (within a
1022
-            // RTCRtpReceiver) have an internally/random id which does not
1023
-            // match the track id signaled in the remote SDP. The shim adds
1024
-            // a custom jitsi-id property with the original track id.
1025
-            this.getTrackID = function(track) {
1026
-                return track.jitsiRemoteId || track.id;
1027
-            };
1016
+            // RTCRtpReceiver) have an internally/random id which does not match
1017
+            // the track id signaled in the remote SDP. The shim adds a custom
1018
+            // jitsi-id property with the original track id.
1019
+            this.getTrackID = track => track.jitsiRemoteId || track.id;
1028 1020
         } else {
1029
-            logger.error('Endpoint does not appear to be WebRTC-capable');
1021
+            const message = 'Endpoint does not appear to be WebRTC-capable';
1030 1022
 
1031
-            throw new Error('Unsupporded endpoint');
1023
+            logger.error(message);
1024
+            throw new Error(message);
1032 1025
         }
1033 1026
 
1034 1027
         this._initPCConstraints(options);
1035 1028
 
1036 1029
         screenObtainer.init(
1037
-            options, this.getUserMediaWithConstraints.bind(this));
1030
+            options,
1031
+            this.getUserMediaWithConstraints.bind(this));
1038 1032
 
1039
-        if (this.isDeviceListAvailable()
1040
-                && rawEnumerateDevicesWithCallback) {
1033
+        if (this.isDeviceListAvailable() && rawEnumerateDevicesWithCallback) {
1041 1034
             rawEnumerateDevicesWithCallback(ds => {
1042 1035
                 availableDevices = ds.splice(0);
1043 1036
 
1044 1037
                 logger.debug('Available devices: ', availableDevices);
1045 1038
                 sendDeviceListToAnalytics(availableDevices);
1046 1039
 
1047
-                eventEmitter.emit(RTCEvents.DEVICE_LIST_AVAILABLE,
1040
+                eventEmitter.emit(
1041
+                    RTCEvents.DEVICE_LIST_AVAILABLE,
1048 1042
                     availableDevices);
1049 1043
 
1050 1044
                 if (isDeviceChangeEventSupported) {
1051 1045
                     navigator.mediaDevices.addEventListener(
1052 1046
                         'devicechange',
1053
-                        () => this.enumerateDevices(
1054
-                                onMediaDevicesListChanged));
1047
+                        () => this.enumerateDevices(onMediaDevicesListChanged));
1055 1048
                 } else {
1056 1049
                     pollForAvailableMediaDevices();
1057 1050
                 }
@@ -1617,10 +1610,10 @@ class RTCUtils extends Listenable {
1617 1610
     }
1618 1611
 
1619 1612
     /**
1620
-     * Checks if its possible to enumerate available cameras/microphones.
1613
+     * Checks whether it is possible to enumerate available cameras/microphones.
1621 1614
      *
1622
-     * @returns {boolean} true if the device listing is available or false
1623
-     * otherwise.
1615
+     * @returns {boolean} {@code true} if the device listing is available;
1616
+     * {@code false}, otherwise.
1624 1617
      */
1625 1618
     isDeviceListAvailable() {
1626 1619
         return Boolean(

Loading…
Cancelar
Guardar