|
@@ -0,0 +1,317 @@
|
|
1
|
+import { Platform } from 'react-native';
|
|
2
|
+import {
|
|
3
|
+ RTCPeerConnection,
|
|
4
|
+ RTCSessionDescription
|
|
5
|
+} from 'react-native-webrtc';
|
|
6
|
+
|
|
7
|
+import { POSIX } from '../../react-native';
|
|
8
|
+
|
|
9
|
+// XXX At the time of this writing extending RTCPeerConnection using ES6 'class'
|
|
10
|
+// and 'extends' causes a runtime error related to the attempt to define the
|
|
11
|
+// onaddstream property setter. The error mentions that babelHelpers.set is
|
|
12
|
+// undefined which appears to be a thing inside React Native's packager. As a
|
|
13
|
+// workaround, extend using the pre-ES6 way.
|
|
14
|
+
|
|
15
|
+/**
|
|
16
|
+ * The RTCPeerConnection provided by react-native-webrtc fires onaddstream
|
|
17
|
+ * before it remembers remotedescription (and thus makes it available to API
|
|
18
|
+ * clients). Because that appears to be a problem for lib-jitsi-meet which has
|
|
19
|
+ * been successfully running on Chrome, Firefox, Temasys, etc. for a very long
|
|
20
|
+ * time, attempt to meets its expectations (by extending RTCPPeerConnection).
|
|
21
|
+ *
|
|
22
|
+ * @class
|
|
23
|
+ */
|
|
24
|
+export default function _RTCPeerConnection(...args) {
|
|
25
|
+
|
|
26
|
+ /* eslint-disable no-invalid-this */
|
|
27
|
+
|
|
28
|
+ RTCPeerConnection.apply(this, args);
|
|
29
|
+
|
|
30
|
+ this.onaddstream = (...args) => // eslint-disable-line no-shadow
|
|
31
|
+ (this._onaddstreamQueue
|
|
32
|
+ ? this._queueOnaddstream
|
|
33
|
+ : this._invokeOnaddstream)
|
|
34
|
+ .apply(this, args);
|
|
35
|
+
|
|
36
|
+ // Shadow RTCPeerConnection's onaddstream but after _RTCPeerConnection has
|
|
37
|
+ // assigned to the property in question. Defining the property on
|
|
38
|
+ // _RTCPeerConnection's prototype may (or may not, I don't know) work but I
|
|
39
|
+ // don't want to try because the following approach appears to work and I
|
|
40
|
+ // understand it.
|
|
41
|
+ Object.defineProperty(this, 'onaddstream', {
|
|
42
|
+ configurable: true,
|
|
43
|
+ enumerable: true,
|
|
44
|
+ get() {
|
|
45
|
+ return this._onaddstream;
|
|
46
|
+ },
|
|
47
|
+ set(value) {
|
|
48
|
+ this._onaddstream = value;
|
|
49
|
+ }
|
|
50
|
+ });
|
|
51
|
+
|
|
52
|
+ /* eslint-enable no-invalid-this */
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+_RTCPeerConnection.prototype = Object.create(RTCPeerConnection.prototype);
|
|
56
|
+_RTCPeerConnection.prototype.constructor = _RTCPeerConnection;
|
|
57
|
+
|
|
58
|
+_RTCPeerConnection.prototype._invokeOnaddstream = function(...args) {
|
|
59
|
+ const onaddstream = this._onaddstream;
|
|
60
|
+
|
|
61
|
+ return onaddstream && onaddstream.apply(this, args);
|
|
62
|
+};
|
|
63
|
+
|
|
64
|
+_RTCPeerConnection.prototype._invokeQueuedOnaddstream = function(q) {
|
|
65
|
+ q && q.forEach(args => {
|
|
66
|
+ try {
|
|
67
|
+ this._invokeOnaddstream(...args);
|
|
68
|
+ } catch (e) {
|
|
69
|
+ // TODO Determine whether the combination of the standard
|
|
70
|
+ // setRemoteDescription and onaddstream results in a similar
|
|
71
|
+ // swallowing of errors.
|
|
72
|
+ _LOGE(e);
|
|
73
|
+ }
|
|
74
|
+ });
|
|
75
|
+};
|
|
76
|
+
|
|
77
|
+_RTCPeerConnection.prototype._queueOnaddstream = function(...args) {
|
|
78
|
+ this._onaddstreamQueue.push(Array.from(args));
|
|
79
|
+};
|
|
80
|
+
|
|
81
|
+_RTCPeerConnection.prototype.setRemoteDescription = function(
|
|
82
|
+ sessionDescription,
|
|
83
|
+ successCallback,
|
|
84
|
+ errorCallback) {
|
|
85
|
+ // If the deprecated callback-based version is used, translate it to the
|
|
86
|
+ // Promise-based version.
|
|
87
|
+ if (typeof successCallback !== 'undefined'
|
|
88
|
+ || typeof errorCallback !== 'undefined') {
|
|
89
|
+ // XXX Returning a Promise is not necessary. But I don't see why it'd
|
|
90
|
+ // hurt (much).
|
|
91
|
+ return (
|
|
92
|
+ _RTCPeerConnection.prototype.setRemoteDescription.call(
|
|
93
|
+ this,
|
|
94
|
+ sessionDescription)
|
|
95
|
+ .then(successCallback, errorCallback));
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ return (
|
|
99
|
+ _synthesizeIPv6Addresses(sessionDescription)
|
|
100
|
+ .catch(reason => {
|
|
101
|
+ reason && _LOGE(reason);
|
|
102
|
+
|
|
103
|
+ return sessionDescription;
|
|
104
|
+ })
|
|
105
|
+ .then(value => _setRemoteDescription.bind(this)(value)));
|
|
106
|
+
|
|
107
|
+};
|
|
108
|
+
|
|
109
|
+/**
|
|
110
|
+ * Logs at error level.
|
|
111
|
+ *
|
|
112
|
+ * @returns {void}
|
|
113
|
+ */
|
|
114
|
+function _LOGE(...args) {
|
|
115
|
+ console && console.error && console.error(...args);
|
|
116
|
+}
|
|
117
|
+
|
|
118
|
+/**
|
|
119
|
+ * Adapts react-native-webrtc's {@link RTCPeerConnection#setRemoteDescription}
|
|
120
|
+ * implementation which uses the deprecated, callback-based version to the
|
|
121
|
+ * <tt>Promise</tt>-based version.
|
|
122
|
+ *
|
|
123
|
+ * @param {RTCSessionDescription} sessionDescription - The RTCSessionDescription
|
|
124
|
+ * which specifies the configuration of the remote end of the connection.
|
|
125
|
+ * @returns {Promise}
|
|
126
|
+ */
|
|
127
|
+function _setRemoteDescription(sessionDescription) {
|
|
128
|
+ return new Promise((resolve, reject) => {
|
|
129
|
+
|
|
130
|
+ /* eslint-disable no-invalid-this */
|
|
131
|
+
|
|
132
|
+ // Ensure I'm not remembering onaddstream invocations from previous
|
|
133
|
+ // setRemoteDescription calls. I shouldn't be but... anyway.
|
|
134
|
+ this._onaddstreamQueue = [];
|
|
135
|
+
|
|
136
|
+ RTCPeerConnection.prototype.setRemoteDescription.call(
|
|
137
|
+ this,
|
|
138
|
+ sessionDescription,
|
|
139
|
+ (...args) => {
|
|
140
|
+ let q;
|
|
141
|
+
|
|
142
|
+ try {
|
|
143
|
+ resolve(...args);
|
|
144
|
+ } finally {
|
|
145
|
+ q = this._onaddstreamQueue;
|
|
146
|
+ this._onaddstreamQueue = undefined;
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ this._invokeQueuedOnaddstream(q);
|
|
150
|
+ },
|
|
151
|
+ (...args) => {
|
|
152
|
+ this._onaddstreamQueue = undefined;
|
|
153
|
+
|
|
154
|
+ reject(...args);
|
|
155
|
+ });
|
|
156
|
+
|
|
157
|
+ /* eslint-enable no-invalid-this */
|
|
158
|
+ });
|
|
159
|
+}
|
|
160
|
+
|
|
161
|
+/**
|
|
162
|
+ * Synthesize IPv6 addresses on iOS in order to support IPv6 NAT64 networks.
|
|
163
|
+ *
|
|
164
|
+ * @param {RTCSessionDescription} sdp - The RTCSessionDescription which
|
|
165
|
+ * specifies the configuration of the remote end of the connection.
|
|
166
|
+ * @returns {Promise}
|
|
167
|
+ */
|
|
168
|
+function _synthesizeIPv6Addresses(sdp) {
|
|
169
|
+ // The synthesis of IPv6 addresses is implemented on iOS only at the time of
|
|
170
|
+ // this writing.
|
|
171
|
+ if (Platform.OS !== 'ios') {
|
|
172
|
+ return Promise.resolve(sdp);
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ return (
|
|
176
|
+ new Promise(resolve => resolve(_synthesizeIPv6Addresses0(sdp)))
|
|
177
|
+ .then(({ ips, lines }) =>
|
|
178
|
+ Promise.all(Array.from(ips.values()))
|
|
179
|
+ .then(() => _synthesizeIPv6Addresses1(sdp, ips, lines))
|
|
180
|
+ ));
|
|
181
|
+}
|
|
182
|
+
|
|
183
|
+/* eslint-disable max-depth */
|
|
184
|
+
|
|
185
|
+/**
|
|
186
|
+ * Implements the initial phase of the synthesis of IPv6 addresses.
|
|
187
|
+ *
|
|
188
|
+ * @param {RTCSessionDescription} sessionDescription - The RTCSessionDescription
|
|
189
|
+ * for which IPv6 addresses will be synthesized.
|
|
190
|
+ * @returns {{
|
|
191
|
+ * ips: Map,
|
|
192
|
+ * lines: Array
|
|
193
|
+ * }}
|
|
194
|
+ */
|
|
195
|
+function _synthesizeIPv6Addresses0(sessionDescription) {
|
|
196
|
+ const sdp = sessionDescription.sdp;
|
|
197
|
+ let start = 0;
|
|
198
|
+ const lines = [];
|
|
199
|
+ const ips = new Map();
|
|
200
|
+
|
|
201
|
+ do {
|
|
202
|
+ const end = sdp.indexOf('\r\n', start);
|
|
203
|
+ let line;
|
|
204
|
+
|
|
205
|
+ if (end === -1) {
|
|
206
|
+ line = sdp.substring(start);
|
|
207
|
+
|
|
208
|
+ // Break out of the loop at the end of the iteration.
|
|
209
|
+ start = undefined;
|
|
210
|
+ } else {
|
|
211
|
+ line = sdp.substring(start, end);
|
|
212
|
+ start = end + 2;
|
|
213
|
+ }
|
|
214
|
+
|
|
215
|
+ if (line.startsWith('a=candidate:')) {
|
|
216
|
+ const candidate = line.split(' ');
|
|
217
|
+
|
|
218
|
+ if (candidate.length >= 10 && candidate[6] === 'typ') {
|
|
219
|
+ const ip4s = [ candidate[4] ];
|
|
220
|
+ let abort = false;
|
|
221
|
+
|
|
222
|
+ for (let i = 8; i < candidate.length; ++i) {
|
|
223
|
+ if (candidate[i] === 'raddr') {
|
|
224
|
+ ip4s.push(candidate[++i]);
|
|
225
|
+ break;
|
|
226
|
+ }
|
|
227
|
+ }
|
|
228
|
+
|
|
229
|
+ for (const ip of ip4s) {
|
|
230
|
+ if (ip.indexOf(':') === -1) {
|
|
231
|
+ ips.has(ip)
|
|
232
|
+ || ips.set(ip, new Promise((resolve, reject) => {
|
|
233
|
+ const v = ips.get(ip);
|
|
234
|
+
|
|
235
|
+ if (v && typeof v === 'string') {
|
|
236
|
+ resolve(v);
|
|
237
|
+ } else {
|
|
238
|
+ POSIX.getaddrinfo(ip).then(
|
|
239
|
+ value => {
|
|
240
|
+ if (value.indexOf(':') === -1
|
|
241
|
+ || value === ips.get(ip)) {
|
|
242
|
+ ips.delete(ip);
|
|
243
|
+ } else {
|
|
244
|
+ ips.set(ip, value);
|
|
245
|
+ }
|
|
246
|
+ resolve(value);
|
|
247
|
+ },
|
|
248
|
+ reject);
|
|
249
|
+ }
|
|
250
|
+ }));
|
|
251
|
+ } else {
|
|
252
|
+ abort = true;
|
|
253
|
+ break;
|
|
254
|
+ }
|
|
255
|
+ }
|
|
256
|
+ if (abort) {
|
|
257
|
+ ips.clear();
|
|
258
|
+ break;
|
|
259
|
+ }
|
|
260
|
+
|
|
261
|
+ line = candidate;
|
|
262
|
+ }
|
|
263
|
+ }
|
|
264
|
+
|
|
265
|
+ lines.push(line);
|
|
266
|
+ } while (start);
|
|
267
|
+
|
|
268
|
+ return {
|
|
269
|
+ ips,
|
|
270
|
+ lines
|
|
271
|
+ };
|
|
272
|
+}
|
|
273
|
+
|
|
274
|
+/* eslint-enable max-depth */
|
|
275
|
+
|
|
276
|
+/**
|
|
277
|
+ * Implements the initial phase of the synthesis of IPv6 addresses.
|
|
278
|
+ *
|
|
279
|
+ * @param {RTCSessionDescription} sessionDescription - The RTCSessionDescription
|
|
280
|
+ * for which IPv6 addresses are being synthesized.
|
|
281
|
+ * @param {Map} ips - A Map of IPv4 addresses found in the specified
|
|
282
|
+ * sessionDescription to synthesized IPv6 addresses.
|
|
283
|
+ * @param {Array} lines - The lines of the specified sessionDescription.
|
|
284
|
+ * @returns {RTCSessionDescription} A RTCSessionDescription that represents the
|
|
285
|
+ * result of the synthesis of IPv6 addresses.
|
|
286
|
+ */
|
|
287
|
+function _synthesizeIPv6Addresses1(sessionDescription, ips, lines) {
|
|
288
|
+ if (ips.size === 0) {
|
|
289
|
+ return sessionDescription;
|
|
290
|
+ }
|
|
291
|
+
|
|
292
|
+ for (let l = 0; l < lines.length; ++l) {
|
|
293
|
+ const candidate = lines[l];
|
|
294
|
+
|
|
295
|
+ if (typeof candidate !== 'string') {
|
|
296
|
+ let ip4 = candidate[4];
|
|
297
|
+ let ip6 = ips.get(ip4);
|
|
298
|
+
|
|
299
|
+ ip6 && (candidate[4] = ip6);
|
|
300
|
+
|
|
301
|
+ for (let i = 8; i < candidate.length; ++i) {
|
|
302
|
+ if (candidate[i] === 'raddr') {
|
|
303
|
+ ip4 = candidate[++i];
|
|
304
|
+ (ip6 = ips.get(ip4)) && (candidate[i] = ip6);
|
|
305
|
+ break;
|
|
306
|
+ }
|
|
307
|
+ }
|
|
308
|
+
|
|
309
|
+ lines[l] = candidate.join(' ');
|
|
310
|
+ }
|
|
311
|
+ }
|
|
312
|
+
|
|
313
|
+ return new RTCSessionDescription({
|
|
314
|
+ sdp: lines.join('\r\n'),
|
|
315
|
+ type: sessionDescription.type
|
|
316
|
+ });
|
|
317
|
+}
|