react-native-webrtc 106.0.0-beta.4 → 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 (39) 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/SerializeUtils.java +1 -1
  4. package/android/src/main/java/com/oney/WebRTCModule/VideoTrackAdapter.java +1 -1
  5. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +39 -62
  6. package/ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.m +54 -48
  7. package/ios/RCTWebRTC/WebRTCModule+Transceivers.m +23 -22
  8. package/ios/RCTWebRTC/WebRTCModule+VideoTrackAdapter.m +1 -1
  9. package/ios/RCTWebRTC/WebRTCModule.h +0 -8
  10. package/ios/RCTWebRTC/WebRTCModule.m +1 -20
  11. package/lib/commonjs/MediaStreamTrack.js +16 -0
  12. package/lib/commonjs/MediaStreamTrack.js.map +1 -1
  13. package/lib/commonjs/RTCPeerConnection.js +92 -109
  14. package/lib/commonjs/RTCPeerConnection.js.map +1 -1
  15. package/lib/commonjs/RTCRtpSender.js +6 -5
  16. package/lib/commonjs/RTCRtpSender.js.map +1 -1
  17. package/lib/commonjs/RTCRtpTransceiver.js +11 -42
  18. package/lib/commonjs/RTCRtpTransceiver.js.map +1 -1
  19. package/lib/commonjs/index.js +3 -0
  20. package/lib/commonjs/index.js.map +1 -1
  21. package/lib/module/MediaStreamTrack.js +17 -1
  22. package/lib/module/MediaStreamTrack.js.map +1 -1
  23. package/lib/module/RTCPeerConnection.js +92 -107
  24. package/lib/module/RTCPeerConnection.js.map +1 -1
  25. package/lib/module/RTCRtpSender.js +6 -5
  26. package/lib/module/RTCRtpSender.js.map +1 -1
  27. package/lib/module/RTCRtpTransceiver.js +11 -37
  28. package/lib/module/RTCRtpTransceiver.js.map +1 -1
  29. package/lib/module/index.js +2 -0
  30. package/lib/module/index.js.map +1 -1
  31. package/lib/typescript/MediaStreamTrack.d.ts +9 -2
  32. package/lib/typescript/RTCPeerConnection.d.ts +5 -5
  33. package/lib/typescript/RTCRtpTransceiver.d.ts +2 -5
  34. package/package.json +2 -2
  35. package/src/MediaStreamTrack.ts +16 -1
  36. package/src/RTCPeerConnection.ts +94 -114
  37. package/src/RTCRtpSender.ts +6 -7
  38. package/src/RTCRtpTransceiver.ts +15 -38
  39. package/src/index.ts +2 -0
