Search in sources :

Example 31 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project android_packages_apps_Snap by LineageOS.

the class Camera2GraphView method startRecordingVideo.

private boolean startRecordingVideo(final int cameraId) {
    if (null == mCameraDevice[cameraId]) {
        return false;
    }
    Log.d(TAG, "StartRecordingVideo " + cameraId);
    mStartRecPending = true;
    mIsRecordingVideo = true;
    mMediaRecorderPausing = false;
    mActivity.updateStorageSpaceAndHint();
    if (mActivity.getStorageSpaceBytes() <= Storage.LOW_STORAGE_THRESHOLD_BYTES) {
        Log.w(TAG, "Storage issue, ignore the start request");
        mStartRecPending = false;
        mIsRecordingVideo = false;
        return false;
    }
    try {
        setUpMediaRecorder(cameraId);
        try {
            // Recording is now started
            mMediaRecorder.start();
        } catch (RuntimeException e) {
            Toast.makeText(mActivity, "Could not start media recorder.\n " + "Can't start video recording.", Toast.LENGTH_LONG).show();
            releaseMediaRecorder();
            releaseAudioFocus();
            mStartRecPending = false;
            mIsRecordingVideo = false;
            return false;
        }
        if (mUnsupportedResolution == true) {
            Log.v(TAG, "Unsupported Resolution according to target");
            mStartRecPending = false;
            mIsRecordingVideo = false;
            return false;
        }
        if (mMediaRecorder == null) {
            Log.e(TAG, "Fail to initialize media recorder");
            mStartRecPending = false;
            mIsRecordingVideo = false;
            return false;
        }
        requestAudioFocus();
        mUI.getFocusRing().stopFocusAnimations();
        mUI.hideUIwhileRecording();
        mCameraHandler.removeMessages(CANCEL_TOUCH_FOCUS, mCameraId[cameraId]);
        mState[cameraId] = STATE_PREVIEW;
        mControlAFMode = CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE;
        closePreviewSession();
        mFrameProcessor.onClose();
        Size preview = mVideoPreviewSize;
        if (mHighSpeedCapture) {
            preview = mVideoSize;
        }
        boolean changed = mUI.setPreviewSize(preview.getWidth(), preview.getHeight());
        if (changed) {
            mUI.hideSurfaceView();
            mUI.showSurfaceView();
        }
        mUI.resetTrackingFocus();
        createVideoSnapshotImageReader();
        mVideoRequestBuilder = mCameraDevice[cameraId].createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
        mVideoRequestBuilder.setTag(cameraId);
        mPreviewRequestBuilder[cameraId] = mVideoRequestBuilder;
        List<Surface> surfaces = new ArrayList<>();
        Surface surface = getPreviewSurfaceForSession(cameraId);
        mFrameProcessor.onOpen(getFrameProcFilterId(), mVideoSize);
        if (mFrameProcessor.isFrameFilterEnabled()) {
            mActivity.runOnUiThread(new Runnable() {

                public void run() {
                    mUI.getSurfaceHolder().setFixedSize(mVideoSize.getHeight(), mVideoSize.getWidth());
                }
            });
        }
        mFrameProcessor.setOutputSurface(surface);
        mFrameProcessor.setVideoOutputSurface(mMediaRecorder.getSurface());
        addPreviewSurface(mVideoRequestBuilder, surfaces, cameraId);
        if (mHighSpeedCapture)
            mVideoRequestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, mHighSpeedFPSRange);
        if (mHighSpeedCapture && ((int) mHighSpeedFPSRange.getUpper() > NORMAL_SESSION_MAX_FPS)) {
            mCameraDevice[cameraId].createConstrainedHighSpeedCaptureSession(surfaces, new CameraConstrainedHighSpeedCaptureSession.StateCallback() {

                @Override
                public void onConfigured(CameraCaptureSession cameraCaptureSession) {
                    mCurrentSession = cameraCaptureSession;
                    mCaptureSession[cameraId] = cameraCaptureSession;
                    CameraConstrainedHighSpeedCaptureSession session = (CameraConstrainedHighSpeedCaptureSession) mCurrentSession;
                    try {
                        List list = CameraUtil.createHighSpeedRequestList(mVideoRequestBuilder.build(), cameraId);
                        session.setRepeatingBurst(list, mCaptureCallback, mCameraHandler);
                    } catch (CameraAccessException e) {
                        Log.e(TAG, "Failed to start high speed video recording " + e.getMessage());
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        Log.e(TAG, "Failed to start high speed video recording " + e.getMessage());
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        Log.e(TAG, "Failed to start high speed video recording " + e.getMessage());
                        e.printStackTrace();
                    }
                    mUI.getFocusRing().stopFocusAnimations();
                    mUI.resetPauseButton();
                    mRecordingTotalTime = 0L;
                    mRecordingStartTime = SystemClock.uptimeMillis();
                    mUI.enableShutter(false);
                    mUI.showRecordingUI(true, true);
                    updateRecordingTime();
                    keepScreenOn();
                }

                @Override
                public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
                    Toast.makeText(mActivity, "Failed", Toast.LENGTH_SHORT).show();
                }
            }, null);
        } else {
            surfaces.add(mVideoSnapshotImageReader.getSurface());
            mCameraDevice[cameraId].createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {

                @Override
                public void onConfigured(CameraCaptureSession cameraCaptureSession) {
                    Log.d(TAG, "StartRecordingVideo session onConfigured");
                    mCurrentSession = cameraCaptureSession;
                    mCaptureSession[cameraId] = cameraCaptureSession;
                    try {
                        setUpVideoCaptureRequestBuilder(mVideoRequestBuilder, cameraId);
                        mCurrentSession.setRepeatingRequest(mVideoRequestBuilder.build(), mCaptureCallback, mCameraHandler);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    }
                    mUI.getFocusRing().stopFocusAnimations();
                    mUI.resetPauseButton();
                    mRecordingTotalTime = 0L;
                    mRecordingStartTime = SystemClock.uptimeMillis();
                    mUI.showRecordingUI(true, false);
                    updateRecordingTime();
                    keepScreenOn();
                }

                @Override
                public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
                    Toast.makeText(mActivity, "Video Failed", Toast.LENGTH_SHORT).show();
                }
            }, null);
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mStartRecPending = false;
    return true;
}
Also used : Size(android.util.Size) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Surface(android.view.Surface) CameraAccessException(android.hardware.camera2.CameraAccessException) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) CameraConstrainedHighSpeedCaptureSession(android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession)

