react-native-webrtc 124.0.7 → 124.0.8

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 (123) hide show
  1. package/.claude/settings.local.json +9 -0
  2. package/.eslintignore +1 -0
  3. package/CLAUDE.md +714 -0
  4. package/android/src/main/java/com/oney/WebRTCModule/AbstractVideoCaptureController.java +5 -3
  5. package/android/src/main/java/com/oney/WebRTCModule/GetUserMediaImpl.java +67 -7
  6. package/android/src/main/java/com/oney/WebRTCModule/MediaProjectionService.java +24 -3
  7. package/android/src/main/java/com/oney/WebRTCModule/ScreenCaptureController.java +4 -4
  8. package/android/src/main/java/com/oney/WebRTCModule/SerializeUtils.java +8 -0
  9. package/android/src/main/java/com/oney/WebRTCModule/ThreadUtils.java +8 -0
  10. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +112 -26
  11. package/android/src/main/java/com/oney/WebRTCModule/WebRTCView.java +56 -28
  12. package/ios/RCTWebRTC/RCTConvert+WebRTC.m +19 -0
  13. package/ios/RCTWebRTC/ScreenCapturePickerViewManager.m +22 -23
  14. package/ios/RCTWebRTC/SerializeUtils.m +6 -0
  15. package/ios/RCTWebRTC/VideoCaptureController.m +40 -1
  16. package/ios/RCTWebRTC/WebRTCModule+RTCMediaStream.m +16 -9
  17. package/ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.h +2 -0
  18. package/ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.m +95 -0
  19. package/ios/RCTWebRTC/WebRTCModule+Transceivers.m +1 -0
  20. package/lib/commonjs/MediaDevices.js +12 -10
  21. package/lib/commonjs/MediaDevices.js.map +1 -1
  22. package/lib/commonjs/MediaStream.js +14 -9
  23. package/lib/commonjs/MediaStream.js.map +1 -1
  24. package/lib/commonjs/MediaStreamTrack.js +28 -12
  25. package/lib/commonjs/MediaStreamTrack.js.map +1 -1
  26. package/lib/commonjs/MediaStreamTrackEvent.js +2 -2
  27. package/lib/commonjs/MediaStreamTrackEvent.js.map +1 -1
  28. package/lib/commonjs/MessageEvent.js +2 -2
  29. package/lib/commonjs/MessageEvent.js.map +1 -1
  30. package/lib/commonjs/RTCCertificate.js +25 -0
  31. package/lib/commonjs/RTCCertificate.js.map +1 -0
  32. package/lib/commonjs/RTCDataChannel.js +38 -13
  33. package/lib/commonjs/RTCDataChannel.js.map +1 -1
  34. package/lib/commonjs/RTCDataChannelEvent.js +2 -2
  35. package/lib/commonjs/RTCDataChannelEvent.js.map +1 -1
  36. package/lib/commonjs/RTCErrorEvent.js +2 -2
  37. package/lib/commonjs/RTCErrorEvent.js.map +1 -1
  38. package/lib/commonjs/RTCIceCandidateEvent.js +2 -2
  39. package/lib/commonjs/RTCIceCandidateEvent.js.map +1 -1
  40. package/lib/commonjs/RTCPeerConnection.js +109 -27
  41. package/lib/commonjs/RTCPeerConnection.js.map +1 -1
  42. package/lib/commonjs/RTCRtpEncodingParameters.js +17 -1
  43. package/lib/commonjs/RTCRtpEncodingParameters.js.map +1 -1
  44. package/lib/commonjs/RTCTrackEvent.js +2 -2
  45. package/lib/commonjs/RTCTrackEvent.js.map +1 -1
  46. package/lib/commonjs/getDisplayMedia.js +2 -1
  47. package/lib/commonjs/getDisplayMedia.js.map +1 -1
  48. package/lib/commonjs/index.js +22 -2
  49. package/lib/commonjs/index.js.map +1 -1
  50. package/lib/commonjs/vendor/event-target-shim/LICENSE +22 -0
  51. package/lib/commonjs/vendor/event-target-shim/index.d.js +6 -0
  52. package/lib/commonjs/vendor/event-target-shim/index.d.js.map +1 -0
  53. package/lib/commonjs/vendor/event-target-shim/index.js +1221 -0
  54. package/lib/commonjs/vendor/event-target-shim/index.js.map +1 -0
  55. package/lib/module/MediaDevices.js +11 -9
  56. package/lib/module/MediaDevices.js.map +1 -1
  57. package/lib/module/MediaStream.js +13 -8
  58. package/lib/module/MediaStream.js.map +1 -1
  59. package/lib/module/MediaStreamTrack.js +25 -9
  60. package/lib/module/MediaStreamTrack.js.map +1 -1
  61. package/lib/module/MediaStreamTrackEvent.js +1 -1
  62. package/lib/module/MediaStreamTrackEvent.js.map +1 -1
  63. package/lib/module/MessageEvent.js +1 -1
  64. package/lib/module/MessageEvent.js.map +1 -1
  65. package/lib/module/RTCCertificate.js +18 -0
  66. package/lib/module/RTCCertificate.js.map +1 -0
  67. package/lib/module/RTCDataChannel.js +37 -12
  68. package/lib/module/RTCDataChannel.js.map +1 -1
  69. package/lib/module/RTCDataChannelEvent.js +1 -1
  70. package/lib/module/RTCDataChannelEvent.js.map +1 -1
  71. package/lib/module/RTCErrorEvent.js +1 -1
  72. package/lib/module/RTCErrorEvent.js.map +1 -1
  73. package/lib/module/RTCIceCandidateEvent.js +1 -1
  74. package/lib/module/RTCIceCandidateEvent.js.map +1 -1
  75. package/lib/module/RTCPeerConnection.js +103 -21
  76. package/lib/module/RTCPeerConnection.js.map +1 -1
  77. package/lib/module/RTCRtpEncodingParameters.js +17 -1
  78. package/lib/module/RTCRtpEncodingParameters.js.map +1 -1
  79. package/lib/module/RTCTrackEvent.js +1 -1
  80. package/lib/module/RTCTrackEvent.js.map +1 -1
  81. package/lib/module/getDisplayMedia.js +2 -1
  82. package/lib/module/getDisplayMedia.js.map +1 -1
  83. package/lib/module/index.js +5 -3
  84. package/lib/module/index.js.map +1 -1
  85. package/lib/module/vendor/event-target-shim/LICENSE +22 -0
  86. package/lib/module/vendor/event-target-shim/index.d.js +52 -0
  87. package/lib/module/vendor/event-target-shim/index.d.js.map +1 -0
  88. package/lib/module/vendor/event-target-shim/index.js +1221 -0
  89. package/lib/module/vendor/event-target-shim/index.js.map +1 -0
  90. package/lib/typescript/MediaDevices.d.ts +8 -4
  91. package/lib/typescript/MediaStream.d.ts +5 -1
  92. package/lib/typescript/MediaStreamTrack.d.ts +7 -1
  93. package/lib/typescript/MediaStreamTrackEvent.d.ts +1 -1
  94. package/lib/typescript/MessageEvent.d.ts +1 -1
  95. package/lib/typescript/RTCCertificate.d.ts +16 -0
  96. package/lib/typescript/RTCDataChannel.d.ts +13 -1
  97. package/lib/typescript/RTCDataChannelEvent.d.ts +1 -1
  98. package/lib/typescript/RTCErrorEvent.d.ts +1 -1
  99. package/lib/typescript/RTCIceCandidateEvent.d.ts +1 -1
  100. package/lib/typescript/RTCPeerConnection.d.ts +30 -1
  101. package/lib/typescript/RTCRtpEncodingParameters.d.ts +4 -0
  102. package/lib/typescript/RTCTrackEvent.d.ts +1 -1
  103. package/lib/typescript/getDisplayMedia.d.ts +7 -1
  104. package/lib/typescript/index.d.ts +4 -1
  105. package/package.json +2 -3
  106. package/src/MediaDevices.ts +15 -14
  107. package/src/MediaStream.ts +17 -9
  108. package/src/MediaStreamTrack.ts +30 -10
  109. package/src/MediaStreamTrackEvent.ts +2 -2
  110. package/src/MessageEvent.ts +1 -1
  111. package/src/RTCCertificate.ts +24 -0
  112. package/src/RTCDataChannel.ts +49 -14
  113. package/src/RTCDataChannelEvent.ts +2 -2
  114. package/src/RTCErrorEvent.ts +1 -1
  115. package/src/RTCIceCandidateEvent.ts +2 -2
  116. package/src/RTCPeerConnection.ts +138 -24
  117. package/src/RTCRtpEncodingParameters.ts +20 -0
  118. package/src/RTCTrackEvent.ts +1 -1
  119. package/src/getDisplayMedia.ts +9 -2
  120. package/src/index.ts +9 -2
  121. package/src/vendor/event-target-shim/LICENSE +22 -0
  122. package/src/vendor/event-target-shim/index.d.ts +411 -0
  123. package/src/vendor/event-target-shim/index.js +1186 -0