package/README.md CHANGED
@@ -34,7 +34,7 @@ Which will allow you to use [(almost)](https://github.com/react-native-webrtc/r
34
34
  * Supported architectures
35
35
  * Android: armeabi-v7a, arm64-v8a, x86, x86_64
36
36
  * iOS: arm64, x86_64
37
- * macOS: ~~~x86_64~~~ (temporarily disabled)
37
+ * macOS: (temporarily disabled)
38
38
 
39
39
  ## Getting Started
40
40
 
@@ -65,4 +65,4 @@ Everyone is welcome and every little helps.
65
65
  ## Related Projects
66
66
 
67
67
  Looking for extra functionality coverage?
68
- The [react-native-webrtc](https://github.com/react-native-webrtc) organization provides a number of packages which are more than useful when developing Real Time Communication applications.
68
+ The [react-native-webrtc](https://github.com/react-native-webrtc) organization provides a number of packages which are more than useful when developing Real Time Communication applications.
@@ -243,7 +243,7 @@ class PeerConnectionObserver implements PeerConnection.Observer {
243
243
  public void onIceCandidate(final IceCandidate candidate) {
244
244
  Log.d(TAG, "onIceCandidate");
245
245
  WritableMap params = Arguments.createMap();
246
- params.putInt("id", id);
246
+ params.putInt("pcId", id);
247
247
  WritableMap candidateParams = Arguments.createMap();
248
248
  candidateParams.putInt("sdpMLineIndex", candidate.sdpMLineIndex);
249
249
  candidateParams.putString("sdpMid", candidate.sdpMid);
@@ -266,7 +266,7 @@ class PeerConnectionObserver implements PeerConnection.Observer {
266
266
  @Override
267
267
  public void onIceConnectionChange(PeerConnection.IceConnectionState iceConnectionState) {
268
268
  WritableMap params = Arguments.createMap();
269
- params.putInt("id", id);
269
+ params.putInt("pcId", id);
270
270
  params.putString("iceConnectionState", iceConnectionStateString(iceConnectionState));
271
271
  webRTCModule.sendEvent("peerConnectionIceConnectionChanged", params);
272
272
  }
@@ -274,7 +274,7 @@ class PeerConnectionObserver implements PeerConnection.Observer {
274
274
  @Override
275
275
  public void onConnectionChange(PeerConnection.PeerConnectionState peerConnectionState) {
276
276
  WritableMap params = Arguments.createMap();
277
- params.putInt("id", id);
277
+ params.putInt("pcId", id);
278
278
  params.putString("connectionState", peerConnectionStateString(peerConnectionState));
279
279
 
280
280
  webRTCModule.sendEvent("peerConnectionStateChanged", params);
@@ -288,7 +288,7 @@ class PeerConnectionObserver implements PeerConnection.Observer {
288
288
  public void onIceGatheringChange(PeerConnection.IceGatheringState iceGatheringState) {
289
289
  Log.d(TAG, "onIceGatheringChange" + iceGatheringState.name());
290
290
  WritableMap params = Arguments.createMap();
291
- params.putInt("id", id);
291
+ params.putInt("pcId", id);
292
292
  params.putString("iceGatheringState", iceGatheringStateString(iceGatheringState));
293
293
  if (iceGatheringState == PeerConnection.IceGatheringState.COMPLETE) {
294
294
  SessionDescription newSdp = peerConnection.getLocalDescription();
@@ -323,7 +323,7 @@ class PeerConnectionObserver implements PeerConnection.Observer {
323
323
  info.putString("readyState", dcw.dataChannelStateString(dataChannel.state()));
324
324
 
325
325
  WritableMap params = Arguments.createMap();
326
- params.putInt("id", id);
326
+ params.putInt("pcId", id);
327
327
  params.putMap("dataChannel", info);
328
328
 
329
329
  webRTCModule.sendEvent("peerConnectionDidOpenDataChannel", params);
@@ -332,14 +332,14 @@ class PeerConnectionObserver implements PeerConnection.Observer {
332
332
  @Override
333
333
  public void onRenegotiationNeeded() {
334
334
  WritableMap params = Arguments.createMap();
335
- params.putInt("id", id);
335
+ params.putInt("pcId", id);
336
336
  webRTCModule.sendEvent("peerConnectionOnRenegotiationNeeded", params);
337
337
  }
338
338
 
339
339
  @Override
340
340
  public void onSignalingChange(PeerConnection.SignalingState signalingState) {
341
341
  WritableMap params = Arguments.createMap();
342
- params.putInt("id", id);
342
+ params.putInt("pcId", id);
343
343
  params.putString("signalingState", signalingStateString(signalingState));
344
344
  webRTCModule.sendEvent("peerConnectionSignalingStateChanged", params);
345
345
  }
@@ -367,17 +367,17 @@ class PeerConnectionObserver implements PeerConnection.Observer {
367
367
 
368
368
  final MediaStreamTrack track = receiver.track();
369
369
 
370
- if (remoteTracks.containsKey(track.id())) {
371
- // Unlike in the WebRTC spec, the libwebrtc native implementation
372
- // fires onTrack on every sRD which has an active receiving transceiver.
373
- // So, if we are already keeping track of this transceiver, ignore the event.
374
- return;
375
- }
370
+ // We need to fire this event for an existing track sometimes, like
371
+ // when the transceiver direction (on the sending side) switches from
372
+ // sendrecv to recvonly and then back.
373
+ final boolean existingTrack = remoteTracks.containsKey(track.id());
376
374
 
377
- if (track.kind().equals(MediaStreamTrack.VIDEO_TRACK_KIND)){
378
- videoTrackAdapters.addAdapter((VideoTrack) track);
375
+ if (!existingTrack) {
376
+ if (track.kind().equals(MediaStreamTrack.VIDEO_TRACK_KIND)){
377
+ videoTrackAdapters.addAdapter((VideoTrack) track);
378
+ }
379
+ remoteTracks.put(track.id(), track);
379
380
  }
380
- remoteTracks.put(track.id(), track);
381
381
 
382
382
  WritableMap params = Arguments.createMap();
383
383
  WritableArray streams = Arguments.createArray();
@@ -402,8 +402,7 @@ class PeerConnectionObserver implements PeerConnection.Observer {
402
402
  params.putMap("receiver", SerializeUtils.serializeReceiver(id, receiver));
403
403
  params.putInt("transceiverOrder", getNextTransceiverId());
404
404
  params.putMap("transceiver", SerializeUtils.serializeTransceiver(id, transceiver));
405
-
406
- params.putInt("id", this.id);
405
+ params.putInt("pcId", this.id);
407
406
 
408
407
  webRTCModule.sendEvent("peerConnectionOnTrack", params);
409
408
  });
@@ -424,18 +423,13 @@ class PeerConnectionObserver implements PeerConnection.Observer {
424
423
  */
425
424
  @Override
426
425
  public void onRemoveTrack(RtpReceiver receiver){
427
- // According to the W3C spec, we need to send out
428
- // the track Id so that we can remove it from the MediaStream objects stored
429
- // at the JS layer, which are the same stream objects passed down to
430
- // the `track` event.
431
426
  ThreadUtils.runOnExecutor(() -> {
432
427
  MediaStreamTrack track = receiver.track();
433
428
  WritableMap params = Arguments.createMap();
434
- params.putInt("id", this.id);
429
+ params.putInt("pcId", this.id);
435
430
  params.putString("trackId", track.id());
436
431
 
437
432
  webRTCModule.sendEvent("peerConnectionOnRemoveTrack", params);
438
-
439
433
  });
440
434
  };
441
435
 
@@ -176,7 +176,7 @@ public class SerializeUtils {
176
176
  // Serializing sdpFmptLine.
177
177
  if (!codec.parameters.isEmpty()) {
178
178
  final String sdpFmptLineParams = codec.parameters.keySet().stream()
179
- .map(key -> key + "=" + map.get(key))
179
+ .map(key -> key + "=" + codec.parameters.get(key))
180
180
  .collect(Collectors.joining(";"));
181
181
  codecMap.putString("sdpFmtpLine", sdpFmptLineParams);
182
182
  }
@@ -115,7 +115,7 @@ public class VideoTrackAdapter {
115
115
 
116
116
  private void emitMuteEvent(boolean muted) {
117
117
  WritableMap params = Arguments.createMap();
118
- params.putInt("peerConnectionId", peerConnectionId);
118
+ params.putInt("pcId", peerConnectionId);
119
119
  params.putString("trackId", trackId);
120
120
  params.putBoolean("muted", muted);
121
121
 
@@ -161,16 +161,6 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
161
161
  .emit(eventName, params);
162
162
  }
163
163
 
164
- void sendError(String eventName, String funcName, @Nullable String message, @Nullable ReadableMap info) {
165
- WritableMap errorInfo = Arguments.createMap();
166
- errorInfo.putString("func", funcName);
167
- if (info != null)
168
- errorInfo.putMap("info", info);
169
- if (message != null)
170
- errorInfo.putString("message", message);
171
- sendEvent(eventName, errorInfo);
172
- }
173
-
174
164
  private PeerConnection.IceServer createIceServer(String url) {
175
165
  return PeerConnection.IceServer.builder(url).createIceServer();
176
166
  }
@@ -591,6 +581,29 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
591
581
  }
592
582
  }
593
583
 
584
+ @ReactMethod(isBlockingSynchronousMethod = true)
585
+ public boolean peerConnectionRemoveTrack(int id, String senderId) {
586
+ try {
587
+ return (boolean) ThreadUtils.submitToExecutor((Callable<Object>) () -> {
588
+ PeerConnectionObserver pco = mPeerConnectionObservers.get(id);
589
+ if (pco == null) {
590
+ Log.d(TAG, "peerConnectionRemoveTrack() peerConnection is null");
591
+ return false;
592
+ }
593
+ RtpSender sender = pco.getSender(senderId);
594
+ if (sender == null) {
595
+ Log.w(TAG, "peerConnectionRemoveTrack() sender is null");
596
+ return false;
597
+ }
598
+
599
+ return pco.getPeerConnection().removeTrack(sender);
600
+ }).get();
601
+ } catch (InterruptedException | ExecutionException e) {
602
+ Log.d(TAG, "peerConnectionRemoveTrack() " + e.getMessage());
603
+ return false;
604
+ }
605
+ }
606
+
594
607
  @ReactMethod
595
608
  public void senderSetParameters(int id, String senderId, ReadableMap options, Promise promise) {
596
609
  ThreadUtils.runOnExecutor(() ->{
@@ -621,62 +634,27 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
621
634
  }
622
635
 
623
636
  @ReactMethod
624
- public void peerConnectionRemoveTrack(int id, String senderId) {
625
- ThreadUtils.runOnExecutor(() -> {
626
- WritableMap identifier = Arguments.createMap();
627
- identifier.putInt("peerConnectionId", id);
628
- identifier.putString("senderId", senderId);
629
- try {
630
- PeerConnectionObserver pco = mPeerConnectionObservers.get(id);
631
- if (pco == null) {
632
- Log.d(TAG, "peerConnectionRemoveTrack() peerConnection is null");
633
- sendError("peerConnectionOnError", "removeTrack", "Peer Connection is not initialized", null);
634
- return;
635
- }
636
- RtpSender sender = pco.getSender(senderId);
637
- if (sender == null) {
638
- Log.w(TAG, "peerConnectionRemoveTrack() sender is null");
639
- sendError("peerConnectionOnError", "removeTrack", "Sender is null", null);
640
- return;
641
- }
642
-
643
- boolean successful = pco.getPeerConnection().removeTrack(sender);
644
- if (successful) {
645
- sendEvent("peerConnectionOnRemoveTrackSuccessful", identifier);
646
- return;
647
- }
648
- sendError("peerConnectionOnError", "removeTrack", "Internal Error", identifier);
649
- } catch (Exception e) {
650
- Log.d(TAG, "peerConnectionRemoveTrack() " + e.getMessage());
651
- sendError("peerConnectionOnError", "removeTrack", e.getMessage(), identifier);
652
- }
653
- });
654
- }
655
-
656
- @ReactMethod
657
- public void transceiverStop(int id,
658
- String senderId) {
637
+ public void transceiverStop(int id, String senderId, Promise promise) {
659
638
  ThreadUtils.runOnExecutor(() -> {
660
- WritableMap identifier = Arguments.createMap();
661
- identifier.putInt("peerConnectionId", id);
662
- identifier.putString("transceiverId", senderId);
663
639
  try {
664
640
  PeerConnectionObserver pco = mPeerConnectionObservers.get(id);
665
641
  if (pco == null) {
666
642
  Log.d(TAG, "transceiverStop() peerConnectionObserver is null");
667
- // Cannot really report an error specific to RTCRtpTransceiver as the peer connection
668
- // hasn't really been initialized, this call from the web API should technically
669
- // not happen unless both peer connection and transceivers are initialized properly
670
- // That is why constructors or create methods should always be synchronous.
643
+ promise.reject(new Exception("Peer Connection is not initialized"));
671
644
  return;
672
645
  }
673
646
  RtpTransceiver transceiver = pco.getTransceiver(senderId);
647
+ if (transceiver == null) {
648
+ Log.w(TAG, "transceiverStop() transceiver is null");
649
+ promise.reject(new Exception("Could not get transceiver"));
650
+ return;
651
+ }
674
652
 
675
653
  transceiver.stopStandard();
676
- sendEvent("transceiverStopSuccessful", identifier);
654
+ promise.resolve(true);
677
655
  } catch (Exception e) {
678
656
  Log.d(TAG, "transceiverStop(): " + e.getMessage());
679
- sendError("transceiverOnError", "stopTransceiver", e.getMessage(), identifier);
657
+ promise.reject(e);
680
658
  }
681
659
  });
682
660
  }
@@ -715,7 +693,8 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
715
693
  @ReactMethod
716
694
  public void transceiverSetDirection(int id,
717
695
  String senderId,
718
- String direction) {
696
+ String direction,
697
+ Promise promise) {
719
698
 
720
699
  ThreadUtils.runOnExecutor(() -> {
721
700
  WritableMap identifier = Arguments.createMap();
@@ -726,24 +705,22 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
726
705
  PeerConnectionObserver pco = mPeerConnectionObservers.get(id);
727
706
  if (pco == null) {
728
707
  Log.d(TAG, "transceiverSetDirection() peerConnectionObserver is null");
729
- // Cannot really report an error specific to RTCRtpSender as the peer connection
730
- // hasn't really been initialized, this call from the web API should technically
731
- // not happen unless both peer connection and sender are initialized
708
+ promise.reject(new Exception("Peer Connection is not initialized"));
732
709
  return;
733
710
  }
734
711
  RtpTransceiver transceiver = pco.getTransceiver(senderId);
735
712
  if (transceiver == null){
736
713
  Log.d(TAG, "transceiverSetDirection() transceiver is null");
714
+ promise.reject(new Exception("Could not get sender"));
737
715
  return;
738
716
  }
739
717
 
740
- RtpTransceiver.RtpTransceiverDirection oldDirection = transceiver.getDirection();
741
- params.putString("oldDirection", SerializeUtils.serializeDirection(oldDirection));
742
718
  transceiver.setDirection(SerializeUtils.parseDirection(direction));
719
+
720
+ promise.resolve(true);
743
721
  } catch (Exception e) {
744
722
  Log.d(TAG, "transceiverSetDirection(): " + e.getMessage());
745
- params.putMap("identifiers", identifier);
746
- sendError("transceiverOnError", "setDirection", e.getMessage(), params);
723
+ promise.reject(e);
747
724
  }
748
725
  });
749
726
  }
@@ -482,46 +482,36 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(peerConnectionAddTransceiver:(nonnull NSN
482
482
  return params;
483
483
  }
484
484
 
485
- RCT_EXPORT_METHOD(peerConnectionRemoveTrack:(nonnull NSNumber *)objectID
486
- senderId:(nonnull NSString *)senderId)
485
+ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(peerConnectionRemoveTrack:(nonnull NSNumber *)objectID
486
+ senderId:(nonnull NSString *)senderId)
487
487
  {
488
+ __block BOOL ret = NO;
489
+
490
+ dispatch_sync(self.workerQueue, ^{
488
491
  RTCPeerConnection *peerConnection = self.peerConnections[objectID];
489
492
  if (!peerConnection) {
490
493
  RCTLogWarn(@"PeerConnection %@ not found in peerConnectionRemoveTrack()", objectID);
491
- [self sendErrorWithEventName: kEventPeerConnectionOnError
492
- funcName: @"removeTrack"
493
- message: @"Peer Connection is not initialized"
494
- info: nil];
495
494
  return;
496
495
  }
497
-
498
- RTCRtpTransceiver *transceiver = nil;
499
-
500
- for (RTCRtpTransceiver *t in peerConnection.transceivers) {
501
- if ([t.sender.senderId isEqual: senderId]) {
502
- transceiver = t;
496
+
497
+ RTCRtpSender *sender = nil;
498
+
499
+ for (RTCRtpSender *s in peerConnection.senders) {
500
+ if ([s.senderId isEqual: senderId]) {
501
+ sender = s;
503
502
  break;
504
503
  }
505
504
  }
506
-
507
- if (!transceiver) {
508
- RCTLogWarn(@"Transceiver not found in peerConnectionRemoveTrack()");
505
+
506
+ if (!sender) {
507
+ RCTLogWarn(@"Sender not found in peerConnectionRemoveTrack()");
509
508
  return;
510
509
  }
511
-
512
- NSMutableDictionary *identifier = [NSMutableDictionary new];
513
- identifier[@"peerConnectionId"] = objectID;
514
- identifier[@"senderId"] = senderId;
515
-
516
- if ([peerConnection removeTrack: transceiver.sender]) {
517
- [self sendEventWithName: kEventPeerConnectionOnRemoveTrackSuccessful
518
- body: identifier];
519
- } else {
520
- [self sendErrorWithEventName: kEventPeerConnectionOnError
521
- funcName: @"removeTrack"
522
- message: @"Cannot remove track"
523
- info: identifier];
524
- }
510
+
511
+ ret = [peerConnection removeTrack: sender];
512
+ });
513
+
514
+ return @(ret);
525
515
  }
526
516
 
527
517
  // TODO: move these below to some SerializeUrils file
@@ -703,25 +693,25 @@ RCT_EXPORT_METHOD(peerConnectionRemoveTrack:(nonnull NSNumber *)objectID
703
693
  - (void)peerConnection:(RTCPeerConnection *)peerConnection didChangeSignalingState:(RTCSignalingState)newState {
704
694
  [self sendEventWithName:kEventPeerConnectionSignalingStateChanged
705
695
  body:@{
706
- @"id": peerConnection.reactTag,
696
+ @"pcId": peerConnection.reactTag,
707
697
  @"signalingState": [self stringForSignalingState:newState]
708
698
  }];
709
699
  }
710
700
 
711
701
  - (void)peerConnectionShouldNegotiate:(RTCPeerConnection *)peerConnection {
712
702
  [self sendEventWithName:kEventPeerConnectionOnRenegotiationNeeded
713
- body:@{ @"id": peerConnection.reactTag }];
703
+ body:@{ @"pcId": peerConnection.reactTag }];
714
704
  }
715
705
 
716
706
  - (void)peerConnection:(RTCPeerConnection *)peerConnection didChangeConnectionState:(RTCPeerConnectionState)newState {
717
707
  [self sendEventWithName:kEventPeerConnectionStateChanged
718
- body:@{@"id": peerConnection.reactTag, @"connectionState": [self stringForPeerConnectionState:newState]}];
708
+ body:@{@"pcId": peerConnection.reactTag, @"connectionState": [self stringForPeerConnectionState:newState]}];
719
709
  }
720
710
 
721
711
  - (void)peerConnection:(RTCPeerConnection *)peerConnection didChangeIceConnectionState:(RTCIceConnectionState)newState {
722
712
  [self sendEventWithName:kEventPeerConnectionIceConnectionChanged
723
713
  body:@{
724
- @"id": peerConnection.reactTag,
714
+ @"pcId": peerConnection.reactTag,
725
715
  @"iceConnectionState": [self stringForICEConnectionState:newState]
726
716
  }];
727
717
  }
@@ -736,7 +726,7 @@ RCT_EXPORT_METHOD(peerConnectionRemoveTrack:(nonnull NSNumber *)objectID
736
726
  }
737
727
  [self sendEventWithName:kEventPeerConnectionIceGatheringChanged
738
728
  body:@{
739
- @"id": peerConnection.reactTag,
729
+ @"pcId": peerConnection.reactTag,
740
730
  @"iceGatheringState": [self stringForICEGatheringState:newState],
741
731
  @"sdp": newSdp
742
732
  }];
@@ -745,7 +735,7 @@ RCT_EXPORT_METHOD(peerConnectionRemoveTrack:(nonnull NSNumber *)objectID
745
735
  - (void)peerConnection:(RTCPeerConnection *)peerConnection didGenerateIceCandidate:(RTCIceCandidate *)candidate {
746
736
  [self sendEventWithName:kEventPeerConnectionGotICECandidate
747
737
  body:@{
748
- @"id": peerConnection.reactTag,
738
+ @"pcId": peerConnection.reactTag,
749
739
  @"candidate": @{
750
740
  @"candidate": candidate.sdp,
751
741
  @"sdpMLineIndex": @(candidate.sdpMLineIndex),
@@ -778,7 +768,7 @@ RCT_EXPORT_METHOD(peerConnectionRemoveTrack:(nonnull NSNumber *)objectID
778
768
  @"readyState": [self stringForDataChannelState:dataChannel.readyState]
779
769
  };
780
770
  NSDictionary *body = @{
781
- @"id": peerConnection.reactTag,
771
+ @"pcId": peerConnection.reactTag,
782
772
  @"dataChannel": dataChannelInfo
783
773
  };
784
774
  [self sendEventWithName:kEventPeerConnectionDidOpenDataChannel body:body];
@@ -802,17 +792,20 @@ RCT_EXPORT_METHOD(peerConnectionRemoveTrack:(nonnull NSNumber *)objectID
802
792
 
803
793
  RTCMediaStreamTrack *track = rtpReceiver.track;
804
794
 
805
- if (peerConnection.remoteTracks[track.trackId]) {
806
- return;
807
- }
808
-
809
- if (track.kind == kRTCMediaStreamTrackKindVideo) {
810
- RTCVideoTrack *videoTrack = (RTCVideoTrack *)track;
811
- [peerConnection addVideoTrackAdapter: videoTrack];
795
+ // We need to fire this event for an existing track sometimes, like
796
+ // when the transceiver direction (on the sending side) switches from
797
+ // sendrecv to recvonly and then back.
798
+ BOOL existingTrack = peerConnection.remoteTracks[track.trackId] != nil;
799
+
800
+ if (!existingTrack) {
801
+ if (track.kind == kRTCMediaStreamTrackKindVideo) {
802
+ RTCVideoTrack *videoTrack = (RTCVideoTrack *)track;
803
+ [peerConnection addVideoTrackAdapter: videoTrack];
804
+ }
805
+
806
+ peerConnection.remoteTracks[track.trackId] = track;
812
807
  }
813
808
 
814
- peerConnection.remoteTracks[track.trackId] = track;
815
-
816
809
  NSMutableDictionary *params = [NSMutableDictionary new];
817
810
  NSMutableArray *streams = [NSMutableArray new];
818
811
 
@@ -843,10 +836,23 @@ RCT_EXPORT_METHOD(peerConnectionRemoveTrack:(nonnull NSNumber *)objectID
843
836
  params[@"receiver"] = [SerializeUtils receiverToJSONWithPeerConnectionId: peerConnection.reactTag receiver: rtpReceiver];
844
837
  params[@"transceiverOrder"] = [NSNumber numberWithInt:_transceiverNextId++];
845
838
  params[@"transceiver"] = [SerializeUtils transceiverToJSONWithPeerConnectionId: peerConnection.reactTag transceiver: transceiver];
846
- params[@"id"] = peerConnection.reactTag;
839
+ params[@"pcId"] = peerConnection.reactTag;
847
840
 
848
- [self sendEventWithName: kEventPeerConnectionOnTrack
849
- body: params];
841
+ [self sendEventWithName: kEventPeerConnectionOnTrack body: params];
842
+ });
843
+ }
844
+
845
+ - (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection
846
+ didRemoveReceiver:(RTC_OBJC_TYPE(RTCRtpReceiver) *)rtpReceiver {
847
+ dispatch_async(self.workerQueue, ^{
848
+ RTCMediaStreamTrack *track = rtpReceiver.track;
849
+
850
+ NSMutableDictionary *params = [NSMutableDictionary new];
851
+
852
+ params[@"pcId"] = peerConnection.reactTag;
853
+ params[@"trackId"] = track.trackId;
854
+
855
+ [self sendEventWithName: kEventPeerConnectionOnRemoveTrack body: params];
850
856
  });
851
857
  }
852
858
 
@@ -94,6 +94,7 @@ RCT_EXPORT_METHOD(senderSetParameters:(nonnull NSNumber *) objectID
94
94
  if (peerConnection == nil) {
95
95
  RCTLogWarn(@"PeerConnection %@ not found in senderSetParameters()", objectID);
96
96
  reject(@"E_INVALID", @"Peer Connection is not initialized", nil);
97
+ return;
97
98
  }
98
99
 
99
100
  RTCRtpTransceiver *transceiver = nil;
@@ -106,22 +107,28 @@ RCT_EXPORT_METHOD(senderSetParameters:(nonnull NSNumber *) objectID
106
107
 
107
108
  if (transceiver == nil) {
108
109
  RCTLogWarn(@"senderSetParameters() transceiver is null");
109
- reject(@"E_INVALID", @"Could not get transceive", nil);
110
+ reject(@"E_INVALID", @"Could not get transceiver", nil);
111
+ return;
110
112
  }
111
113
 
112
114
  RTCRtpSender *sender = transceiver.sender;
113
115
  RTCRtpParameters *parameters = sender.parameters;
114
116
  [sender setParameters:[self updateParametersWithOptions: options params: parameters]];
117
+
118
+ resolve([SerializeUtils parametersToJSON: sender.parameters]);
115
119
  }
116
120
 
117
121
  RCT_EXPORT_METHOD(transceiverSetDirection:(nonnull NSNumber *) objectID
118
- senderId:(NSString *)senderId
119
- direction:(NSString *)direction)
122
+ senderId:(NSString *)senderId
123
+ direction:(NSString *)direction
124
+ resolver:(RCTPromiseResolveBlock)resolve
125
+ rejecter:(RCTPromiseRejectBlock)reject)
120
126
  {
121
127
  RTCPeerConnection *peerConnection = self.peerConnections[objectID];
122
128
 
123
129
  if (peerConnection == nil) {
124
130
  RCTLogWarn(@"transceiverSetDirection() PeerConnection %@ not found in transceiverSetDirection()", objectID);
131
+ reject(@"E_INVALID", @"Peer Connection is not initialized", nil);
125
132
  return;
126
133
  }
127
134
 
@@ -135,34 +142,30 @@ RCT_EXPORT_METHOD(transceiverSetDirection:(nonnull NSNumber *) objectID
135
142
 
136
143
  if (transceiver == nil) {
137
144
  RCTLogWarn(@"transceiverSetDirection() transceiver is null");
145
+ reject(@"E_INVALID", @"Could not get transceiver", nil);
138
146
  return;
139
147
  }
140
-
141
- NSMutableDictionary *identifier = [NSMutableDictionary new];
142
- identifier[@"peerConnectionId"] = objectID;
143
- identifier[@"transceiverId"] = senderId;
144
-
145
- NSMutableDictionary *params = [NSMutableDictionary new];
146
- RTCRtpTransceiverDirection oldDirrection = transceiver.direction;
147
- params[@"oldDirection"] = [SerializeUtils serializeDirection: oldDirrection];
148
+
148
149
  NSError *error;
149
150
  [transceiver setDirection:[SerializeUtils parseDirection:direction] error: &error];
150
-
151
+
151
152
  if (error) {
152
- [self sendErrorWithEventName: kEventTransceiverOnError
153
- funcName: @"transceiverSetDirection"
154
- message: [error localizedDescription]
155
- info: nil];
153
+ reject(@"E_SET_DIRECTION", @"Could not set direction", error);
154
+ } else {
155
+ resolve(@true);
156
156
  }
157
157
  }
158
158
 
159
159
  RCT_EXPORT_METHOD(transceiverStop:(nonnull NSNumber *) objectID
160
- senderId:(NSString *)senderId)
160
+ senderId:(NSString *)senderId
161
+ resolver:(RCTPromiseResolveBlock)resolve
162
+ rejecter:(RCTPromiseRejectBlock)reject)
161
163
  {
162
164
  RTCPeerConnection *peerConnection = self.peerConnections[objectID];
163
165
 
164
166
  if (peerConnection == nil) {
165
167
  RCTLogWarn(@"PeerConnection %@ not found in transceiverStop()", objectID);
168
+ reject(@"E_INVALID", @"Peer Connection is not initialized", nil);
166
169
  return;
167
170
  }
168
171
 
@@ -176,15 +179,13 @@ RCT_EXPORT_METHOD(transceiverStop:(nonnull NSNumber *) objectID
176
179
 
177
180
  if (transceiver == nil) {
178
181
  RCTLogWarn(@"senderSetParameters() transceiver is null");
182
+ reject(@"E_INVALID", @"Could not get transceiver", nil);
179
183
  return;
180
184
  }
181
185
 
182
186
  [transceiver stopInternal];
183
- [self sendEventWithName:kEventTransceiverStopSuccessful
184
- body:@{
185
- @"peerConnectionId": objectID,
186
- @"transceiverId": senderId
187
- }];
187
+
188
+ resolve(@true);
188
189
  }
189
190
 
190
191
  - (RTCRtpParameters *) updateParametersWithOptions: (NSDictionary *) options
@@ -70,7 +70,7 @@ static const NSTimeInterval MUTE_DELAY = 1.5;
70
70
  - (void)emitMuteEvent:(BOOL)muted {
71
71
  [self.module sendEventWithName:kEventMediaStreamTrackMuteChanged
72
72
  body:@{
73
- @"peerConnectionId": self.peerConnectionId,
73
+ @"pcId": self.peerConnectionId,
74
74
  @"trackId": self.trackId,
75
75
  @"muted": @(muted)
76
76
  }];
@@ -30,12 +30,9 @@ static NSString *const kEventPeerConnectionDidOpenDataChannel = @"peerConnection
30
30
  static NSString *const kEventDataChannelStateChanged = @"dataChannelStateChanged";
31
31
  static NSString *const kEventDataChannelReceiveMessage = @"dataChannelReceiveMessage";
32
32
  static NSString *const kEventMediaStreamTrackMuteChanged = @"mediaStreamTrackMuteChanged";
33
- static NSString *const kEventTransceiverStopSuccessful = @"transceiverStopSuccessful";
34
33
  static NSString *const kEventTransceiverOnError = @"transceiverOnError";
35
34
  static NSString *const kEventPeerConnectionOnRemoveTrack = @"peerConnectionOnRemoveTrack";
36
- static NSString *const kEventPeerConnectionOnRemoveTrackSuccessful = @"peerConnectionOnRemoveTrackSuccessful";
37
35
  static NSString *const kEventPeerConnectionOnTrack = @"peerConnectionOnTrack";
38
- static NSString *const kEventPeerConnectionOnError = @"peerConnectionOnError";
39
36
 
40
37
  @interface WebRTCModule : RCTEventEmitter <RCTBridgeModule>
41
38
 
@@ -54,9 +51,4 @@ static NSString *const kEventPeerConnectionOnError = @"peerConnectionOnError";
54
51
 
55
52
  - (RTCMediaStream*)streamForReactTag:(NSString*)reactTag;
56
53
 
57
- - (void)sendErrorWithEventName: (NSString *) eventName
58
- funcName: (NSString *) funcName
59
- message: (NSString *) message
60
- info: (NSDictionary *) info;
61
-
62
54
  @end