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