@@ -1,5 +1,7 @@
1
1
  package com.oney.WebRTCModule;
2
2
 
3
+ import android.util.Log;
4
+
3
5
  import androidx.annotation.Nullable;
4
6
  import androidx.core.util.Consumer;
5
7
 
@@ -10,6 +12,8 @@ import com.facebook.react.bridge.WritableMap;
10
12
  import org.webrtc.VideoCapturer;
11
13
 
12
14
  public abstract class AbstractVideoCaptureController {
15
+ private static final String TAG = AbstractVideoCaptureController.class.getSimpleName();
16
+
13
17
  protected int targetWidth;
14
18
  protected int targetHeight;
15
19
  protected int targetFps;
@@ -78,9 +82,7 @@ public abstract class AbstractVideoCaptureController {
78
82
  try {
79
83
  videoCapturer.startCapture(targetWidth, targetHeight, targetFps);
80
84
  } catch (RuntimeException e) {
81
- // XXX This can only fail if we initialize the capturer incorrectly,
82
- // which we don't. Thus, ignore any failures here since we trust
83
- // ourselves.
85
+ Log.e(TAG, "Failed to start capture", e);
84
86
  }
85
87
  }
86
88
 
@@ -4,8 +4,10 @@ import android.app.Activity;
4
4
  import android.content.Context;
5
5
  import android.content.Intent;
6
6
  import android.media.projection.MediaProjectionManager;
7
+ import android.media.projection.MediaProjectionConfig;
7
8
  import android.util.DisplayMetrics;
8
9
  import android.util.Log;
10
+ import android.os.Build;
9
11
 
10
12
  import androidx.core.util.Consumer;
11
13
 
@@ -16,6 +18,7 @@ import com.facebook.react.bridge.Promise;
16
18
  import com.facebook.react.bridge.ReactApplicationContext;
17
19
  import com.facebook.react.bridge.ReadableArray;
18
20
  import com.facebook.react.bridge.ReadableMap;
21
+ import com.facebook.react.bridge.ReadableType;
19
22
  import com.facebook.react.bridge.UiThreadUtil;
20
23
  import com.facebook.react.bridge.WritableArray;
21
24
  import com.facebook.react.bridge.WritableMap;
@@ -31,6 +34,7 @@ import java.util.List;
31
34
  import java.util.Map;
32
35
  import java.util.Objects;
33
36
  import java.util.UUID;
37
+ import java.util.concurrent.TimeUnit;
34
38
  import java.util.stream.Collectors;
35
39
 
36
40
  /**
@@ -59,6 +63,8 @@ class GetUserMediaImpl {
59
63
 
60
64
  private Promise displayMediaPromise;
61
65
  private Intent mediaProjectionPermissionResultData;
66
+ private boolean createConfigForDefaultDisplay = false;
67
+ private float resolutionScale = 1.0f;
62
68
 
63
69
  GetUserMediaImpl(WebRTCModule webRTCModule, ReactApplicationContext reactContext) {
64
70
  this.webRTCModule = webRTCModule;
@@ -77,10 +83,19 @@ class GetUserMediaImpl {
77
83
 
78
84
  mediaProjectionPermissionResultData = data;
79
85
 
80
- ThreadUtils.runOnExecutor(() -> {
81
- MediaProjectionService.launch(activity);
82
- createScreenStream();
83
- });
86
+ MediaProjectionService.launch(activity)
87
+ .orTimeout(10, TimeUnit.SECONDS)
88
+ .whenCompleteAsync((value, error) -> {
89
+ if (error != null) {
90
+ Log.e(TAG, "Failed to start MediaProjection service", error);
91
+ displayMediaPromise.reject("DOMException", "AbortError");
92
+ displayMediaPromise = null;
93
+ mediaProjectionPermissionResultData = null;
94
+ return;
95
+ }
96
+
97
+ createScreenStream();
98
+ }, ThreadUtils.getExecutor());
84
99
  }
85
100
  }
86
101
  });
@@ -264,7 +279,41 @@ class GetUserMediaImpl {
264
279
  }
265
280
  }
266
281
 
267
- void getDisplayMedia(Promise promise) {
282
+ void initializeConstraints(ReadableMap constraints) {
283
+
284
+ // Handle the incoming params
285
+
286
+ ReadableMap androidConstraints = null;
287
+ if (constraints.hasKey("android") && constraints.getType("android") == ReadableType.Map) {
288
+ androidConstraints = constraints.getMap("android");
289
+ }
290
+
291
+ // Default values
292
+ boolean createConfigForDefaultDisplay = false;
293
+ float scale = 1.0f;
294
+
295
+ if (androidConstraints != null) {
296
+ // MediaProjectionConfig need API level 34
297
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE
298
+ && androidConstraints.hasKey("createConfigForDefaultDisplay")
299
+ && androidConstraints.getType("createConfigForDefaultDisplay") == ReadableType.Boolean) {
300
+ createConfigForDefaultDisplay = androidConstraints.getBoolean("createConfigForDefaultDisplay");
301
+ }
302
+ if (androidConstraints.hasKey("resolutionScale")
303
+ && androidConstraints.getType("resolutionScale") == ReadableType.Number) {
304
+ scale = (float) androidConstraints.getDouble("resolutionScale");
305
+ }
306
+ }
307
+
308
+ this.createConfigForDefaultDisplay = createConfigForDefaultDisplay;
309
+ // Force the value in [0, 1]
310
+ this.resolutionScale = Math.max(0.0f, Math.min(1.0f, scale));
311
+
312
+ Log.d(TAG, "initializeConstraints: createConfigForDefaultDisplay=" + this.createConfigForDefaultDisplay
313
+ + " resolutionScale=" + this.resolutionScale);
314
+ }
315
+
316
+ void getDisplayMedia(final ReadableMap constraints, Promise promise) {
268
317
  if (this.displayMediaPromise != null) {
269
318
  promise.reject(new RuntimeException("Another operation is pending."));
270
319
  return;
@@ -276,6 +325,8 @@ class GetUserMediaImpl {
276
325
  return;
277
326
  }
278
327
 
328
+ this.initializeConstraints(constraints);
329
+
279
330
  this.displayMediaPromise = promise;
280
331
 
281
332
  MediaProjectionManager mediaProjectionManager =
@@ -286,8 +337,17 @@ class GetUserMediaImpl {
286
337
  UiThreadUtil.runOnUiThread(new Runnable() {
287
338
  @Override
288
339
  public void run() {
289
- currentActivity.startActivityForResult(
340
+
341
+ if (createConfigForDefaultDisplay == true) {
342
+ //MediaProjectionConfig need API level 34
343
+ //Return mediaProjection which restricts the user to capturing the default display
344
+ currentActivity.startActivityForResult(
345
+ mediaProjectionManager.createScreenCaptureIntent(MediaProjectionConfig.createConfigForDefaultDisplay()), PERMISSION_REQUEST_CODE);
346
+ } else {
347
+ //Return mediaProjection which allows the user to decide which region is captured
348
+ currentActivity.startActivityForResult(
290
349
  mediaProjectionManager.createScreenCaptureIntent(), PERMISSION_REQUEST_CODE);
350
+ }
291
351
  }
292
352
  });
293
353
 
@@ -374,7 +434,7 @@ class GetUserMediaImpl {
374
434
  int width = displayMetrics.widthPixels;
375
435
  int height = displayMetrics.heightPixels;
376
436
  ScreenCaptureController screenCaptureController = new ScreenCaptureController(
377
- reactContext.getCurrentActivity(), width, height, mediaProjectionPermissionResultData);
437
+ reactContext.getCurrentActivity(), width, height, mediaProjectionPermissionResultData, resolutionScale);
378
438
  return createVideoTrack(screenCaptureController);
379
439
  }
380
440
 
@@ -11,6 +11,7 @@ import android.os.IBinder;
11
11
  import android.util.Log;
12
12
 
13
13
  import java.util.Random;
14
+ import java.util.concurrent.CompletableFuture;
14
15
 
15
16
  /**
16
17
  * This class implements an Android {@link Service}, a foreground one specifically, and it's
@@ -24,11 +25,17 @@ public class MediaProjectionService extends Service {
24
25
 
25
26
  static final int NOTIFICATION_ID = new Random().nextInt(99999) + 10000;
26
27
 
27
- public static void launch(Context context) {
28
+ private static volatile CompletableFuture<Void> startFuture;
29
+
30
+ public static CompletableFuture<Void> launch(Context context) {
28
31
  if (!WebRTCModuleOptions.getInstance().enableMediaProjectionService) {
29
- return;
32
+ return CompletableFuture.completedFuture(null);
30
33
  }
31
34
 
35
+ CompletableFuture<Void> future = new CompletableFuture<>();
36
+
37
+ startFuture = future;
38
+
32
39
  MediaProjectionNotification.createNotificationChannel(context);
33
40
  Intent intent = new Intent(context, MediaProjectionService.class);
34
41
  ComponentName componentName;
@@ -43,14 +50,21 @@ public class MediaProjectionService extends Service {
43
50
  // Avoid crashing due to ForegroundServiceStartNotAllowedException (API level 31).
44
51
  // See: https://developer.android.com/guide/components/foreground-services#background-start-restrictions
45
52
  Log.w(TAG, "Media projection service not started", e);
46
- return;
53
+ startFuture = null;
54
+ future.completeExceptionally(e);
55
+
56
+ return future;
47
57
  }
48
58
 
49
59
  if (componentName == null) {
50
60
  Log.w(TAG, "Media projection service not started");
61
+ startFuture = null;
62
+ future.completeExceptionally(new RuntimeException("Media projection service not started"));
51
63
  } else {
52
64
  Log.i(TAG, "Media projection service started");
53
65
  }
66
+
67
+ return future;
54
68
  }
55
69
 
56
70
  public static void abort(Context context) {
@@ -77,6 +91,13 @@ public class MediaProjectionService extends Service {
77
91
  startForeground(NOTIFICATION_ID, notification);
78
92
  }
79
93
 
94
+ CompletableFuture<Void> fut = startFuture;
95
+
96
+ if (fut != null) {
97
+ startFuture = null;
98
+ fut.complete(null);
99
+ }
100
+
80
101
  return START_NOT_STICKY;
81
102
  }
82
103
  }
@@ -25,8 +25,8 @@ public class ScreenCaptureController extends AbstractVideoCaptureController {
25
25
 
26
26
  private final Context context;
27
27
 
28
- public ScreenCaptureController(Context context, int width, int height, Intent mediaProjectionPermissionResultData) {
29
- super(width, height, DEFAULT_FPS);
28
+ public ScreenCaptureController(Context context, int width, int height, Intent mediaProjectionPermissionResultData, float resolutionScale) {
29
+ super((int)(width * resolutionScale), (int)(height * resolutionScale), DEFAULT_FPS);
30
30
 
31
31
  this.mediaProjectionPermissionResultData = mediaProjectionPermissionResultData;
32
32
 
@@ -36,8 +36,8 @@ public class ScreenCaptureController extends AbstractVideoCaptureController {
36
36
  @Override
37
37
  public void onOrientationChanged(int orientation) {
38
38
  DisplayMetrics displayMetrics = DisplayUtils.getDisplayMetrics((Activity) context);
39
- final int width = displayMetrics.widthPixels;
40
- final int height = displayMetrics.heightPixels;
39
+ final int width = (int)(displayMetrics.widthPixels * resolutionScale);
40
+ final int height = (int)(displayMetrics.heightPixels * resolutionScale);
41
41
 
42
42
  // Pivot to the executor thread because videoCapturer.changeCaptureFormat runs in the main
43
43
  // thread and may deadlock.
@@ -150,6 +150,9 @@ public class SerializeUtils {
150
150
  if (encoding.maxBitrateBps != null) {
151
151
  encodingMap.putInt("maxBitrate", encoding.maxBitrateBps);
152
152
  }
153
+ if (encoding.minBitrateBps != null) {
154
+ encodingMap.putInt("minBitrate", encoding.minBitrateBps);
155
+ }
153
156
  if (encoding.maxFramerate != null) {
154
157
  encodingMap.putInt("maxFramerate", encoding.maxFramerate);
155
158
  }
@@ -238,6 +241,7 @@ public class SerializeUtils {
238
241
  RtpParameters.Encoding encoding = encodings.get(i);
239
242
  // Dealing with nullable Integers
240
243
  Integer maxBitrate = encodingUpdate.hasKey("maxBitrate") ? encodingUpdate.getInt("maxBitrate") : null;
244
+ Integer minBitrate = encodingUpdate.hasKey("minBitrate") ? encodingUpdate.getInt("minBitrate") : null;
241
245
  Integer maxFramerate = encodingUpdate.hasKey("maxFramerate") ? encodingUpdate.getInt("maxFramerate") : null;
242
246
  Double scaleResolutionDownBy = encodingUpdate.hasKey("scaleResolutionDownBy")
243
247
  ? encodingUpdate.getDouble("scaleResolutionDownBy")
@@ -246,6 +250,7 @@ public class SerializeUtils {
246
250
  encoding.active = encodingUpdate.getBoolean("active");
247
251
  encoding.rid = encodingUpdate.getString("rid");
248
252
  encoding.maxBitrateBps = maxBitrate;
253
+ encoding.minBitrateBps = minBitrate;
249
254
  encoding.maxFramerate = maxFramerate;
250
255
  encoding.scaleResolutionDownBy = scaleResolutionDownBy;
251
256
  }
@@ -294,6 +299,9 @@ public class SerializeUtils {
294
299
  if (params.hasKey("maxBitrate")) {
295
300
  encoding.maxBitrateBps = params.getInt("maxBitrate");
296
301
  }
302
+ if (params.hasKey("minBitrate")) {
303
+ encoding.minBitrateBps = params.getInt("minBitrate");
304
+ }
297
305
  if (params.hasKey("maxFramerate")) {
298
306
  encoding.maxFramerate = params.getInt("maxFramerate");
299
307
  }
@@ -13,6 +13,14 @@ final class ThreadUtils {
13
13
  */
14
14
  private static final ExecutorService executor = Executors.newSingleThreadExecutor();
15
15
 
16
+ /**
17
+ * Gets the executor used to run all WebRTC PeerConnection APIs.
18
+ * @return ExecutorService.
19
+ */
20
+ public static ExecutorService getExecutor() {
21
+ return executor;
22
+ }
23
+
16
24
  /**
17
25
  * Runs the given {@link Runnable} on the executor.
18
26
  * @param runnable
@@ -24,6 +24,11 @@ import com.facebook.react.modules.core.DeviceEventManagerModule;
24
24
  import com.oney.WebRTCModule.webrtcutils.H264AndSoftwareVideoDecoderFactory;
25
25
  import com.oney.WebRTCModule.webrtcutils.H264AndSoftwareVideoEncoderFactory;
26
26
 
27
+ import java.io.ByteArrayInputStream;
28
+ import java.nio.charset.StandardCharsets;
29
+ import java.security.MessageDigest;
30
+ import java.security.cert.CertificateFactory;
31
+ import java.security.cert.X509Certificate;
27
32
  import org.webrtc.*;
28
33
  import org.webrtc.audio.AudioDeviceModule;
29
34
  import org.webrtc.audio.JavaAudioDeviceModule;
@@ -49,6 +54,9 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
49
54
  private final SparseArray<PeerConnectionObserver> mPeerConnectionObservers;
50
55
  final Map<String, MediaStream> localStreams;
51
56
 
57
+ // Store generated certificates by ID to avoid exposing private keys to JS
58
+ private static final Map<String, RtcCertificatePem> mCertificates = new HashMap<>();
59
+
52
60
  private final GetUserMediaImpl getUserMediaImpl;
53
61
 
54
62
  public WebRTCModule(ReactApplicationContext reactContext) {
@@ -254,7 +262,24 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
254
262
  }
255
263
 
256
264
  // FIXME: peerIdentity of type DOMString (public api)
257
- // FIXME: certificates of type sequence<RTCCertificate> (public api)
265
+
266
+ // certificates (public api)
267
+ if (map.hasKey("certificates") && map.getType("certificates") == ReadableType.Array) {
268
+ ReadableArray certificates = map.getArray("certificates");
269
+ if (certificates.size() > 0) {
270
+ ReadableMap certMap = certificates.getMap(0);
271
+ if (certMap.hasKey("certificateId")) {
272
+ String certId = certMap.getString("certificateId");
273
+ RtcCertificatePem cert;
274
+ synchronized (mCertificates) {
275
+ cert = mCertificates.get(certId);
276
+ }
277
+ if (cert != null) {
278
+ conf.certificate = cert;
279
+ }
280
+ }
281
+ }
282
+ }
258
283
 
259
284
  // iceCandidatePoolSize of type unsigned short, defaulting to 0
260
285
  if (map.hasKey("iceCandidatePoolSize") && map.getType("iceCandidatePoolSize") == ReadableType.Number) {
@@ -395,33 +420,23 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
395
420
  }
396
421
  }
397
422
 
423
+ // Must be called in the executor.
398
424
  MediaStream getStreamForReactTag(String streamReactTag) {
399
- // This function _only_ gets called from WebRTCView, in the UI thread.
400
- // Hence make sure we run this code in the executor or we run at the risk
401
- // of being out of sync.
402
- try {
403
- return (MediaStream) ThreadUtils
404
- .submitToExecutor((Callable<Object>) () -> {
405
- MediaStream stream = localStreams.get(streamReactTag);
425
+ MediaStream stream = localStreams.get(streamReactTag);
406
426
 
407
- if (stream != null) {
408
- return stream;
409
- }
410
-
411
- for (int i = 0, size = mPeerConnectionObservers.size(); i < size; i++) {
412
- PeerConnectionObserver pco = mPeerConnectionObservers.valueAt(i);
413
- stream = pco.remoteStreams.get(streamReactTag);
414
- if (stream != null) {
415
- return stream;
416
- }
417
- }
427
+ if (stream != null) {
428
+ return stream;
429
+ }
418
430
 
419
- return null;
420
- })
421
- .get();
422
- } catch (ExecutionException | InterruptedException e) {
423
- return null;
431
+ for (int i = 0, size = mPeerConnectionObservers.size(); i < size; i++) {
432
+ PeerConnectionObserver pco = mPeerConnectionObservers.valueAt(i);
433
+ stream = pco.remoteStreams.get(streamReactTag);
434
+ if (stream != null) {
435
+ return stream;
436
+ }
424
437
  }
438
+
439
+ return null;
425
440
  }
426
441
 
427
442
  public MediaStreamTrack getTrack(int pcId, String trackId) {
@@ -769,8 +784,8 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
769
784
  }
770
785
 
771
786
  @ReactMethod
772
- public void getDisplayMedia(Promise promise) {
773
- ThreadUtils.runOnExecutor(() -> getUserMediaImpl.getDisplayMedia(promise));
787
+ public void getDisplayMedia(ReadableMap constraints, Promise promise) {
788
+ ThreadUtils.runOnExecutor(() -> getUserMediaImpl.getDisplayMedia(constraints, promise));
774
789
  }
775
790
 
776
791
  @ReactMethod
@@ -1418,6 +1433,77 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
1418
1433
  });
1419
1434
  }
1420
1435
 
1436
+ @ReactMethod
1437
+ public void generateCertificate(ReadableMap options, Promise promise) {
1438
+ ThreadUtils.runOnExecutor(() -> {
1439
+ try {
1440
+ PeerConnection.KeyType keyType = PeerConnection.KeyType.ECDSA;
1441
+ long expires = 2592000L; // Default 30 days
1442
+
1443
+ if (options.hasKey("keyType")) {
1444
+ String keyTypeStr = options.getString("keyType");
1445
+ if ("RSA".equals(keyTypeStr)) {
1446
+ keyType = PeerConnection.KeyType.RSA;
1447
+ } else if ("ECDSA".equals(keyTypeStr)) {
1448
+ keyType = PeerConnection.KeyType.ECDSA;
1449
+ }
1450
+ }
1451
+
1452
+ if (options.hasKey("expires")) {
1453
+ expires = (long) options.getDouble("expires");
1454
+ }
1455
+
1456
+ RtcCertificatePem cert = RtcCertificatePem.generateCertificate(keyType, expires);
1457
+ String certId = java.util.UUID.randomUUID().toString();
1458
+ synchronized (mCertificates) {
1459
+ mCertificates.put(certId, cert);
1460
+ }
1461
+
1462
+ WritableMap params = Arguments.createMap();
1463
+ params.putString("certificateId", certId);
1464
+ // Return expires as millis since epoch
1465
+ params.putDouble("expires", System.currentTimeMillis() + expires * 1000);
1466
+
1467
+ // Calculate fingerprints
1468
+ WritableArray fingerprints = Arguments.createArray();
1469
+
1470
+ try {
1471
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
1472
+ ByteArrayInputStream is = new ByteArrayInputStream(cert.certificate.getBytes(StandardCharsets.UTF_8));
1473
+ X509Certificate x509Cert = (X509Certificate) cf.generateCertificate(is);
1474
+
1475
+ MessageDigest digest = MessageDigest.getInstance("SHA-256");
1476
+ byte[] hash = digest.digest(x509Cert.getEncoded());
1477
+
1478
+ WritableMap fingerprint = Arguments.createMap();
1479
+ fingerprint.putString("algorithm", "sha-256");
1480
+ fingerprint.putString("value", bytesToHex(hash));
1481
+ fingerprints.pushMap(fingerprint);
1482
+ } catch (Exception e) {
1483
+ Log.e(TAG, "Failed to calculate fingerprint: " + e.getMessage());
1484
+ }
1485
+
1486
+ params.putArray("fingerprints", fingerprints);
1487
+
1488
+ promise.resolve(params);
1489
+ } catch (Exception e) {
1490
+ promise.reject(e);
1491
+ }
1492
+ });
1493
+ }
1494
+
1495
+ private String bytesToHex(byte[] bytes) {
1496
+ StringBuilder sb = new StringBuilder();
1497
+ for (byte b : bytes) {
1498
+ sb.append(String.format("%02x", b));
1499
+ sb.append(":");
1500
+ }
1501
+ if (sb.length() > 0) {
1502
+ sb.setLength(sb.length() - 1);
1503
+ }
1504
+ return sb.toString();
1505
+ }
1506
+
1421
1507
  @ReactMethod
1422
1508
  public void addListener(String eventName) {
1423
1509
  // Keep: Required for RN built in Event Emitter Calls.
@@ -173,28 +173,53 @@ public class WebRTCView extends ViewGroup {
173
173
  surfaceViewRenderer.clearImage();
174
174
  }
175
175
 
176
- private VideoTrack getVideoTrackForStreamURL(String streamURL) {
177
- VideoTrack videoTrack = null;
176
+ /**
177
+ * Asynchronously retrieves the VideoTrack for the given streamURL.
178
+ * This method avoids blocking the UI thread by performing the lookup
179
+ * on the WebRTC executor thread and posting the result back to the UI thread.
180
+ *
181
+ * @param streamURL The stream URL to lookup
182
+ * @param callback Callback invoked on UI thread with the VideoTrack (or null if not found)
183
+ */
184
+ private void getVideoTrackForStreamURL(String streamURL, java.util.function.Consumer<VideoTrack> callback) {
185
+ if (streamURL == null) {
186
+ callback.accept(null);
187
+ return;
188
+ }
178
189
 
179
- if (streamURL != null) {
180
- ReactContext reactContext = (ReactContext) getContext();
181
- WebRTCModule module = reactContext.getNativeModule(WebRTCModule.class);
182
- MediaStream stream = module.getStreamForReactTag(streamURL);
190
+ ReactContext reactContext = (ReactContext) getContext();
191
+ WebRTCModule module = reactContext.getNativeModule(WebRTCModule.class);
183
192
 
184
- if (stream != null) {
185
- List<VideoTrack> videoTracks = stream.videoTracks;
193
+ // Submit lookup to executor thread to avoid blocking UI thread
194
+ ThreadUtils.runOnExecutor(() -> {
195
+ try {
196
+ MediaStream stream = module.getStreamForReactTag(streamURL);
197
+ if (stream == null) {
198
+ Log.w(TAG, "Stream not found for URL: " + streamURL);
199
+ post(() -> callback.accept(null));
200
+ return;
201
+ }
186
202
 
203
+ VideoTrack videoTrack = null;
204
+ List<VideoTrack> videoTracks = stream.videoTracks;
187
205
  if (!videoTracks.isEmpty()) {
188
206
  videoTrack = videoTracks.get(0);
189
207
  }
190
- }
191
208
 
192
- if (videoTrack == null) {
193
- Log.w(TAG, "No video stream for react tag: " + streamURL);
194
- }
195
- }
209
+ if (videoTrack == null) {
210
+ Log.w(TAG, "No video stream for react tag: " + streamURL);
211
+ post(() -> callback.accept(null));
212
+ return;
213
+ }
196
214
 
197
- return videoTrack;
215
+ // Post result back to UI thread
216
+ final VideoTrack result = videoTrack;
217
+ post(() -> callback.accept(result));
218
+ } catch (Throwable tr) {
219
+ Log.e(TAG, "Error getting video track for stream URL: " + streamURL, tr);
220
+ post(() -> callback.accept(null));
221
+ }
222
+ });
198
223
  }
199
224
 
200
225
  @Override
@@ -452,20 +477,23 @@ public class WebRTCView extends ViewGroup {
452
477
  * this {@code WebRTCView} or {@code null}.
453
478
  */
454
479
  void setStreamURL(String streamURL) {
455
- // Is the value of this.streamURL really changing?
456
- if (!Objects.equals(streamURL, this.streamURL)) {
457
- // XXX The value of this.streamURL is really changing. Before
458
- // realizing/applying the change, let go of the old videoTrack. Of
459
- // course, that is only necessary if the value of videoTrack will
460
- // really change. Please note though that letting go of the old
461
- // videoTrack before assigning to this.streamURL is vital;
462
- // otherwise, removeRendererFromVideoTrack will fail to remove the
463
- // old videoTrack from the associated videoRenderer, two
464
- // VideoTracks (the old and the new) may start rendering and, most
465
- // importantly the videoRender may eventually crash when the old
466
- // videoTrack is disposed.
467
- VideoTrack videoTrack = getVideoTrackForStreamURL(streamURL);
480
+ Log.d(TAG, "Set stream URL " + streamURL + " current: " + this.streamURL);
481
+ if (Objects.equals(streamURL, this.streamURL)) {
482
+ return;
483
+ }
468
484
 
485
+ // The value of this.streamURL is really changing. Before
486
+ // realizing/applying the change, let go of the old videoTrack. Of
487
+ // course, that is only necessary if the value of videoTrack will
488
+ // really change. Please note though that letting go of the old
489
+ // videoTrack before assigning to this.streamURL is vital;
490
+ // otherwise, removeRendererFromVideoTrack will fail to remove the
491
+ // old videoTrack from the associated videoRenderer, two
492
+ // VideoTracks (the old and the new) may start rendering and, most
493
+ // importantly the videoRender may eventually crash when the old
494
+ // videoTrack is disposed.
495
+ getVideoTrackForStreamURL(streamURL, videoTrack -> {
496
+ Log.d(TAG, "Got video track for stream URL " + streamURL + " -> " + videoTrack);
469
497
  if (this.videoTrack != videoTrack) {
470
498
  setVideoTrack(null);
471
499
  }
@@ -475,7 +503,7 @@ public class WebRTCView extends ViewGroup {
475
503
  // After realizing/applying the change in the value of
476
504
  // this.streamURL, reflect it on the value of videoTrack.
477
505
  setVideoTrack(videoTrack);
478
- }
506
+ });
479
507
  }
480
508
 
481
509
  /**
@@ -1,9 +1,12 @@
1
1
  #import <React/RCTLog.h>
2
+ #import <WebRTC/RTCCertificate.h>
2
3
  #import <WebRTC/RTCDataChannelConfiguration.h>
3
4
  #import <WebRTC/RTCIceServer.h>
4
5
  #import <WebRTC/RTCSessionDescription.h>
5
6
  #import "RCTConvert+WebRTC.h"
6
7
 
8
+ #import "WebRTCModule+RTCPeerConnection.h"
9
+
7
10
  @implementation RCTConvert (WebRTC)
8
11
 
9
12
  + (RTCSessionDescription *)RTCSessionDescription:(id)json {
@@ -172,6 +175,22 @@
172
175
  }
173
176
  }
174
177
 
178
+ if (json[@"certificates"] != nil && [json[@"certificates"] isKindOfClass:[NSArray class]]) {
179
+ NSArray *certs = json[@"certificates"];
180
+ if (certs.count > 0) {
181
+ id certInfo = certs[0];
182
+ if ([certInfo isKindOfClass:[NSDictionary class]]) {
183
+ NSString *certId = certInfo[@"certificateId"];
184
+ if (certId) {
185
+ RTCCertificate *cert = [WebRTCModule getCertificate:certId];
186
+ if (cert) {
187
+ config.certificate = cert;
188
+ }
189
+ }
190
+ }
191
+ }
192
+ }
193
+
175
194
  return config;
176
195
  }
177
196