react-native-webrtc 1.92.2 → 1.94.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/.prettierignore +9 -0
  2. package/.prettierrc.json +10 -0
  3. package/android/libs/.placeholder +0 -0
  4. package/android/src/main/java/com/oney/WebRTCModule/GetUserMediaImpl.java +0 -5
  5. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +199 -255
  6. package/apple/.placeholder +0 -0
  7. package/ios/RCTWebRTC/VideoCaptureController.m +5 -3
  8. package/metro.config.js +3 -4
  9. package/metro.config.macos.js +7 -8
  10. package/package.json +20 -12
  11. package/react-native.config.js +5 -6
  12. package/src/EventEmitter.js +1 -1
  13. package/src/MediaDevices.js +6 -12
  14. package/src/MediaStream.js +120 -131
  15. package/src/MediaStreamError.js +8 -10
  16. package/src/MediaStreamErrorEvent.js +6 -7
  17. package/src/MediaStreamEvent.js +6 -7
  18. package/src/MediaStreamTrack.js +89 -103
  19. package/src/MediaStreamTrackEvent.js +6 -7
  20. package/src/MessageEvent.js +7 -8
  21. package/src/Permissions.js +26 -23
  22. package/src/RTCDataChannel.js +127 -145
  23. package/src/RTCDataChannelEvent.js +6 -7
  24. package/src/RTCEvent.js +5 -6
  25. package/src/RTCIceCandidate.js +15 -16
  26. package/src/RTCIceCandidateEvent.js +8 -9
  27. package/src/RTCPeerConnection.js +295 -325
  28. package/src/RTCSessionDescription.js +15 -16
  29. package/src/RTCUtil.js +56 -57
  30. package/src/RTCView.js +59 -69
  31. package/src/ScreenCapturePickerView.js +2 -5
  32. package/src/getDisplayMedia.js +7 -6
  33. package/src/getUserMedia.js +85 -85
  34. package/src/index.js +28 -34
  35. package/tools/build-webrtc.py +61 -49
  36. package/tools/downloadBitcode.sh +4 -16
  37. package/tools/downloadWebRTC.js +100 -0
  38. package/android/libs/libjingle_peerconnection.so.jar +0 -0
  39. package/android/libs/libwebrtc.jar +0 -0
  40. package/apple/WebRTC.xcframework.tgz +0 -0
  41. package/ios/RCTWebRTC.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  42. package/ios/RCTWebRTC.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  43. package/ios/RCTWebRTC.xcodeproj/project.xcworkspace/xcuserdata/saghul.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  44. package/ios/RCTWebRTC.xcodeproj/xcuserdata/saghul.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
@@ -1,6 +1,5 @@
1
- 'use strict';
2
1
 
3
- import EventTarget from 'event-target-shim';
2
+ import { defineCustomEventTarget } from 'event-target-shim';
4
3
  import { NativeModules } from 'react-native';
5
4
 
6
5
  import MediaStream from './MediaStream';
@@ -19,187 +18,163 @@ import EventEmitter from './EventEmitter';
19
18
  const { WebRTCModule } = NativeModules;
20
19
 
21
20
  type RTCSignalingState =
22
- 'stable' |
23
- 'have-local-offer' |
24
- 'have-remote-offer' |
25
- 'have-local-pranswer' |
26
- 'have-remote-pranswer' |
27
- 'closed';
28
-
29
- type RTCIceGatheringState =
30
- 'new' |
31
- 'gathering' |
32
- 'complete';
33
-
34
- type RTCPeerConnectionState =
35
- 'new' |
36
- 'connecting' |
37
- 'connected' |
38
- 'disconnected' |
39
- 'failed' |
40
- 'closed';
41
-
42
- type RTCIceConnectionState =
43
- 'new' |
44
- 'checking' |
45
- 'connected' |
46
- 'completed' |
47
- 'failed' |
48
- 'disconnected' |
49
- 'closed';
21
+ | 'stable'
22
+ | 'have-local-offer'
23
+ | 'have-remote-offer'
24
+ | 'have-local-pranswer'
25
+ | 'have-remote-pranswer'
26
+ | 'closed';
27
+
28
+ type RTCIceGatheringState = 'new' | 'gathering' | 'complete';
29
+
30
+ type RTCPeerConnectionState = 'new' | 'connecting' | 'connected' | 'disconnected' | 'failed' | 'closed';
31
+
32
+ type RTCIceConnectionState = 'new' | 'checking' | 'connected' | 'completed' | 'failed' | 'disconnected' | 'closed';
50
33
 
