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

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 (38) hide show
  1. package/README.md +2 -2
  2. package/android/src/main/java/com/oney/WebRTCModule/PeerConnectionObserver.java +18 -24
  3. package/android/src/main/java/com/oney/WebRTCModule/VideoTrackAdapter.java +1 -1
  4. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +39 -62
  5. package/ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.m +54 -48
  6. package/ios/RCTWebRTC/WebRTCModule+Transceivers.m +23 -22
  7. package/ios/RCTWebRTC/WebRTCModule+VideoTrackAdapter.m +1 -1
  8. package/ios/RCTWebRTC/WebRTCModule.h +0 -8
  9. package/ios/RCTWebRTC/WebRTCModule.m +1 -20
  10. package/lib/commonjs/MediaStreamTrack.js +16 -0
  11. package/lib/commonjs/MediaStreamTrack.js.map +1 -1
  12. package/lib/commonjs/RTCPeerConnection.js +92 -109
  13. package/lib/commonjs/RTCPeerConnection.js.map +1 -1
  14. package/lib/commonjs/RTCRtpSender.js +6 -5
  15. package/lib/commonjs/RTCRtpSender.js.map +1 -1
  16. package/lib/commonjs/RTCRtpTransceiver.js +11 -42
  17. package/lib/commonjs/RTCRtpTransceiver.js.map +1 -1
  18. package/lib/commonjs/index.js +3 -0
  19. package/lib/commonjs/index.js.map +1 -1
  20. package/lib/module/MediaStreamTrack.js +17 -1
  21. package/lib/module/MediaStreamTrack.js.map +1 -1
  22. package/lib/module/RTCPeerConnection.js +92 -107
  23. package/lib/module/RTCPeerConnection.js.map +1 -1
  24. package/lib/module/RTCRtpSender.js +6 -5
  25. package/lib/module/RTCRtpSender.js.map +1 -1
  26. package/lib/module/RTCRtpTransceiver.js +11 -37
  27. package/lib/module/RTCRtpTransceiver.js.map +1 -1
  28. package/lib/module/index.js +2 -0
  29. package/lib/module/index.js.map +1 -1
  30. package/lib/typescript/MediaStreamTrack.d.ts +9 -2
  31. package/lib/typescript/RTCPeerConnection.d.ts +5 -5
  32. package/lib/typescript/RTCRtpTransceiver.d.ts +2 -5
  33. package/package.json +2 -2
  34. package/src/MediaStreamTrack.ts +16 -1
  35. package/src/RTCPeerConnection.ts +94 -114
  36. package/src/RTCRtpSender.ts +6 -7
  37. package/src/RTCRtpTransceiver.ts +15 -38
  38. 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,16 @@ 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);
160
158
 
161
159
  this.localDescription = new RTCSessionDescription(sdpInfo);
162
160
  this._updateTransceivers(transceiversInfo);
163
161
 
164
- log.debug(`${this._peerConnectionId} setLocalDescription OK`);
162
+ log.debug(`${this._pcId} setLocalDescription OK`);
165
163
  }
166
164
 
