react-native-webrtc 1.100.0 → 106.0.0-beta.1

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 (167) hide show
  1. package/ISSUE_TEMPLATE.md +17 -13
  2. package/README.md +46 -129
  3. package/android/src/main/java/com/oney/WebRTCModule/PeerConnectionObserver.java +160 -165
  4. package/android/src/main/java/com/oney/WebRTCModule/SerializeUtils.java +279 -0
  5. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +361 -56
  6. package/android/src/main/java/com/oney/WebRTCModule/WebRTCView.java +5 -1
  7. package/ios/RCTWebRTC/RCTConvert+WebRTC.m +2 -6
  8. package/ios/RCTWebRTC/ScreenCaptureController.m +4 -0
  9. package/ios/RCTWebRTC/ScreenCapturer.m +7 -1
  10. package/ios/RCTWebRTC/SerializeUtils.h +25 -0
  11. package/ios/RCTWebRTC/SerializeUtils.m +219 -0
  12. package/ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.m +286 -128
  13. package/ios/RCTWebRTC/WebRTCModule+Transceivers.m +216 -0
  14. package/ios/RCTWebRTC/WebRTCModule.h +15 -4
  15. package/ios/RCTWebRTC/WebRTCModule.m +26 -6
  16. package/lib/commonjs/EventEmitter.js +24 -0
  17. package/lib/commonjs/EventEmitter.js.map +1 -1
  18. package/lib/commonjs/Logger.js +65 -0
  19. package/lib/commonjs/Logger.js.map +1 -0
  20. package/lib/commonjs/MediaStream.js +1 -1
  21. package/lib/commonjs/MediaStream.js.map +1 -1
  22. package/lib/commonjs/MediaStreamTrack.js +4 -2
  23. package/lib/commonjs/MediaStreamTrack.js.map +1 -1
  24. package/lib/commonjs/Permissions.js +1 -1
  25. package/lib/commonjs/Permissions.js.map +1 -1
  26. package/lib/commonjs/RTCDataChannel.js.map +1 -1
  27. package/lib/commonjs/RTCErrorEvent.js +33 -0
  28. package/lib/commonjs/RTCErrorEvent.js.map +1 -0
  29. package/lib/commonjs/RTCPeerConnection.js +354 -103
  30. package/lib/commonjs/RTCPeerConnection.js.map +1 -1
  31. package/lib/commonjs/RTCRtcpParameters.js +24 -0
  32. package/lib/commonjs/RTCRtcpParameters.js.map +1 -0
  33. package/lib/commonjs/RTCRtpCapabilities.js +79 -0
  34. package/lib/commonjs/RTCRtpCapabilities.js.map +1 -0
  35. package/lib/commonjs/RTCRtpCodecCapability.js +25 -0
  36. package/lib/commonjs/RTCRtpCodecCapability.js.map +1 -0
  37. package/lib/commonjs/RTCRtpCodecParameters.js +33 -0
  38. package/lib/commonjs/RTCRtpCodecParameters.js.map +1 -0
  39. package/lib/commonjs/RTCRtpEncodingParameters.js +81 -0
  40. package/lib/commonjs/RTCRtpEncodingParameters.js.map +1 -0
  41. package/lib/commonjs/RTCRtpHeaderExtension.js +27 -0
  42. package/lib/commonjs/RTCRtpHeaderExtension.js.map +1 -0
  43. package/lib/commonjs/RTCRtpParameters.js +26 -0
  44. package/lib/commonjs/RTCRtpParameters.js.map +1 -0
  45. package/lib/commonjs/RTCRtpReceiveParameters.js +20 -0
  46. package/lib/commonjs/RTCRtpReceiveParameters.js.map +1 -0
  47. package/lib/commonjs/RTCRtpReceiver.js +51 -0
  48. package/lib/commonjs/RTCRtpReceiver.js.map +1 -0
  49. package/lib/commonjs/RTCRtpSendParameters.js +60 -0
  50. package/lib/commonjs/RTCRtpSendParameters.js.map +1 -0
  51. package/lib/commonjs/RTCRtpSender.js +85 -0
  52. package/lib/commonjs/RTCRtpSender.js.map +1 -0
  53. package/lib/commonjs/RTCRtpTransceiver.js +141 -0
  54. package/lib/commonjs/RTCRtpTransceiver.js.map +1 -0
  55. package/lib/commonjs/RTCSessionDescription.js.map +1 -1
  56. package/lib/commonjs/RTCTrackEvent.js +32 -0
  57. package/lib/commonjs/RTCTrackEvent.js.map +1 -0
  58. package/lib/commonjs/RTCUtil.js +20 -37
  59. package/lib/commonjs/RTCUtil.js.map +1 -1
  60. package/lib/commonjs/getDisplayMedia.js.map +1 -1
  61. package/lib/commonjs/getUserMedia.js.map +1 -1
  62. package/lib/commonjs/index.js +127 -3
  63. package/lib/commonjs/index.js.map +1 -1
  64. package/lib/module/EventEmitter.js +21 -0
  65. package/lib/module/EventEmitter.js.map +1 -1
  66. package/lib/module/Logger.js +53 -0
  67. package/lib/module/Logger.js.map +1 -0
  68. package/lib/module/MediaStream.js +1 -1
  69. package/lib/module/MediaStream.js.map +1 -1
  70. package/lib/module/MediaStreamTrack.js +4 -2
  71. package/lib/module/MediaStreamTrack.js.map +1 -1
  72. package/lib/module/Permissions.js +1 -1
  73. package/lib/module/Permissions.js.map +1 -1
  74. package/lib/module/RTCDataChannel.js.map +1 -1
  75. package/lib/module/RTCErrorEvent.js +22 -0
  76. package/lib/module/RTCErrorEvent.js.map +1 -0
  77. package/lib/module/RTCPeerConnection.js +348 -103
  78. package/lib/module/RTCPeerConnection.js.map +1 -1
  79. package/lib/module/RTCRtcpParameters.js +15 -0
  80. package/lib/module/RTCRtcpParameters.js.map +1 -0
  81. package/lib/module/RTCRtpCapabilities.js +63 -0
  82. package/lib/module/RTCRtpCapabilities.js.map +1 -0
  83. package/lib/module/RTCRtpCodecCapability.js +16 -0
  84. package/lib/module/RTCRtpCodecCapability.js.map +1 -0
  85. package/lib/module/RTCRtpCodecParameters.js +24 -0
  86. package/lib/module/RTCRtpCodecParameters.js.map +1 -0
  87. package/lib/module/RTCRtpEncodingParameters.js +72 -0
  88. package/lib/module/RTCRtpEncodingParameters.js.map +1 -0
  89. package/lib/module/RTCRtpHeaderExtension.js +18 -0
  90. package/lib/module/RTCRtpHeaderExtension.js.map +1 -0
  91. package/lib/module/RTCRtpParameters.js +17 -0
  92. package/lib/module/RTCRtpParameters.js.map +1 -0
  93. package/lib/module/RTCRtpReceiveParameters.js +8 -0
  94. package/lib/module/RTCRtpReceiveParameters.js.map +1 -0
  95. package/lib/module/RTCRtpReceiver.js +39 -0
  96. package/lib/module/RTCRtpReceiver.js.map +1 -0
  97. package/lib/module/RTCRtpSendParameters.js +49 -0
  98. package/lib/module/RTCRtpSendParameters.js.map +1 -0
  99. package/lib/module/RTCRtpSender.js +70 -0
  100. package/lib/module/RTCRtpSender.js.map +1 -0
  101. package/lib/module/RTCRtpTransceiver.js +125 -0
  102. package/lib/module/RTCRtpTransceiver.js.map +1 -0
  103. package/lib/module/RTCSessionDescription.js.map +1 -1
  104. package/lib/module/RTCTrackEvent.js +23 -0
  105. package/lib/module/RTCTrackEvent.js.map +1 -0
  106. package/lib/module/RTCUtil.js +18 -36
  107. package/lib/module/RTCUtil.js.map +1 -1
  108. package/lib/module/getDisplayMedia.js.map +1 -1
  109. package/lib/module/getUserMedia.js.map +1 -1
  110. package/lib/module/index.js +35 -4
  111. package/lib/module/index.js.map +1 -1
  112. package/lib/typescript/EventEmitter.d.ts +3 -0
  113. package/lib/typescript/Logger.d.ts +13 -0
  114. package/lib/typescript/MediaDevices.d.ts +2 -2
  115. package/lib/typescript/MediaStream.d.ts +6 -6
  116. package/lib/typescript/MediaStreamTrack.d.ts +10 -9
  117. package/lib/typescript/RTCDataChannel.d.ts +4 -2
  118. package/lib/typescript/RTCErrorEvent.d.ts +12 -0
  119. package/lib/typescript/RTCPeerConnection.d.ts +42 -11
  120. package/lib/typescript/RTCRtcpParameters.d.ts +8 -0
  121. package/lib/typescript/RTCRtpCapabilities.d.ts +19 -0
  122. package/lib/typescript/RTCRtpCodecCapability.d.ts +7 -0
  123. package/lib/typescript/RTCRtpCodecParameters.d.ts +14 -0
  124. package/lib/typescript/RTCRtpEncodingParameters.d.ts +21 -0
  125. package/lib/typescript/RTCRtpHeaderExtension.d.ts +10 -0
  126. package/lib/typescript/RTCRtpParameters.d.ts +14 -0
  127. package/lib/typescript/RTCRtpReceiveParameters.d.ts +4 -0
  128. package/lib/typescript/RTCRtpReceiver.d.ts +15 -0
  129. package/lib/typescript/RTCRtpSendParameters.d.ts +17 -0
  130. package/lib/typescript/RTCRtpSender.d.ts +21 -0
  131. package/lib/typescript/RTCRtpTransceiver.d.ts +35 -0
  132. package/lib/typescript/RTCTrackEvent.d.ts +15 -0
  133. package/lib/typescript/RTCUtil.d.ts +5 -9
  134. package/lib/typescript/getDisplayMedia.d.ts +2 -1
  135. package/lib/typescript/getUserMedia.d.ts +2 -1
  136. package/lib/typescript/index.d.ts +16 -3
  137. package/package.json +6 -4
  138. package/react-native-webrtc.podspec +1 -1
  139. package/src/EventEmitter.ts +21 -1
  140. package/src/Logger.ts +49 -0
  141. package/src/MediaStream.ts +11 -11
  142. package/src/MediaStreamTrack.ts +14 -13
  143. package/src/Permissions.ts +1 -1
  144. package/src/RTCDataChannel.ts +11 -8
  145. package/src/RTCErrorEvent.ts +21 -0
  146. package/src/RTCPeerConnection.ts +400 -158
  147. package/src/RTCRtcpParameters.ts +14 -0
  148. package/src/RTCRtpCapabilities.ts +54 -0
  149. package/src/RTCRtpCodecCapability.ts +13 -0
  150. package/src/RTCRtpCodecParameters.ts +23 -0
  151. package/src/RTCRtpEncodingParameters.ts +67 -0
  152. package/src/RTCRtpHeaderExtension.ts +18 -0
  153. package/src/RTCRtpParameters.ts +21 -0
  154. package/src/RTCRtpReceiveParameters.ts +7 -0
  155. package/src/RTCRtpReceiver.ts +35 -0
  156. package/src/RTCRtpSendParameters.ts +51 -0
  157. package/src/RTCRtpSender.ts +72 -0
  158. package/src/RTCRtpTransceiver.ts +125 -0
  159. package/src/RTCSessionDescription.ts +3 -3
  160. package/src/RTCTrackEvent.ts +21 -0
  161. package/src/RTCUtil.ts +20 -40
  162. package/src/getDisplayMedia.ts +1 -1
  163. package/src/getUserMedia.ts +2 -2
  164. package/src/index.ts +51 -6
  165. package/tools/build-webrtc.py +32 -33
  166. package/tools/downloadBitcode.sh +2 -12
  167. package/tools/downloadWebRTC.js +21 -12
