Skip to content

Commit

Permalink
Handle camera disconnection
Browse files Browse the repository at this point in the history
Stop mirroring on camera disconnection.

PR #4213 <#4213>
  • Loading branch information
rom1v committed Oct 31, 2023
1 parent d544e57 commit 64930e7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
11 changes: 10 additions & 1 deletion server/src/main/java/com/genymobile/scrcpy/CameraCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;

public class CameraCapture extends SurfaceCapture {

Expand All @@ -33,6 +34,8 @@ public class CameraCapture extends SurfaceCapture {
private CameraDevice cameraDevice;
private Executor cameraExecutor;

private final AtomicBoolean disconnected = new AtomicBoolean();

public CameraCapture(String explicitCameraId, Size explicitSize) {
this.explicitCameraId = explicitCameraId;
this.explicitSize = explicitSize;
Expand Down Expand Up @@ -97,7 +100,8 @@ public void onOpened(CameraDevice camera) {
@Override
public void onDisconnected(CameraDevice camera) {
Ln.w("Camera disconnected");
// TODO
disconnected.set(true);
requestReset();
}

@Override
Expand Down Expand Up @@ -177,4 +181,9 @@ public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request
}
}, cameraHandler);
}

@Override
public boolean isClosed() {
return disconnected.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,13 @@ public boolean consumeReset() {
* @param maxSize Maximum size
*/
public abstract boolean setMaxSize(int maxSize);

/**
* Indicate if the capture has been closed internally.
*
* @return {@code true} is the capture is closed, {@code false} otherwise.
*/
public boolean isClosed() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ private boolean encode(MediaCodec codec, Streamer streamer) throws IOException {
}
}

if (capture.isClosed()) {
// The capture might have been closed internally (for example if the camera is disconnected)
alive = false;
}

return !eof && alive;
}

Expand Down

0 comments on commit 64930e7

Please sign in to comment.