Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch mode without restart #806

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,36 @@ protected boolean collectCameraInfo(@NonNull Facing facing) {
return false;
}

@EngineThread
@Override
protected void prepareNewMode() {
if(previewAspectRatioEqualsForModes()) {
Camera.Parameters params = mCamera.getParameters();
setPictureSize(params);
applyDefaultFocus(params);
params.setRecordingHint(getMode() == Mode.VIDEO);
mCamera.setParameters(params);
} else {
restartBind();
}
}

private boolean previewAspectRatioEqualsForModes() {
if (mCaptureSize == null) {
return false;
}
Size prevModeSize = mCaptureSize;
mCaptureSize = computeCaptureSize();

boolean flip = getAngles().flip(Reference.SENSOR, Reference.VIEW);
AspectRatio prevModePreviewTargetRatio = AspectRatio.of(prevModeSize.getWidth(), prevModeSize.getHeight());
AspectRatio targetRatio = AspectRatio.of(mCaptureSize.getWidth(), mCaptureSize.getHeight());
if (flip) prevModePreviewTargetRatio = prevModePreviewTargetRatio.flip();
if (flip) targetRatio = targetRatio.flip();

return prevModePreviewTargetRatio.equals(targetRatio);
}

//endregion

//region Start
Expand Down Expand Up @@ -216,18 +246,7 @@ protected Task<Void> onStartPreview() {
params.setPreviewFormat(ImageFormat.NV21);
// setPreviewSize is not allowed during preview
params.setPreviewSize(mPreviewStreamSize.getWidth(), mPreviewStreamSize.getHeight());
if (getMode() == Mode.PICTURE) {
// setPictureSize is allowed during preview
params.setPictureSize(mCaptureSize.getWidth(), mCaptureSize.getHeight());
} else {
// mCaptureSize in this case is a video size. The available video sizes are not
// necessarily a subset of the picture sizes, so we can't use the mCaptureSize value:
// it might crash. However, the setPictureSize() passed here is useless : we don't allow
// HQ pictures in video mode.
// While this might be lifted in the future, for now, just use a picture capture size.
Size pictureSize = computeCaptureSize(Mode.PICTURE);
params.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight());
}
setPictureSize(params);
mCamera.setParameters(params);

mCamera.setPreviewCallbackWithBuffer(null); // Release anything left
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,13 @@ protected final boolean collectCameraInfo(@NonNull Facing facing) {
return false;
}

@EngineThread
@Override
protected void prepareNewMode() {
LOG.w("prepareNewMode:", "can't prepare new mode without restart for Engine2");
restartBind();
}

//endregion

//region Start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.graphics.PointF;
import android.graphics.RectF;
import android.hardware.Camera;
import android.location.Location;

import androidx.annotation.CallSuper;
Expand Down Expand Up @@ -390,12 +391,14 @@ public final void setMode(@NonNull Mode mode) {
new Runnable() {
@Override
public void run() {
restart();
prepareNewMode();
}
});
}
}

protected abstract void prepareNewMode();

@NonNull
@Override
public final Mode getMode() {
Expand Down Expand Up @@ -750,6 +753,21 @@ private Size getPreviewSurfaceSize(@NonNull Reference reference) {
: preview.getSurfaceSize();
}

void setPictureSize(Camera.Parameters params) {
if (getMode() == Mode.PICTURE) {
// setPictureSize is allowed during preview
params.setPictureSize(mCaptureSize.getWidth(), mCaptureSize.getHeight());
} else {
// mCaptureSize in this case is a video size. The available video sizes are not
// necessarily a subset of the picture sizes, so we can't use the mCaptureSize value:
// it might crash. However, the setPictureSize() passed here is useless : we don't allow
// HQ pictures in video mode.
// While this might be lifted in the future, for now, just use a picture capture size.
Size pictureSize = computeCaptureSize(Mode.PICTURE);
params.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight());
}
}

/**
* Returns the snapshot size, but not cropped with the view dimensions, which
* is what we will do before creating the snapshot. However, cropping is done at various
Expand Down