@@ -19,11 +19,15 @@
19
19
  #import <WebRTC/RTCIceCandidate.h>
20
20
  #import <WebRTC/RTCSessionDescription.h>
21
21
  #import <WebRTC/RTCStatisticsReport.h>
22
+ #import <WebRTC/RTCMediaStreamTrack.h>
23
+ #import <WebRTC/RTCRtpReceiver.h>
24
+ #import <WebRTC/RTCRtpTransceiver.h>
22
25
 
23
26
  #import "WebRTCModule.h"
24
27
  #import "WebRTCModule+RTCDataChannel.h"
25
28
  #import "WebRTCModule+RTCPeerConnection.h"
26
29
  #import "WebRTCModule+VideoTrackAdapter.h"
30
+ #import "SerializeUtils.h"
27
31
 
28
32
  @implementation RTCPeerConnection (React)
29
33
 
@@ -81,6 +85,8 @@
81
85
 
82
86
  @implementation WebRTCModule (RTCPeerConnection)
83
87
 
88
+ int _transceiverNextId = 0;
89
+
84
90
  /*
85
91
  * This method is synchronous and blocking. This is done so we can implement createDataChannel
86
92
  * in the same way (synchronous) since the peer connection needs to exist before.
@@ -119,35 +125,6 @@ RCT_EXPORT_METHOD(peerConnectionSetConfiguration:(RTCConfiguration*)configuratio
119
125
  [peerConnection setConfiguration:configuration];
120
126
  }
121
127
 
122
- RCT_EXPORT_METHOD(peerConnectionAddStream:(nonnull NSString *)streamID objectID:(nonnull NSNumber *)objectID)
123
- {
124
- RTCPeerConnection *peerConnection = self.peerConnections[objectID];
125
- if (!peerConnection) {
126
- return;
127
- }
128
- RTCMediaStream *stream = self.localStreams[streamID];
129
- if (!stream) {
130
- return;
131
- }
132
-
133
- [peerConnection addStream:stream];
134
- }
135
-
136
- RCT_EXPORT_METHOD(peerConnectionRemoveStream:(nonnull NSString *)streamID objectID:(nonnull NSNumber *)objectID)
137
- {
138
- RTCPeerConnection *peerConnection = self.peerConnections[objectID];
139
- if (!peerConnection) {
140
- return;
141
- }
142
- RTCMediaStream *stream = self.localStreams[streamID];
143
- if (!stream) {
144
- return;
145
- }
146
-
147
- [peerConnection removeStream:stream];
148
- }
149
-
150
-
151
128
  RCT_EXPORT_METHOD(peerConnectionCreateOffer:(nonnull NSNumber *)objectID
152
129
  options:(NSDictionary *)options
153
130
  callback:(RCTResponseSenderBlock)callback)
@@ -174,16 +151,22 @@ RCT_EXPORT_METHOD(peerConnectionCreateOffer:(nonnull NSNumber *)objectID
174
151
  ]);
175
152
  } else {
176
153
  NSString *type = [RTCSessionDescription stringForType:sdp.type];
177
- callback(@[@(YES), @{@"sdp": sdp.sdp, @"type": type}]);
154
+ callback(@[@(YES), @{
155
+ @"sdpInfo": @{
156
+ @"sdp": sdp.sdp,
157
+ @"type": type
158
+ },
159
+ @"transceiversInfo": [SerializeUtils constructTransceiversInfoArrayWithPeerConnection:peerConnection peerConnectionId: objectID]
160
+ }]);
178
161
  }
179
162
  }];
180
163
  }
181
164
 
182
- RCT_EXPORT_METHOD(peerConnectionCreateAnswer:(nonnull NSNumber *)peerConnectionId
165
+ RCT_EXPORT_METHOD(peerConnectionCreateAnswer:(nonnull NSNumber *)objectID
183
166
  options:(NSDictionary *)options
184
167
  callback:(RCTResponseSenderBlock)callback)
185
168
  {
186
- RTCPeerConnection *peerConnection = self.peerConnections[peerConnectionId];
169
+ RTCPeerConnection *peerConnection = self.peerConnections[objectID];
187
170
  if (!peerConnection) {
188
171
  return;
189
172
  }
@@ -205,7 +188,11 @@ RCT_EXPORT_METHOD(peerConnectionCreateAnswer:(nonnull NSNumber *)peerConnectionI
205
188
  ]);
206
189
  } else {
207
190
  NSString *type = [RTCSessionDescription stringForType:sdp.type];
208
- callback(@[@(YES), @{@"sdp": sdp.sdp, @"type": type}]);
191
+ callback(@[@(YES), @{
192
+ @"sdpInfo": @{@"sdp": sdp.sdp,
193
+ @"type": type},
194
+ @"transceiversInfo": [SerializeUtils constructTransceiversInfoArrayWithPeerConnection:peerConnection peerConnectionId: objectID]
195
+ }]);
209
196
  }
210
197
  }];
211
198
  }
@@ -229,8 +216,11 @@ RCT_EXPORT_METHOD(peerConnectionSetLocalDescription:(nonnull NSNumber *)objectID
229
216
  } else {
230
217
  RTCPeerConnection *strongPc = weakPc;
231
218
  id newSdp = @{
232
- @"type": [RTCSessionDescription stringForType:strongPc.localDescription.type],
233
- @"sdp": strongPc.localDescription.sdp
219
+
220
+ @"sdpInfo": @{@"type": [RTCSessionDescription stringForType:strongPc.localDescription.type],
221
+ @"sdp": strongPc.localDescription.sdp
222
+ },
223
+ @"transceiversInfo": [SerializeUtils constructTransceiversInfoArrayWithPeerConnection:peerConnection peerConnectionId: objectID]
234
224
  };
235
225
  resolve(newSdp);
236
226
  }
@@ -251,19 +241,21 @@ RCT_EXPORT_METHOD(peerConnectionSetRemoteDescription:(RTCSessionDescription *)sd
251
241
  }
252
242
 
253
243
  [peerConnection setRemoteDescription: sdp completionHandler: ^(NSError *error) {
254
- if (error) {
255
- id errorResponse = @{
256
- @"name": @"SetRemoteDescriptionFailed",
257
- @"message": error.localizedDescription ?: [NSNull null]
258
- };
259
- callback(@[@(NO), errorResponse]);
260
- } else {
261
- id newSdp = @{
262
- @"type": [RTCSessionDescription stringForType:peerConnection.remoteDescription.type],
263
- @"sdp": peerConnection.remoteDescription.sdp
264
- };
265
- callback(@[@(YES), newSdp]);
266
- }
244
+ if (error) {
245
+ id errorResponse = @{
246
+ @"name": @"SetRemoteDescriptionFailed",
247
+ @"message": error.localizedDescription ?: [NSNull null]
248
+ };
249
+ callback(@[@(NO), errorResponse]);
250
+ } else {
251
+ id newSdp = @{
252
+ @"sdpInfo": @{
253
+ @"type": [RTCSessionDescription stringForType:peerConnection.remoteDescription.type],
254
+ @"sdp": peerConnection.remoteDescription.sdp},
255
+ @"transceiversInfo": [SerializeUtils constructTransceiversInfoArrayWithPeerConnection:peerConnection peerConnectionId: objectID]
256
+ };
257
+ callback(@[@(YES), newSdp]);
258
+ }
267
259
  }];
268
260
  }
269
261
 
@@ -302,10 +294,11 @@ RCT_EXPORT_METHOD(peerConnectionClose:(nonnull NSNumber *)objectID)
302
294
  }
303
295
 
304
296
  // Remove video track adapters
305
- for(RTCMediaStream *stream in [peerConnection.remoteStreams allValues]) {
306
- for (RTCVideoTrack *track in stream.videoTracks) {
307
- [peerConnection removeVideoTrackAdapter:track];
308
- }
297
+ for (NSString *key in peerConnection.remoteTracks.allKeys) {
298
+ RTCMediaStreamTrack *track = peerConnection.remoteTracks[key];
299
+ if (track.kind == kRTCMediaStreamTrackKindVideo) {
300
+ [peerConnection removeVideoTrackAdapter: (RTCVideoTrack*) track];
301
+ }
309
302
  }
310
303
 
311
304
  [peerConnection close];
@@ -351,6 +344,163 @@ RCT_EXPORT_METHOD(peerConnectionRestartIce:(nonnull NSNumber *)objectID)
351
344
  [peerConnection restartIce];
352
345
  }
353
346
 
347
+ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(peerConnectionAddTrack:(nonnull NSNumber *)objectID
348
+ trackId:(NSString *)trackId
349
+ options:(NSDictionary *)options)
350
+ {
351
+ __block id params;
352
+
353
+ dispatch_sync(self.workerQueue, ^{
354
+ RTCPeerConnection *peerConnection = self.peerConnections[objectID];
355
+ if (!peerConnection) {
356
+ RCTLogWarn(@"PeerConnection %@ not found in peerConnectionAddTrack()", objectID);
357
+ return;
358
+ }
359
+
360
+ RTCMediaStreamTrack *track = self.localTracks[trackId];
361
+
362
+ NSArray *streamIds = [options objectForKey:@"streamIds"];
363
+ RTCRtpSender *sender = [peerConnection addTrack: track streamIds: streamIds];
364
+ RTCRtpTransceiver *transceiver = nil;
365
+
366
+ for (RTCRtpTransceiver *t in peerConnection.transceivers) {
367
+ if([t.sender.senderId isEqual: sender.senderId]) {
368
+ transceiver = t;
369
+ break;
370
+ }
371
+ }
372
+
373
+ if (!transceiver) {
374
+ RCTLogWarn(@"Transceiver not found in peerConnectionAddTrack()");
375
+ return;
376
+ }
377
+
378
+ params = @{
379
+ @"transceiverOrder": [NSNumber numberWithInt:_transceiverNextId++],
380
+ @"transceiver": [SerializeUtils transceiverToJSONWithPeerConnectionId: objectID transceiver: transceiver],
381
+ @"sender": [SerializeUtils senderToJSONWithPeerConnectionId: objectID sender: sender]
382
+ };
383
+ });
384
+
385
+ return params;
386
+ }
387
+
388
+ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(peerConnectionAddTransceiver:(nonnull NSNumber *)objectID
389
+ options:(NSDictionary *)options)
390
+ {
391
+ __block id params;
392
+
393
+ dispatch_sync(self.workerQueue, ^{
394
+ RTCPeerConnection *peerConnection = self.peerConnections[objectID];
395
+ if (!peerConnection) {
396
+ RCTLogWarn(@"PeerConnection %@ not found in peerConnectionAddTransceiver()", objectID);
397
+ return;
398
+ }
399
+
400
+ RTCRtpTransceiver *transceiver = nil;
401
+ NSString *kind = [options objectForKey:@"type"];
402
+ NSString *trackId = [options objectForKey:@"trackId"];
403
+ RTCRtpMediaType type = RTCRtpMediaTypeUnsupported;
404
+
405
+ if (kind) {
406
+ if ([kind isEqual: @"audio"]) {
407
+ type = RTCRtpMediaTypeAudio;
408
+ } else if ([kind isEqual: @"video"]) {
409
+ type = RTCRtpMediaTypeVideo;
410
+ }
411
+ transceiver = [peerConnection addTransceiverOfType:type];
412
+ } else if (trackId) {
413
+ RTCMediaStreamTrack *track = self.localTracks[trackId];
414
+
415
+ if(!track) {
416
+ track = peerConnection.remoteTracks[trackId];
417
+ }
418
+
419
+ NSDictionary *initOptions = [options objectForKey:@"init"];
420
+ RTCRtpTransceiverInit *transceiverInit = [RTCRtpTransceiverInit new];
421
+
422
+ if (initOptions) {
423
+ NSString *dirrection = [initOptions objectForKey:@"direction"];
424
+ if ([dirrection isEqual: @"sendrecv"]) {
425
+ [transceiverInit setDirection:RTCRtpTransceiverDirectionSendRecv];
426
+ } else if ([dirrection isEqual: @"sendonly"]) {
427
+ [transceiverInit setDirection:RTCRtpTransceiverDirectionSendOnly];
428
+ } else if ([dirrection isEqual: @"recvonly"]) {
429
+ [transceiverInit setDirection:RTCRtpTransceiverDirectionRecvOnly];
430
+ } else if ([dirrection isEqual: @"inactive"]) {
431
+ [transceiverInit setDirection:RTCRtpTransceiverDirectionInactive];
432
+ }
433
+
434
+ NSArray *streamIds = [initOptions objectForKey:@"streamIds"];
435
+ if (streamIds) {
436
+ [transceiverInit setStreamIds:streamIds];
437
+ }
438
+ }
439
+
440
+ transceiver = [peerConnection addTransceiverWithTrack:track init:transceiverInit];
441
+ } else {
442
+ RCTLogWarn(@"peerConnectionAddTransceiver() no type nor trackId provided in options");
443
+ return;
444
+ }
445
+
446
+ if (transceiver == nil) {
447
+ RCTLogWarn(@"peerConnectionAddTransceiver() Error adding transceiver");
448
+ return;
449
+ }
450
+
451
+ params = @{
452
+ @"transceiverOrder": [NSNumber numberWithInt:_transceiverNextId++],
453
+ @"transceiver": [SerializeUtils transceiverToJSONWithPeerConnectionId: objectID transceiver: transceiver ]
454
+ };
455
+ });
456
+
457
+ return params;
458
+ }
459
+
460
+ RCT_EXPORT_METHOD(peerConnectionRemoveTrack:(nonnull NSNumber *)objectID
461
+ senderId:(nonnull NSString *)senderId)
462
+ {
463
+ RTCPeerConnection *peerConnection = self.peerConnections[objectID];
464
+ if (!peerConnection) {
465
+ RCTLogWarn(@"PeerConnection %@ not found in peerConnectionRemoveTrack()", objectID);
466
+ [self sendErrorWithEventName: kEventPeerConnectionOnError
467
+ funcName: @"removeTrack"
468
+ message: @"Peer Connection is not initialized"
469
+ info: nil];
470
+ return;
471
+ }
472
+
473
+ RTCRtpTransceiver *transceiver = nil;
474
+
475
+ for (RTCRtpTransceiver *t in peerConnection.transceivers) {
476
+ if (t.sender.senderId == senderId) {
477
+ transceiver = t;
478
+ break;
479
+ }
480
+ }
481
+
482
+ if (!transceiver) {
483
+ RCTLogWarn(@"Transceiver not found in peerConnectionRemoveTrack()");
484
+ return;
485
+ }
486
+
487
+ NSMutableDictionary *identifier = [NSMutableDictionary new];
488
+ identifier[@"peerConnectionId"] = objectID;
489
+ identifier[@"senderId"] = senderId;
490
+
491
+ if ([peerConnection removeTrack: transceiver.sender]) {
492
+ [self sendEventWithName: kEventPeerConnectionOnRemoveTrackSuccessful
493
+ body: identifier];
494
+ } else {
495
+ [self sendErrorWithEventName: kEventPeerConnectionOnError
496
+ funcName: @"removeTrack"
497
+ message: @"Cannot remove track"
498
+ info: identifier];
499
+ }
500
+ }
501
+
502
+ // TODO: move these below to some SerializeUrils file
503
+
354
504
  /**
355
505
  * Constructs a JSON <tt>NSString</tt> representation of a specific
356
506
  * <tt>RTCStatisticsReport</tt>s.
@@ -362,14 +512,14 @@ RCT_EXPORT_METHOD(peerConnectionRestartIce:(nonnull NSNumber *)objectID)
362
512
  */