Example 32 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project android_packages_apps_Snap by LineageOS.

the class Camera2GraphView method captureStillPictureForLongshot.

private void captureStillPictureForLongshot(CaptureRequest.Builder captureBuilder, int id) throws CameraAccessException {
    Log.d(TAG, "captureStillPictureForLongshot " + id);
    List<CaptureRequest> burstList = new ArrayList<>();
    for (int i = 0; i < PersistUtil.getLongshotShotLimit(); i++) {
        burstList.add(captureBuilder.build());
    }
    mCaptureSession[id].captureBurst(burstList, new CameraCaptureSession.CaptureCallback() {

        @Override
        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
            Log.d(TAG, "captureStillPictureForLongshot onCaptureCompleted: " + id);
            if (mLongshotActive) {
                checkAndPlayShutterSound(id);
            }
        }

        @Override
        public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request, CaptureFailure result) {
            Log.d(TAG, "captureStillPictureForLongshot onCaptureFailed: " + id);
        }

        @Override
        public void onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId, long frameNumber) {
            Log.d(TAG, "captureStillPictureForLongshot onCaptureSequenceCompleted: " + id);
            mLongshotActive = false;
            unlockFocus(id);
        }
    }, mCaptureCallbackHandler);
    mActivity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            mUI.enableVideo(false);
        }
    });
}
Also used : CaptureFailure(android.hardware.camera2.CaptureFailure) ArrayList(java.util.ArrayList) TotalCaptureResult(android.hardware.camera2.TotalCaptureResult) CaptureRequest(android.hardware.camera2.CaptureRequest) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Paint(android.graphics.Paint) Point(android.graphics.Point)

Example 33 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project android_packages_apps_Snap by LineageOS.

the class PanoCaptureModule method createSession.

