Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.hardware.camera2.legacy; |
| 18 | |
Ruben Brunk | 0c79884 | 2014-09-30 03:42:13 -0700 | [diff] [blame] | 19 | import android.graphics.ImageFormat; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 20 | import android.graphics.SurfaceTexture; |
| 21 | import android.hardware.Camera; |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 22 | import android.hardware.camera2.CameraCharacteristics; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 23 | import android.hardware.camera2.CaptureRequest; |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 24 | import android.hardware.camera2.impl.CameraDeviceImpl; |
Ruben Brunk | 0c79884 | 2014-09-30 03:42:13 -0700 | [diff] [blame] | 25 | import android.hardware.camera2.params.StreamConfigurationMap; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 26 | import android.hardware.camera2.utils.LongParcelable; |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 27 | import android.hardware.camera2.utils.SizeAreaComparator; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 28 | import android.hardware.camera2.impl.CameraMetadataNative; |
| 29 | import android.os.ConditionVariable; |
| 30 | import android.os.Handler; |
| 31 | import android.os.Message; |
| 32 | import android.os.SystemClock; |
| 33 | import android.util.Log; |
Igor Murashkin | 83d8639 | 2014-07-18 14:37:19 -0700 | [diff] [blame] | 34 | import android.util.MutableLong; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 35 | import android.util.Pair; |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 36 | import android.util.Size; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 37 | import android.view.Surface; |
| 38 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 39 | import java.io.IOException; |
| 40 | import java.util.ArrayList; |
Ruben Brunk | 0c79884 | 2014-09-30 03:42:13 -0700 | [diff] [blame] | 41 | import java.util.Arrays; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 42 | import java.util.Collection; |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 43 | import java.util.Collections; |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 44 | import java.util.Iterator; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 45 | import java.util.List; |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 46 | import java.util.concurrent.TimeUnit; |
Ruben Brunk | e3c0434 | 2015-02-04 17:26:17 -0800 | [diff] [blame] | 47 | import java.util.concurrent.atomic.AtomicBoolean; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 48 | |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 49 | import static com.android.internal.util.Preconditions.*; |
| 50 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 51 | /** |
| 52 | * This class executes requests to the {@link Camera}. |
| 53 | * |
| 54 | * <p> |
| 55 | * The main components of this class are: |
| 56 | * - A message queue of requests to the {@link Camera}. |
| 57 | * - A thread that consumes requests to the {@link Camera} and executes them. |
| 58 | * - A {@link GLThreadManager} that draws to the configured output {@link Surface}s. |
| 59 | * - An {@link CameraDeviceState} state machine that manages the callbacks for various operations. |
| 60 | * </p> |
| 61 | */ |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 62 | @SuppressWarnings("deprecation") |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 63 | public class RequestThreadManager { |
| 64 | private final String TAG; |
| 65 | private final int mCameraId; |
| 66 | private final RequestHandlerThread mRequestThread; |
| 67 | |
| 68 | private static final boolean DEBUG = Log.isLoggable(LegacyCameraDevice.DEBUG_PROP, Log.DEBUG); |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 69 | // For slightly more spammy messages that will get repeated every frame |
| 70 | private static final boolean VERBOSE = |
| 71 | Log.isLoggable(LegacyCameraDevice.DEBUG_PROP, Log.VERBOSE); |
Ruben Brunk | e3c0434 | 2015-02-04 17:26:17 -0800 | [diff] [blame] | 72 | private Camera mCamera; |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 73 | private final CameraCharacteristics mCharacteristics; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 74 | |
| 75 | private final CameraDeviceState mDeviceState; |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 76 | private final CaptureCollector mCaptureCollector; |
Igor Murashkin | 83d8639 | 2014-07-18 14:37:19 -0700 | [diff] [blame] | 77 | private final LegacyFocusStateMapper mFocusStateMapper; |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 78 | private final LegacyFaceDetectMapper mFaceDetectMapper; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 79 | |
| 80 | private static final int MSG_CONFIGURE_OUTPUTS = 1; |
| 81 | private static final int MSG_SUBMIT_CAPTURE_REQUEST = 2; |
| 82 | private static final int MSG_CLEANUP = 3; |
| 83 | |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 84 | private static final int MAX_IN_FLIGHT_REQUESTS = 2; |
| 85 | |
Igor Murashkin | 3e2c14f | 2014-09-24 13:48:09 -0700 | [diff] [blame] | 86 | private static final int PREVIEW_FRAME_TIMEOUT = 1000; // ms |
Ruben Brunk | e3c0434 | 2015-02-04 17:26:17 -0800 | [diff] [blame] | 87 | private static final int JPEG_FRAME_TIMEOUT = 4000; // ms (same as CTS for API2) |
| 88 | private static final int REQUEST_COMPLETE_TIMEOUT = JPEG_FRAME_TIMEOUT; // ms (same as JPEG timeout) |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 89 | |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 90 | private static final float ASPECT_RATIO_TOLERANCE = 0.01f; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 91 | private boolean mPreviewRunning = false; |
| 92 | |
Ruben Brunk | 7f2372b | 2014-07-02 11:05:08 -0700 | [diff] [blame] | 93 | private final List<Surface> mPreviewOutputs = new ArrayList<>(); |
| 94 | private final List<Surface> mCallbackOutputs = new ArrayList<>(); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 95 | private GLThreadManager mGLThreadManager; |
| 96 | private SurfaceTexture mPreviewTexture; |
Ruben Brunk | 3e4fed2 | 2014-06-18 17:08:42 -0700 | [diff] [blame] | 97 | private Camera.Parameters mParams; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 98 | |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 99 | private final List<Long> mJpegSurfaceIds = new ArrayList<>(); |
| 100 | |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 101 | private Size mIntermediateBufferSize; |
| 102 | |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 103 | private final RequestQueue mRequestQueue = new RequestQueue(mJpegSurfaceIds); |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 104 | private LegacyRequest mLastRequest = null; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 105 | private SurfaceTexture mDummyTexture; |
| 106 | private Surface mDummySurface; |
| 107 | |
Ruben Brunk | 4aed87a | 2014-09-21 18:35:31 -0700 | [diff] [blame] | 108 | private final Object mIdleLock = new Object(); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 109 | private final FpsCounter mPrevCounter = new FpsCounter("Incoming Preview"); |
Ruben Brunk | 3e4fed2 | 2014-06-18 17:08:42 -0700 | [diff] [blame] | 110 | private final FpsCounter mRequestCounter = new FpsCounter("Incoming Requests"); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 111 | |
Ruben Brunk | e3c0434 | 2015-02-04 17:26:17 -0800 | [diff] [blame] | 112 | private final AtomicBoolean mQuit = new AtomicBoolean(false); |
| 113 | |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 114 | // Stuff JPEGs into HAL_PIXEL_FORMAT_RGBA_8888 gralloc buffers to get around SW write |
| 115 | // limitations for (b/17379185). |
| 116 | private static final boolean USE_BLOB_FORMAT_OVERRIDE = true; |
| 117 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Container object for Configure messages. |
| 120 | */ |
| 121 | private static class ConfigureHolder { |
| 122 | public final ConditionVariable condition; |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 123 | public final Collection<Pair<Surface, Size>> surfaces; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 124 | |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 125 | public ConfigureHolder(ConditionVariable condition, Collection<Pair<Surface, |
| 126 | Size>> surfaces) { |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 127 | this.condition = condition; |
| 128 | this.surfaces = surfaces; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Counter class used to calculate and log the current FPS of frame production. |
| 134 | */ |
| 135 | public static class FpsCounter { |
| 136 | //TODO: Hook this up to SystTrace? |
| 137 | private static final String TAG = "FpsCounter"; |
| 138 | private int mFrameCount = 0; |
| 139 | private long mLastTime = 0; |
| 140 | private long mLastPrintTime = 0; |
| 141 | private double mLastFps = 0; |
| 142 | private final String mStreamType; |
| 143 | private static final long NANO_PER_SECOND = 1000000000; //ns |
| 144 | |
| 145 | public FpsCounter(String streamType) { |
| 146 | mStreamType = streamType; |
| 147 | } |
| 148 | |
| 149 | public synchronized void countFrame() { |
| 150 | mFrameCount++; |
| 151 | long nextTime = SystemClock.elapsedRealtimeNanos(); |
| 152 | if (mLastTime == 0) { |
| 153 | mLastTime = nextTime; |
| 154 | } |
| 155 | if (nextTime > mLastTime + NANO_PER_SECOND) { |
| 156 | long elapsed = nextTime - mLastTime; |
| 157 | mLastFps = mFrameCount * (NANO_PER_SECOND / (double) elapsed); |
| 158 | mFrameCount = 0; |
| 159 | mLastTime = nextTime; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | public synchronized double checkFps() { |
| 164 | return mLastFps; |
| 165 | } |
| 166 | |
| 167 | public synchronized void staggeredLog() { |
| 168 | if (mLastTime > mLastPrintTime + 5 * NANO_PER_SECOND) { |
| 169 | mLastPrintTime = mLastTime; |
| 170 | Log.d(TAG, "FPS for " + mStreamType + " stream: " + mLastFps ); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | public synchronized void countAndLog() { |
| 175 | countFrame(); |
| 176 | staggeredLog(); |
| 177 | } |
| 178 | } |
| 179 | /** |
| 180 | * Fake preview for jpeg captures when there is no active preview |
| 181 | */ |
| 182 | private void createDummySurface() { |
| 183 | if (mDummyTexture == null || mDummySurface == null) { |
| 184 | mDummyTexture = new SurfaceTexture(/*ignored*/0); |
| 185 | // TODO: use smallest default sizes |
| 186 | mDummyTexture.setDefaultBufferSize(640, 480); |
| 187 | mDummySurface = new Surface(mDummyTexture); |
| 188 | } |
| 189 | } |
| 190 | |
Ruben Brunk | 4aed87a | 2014-09-21 18:35:31 -0700 | [diff] [blame] | 191 | private final Camera.ErrorCallback mErrorCallback = new Camera.ErrorCallback() { |
| 192 | @Override |
| 193 | public void onError(int i, Camera camera) { |
| 194 | Log.e(TAG, "Received error " + i + " from the Camera1 ErrorCallback"); |
| 195 | mDeviceState.setError(CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
| 196 | } |
| 197 | }; |
| 198 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 199 | private final ConditionVariable mReceivedJpeg = new ConditionVariable(false); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 200 | |
| 201 | private final Camera.PictureCallback mJpegCallback = new Camera.PictureCallback() { |
| 202 | @Override |
| 203 | public void onPictureTaken(byte[] data, Camera camera) { |
| 204 | Log.i(TAG, "Received jpeg."); |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 205 | Pair<RequestHolder, Long> captureInfo = mCaptureCollector.jpegProduced(); |
Ruben Brunk | 2c3d7c5 | 2014-09-25 11:48:27 -0700 | [diff] [blame] | 206 | if (captureInfo == null || captureInfo.first == null) { |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 207 | Log.e(TAG, "Dropping jpeg frame."); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 208 | return; |
| 209 | } |
Ruben Brunk | 2c3d7c5 | 2014-09-25 11:48:27 -0700 | [diff] [blame] | 210 | RequestHolder holder = captureInfo.first; |
| 211 | long timestamp = captureInfo.second; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 212 | for (Surface s : holder.getHolderTargets()) { |
Ruben Brunk | ef14da3 | 2014-06-24 16:06:54 -0700 | [diff] [blame] | 213 | try { |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 214 | if (LegacyCameraDevice.containsSurfaceId(s, mJpegSurfaceIds)) { |
Ruben Brunk | ef14da3 | 2014-06-24 16:06:54 -0700 | [diff] [blame] | 215 | Log.i(TAG, "Producing jpeg buffer..."); |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 216 | |
| 217 | int totalSize = data.length + LegacyCameraDevice.nativeGetJpegFooterSize(); |
Ruben Brunk | 31798f3 | 2014-09-25 19:56:54 -0700 | [diff] [blame] | 218 | totalSize = (totalSize + 3) & ~0x3; // round up to nearest octonibble |
Ruben Brunk | 0c79884 | 2014-09-30 03:42:13 -0700 | [diff] [blame] | 219 | LegacyCameraDevice.setNextTimestamp(s, timestamp); |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 220 | |
Ruben Brunk | 31798f3 | 2014-09-25 19:56:54 -0700 | [diff] [blame] | 221 | if (USE_BLOB_FORMAT_OVERRIDE) { |
| 222 | // Override to RGBA_8888 format. |
| 223 | LegacyCameraDevice.setSurfaceFormat(s, |
| 224 | LegacyMetadataMapper.HAL_PIXEL_FORMAT_RGBA_8888); |
Ruben Brunk | 0c79884 | 2014-09-30 03:42:13 -0700 | [diff] [blame] | 225 | |
| 226 | int dimen = (int) Math.ceil(Math.sqrt(totalSize)); |
| 227 | dimen = (dimen + 0xf) & ~0xf; // round up to nearest multiple of 16 |
| 228 | LegacyCameraDevice.setSurfaceDimens(s, dimen, dimen); |
| 229 | LegacyCameraDevice.produceFrame(s, data, dimen, dimen, |
| 230 | CameraMetadataNative.NATIVE_JPEG_FORMAT); |
| 231 | } else { |
| 232 | LegacyCameraDevice.setSurfaceDimens(s, totalSize, /*height*/1); |
| 233 | LegacyCameraDevice.produceFrame(s, data, totalSize, /*height*/1, |
| 234 | CameraMetadataNative.NATIVE_JPEG_FORMAT); |
Ruben Brunk | 31798f3 | 2014-09-25 19:56:54 -0700 | [diff] [blame] | 235 | } |
Ruben Brunk | ef14da3 | 2014-06-24 16:06:54 -0700 | [diff] [blame] | 236 | } |
| 237 | } catch (LegacyExceptionUtils.BufferQueueAbandonedException e) { |
| 238 | Log.w(TAG, "Surface abandoned, dropping frame. ", e); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 241 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 242 | mReceivedJpeg.open(); |
| 243 | } |
| 244 | }; |
| 245 | |
Ruben Brunk | 3e4fed2 | 2014-06-18 17:08:42 -0700 | [diff] [blame] | 246 | private final Camera.ShutterCallback mJpegShutterCallback = new Camera.ShutterCallback() { |
| 247 | @Override |
| 248 | public void onShutter() { |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 249 | mCaptureCollector.jpegCaptured(SystemClock.elapsedRealtimeNanos()); |
Ruben Brunk | 3e4fed2 | 2014-06-18 17:08:42 -0700 | [diff] [blame] | 250 | } |
| 251 | }; |
| 252 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 253 | private final SurfaceTexture.OnFrameAvailableListener mPreviewCallback = |
| 254 | new SurfaceTexture.OnFrameAvailableListener() { |
| 255 | @Override |
| 256 | public void onFrameAvailable(SurfaceTexture surfaceTexture) { |
Ruben Brunk | d1f113d | 2014-07-11 11:46:20 -0700 | [diff] [blame] | 257 | if (DEBUG) { |
| 258 | mPrevCounter.countAndLog(); |
| 259 | } |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 260 | mGLThreadManager.queueNewFrame(); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 261 | } |
| 262 | }; |
| 263 | |
| 264 | private void stopPreview() { |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 265 | if (VERBOSE) { |
| 266 | Log.v(TAG, "stopPreview - preview running? " + mPreviewRunning); |
| 267 | } |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 268 | if (mPreviewRunning) { |
| 269 | mCamera.stopPreview(); |
| 270 | mPreviewRunning = false; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | private void startPreview() { |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 275 | if (VERBOSE) { |
| 276 | Log.v(TAG, "startPreview - preview running? " + mPreviewRunning); |
| 277 | } |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 278 | if (!mPreviewRunning) { |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 279 | // XX: CameraClient:;startPreview is not getting called after a stop |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 280 | mCamera.startPreview(); |
| 281 | mPreviewRunning = true; |
| 282 | } |
| 283 | } |
| 284 | |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 285 | private void doJpegCapturePrepare(RequestHolder request) throws IOException { |
| 286 | if (DEBUG) Log.d(TAG, "doJpegCapturePrepare - preview running? " + mPreviewRunning); |
Igor Murashkin | a296fec | 2014-06-23 14:44:09 -0700 | [diff] [blame] | 287 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 288 | if (!mPreviewRunning) { |
Igor Murashkin | a296fec | 2014-06-23 14:44:09 -0700 | [diff] [blame] | 289 | if (DEBUG) Log.d(TAG, "doJpegCapture - create fake surface"); |
| 290 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 291 | createDummySurface(); |
| 292 | mCamera.setPreviewTexture(mDummyTexture); |
| 293 | startPreview(); |
| 294 | } |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | private void doJpegCapture(RequestHolder request) { |
| 298 | if (DEBUG) Log.d(TAG, "doJpegCapturePrepare"); |
| 299 | |
Ruben Brunk | 3e4fed2 | 2014-06-18 17:08:42 -0700 | [diff] [blame] | 300 | mCamera.takePicture(mJpegShutterCallback, /*raw*/null, mJpegCallback); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 301 | mPreviewRunning = false; |
| 302 | } |
| 303 | |
| 304 | private void doPreviewCapture(RequestHolder request) throws IOException { |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 305 | if (VERBOSE) { |
| 306 | Log.v(TAG, "doPreviewCapture - preview running? " + mPreviewRunning); |
| 307 | } |
| 308 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 309 | if (mPreviewRunning) { |
| 310 | return; // Already running |
| 311 | } |
| 312 | |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 313 | if (mPreviewTexture == null) { |
| 314 | throw new IllegalStateException( |
| 315 | "Preview capture called with no preview surfaces configured."); |
| 316 | } |
| 317 | |
| 318 | mPreviewTexture.setDefaultBufferSize(mIntermediateBufferSize.getWidth(), |
| 319 | mIntermediateBufferSize.getHeight()); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 320 | mCamera.setPreviewTexture(mPreviewTexture); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 321 | |
| 322 | startPreview(); |
| 323 | } |
| 324 | |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 325 | private void configureOutputs(Collection<Pair<Surface, Size>> outputs) { |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 326 | if (DEBUG) { |
| 327 | String outputsStr = outputs == null ? "null" : (outputs.size() + " surfaces"); |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 328 | Log.d(TAG, "configureOutputs with " + outputsStr); |
| 329 | } |
| 330 | |
Ruben Brunk | e3c0434 | 2015-02-04 17:26:17 -0800 | [diff] [blame] | 331 | try { |
| 332 | stopPreview(); |
| 333 | } catch (RuntimeException e) { |
| 334 | Log.e(TAG, "Received device exception in configure call: ", e); |
| 335 | mDeviceState.setError( |
| 336 | CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
| 337 | return; |
| 338 | } |
| 339 | |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 340 | /* |
| 341 | * Try to release the previous preview's surface texture earlier if we end up |
| 342 | * using a different one; this also reduces the likelihood of getting into a deadlock |
| 343 | * when disconnecting from the old previous texture at a later time. |
| 344 | */ |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 345 | try { |
| 346 | mCamera.setPreviewTexture(/*surfaceTexture*/null); |
| 347 | } catch (IOException e) { |
| 348 | Log.w(TAG, "Failed to clear prior SurfaceTexture, may cause GL deadlock: ", e); |
Ruben Brunk | e3c0434 | 2015-02-04 17:26:17 -0800 | [diff] [blame] | 349 | } catch (RuntimeException e) { |
| 350 | Log.e(TAG, "Received device exception in configure call: ", e); |
| 351 | mDeviceState.setError( |
| 352 | CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
| 353 | return; |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 354 | } |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 355 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 356 | if (mGLThreadManager != null) { |
| 357 | mGLThreadManager.waitUntilStarted(); |
| 358 | mGLThreadManager.ignoreNewFrames(); |
| 359 | mGLThreadManager.waitUntilIdle(); |
| 360 | } |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 361 | resetJpegSurfaceFormats(mCallbackOutputs); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 362 | mPreviewOutputs.clear(); |
| 363 | mCallbackOutputs.clear(); |
Eino-Ville Talvala | 52571b9 | 2014-09-25 12:52:20 -0700 | [diff] [blame] | 364 | mJpegSurfaceIds.clear(); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 365 | mPreviewTexture = null; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 366 | |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 367 | List<Size> previewOutputSizes = new ArrayList<>(); |
| 368 | List<Size> callbackOutputSizes = new ArrayList<>(); |
| 369 | |
Ruben Brunk | 28c49c9 | 2014-06-16 18:43:59 -0700 | [diff] [blame] | 370 | int facing = mCharacteristics.get(CameraCharacteristics.LENS_FACING); |
| 371 | int orientation = mCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 372 | if (outputs != null) { |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 373 | for (Pair<Surface, Size> outPair : outputs) { |
| 374 | Surface s = outPair.first; |
| 375 | Size outSize = outPair.second; |
Ruben Brunk | ef14da3 | 2014-06-24 16:06:54 -0700 | [diff] [blame] | 376 | try { |
| 377 | int format = LegacyCameraDevice.detectSurfaceType(s); |
Ruben Brunk | 28c49c9 | 2014-06-16 18:43:59 -0700 | [diff] [blame] | 378 | LegacyCameraDevice.setSurfaceOrientation(s, facing, orientation); |
Ruben Brunk | ef14da3 | 2014-06-24 16:06:54 -0700 | [diff] [blame] | 379 | switch (format) { |
| 380 | case CameraMetadataNative.NATIVE_JPEG_FORMAT: |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 381 | if (USE_BLOB_FORMAT_OVERRIDE) { |
| 382 | // Override to RGBA_8888 format. |
| 383 | LegacyCameraDevice.setSurfaceFormat(s, |
| 384 | LegacyMetadataMapper.HAL_PIXEL_FORMAT_RGBA_8888); |
| 385 | } |
| 386 | mJpegSurfaceIds.add(LegacyCameraDevice.getSurfaceId(s)); |
Ruben Brunk | ef14da3 | 2014-06-24 16:06:54 -0700 | [diff] [blame] | 387 | mCallbackOutputs.add(s); |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 388 | callbackOutputSizes.add(outSize); |
Ruben Brunk | ef14da3 | 2014-06-24 16:06:54 -0700 | [diff] [blame] | 389 | break; |
| 390 | default: |
| 391 | mPreviewOutputs.add(s); |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 392 | previewOutputSizes.add(outSize); |
Ruben Brunk | ef14da3 | 2014-06-24 16:06:54 -0700 | [diff] [blame] | 393 | break; |
| 394 | } |
| 395 | } catch (LegacyExceptionUtils.BufferQueueAbandonedException e) { |
| 396 | Log.w(TAG, "Surface abandoned, skipping...", e); |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 397 | } |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
Eino-Ville Talvala | eecc904 | 2014-09-23 16:37:31 -0700 | [diff] [blame] | 400 | try { |
| 401 | mParams = mCamera.getParameters(); |
| 402 | } catch (RuntimeException e) { |
| 403 | Log.e(TAG, "Received device exception: ", e); |
| 404 | mDeviceState.setError( |
| 405 | CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
| 406 | return; |
| 407 | } |
Ruben Brunk | 7f2372b | 2014-07-02 11:05:08 -0700 | [diff] [blame] | 408 | |
| 409 | List<int[]> supportedFpsRanges = mParams.getSupportedPreviewFpsRange(); |
| 410 | int[] bestRange = getPhotoPreviewFpsRange(supportedFpsRanges); |
| 411 | if (DEBUG) { |
| 412 | Log.d(TAG, "doPreviewCapture - Selected range [" + |
| 413 | bestRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX] + "," + |
| 414 | bestRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX] + "]"); |
| 415 | } |
| 416 | mParams.setPreviewFpsRange(bestRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX], |
| 417 | bestRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]); |
Ruben Brunk | 7f2372b | 2014-07-02 11:05:08 -0700 | [diff] [blame] | 418 | |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 419 | if (previewOutputSizes.size() > 0) { |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 420 | |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 421 | Size largestOutput = SizeAreaComparator.findLargestByArea(previewOutputSizes); |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 422 | |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 423 | // Find largest jpeg dimension - assume to have the same aspect ratio as sensor. |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 424 | Size largestJpegDimen = ParameterUtils.getLargestSupportedJpegSizeByArea(mParams); |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 425 | |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 426 | List<Size> supportedPreviewSizes = ParameterUtils.convertSizeList( |
| 427 | mParams.getSupportedPreviewSizes()); |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 428 | |
| 429 | // Use smallest preview dimension with same aspect ratio as sensor that is >= than all |
| 430 | // of the configured output dimensions. If none exists, fall back to using the largest |
| 431 | // supported preview size. |
| 432 | long largestOutputArea = largestOutput.getHeight() * (long) largestOutput.getWidth(); |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 433 | Size bestPreviewDimen = SizeAreaComparator.findLargestByArea(supportedPreviewSizes); |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 434 | for (Size s : supportedPreviewSizes) { |
| 435 | long currArea = s.getWidth() * s.getHeight(); |
| 436 | long bestArea = bestPreviewDimen.getWidth() * bestPreviewDimen.getHeight(); |
| 437 | if (checkAspectRatiosMatch(largestJpegDimen, s) && (currArea < bestArea && |
| 438 | currArea >= largestOutputArea)) { |
| 439 | bestPreviewDimen = s; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | mIntermediateBufferSize = bestPreviewDimen; |
Ruben Brunk | 059d766 | 2014-09-09 18:46:00 -0700 | [diff] [blame] | 444 | mParams.setPreviewSize(mIntermediateBufferSize.getWidth(), |
| 445 | mIntermediateBufferSize.getHeight()); |
| 446 | |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 447 | if (DEBUG) { |
| 448 | Log.d(TAG, "Intermediate buffer selected with dimens: " + |
| 449 | bestPreviewDimen.toString()); |
| 450 | } |
| 451 | } else { |
| 452 | mIntermediateBufferSize = null; |
| 453 | if (DEBUG) { |
| 454 | Log.d(TAG, "No Intermediate buffer selected, no preview outputs were configured"); |
| 455 | } |
| 456 | } |
| 457 | |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 458 | Size smallestSupportedJpegSize = calculatePictureSize(mCallbackOutputs, |
| 459 | callbackOutputSizes, mParams); |
Igor Murashkin | a296fec | 2014-06-23 14:44:09 -0700 | [diff] [blame] | 460 | if (smallestSupportedJpegSize != null) { |
| 461 | /* |
| 462 | * Set takePicture size to the smallest supported JPEG size large enough |
| 463 | * to scale/crop out of for the bounding rectangle of the configured JPEG sizes. |
| 464 | */ |
| 465 | |
| 466 | Log.i(TAG, "configureOutputs - set take picture size to " + smallestSupportedJpegSize); |
| 467 | mParams.setPictureSize( |
| 468 | smallestSupportedJpegSize.getWidth(), smallestSupportedJpegSize.getHeight()); |
| 469 | } |
| 470 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 471 | // TODO: Detect and optimize single-output paths here to skip stream teeing. |
| 472 | if (mGLThreadManager == null) { |
Ruben Brunk | 4aed87a | 2014-09-21 18:35:31 -0700 | [diff] [blame] | 473 | mGLThreadManager = new GLThreadManager(mCameraId, facing, mDeviceState); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 474 | mGLThreadManager.start(); |
| 475 | } |
| 476 | mGLThreadManager.waitUntilStarted(); |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 477 | List<Pair<Surface, Size>> previews = new ArrayList<>(); |
| 478 | Iterator<Size> previewSizeIter = previewOutputSizes.iterator(); |
| 479 | for (Surface p : mPreviewOutputs) { |
| 480 | previews.add(new Pair<>(p, previewSizeIter.next())); |
| 481 | } |
| 482 | mGLThreadManager.setConfigurationAndWait(previews, mCaptureCollector); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 483 | mGLThreadManager.allowNewFrames(); |
| 484 | mPreviewTexture = mGLThreadManager.getCurrentSurfaceTexture(); |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 485 | if (mPreviewTexture != null) { |
| 486 | mPreviewTexture.setOnFrameAvailableListener(mPreviewCallback); |
| 487 | } |
Igor Murashkin | 5096def | 2014-06-24 10:49:11 -0700 | [diff] [blame] | 488 | |
Ruben Brunk | e3c0434 | 2015-02-04 17:26:17 -0800 | [diff] [blame] | 489 | try { |
| 490 | mCamera.setParameters(mParams); |
| 491 | } catch (RuntimeException e) { |
| 492 | Log.e(TAG, "Received device exception while configuring: ", e); |
| 493 | mDeviceState.setError( |
| 494 | CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
| 495 | |
| 496 | } |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | private void resetJpegSurfaceFormats(Collection<Surface> surfaces) { |
| 500 | if (!USE_BLOB_FORMAT_OVERRIDE || surfaces == null) { |
| 501 | return; |
| 502 | } |
| 503 | for(Surface s : surfaces) { |
| 504 | try { |
| 505 | LegacyCameraDevice.setSurfaceFormat(s, LegacyMetadataMapper.HAL_PIXEL_FORMAT_BLOB); |
| 506 | } catch (LegacyExceptionUtils.BufferQueueAbandonedException e) { |
| 507 | Log.w(TAG, "Surface abandoned, skipping...", e); |
| 508 | } |
| 509 | } |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Igor Murashkin | a296fec | 2014-06-23 14:44:09 -0700 | [diff] [blame] | 512 | /** |
| 513 | * Find a JPEG size (that is supported by the legacy camera device) which is equal to or larger |
| 514 | * than all of the configured {@code JPEG} outputs (by both width and height). |
| 515 | * |
| 516 | * <p>If multiple supported JPEG sizes are larger, select the smallest of them which |
| 517 | * still satisfies the above constraint.</p> |
| 518 | * |
| 519 | * <p>As a result, the returned size is guaranteed to be usable without needing |
| 520 | * to upscale any of the outputs. If only one {@code JPEG} surface is used, |
| 521 | * then no scaling/cropping is necessary between the taken picture and |
| 522 | * the {@code JPEG} output surface.</p> |
| 523 | * |
| 524 | * @param callbackOutputs a non-{@code null} list of {@code Surface}s with any image formats |
| 525 | * @param params api1 parameters (used for reading only) |
| 526 | * |
| 527 | * @return a size large enough to fit all of the configured {@code JPEG} outputs, or |
| 528 | * {@code null} if the {@code callbackOutputs} did not have any {@code JPEG} |
| 529 | * surfaces. |
| 530 | */ |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 531 | private Size calculatePictureSize( List<Surface> callbackOutputs, |
| 532 | List<Size> callbackSizes, Camera.Parameters params) { |
Igor Murashkin | a296fec | 2014-06-23 14:44:09 -0700 | [diff] [blame] | 533 | /* |
| 534 | * Find the largest JPEG size (if any), from the configured outputs: |
| 535 | * - the api1 picture size should be set to the smallest legal size that's at least as large |
| 536 | * as the largest configured JPEG size |
| 537 | */ |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 538 | if (callbackOutputs.size() != callbackSizes.size()) { |
| 539 | throw new IllegalStateException("Input collections must be same length"); |
| 540 | } |
| 541 | List<Size> configuredJpegSizes = new ArrayList<>(); |
| 542 | Iterator<Size> sizeIterator = callbackSizes.iterator(); |
Igor Murashkin | a296fec | 2014-06-23 14:44:09 -0700 | [diff] [blame] | 543 | for (Surface callbackSurface : callbackOutputs) { |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 544 | Size jpegSize = sizeIterator.next(); |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 545 | if (!LegacyCameraDevice.containsSurfaceId(callbackSurface, mJpegSurfaceIds)) { |
Ruben Brunk | ef14da3 | 2014-06-24 16:06:54 -0700 | [diff] [blame] | 546 | continue; // Ignore non-JPEG callback formats |
| 547 | } |
| 548 | |
Ruben Brunk | ef14da3 | 2014-06-24 16:06:54 -0700 | [diff] [blame] | 549 | configuredJpegSizes.add(jpegSize); |
Igor Murashkin | a296fec | 2014-06-23 14:44:09 -0700 | [diff] [blame] | 550 | } |
| 551 | if (!configuredJpegSizes.isEmpty()) { |
| 552 | /* |
| 553 | * Find the largest configured JPEG width, and height, independently |
| 554 | * of the rest. |
| 555 | * |
| 556 | * The rest of the JPEG streams can be cropped out of this smallest bounding |
| 557 | * rectangle. |
| 558 | */ |
| 559 | int maxConfiguredJpegWidth = -1; |
| 560 | int maxConfiguredJpegHeight = -1; |
| 561 | for (Size jpegSize : configuredJpegSizes) { |
| 562 | maxConfiguredJpegWidth = jpegSize.getWidth() > maxConfiguredJpegWidth ? |
| 563 | jpegSize.getWidth() : maxConfiguredJpegWidth; |
| 564 | maxConfiguredJpegHeight = jpegSize.getHeight() > maxConfiguredJpegHeight ? |
| 565 | jpegSize.getHeight() : maxConfiguredJpegHeight; |
| 566 | } |
| 567 | Size smallestBoundJpegSize = new Size(maxConfiguredJpegWidth, maxConfiguredJpegHeight); |
| 568 | |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 569 | List<Size> supportedJpegSizes = ParameterUtils.convertSizeList( |
| 570 | params.getSupportedPictureSizes()); |
Igor Murashkin | a296fec | 2014-06-23 14:44:09 -0700 | [diff] [blame] | 571 | |
| 572 | /* |
| 573 | * Find the smallest supported JPEG size that can fit the smallest bounding |
| 574 | * rectangle for the configured JPEG sizes. |
| 575 | */ |
| 576 | List<Size> candidateSupportedJpegSizes = new ArrayList<>(); |
| 577 | for (Size supportedJpegSize : supportedJpegSizes) { |
| 578 | if (supportedJpegSize.getWidth() >= maxConfiguredJpegWidth && |
| 579 | supportedJpegSize.getHeight() >= maxConfiguredJpegHeight) { |
| 580 | candidateSupportedJpegSizes.add(supportedJpegSize); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | if (candidateSupportedJpegSizes.isEmpty()) { |
| 585 | throw new AssertionError( |
| 586 | "Could not find any supported JPEG sizes large enough to fit " + |
| 587 | smallestBoundJpegSize); |
| 588 | } |
| 589 | |
| 590 | Size smallestSupportedJpegSize = Collections.min(candidateSupportedJpegSizes, |
| 591 | new SizeAreaComparator()); |
| 592 | |
| 593 | if (!smallestSupportedJpegSize.equals(smallestBoundJpegSize)) { |
| 594 | Log.w(TAG, |
| 595 | String.format( |
| 596 | "configureOutputs - Will need to crop picture %s into " |
| 597 | + "smallest bound size %s", |
| 598 | smallestSupportedJpegSize, smallestBoundJpegSize)); |
| 599 | } |
| 600 | |
| 601 | return smallestSupportedJpegSize; |
| 602 | } |
| 603 | |
| 604 | return null; |
| 605 | } |
| 606 | |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 607 | private static boolean checkAspectRatiosMatch(Size a, Size b) { |
| 608 | float aAspect = a.getWidth() / (float) a.getHeight(); |
| 609 | float bAspect = b.getWidth() / (float) b.getHeight(); |
| 610 | |
| 611 | return Math.abs(aAspect - bAspect) < ASPECT_RATIO_TOLERANCE; |
| 612 | } |
| 613 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 614 | // Calculate the highest FPS range supported |
| 615 | private int[] getPhotoPreviewFpsRange(List<int[]> frameRates) { |
| 616 | if (frameRates.size() == 0) { |
| 617 | Log.e(TAG, "No supported frame rates returned!"); |
| 618 | return null; |
| 619 | } |
| 620 | |
| 621 | int bestMin = 0; |
| 622 | int bestMax = 0; |
| 623 | int bestIndex = 0; |
| 624 | int index = 0; |
| 625 | for (int[] rate : frameRates) { |
| 626 | int minFps = rate[Camera.Parameters.PREVIEW_FPS_MIN_INDEX]; |
| 627 | int maxFps = rate[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]; |
| 628 | if (maxFps > bestMax || (maxFps == bestMax && minFps > bestMin)) { |
| 629 | bestMin = minFps; |
| 630 | bestMax = maxFps; |
| 631 | bestIndex = index; |
| 632 | } |
| 633 | index++; |
| 634 | } |
| 635 | |
| 636 | return frameRates.get(bestIndex); |
| 637 | } |
| 638 | |
| 639 | private final Handler.Callback mRequestHandlerCb = new Handler.Callback() { |
| 640 | private boolean mCleanup = false; |
Igor Murashkin | 83d8639 | 2014-07-18 14:37:19 -0700 | [diff] [blame] | 641 | private final LegacyResultMapper mMapper = new LegacyResultMapper(); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 642 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 643 | @Override |
| 644 | public boolean handleMessage(Message msg) { |
| 645 | if (mCleanup) { |
| 646 | return true; |
| 647 | } |
| 648 | |
Ruben Brunk | 3e4fed2 | 2014-06-18 17:08:42 -0700 | [diff] [blame] | 649 | if (DEBUG) { |
| 650 | Log.d(TAG, "Request thread handling message:" + msg.what); |
| 651 | } |
Ruben Brunk | d1f113d | 2014-07-11 11:46:20 -0700 | [diff] [blame] | 652 | long startTime = 0; |
| 653 | if (DEBUG) { |
| 654 | startTime = SystemClock.elapsedRealtimeNanos(); |
| 655 | } |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 656 | switch (msg.what) { |
| 657 | case MSG_CONFIGURE_OUTPUTS: |
| 658 | ConfigureHolder config = (ConfigureHolder) msg.obj; |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 659 | int sizes = config.surfaces != null ? config.surfaces.size() : 0; |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 660 | Log.i(TAG, "Configure outputs: " + sizes + " surfaces configured."); |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 661 | |
| 662 | try { |
| 663 | boolean success = mCaptureCollector.waitForEmpty(JPEG_FRAME_TIMEOUT, |
| 664 | TimeUnit.MILLISECONDS); |
| 665 | if (!success) { |
| 666 | Log.e(TAG, "Timed out while queueing configure request."); |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 667 | mCaptureCollector.failAll(); |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 668 | } |
| 669 | } catch (InterruptedException e) { |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 670 | Log.e(TAG, "Interrupted while waiting for requests to complete."); |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 671 | mDeviceState.setError( |
| 672 | CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
| 673 | break; |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 676 | configureOutputs(config.surfaces); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 677 | config.condition.open(); |
Ruben Brunk | d1f113d | 2014-07-11 11:46:20 -0700 | [diff] [blame] | 678 | if (DEBUG) { |
| 679 | long totalTime = SystemClock.elapsedRealtimeNanos() - startTime; |
| 680 | Log.d(TAG, "Configure took " + totalTime + " ns"); |
| 681 | } |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 682 | break; |
| 683 | case MSG_SUBMIT_CAPTURE_REQUEST: |
| 684 | Handler handler = RequestThreadManager.this.mRequestThread.getHandler(); |
| 685 | |
| 686 | // Get the next burst from the request queue. |
| 687 | Pair<BurstHolder, Long> nextBurst = mRequestQueue.getNext(); |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 688 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 689 | if (nextBurst == null) { |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 690 | // If there are no further requests queued, wait for any currently executing |
| 691 | // requests to complete, then switch to idle state. |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 692 | try { |
| 693 | boolean success = mCaptureCollector.waitForEmpty(JPEG_FRAME_TIMEOUT, |
| 694 | TimeUnit.MILLISECONDS); |
| 695 | if (!success) { |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 696 | Log.e(TAG, |
| 697 | "Timed out while waiting for prior requests to complete."); |
| 698 | mCaptureCollector.failAll(); |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 699 | } |
| 700 | } catch (InterruptedException e) { |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 701 | Log.e(TAG, "Interrupted while waiting for requests to complete: ", e); |
| 702 | mDeviceState.setError( |
| 703 | CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
| 704 | break; |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 705 | } |
Ruben Brunk | 4aed87a | 2014-09-21 18:35:31 -0700 | [diff] [blame] | 706 | |
| 707 | synchronized (mIdleLock) { |
| 708 | // Retry the the request queue. |
| 709 | nextBurst = mRequestQueue.getNext(); |
| 710 | |
| 711 | // If we still have no queued requests, go idle. |
| 712 | if (nextBurst == null) { |
| 713 | mDeviceState.setIdle(); |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | if (nextBurst != null) { |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 720 | // Queue another capture if we did not get the last burst. |
| 721 | handler.sendEmptyMessage(MSG_SUBMIT_CAPTURE_REQUEST); |
| 722 | } |
| 723 | |
| 724 | // Complete each request in the burst |
| 725 | List<RequestHolder> requests = |
| 726 | nextBurst.first.produceRequestHolders(nextBurst.second); |
| 727 | for (RequestHolder holder : requests) { |
Ruben Brunk | 5776aaf | 2014-06-19 13:42:40 -0700 | [diff] [blame] | 728 | CaptureRequest request = holder.getRequest(); |
Ruben Brunk | d1f113d | 2014-07-11 11:46:20 -0700 | [diff] [blame] | 729 | |
| 730 | boolean paramsChanged = false; |
Igor Murashkin | 83d8639 | 2014-07-18 14:37:19 -0700 | [diff] [blame] | 731 | |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 732 | // Only update parameters if the request has changed |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 733 | if (mLastRequest == null || mLastRequest.captureRequest != request) { |
| 734 | |
| 735 | // The intermediate buffer is sometimes null, but we always need |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 736 | // the Camera1 API configured preview size |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 737 | Size previewSize = ParameterUtils.convertSize(mParams.getPreviewSize()); |
| 738 | |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 739 | LegacyRequest legacyRequest = new LegacyRequest(mCharacteristics, |
| 740 | request, previewSize, mParams); // params are copied |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 741 | |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 742 | |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 743 | // Parameters are mutated as a side-effect |
| 744 | LegacyMetadataMapper.convertRequestMetadata(/*inout*/legacyRequest); |
| 745 | |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 746 | // If the parameters have changed, set them in the Camera1 API. |
Ruben Brunk | d1f113d | 2014-07-11 11:46:20 -0700 | [diff] [blame] | 747 | if (!mParams.same(legacyRequest.parameters)) { |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 748 | try { |
| 749 | mCamera.setParameters(legacyRequest.parameters); |
| 750 | } catch (RuntimeException e) { |
| 751 | // If setting the parameters failed, report a request error to |
| 752 | // the camera client, and skip any further work for this request |
| 753 | Log.e(TAG, "Exception while setting camera parameters: ", e); |
| 754 | holder.failRequest(); |
| 755 | mDeviceState.setCaptureStart(holder, /*timestamp*/0, |
| 756 | CameraDeviceImpl.CameraDeviceCallbacks. |
| 757 | ERROR_CAMERA_REQUEST); |
| 758 | continue; |
| 759 | } |
Ruben Brunk | d1f113d | 2014-07-11 11:46:20 -0700 | [diff] [blame] | 760 | paramsChanged = true; |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 761 | mParams = legacyRequest.parameters; |
Ruben Brunk | d1f113d | 2014-07-11 11:46:20 -0700 | [diff] [blame] | 762 | } |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 763 | |
| 764 | mLastRequest = legacyRequest; |
Ruben Brunk | 5776aaf | 2014-06-19 13:42:40 -0700 | [diff] [blame] | 765 | } |
Ruben Brunk | d1f113d | 2014-07-11 11:46:20 -0700 | [diff] [blame] | 766 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 767 | try { |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 768 | boolean success = mCaptureCollector.queueRequest(holder, |
| 769 | mLastRequest, JPEG_FRAME_TIMEOUT, TimeUnit.MILLISECONDS); |
| 770 | |
| 771 | if (!success) { |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 772 | // Report a request error if we timed out while queuing this. |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 773 | Log.e(TAG, "Timed out while queueing capture request."); |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 774 | holder.failRequest(); |
| 775 | mDeviceState.setCaptureStart(holder, /*timestamp*/0, |
| 776 | CameraDeviceImpl.CameraDeviceCallbacks. |
| 777 | ERROR_CAMERA_REQUEST); |
| 778 | continue; |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 779 | } |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 780 | |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 781 | // Starting the preview needs to happen before enabling |
| 782 | // face detection or auto focus |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 783 | if (holder.hasPreviewTargets()) { |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 784 | doPreviewCapture(holder); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 785 | } |
| 786 | if (holder.hasJpegTargets()) { |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 787 | while(!mCaptureCollector.waitForPreviewsEmpty(PREVIEW_FRAME_TIMEOUT, |
| 788 | TimeUnit.MILLISECONDS)) { |
| 789 | // Fail preview requests until the queue is empty. |
| 790 | Log.e(TAG, "Timed out while waiting for preview requests to " + |
| 791 | "complete."); |
| 792 | mCaptureCollector.failNextPreview(); |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 793 | } |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 794 | mReceivedJpeg.close(); |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 795 | doJpegCapturePrepare(holder); |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /* |
| 799 | * Do all the actions that require a preview to have been started |
| 800 | */ |
| 801 | |
| 802 | // Toggle face detection on/off |
| 803 | // - do this before AF to give AF a chance to use faces |
| 804 | mFaceDetectMapper.processFaceDetectMode(request, /*in*/mParams); |
| 805 | |
| 806 | // Unconditionally process AF triggers, since they're non-idempotent |
| 807 | // - must be done after setting the most-up-to-date AF mode |
| 808 | mFocusStateMapper.processRequestTriggers(request, mParams); |
| 809 | |
| 810 | if (holder.hasJpegTargets()) { |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 811 | doJpegCapture(holder); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 812 | if (!mReceivedJpeg.block(JPEG_FRAME_TIMEOUT)) { |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 813 | Log.e(TAG, "Hit timeout for jpeg callback!"); |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 814 | mCaptureCollector.failNextJpeg(); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 815 | } |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 816 | } |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 817 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 818 | } catch (IOException e) { |
Ruben Brunk | e3c0434 | 2015-02-04 17:26:17 -0800 | [diff] [blame] | 819 | Log.e(TAG, "Received device exception during capture call: ", e); |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 820 | mDeviceState.setError( |
| 821 | CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
| 822 | break; |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 823 | } catch (InterruptedException e) { |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 824 | Log.e(TAG, "Interrupted during capture: ", e); |
| 825 | mDeviceState.setError( |
| 826 | CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
| 827 | break; |
Ruben Brunk | e3c0434 | 2015-02-04 17:26:17 -0800 | [diff] [blame] | 828 | } catch (RuntimeException e) { |
| 829 | Log.e(TAG, "Received device exception during capture call: ", e); |
| 830 | mDeviceState.setError( |
| 831 | CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
| 832 | break; |
Ruben Brunk | 91b9aab | 2014-06-20 00:24:56 -0700 | [diff] [blame] | 833 | } |
Ruben Brunk | d1f113d | 2014-07-11 11:46:20 -0700 | [diff] [blame] | 834 | |
| 835 | if (paramsChanged) { |
| 836 | if (DEBUG) { |
| 837 | Log.d(TAG, "Params changed -- getting new Parameters from HAL."); |
| 838 | } |
Eino-Ville Talvala | eecc904 | 2014-09-23 16:37:31 -0700 | [diff] [blame] | 839 | try { |
| 840 | mParams = mCamera.getParameters(); |
| 841 | } catch (RuntimeException e) { |
| 842 | Log.e(TAG, "Received device exception: ", e); |
| 843 | mDeviceState.setError( |
| 844 | CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
| 845 | break; |
| 846 | } |
Ruben Brunk | d1f113d | 2014-07-11 11:46:20 -0700 | [diff] [blame] | 847 | |
| 848 | // Update parameters to the latest that we think the camera is using |
| 849 | mLastRequest.setParameters(mParams); |
| 850 | } |
Igor Murashkin | 83d8639 | 2014-07-18 14:37:19 -0700 | [diff] [blame] | 851 | |
| 852 | MutableLong timestampMutable = new MutableLong(/*value*/0L); |
| 853 | try { |
| 854 | boolean success = mCaptureCollector.waitForRequestCompleted(holder, |
| 855 | REQUEST_COMPLETE_TIMEOUT, TimeUnit.MILLISECONDS, |
| 856 | /*out*/timestampMutable); |
| 857 | |
| 858 | if (!success) { |
| 859 | Log.e(TAG, "Timed out while waiting for request to complete."); |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 860 | mCaptureCollector.failAll(); |
Igor Murashkin | 83d8639 | 2014-07-18 14:37:19 -0700 | [diff] [blame] | 861 | } |
| 862 | } catch (InterruptedException e) { |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 863 | Log.e(TAG, "Interrupted waiting for request completion: ", e); |
| 864 | mDeviceState.setError( |
| 865 | CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
| 866 | break; |
Igor Murashkin | 83d8639 | 2014-07-18 14:37:19 -0700 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | CameraMetadataNative result = mMapper.cachedConvertResultMetadata( |
Igor Murashkin | 56678d8 | 2014-07-28 10:50:08 -0700 | [diff] [blame] | 870 | mLastRequest, timestampMutable.value); |
Igor Murashkin | 8c4486c1 | 2014-08-08 14:02:36 -0700 | [diff] [blame] | 871 | /* |
| 872 | * Order matters: The default result mapper is state-less; the |
| 873 | * other mappers carry state and may override keys set by the default |
| 874 | * mapper with their own values. |
| 875 | */ |
| 876 | |
Igor Murashkin | 83d8639 | 2014-07-18 14:37:19 -0700 | [diff] [blame] | 877 | // Update AF state |
| 878 | mFocusStateMapper.mapResultTriggers(result); |
Igor Murashkin | 8c4486c1 | 2014-08-08 14:02:36 -0700 | [diff] [blame] | 879 | // Update face-related results |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 880 | mFaceDetectMapper.mapResultFaces(result, mLastRequest); |
Igor Murashkin | 83d8639 | 2014-07-18 14:37:19 -0700 | [diff] [blame] | 881 | |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 882 | if (!holder.requestFailed()) { |
| 883 | mDeviceState.setCaptureResult(holder, result, |
| 884 | CameraDeviceState.NO_CAPTURE_ERROR); |
| 885 | } |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 886 | } |
Ruben Brunk | 3e4fed2 | 2014-06-18 17:08:42 -0700 | [diff] [blame] | 887 | if (DEBUG) { |
Ruben Brunk | d1f113d | 2014-07-11 11:46:20 -0700 | [diff] [blame] | 888 | long totalTime = SystemClock.elapsedRealtimeNanos() - startTime; |
| 889 | Log.d(TAG, "Capture request took " + totalTime + " ns"); |
Ruben Brunk | 3e4fed2 | 2014-06-18 17:08:42 -0700 | [diff] [blame] | 890 | mRequestCounter.countAndLog(); |
| 891 | } |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 892 | break; |
| 893 | case MSG_CLEANUP: |
| 894 | mCleanup = true; |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 895 | try { |
| 896 | boolean success = mCaptureCollector.waitForEmpty(JPEG_FRAME_TIMEOUT, |
| 897 | TimeUnit.MILLISECONDS); |
| 898 | if (!success) { |
| 899 | Log.e(TAG, "Timed out while queueing cleanup request."); |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 900 | mCaptureCollector.failAll(); |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 901 | } |
| 902 | } catch (InterruptedException e) { |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 903 | Log.e(TAG, "Interrupted while waiting for requests to complete: ", e); |
| 904 | mDeviceState.setError( |
| 905 | CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE); |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 906 | } |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 907 | if (mGLThreadManager != null) { |
| 908 | mGLThreadManager.quit(); |
Ruben Brunk | e3c0434 | 2015-02-04 17:26:17 -0800 | [diff] [blame] | 909 | mGLThreadManager = null; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 910 | } |
| 911 | if (mCamera != null) { |
| 912 | mCamera.release(); |
Ruben Brunk | e3c0434 | 2015-02-04 17:26:17 -0800 | [diff] [blame] | 913 | mCamera = null; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 914 | } |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 915 | resetJpegSurfaceFormats(mCallbackOutputs); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 916 | break; |
Igor Murashkin | 51dcfd65 | 2014-09-25 16:55:01 -0700 | [diff] [blame] | 917 | case RequestHandlerThread.MSG_POKE_IDLE_HANDLER: |
| 918 | // OK: Ignore message. |
| 919 | break; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 920 | default: |
| 921 | throw new AssertionError("Unhandled message " + msg.what + |
| 922 | " on RequestThread."); |
| 923 | } |
| 924 | return true; |
| 925 | } |
| 926 | }; |
| 927 | |
| 928 | /** |
| 929 | * Create a new RequestThreadManager. |
| 930 | * |
| 931 | * @param cameraId the id of the camera to use. |
| 932 | * @param camera an open camera object. The RequestThreadManager takes ownership of this camera |
| 933 | * object, and is responsible for closing it. |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 934 | * @param characteristics the static camera characteristics corresponding to this camera device |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 935 | * @param deviceState a {@link CameraDeviceState} state machine. |
| 936 | */ |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 937 | public RequestThreadManager(int cameraId, Camera camera, CameraCharacteristics characteristics, |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 938 | CameraDeviceState deviceState) { |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 939 | mCamera = checkNotNull(camera, "camera must not be null"); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 940 | mCameraId = cameraId; |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 941 | mCharacteristics = checkNotNull(characteristics, "characteristics must not be null"); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 942 | String name = String.format("RequestThread-%d", cameraId); |
| 943 | TAG = name; |
Igor Murashkin | df6242e | 2014-07-01 18:06:13 -0700 | [diff] [blame] | 944 | mDeviceState = checkNotNull(deviceState, "deviceState must not be null"); |
Igor Murashkin | 83d8639 | 2014-07-18 14:37:19 -0700 | [diff] [blame] | 945 | mFocusStateMapper = new LegacyFocusStateMapper(mCamera); |
Igor Murashkin | 0a1ef4d | 2014-07-31 15:53:34 -0700 | [diff] [blame] | 946 | mFaceDetectMapper = new LegacyFaceDetectMapper(mCamera, mCharacteristics); |
Ruben Brunk | 91838de | 2014-07-16 17:24:17 -0700 | [diff] [blame] | 947 | mCaptureCollector = new CaptureCollector(MAX_IN_FLIGHT_REQUESTS, mDeviceState); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 948 | mRequestThread = new RequestHandlerThread(name, mRequestHandlerCb); |
Ruben Brunk | 4aed87a | 2014-09-21 18:35:31 -0700 | [diff] [blame] | 949 | mCamera.setErrorCallback(mErrorCallback); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /** |
| 953 | * Start the request thread. |
| 954 | */ |
| 955 | public void start() { |
| 956 | mRequestThread.start(); |
| 957 | } |
| 958 | |
| 959 | /** |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 960 | * Flush any pending requests. |
| 961 | * |
| 962 | * @return the last frame number. |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 963 | */ |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 964 | public long flush() { |
| 965 | Log.i(TAG, "Flushing all pending requests."); |
| 966 | long lastFrame = mRequestQueue.stopRepeating(); |
| 967 | mCaptureCollector.failAll(); |
| 968 | return lastFrame; |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /** |
| 972 | * Quit the request thread, and clean up everything. |
| 973 | */ |
| 974 | public void quit() { |
Ruben Brunk | e3c0434 | 2015-02-04 17:26:17 -0800 | [diff] [blame] | 975 | if (!mQuit.getAndSet(true)) { // Avoid sending messages on dead thread's handler. |
| 976 | Handler handler = mRequestThread.waitAndGetHandler(); |
| 977 | handler.sendMessageAtFrontOfQueue(handler.obtainMessage(MSG_CLEANUP)); |
| 978 | mRequestThread.quitSafely(); |
| 979 | try { |
| 980 | mRequestThread.join(); |
| 981 | } catch (InterruptedException e) { |
| 982 | Log.e(TAG, String.format("Thread %s (%d) interrupted while quitting.", |
| 983 | mRequestThread.getName(), mRequestThread.getId())); |
| 984 | } |
Ruben Brunk | d85e1a6 | 2014-06-11 10:35:45 -0700 | [diff] [blame] | 985 | } |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | /** |
| 989 | * Submit the given burst of requests to be captured. |
| 990 | * |
| 991 | * <p>If the burst is repeating, replace the current repeating burst.</p> |
| 992 | * |
| 993 | * @param requests the burst of requests to add to the queue. |
| 994 | * @param repeating true if the burst is repeating. |
| 995 | * @param frameNumber an output argument that contains either the frame number of the last frame |
| 996 | * that will be returned for this request, or the frame number of the last |
| 997 | * frame that will be returned for the current repeating request if this |
| 998 | * burst is set to be repeating. |
| 999 | * @return the request id. |
| 1000 | */ |
| 1001 | public int submitCaptureRequests(List<CaptureRequest> requests, boolean repeating, |
| 1002 | /*out*/LongParcelable frameNumber) { |
| 1003 | Handler handler = mRequestThread.waitAndGetHandler(); |
Ruben Brunk | 4aed87a | 2014-09-21 18:35:31 -0700 | [diff] [blame] | 1004 | int ret; |
| 1005 | synchronized (mIdleLock) { |
| 1006 | ret = mRequestQueue.submit(requests, repeating, frameNumber); |
| 1007 | handler.sendEmptyMessage(MSG_SUBMIT_CAPTURE_REQUEST); |
| 1008 | } |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 1009 | return ret; |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * Cancel a repeating request. |
| 1014 | * |
| 1015 | * @param requestId the id of the repeating request to cancel. |
| 1016 | * @return the last frame to be returned from the HAL for the given repeating request, or |
| 1017 | * {@code INVALID_FRAME} if none exists. |
| 1018 | */ |
| 1019 | public long cancelRepeating(int requestId) { |
| 1020 | return mRequestQueue.stopRepeating(requestId); |
| 1021 | } |
| 1022 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 1023 | /** |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1024 | * Configure with the current list of output Surfaces. |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 1025 | * |
| 1026 | * <p> |
| 1027 | * This operation blocks until the configuration is complete. |
| 1028 | * </p> |
| 1029 | * |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1030 | * <p>Using a {@code null} or empty {@code outputs} list is the equivalent of unconfiguring.</p> |
| 1031 | * |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 1032 | * @param outputs a {@link java.util.Collection} of outputs to configure. |
| 1033 | */ |
Ruben Brunk | f4a637d | 2014-11-20 18:01:36 -0800 | [diff] [blame] | 1034 | public void configure(Collection<Pair<Surface, Size>> outputs) { |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 1035 | Handler handler = mRequestThread.waitAndGetHandler(); |
| 1036 | final ConditionVariable condition = new ConditionVariable(/*closed*/false); |
| 1037 | ConfigureHolder holder = new ConfigureHolder(condition, outputs); |
| 1038 | handler.sendMessage(handler.obtainMessage(MSG_CONFIGURE_OUTPUTS, 0, 0, holder)); |
| 1039 | condition.block(); |
| 1040 | } |
| 1041 | } |