363
513
  - (NSString *)statsToJSON:(RTCStatisticsReport *)report
364
514
  {
365
- /*
515
+ /*
366
516
  The initial capacity matters, of course, because it determines how many
367
517
  times the NSMutableString will have grow. But walking through the reports
368
518
  to compute an initial capacity which exactly matches the requirements of
369
519
  the reports is too much work without real-world bang here. An improvement
370
- should be caching the required capacity from the previous invocation of the
371
- method and using it as the initial capacity in the next invocation.
372
- As I didn't want to go even through that,choosing just about any initial
520
+ should be caching the required capacity from the previous invocation of the
521
+ method and using it as the initial capacity in the next invocation.
522
+ As I didn't want to go even through that,choosing just about any initial
373
523
  capacity is OK because NSMutableCopy doesn't have too bad a strategy of growing.
374
524
  */
375
525
  NSMutableString *s = [NSMutableString stringWithCapacity:16 * 1024];
@@ -390,7 +540,7 @@ RCT_EXPORT_METHOD(peerConnectionRestartIce:(nonnull NSNumber *)objectID)
390
540
  RTCStatistics *statistics = report.statistics[key];
391
541
  [s appendString:@"\"timestamp\":"];
392
542
  [s appendFormat:@"%f", statistics.timestamp_us / 1000.0];
393
- [s appendString:@",\"type\":\""];
543
+ [s appendString:@",\"type\":\""];
394
544
  [s appendString:statistics.type];
395
545
  [s appendString:@"\",\"id\":\""];
396
546
  [s appendString:statistics.id];
@@ -406,7 +556,7 @@ RCT_EXPORT_METHOD(peerConnectionRestartIce:(nonnull NSNumber *)objectID)
406
556
  }