private void createSession() {
    if (!mCameraOpened || !mSurfaceReady)
        return;
    synchronized (mSessionLock) {
        List<Surface> list = new LinkedList<Surface>();
        try {
            Surface surface = null;
            SurfaceHolder sh = mUI.getSurfaceHolder();
            if (sh != null) {
                surface = sh.getSurface();
            }
            if (surface == null)
                return;
            if (mFrameProcessor == null) {
                mFrameProcessor = new PanoCaptureFrameProcessor(mOutputSize, mActivity, mUI, this);
            }
            mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            mPreviewRequestBuilder.addTarget(mFrameProcessor.getInputSurface());
            mPreviewRequestBuilder.addTarget(surface);
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
            mPreviewRequest = mPreviewRequestBuilder.build();
            list.add(surface);
            list.add(mFrameProcessor.getInputSurface());
            mCameraDevice.createCaptureSession(list, new CameraCaptureSession.StateCallback() {

                @Override
                public void onConfigured(CameraCaptureSession cameraCaptureSession) {
                    if (null == mCameraDevice) {
                        Log.e(TAG, "The camera is already closed.");
                        return;
                    }
                    // When the session is ready, we start displaying the preview.
                    mCaptureSession = cameraCaptureSession;
                    try {
                        mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mCameraHandler);
                    } catch (CameraAccessException e) {
                        Log.e(TAG, "createCaptureSession: " + e.toString());
                    }
                }

                @Override
                public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
                    Log.e(TAG, "Capture session configuration is failed");
                }
            }, null);
        } catch (CameraAccessException e) {
            Log.e(TAG, "createSession: " + e.toString());
            mActivity.finish();
        } catch (SecurityException e) {
            Log.e(TAG, "createSession: " + e.toString());
            mActivity.finish();
        }
    }
}
Also used : SurfaceHolder(android.view.SurfaceHolder) CameraAccessException(android.hardware.camera2.CameraAccessException) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) LinkedList(java.util.LinkedList) Surface(android.view.Surface)

Example 34 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project OpenCamera by ageback.

the class CameraController2 method createCaptureSession.

