Browse Source

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 7 years ago
parent
commit
05b7ae60dc
4 changed files with 71 additions and 75 deletions
  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 View File

198
             Statistics.sendLog(JSON.stringify(logObject));
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
     },
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
     isWebRtcSupported() {
221
     isWebRtcSupported() {
220
         return RTC.isWebRtcSupported();
222
         return RTC.isWebRtcSupported();

+ 25
- 26
doc/example/example.js View File

257
     desktopSharingFirefoxDisabled: true
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
 if (JitsiMeetJS.mediaDevices.isDeviceChangeAvailable('output')) {
286
 if (JitsiMeetJS.mediaDevices.isDeviceChangeAvailable('output')) {
288
     JitsiMeetJS.mediaDevices.enumerateDevices(devices => {
287
     JitsiMeetJS.mediaDevices.enumerateDevices(devices => {

+ 6
- 4
modules/RTC/RTC.js View File

398
     static init(options = {}) {
398
     static init(options = {}) {
399
         this.options = options;
399
         this.options = options;
400
 
400
 
401
-        RTCUtils.init(this.options);
401
+        return RTCUtils.init(this.options);
402
     }
402
     }
403
 
403
 
404
     /**
404
     /**
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
     static isWebRtcSupported() {
683
     static isWebRtcSupported() {
682
         return browser.isSupported();
684
         return browser.isSupported();

+ 34
- 41
modules/RTC/RTCUtils.js View File

8
           webkitRTCPeerConnection,
8
           webkitRTCPeerConnection,
9
           webkitURL
9
           webkitURL
10
 */
10
 */
11
+
11
 import { AVAILABLE_DEVICE } from '../../service/statistics/AnalyticsEvents';
12
 import { AVAILABLE_DEVICE } from '../../service/statistics/AnalyticsEvents';
12
 import CameraFacingMode from '../../service/RTC/CameraFacingMode';
13
 import CameraFacingMode from '../../service/RTC/CameraFacingMode';
13
 import EventEmitter from 'events';
14
 import EventEmitter from 'events';
130
             : function(callback) {
131
             : function(callback) {
131
                 MediaStreamTrack.getSources(
132
                 MediaStreamTrack.getSources(
132
                     sources =>
133
                     sources =>
133
-                        callback(
134
-                            sources.map(convertMediaStreamTrackSource)));
134
+                        callback(sources.map(convertMediaStreamTrackSource)));
135
             };
135
             };
136
 }
136
 }
137
 
137
 
870
 
870
 
871
     /**
871
     /**
872
      * Depending on the browser, sets difference instance methods for
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
      * objects. Also creates an instance variable for peer connection
874
      * objects. Also creates an instance variable for peer connection
875
      * constraints.
875
      * constraints.
876
      *
876
      *
907
 
907
 
908
             this.getUserMedia
908
             this.getUserMedia
909
                 = (constraints, successCallback, errorCallback) =>
909
                 = (constraints, successCallback, errorCallback) =>
910
-                    window.navigator.mediaDevices
911
-                        .getUserMedia(constraints)
910
+                    window.navigator.mediaDevices.getUserMedia(constraints)
912
                         .then(stream => {
911
                         .then(stream => {
913
                             successCallback && successCallback(stream);
912
                             successCallback && successCallback(stream);
914
 
913
 
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
         } else if (browser.isChrome() // this is chrome < 61
946
         } else if (browser.isChrome() // this is chrome < 61
948
                 || browser.isOpera()
947
                 || browser.isOpera()
949
                 || browser.isNWJS()
948
                 || browser.isNWJS()
951
                 || browser.isReactNative()) {
950
                 || browser.isReactNative()) {
952
 
951
 
953
             this.RTCPeerConnectionType = webkitRTCPeerConnection;
952
             this.RTCPeerConnectionType = webkitRTCPeerConnection;
954
-            const getUserMedia
955
-                = navigator.webkitGetUserMedia.bind(navigator);
953
+
954
+            const getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
956
 
955
 
957
             this.getUserMedia = wrapGetUserMedia(getUserMedia);
956
             this.getUserMedia = wrapGetUserMedia(getUserMedia);
957
+
958
             this.enumerateDevices = rawEnumerateDevicesWithCallback;
958
             this.enumerateDevices = rawEnumerateDevicesWithCallback;
959
 
959
 
960
             this.attachMediaStream
960
             this.attachMediaStream
963
 
963
 
964
                     return element;
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
                 return (
974
                 return (
979
                     typeof id === 'number'
975
                     typeof id === 'number'
980
                         ? id
976
                         ? id
981
                         : SDPUtil.filterSpecialChars(id));
977
                         : SDPUtil.filterSpecialChars(id));
982
             };
978
             };
983
-            this.getTrackID = function(track) {
984
-                return track.id;
985
-            };
979
+            this.getTrackID = ({ id }) => id;
986
 
980
 
987
             if (!webkitMediaStream.prototype.getVideoTracks) {
981
             if (!webkitMediaStream.prototype.getVideoTracks) {
988
                 webkitMediaStream.prototype.getVideoTracks = function() {
982
                 webkitMediaStream.prototype.getVideoTracks = function() {
1019
             };
1013
             };
1020
 
1014
 
1021
             // Remote MediaStreamTracks generated by ORTC (within a
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
         } else {
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
         this._initPCConstraints(options);
1027
         this._initPCConstraints(options);
1035
 
1028
 
1036
         screenObtainer.init(
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
             rawEnumerateDevicesWithCallback(ds => {
1034
             rawEnumerateDevicesWithCallback(ds => {
1042
                 availableDevices = ds.splice(0);
1035
                 availableDevices = ds.splice(0);
1043
 
1036
 
1044
                 logger.debug('Available devices: ', availableDevices);
1037
                 logger.debug('Available devices: ', availableDevices);
1045
                 sendDeviceListToAnalytics(availableDevices);
1038
                 sendDeviceListToAnalytics(availableDevices);
1046
 
1039
 
1047
-                eventEmitter.emit(RTCEvents.DEVICE_LIST_AVAILABLE,
1040
+                eventEmitter.emit(
1041
+                    RTCEvents.DEVICE_LIST_AVAILABLE,
1048
                     availableDevices);
1042
                     availableDevices);
1049
 
1043
 
1050
                 if (isDeviceChangeEventSupported) {
1044
                 if (isDeviceChangeEventSupported) {
1051
                     navigator.mediaDevices.addEventListener(
1045
                     navigator.mediaDevices.addEventListener(
1052
                         'devicechange',
1046
                         'devicechange',
1053
-                        () => this.enumerateDevices(
1054
-                                onMediaDevicesListChanged));
1047
+                        () => this.enumerateDevices(onMediaDevicesListChanged));
1055
                 } else {
1048
                 } else {
1056
                     pollForAvailableMediaDevices();
1049
                     pollForAvailableMediaDevices();
1057
                 }
1050
                 }
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
     isDeviceListAvailable() {
1618
     isDeviceListAvailable() {
1626
         return Boolean(
1619
         return Boolean(

Loading…
Cancel
Save