react-native-webrtc 124.0.3 → 124.0.5

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 (91) hide show
  1. package/android/build.gradle +2 -1
  2. package/android/consumer-rules.pro +2 -0
  3. package/android/src/main/AndroidManifest.xml +0 -5
  4. package/android/src/main/java/com/oney/WebRTCModule/AbstractVideoCaptureController.java +43 -10
  5. package/android/src/main/java/com/oney/WebRTCModule/CameraCaptureController.java +210 -66
  6. package/android/src/main/java/com/oney/WebRTCModule/GetUserMediaImpl.java +23 -8
  7. package/android/src/main/java/com/oney/WebRTCModule/ScreenCaptureController.java +5 -0
  8. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +4 -2
  9. package/android/src/main/java/org/webrtc/Camera1Helper.java +55 -0
  10. package/android/src/main/java/org/webrtc/Camera2Helper.java +51 -0
  11. package/ios/RCTWebRTC/CaptureController.h +3 -0
  12. package/ios/RCTWebRTC/CaptureController.m +13 -0
  13. package/ios/RCTWebRTC/I420Converter.h +21 -0
  14. package/ios/RCTWebRTC/I420Converter.m +156 -0
  15. package/ios/RCTWebRTC/PIPController.h +24 -0
  16. package/ios/RCTWebRTC/PIPController.m +242 -0
  17. package/ios/RCTWebRTC/RTCVideoViewManager.h +23 -0
  18. package/ios/RCTWebRTC/RTCVideoViewManager.m +105 -23
  19. package/ios/RCTWebRTC/SampleBufferVideoCallView.h +12 -0
  20. package/ios/RCTWebRTC/SampleBufferVideoCallView.m +180 -0
  21. package/ios/RCTWebRTC/ScreenCaptureController.m +8 -0
  22. package/ios/RCTWebRTC/VideoCaptureController.h +2 -0
  23. package/ios/RCTWebRTC/VideoCaptureController.m +96 -30
  24. package/ios/RCTWebRTC/WebRTCModule+RTCAudioSession.m +20 -0
  25. package/ios/RCTWebRTC/WebRTCModule+RTCMediaStream.m +47 -15
  26. package/ios/RCTWebRTC/WebRTCModuleOptions.h +1 -0
  27. package/ios/RCTWebRTC.xcodeproj/project.pbxproj +4 -0
  28. package/lib/commonjs/Constraints.js +2 -0
  29. package/lib/commonjs/Constraints.js.map +1 -0
  30. package/lib/commonjs/MediaDevices.js.map +1 -1
  31. package/lib/commonjs/MediaStreamTrack.js +26 -3
  32. package/lib/commonjs/MediaStreamTrack.js.map +1 -1
  33. package/lib/commonjs/RTCAudioSession.js +33 -0
  34. package/lib/commonjs/RTCAudioSession.js.map +1 -0
  35. package/lib/commonjs/RTCPIPView.js +42 -0
  36. package/lib/commonjs/RTCPIPView.js.map +1 -0
  37. package/lib/commonjs/RTCPeerConnection.js +5 -2
  38. package/lib/commonjs/RTCPeerConnection.js.map +1 -1
  39. package/lib/commonjs/RTCRtpParameters.js +4 -3
  40. package/lib/commonjs/RTCRtpParameters.js.map +1 -1
  41. package/lib/commonjs/RTCRtpSendParameters.js +2 -1
  42. package/lib/commonjs/RTCRtpSendParameters.js.map +1 -1
  43. package/lib/commonjs/RTCView.js.map +1 -1
  44. package/lib/commonjs/getUserMedia.js.map +1 -1
  45. package/lib/commonjs/index.js +28 -0
  46. package/lib/commonjs/index.js.map +1 -1
  47. package/lib/module/Constraints.js +2 -0
  48. package/lib/module/Constraints.js.map +1 -0
  49. package/lib/module/MediaDevices.js.map +1 -1
  50. package/lib/module/MediaStreamTrack.js +27 -4
  51. package/lib/module/MediaStreamTrack.js.map +1 -1
  52. package/lib/module/RTCAudioSession.js +26 -0
  53. package/lib/module/RTCAudioSession.js.map +1 -0
  54. package/lib/module/RTCPIPView.js +30 -0
  55. package/lib/module/RTCPIPView.js.map +1 -0
  56. package/lib/module/RTCPeerConnection.js +5 -2
  57. package/lib/module/RTCPeerConnection.js.map +1 -1
  58. package/lib/module/RTCRtpParameters.js +4 -3
  59. package/lib/module/RTCRtpParameters.js.map +1 -1
  60. package/lib/module/RTCRtpSendParameters.js +2 -1
  61. package/lib/module/RTCRtpSendParameters.js.map +1 -1
  62. package/lib/module/RTCView.js.map +1 -1
  63. package/lib/module/getUserMedia.js.map +1 -1
  64. package/lib/module/index.js +3 -1
  65. package/lib/module/index.js.map +1 -1
  66. package/lib/typescript/Constraints.d.ts +19 -0
  67. package/lib/typescript/MediaDevices.d.ts +2 -1
  68. package/lib/typescript/MediaStreamTrack.d.ts +26 -5
  69. package/lib/typescript/RTCAudioSession.d.ts +10 -0
  70. package/lib/typescript/RTCPIPView.d.ts +14 -0
  71. package/lib/typescript/RTCRtpParameters.d.ts +6 -2
  72. package/lib/typescript/RTCRtpSendParameters.d.ts +6 -2
  73. package/lib/typescript/RTCView.d.ts +44 -1
  74. package/lib/typescript/getUserMedia.d.ts +4 -4
  75. package/lib/typescript/index.d.ts +5 -3
  76. package/package.json +2 -1
  77. package/src/.eslintrc.cjs +1 -1
  78. package/src/Constraints.ts +21 -0
  79. package/src/MediaDevices.ts +2 -2
  80. package/src/MediaStreamTrack.ts +41 -7
  81. package/src/RTCAudioSession.ts +25 -0
  82. package/src/RTCPIPView.tsx +46 -0
  83. package/src/RTCPeerConnection.ts +6 -2
  84. package/src/RTCRtpParameters.ts +6 -5
  85. package/src/RTCRtpSendParameters.ts +5 -5
  86. package/src/RTCView.ts +50 -2
  87. package/src/getUserMedia.ts +4 -3
  88. package/src/index.ts +12 -3
  89. package/tsconfig.json +3 -1
  90. package/ios/RCTWebRTC.xcodeproj/xcuserdata/saghul.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
  91. package/ios/RCTWebRTC.xcworkspace/xcuserdata/saghul.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