51
34
  type RTCDataChannelInit = {
52
- ordered?: boolean;
53
- maxPacketLifeTime?: number;
54
- maxRetransmits?: number;
55
- protocol?: string;
56
- negotiated?: boolean;
57
- id?: number;
35
+ ordered?: boolean,
36
+ maxPacketLifeTime?: number,
37
+ maxRetransmits?: number,
38
+ protocol?: string,
39
+ negotiated?: boolean,
40
+ id?: number
58
41
  };
59
42
 
60
43
  const PEER_CONNECTION_EVENTS = [
61
- 'connectionstatechange',
62
- 'icecandidate',
63
- 'icecandidateerror',
64
- 'iceconnectionstatechange',
65
- 'icegatheringstatechange',
66
- 'negotiationneeded',
67
- 'signalingstatechange',
68
- // Peer-to-peer Data API:
69
- 'datachannel',
70
- // old:
71
- 'addstream',
72
- 'removestream',
44
+ 'connectionstatechange',
45
+ 'icecandidate',
46
+ 'icecandidateerror',
47
+ 'iceconnectionstatechange',
48
+ 'icegatheringstatechange',
49
+ 'negotiationneeded',
50
+ 'signalingstatechange',
51
+ 'datachannel',
52
+ 'addstream',
53
+ 'removestream'
73
54
  ];
74
55
 
75
56
  let nextPeerConnectionId = 0;
76
57
 
