Search in sources :

Example 6 with CameraConstrainedHighSpeedCaptureSession

use of android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession in project android_frameworks_base by DirtyUnicorns.

the class Camera2RecordingTest method startSlowMotionRecording.

private void startSlowMotionRecording(boolean useMediaRecorder, int videoFrameRate, int captureRate, Range<Integer> fpsRange, CameraCaptureSession.CaptureCallback listener, boolean useHighSpeedSession) throws Exception {
    List<Surface> outputSurfaces = new ArrayList<Surface>(2);
    assertTrue("Both preview and recording surfaces should be valid", mPreviewSurface.isValid() && mRecordingSurface.isValid());
    outputSurfaces.add(mPreviewSurface);
    outputSurfaces.add(mRecordingSurface);
    // Video snapshot surface
    if (mReaderSurface != null) {
        outputSurfaces.add(mReaderSurface);
    }
    mSessionListener = new BlockingSessionCallback();
    mSession = configureCameraSession(mCamera, outputSurfaces, useHighSpeedSession, mSessionListener, mHandler);
    // Create slow motion request list
    List<CaptureRequest> slowMoRequests = null;
    if (useHighSpeedSession) {
        CaptureRequest.Builder requestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
        requestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, fpsRange);
        requestBuilder.addTarget(mPreviewSurface);
        requestBuilder.addTarget(mRecordingSurface);
        slowMoRequests = ((CameraConstrainedHighSpeedCaptureSession) mSession).createHighSpeedRequestList(requestBuilder.build());
    } else {
        CaptureRequest.Builder recordingRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
        recordingRequestBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_USE_SCENE_MODE);
        recordingRequestBuilder.set(CaptureRequest.CONTROL_SCENE_MODE, CaptureRequest.CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO);
        CaptureRequest.Builder recordingOnlyBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
        recordingOnlyBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_USE_SCENE_MODE);
        recordingOnlyBuilder.set(CaptureRequest.CONTROL_SCENE_MODE, CaptureRequest.CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO);
        int slowMotionFactor = captureRate / videoFrameRate;
        // Make sure camera output frame rate is set to correct value.
        recordingRequestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, fpsRange);
        recordingRequestBuilder.addTarget(mRecordingSurface);
        recordingRequestBuilder.addTarget(mPreviewSurface);
        recordingOnlyBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, fpsRange);
        recordingOnlyBuilder.addTarget(mRecordingSurface);
        slowMoRequests = new ArrayList<CaptureRequest>();
        // Preview + recording.
        slowMoRequests.add(recordingRequestBuilder.build());
        for (int i = 0; i < slowMotionFactor - 1; i++) {
            // Recording only.
            slowMoRequests.add(recordingOnlyBuilder.build());
        }
    }
    mSession.setRepeatingBurst(slowMoRequests, listener, mHandler);
    if (useMediaRecorder) {
        mMediaRecorder.start();
    } else {
    // TODO: need implement MediaCodec path.
    }
}
Also used : BlockingSessionCallback(com.android.ex.camera2.blocking.BlockingSessionCallback) ArrayList(java.util.ArrayList) CaptureRequest(android.hardware.camera2.CaptureRequest) Surface(android.view.Surface)

Example 7 with CameraConstrainedHighSpeedCaptureSession

use of android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession in project android_frameworks_base by crdroidandroid.

the class Camera2RecordingTest method startSlowMotionRecording.

private void startSlowMotionRecording(boolean useMediaRecorder, int videoFrameRate, int captureRate, Range<Integer> fpsRange, CameraCaptureSession.CaptureCallback listener, boolean useHighSpeedSession) throws Exception {
    List<Surface> outputSurfaces = new ArrayList<Surface>(2);
    assertTrue("Both preview and recording surfaces should be valid", mPreviewSurface.isValid() && mRecordingSurface.isValid());
    outputSurfaces.add(mPreviewSurface);
    outputSurfaces.add(mRecordingSurface);
    // Video snapshot surface
    if (mReaderSurface != null) {
        outputSurfaces.add(mReaderSurface);
    }
    mSessionListener = new BlockingSessionCallback();
    mSession = configureCameraSession(mCamera, outputSurfaces, useHighSpeedSession, mSessionListener, mHandler);
    // Create slow motion request list
    List<CaptureRequest> slowMoRequests = null;
    if (useHighSpeedSession) {
        CaptureRequest.Builder requestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
        requestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, fpsRange);
        requestBuilder.addTarget(mPreviewSurface);
        requestBuilder.addTarget(mRecordingSurface);
        slowMoRequests = ((CameraConstrainedHighSpeedCaptureSession) mSession).createHighSpeedRequestList(requestBuilder.build());
    } else {
        CaptureRequest.Builder recordingRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
        recordingRequestBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_USE_SCENE_MODE);
        recordingRequestBuilder.set(CaptureRequest.CONTROL_SCENE_MODE, CaptureRequest.CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO);
        CaptureRequest.Builder recordingOnlyBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
        recordingOnlyBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_USE_SCENE_MODE);
        recordingOnlyBuilder.set(CaptureRequest.CONTROL_SCENE_MODE, CaptureRequest.CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO);
        int slowMotionFactor = captureRate / videoFrameRate;
        // Make sure camera output frame rate is set to correct value.
        recordingRequestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, fpsRange);
        recordingRequestBuilder.addTarget(mRecordingSurface);
        recordingRequestBuilder.addTarget(mPreviewSurface);
        recordingOnlyBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, fpsRange);
        recordingOnlyBuilder.addTarget(mRecordingSurface);
        slowMoRequests = new ArrayList<CaptureRequest>();
        // Preview + recording.
        slowMoRequests.add(recordingRequestBuilder.build());
        for (int i = 0; i < slowMotionFactor - 1; i++) {
            // Recording only.
            slowMoRequests.add(recordingOnlyBuilder.build());
        }
    }
    mSession.setRepeatingBurst(slowMoRequests, listener, mHandler);
    if (useMediaRecorder) {
        mMediaRecorder.start();
    } else {
    // TODO: need implement MediaCodec path.
    }
}
Also used : BlockingSessionCallback(com.android.ex.camera2.blocking.BlockingSessionCallback) ArrayList(java.util.ArrayList) CaptureRequest(android.hardware.camera2.CaptureRequest) Surface(android.view.Surface)

Example 8 with CameraConstrainedHighSpeedCaptureSession

use of android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession 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)

Aggregations

ArrayList (java.util.ArrayList)7 CaptureRequest (android.hardware.camera2.CaptureRequest)6 Surface (android.view.Surface)6 BlockingSessionCallback (com.android.ex.camera2.blocking.BlockingSessionCallback)5 CameraConstrainedHighSpeedCaptureSession (android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession)3 CameraAccessException (android.hardware.camera2.CameraAccessException)2 CameraCaptureSession (android.hardware.camera2.CameraCaptureSession)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Size (android.util.Size)1 IOException (java.io.IOException)1