blob: cc8c182b867e20dfe183835cdcd5de6c4c5ea71d [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
Svet Ganov82f09bc2018-01-12 22:08:40 -080019import static com.android.internal.util.function.pooled.PooledLambda.obtainRunnable;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070020
Emilian Peevb9a51942018-03-14 17:18:49 +000021import android.annotation.NonNull;
Shuzhen Wang23d29382017-11-26 17:24:56 -080022import android.hardware.ICameraService;
Eino-Ville Talvala2f1a2e42013-07-25 17:12:05 -070023import android.hardware.camera2.CameraAccessException;
Eino-Ville Talvalacca00c62014-05-14 10:53:20 -070024import android.hardware.camera2.CameraCaptureSession;
Ruben Brunk57493682014-05-27 18:58:08 -070025import android.hardware.camera2.CameraCharacteristics;
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -070026import android.hardware.camera2.CameraDevice;
Shuzhen Wang23d29382017-11-26 17:24:56 -080027import android.hardware.camera2.CaptureFailure;
Eino-Ville Talvala2f1a2e42013-07-25 17:12:05 -070028import android.hardware.camera2.CaptureRequest;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070029import android.hardware.camera2.CaptureResult;
30import android.hardware.camera2.ICameraDeviceCallbacks;
31import android.hardware.camera2.ICameraDeviceUser;
Igor Murashkindb075af2014-05-21 10:07:08 -070032import android.hardware.camera2.TotalCaptureResult;
Chien-Yu Chen5398a672015-03-19 14:48:43 -070033import android.hardware.camera2.params.InputConfiguration;
Yin-Chia Yeh49ea6ae2015-03-09 16:53:14 -070034import android.hardware.camera2.params.OutputConfiguration;
Emilian Peev75a55702017-11-07 16:09:59 +000035import android.hardware.camera2.params.SessionConfiguration;
Chien-Yu Chen5398a672015-03-19 14:48:43 -070036import android.hardware.camera2.params.StreamConfigurationMap;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -080037import android.hardware.camera2.utils.SubmitInfo;
Zhijun Hea7677722015-06-01 16:36:06 -070038import android.hardware.camera2.utils.SurfaceUtils;
Emilian Peev77ca8662018-03-14 17:12:13 +000039import android.os.Binder;
Chien-Yu Chen3d2b5fe2017-04-05 16:42:31 -070040import android.os.Build;
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -070041import android.os.Handler;
Ruben Brunkdecfe952013-10-29 11:00:32 -070042import android.os.IBinder;
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -070043import android.os.Looper;
Ruben Brunkdecfe952013-10-29 11:00:32 -070044import android.os.RemoteException;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -080045import android.os.ServiceSpecificException;
Igor Murashkin70725502013-06-25 20:27:06 +000046import android.util.Log;
Zhijun Hea7677722015-06-01 16:36:06 -070047import android.util.Range;
Chien-Yu Chen5398a672015-03-19 14:48:43 -070048import android.util.Size;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070049import android.util.SparseArray;
Igor Murashkin70725502013-06-25 20:27:06 +000050import android.view.Surface;
51
Emilian Peevb9a51942018-03-14 17:18:49 +000052import com.android.internal.util.Preconditions;
53
Jianing Weid2c3a822014-03-27 18:27:43 -070054import java.util.AbstractMap.SimpleEntry;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070055import java.util.ArrayList;
Zhijun Hea7677722015-06-01 16:36:06 -070056import java.util.Collection;
Igor Murashkin1e854c52014-08-28 15:21:49 -070057import java.util.HashMap;
Igor Murashkin57ea59b2013-08-23 16:55:57 -070058import java.util.HashSet;
Ruben Brunkdecfe952013-10-29 11:00:32 -070059import java.util.Iterator;
Chien-Yu Chen5398a672015-03-19 14:48:43 -070060import java.util.LinkedList;
Shuzhen Wang23d29382017-11-26 17:24:56 -080061import java.util.List;
Emilian Peev2100ae72018-01-12 16:56:25 +000062import java.util.Set;
Chien-Yu Chen5398a672015-03-19 14:48:43 -070063import java.util.TreeMap;
Shuzhen Wang23d29382017-11-26 17:24:56 -080064import java.util.concurrent.atomic.AtomicBoolean;
Emilian Peev77ca8662018-03-14 17:12:13 +000065import java.util.concurrent.Executor;
Igor Murashkin70725502013-06-25 20:27:06 +000066
Emilian Peev9a0ec8c2018-02-28 14:53:30 +000067
Igor Murashkin70725502013-06-25 20:27:06 +000068/**
Eino-Ville Talvala70c22072013-08-27 12:09:04 -070069 * HAL2.1+ implementation of CameraDevice. Use CameraManager#open to instantiate
Igor Murashkin70725502013-06-25 20:27:06 +000070 */
Shuzhen Wang78392932016-03-04 15:26:01 -080071public class CameraDeviceImpl extends CameraDevice
72 implements IBinder.DeathRecipient {
Igor Murashkin70725502013-06-25 20:27:06 +000073 private final String TAG;
Eino-Ville Talvalaa78791f2015-06-01 12:39:54 -070074 private final boolean DEBUG = false;
Igor Murashkin70725502013-06-25 20:27:06 +000075
Ruben Brunkdecfe952013-10-29 11:00:32 -070076 private static final int REQUEST_ID_NONE = -1;
77
Igor Murashkin70725502013-06-25 20:27:06 +000078 // TODO: guard every function with if (!mRemoteDevice) check (if it was closed)
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -080079 private ICameraDeviceUserWrapper mRemoteDevice;
Igor Murashkin70725502013-06-25 20:27:06 +000080
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -070081 // Lock to synchronize cross-thread access to device public interface
Igor Murashkin51dcfd652014-09-25 16:55:01 -070082 final Object mInterfaceLock = new Object(); // access from this class and Session only!
Igor Murashkin70725502013-06-25 20:27:06 +000083 private final CameraDeviceCallbacks mCallbacks = new CameraDeviceCallbacks();
84
Eino-Ville Talvalafd887432014-09-04 13:07:40 -070085 private final StateCallback mDeviceCallback;
86 private volatile StateCallbackKK mSessionStateCallback;
Emilian Peev9a0ec8c2018-02-28 14:53:30 +000087 private final Executor mDeviceExecutor;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -070088
Ruben Brunk0953b642015-06-11 16:12:35 -070089 private final AtomicBoolean mClosing = new AtomicBoolean();
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -070090 private boolean mInError = false;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -070091 private boolean mIdle = true;
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -070092
Eino-Ville Talvalafd887432014-09-04 13:07:40 -070093 /** map request IDs to callback/request data */
94 private final SparseArray<CaptureCallbackHolder> mCaptureCallbackMap =
95 new SparseArray<CaptureCallbackHolder>();
Igor Murashkin70725502013-06-25 20:27:06 +000096
Ruben Brunkdecfe952013-10-29 11:00:32 -070097 private int mRepeatingRequestId = REQUEST_ID_NONE;
Shuzhen Wangb45449b2019-04-17 13:32:03 -070098 // Latest repeating request list's types
99 private int[] mRepeatingRequestTypes;
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700100 // Map stream IDs to input/output configurations
101 private SimpleEntry<Integer, InputConfiguration> mConfiguredInput =
102 new SimpleEntry<>(REQUEST_ID_NONE, null);
Yin-Chia Yehbfbbee72015-03-12 13:39:26 -0700103 private final SparseArray<OutputConfiguration> mConfiguredOutputs =
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700104 new SparseArray<>();
Igor Murashkin70725502013-06-25 20:27:06 +0000105
106 private final String mCameraId;
Ruben Brunk57493682014-05-27 18:58:08 -0700107 private final CameraCharacteristics mCharacteristics;
Zhijun He83159152014-07-16 11:32:59 -0700108 private final int mTotalPartialCount;
Igor Murashkin70725502013-06-25 20:27:06 +0000109
Shuzhen Wangae10eb92017-03-01 12:58:04 -0800110 private static final long NANO_PER_SECOND = 1000000000; //ns
111
Jianing Weid2c3a822014-03-27 18:27:43 -0700112 /**
Shuzhen Wangb45449b2019-04-17 13:32:03 -0700113 * A list tracking request and its expected last regular/reprocess/zslStill frame
Chien-Yu Chen4560df72015-04-27 17:21:31 -0700114 * number. Updated when calling ICameraDeviceUser methods.
Jianing Weid2c3a822014-03-27 18:27:43 -0700115 */
Chien-Yu Chen4560df72015-04-27 17:21:31 -0700116 private final List<RequestLastFrameNumbersHolder> mRequestLastFrameNumbersList =
117 new ArrayList<>();
Jianing Weid2c3a822014-03-27 18:27:43 -0700118
119 /**
120 * An object tracking received frame numbers.
121 * Updated when receiving callbacks from ICameraDeviceCallbacks.
122 */
123 private final FrameNumberTracker mFrameNumberTracker = new FrameNumberTracker();
124
Eino-Ville Talvala639fffe2015-06-30 10:34:48 -0700125 private CameraCaptureSessionCore mCurrentSession;
Eino-Ville Talvala95037852014-09-14 14:07:05 -0700126 private int mNextSessionId = 0;
Igor Murashkin0b27d342014-05-30 09:45:05 -0700127
Chien-Yu Chen3d2b5fe2017-04-05 16:42:31 -0700128 private final int mAppTargetSdkVersion;
129
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700130 // Runnables for all state transitions, except error, which needs the
131 // error code argument
132
133 private final Runnable mCallOnOpened = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700134 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700135 public void run() {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700136 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700137 synchronized(mInterfaceLock) {
138 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -0700139
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700140 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700141 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700142 if (sessionCallback != null) {
143 sessionCallback.onOpened(CameraDeviceImpl.this);
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700144 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700145 mDeviceCallback.onOpened(CameraDeviceImpl.this);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700146 }
147 };
148
149 private final Runnable mCallOnUnconfigured = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700150 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700151 public void run() {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700152 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700153 synchronized(mInterfaceLock) {
154 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -0700155
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700156 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700157 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700158 if (sessionCallback != null) {
159 sessionCallback.onUnconfigured(CameraDeviceImpl.this);
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700160 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700161 }
162 };
163
164 private final Runnable mCallOnActive = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700165 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700166 public void run() {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700167 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700168 synchronized(mInterfaceLock) {
169 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -0700170
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700171 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700172 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700173 if (sessionCallback != null) {
174 sessionCallback.onActive(CameraDeviceImpl.this);
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700175 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700176 }
177 };
178
179 private final Runnable mCallOnBusy = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700180 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700181 public void run() {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700182 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700183 synchronized(mInterfaceLock) {
184 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -0700185
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700186 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700187 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700188 if (sessionCallback != null) {
189 sessionCallback.onBusy(CameraDeviceImpl.this);
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700190 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700191 }
192 };
193
194 private final Runnable mCallOnClosed = new Runnable() {
Igor Murashkin49b2b132014-06-18 19:03:00 -0700195 private boolean mClosedOnce = false;
196
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 if (mClosedOnce) {
200 throw new AssertionError("Don't post #onClosed more than once");
201 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700202 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700203 synchronized(mInterfaceLock) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700204 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700205 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700206 if (sessionCallback != null) {
207 sessionCallback.onClosed(CameraDeviceImpl.this);
Igor Murashkin0b27d342014-05-30 09:45:05 -0700208 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700209 mDeviceCallback.onClosed(CameraDeviceImpl.this);
Igor Murashkin49b2b132014-06-18 19:03:00 -0700210 mClosedOnce = true;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700211 }
212 };
213
214 private final Runnable mCallOnIdle = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700215 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700216 public void run() {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700217 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700218 synchronized(mInterfaceLock) {
219 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -0700220
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700221 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700222 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700223 if (sessionCallback != null) {
224 sessionCallback.onIdle(CameraDeviceImpl.this);
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700225 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700226 }
227 };
228
229 private final Runnable mCallOnDisconnected = new Runnable() {
Jianing Weid2c3a822014-03-27 18:27:43 -0700230 @Override
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700231 public void run() {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700232 StateCallbackKK sessionCallback = null;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700233 synchronized(mInterfaceLock) {
234 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -0700235
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700236 sessionCallback = mSessionStateCallback;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700237 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700238 if (sessionCallback != null) {
239 sessionCallback.onDisconnected(CameraDeviceImpl.this);
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700240 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700241 mDeviceCallback.onDisconnected(CameraDeviceImpl.this);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700242 }
243 };
244
Emilian Peev9129aa22018-03-20 15:39:34 +0000245 public CameraDeviceImpl(String cameraId, StateCallback callback, Executor executor,
Chien-Yu Chen3d2b5fe2017-04-05 16:42:31 -0700246 CameraCharacteristics characteristics, int appTargetSdkVersion) {
Emilian Peev9129aa22018-03-20 15:39:34 +0000247 if (cameraId == null || callback == null || executor == null || characteristics == null) {
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700248 throw new IllegalArgumentException("Null argument given");
249 }
Igor Murashkin70725502013-06-25 20:27:06 +0000250 mCameraId = cameraId;
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700251 mDeviceCallback = callback;
Emilian Peev9129aa22018-03-20 15:39:34 +0000252 mDeviceExecutor = executor;
Ruben Brunk57493682014-05-27 18:58:08 -0700253 mCharacteristics = characteristics;
Chien-Yu Chen3d2b5fe2017-04-05 16:42:31 -0700254 mAppTargetSdkVersion = appTargetSdkVersion;
Eino-Ville Talvalab72d1ef2014-04-28 13:21:18 -0700255
256 final int MAX_TAG_LEN = 23;
257 String tag = String.format("CameraDevice-JV-%s", mCameraId);
258 if (tag.length() > MAX_TAG_LEN) {
259 tag = tag.substring(0, MAX_TAG_LEN);
260 }
261 TAG = tag;
Zhijun He83159152014-07-16 11:32:59 -0700262
263 Integer partialCount =
264 mCharacteristics.get(CameraCharacteristics.REQUEST_PARTIAL_RESULT_COUNT);
265 if (partialCount == null) {
266 // 1 means partial result is not supported.
267 mTotalPartialCount = 1;
268 } else {
269 mTotalPartialCount = partialCount;
270 }
Igor Murashkin70725502013-06-25 20:27:06 +0000271 }
272
273 public CameraDeviceCallbacks getCallbacks() {
274 return mCallbacks;
275 }
276
Shuzhen Wang78392932016-03-04 15:26:01 -0800277 /**
278 * Set remote device, which triggers initial onOpened/onUnconfigured callbacks
279 *
280 * <p>This function may post onDisconnected and throw CAMERA_DISCONNECTED if remoteDevice dies
281 * during setup.</p>
282 *
283 */
284 public void setRemoteDevice(ICameraDeviceUser remoteDevice) throws CameraAccessException {
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700285 synchronized(mInterfaceLock) {
286 // TODO: Move from decorator to direct binder-mediated exceptions
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700287 // If setRemoteFailure already called, do nothing
288 if (mInError) return;
289
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800290 mRemoteDevice = new ICameraDeviceUserWrapper(remoteDevice);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700291
Shuzhen Wang78392932016-03-04 15:26:01 -0800292 IBinder remoteDeviceBinder = remoteDevice.asBinder();
293 // For legacy camera device, remoteDevice is in the same process, and
294 // asBinder returns NULL.
295 if (remoteDeviceBinder != null) {
296 try {
297 remoteDeviceBinder.linkToDeath(this, /*flag*/ 0);
298 } catch (RemoteException e) {
Emilian Peev9a0ec8c2018-02-28 14:53:30 +0000299 CameraDeviceImpl.this.mDeviceExecutor.execute(mCallOnDisconnected);
Shuzhen Wang78392932016-03-04 15:26:01 -0800300
301 throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED,
302 "The camera device has encountered a serious error");
303 }
304 }
305
Emilian Peev9a0ec8c2018-02-28 14:53:30 +0000306 mDeviceExecutor.execute(mCallOnOpened);
307 mDeviceExecutor.execute(mCallOnUnconfigured);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700308 }
Igor Murashkin70725502013-06-25 20:27:06 +0000309 }
310
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700311 /**
312 * Call to indicate failed connection to a remote camera device.
313 *
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700314 * <p>This places the camera device in the error state and informs the callback.
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700315 * Use in place of setRemoteDevice() when startup fails.</p>
316 */
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800317 public void setRemoteFailure(final ServiceSpecificException failure) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700318 int failureCode = StateCallback.ERROR_CAMERA_DEVICE;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700319 boolean failureIsError = true;
320
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800321 switch (failure.errorCode) {
322 case ICameraService.ERROR_CAMERA_IN_USE:
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700323 failureCode = StateCallback.ERROR_CAMERA_IN_USE;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700324 break;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800325 case ICameraService.ERROR_MAX_CAMERAS_IN_USE:
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700326 failureCode = StateCallback.ERROR_MAX_CAMERAS_IN_USE;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700327 break;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800328 case ICameraService.ERROR_DISABLED:
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700329 failureCode = StateCallback.ERROR_CAMERA_DISABLED;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700330 break;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800331 case ICameraService.ERROR_DISCONNECTED:
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700332 failureIsError = false;
333 break;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800334 case ICameraService.ERROR_INVALID_OPERATION:
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700335 failureCode = StateCallback.ERROR_CAMERA_DEVICE;
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700336 break;
337 default:
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800338 Log.e(TAG, "Unexpected failure in opening camera device: " + failure.errorCode +
339 failure.getMessage());
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700340 break;
341 }
342 final int code = failureCode;
343 final boolean isError = failureIsError;
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700344 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700345 mInError = true;
Emilian Peev9a0ec8c2018-02-28 14:53:30 +0000346 mDeviceExecutor.execute(new Runnable() {
Igor Murashkine1442202014-06-09 17:51:24 -0700347 @Override
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700348 public void run() {
349 if (isError) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700350 mDeviceCallback.onError(CameraDeviceImpl.this, code);
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700351 } else {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700352 mDeviceCallback.onDisconnected(CameraDeviceImpl.this);
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700353 }
354 }
355 });
356 }
357 }
358
Igor Murashkin70725502013-06-25 20:27:06 +0000359 @Override
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -0700360 public String getId() {
361 return mCameraId;
362 }
363
Igor Murashkin70725502013-06-25 20:27:06 +0000364 public void configureOutputs(List<Surface> outputs) throws CameraAccessException {
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700365 // Leave this here for backwards compatibility with older code using this directly
Yin-Chia Yehbfbbee72015-03-12 13:39:26 -0700366 ArrayList<OutputConfiguration> outputConfigs = new ArrayList<>(outputs.size());
367 for (Surface s : outputs) {
368 outputConfigs.add(new OutputConfiguration(s));
369 }
Zhijun Hea7677722015-06-01 16:36:06 -0700370 configureStreamsChecked(/*inputConfig*/null, outputConfigs,
Emilian Peev75a55702017-11-07 16:09:59 +0000371 /*operatingMode*/ICameraDeviceUser.NORMAL_MODE, /*sessionParams*/ null);
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700372
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700373 }
374
375 /**
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700376 * Attempt to configure the input and outputs; the device goes to idle and then configures the
377 * new input and outputs if possible.
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700378 *
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700379 * <p>The configuration may gracefully fail, if input configuration is not supported,
380 * if there are too many outputs, if the formats are not supported, or if the sizes for that
381 * format is not supported. In this case this function will return {@code false} and the
382 * unconfigured callback will be fired.</p>
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700383 *
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700384 * <p>If the configuration succeeds (with 1 or more outputs with or without an input),
385 * then the idle callback is fired. Unconfiguring the device always fires the idle callback.</p>
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700386 *
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700387 * @param inputConfig input configuration or {@code null} for no input
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700388 * @param outputs a list of one or more surfaces, or {@code null} to unconfigure
Eino-Ville Talvala0e04e912017-02-28 17:49:41 -0800389 * @param operatingMode If the stream configuration is for a normal session,
390 * a constrained high speed session, or something else.
Emilian Peev75a55702017-11-07 16:09:59 +0000391 * @param sessionParams Session parameters.
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700392 * @return whether or not the configuration was successful
393 *
394 * @throws CameraAccessException if there were any unexpected problems during configuration
395 */
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700396 public boolean configureStreamsChecked(InputConfiguration inputConfig,
Emilian Peev75a55702017-11-07 16:09:59 +0000397 List<OutputConfiguration> outputs, int operatingMode, CaptureRequest sessionParams)
Zhijun Hea7677722015-06-01 16:36:06 -0700398 throws CameraAccessException {
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700399 // Treat a null input the same an empty list
400 if (outputs == null) {
Yin-Chia Yehbfbbee72015-03-12 13:39:26 -0700401 outputs = new ArrayList<OutputConfiguration>();
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700402 }
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700403 if (outputs.size() == 0 && inputConfig != null) {
404 throw new IllegalArgumentException("cannot configure an input stream without " +
405 "any output streams");
406 }
407
408 checkInputConfiguration(inputConfig);
409
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700410 boolean success = false;
411
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700412 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700413 checkIfCameraClosedOrInError();
Yin-Chia Yehbfbbee72015-03-12 13:39:26 -0700414 // Streams to create
415 HashSet<OutputConfiguration> addSet = new HashSet<OutputConfiguration>(outputs);
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700416 // Streams to delete
Yin-Chia Yehbfbbee72015-03-12 13:39:26 -0700417 List<Integer> deleteList = new ArrayList<Integer>();
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700418
419 // Determine which streams need to be created, which to be deleted
420 for (int i = 0; i < mConfiguredOutputs.size(); ++i) {
421 int streamId = mConfiguredOutputs.keyAt(i);
Yin-Chia Yehbfbbee72015-03-12 13:39:26 -0700422 OutputConfiguration outConfig = mConfiguredOutputs.valueAt(i);
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700423
Zhijun Hec8b181e2016-05-30 14:54:39 -0700424 if (!outputs.contains(outConfig) || outConfig.isDeferredConfiguration()) {
425 // Always delete the deferred output configuration when the session
426 // is created, as the deferred output configuration doesn't have unique surface
427 // related identifies.
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700428 deleteList.add(streamId);
429 } else {
Yin-Chia Yehbfbbee72015-03-12 13:39:26 -0700430 addSet.remove(outConfig); // Don't create a stream previously created
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700431 }
432 }
433
Emilian Peev9a0ec8c2018-02-28 14:53:30 +0000434 mDeviceExecutor.execute(mCallOnBusy);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700435 stopRepeating();
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700436
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700437 try {
Ruben Brunkdecfe952013-10-29 11:00:32 -0700438 waitUntilIdle();
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700439
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700440 mRemoteDevice.beginConfigure();
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700441
442 // reconfigure the input stream if the input configuration is different.
443 InputConfiguration currentInputConfig = mConfiguredInput.getValue();
444 if (inputConfig != currentInputConfig &&
445 (inputConfig == null || !inputConfig.equals(currentInputConfig))) {
446 if (currentInputConfig != null) {
447 mRemoteDevice.deleteStream(mConfiguredInput.getKey());
448 mConfiguredInput = new SimpleEntry<Integer, InputConfiguration>(
449 REQUEST_ID_NONE, null);
450 }
451 if (inputConfig != null) {
452 int streamId = mRemoteDevice.createInputStream(inputConfig.getWidth(),
453 inputConfig.getHeight(), inputConfig.getFormat());
454 mConfiguredInput = new SimpleEntry<Integer, InputConfiguration>(
455 streamId, inputConfig);
456 }
457 }
458
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700459 // Delete all streams first (to free up HW resources)
460 for (Integer streamId : deleteList) {
461 mRemoteDevice.deleteStream(streamId);
462 mConfiguredOutputs.delete(streamId);
463 }
464
465 // Add all new streams
Yin-Chia Yehbfbbee72015-03-12 13:39:26 -0700466 for (OutputConfiguration outConfig : outputs) {
467 if (addSet.contains(outConfig)) {
468 int streamId = mRemoteDevice.createStream(outConfig);
469 mConfiguredOutputs.put(streamId, outConfig);
470 }
Igor Murashkin70725502013-06-25 20:27:06 +0000471 }
Igor Murashkin57ea59b2013-08-23 16:55:57 -0700472
Emilian Peev75a55702017-11-07 16:09:59 +0000473 if (sessionParams != null) {
474 mRemoteDevice.endConfigure(operatingMode, sessionParams.getNativeCopy());
475 } else {
476 mRemoteDevice.endConfigure(operatingMode, null);
477 }
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700478
479 success = true;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800480 } catch (IllegalArgumentException e) {
481 // OK. camera service can reject stream config if it's not supported by HAL
482 // This is only the result of a programmer misusing the camera2 api.
483 Log.w(TAG, "Stream configuration failed due to: " + e.getMessage());
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700484 return false;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800485 } catch (CameraAccessException e) {
486 if (e.getReason() == CameraAccessException.CAMERA_IN_USE) {
487 throw new IllegalStateException("The camera is currently busy." +
488 " You must wait until the previous operation completes.", e);
489 }
490 throw e;
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700491 } finally {
492 if (success && outputs.size() > 0) {
Emilian Peev9a0ec8c2018-02-28 14:53:30 +0000493 mDeviceExecutor.execute(mCallOnIdle);
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700494 } else {
495 // Always return to the 'unconfigured' state if we didn't hit a fatal error
Emilian Peev9a0ec8c2018-02-28 14:53:30 +0000496 mDeviceExecutor.execute(mCallOnUnconfigured);
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700497 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -0700498 }
Igor Murashkin70725502013-06-25 20:27:06 +0000499 }
Igor Murashkin10fbbbb2014-08-19 16:19:30 -0700500
501 return success;
Igor Murashkin70725502013-06-25 20:27:06 +0000502 }
503
504 @Override
Eino-Ville Talvalacca00c62014-05-14 10:53:20 -0700505 public void createCaptureSession(List<Surface> outputs,
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700506 CameraCaptureSession.StateCallback callback, Handler handler)
Eino-Ville Talvalacca00c62014-05-14 10:53:20 -0700507 throws CameraAccessException {
Yin-Chia Yeh49ea6ae2015-03-09 16:53:14 -0700508 List<OutputConfiguration> outConfigurations = new ArrayList<>(outputs.size());
509 for (Surface surface : outputs) {
510 outConfigurations.add(new OutputConfiguration(surface));
511 }
Emilian Peev77ca8662018-03-14 17:12:13 +0000512 createCaptureSessionInternal(null, outConfigurations, callback,
513 checkAndWrapHandler(handler), /*operatingMode*/ICameraDeviceUser.NORMAL_MODE,
514 /*sessionParams*/ null);
Yin-Chia Yeh49ea6ae2015-03-09 16:53:14 -0700515 }
516
517 @Override
Zhijun He00347432016-04-07 17:34:10 -0700518 public void createCaptureSessionByOutputConfigurations(
Yin-Chia Yeh49ea6ae2015-03-09 16:53:14 -0700519 List<OutputConfiguration> outputConfigurations,
520 CameraCaptureSession.StateCallback callback, Handler handler)
521 throws CameraAccessException {
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700522 if (DEBUG) {
Shuzhen Wang9c663d42016-10-28 23:25:02 -0700523 Log.d(TAG, "createCaptureSessionByOutputConfigurations");
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700524 }
525
Eino-Ville Talvalafa5e2a12016-04-11 12:49:17 -0700526 // OutputConfiguration objects are immutable, but need to have our own array
527 List<OutputConfiguration> currentOutputs = new ArrayList<>(outputConfigurations);
Zhijun He445b3162016-01-18 15:34:28 -0800528
Emilian Peev77ca8662018-03-14 17:12:13 +0000529 createCaptureSessionInternal(null, currentOutputs, callback, checkAndWrapHandler(handler),
Emilian Peev75a55702017-11-07 16:09:59 +0000530 /*operatingMode*/ICameraDeviceUser.NORMAL_MODE, /*sessionParams*/null);
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700531 }
532
533 @Override
Chien-Yu Chen8062d312015-05-12 14:24:10 -0700534 public void createReprocessableCaptureSession(InputConfiguration inputConfig,
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700535 List<Surface> outputs, CameraCaptureSession.StateCallback callback, Handler handler)
536 throws CameraAccessException {
537 if (DEBUG) {
Chien-Yu Chen8062d312015-05-12 14:24:10 -0700538 Log.d(TAG, "createReprocessableCaptureSession");
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700539 }
540
541 if (inputConfig == null) {
542 throw new IllegalArgumentException("inputConfig cannot be null when creating a " +
Chien-Yu Chen8062d312015-05-12 14:24:10 -0700543 "reprocessable capture session");
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700544 }
545 List<OutputConfiguration> outConfigurations = new ArrayList<>(outputs.size());
546 for (Surface surface : outputs) {
547 outConfigurations.add(new OutputConfiguration(surface));
548 }
Emilian Peev77ca8662018-03-14 17:12:13 +0000549 createCaptureSessionInternal(inputConfig, outConfigurations, callback,
550 checkAndWrapHandler(handler), /*operatingMode*/ICameraDeviceUser.NORMAL_MODE,
551 /*sessionParams*/ null);
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700552 }
553
Eino-Ville Talvala639fffe2015-06-30 10:34:48 -0700554 @Override
Zhijun He00347432016-04-07 17:34:10 -0700555 public void createReprocessableCaptureSessionByConfigurations(InputConfiguration inputConfig,
Zhijun He445b3162016-01-18 15:34:28 -0800556 List<OutputConfiguration> outputs,
557 android.hardware.camera2.CameraCaptureSession.StateCallback callback, Handler handler)
558 throws CameraAccessException {
559 if (DEBUG) {
560 Log.d(TAG, "createReprocessableCaptureSessionWithConfigurations");
561 }
562
563 if (inputConfig == null) {
564 throw new IllegalArgumentException("inputConfig cannot be null when creating a " +
565 "reprocessable capture session");
566 }
567
568 if (outputs == null) {
569 throw new IllegalArgumentException("Output configurations cannot be null when " +
570 "creating a reprocessable capture session");
571 }
572
573 // OutputConfiguration objects aren't immutable, make a copy before using.
574 List<OutputConfiguration> currentOutputs = new ArrayList<OutputConfiguration>();
575 for (OutputConfiguration output : outputs) {
576 currentOutputs.add(new OutputConfiguration(output));
577 }
578 createCaptureSessionInternal(inputConfig, currentOutputs,
Emilian Peev77ca8662018-03-14 17:12:13 +0000579 callback, checkAndWrapHandler(handler),
580 /*operatingMode*/ICameraDeviceUser.NORMAL_MODE, /*sessionParams*/ null);
Zhijun He445b3162016-01-18 15:34:28 -0800581 }
582
583 @Override
Eino-Ville Talvala639fffe2015-06-30 10:34:48 -0700584 public void createConstrainedHighSpeedCaptureSession(List<Surface> outputs,
585 android.hardware.camera2.CameraCaptureSession.StateCallback callback, Handler handler)
586 throws CameraAccessException {
587 if (outputs == null || outputs.size() == 0 || outputs.size() > 2) {
588 throw new IllegalArgumentException(
589 "Output surface list must not be null and the size must be no more than 2");
590 }
Eino-Ville Talvala639fffe2015-06-30 10:34:48 -0700591 List<OutputConfiguration> outConfigurations = new ArrayList<>(outputs.size());
592 for (Surface surface : outputs) {
593 outConfigurations.add(new OutputConfiguration(surface));
594 }
Emilian Peev77ca8662018-03-14 17:12:13 +0000595 createCaptureSessionInternal(null, outConfigurations, callback,
596 checkAndWrapHandler(handler),
Emilian Peev75a55702017-11-07 16:09:59 +0000597 /*operatingMode*/ICameraDeviceUser.CONSTRAINED_HIGH_SPEED_MODE,
598 /*sessionParams*/ null);
Eino-Ville Talvala0e04e912017-02-28 17:49:41 -0800599 }
600
601 @Override
602 public void createCustomCaptureSession(InputConfiguration inputConfig,
603 List<OutputConfiguration> outputs,
604 int operatingMode,
605 android.hardware.camera2.CameraCaptureSession.StateCallback callback,
606 Handler handler) throws CameraAccessException {
607 List<OutputConfiguration> currentOutputs = new ArrayList<OutputConfiguration>();
608 for (OutputConfiguration output : outputs) {
609 currentOutputs.add(new OutputConfiguration(output));
610 }
Emilian Peev77ca8662018-03-14 17:12:13 +0000611 createCaptureSessionInternal(inputConfig, currentOutputs, callback,
612 checkAndWrapHandler(handler), operatingMode, /*sessionParams*/ null);
Emilian Peev75a55702017-11-07 16:09:59 +0000613 }
614
615 @Override
616 public void createCaptureSession(SessionConfiguration config)
617 throws CameraAccessException {
618 if (config == null) {
619 throw new IllegalArgumentException("Invalid session configuration");
620 }
621
622 List<OutputConfiguration> outputConfigs = config.getOutputConfigurations();
623 if (outputConfigs == null) {
624 throw new IllegalArgumentException("Invalid output configurations");
625 }
Emilian Peev77ca8662018-03-14 17:12:13 +0000626 if (config.getExecutor() == null) {
627 throw new IllegalArgumentException("Invalid executor");
628 }
Emilian Peev75a55702017-11-07 16:09:59 +0000629 createCaptureSessionInternal(config.getInputConfiguration(), outputConfigs,
Emilian Peev77ca8662018-03-14 17:12:13 +0000630 config.getStateCallback(), config.getExecutor(), config.getSessionType(),
Emilian Peev75a55702017-11-07 16:09:59 +0000631 config.getSessionParameters());
Eino-Ville Talvala639fffe2015-06-30 10:34:48 -0700632 }
633
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700634 private void createCaptureSessionInternal(InputConfiguration inputConfig,
635 List<OutputConfiguration> outputConfigurations,
Emilian Peev77ca8662018-03-14 17:12:13 +0000636 CameraCaptureSession.StateCallback callback, Executor executor,
Emilian Peev75a55702017-11-07 16:09:59 +0000637 int operatingMode, CaptureRequest sessionParams) throws CameraAccessException {
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700638 synchronized(mInterfaceLock) {
Igor Murashkin0b27d342014-05-30 09:45:05 -0700639 if (DEBUG) {
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700640 Log.d(TAG, "createCaptureSessionInternal");
Igor Murashkin0b27d342014-05-30 09:45:05 -0700641 }
642
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700643 checkIfCameraClosedOrInError();
Igor Murashkin0b27d342014-05-30 09:45:05 -0700644
Eino-Ville Talvala0e04e912017-02-28 17:49:41 -0800645 boolean isConstrainedHighSpeed =
646 (operatingMode == ICameraDeviceUser.CONSTRAINED_HIGH_SPEED_MODE);
Zhijun Hea7677722015-06-01 16:36:06 -0700647 if (isConstrainedHighSpeed && inputConfig != null) {
648 throw new IllegalArgumentException("Constrained high speed session doesn't support"
649 + " input configuration yet.");
650 }
651
Eino-Ville Talvalad3b85f62014-08-05 15:02:31 -0700652 // Notify current session that it's going away, before starting camera operations
653 // After this call completes, the session is not allowed to call into CameraDeviceImpl
654 if (mCurrentSession != null) {
655 mCurrentSession.replaceSessionClose();
656 }
Igor Murashkin0b27d342014-05-30 09:45:05 -0700657
658 // TODO: dont block for this
659 boolean configureSuccess = true;
660 CameraAccessException pendingException = null;
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700661 Surface input = null;
Igor Murashkin0b27d342014-05-30 09:45:05 -0700662 try {
Chien-Yu Chenc58a6022015-06-16 11:13:48 -0700663 // configure streams and then block until IDLE
Zhijun Hea7677722015-06-01 16:36:06 -0700664 configureSuccess = configureStreamsChecked(inputConfig, outputConfigurations,
Emilian Peev75a55702017-11-07 16:09:59 +0000665 operatingMode, sessionParams);
Chien-Yu Chenc58a6022015-06-16 11:13:48 -0700666 if (configureSuccess == true && inputConfig != null) {
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800667 input = mRemoteDevice.getInputSurface();
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700668 }
Igor Murashkin0b27d342014-05-30 09:45:05 -0700669 } catch (CameraAccessException e) {
670 configureSuccess = false;
671 pendingException = e;
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700672 input = null;
Igor Murashkine1442202014-06-09 17:51:24 -0700673 if (DEBUG) {
674 Log.v(TAG, "createCaptureSession - failed with exception ", e);
675 }
Igor Murashkin0b27d342014-05-30 09:45:05 -0700676 }
677
678 // Fire onConfigured if configureOutputs succeeded, fire onConfigureFailed otherwise.
Eino-Ville Talvala639fffe2015-06-30 10:34:48 -0700679 CameraCaptureSessionCore newSession = null;
680 if (isConstrainedHighSpeed) {
Emilian Peev75a55702017-11-07 16:09:59 +0000681 ArrayList<Surface> surfaces = new ArrayList<>(outputConfigurations.size());
682 for (OutputConfiguration outConfig : outputConfigurations) {
683 surfaces.add(outConfig.getSurface());
684 }
685 StreamConfigurationMap config =
686 getCharacteristics().get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
687 SurfaceUtils.checkConstrainedHighSpeedSurfaces(surfaces, /*fpsRange*/null, config);
688
Eino-Ville Talvala639fffe2015-06-30 10:34:48 -0700689 newSession = new CameraConstrainedHighSpeedCaptureSessionImpl(mNextSessionId++,
Emilian Peev9a0ec8c2018-02-28 14:53:30 +0000690 callback, executor, this, mDeviceExecutor, configureSuccess,
Eino-Ville Talvala639fffe2015-06-30 10:34:48 -0700691 mCharacteristics);
692 } else {
693 newSession = new CameraCaptureSessionImpl(mNextSessionId++, input,
Emilian Peev9a0ec8c2018-02-28 14:53:30 +0000694 callback, executor, this, mDeviceExecutor, configureSuccess);
Eino-Ville Talvala639fffe2015-06-30 10:34:48 -0700695 }
Igor Murashkin0b27d342014-05-30 09:45:05 -0700696
Igor Murashkin0b27d342014-05-30 09:45:05 -0700697 // TODO: wait until current session closes, then create the new session
698 mCurrentSession = newSession;
699
700 if (pendingException != null) {
701 throw pendingException;
702 }
703
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700704 mSessionStateCallback = mCurrentSession.getDeviceStateCallback();
Igor Murashkin0b27d342014-05-30 09:45:05 -0700705 }
Eino-Ville Talvalacca00c62014-05-14 10:53:20 -0700706 }
707
Emilian Peev277de172018-11-06 16:15:59 +0000708 @Override
709 public boolean isSessionConfigurationSupported(
710 @NonNull SessionConfiguration sessionConfig) throws CameraAccessException,
711 UnsupportedOperationException, IllegalArgumentException {
712 synchronized(mInterfaceLock) {
713 checkIfCameraClosedOrInError();
714
715 return mRemoteDevice.isSessionConfigurationSupported(sessionConfig);
716 }
717 }
718
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700719 /**
720 * For use by backwards-compatibility code only.
721 */
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700722 public void setSessionListener(StateCallbackKK sessionCallback) {
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700723 synchronized(mInterfaceLock) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700724 mSessionStateCallback = sessionCallback;
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -0700725 }
726 }
727
Chien-Yu Chen3d2b5fe2017-04-05 16:42:31 -0700728 private void overrideEnableZsl(CameraMetadataNative request, boolean newValue) {
729 Boolean enableZsl = request.get(CaptureRequest.CONTROL_ENABLE_ZSL);
730 if (enableZsl == null) {
731 // If enableZsl is not available, don't override.
732 return;
733 }
734
735 request.set(CaptureRequest.CONTROL_ENABLE_ZSL, newValue);
736 }
737
Eino-Ville Talvalacca00c62014-05-14 10:53:20 -0700738 @Override
Emilian Peev2100ae72018-01-12 16:56:25 +0000739 public CaptureRequest.Builder createCaptureRequest(int templateType,
740 Set<String> physicalCameraIdSet)
741 throws CameraAccessException {
742 synchronized(mInterfaceLock) {
743 checkIfCameraClosedOrInError();
744
745 for (String physicalId : physicalCameraIdSet) {
746 if (physicalId == getId()) {
747 throw new IllegalStateException("Physical id matches the logical id!");
748 }
749 }
750
751 CameraMetadataNative templatedRequest = null;
752
753 templatedRequest = mRemoteDevice.createDefaultRequest(templateType);
754
755 // If app target SDK is older than O, or it's not a still capture template, enableZsl
756 // must be false in the default request.
757 if (mAppTargetSdkVersion < Build.VERSION_CODES.O ||
758 templateType != TEMPLATE_STILL_CAPTURE) {
759 overrideEnableZsl(templatedRequest, false);
760 }
761
762 CaptureRequest.Builder builder = new CaptureRequest.Builder(
763 templatedRequest, /*reprocess*/false, CameraCaptureSession.SESSION_ID_NONE,
764 getId(), physicalCameraIdSet);
765
766 return builder;
767 }
768 }
769
770 @Override
Eino-Ville Talvala70c22072013-08-27 12:09:04 -0700771 public CaptureRequest.Builder createCaptureRequest(int templateType)
772 throws CameraAccessException {
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -0700773 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -0700774 checkIfCameraClosedOrInError();
Igor Murashkin70725502013-06-25 20:27:06 +0000775
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800776 CameraMetadataNative templatedRequest = null;
Igor Murashkin70725502013-06-25 20:27:06 +0000777
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800778 templatedRequest = mRemoteDevice.createDefaultRequest(templateType);
Igor Murashkin70725502013-06-25 20:27:06 +0000779
Chien-Yu Chen3d2b5fe2017-04-05 16:42:31 -0700780 // If app target SDK is older than O, or it's not a still capture template, enableZsl
781 // must be false in the default request.
782 if (mAppTargetSdkVersion < Build.VERSION_CODES.O ||
783 templateType != TEMPLATE_STILL_CAPTURE) {
784 overrideEnableZsl(templatedRequest, false);
785 }
786
Chien-Yu Chen7a316f6b2015-04-13 14:31:45 -0700787 CaptureRequest.Builder builder = new CaptureRequest.Builder(
Emilian Peev2100ae72018-01-12 16:56:25 +0000788 templatedRequest, /*reprocess*/false, CameraCaptureSession.SESSION_ID_NONE,
789 getId(), /*physicalCameraIdSet*/ null);
Igor Murashkin70725502013-06-25 20:27:06 +0000790
Eino-Ville Talvala70c22072013-08-27 12:09:04 -0700791 return builder;
Igor Murashkin70725502013-06-25 20:27:06 +0000792 }
793 }
794
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700795 @Override
796 public CaptureRequest.Builder createReprocessCaptureRequest(TotalCaptureResult inputResult)
797 throws CameraAccessException {
798 synchronized(mInterfaceLock) {
799 checkIfCameraClosedOrInError();
800
801 CameraMetadataNative resultMetadata = new
802 CameraMetadataNative(inputResult.getNativeCopy());
803
Chien-Yu Chen7a316f6b2015-04-13 14:31:45 -0700804 return new CaptureRequest.Builder(resultMetadata, /*reprocess*/true,
Emilian Peev2100ae72018-01-12 16:56:25 +0000805 inputResult.getSessionId(), getId(), /*physicalCameraIdSet*/ null);
Chien-Yu Chen5398a672015-03-19 14:48:43 -0700806 }
807 }
808
Eino-Ville Talvalaad916f72015-04-11 12:09:11 -0700809 public void prepare(Surface surface) throws CameraAccessException {
Eino-Ville Talvala8b905572015-05-14 15:43:01 -0700810 if (surface == null) throw new IllegalArgumentException("Surface is null");
811
Eino-Ville Talvalaad916f72015-04-11 12:09:11 -0700812 synchronized(mInterfaceLock) {
813 int streamId = -1;
814 for (int i = 0; i < mConfiguredOutputs.size(); i++) {
Shuzhen Wange0a66672017-03-21 12:05:14 -0700815 final List<Surface> surfaces = mConfiguredOutputs.valueAt(i).getSurfaces();
816 if (surfaces.contains(surface)) {
Eino-Ville Talvalaad916f72015-04-11 12:09:11 -0700817 streamId = mConfiguredOutputs.keyAt(i);
818 break;
819 }
820 }
821 if (streamId == -1) {
822 throw new IllegalArgumentException("Surface is not part of this session");
823 }
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800824
825 mRemoteDevice.prepare(streamId);
Eino-Ville Talvalaad916f72015-04-11 12:09:11 -0700826 }
827 }
828
Ruben Brunk7ed1aaa2015-08-13 17:57:14 -0700829 public void prepare(int maxCount, Surface surface) throws CameraAccessException {
830 if (surface == null) throw new IllegalArgumentException("Surface is null");
831 if (maxCount <= 0) throw new IllegalArgumentException("Invalid maxCount given: " +
832 maxCount);
833
834 synchronized(mInterfaceLock) {
835 int streamId = -1;
836 for (int i = 0; i < mConfiguredOutputs.size(); i++) {
837 if (surface == mConfiguredOutputs.valueAt(i).getSurface()) {
838 streamId = mConfiguredOutputs.keyAt(i);
839 break;
840 }
841 }
842 if (streamId == -1) {
843 throw new IllegalArgumentException("Surface is not part of this session");
844 }
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800845
846 mRemoteDevice.prepare2(maxCount, streamId);
Ruben Brunk7ed1aaa2015-08-13 17:57:14 -0700847 }
848 }
849
Emilian Peev03233152017-10-27 16:01:20 +0100850 public void updateOutputConfiguration(OutputConfiguration config)
851 throws CameraAccessException {
852 synchronized(mInterfaceLock) {
853 int streamId = -1;
854 for (int i = 0; i < mConfiguredOutputs.size(); i++) {
855 if (config.getSurface() == mConfiguredOutputs.valueAt(i).getSurface()) {
856 streamId = mConfiguredOutputs.keyAt(i);
857 break;
858 }
859 }
860 if (streamId == -1) {
861 throw new IllegalArgumentException("Invalid output configuration");
862 }
863
864 mRemoteDevice.updateOutputConfiguration(streamId, config);
Yin-Chia Yehc60af902017-11-10 20:02:29 -0800865 mConfiguredOutputs.put(streamId, config);
Emilian Peev03233152017-10-27 16:01:20 +0100866 }
867 }
868
Eino-Ville Talvala14c09fa2015-07-15 16:04:04 -0700869 public void tearDown(Surface surface) throws CameraAccessException {
870 if (surface == null) throw new IllegalArgumentException("Surface is null");
871
872 synchronized(mInterfaceLock) {
873 int streamId = -1;
874 for (int i = 0; i < mConfiguredOutputs.size(); i++) {
875 if (surface == mConfiguredOutputs.valueAt(i).getSurface()) {
876 streamId = mConfiguredOutputs.keyAt(i);
877 break;
878 }
879 }
880 if (streamId == -1) {
881 throw new IllegalArgumentException("Surface is not part of this session");
882 }
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800883
884 mRemoteDevice.tearDown(streamId);
Eino-Ville Talvala14c09fa2015-07-15 16:04:04 -0700885 }
886 }
887
Shuzhen Wang4bd7abe2017-01-11 09:58:58 -0800888 public void finalizeOutputConfigs(List<OutputConfiguration> outputConfigs)
Zhijun Hec8b181e2016-05-30 14:54:39 -0700889 throws CameraAccessException {
Shuzhen Wang4bd7abe2017-01-11 09:58:58 -0800890 if (outputConfigs == null || outputConfigs.size() == 0) {
Zhijun Hec8b181e2016-05-30 14:54:39 -0700891 throw new IllegalArgumentException("deferred config is null or empty");
892 }
893
894 synchronized(mInterfaceLock) {
Shuzhen Wang4bd7abe2017-01-11 09:58:58 -0800895 for (OutputConfiguration config : outputConfigs) {
Zhijun Hec8b181e2016-05-30 14:54:39 -0700896 int streamId = -1;
897 for (int i = 0; i < mConfiguredOutputs.size(); i++) {
898 // Have to use equal here, as createCaptureSessionByOutputConfigurations() and
899 // createReprocessableCaptureSessionByConfigurations() do a copy of the configs.
900 if (config.equals(mConfiguredOutputs.valueAt(i))) {
901 streamId = mConfiguredOutputs.keyAt(i);
902 break;
903 }
904 }
905 if (streamId == -1) {
906 throw new IllegalArgumentException("Deferred config is not part of this "
907 + "session");
908 }
909
Shuzhen Wang4bd7abe2017-01-11 09:58:58 -0800910 if (config.getSurfaces().size() == 0) {
911 throw new IllegalArgumentException("The final config for stream " + streamId
912 + " must have at least 1 surface");
Zhijun Hec8b181e2016-05-30 14:54:39 -0700913 }
Shuzhen Wang4bd7abe2017-01-11 09:58:58 -0800914 mRemoteDevice.finalizeOutputConfigurations(streamId, config);
Yin-Chia Yehc60af902017-11-10 20:02:29 -0800915 mConfiguredOutputs.put(streamId, config);
Zhijun Hec8b181e2016-05-30 14:54:39 -0700916 }
917 }
918 }
919
Emilian Peev9a0ec8c2018-02-28 14:53:30 +0000920 public int capture(CaptureRequest request, CaptureCallback callback, Executor executor)
Igor Murashkin70725502013-06-25 20:27:06 +0000921 throws CameraAccessException {
Jianing Weid2c3a822014-03-27 18:27:43 -0700922 if (DEBUG) {
923 Log.d(TAG, "calling capture");
924 }
925 List<CaptureRequest> requestList = new ArrayList<CaptureRequest>();
926 requestList.add(request);
Emilian Peev9a0ec8c2018-02-28 14:53:30 +0000927 return submitCaptureRequest(requestList, callback, executor, /*streaming*/false);
Igor Murashkin70725502013-06-25 20:27:06 +0000928 }
929
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700930 public int captureBurst(List<CaptureRequest> requests, CaptureCallback callback,
Emilian Peev9a0ec8c2018-02-28 14:53:30 +0000931 Executor executor) throws CameraAccessException {
Igor Murashkin0b27d342014-05-30 09:45:05 -0700932 if (requests == null || requests.isEmpty()) {
933 throw new IllegalArgumentException("At least one request must be given");
Zhijun Hefc19e2c2013-08-22 14:43:07 -0700934 }
Emilian Peev9a0ec8c2018-02-28 14:53:30 +0000935 return submitCaptureRequest(requests, callback, executor, /*streaming*/false);
Igor Murashkin70725502013-06-25 20:27:06 +0000936 }
937
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700938 /**
939 * This method checks lastFrameNumber returned from ICameraDeviceUser methods for
940 * starting and stopping repeating request and flushing.
941 *
942 * <p>If lastFrameNumber is NO_FRAMES_CAPTURED, it means that the request was never
Eino-Ville Talvalaee37e302014-06-10 15:24:26 -0700943 * sent to HAL. Then onCaptureSequenceAborted is immediately triggered.
Chien-Yu Chen4560df72015-04-27 17:21:31 -0700944 * If lastFrameNumber is non-negative, then the requestId and lastFrameNumber as the last
945 * regular frame number will be added to the list mRequestLastFrameNumbersList.</p>
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700946 *
947 * @param requestId the request ID of the current repeating request.
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700948 * @param lastFrameNumber last frame number returned from binder.
Shuzhen Wangb45449b2019-04-17 13:32:03 -0700949 * @param repeatingRequestTypes the repeating requests' types.
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700950 */
951 private void checkEarlyTriggerSequenceComplete(
Shuzhen Wangb45449b2019-04-17 13:32:03 -0700952 final int requestId, final long lastFrameNumber,
953 final int[] repeatingRequestTypes) {
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700954 // lastFrameNumber being equal to NO_FRAMES_CAPTURED means that the request
Eino-Ville Talvalaee37e302014-06-10 15:24:26 -0700955 // was never sent to HAL. Should trigger onCaptureSequenceAborted immediately.
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700956 if (lastFrameNumber == CaptureCallback.NO_FRAMES_CAPTURED) {
957 final CaptureCallbackHolder holder;
958 int index = mCaptureCallbackMap.indexOfKey(requestId);
959 holder = (index >= 0) ? mCaptureCallbackMap.valueAt(index) : null;
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700960 if (holder != null) {
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700961 mCaptureCallbackMap.removeAt(index);
Jianing Weibaf0c652014-04-18 17:35:00 -0700962 if (DEBUG) {
963 Log.v(TAG, String.format(
964 "remove holder for requestId %d, "
965 + "because lastFrame is %d.",
966 requestId, lastFrameNumber));
967 }
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700968 }
969
970 if (holder != null) {
971 if (DEBUG) {
Eino-Ville Talvalaee37e302014-06-10 15:24:26 -0700972 Log.v(TAG, "immediately trigger onCaptureSequenceAborted because"
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700973 + " request did not reach HAL");
974 }
975
976 Runnable resultDispatch = new Runnable() {
977 @Override
978 public void run() {
Igor Murashkin21547d62014-06-04 15:21:42 -0700979 if (!CameraDeviceImpl.this.isClosed()) {
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700980 if (DEBUG) {
981 Log.d(TAG, String.format(
982 "early trigger sequence complete for request %d",
983 requestId));
984 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700985 holder.getCallback().onCaptureSequenceAborted(
Igor Murashkin21547d62014-06-04 15:21:42 -0700986 CameraDeviceImpl.this,
Eino-Ville Talvalaee37e302014-06-10 15:24:26 -0700987 requestId);
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700988 }
989 }
990 };
Emilian Peev77ca8662018-03-14 17:12:13 +0000991 final long ident = Binder.clearCallingIdentity();
992 try {
993 holder.getExecutor().execute(resultDispatch);
994 } finally {
995 Binder.restoreCallingIdentity(ident);
996 }
Jianing Wei5a4b02b2014-04-11 09:59:24 -0700997 } else {
998 Log.w(TAG, String.format(
Eino-Ville Talvalafd887432014-09-04 13:07:40 -0700999 "did not register callback to request %d",
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001000 requestId));
1001 }
1002 } else {
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001003 // This function is only called for regular/ZslStill request so lastFrameNumber is the
1004 // last regular/ZslStill frame number.
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001005 mRequestLastFrameNumbersList.add(new RequestLastFrameNumbersHolder(requestId,
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001006 lastFrameNumber, repeatingRequestTypes));
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001007
Eino-Ville Talvala848fe732014-09-15 16:24:08 -07001008 // It is possible that the last frame has already arrived, so we need to check
1009 // for sequence completion right away
1010 checkAndFireSequenceComplete();
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001011 }
1012 }
1013
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001014 private int[] getRequestTypes(final CaptureRequest[] requestArray) {
1015 int[] requestTypes = new int[requestArray.length];
1016 for (int i = 0; i < requestArray.length; i++) {
1017 requestTypes[i] = requestArray[i].getRequestType();
1018 }
1019 return requestTypes;
1020 }
1021
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001022 private int submitCaptureRequest(List<CaptureRequest> requestList, CaptureCallback callback,
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00001023 Executor executor, boolean repeating) throws CameraAccessException {
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -07001024
Emilian Peev77ca8662018-03-14 17:12:13 +00001025 // Need a valid executor, or current thread needs to have a looper, if
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001026 // callback is valid
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00001027 executor = checkExecutor(executor, callback);
Igor Murashkin70725502013-06-25 20:27:06 +00001028
Shuzhen Wang23d29382017-11-26 17:24:56 -08001029 // Make sure that there all requests have at least 1 surface; all surfaces are non-null;
1030 // the surface isn't a physical stream surface for reprocessing request
Ruben Brunk7f2372b2014-07-02 11:05:08 -07001031 for (CaptureRequest request : requestList) {
1032 if (request.getTargets().isEmpty()) {
1033 throw new IllegalArgumentException(
1034 "Each request must have at least one Surface target");
1035 }
1036
1037 for (Surface surface : request.getTargets()) {
1038 if (surface == null) {
1039 throw new IllegalArgumentException("Null Surface targets are not allowed");
1040 }
Shuzhen Wang23d29382017-11-26 17:24:56 -08001041
Shuzhen Wang23d29382017-11-26 17:24:56 -08001042 for (int i = 0; i < mConfiguredOutputs.size(); i++) {
1043 OutputConfiguration configuration = mConfiguredOutputs.valueAt(i);
1044 if (configuration.isForPhysicalCamera()
1045 && configuration.getSurfaces().contains(surface)) {
Shuzhen Wang0960fb42018-01-10 20:35:11 -08001046 if (request.isReprocess()) {
1047 throw new IllegalArgumentException(
1048 "Reprocess request on physical stream is not allowed");
1049 }
Shuzhen Wang23d29382017-11-26 17:24:56 -08001050 }
1051 }
Ruben Brunk7f2372b2014-07-02 11:05:08 -07001052 }
1053 }
1054
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07001055 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -07001056 checkIfCameraClosedOrInError();
Ruben Brunke73b41b2013-11-07 19:30:43 -08001057 if (repeating) {
1058 stopRepeating();
1059 }
1060
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001061 SubmitInfo requestInfo;
1062
1063 CaptureRequest[] requestArray = requestList.toArray(new CaptureRequest[requestList.size()]);
Yin-Chia Yehc60af902017-11-10 20:02:29 -08001064 // Convert Surface to streamIdx and surfaceIdx
1065 for (CaptureRequest request : requestArray) {
1066 request.convertSurfaceToStreamId(mConfiguredOutputs);
1067 }
1068
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001069 requestInfo = mRemoteDevice.submitRequestList(requestArray, repeating);
1070 if (DEBUG) {
1071 Log.v(TAG, "last frame number " + requestInfo.getLastFrameNumber());
Igor Murashkin70725502013-06-25 20:27:06 +00001072 }
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001073
Yin-Chia Yehc60af902017-11-10 20:02:29 -08001074 for (CaptureRequest request : requestArray) {
1075 request.recoverStreamIdToSurface();
1076 }
1077
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001078 if (callback != null) {
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001079 mCaptureCallbackMap.put(requestInfo.getRequestId(),
1080 new CaptureCallbackHolder(
Emilian Peev77ca8662018-03-14 17:12:13 +00001081 callback, requestList, executor, repeating, mNextSessionId - 1));
Jianing Weibaf0c652014-04-18 17:35:00 -07001082 } else {
1083 if (DEBUG) {
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001084 Log.d(TAG, "Listen for request " + requestInfo.getRequestId() + " is null");
Jianing Weibaf0c652014-04-18 17:35:00 -07001085 }
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -07001086 }
Igor Murashkin70725502013-06-25 20:27:06 +00001087
1088 if (repeating) {
Jianing Weid2c3a822014-03-27 18:27:43 -07001089 if (mRepeatingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001090 checkEarlyTriggerSequenceComplete(mRepeatingRequestId,
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001091 requestInfo.getLastFrameNumber(),
1092 mRepeatingRequestTypes);
Jianing Weid2c3a822014-03-27 18:27:43 -07001093 }
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001094 mRepeatingRequestId = requestInfo.getRequestId();
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001095 mRepeatingRequestTypes = getRequestTypes(requestArray);
Jianing Weid2c3a822014-03-27 18:27:43 -07001096 } else {
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001097 mRequestLastFrameNumbersList.add(
1098 new RequestLastFrameNumbersHolder(requestList, requestInfo));
Igor Murashkin70725502013-06-25 20:27:06 +00001099 }
1100
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001101 if (mIdle) {
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00001102 mDeviceExecutor.execute(mCallOnActive);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001103 }
1104 mIdle = false;
1105
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001106 return requestInfo.getRequestId();
Igor Murashkin70725502013-06-25 20:27:06 +00001107 }
1108 }
1109
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001110 public int setRepeatingRequest(CaptureRequest request, CaptureCallback callback,
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00001111 Executor executor) throws CameraAccessException {
Jianing Weid2c3a822014-03-27 18:27:43 -07001112 List<CaptureRequest> requestList = new ArrayList<CaptureRequest>();
1113 requestList.add(request);
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00001114 return submitCaptureRequest(requestList, callback, executor, /*streaming*/true);
Igor Murashkin70725502013-06-25 20:27:06 +00001115 }
1116
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001117 public int setRepeatingBurst(List<CaptureRequest> requests, CaptureCallback callback,
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00001118 Executor executor) throws CameraAccessException {
Igor Murashkin0b27d342014-05-30 09:45:05 -07001119 if (requests == null || requests.isEmpty()) {
1120 throw new IllegalArgumentException("At least one request must be given");
Zhijun Hefc19e2c2013-08-22 14:43:07 -07001121 }
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00001122 return submitCaptureRequest(requests, callback, executor, /*streaming*/true);
Igor Murashkin70725502013-06-25 20:27:06 +00001123 }
1124
Igor Murashkin70725502013-06-25 20:27:06 +00001125 public void stopRepeating() throws CameraAccessException {
1126
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07001127 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -07001128 checkIfCameraClosedOrInError();
Ruben Brunkdecfe952013-10-29 11:00:32 -07001129 if (mRepeatingRequestId != REQUEST_ID_NONE) {
1130
1131 int requestId = mRepeatingRequestId;
1132 mRepeatingRequestId = REQUEST_ID_NONE;
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001133 int[] requestTypes = mRepeatingRequestTypes;
1134 mRepeatingRequestTypes = null;
Ruben Brunkdecfe952013-10-29 11:00:32 -07001135
Chien-Yu Chen2da496f2016-04-14 13:33:00 -07001136 long lastFrameNumber;
1137 try {
1138 lastFrameNumber = mRemoteDevice.cancelRequest(requestId);
1139 } catch (IllegalArgumentException e) {
1140 if (DEBUG) {
1141 Log.v(TAG, "Repeating request was already stopped for request " + requestId);
1142 }
1143 // Repeating request was already stopped. Nothing more to do.
1144 return;
1145 }
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001146
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001147 checkEarlyTriggerSequenceComplete(requestId, lastFrameNumber, requestTypes);
Igor Murashkin70725502013-06-25 20:27:06 +00001148 }
1149 }
1150 }
1151
Zhijun Hed842fcd2013-12-26 14:14:04 -08001152 private void waitUntilIdle() throws CameraAccessException {
Zhijun He7f4d3142013-07-23 07:54:38 -07001153
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07001154 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -07001155 checkIfCameraClosedOrInError();
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07001156
Ruben Brunkdecfe952013-10-29 11:00:32 -07001157 if (mRepeatingRequestId != REQUEST_ID_NONE) {
Zhijun He7f4d3142013-07-23 07:54:38 -07001158 throw new IllegalStateException("Active repeating request ongoing");
1159 }
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001160
1161 mRemoteDevice.waitUntilIdle();
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001162 }
Igor Murashkin70725502013-06-25 20:27:06 +00001163 }
1164
Eino-Ville Talvala8ebd52b2013-08-13 12:09:44 -07001165 public void flush() throws CameraAccessException {
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07001166 synchronized(mInterfaceLock) {
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -07001167 checkIfCameraClosedOrInError();
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001168
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00001169 mDeviceExecutor.execute(mCallOnBusy);
Eino-Ville Talvalac346bbf2014-08-27 17:05:29 -07001170
1171 // If already idle, just do a busy->idle transition immediately, don't actually
1172 // flush.
1173 if (mIdle) {
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00001174 mDeviceExecutor.execute(mCallOnIdle);
Eino-Ville Talvalac346bbf2014-08-27 17:05:29 -07001175 return;
1176 }
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001177
1178 long lastFrameNumber = mRemoteDevice.flush();
1179 if (mRepeatingRequestId != REQUEST_ID_NONE) {
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001180 checkEarlyTriggerSequenceComplete(mRepeatingRequestId, lastFrameNumber,
1181 mRepeatingRequestTypes);
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001182 mRepeatingRequestId = REQUEST_ID_NONE;
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001183 mRepeatingRequestTypes = null;
Eino-Ville Talvala8ebd52b2013-08-13 12:09:44 -07001184 }
1185 }
1186 }
1187
1188 @Override
Igor Murashkin5c9eaf62013-09-10 19:35:24 -07001189 public void close() {
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07001190 synchronized (mInterfaceLock) {
Ruben Brunk0953b642015-06-11 16:12:35 -07001191 if (mClosing.getAndSet(true)) {
1192 return;
1193 }
1194
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001195 if (mRemoteDevice != null) {
1196 mRemoteDevice.disconnect();
Eino-Ville Talvalaee46b582016-04-04 12:26:40 -07001197 mRemoteDevice.unlinkToDeath(this, /*flags*/0);
Igor Murashkin70725502013-06-25 20:27:06 +00001198 }
1199
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -07001200 // Only want to fire the onClosed callback once;
1201 // either a normal close where the remote device is valid
1202 // or a close after a startup error (no remote device but in error state)
1203 if (mRemoteDevice != null || mInError) {
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00001204 mDeviceExecutor.execute(mCallOnClosed);
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001205 }
Igor Murashkin70725502013-06-25 20:27:06 +00001206
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001207 mRemoteDevice = null;
Igor Murashkin70725502013-06-25 20:27:06 +00001208 }
1209 }
1210
1211 @Override
1212 protected void finalize() throws Throwable {
1213 try {
1214 close();
Igor Murashkin70725502013-06-25 20:27:06 +00001215 }
1216 finally {
1217 super.finalize();
1218 }
1219 }
1220
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001221 private void checkInputConfiguration(InputConfiguration inputConfig) {
1222 if (inputConfig != null) {
1223 StreamConfigurationMap configMap = mCharacteristics.get(
1224 CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
1225
1226 int[] inputFormats = configMap.getInputFormats();
1227 boolean validFormat = false;
1228 for (int format : inputFormats) {
1229 if (format == inputConfig.getFormat()) {
1230 validFormat = true;
1231 }
1232 }
1233
1234 if (validFormat == false) {
1235 throw new IllegalArgumentException("input format " + inputConfig.getFormat() +
1236 " is not valid");
1237 }
1238
1239 boolean validSize = false;
1240 Size[] inputSizes = configMap.getInputSizes(inputConfig.getFormat());
1241 for (Size s : inputSizes) {
1242 if (inputConfig.getWidth() == s.getWidth() &&
1243 inputConfig.getHeight() == s.getHeight()) {
1244 validSize = true;
1245 }
1246 }
1247
1248 if (validSize == false) {
1249 throw new IllegalArgumentException("input size " + inputConfig.getWidth() + "x" +
1250 inputConfig.getHeight() + " is not valid");
1251 }
1252 }
1253 }
1254
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001255 /**
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001256 * <p>A callback for tracking the progress of a {@link CaptureRequest}
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001257 * submitted to the camera device.</p>
1258 *
Eino-Ville Talvalacad1bfe2017-05-23 14:10:49 -07001259 * An interface instead of an abstract class because this is internal and
1260 * we want to make sure we always implement all its callbacks until we reach
1261 * the public layer.
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001262 */
Eino-Ville Talvalacad1bfe2017-05-23 14:10:49 -07001263 public interface CaptureCallback {
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001264
1265 /**
1266 * This constant is used to indicate that no images were captured for
1267 * the request.
1268 *
1269 * @hide
1270 */
1271 public static final int NO_FRAMES_CAPTURED = -1;
1272
1273 /**
1274 * This method is called when the camera device has started capturing
1275 * the output image for the request, at the beginning of image exposure.
1276 *
1277 * @see android.media.MediaActionSound
1278 */
1279 public void onCaptureStarted(CameraDevice camera,
Eino-Ville Talvalacad1bfe2017-05-23 14:10:49 -07001280 CaptureRequest request, long timestamp, long frameNumber);
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001281
1282 /**
1283 * This method is called when some results from an image capture are
1284 * available.
1285 *
1286 * @hide
1287 */
1288 public void onCapturePartial(CameraDevice camera,
Eino-Ville Talvalacad1bfe2017-05-23 14:10:49 -07001289 CaptureRequest request, CaptureResult result);
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001290
1291 /**
1292 * This method is called when an image capture makes partial forward progress; some
1293 * (but not all) results from an image capture are available.
1294 *
1295 */
1296 public void onCaptureProgressed(CameraDevice camera,
Eino-Ville Talvalacad1bfe2017-05-23 14:10:49 -07001297 CaptureRequest request, CaptureResult partialResult);
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001298
1299 /**
1300 * This method is called when an image capture has fully completed and all the
1301 * result metadata is available.
1302 */
1303 public void onCaptureCompleted(CameraDevice camera,
Eino-Ville Talvalacad1bfe2017-05-23 14:10:49 -07001304 CaptureRequest request, TotalCaptureResult result);
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001305
1306 /**
1307 * This method is called instead of {@link #onCaptureCompleted} when the
1308 * camera device failed to produce a {@link CaptureResult} for the
1309 * request.
1310 */
1311 public void onCaptureFailed(CameraDevice camera,
Eino-Ville Talvalacad1bfe2017-05-23 14:10:49 -07001312 CaptureRequest request, CaptureFailure failure);
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001313
1314 /**
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001315 * This method is called independently of the others in CaptureCallback,
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001316 * when a capture sequence finishes and all {@link CaptureResult}
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001317 * or {@link CaptureFailure} for it have been returned via this callback.
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001318 */
1319 public void onCaptureSequenceCompleted(CameraDevice camera,
Eino-Ville Talvalacad1bfe2017-05-23 14:10:49 -07001320 int sequenceId, long frameNumber);
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001321
1322 /**
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001323 * This method is called independently of the others in CaptureCallback,
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001324 * when a capture sequence aborts before any {@link CaptureResult}
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001325 * or {@link CaptureFailure} for it have been returned via this callback.
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001326 */
1327 public void onCaptureSequenceAborted(CameraDevice camera,
Eino-Ville Talvalacad1bfe2017-05-23 14:10:49 -07001328 int sequenceId);
Eino-Ville Talvala3e0023a2016-03-06 18:40:01 -08001329
Eino-Ville Talvalacad1bfe2017-05-23 14:10:49 -07001330 /**
1331 * This method is called independently of the others in CaptureCallback, if an output buffer
1332 * is dropped for a particular capture request.
1333 *
1334 * Loss of metadata is communicated via onCaptureFailed, independently of any buffer loss.
1335 */
Eino-Ville Talvala3e0023a2016-03-06 18:40:01 -08001336 public void onCaptureBufferLost(CameraDevice camera,
Eino-Ville Talvalacad1bfe2017-05-23 14:10:49 -07001337 CaptureRequest request, Surface target, long frameNumber);
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001338 }
1339
1340 /**
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001341 * A callback for notifications about the state of a camera device, adding in the callbacks that
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001342 * were part of the earlier KK API design, but now only used internally.
1343 */
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001344 public static abstract class StateCallbackKK extends StateCallback {
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001345 /**
1346 * The method called when a camera device has no outputs configured.
1347 *
1348 */
1349 public void onUnconfigured(CameraDevice camera) {
1350 // Default empty implementation
1351 }
1352
1353 /**
1354 * The method called when a camera device begins processing
1355 * {@link CaptureRequest capture requests}.
1356 *
1357 */
1358 public void onActive(CameraDevice camera) {
1359 // Default empty implementation
1360 }
1361
1362 /**
1363 * The method called when a camera device is busy.
1364 *
1365 */
1366 public void onBusy(CameraDevice camera) {
1367 // Default empty implementation
1368 }
1369
1370 /**
1371 * The method called when a camera device has finished processing all
1372 * submitted capture requests and has reached an idle state.
1373 *
1374 */
1375 public void onIdle(CameraDevice camera) {
1376 // Default empty implementation
1377 }
Eino-Ville Talvalaad916f72015-04-11 12:09:11 -07001378
1379 /**
Shuzhen Wang88f1af22016-09-30 10:29:28 -07001380 * This method is called when camera device's non-repeating request queue is empty,
1381 * and is ready to start capturing next image.
1382 */
1383 public void onRequestQueueEmpty() {
1384 // Default empty implementation
1385 }
1386
1387 /**
Eino-Ville Talvalaad916f72015-04-11 12:09:11 -07001388 * The method called when the camera device has finished preparing
1389 * an output Surface
1390 */
1391 public void onSurfacePrepared(Surface surface) {
1392 // Default empty implementation
1393 }
Eino-Ville Talvalaa6b5ba52014-07-02 16:30:53 -07001394 }
1395
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001396 static class CaptureCallbackHolder {
Igor Murashkin70725502013-06-25 20:27:06 +00001397
1398 private final boolean mRepeating;
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001399 private final CaptureCallback mCallback;
Jianing Weid2c3a822014-03-27 18:27:43 -07001400 private final List<CaptureRequest> mRequestList;
Emilian Peev77ca8662018-03-14 17:12:13 +00001401 private final Executor mExecutor;
Chien-Yu Chen7a316f6b2015-04-13 14:31:45 -07001402 private final int mSessionId;
Shuzhen Wangae10eb92017-03-01 12:58:04 -08001403 /**
1404 * <p>Determine if the callback holder is for a constrained high speed request list that
1405 * expects batched capture results. Capture results will be batched if the request list
1406 * is interleaved with preview and video requests. Capture results won't be batched if the
1407 * request list only contains preview requests, or if the request doesn't belong to a
1408 * constrained high speed list.
1409 */
1410 private final boolean mHasBatchedOutputs;
Igor Murashkin70725502013-06-25 20:27:06 +00001411
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001412 CaptureCallbackHolder(CaptureCallback callback, List<CaptureRequest> requestList,
Emilian Peev77ca8662018-03-14 17:12:13 +00001413 Executor executor, boolean repeating, int sessionId) {
1414 if (callback == null || executor == null) {
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -07001415 throw new UnsupportedOperationException(
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001416 "Must have a valid handler and a valid callback");
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -07001417 }
Igor Murashkin70725502013-06-25 20:27:06 +00001418 mRepeating = repeating;
Emilian Peev77ca8662018-03-14 17:12:13 +00001419 mExecutor = executor;
Jianing Weid2c3a822014-03-27 18:27:43 -07001420 mRequestList = new ArrayList<CaptureRequest>(requestList);
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001421 mCallback = callback;
Chien-Yu Chen7a316f6b2015-04-13 14:31:45 -07001422 mSessionId = sessionId;
Shuzhen Wangae10eb92017-03-01 12:58:04 -08001423
1424 // Check whether this callback holder is for batched outputs.
1425 // The logic here should match createHighSpeedRequestList.
1426 boolean hasBatchedOutputs = true;
1427 for (int i = 0; i < requestList.size(); i++) {
1428 CaptureRequest request = requestList.get(i);
1429 if (!request.isPartOfCRequestList()) {
1430 hasBatchedOutputs = false;
1431 break;
1432 }
1433 if (i == 0) {
1434 Collection<Surface> targets = request.getTargets();
1435 if (targets.size() != 2) {
1436 hasBatchedOutputs = false;
1437 break;
1438 }
1439 }
1440 }
1441 mHasBatchedOutputs = hasBatchedOutputs;
Igor Murashkin70725502013-06-25 20:27:06 +00001442 }
1443
1444 public boolean isRepeating() {
1445 return mRepeating;
1446 }
1447
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001448 public CaptureCallback getCallback() {
1449 return mCallback;
Igor Murashkin70725502013-06-25 20:27:06 +00001450 }
1451
Jianing Weid2c3a822014-03-27 18:27:43 -07001452 public CaptureRequest getRequest(int subsequenceId) {
1453 if (subsequenceId >= mRequestList.size()) {
1454 throw new IllegalArgumentException(
1455 String.format(
1456 "Requested subsequenceId %d is larger than request list size %d.",
1457 subsequenceId, mRequestList.size()));
1458 } else {
1459 if (subsequenceId < 0) {
1460 throw new IllegalArgumentException(String.format(
1461 "Requested subsequenceId %d is negative", subsequenceId));
1462 } else {
1463 return mRequestList.get(subsequenceId);
1464 }
1465 }
1466 }
1467
Igor Murashkin70725502013-06-25 20:27:06 +00001468 public CaptureRequest getRequest() {
Jianing Weid2c3a822014-03-27 18:27:43 -07001469 return getRequest(0);
Igor Murashkin70725502013-06-25 20:27:06 +00001470 }
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -07001471
Emilian Peev77ca8662018-03-14 17:12:13 +00001472 public Executor getExecutor() {
1473 return mExecutor;
Eino-Ville Talvala4af73c22013-08-14 10:35:46 -07001474 }
1475
Chien-Yu Chen7a316f6b2015-04-13 14:31:45 -07001476 public int getSessionId() {
1477 return mSessionId;
1478 }
Shuzhen Wangae10eb92017-03-01 12:58:04 -08001479
1480 public int getRequestCount() {
1481 return mRequestList.size();
1482 }
1483
1484 public boolean hasBatchedOutputs() {
1485 return mHasBatchedOutputs;
1486 }
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001487 }
Chien-Yu Chen7a316f6b2015-04-13 14:31:45 -07001488
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001489 /**
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001490 * This class holds a capture ID and its expected last regular, zslStill, and reprocess
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001491 * frame number.
1492 */
1493 static class RequestLastFrameNumbersHolder {
1494 // request ID
1495 private final int mRequestId;
1496 // The last regular frame number for this request ID. It's
1497 // CaptureCallback.NO_FRAMES_CAPTURED if the request ID has no regular request.
1498 private final long mLastRegularFrameNumber;
1499 // The last reprocess frame number for this request ID. It's
1500 // CaptureCallback.NO_FRAMES_CAPTURED if the request ID has no reprocess request.
1501 private final long mLastReprocessFrameNumber;
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001502 // The last ZSL still capture frame number for this request ID. It's
1503 // CaptureCallback.NO_FRAMES_CAPTURED if the request ID has no zsl request.
1504 private final long mLastZslStillFrameNumber;
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001505
1506 /**
1507 * Create a request-last-frame-numbers holder with a list of requests, request ID, and
1508 * the last frame number returned by camera service.
1509 */
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001510 public RequestLastFrameNumbersHolder(List<CaptureRequest> requestList, SubmitInfo requestInfo) {
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001511 long lastRegularFrameNumber = CaptureCallback.NO_FRAMES_CAPTURED;
1512 long lastReprocessFrameNumber = CaptureCallback.NO_FRAMES_CAPTURED;
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001513 long lastZslStillFrameNumber = CaptureCallback.NO_FRAMES_CAPTURED;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001514 long frameNumber = requestInfo.getLastFrameNumber();
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001515
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001516 if (requestInfo.getLastFrameNumber() < requestList.size() - 1) {
1517 throw new IllegalArgumentException(
1518 "lastFrameNumber: " + requestInfo.getLastFrameNumber() +
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001519 " should be at least " + (requestList.size() - 1) + " for the number of " +
1520 " requests in the list: " + requestList.size());
1521 }
1522
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001523 // find the last regular, zslStill, and reprocess frame number
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001524 for (int i = requestList.size() - 1; i >= 0; i--) {
1525 CaptureRequest request = requestList.get(i);
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001526 int requestType = request.getRequestType();
1527 if (requestType == CaptureRequest.REQUEST_TYPE_REPROCESS
1528 && lastReprocessFrameNumber == CaptureCallback.NO_FRAMES_CAPTURED) {
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001529 lastReprocessFrameNumber = frameNumber;
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001530 } else if (requestType == CaptureRequest.REQUEST_TYPE_ZSL_STILL
1531 && lastZslStillFrameNumber == CaptureCallback.NO_FRAMES_CAPTURED) {
1532 lastZslStillFrameNumber = frameNumber;
1533 } else if (requestType == CaptureRequest.REQUEST_TYPE_REGULAR
1534 && lastRegularFrameNumber == CaptureCallback.NO_FRAMES_CAPTURED) {
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001535 lastRegularFrameNumber = frameNumber;
1536 }
1537
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001538 if (lastReprocessFrameNumber != CaptureCallback.NO_FRAMES_CAPTURED
1539 && lastZslStillFrameNumber != CaptureCallback.NO_FRAMES_CAPTURED
1540 && lastRegularFrameNumber != CaptureCallback.NO_FRAMES_CAPTURED) {
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001541 break;
1542 }
1543
1544 frameNumber--;
1545 }
1546
1547 mLastRegularFrameNumber = lastRegularFrameNumber;
1548 mLastReprocessFrameNumber = lastReprocessFrameNumber;
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001549 mLastZslStillFrameNumber = lastZslStillFrameNumber;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -08001550 mRequestId = requestInfo.getRequestId();
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001551 }
1552
1553 /**
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001554 * Create a request-last-frame-numbers holder with a request ID and last regular/ZslStill
1555 * frame number.
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001556 */
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001557 RequestLastFrameNumbersHolder(int requestId, long lastFrameNumber,
1558 int[] repeatingRequestTypes) {
1559 long lastRegularFrameNumber = CaptureCallback.NO_FRAMES_CAPTURED;
1560 long lastZslStillFrameNumber = CaptureCallback.NO_FRAMES_CAPTURED;
1561
1562 if (repeatingRequestTypes == null) {
1563 throw new IllegalArgumentException(
1564 "repeatingRequest list must not be null");
1565 }
1566 if (lastFrameNumber < repeatingRequestTypes.length - 1) {
1567 throw new IllegalArgumentException(
1568 "lastFrameNumber: " + lastFrameNumber + " should be at least "
1569 + (repeatingRequestTypes.length - 1)
1570 + " for the number of requests in the list: "
1571 + repeatingRequestTypes.length);
1572 }
1573
1574 long frameNumber = lastFrameNumber;
1575 for (int i = repeatingRequestTypes.length - 1; i >= 0; i--) {
1576 if (repeatingRequestTypes[i] == CaptureRequest.REQUEST_TYPE_ZSL_STILL
1577 && lastZslStillFrameNumber == CaptureCallback.NO_FRAMES_CAPTURED) {
1578 lastZslStillFrameNumber = frameNumber;
1579 } else if (repeatingRequestTypes[i] == CaptureRequest.REQUEST_TYPE_REGULAR
1580 && lastRegularFrameNumber == CaptureCallback.NO_FRAMES_CAPTURED) {
1581 lastRegularFrameNumber = frameNumber;
1582 }
1583
1584 if (lastZslStillFrameNumber != CaptureCallback.NO_FRAMES_CAPTURED
1585 && lastRegularFrameNumber != CaptureCallback.NO_FRAMES_CAPTURED) {
1586 break;
1587 }
1588
1589 frameNumber--;
1590 }
1591
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001592 mLastRegularFrameNumber = lastRegularFrameNumber;
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001593 mLastZslStillFrameNumber = lastZslStillFrameNumber;
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001594 mLastReprocessFrameNumber = CaptureCallback.NO_FRAMES_CAPTURED;
1595 mRequestId = requestId;
1596 }
1597
1598 /**
1599 * Return the last regular frame number. Return CaptureCallback.NO_FRAMES_CAPTURED if
1600 * it contains no regular request.
1601 */
1602 public long getLastRegularFrameNumber() {
1603 return mLastRegularFrameNumber;
1604 }
1605
1606 /**
1607 * Return the last reprocess frame number. Return CaptureCallback.NO_FRAMES_CAPTURED if
1608 * it contains no reprocess request.
1609 */
1610 public long getLastReprocessFrameNumber() {
1611 return mLastReprocessFrameNumber;
1612 }
1613
1614 /**
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001615 * Return the last ZslStill frame number. Return CaptureCallback.NO_FRAMES_CAPTURED if
1616 * it contains no Zsl request.
1617 */
1618 public long getLastZslStillFrameNumber() {
1619 return mLastZslStillFrameNumber;
1620 }
1621
1622 /**
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001623 * Return the last frame number overall.
1624 */
1625 public long getLastFrameNumber() {
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001626 return Math.max(mLastZslStillFrameNumber,
1627 Math.max(mLastRegularFrameNumber, mLastReprocessFrameNumber));
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001628 }
1629
1630 /**
1631 * Return the request ID.
1632 */
1633 public int getRequestId() {
1634 return mRequestId;
1635 }
Igor Murashkin70725502013-06-25 20:27:06 +00001636 }
1637
Jianing Weid2c3a822014-03-27 18:27:43 -07001638 /**
1639 * This class tracks the last frame number for submitted requests.
1640 */
1641 public class FrameNumberTracker {
1642
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001643 /** the completed frame number for each type of capture results */
1644 private long[] mCompletedFrameNumber = new long[CaptureRequest.REQUEST_TYPE_COUNT];
1645
1646 /** the skipped frame numbers that don't belong to each type of capture results */
1647 private final LinkedList<Long>[] mSkippedOtherFrameNumbers =
1648 new LinkedList[CaptureRequest.REQUEST_TYPE_COUNT];
1649
1650 /** the skipped frame numbers that belong to each type of capture results */
1651 private final LinkedList<Long>[] mSkippedFrameNumbers =
1652 new LinkedList[CaptureRequest.REQUEST_TYPE_COUNT];
1653
1654 /** frame number -> request type */
1655 private final TreeMap<Long, Integer> mFutureErrorMap = new TreeMap<Long, Integer>();
Igor Murashkin1e854c52014-08-28 15:21:49 -07001656 /** Map frame numbers to list of partial results */
1657 private final HashMap<Long, List<CaptureResult>> mPartialResults = new HashMap<>();
Jianing Weid2c3a822014-03-27 18:27:43 -07001658
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001659 public FrameNumberTracker() {
1660 for (int i = 0; i < CaptureRequest.REQUEST_TYPE_COUNT; i++) {
1661 mCompletedFrameNumber[i] = CaptureCallback.NO_FRAMES_CAPTURED;
1662 mSkippedOtherFrameNumbers[i] = new LinkedList<Long>();
1663 mSkippedFrameNumbers[i] = new LinkedList<Long>();
1664 }
1665 }
1666
Jianing Weid2c3a822014-03-27 18:27:43 -07001667 private void update() {
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001668 Iterator iter = mFutureErrorMap.entrySet().iterator();
Jianing Weid2c3a822014-03-27 18:27:43 -07001669 while (iter.hasNext()) {
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001670 TreeMap.Entry pair = (TreeMap.Entry)iter.next();
1671 Long errorFrameNumber = (Long)pair.getKey();
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001672 int requestType = (int) pair.getValue();
1673 Boolean removeError = false;
1674 if (errorFrameNumber == mCompletedFrameNumber[requestType] + 1) {
1675 mCompletedFrameNumber[requestType] = errorFrameNumber;
1676 removeError = true;
Jianing Weid2c3a822014-03-27 18:27:43 -07001677 } else {
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001678 if (!mSkippedFrameNumbers[requestType].isEmpty()) {
1679 if (errorFrameNumber == mSkippedFrameNumbers[requestType].element()) {
1680 mCompletedFrameNumber[requestType] = errorFrameNumber;
1681 mSkippedFrameNumbers[requestType].remove();
1682 removeError = true;
1683 }
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001684 } else {
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001685 for (int i = 1; i < CaptureRequest.REQUEST_TYPE_COUNT; i++) {
1686 int otherType = (requestType + i) % CaptureRequest.REQUEST_TYPE_COUNT;
1687 if (!mSkippedOtherFrameNumbers[otherType].isEmpty() && errorFrameNumber
1688 == mSkippedOtherFrameNumbers[otherType].element()) {
1689 mCompletedFrameNumber[requestType] = errorFrameNumber;
1690 mSkippedOtherFrameNumbers[otherType].remove();
1691 removeError = true;
1692 break;
1693 }
1694 }
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001695 }
1696 }
1697 if (removeError) {
1698 iter.remove();
Jianing Weid2c3a822014-03-27 18:27:43 -07001699 }
1700 }
1701 }
1702
1703 /**
1704 * This function is called every time when a result or an error is received.
Igor Murashkin1e854c52014-08-28 15:21:49 -07001705 * @param frameNumber the frame number corresponding to the result or error
1706 * @param isError true if it is an error, false if it is not an error
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001707 * @param requestType the type of capture request: Reprocess, ZslStill, or Regular.
Jianing Weid2c3a822014-03-27 18:27:43 -07001708 */
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001709 public void updateTracker(long frameNumber, boolean isError, int requestType) {
Jianing Weid2c3a822014-03-27 18:27:43 -07001710 if (isError) {
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001711 mFutureErrorMap.put(frameNumber, requestType);
Jianing Weid2c3a822014-03-27 18:27:43 -07001712 } else {
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001713 try {
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001714 updateCompletedFrameNumber(frameNumber, requestType);
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001715 } catch (IllegalArgumentException e) {
1716 Log.e(TAG, e.getMessage());
Jianing Weid2c3a822014-03-27 18:27:43 -07001717 }
Jianing Weid2c3a822014-03-27 18:27:43 -07001718 }
1719 update();
1720 }
1721
Igor Murashkin1e854c52014-08-28 15:21:49 -07001722 /**
1723 * This function is called every time a result has been completed.
1724 *
1725 * <p>It keeps a track of all the partial results already created for a particular
1726 * frame number.</p>
1727 *
1728 * @param frameNumber the frame number corresponding to the result
1729 * @param result the total or partial result
1730 * @param partial {@true} if the result is partial, {@code false} if total
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001731 * @param requestType the type of capture request: Reprocess, ZslStill, or Regular.
Igor Murashkin1e854c52014-08-28 15:21:49 -07001732 */
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001733 public void updateTracker(long frameNumber, CaptureResult result, boolean partial,
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001734 int requestType) {
Igor Murashkin1e854c52014-08-28 15:21:49 -07001735 if (!partial) {
1736 // Update the total result's frame status as being successful
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001737 updateTracker(frameNumber, /*isError*/false, requestType);
Igor Murashkin1e854c52014-08-28 15:21:49 -07001738 // Don't keep a list of total results, we don't need to track them
1739 return;
1740 }
1741
1742 if (result == null) {
1743 // Do not record blank results; this also means there will be no total result
1744 // so it doesn't matter that the partials were not recorded
1745 return;
1746 }
1747
1748 // Partial results must be aggregated in-order for that frame number
1749 List<CaptureResult> partials = mPartialResults.get(frameNumber);
1750 if (partials == null) {
1751 partials = new ArrayList<>();
1752 mPartialResults.put(frameNumber, partials);
1753 }
1754
1755 partials.add(result);
1756 }
1757
1758 /**
1759 * Attempt to pop off all of the partial results seen so far for the {@code frameNumber}.
1760 *
1761 * <p>Once popped-off, the partial results are forgotten (unless {@code updateTracker}
1762 * is called again with new partials for that frame number).</p>
1763 *
1764 * @param frameNumber the frame number corresponding to the result
1765 * @return a list of partial results for that frame with at least 1 element,
1766 * or {@code null} if there were no partials recorded for that frame
1767 */
1768 public List<CaptureResult> popPartialResults(long frameNumber) {
1769 return mPartialResults.remove(frameNumber);
1770 }
1771
Jianing Weid2c3a822014-03-27 18:27:43 -07001772 public long getCompletedFrameNumber() {
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001773 return mCompletedFrameNumber[CaptureRequest.REQUEST_TYPE_REGULAR];
Jianing Weid2c3a822014-03-27 18:27:43 -07001774 }
1775
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001776 public long getCompletedReprocessFrameNumber() {
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001777 return mCompletedFrameNumber[CaptureRequest.REQUEST_TYPE_REPROCESS];
1778 }
1779
1780 public long getCompletedZslStillFrameNumber() {
1781 return mCompletedFrameNumber[CaptureRequest.REQUEST_TYPE_ZSL_STILL];
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001782 }
1783
1784 /**
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001785 * Update the completed frame number for results of 3 categories
1786 * (Regular/Reprocess/ZslStill).
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001787 *
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001788 * It validates that all previous frames of the same category have arrived.
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001789 *
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001790 * If there is a gap since previous frame number of the same category, assume the frames in
1791 * the gap are other categories and store them in the skipped frame number queue to check
1792 * against when frames of those categories arrive.
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001793 */
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001794 private void updateCompletedFrameNumber(long frameNumber,
1795 int requestType) throws IllegalArgumentException {
1796 if (frameNumber <= mCompletedFrameNumber[requestType]) {
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001797 throw new IllegalArgumentException("frame number " + frameNumber + " is a repeat");
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001798 }
1799
1800 // Assume there are only 3 different types of capture requests.
1801 int otherType1 = (requestType + 1) % CaptureRequest.REQUEST_TYPE_COUNT;
1802 int otherType2 = (requestType + 2) % CaptureRequest.REQUEST_TYPE_COUNT;
1803 long maxOtherFrameNumberSeen =
1804 Math.max(mCompletedFrameNumber[otherType1], mCompletedFrameNumber[otherType2]);
1805 if (frameNumber < maxOtherFrameNumberSeen) {
1806 // if frame number is smaller than completed frame numbers of other categories,
1807 // it must be:
1808 // - the head of mSkippedFrameNumbers for this category, or
1809 // - in one of other mSkippedOtherFrameNumbers
1810 if (!mSkippedFrameNumbers[requestType].isEmpty()) {
1811 // frame number must be head of current type of mSkippedFrameNumbers if
1812 // mSkippedFrameNumbers isn't empty.
1813 if (frameNumber < mSkippedFrameNumbers[requestType].element()) {
1814 throw new IllegalArgumentException("frame number " + frameNumber
1815 + " is a repeat");
1816 } else if (frameNumber > mSkippedFrameNumbers[requestType].element()) {
1817 throw new IllegalArgumentException("frame number " + frameNumber
1818 + " comes out of order. Expecting "
1819 + mSkippedFrameNumbers[requestType].element());
1820 }
1821 // frame number matches the head of the skipped frame number queue.
1822 mSkippedFrameNumbers[requestType].remove();
1823 } else {
1824 // frame number must be in one of the other mSkippedOtherFrameNumbers.
1825 int index1 = mSkippedOtherFrameNumbers[otherType1].indexOf(frameNumber);
1826 int index2 = mSkippedOtherFrameNumbers[otherType2].indexOf(frameNumber);
1827 boolean inSkippedOther1 = index1 != -1;
1828 boolean inSkippedOther2 = index2 != -1;
1829 if (!(inSkippedOther1 ^ inSkippedOther2)) {
1830 throw new IllegalArgumentException("frame number " + frameNumber
1831 + " is a repeat or invalid");
1832 }
1833
1834 // We know the category of frame numbers in skippedOtherFrameNumbers leading up
1835 // to the current frame number. Move them into the correct skippedFrameNumbers.
1836 LinkedList<Long> srcList, dstList;
1837 int index;
1838 if (inSkippedOther1) {
1839 srcList = mSkippedOtherFrameNumbers[otherType1];
1840 dstList = mSkippedFrameNumbers[otherType2];
1841 index = index1;
1842 } else {
1843 srcList = mSkippedOtherFrameNumbers[otherType2];
1844 dstList = mSkippedFrameNumbers[otherType1];
1845 index = index2;
1846 }
1847 for (int i = 0; i < index; i++) {
1848 dstList.add(srcList.removeFirst());
1849 }
1850
1851 // Remove current frame number from skippedOtherFrameNumbers
1852 srcList.remove();
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001853 }
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001854 } else {
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001855 // there is a gap of unseen frame numbers which should belong to the other
1856 // 2 categories. Put all the skipped frame numbers in the queue.
1857 for (long i =
1858 Math.max(maxOtherFrameNumberSeen, mCompletedFrameNumber[requestType]) + 1;
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001859 i < frameNumber; i++) {
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001860 mSkippedOtherFrameNumbers[requestType].add(i);
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001861 }
1862 }
1863
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001864 mCompletedFrameNumber[requestType] = frameNumber;
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001865 }
Jianing Weid2c3a822014-03-27 18:27:43 -07001866 }
1867
1868 private void checkAndFireSequenceComplete() {
1869 long completedFrameNumber = mFrameNumberTracker.getCompletedFrameNumber();
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001870 long completedReprocessFrameNumber = mFrameNumberTracker.getCompletedReprocessFrameNumber();
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001871 long completedZslStillFrameNumber = mFrameNumberTracker.getCompletedZslStillFrameNumber();
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001872 boolean isReprocess = false;
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001873 Iterator<RequestLastFrameNumbersHolder> iter = mRequestLastFrameNumbersList.iterator();
Jianing Weid2c3a822014-03-27 18:27:43 -07001874 while (iter.hasNext()) {
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001875 final RequestLastFrameNumbersHolder requestLastFrameNumbers = iter.next();
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001876 boolean sequenceCompleted = false;
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001877 final int requestId = requestLastFrameNumbers.getRequestId();
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001878 final CaptureCallbackHolder holder;
1879 synchronized(mInterfaceLock) {
1880 if (mRemoteDevice == null) {
1881 Log.w(TAG, "Camera closed while checking sequences");
1882 return;
1883 }
Jianing Weid2c3a822014-03-27 18:27:43 -07001884
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001885 int index = mCaptureCallbackMap.indexOfKey(requestId);
1886 holder = (index >= 0) ?
1887 mCaptureCallbackMap.valueAt(index) : null;
1888 if (holder != null) {
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001889 long lastRegularFrameNumber =
1890 requestLastFrameNumbers.getLastRegularFrameNumber();
1891 long lastReprocessFrameNumber =
1892 requestLastFrameNumbers.getLastReprocessFrameNumber();
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001893 long lastZslStillFrameNumber =
1894 requestLastFrameNumbers.getLastZslStillFrameNumber();
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001895 // check if it's okay to remove request from mCaptureCallbackMap
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001896 if (lastRegularFrameNumber <= completedFrameNumber
1897 && lastReprocessFrameNumber <= completedReprocessFrameNumber
1898 && lastZslStillFrameNumber <= completedZslStillFrameNumber) {
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001899 sequenceCompleted = true;
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07001900 mCaptureCallbackMap.removeAt(index);
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001901 if (DEBUG) {
1902 Log.v(TAG, String.format(
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001903 "Remove holder for requestId %d, because lastRegularFrame %d "
1904 + "is <= %d, lastReprocessFrame %d is <= %d, "
1905 + "lastZslStillFrame %d is <= %d", requestId,
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001906 lastRegularFrameNumber, completedFrameNumber,
Shuzhen Wangb45449b2019-04-17 13:32:03 -07001907 lastReprocessFrameNumber, completedReprocessFrameNumber,
1908 lastZslStillFrameNumber, completedZslStillFrameNumber));
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001909 }
Jianing Weid2c3a822014-03-27 18:27:43 -07001910 }
1911 }
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001912 }
1913
1914 // If no callback is registered for this requestId or sequence completed, remove it
1915 // from the frame number->request pair because it's not needed anymore.
1916 if (holder == null || sequenceCompleted) {
Jianing Weid2c3a822014-03-27 18:27:43 -07001917 iter.remove();
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001918 }
Jianing Weid2c3a822014-03-27 18:27:43 -07001919
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001920 // Call onCaptureSequenceCompleted
1921 if (sequenceCompleted) {
1922 Runnable resultDispatch = new Runnable() {
1923 @Override
1924 public void run() {
1925 if (!CameraDeviceImpl.this.isClosed()){
1926 if (DEBUG) {
1927 Log.d(TAG, String.format(
1928 "fire sequence complete for request %d",
1929 requestId));
Jianing Weid2c3a822014-03-27 18:27:43 -07001930 }
Jianing Weid2c3a822014-03-27 18:27:43 -07001931
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001932 holder.getCallback().onCaptureSequenceCompleted(
1933 CameraDeviceImpl.this,
1934 requestId,
Chien-Yu Chen4560df72015-04-27 17:21:31 -07001935 requestLastFrameNumbers.getLastFrameNumber());
Chien-Yu Chen5398a672015-03-19 14:48:43 -07001936 }
1937 }
1938 };
Emilian Peev77ca8662018-03-14 17:12:13 +00001939 final long ident = Binder.clearCallingIdentity();
1940 try {
1941 holder.getExecutor().execute(resultDispatch);
1942 } finally {
1943 Binder.restoreCallingIdentity(ident);
1944 }
Jianing Weid2c3a822014-03-27 18:27:43 -07001945 }
1946 }
1947 }
1948
Zhijun Heecb323e2013-07-31 09:40:27 -07001949 public class CameraDeviceCallbacks extends ICameraDeviceCallbacks.Stub {
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001950
Igor Murashkin70725502013-06-25 20:27:06 +00001951 @Override
1952 public IBinder asBinder() {
1953 return this;
1954 }
1955
Igor Murashkin70725502013-06-25 20:27:06 +00001956 @Override
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001957 public void onDeviceError(final int errorCode, CaptureResultExtras resultExtras) {
1958 if (DEBUG) {
1959 Log.d(TAG, String.format(
Ruben Brunke663cb772014-09-16 13:18:31 -07001960 "Device error received, code %d, frame number %d, request ID %d, subseq ID %d",
1961 errorCode, resultExtras.getFrameNumber(), resultExtras.getRequestId(),
1962 resultExtras.getSubsequenceId()));
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001963 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07001964
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07001965 synchronized(mInterfaceLock) {
1966 if (mRemoteDevice == null) {
Igor Murashkin49b2b132014-06-18 19:03:00 -07001967 return; // Camera already closed
1968 }
1969
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001970 switch (errorCode) {
1971 case ERROR_CAMERA_DISCONNECTED:
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00001972 final long ident = Binder.clearCallingIdentity();
1973 try {
1974 CameraDeviceImpl.this.mDeviceExecutor.execute(mCallOnDisconnected);
1975 } finally {
1976 Binder.restoreCallingIdentity(ident);
1977 }
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001978 break;
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07001979 case ERROR_CAMERA_REQUEST:
1980 case ERROR_CAMERA_RESULT:
1981 case ERROR_CAMERA_BUFFER:
1982 onCaptureErrorLocked(errorCode, resultExtras);
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001983 break;
Svet Ganov82f09bc2018-01-12 22:08:40 -08001984 case ERROR_CAMERA_DEVICE:
1985 scheduleNotifyError(StateCallback.ERROR_CAMERA_DEVICE);
1986 break;
1987 case ERROR_CAMERA_DISABLED:
1988 scheduleNotifyError(StateCallback.ERROR_CAMERA_DISABLED);
1989 break;
1990 default:
1991 Log.e(TAG, "Unknown error from camera device: " + errorCode);
1992 scheduleNotifyError(StateCallback.ERROR_CAMERA_SERVICE);
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001993 }
Jianing Wei5a4b02b2014-04-11 09:59:24 -07001994 }
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07001995 }
1996
Svet Ganov82f09bc2018-01-12 22:08:40 -08001997 private void scheduleNotifyError(int code) {
1998 mInError = true;
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00001999 final long ident = Binder.clearCallingIdentity();
2000 try {
2001 CameraDeviceImpl.this.mDeviceExecutor.execute(obtainRunnable(
Yohei Yukawabfcf9a82018-09-22 13:36:47 -07002002 CameraDeviceCallbacks::notifyError, this, code).recycleOnUse());
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00002003 } finally {
2004 Binder.restoreCallingIdentity(ident);
2005 }
Svet Ganov82f09bc2018-01-12 22:08:40 -08002006 }
2007
2008 private void notifyError(int code) {
2009 if (!CameraDeviceImpl.this.isClosed()) {
2010 mDeviceCallback.onError(CameraDeviceImpl.this, code);
2011 }
2012 }
2013
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07002014 @Override
Yin-Chia Yeh8cd12e92017-09-05 18:14:21 -07002015 public void onRepeatingRequestError(long lastFrameNumber, int repeatingRequestId) {
Chien-Yu Chen2da496f2016-04-14 13:33:00 -07002016 if (DEBUG) {
2017 Log.d(TAG, "Repeating request error received. Last frame number is " +
2018 lastFrameNumber);
2019 }
2020
2021 synchronized(mInterfaceLock) {
2022 // Camera is already closed or no repeating request is present.
2023 if (mRemoteDevice == null || mRepeatingRequestId == REQUEST_ID_NONE) {
2024 return; // Camera already closed
2025 }
2026
Shuzhen Wangb45449b2019-04-17 13:32:03 -07002027 checkEarlyTriggerSequenceComplete(mRepeatingRequestId, lastFrameNumber,
2028 mRepeatingRequestTypes);
Yin-Chia Yeh8cd12e92017-09-05 18:14:21 -07002029 // Check if there is already a new repeating request
2030 if (mRepeatingRequestId == repeatingRequestId) {
2031 mRepeatingRequestId = REQUEST_ID_NONE;
Shuzhen Wangb45449b2019-04-17 13:32:03 -07002032 mRepeatingRequestTypes = null;
Yin-Chia Yeh8cd12e92017-09-05 18:14:21 -07002033 }
Chien-Yu Chen2da496f2016-04-14 13:33:00 -07002034 }
2035 }
2036
2037 @Override
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07002038 public void onDeviceIdle() {
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07002039 if (DEBUG) {
2040 Log.d(TAG, "Camera now idle");
2041 }
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07002042 synchronized(mInterfaceLock) {
2043 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -07002044
Igor Murashkin21547d62014-06-04 15:21:42 -07002045 if (!CameraDeviceImpl.this.mIdle) {
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00002046 final long ident = Binder.clearCallingIdentity();
2047 try {
2048 CameraDeviceImpl.this.mDeviceExecutor.execute(mCallOnIdle);
2049 } finally {
2050 Binder.restoreCallingIdentity(ident);
2051 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07002052 }
Igor Murashkin21547d62014-06-04 15:21:42 -07002053 CameraDeviceImpl.this.mIdle = true;
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07002054 }
2055 }
2056
2057 @Override
Jianing Weid2c3a822014-03-27 18:27:43 -07002058 public void onCaptureStarted(final CaptureResultExtras resultExtras, final long timestamp) {
2059 int requestId = resultExtras.getRequestId();
Eino-Ville Talvalae8df3092014-09-08 14:30:50 -07002060 final long frameNumber = resultExtras.getFrameNumber();
2061
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07002062 if (DEBUG) {
Eino-Ville Talvalae8df3092014-09-08 14:30:50 -07002063 Log.d(TAG, "Capture started for id " + requestId + " frame number " + frameNumber);
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07002064 }
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07002065 final CaptureCallbackHolder holder;
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07002066
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07002067 synchronized(mInterfaceLock) {
2068 if (mRemoteDevice == null) return; // Camera already closed
Igor Murashkin49b2b132014-06-18 19:03:00 -07002069
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07002070 // Get the callback for this frame ID, if there is one
2071 holder = CameraDeviceImpl.this.mCaptureCallbackMap.get(requestId);
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07002072
Igor Murashkin49b2b132014-06-18 19:03:00 -07002073 if (holder == null) {
2074 return;
2075 }
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07002076
Igor Murashkin49b2b132014-06-18 19:03:00 -07002077 if (isClosed()) return;
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07002078
Igor Murashkin49b2b132014-06-18 19:03:00 -07002079 // Dispatch capture start notice
Emilian Peev77ca8662018-03-14 17:12:13 +00002080 final long ident = Binder.clearCallingIdentity();
2081 try {
2082 holder.getExecutor().execute(
2083 new Runnable() {
2084 @Override
2085 public void run() {
2086 if (!CameraDeviceImpl.this.isClosed()) {
2087 final int subsequenceId = resultExtras.getSubsequenceId();
2088 final CaptureRequest request = holder.getRequest(subsequenceId);
Shuzhen Wangae10eb92017-03-01 12:58:04 -08002089
Emilian Peev77ca8662018-03-14 17:12:13 +00002090 if (holder.hasBatchedOutputs()) {
2091 // Send derived onCaptureStarted for requests within the
2092 // batch
2093 final Range<Integer> fpsRange =
2094 request.get(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE);
2095 for (int i = 0; i < holder.getRequestCount(); i++) {
2096 holder.getCallback().onCaptureStarted(
2097 CameraDeviceImpl.this,
2098 holder.getRequest(i),
2099 timestamp - (subsequenceId - i) *
2100 NANO_PER_SECOND/fpsRange.getUpper(),
2101 frameNumber - (subsequenceId - i));
2102 }
2103 } else {
Shuzhen Wangae10eb92017-03-01 12:58:04 -08002104 holder.getCallback().onCaptureStarted(
2105 CameraDeviceImpl.this,
Emilian Peev77ca8662018-03-14 17:12:13 +00002106 holder.getRequest(resultExtras.getSubsequenceId()),
2107 timestamp, frameNumber);
Shuzhen Wangae10eb92017-03-01 12:58:04 -08002108 }
Shuzhen Wangae10eb92017-03-01 12:58:04 -08002109 }
Igor Murashkin49b2b132014-06-18 19:03:00 -07002110 }
Emilian Peev77ca8662018-03-14 17:12:13 +00002111 });
2112 } finally {
2113 Binder.restoreCallingIdentity(ident);
2114 }
Igor Murashkin49b2b132014-06-18 19:03:00 -07002115 }
Igor Murashkin70725502013-06-25 20:27:06 +00002116 }
2117
2118 @Override
Jianing Weid2c3a822014-03-27 18:27:43 -07002119 public void onResultReceived(CameraMetadataNative result,
Shuzhen Wang0960fb42018-01-10 20:35:11 -08002120 CaptureResultExtras resultExtras, PhysicalCaptureResultInfo physicalResults[])
2121 throws RemoteException {
Ruben Brunk57493682014-05-27 18:58:08 -07002122
Jianing Weid2c3a822014-03-27 18:27:43 -07002123 int requestId = resultExtras.getRequestId();
Igor Murashkinbdf366c2014-07-25 16:54:20 -07002124 long frameNumber = resultExtras.getFrameNumber();
2125
Zhijun Heecb323e2013-07-31 09:40:27 -07002126 if (DEBUG) {
Igor Murashkinbdf366c2014-07-25 16:54:20 -07002127 Log.v(TAG, "Received result frame " + frameNumber + " for id "
Jianing Weibaf0c652014-04-18 17:35:00 -07002128 + requestId);
Zhijun Heecb323e2013-07-31 09:40:27 -07002129 }
Ruben Brunk57493682014-05-27 18:58:08 -07002130
Eino-Ville Talvala2f75e7d2014-07-15 10:31:54 -07002131 synchronized(mInterfaceLock) {
2132 if (mRemoteDevice == null) return; // Camera already closed
Ruben Brunk57493682014-05-27 18:58:08 -07002133
Igor Murashkin49b2b132014-06-18 19:03:00 -07002134 // TODO: Handle CameraCharacteristics access from CaptureResult correctly.
2135 result.set(CameraCharacteristics.LENS_INFO_SHADING_MAP_SIZE,
2136 getCharacteristics().get(CameraCharacteristics.LENS_INFO_SHADING_MAP_SIZE));
Ruben Brunk57493682014-05-27 18:58:08 -07002137
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07002138 final CaptureCallbackHolder holder =
2139 CameraDeviceImpl.this.mCaptureCallbackMap.get(requestId);
Chien-Yu Chen5398a672015-03-19 14:48:43 -07002140 final CaptureRequest request = holder.getRequest(resultExtras.getSubsequenceId());
Igor Murashkin70725502013-06-25 20:27:06 +00002141
Zhijun He83159152014-07-16 11:32:59 -07002142 boolean isPartialResult =
2143 (resultExtras.getPartialResultCount() < mTotalPartialCount);
Shuzhen Wangb45449b2019-04-17 13:32:03 -07002144 int requestType = request.getRequestType();
Eino-Ville Talvala7a313102013-11-07 14:45:06 -08002145
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07002146 // Check if we have a callback for this
Igor Murashkin49b2b132014-06-18 19:03:00 -07002147 if (holder == null) {
2148 if (DEBUG) {
2149 Log.d(TAG,
2150 "holder is null, early return at frame "
Igor Murashkinbdf366c2014-07-25 16:54:20 -07002151 + frameNumber);
Igor Murashkin49b2b132014-06-18 19:03:00 -07002152 }
Igor Murashkin1e854c52014-08-28 15:21:49 -07002153
Chien-Yu Chen5398a672015-03-19 14:48:43 -07002154 mFrameNumberTracker.updateTracker(frameNumber, /*result*/null, isPartialResult,
Shuzhen Wangb45449b2019-04-17 13:32:03 -07002155 requestType);
Igor Murashkin1e854c52014-08-28 15:21:49 -07002156
Igor Murashkin49b2b132014-06-18 19:03:00 -07002157 return;
Jianing Wei5a4b02b2014-04-11 09:59:24 -07002158 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07002159
Igor Murashkin49b2b132014-06-18 19:03:00 -07002160 if (isClosed()) {
2161 if (DEBUG) {
2162 Log.d(TAG,
2163 "camera is closed, early return at frame "
Igor Murashkinbdf366c2014-07-25 16:54:20 -07002164 + frameNumber);
Eino-Ville Talvala7a313102013-11-07 14:45:06 -08002165 }
Igor Murashkin1e854c52014-08-28 15:21:49 -07002166
Chien-Yu Chen5398a672015-03-19 14:48:43 -07002167 mFrameNumberTracker.updateTracker(frameNumber, /*result*/null, isPartialResult,
Shuzhen Wangb45449b2019-04-17 13:32:03 -07002168 requestType);
Igor Murashkin49b2b132014-06-18 19:03:00 -07002169 return;
2170 }
Igor Murashkindb075af2014-05-21 10:07:08 -07002171
Igor Murashkin49b2b132014-06-18 19:03:00 -07002172
Igor Murashkin49b2b132014-06-18 19:03:00 -07002173 Runnable resultDispatch = null;
2174
Igor Murashkin1e854c52014-08-28 15:21:49 -07002175 CaptureResult finalResult;
Shuzhen Wangae10eb92017-03-01 12:58:04 -08002176 // Make a copy of the native metadata before it gets moved to a CaptureResult
2177 // object.
2178 final CameraMetadataNative resultCopy;
2179 if (holder.hasBatchedOutputs()) {
2180 resultCopy = new CameraMetadataNative(result);
2181 } else {
2182 resultCopy = null;
2183 }
Igor Murashkin1e854c52014-08-28 15:21:49 -07002184
Igor Murashkin49b2b132014-06-18 19:03:00 -07002185 // Either send a partial result or the final capture completed result
Zhijun He83159152014-07-16 11:32:59 -07002186 if (isPartialResult) {
Igor Murashkin49b2b132014-06-18 19:03:00 -07002187 final CaptureResult resultAsCapture =
Igor Murashkinbdf366c2014-07-25 16:54:20 -07002188 new CaptureResult(result, request, resultExtras);
Igor Murashkin49b2b132014-06-18 19:03:00 -07002189 // Partial result
2190 resultDispatch = new Runnable() {
2191 @Override
2192 public void run() {
Shuzhen Wangae10eb92017-03-01 12:58:04 -08002193 if (!CameraDeviceImpl.this.isClosed()) {
2194 if (holder.hasBatchedOutputs()) {
2195 // Send derived onCaptureProgressed for requests within
2196 // the batch.
2197 for (int i = 0; i < holder.getRequestCount(); i++) {
2198 CameraMetadataNative resultLocal =
2199 new CameraMetadataNative(resultCopy);
2200 CaptureResult resultInBatch = new CaptureResult(
2201 resultLocal, holder.getRequest(i), resultExtras);
2202
2203 holder.getCallback().onCaptureProgressed(
2204 CameraDeviceImpl.this,
2205 holder.getRequest(i),
2206 resultInBatch);
2207 }
2208 } else {
2209 holder.getCallback().onCaptureProgressed(
2210 CameraDeviceImpl.this,
2211 request,
2212 resultAsCapture);
2213 }
Igor Murashkin49b2b132014-06-18 19:03:00 -07002214 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07002215 }
Igor Murashkin49b2b132014-06-18 19:03:00 -07002216 };
Igor Murashkin1e854c52014-08-28 15:21:49 -07002217 finalResult = resultAsCapture;
Igor Murashkin49b2b132014-06-18 19:03:00 -07002218 } else {
Igor Murashkin1e854c52014-08-28 15:21:49 -07002219 List<CaptureResult> partialResults =
2220 mFrameNumberTracker.popPartialResults(frameNumber);
2221
Shuzhen Wangae10eb92017-03-01 12:58:04 -08002222 final long sensorTimestamp =
2223 result.get(CaptureResult.SENSOR_TIMESTAMP);
2224 final Range<Integer> fpsRange =
2225 request.get(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE);
2226 final int subsequenceId = resultExtras.getSubsequenceId();
Chien-Yu Chen7a316f6b2015-04-13 14:31:45 -07002227 final TotalCaptureResult resultAsCapture = new TotalCaptureResult(result,
Shuzhen Wang0960fb42018-01-10 20:35:11 -08002228 request, resultExtras, partialResults, holder.getSessionId(),
2229 physicalResults);
Igor Murashkin49b2b132014-06-18 19:03:00 -07002230 // Final capture result
2231 resultDispatch = new Runnable() {
2232 @Override
2233 public void run() {
2234 if (!CameraDeviceImpl.this.isClosed()){
Shuzhen Wangae10eb92017-03-01 12:58:04 -08002235 if (holder.hasBatchedOutputs()) {
2236 // Send derived onCaptureCompleted for requests within
2237 // the batch.
2238 for (int i = 0; i < holder.getRequestCount(); i++) {
2239 resultCopy.set(CaptureResult.SENSOR_TIMESTAMP,
2240 sensorTimestamp - (subsequenceId - i) *
2241 NANO_PER_SECOND/fpsRange.getUpper());
2242 CameraMetadataNative resultLocal =
2243 new CameraMetadataNative(resultCopy);
Shuzhen Wang0960fb42018-01-10 20:35:11 -08002244 // No logical multi-camera support for batched output mode.
Shuzhen Wangae10eb92017-03-01 12:58:04 -08002245 TotalCaptureResult resultInBatch = new TotalCaptureResult(
2246 resultLocal, holder.getRequest(i), resultExtras,
Shuzhen Wang0960fb42018-01-10 20:35:11 -08002247 partialResults, holder.getSessionId(),
2248 new PhysicalCaptureResultInfo[0]);
Shuzhen Wangae10eb92017-03-01 12:58:04 -08002249
2250 holder.getCallback().onCaptureCompleted(
2251 CameraDeviceImpl.this,
2252 holder.getRequest(i),
2253 resultInBatch);
2254 }
2255 } else {
2256 holder.getCallback().onCaptureCompleted(
2257 CameraDeviceImpl.this,
2258 request,
2259 resultAsCapture);
2260 }
Igor Murashkin49b2b132014-06-18 19:03:00 -07002261 }
2262 }
2263 };
Igor Murashkin1e854c52014-08-28 15:21:49 -07002264 finalResult = resultAsCapture;
Igor Murashkin49b2b132014-06-18 19:03:00 -07002265 }
Jianing Weid2c3a822014-03-27 18:27:43 -07002266
Emilian Peev77ca8662018-03-14 17:12:13 +00002267 final long ident = Binder.clearCallingIdentity();
2268 try {
2269 holder.getExecutor().execute(resultDispatch);
2270 } finally {
2271 Binder.restoreCallingIdentity(ident);
2272 }
Igor Murashkin49b2b132014-06-18 19:03:00 -07002273
Igor Murashkin1e854c52014-08-28 15:21:49 -07002274 // Collect the partials for a total result; or mark the frame as totally completed
Chien-Yu Chen7a316f6b2015-04-13 14:31:45 -07002275 mFrameNumberTracker.updateTracker(frameNumber, finalResult, isPartialResult,
Shuzhen Wangb45449b2019-04-17 13:32:03 -07002276 requestType);
Igor Murashkin1e854c52014-08-28 15:21:49 -07002277
Igor Murashkin49b2b132014-06-18 19:03:00 -07002278 // Fire onCaptureSequenceCompleted
Zhijun He83159152014-07-16 11:32:59 -07002279 if (!isPartialResult) {
Igor Murashkin49b2b132014-06-18 19:03:00 -07002280 checkAndFireSequenceComplete();
2281 }
Jianing Weid2c3a822014-03-27 18:27:43 -07002282 }
Igor Murashkin70725502013-06-25 20:27:06 +00002283 }
2284
Eino-Ville Talvalaad916f72015-04-11 12:09:11 -07002285 @Override
2286 public void onPrepared(int streamId) {
2287 final OutputConfiguration output;
2288 final StateCallbackKK sessionCallback;
2289
2290 if (DEBUG) {
2291 Log.v(TAG, "Stream " + streamId + " is prepared");
2292 }
2293
2294 synchronized(mInterfaceLock) {
2295 output = mConfiguredOutputs.get(streamId);
2296 sessionCallback = mSessionStateCallback;
2297 }
2298
2299 if (sessionCallback == null) return;
2300
2301 if (output == null) {
2302 Log.w(TAG, "onPrepared invoked for unknown output Surface");
2303 return;
2304 }
Shuzhen Wange0a66672017-03-21 12:05:14 -07002305 final List<Surface> surfaces = output.getSurfaces();
2306 for (Surface surface : surfaces) {
2307 sessionCallback.onSurfacePrepared(surface);
2308 }
Eino-Ville Talvalaad916f72015-04-11 12:09:11 -07002309 }
2310
Shuzhen Wang88f1af22016-09-30 10:29:28 -07002311 @Override
2312 public void onRequestQueueEmpty() {
2313 final StateCallbackKK sessionCallback;
2314
2315 if (DEBUG) {
2316 Log.v(TAG, "Request queue becomes empty");
2317 }
2318
2319 synchronized(mInterfaceLock) {
2320 sessionCallback = mSessionStateCallback;
2321 }
2322
2323 if (sessionCallback == null) return;
2324
2325 sessionCallback.onRequestQueueEmpty();
2326 }
2327
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07002328 /**
2329 * Called by onDeviceError for handling single-capture failures.
2330 */
2331 private void onCaptureErrorLocked(int errorCode, CaptureResultExtras resultExtras) {
2332
2333 final int requestId = resultExtras.getRequestId();
2334 final int subsequenceId = resultExtras.getSubsequenceId();
2335 final long frameNumber = resultExtras.getFrameNumber();
Emilian Peevd0e96e32019-03-19 17:54:33 -07002336 final String errorPhysicalCameraId = resultExtras.getErrorPhysicalCameraId();
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07002337 final CaptureCallbackHolder holder =
2338 CameraDeviceImpl.this.mCaptureCallbackMap.get(requestId);
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07002339
2340 final CaptureRequest request = holder.getRequest(subsequenceId);
2341
Eino-Ville Talvala3e0023a2016-03-06 18:40:01 -08002342 Runnable failureDispatch = null;
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07002343 if (errorCode == ERROR_CAMERA_BUFFER) {
Shuzhen Wang9c663d42016-10-28 23:25:02 -07002344 // Because 1 stream id could map to multiple surfaces, we need to specify both
2345 // streamId and surfaceId.
2346 List<Surface> surfaces =
2347 mConfiguredOutputs.get(resultExtras.getErrorStreamId()).getSurfaces();
2348 for (Surface surface : surfaces) {
2349 if (!request.containsTarget(surface)) {
2350 continue;
Eino-Ville Talvala3e0023a2016-03-06 18:40:01 -08002351 }
Shuzhen Wang9c663d42016-10-28 23:25:02 -07002352 if (DEBUG) {
2353 Log.v(TAG, String.format("Lost output buffer reported for frame %d, target %s",
2354 frameNumber, surface));
2355 }
2356 failureDispatch = new Runnable() {
2357 @Override
2358 public void run() {
2359 if (!CameraDeviceImpl.this.isClosed()){
2360 holder.getCallback().onCaptureBufferLost(
2361 CameraDeviceImpl.this,
2362 request,
2363 surface,
2364 frameNumber);
2365 }
2366 }
2367 };
2368 // Dispatch the failure callback
Emilian Peev77ca8662018-03-14 17:12:13 +00002369 final long ident = Binder.clearCallingIdentity();
2370 try {
2371 holder.getExecutor().execute(failureDispatch);
2372 } finally {
2373 Binder.restoreCallingIdentity(ident);
2374 }
Shuzhen Wang9c663d42016-10-28 23:25:02 -07002375 }
Eino-Ville Talvala3e0023a2016-03-06 18:40:01 -08002376 } else {
2377 boolean mayHaveBuffers = (errorCode == ERROR_CAMERA_RESULT);
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07002378
Eino-Ville Talvala3e0023a2016-03-06 18:40:01 -08002379 // This is only approximate - exact handling needs the camera service and HAL to
2380 // disambiguate between request failures to due abort and due to real errors. For
2381 // now, assume that if the session believes we're mid-abort, then the error is due
2382 // to abort.
2383 int reason = (mCurrentSession != null && mCurrentSession.isAborting()) ?
2384 CaptureFailure.REASON_FLUSHED :
2385 CaptureFailure.REASON_ERROR;
2386
2387 final CaptureFailure failure = new CaptureFailure(
2388 request,
2389 reason,
2390 /*dropped*/ mayHaveBuffers,
2391 requestId,
Emilian Peevd0e96e32019-03-19 17:54:33 -07002392 frameNumber,
2393 errorPhysicalCameraId);
Eino-Ville Talvala3e0023a2016-03-06 18:40:01 -08002394
2395 failureDispatch = new Runnable() {
2396 @Override
2397 public void run() {
2398 if (!CameraDeviceImpl.this.isClosed()){
2399 holder.getCallback().onCaptureFailed(
2400 CameraDeviceImpl.this,
2401 request,
2402 failure);
2403 }
2404 }
2405 };
2406
2407 // Fire onCaptureSequenceCompleted if appropriate
2408 if (DEBUG) {
2409 Log.v(TAG, String.format("got error frame %d", frameNumber));
2410 }
Shuzhen Wangb45449b2019-04-17 13:32:03 -07002411 mFrameNumberTracker.updateTracker(frameNumber,
2412 /*error*/true, request.getRequestType());
Eino-Ville Talvala3e0023a2016-03-06 18:40:01 -08002413 checkAndFireSequenceComplete();
Shuzhen Wang9c663d42016-10-28 23:25:02 -07002414
2415 // Dispatch the failure callback
Emilian Peev77ca8662018-03-14 17:12:13 +00002416 final long ident = Binder.clearCallingIdentity();
2417 try {
2418 holder.getExecutor().execute(failureDispatch);
2419 } finally {
2420 Binder.restoreCallingIdentity(ident);
2421 }
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07002422 }
Eino-Ville Talvala3e0023a2016-03-06 18:40:01 -08002423
Eino-Ville Talvalaacc00952014-08-06 14:31:08 -07002424 }
2425
2426 } // public class CameraDeviceCallbacks
Igor Murashkin70725502013-06-25 20:27:06 +00002427
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07002428 /**
Emilian Peevb9a51942018-03-14 17:18:49 +00002429 * A camera specific adapter {@link Executor} that posts all executed tasks onto the given
2430 * {@link Handler}.
2431 *
2432 * @hide
2433 */
2434 private static class CameraHandlerExecutor implements Executor {
2435 private final Handler mHandler;
2436
2437 public CameraHandlerExecutor(@NonNull Handler handler) {
2438 mHandler = Preconditions.checkNotNull(handler);
2439 }
2440
2441 @Override
2442 public void execute(Runnable command) {
2443 // Return value of 'post()' will be ignored in order to keep the
2444 // same camera behavior. For further details see b/74605221 .
2445 mHandler.post(command);
2446 }
2447 }
2448
2449 /**
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00002450 * Default executor management.
Emilian Peev77ca8662018-03-14 17:12:13 +00002451 *
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00002452 * <p>
2453 * If executor is null, get the current thread's
2454 * Looper to create a Executor with. If no looper exists, throw
2455 * {@code IllegalArgumentException}.
2456 * </p>
Emilian Peev77ca8662018-03-14 17:12:13 +00002457 */
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00002458 static Executor checkExecutor(Executor executor) {
2459 return (executor == null) ? checkAndWrapHandler(null) : executor;
2460 }
Emilian Peev77ca8662018-03-14 17:12:13 +00002461
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00002462 /**
2463 * Default executor management.
2464 *
2465 * <p>If the callback isn't null, check the executor, otherwise pass it through.</p>
2466 */
Emilian Peev2f0184f2018-03-21 11:57:33 +00002467 public static <T> Executor checkExecutor(Executor executor, T callback) {
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00002468 return (callback != null) ? checkExecutor(executor) : executor;
Emilian Peev77ca8662018-03-14 17:12:13 +00002469 }
2470
2471 /**
2472 * Wrap Handler in Executor.
2473 *
2474 * <p>
2475 * If handler is null, get the current thread's
2476 * Looper to create a Executor with. If no looper exists, throw
2477 * {@code IllegalArgumentException}.
2478 * </p>
2479 */
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00002480 public static Executor checkAndWrapHandler(Handler handler) {
Emilian Peevb9a51942018-03-14 17:18:49 +00002481 return new CameraHandlerExecutor(checkHandler(handler));
Emilian Peev77ca8662018-03-14 17:12:13 +00002482 }
2483
2484 /**
Igor Murashkin0b27d342014-05-30 09:45:05 -07002485 * Default handler management.
2486 *
2487 * <p>
2488 * If handler is null, get the current thread's
2489 * Looper to create a Handler with. If no looper exists, throw {@code IllegalArgumentException}.
2490 * </p>
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07002491 */
Igor Murashkin0b27d342014-05-30 09:45:05 -07002492 static Handler checkHandler(Handler handler) {
Eino-Ville Talvalae841d4e2013-09-05 09:04:08 -07002493 if (handler == null) {
2494 Looper looper = Looper.myLooper();
2495 if (looper == null) {
2496 throw new IllegalArgumentException(
2497 "No handler given, and current thread has no looper!");
2498 }
2499 handler = new Handler(looper);
2500 }
2501 return handler;
2502 }
2503
Eino-Ville Talvala7875a882014-07-31 12:47:07 -07002504 /**
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07002505 * Default handler management, conditional on there being a callback.
Eino-Ville Talvala7875a882014-07-31 12:47:07 -07002506 *
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07002507 * <p>If the callback isn't null, check the handler, otherwise pass it through.</p>
Eino-Ville Talvala7875a882014-07-31 12:47:07 -07002508 */
Eino-Ville Talvalafd887432014-09-04 13:07:40 -07002509 static <T> Handler checkHandler(Handler handler, T callback) {
2510 if (callback != null) {
Eino-Ville Talvala7875a882014-07-31 12:47:07 -07002511 return checkHandler(handler);
2512 }
2513 return handler;
2514 }
2515
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -07002516 private void checkIfCameraClosedOrInError() throws CameraAccessException {
Ruben Brunka45aa0d2015-06-03 19:40:22 -07002517 if (mRemoteDevice == null) {
2518 throw new IllegalStateException("CameraDevice was already closed");
2519 }
Eino-Ville Talvala7fcb35782014-06-03 18:30:27 -07002520 if (mInError) {
2521 throw new CameraAccessException(CameraAccessException.CAMERA_ERROR,
2522 "The camera device has encountered a serious error");
2523 }
Zhijun He7f4d3142013-07-23 07:54:38 -07002524 }
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07002525
Igor Murashkin49b2b132014-06-18 19:03:00 -07002526 /** Whether the camera device has started to close (may not yet have finished) */
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07002527 private boolean isClosed() {
Ruben Brunk0953b642015-06-11 16:12:35 -07002528 return mClosing.get();
Eino-Ville Talvala868d9042013-10-03 11:15:21 -07002529 }
Ruben Brunk57493682014-05-27 18:58:08 -07002530
2531 private CameraCharacteristics getCharacteristics() {
2532 return mCharacteristics;
2533 }
Zhijun Heb1300e32015-05-28 12:51:52 -07002534
Shuzhen Wang78392932016-03-04 15:26:01 -08002535 /**
2536 * Listener for binder death.
2537 *
2538 * <p> Handle binder death for ICameraDeviceUser. Trigger onError.</p>
2539 */
Zhijun Hec8b181e2016-05-30 14:54:39 -07002540 @Override
Shuzhen Wang78392932016-03-04 15:26:01 -08002541 public void binderDied() {
2542 Log.w(TAG, "CameraDevice " + mCameraId + " died unexpectedly");
2543
2544 if (mRemoteDevice == null) {
2545 return; // Camera already closed
2546 }
2547
2548 mInError = true;
2549 Runnable r = new Runnable() {
2550 @Override
2551 public void run() {
2552 if (!isClosed()) {
2553 mDeviceCallback.onError(CameraDeviceImpl.this,
Eino-Ville Talvala50eebe02016-06-09 17:07:13 -07002554 StateCallback.ERROR_CAMERA_SERVICE);
Shuzhen Wang78392932016-03-04 15:26:01 -08002555 }
2556 }
2557 };
Emilian Peev9a0ec8c2018-02-28 14:53:30 +00002558 final long ident = Binder.clearCallingIdentity();
2559 try {
2560 CameraDeviceImpl.this.mDeviceExecutor.execute(r);
2561 } finally {
2562 Binder.restoreCallingIdentity(ident);
2563 }
Shuzhen Wang78392932016-03-04 15:26:01 -08002564 }
Igor Murashkin70725502013-06-25 20:27:06 +00002565}