Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
android/build
examples
lib
src/vendor
Expand Down
12 changes: 12 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

// JVM unit tests (src/test) run with Robolectric.
testOptions {
unitTests {
includeAndroidResources = true
}
}
}

def kotlin_version = getExtOrDefault('kotlinVersion', '1.8.10')
Expand All @@ -102,4 +109,9 @@ dependencies {
implementation "com.facebook.react:react-android:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "androidx.core:core:1.7.0"

// Test-only: not part of what apps get from this library.
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.robolectric:robolectric:4.14.1'
testImplementation 'org.mockito:mockito-core:5.14.2'
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.oney.WebRTCModule;

import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.util.Consumer;

Expand All @@ -24,6 +25,7 @@ public abstract class AbstractVideoCaptureController {
protected VideoCapturer videoCapturer;

protected CapturerEventsListener capturerEventsListener;
private String TAG = "AbsVidCaptControl";

public AbstractVideoCaptureController(int width, int height, int fps) {
this.targetWidth = width;
Expand Down Expand Up @@ -78,9 +80,10 @@ public void startCapture() {
try {
videoCapturer.startCapture(targetWidth, targetHeight, targetFps);
} catch (RuntimeException e) {
// XXX This can only fail if we initialize the capturer incorrectly,
// which we don't. Thus, ignore any failures here since we trust
// ourselves.
// CameraCapturer throws only if it was not initialized, which we always do first. Other
// capturers may throw at runtime (for example screen capture). Log it: an error here
// never reaches CameraEventsHandler, so nothing else would report it.
Log.e(TAG, "[startCapture] failed", e);
}
}

Expand Down Expand Up @@ -111,5 +114,11 @@ public void setCapturerEventsListener(CapturerEventsListener listener) {
public interface CapturerEventsListener {
/** Called when the capturer is ended and in an irrecoverable state. */
public void onCapturerEnded();

/**
* Called when the capturer stops sending frames for a reason outside the app
* (muted = true), and when frames come back (muted = false). The track stays live.
*/
default void onCapturerMuteChanged(boolean muted) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.webrtc.Camera1Capturer;
import org.webrtc.Camera1Helper;
import org.webrtc.Camera2Capturer;
import org.webrtc.Camera2Enumerator;
import org.webrtc.Camera2Helper;
import org.webrtc.CameraEnumerator;
import org.webrtc.CameraVideoCapturer;
Expand Down Expand Up @@ -60,13 +61,70 @@ public void onCameraOpening(String cameraName) {
int cameraIndex = findCameraIndex(cameraName);
updateActualSize(cameraIndex, cameraName, videoCapturer);
CameraCaptureController.this.currentDeviceId = cameraIndex == -1 ? null : String.valueOf(cameraIndex);
CameraCaptureController.this.currentCameraName = cameraName;
}

// Android took our camera (another app opened a camera).
@Override
public void onCameraDisconnected() {
super.onCameraDisconnected();
cameraRecovery.onCameraDisconnected(currentCameraManagerId());
}

// The camera could not be opened, a fatal camera error happened, or the camera is slow to
// start (a warning: the open continues).
@Override
public void onCameraError(String errorDescription) {
super.onCameraError(errorDescription);
cameraRecovery.onCameraError(errorDescription);
}

@Override
public void onFirstFrameAvailable() {
super.onFirstFrameAvailable();
cameraRecovery.onFramesArrived();
}
};

/** Gets the camera back after another app took it. See its class doc for which Android versions. */
private final CameraRecoveryController cameraRecovery;

/** Name of the camera that was opened last. For Camera2 this is the CameraManager id. */
@Nullable
private volatile String currentCameraName;

/**
* The CameraManager id of the camera that was opened last, or null if unknown. Camera2: the
* camera name is the CameraManager id. Camera1: the camera index is the same number.
*/
@Nullable
private String currentCameraManagerId() {
return cameraEnumerator instanceof Camera2Enumerator ? currentCameraName : currentDeviceId;
}

public CameraCaptureController(Context context, CameraEnumerator cameraEnumerator, ReadableMap constraints) {
super(constraints.getInt("width"), constraints.getInt("height"), constraints.getInt("frameRate"));

this.context = context;
this.cameraRecovery = new CameraRecoveryController(
context,
// The parent's startCapture: the app's request (appRequestsCapture) does not change.
CameraCaptureController.super::startCapture,
new CameraRecoveryController.Listener() {
@Override
public void onCaptureInterruptedChanged(boolean interrupted) {
if (capturerEventsListener != null) {
capturerEventsListener.onCapturerMuteChanged(interrupted);
}
}

@Override
public void onRecoveryFailed() {
if (capturerEventsListener != null) {
capturerEventsListener.onCapturerEnded();
}
}
});
this.cameraEnumerator = cameraEnumerator;
this.constraints = constraints;

Expand All @@ -80,6 +138,25 @@ public String getDeviceId() {
return currentDeviceId;
}

@Override
public void startCapture() {
cameraRecovery.setCaptureRequested(true);
super.startCapture();
}

@Override
public boolean stopCapture() {
// First, so a reopen that is waiting can't turn the camera back on.
cameraRecovery.setCaptureRequested(false);
return super.stopCapture();
}

@Override
public void dispose() {
cameraRecovery.setCaptureRequested(false);
super.dispose();
}

private int findCameraIndex(String cameraName) {
String[] deviceNames = cameraEnumerator.getDeviceNames();
for (int i = 0; i < deviceNames.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.oney.WebRTCModule;

import androidx.annotation.Nullable;

/**
* Error descriptions that libwebrtc passes to {@code CameraEventsHandler.onCameraError}, and what
* they mean for {@link CameraRecoveryController}.
*
* <p>Copied from io.getstream:stream-video-webrtc-android 156.0.7. Check them when updating WebRTC:
* if one changes, that error no longer matches and counts as an ordinary failed try. The Camera1
* capturer uses other descriptions, so none of these match there.
*/
final class CameraErrorDescriptions {

/** CameraCapturer: a warning after 10 s. The open continues and may still succeed. */
static final String CAMERA_START_TIMEOUT = "Camera failed to start within timeout.";

/** Camera2Session, ERROR_CAMERA_IN_USE: another app holds the camera. */
static final String CAMERA_IN_USE = "Camera device is in use already.";

/** Camera2Session, ERROR_MAX_CAMERAS_IN_USE: another app holds a camera. Seen on Samsung. */
static final String MAX_CAMERAS_IN_USE =
"Camera device could not be opened because there are too many other open camera devices.";

/** True if the camera is only slow to start: the open continues. */
static boolean isStartTimeout(@Nullable String errorDescription) {
return CAMERA_START_TIMEOUT.equals(errorDescription);
}

/** True if another app holds the camera, so a later try may work. */
static boolean isCameraHeldByAnotherApp(@Nullable String errorDescription) {
return CAMERA_IN_USE.equals(errorDescription) || MAX_CAMERAS_IN_USE.equals(errorDescription);
}

private CameraErrorDescriptions() {}
}
Loading
Loading