From 639fffee624302ec5b175503d7bd8a441340a629 Mon Sep 17 00:00:00 2001 From: Eino-Ville Talvala Date: Tue, 30 Jun 2015 10:34:48 -0700 Subject: Camera2: Reorganize high-speed recording API slightly - Split off distinct high speed capture session class from base capture session - Move createHighSpeedRequestList to CameraConstrainedHighSpeedCaptureSession Bug: 21664295 Change-Id: I67d705fdeee1eaa6e5e3e1416771d5d0df642843 --- .../hardware/camera2/CameraCaptureSession.java | 11 - .../hardware/camera2/CameraCharacteristics.java | 2 +- .../CameraConstrainedHighSpeedCaptureSession.java | 131 ++++++++++ .../android/hardware/camera2/CameraDevice.java | 65 +---- .../android/hardware/camera2/CameraMetadata.java | 13 +- .../android/hardware/camera2/CaptureRequest.java | 25 +- .../camera2/impl/CameraCaptureSessionCore.java | 64 +++++ .../camera2/impl/CameraCaptureSessionImpl.java | 58 +---- ...meraConstrainedHighSpeedCaptureSessionImpl.java | 283 +++++++++++++++++++++ .../hardware/camera2/impl/CameraDeviceImpl.java | 213 +++------------- .../camera2/params/StreamConfigurationMap.java | 15 +- .../hardware/camera2/utils/SurfaceUtils.java | 96 +++++++ 12 files changed, 647 insertions(+), 329 deletions(-) create mode 100644 core/java/android/hardware/camera2/CameraConstrainedHighSpeedCaptureSession.java create mode 100644 core/java/android/hardware/camera2/impl/CameraCaptureSessionCore.java create mode 100644 core/java/android/hardware/camera2/impl/CameraConstrainedHighSpeedCaptureSessionImpl.java (limited to 'core/java/android/hardware') diff --git a/core/java/android/hardware/camera2/CameraCaptureSession.java b/core/java/android/hardware/camera2/CameraCaptureSession.java index 62ebfb3..46cafad 100644 --- a/core/java/android/hardware/camera2/CameraCaptureSession.java +++ b/core/java/android/hardware/camera2/CameraCaptureSession.java @@ -482,17 +482,6 @@ public abstract class CameraCaptureSession implements AutoCloseable { public abstract boolean isReprocessable(); /** - * Return if this capture session is constrained high speed session that is created by - * {@link CameraDevice#createConstrainedHighSpeedCaptureSession}. - * - * @return {@code true} if this session is constrained high speed capture session, - * {@code false} otherwise. - * - * @see CameraDevice#createConstrainedHighSpeedCaptureSession - */ - public abstract boolean isConstrainedHighSpeed(); - - /** * Get the input Surface associated with a reprocessable capture session. * *

Each reprocessable capture session has an input {@link Surface} where the reprocess diff --git a/core/java/android/hardware/camera2/CameraCharacteristics.java b/core/java/android/hardware/camera2/CameraCharacteristics.java index 547cce4..30aa2d5 100644 --- a/core/java/android/hardware/camera2/CameraCharacteristics.java +++ b/core/java/android/hardware/camera2/CameraCharacteristics.java @@ -664,7 +664,7 @@ public final class CameraCharacteristics extends CameraMetadata *

- *

When above conditions are NOT satistied, the + *

When above conditions are NOT satistied, * {@link android.hardware.camera2.CameraDevice#createConstrainedHighSpeedCaptureSession } - * and {@link android.hardware.camera2.CameraDevice#createConstrainedHighSpeedRequestList } will fail.

+ * will fail.

*

Switching to a FPS range that has different maximum FPS may trigger some camera device * reconfigurations, which may introduce extra latency. It is recommended that * the application avoids unnecessary maximum target FPS changes as much as possible @@ -1813,9 +1813,8 @@ public abstract class CameraMetadata { public static final int CONTROL_SCENE_MODE_BARCODE = 16; /** - *

This is deprecated, please use - * {@link android.hardware.camera2.CameraDevice#createConstrainedHighSpeedCaptureSession } - * and {@link android.hardware.camera2.CameraDevice#createConstrainedHighSpeedRequestList } + *

This is deprecated, please use {@link android.hardware.camera2.CameraDevice#createConstrainedHighSpeedCaptureSession } + * and {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList } * for high speed video recording.

*

Optimized for high speed video recording (frame rate >=60fps) use case.

*

The supported high speed video sizes and fps ranges are specified in diff --git a/core/java/android/hardware/camera2/CaptureRequest.java b/core/java/android/hardware/camera2/CaptureRequest.java index 6d8cc54..a136d0f 100644 --- a/core/java/android/hardware/camera2/CaptureRequest.java +++ b/core/java/android/hardware/camera2/CaptureRequest.java @@ -184,7 +184,7 @@ public final class CaptureRequest extends CameraMetadata> private final CameraMetadataNative mSettings; private boolean mIsReprocess; // If this request is part of constrained high speed request list that was created by - // {@link CameraDevice#createConstrainedHighSpeedRequestList}. + // {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList} private boolean mIsPartOfCHSRequestList = false; // Each reprocess request must be tied to a reprocessable session ID. // Valid only for reprocess requests (mIsReprocess == true). @@ -340,14 +340,14 @@ public final class CaptureRequest extends CameraMetadata> /** *

Determine if this request is part of a constrained high speed request list that was - * created by {@link CameraDevice#createConstrainedHighSpeedRequestList}. A constrained high - * speed request list contains some constrained high speed capture requests with certain - * interleaved pattern that is suitable for high speed preview/video streaming. An active - * constrained high speed capture session only accepts constrained high speed request lists. - * This method can be used to do the sanity check when a constrained high speed capture session - * receives a request list via {@link CameraCaptureSession#setRepeatingBurst} or - * {@link CameraCaptureSession#captureBurst}. - *

+ * created by + * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList}. + * A constrained high speed request list contains some constrained high speed capture requests + * with certain interleaved pattern that is suitable for high speed preview/video streaming. An + * active constrained high speed capture session only accepts constrained high speed request + * lists. This method can be used to do the sanity check when a constrained high speed capture + * session receives a request list via {@link CameraCaptureSession#setRepeatingBurst} or + * {@link CameraCaptureSession#captureBurst}.

* * * @return {@code true} if this request is part of a constrained high speed request list, @@ -595,9 +595,10 @@ public final class CaptureRequest extends CameraMetadata> /** *

Mark this request as part of a constrained high speed request list created by - * {@link CameraDevice#createConstrainedHighSpeedRequestList}. A constrained high speed - * request list contains some constrained high speed capture requests with certain - * interleaved pattern that is suitable for high speed preview/video streaming.

+ * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList}. + * A constrained high speed request list contains some constrained high speed capture + * requests with certain interleaved pattern that is suitable for high speed preview/video + * streaming.

* * @hide */ diff --git a/core/java/android/hardware/camera2/impl/CameraCaptureSessionCore.java b/core/java/android/hardware/camera2/impl/CameraCaptureSessionCore.java new file mode 100644 index 0000000..116f0f1 --- /dev/null +++ b/core/java/android/hardware/camera2/impl/CameraCaptureSessionCore.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.camera2.impl; + +/** + * Internal interface for CameraDeviceImpl to CameraCaptureSessionImpl(s) communication + */ +public interface CameraCaptureSessionCore { + + /** + * Replace this session with another session. + * + *

This is an optimization to avoid unconfiguring and then immediately having to + * reconfigure again.

+ * + *

The semantics are identical to {@link #close}, except that unconfiguring will be skipped. + *

+ * + *

After this call completes, the session will not call any further methods on the camera + * device.

+ * + * @see CameraCaptureSession#close + */ + void replaceSessionClose(); + + /** + * + * Create an internal state callback, to be invoked on the mDeviceHandler + * + *

It has a few behaviors: + *

+ *

+ * */ + CameraDeviceImpl.StateCallbackKK getDeviceStateCallback(); + + /** + * Whether currently in mid-abort. + * + *

This is used by the implementation to set the capture failure + * reason, in lieu of more accurate error codes from the camera service. + * Unsynchronized to avoid deadlocks between simultaneous session->device, + * device->session calls.

+ * + */ + boolean isAborting(); + +} diff --git a/core/java/android/hardware/camera2/impl/CameraCaptureSessionImpl.java b/core/java/android/hardware/camera2/impl/CameraCaptureSessionImpl.java index 3d261dd..3c19cd2 100644 --- a/core/java/android/hardware/camera2/impl/CameraCaptureSessionImpl.java +++ b/core/java/android/hardware/camera2/impl/CameraCaptureSessionImpl.java @@ -36,7 +36,8 @@ import java.util.List; import static android.hardware.camera2.impl.CameraDeviceImpl.checkHandler; import static com.android.internal.util.Preconditions.*; -public class CameraCaptureSessionImpl extends CameraCaptureSession { +public class CameraCaptureSessionImpl extends CameraCaptureSession + implements CameraCaptureSessionCore { private static final String TAG = "CameraCaptureSession"; private static final boolean DEBUG = false; @@ -60,7 +61,6 @@ public class CameraCaptureSessionImpl extends CameraCaptureSession { private final android.hardware.camera2.impl.CameraDeviceImpl mDeviceImpl; /** Internal handler; used for all incoming events to preserve total order */ private final Handler mDeviceHandler; - private final boolean mIsConstrainedHighSpeedSession; /** Drain Sequence IDs which have been queued but not yet finished with aborted/completed */ private final TaskDrainer mSequenceDrainer; @@ -89,14 +89,13 @@ public class CameraCaptureSessionImpl extends CameraCaptureSession { CameraCaptureSessionImpl(int id, Surface input, List outputs, CameraCaptureSession.StateCallback callback, Handler stateHandler, android.hardware.camera2.impl.CameraDeviceImpl deviceImpl, - Handler deviceStateHandler, boolean configureSuccess, boolean isConstrainedHighSpeed) { + Handler deviceStateHandler, boolean configureSuccess) { if (outputs == null || outputs.isEmpty()) { throw new IllegalArgumentException("outputs must be a non-null, non-empty list"); } else if (callback == null) { throw new IllegalArgumentException("callback must not be null"); } - mIsConstrainedHighSpeedSession = isConstrainedHighSpeed; mId = id; mIdString = String.format("Session %d: ", mId); @@ -136,30 +135,6 @@ public class CameraCaptureSessionImpl extends CameraCaptureSession { } } - - private boolean isConstrainedHighSpeedRequestList(List requestList) { - checkCollectionNotEmpty(requestList, "High speed request list"); - for (CaptureRequest request : requestList) { - if (!request.isPartOfCRequestList()) { - return false; - } - } - return true; - } - - /** - * If the session is constrained high speed session, it only accept constrained high speed - * request list. - */ - private void checkConstrainedHighSpeedRequestSanity(List requestList) { - if (mIsConstrainedHighSpeedSession) { - if (!isConstrainedHighSpeedRequestList(requestList)) { - throw new IllegalArgumentException("It is only allowed to submit a constrained " - + "high speed request list to a constrained high speed session!!!"); - } - } - } - @Override public CameraDevice getDevice() { return mDeviceImpl; @@ -181,10 +156,6 @@ public class CameraCaptureSessionImpl extends CameraCaptureSession { } else if (request.isReprocess() && request.getReprocessableSessionId() != mId) { throw new IllegalArgumentException("capture request was created for another session"); } - if (mIsConstrainedHighSpeedSession) { - throw new UnsupportedOperationException("Constrained high speed session doesn't support" - + " this method"); - } checkNotClosed(); @@ -208,8 +179,6 @@ public class CameraCaptureSessionImpl extends CameraCaptureSession { throw new IllegalArgumentException("Requests must have at least one element"); } - checkConstrainedHighSpeedRequestSanity(requests); - for (CaptureRequest request : requests) { if (request.isReprocess()) { if (!isReprocessable()) { @@ -244,10 +213,6 @@ public class CameraCaptureSessionImpl extends CameraCaptureSession { } else if (request.isReprocess()) { throw new IllegalArgumentException("repeating reprocess requests are not supported"); } - if (mIsConstrainedHighSpeedSession) { - throw new UnsupportedOperationException("Constrained high speed session doesn't support" - + " this method"); - } checkNotClosed(); @@ -271,8 +236,6 @@ public class CameraCaptureSessionImpl extends CameraCaptureSession { throw new IllegalArgumentException("requests must have at least one element"); } - checkConstrainedHighSpeedRequestSanity(requests); - for (CaptureRequest r : requests) { if (r.isReprocess()) { throw new IllegalArgumentException("repeating reprocess burst requests are not " + @@ -349,7 +312,8 @@ public class CameraCaptureSessionImpl extends CameraCaptureSession { * * @see CameraCaptureSession#close */ - synchronized void replaceSessionClose() { + @Override + public synchronized void replaceSessionClose() { /* * In order for creating new sessions to be fast, the new session should be created * before the old session is closed. @@ -431,9 +395,9 @@ public class CameraCaptureSessionImpl extends CameraCaptureSession { * Unsynchronized to avoid deadlocks between simultaneous session->device, * device->session calls.

* - *

Package-private.

*/ - boolean isAborting() { + @Override + public boolean isAborting() { return mAborting; } @@ -521,7 +485,8 @@ public class CameraCaptureSessionImpl extends CameraCaptureSession { * *

* */ - CameraDeviceImpl.StateCallbackKK getDeviceStateCallback() { + @Override + public CameraDeviceImpl.StateCallbackKK getDeviceStateCallback() { final CameraCaptureSession session = this; return new CameraDeviceImpl.StateCallbackKK() { @@ -759,9 +724,4 @@ public class CameraCaptureSessionImpl extends CameraCaptureSession { } } - @Override - public boolean isConstrainedHighSpeed() { - return mIsConstrainedHighSpeedSession; - } - } diff --git a/core/java/android/hardware/camera2/impl/CameraConstrainedHighSpeedCaptureSessionImpl.java b/core/java/android/hardware/camera2/impl/CameraConstrainedHighSpeedCaptureSessionImpl.java new file mode 100644 index 0000000..d732535 --- /dev/null +++ b/core/java/android/hardware/camera2/impl/CameraConstrainedHighSpeedCaptureSessionImpl.java @@ -0,0 +1,283 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.hardware.camera2.impl; + +import android.hardware.camera2.CameraAccessException; +import android.hardware.camera2.CameraCaptureSession; +import android.hardware.camera2.CameraCharacteristics; +import android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession; +import android.hardware.camera2.CameraDevice; +import android.hardware.camera2.CaptureRequest; +import android.hardware.camera2.params.StreamConfigurationMap; +import android.hardware.camera2.utils.SurfaceUtils; +import android.os.Handler; +import android.util.Range; +import android.view.Surface; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import static com.android.internal.util.Preconditions.*; + +/** + * Standard implementation of CameraConstrainedHighSpeedCaptureSession. + * + *

+ * Mostly just forwards calls to an instance of CameraCaptureSessionImpl, + * but implements the few necessary behavior changes and additional methods required + * for the constrained high speed speed mode. + *

+ */ + +public class CameraConstrainedHighSpeedCaptureSessionImpl + extends CameraConstrainedHighSpeedCaptureSession implements CameraCaptureSessionCore { + private final CameraCharacteristics mCharacteristics; + private final CameraCaptureSessionImpl mSessionImpl; + + /** + * Create a new CameraCaptureSession. + * + *

The camera device must already be in the {@code IDLE} state when this is invoked. + * There must be no pending actions + * (e.g. no pending captures, no repeating requests, no flush).

+ */ + CameraConstrainedHighSpeedCaptureSessionImpl(int id, List outputs, + CameraCaptureSession.StateCallback callback, Handler stateHandler, + android.hardware.camera2.impl.CameraDeviceImpl deviceImpl, + Handler deviceStateHandler, boolean configureSuccess, + CameraCharacteristics characteristics) { + mCharacteristics = characteristics; + CameraCaptureSession.StateCallback wrapperCallback = new WrapperCallback(callback); + mSessionImpl = new CameraCaptureSessionImpl(id, /*input*/null, outputs, wrapperCallback, + stateHandler, deviceImpl, deviceStateHandler, configureSuccess); + } + + @Override + public List createHighSpeedRequestList(CaptureRequest request) + throws CameraAccessException { + if (request == null) { + throw new IllegalArgumentException("Input capture request must not be null"); + } + Collection outputSurfaces = request.getTargets(); + Range fpsRange = request.get(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE); + + StreamConfigurationMap config = + mCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); + SurfaceUtils.checkConstrainedHighSpeedSurfaces(outputSurfaces, fpsRange, config); + + // Request list size: to limit the preview to 30fps, need use maxFps/30; to maximize + // the preview frame rate, should use maxBatch size for that high speed stream + // configuration. We choose the former for now. + int requestListSize = fpsRange.getUpper() / 30; + List requestList = new ArrayList(); + + // Prepare the Request builders: need carry over the request controls. + // First, create a request builder that will only include preview or recording target. + CameraMetadataNative requestMetadata = new CameraMetadataNative(request.getNativeCopy()); + // Note that after this step, the requestMetadata is mutated (swapped) and can not be used + // for next request builder creation. + CaptureRequest.Builder singleTargetRequestBuilder = new CaptureRequest.Builder( + requestMetadata, /*reprocess*/false, CameraCaptureSession.SESSION_ID_NONE); + + // Overwrite the capture intent to make sure a good value is set. + Iterator iterator = outputSurfaces.iterator(); + Surface firstSurface = iterator.next(); + Surface secondSurface = null; + if (outputSurfaces.size() == 1 && SurfaceUtils.isSurfaceForHwVideoEncoder(firstSurface)) { + singleTargetRequestBuilder.set(CaptureRequest.CONTROL_CAPTURE_INTENT, + CaptureRequest.CONTROL_CAPTURE_INTENT_PREVIEW); + } else { + // Video only, or preview + video + singleTargetRequestBuilder.set(CaptureRequest.CONTROL_CAPTURE_INTENT, + CaptureRequest.CONTROL_CAPTURE_INTENT_VIDEO_RECORD); + } + singleTargetRequestBuilder.setPartOfCHSRequestList(/*partOfCHSList*/true); + + // Second, Create a request builder that will include both preview and recording targets. + CaptureRequest.Builder doubleTargetRequestBuilder = null; + if (outputSurfaces.size() == 2) { + // Have to create a new copy, the original one was mutated after a new + // CaptureRequest.Builder creation. + requestMetadata = new CameraMetadataNative(request.getNativeCopy()); + doubleTargetRequestBuilder = new CaptureRequest.Builder( + requestMetadata, /*reprocess*/false, CameraCaptureSession.SESSION_ID_NONE); + doubleTargetRequestBuilder.set(CaptureRequest.CONTROL_CAPTURE_INTENT, + CaptureRequest.CONTROL_CAPTURE_INTENT_VIDEO_RECORD); + doubleTargetRequestBuilder.addTarget(firstSurface); + secondSurface = iterator.next(); + doubleTargetRequestBuilder.addTarget(secondSurface); + doubleTargetRequestBuilder.setPartOfCHSRequestList(/*partOfCHSList*/true); + // Make sure singleTargetRequestBuilder contains only recording surface for + // preview + recording case. + Surface recordingSurface = firstSurface; + if (!SurfaceUtils.isSurfaceForHwVideoEncoder(recordingSurface)) { + recordingSurface = secondSurface; + } + singleTargetRequestBuilder.addTarget(recordingSurface); + } else { + // Single output case: either recording or preview. + singleTargetRequestBuilder.addTarget(firstSurface); + } + + // Generate the final request list. + for (int i = 0; i < requestListSize; i++) { + if (i == 0 && doubleTargetRequestBuilder != null) { + // First request should be recording + preview request + requestList.add(doubleTargetRequestBuilder.build()); + } else { + requestList.add(singleTargetRequestBuilder.build()); + } + } + + return Collections.unmodifiableList(requestList); + } + + private boolean isConstrainedHighSpeedRequestList(List requestList) { + checkCollectionNotEmpty(requestList, "High speed request list"); + for (CaptureRequest request : requestList) { + if (!request.isPartOfCRequestList()) { + return false; + } + } + return true; + } + + @Override + public CameraDevice getDevice() { + return mSessionImpl.getDevice(); + } + + @Override + public void prepare(Surface surface) throws CameraAccessException { + mSessionImpl.prepare(surface); + } + + @Override + public int capture(CaptureRequest request, CaptureCallback listener, Handler handler) + throws CameraAccessException { + throw new UnsupportedOperationException("Constrained high speed session doesn't support" + + " this method"); + } + + @Override + public int captureBurst(List requests, CaptureCallback listener, + Handler handler) throws CameraAccessException { + if (!isConstrainedHighSpeedRequestList(requests)) { + throw new IllegalArgumentException( + "Only request lists created by createHighSpeedRequestList() can be submitted to " + + "a constrained high speed capture session"); + } + return mSessionImpl.captureBurst(requests, listener, handler); + } + + @Override + public int setRepeatingRequest(CaptureRequest request, CaptureCallback listener, + Handler handler) throws CameraAccessException { + throw new UnsupportedOperationException("Constrained high speed session doesn't support" + + " this method"); + } + + @Override + public int setRepeatingBurst(List requests, CaptureCallback listener, + Handler handler) throws CameraAccessException { + if (!isConstrainedHighSpeedRequestList(requests)) { + throw new IllegalArgumentException( + "Only request lists created by createHighSpeedRequestList() can be submitted to " + + "a constrained high speed capture session"); + } + return mSessionImpl.setRepeatingBurst(requests, listener, handler); + } + + @Override + public void stopRepeating() throws CameraAccessException { + mSessionImpl.stopRepeating(); + } + + @Override + public void abortCaptures() throws CameraAccessException { + mSessionImpl.abortCaptures(); + } + + @Override + public Surface getInputSurface() { + return null; + } + + @Override + public void close() { + mSessionImpl.close(); + } + + @Override + public boolean isReprocessable() { + return false; + } + + // Implementation of CameraCaptureSessionCore methods + + @Override + public void replaceSessionClose() { + mSessionImpl.replaceSessionClose(); + } + + @Override + public CameraDeviceImpl.StateCallbackKK getDeviceStateCallback() { + return mSessionImpl.getDeviceStateCallback(); + } + + @Override + public boolean isAborting() { + return mSessionImpl.isAborting(); + } + + private class WrapperCallback extends StateCallback { + private final StateCallback mCallback; + + public WrapperCallback(StateCallback callback) { + mCallback = callback; + } + + public void onConfigured(CameraCaptureSession session) { + mCallback.onConfigured(CameraConstrainedHighSpeedCaptureSessionImpl.this); + } + + public void onConfigureFailed(CameraCaptureSession session) { + mCallback.onConfigureFailed(CameraConstrainedHighSpeedCaptureSessionImpl.this); + } + + public void onReady(CameraCaptureSession session) { + mCallback.onReady(CameraConstrainedHighSpeedCaptureSessionImpl.this); + } + + public void onActive(CameraCaptureSession session) { + mCallback.onActive(CameraConstrainedHighSpeedCaptureSessionImpl.this); + } + + public void onClosed(CameraCaptureSession session) { + mCallback.onClosed(CameraConstrainedHighSpeedCaptureSessionImpl.this); + } + + public void onSurfacePrepared(CameraCaptureSession session, Surface surface) { + mCallback.onSurfacePrepared(CameraConstrainedHighSpeedCaptureSessionImpl.this, + surface); + } + + + } +} diff --git a/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java b/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java index 83128c3..c594228 100644 --- a/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java +++ b/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java @@ -113,7 +113,7 @@ public class CameraDeviceImpl extends CameraDevice { */ private final FrameNumberTracker mFrameNumberTracker = new FrameNumberTracker(); - private CameraCaptureSessionImpl mCurrentSession; + private CameraCaptureSessionCore mCurrentSession; private int mNextSessionId = 0; // Runnables for all state transitions, except error, which needs the @@ -510,6 +510,26 @@ public class CameraDeviceImpl extends CameraDevice { /*isConstrainedHighSpeed*/false); } + @Override + public void createConstrainedHighSpeedCaptureSession(List outputs, + android.hardware.camera2.CameraCaptureSession.StateCallback callback, Handler handler) + throws CameraAccessException { + if (outputs == null || outputs.size() == 0 || outputs.size() > 2) { + throw new IllegalArgumentException( + "Output surface list must not be null and the size must be no more than 2"); + } + StreamConfigurationMap config = + getCharacteristics().get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); + SurfaceUtils.checkConstrainedHighSpeedSurfaces(outputs, /*fpsRange*/null, config); + + List outConfigurations = new ArrayList<>(outputs.size()); + for (Surface surface : outputs) { + outConfigurations.add(new OutputConfiguration(surface)); + } + createCaptureSessionInternal(null, outConfigurations, callback, handler, + /*isConstrainedHighSpeed*/true); + } + private void createCaptureSessionInternal(InputConfiguration inputConfig, List outputConfigurations, CameraCaptureSession.StateCallback callback, Handler handler, @@ -565,10 +585,16 @@ public class CameraDeviceImpl extends CameraDevice { outSurfaces.add(config.getSurface()); } // Fire onConfigured if configureOutputs succeeded, fire onConfigureFailed otherwise. - CameraCaptureSessionImpl newSession = - new CameraCaptureSessionImpl(mNextSessionId++, input, - outSurfaces, callback, handler, this, mDeviceHandler, - configureSuccess, isConstrainedHighSpeed); + CameraCaptureSessionCore newSession = null; + if (isConstrainedHighSpeed) { + newSession = new CameraConstrainedHighSpeedCaptureSessionImpl(mNextSessionId++, + outSurfaces, callback, handler, this, mDeviceHandler, configureSuccess, + mCharacteristics); + } else { + newSession = new CameraCaptureSessionImpl(mNextSessionId++, input, + outSurfaces, callback, handler, this, mDeviceHandler, + configureSuccess); + } // TODO: wait until current session closes, then create the new session mCurrentSession = newSession; @@ -1933,181 +1959,4 @@ public class CameraDeviceImpl extends CameraDevice { return mCharacteristics; } - /** - * A high speed output surface can only be preview or hardware encoder surface. - * - * @param surface The high speed output surface to be checked. - */ - private void checkHighSpeedSurfaceFormat(Surface surface) { - // TODO: remove this override since the default format should be - // ImageFormat.PRIVATE. b/9487482 - final int HAL_FORMAT_RGB_START = 1; // HAL_PIXEL_FORMAT_RGBA_8888 from graphics.h - final int HAL_FORMAT_RGB_END = 5; // HAL_PIXEL_FORMAT_BGRA_8888 from graphics.h - int surfaceFormat = SurfaceUtils.getSurfaceFormat(surface); - if (surfaceFormat >= HAL_FORMAT_RGB_START && - surfaceFormat <= HAL_FORMAT_RGB_END) { - surfaceFormat = ImageFormat.PRIVATE; - } - - if (surfaceFormat != ImageFormat.PRIVATE) { - throw new IllegalArgumentException("Surface format(" + surfaceFormat + ") is not" - + " for preview or hardware video encoding!"); - } - } - - private void checkConstrainedHighSpeedSurfaces(Collection surfaces, - Range fpsRange) { - if (surfaces == null || surfaces.size() == 0 || surfaces.size() > 2) { - throw new IllegalArgumentException("Output target surface list must not be null and" - + " the size must be 1 or 2"); - } - - StreamConfigurationMap config = - getCharacteristics().get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); - List highSpeedSizes = null; - if (fpsRange == null) { - highSpeedSizes = Arrays.asList(config.getHighSpeedVideoSizes()); - } else { - // Check the FPS range first if provided - Range[] highSpeedFpsRanges = config.getHighSpeedVideoFpsRanges(); - if(!Arrays.asList(highSpeedFpsRanges).contains(fpsRange)) { - throw new IllegalArgumentException("Fps range " + fpsRange.toString() + " in the" - + " request is not a supported high speed fps range " + - Arrays.toString(highSpeedFpsRanges)); - } - highSpeedSizes = Arrays.asList(config.getHighSpeedVideoSizesFor(fpsRange)); - } - - for (Surface surface : surfaces) { - checkHighSpeedSurfaceFormat(surface); - - // Surface size must be supported high speed sizes. - Size surfaceSize = SurfaceUtils.getSurfaceSize(surface); - if (!highSpeedSizes.contains(surfaceSize)) { - throw new IllegalArgumentException("Surface size " + surfaceSize.toString() + " is" - + " not part of the high speed supported size list " + - Arrays.toString(highSpeedSizes.toArray())); - } - // Each output surface must be either preview surface or recording surface. - if (!SurfaceUtils.isSurfaceForPreview(surface) && - !SurfaceUtils.isSurfaceForHwVideoEncoder(surface)) { - throw new IllegalArgumentException("This output surface is neither preview nor " - + "hardware video encoding surface"); - } - if (SurfaceUtils.isSurfaceForPreview(surface) && - SurfaceUtils.isSurfaceForHwVideoEncoder(surface)) { - throw new IllegalArgumentException("This output surface can not be both preview" - + " and hardware video encoding surface"); - } - } - - // For 2 output surface case, they shouldn't be same type. - if (surfaces.size() == 2) { - // Up to here, each surface can only be either preview or recording. - Iterator iterator = surfaces.iterator(); - boolean isFirstSurfacePreview = - SurfaceUtils.isSurfaceForPreview(iterator.next()); - boolean isSecondSurfacePreview = - SurfaceUtils.isSurfaceForPreview(iterator.next()); - if (isFirstSurfacePreview == isSecondSurfacePreview) { - throw new IllegalArgumentException("The 2 output surfaces must have different" - + " type"); - } - } - } - - @Override - public void createConstrainedHighSpeedCaptureSession(List outputs, - android.hardware.camera2.CameraCaptureSession.StateCallback callback, Handler handler) - throws CameraAccessException { - if (outputs == null || outputs.size() == 0 || outputs.size() > 2) { - throw new IllegalArgumentException( - "Output surface list must not be null and the size must be no more than 2"); - } - checkConstrainedHighSpeedSurfaces(outputs, /*fpsRange*/null); - - List outConfigurations = new ArrayList<>(outputs.size()); - for (Surface surface : outputs) { - outConfigurations.add(new OutputConfiguration(surface)); - } - createCaptureSessionInternal(null, outConfigurations, callback, handler, - /*isConstrainedHighSpeed*/true); - } - - @Override - public List createConstrainedHighSpeedRequestList(CaptureRequest request) - throws CameraAccessException { - if (request == null) { - throw new IllegalArgumentException("Input capture request must not be null"); - } - Collection outputSurfaces = request.getTargets(); - Range fpsRange = request.get(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE); - checkConstrainedHighSpeedSurfaces(outputSurfaces, fpsRange); - - // Request list size: to limit the preview to 30fps, need use maxFps/30; to maximize - // the preview frame rate, should use maxBatch size for that high speed stream - // configuration. We choose the former for now. - int requestListSize = fpsRange.getUpper() / 30; - List requestList = new ArrayList(); - - // Prepare the Request builders: need carry over the request controls. - // First, create a request builder that will only include preview or recording target. - CameraMetadataNative requestMetadata = new CameraMetadataNative(request.getNativeCopy()); - // Note that after this step, the requestMetadata is mutated (swapped) and can not be used - // for next request builder creation. - CaptureRequest.Builder singleTargetRequestBuilder = new CaptureRequest.Builder( - requestMetadata, /*reprocess*/false, CameraCaptureSession.SESSION_ID_NONE); - - // Overwrite the capture intent to make sure a good value is set. - Iterator iterator = outputSurfaces.iterator(); - Surface firstSurface = iterator.next(); - Surface secondSurface = null; - if (outputSurfaces.size() == 1 && SurfaceUtils.isSurfaceForHwVideoEncoder(firstSurface)) { - singleTargetRequestBuilder.set(CaptureRequest.CONTROL_CAPTURE_INTENT, - CaptureRequest.CONTROL_CAPTURE_INTENT_PREVIEW); - } else { - // Video only, or preview + video - singleTargetRequestBuilder.set(CaptureRequest.CONTROL_CAPTURE_INTENT, - CaptureRequest.CONTROL_CAPTURE_INTENT_VIDEO_RECORD); - } - singleTargetRequestBuilder.setPartOfCHSRequestList(/*partOfCHSList*/true); - - // Second, Create a request builder that will include both preview and recording targets. - CaptureRequest.Builder doubleTargetRequestBuilder = null; - if (outputSurfaces.size() == 2) { - // Have to create a new copy, the original one was mutated after a new - // CaptureRequest.Builder creation. - requestMetadata = new CameraMetadataNative(request.getNativeCopy()); - doubleTargetRequestBuilder = new CaptureRequest.Builder( - requestMetadata, /*reprocess*/false, CameraCaptureSession.SESSION_ID_NONE); - doubleTargetRequestBuilder.set(CaptureRequest.CONTROL_CAPTURE_INTENT, - CaptureRequest.CONTROL_CAPTURE_INTENT_VIDEO_RECORD); - doubleTargetRequestBuilder.addTarget(firstSurface); - secondSurface = iterator.next(); - doubleTargetRequestBuilder.addTarget(secondSurface); - doubleTargetRequestBuilder.setPartOfCHSRequestList(/*partOfCHSList*/true); - // Make sure singleTargetRequestBuilder contains only recording surface for - // preview + recording case. - Surface recordingSurface = firstSurface; - if (!SurfaceUtils.isSurfaceForHwVideoEncoder(recordingSurface)) { - recordingSurface = secondSurface; - } - singleTargetRequestBuilder.addTarget(recordingSurface); - } else { - // Single output case: either recording or preview. - singleTargetRequestBuilder.addTarget(firstSurface); - } - - // Generate the final request list. - for (int i = 0; i < requestListSize; i++) { - if (i == 0 && doubleTargetRequestBuilder != null) { - // First request should be recording + preview request - requestList.add(doubleTargetRequestBuilder.build()); - } else { - requestList.add(singleTargetRequestBuilder.build()); - } - } - - return Collections.unmodifiableList(requestList); - } } diff --git a/core/java/android/hardware/camera2/params/StreamConfigurationMap.java b/core/java/android/hardware/camera2/params/StreamConfigurationMap.java index 639ad60..8e0eab2 100644 --- a/core/java/android/hardware/camera2/params/StreamConfigurationMap.java +++ b/core/java/android/hardware/camera2/params/StreamConfigurationMap.java @@ -495,7 +495,8 @@ public final class StreamConfigurationMap { *

* To enable high speed video recording, application must create a constrained create high speed * capture session via {@link CameraDevice#createConstrainedHighSpeedCaptureSession}, and submit - * a CaptureRequest list created by {@link CameraDevice#createConstrainedHighSpeedRequestList} + * a CaptureRequest list created by + * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList} * to this session. The application must select the video size from this method and * {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS range} from * {@link #getHighSpeedVideoFpsRangesFor} to configure the constrained high speed session and @@ -506,14 +507,15 @@ public final class StreamConfigurationMap { * the same size). Otherwise, the high speed session creation will fail. Once the size is * selected, application can get the supported FPS ranges by * {@link #getHighSpeedVideoFpsRangesFor}, and use these FPS ranges to setup the recording - * request lists via {@link CameraDevice#createConstrainedHighSpeedRequestList}. + * request lists via + * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList}. *

* * @return an array of supported high speed video recording sizes * @see #getHighSpeedVideoFpsRangesFor(Size) * @see CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO * @see CameraDevice#createConstrainedHighSpeedCaptureSession - * @see CameraDevice#createConstrainedHighSpeedRequestList + * @see android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList */ public Size[] getHighSpeedVideoSizes() { Set keySet = mHighSpeedVideoSizeMap.keySet(); @@ -571,7 +573,8 @@ public final class StreamConfigurationMap { *

* To enable high speed video recording, application must create a constrained create high speed * capture session via {@link CameraDevice#createConstrainedHighSpeedCaptureSession}, and submit - * a CaptureRequest list created by {@link CameraDevice#createConstrainedHighSpeedRequestList} + * a CaptureRequest list created by + * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList} * to this session. The application must select the video size from this method and * {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS range} from * {@link #getHighSpeedVideoFpsRangesFor} to configure the constrained high speed session and @@ -583,7 +586,7 @@ public final class StreamConfigurationMap { * recording streams must have the same size). Otherwise, the high speed session creation will * fail. Once the high speed capture session is created, the application can set the FPS range * in the recording request lists via - * {@link CameraDevice#createConstrainedHighSpeedRequestList}. + * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList}. *

*

* The FPS ranges reported by this method will have below characteristics: @@ -601,7 +604,7 @@ public final class StreamConfigurationMap { * @see #getHighSpeedVideoSizesFor * @see CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO * @see CameraDevice#createConstrainedHighSpeedCaptureSession - * @see CameraDevice#createConstrainedHighSpeedRequestList + * @see CameraDevice#createHighSpeedRequestList */ @SuppressWarnings("unchecked") public Range[] getHighSpeedVideoFpsRanges() { diff --git a/core/java/android/hardware/camera2/utils/SurfaceUtils.java b/core/java/android/hardware/camera2/utils/SurfaceUtils.java index 064b21a..4b958df 100644 --- a/core/java/android/hardware/camera2/utils/SurfaceUtils.java +++ b/core/java/android/hardware/camera2/utils/SurfaceUtils.java @@ -19,9 +19,16 @@ package android.hardware.camera2.utils; import android.graphics.ImageFormat; import android.hardware.camera2.legacy.LegacyCameraDevice; import android.hardware.camera2.legacy.LegacyExceptionUtils.BufferQueueAbandonedException; +import android.hardware.camera2.params.StreamConfigurationMap; +import android.util.Range; import android.util.Size; import android.view.Surface; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + /** * Various Surface utilities. */ @@ -105,4 +112,93 @@ public class SurfaceUtils { return LegacyCameraDevice.isFlexibleConsumer(output); } + /** + * A high speed output surface can only be preview or hardware encoder surface. + * + * @param surface The high speed output surface to be checked. + */ + private static void checkHighSpeedSurfaceFormat(Surface surface) { + // TODO: remove this override since the default format should be + // ImageFormat.PRIVATE. b/9487482 + final int HAL_FORMAT_RGB_START = 1; // HAL_PIXEL_FORMAT_RGBA_8888 from graphics.h + final int HAL_FORMAT_RGB_END = 5; // HAL_PIXEL_FORMAT_BGRA_8888 from graphics.h + int surfaceFormat = SurfaceUtils.getSurfaceFormat(surface); + if (surfaceFormat >= HAL_FORMAT_RGB_START && + surfaceFormat <= HAL_FORMAT_RGB_END) { + surfaceFormat = ImageFormat.PRIVATE; + } + + if (surfaceFormat != ImageFormat.PRIVATE) { + throw new IllegalArgumentException("Surface format(" + surfaceFormat + ") is not" + + " for preview or hardware video encoding!"); + } + } + + /** + * Verify that that the surfaces are valid for high-speed recording mode, + * and that the FPS range is supported + * + * @param surfaces the surfaces to verify as valid in terms of size and format + * @param fpsRange the target high-speed FPS range to validate + * @param config The stream configuration map for the device in question + */ + public static void checkConstrainedHighSpeedSurfaces(Collection surfaces, + Range fpsRange, StreamConfigurationMap config) { + if (surfaces == null || surfaces.size() == 0 || surfaces.size() > 2) { + throw new IllegalArgumentException("Output target surface list must not be null and" + + " the size must be 1 or 2"); + } + + List highSpeedSizes = null; + if (fpsRange == null) { + highSpeedSizes = Arrays.asList(config.getHighSpeedVideoSizes()); + } else { + // Check the FPS range first if provided + Range[] highSpeedFpsRanges = config.getHighSpeedVideoFpsRanges(); + if(!Arrays.asList(highSpeedFpsRanges).contains(fpsRange)) { + throw new IllegalArgumentException("Fps range " + fpsRange.toString() + " in the" + + " request is not a supported high speed fps range " + + Arrays.toString(highSpeedFpsRanges)); + } + highSpeedSizes = Arrays.asList(config.getHighSpeedVideoSizesFor(fpsRange)); + } + + for (Surface surface : surfaces) { + checkHighSpeedSurfaceFormat(surface); + + // Surface size must be supported high speed sizes. + Size surfaceSize = SurfaceUtils.getSurfaceSize(surface); + if (!highSpeedSizes.contains(surfaceSize)) { + throw new IllegalArgumentException("Surface size " + surfaceSize.toString() + " is" + + " not part of the high speed supported size list " + + Arrays.toString(highSpeedSizes.toArray())); + } + // Each output surface must be either preview surface or recording surface. + if (!SurfaceUtils.isSurfaceForPreview(surface) && + !SurfaceUtils.isSurfaceForHwVideoEncoder(surface)) { + throw new IllegalArgumentException("This output surface is neither preview nor " + + "hardware video encoding surface"); + } + if (SurfaceUtils.isSurfaceForPreview(surface) && + SurfaceUtils.isSurfaceForHwVideoEncoder(surface)) { + throw new IllegalArgumentException("This output surface can not be both preview" + + " and hardware video encoding surface"); + } + } + + // For 2 output surface case, they shouldn't be same type. + if (surfaces.size() == 2) { + // Up to here, each surface can only be either preview or recording. + Iterator iterator = surfaces.iterator(); + boolean isFirstSurfacePreview = + SurfaceUtils.isSurfaceForPreview(iterator.next()); + boolean isSecondSurfacePreview = + SurfaceUtils.isSurfaceForPreview(iterator.next()); + if (isFirstSurfacePreview == isSecondSurfacePreview) { + throw new IllegalArgumentException("The 2 output surfaces must have different" + + " type"); + } + } + } + } -- cgit v1.1