167
165
  setRemoteDescription(sessionDescription: RTCSessionDescription | RTCSessionDescriptionInit): Promise<void> {
168
- log.debug(`${this._peerConnectionId} setRemoteDescription`);
166
+ log.debug(`${this._pcId} setRemoteDescription`);
169
167
 
170
168
  if (!sessionDescription) {
171
169
  return Promise.reject(new Error('No session description provided'));
@@ -183,10 +181,10 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
183
181
  return new Promise((resolve, reject) => {
184
182
  WebRTCModule.peerConnectionSetRemoteDescription(
185
183
  desc,
186
- this._peerConnectionId,
184
+ this._pcId,
187
185
  (successful, data) => {
188
186
  if (successful) {
189
- log.debug(`${this._peerConnectionId} setRemoteDescription OK`);
187
+ log.debug(`${this._pcId} setRemoteDescription OK`);
190
188
 
191
189
  this.remoteDescription = new RTCSessionDescription(data.sdpInfo);
192
190
 
@@ -215,7 +213,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
215
213
  }
216
214
 
217
215
  async addIceCandidate(candidate): Promise<void> {
218
- log.debug(`${this._peerConnectionId} addIceCandidate`);
216
+ log.debug(`${this._pcId} addIceCandidate`);
219
217
 
220
218
  if (!candidate || !candidate.candidate) {
221
219
  // XXX end-of cantidates is not implemented: https://bugs.chromium.org/p/webrtc/issues/detail?id=9218
@@ -223,7 +221,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
223
221
  }
224
222
 
225
223
  const newSdp = await WebRTCModule.peerConnectionAddICECandidate(
226
- this._peerConnectionId,
224
+ this._pcId,
227
225
  candidate.toJSON ? candidate.toJSON() : candidate
228
226
  );
229
227
 
@@ -232,14 +230,14 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
232
230
 
233
231
  /**
234
232
  * @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
233
+ * and indicates that it is contained in the specified {@link MediaStream}s.
234
+ * This method has to be synchronous as the W3C API expects a track to be returned
237
235
  * @param {MediaStreamTrack} track The track to be added
238
- * @param {MediaStream[]} streams An array of streams the track needs to be added to
236
+ * @param {...MediaStream} streams One or more {@link MediaStream}s the track needs to be added to
239
237
  * https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtrack
240
238
  */
241
- addTrack(track: MediaStreamTrack, streams: MediaStream[] = []): RTCRtpSender {
242
- log.debug(`${this._peerConnectionId} addTrack`);
239
+ addTrack(track: MediaStreamTrack, ...streams: MediaStream[]): RTCRtpSender {
240
+ log.debug(`${this._pcId} addTrack`);
243
241
 
244
242
  if (this.connectionState === 'closed') {
245
243
  throw new Error('Peer Connection is closed');
@@ -250,7 +248,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
250
248
  }
251
249
 
252
250
  const streamIds = streams.map(s => s.id);
253
- const result = WebRTCModule.peerConnectionAddTrack(this._peerConnectionId, track.id, { streamIds });
251
+ const result = WebRTCModule.peerConnectionAddTrack(this._pcId, track.id, { streamIds });
254
252
 
255
253
  if (result === null) {
256
254
  throw new Error('Could not add sender');
@@ -295,7 +293,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
295
293
  }
296
294
 
297
295
  addTransceiver(source: 'audio' | 'video' | MediaStreamTrack, init): RTCRtpTransceiver {
298
- log.debug(`${this._peerConnectionId} addTransceiver`);
296
+ log.debug(`${this._pcId} addTransceiver`);
299
297
 
300
298
  let src = {};
301
299
 
@@ -312,7 +310,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
312
310
  init.streamIds = init.streams.map(stream => stream.id);
313
311
  }
314
312
 
315
- const result = WebRTCModule.peerConnectionAddTransceiver(this._peerConnectionId, { ...src, init: { ...init } });
313
+ const result = WebRTCModule.peerConnectionAddTransceiver(this._pcId, { ...src, init: { ...init } });
316
314
 
317
315
  if (result === null) {
318
316
  throw new Error('Transceiver could not be added');
@@ -345,9 +343,9 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
345
343
  }
346
344
 
347
345
  removeTrack(sender: RTCRtpSender) {
348
- log.debug(`${this._peerConnectionId} removeTrack`);
346
+ log.debug(`${this._pcId} removeTrack`);
349
347
 
350
- if (this._peerConnectionId !== sender._peerConnectionId) {
348
+ if (this._pcId !== sender._peerConnectionId) {
351
349
  throw new Error('Sender does not belong to this peer connection');
352
350
  }
353
351
 
@@ -367,13 +365,22 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
367
365
  return;
368
366
  }
369
367
 
370
- WebRTCModule.peerConnectionRemoveTrack(this._peerConnectionId, sender.id);
368
+ // Blocking!
369
+ WebRTCModule.peerConnectionRemoveTrack(this._pcId, sender.id);
370
+
371
+ existingSender._track = null;
372
+
373
+ const [ existingTransceiver ] = this
374
+ .getTransceivers()
375
+ .filter(t => t.sender.id === existingSender.id);
376
+
377
+ existingTransceiver._direction = existingTransceiver.direction === 'sendrecv' ? 'recvonly' : 'inactive';
371
378
  }
372
379
 
373
380
  getStats() {
374
- log.debug(`${this._peerConnectionId} getStats`);
381
+ log.debug(`${this._pcId} getStats`);
375
382
 
376
- return WebRTCModule.peerConnectionGetStats(this._peerConnectionId).then(data =>
383
+ return WebRTCModule.peerConnectionGetStats(this._pcId).then(data =>
377
384
  /* On both Android and iOS it is faster to construct a single
378
385
  JSON string representing the Map of StatsReports and have it
379
386
  pass through the React Native bridge rather than the Map of
@@ -402,23 +409,23 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
402
409
  }
403
410
 
404
411
  close(): void {
405
- log.debug(`${this._peerConnectionId} close`);
412
+ log.debug(`${this._pcId} close`);
406
413
 
407
- // According to the W3C spec: https://w3c.github.io/webrtc-pc/#rtcpeerconnection-interface
408
- // transceivers have to be stopped
414
+ WebRTCModule.peerConnectionClose(this._pcId);
415
+
416
+ // Mark transceivers as stopped.
409
417
  this._transceivers.forEach(({ transceiver })=> {
410
- transceiver.stop();
418
+ transceiver._setStopped();
411
419
  });
412
- WebRTCModule.peerConnectionClose(this._peerConnectionId);
413
420
  }
414
421
 
415
422
  restartIce(): void {
416
- WebRTCModule.peerConnectionRestartIce(this._peerConnectionId);
423
+ WebRTCModule.peerConnectionRestartIce(this._pcId);
417
424
  }
418
425
 
419
426
  _registerEvents(): void {
420
427
  addListener(this, 'peerConnectionOnRenegotiationNeeded', (ev: any) => {
421
- if (ev.id !== this._peerConnectionId) {
428
+ if (ev.pcId !== this._pcId) {
422
429
  return;
423
430
  }
424
431
 
@@ -427,7 +434,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
427
434
  });
428
435
 
429
436
  addListener(this, 'peerConnectionIceConnectionChanged', (ev: any) => {
430
- if (ev.id !== this._peerConnectionId) {
437
+ if (ev.pcId !== this._pcId) {
431
438
  return;
432
439
  }
433
440
 
@@ -443,7 +450,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
443
450
  });
444
451
 
445
452
  addListener(this, 'peerConnectionStateChanged', (ev: any) => {
446
- if (ev.id !== this._peerConnectionId) {
453
+ if (ev.pcId !== this._pcId) {
447
454
  return;
448
455
  }
449
456
 
@@ -459,7 +466,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
459
466
  });
460
467
 
461
468
  addListener(this, 'peerConnectionSignalingStateChanged', (ev: any) => {
462
- if (ev.id !== this._peerConnectionId) {
469
+ if (ev.pcId !== this._pcId) {
463
470
  return;
464
471
  }
465
472
 
@@ -470,13 +477,13 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
470
477
 
471
478
  // Consider moving away from this event: https://github.com/WebKit/WebKit/pull/3953
472
479
  addListener(this, 'peerConnectionOnTrack', (ev: any) => {
473
- if (ev.id !== this._peerConnectionId) {
480
+ if (ev.pcId !== this._pcId) {
474
481
  return;
475
482
  }
476
483
 
477
- log.debug(`${this._peerConnectionId} ontrack`);
484
+ log.debug(`${this._pcId} ontrack`);
478
485
 
479
- const track = new MediaStreamTrack(ev.receiver.track);
486
+ let track;
480
487
  let transceiver;
481
488
 
482
489
  // Make sure transceivers are stored in timestamp order. Also, we have to make
@@ -484,19 +491,23 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
484
491
  const [ { transceiver: oldTransceiver } = { transceiver: null } ]
485
492
  = this._transceivers.filter(({ transceiver }) => transceiver.id === ev.transceiver.id);
486
493
 
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 });
494
+ // We need to fire this event for an existing track sometimes, like
495
+ // when the transceiver direction (on the sending side) switches from
496
+ // sendrecv to recvonly and then back.
491
497
 
492
- transceiver = new RTCRtpTransceiver({ ...ev.transceiver, receiver, sender });
493
- this._insertTransceiverSorted(ev.transceiverOrder, transceiver);
494
- } else {
498
+ if (oldTransceiver) {
495
499
  transceiver = oldTransceiver;
496
- transceiver._receiver._track = track;
500
+ track = transceiver._receiver._track;
497
501
  transceiver._mid = ev.transceiver.mid;
498
502
  transceiver._currentDirection = ev.transceiver.currentDirection;
499
503
  transceiver._direction = ev.transceiver.direction;
504
+ } else {
505
+ track = new MediaStreamTrack(ev.receiver.track);
506
+ const sender = new RTCRtpSender({ ...ev.transceiver.sender });
507
+ const receiver = new RTCRtpReceiver({ ...ev.receiver, track });
508
+
509
+ transceiver = new RTCRtpTransceiver({ ...ev.transceiver, receiver, sender });
510
+ this._insertTransceiverSorted(ev.transceiverOrder, transceiver);
500
511
  }
501
512
 
502
513
  // Get the stream object from the event. Create if necessary.
@@ -515,7 +526,9 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
515
526
 
516
527
  const stream = this._remoteStreams.get(streamInfo.streamId);
517
528
 
518
- stream?._tracks.push(track);
529
+ if (!stream?._tracks.includes(track)) {
530
+ stream?._tracks.push(track);
531
+ }
519
532
 
520
533
  return stream;
521
534
  });
@@ -529,53 +542,39 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
529
542
 
530
543
  // @ts-ignore
531
544
  this.dispatchEvent(new RTCTrackEvent('track', eventData));
545
+
546
+ // Dispatch an unmute event for the track.
547
+ track._setMutedInternal(false);
532
548
  });
533
549
 
534
550
  addListener(this, 'peerConnectionOnRemoveTrack', (ev: any) => {
535
- if (ev.peerConnectionId !== this._peerConnectionId) {
551
+ if (ev.pcId !== this._pcId) {
536
552
  return;
537
553
  }
538
554
 
539
- log.debug(`${this._peerConnectionId} onremovetrack`);
555
+ log.debug(`${this._pcId} onremovetrack`);
540
556
 
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.
557
+ // As per the spec:
558
+ // - Remove the track from any media streams that were previously passed to the `track` event.
559
+ // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-removetrack,
560
+ // - Mark the track as muted:
561
+ // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
544
562
  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
- }
563
+ const [ track ] = stream._tracks.filter(t => t.id === ev.trackId);
557
564
 
558
- const [ existingSender ] = this
559
- .getSenders()
560
- .filter(s => s.id === ev.senderId);
565
+ if (track) {
566
+ const trackIdx = stream._tracks.indexOf(track);
561
567
 
562
- const oldTrack = existingSender._track;
568
+ stream._tracks.splice(trackIdx, 1);
563
569
 
564
- if (oldTrack) {
565
- oldTrack._muted = true;
570
+ // Dispatch a mute event for the track.
571
+ track._setMutedInternal(true);
572
+ }
566
573
  }
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
574
  });
576
575
 
577
576
  addListener(this, 'peerConnectionGotICECandidate', (ev: any) => {
578
- if (ev.id !== this._peerConnectionId) {
577
+ if (ev.pcId !== this._pcId) {
579
578
  return;
580
579
  }
581
580
 
@@ -587,7 +586,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
587
586
  });
588
587
 
589
588
  addListener(this, 'peerConnectionIceGatheringChanged', (ev: any) => {
590
- if (ev.id !== this._peerConnectionId) {
589
+ if (ev.pcId !== this._pcId) {
591
590
  return;
592
591
  }
593
592
 
@@ -604,7 +603,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
604
603
  });
605
604
 
606
605
  addListener(this, 'peerConnectionDidOpenDataChannel', (ev: any) => {
607
- if (ev.id !== this._peerConnectionId) {
606
+ if (ev.pcId !== this._pcId) {
608
607
  return;
609
608
  }
610
609
 
@@ -614,23 +613,8 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
614
613
  this.dispatchEvent(new RTCDataChannelEvent('datachannel', { channel }));
615
614
  });
616
615
 
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
616
  addListener(this, 'mediaStreamTrackMuteChanged', (ev: any) => {
633
- if (ev.peerConnectionId !== this._peerConnectionId) {
617
+ if (ev.pcId !== this._pcId) {
634
618
  return;
635
619
  }
636
620
 
@@ -639,11 +623,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
639
623
  ] = this.getReceivers().map(r => r.track).filter(t => t?.id === ev.trackId);
640
624
 
641
625
  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 }));
626
+ track._setMutedInternal(ev.muted);
647
627
  }
648
628
  });
649
629
  }
@@ -668,7 +648,7 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
668
648
  }
669
649
  }
670
650
 
671
- const channelInfo = WebRTCModule.createDataChannel(this._peerConnectionId, label, dataChannelDict);
651
+ const channelInfo = WebRTCModule.createDataChannel(this._pcId, label, dataChannelDict);
672
652
 
673
653
  if (channelInfo === null) {
674
654
  throw new TypeError('Failed to create new DataChannel');
@@ -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,18 +1,12 @@
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;
@@ -33,7 +27,6 @@ export default class RTCRtpTransceiver extends defineCustomEventTarget(...TRANSC
33
27
  sender: RTCRtpSender,
34
28
  receiver: RTCRtpReceiver,
35
29
  }) {
36
- super();
37
30
  this._peerConnectionId = args.peerConnectionId;
38
31
  this._id = args.id;
39
32
  this._mid = args.mid ? args.mid : null;
@@ -42,7 +35,6 @@ export default class RTCRtpTransceiver extends defineCustomEventTarget(...TRANSC
42
35
  this._stopped = args.isStopped;
43
36
  this._sender = args.sender;
44
37
  this._receiver = args.receiver;
45
- this._registerEvents();
46
38
  }
47
39
 
48
40
  get id() {
@@ -74,7 +66,13 @@ export default class RTCRtpTransceiver extends defineCustomEventTarget(...TRANSC
74
66
  return;
75
67
  }
76
68
 
77
- WebRTCModule.transceiverSetDirection(this._peerConnectionId, this.id, val);
69
+ const oldDirection = this._direction;
70
+
71
+ WebRTCModule.transceiverSetDirection(this._peerConnectionId, this.id, val)
72
+ .catch(() => {
73
+ this._direction = oldDirection;
74
+ });
75
+
78
76
  this._direction = val;
79
77
  }
80
78
 
@@ -95,35 +93,14 @@ export default class RTCRtpTransceiver extends defineCustomEventTarget(...TRANSC
95
93
  return;
96
94
  }
97
95
 
98
- WebRTCModule.transceiverStop(this._peerConnectionId, this.id);
96
+ WebRTCModule.transceiverStop(this._peerConnectionId, this.id)
97
+ .then(() => this._setStopped());
99
98
  }
100
99
 
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
- });
100
+ _setStopped() {
101
+ this._stopped = true;
102
+ this._direction = 'stopped';
103
+ this._currentDirection = 'stopped';
104
+ this._mid = null;
128
105
  }
129
106
  }
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ import Logger from './Logger';
2
2
  import mediaDevices from './MediaDevices';
3
3
  import MediaStream from './MediaStream';
4
4
  import MediaStreamTrack from './MediaStreamTrack';
5
+ import MediaStreamTrackEvent from './MediaStreamTrackEvent';
5
6
  import permissions from './Permissions';
6
7
  import RTCErrorEvent from './RTCErrorEvent';
7
8
  import RTCIceCandidate from './RTCIceCandidate';
@@ -56,6 +57,7 @@ function registerGlobals(): void {
56
57
  global.RTCSessionDescription = RTCSessionDescription;
57
58
  global.MediaStream = MediaStream;
58
59
  global.MediaStreamTrack = MediaStreamTrack;
60
+ global.MediaStreamTrackEvent = MediaStreamTrackEvent;
59
61
  global.RTCRtpTransceiver = RTCRtpTransceiver;
60
62
  global.RTCRtpReceiver = RTCRtpReceiver;
61
63
  global.RTCRtpSender = RTCRtpSender;