77
- export default class RTCPeerConnection extends EventTarget(PEER_CONNECTION_EVENTS) {
78
- localDescription: RTCSessionDescription;
79
- remoteDescription: RTCSessionDescription;
80
-
81
- signalingState: RTCSignalingState = 'stable';
82
- iceGatheringState: RTCIceGatheringState = 'new';
83
- connectionState: RTCPeerConnectionState = 'new';
84
- iceConnectionState: RTCIceConnectionState = 'new';
85
-
86
- onconnectionstatechange: ?Function;
87
- onicecandidate: ?Function;
88
- onicecandidateerror: ?Function;
89
- oniceconnectionstatechange: ?Function;
90
- onicegatheringstatechange: ?Function;
91
- onnegotiationneeded: ?Function;
92
- onsignalingstatechange: ?Function;
93
-
94
- onaddstream: ?Function;
95
- onremovestream: ?Function;
96
-
97
- _peerConnectionId: number;
98
- _localStreams: Array<MediaStream> = [];
99
- _remoteStreams: Array<MediaStream> = [];
100
- _subscriptions: Array<any>;
101
-
102
- constructor(configuration) {
103
- super();
104
- this._peerConnectionId = nextPeerConnectionId++;
105
- WebRTCModule.peerConnectionInit(configuration, this._peerConnectionId);
106
- this._registerEvents();
107
- }
108
-
109
- addStream(stream: MediaStream) {
110
- const index = this._localStreams.indexOf(stream);
111
- if (index !== -1) {
112
- return;
113
- }
114
- WebRTCModule.peerConnectionAddStream(stream._reactTag, this._peerConnectionId);
115
- this._localStreams.push(stream);
116
- }
117
-
118
- removeStream(stream: MediaStream) {
119
- const index = this._localStreams.indexOf(stream);
120
- if (index === -1) {
121
- return;
122
- }
123
- this._localStreams.splice(index, 1);
124
- WebRTCModule.peerConnectionRemoveStream(stream._reactTag, this._peerConnectionId);
125
- }
126
-
127
- createOffer(options) {
128
- return new Promise((resolve, reject) => {
129
- WebRTCModule.peerConnectionCreateOffer(
130
- this._peerConnectionId,
131
- RTCUtil.normalizeOfferAnswerOptions(options),
132
- (successful, data) => {
133
- if (successful) {
134
- resolve(data);
135
- } else {
136
- reject(data); // TODO: convert to NavigatorUserMediaError
137
- }
58
+ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_CONNECTION_EVENTS) {
59
+ localDescription: RTCSessionDescription;
60
+ remoteDescription: RTCSessionDescription;
61
+
62
+ signalingState: RTCSignalingState = 'stable';
63
+ iceGatheringState: RTCIceGatheringState = 'new';
64
+ connectionState: RTCPeerConnectionState = 'new';
65
+ iceConnectionState: RTCIceConnectionState = 'new';
66
+
67
+ _peerConnectionId: number;
68
+ _localStreams: Array<MediaStream> = [];
69
+ _remoteStreams: Array<MediaStream> = [];
70
+ _subscriptions: Array<any>;
71
+
72
+ constructor(configuration) {
73
+ super();
74
+ this._peerConnectionId = nextPeerConnectionId++;
75
+ WebRTCModule.peerConnectionInit(configuration, this._peerConnectionId);
76
+ this._registerEvents();
77
+ }
78
+
79
+ addStream(stream: MediaStream) {
80
+ const index = this._localStreams.indexOf(stream);
81
+ if (index !== -1) {
82
+ return;
83
+ }
84
+ WebRTCModule.peerConnectionAddStream(stream._reactTag, this._peerConnectionId);
85
+ this._localStreams.push(stream);
86
+ }
87
+
88
+ removeStream(stream: MediaStream) {
89
+ const index = this._localStreams.indexOf(stream);
90
+ if (index === -1) {
91
+ return;
92
+ }
93
+ this._localStreams.splice(index, 1);
94
+ WebRTCModule.peerConnectionRemoveStream(stream._reactTag, this._peerConnectionId);
95
+ }
96
+
97
+ createOffer(options) {
98
+ return new Promise((resolve, reject) => {
99
+ WebRTCModule.peerConnectionCreateOffer(
100
+ this._peerConnectionId,
101
+ RTCUtil.normalizeOfferAnswerOptions(options),
102
+ (successful, data) => {
103
+ if (successful) {
104
+ resolve(data);
105
+ } else {
106
+ reject(data); // TODO: convert to NavigatorUserMediaError
107
+ }
108
+ }
109
+ );
110
+ });
111
+ }
112
+
113
+ createAnswer(options = {}) {
114
+ return new Promise((resolve, reject) => {
115
+ WebRTCModule.peerConnectionCreateAnswer(
116
+ this._peerConnectionId,
117
+ RTCUtil.normalizeOfferAnswerOptions(options),
118
+ (successful, data) => {
119
+ if (successful) {
120
+ resolve(data);
121
+ } else {
122
+ reject(data);
123
+ }
124
+ }
125
+ );
138
126
  });
139
- });
140
- }
141
-
142
- createAnswer(options = {}) {
143
- return new Promise((resolve, reject) => {
144
- WebRTCModule.peerConnectionCreateAnswer(
145
- this._peerConnectionId,
146
- RTCUtil.normalizeOfferAnswerOptions(options),
147
- (successful, data) => {
148
- if (successful) {
149
- resolve(data);
150
- } else {
151
- reject(data);
152
- }
127
+ }
128
+
129
+ setConfiguration(configuration) {
130
+ WebRTCModule.peerConnectionSetConfiguration(configuration, this._peerConnectionId);
131
+ }
132
+
133
+ async setLocalDescription(sessionDescription: ?RTCSessionDescription) {
134
+ const desc = sessionDescription
135
+ ? sessionDescription.toJSON
136
+ ? sessionDescription.toJSON()
137
+ : sessionDescription
138
+ : null;
139
+ const newSdp = await WebRTCModule.peerConnectionSetLocalDescription(this._peerConnectionId, desc);
140
+
141
+ this.localDescription = new RTCSessionDescription(newSdp);
142
+ }
143
+
144
+ setRemoteDescription(sessionDescription: RTCSessionDescription) {
145
+ return new Promise((resolve, reject) => {
146
+ WebRTCModule.peerConnectionSetRemoteDescription(
147
+ sessionDescription.toJSON ? sessionDescription.toJSON() : sessionDescription,
148
+ this._peerConnectionId,
149
+ (successful, data) => {
150
+ if (successful) {
151
+ this.remoteDescription = new RTCSessionDescription(data);
152
+ resolve();
153
+ } else {
154
+ reject(data);
155
+ }
156
+ }
157
+ );
153
158
  });
154
- });
155
- }
156
-
157
- setConfiguration(configuration) {
158
- WebRTCModule.peerConnectionSetConfiguration(configuration, this._peerConnectionId);
159
- }
160
-
161
- async setLocalDescription(sessionDescription: ?RTCSessionDescription) {
162
- const desc = sessionDescription ?
163
- (sessionDescription.toJSON ? sessionDescription.toJSON() : sessionDescription) : null;
164
- const newSdp = await WebRTCModule.peerConnectionSetLocalDescription(this._peerConnectionId, desc);
165
-
166
- this.localDescription = new RTCSessionDescription(newSdp);
167
- }
168
-
169
- setRemoteDescription(sessionDescription: RTCSessionDescription) {
170
- return new Promise((resolve, reject) => {
171
- WebRTCModule.peerConnectionSetRemoteDescription(
172
- sessionDescription.toJSON ? sessionDescription.toJSON() : sessionDescription,
173
- this._peerConnectionId,
174
- (successful, data) => {
175
- if (successful) {
176
- this.remoteDescription = new RTCSessionDescription(data);
177
- resolve();
178
- } else {
179
- reject(data);
180
- }
181
- });
182
- });
183
- }
184
-
185
- async addIceCandidate(candidate) {
186
- if (!candidate || !candidate.candidate) {
187
- // XXX end-of cantidates is not implemented: https://bugs.chromium.org/p/webrtc/issues/detail?id=9218
188
- return;
189
159
  }
190
160
 
191
- const newSdp = await WebRTCModule.peerConnectionAddICECandidate(
192
- this._peerConnectionId,
193
- candidate.toJSON ? candidate.toJSON() : candidate
194
- );
161
+ async addIceCandidate(candidate) {
162
+ if (!candidate || !candidate.candidate) {
163
+ // XXX end-of cantidates is not implemented: https://bugs.chromium.org/p/webrtc/issues/detail?id=9218
164
+ return;
165
+ }
166
+
167
+ const newSdp = await WebRTCModule.peerConnectionAddICECandidate(
168
+ this._peerConnectionId,
169
+ candidate.toJSON ? candidate.toJSON() : candidate
170
+ );
195
171
 
196
- this.remoteDescription = new RTCSessionDescription(newSdp);
197
- }
172
+ this.remoteDescription = new RTCSessionDescription(newSdp);
173
+ }
198
174
 
199
- getStats() {
200
- return WebRTCModule.peerConnectionGetStats(this._peerConnectionId)
201
- .then( data => {
202
- /* On both Android and iOS it is faster to construct a single
175
+ getStats() {
176
+ return WebRTCModule.peerConnectionGetStats(this._peerConnectionId).then(data => {
177
+ /* On both Android and iOS it is faster to construct a single
203
178
  JSON string representing the Map of StatsReports and have it
204
179
  pass through the React Native bridge rather than the Map of
205
180
  StatsReports. While the implementations do try to be faster in
@@ -212,168 +187,163 @@ export default class RTCPeerConnection extends EventTarget(PEER_CONNECTION_EVENT
212
187
  */
213
188
  return new Map(JSON.parse(data));
214
189
  });
215
- }
216
-
217
- getLocalStreams() {
218
- return this._localStreams.slice();
219
- }
220
-
221
- getRemoteStreams() {
222
- return this._remoteStreams.slice();
223
- }
224
-
225
- close() {
226
- WebRTCModule.peerConnectionClose(this._peerConnectionId);
227
- }
228
-
229
- restartIce() {
230
- WebRTCModule.peerConnectionRestartIce(this._peerConnectionId);
231
- }
232
-
233
- _getTrack(streamReactTag, trackId): MediaStreamTrack {
234
- const stream
235
- = this._remoteStreams.find(
236
- stream => stream._reactTag === streamReactTag);
237
-
238
- return stream && stream._tracks.find(track => track.id === trackId);
239
- }
240
-
241
- _unregisterEvents(): void {
242
- this._subscriptions.forEach(e => e.remove());
243
- this._subscriptions = [];
244
- }
245
-
246
- _registerEvents(): void {
247
- this._subscriptions = [
248
- EventEmitter.addListener('peerConnectionOnRenegotiationNeeded', ev => {
249
- if (ev.id !== this._peerConnectionId) {
250
- return;
251
- }
252
- this.dispatchEvent(new RTCEvent('negotiationneeded'));
253
- }),
254
- EventEmitter.addListener('peerConnectionIceConnectionChanged', ev => {
255
- if (ev.id !== this._peerConnectionId) {
256
- return;
257
- }
258
- this.iceConnectionState = ev.iceConnectionState;
259
- this.dispatchEvent(new RTCEvent('iceconnectionstatechange'));
260
- if (ev.iceConnectionState === 'closed') {
261
- // This PeerConnection is done, clean up event handlers.
262
- this._unregisterEvents();
263
- }
264
- }),
265
- EventEmitter.addListener('peerConnectionStateChanged', ev => {
266
- if (ev.id !== this._peerConnectionId) {
267
- return;
268
- }
269
- this.connectionState = ev.connectionState;
270
- this.dispatchEvent(new RTCEvent('connectionstatechange'));
271
- if (ev.connectionState === 'closed') {
272
- // This PeerConnection is done, clean up event handlers.
273
- this._unregisterEvents();
274
- }
275
- }),
276
- EventEmitter.addListener('peerConnectionSignalingStateChanged', ev => {
277
- if (ev.id !== this._peerConnectionId) {
278
- return;
279
- }
280
- this.signalingState = ev.signalingState;
281
- this.dispatchEvent(new RTCEvent('signalingstatechange'));
282
- }),
283
- EventEmitter.addListener('peerConnectionAddedStream', ev => {
284
- if (ev.id !== this._peerConnectionId) {
285
- return;
286
- }
287
- const stream = new MediaStream(ev);
288
- this._remoteStreams.push(stream);
289
- this.remoteDescription = new RTCSessionDescription(ev.sdp);
290
- this.dispatchEvent(new MediaStreamEvent('addstream', {stream}));
291
- }),
292
- EventEmitter.addListener('peerConnectionRemovedStream', ev => {
293
- if (ev.id !== this._peerConnectionId) {
294
- return;
295
- }
296
- const stream = this._remoteStreams.find(s => s._reactTag === ev.streamId);
297
- if (stream) {
298
- const index = this._remoteStreams.indexOf(stream);
299
- if (index !== -1) {
300
- this._remoteStreams.splice(index, 1);
301
- }
302
- }
303
- this.remoteDescription = new RTCSessionDescription(ev.sdp);
304
- this.dispatchEvent(new MediaStreamEvent('removestream', {stream}));
305
- }),
306
- EventEmitter.addListener('mediaStreamTrackMuteChanged', ev => {
307
- if (ev.peerConnectionId !== this._peerConnectionId) {
308
- return;
309
- }
310
- const track = this._getTrack(ev.streamReactTag, ev.trackId);
311
- if (track) {
312
- track.muted = ev.muted;
313
- const eventName = ev.muted ? 'mute' : 'unmute';
314
- track.dispatchEvent(new MediaStreamTrackEvent(eventName, {track}));
315
- }
316
- }),
317
- EventEmitter.addListener('peerConnectionGotICECandidate', ev => {
318
- if (ev.id !== this._peerConnectionId) {
319
- return;
320
- }
321
- this.localDescription = new RTCSessionDescription(ev.sdp);
322
- const candidate = new RTCIceCandidate(ev.candidate);
323
- const event = new RTCIceCandidateEvent('icecandidate', {candidate});
324
- this.dispatchEvent(event);
325
- }),
326
- EventEmitter.addListener('peerConnectionIceGatheringChanged', ev => {
327
- if (ev.id !== this._peerConnectionId) {
328
- return;
329
- }
330
- this.iceGatheringState = ev.iceGatheringState;
190
+ }
331
191
 
332
- if (this.iceGatheringState === 'complete') {
333
- this.localDescription = new RTCSessionDescription(ev.sdp);
334
- this.dispatchEvent(new RTCIceCandidateEvent('icecandidate', null));
335
- }
192
+ getLocalStreams() {
193
+ return this._localStreams.slice();
194
+ }
336
195
 
337
- this.dispatchEvent(new RTCEvent('icegatheringstatechange'));
338
- }),
339
- EventEmitter.addListener('peerConnectionDidOpenDataChannel', ev => {
340
- if (ev.id !== this._peerConnectionId) {
341
- return;
342
- }
343
- const channel = new RTCDataChannel(ev.dataChannel);
344
- this.dispatchEvent(new RTCDataChannelEvent('datachannel', {channel}));
345
- })
346
- ];
347
- }
348
-
349
- /**
350
- * Creates a new RTCDataChannel object with the given label. The
351
- * RTCDataChannelInit dictionary can be used to configure properties of the
352
- * underlying channel such as data reliability.
353
- *
354
- * @param {string} label - the value with which the label attribute of the new
355
- * instance is to be initialized
356
- * @param {RTCDataChannelInit} dataChannelDict - an optional dictionary of
357
- * values with which to initialize corresponding attributes of the new
358
- * instance such as id
359
- */
360
- createDataChannel(label: string, dataChannelDict: ?RTCDataChannelInit) {
361
- if (dataChannelDict && 'id' in dataChannelDict) {
362
- const id = dataChannelDict.id;
363
- if (typeof id !== 'number') {
364
- throw new TypeError('DataChannel id must be a number: ' + id);
365
- }
196
+ getRemoteStreams() {
197
+ return this._remoteStreams.slice();
366
198
  }
367
199
 
368
- const channelInfo = WebRTCModule.createDataChannel(
369
- this._peerConnectionId,
370
- label,
371
- dataChannelDict);
200
+ close() {
201
+ WebRTCModule.peerConnectionClose(this._peerConnectionId);
202
+ }
372
203
 
373
- if (channelInfo === null) {
374
- throw new TypeError('Failed to create new DataChannel');
204
+ restartIce() {
205
+ WebRTCModule.peerConnectionRestartIce(this._peerConnectionId);
375
206
  }
376
207
 
377
- return new RTCDataChannel(channelInfo);
378
- }
208
+ _getTrack(streamReactTag, trackId): MediaStreamTrack {
209
+ const stream = this._remoteStreams.find(stream => stream._reactTag === streamReactTag);
210
+
211
+ return stream && stream._tracks.find(track => track.id === trackId);
212
+ }
213
+
214
+ _unregisterEvents(): void {
215
+ this._subscriptions.forEach(e => e.remove());
216
+ this._subscriptions = [];
217
+ }
218
+
219
+ _registerEvents(): void {
220
+ this._subscriptions = [
221
+ EventEmitter.addListener('peerConnectionOnRenegotiationNeeded', ev => {
222
+ if (ev.id !== this._peerConnectionId) {
223
+ return;
224
+ }
225
+ this.dispatchEvent(new RTCEvent('negotiationneeded'));
226
+ }),
227
+ EventEmitter.addListener('peerConnectionIceConnectionChanged', ev => {
228
+ if (ev.id !== this._peerConnectionId) {
229
+ return;
230
+ }
231
+ this.iceConnectionState = ev.iceConnectionState;
232
+ this.dispatchEvent(new RTCEvent('iceconnectionstatechange'));
233
+ if (ev.iceConnectionState === 'closed') {
234
+ // This PeerConnection is done, clean up event handlers.
235
+ this._unregisterEvents();
236
+ }
237
+ }),
238
+ EventEmitter.addListener('peerConnectionStateChanged', ev => {
239
+ if (ev.id !== this._peerConnectionId) {
240
+ return;
241
+ }
242
+ this.connectionState = ev.connectionState;
243
+ this.dispatchEvent(new RTCEvent('connectionstatechange'));
244
+ if (ev.connectionState === 'closed') {
245
+ // This PeerConnection is done, clean up event handlers.
246
+ this._unregisterEvents();
247
+ }
248
+ }),
249
+ EventEmitter.addListener('peerConnectionSignalingStateChanged', ev => {
250
+ if (ev.id !== this._peerConnectionId) {
251
+ return;
252
+ }
253
+ this.signalingState = ev.signalingState;
254
+ this.dispatchEvent(new RTCEvent('signalingstatechange'));
255
+ }),
256
+ EventEmitter.addListener('peerConnectionAddedStream', ev => {
257
+ if (ev.id !== this._peerConnectionId) {
258
+ return;
259
+ }
260
+ const stream = new MediaStream(ev);
261
+ this._remoteStreams.push(stream);
262
+ this.remoteDescription = new RTCSessionDescription(ev.sdp);
263
+ this.dispatchEvent(new MediaStreamEvent('addstream', { stream }));
264
+ }),
265
+ EventEmitter.addListener('peerConnectionRemovedStream', ev => {
266
+ if (ev.id !== this._peerConnectionId) {
267
+ return;
268
+ }
269
+ const stream = this._remoteStreams.find(s => s._reactTag === ev.streamId);
270
+ if (stream) {
271
+ const index = this._remoteStreams.indexOf(stream);
272
+ if (index !== -1) {
273
+ this._remoteStreams.splice(index, 1);
274
+ }
275
+ }
276
+ this.remoteDescription = new RTCSessionDescription(ev.sdp);
277
+ this.dispatchEvent(new MediaStreamEvent('removestream', { stream }));
278
+ }),
279
+ EventEmitter.addListener('mediaStreamTrackMuteChanged', ev => {
280
+ if (ev.peerConnectionId !== this._peerConnectionId) {
281
+ return;
282
+ }
283
+ const track = this._getTrack(ev.streamReactTag, ev.trackId);
284
+ if (track) {
285
+ track.muted = ev.muted;
286
+ const eventName = ev.muted ? 'mute' : 'unmute';
287
+ track.dispatchEvent(new MediaStreamTrackEvent(eventName, { track }));
288
+ }
289
+ }),
290
+ EventEmitter.addListener('peerConnectionGotICECandidate', ev => {
291
+ if (ev.id !== this._peerConnectionId) {
292
+ return;
293
+ }
294
+ this.localDescription = new RTCSessionDescription(ev.sdp);
295
+ const candidate = new RTCIceCandidate(ev.candidate);
296
+ const event = new RTCIceCandidateEvent('icecandidate', { candidate });
297
+ this.dispatchEvent(event);
298
+ }),
299
+ EventEmitter.addListener('peerConnectionIceGatheringChanged', ev => {
300
+ if (ev.id !== this._peerConnectionId) {
301
+ return;
302
+ }
303
+ this.iceGatheringState = ev.iceGatheringState;
304
+
305
+ if (this.iceGatheringState === 'complete') {
306
+ this.localDescription = new RTCSessionDescription(ev.sdp);
307
+ this.dispatchEvent(new RTCIceCandidateEvent('icecandidate', null));
308
+ }
309
+
310
+ this.dispatchEvent(new RTCEvent('icegatheringstatechange'));
311
+ }),
312
+ EventEmitter.addListener('peerConnectionDidOpenDataChannel', ev => {
313
+ if (ev.id !== this._peerConnectionId) {
314
+ return;
315
+ }
316
+ const channel = new RTCDataChannel(ev.dataChannel);
317
+ this.dispatchEvent(new RTCDataChannelEvent('datachannel', { channel }));
318
+ })
319
+ ];
320
+ }
321
+
322
+ /**
323
+ * Creates a new RTCDataChannel object with the given label. The
324
+ * RTCDataChannelInit dictionary can be used to configure properties of the
325
+ * underlying channel such as data reliability.
326
+ *
327
+ * @param {string} label - the value with which the label attribute of the new
328
+ * instance is to be initialized
329
+ * @param {RTCDataChannelInit} dataChannelDict - an optional dictionary of
330
+ * values with which to initialize corresponding attributes of the new
331
+ * instance such as id
332
+ */
333
+ createDataChannel(label: string, dataChannelDict: ?RTCDataChannelInit) {
334
+ if (dataChannelDict && 'id' in dataChannelDict) {
335
+ const id = dataChannelDict.id;
336
+ if (typeof id !== 'number') {
337
+ throw new TypeError('DataChannel id must be a number: ' + id);
338
+ }
339
+ }
340
+
341
+ const channelInfo = WebRTCModule.createDataChannel(this._peerConnectionId, label, dataChannelDict);
342
+
343
+ if (channelInfo === null) {
344
+ throw new TypeError('Failed to create new DataChannel');
345
+ }
346
+
347
+ return new RTCDataChannel(channelInfo);
348
+ }
379
349
  }