private void createCaptureSession(final MediaRecorder video_recorder) throws CameraControllerException {
    if (MyDebug.LOG)
        Log.d(TAG, "create capture session");
    if (previewBuilder == null) {
        if (MyDebug.LOG)
            Log.d(TAG, "previewBuilder not present!");
        // throw as RuntimeException, as this is a programming error
        throw new RuntimeException();
    }
    if (camera == null) {
        if (MyDebug.LOG)
            Log.e(TAG, "no camera");
        return;
    }
    if (captureSession != null) {
        if (MyDebug.LOG)
            Log.d(TAG, "close old capture session");
        captureSession.close();
        captureSession = null;
    // pending_request_when_ready = null;
    }
    try {
        if (video_recorder != null) {
            if (supports_photo_video_recording && !want_video_high_speed) {
                createPictureImageReader();
            } else {
                closePictureImageReader();
            }
        } else {
            // in some cases need to recreate picture imageReader and the texture default buffer size (e.g., see test testTakePhotoPreviewPaused())
            createPictureImageReader();
        }
        if (texture != null) {
            // need to set the texture size
            if (MyDebug.LOG)
                Log.d(TAG, "set size of preview texture");
            if (preview_width == 0 || preview_height == 0) {
                if (MyDebug.LOG)
                    Log.e(TAG, "application needs to call setPreviewSize()");
                // throw as RuntimeException, as this is a programming error
                throw new RuntimeException();
            }
            texture.setDefaultBufferSize(preview_width, preview_height);
            // also need to create a new surface for the texture, in case the size has changed - but make sure we remove the old one first!
            if (surface_texture != null) {
                if (MyDebug.LOG)
                    Log.d(TAG, "remove old target: " + surface_texture);
                previewBuilder.removeTarget(surface_texture);
            }
            this.surface_texture = new Surface(texture);
            if (MyDebug.LOG)
                Log.d(TAG, "created new target: " + surface_texture);
        }
        if (video_recorder != null) {
            if (MyDebug.LOG)
                Log.d(TAG, "creating capture session for video recording");
        } else {
            if (MyDebug.LOG)
                Log.d(TAG, "picture size: " + imageReader.getWidth() + " x " + imageReader.getHeight());
        }
        /*if( MyDebug.LOG )
			Log.d(TAG, "preview size: " + previewImageReader.getWidth() + " x " + previewImageReader.getHeight());*/
        if (MyDebug.LOG)
            Log.d(TAG, "preview size: " + this.preview_width + " x " + this.preview_height);
        class MyStateCallback extends CameraCaptureSession.StateCallback {

            // must sychronize on this and notifyAll when setting to true
            private boolean callback_done;

            @Override
            public void onConfigured(@NonNull CameraCaptureSession session) {
                if (MyDebug.LOG) {
                    Log.d(TAG, "onConfigured: " + session);
                    Log.d(TAG, "captureSession was: " + captureSession);
                }
                if (camera == null) {
                    if (MyDebug.LOG) {
                        Log.d(TAG, "camera is closed");
                    }
                    synchronized (create_capture_session_lock) {
                        callback_done = true;
                        create_capture_session_lock.notifyAll();
                    }
                    return;
                }
                captureSession = session;
                Surface surface = getPreviewSurface();
                previewBuilder.addTarget(surface);
                if (video_recorder != null)
                    previewBuilder.addTarget(video_recorder.getSurface());
                try {
                    setRepeatingRequest();
                } catch (CameraAccessException e) {
                    if (MyDebug.LOG) {
                        Log.e(TAG, "failed to start preview");
                        Log.e(TAG, "reason: " + e.getReason());
                        Log.e(TAG, "message: " + e.getMessage());
                    }
                    e.printStackTrace();
                    // we indicate that we failed to start the preview by setting captureSession back to null
                    // this will cause a CameraControllerException to be thrown below
                    captureSession = null;
                }
                synchronized (create_capture_session_lock) {
                    callback_done = true;
                    create_capture_session_lock.notifyAll();
                }
            }

            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                if (MyDebug.LOG) {
                    Log.d(TAG, "onConfigureFailed: " + session);
                    Log.d(TAG, "captureSession was: " + captureSession);
                }
                synchronized (create_capture_session_lock) {
                    callback_done = true;
                    create_capture_session_lock.notifyAll();
                }
            // don't throw CameraControllerException here, as won't be caught - instead we throw CameraControllerException below
            }
            /*@Override
				public void onReady(CameraCaptureSession session) {
					if( MyDebug.LOG )
						Log.d(TAG, "onReady: " + session);
					if( pending_request_when_ready != null ) {
						if( MyDebug.LOG )
							Log.d(TAG, "have pending_request_when_ready: " + pending_request_when_ready);
						CaptureRequest request = pending_request_when_ready;
						pending_request_when_ready = null;
						try {
							captureSession.capture(request, previewCaptureCallback, handler);
						}
						catch(CameraAccessException e) {
							if( MyDebug.LOG ) {
								Log.e(TAG, "failed to take picture");
								Log.e(TAG, "reason: " + e.getReason());
								Log.e(TAG, "message: " + e.getMessage());
							}
							e.printStackTrace();
							jpeg_cb = null;
							if( take_picture_error_cb != null ) {
								take_picture_error_cb.onError();
								take_picture_error_cb = null;
							}
						}
					}
				}*/
        }
        final MyStateCallback myStateCallback = new MyStateCallback();
        Surface preview_surface = getPreviewSurface();
        List<Surface> surfaces;
        if (video_recorder != null) {
            if (supports_photo_video_recording && !want_video_high_speed) {
                surfaces = Arrays.asList(preview_surface, video_recorder.getSurface(), imageReader.getSurface());
            } else {
                surfaces = Arrays.asList(preview_surface, video_recorder.getSurface());
            }
        // n.b., raw not supported for photo snapshots while video recording
        } else if (imageReaderRaw != null) {
            surfaces = Arrays.asList(preview_surface, imageReader.getSurface(), imageReaderRaw.getSurface());
        } else {
            surfaces = Arrays.asList(preview_surface, imageReader.getSurface());
        }
        if (MyDebug.LOG) {
            Log.d(TAG, "texture: " + texture);
            Log.d(TAG, "preview_surface: " + preview_surface);
            if (video_recorder == null) {
                if (imageReaderRaw != null) {
                    Log.d(TAG, "imageReaderRaw: " + imageReaderRaw);
                    Log.d(TAG, "imageReaderRaw: " + imageReaderRaw.getWidth());
                    Log.d(TAG, "imageReaderRaw: " + imageReaderRaw.getHeight());
                    Log.d(TAG, "imageReaderRaw: " + imageReaderRaw.getImageFormat());
                } else {
                    Log.d(TAG, "imageReader: " + imageReader);
                    Log.d(TAG, "imageReader width: " + imageReader.getWidth());
                    Log.d(TAG, "imageReader height: " + imageReader.getHeight());
                    Log.d(TAG, "imageReader format: " + imageReader.getImageFormat());
                }
            }
        }
        if (video_recorder != null && want_video_high_speed && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            camera.createConstrainedHighSpeedCaptureSession(surfaces, myStateCallback, handler);
            is_video_high_speed = true;
        } else {
            camera.createCaptureSession(surfaces, myStateCallback, handler);
            is_video_high_speed = false;
        }
        if (MyDebug.LOG)
            Log.d(TAG, "wait until session created...");
        synchronized (create_capture_session_lock) {
            while (!myStateCallback.callback_done) {
                try {
                    // release the lock, and wait until myStateCallback calls notifyAll()
                    create_capture_session_lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        if (MyDebug.LOG) {
            Log.d(TAG, "created captureSession: " + captureSession);
        }
        if (captureSession == null) {
            if (MyDebug.LOG)
                Log.e(TAG, "failed to create capture session");
            throw new CameraControllerException();
        }
    } catch (CameraAccessException e) {
        if (MyDebug.LOG) {
            Log.e(TAG, "CameraAccessException trying to create capture session");
            Log.e(TAG, "reason: " + e.getReason());
            Log.e(TAG, "message: " + e.getMessage());
        }
        e.printStackTrace();
        throw new CameraControllerException();
    }
}
Also used : CameraAccessException(android.hardware.camera2.CameraAccessException) NonNull(android.support.annotation.NonNull) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Surface(android.view.Surface)

Example 35 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project collect by opendatakit.

the class Camera2VideoFragment method startPreview.

/**
 * Start the camera preview.
 */
private void startPreview() {
    if (null == cameraDevice || !textureView.isAvailable() || null == previewSize) {
        return;
    }
    try {
        closePreviewSession();
        SurfaceTexture texture = textureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
        previewBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        Surface previewSurface = new Surface(texture);
        previewBuilder.addTarget(previewSurface);
        cameraDevice.createCaptureSession(Collections.singletonList(previewSurface), new CameraCaptureSession.StateCallback() {

            @Override
            public void onConfigured(@NonNull CameraCaptureSession session) {
                previewSession = session;
                updatePreview();
            }

            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                Activity activity = getActivity();
                if (null != activity) {
                    ToastUtils.showShortToast(getActivity(), "Failed");
                }
            }
        }, backgroundHandler);
    } catch (CameraAccessException e) {
        Timber.e(e);
    }
}
Also used : CameraAccessException(android.hardware.camera2.CameraAccessException) SurfaceTexture(android.graphics.SurfaceTexture) Activity(android.app.Activity) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Surface(android.view.Surface)

Aggregations

CameraCaptureSession (android.hardware.camera2.CameraCaptureSession)38 CameraAccessException (android.hardware.camera2.CameraAccessException)18 Surface (android.view.Surface)18 BlockingSessionCallback (com.android.ex.camera2.blocking.BlockingSessionCallback)10 ArrayList (java.util.ArrayList)10 CaptureRequest (android.hardware.camera2.CaptureRequest)9 TotalCaptureResult (android.hardware.camera2.TotalCaptureResult)8 SurfaceTexture (android.graphics.SurfaceTexture)6 CameraDevice (android.hardware.camera2.CameraDevice)6 Activity (android.app.Activity)5 Point (android.graphics.Point)5 CameraCharacteristics (android.hardware.camera2.CameraCharacteristics)4 CaptureFailure (android.hardware.camera2.CaptureFailure)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 CameraManager (android.hardware.camera2.CameraManager)3 Image (android.media.Image)3 NonNull (android.support.annotation.NonNull)3 Size (android.util.Size)3 IOException (java.io.IOException)3