react-native-webrtc 106.0.0-beta.5 → 106.0.0-beta.7

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 (53) hide show
  1. package/README.md +2 -2
  2. package/android/src/main/java/com/oney/WebRTCModule/GetUserMediaImpl.java +1 -1
  3. package/android/src/main/java/com/oney/WebRTCModule/PeerConnectionObserver.java +18 -24
  4. package/android/src/main/java/com/oney/WebRTCModule/VideoTrackAdapter.java +1 -1
  5. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +59 -68
  6. package/ios/RCTWebRTC/RCTConvert+WebRTC.m +10 -0
  7. package/ios/RCTWebRTC/SerializeUtils.m +4 -2
  8. package/ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.m +73 -60
  9. package/ios/RCTWebRTC/WebRTCModule+Transceivers.m +23 -22
  10. package/ios/RCTWebRTC/WebRTCModule+VideoTrackAdapter.m +1 -1
  11. package/ios/RCTWebRTC/WebRTCModule.h +0 -8
  12. package/ios/RCTWebRTC/WebRTCModule.m +1 -20
  13. package/lib/commonjs/MediaStreamTrack.js +24 -7
  14. package/lib/commonjs/MediaStreamTrack.js.map +1 -1
  15. package/lib/commonjs/RTCIceCandidate.js +14 -4
  16. package/lib/commonjs/RTCIceCandidate.js.map +1 -1
  17. package/lib/commonjs/RTCPeerConnection.js +129 -132
  18. package/lib/commonjs/RTCPeerConnection.js.map +1 -1
  19. package/lib/commonjs/RTCRtpSender.js +6 -5
  20. package/lib/commonjs/RTCRtpSender.js.map +1 -1
  21. package/lib/commonjs/RTCRtpTransceiver.js +11 -49
  22. package/lib/commonjs/RTCRtpTransceiver.js.map +1 -1
  23. package/lib/commonjs/RTCView.js +0 -55
  24. package/lib/commonjs/RTCView.js.map +1 -1
  25. package/lib/commonjs/index.js +3 -0
  26. package/lib/commonjs/index.js.map +1 -1
  27. package/lib/module/MediaStreamTrack.js +25 -8
  28. package/lib/module/MediaStreamTrack.js.map +1 -1
  29. package/lib/module/RTCIceCandidate.js +14 -4
  30. package/lib/module/RTCIceCandidate.js.map +1 -1
  31. package/lib/module/RTCPeerConnection.js +129 -130
  32. package/lib/module/RTCPeerConnection.js.map +1 -1
  33. package/lib/module/RTCRtpSender.js +6 -5
  34. package/lib/module/RTCRtpSender.js.map +1 -1
  35. package/lib/module/RTCRtpTransceiver.js +11 -44
  36. package/lib/module/RTCRtpTransceiver.js.map +1 -1
  37. package/lib/module/RTCView.js +0 -49
  38. package/lib/module/RTCView.js.map +1 -1
  39. package/lib/module/index.js +2 -0
  40. package/lib/module/index.js.map +1 -1
  41. package/lib/typescript/MediaStreamTrack.d.ts +11 -3
  42. package/lib/typescript/RTCIceCandidate.d.ts +5 -1
  43. package/lib/typescript/RTCPeerConnection.d.ts +5 -5
  44. package/lib/typescript/RTCRtpTransceiver.d.ts +2 -8
  45. package/lib/typescript/RTCView.d.ts +53 -46
  46. package/package.json +2 -2
  47. package/src/MediaStreamTrack.ts +23 -7
  48. package/src/RTCIceCandidate.ts +8 -5
  49. package/src/RTCPeerConnection.ts +133 -126
  50. package/src/RTCRtpSender.ts +6 -7
  51. package/src/RTCRtpTransceiver.ts +15 -45
  52. package/src/RTCView.ts +53 -49
  53. package/src/index.ts +2 -0
@@ -6,10 +6,8 @@ import { addListener, removeListener } from './EventEmitter';
6
6
  import Logger from './Logger';
7
7
  import MediaStream from './MediaStream';
8
8
  import MediaStreamTrack from './MediaStreamTrack';
9
- import MediaStreamTrackEvent from './MediaStreamTrackEvent';
10
9
  import RTCDataChannel from './RTCDataChannel';
