Bläddra i källkod

[RN] Promised-base RTCPeerConnection API

Recent changes in lib-jitsi-meet probably led to (1) our
RTCPeerConnection customizations on react-native not being used which is
a problem because we need them for at least NAT64 on iOS in order to
pass the review in Apple's App Store and (2) unexpected exceptions
inside react-native-webrtc.

The Promise-based WebRTC API should be merged from react-native-webrtc's
upstream but I don't want to do it right now because last time we got
multiple bugs in addition.
master
Lyubo Marinov 7 år sedan
förälder
incheckning
e622829c1c

+ 60
- 1
react/features/base/lib-jitsi-meet/native/RTCPeerConnection.js Visa fil

1
+// @flow
2
+
1
 import { NativeModules } from 'react-native';
3
 import { NativeModules } from 'react-native';
2
 import { RTCPeerConnection, RTCSessionDescription } from 'react-native-webrtc';
4
 import { RTCPeerConnection, RTCSessionDescription } from 'react-native-webrtc';
3
 
5
 
33
  *
35
  *
34
  * @class
36
  * @class
35
  */
37
  */
36
-export default function _RTCPeerConnection(...args) {
38
+export default function _RTCPeerConnection(...args: any[]) {
37
 
39
 
38
     /* eslint-disable indent, no-invalid-this */
40
     /* eslint-disable indent, no-invalid-this */
39
 
41
 
50
     // _RTCPeerConnection's prototype may (or may not, I don't know) work but I
52
     // _RTCPeerConnection's prototype may (or may not, I don't know) work but I
51
     // don't want to try because the following approach appears to work and I
53
     // don't want to try because the following approach appears to work and I
52
     // understand it.
54
     // understand it.
55
+
56
+    // $FlowFixMe
53
     Object.defineProperty(this, 'onaddstream', {
57
     Object.defineProperty(this, 'onaddstream', {
54
         configurable: true,
58
         configurable: true,
55
         enumerable: true,
59
         enumerable: true,
67
 _RTCPeerConnection.prototype = Object.create(RTCPeerConnection.prototype);
71
 _RTCPeerConnection.prototype = Object.create(RTCPeerConnection.prototype);
68
 _RTCPeerConnection.prototype.constructor = _RTCPeerConnection;
72
 _RTCPeerConnection.prototype.constructor = _RTCPeerConnection;
69
 
73
 
74
+_RTCPeerConnection.prototype.addIceCandidate
75
+    = _makePromiseAware(RTCPeerConnection.prototype.addIceCandidate, 1, 0);
76
+
77
+_RTCPeerConnection.prototype.createAnswer
78
+    = _makePromiseAware(RTCPeerConnection.prototype.createAnswer, 0, 1);
79
+_RTCPeerConnection.prototype.createOffer
80
+    = _makePromiseAware(RTCPeerConnection.prototype.createOffer, 0, 1);
81
+
70
 _RTCPeerConnection.prototype._invokeOnaddstream = function(...args) {
82
 _RTCPeerConnection.prototype._invokeOnaddstream = function(...args) {
71
     const onaddstream = this._onaddstream;
83
     const onaddstream = this._onaddstream;
72
 
84
 
90
     this._onaddstreamQueue.push(Array.from(args));
102
     this._onaddstreamQueue.push(Array.from(args));
91
 };
103
 };
92
 
104
 
105
+_RTCPeerConnection.prototype.setLocalDescription
106
+  = _makePromiseAware(RTCPeerConnection.prototype.setLocalDescription, 1, 0);
107
+
93
 _RTCPeerConnection.prototype.setRemoteDescription = function(
108
 _RTCPeerConnection.prototype.setRemoteDescription = function(
94
         sessionDescription,
109
         sessionDescription,
95
         successCallback,
110
         successCallback,
128
     console && console.error && console.error(...args);
143
     console && console.error && console.error(...args);
129
 }
144
 }
130
 
145
 
146
+/**
147
+ * Makes a {@code Promise}-returning function out of a specific void function
148
+ * with {@code successCallback} and {@code failureCallback}.
149
+ *
150
+ * @param {Function} f - The (void) function with {@code successCallback} and
151
+ * {@code failureCallback}.
152
+ * @param {number} beforeCallbacks - The number of arguments before
153
+ * {@code successCallback} and {@code failureCallback}.
154
+ * @param {number} afterCallbacks - The number of arguments after
155
+ * {@code successCallback} and {@code failureCallback}.
156
+ * @returns {Promise}
157
+ */
158
+function _makePromiseAware(
159
+        f: Function,
160
+        beforeCallbacks: number,
161
+        afterCallbacks: number) {
162
+    return function(...args) {
163
+        return new Promise((resolve, reject) => {
164
+
165
+            if (args.length <= beforeCallbacks + afterCallbacks) {
166
+                args.splice(beforeCallbacks, 0, resolve, reject);
167
+            }
168
+
169
+            let fPromise;
170
+
171
+            try {
172
+                // eslint-disable-next-line no-invalid-this
173
+                fPromise = f.apply(this, args);
174
+            } catch (e) {
175
+                reject(e);
176
+            }
177
+
178
+            // If the super implementation returns a Promise from the deprecated
179
+            // invocation by any chance, try to make sense of it.
180
+            if (fPromise) {
181
+                const { then } = fPromise;
182
+
183
+                typeof then === 'function'
184
+                    && then.call(fPromise, resolve, reject);
185
+            }
186
+        });
187
+    };
188
+}
189
+
131
 /**
190
 /**
132
  * Adapts react-native-webrtc's {@link RTCPeerConnection#setRemoteDescription}
191
  * Adapts react-native-webrtc's {@link RTCPeerConnection#setRemoteDescription}
133
  * implementation which uses the deprecated, callback-based version to the
192
  * implementation which uses the deprecated, callback-based version to the

+ 6
- 3
react/features/base/lib-jitsi-meet/native/polyfills-webrtc.js Visa fil

15
     if (typeof global.MediaStreamTrack === 'undefined') {
15
     if (typeof global.MediaStreamTrack === 'undefined') {
16
         global.MediaStreamTrack = MediaStreamTrack;
16
         global.MediaStreamTrack = MediaStreamTrack;
17
     }
17
     }
18
+    if (typeof global.RTCIceCandidate === 'undefined') {
19
+        global.RTCIceCandidate = RTCIceCandidate;
20
+    }
21
+    if (typeof global.RTCPeerConnection === 'undefined') {
22
+        global.RTCPeerConnection = RTCPeerConnection;
23
+    }
18
     if (typeof global.webkitRTCPeerConnection === 'undefined') {
24
     if (typeof global.webkitRTCPeerConnection === 'undefined') {
19
         global.webkitRTCPeerConnection = RTCPeerConnection;
25
         global.webkitRTCPeerConnection = RTCPeerConnection;
20
     }
26
     }
21
     if (typeof global.RTCSessionDescription === 'undefined') {
27
     if (typeof global.RTCSessionDescription === 'undefined') {
22
         global.RTCSessionDescription = RTCSessionDescription;
28
         global.RTCSessionDescription = RTCSessionDescription;
23
     }
29
     }
24
-    if (typeof global.RTCIceCandidate === 'undefined') {
25
-        global.RTCIceCandidate = RTCIceCandidate;
26
-    }
27
 
30
 
28
     const navigator = global.navigator;
31
     const navigator = global.navigator;
29
 
32
 

Laddar…
Avbryt
Spara