@@ -14,10 +14,11 @@ android {
14
14
  buildToolsVersion safeExtGet('buildToolsVersion', "23.0.1")
15
15
 
16
16
  defaultConfig {
17
- minSdkVersion safeExtGet('minSdkVersion', 24)
17
+ minSdkVersion 24
18
18
  targetSdkVersion safeExtGet('targetSdkVersion', 24)
19
19
  versionCode 1
20
20
  versionName "1.0"
21
+ consumerProguardFiles 'consumer-rules.pro'
21
22
  }
22
23
 
23
24
  // WebRTC requires Java 8 features
@@ -0,0 +1,2 @@
1
+ # WebRTC
2
+ -keep class org.webrtc.** { *; }
@@ -8,9 +8,4 @@
8
8
  android:foregroundServiceType="mediaProjection">
9
9
  </service>
10
10
  </application>
11
- <uses-feature
12
- android:name="android.hardware.usb.host"
13
- android:required="false"
14
- tools:node="replace"
15
- />
16
11
  </manifest>
@@ -1,11 +1,22 @@
1
1
  package com.oney.WebRTCModule;
2
2
 
3
+ import androidx.annotation.Nullable;
4
+ import androidx.core.util.Consumer;
5
+
6
+ import com.facebook.react.bridge.Arguments;
7
+ import com.facebook.react.bridge.ReadableMap;
8
+ import com.facebook.react.bridge.WritableMap;
9
+
3
10
  import org.webrtc.VideoCapturer;
4
11
 
5
12
  public abstract class AbstractVideoCaptureController {
6
- private final int width;
7
- private final int height;
8
- private final int fps;
13
+ protected int targetWidth;
14
+ protected int targetHeight;
15
+ protected int targetFps;
16
+
17
+ protected int actualWidth;
18
+ protected int actualHeight;
19
+ protected int actualFps;
9
20
 
10
21
  /**
11
22
  * {@link VideoCapturer} which this controller manages.
@@ -15,15 +26,21 @@ public abstract class AbstractVideoCaptureController {
15
26
  protected CapturerEventsListener capturerEventsListener;
16
27
 
17
28
  public AbstractVideoCaptureController(int width, int height, int fps) {
18
- this.width = width;
19
- this.height = height;
20
- this.fps = fps;
29
+ this.targetWidth = width;
30
+ this.targetHeight = height;
31
+ this.targetFps = fps;
32
+ this.actualWidth = width;
33
+ this.actualHeight = height;
34
+ this.actualFps = fps;
21
35
  }
22
36
 
23
37
  public void initializeVideoCapturer() {
24
38
  videoCapturer = createVideoCapturer();
25
39
  }
26
40
 
41
+ @Nullable
42
+ public abstract String getDeviceId();
43
+
27
44
  public void dispose() {
28
45
  if (videoCapturer != null) {
29
46
  videoCapturer.dispose();
@@ -32,15 +49,25 @@ public abstract class AbstractVideoCaptureController {
32
49
  }
33
50
 
34
51
  public int getHeight() {
35
- return height;
52
+ return actualHeight;
36
53
  }
37
54
 
38
55
  public int getWidth() {
39
- return width;
56
+ return actualWidth;
40
57
  }
41
58
 
42
59
  public int getFrameRate() {
43
- return fps;
60
+ return actualFps;
61
+ }
62
+
63
+ public WritableMap getSettings() {
64
+ WritableMap settings = Arguments.createMap();
65
+ settings.putString("deviceId", getDeviceId());
66
+ settings.putString("groupId", "");
67
+ settings.putInt("height", getHeight());
68
+ settings.putInt("width", getWidth());
69
+ settings.putInt("frameRate", getFrameRate());
70
+ return settings;
44
71
  }
45
72
 
46
73
  public VideoCapturer getVideoCapturer() {
@@ -49,7 +76,7 @@ public abstract class AbstractVideoCaptureController {
49
76
 
50
77
  public void startCapture() {
51
78
  try {
52
- videoCapturer.startCapture(width, height, fps);
79
+ videoCapturer.startCapture(targetWidth, targetHeight, targetFps);
53
80
  } catch (RuntimeException e) {
54
81
  // XXX This can only fail if we initialize the capturer incorrectly,
55
82
  // which we don't. Thus, ignore any failures here since we trust
@@ -66,6 +93,12 @@ public abstract class AbstractVideoCaptureController {
66
93
  }
67
94
  }
68
95
 
96
+ public void applyConstraints(ReadableMap constraints, @Nullable Consumer<Exception> onFinishedCallback) {
97
+ if (onFinishedCallback != null) {
98
+ onFinishedCallback.accept(new UnsupportedOperationException("This video track does not support applyConstraints."));
99
+ }
100
+ }
101
+
69
102
  public void setCapturerEventsListener(CapturerEventsListener listener) {
70
103
  this.capturerEventsListener = listener;
71
104
  }
@@ -1,15 +1,27 @@
1
1
  package com.oney.WebRTCModule;
2
2
 
3
+ import android.content.Context;
4
+ import android.hardware.camera2.CameraManager;
3
5
  import android.util.Log;
6
+ import android.util.Pair;
7
+ import androidx.annotation.Nullable;
8
+ import androidx.core.util.Consumer;
4
9
 
5
10
  import com.facebook.react.bridge.ReadableMap;
11
+ import com.facebook.react.bridge.WritableMap;
6
12
 
13
+ import org.webrtc.Camera1Capturer;
14
+ import org.webrtc.Camera1Helper;
15
+ import org.webrtc.Camera2Capturer;
16
+ import org.webrtc.Camera2Helper;
7
17
  import org.webrtc.CameraEnumerator;
8
18
  import org.webrtc.CameraVideoCapturer;
19
+ import org.webrtc.Size;
9
20
  import org.webrtc.VideoCapturer;
10
21
 
11
22
  import java.util.ArrayList;
12
23
  import java.util.List;
24
+ import java.util.Objects;
13
25
 
14
26
  public class CameraCaptureController extends AbstractVideoCaptureController {
15
27
  /**
@@ -19,54 +31,170 @@ public class CameraCaptureController extends AbstractVideoCaptureController {
19
31
 
20
32
  private boolean isFrontFacing;
21
33
 
34
+ /**
35
+ * Equivalent to the camera index as a String
36
+ */
37
+ @Nullable
38
+ private String currentDeviceId;
39
+
40
+ private final Context context;
22
41
  private final CameraEnumerator cameraEnumerator;
23
- private final ReadableMap constraints;
42
+ private ReadableMap constraints;
24
43
 
25
44
  /**
26
45
  * The {@link CameraEventsHandler} used with
27
- * {@link CameraEnumerator#createCapturer}. Cached because the
28
- * implementation does not do anything but logging unspecific to the camera
29
- * device's name anyway.
46
+ * {@link CameraEnumerator#createCapturer}.
30
47
  */
31
- private final CameraEventsHandler cameraEventsHandler = new CameraEventsHandler();
48
+ private final CameraEventsHandler cameraEventsHandler = new CameraEventsHandler() {
49
+
50
+ @Override
51
+ public void onCameraOpening(String cameraName) {
52
+ super.onCameraOpening(cameraName);
53
+ int cameraIndex = findCameraIndex(cameraName);
54
+ updateActualSize(cameraIndex, cameraName, videoCapturer);
55
+ CameraCaptureController.this.currentDeviceId = cameraIndex == -1 ? null : String.valueOf(cameraIndex);
56
+ }
57
+ };
32
58
 
33
- public CameraCaptureController(CameraEnumerator cameraEnumerator, ReadableMap constraints) {
59
+ public CameraCaptureController(Context context, CameraEnumerator cameraEnumerator, ReadableMap constraints) {
34
60
  super(constraints.getInt("width"), constraints.getInt("height"), constraints.getInt("frameRate"));
35
61
 
62
+ this.context = context;
36
63
  this.cameraEnumerator = cameraEnumerator;
37
64
  this.constraints = constraints;
38
65
  }
39
66
 
40
- public void switchCamera() {
41
- if (videoCapturer instanceof CameraVideoCapturer) {
42
- CameraVideoCapturer capturer = (CameraVideoCapturer) videoCapturer;
43
- String[] deviceNames = cameraEnumerator.getDeviceNames();
44
- int deviceCount = deviceNames.length;
67
+ @Nullable
68
+ @Override
69
+ public String getDeviceId() {
70
+ return currentDeviceId;
71
+ }
45
72
 
46
- // Nothing to switch to.
47
- if (deviceCount < 2) {
48
- return;
73
+ private int findCameraIndex(String cameraName) {
74
+ String[] deviceNames = cameraEnumerator.getDeviceNames();
75
+ for (int i = 0; i < deviceNames.length; i++) {
76
+ if (Objects.equals(deviceNames[i], cameraName)) {
77
+ return i;
49
78
  }
79
+ }
80
+ return -1;
81
+ }
50
82
 
51
- // The usual case.
52
- if (deviceCount == 2) {
53
- capturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
54
- @Override
55
- public void onCameraSwitchDone(boolean b) {
56
- isFrontFacing = b;
57
- }
83
+ @Override
84
+ public WritableMap getSettings() {
85
+ WritableMap settings = super.getSettings();
86
+ settings.putString("facingMode", isFrontFacing ? "user" : "environment");
87
+ return settings;
88
+ }
58
89
 
59
- @Override
60
- public void onCameraSwitchError(String s) {
61
- Log.e(TAG, "Error switching camera: " + s);
62
- }
63
- });
64
- return;
90
+ @Override
91
+ public void applyConstraints(ReadableMap constraints, @Nullable Consumer<Exception> onFinishedCallback) {
92
+ ReadableMap oldConstraints = this.constraints;
93
+ int oldTargetWidth = this.targetWidth;
94
+ int oldTargetHeight = this.targetHeight;
95
+ int oldTargetFps = this.targetFps;
96
+
97
+ // Don't save constraints yet, since we may fail to find a fit.
98
+ Runnable saveConstraints = () -> {
99
+ this.constraints = constraints;
100
+ this.targetWidth = constraints.getInt("width");
101
+ this.targetHeight = constraints.getInt("height");
102
+ this.targetFps = constraints.getInt("frameRate");
103
+ };
104
+
105
+ if (videoCapturer == null) {
106
+ // No existing capturer, just let it initialize normally.
107
+ saveConstraints.run();
108
+ if (onFinishedCallback != null) {
109
+ onFinishedCallback.accept(null);
110
+ }
111
+ return;
112
+ }
113
+
114
+ // Find target camera to switch to.
115
+ String[] deviceNames = cameraEnumerator.getDeviceNames();
116
+ final String deviceId = ReactBridgeUtil.getMapStrValue(constraints, "deviceId");
117
+ final String facingMode = ReactBridgeUtil.getMapStrValue(constraints, "facingMode");
118
+ int cameraIndex = -1;
119
+ String cameraName = null;
120
+
121
+ // If deviceId is specified, then it takes precedence over facingMode.
122
+ if (deviceId != null) {
123
+ try {
124
+ cameraIndex = Integer.parseInt(deviceId);
125
+ cameraName = deviceNames[cameraIndex];
126
+ } catch (Exception e) {
127
+ Log.d(TAG, "failed to find device with id: " + deviceId);
65
128
  }
129
+ }
130
+
131
+
132
+ // Otherwise, use facingMode (defaulting to front/user facing).
133
+ if (cameraName == null) {
134
+ cameraIndex = -1;
135
+ final boolean isFrontFacing = facingMode == null || facingMode.equals("user");
136
+ for (String name : deviceNames) {
137
+ cameraIndex++;
138
+ if (cameraEnumerator.isFrontFacing(name) == isFrontFacing) {
139
+ cameraName = name;
140
+ break;
141
+ }
142
+ }
143
+ }
66
144
 
67
- // If we are here the device has more than 2 cameras. Cycle through them
68
- // and switch to the first one of the desired facing mode.
69
- switchCamera(!isFrontFacing, deviceCount);
145
+ if (cameraName == null) {
146
+ if (onFinishedCallback != null) {
147
+ onFinishedCallback.accept(new Exception("OverconstrainedError: could not find camera with deviceId: " + deviceId + " or facingMode: " + facingMode));
148
+ }
149
+ return;
150
+ }
151
+
152
+ // For lambda reference
153
+ final int finalCameraIndex = cameraIndex;
154
+ final String finalCameraName = cameraName;
155
+ boolean shouldSwitchCamera = false;
156
+ try {
157
+ int currentCameraIndex = Integer.parseInt(currentDeviceId);
158
+ shouldSwitchCamera = cameraIndex != currentCameraIndex;
159
+ } catch (Exception e) {
160
+ shouldSwitchCamera = true;
161
+ Log.d(TAG, "Forcing camera switch, couldn't parse current device id: " + currentDeviceId);
162
+ }
163
+
164
+ CameraVideoCapturer capturer = (CameraVideoCapturer) videoCapturer;
165
+ Runnable changeFormatIfNeededAndFinish = () -> {
166
+ saveConstraints.run();
167
+ if (targetWidth != oldTargetWidth ||
168
+ targetHeight != oldTargetHeight ||
169
+ targetFps != oldTargetFps) {
170
+ updateActualSize(finalCameraIndex, finalCameraName, videoCapturer);
171
+ capturer.changeCaptureFormat(targetWidth, targetHeight, targetFps);
172
+ }
173
+ if (onFinishedCallback != null) {
174
+ onFinishedCallback.accept(null);
175
+ }
176
+ };
177
+
178
+ if (shouldSwitchCamera) {
179
+ capturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
180
+ @Override
181
+ public void onCameraSwitchDone(boolean isFrontCamera) {
182
+ CameraCaptureController.this.isFrontFacing = isFrontCamera;
183
+ changeFormatIfNeededAndFinish.run();
184
+ }
185
+
186
+ @Override
187
+ public void onCameraSwitchError(String s) {
188
+ Exception e = new Exception("Error switching camera: " + s);
189
+ Log.e(TAG, "OnCameraSwitchError", e);
190
+ if (onFinishedCallback != null) {
191
+ onFinishedCallback.accept(e);
192
+ }
193
+ }
194
+ }, cameraName);
195
+ } else {
196
+ // No camera switch needed, just change format if needed.
197
+ changeFormatIfNeededAndFinish.run();
70
198
  }
71
199
  }
72
200
 
@@ -75,36 +203,30 @@ public class CameraCaptureController extends AbstractVideoCaptureController {
75
203
  String deviceId = ReactBridgeUtil.getMapStrValue(this.constraints, "deviceId");
76
204
  String facingMode = ReactBridgeUtil.getMapStrValue(this.constraints, "facingMode");
77
205
 
78
- return createVideoCapturer(deviceId, facingMode);
79
- }
206
+ CreateCapturerResult result = createVideoCapturer(deviceId, facingMode);
207
+ if(result == null) {
208
+ return null;
209
+ }
80
210
 
81
- /**
82
- * Helper function which tries to switch cameras until the desired facing mode is found.
83
- *
84
- * @param desiredFrontFacing - The desired front facing value.
85
- * @param tries - How many times to try switching.
86
- */
87
- private void switchCamera(boolean desiredFrontFacing, int tries) {
88
- CameraVideoCapturer capturer = (CameraVideoCapturer) videoCapturer;
211
+ updateActualSize(result.cameraIndex, result.cameraName, result.videoCapturer);
89
212
 
90
- capturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
91
- @Override
92
- public void onCameraSwitchDone(boolean b) {
93
- if (b != desiredFrontFacing) {
94
- int newTries = tries - 1;
95
- if (newTries > 0) {
96
- switchCamera(desiredFrontFacing, newTries);
97
- }
98
- } else {
99
- isFrontFacing = desiredFrontFacing;
100
- }
101
- }
213
+ return result.videoCapturer;
214
+ }
102
215
 
103
- @Override
104
- public void onCameraSwitchError(String s) {
105
- Log.e(TAG, "Error switching camera: " + s);
106
- }
107
- });
216
+ private void updateActualSize(int cameraIndex, String cameraName, VideoCapturer videoCapturer) {
217
+ // Find actual capture format.
218
+ Size actualSize = null;
219
+ if (videoCapturer instanceof Camera1Capturer) {
220
+ actualSize = Camera1Helper.findClosestCaptureFormat(cameraIndex, targetWidth, targetHeight);
221
+ } else if (videoCapturer instanceof Camera2Capturer) {
222
+ CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
223
+ actualSize = Camera2Helper.findClosestCaptureFormat(cameraManager, cameraName, targetWidth, targetHeight);
224
+ }
225
+
226
+ if (actualSize != null) {
227
+ actualWidth = actualSize.width;
228
+ actualHeight = actualSize.height;
229
+ }
108
230
  }
109
231
 
110
232
  /**
@@ -117,17 +239,19 @@ public class CameraCaptureController extends AbstractVideoCaptureController {
117
239
  * @param facingMode the facing of the requested video source such as
118
240
  * {@code user} and {@code environment}. If {@code null}, "user" is
119
241
  * presumed.
120
- * @return a {@code VideoCapturer} satisfying the {@code facingMode} or
121
- * {@code deviceId} constraint
242
+ * @return a pair containing the deviceId and {@code VideoCapturer} satisfying the {@code facingMode} or
243
+ * {@code deviceId} constraint, or null.
122
244
  */
123
- private VideoCapturer createVideoCapturer(String deviceId, String facingMode) {
245
+ @Nullable
246
+ private CreateCapturerResult createVideoCapturer(String deviceId, String facingMode) {
124
247
  String[] deviceNames = cameraEnumerator.getDeviceNames();
125
248
  List<String> failedDevices = new ArrayList<>();
126
249
 
127
250
  String cameraName = null;
251
+ int cameraIndex = -1;
128
252
  try {
129
- int index = Integer.parseInt(deviceId);
130
- cameraName = deviceNames[index];
253
+ cameraIndex = Integer.parseInt(deviceId);
254
+ cameraName = deviceNames[cameraIndex];
131
255
  } catch (Exception e) {
132
256
  Log.d(TAG, "failed to find device with id: " + deviceId);
133
257
  }
@@ -139,7 +263,8 @@ public class CameraCaptureController extends AbstractVideoCaptureController {
139
263
  if (videoCapturer != null) {
140
264
  Log.d(TAG, message + " succeeded");
141
265
  this.isFrontFacing = cameraEnumerator.isFrontFacing(cameraName);
142
- return videoCapturer;
266
+ this.currentDeviceId = String.valueOf(cameraIndex);
267
+ return new CreateCapturerResult(cameraIndex, cameraName, videoCapturer);
143
268
  } else {
144
269
  // fallback to facingMode
145
270
  Log.d(TAG, message + " failed");
@@ -148,8 +273,10 @@ public class CameraCaptureController extends AbstractVideoCaptureController {
148
273
  }
149
274
 
150
275
  // Otherwise, use facingMode (defaulting to front/user facing).
151
- final boolean isFrontFacing = facingMode == null || !facingMode.equals("environment");
276
+ final boolean isFrontFacing = facingMode == null || facingMode.equals("user");
277
+ cameraIndex = -1;
152
278
  for (String name : deviceNames) {
279
+ cameraIndex++;
153
280
  if (failedDevices.contains(name)) {
154
281
  continue;
155
282
  }
@@ -161,22 +288,26 @@ public class CameraCaptureController extends AbstractVideoCaptureController {
161
288
  if (videoCapturer != null) {
162
289
  Log.d(TAG, message + " succeeded");
163
290
  this.isFrontFacing = cameraEnumerator.isFrontFacing(name);
164
- return videoCapturer;
291
+ this.currentDeviceId = String.valueOf(cameraIndex);
292
+ return new CreateCapturerResult(cameraIndex, name, videoCapturer);
165
293
  } else {
166
294
  Log.d(TAG, message + " failed");
167
295
  failedDevices.add(name);
168
296
  }
169
297
  }
170
298
 
299
+ cameraIndex = -1;
171
300
  // Fallback to any available camera.
172
301
  for (String name : deviceNames) {
302
+ cameraIndex++;
173
303
  if (!failedDevices.contains(name)) {
174
304
  VideoCapturer videoCapturer = cameraEnumerator.createCapturer(name, cameraEventsHandler);
175
305
  String message = "Create fallback camera " + name;
176
306
  if (videoCapturer != null) {
177
307
  Log.d(TAG, message + " succeeded");
178
308
  this.isFrontFacing = cameraEnumerator.isFrontFacing(name);
179
- return videoCapturer;
309
+ this.currentDeviceId = String.valueOf(cameraIndex);
310
+ return new CreateCapturerResult(cameraIndex, name, videoCapturer);
180
311
  } else {
181
312
  Log.d(TAG, message + " failed");
182
313
  failedDevices.add(name);
@@ -184,8 +315,21 @@ public class CameraCaptureController extends AbstractVideoCaptureController {
184
315
  }
185
316
  }
186
317
 
318
+ currentDeviceId = null;
187
319
  Log.w(TAG, "Unable to identify a suitable camera.");
188
320
 
189
321
  return null;
190
322
  }
323
+
324
+ private static class CreateCapturerResult {
325
+ public final int cameraIndex;
326
+ public final String cameraName;
327
+ public final VideoCapturer videoCapturer;
328
+
329
+ public CreateCapturerResult(int cameraIndex, String cameraName, VideoCapturer videoCapturer) {
330
+ this.cameraIndex = cameraIndex;
331
+ this.cameraName = cameraName;
332
+ this.videoCapturer = videoCapturer;
333
+ }
334
+ }
191
335
  }
@@ -6,6 +6,7 @@ import android.content.Intent;
6
6
  import android.media.projection.MediaProjectionManager;
7
7
  import android.util.DisplayMetrics;
8
8
  import android.util.Log;
9
+ import androidx.core.util.Consumer;
9
10
 
10
11
  import com.facebook.react.bridge.Arguments;
11
12
  import com.facebook.react.bridge.BaseActivityEventListener;
@@ -192,7 +193,7 @@ class GetUserMediaImpl {
192
193
  Log.d(TAG, "getUserMedia(video): " + videoConstraintsMap);
193
194
 
194
195
  CameraCaptureController cameraCaptureController =
195
- new CameraCaptureController(getCameraEnumerator(), videoConstraintsMap);
196
+ new CameraCaptureController(reactContext.getCurrentActivity(), getCameraEnumerator(), videoConstraintsMap);
196
197
 
197
198
  videoTrack = createVideoTrack(cameraCaptureController);
198
199
  }
@@ -233,11 +234,22 @@ class GetUserMediaImpl {
233
234
  }
234
235
  }
235
236
 
236
- void switchCamera(String trackId) {
237
+ void applyConstraints(String trackId, ReadableMap constraints, Promise promise) {
237
238
  TrackPrivate track = tracks.get(trackId);
238
- if (track != null && track.videoCaptureController instanceof CameraCaptureController) {
239
- CameraCaptureController cameraCaptureController = (CameraCaptureController) track.videoCaptureController;
240
- cameraCaptureController.switchCamera();
239
+ if (track != null && track.videoCaptureController instanceof AbstractVideoCaptureController) {
240
+ AbstractVideoCaptureController captureController = (AbstractVideoCaptureController) track.videoCaptureController;
241
+ captureController.applyConstraints(constraints, new Consumer<Exception>() {
242
+ public void accept(Exception e) {
243
+ if(e != null) {
244
+ promise.reject(e);
245
+ return;
246
+ }
247
+
248
+ promise.resolve(captureController.getSettings());
249
+ }
250
+ });
251
+ } else {
252
+ promise.reject(new Exception("Camera track not found!"));
241
253
  }
242
254
  }
243
255
 
@@ -327,10 +339,13 @@ class GetUserMediaImpl {
327
339
  if (track instanceof VideoTrack) {
328
340
  TrackPrivate tp = this.tracks.get(trackId);
329
341
  AbstractVideoCaptureController vcc = tp.videoCaptureController;
342
+ trackInfo.putMap("settings", vcc.getSettings());
343
+ }
344
+
345
+ if (track instanceof AudioTrack) {
330
346
  WritableMap settings = Arguments.createMap();
331
- settings.putInt("height", vcc.getHeight());
332
- settings.putInt("width", vcc.getWidth());
333
- settings.putInt("frameRate", vcc.getFrameRate());
347
+ settings.putString("deviceId", "audio-1");
348
+ settings.putString("groupId", "");
334
349
  trackInfo.putMap("settings", settings);
335
350
  }
336
351
 
@@ -60,6 +60,11 @@ public class ScreenCaptureController extends AbstractVideoCaptureController {
60
60
  }
61
61
  }
62
62
 
63
+ @Override
64
+ public String getDeviceId() {
65
+ return "screen-capture";
66
+ }
67
+
63
68
  @Override
64
69
  public void dispose() {
65
70
  MediaProjectionService.abort(context);
@@ -882,11 +882,13 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
882
882
  }
883
883
 
884
884
  @ReactMethod
885
- public void mediaStreamTrackSwitchCamera(String id) {
885
+ public void mediaStreamTrackApplyConstraints(String id, ReadableMap constraints, Promise promise) {
886
886
  ThreadUtils.runOnExecutor(() -> {
887
887
  MediaStreamTrack track = getLocalTrack(id);
888
888
  if (track != null) {
889
- getUserMediaImpl.switchCamera(id);
889
+ getUserMediaImpl.applyConstraints(id, constraints, promise);
890
+ } else {
891
+ promise.reject(new Exception("mediaStreamTrackApplyConstraints() could not find track " + id));
890
892
  }
891
893
  });
892
894
  }
@@ -0,0 +1,55 @@
1
+ /*
2
+ * Copyright 2023-2024 LiveKit, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package org.webrtc;
18
+
19
+ import androidx.annotation.Nullable;
20
+
21
+ import java.util.ArrayList;
22
+ import java.util.List;
23
+
24
+ /**
25
+ * A helper to access package-protected methods used in [Camera2Session]
26
+ * <p>
27
+ * Note: cameraId as used in the Camera1XXX classes refers to the index within the list of cameras.
28
+ *
29
+ * @suppress
30
+ */
31
+
32
+ public class Camera1Helper {
33
+
34
+ public static int getCameraId(String deviceName) {
35
+ return Camera1Enumerator.getCameraIndex(deviceName);
36
+ }
37
+
38
+ @Nullable
39
+ public static List<CameraEnumerationAndroid.CaptureFormat> getSupportedFormats(int cameraId) {
40
+ return Camera1Enumerator.getSupportedFormats(cameraId);
41
+ }
42
+
43
+ public static Size findClosestCaptureFormat(int cameraId, int width, int height) {
44
+ List<CameraEnumerationAndroid.CaptureFormat> formats = getSupportedFormats(cameraId);
45
+
46
+ List<Size> sizes = new ArrayList<>();
47
+ if (formats != null) {
48
+ for (CameraEnumerationAndroid.CaptureFormat format : formats) {
49
+ sizes.add(new Size(format.width, format.height));
50
+ }
51
+ }
52
+
53
+ return CameraEnumerationAndroid.getClosestSupportedSize(sizes, width, height);
54
+ }
55
+ }