11
10
  import RTCDataChannelEvent from './RTCDataChannelEvent';
12
- import RTCErrorEvent from './RTCErrorEvent';
13
11
  import RTCEvent from './RTCEvent';
14
12
  import RTCIceCandidate from './RTCIceCandidate';
15
13
  import RTCIceCandidateEvent from './RTCIceCandidateEvent';
@@ -72,34 +70,34 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
72
70
  connectionState: RTCPeerConnectionState = 'new';
73
71
  iceConnectionState: RTCIceConnectionState = 'new';
74
72
 
75
- _peerConnectionId: number;
73
+ _pcId: number;
76
74
  _transceivers: { order: number, transceiver: RTCRtpTransceiver }[] = [];
77
75
  _remoteStreams: Map<string, MediaStream> = new Map<string, MediaStream>();
78
76
 
79
77
  constructor(configuration) {
80
78
  super();
81
- this._peerConnectionId = nextPeerConnectionId++;
82
- WebRTCModule.peerConnectionInit(configuration, this._peerConnectionId);
79
+ this._pcId = nextPeerConnectionId++;
80
+ WebRTCModule.peerConnectionInit(configuration, this._pcId);
83
81
  this._registerEvents();
84
82
 
85
- log.debug(`${this._peerConnectionId} ctor`);
83
+ log.debug(`${this._pcId} ctor`);
86
84
  }
87
85
 