407
557
 
408
558
  [s appendString:@"}]"];
409
- }
559
+ }
410
560
 
411
561
  [s appendString:@"]"];
412
562
 
@@ -434,7 +584,7 @@ RCT_EXPORT_METHOD(peerConnectionRestartIce:(nonnull NSNumber *)objectID)
434
584
  NSError *error;
435
585
  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:statisticsValue
436
586
  options:0
437
- error:&error];
587
+ error:&error];
438
588
 
439
589
  if (!jsonData) {
440
590
  [s appendString:@"\""];
@@ -444,6 +594,8 @@ RCT_EXPORT_METHOD(peerConnectionRestartIce:(nonnull NSNumber *)objectID)
444
594
  NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
445
595
  [s appendString:jsonString];
446
596
  }
597
+ } else if ([statisticsValue isKindOfClass:[NSNumber class]]) {
598
+ [s appendString:[NSString stringWithFormat:@"%@", statisticsValue]];
447
599
  } else {
448
600
  [s appendString:@"\""];
449
601
  [s appendString:[NSString stringWithFormat:@"%@", statisticsValue]];
@@ -508,74 +660,6 @@ RCT_EXPORT_METHOD(peerConnectionRestartIce:(nonnull NSNumber *)objectID)
508
660
  }];
509
661
  }
