Improve handling of missing camera during calls.

This commit is contained in:
Miriam Zimmerman
2025-05-30 14:02:19 -04:00
committed by Cody Henthorne
parent faf0b630c1
commit 340b94f849
5 changed files with 80 additions and 7 deletions

View File

@@ -39,11 +39,14 @@ public class Camera implements CameraControl, CameraVideoCapturer.CameraSwitchHa
private static final String TAG = Log.tag(Camera.class);
@NonNull private final Context context;
@Nullable private final CameraVideoCapturer capturer;
@Nullable private CameraVideoCapturer capturer;
@Nullable private CameraEventListener cameraEventListener;
@NonNull private final EglBaseWrapper eglBase;
private final int cameraCount;
@NonNull private CameraState.Direction activeDirection;
private CameraState.Direction oldActiveDirection;
private CapturerObserver observer;
private boolean enabled;
private boolean isInitialized;
private int orientation;
@@ -79,6 +82,7 @@ public class Camera implements CameraControl, CameraVideoCapturer.CameraSwitchHa
@Override
public void initCapturer(@NonNull CapturerObserver observer) {
if (capturer != null) {
this.observer = observer; // save in case we need to disposeAndFlipCamera
eglBase.performWithValidEglBase(base -> {
capturer.initialize(SurfaceTextureHelper.create("WebRTC-SurfaceTextureHelper", base.getEglBaseContext()),
context,
@@ -99,6 +103,7 @@ public class Camera implements CameraControl, CameraVideoCapturer.CameraSwitchHa
if (capturer == null || cameraCount < 2) {
throw new AssertionError("Tried to flip the camera, but we only have " + cameraCount + " of them.");
}
oldActiveDirection = activeDirection;
activeDirection = PENDING;
capturer.switchCamera(this);
}
@@ -146,6 +151,28 @@ public class Camera implements CameraControl, CameraVideoCapturer.CameraSwitchHa
}
}
public void disposeAndFlipCamera() {
if (capturer != null) {
capturer.dispose();
boolean wasInitialized = isInitialized;
isInitialized = false;
CameraState.Direction candidateDirection = oldActiveDirection.switchDirection();
CameraVideoCapturer captureCandidate = createVideoCapturer(getCameraEnumerator(context), candidateDirection);
if (captureCandidate != null) {
capturer = captureCandidate;
activeDirection = candidateDirection;
if (wasInitialized) {
initCapturer(this.observer);
}
if (enabled) {
setEnabled(true);
}
} else {
activeDirection = NONE;
}
}
}
int getCount() {
return cameraCount;
}
@@ -204,7 +231,9 @@ public class Camera implements CameraControl, CameraVideoCapturer.CameraSwitchHa
@Override
public void onCameraSwitchError(String errorMessage) {
Log.e(TAG, "onCameraSwitchError: " + errorMessage);
if (cameraEventListener != null) cameraEventListener.onCameraSwitchCompleted(new CameraState(getActiveDirection(), getCount()));
if (cameraEventListener != null) {
cameraEventListener.onCameraSwitchFailure(new CameraState(getActiveDirection(), getCount()));
}
}
private static class FilteredCamera2Enumerator extends Camera2Enumerator {
@@ -314,7 +343,7 @@ public class Camera implements CameraControl, CameraVideoCapturer.CameraSwitchHa
public void onCapturerStarted(boolean success) {
observer.onCapturerStarted(success);
if (success && cameraEventListener != null) {
cameraEventListener.onFullyInitialized();
cameraEventListener.onFullyInitialized(getCameraState());
}
}

View File

@@ -8,7 +8,8 @@ import androidx.annotation.NonNull;
* onCameraSwitchCompleted is triggered by {@link org.webrtc.CameraVideoCapturer.CameraSwitchHandler}
*/
public interface CameraEventListener {
void onFullyInitialized();
void onFullyInitialized(@NonNull CameraState newCameraState);
void onCameraSwitchCompleted(@NonNull CameraState newCameraState);
void onCameraSwitchFailure(@NonNull CameraState newCameraState);
void onCameraStopped();
}