blob: f917fdce137703dfab1ad22bb8ed2e789a93f599 [file] [log] [blame]
Igor Murashkin70725502013-06-25 20:27:06 +00001/*
2 * Copyright (C) 2013 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
Eino-Ville Talvala2f1a2e42013-07-25 17:12:05 -070017package android.hardware.camera2.impl;
Igor Murashkin70725502013-06-25 20:27:06 +000018
Igor Murashkin57ea59b2013-08-23 16:55:57 -070019import static android.hardware.camera2.CameraAccessException.CAMERA_IN_USE;
20
Eino-Ville Talvala2f1a2e42013-07-25 17:12:05 -070021import android.hardware.camera2.CameraAccessException;
Eino-Ville Talvalacca00c62014-05-14 10:53:20 -070022import android.hardware.camera2.CameraCaptureSession;
Ruben Brunk57493682014-05-27 18:58:08 -070023import android.hardware.camera2.CameraCharacteristics;
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -070024import android.hardware.camera2.CameraDevice;
Eino-Ville Talvala2f1a2e42013-07-25 17:12:05 -070025import android.hardware.camera2.CaptureRequest;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070026import android.hardware.camera2.CaptureResult;
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -070027import android.hardware.camera2.CaptureFailure;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070028import android.hardware.camera2.ICameraDeviceCallbacks;
29import android.hardware.camera2.ICameraDeviceUser;
Igor Murashkindb075af2014-05-21 10:07:08 -070030import android.hardware.camera2.TotalCaptureResult;
Eino-Ville Talvala5afd3e92013-08-21 10:37:04 -070031import android.hardware.camera2.utils.CameraBinderDecorator;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070032import android.hardware.camera2.utils.CameraRuntimeException;
Igor Murashkin9c595172014-05-12 13:56:20 -070033import android.hardware.camera2.utils.LongParcelable;
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -070034import android.os.Handler;
Ruben Brunkdecfe952013-10-29 11:00:32 -070035import android.os.IBinder;
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -070036import android.os.Looper;
Ruben Brunkdecfe952013-10-29 11:00:32 -070037import android.os.RemoteException;
Igor Murashkin70725502013-06-25 20:27:06 +000038import android.util.Log;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070039import android.util.SparseArray;
Igor Murashkin70725502013-06-25 20:27:06 +000040import android.view.Surface;
41
Jianing Weid2c3a822014-03-27 18:27:43 -070042import java.util.AbstractMap.SimpleEntry;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070043import java.util.ArrayList;
Igor Murashkin1e854c52014-08-28 15:21:49 -070044import java.util.HashMap;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070045import java.util.HashSet;
Ruben Brunkdecfe952013-10-29 11:00:32 -070046import java.util.Iterator;
Igor Murashkin70725502013-06-25 20:27:06 +000047import java.util.List;
Jianing Weid2c3a822014-03-27 18:27:43 -070048import java.util.TreeSet;
Igor Murashkin70725502013-06-25 20:27:06 +000049
50/**
Eino-Ville Talvala70c22072013-08-27 12:09:04 -070051 * HAL2.1+ implementation of CameraDevice. Use CameraManager#open to instantiate
Igor Murashkin70725502013-06-25 20:27:06 +000052 */
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -070053public class CameraDeviceImpl extends CameraDevice {
Igor Murashkin70725502013-06-25 20:27:06 +000054
55 private final String TAG;
Zhijun Heecb323e2013-07-31 09:40:27 -070056 private final boolean DEBUG;
Igor Murashkin70725502013-06-25 20:27:06 +000057
Ruben Brunkdecfe952013-10-29 11:00:32 -070058 private static final int REQUEST_ID_NONE = -1;
59
Igor Murashkin70725502013-06-25 20:27:06 +000060 // TODO: guard every function with if (!mRemoteDevice) check (if it was closed)
61 private ICameraDeviceUser mRemoteDevice;
62
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -070063 // Lock to synchronize cross-thread access to device public interface
64 private final Object mInterfaceLock = new Object();
Igor Murashkin70725502013-06-25 20:27:06 +000065 private final CameraDeviceCallbacks mCallbacks = new CameraDeviceCallbacks();
66
Eino-Ville Talvalafd887432014-09-04 13:07:40 -070067 private final StateCallback mDeviceCallback;
68 private volatile StateCallbackKK mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -070069 private final Handler mDeviceHandler;
70
Igor Murashkin49b2b132014-06-18 19:03:00 -070071 private volatile boolean mClosing = false;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -070072 private boolean mInError = false;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -070073 private boolean mIdle = true;
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -070074
Eino-Ville Talvalafd887432014-09-04 13:07:40 -070075 /** map request IDs to callback/request data */
76 private final SparseArray<CaptureCallbackHolder> mCaptureCallbackMap =
77 new SparseArray<CaptureCallbackHolder>();
Igor Murashkin70725502013-06-25 20:27:06 +000078
Ruben Brunkdecfe952013-10-29 11:00:32 -070079 private int mRepeatingRequestId = REQUEST_ID_NONE;
80 private final ArrayList<Integer> mRepeatingRequestIdDeletedList = new ArrayList<Integer>();
Igor Murashkin57ea59b2013-08-23 16:55:57 -070081 // Map stream IDs to Surfaces
82 private final SparseArray<Surface> mConfiguredOutputs = new SparseArray<Surface>();
Igor Murashkin70725502013-06-25 20:27:06 +000083
84 private final String mCameraId;
Ruben Brunk57493682014-05-27 18:58:08 -070085 private final CameraCharacteristics mCharacteristics;
Zhijun He83159152014-07-16 11:32:59 -070086 private final int mTotalPartialCount;
Igor Murashkin70725502013-06-25 20:27:06 +000087
Jianing Weid2c3a822014-03-27 18:27:43 -070088 /**
89 * A list tracking request and its expected last frame.
90 * Updated when calling ICameraDeviceUser methods.
91 */
92 private final List<SimpleEntry</*frameNumber*/Long, /*requestId*/Integer>>
93 mFrameNumberRequestPairs = new ArrayList<SimpleEntry<Long, Integer>>();
94
95 /**
96 * An object tracking received frame numbers.
97 * Updated when receiving callbacks from ICameraDeviceCallbacks.
98 */
99 private final FrameNumberTracker mFrameNumberTracker = new FrameNumberTracker();
100
Igor Murashkin0b27d342014-05-30 09:45:05 -0700101 private CameraCaptureSessionImpl mCurrentSession;
Eino-Ville Talvala95037852014-09-14 14:07:05 -0700102 private int mNextSessionId = 0;
Igor Murashkin0b27d342014-05-30 09:45:05 -0700103
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700104 // Runnables for all state transitions, except error, which needs the
105 // error code argument
106
107 private final Runnable mCallOnOpened = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700108 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700109 public void run() {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700110 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700111 synchronized(mInterfaceLock) {
112 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -0700113
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700114 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700115 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700116 if (sessionCallback != null) {
117 sessionCallback.onOpened(CameraDeviceImpl.this);
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700118 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700119 mDeviceCallback.onOpened(CameraDeviceImpl.this);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700120 }
121 };
122
123 private final Runnable mCallOnUnconfigured = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700124 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700125 public void run() {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700126 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700127 synchronized(mInterfaceLock) {
128 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -0700129
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700130 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700131 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700132 if (sessionCallback != null) {
133 sessionCallback.onUnconfigured(CameraDeviceImpl.this);
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700134 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700135 }
136 };
137
138 private final Runnable mCallOnActive = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700139 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700140 public void run() {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700141 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700142 synchronized(mInterfaceLock) {
143 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -0700144
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700145 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700146 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700147 if (sessionCallback != null) {
148 sessionCallback.onActive(CameraDeviceImpl.this);
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700149 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700150 }
151 };
152
153 private final Runnable mCallOnBusy = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700154 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700155 public void run() {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700156 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700157 synchronized(mInterfaceLock) {
158 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -0700159
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700160 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700161 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700162 if (sessionCallback != null) {
163 sessionCallback.onBusy(CameraDeviceImpl.this);
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700164 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700165 }
166 };
167
168 private final Runnable mCallOnClosed = new Runnable() {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700169 private boolean mClosedOnce = false;
170
Jianing Weid2c3a822014-03-27 18:27:43 -0700171 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700172 public void run() {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700173 if (mClosedOnce) {
174 throw new AssertionError("Don't post #onClosed more than once");
175 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700176 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700177 synchronized(mInterfaceLock) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700178 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700179 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700180 if (sessionCallback != null) {
181 sessionCallback.onClosed(CameraDeviceImpl.this);
Igor Murashkin0b27d342014-05-30 09:45:05 -0700182 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700183 mDeviceCallback.onClosed(CameraDeviceImpl.this);
Igor Murashkin49b2b132014-06-18 19:03:00 -0700184 mClosedOnce = true;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700185 }
186 };
187
188 private final Runnable mCallOnIdle = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700189 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700190 public void run() {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700191 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700192 synchronized(mInterfaceLock) {
193 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -0700194
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700195 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700196 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700197 if (sessionCallback != null) {
198 sessionCallback.onIdle(CameraDeviceImpl.this);
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700199 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700200 }
201 };
202
203 private final Runnable mCallOnDisconnected = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700204 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700205 public void run() {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700206 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700207 synchronized(mInterfaceLock) {
208 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -0700209
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700210 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700211 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700212 if (sessionCallback != null) {
213 sessionCallback.onDisconnected(CameraDeviceImpl.this);
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700214 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700215 mDeviceCallback.onDisconnected(CameraDeviceImpl.this);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700216 }
217 };
218
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700219 public CameraDeviceImpl(String cameraId, StateCallback callback, Handler handler,
Ruben Brunk57493682014-05-27 18:58:08 -0700220 CameraCharacteristics characteristics) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700221 if (cameraId == null || callback == null || handler == null || characteristics == null) {
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700222 throw new IllegalArgumentException("Null argument given");
223 }
Igor Murashkin70725502013-06-25 20:27:06 +0000224 mCameraId = cameraId;
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700225 mDeviceCallback = callback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700226 mDeviceHandler = handler;
Ruben Brunk57493682014-05-27 18:58:08 -0700227 mCharacteristics = characteristics;
Eino-Ville Talvalab72d1ef2014-04-28 13:21:18 -0700228
229 final int MAX_TAG_LEN = 23;
230 String tag = String.format("CameraDevice-JV-%s", mCameraId);
231 if (tag.length() > MAX_TAG_LEN) {
232 tag = tag.substring(0, MAX_TAG_LEN);
233 }
234 TAG = tag;
Zhijun Heecb323e2013-07-31 09:40:27 -0700235 DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Zhijun He83159152014-07-16 11:32:59 -0700236
237 Integer partialCount =
238 mCharacteristics.get(CameraCharacteristics.REQUEST_PARTIAL_RESULT_COUNT);
239 if (partialCount == null) {
240 // 1 means partial result is not supported.
241 mTotalPartialCount = 1;
242 } else {
243 mTotalPartialCount = partialCount;
244 }
Igor Murashkin70725502013-06-25 20:27:06 +0000245 }
246
247 public CameraDeviceCallbacks getCallbacks() {
248 return mCallbacks;
249 }
250
Igor Murashkin70725502013-06-25 20:27:06 +0000251 public void setRemoteDevice(ICameraDeviceUser remoteDevice) {
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700252 synchronized(mInterfaceLock) {
253 // TODO: Move from decorator to direct binder-mediated exceptions
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700254 // If setRemoteFailure already called, do nothing
255 if (mInError) return;
256
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700257 mRemoteDevice = CameraBinderDecorator.newInstance(remoteDevice);
258
259 mDeviceHandler.post(mCallOnOpened);
260 mDeviceHandler.post(mCallOnUnconfigured);
261 }
Igor Murashkin70725502013-06-25 20:27:06 +0000262 }
263
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700264 /**
265 * Call to indicate failed connection to a remote camera device.
266 *
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700267 * <p>This places the camera device in the error state and informs the callback.
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700268 * Use in place of setRemoteDevice() when startup fails.</p>
269 */
270 public void setRemoteFailure(final CameraRuntimeException failure) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700271 int failureCode = StateCallback.ERROR_CAMERA_DEVICE;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700272 boolean failureIsError = true;
273
274 switch (failure.getReason()) {
275 case CameraAccessException.CAMERA_IN_USE:
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700276 failureCode = StateCallback.ERROR_CAMERA_IN_USE;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700277 break;
278 case CameraAccessException.MAX_CAMERAS_IN_USE:
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700279 failureCode = StateCallback.ERROR_MAX_CAMERAS_IN_USE;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700280 break;
281 case CameraAccessException.CAMERA_DISABLED:
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700282 failureCode = StateCallback.ERROR_CAMERA_DISABLED;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700283 break;
284 case CameraAccessException.CAMERA_DISCONNECTED:
285 failureIsError = false;
286 break;
287 case CameraAccessException.CAMERA_ERROR:
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700288 failureCode = StateCallback.ERROR_CAMERA_DEVICE;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700289 break;
290 default:
291 Log.wtf(TAG, "Unknown failure in opening camera device: " + failure.getReason());
292 break;
293 }
294 final int code = failureCode;
295 final boolean isError = failureIsError;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700296 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700297 mInError = true;
298 mDeviceHandler.post(new Runnable() {
Igor Murashkine1442202014-06-09 17:51:24 -0700299 @Override
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700300 public void run() {
301 if (isError) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700302 mDeviceCallback.onError(CameraDeviceImpl.this, code);
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700303 } else {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700304 mDeviceCallback.onDisconnected(CameraDeviceImpl.this);
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700305 }
306 }
307 });
308 }
309 }
310
Igor Murashkin70725502013-06-25 20:27:06 +0000311 @Override
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700312 public String getId() {
313 return mCameraId;
314 }
315
Igor Murashkin70725502013-06-25 20:27:06 +0000316 public void configureOutputs(List<Surface> outputs) throws CameraAccessException {
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700317 // Leave this here for backwards compatibility with older code using this directly
318 configureOutputsChecked(outputs);
319 }
320
321 /**
322 * Attempt to configure the outputs; the device goes to idle and then configures the
323 * new outputs if possible.
324 *
325 * <p>The configuration may gracefully fail, if there are too many outputs, if the formats
326 * are not supported, or if the sizes for that format is not supported. In this case this
327 * function will return {@code false} and the unconfigured callback will be fired.</p>
328 *
329 * <p>If the configuration succeeds (with 1 or more outputs), then the idle callback is fired.
330 * Unconfiguring the device always fires the idle callback.</p>
331 *
332 * @param outputs a list of one or more surfaces, or {@code null} to unconfigure
333 * @return whether or not the configuration was successful
334 *
335 * @throws CameraAccessException if there were any unexpected problems during configuration
336 */
337 public boolean configureOutputsChecked(List<Surface> outputs) throws CameraAccessException {
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700338 // Treat a null input the same an empty list
339 if (outputs == null) {
340 outputs = new ArrayList<Surface>();
341 }
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700342 boolean success = false;
343
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700344 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700345 checkIfCameraClosedOrInError();
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700346
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700347 HashSet<Surface> addSet = new HashSet<Surface>(outputs); // Streams to create
348 List<Integer> deleteList = new ArrayList<Integer>(); // Streams to delete
349
350 // Determine which streams need to be created, which to be deleted
351 for (int i = 0; i < mConfiguredOutputs.size(); ++i) {
352 int streamId = mConfiguredOutputs.keyAt(i);
353 Surface s = mConfiguredOutputs.valueAt(i);
354
355 if (!outputs.contains(s)) {
356 deleteList.add(streamId);
357 } else {
358 addSet.remove(s); // Don't create a stream previously created
359 }
360 }
361
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700362 mDeviceHandler.post(mCallOnBusy);
363 stopRepeating();
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700364
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700365 try {
Ruben Brunkdecfe952013-10-29 11:00:32 -0700366 waitUntilIdle();
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700367
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700368 mRemoteDevice.beginConfigure();
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700369 // Delete all streams first (to free up HW resources)
370 for (Integer streamId : deleteList) {
371 mRemoteDevice.deleteStream(streamId);
372 mConfiguredOutputs.delete(streamId);
373 }
374
375 // Add all new streams
376 for (Surface s : addSet) {
Igor Murashkin70725502013-06-25 20:27:06 +0000377 // TODO: remove width,height,format since we are ignoring
378 // it.
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700379 int streamId = mRemoteDevice.createStream(0, 0, 0, s);
380 mConfiguredOutputs.put(streamId, s);
Igor Murashkin70725502013-06-25 20:27:06 +0000381 }
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700382
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700383 try {
384 mRemoteDevice.endConfigure();
385 }
386 catch (IllegalArgumentException e) {
387 // OK. camera service can reject stream config if it's not supported by HAL
388 // This is only the result of a programmer misusing the camera2 api.
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -0700389 Log.w(TAG, "Stream configuration failed");
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700390 return false;
391 }
392
393 success = true;
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700394 } catch (CameraRuntimeException e) {
395 if (e.getReason() == CAMERA_IN_USE) {
396 throw new IllegalStateException("The camera is currently busy." +
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700397 " You must wait until the previous operation completes.");
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700398 }
399
400 throw e.asChecked();
401 } catch (RemoteException e) {
402 // impossible
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700403 return false;
404 } finally {
405 if (success && outputs.size() > 0) {
406 mDeviceHandler.post(mCallOnIdle);
407 } else {
408 // Always return to the 'unconfigured' state if we didn't hit a fatal error
409 mDeviceHandler.post(mCallOnUnconfigured);
410 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700411 }
Igor Murashkin70725502013-06-25 20:27:06 +0000412 }
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700413
414 return success;
Igor Murashkin70725502013-06-25 20:27:06 +0000415 }
416
417 @Override
Eino-Ville Talvalacca00c62014-05-14 10:53:20 -0700418 public void createCaptureSession(List<Surface> outputs,
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700419 CameraCaptureSession.StateCallback callback, Handler handler)
Eino-Ville Talvalacca00c62014-05-14 10:53:20 -0700420 throws CameraAccessException {
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700421 synchronized(mInterfaceLock) {
Igor Murashkin0b27d342014-05-30 09:45:05 -0700422 if (DEBUG) {
423 Log.d(TAG, "createCaptureSession");
424 }
425
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700426 checkIfCameraClosedOrInError();
Igor Murashkin0b27d342014-05-30 09:45:05 -0700427
Eino-Ville Talvalad3b85f62014-08-05 15:02:31 -0700428 // Notify current session that it's going away, before starting camera operations
429 // After this call completes, the session is not allowed to call into CameraDeviceImpl
430 if (mCurrentSession != null) {
431 mCurrentSession.replaceSessionClose();
432 }
Igor Murashkin0b27d342014-05-30 09:45:05 -0700433
434 // TODO: dont block for this
435 boolean configureSuccess = true;
436 CameraAccessException pendingException = null;
437 try {
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700438 configureSuccess = configureOutputsChecked(outputs); // and then block until IDLE
Igor Murashkin0b27d342014-05-30 09:45:05 -0700439 } catch (CameraAccessException e) {
440 configureSuccess = false;
441 pendingException = e;
Igor Murashkine1442202014-06-09 17:51:24 -0700442 if (DEBUG) {
443 Log.v(TAG, "createCaptureSession - failed with exception ", e);
444 }
Igor Murashkin0b27d342014-05-30 09:45:05 -0700445 }
446
447 // Fire onConfigured if configureOutputs succeeded, fire onConfigureFailed otherwise.
448 CameraCaptureSessionImpl newSession =
Eino-Ville Talvala95037852014-09-14 14:07:05 -0700449 new CameraCaptureSessionImpl(mNextSessionId++,
450 outputs, callback, handler, this, mDeviceHandler,
Igor Murashkin0b27d342014-05-30 09:45:05 -0700451 configureSuccess);
452
Igor Murashkin0b27d342014-05-30 09:45:05 -0700453 // TODO: wait until current session closes, then create the new session
454 mCurrentSession = newSession;
455
456 if (pendingException != null) {
457 throw pendingException;
458 }
459
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700460 mSessionStateCallback = mCurrentSession.getDeviceStateCallback();
Igor Murashkin0b27d342014-05-30 09:45:05 -0700461 }
Eino-Ville Talvalacca00c62014-05-14 10:53:20 -0700462 }
463
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700464 /**
465 * For use by backwards-compatibility code only.
466 */
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700467 public void setSessionListener(StateCallbackKK sessionCallback) {
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700468 synchronized(mInterfaceLock) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700469 mSessionStateCallback = sessionCallback;
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700470 }
471 }
472
Eino-Ville Talvalacca00c62014-05-14 10:53:20 -0700473 @Override
Eino-Ville Talvala70c22072013-08-27 12:09:04 -0700474 public CaptureRequest.Builder createCaptureRequest(int templateType)
475 throws CameraAccessException {
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700476 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700477 checkIfCameraClosedOrInError();
Igor Murashkin70725502013-06-25 20:27:06 +0000478
Eino-Ville Talvala70c22072013-08-27 12:09:04 -0700479 CameraMetadataNative templatedRequest = new CameraMetadataNative();
Igor Murashkin70725502013-06-25 20:27:06 +0000480
481 try {
Igor Murashkin0b27d342014-05-30 09:45:05 -0700482 mRemoteDevice.createDefaultRequest(templateType, /*out*/templatedRequest);
Igor Murashkin70725502013-06-25 20:27:06 +0000483 } catch (CameraRuntimeException e) {
484 throw e.asChecked();
485 } catch (RemoteException e) {
486 // impossible
487 return null;
488 }
489
Eino-Ville Talvala70c22072013-08-27 12:09:04 -0700490 CaptureRequest.Builder builder =
491 new CaptureRequest.Builder(templatedRequest);
Igor Murashkin70725502013-06-25 20:27:06 +0000492
Eino-Ville Talvala70c22072013-08-27 12:09:04 -0700493 return builder;
Igor Murashkin70725502013-06-25 20:27:06 +0000494 }
495 }
496
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700497 public int capture(CaptureRequest request, CaptureCallback callback, Handler handler)
Igor Murashkin70725502013-06-25 20:27:06 +0000498 throws CameraAccessException {
Jianing Weid2c3a822014-03-27 18:27:43 -0700499 if (DEBUG) {
500 Log.d(TAG, "calling capture");
501 }
502 List<CaptureRequest> requestList = new ArrayList<CaptureRequest>();
503 requestList.add(request);
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700504 return submitCaptureRequest(requestList, callback, handler, /*streaming*/false);
Igor Murashkin70725502013-06-25 20:27:06 +0000505 }
506
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700507 public int captureBurst(List<CaptureRequest> requests, CaptureCallback callback,
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700508 Handler handler) throws CameraAccessException {
Igor Murashkin0b27d342014-05-30 09:45:05 -0700509 if (requests == null || requests.isEmpty()) {
510 throw new IllegalArgumentException("At least one request must be given");
Zhijun Hefc19e2c2013-08-22 14:43:07 -0700511 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700512 return submitCaptureRequest(requests, callback, handler, /*streaming*/false);
Igor Murashkin70725502013-06-25 20:27:06 +0000513 }
514
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700515 /**
516 * This method checks lastFrameNumber returned from ICameraDeviceUser methods for
517 * starting and stopping repeating request and flushing.
518 *
519 * <p>If lastFrameNumber is NO_FRAMES_CAPTURED, it means that the request was never
Eino-Ville Talvalaee37e302014-06-10 15:24:26 -0700520 * sent to HAL. Then onCaptureSequenceAborted is immediately triggered.
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700521 * If lastFrameNumber is non-negative, then the requestId and lastFrameNumber pair
522 * is added to the list mFrameNumberRequestPairs.</p>
523 *
524 * @param requestId the request ID of the current repeating request.
525 *
526 * @param lastFrameNumber last frame number returned from binder.
527 */
528 private void checkEarlyTriggerSequenceComplete(
529 final int requestId, final long lastFrameNumber) {
530 // lastFrameNumber being equal to NO_FRAMES_CAPTURED means that the request
Eino-Ville Talvalaee37e302014-06-10 15:24:26 -0700531 // was never sent to HAL. Should trigger onCaptureSequenceAborted immediately.
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700532 if (lastFrameNumber == CaptureCallback.NO_FRAMES_CAPTURED) {
533 final CaptureCallbackHolder holder;
534 int index = mCaptureCallbackMap.indexOfKey(requestId);
535 holder = (index >= 0) ? mCaptureCallbackMap.valueAt(index) : null;
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700536 if (holder != null) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700537 mCaptureCallbackMap.removeAt(index);
Jianing Weibaf0c652014-04-18 17:35:00 -0700538 if (DEBUG) {
539 Log.v(TAG, String.format(
540 "remove holder for requestId %d, "
541 + "because lastFrame is %d.",
542 requestId, lastFrameNumber));
543 }
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700544 }
545
546 if (holder != null) {
547 if (DEBUG) {
Eino-Ville Talvalaee37e302014-06-10 15:24:26 -0700548 Log.v(TAG, "immediately trigger onCaptureSequenceAborted because"
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700549 + " request did not reach HAL");
550 }
551
552 Runnable resultDispatch = new Runnable() {
553 @Override
554 public void run() {
Igor Murashkin21547d62014-06-04 15:21:42 -0700555 if (!CameraDeviceImpl.this.isClosed()) {
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700556 if (DEBUG) {
557 Log.d(TAG, String.format(
558 "early trigger sequence complete for request %d",
559 requestId));
560 }
561 if (lastFrameNumber < Integer.MIN_VALUE
562 || lastFrameNumber > Integer.MAX_VALUE) {
563 throw new AssertionError(lastFrameNumber + " cannot be cast to int");
564 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700565 holder.getCallback().onCaptureSequenceAborted(
Igor Murashkin21547d62014-06-04 15:21:42 -0700566 CameraDeviceImpl.this,
Eino-Ville Talvalaee37e302014-06-10 15:24:26 -0700567 requestId);
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700568 }
569 }
570 };
571 holder.getHandler().post(resultDispatch);
572 } else {
573 Log.w(TAG, String.format(
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700574 "did not register callback to request %d",
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700575 requestId));
576 }
577 } else {
578 mFrameNumberRequestPairs.add(
579 new SimpleEntry<Long, Integer>(lastFrameNumber,
580 requestId));
581 }
582 }
583
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700584 private int submitCaptureRequest(List<CaptureRequest> requestList, CaptureCallback callback,
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700585 Handler handler, boolean repeating) throws CameraAccessException {
586
587 // Need a valid handler, or current thread needs to have a looper, if
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700588 // callback is valid
589 handler = checkHandler(handler, callback);
Igor Murashkin70725502013-06-25 20:27:06 +0000590
Ruben Brunk7f2372b2014-07-02 11:05:08 -0700591 // Make sure that there all requests have at least 1 surface; all surfaces are non-null
592 for (CaptureRequest request : requestList) {
593 if (request.getTargets().isEmpty()) {
594 throw new IllegalArgumentException(
595 "Each request must have at least one Surface target");
596 }
597
598 for (Surface surface : request.getTargets()) {
599 if (surface == null) {
600 throw new IllegalArgumentException("Null Surface targets are not allowed");
601 }
602 }
603 }
604
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700605 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700606 checkIfCameraClosedOrInError();
Igor Murashkin70725502013-06-25 20:27:06 +0000607 int requestId;
608
Ruben Brunke73b41b2013-11-07 19:30:43 -0800609 if (repeating) {
610 stopRepeating();
611 }
612
Jianing Weid2c3a822014-03-27 18:27:43 -0700613 LongParcelable lastFrameNumberRef = new LongParcelable();
Igor Murashkin70725502013-06-25 20:27:06 +0000614 try {
Jianing Weid2c3a822014-03-27 18:27:43 -0700615 requestId = mRemoteDevice.submitRequestList(requestList, repeating,
616 /*out*/lastFrameNumberRef);
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700617 if (DEBUG) {
Jianing Weid2c3a822014-03-27 18:27:43 -0700618 Log.v(TAG, "last frame number " + lastFrameNumberRef.getNumber());
619 }
Igor Murashkin70725502013-06-25 20:27:06 +0000620 } catch (CameraRuntimeException e) {
621 throw e.asChecked();
622 } catch (RemoteException e) {
623 // impossible
Igor Murashkin6bbf9dc2013-09-05 12:22:00 -0700624 return -1;
Igor Murashkin70725502013-06-25 20:27:06 +0000625 }
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700626
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700627 if (callback != null) {
628 mCaptureCallbackMap.put(requestId, new CaptureCallbackHolder(callback,
Jianing Weid2c3a822014-03-27 18:27:43 -0700629 requestList, handler, repeating));
Jianing Weibaf0c652014-04-18 17:35:00 -0700630 } else {
631 if (DEBUG) {
632 Log.d(TAG, "Listen for request " + requestId + " is null");
633 }
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700634 }
Igor Murashkin70725502013-06-25 20:27:06 +0000635
Jianing Weid2c3a822014-03-27 18:27:43 -0700636 long lastFrameNumber = lastFrameNumberRef.getNumber();
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700637
Igor Murashkin70725502013-06-25 20:27:06 +0000638 if (repeating) {
Jianing Weid2c3a822014-03-27 18:27:43 -0700639 if (mRepeatingRequestId != REQUEST_ID_NONE) {
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700640 checkEarlyTriggerSequenceComplete(mRepeatingRequestId, lastFrameNumber);
Jianing Weid2c3a822014-03-27 18:27:43 -0700641 }
Ruben Brunkdecfe952013-10-29 11:00:32 -0700642 mRepeatingRequestId = requestId;
Jianing Weid2c3a822014-03-27 18:27:43 -0700643 } else {
644 mFrameNumberRequestPairs.add(
645 new SimpleEntry<Long, Integer>(lastFrameNumber, requestId));
Igor Murashkin70725502013-06-25 20:27:06 +0000646 }
647
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700648 if (mIdle) {
649 mDeviceHandler.post(mCallOnActive);
650 }
651 mIdle = false;
652
Igor Murashkin6bbf9dc2013-09-05 12:22:00 -0700653 return requestId;
Igor Murashkin70725502013-06-25 20:27:06 +0000654 }
655 }
656
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700657 public int setRepeatingRequest(CaptureRequest request, CaptureCallback callback,
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700658 Handler handler) throws CameraAccessException {
Jianing Weid2c3a822014-03-27 18:27:43 -0700659 List<CaptureRequest> requestList = new ArrayList<CaptureRequest>();
660 requestList.add(request);
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700661 return submitCaptureRequest(requestList, callback, handler, /*streaming*/true);
Igor Murashkin70725502013-06-25 20:27:06 +0000662 }
663
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700664 public int setRepeatingBurst(List<CaptureRequest> requests, CaptureCallback callback,
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700665 Handler handler) throws CameraAccessException {
Igor Murashkin0b27d342014-05-30 09:45:05 -0700666 if (requests == null || requests.isEmpty()) {
667 throw new IllegalArgumentException("At least one request must be given");
Zhijun Hefc19e2c2013-08-22 14:43:07 -0700668 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700669 return submitCaptureRequest(requests, callback, handler, /*streaming*/true);
Igor Murashkin70725502013-06-25 20:27:06 +0000670 }
671
Igor Murashkin70725502013-06-25 20:27:06 +0000672 public void stopRepeating() throws CameraAccessException {
673
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700674 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700675 checkIfCameraClosedOrInError();
Ruben Brunkdecfe952013-10-29 11:00:32 -0700676 if (mRepeatingRequestId != REQUEST_ID_NONE) {
677
678 int requestId = mRepeatingRequestId;
679 mRepeatingRequestId = REQUEST_ID_NONE;
680
681 // Queue for deletion after in-flight requests finish
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700682 if (mCaptureCallbackMap.get(requestId) != null) {
Zhijun He1a9b6462014-03-31 16:11:33 -0700683 mRepeatingRequestIdDeletedList.add(requestId);
684 }
Igor Murashkin70725502013-06-25 20:27:06 +0000685
686 try {
Jianing Weid2c3a822014-03-27 18:27:43 -0700687 LongParcelable lastFrameNumberRef = new LongParcelable();
688 mRemoteDevice.cancelRequest(requestId, /*out*/lastFrameNumberRef);
689 long lastFrameNumber = lastFrameNumberRef.getNumber();
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700690
691 checkEarlyTriggerSequenceComplete(requestId, lastFrameNumber);
692
Igor Murashkin70725502013-06-25 20:27:06 +0000693 } catch (CameraRuntimeException e) {
694 throw e.asChecked();
695 } catch (RemoteException e) {
696 // impossible
697 return;
698 }
699 }
700 }
701 }
702
Zhijun Hed842fcd2013-12-26 14:14:04 -0800703 private void waitUntilIdle() throws CameraAccessException {
Zhijun He7f4d3142013-07-23 07:54:38 -0700704
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700705 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700706 checkIfCameraClosedOrInError();
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700707
Ruben Brunkdecfe952013-10-29 11:00:32 -0700708 if (mRepeatingRequestId != REQUEST_ID_NONE) {
Zhijun He7f4d3142013-07-23 07:54:38 -0700709 throw new IllegalStateException("Active repeating request ongoing");
710 }
Zhijun He7f4d3142013-07-23 07:54:38 -0700711 try {
712 mRemoteDevice.waitUntilIdle();
713 } catch (CameraRuntimeException e) {
714 throw e.asChecked();
715 } catch (RemoteException e) {
716 // impossible
717 return;
718 }
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700719 }
Igor Murashkin70725502013-06-25 20:27:06 +0000720 }
721
Eino-Ville Talvala8ebd52b2013-08-13 12:09:44 -0700722 public void flush() throws CameraAccessException {
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700723 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700724 checkIfCameraClosedOrInError();
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700725
726 mDeviceHandler.post(mCallOnBusy);
Eino-Ville Talvalac346bbf2014-08-27 17:05:29 -0700727
728 // If already idle, just do a busy->idle transition immediately, don't actually
729 // flush.
730 if (mIdle) {
731 mDeviceHandler.post(mCallOnIdle);
732 return;
733 }
Eino-Ville Talvala8ebd52b2013-08-13 12:09:44 -0700734 try {
Jianing Weid2c3a822014-03-27 18:27:43 -0700735 LongParcelable lastFrameNumberRef = new LongParcelable();
736 mRemoteDevice.flush(/*out*/lastFrameNumberRef);
737 if (mRepeatingRequestId != REQUEST_ID_NONE) {
738 long lastFrameNumber = lastFrameNumberRef.getNumber();
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700739 checkEarlyTriggerSequenceComplete(mRepeatingRequestId, lastFrameNumber);
Jianing Weid2c3a822014-03-27 18:27:43 -0700740 mRepeatingRequestId = REQUEST_ID_NONE;
741 }
Eino-Ville Talvala8ebd52b2013-08-13 12:09:44 -0700742 } catch (CameraRuntimeException e) {
743 throw e.asChecked();
744 } catch (RemoteException e) {
745 // impossible
746 return;
747 }
748 }
749 }
750
751 @Override
Igor Murashkin5c9eaf62013-09-10 19:35:24 -0700752 public void close() {
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700753 synchronized (mInterfaceLock) {
Igor Murashkin70725502013-06-25 20:27:06 +0000754 try {
Igor Murashkin2a3eced2013-08-28 17:35:10 -0700755 if (mRemoteDevice != null) {
756 mRemoteDevice.disconnect();
757 }
Igor Murashkin70725502013-06-25 20:27:06 +0000758 } catch (CameraRuntimeException e) {
Igor Murashkin5c9eaf62013-09-10 19:35:24 -0700759 Log.e(TAG, "Exception while closing: ", e.asChecked());
Igor Murashkin70725502013-06-25 20:27:06 +0000760 } catch (RemoteException e) {
761 // impossible
762 }
763
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700764 // Only want to fire the onClosed callback once;
765 // either a normal close where the remote device is valid
766 // or a close after a startup error (no remote device but in error state)
767 if (mRemoteDevice != null || mInError) {
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700768 mDeviceHandler.post(mCallOnClosed);
769 }
Igor Murashkin70725502013-06-25 20:27:06 +0000770
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700771 mRemoteDevice = null;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700772 mInError = false;
Igor Murashkin70725502013-06-25 20:27:06 +0000773 }
774 }
775
776 @Override
777 protected void finalize() throws Throwable {
778 try {
779 close();
Igor Murashkin70725502013-06-25 20:27:06 +0000780 }
781 finally {
782 super.finalize();
783 }
784 }
785
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700786 /**
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700787 * <p>A callback for tracking the progress of a {@link CaptureRequest}
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700788 * submitted to the camera device.</p>
789 *
790 */
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700791 public static abstract class CaptureCallback {
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700792
793 /**
794 * This constant is used to indicate that no images were captured for
795 * the request.
796 *
797 * @hide
798 */
799 public static final int NO_FRAMES_CAPTURED = -1;
800
801 /**
802 * This method is called when the camera device has started capturing
803 * the output image for the request, at the beginning of image exposure.
804 *
805 * @see android.media.MediaActionSound
806 */
807 public void onCaptureStarted(CameraDevice camera,
Eino-Ville Talvalae8df3092014-09-08 14:30:50 -0700808 CaptureRequest request, long timestamp, long frameNumber) {
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700809 // default empty implementation
810 }
811
812 /**
813 * This method is called when some results from an image capture are
814 * available.
815 *
816 * @hide
817 */
818 public void onCapturePartial(CameraDevice camera,
819 CaptureRequest request, CaptureResult result) {
820 // default empty implementation
821 }
822
823 /**
824 * This method is called when an image capture makes partial forward progress; some
825 * (but not all) results from an image capture are available.
826 *
827 */
828 public void onCaptureProgressed(CameraDevice camera,
829 CaptureRequest request, CaptureResult partialResult) {
830 // default empty implementation
831 }
832
833 /**
834 * This method is called when an image capture has fully completed and all the
835 * result metadata is available.
836 */
837 public void onCaptureCompleted(CameraDevice camera,
838 CaptureRequest request, TotalCaptureResult result) {
839 // default empty implementation
840 }
841
842 /**
843 * This method is called instead of {@link #onCaptureCompleted} when the
844 * camera device failed to produce a {@link CaptureResult} for the
845 * request.
846 */
847 public void onCaptureFailed(CameraDevice camera,
848 CaptureRequest request, CaptureFailure failure) {
849 // default empty implementation
850 }
851
852 /**
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700853 * This method is called independently of the others in CaptureCallback,
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700854 * when a capture sequence finishes and all {@link CaptureResult}
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700855 * or {@link CaptureFailure} for it have been returned via this callback.
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700856 */
857 public void onCaptureSequenceCompleted(CameraDevice camera,
858 int sequenceId, long frameNumber) {
859 // default empty implementation
860 }
861
862 /**
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700863 * This method is called independently of the others in CaptureCallback,
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700864 * when a capture sequence aborts before any {@link CaptureResult}
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700865 * or {@link CaptureFailure} for it have been returned via this callback.
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700866 */
867 public void onCaptureSequenceAborted(CameraDevice camera,
868 int sequenceId) {
869 // default empty implementation
870 }
871 }
872
873 /**
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700874 * A callback for notifications about the state of a camera device, adding in the callbacks that
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700875 * were part of the earlier KK API design, but now only used internally.
876 */
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700877 public static abstract class StateCallbackKK extends StateCallback {
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700878 /**
879 * The method called when a camera device has no outputs configured.
880 *
881 */
882 public void onUnconfigured(CameraDevice camera) {
883 // Default empty implementation
884 }
885
886 /**
887 * The method called when a camera device begins processing
888 * {@link CaptureRequest capture requests}.
889 *
890 */
891 public void onActive(CameraDevice camera) {
892 // Default empty implementation
893 }
894
895 /**
896 * The method called when a camera device is busy.
897 *
898 */
899 public void onBusy(CameraDevice camera) {
900 // Default empty implementation
901 }
902
903 /**
904 * The method called when a camera device has finished processing all
905 * submitted capture requests and has reached an idle state.
906 *
907 */
908 public void onIdle(CameraDevice camera) {
909 // Default empty implementation
910 }
911 }
912
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700913 static class CaptureCallbackHolder {
Igor Murashkin70725502013-06-25 20:27:06 +0000914
915 private final boolean mRepeating;
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700916 private final CaptureCallback mCallback;
Jianing Weid2c3a822014-03-27 18:27:43 -0700917 private final List<CaptureRequest> mRequestList;
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700918 private final Handler mHandler;
Igor Murashkin70725502013-06-25 20:27:06 +0000919
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700920 CaptureCallbackHolder(CaptureCallback callback, List<CaptureRequest> requestList,
Jianing Weid2c3a822014-03-27 18:27:43 -0700921 Handler handler, boolean repeating) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700922 if (callback == null || handler == null) {
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700923 throw new UnsupportedOperationException(
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700924 "Must have a valid handler and a valid callback");
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700925 }
Igor Murashkin70725502013-06-25 20:27:06 +0000926 mRepeating = repeating;
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700927 mHandler = handler;
Jianing Weid2c3a822014-03-27 18:27:43 -0700928 mRequestList = new ArrayList<CaptureRequest>(requestList);
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700929 mCallback = callback;
Igor Murashkin70725502013-06-25 20:27:06 +0000930 }
931
932 public boolean isRepeating() {
933 return mRepeating;
934 }
935
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700936 public CaptureCallback getCallback() {
937 return mCallback;
Igor Murashkin70725502013-06-25 20:27:06 +0000938 }
939
Jianing Weid2c3a822014-03-27 18:27:43 -0700940 public CaptureRequest getRequest(int subsequenceId) {
941 if (subsequenceId >= mRequestList.size()) {
942 throw new IllegalArgumentException(
943 String.format(
944 "Requested subsequenceId %d is larger than request list size %d.",
945 subsequenceId, mRequestList.size()));
946 } else {
947 if (subsequenceId < 0) {
948 throw new IllegalArgumentException(String.format(
949 "Requested subsequenceId %d is negative", subsequenceId));
950 } else {
951 return mRequestList.get(subsequenceId);
952 }
953 }
954 }
955
Igor Murashkin70725502013-06-25 20:27:06 +0000956 public CaptureRequest getRequest() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700957 return getRequest(0);
Igor Murashkin70725502013-06-25 20:27:06 +0000958 }
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700959
960 public Handler getHandler() {
961 return mHandler;
962 }
963
Igor Murashkin70725502013-06-25 20:27:06 +0000964 }
965
Jianing Weid2c3a822014-03-27 18:27:43 -0700966 /**
967 * This class tracks the last frame number for submitted requests.
968 */
969 public class FrameNumberTracker {
970
971 private long mCompletedFrameNumber = -1;
972 private final TreeSet<Long> mFutureErrorSet = new TreeSet<Long>();
Igor Murashkin1e854c52014-08-28 15:21:49 -0700973 /** Map frame numbers to list of partial results */
974 private final HashMap<Long, List<CaptureResult>> mPartialResults = new HashMap<>();
Jianing Weid2c3a822014-03-27 18:27:43 -0700975
976 private void update() {
977 Iterator<Long> iter = mFutureErrorSet.iterator();
978 while (iter.hasNext()) {
979 long errorFrameNumber = iter.next();
980 if (errorFrameNumber == mCompletedFrameNumber + 1) {
981 mCompletedFrameNumber++;
982 iter.remove();
983 } else {
984 break;
985 }
986 }
987 }
988
989 /**
990 * This function is called every time when a result or an error is received.
Igor Murashkin1e854c52014-08-28 15:21:49 -0700991 * @param frameNumber the frame number corresponding to the result or error
992 * @param isError true if it is an error, false if it is not an error
Jianing Weid2c3a822014-03-27 18:27:43 -0700993 */
994 public void updateTracker(long frameNumber, boolean isError) {
995 if (isError) {
996 mFutureErrorSet.add(frameNumber);
997 } else {
998 /**
999 * HAL cannot send an OnResultReceived for frame N unless it knows for
1000 * sure that all frames prior to N have either errored out or completed.
1001 * So if the current frame is not an error, then all previous frames
1002 * should have arrived. The following line checks whether this holds.
1003 */
1004 if (frameNumber != mCompletedFrameNumber + 1) {
Zhijun He22589b42014-05-01 10:37:36 -07001005 Log.e(TAG, String.format(
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001006 "result frame number %d comes out of order, should be %d + 1",
1007 frameNumber, mCompletedFrameNumber));
Eino-Ville Talvala95037852014-09-14 14:07:05 -07001008 // Continue on to set the completed frame number to this frame anyway,
1009 // to be robust to lower-level errors and allow for clean shutdowns.
Jianing Weid2c3a822014-03-27 18:27:43 -07001010 }
Eino-Ville Talvala95037852014-09-14 14:07:05 -07001011 mCompletedFrameNumber = frameNumber;
Jianing Weid2c3a822014-03-27 18:27:43 -07001012 }
1013 update();
1014 }
1015
Igor Murashkin1e854c52014-08-28 15:21:49 -07001016 /**
1017 * This function is called every time a result has been completed.
1018 *
1019 * <p>It keeps a track of all the partial results already created for a particular
1020 * frame number.</p>
1021 *
1022 * @param frameNumber the frame number corresponding to the result
1023 * @param result the total or partial result
1024 * @param partial {@true} if the result is partial, {@code false} if total
1025 */
1026 public void updateTracker(long frameNumber, CaptureResult result, boolean partial) {
1027
1028 if (!partial) {
1029 // Update the total result's frame status as being successful
1030 updateTracker(frameNumber, /*isError*/false);
1031 // Don't keep a list of total results, we don't need to track them
1032 return;
1033 }
1034
1035 if (result == null) {
1036 // Do not record blank results; this also means there will be no total result
1037 // so it doesn't matter that the partials were not recorded
1038 return;
1039 }
1040
1041 // Partial results must be aggregated in-order for that frame number
1042 List<CaptureResult> partials = mPartialResults.get(frameNumber);
1043 if (partials == null) {
1044 partials = new ArrayList<>();
1045 mPartialResults.put(frameNumber, partials);
1046 }
1047
1048 partials.add(result);
1049 }
1050
1051 /**
1052 * Attempt to pop off all of the partial results seen so far for the {@code frameNumber}.
1053 *
1054 * <p>Once popped-off, the partial results are forgotten (unless {@code updateTracker}
1055 * is called again with new partials for that frame number).</p>
1056 *
1057 * @param frameNumber the frame number corresponding to the result
1058 * @return a list of partial results for that frame with at least 1 element,
1059 * or {@code null} if there were no partials recorded for that frame
1060 */
1061 public List<CaptureResult> popPartialResults(long frameNumber) {
1062 return mPartialResults.remove(frameNumber);
1063 }
1064
Jianing Weid2c3a822014-03-27 18:27:43 -07001065 public long getCompletedFrameNumber() {
1066 return mCompletedFrameNumber;
1067 }
1068
1069 }
1070
1071 private void checkAndFireSequenceComplete() {
1072 long completedFrameNumber = mFrameNumberTracker.getCompletedFrameNumber();
1073 Iterator<SimpleEntry<Long, Integer> > iter = mFrameNumberRequestPairs.iterator();
1074 while (iter.hasNext()) {
1075 final SimpleEntry<Long, Integer> frameNumberRequestPair = iter.next();
1076 if (frameNumberRequestPair.getKey() <= completedFrameNumber) {
1077
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001078 // remove request from mCaptureCallbackMap
Jianing Weid2c3a822014-03-27 18:27:43 -07001079 final int requestId = frameNumberRequestPair.getValue();
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001080 final CaptureCallbackHolder holder;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07001081 synchronized(mInterfaceLock) {
1082 if (mRemoteDevice == null) {
Igor Murashkin49b2b132014-06-18 19:03:00 -07001083 Log.w(TAG, "Camera closed while checking sequences");
1084 return;
1085 }
1086
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001087 int index = mCaptureCallbackMap.indexOfKey(requestId);
1088 holder = (index >= 0) ? mCaptureCallbackMap.valueAt(index)
Jianing Weid2c3a822014-03-27 18:27:43 -07001089 : null;
1090 if (holder != null) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001091 mCaptureCallbackMap.removeAt(index);
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001092 if (DEBUG) {
1093 Log.v(TAG, String.format(
1094 "remove holder for requestId %d, "
1095 + "because lastFrame %d is <= %d",
1096 requestId, frameNumberRequestPair.getKey(),
1097 completedFrameNumber));
1098 }
Jianing Weid2c3a822014-03-27 18:27:43 -07001099 }
1100 }
1101 iter.remove();
1102
1103 // Call onCaptureSequenceCompleted
1104 if (holder != null) {
1105 Runnable resultDispatch = new Runnable() {
1106 @Override
1107 public void run() {
Igor Murashkin21547d62014-06-04 15:21:42 -07001108 if (!CameraDeviceImpl.this.isClosed()){
Jianing Weid2c3a822014-03-27 18:27:43 -07001109 if (DEBUG) {
1110 Log.d(TAG, String.format(
1111 "fire sequence complete for request %d",
1112 requestId));
1113 }
1114
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001115 long lastFrameNumber = frameNumberRequestPair.getKey();
1116 if (lastFrameNumber < Integer.MIN_VALUE
1117 || lastFrameNumber > Integer.MAX_VALUE) {
1118 throw new AssertionError(lastFrameNumber
1119 + " cannot be cast to int");
1120 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001121 holder.getCallback().onCaptureSequenceCompleted(
Igor Murashkin21547d62014-06-04 15:21:42 -07001122 CameraDeviceImpl.this,
Jianing Weid2c3a822014-03-27 18:27:43 -07001123 requestId,
Igor Murashkindb075af2014-05-21 10:07:08 -07001124 lastFrameNumber);
Jianing Weid2c3a822014-03-27 18:27:43 -07001125 }
1126 }
1127 };
1128 holder.getHandler().post(resultDispatch);
1129 }
1130
1131 }
1132 }
1133 }
1134
Zhijun Heecb323e2013-07-31 09:40:27 -07001135 public class CameraDeviceCallbacks extends ICameraDeviceCallbacks.Stub {
Igor Murashkin70725502013-06-25 20:27:06 +00001136
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001137 //
1138 // Constants below need to be kept up-to-date with
1139 // frameworks/av/include/camera/camera2/ICameraDeviceCallbacks.h
1140 //
1141
1142 //
1143 // Error codes for onCameraError
1144 //
1145
1146 /**
1147 * Camera has been disconnected
1148 */
1149 static final int ERROR_CAMERA_DISCONNECTED = 0;
1150
1151 /**
1152 * Camera has encountered a device-level error
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001153 * Matches CameraDevice.StateCallback#ERROR_CAMERA_DEVICE
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001154 */
1155 static final int ERROR_CAMERA_DEVICE = 1;
1156
1157 /**
1158 * Camera has encountered a service-level error
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001159 * Matches CameraDevice.StateCallback#ERROR_CAMERA_SERVICE
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001160 */
1161 static final int ERROR_CAMERA_SERVICE = 2;
1162
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001163 /**
1164 * Camera has encountered an error processing a single request.
1165 */
1166 static final int ERROR_CAMERA_REQUEST = 3;
1167
1168 /**
1169 * Camera has encountered an error producing metadata for a single capture
1170 */
1171 static final int ERROR_CAMERA_RESULT = 4;
1172
1173 /**
1174 * Camera has encountered an error producing an image buffer for a single capture
1175 */
1176 static final int ERROR_CAMERA_BUFFER = 5;
1177
Igor Murashkin70725502013-06-25 20:27:06 +00001178 @Override
1179 public IBinder asBinder() {
1180 return this;
1181 }
1182
Igor Murashkin70725502013-06-25 20:27:06 +00001183 @Override
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001184 public void onDeviceError(final int errorCode, CaptureResultExtras resultExtras) {
1185 if (DEBUG) {
1186 Log.d(TAG, String.format(
1187 "Device error received, code %d, frame number %d, request ID %d, subseq ID %d",
1188 errorCode, resultExtras.getFrameNumber(), resultExtras.getRequestId(),
1189 resultExtras.getSubsequenceId()));
1190 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001191
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07001192 synchronized(mInterfaceLock) {
1193 if (mRemoteDevice == null) {
Igor Murashkin49b2b132014-06-18 19:03:00 -07001194 return; // Camera already closed
1195 }
1196
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001197 switch (errorCode) {
1198 case ERROR_CAMERA_DISCONNECTED:
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001199 CameraDeviceImpl.this.mDeviceHandler.post(mCallOnDisconnected);
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001200 break;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001201 default:
1202 Log.e(TAG, "Unknown error from camera device: " + errorCode);
1203 // no break
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001204 case ERROR_CAMERA_DEVICE:
1205 case ERROR_CAMERA_SERVICE:
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001206 mInError = true;
1207 Runnable r = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -07001208 @Override
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001209 public void run() {
Igor Murashkin21547d62014-06-04 15:21:42 -07001210 if (!CameraDeviceImpl.this.isClosed()) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001211 mDeviceCallback.onError(CameraDeviceImpl.this, errorCode);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001212 }
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001213 }
1214 };
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001215 CameraDeviceImpl.this.mDeviceHandler.post(r);
1216 break;
1217 case ERROR_CAMERA_REQUEST:
1218 case ERROR_CAMERA_RESULT:
1219 case ERROR_CAMERA_BUFFER:
1220 onCaptureErrorLocked(errorCode, resultExtras);
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001221 break;
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001222 }
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001223 }
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001224 }
1225
1226 @Override
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001227 public void onDeviceIdle() {
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001228 if (DEBUG) {
1229 Log.d(TAG, "Camera now idle");
1230 }
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07001231 synchronized(mInterfaceLock) {
1232 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -07001233
Igor Murashkin21547d62014-06-04 15:21:42 -07001234 if (!CameraDeviceImpl.this.mIdle) {
1235 CameraDeviceImpl.this.mDeviceHandler.post(mCallOnIdle);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001236 }
Igor Murashkin21547d62014-06-04 15:21:42 -07001237 CameraDeviceImpl.this.mIdle = true;
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001238 }
1239 }
1240
1241 @Override
Jianing Weid2c3a822014-03-27 18:27:43 -07001242 public void onCaptureStarted(final CaptureResultExtras resultExtras, final long timestamp) {
1243 int requestId = resultExtras.getRequestId();
Eino-Ville Talvalae8df3092014-09-08 14:30:50 -07001244 final long frameNumber = resultExtras.getFrameNumber();
1245
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001246 if (DEBUG) {
Eino-Ville Talvalae8df3092014-09-08 14:30:50 -07001247 Log.d(TAG, "Capture started for id " + requestId + " frame number " + frameNumber);
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001248 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001249 final CaptureCallbackHolder holder;
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001250
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07001251 synchronized(mInterfaceLock) {
1252 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -07001253
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001254 // Get the callback for this frame ID, if there is one
1255 holder = CameraDeviceImpl.this.mCaptureCallbackMap.get(requestId);
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001256
Igor Murashkin49b2b132014-06-18 19:03:00 -07001257 if (holder == null) {
1258 return;
1259 }
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001260
Igor Murashkin49b2b132014-06-18 19:03:00 -07001261 if (isClosed()) return;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001262
Igor Murashkin49b2b132014-06-18 19:03:00 -07001263 // Dispatch capture start notice
1264 holder.getHandler().post(
1265 new Runnable() {
1266 @Override
1267 public void run() {
1268 if (!CameraDeviceImpl.this.isClosed()) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001269 holder.getCallback().onCaptureStarted(
Igor Murashkin49b2b132014-06-18 19:03:00 -07001270 CameraDeviceImpl.this,
1271 holder.getRequest(resultExtras.getSubsequenceId()),
Eino-Ville Talvalae8df3092014-09-08 14:30:50 -07001272 timestamp, frameNumber);
Igor Murashkin49b2b132014-06-18 19:03:00 -07001273 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001274 }
Igor Murashkin49b2b132014-06-18 19:03:00 -07001275 });
1276
1277 }
Igor Murashkin70725502013-06-25 20:27:06 +00001278 }
1279
1280 @Override
Jianing Weid2c3a822014-03-27 18:27:43 -07001281 public void onResultReceived(CameraMetadataNative result,
1282 CaptureResultExtras resultExtras) throws RemoteException {
Ruben Brunk57493682014-05-27 18:58:08 -07001283
Jianing Weid2c3a822014-03-27 18:27:43 -07001284 int requestId = resultExtras.getRequestId();
Igor Murashkinbdf366c2014-07-25 16:54:20 -07001285 long frameNumber = resultExtras.getFrameNumber();
1286
Zhijun Heecb323e2013-07-31 09:40:27 -07001287 if (DEBUG) {
Igor Murashkinbdf366c2014-07-25 16:54:20 -07001288 Log.v(TAG, "Received result frame " + frameNumber + " for id "
Jianing Weibaf0c652014-04-18 17:35:00 -07001289 + requestId);
Zhijun Heecb323e2013-07-31 09:40:27 -07001290 }
Ruben Brunk57493682014-05-27 18:58:08 -07001291
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07001292 synchronized(mInterfaceLock) {
1293 if (mRemoteDevice == null) return; // Camera already closed
Ruben Brunk57493682014-05-27 18:58:08 -07001294
Igor Murashkin49b2b132014-06-18 19:03:00 -07001295 // TODO: Handle CameraCharacteristics access from CaptureResult correctly.
1296 result.set(CameraCharacteristics.LENS_INFO_SHADING_MAP_SIZE,
1297 getCharacteristics().get(CameraCharacteristics.LENS_INFO_SHADING_MAP_SIZE));
Ruben Brunk57493682014-05-27 18:58:08 -07001298
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001299 final CaptureCallbackHolder holder =
1300 CameraDeviceImpl.this.mCaptureCallbackMap.get(requestId);
Igor Murashkin70725502013-06-25 20:27:06 +00001301
Zhijun He83159152014-07-16 11:32:59 -07001302 boolean isPartialResult =
1303 (resultExtras.getPartialResultCount() < mTotalPartialCount);
Eino-Ville Talvala7a313102013-11-07 14:45:06 -08001304
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001305 // Check if we have a callback for this
Igor Murashkin49b2b132014-06-18 19:03:00 -07001306 if (holder == null) {
1307 if (DEBUG) {
1308 Log.d(TAG,
1309 "holder is null, early return at frame "
Igor Murashkinbdf366c2014-07-25 16:54:20 -07001310 + frameNumber);
Igor Murashkin49b2b132014-06-18 19:03:00 -07001311 }
Igor Murashkin1e854c52014-08-28 15:21:49 -07001312
1313 mFrameNumberTracker.updateTracker(frameNumber, /*result*/null, isPartialResult);
1314
Igor Murashkin49b2b132014-06-18 19:03:00 -07001315 return;
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001316 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001317
Igor Murashkin49b2b132014-06-18 19:03:00 -07001318 if (isClosed()) {
1319 if (DEBUG) {
1320 Log.d(TAG,
1321 "camera is closed, early return at frame "
Igor Murashkinbdf366c2014-07-25 16:54:20 -07001322 + frameNumber);
Eino-Ville Talvala7a313102013-11-07 14:45:06 -08001323 }
Igor Murashkin1e854c52014-08-28 15:21:49 -07001324
1325 mFrameNumberTracker.updateTracker(frameNumber, /*result*/null, isPartialResult);
Igor Murashkin49b2b132014-06-18 19:03:00 -07001326 return;
1327 }
Igor Murashkindb075af2014-05-21 10:07:08 -07001328
Igor Murashkin49b2b132014-06-18 19:03:00 -07001329 final CaptureRequest request = holder.getRequest(resultExtras.getSubsequenceId());
1330
Igor Murashkin49b2b132014-06-18 19:03:00 -07001331 Runnable resultDispatch = null;
1332
Igor Murashkin1e854c52014-08-28 15:21:49 -07001333 CaptureResult finalResult;
1334
Igor Murashkin49b2b132014-06-18 19:03:00 -07001335 // Either send a partial result or the final capture completed result
Zhijun He83159152014-07-16 11:32:59 -07001336 if (isPartialResult) {
Igor Murashkin49b2b132014-06-18 19:03:00 -07001337 final CaptureResult resultAsCapture =
Igor Murashkinbdf366c2014-07-25 16:54:20 -07001338 new CaptureResult(result, request, resultExtras);
Igor Murashkin49b2b132014-06-18 19:03:00 -07001339
1340 // Partial result
1341 resultDispatch = new Runnable() {
1342 @Override
1343 public void run() {
1344 if (!CameraDeviceImpl.this.isClosed()){
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001345 holder.getCallback().onCaptureProgressed(
Igor Murashkin49b2b132014-06-18 19:03:00 -07001346 CameraDeviceImpl.this,
1347 request,
1348 resultAsCapture);
1349 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001350 }
Igor Murashkin49b2b132014-06-18 19:03:00 -07001351 };
Igor Murashkin1e854c52014-08-28 15:21:49 -07001352
1353 finalResult = resultAsCapture;
Igor Murashkin49b2b132014-06-18 19:03:00 -07001354 } else {
Igor Murashkin1e854c52014-08-28 15:21:49 -07001355 List<CaptureResult> partialResults =
1356 mFrameNumberTracker.popPartialResults(frameNumber);
1357
Igor Murashkin49b2b132014-06-18 19:03:00 -07001358 final TotalCaptureResult resultAsCapture =
Igor Murashkin1e854c52014-08-28 15:21:49 -07001359 new TotalCaptureResult(result, request, resultExtras, partialResults);
Eino-Ville Talvala7a313102013-11-07 14:45:06 -08001360
Igor Murashkin49b2b132014-06-18 19:03:00 -07001361 // Final capture result
1362 resultDispatch = new Runnable() {
1363 @Override
1364 public void run() {
1365 if (!CameraDeviceImpl.this.isClosed()){
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001366 holder.getCallback().onCaptureCompleted(
Igor Murashkin49b2b132014-06-18 19:03:00 -07001367 CameraDeviceImpl.this,
1368 request,
1369 resultAsCapture);
1370 }
1371 }
1372 };
Igor Murashkin1e854c52014-08-28 15:21:49 -07001373
1374 finalResult = resultAsCapture;
Igor Murashkin49b2b132014-06-18 19:03:00 -07001375 }
Jianing Weid2c3a822014-03-27 18:27:43 -07001376
Igor Murashkin49b2b132014-06-18 19:03:00 -07001377 holder.getHandler().post(resultDispatch);
1378
Igor Murashkin1e854c52014-08-28 15:21:49 -07001379 // Collect the partials for a total result; or mark the frame as totally completed
1380 mFrameNumberTracker.updateTracker(frameNumber, finalResult, isPartialResult);
1381
Igor Murashkin49b2b132014-06-18 19:03:00 -07001382 // Fire onCaptureSequenceCompleted
Zhijun He83159152014-07-16 11:32:59 -07001383 if (!isPartialResult) {
Igor Murashkin49b2b132014-06-18 19:03:00 -07001384 checkAndFireSequenceComplete();
1385 }
Jianing Weid2c3a822014-03-27 18:27:43 -07001386 }
Igor Murashkin70725502013-06-25 20:27:06 +00001387 }
1388
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001389 /**
1390 * Called by onDeviceError for handling single-capture failures.
1391 */
1392 private void onCaptureErrorLocked(int errorCode, CaptureResultExtras resultExtras) {
1393
1394 final int requestId = resultExtras.getRequestId();
1395 final int subsequenceId = resultExtras.getSubsequenceId();
1396 final long frameNumber = resultExtras.getFrameNumber();
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001397 final CaptureCallbackHolder holder =
1398 CameraDeviceImpl.this.mCaptureCallbackMap.get(requestId);
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001399
1400 final CaptureRequest request = holder.getRequest(subsequenceId);
1401
1402 // No way to report buffer errors right now
1403 if (errorCode == ERROR_CAMERA_BUFFER) {
1404 Log.e(TAG, String.format("Lost output buffer reported for frame %d", frameNumber));
1405 return;
1406 }
1407
1408 boolean mayHaveBuffers = (errorCode == ERROR_CAMERA_RESULT);
1409
1410 // This is only approximate - exact handling needs the camera service and HAL to
1411 // disambiguate between request failures to due abort and due to real errors.
1412 // For now, assume that if the session believes we're mid-abort, then the error
1413 // is due to abort.
1414 int reason = (mCurrentSession != null && mCurrentSession.isAborting()) ?
1415 CaptureFailure.REASON_FLUSHED :
1416 CaptureFailure.REASON_ERROR;
1417
1418 final CaptureFailure failure = new CaptureFailure(
1419 request,
1420 reason,
1421 /*dropped*/ mayHaveBuffers,
1422 requestId,
1423 frameNumber);
1424
1425 Runnable failureDispatch = new Runnable() {
1426 @Override
1427 public void run() {
1428 if (!CameraDeviceImpl.this.isClosed()){
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001429 holder.getCallback().onCaptureFailed(
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001430 CameraDeviceImpl.this,
1431 request,
1432 failure);
1433 }
1434 }
1435 };
1436 holder.getHandler().post(failureDispatch);
1437
1438 // Fire onCaptureSequenceCompleted if appropriate
1439 if (DEBUG) {
1440 Log.v(TAG, String.format("got error frame %d", frameNumber));
1441 }
1442 mFrameNumberTracker.updateTracker(frameNumber, /*error*/true);
1443 checkAndFireSequenceComplete();
1444 }
1445
1446 } // public class CameraDeviceCallbacks
Igor Murashkin70725502013-06-25 20:27:06 +00001447
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001448 /**
Igor Murashkin0b27d342014-05-30 09:45:05 -07001449 * Default handler management.
1450 *
1451 * <p>
1452 * If handler is null, get the current thread's
1453 * Looper to create a Handler with. If no looper exists, throw {@code IllegalArgumentException}.
1454 * </p>
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001455 */
Igor Murashkin0b27d342014-05-30 09:45:05 -07001456 static Handler checkHandler(Handler handler) {
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001457 if (handler == null) {
1458 Looper looper = Looper.myLooper();
1459 if (looper == null) {
1460 throw new IllegalArgumentException(
1461 "No handler given, and current thread has no looper!");
1462 }
1463 handler = new Handler(looper);
1464 }
1465 return handler;
1466 }
1467
Eino-Ville Talvala7875a882014-07-31 12:47:07 -07001468 /**
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001469 * Default handler management, conditional on there being a callback.
Eino-Ville Talvala7875a882014-07-31 12:47:07 -07001470 *
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001471 * <p>If the callback isn't null, check the handler, otherwise pass it through.</p>
Eino-Ville Talvala7875a882014-07-31 12:47:07 -07001472 */
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001473 static <T> Handler checkHandler(Handler handler, T callback) {
1474 if (callback != null) {
Eino-Ville Talvala7875a882014-07-31 12:47:07 -07001475 return checkHandler(handler);
1476 }
1477 return handler;
1478 }
1479
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -07001480 private void checkIfCameraClosedOrInError() throws CameraAccessException {
1481 if (mInError) {
1482 throw new CameraAccessException(CameraAccessException.CAMERA_ERROR,
1483 "The camera device has encountered a serious error");
1484 }
Zhijun He7f4d3142013-07-23 07:54:38 -07001485 if (mRemoteDevice == null) {
1486 throw new IllegalStateException("CameraDevice was already closed");
1487 }
1488 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001489
Igor Murashkin49b2b132014-06-18 19:03:00 -07001490 /** Whether the camera device has started to close (may not yet have finished) */
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001491 private boolean isClosed() {
Igor Murashkin49b2b132014-06-18 19:03:00 -07001492 return mClosing;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001493 }
Ruben Brunk57493682014-05-27 18:58:08 -07001494
1495 private CameraCharacteristics getCharacteristics() {
1496 return mCharacteristics;
1497 }
Igor Murashkin70725502013-06-25 20:27:06 +00001498}