510
662
 
511
- - (void)peerConnection:(RTCPeerConnection *)peerConnection didAddStream:(RTCMediaStream *)stream {
512
- NSString *streamReactTag = [[NSUUID UUID] UUIDString];
513
- NSMutableArray *tracks = [NSMutableArray array];
514
- for (RTCVideoTrack *track in stream.videoTracks) {
515
- peerConnection.remoteTracks[track.trackId] = track;
516
- [peerConnection addVideoTrackAdapter:track];
517
- [tracks addObject:@{@"id": track.trackId, @"kind": track.kind, @"label": track.trackId, @"enabled": @(track.isEnabled), @"remote": @(YES), @"readyState": @"live"}];
518
- }
519
- for (RTCAudioTrack *track in stream.audioTracks) {
520
- peerConnection.remoteTracks[track.trackId] = track;
521
- [tracks addObject:@{@"id": track.trackId, @"kind": track.kind, @"label": track.trackId, @"enabled": @(track.isEnabled), @"remote": @(YES), @"readyState": @"live"}];
522
- }
523
-
524
- peerConnection.remoteStreams[streamReactTag] = stream;
525
-
526
- id newSdp = @{
527
- @"type": [RTCSessionDescription stringForType:peerConnection.remoteDescription.type],
528
- @"sdp": peerConnection.remoteDescription.sdp
529
- };
530
-
531
- [self sendEventWithName:kEventPeerConnectionAddedStream
532
- body:@{
533
- @"id": peerConnection.reactTag,
534
- @"streamId": stream.streamId,
535
- @"streamReactTag": streamReactTag,
536
- @"tracks": tracks,
537
- @"sdp": newSdp
538
- }];
539
- }
540
-
541
- - (void)peerConnection:(RTCPeerConnection *)peerConnection didRemoveStream:(RTCMediaStream *)stream {
542
- // XXX Find the stream by comparing the 'streamId' values. It turns out that WebRTC (as of M69) creates new wrapper
543
- // instance for the native media stream before invoking the 'didRemoveStream' callback. This means it's a different
544
- // RTCMediaStream instance passed to 'didAddStream' and 'didRemoveStream'.
545
- NSString *streamReactTag = nil;
546
- for (NSString *aReactTag in peerConnection.remoteStreams) {
547
- RTCMediaStream *aStream = peerConnection.remoteStreams[aReactTag];
548
- if ([aStream.streamId isEqualToString:stream.streamId]) {
549
- streamReactTag = aReactTag;
550
- break;
551
- }
552
- }
553
- if (!streamReactTag) {
554
- RCTLogWarn(@"didRemoveStream - stream not found, id: %@", stream.streamId);
555
- return;
556
- }
557
- for (RTCVideoTrack *track in stream.videoTracks) {
558
- [peerConnection removeVideoTrackAdapter:track];
559
- [peerConnection.remoteTracks removeObjectForKey:track.trackId];
560
- }
561
- for (RTCAudioTrack *track in stream.audioTracks) {
562
- [peerConnection.remoteTracks removeObjectForKey:track.trackId];
563
- }
564
- [peerConnection.remoteStreams removeObjectForKey:streamReactTag];
565
-
566
- id newSdp = @{
567
- @"type": [RTCSessionDescription stringForType:peerConnection.remoteDescription.type],
568
- @"sdp": peerConnection.remoteDescription.sdp
569
- };
570
-
571
- [self sendEventWithName:kEventPeerConnectionRemovedStream
572
- body:@{
573
- @"id": peerConnection.reactTag,
574
- @"streamId": streamReactTag,
575
- @"sdp": newSdp
576
- }];
577
- }
578
-
579
663
  - (void)peerConnectionShouldNegotiate:(RTCPeerConnection *)peerConnection {
580
664
  [self sendEventWithName:kEventPeerConnectionOnRenegotiationNeeded
581
665
  body:@{ @"id": peerConnection.reactTag }];
@@ -652,8 +736,82 @@ RCT_EXPORT_METHOD(peerConnectionRestartIce:(nonnull NSNumber *)objectID)
652
736
  [self sendEventWithName:kEventPeerConnectionDidOpenDataChannel body:body];
653
737
  }
