Create EngineThread annotation

pull/630/head
Mattia Iavarone 6 years ago
parent 29da606524
commit 92393c19b2
  1. 21
      cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java
  2. 20
      cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java
  3. 52
      cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraEngine.java
  4. 7
      cameraview/src/main/java/com/otaliastudios/cameraview/engine/EngineThread.java

@ -97,6 +97,7 @@ public class Camera1Engine extends CameraEngine implements
//region Protected APIs //region Protected APIs
@EngineThread
@NonNull @NonNull
@Override @Override
protected List<Size> getPreviewStreamAvailableSizes() { protected List<Size> getPreviewStreamAvailableSizes() {
@ -110,12 +111,13 @@ public class Camera1Engine extends CameraEngine implements
return result; return result;
} }
@WorkerThread @EngineThread
@Override @Override
protected void onPreviewStreamSizeChanged() { protected void onPreviewStreamSizeChanged() {
restartPreview(); restartPreview();
} }
@EngineThread
@Override @Override
protected boolean collectCameraInfo(@NonNull Facing facing) { protected boolean collectCameraInfo(@NonNull Facing facing) {
int internalFacing = mMapper.mapFacing(facing); int internalFacing = mMapper.mapFacing(facing);
@ -140,7 +142,7 @@ public class Camera1Engine extends CameraEngine implements
//region Start //region Start
@NonNull @NonNull
@WorkerThread @EngineThread
@Override @Override
protected Task<Void> onStartEngine() { protected Task<Void> onStartEngine() {
try { try {
@ -164,6 +166,7 @@ public class Camera1Engine extends CameraEngine implements
return Tasks.forResult(null); return Tasks.forResult(null);
} }
@EngineThread
@NonNull @NonNull
@Override @Override
protected Task<Void> onStartBind() { protected Task<Void> onStartBind() {
@ -187,6 +190,7 @@ public class Camera1Engine extends CameraEngine implements
return Tasks.forResult(null); return Tasks.forResult(null);
} }
@EngineThread
@NonNull @NonNull
@Override @Override
protected Task<Void> onStartPreview() { protected Task<Void> onStartPreview() {
@ -238,6 +242,7 @@ public class Camera1Engine extends CameraEngine implements
//region Stop //region Stop
@EngineThread
@NonNull @NonNull
@Override @Override
protected Task<Void> onStopPreview() { protected Task<Void> onStopPreview() {
@ -256,6 +261,7 @@ public class Camera1Engine extends CameraEngine implements
return Tasks.forResult(null); return Tasks.forResult(null);
} }
@EngineThread
@NonNull @NonNull
@Override @Override
protected Task<Void> onStopBind() { protected Task<Void> onStopBind() {
@ -275,8 +281,8 @@ public class Camera1Engine extends CameraEngine implements
return Tasks.forResult(null); return Tasks.forResult(null);
} }
@EngineThread
@NonNull @NonNull
@WorkerThread
@Override @Override
protected Task<Void> onStopEngine() { protected Task<Void> onStopEngine() {
LOG.i("onStopEngine:", "About to clean up."); LOG.i("onStopEngine:", "About to clean up.");
@ -306,7 +312,7 @@ public class Camera1Engine extends CameraEngine implements
//region Pictures //region Pictures
@WorkerThread @EngineThread
@Override @Override
protected void onTakePicture(@NonNull PictureResult.Stub stub, boolean doMetering) { protected void onTakePicture(@NonNull PictureResult.Stub stub, boolean doMetering) {
stub.rotation = getAngles().offset(Reference.SENSOR, Reference.OUTPUT, stub.rotation = getAngles().offset(Reference.SENSOR, Reference.OUTPUT,
@ -316,7 +322,7 @@ public class Camera1Engine extends CameraEngine implements
mPictureRecorder.take(); mPictureRecorder.take();
} }
@WorkerThread @EngineThread
@Override @Override
protected void onTakePictureSnapshot(@NonNull PictureResult.Stub stub, protected void onTakePictureSnapshot(@NonNull PictureResult.Stub stub,
@NonNull AspectRatio outputRatio, @NonNull AspectRatio outputRatio,
@ -337,6 +343,7 @@ public class Camera1Engine extends CameraEngine implements
//region Videos //region Videos
@EngineThread
@Override @Override
protected void onTakeVideo(@NonNull VideoResult.Stub stub) { protected void onTakeVideo(@NonNull VideoResult.Stub stub) {
stub.rotation = getAngles().offset(Reference.SENSOR, Reference.OUTPUT, stub.rotation = getAngles().offset(Reference.SENSOR, Reference.OUTPUT,
@ -357,7 +364,7 @@ public class Camera1Engine extends CameraEngine implements
} }
@SuppressLint("NewApi") @SuppressLint("NewApi")
@WorkerThread @EngineThread
@Override @Override
protected void onTakeVideoSnapshot(@NonNull VideoResult.Stub stub, protected void onTakeVideoSnapshot(@NonNull VideoResult.Stub stub,
@NonNull AspectRatio outputRatio) { @NonNull AspectRatio outputRatio) {
@ -765,7 +772,7 @@ public class Camera1Engine extends CameraEngine implements
} }
@NonNull @NonNull
@WorkerThread @EngineThread
private static List<Camera.Area> computeMeteringAreas(double viewClickX, double viewClickY, private static List<Camera.Area> computeMeteringAreas(double viewClickX, double viewClickY,
int viewWidth, int viewHeight, int viewWidth, int viewHeight,
int sensorToDisplay) { int sensorToDisplay) {

@ -28,7 +28,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;
import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource; import com.google.android.gms.tasks.TaskCompletionSource;
@ -286,6 +285,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
//region Protected APIs //region Protected APIs
@EngineThread
@NonNull @NonNull
@Override @Override
protected List<Size> getPreviewStreamAvailableSizes() { protected List<Size> getPreviewStreamAvailableSizes() {
@ -310,12 +310,13 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
} }
} }
@WorkerThread @EngineThread
@Override @Override
protected void onPreviewStreamSizeChanged() { protected void onPreviewStreamSizeChanged() {
restartBind(); restartBind();
} }
@EngineThread
@Override @Override
protected final boolean collectCameraInfo(@NonNull Facing facing) { protected final boolean collectCameraInfo(@NonNull Facing facing) {
int internalFacing = mMapper.mapFacing(facing); int internalFacing = mMapper.mapFacing(facing);
@ -353,6 +354,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
//region Start //region Start
@EngineThread
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
@NonNull @NonNull
@Override @Override
@ -402,6 +404,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
return task.getTask(); return task.getTask();
} }
@EngineThread
@NonNull @NonNull
@Override @Override
protected Task<Void> onStartBind() { protected Task<Void> onStartBind() {
@ -525,6 +528,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
return task.getTask(); return task.getTask();
} }
@EngineThread
@NonNull @NonNull
@Override @Override
protected Task<Void> onStartPreview() { protected Task<Void> onStartPreview() {
@ -569,6 +573,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
//region Stop //region Stop
@EngineThread
@NonNull @NonNull
@Override @Override
protected Task<Void> onStopPreview() { protected Task<Void> onStopPreview() {
@ -598,7 +603,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
return Tasks.forResult(null); return Tasks.forResult(null);
} }
@EngineThread
@NonNull @NonNull
@Override @Override
protected Task<Void> onStopBind() { protected Task<Void> onStopBind() {
@ -627,7 +632,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
return Tasks.forResult(null); return Tasks.forResult(null);
} }
@EngineThread
@NonNull @NonNull
@Override @Override
protected Task<Void> onStopEngine() { protected Task<Void> onStopEngine() {
@ -660,7 +665,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
//region Pictures //region Pictures
@WorkerThread @EngineThread
@Override @Override
protected void onTakePictureSnapshot(@NonNull final PictureResult.Stub stub, protected void onTakePictureSnapshot(@NonNull final PictureResult.Stub stub,
@NonNull final AspectRatio outputRatio, @NonNull final AspectRatio outputRatio,
@ -692,6 +697,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
} }
} }
@EngineThread
@Override @Override
protected void onTakePicture(@NonNull final PictureResult.Stub stub, boolean doMetering) { protected void onTakePicture(@NonNull final PictureResult.Stub stub, boolean doMetering) {
if (doMetering) { if (doMetering) {
@ -752,7 +758,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
//region Videos //region Videos
@WorkerThread @EngineThread
@Override @Override
protected void onTakeVideo(@NonNull VideoResult.Stub stub) { protected void onTakeVideo(@NonNull VideoResult.Stub stub) {
LOG.i("onTakeVideo", "called."); LOG.i("onTakeVideo", "called.");
@ -787,7 +793,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
} }
} }
@WorkerThread @EngineThread
@Override @Override
protected void onTakeVideoSnapshot(@NonNull VideoResult.Stub stub, protected void onTakeVideoSnapshot(@NonNull VideoResult.Stub stub,
@NonNull AspectRatio outputRatio) { @NonNull AspectRatio outputRatio) {

@ -47,7 +47,6 @@ import androidx.annotation.CallSuper;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -303,7 +302,7 @@ public abstract class CameraEngine implements
final CameraException cameraException = (CameraException) throwable; final CameraException cameraException = (CameraException) throwable;
LOG.e("uncaughtException:", "Got CameraException:", cameraException, LOG.e("uncaughtException:", "Got CameraException:", cameraException,
"on engine state:", getEngineStateName()); "on engine state:", getEngineState());
if (fromExceptionHandler) { if (fromExceptionHandler) {
// Got to restart the handler. // Got to restart the handler.
thread.interrupt(); thread.interrupt();
@ -336,11 +335,6 @@ public abstract class CameraEngine implements
return mPreviewStep.getState(); return mPreviewStep.getState();
} }
@NonNull
private String getEngineStateName() {
return mEngineStep.getStateName();
}
private boolean canStartEngine() { private boolean canStartEngine() {
return mEngineStep.isStoppingOrStopped(); return mEngineStep.isStoppingOrStopped();
} }
@ -375,7 +369,7 @@ public abstract class CameraEngine implements
//region Start & Stop the engine //region Start & Stop the engine
@NonNull @NonNull
@WorkerThread @EngineThread
private Task<Void> startEngine() { private Task<Void> startEngine() {
if (canStartEngine()) { if (canStartEngine()) {
mEngineStep.doStart(false, new Callable<Task<Void>>() { mEngineStep.doStart(false, new Callable<Task<Void>>() {
@ -398,7 +392,7 @@ public abstract class CameraEngine implements
} }
@NonNull @NonNull
@WorkerThread @EngineThread
private Task<Void> stopEngine(boolean swallowExceptions) { private Task<Void> stopEngine(boolean swallowExceptions) {
if (needsStopEngine()) { if (needsStopEngine()) {
mEngineStep.doStop(swallowExceptions, new Callable<Task<Void>>() { mEngineStep.doStop(swallowExceptions, new Callable<Task<Void>>() {
@ -421,7 +415,7 @@ public abstract class CameraEngine implements
* @return a task * @return a task
*/ */
@NonNull @NonNull
@WorkerThread @EngineThread
protected abstract Task<Void> onStartEngine(); protected abstract Task<Void> onStartEngine();
/** /**
@ -431,7 +425,7 @@ public abstract class CameraEngine implements
* @return a task * @return a task
*/ */
@NonNull @NonNull
@WorkerThread @EngineThread
protected abstract Task<Void> onStopEngine(); protected abstract Task<Void> onStopEngine();
//endregion //endregion
@ -439,7 +433,7 @@ public abstract class CameraEngine implements
//region Start & Stop binding //region Start & Stop binding
@NonNull @NonNull
@WorkerThread @EngineThread
private Task<Void> startBind() { private Task<Void> startBind() {
if (canStartBind()) { if (canStartBind()) {
mBindStep.doStart(false, new Callable<Task<Void>>() { mBindStep.doStart(false, new Callable<Task<Void>>() {
@ -453,7 +447,7 @@ public abstract class CameraEngine implements
} }
@NonNull @NonNull
@WorkerThread @EngineThread
private Task<Void> stopBind(boolean swallowExceptions) { private Task<Void> stopBind(boolean swallowExceptions) {
if (needsStopBind()) { if (needsStopBind()) {
mBindStep.doStop(swallowExceptions, new Callable<Task<Void>>() { mBindStep.doStop(swallowExceptions, new Callable<Task<Void>>() {
@ -471,7 +465,7 @@ public abstract class CameraEngine implements
* @return a task * @return a task
*/ */
@NonNull @NonNull
@WorkerThread @EngineThread
protected abstract Task<Void> onStartBind(); protected abstract Task<Void> onStartBind();
/** /**
@ -481,7 +475,7 @@ public abstract class CameraEngine implements
* @return a task * @return a task
*/ */
@NonNull @NonNull
@WorkerThread @EngineThread
protected abstract Task<Void> onStopBind(); protected abstract Task<Void> onStopBind();
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
@ -522,7 +516,7 @@ public abstract class CameraEngine implements
//region Start & Stop preview //region Start & Stop preview
@NonNull @NonNull
@WorkerThread @EngineThread
private Task<Void> startPreview() { private Task<Void> startPreview() {
LOG.i("startPreview", "canStartPreview:", canStartPreview()); LOG.i("startPreview", "canStartPreview:", canStartPreview());
if (canStartPreview()) { if (canStartPreview()) {
@ -537,7 +531,7 @@ public abstract class CameraEngine implements
} }
@NonNull @NonNull
@WorkerThread @EngineThread
private Task<Void> stopPreview(boolean swallowExceptions) { private Task<Void> stopPreview(boolean swallowExceptions) {
LOG.i("stopPreview", LOG.i("stopPreview",
"needsStopPreview:", needsStopPreview(), "needsStopPreview:", needsStopPreview(),
@ -571,7 +565,7 @@ public abstract class CameraEngine implements
* @return a task * @return a task
*/ */
@NonNull @NonNull
@WorkerThread @EngineThread
protected abstract Task<Void> onStartPreview(); protected abstract Task<Void> onStartPreview();
/** /**
@ -581,7 +575,7 @@ public abstract class CameraEngine implements
* @return a task * @return a task
*/ */
@NonNull @NonNull
@WorkerThread @EngineThread
protected abstract Task<Void> onStopPreview(); protected abstract Task<Void> onStopPreview();
//endregion //endregion
@ -643,7 +637,7 @@ public abstract class CameraEngine implements
* *
* It basically depends on the step at which the preview stream size is actually used. * It basically depends on the step at which the preview stream size is actually used.
*/ */
@WorkerThread @EngineThread
protected abstract void onPreviewStreamSizeChanged(); protected abstract void onPreviewStreamSizeChanged();
@Override @Override
@ -676,7 +670,7 @@ public abstract class CameraEngine implements
* that would cause deadlocks due to us awaiting for {@link #stop()} to return. * that would cause deadlocks due to us awaiting for {@link #stop()} to return.
*/ */
public void destroy() { public void destroy() {
LOG.i("destroy:", "state:", getEngineStateName(), "thread:", Thread.currentThread()); LOG.i("destroy:", "state:", getEngineState(), "thread:", Thread.currentThread());
// Prevent CameraEngine leaks. Don't set to null, or exceptions // Prevent CameraEngine leaks. Don't set to null, or exceptions
// inside the standard stop() method might crash the main thread. // inside the standard stop() method might crash the main thread.
mHandler.getThread().setUncaughtExceptionHandler(new NoOpExceptionHandler()); mHandler.getThread().setUncaughtExceptionHandler(new NoOpExceptionHandler());
@ -710,7 +704,7 @@ public abstract class CameraEngine implements
@NonNull @NonNull
public Task<Void> start() { public Task<Void> start() {
LOG.i("Start:", "posting runnable. State:", getEngineStateName()); LOG.i("Start:", "posting runnable. State:", getEngineState());
final TaskCompletionSource<Void> outTask = new TaskCompletionSource<>(); final TaskCompletionSource<Void> outTask = new TaskCompletionSource<>();
mHandler.run(new Runnable() { mHandler.run(new Runnable() {
@Override @Override
@ -765,7 +759,7 @@ public abstract class CameraEngine implements
@NonNull @NonNull
private Task<Void> stop(final boolean swallowExceptions) { private Task<Void> stop(final boolean swallowExceptions) {
LOG.i("Stop:", "posting runnable. State:", getEngineStateName()); LOG.i("Stop:", "posting runnable. State:", getEngineState());
final TaskCompletionSource<Void> outTask = new TaskCompletionSource<>(); final TaskCompletionSource<Void> outTask = new TaskCompletionSource<>();
mHandler.run(new Runnable() { mHandler.run(new Runnable() {
@Override @Override
@ -1077,6 +1071,7 @@ public abstract class CameraEngine implements
* @param facing the facing value * @param facing the facing value
* @return true if we have one * @return true if we have one
*/ */
@EngineThread
protected abstract boolean collectCameraInfo(@NonNull Facing facing); protected abstract boolean collectCameraInfo(@NonNull Facing facing);
/** /**
@ -1256,6 +1251,7 @@ public abstract class CameraEngine implements
}); });
} }
@EngineThread
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
protected void onStopVideo() { protected void onStopVideo() {
if (mVideoRecorder != null) { if (mVideoRecorder != null) {
@ -1289,19 +1285,19 @@ public abstract class CameraEngine implements
mCallback.dispatchOnVideoRecordingEnd(); mCallback.dispatchOnVideoRecordingEnd();
} }
@WorkerThread @EngineThread
protected abstract void onTakePicture(@NonNull PictureResult.Stub stub, boolean doMetering); protected abstract void onTakePicture(@NonNull PictureResult.Stub stub, boolean doMetering);
@WorkerThread @EngineThread
protected abstract void onTakePictureSnapshot(@NonNull PictureResult.Stub stub, protected abstract void onTakePictureSnapshot(@NonNull PictureResult.Stub stub,
@NonNull AspectRatio outputRatio, @NonNull AspectRatio outputRatio,
boolean doMetering); boolean doMetering);
@WorkerThread @EngineThread
protected abstract void onTakeVideoSnapshot(@NonNull VideoResult.Stub stub, protected abstract void onTakeVideoSnapshot(@NonNull VideoResult.Stub stub,
@NonNull AspectRatio outputRatio); @NonNull AspectRatio outputRatio);
@WorkerThread @EngineThread
protected abstract void onTakeVideo(@NonNull VideoResult.Stub stub); protected abstract void onTakeVideo(@NonNull VideoResult.Stub stub);
//endregion //endregion
@ -1430,9 +1426,11 @@ public abstract class CameraEngine implements
* we can be sure that the camera is available (engineState == STARTED). * we can be sure that the camera is available (engineState == STARTED).
* @return a list of available sizes for preview * @return a list of available sizes for preview
*/ */
@EngineThread
@NonNull @NonNull
protected abstract List<Size> getPreviewStreamAvailableSizes(); protected abstract List<Size> getPreviewStreamAvailableSizes();
@EngineThread
@NonNull @NonNull
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
protected final Size computePreviewStreamSize() { protected final Size computePreviewStreamSize() {

@ -0,0 +1,7 @@
package com.otaliastudios.cameraview.engine;
/**
* Indicates that some action is being executed on the {@link CameraEngine} thread.
*/
@SuppressWarnings("WeakerAccess")
public @interface EngineThread {}
Loading…
Cancel
Save