Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce8eff82 authored by Angus Kong's avatar Angus Kong Committed by Android (Google) Code Review
Browse files

Merge "Remove unnecessary startPreview thread in video." into gb-ub-photos-carlsbad

parents d483f8bf 9dd53602
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -35,4 +35,8 @@ public interface VideoController extends OnShutterButtonListener {
    public void stopPreview();

    public void updateCameraOrientation();

    // Callbacks for camera preview UI events.
    public void onPreviewUIReady();
    public void onPreviewUIDestroyed();
}
+33 −37
Original line number Diff line number Diff line
@@ -49,7 +49,6 @@ import android.provider.MediaStore.MediaColumns;
import android.provider.MediaStore.Video;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.View;
@@ -115,7 +114,7 @@ public class VideoModule implements CameraModule,
    private int mCameraId;
    private Parameters mParameters;

    private Boolean mCameraOpened = false;
    private boolean mCameraOpened = false;
    private boolean mIsInReviewMode;
    private boolean mSnapshotInProgress = false;

@@ -125,6 +124,9 @@ public class VideoModule implements CameraModule,

    private ComboPreferences mPreferences;
    private PreferenceGroup mPreferenceGroup;
    // Preference must be read before starting preview. We check this before starting
    // preview.
    private boolean mPreferenceRead;

    private boolean mIsVideoCaptureIntent;
    private boolean mQuickCapture;
@@ -226,12 +228,10 @@ public class VideoModule implements CameraModule,

    private void openCamera() {
        try {
            synchronized(mCameraOpened) {
            if (!mCameraOpened) {
                mCameraDevice = Util.openCamera(mActivity, mCameraId);
                mCameraOpened = true;
            }
            }
            mParameters = mCameraDevice.getParameters();
        } catch (CameraHardwareException e) {
            mOpenCameraFail = true;
@@ -667,6 +667,7 @@ public class VideoModule implements CameraModule,
        if (mCaptureTimeLapse) quality += 1000;
        mProfile = CamcorderProfile.get(mCameraId, quality);
        getDesiredPreviewSize();
        mPreferenceRead = true;
    }

    private void writeDefaultEffectToPrefs()  {
@@ -752,16 +753,7 @@ public class VideoModule implements CameraModule,
            }
            readVideoPreferences();
            resizeForPreviewAspectRatio();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (mCameraOpened) {
                        if (mCameraOpened) {
            startPreview();
                        }
                    }
                }
            }).start();
        } else {
            // preview already started
            mUI.enableShutter(true);
@@ -818,9 +810,13 @@ public class VideoModule implements CameraModule,
        if (p != null) return p.getZoom();
        return index;
    }

    private void startPreview() {
        Log.v(TAG, "startPreview");

        SurfaceTexture surfaceTexture = mUI.getSurfaceTexture();
        if (!mPreferenceRead || surfaceTexture == null || mPaused == true) return;

        mCameraDevice.setErrorCallback(mErrorCallback);
        if (mPreviewing == true) {
            stopPreview();
@@ -836,10 +832,6 @@ public class VideoModule implements CameraModule,

        try {
            if (!effectsActive()) {
                SurfaceTexture surfaceTexture = mUI.getSurfaceTexture();
                if (surfaceTexture == null) {
                    return; // The texture has been destroyed (pause, etc)
                }
                mCameraDevice.setPreviewTextureAsync(surfaceTexture);
                mCameraDevice.startPreviewAsync();
                mPreviewing = true;
@@ -854,17 +846,12 @@ public class VideoModule implements CameraModule,
            closeCamera();
            throw new RuntimeException("startPreview failed", ex);
        } finally {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
            if (mOpenCameraFail) {
                Util.showErrorAndFinish(mActivity, R.string.cannot_connect_camera);
            } else if (mCameraDisabled) {
                Util.showErrorAndFinish(mActivity, R.string.camera_disabled);
            }
        }
            });
        }

    }

@@ -924,13 +911,11 @@ public class VideoModule implements CameraModule,
        if (closeEffectsAlso) closeEffects();
        mCameraDevice.setZoomChangeListener(null);
        mCameraDevice.setErrorCallback(null);
        synchronized(mCameraOpened) {
        if (mCameraOpened) {
            CameraHolder.instance().release();
        }
        mCameraOpened = false;
        mCameraDevice = null;
        }
        mPreviewing = false;
        mSnapshotInProgress = false;
    }
@@ -984,6 +969,7 @@ public class VideoModule implements CameraModule,
        mHandler.removeMessages(SWITCH_CAMERA_START_ANIMATION);
        mPendingSwitchCameraId = -1;
        mSwitchingCamera = false;
        mPreferenceRead = false;
        // Call onPause after stopping video recording. So the camera can be
        // released as soon as possible.
    }
@@ -2246,4 +2232,14 @@ public class VideoModule implements CameraModule,
    public void onMediaSaveServiceConnected(MediaSaveService s) {
        // do nothing.
    }

    @Override
    public void onPreviewUIReady() {
        startPreview();
    }

    @Override
    public void onPreviewUIDestroyed() {
        stopPreview();
    }
}
+4 −15
Original line number Diff line number Diff line
@@ -57,7 +57,7 @@ public class VideoUI implements PieRenderer.PieListener,
        PreviewGestures.SingleTapListener,
        CameraRootView.MyDisplayListener,
        SurfaceTextureListener, SurfaceHolder.Callback {
    private final static String TAG = "CAM_VideoUI";
    private static final String TAG = "CAM_VideoUI";
    private static final int UPDATE_TRANSFORM_MATRIX = 1;
    // module fields
    private CameraActivity mActivity;
@@ -653,31 +653,20 @@ public class VideoUI implements PieRenderer.PieListener,
    }

    public SurfaceTexture getSurfaceTexture() {
        synchronized (mLock) {
            if (mSurfaceTexture == null) {
                try {
                    mLock.wait();
                } catch (InterruptedException e) {
                    Log.w(TAG, "Unexpected interruption when waiting to get surface texture");
                }
            }
        }
        return mSurfaceTexture;
    }

    // SurfaceTexture callbacks
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        synchronized (mLock) {
        mSurfaceTexture = surface;
            mLock.notifyAll();
        }
        mController.onPreviewUIReady();
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mSurfaceTexture = null;
        mController.stopPreview();
        mController.onPreviewUIDestroyed();
        Log.d(TAG, "surfaceTexture is destroyed");
        return true;
    }