654
738
 
739
+ - (void)peerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)peerConnection didAddReceiver:(RTC_OBJC_TYPE(RTCRtpReceiver) *)rtpReceiver
740
+ streams:(NSArray<RTC_OBJC_TYPE(RTCMediaStream) *> *)mediaStreams {
741
+ dispatch_async(self.workerQueue, ^{
742
+ RTCRtpTransceiver *transceiver = nil;
743
+ for (RTCRtpTransceiver *t in peerConnection.transceivers) {
744
+ if ([rtpReceiver.receiverId isEqual: t.receiver.receiverId]) {
745
+ transceiver = t;
746
+ break;
747
+ }
748
+ }
749
+
750
+ if (!transceiver) {
751
+ RCTLogWarn(@"Transceiver not found in didAddReceiver()");
752
+ return;
753
+ }
754
+
755
+ RTCMediaStreamTrack *track = rtpReceiver.track;
756
+
757
+ if (peerConnection.remoteTracks[track.trackId]) {
758
+ return;
759
+ }
760
+
761
+ if (track.kind == kRTCMediaStreamTrackKindVideo) {
762
+ RTCVideoTrack *videoTrack = (RTCVideoTrack *)track;
763
+ [peerConnection addVideoTrackAdapter: videoTrack];
764
+ }
765
+
766
+ peerConnection.remoteTracks[track.trackId] = track;
767
+
768
+ NSMutableDictionary *params = [NSMutableDictionary new];
769
+ NSMutableArray *streams = [NSMutableArray new];
770
+
771
+ for (RTCMediaStream * stream in mediaStreams) {
772
+ NSString *streamReactTag = nil;
773
+
774
+ for (NSString * key in [peerConnection.remoteStreams allKeys]) {
775
+ if ([[peerConnection.remoteStreams objectForKey:key] isEqual:stream]) {
776
+ streamReactTag = key;
777
+ break;
778
+ }
779
+ }
780
+
781
+ if (!peerConnection.remoteStreams[stream.streamId]) {
782
+ peerConnection.remoteStreams[stream.streamId] = stream;
783
+ }
784
+
785
+ if (!streamReactTag) {
786
+ NSUUID *uuid = [NSUUID UUID];
787
+ streamReactTag = [uuid UUIDString];
788
+ peerConnection.remoteStreams[streamReactTag] = stream;
789
+ }
790
+
791
+ [streams addObject:[SerializeUtils streamToJSONWithPeerConnectionId:peerConnection.reactTag stream:stream streamReactTag:streamReactTag]];
792
+ }
793
+
794
+ params[@"streams"] = streams;
795
+ params[@"receiver"] = [SerializeUtils receiverToJSONWithPeerConnectionId: peerConnection.reactTag receiver: rtpReceiver];
796
+ params[@"transceiverOrder"] = [NSNumber numberWithInt:_transceiverNextId++];
797
+ params[@"transceiver"] = [SerializeUtils transceiverToJSONWithPeerConnectionId: peerConnection.reactTag transceiver: transceiver];
798
+ params[@"id"] = peerConnection.reactTag;
799
+
800
+ [self sendEventWithName: kEventPeerConnectionOnTrack
801
+ body: params];
802
+ });
803
+ }
804
+
655
805
  - (void)peerConnection:(nonnull RTCPeerConnection *)peerConnection didRemoveIceCandidates:(nonnull NSArray<RTCIceCandidate *> *)candidates {
656
- // TODO
806
+ // Unimplemented, there is no matching web API.
807
+ }
808
+
809
+ - (void)peerConnection:(nonnull RTCPeerConnection *)peerConnection didAddStream:(nonnull RTCMediaStream *)stream {
810
+ // Unused in Unified Plan.
811
+ }
812
+
813
+ - (void)peerConnection:(nonnull RTCPeerConnection *)peerConnection didRemoveStream:(nonnull RTCMediaStream *)stream {
814
+ // Unused in Unified Plan.
657
815
  }
658
816
 
659
817
  @end