blob: de1d9a39114d9f978973346a8b4826e2b50d4063 [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 Talvala2f1a2e42013-07-25 17:12:05 -070024import android.hardware.camera2.CaptureRequest;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070025import android.hardware.camera2.CaptureResult;
26import android.hardware.camera2.ICameraDeviceCallbacks;
27import android.hardware.camera2.ICameraDeviceUser;
Igor Murashkindb075af2014-05-21 10:07:08 -070028import android.hardware.camera2.TotalCaptureResult;
Eino-Ville Talvala5afd3e92013-08-21 10:37:04 -070029import android.hardware.camera2.utils.CameraBinderDecorator;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070030import android.hardware.camera2.utils.CameraRuntimeException;
Igor Murashkin49b2b132014-06-18 19:03:00 -070031import android.hardware.camera2.utils.CloseableLock;
32import android.hardware.camera2.utils.CloseableLock.ScopedLock;
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 Murashkin57ea59b2013-08-23 16:55:57 -070044import java.util.HashSet;
Ruben Brunkdecfe952013-10-29 11:00:32 -070045import java.util.Iterator;
Igor Murashkin70725502013-06-25 20:27:06 +000046import java.util.List;
Jianing Weid2c3a822014-03-27 18:27:43 -070047import java.util.TreeSet;
Igor Murashkin70725502013-06-25 20:27:06 +000048
49/**
Eino-Ville Talvala70c22072013-08-27 12:09:04 -070050 * HAL2.1+ implementation of CameraDevice. Use CameraManager#open to instantiate
Igor Murashkin70725502013-06-25 20:27:06 +000051 */
Igor Murashkin21547d62014-06-04 15:21:42 -070052public class CameraDeviceImpl extends android.hardware.camera2.CameraDevice {
Igor Murashkin70725502013-06-25 20:27:06 +000053
54 private final String TAG;
Zhijun Heecb323e2013-07-31 09:40:27 -070055 private final boolean DEBUG;
Igor Murashkin70725502013-06-25 20:27:06 +000056
Ruben Brunkdecfe952013-10-29 11:00:32 -070057 private static final int REQUEST_ID_NONE = -1;
58
Igor Murashkin70725502013-06-25 20:27:06 +000059 // TODO: guard every function with if (!mRemoteDevice) check (if it was closed)
60 private ICameraDeviceUser mRemoteDevice;
61
Igor Murashkin49b2b132014-06-18 19:03:00 -070062 private final CloseableLock mCloseLock;
Igor Murashkin70725502013-06-25 20:27:06 +000063 private final CameraDeviceCallbacks mCallbacks = new CameraDeviceCallbacks();
64
Eino-Ville Talvala868d9042013-10-03 11:15:21 -070065 private final StateListener mDeviceListener;
Igor Murashkin0b27d342014-05-30 09:45:05 -070066 private volatile StateListener mSessionStateListener;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -070067 private final Handler mDeviceHandler;
68
Igor Murashkin49b2b132014-06-18 19:03:00 -070069 private volatile boolean mClosing = false;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -070070 private boolean mInError = false;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -070071 private boolean mIdle = true;
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -070072
Igor Murashkin21547d62014-06-04 15:21:42 -070073 /** map request IDs to listener/request data */
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -070074 private final SparseArray<CaptureListenerHolder> mCaptureListenerMap =
75 new SparseArray<CaptureListenerHolder>();
Igor Murashkin70725502013-06-25 20:27:06 +000076
Ruben Brunkdecfe952013-10-29 11:00:32 -070077 private int mRepeatingRequestId = REQUEST_ID_NONE;
78 private final ArrayList<Integer> mRepeatingRequestIdDeletedList = new ArrayList<Integer>();
Igor Murashkin57ea59b2013-08-23 16:55:57 -070079 // Map stream IDs to Surfaces
80 private final SparseArray<Surface> mConfiguredOutputs = new SparseArray<Surface>();
Igor Murashkin70725502013-06-25 20:27:06 +000081
82 private final String mCameraId;
Ruben Brunk57493682014-05-27 18:58:08 -070083 private final CameraCharacteristics mCharacteristics;
Igor Murashkin70725502013-06-25 20:27:06 +000084
Jianing Weid2c3a822014-03-27 18:27:43 -070085 /**
86 * A list tracking request and its expected last frame.
87 * Updated when calling ICameraDeviceUser methods.
88 */
89 private final List<SimpleEntry</*frameNumber*/Long, /*requestId*/Integer>>
90 mFrameNumberRequestPairs = new ArrayList<SimpleEntry<Long, Integer>>();
91
92 /**
93 * An object tracking received frame numbers.
94 * Updated when receiving callbacks from ICameraDeviceCallbacks.
95 */
96 private final FrameNumberTracker mFrameNumberTracker = new FrameNumberTracker();
97
Igor Murashkin0b27d342014-05-30 09:45:05 -070098 private CameraCaptureSessionImpl mCurrentSession;
99
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700100 // Runnables for all state transitions, except error, which needs the
101 // error code argument
102
103 private final Runnable mCallOnOpened = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700104 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700105 public void run() {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700106 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
107 if (scopedLock == null) return; // Camera already closed
108
Igor Murashkin0b27d342014-05-30 09:45:05 -0700109 StateListener sessionListener = mSessionStateListener;
110 if (sessionListener != null) {
Igor Murashkin21547d62014-06-04 15:21:42 -0700111 sessionListener.onOpened(CameraDeviceImpl.this);
Igor Murashkin0b27d342014-05-30 09:45:05 -0700112 }
Sol Boucher4b3f8002014-06-05 13:47:24 -0700113 mDeviceListener.onOpened(CameraDeviceImpl.this);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700114 }
115 }
116 };
117
118 private final Runnable mCallOnUnconfigured = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700119 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700120 public void run() {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700121 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
122 if (scopedLock == null) return; // Camera already closed
123
Igor Murashkin0b27d342014-05-30 09:45:05 -0700124 StateListener sessionListener = mSessionStateListener;
125 if (sessionListener != null) {
Igor Murashkin21547d62014-06-04 15:21:42 -0700126 sessionListener.onUnconfigured(CameraDeviceImpl.this);
Igor Murashkin0b27d342014-05-30 09:45:05 -0700127 }
Sol Boucher4b3f8002014-06-05 13:47:24 -0700128 mDeviceListener.onUnconfigured(CameraDeviceImpl.this);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700129 }
130 }
131 };
132
133 private final Runnable mCallOnActive = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700134 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700135 public void run() {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700136 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
137 if (scopedLock == null) return; // Camera already closed
138
Igor Murashkin0b27d342014-05-30 09:45:05 -0700139 StateListener sessionListener = mSessionStateListener;
140 if (sessionListener != null) {
Igor Murashkin21547d62014-06-04 15:21:42 -0700141 sessionListener.onActive(CameraDeviceImpl.this);
Igor Murashkin0b27d342014-05-30 09:45:05 -0700142 }
Sol Boucher4b3f8002014-06-05 13:47:24 -0700143 mDeviceListener.onActive(CameraDeviceImpl.this);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700144 }
145 }
146 };
147
148 private final Runnable mCallOnBusy = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700149 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700150 public void run() {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700151 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
152 if (scopedLock == null) return; // Camera already closed
153
Igor Murashkin0b27d342014-05-30 09:45:05 -0700154 StateListener sessionListener = mSessionStateListener;
155 if (sessionListener != null) {
Igor Murashkin21547d62014-06-04 15:21:42 -0700156 sessionListener.onBusy(CameraDeviceImpl.this);
Igor Murashkin0b27d342014-05-30 09:45:05 -0700157 }
Sol Boucher4b3f8002014-06-05 13:47:24 -0700158 mDeviceListener.onBusy(CameraDeviceImpl.this);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700159 }
160 }
161 };
162
163 private final Runnable mCallOnClosed = new Runnable() {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700164 private boolean mClosedOnce = false;
165
Jianing Weid2c3a822014-03-27 18:27:43 -0700166 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700167 public void run() {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700168 if (mClosedOnce) {
169 throw new AssertionError("Don't post #onClosed more than once");
170 }
171
Igor Murashkin0b27d342014-05-30 09:45:05 -0700172 StateListener sessionListener = mSessionStateListener;
173 if (sessionListener != null) {
Igor Murashkin21547d62014-06-04 15:21:42 -0700174 sessionListener.onClosed(CameraDeviceImpl.this);
Igor Murashkin0b27d342014-05-30 09:45:05 -0700175 }
Sol Boucher4b3f8002014-06-05 13:47:24 -0700176 mDeviceListener.onClosed(CameraDeviceImpl.this);
Igor Murashkin49b2b132014-06-18 19:03:00 -0700177 mClosedOnce = true;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700178 }
179 };
180
181 private final Runnable mCallOnIdle = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700182 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700183 public void run() {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700184 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
185 if (scopedLock == null) return; // Camera already closed
186
Igor Murashkin0b27d342014-05-30 09:45:05 -0700187 StateListener sessionListener = mSessionStateListener;
188 if (sessionListener != null) {
Igor Murashkin21547d62014-06-04 15:21:42 -0700189 sessionListener.onIdle(CameraDeviceImpl.this);
Igor Murashkin0b27d342014-05-30 09:45:05 -0700190 }
Sol Boucher4b3f8002014-06-05 13:47:24 -0700191 mDeviceListener.onIdle(CameraDeviceImpl.this);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700192 }
193 }
194 };
195
196 private final Runnable mCallOnDisconnected = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700197 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700198 public void run() {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700199 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
200 if (scopedLock == null) return; // Camera already closed
201
Igor Murashkin0b27d342014-05-30 09:45:05 -0700202 StateListener sessionListener = mSessionStateListener;
203 if (sessionListener != null) {
Igor Murashkin21547d62014-06-04 15:21:42 -0700204 sessionListener.onDisconnected(CameraDeviceImpl.this);
Igor Murashkin0b27d342014-05-30 09:45:05 -0700205 }
Sol Boucher4b3f8002014-06-05 13:47:24 -0700206 mDeviceListener.onDisconnected(CameraDeviceImpl.this);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700207 }
208 }
209 };
210
Igor Murashkin21547d62014-06-04 15:21:42 -0700211 public CameraDeviceImpl(String cameraId, StateListener listener, Handler handler,
Ruben Brunk57493682014-05-27 18:58:08 -0700212 CameraCharacteristics characteristics) {
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700213 if (cameraId == null || listener == null || handler == null) {
214 throw new IllegalArgumentException("Null argument given");
215 }
Igor Murashkin70725502013-06-25 20:27:06 +0000216 mCameraId = cameraId;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700217 mDeviceListener = listener;
218 mDeviceHandler = handler;
Ruben Brunk57493682014-05-27 18:58:08 -0700219 mCharacteristics = characteristics;
Igor Murashkin49b2b132014-06-18 19:03:00 -0700220 mCloseLock = new CloseableLock(/*name*/"CD-" + mCameraId);
Eino-Ville Talvalab72d1ef2014-04-28 13:21:18 -0700221
222 final int MAX_TAG_LEN = 23;
223 String tag = String.format("CameraDevice-JV-%s", mCameraId);
224 if (tag.length() > MAX_TAG_LEN) {
225 tag = tag.substring(0, MAX_TAG_LEN);
226 }
227 TAG = tag;
Zhijun Heecb323e2013-07-31 09:40:27 -0700228 DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Igor Murashkin70725502013-06-25 20:27:06 +0000229 }
230
231 public CameraDeviceCallbacks getCallbacks() {
232 return mCallbacks;
233 }
234
Igor Murashkin70725502013-06-25 20:27:06 +0000235 public void setRemoteDevice(ICameraDeviceUser remoteDevice) {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700236 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
Eino-Ville Talvala5afd3e92013-08-21 10:37:04 -0700237 // TODO: Move from decorator to direct binder-mediated exceptions
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700238 // If setRemoteFailure already called, do nothing
239 if (mInError) return;
240
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700241 mRemoteDevice = CameraBinderDecorator.newInstance(remoteDevice);
242
243 mDeviceHandler.post(mCallOnOpened);
244 mDeviceHandler.post(mCallOnUnconfigured);
245 }
Igor Murashkin70725502013-06-25 20:27:06 +0000246 }
247
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700248 /**
249 * Call to indicate failed connection to a remote camera device.
250 *
251 * <p>This places the camera device in the error state and informs the listener.
252 * Use in place of setRemoteDevice() when startup fails.</p>
253 */
254 public void setRemoteFailure(final CameraRuntimeException failure) {
255 int failureCode = StateListener.ERROR_CAMERA_DEVICE;
256 boolean failureIsError = true;
257
258 switch (failure.getReason()) {
259 case CameraAccessException.CAMERA_IN_USE:
260 failureCode = StateListener.ERROR_CAMERA_IN_USE;
261 break;
262 case CameraAccessException.MAX_CAMERAS_IN_USE:
263 failureCode = StateListener.ERROR_MAX_CAMERAS_IN_USE;
264 break;
265 case CameraAccessException.CAMERA_DISABLED:
266 failureCode = StateListener.ERROR_CAMERA_DISABLED;
267 break;
268 case CameraAccessException.CAMERA_DISCONNECTED:
269 failureIsError = false;
270 break;
271 case CameraAccessException.CAMERA_ERROR:
272 failureCode = StateListener.ERROR_CAMERA_DEVICE;
273 break;
274 default:
275 Log.wtf(TAG, "Unknown failure in opening camera device: " + failure.getReason());
276 break;
277 }
278 final int code = failureCode;
279 final boolean isError = failureIsError;
Igor Murashkin49b2b132014-06-18 19:03:00 -0700280 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
281 if (scopedLock == null) return; // Camera already closed, can't go to error state
282
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700283 mInError = true;
284 mDeviceHandler.post(new Runnable() {
Igor Murashkine1442202014-06-09 17:51:24 -0700285 @Override
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700286 public void run() {
287 if (isError) {
288 mDeviceListener.onError(CameraDeviceImpl.this, code);
289 } else {
290 mDeviceListener.onDisconnected(CameraDeviceImpl.this);
291 }
292 }
293 });
294 }
295 }
296
Igor Murashkin70725502013-06-25 20:27:06 +0000297 @Override
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700298 public String getId() {
299 return mCameraId;
300 }
301
302 @Override
Igor Murashkin70725502013-06-25 20:27:06 +0000303 public void configureOutputs(List<Surface> outputs) throws CameraAccessException {
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700304 // Treat a null input the same an empty list
305 if (outputs == null) {
306 outputs = new ArrayList<Surface>();
307 }
Igor Murashkin49b2b132014-06-18 19:03:00 -0700308 try (ScopedLock scopedLock = mCloseLock.acquireExclusiveLock()) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700309 checkIfCameraClosedOrInError();
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700310
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700311 HashSet<Surface> addSet = new HashSet<Surface>(outputs); // Streams to create
312 List<Integer> deleteList = new ArrayList<Integer>(); // Streams to delete
313
314 // Determine which streams need to be created, which to be deleted
315 for (int i = 0; i < mConfiguredOutputs.size(); ++i) {
316 int streamId = mConfiguredOutputs.keyAt(i);
317 Surface s = mConfiguredOutputs.valueAt(i);
318
319 if (!outputs.contains(s)) {
320 deleteList.add(streamId);
321 } else {
322 addSet.remove(s); // Don't create a stream previously created
323 }
324 }
325
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700326 mDeviceHandler.post(mCallOnBusy);
327 stopRepeating();
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700328
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700329 try {
Ruben Brunkdecfe952013-10-29 11:00:32 -0700330 waitUntilIdle();
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700331
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700332 mRemoteDevice.beginConfigure();
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700333 // Delete all streams first (to free up HW resources)
334 for (Integer streamId : deleteList) {
335 mRemoteDevice.deleteStream(streamId);
336 mConfiguredOutputs.delete(streamId);
337 }
338
339 // Add all new streams
340 for (Surface s : addSet) {
Igor Murashkin70725502013-06-25 20:27:06 +0000341 // TODO: remove width,height,format since we are ignoring
342 // it.
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700343 int streamId = mRemoteDevice.createStream(0, 0, 0, s);
344 mConfiguredOutputs.put(streamId, s);
Igor Murashkin70725502013-06-25 20:27:06 +0000345 }
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700346
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700347 mRemoteDevice.endConfigure();
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700348 } catch (CameraRuntimeException e) {
349 if (e.getReason() == CAMERA_IN_USE) {
350 throw new IllegalStateException("The camera is currently busy." +
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700351 " You must wait until the previous operation completes.");
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700352 }
353
354 throw e.asChecked();
355 } catch (RemoteException e) {
356 // impossible
357 return;
Igor Murashkin70725502013-06-25 20:27:06 +0000358 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700359
360 if (outputs.size() > 0) {
361 mDeviceHandler.post(mCallOnIdle);
362 } else {
363 mDeviceHandler.post(mCallOnUnconfigured);
364 }
Igor Murashkin70725502013-06-25 20:27:06 +0000365 }
366 }
367
368 @Override
Eino-Ville Talvalacca00c62014-05-14 10:53:20 -0700369 public void createCaptureSession(List<Surface> outputs,
370 CameraCaptureSession.StateListener listener, Handler handler)
371 throws CameraAccessException {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700372 try (ScopedLock scopedLock = mCloseLock.acquireExclusiveLock()) {
Igor Murashkin0b27d342014-05-30 09:45:05 -0700373 if (DEBUG) {
374 Log.d(TAG, "createCaptureSession");
375 }
376
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700377 checkIfCameraClosedOrInError();
Igor Murashkin0b27d342014-05-30 09:45:05 -0700378
379 // TODO: we must be in UNCONFIGURED mode to begin with, or using another session
380
381 // TODO: dont block for this
382 boolean configureSuccess = true;
383 CameraAccessException pendingException = null;
384 try {
385 configureOutputs(outputs); // and then block until IDLE
386 } catch (CameraAccessException e) {
387 configureSuccess = false;
388 pendingException = e;
Igor Murashkine1442202014-06-09 17:51:24 -0700389 if (DEBUG) {
390 Log.v(TAG, "createCaptureSession - failed with exception ", e);
391 }
Igor Murashkin0b27d342014-05-30 09:45:05 -0700392 }
393
394 // Fire onConfigured if configureOutputs succeeded, fire onConfigureFailed otherwise.
395 CameraCaptureSessionImpl newSession =
396 new CameraCaptureSessionImpl(outputs, listener, handler, this, mDeviceHandler,
397 configureSuccess);
398
399 if (mCurrentSession != null) {
400 mCurrentSession.replaceSessionClose(newSession);
401 }
402
403 // TODO: wait until current session closes, then create the new session
404 mCurrentSession = newSession;
405
406 if (pendingException != null) {
407 throw pendingException;
408 }
409
410 mSessionStateListener = mCurrentSession.getDeviceStateListener();
411 }
Eino-Ville Talvalacca00c62014-05-14 10:53:20 -0700412 }
413
414 @Override
Eino-Ville Talvala70c22072013-08-27 12:09:04 -0700415 public CaptureRequest.Builder createCaptureRequest(int templateType)
416 throws CameraAccessException {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700417 try (ScopedLock scopedLock = mCloseLock.acquireExclusiveLock()) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700418 checkIfCameraClosedOrInError();
Igor Murashkin70725502013-06-25 20:27:06 +0000419
Eino-Ville Talvala70c22072013-08-27 12:09:04 -0700420 CameraMetadataNative templatedRequest = new CameraMetadataNative();
Igor Murashkin70725502013-06-25 20:27:06 +0000421
422 try {
Igor Murashkin0b27d342014-05-30 09:45:05 -0700423 mRemoteDevice.createDefaultRequest(templateType, /*out*/templatedRequest);
Igor Murashkin70725502013-06-25 20:27:06 +0000424 } catch (CameraRuntimeException e) {
425 throw e.asChecked();
426 } catch (RemoteException e) {
427 // impossible
428 return null;
429 }
430
Eino-Ville Talvala70c22072013-08-27 12:09:04 -0700431 CaptureRequest.Builder builder =
432 new CaptureRequest.Builder(templatedRequest);
Igor Murashkin70725502013-06-25 20:27:06 +0000433
Eino-Ville Talvala70c22072013-08-27 12:09:04 -0700434 return builder;
Igor Murashkin70725502013-06-25 20:27:06 +0000435 }
436 }
437
438 @Override
Igor Murashkin6bbf9dc2013-09-05 12:22:00 -0700439 public int capture(CaptureRequest request, CaptureListener listener, Handler handler)
Igor Murashkin70725502013-06-25 20:27:06 +0000440 throws CameraAccessException {
Jianing Weid2c3a822014-03-27 18:27:43 -0700441 if (DEBUG) {
442 Log.d(TAG, "calling capture");
443 }
444 List<CaptureRequest> requestList = new ArrayList<CaptureRequest>();
445 requestList.add(request);
446 return submitCaptureRequest(requestList, listener, handler, /*streaming*/false);
Igor Murashkin70725502013-06-25 20:27:06 +0000447 }
448
449 @Override
Igor Murashkin6bbf9dc2013-09-05 12:22:00 -0700450 public int captureBurst(List<CaptureRequest> requests, CaptureListener listener,
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700451 Handler handler) throws CameraAccessException {
Igor Murashkin0b27d342014-05-30 09:45:05 -0700452 if (requests == null || requests.isEmpty()) {
453 throw new IllegalArgumentException("At least one request must be given");
Zhijun Hefc19e2c2013-08-22 14:43:07 -0700454 }
Jianing Weid2c3a822014-03-27 18:27:43 -0700455 return submitCaptureRequest(requests, listener, handler, /*streaming*/false);
Igor Murashkin70725502013-06-25 20:27:06 +0000456 }
457
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700458 /**
459 * This method checks lastFrameNumber returned from ICameraDeviceUser methods for
460 * starting and stopping repeating request and flushing.
461 *
462 * <p>If lastFrameNumber is NO_FRAMES_CAPTURED, it means that the request was never
463 * sent to HAL. Then onCaptureSequenceCompleted is immediately triggered.
464 * If lastFrameNumber is non-negative, then the requestId and lastFrameNumber pair
465 * is added to the list mFrameNumberRequestPairs.</p>
466 *
467 * @param requestId the request ID of the current repeating request.
468 *
469 * @param lastFrameNumber last frame number returned from binder.
470 */
471 private void checkEarlyTriggerSequenceComplete(
472 final int requestId, final long lastFrameNumber) {
473 // lastFrameNumber being equal to NO_FRAMES_CAPTURED means that the request
474 // was never sent to HAL. Should trigger onCaptureSequenceCompleted immediately.
475 if (lastFrameNumber == CaptureListener.NO_FRAMES_CAPTURED) {
476 final CaptureListenerHolder holder;
477 int index = mCaptureListenerMap.indexOfKey(requestId);
478 holder = (index >= 0) ? mCaptureListenerMap.valueAt(index) : null;
479 if (holder != null) {
480 mCaptureListenerMap.removeAt(index);
Jianing Weibaf0c652014-04-18 17:35:00 -0700481 if (DEBUG) {
482 Log.v(TAG, String.format(
483 "remove holder for requestId %d, "
484 + "because lastFrame is %d.",
485 requestId, lastFrameNumber));
486 }
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700487 }
488
489 if (holder != null) {
490 if (DEBUG) {
491 Log.v(TAG, "immediately trigger onCaptureSequenceCompleted because"
492 + " request did not reach HAL");
493 }
494
495 Runnable resultDispatch = new Runnable() {
496 @Override
497 public void run() {
Igor Murashkin21547d62014-06-04 15:21:42 -0700498 if (!CameraDeviceImpl.this.isClosed()) {
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700499 if (DEBUG) {
500 Log.d(TAG, String.format(
501 "early trigger sequence complete for request %d",
502 requestId));
503 }
504 if (lastFrameNumber < Integer.MIN_VALUE
505 || lastFrameNumber > Integer.MAX_VALUE) {
506 throw new AssertionError(lastFrameNumber + " cannot be cast to int");
507 }
508 holder.getListener().onCaptureSequenceCompleted(
Igor Murashkin21547d62014-06-04 15:21:42 -0700509 CameraDeviceImpl.this,
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700510 requestId,
Igor Murashkindb075af2014-05-21 10:07:08 -0700511 lastFrameNumber);
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700512 }
513 }
514 };
515 holder.getHandler().post(resultDispatch);
516 } else {
517 Log.w(TAG, String.format(
518 "did not register listener to request %d",
519 requestId));
520 }
521 } else {
522 mFrameNumberRequestPairs.add(
523 new SimpleEntry<Long, Integer>(lastFrameNumber,
524 requestId));
525 }
526 }
527
Jianing Weid2c3a822014-03-27 18:27:43 -0700528 private int submitCaptureRequest(List<CaptureRequest> requestList, CaptureListener listener,
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700529 Handler handler, boolean repeating) throws CameraAccessException {
530
531 // Need a valid handler, or current thread needs to have a looper, if
532 // listener is valid
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700533 if (listener != null) {
534 handler = checkHandler(handler);
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700535 }
Igor Murashkin70725502013-06-25 20:27:06 +0000536
Igor Murashkin49b2b132014-06-18 19:03:00 -0700537 try (ScopedLock scopedLock = mCloseLock.acquireExclusiveLock()) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700538 checkIfCameraClosedOrInError();
Igor Murashkin70725502013-06-25 20:27:06 +0000539 int requestId;
540
Ruben Brunke73b41b2013-11-07 19:30:43 -0800541 if (repeating) {
542 stopRepeating();
543 }
544
Jianing Weid2c3a822014-03-27 18:27:43 -0700545 LongParcelable lastFrameNumberRef = new LongParcelable();
Igor Murashkin70725502013-06-25 20:27:06 +0000546 try {
Jianing Weid2c3a822014-03-27 18:27:43 -0700547 requestId = mRemoteDevice.submitRequestList(requestList, repeating,
548 /*out*/lastFrameNumberRef);
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700549 if (DEBUG) {
Jianing Weid2c3a822014-03-27 18:27:43 -0700550 Log.v(TAG, "last frame number " + lastFrameNumberRef.getNumber());
551 }
Igor Murashkin70725502013-06-25 20:27:06 +0000552 } catch (CameraRuntimeException e) {
553 throw e.asChecked();
554 } catch (RemoteException e) {
555 // impossible
Igor Murashkin6bbf9dc2013-09-05 12:22:00 -0700556 return -1;
Igor Murashkin70725502013-06-25 20:27:06 +0000557 }
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700558
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700559 if (listener != null) {
Jianing Weid2c3a822014-03-27 18:27:43 -0700560 mCaptureListenerMap.put(requestId, new CaptureListenerHolder(listener,
561 requestList, handler, repeating));
Jianing Weibaf0c652014-04-18 17:35:00 -0700562 } else {
563 if (DEBUG) {
564 Log.d(TAG, "Listen for request " + requestId + " is null");
565 }
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700566 }
Igor Murashkin70725502013-06-25 20:27:06 +0000567
Jianing Weid2c3a822014-03-27 18:27:43 -0700568 long lastFrameNumber = lastFrameNumberRef.getNumber();
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700569
Igor Murashkin70725502013-06-25 20:27:06 +0000570 if (repeating) {
Jianing Weid2c3a822014-03-27 18:27:43 -0700571 if (mRepeatingRequestId != REQUEST_ID_NONE) {
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700572 checkEarlyTriggerSequenceComplete(mRepeatingRequestId, lastFrameNumber);
Jianing Weid2c3a822014-03-27 18:27:43 -0700573 }
Ruben Brunkdecfe952013-10-29 11:00:32 -0700574 mRepeatingRequestId = requestId;
Jianing Weid2c3a822014-03-27 18:27:43 -0700575 } else {
576 mFrameNumberRequestPairs.add(
577 new SimpleEntry<Long, Integer>(lastFrameNumber, requestId));
Igor Murashkin70725502013-06-25 20:27:06 +0000578 }
579
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700580 if (mIdle) {
581 mDeviceHandler.post(mCallOnActive);
582 }
583 mIdle = false;
584
Igor Murashkin6bbf9dc2013-09-05 12:22:00 -0700585 return requestId;
Igor Murashkin70725502013-06-25 20:27:06 +0000586 }
587 }
588
589 @Override
Igor Murashkin6bbf9dc2013-09-05 12:22:00 -0700590 public int setRepeatingRequest(CaptureRequest request, CaptureListener listener,
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700591 Handler handler) throws CameraAccessException {
Jianing Weid2c3a822014-03-27 18:27:43 -0700592 List<CaptureRequest> requestList = new ArrayList<CaptureRequest>();
593 requestList.add(request);
594 return submitCaptureRequest(requestList, listener, handler, /*streaming*/true);
Igor Murashkin70725502013-06-25 20:27:06 +0000595 }
596
597 @Override
Igor Murashkin6bbf9dc2013-09-05 12:22:00 -0700598 public int setRepeatingBurst(List<CaptureRequest> requests, CaptureListener listener,
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700599 Handler handler) throws CameraAccessException {
Igor Murashkin0b27d342014-05-30 09:45:05 -0700600 if (requests == null || requests.isEmpty()) {
601 throw new IllegalArgumentException("At least one request must be given");
Zhijun Hefc19e2c2013-08-22 14:43:07 -0700602 }
Jianing Weid2c3a822014-03-27 18:27:43 -0700603 return submitCaptureRequest(requests, listener, handler, /*streaming*/true);
Igor Murashkin70725502013-06-25 20:27:06 +0000604 }
605
606 @Override
607 public void stopRepeating() throws CameraAccessException {
608
Igor Murashkin49b2b132014-06-18 19:03:00 -0700609 try (ScopedLock scopedLock = mCloseLock.acquireExclusiveLock()) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700610 checkIfCameraClosedOrInError();
Ruben Brunkdecfe952013-10-29 11:00:32 -0700611 if (mRepeatingRequestId != REQUEST_ID_NONE) {
612
613 int requestId = mRepeatingRequestId;
614 mRepeatingRequestId = REQUEST_ID_NONE;
615
616 // Queue for deletion after in-flight requests finish
Zhijun He1a9b6462014-03-31 16:11:33 -0700617 if (mCaptureListenerMap.get(requestId) != null) {
618 mRepeatingRequestIdDeletedList.add(requestId);
619 }
Igor Murashkin70725502013-06-25 20:27:06 +0000620
621 try {
Jianing Weid2c3a822014-03-27 18:27:43 -0700622 LongParcelable lastFrameNumberRef = new LongParcelable();
623 mRemoteDevice.cancelRequest(requestId, /*out*/lastFrameNumberRef);
624 long lastFrameNumber = lastFrameNumberRef.getNumber();
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700625
626 checkEarlyTriggerSequenceComplete(requestId, lastFrameNumber);
627
Igor Murashkin70725502013-06-25 20:27:06 +0000628 } catch (CameraRuntimeException e) {
629 throw e.asChecked();
630 } catch (RemoteException e) {
631 // impossible
632 return;
633 }
634 }
635 }
636 }
637
Zhijun Hed842fcd2013-12-26 14:14:04 -0800638 private void waitUntilIdle() throws CameraAccessException {
Zhijun He7f4d3142013-07-23 07:54:38 -0700639
Igor Murashkin49b2b132014-06-18 19:03:00 -0700640 try (ScopedLock scopedLock = mCloseLock.acquireExclusiveLock()) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700641 checkIfCameraClosedOrInError();
Ruben Brunkdecfe952013-10-29 11:00:32 -0700642 if (mRepeatingRequestId != REQUEST_ID_NONE) {
Zhijun He7f4d3142013-07-23 07:54:38 -0700643 throw new IllegalStateException("Active repeating request ongoing");
644 }
645
646 try {
647 mRemoteDevice.waitUntilIdle();
648 } catch (CameraRuntimeException e) {
649 throw e.asChecked();
650 } catch (RemoteException e) {
651 // impossible
652 return;
653 }
Ruben Brunkdecfe952013-10-29 11:00:32 -0700654
655 mRepeatingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700656 }
Igor Murashkin70725502013-06-25 20:27:06 +0000657 }
658
659 @Override
Eino-Ville Talvala8ebd52b2013-08-13 12:09:44 -0700660 public void flush() throws CameraAccessException {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700661 try (ScopedLock scopedLock = mCloseLock.acquireExclusiveLock()) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700662 checkIfCameraClosedOrInError();
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700663
664 mDeviceHandler.post(mCallOnBusy);
Eino-Ville Talvala8ebd52b2013-08-13 12:09:44 -0700665 try {
Jianing Weid2c3a822014-03-27 18:27:43 -0700666 LongParcelable lastFrameNumberRef = new LongParcelable();
667 mRemoteDevice.flush(/*out*/lastFrameNumberRef);
668 if (mRepeatingRequestId != REQUEST_ID_NONE) {
669 long lastFrameNumber = lastFrameNumberRef.getNumber();
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700670 checkEarlyTriggerSequenceComplete(mRepeatingRequestId, lastFrameNumber);
Jianing Weid2c3a822014-03-27 18:27:43 -0700671 mRepeatingRequestId = REQUEST_ID_NONE;
672 }
Eino-Ville Talvala8ebd52b2013-08-13 12:09:44 -0700673 } catch (CameraRuntimeException e) {
674 throw e.asChecked();
675 } catch (RemoteException e) {
676 // impossible
677 return;
678 }
679 }
680 }
681
682 @Override
Igor Murashkin5c9eaf62013-09-10 19:35:24 -0700683 public void close() {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700684 mClosing = true;
685 // Acquire exclusive lock, close, release (idempotent)
686 mCloseLock.close();
Igor Murashkin70725502013-06-25 20:27:06 +0000687
Igor Murashkin49b2b132014-06-18 19:03:00 -0700688 /*
689 * The rest of this is safe, since no other methods will be able to execute
690 * (they will throw ISE instead; the callbacks will get dropped)
691 */
692 {
Igor Murashkin70725502013-06-25 20:27:06 +0000693 try {
Igor Murashkin2a3eced2013-08-28 17:35:10 -0700694 if (mRemoteDevice != null) {
695 mRemoteDevice.disconnect();
696 }
Igor Murashkin70725502013-06-25 20:27:06 +0000697 } catch (CameraRuntimeException e) {
Igor Murashkin5c9eaf62013-09-10 19:35:24 -0700698 Log.e(TAG, "Exception while closing: ", e.asChecked());
Igor Murashkin70725502013-06-25 20:27:06 +0000699 } catch (RemoteException e) {
700 // impossible
701 }
702
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700703 // Only want to fire the onClosed callback once;
704 // either a normal close where the remote device is valid
705 // or a close after a startup error (no remote device but in error state)
706 if (mRemoteDevice != null || mInError) {
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700707 mDeviceHandler.post(mCallOnClosed);
708 }
Igor Murashkin70725502013-06-25 20:27:06 +0000709
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700710 mRemoteDevice = null;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700711 mInError = false;
Igor Murashkin70725502013-06-25 20:27:06 +0000712 }
713 }
714
715 @Override
716 protected void finalize() throws Throwable {
717 try {
718 close();
Igor Murashkin70725502013-06-25 20:27:06 +0000719 }
720 finally {
721 super.finalize();
722 }
723 }
724
725 static class CaptureListenerHolder {
726
727 private final boolean mRepeating;
728 private final CaptureListener mListener;
Jianing Weid2c3a822014-03-27 18:27:43 -0700729 private final List<CaptureRequest> mRequestList;
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700730 private final Handler mHandler;
Igor Murashkin70725502013-06-25 20:27:06 +0000731
Jianing Weid2c3a822014-03-27 18:27:43 -0700732 CaptureListenerHolder(CaptureListener listener, List<CaptureRequest> requestList,
733 Handler handler, boolean repeating) {
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700734 if (listener == null || handler == null) {
735 throw new UnsupportedOperationException(
736 "Must have a valid handler and a valid listener");
737 }
Igor Murashkin70725502013-06-25 20:27:06 +0000738 mRepeating = repeating;
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700739 mHandler = handler;
Jianing Weid2c3a822014-03-27 18:27:43 -0700740 mRequestList = new ArrayList<CaptureRequest>(requestList);
Igor Murashkin70725502013-06-25 20:27:06 +0000741 mListener = listener;
742 }
743
744 public boolean isRepeating() {
745 return mRepeating;
746 }
747
748 public CaptureListener getListener() {
749 return mListener;
750 }
751
Jianing Weid2c3a822014-03-27 18:27:43 -0700752 public CaptureRequest getRequest(int subsequenceId) {
753 if (subsequenceId >= mRequestList.size()) {
754 throw new IllegalArgumentException(
755 String.format(
756 "Requested subsequenceId %d is larger than request list size %d.",
757 subsequenceId, mRequestList.size()));
758 } else {
759 if (subsequenceId < 0) {
760 throw new IllegalArgumentException(String.format(
761 "Requested subsequenceId %d is negative", subsequenceId));
762 } else {
763 return mRequestList.get(subsequenceId);
764 }
765 }
766 }
767
Igor Murashkin70725502013-06-25 20:27:06 +0000768 public CaptureRequest getRequest() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700769 return getRequest(0);
Igor Murashkin70725502013-06-25 20:27:06 +0000770 }
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700771
772 public Handler getHandler() {
773 return mHandler;
774 }
775
Igor Murashkin70725502013-06-25 20:27:06 +0000776 }
777
Jianing Weid2c3a822014-03-27 18:27:43 -0700778 /**
779 * This class tracks the last frame number for submitted requests.
780 */
781 public class FrameNumberTracker {
782
783 private long mCompletedFrameNumber = -1;
784 private final TreeSet<Long> mFutureErrorSet = new TreeSet<Long>();
785
786 private void update() {
787 Iterator<Long> iter = mFutureErrorSet.iterator();
788 while (iter.hasNext()) {
789 long errorFrameNumber = iter.next();
790 if (errorFrameNumber == mCompletedFrameNumber + 1) {
791 mCompletedFrameNumber++;
792 iter.remove();
793 } else {
794 break;
795 }
796 }
797 }
798
799 /**
800 * This function is called every time when a result or an error is received.
801 * @param frameNumber: the frame number corresponding to the result or error
802 * @param isError: true if it is an error, false if it is not an error
803 */
804 public void updateTracker(long frameNumber, boolean isError) {
805 if (isError) {
806 mFutureErrorSet.add(frameNumber);
807 } else {
808 /**
809 * HAL cannot send an OnResultReceived for frame N unless it knows for
810 * sure that all frames prior to N have either errored out or completed.
811 * So if the current frame is not an error, then all previous frames
812 * should have arrived. The following line checks whether this holds.
813 */
814 if (frameNumber != mCompletedFrameNumber + 1) {
Zhijun He22589b42014-05-01 10:37:36 -0700815 Log.e(TAG, String.format(
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700816 "result frame number %d comes out of order, should be %d + 1",
817 frameNumber, mCompletedFrameNumber));
Jianing Weid2c3a822014-03-27 18:27:43 -0700818 }
819 mCompletedFrameNumber++;
820 }
821 update();
822 }
823
824 public long getCompletedFrameNumber() {
825 return mCompletedFrameNumber;
826 }
827
828 }
829
830 private void checkAndFireSequenceComplete() {
831 long completedFrameNumber = mFrameNumberTracker.getCompletedFrameNumber();
832 Iterator<SimpleEntry<Long, Integer> > iter = mFrameNumberRequestPairs.iterator();
833 while (iter.hasNext()) {
834 final SimpleEntry<Long, Integer> frameNumberRequestPair = iter.next();
835 if (frameNumberRequestPair.getKey() <= completedFrameNumber) {
836
837 // remove request from mCaptureListenerMap
838 final int requestId = frameNumberRequestPair.getValue();
839 final CaptureListenerHolder holder;
Igor Murashkin49b2b132014-06-18 19:03:00 -0700840 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
841 if (scopedLock == null) {
842 Log.w(TAG, "Camera closed while checking sequences");
843 return;
844 }
845
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700846 int index = mCaptureListenerMap.indexOfKey(requestId);
847 holder = (index >= 0) ? mCaptureListenerMap.valueAt(index)
Jianing Weid2c3a822014-03-27 18:27:43 -0700848 : null;
849 if (holder != null) {
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700850 mCaptureListenerMap.removeAt(index);
851 if (DEBUG) {
852 Log.v(TAG, String.format(
853 "remove holder for requestId %d, "
854 + "because lastFrame %d is <= %d",
855 requestId, frameNumberRequestPair.getKey(),
856 completedFrameNumber));
857 }
Jianing Weid2c3a822014-03-27 18:27:43 -0700858 }
859 }
860 iter.remove();
861
862 // Call onCaptureSequenceCompleted
863 if (holder != null) {
864 Runnable resultDispatch = new Runnable() {
865 @Override
866 public void run() {
Igor Murashkin21547d62014-06-04 15:21:42 -0700867 if (!CameraDeviceImpl.this.isClosed()){
Jianing Weid2c3a822014-03-27 18:27:43 -0700868 if (DEBUG) {
869 Log.d(TAG, String.format(
870 "fire sequence complete for request %d",
871 requestId));
872 }
873
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700874 long lastFrameNumber = frameNumberRequestPair.getKey();
875 if (lastFrameNumber < Integer.MIN_VALUE
876 || lastFrameNumber > Integer.MAX_VALUE) {
877 throw new AssertionError(lastFrameNumber
878 + " cannot be cast to int");
879 }
Jianing Weid2c3a822014-03-27 18:27:43 -0700880 holder.getListener().onCaptureSequenceCompleted(
Igor Murashkin21547d62014-06-04 15:21:42 -0700881 CameraDeviceImpl.this,
Jianing Weid2c3a822014-03-27 18:27:43 -0700882 requestId,
Igor Murashkindb075af2014-05-21 10:07:08 -0700883 lastFrameNumber);
Jianing Weid2c3a822014-03-27 18:27:43 -0700884 }
885 }
886 };
887 holder.getHandler().post(resultDispatch);
888 }
889
890 }
891 }
892 }
893
Zhijun Heecb323e2013-07-31 09:40:27 -0700894 public class CameraDeviceCallbacks extends ICameraDeviceCallbacks.Stub {
Igor Murashkin70725502013-06-25 20:27:06 +0000895
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700896 //
897 // Constants below need to be kept up-to-date with
898 // frameworks/av/include/camera/camera2/ICameraDeviceCallbacks.h
899 //
900
901 //
902 // Error codes for onCameraError
903 //
904
905 /**
906 * Camera has been disconnected
907 */
908 static final int ERROR_CAMERA_DISCONNECTED = 0;
909
910 /**
911 * Camera has encountered a device-level error
912 * Matches CameraDevice.StateListener#ERROR_CAMERA_DEVICE
913 */
914 static final int ERROR_CAMERA_DEVICE = 1;
915
916 /**
917 * Camera has encountered a service-level error
918 * Matches CameraDevice.StateListener#ERROR_CAMERA_SERVICE
919 */
920 static final int ERROR_CAMERA_SERVICE = 2;
921
Igor Murashkin70725502013-06-25 20:27:06 +0000922 @Override
923 public IBinder asBinder() {
924 return this;
925 }
926
Igor Murashkin70725502013-06-25 20:27:06 +0000927 @Override
Jianing Weid2c3a822014-03-27 18:27:43 -0700928 public void onCameraError(final int errorCode, CaptureResultExtras resultExtras) {
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700929 Runnable r = null;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700930
Igor Murashkin49b2b132014-06-18 19:03:00 -0700931 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
932 if (scopedLock == null) {
933 return; // Camera already closed
934 }
935
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700936 mInError = true;
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700937 switch (errorCode) {
938 case ERROR_CAMERA_DISCONNECTED:
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700939 r = mCallOnDisconnected;
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700940 break;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700941 default:
942 Log.e(TAG, "Unknown error from camera device: " + errorCode);
943 // no break
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700944 case ERROR_CAMERA_DEVICE:
945 case ERROR_CAMERA_SERVICE:
946 r = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700947 @Override
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700948 public void run() {
Igor Murashkin21547d62014-06-04 15:21:42 -0700949 if (!CameraDeviceImpl.this.isClosed()) {
950 mDeviceListener.onError(CameraDeviceImpl.this, errorCode);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700951 }
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700952 }
953 };
954 break;
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700955 }
Igor Murashkin21547d62014-06-04 15:21:42 -0700956 CameraDeviceImpl.this.mDeviceHandler.post(r);
Jianing Weid2c3a822014-03-27 18:27:43 -0700957
Igor Murashkin49b2b132014-06-18 19:03:00 -0700958 // Fire onCaptureSequenceCompleted
959 if (DEBUG) {
960 Log.v(TAG, String.format("got error frame %d", resultExtras.getFrameNumber()));
961 }
962 mFrameNumberTracker.updateTracker(resultExtras.getFrameNumber(), /*error*/true);
963 checkAndFireSequenceComplete();
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700964 }
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700965 }
966
967 @Override
968 public void onCameraIdle() {
969 if (DEBUG) {
970 Log.d(TAG, "Camera now idle");
971 }
Igor Murashkin49b2b132014-06-18 19:03:00 -0700972 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
973 if (scopedLock == null) return; // Camera already closed
974
Igor Murashkin21547d62014-06-04 15:21:42 -0700975 if (!CameraDeviceImpl.this.mIdle) {
976 CameraDeviceImpl.this.mDeviceHandler.post(mCallOnIdle);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700977 }
Igor Murashkin21547d62014-06-04 15:21:42 -0700978 CameraDeviceImpl.this.mIdle = true;
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700979 }
980 }
981
982 @Override
Jianing Weid2c3a822014-03-27 18:27:43 -0700983 public void onCaptureStarted(final CaptureResultExtras resultExtras, final long timestamp) {
984 int requestId = resultExtras.getRequestId();
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700985 if (DEBUG) {
986 Log.d(TAG, "Capture started for id " + requestId);
987 }
988 final CaptureListenerHolder holder;
989
Igor Murashkin49b2b132014-06-18 19:03:00 -0700990 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
991 if (scopedLock == null) return; // Camera already closed
992
993 // Get the listener for this frame ID, if there is one
Igor Murashkin21547d62014-06-04 15:21:42 -0700994 holder = CameraDeviceImpl.this.mCaptureListenerMap.get(requestId);
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700995
Igor Murashkin49b2b132014-06-18 19:03:00 -0700996 if (holder == null) {
997 return;
998 }
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -0700999
Igor Murashkin49b2b132014-06-18 19:03:00 -07001000 if (isClosed()) return;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001001
Igor Murashkin49b2b132014-06-18 19:03:00 -07001002 // Dispatch capture start notice
1003 holder.getHandler().post(
1004 new Runnable() {
1005 @Override
1006 public void run() {
1007 if (!CameraDeviceImpl.this.isClosed()) {
1008 holder.getListener().onCaptureStarted(
1009 CameraDeviceImpl.this,
1010 holder.getRequest(resultExtras.getSubsequenceId()),
1011 timestamp);
1012 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001013 }
Igor Murashkin49b2b132014-06-18 19:03:00 -07001014 });
1015
1016 }
Igor Murashkin70725502013-06-25 20:27:06 +00001017 }
1018
1019 @Override
Jianing Weid2c3a822014-03-27 18:27:43 -07001020 public void onResultReceived(CameraMetadataNative result,
1021 CaptureResultExtras resultExtras) throws RemoteException {
Ruben Brunk57493682014-05-27 18:58:08 -07001022
Jianing Weid2c3a822014-03-27 18:27:43 -07001023 int requestId = resultExtras.getRequestId();
Zhijun Heecb323e2013-07-31 09:40:27 -07001024 if (DEBUG) {
Jianing Weibaf0c652014-04-18 17:35:00 -07001025 Log.v(TAG, "Received result frame " + resultExtras.getFrameNumber() + " for id "
1026 + requestId);
Zhijun Heecb323e2013-07-31 09:40:27 -07001027 }
Ruben Brunk57493682014-05-27 18:58:08 -07001028
Igor Murashkin49b2b132014-06-18 19:03:00 -07001029 try (ScopedLock scopedLock = mCloseLock.acquireLock()) {
1030 if (scopedLock == null) return; // Camera already closed
Ruben Brunk57493682014-05-27 18:58:08 -07001031
Igor Murashkin49b2b132014-06-18 19:03:00 -07001032 // TODO: Handle CameraCharacteristics access from CaptureResult correctly.
1033 result.set(CameraCharacteristics.LENS_INFO_SHADING_MAP_SIZE,
1034 getCharacteristics().get(CameraCharacteristics.LENS_INFO_SHADING_MAP_SIZE));
Ruben Brunk57493682014-05-27 18:58:08 -07001035
Igor Murashkin49b2b132014-06-18 19:03:00 -07001036 final CaptureListenerHolder holder =
1037 CameraDeviceImpl.this.mCaptureListenerMap.get(requestId);
Igor Murashkin70725502013-06-25 20:27:06 +00001038
Igor Murashkin49b2b132014-06-18 19:03:00 -07001039 Boolean quirkPartial = result.get(CaptureResult.QUIRKS_PARTIAL_RESULT);
1040 boolean quirkIsPartialResult = (quirkPartial != null && quirkPartial);
Eino-Ville Talvala7a313102013-11-07 14:45:06 -08001041
Igor Murashkin49b2b132014-06-18 19:03:00 -07001042 // Update tracker (increment counter) when it's not a partial result.
1043 if (!quirkIsPartialResult) {
1044 mFrameNumberTracker.updateTracker(resultExtras.getFrameNumber(),
1045 /*error*/false);
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001046 }
Igor Murashkin70725502013-06-25 20:27:06 +00001047
Igor Murashkin49b2b132014-06-18 19:03:00 -07001048 // Check if we have a listener for this
1049 if (holder == null) {
1050 if (DEBUG) {
1051 Log.d(TAG,
1052 "holder is null, early return at frame "
1053 + resultExtras.getFrameNumber());
1054 }
1055 return;
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001056 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001057
Igor Murashkin49b2b132014-06-18 19:03:00 -07001058 if (isClosed()) {
1059 if (DEBUG) {
1060 Log.d(TAG,
1061 "camera is closed, early return at frame "
1062 + resultExtras.getFrameNumber());
Eino-Ville Talvala7a313102013-11-07 14:45:06 -08001063 }
Igor Murashkin49b2b132014-06-18 19:03:00 -07001064 return;
1065 }
Igor Murashkindb075af2014-05-21 10:07:08 -07001066
Igor Murashkin49b2b132014-06-18 19:03:00 -07001067 final CaptureRequest request = holder.getRequest(resultExtras.getSubsequenceId());
1068
1069
1070 Runnable resultDispatch = null;
1071
1072 // Either send a partial result or the final capture completed result
1073 if (quirkIsPartialResult) {
1074 final CaptureResult resultAsCapture =
1075 new CaptureResult(result, request, requestId);
1076
1077 // Partial result
1078 resultDispatch = new Runnable() {
1079 @Override
1080 public void run() {
1081 if (!CameraDeviceImpl.this.isClosed()){
1082 holder.getListener().onCapturePartial(
1083 CameraDeviceImpl.this,
1084 request,
1085 resultAsCapture);
1086 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001087 }
Igor Murashkin49b2b132014-06-18 19:03:00 -07001088 };
1089 } else {
1090 final TotalCaptureResult resultAsCapture =
1091 new TotalCaptureResult(result, request, requestId);
Eino-Ville Talvala7a313102013-11-07 14:45:06 -08001092
Igor Murashkin49b2b132014-06-18 19:03:00 -07001093 // Final capture result
1094 resultDispatch = new Runnable() {
1095 @Override
1096 public void run() {
1097 if (!CameraDeviceImpl.this.isClosed()){
1098 holder.getListener().onCaptureCompleted(
1099 CameraDeviceImpl.this,
1100 request,
1101 resultAsCapture);
1102 }
1103 }
1104 };
1105 }
Jianing Weid2c3a822014-03-27 18:27:43 -07001106
Igor Murashkin49b2b132014-06-18 19:03:00 -07001107 holder.getHandler().post(resultDispatch);
1108
1109 // Fire onCaptureSequenceCompleted
1110 if (!quirkIsPartialResult) {
1111 checkAndFireSequenceComplete();
1112 }
1113
Jianing Weid2c3a822014-03-27 18:27:43 -07001114 }
Igor Murashkin70725502013-06-25 20:27:06 +00001115 }
1116
1117 }
1118
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001119 /**
Igor Murashkin0b27d342014-05-30 09:45:05 -07001120 * Default handler management.
1121 *
1122 * <p>
1123 * If handler is null, get the current thread's
1124 * Looper to create a Handler with. If no looper exists, throw {@code IllegalArgumentException}.
1125 * </p>
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001126 */
Igor Murashkin0b27d342014-05-30 09:45:05 -07001127 static Handler checkHandler(Handler handler) {
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001128 if (handler == null) {
1129 Looper looper = Looper.myLooper();
1130 if (looper == null) {
1131 throw new IllegalArgumentException(
1132 "No handler given, and current thread has no looper!");
1133 }
1134 handler = new Handler(looper);
1135 }
1136 return handler;
1137 }
1138
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -07001139 private void checkIfCameraClosedOrInError() throws CameraAccessException {
1140 if (mInError) {
1141 throw new CameraAccessException(CameraAccessException.CAMERA_ERROR,
1142 "The camera device has encountered a serious error");
1143 }
Zhijun He7f4d3142013-07-23 07:54:38 -07001144 if (mRemoteDevice == null) {
1145 throw new IllegalStateException("CameraDevice was already closed");
1146 }
1147 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001148
Igor Murashkin49b2b132014-06-18 19:03:00 -07001149 /** Whether the camera device has started to close (may not yet have finished) */
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001150 private boolean isClosed() {
Igor Murashkin49b2b132014-06-18 19:03:00 -07001151 return mClosing;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001152 }
Ruben Brunk57493682014-05-27 18:58:08 -07001153
1154 private CameraCharacteristics getCharacteristics() {
1155 return mCharacteristics;
1156 }
Igor Murashkin70725502013-06-25 20:27:06 +00001157}