88
86
  createOffer(options) {
89
- log.debug(`${this._peerConnectionId} createOffer`);
87
+ log.debug(`${this._pcId} createOffer`);
90
88
 
91
89
  return new Promise((resolve, reject) => {
92
90
  WebRTCModule.peerConnectionCreateOffer(
93
- this._peerConnectionId,
91
+ this._pcId,
94
92
  RTCUtil.normalizeOfferOptions(options),
95
93
  (successful, data) => {
96
94
  if (successful) {
97
- log.debug(`${this._peerConnectionId} createOffer OK`);
95
+ log.debug(`${this._pcId} createOffer OK`);
98
96
 
99
97
  this._updateTransceivers(data.transceiversInfo);
100
98
  resolve(data.sdpInfo);
101
99
  } else {
102
- log.debug(`${this._peerConnectionId} createOffer ERROR`);
100
+ log.debug(`${this._pcId} createOffer ERROR`);
103
101
 
104
102
  reject(data);
105
103
  }
@@ -109,20 +107,20 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
109
107
  }
110
108
 
111
109
  createAnswer() {
112
- log.debug(`${this._peerConnectionId} createAnswer`);
110
+ log.debug(`${this._pcId} createAnswer`);
113
111
 
114
112
  return new Promise((resolve, reject) => {
115
113
  WebRTCModule.peerConnectionCreateAnswer(
116
- this._peerConnectionId,
114
+ this._pcId,
117
115
  {},
118
116
  (successful, data) => {
119
117
  if (successful) {
120
- log.debug(`${this._peerConnectionId} createAnswer OK`);
118
+ log.debug(`${this._pcId} createAnswer OK`);
121
119
 
122
120
  this._updateTransceivers(data.transceiversInfo);
123
121
  resolve(data.sdpInfo);
124
122
  } else {
125
- log.debug(`${this._peerConnectionId} createAnswer ERROR`);
123
+ log.debug(`${this._pcId} createAnswer ERROR`);
126
124
 
127
125
  reject(data);
128
126
  }
@@ -132,11 +130,11 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
132
130
  }
133
131
 
134
132
  setConfiguration(configuration): void {
135
- WebRTCModule.peerConnectionSetConfiguration(configuration, this._peerConnectionId);
133
+ WebRTCModule.peerConnectionSetConfiguration(configuration, this._pcId);
136
134
  }
137
135
 
138
136
  async setLocalDescription(sessionDescription?: RTCSessionDescription | RTCSessionDescriptionInit): Promise<void> {
139
- log.debug(`${this._peerConnectionId} setLocalDescription`);
137
+ log.debug(`${this._pcId} setLocalDescription`);
140
138
 
141
139
  let desc;
142
140
 
@@ -156,16 +154,21 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
156
154
  const {
157
155
  sdpInfo,
158
156
  transceiversInfo
159
- } = await WebRTCModule.peerConnectionSetLocalDescription(this._peerConnectionId, desc);
157
+ } = await WebRTCModule.peerConnectionSetLocalDescription(this._pcId, desc);
158
+
159
+ if (sdpInfo.type && sdpInfo.sdp) {
160
+ this.localDescription = new RTCSessionDescription(sdpInfo);
161
+ } else {
162
+ this.localDescription = null;
163
+ }
160
164
 
161
- this.localDescription = new RTCSessionDescription(sdpInfo);
162
165
  this._updateTransceivers(transceiversInfo);
163
166
 
164
- log.debug(`${this._peerConnectionId} setLocalDescription OK`);
167
+ log.debug(`${this._pcId} setLocalDescription OK`);
165
168
  }
166
169
 
167
170
  setRemoteDescription(sessionDescription: RTCSessionDescription | RTCSessionDescriptionInit): Promise<void> {
168
- log.debug(`${this._peerConnectionId} setRemoteDescription`);
171
+ log.debug(`${this._pcId} setRemoteDescription`);
169
172
 
170
173
  if (!sessionDescription) {
171
174
  return Promise.reject(new Error('No session description provided'));
@@ -183,17 +186,29 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
183
186
  return new Promise((resolve, reject) => {
184
187
  WebRTCModule.peerConnectionSetRemoteDescription(
185
188
  desc,
186
- this._peerConnectionId,
189
+ this._pcId,
187
190
  (successful, data) => {
188
191
  if (successful) {
189
- log.debug(`${this._peerConnectionId} setRemoteDescription OK`);
192
+ log.debug(`${this._pcId} setRemoteDescription OK`);
190
193
 
191
- this.remoteDescription = new RTCSessionDescription(data.sdpInfo);
194
+ const {
195
+ sdpInfo,
196
+ newTransceivers,
197
+ transceiversInfo
198
+ } = data;
192
199
 
193
- data.newTransceivers?.forEach( t => {
200
+ if (sdpInfo.type && sdpInfo.sdp) {
201
+ this.remoteDescription = new RTCSessionDescription(sdpInfo);
202
+ } else {
203
+ this.remoteDescription = null;
204
+ }
205
+
206
+ newTransceivers?.forEach( t => {
194
207
  const { transceiverOrder, transceiver } = t;
195
- const newSender = new RTCRtpSender(transceiver.sender);
196
- const newReceiver = new RTCRtpReceiver(transceiver.receiver);
208
+ const newSender = new RTCRtpSender({ ...transceiver.sender, track: null });
209
+ const remoteTrack
210
+ = transceiver.receiver.track ? new MediaStreamTrack(transceiver.receiver.track) : null;
211
+ const newReceiver = new RTCRtpReceiver({ ...transceiver.receiver, track: remoteTrack });
197
212
  const newTransceiver = new RTCRtpTransceiver({
198
213
  ...transceiver,
199
214
  sender: newSender,
@@ -203,7 +218,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
203
218
  this._insertTransceiverSorted(transceiverOrder, newTransceiver);
204
219
  });
205
220
 
206
- this._updateTransceivers(data.transceiversInfo);
221
+ this._updateTransceivers(transceiversInfo);
207
222
 
208
223
  resolve();
209
224
  } else {
@@ -215,15 +230,24 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
215
230
  }
216
231
 
217
232
  async addIceCandidate(candidate): Promise<void> {
218
- log.debug(`${this._peerConnectionId} addIceCandidate`);
233
+ log.debug(`${this._pcId} addIceCandidate`);
219
234
 
220
235
  if (!candidate || !candidate.candidate) {
221
- // XXX end-of cantidates is not implemented: https://bugs.chromium.org/p/webrtc/issues/detail?id=9218
236
+ // XXX end-of candidates is not implemented: https://bugs.chromium.org/p/webrtc/issues/detail?id=9218
222
237
  return;
223
238
  }
224
239
 
240
+ if (
241
+ candidate.sdpMLineIndex === null ||
242
+ candidate.sdpMLineIndex === undefined ||
243
+ candidate.sdpMid === null ||
244
+ candidate.sdpMid === undefined
245
+ ) {
246
+ throw new TypeError('`sdpMLineIndex` and `sdpMid` must not null or undefined');
247
+ }
248
+
225
249
  const newSdp = await WebRTCModule.peerConnectionAddICECandidate(
226
- this._peerConnectionId,
250
+ this._pcId,
227
251
  candidate.toJSON ? candidate.toJSON() : candidate
228
252
  );
229
253
 
@@ -232,14 +256,14 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
232
256
 
233
257
  /**
234
258
  * @brief Adds a new track to the {@link RTCPeerConnection},
235
- * and indicates that it is contained in the specified {@link MediaStream} array.
236
- * This method has to be synchronous as the W3C API expects a track to be return
259
+ * and indicates that it is contained in the specified {@link MediaStream}s.
260
+ * This method has to be synchronous as the W3C API expects a track to be returned
237
261
  * @param {MediaStreamTrack} track The track to be added
238
- * @param {MediaStream[]} streams An array of streams the track needs to be added to
262
+ * @param {...MediaStream} streams One or more {@link MediaStream}s the track needs to be added to
239
263
  * https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtrack
240
264
  */
241
- addTrack(track: MediaStreamTrack, streams: MediaStream[] = []): RTCRtpSender {
242
- log.debug(`${this._peerConnectionId} addTrack`);
265
+ addTrack(track: MediaStreamTrack, ...streams: MediaStream[]): RTCRtpSender {
266
+ log.debug(`${this._pcId} addTrack`);
243
267
 
244
268
  if (this.connectionState === 'closed') {
245
269
  throw new Error('Peer Connection is closed');
@@ -250,7 +274,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
250
274
  }
251
275
 
252
276
  const streamIds = streams.map(s => s.id);
253
- const result = WebRTCModule.peerConnectionAddTrack(this._peerConnectionId, track.id, { streamIds });
277
+ const result = WebRTCModule.peerConnectionAddTrack(this._pcId, track.id, { streamIds });
254
278
 
255
279
  if (result === null) {
256
280
  throw new Error('Could not add sender');
@@ -295,7 +319,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
295
319
  }
296
320
 
297
321
  addTransceiver(source: 'audio' | 'video' | MediaStreamTrack, init): RTCRtpTransceiver {
298
- log.debug(`${this._peerConnectionId} addTransceiver`);
322
+ log.debug(`${this._pcId} addTransceiver`);
299
323
 
300
324
  let src = {};
301
325
 
@@ -312,7 +336,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
312
336
  init.streamIds = init.streams.map(stream => stream.id);
313
337
  }
314
338
 
315
- const result = WebRTCModule.peerConnectionAddTransceiver(this._peerConnectionId, { ...src, init: { ...init } });
339
+ const result = WebRTCModule.peerConnectionAddTransceiver(this._pcId, { ...src, init: { ...init } });
316
340
 
317
341
  if (result === null) {
318
342
  throw new Error('Transceiver could not be added');
@@ -345,9 +369,9 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
345
369
  }
346
370
 
347
371
  removeTrack(sender: RTCRtpSender) {
348
- log.debug(`${this._peerConnectionId} removeTrack`);
372
+ log.debug(`${this._pcId} removeTrack`);
349
373
 
350
- if (this._peerConnectionId !== sender._peerConnectionId) {
374
+ if (this._pcId !== sender._peerConnectionId) {
351
375
  throw new Error('Sender does not belong to this peer connection');
352
376
  }
353
377
 
@@ -367,13 +391,22 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
367
391
  return;
368
392
  }
369
393
 
370
- WebRTCModule.peerConnectionRemoveTrack(this._peerConnectionId, sender.id);
394
+ // Blocking!
395
+ WebRTCModule.peerConnectionRemoveTrack(this._pcId, sender.id);
396
+
397
+ existingSender._track = null;
398
+
399
+ const [ existingTransceiver ] = this
400
+ .getTransceivers()
401
+ .filter(t => t.sender.id === existingSender.id);
402
+
403
+ existingTransceiver._direction = existingTransceiver.direction === 'sendrecv' ? 'recvonly' : 'inactive';
371
404
  }
372
405
 
373
406
  getStats() {
374
- log.debug(`${this._peerConnectionId} getStats`);
407
+ log.debug(`${this._pcId} getStats`);
375
408
 
376
- return WebRTCModule.peerConnectionGetStats(this._peerConnectionId).then(data =>
409
+ return WebRTCModule.peerConnectionGetStats(this._pcId).then(data =>
377
410
  /* On both Android and iOS it is faster to construct a single
378
411
  JSON string representing the Map of StatsReports and have it
379
412
  pass through the React Native bridge rather than the Map of
@@ -402,23 +435,23 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
402
435
  }
403
436
 
404
437
  close(): void {
405
- log.debug(`${this._peerConnectionId} close`);
438
+ log.debug(`${this._pcId} close`);
439
+
440
+ WebRTCModule.peerConnectionClose(this._pcId);
406
441
 
407
- // According to the W3C spec: https://w3c.github.io/webrtc-pc/#rtcpeerconnection-interface
408
- // transceivers have to be stopped
442
+ // Mark transceivers as stopped.
409
443
  this._transceivers.forEach(({ transceiver })=> {
410
- transceiver.stop();
444
+ transceiver._setStopped();
411
445
  });
412
- WebRTCModule.peerConnectionClose(this._peerConnectionId);
413
446
  }
414
447
 
415
448
  restartIce(): void {
416
- WebRTCModule.peerConnectionRestartIce(this._peerConnectionId);
449
+ WebRTCModule.peerConnectionRestartIce(this._pcId);
417
450
  }
418
451
 
419
452
  _registerEvents(): void {
420
453
  addListener(this, 'peerConnectionOnRenegotiationNeeded', (ev: any) => {
421
- if (ev.id !== this._peerConnectionId) {
454
+ if (ev.pcId !== this._pcId) {
422
455
  return;
423
456
  }
424
457
 
@@ -427,7 +460,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
427
460
  });
428
461
 
429
462
  addListener(this, 'peerConnectionIceConnectionChanged', (ev: any) => {
430
- if (ev.id !== this._peerConnectionId) {
463
+ if (ev.pcId !== this._pcId) {
431
464
  return;
432
465
  }
433
466
 
@@ -443,7 +476,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
443
476
  });
444
477
 
445
478
  addListener(this, 'peerConnectionStateChanged', (ev: any) => {
446
- if (ev.id !== this._peerConnectionId) {
479
+ if (ev.pcId !== this._pcId) {
447
480
  return;
448
481
  }
449
482
 
@@ -459,7 +492,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
459
492
  });
460
493
 
461
494
  addListener(this, 'peerConnectionSignalingStateChanged', (ev: any) => {
462
- if (ev.id !== this._peerConnectionId) {
495
+ if (ev.pcId !== this._pcId) {
463
496
  return;
464
497
  }
465
498
 
@@ -470,33 +503,38 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
470
503
 
471
504
  // Consider moving away from this event: https://github.com/WebKit/WebKit/pull/3953
472
505
  addListener(this, 'peerConnectionOnTrack', (ev: any) => {
473
- if (ev.id !== this._peerConnectionId) {
506
+ if (ev.pcId !== this._pcId) {
474
507
  return;
475
508
  }
476
509
 
477
- log.debug(`${this._peerConnectionId} ontrack`);
510
+ log.debug(`${this._pcId} ontrack`);
478
511
 
479
- const track = new MediaStreamTrack(ev.receiver.track);
512
+ let track;
480
513
  let transceiver;
481
514
 
482
- // Make sure transceivers are stored in timestamp order. Also, we have to make
483
- // sure we do not add a transceiver if it exists.
484
- const [ { transceiver: oldTransceiver } = { transceiver: null } ]
485
- = this._transceivers.filter(({ transceiver }) => transceiver.id === ev.transceiver.id);
515
+ const [ oldTransceiver ] = this
516
+ .getTransceivers()
517
+ .filter(t => t.receiver.id === ev.receiver.id);
486
518
 
487
- if (!oldTransceiver) {
488
- // Creating objects out of the event data.
489
- const sender = new RTCRtpSender({ ...ev.transceiver.sender });
490
- const receiver = new RTCRtpReceiver({ ...ev.receiver, track });
519
+ // We need to fire this event for an existing track sometimes, like
520
+ // when the transceiver direction (on the sending side) switches from
521
+ // sendrecv to recvonly and then back.
491
522
 
492
- transceiver = new RTCRtpTransceiver({ ...ev.transceiver, receiver, sender });
493
- this._insertTransceiverSorted(ev.transceiverOrder, transceiver);
494
- } else {
523
+ if (oldTransceiver) {
495
524
  transceiver = oldTransceiver;
496
- transceiver._receiver._track = track;
525
+ track = transceiver.receiver.track;
497
526
  transceiver._mid = ev.transceiver.mid;
498
527
  transceiver._currentDirection = ev.transceiver.currentDirection;
499
528
  transceiver._direction = ev.transceiver.direction;
529
+ } else {
530
+ // TODO: re-evaluate if we need this branch at all. Since we now add new transceivers
531
+ // in sRD, we should always find them when this handler runs.
532
+ track = new MediaStreamTrack(ev.receiver.track);
533
+ const sender = new RTCRtpSender({ ...ev.transceiver, track: null });
534
+ const receiver = new RTCRtpReceiver({ ...ev.receiver, track });
535
+
536
+ transceiver = new RTCRtpTransceiver({ ...ev.transceiver, receiver, sender });
537
+ this._insertTransceiverSorted(ev.transceiverOrder, transceiver);
500
538
  }
501
539
 
502
540
  // Get the stream object from the event. Create if necessary.
@@ -515,7 +553,9 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
515
553
 
516
554
  const stream = this._remoteStreams.get(streamInfo.streamId);
517
555
 
518
- stream?._tracks.push(track);
556
+ if (!stream?._tracks.includes(track)) {
557
+ stream?._tracks.push(track);
558
+ }
519
559
 
520
560
  return stream;
521
561
  });
@@ -529,53 +569,39 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
529
569
 
530
570
  // @ts-ignore
531
571
  this.dispatchEvent(new RTCTrackEvent('track', eventData));
572
+
573
+ // Dispatch an unmute event for the track.
574
+ track._setMutedInternal(false);
532
575
  });
533
576
 
534
577
  addListener(this, 'peerConnectionOnRemoveTrack', (ev: any) => {
535
- if (ev.peerConnectionId !== this._peerConnectionId) {
578
+ if (ev.pcId !== this._pcId) {
536
579
  return;
537
580
  }
538
581
 
539
- log.debug(`${this._peerConnectionId} onremovetrack`);
582
+ log.debug(`${this._pcId} onremovetrack`);
540
583
 
541
- // Based on the W3C specs https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-removetrack,
542
- // we need to remove the track from any media streams
543
- // that were previously passed to the `track` event.
584
+ // As per the spec:
585
+ // - Remove the track from any media streams that were previously passed to the `track` event.
586
+ // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-removetrack,
587
+ // - Mark the track as muted:
588
+ // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
544
589
  for (const stream of this._remoteStreams.values()) {
545
- const trackIdx = stream._tracks.findIndex(t => t.id === ev.trackId);
546
-
547
- if (trackIdx !== -1) {
548
- stream._tracks.splice(trackIdx, 1);
549
- }
550
- }
551
- });
552
-
553
- addListener(this, 'peerConnectionOnRemoveTrackSuccessful', (ev: any) => {
554
- if (ev.peerConnectionId !== this._peerConnectionId) {
555
- return;
556
- }
590
+ const [ track ] = stream._tracks.filter(t => t.id === ev.trackId);
557
591
 
558
- const [ existingSender ] = this
559
- .getSenders()
560
- .filter(s => s.id === ev.senderId);
592
+ if (track) {
593
+ const trackIdx = stream._tracks.indexOf(track);
561
594
 
562
- const oldTrack = existingSender._track;
595
+ stream._tracks.splice(trackIdx, 1);
563
596
 
564
- if (oldTrack) {
565
- oldTrack._muted = true;
597
+ // Dispatch a mute event for the track.
598
+ track._setMutedInternal(true);
599
+ }
566
600
  }
567
-
568
- existingSender._track = null;
569
-
570
- const [ existingTransceiver ] = this
571
- .getTransceivers()
572
- .filter(t => t.sender.id === existingSender.id);
573
-
574
- existingTransceiver._direction = existingTransceiver.direction === 'sendrecv' ? 'recvonly' : 'inactive';
575
601
  });
576
602
 
577
603
  addListener(this, 'peerConnectionGotICECandidate', (ev: any) => {
578
- if (ev.id !== this._peerConnectionId) {
604
+ if (ev.pcId !== this._pcId) {
579
605
  return;
580
606
  }
581
607
 
@@ -587,7 +613,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
587
613
  });
588
614
 
589
615
  addListener(this, 'peerConnectionIceGatheringChanged', (ev: any) => {
590
- if (ev.id !== this._peerConnectionId) {
616
+ if (ev.pcId !== this._pcId) {
591
617
  return;
592
618
  }
593
619
 
@@ -604,7 +630,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
604
630
  });
605
631
 
606
632
  addListener(this, 'peerConnectionDidOpenDataChannel', (ev: any) => {
607
- if (ev.id !== this._peerConnectionId) {
633
+ if (ev.pcId !== this._pcId) {
608
634
  return;
609
635
  }
610
636
 
@@ -614,23 +640,8 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
614
640
  this.dispatchEvent(new RTCDataChannelEvent('datachannel', { channel }));
615
641
  });
616
642
 
617
- // Since the current underlying architecture performs certain actions
618
- // Asynchronously when the outer web API expects synchronous behaviour
619
- // This is the only way to report error on operations for those who wish
620
- // to handle them.
621
- addListener(this, 'peerConnectionOnError', (ev: any) => {
622
- if (ev.info.peerConnectionId !== this._peerConnectionId) {
623
- return;
624
- }
625
-
626
- log.error(`onerror: ${JSON.stringify(ev)}`);
627
-
628
- // @ts-ignore
629
- this.dispatchEvent(new RTCErrorEvent('error', ev.func, ev.message));
630
- });
631
-
632
643
  addListener(this, 'mediaStreamTrackMuteChanged', (ev: any) => {
633
- if (ev.peerConnectionId !== this._peerConnectionId) {
644
+ if (ev.pcId !== this._pcId) {
634
645
  return;
635
646
  }
636
647
 
@@ -639,11 +650,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
639
650
  ] = this.getReceivers().map(r => r.track).filter(t => t?.id === ev.trackId);
640
651
 
641
652
  if (track) {
642
- track._muted = ev.muted;
643
- const eventName = ev.muted ? 'mute' : 'unmute';
644
-
645
- // @ts-ignore
646
- track.dispatchEvent(new MediaStreamTrackEvent(eventName, { track }));
653
+ track._setMutedInternal(ev.muted);
647
654
  }
648
655
  });
649
656
  }
@@ -668,7 +675,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
668
675
  }
669
676
  }
670
677
 
671
- const channelInfo = WebRTCModule.createDataChannel(this._peerConnectionId, label, dataChannelDict);
678
+ const channelInfo = WebRTCModule.createDataChannel(this._pcId, label, dataChannelDict);
672
679
 
673
680
  if (channelInfo === null) {
674
681
  throw new TypeError('Failed to create new DataChannel');
@@ -698,7 +705,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
698
705
  for (const update of transceiverUpdates) {
699
706
  const [ transceiver ] = this
700
707
  .getTransceivers()
701
- .filter(t => t.id === update.transceiverId);
708
+ .filter(t => t.sender.id === update.transceiverId);
702
709
 
703
710
  if (!transceiver) {
704
711
  continue;
@@ -54,13 +54,12 @@ export default class RTCRtpSender {
54
54
  return this._rtpParameters;
55
55
  }
56
56
 
57
- setParameters(parameters: RTCRtpSendParameters): Promise<void> {
58
- return WebRTCModule.senderSetParameters(this._peerConnectionId,
59
- this._id,
60
- JSON.parse(JSON.stringify(parameters)))// This allows us to get rid of private "underscore properties"
61
- .then(newParameters => {
62
- this._rtpParameters = new RTCRtpSendParameters(newParameters);
63
- });
57
+ async setParameters(parameters: RTCRtpSendParameters): Promise<void> {
58
+ // This allows us to get rid of private "underscore properties"
59
+ const _params = JSON.parse(JSON.stringify(parameters));
60
+ const newParameters = await WebRTCModule.senderSetParameters(this._peerConnectionId, this._id, _params);
61
+
62
+ this._rtpParameters = new RTCRtpSendParameters(newParameters);
64
63
  }
65
64
 
66
65
  get track() {
@@ -1,23 +1,16 @@
1
- import { defineCustomEventTarget } from 'event-target-shim';
2
1
  import { NativeModules } from 'react-native';
3
2
 
4
- import { addListener, removeListener } from './EventEmitter';
5
- import RTCErrorEvent from './RTCErrorEvent';
6
3
  import RTCRtpReceiver from './RTCRtpReceiver';
7
4
  import RTCRtpSender from './RTCRtpSender';
8
5
 
9
6
  const { WebRTCModule } = NativeModules;
10
7
 
11
- const TRANSCEIVER_EVENTS = [
12
- 'error'
13
- ];
14
8
 
15
- export default class RTCRtpTransceiver extends defineCustomEventTarget(...TRANSCEIVER_EVENTS) {
9
+ export default class RTCRtpTransceiver {
16
10
  _peerConnectionId: number;
17
11
  _sender: RTCRtpSender;
18
12
  _receiver: RTCRtpReceiver;
19
13
 
20
- _id: string;
21
14
  _mid: string | null = null;
22
15
  _direction: string;
23
16
  _currentDirection: string;
@@ -25,7 +18,6 @@ export default class RTCRtpTransceiver extends defineCustomEventTarget(...TRANSC
25
18
 
26
19
  constructor(args: {
27
20
  peerConnectionId: number,
28
- id: string,
29
21
  isStopped: boolean,
30
22
  direction: string,
31
23
  currentDirection: string,
@@ -33,20 +25,13 @@ export default class RTCRtpTransceiver extends defineCustomEventTarget(...TRANSC
33
25
  sender: RTCRtpSender,
34
26
  receiver: RTCRtpReceiver,
35
27
  }) {
36
- super();
37
28
  this._peerConnectionId = args.peerConnectionId;
38
- this._id = args.id;
39
29
  this._mid = args.mid ? args.mid : null;
40
30
  this._direction = args.direction;
41
31
  this._currentDirection = args.currentDirection;
42
32
  this._stopped = args.isStopped;
43
33
  this._sender = args.sender;
44
34
  this._receiver = args.receiver;
45
- this._registerEvents();
46
- }
47
-
48
- get id() {
49
- return this._id;
50
35
  }
51
36
 
52
37
  get mid() {
@@ -74,7 +59,13 @@ export default class RTCRtpTransceiver extends defineCustomEventTarget(...TRANSC
74
59
  return;
75
60
  }
76
61
 
77
- WebRTCModule.transceiverSetDirection(this._peerConnectionId, this.id, val);
62
+ const oldDirection = this._direction;
63
+
64
+ WebRTCModule.transceiverSetDirection(this._peerConnectionId, this.sender.id, val)
65
+ .catch(() => {
66
+ this._direction = oldDirection;
67
+ });
68
+
78
69
  this._direction = val;
79
70
  }
80
71
 
@@ -95,35 +86,14 @@ export default class RTCRtpTransceiver extends defineCustomEventTarget(...TRANSC
95
86
  return;
96
87
  }
97
88
 
98
- WebRTCModule.transceiverStop(this._peerConnectionId, this.id);
89
+ WebRTCModule.transceiverStop(this._peerConnectionId, this.sender.id)
90
+ .then(() => this._setStopped());
99
91
  }
100
92
 
101
- _registerEvents(): void {
102
- addListener(this, 'transceiverStopSuccessful', (ev: any) => {
103
- if (ev.peerConnectionId !== this._peerConnectionId || ev.transceiverId !== this._id) {
104
- return;
105
- }
106
-
107
- this._stopped = true;
108
- this._direction = 'stopped';
109
- this._currentDirection = 'stopped';
110
- this._mid = null;
111
- removeListener(this);
112
- });
113
-
114
- addListener(this, 'transceiverOnError', (ev: any) => {
115
- if (ev.info.peerConnectionId !== this._peerConnectionId || ev.info.transceiverId !== this._id) {
116
- return;
117
- }
118
-
119
- if (ev.func === 'stopTransceiver') {
120
- this._stopped = false;
121
- } else if (ev.func === 'setDirection') {
122
- this._direction = ev.info.oldDirection;
123
- }
124
-
125
- // @ts-ignore
126
- this.dispatchEvent(new RTCErrorEvent('error', ev.func, ev.message));
127
- });
93
+ _setStopped() {
94
+ this._stopped = true;
95
+ this._direction = 'stopped';
96
+ this._currentDirection = 'stopped';
97
+ this._mid = null;
128
98
  }
129
99
  }