Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package android.hardware.camera2.impl; |
| 17 | |
| 18 | import android.hardware.camera2.CameraAccessException; |
| 19 | import android.hardware.camera2.CameraCaptureSession; |
| 20 | import android.hardware.camera2.CameraDevice; |
| 21 | import android.hardware.camera2.CaptureRequest; |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 22 | import android.hardware.camera2.dispatch.ArgumentReplacingDispatcher; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 23 | import android.hardware.camera2.dispatch.BroadcastDispatcher; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 24 | import android.hardware.camera2.dispatch.DuckTypingDispatcher; |
| 25 | import android.hardware.camera2.dispatch.HandlerDispatcher; |
| 26 | import android.hardware.camera2.dispatch.InvokeDispatcher; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 27 | import android.hardware.camera2.utils.TaskDrainer; |
| 28 | import android.hardware.camera2.utils.TaskSingleDrainer; |
| 29 | import android.os.Handler; |
| 30 | import android.util.Log; |
| 31 | import android.view.Surface; |
| 32 | |
| 33 | import java.util.Arrays; |
| 34 | import java.util.List; |
| 35 | |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 36 | import static android.hardware.camera2.impl.CameraDeviceImpl.checkHandler; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 37 | import static com.android.internal.util.Preconditions.*; |
| 38 | |
| 39 | public class CameraCaptureSessionImpl extends CameraCaptureSession { |
| 40 | private static final String TAG = "CameraCaptureSession"; |
| 41 | private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); |
| 42 | |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 43 | /** Simple integer ID for session for debugging */ |
| 44 | private final int mId; |
| 45 | private final String mIdString; |
| 46 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 47 | /** Input surface configured by native camera framework based on user-specified configuration */ |
| 48 | private final Surface mInput; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 49 | /** User-specified set of surfaces used as the configuration outputs */ |
| 50 | private final List<Surface> mOutputs; |
| 51 | /** |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 52 | * User-specified state callback, used for outgoing events; calls to this object will be |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 53 | * automatically {@link Handler#post(Runnable) posted} to {@code mStateHandler}. |
| 54 | */ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 55 | private final CameraCaptureSession.StateCallback mStateCallback; |
| 56 | /** User-specified state handler used for outgoing state callback events */ |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 57 | private final Handler mStateHandler; |
| 58 | |
| 59 | /** Internal camera device; used to translate calls into existing deprecated API */ |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 60 | private final android.hardware.camera2.impl.CameraDeviceImpl mDeviceImpl; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 61 | /** Internal handler; used for all incoming events to preserve total order */ |
| 62 | private final Handler mDeviceHandler; |
| 63 | |
| 64 | /** Drain Sequence IDs which have been queued but not yet finished with aborted/completed */ |
| 65 | private final TaskDrainer<Integer> mSequenceDrainer; |
| 66 | /** Drain state transitions from ACTIVE -> IDLE */ |
| 67 | private final TaskSingleDrainer mIdleDrainer; |
| 68 | /** Drain state transitions from BUSY -> IDLE */ |
| 69 | private final TaskSingleDrainer mAbortDrainer; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 70 | |
| 71 | /** This session is closed; all further calls will throw ISE */ |
| 72 | private boolean mClosed = false; |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 73 | /** This session failed to be configured successfully */ |
| 74 | private final boolean mConfigureSuccess; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 75 | /** Do not unconfigure if this is set; another session will overwrite configuration */ |
| 76 | private boolean mSkipUnconfigure = false; |
| 77 | |
| 78 | /** Is the session in the process of aborting? Pay attention to BUSY->IDLE transitions. */ |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 79 | private volatile boolean mAborting; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Create a new CameraCaptureSession. |
| 83 | * |
| 84 | * <p>The camera device must already be in the {@code IDLE} state when this is invoked. |
| 85 | * There must be no pending actions |
| 86 | * (e.g. no pending captures, no repeating requests, no flush).</p> |
| 87 | */ |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 88 | CameraCaptureSessionImpl(int id, Surface input, List<Surface> outputs, |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 89 | CameraCaptureSession.StateCallback callback, Handler stateHandler, |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 90 | android.hardware.camera2.impl.CameraDeviceImpl deviceImpl, |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 91 | Handler deviceStateHandler, boolean configureSuccess) { |
| 92 | if (outputs == null || outputs.isEmpty()) { |
| 93 | throw new IllegalArgumentException("outputs must be a non-null, non-empty list"); |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 94 | } else if (callback == null) { |
| 95 | throw new IllegalArgumentException("callback must not be null"); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 98 | mId = id; |
| 99 | mIdString = String.format("Session %d: ", mId); |
| 100 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 101 | // TODO: extra verification of outputs |
| 102 | mOutputs = outputs; |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 103 | mInput = input; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 104 | mStateHandler = checkHandler(stateHandler); |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 105 | mStateCallback = createUserStateCallbackProxy(mStateHandler, callback); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 106 | |
| 107 | mDeviceHandler = checkNotNull(deviceStateHandler, "deviceStateHandler must not be null"); |
| 108 | mDeviceImpl = checkNotNull(deviceImpl, "deviceImpl must not be null"); |
| 109 | |
| 110 | /* |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 111 | * Use the same handler as the device's StateCallback for all the internal coming events |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 112 | * |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 113 | * This ensures total ordering between CameraDevice.StateCallback and |
| 114 | * CameraDeviceImpl.CaptureCallback events. |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 115 | */ |
| 116 | mSequenceDrainer = new TaskDrainer<>(mDeviceHandler, new SequenceDrainListener(), |
| 117 | /*name*/"seq"); |
| 118 | mIdleDrainer = new TaskSingleDrainer(mDeviceHandler, new IdleDrainListener(), |
| 119 | /*name*/"idle"); |
| 120 | mAbortDrainer = new TaskSingleDrainer(mDeviceHandler, new AbortDrainListener(), |
| 121 | /*name*/"abort"); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 122 | |
| 123 | // CameraDevice should call configureOutputs and have it finish before constructing us |
| 124 | |
| 125 | if (configureSuccess) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 126 | mStateCallback.onConfigured(this); |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 127 | if (VERBOSE) Log.v(TAG, mIdString + "Created session successfully"); |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 128 | mConfigureSuccess = true; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 129 | } else { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 130 | mStateCallback.onConfigureFailed(this); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 131 | mClosed = true; // do not fire any other callbacks, do not allow any other work |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 132 | Log.e(TAG, mIdString + "Failed to create capture session; configuration failed"); |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 133 | mConfigureSuccess = false; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public CameraDevice getDevice() { |
| 139 | return mDeviceImpl; |
| 140 | } |
| 141 | |
| 142 | @Override |
Eino-Ville Talvala | ad916f7 | 2015-04-11 12:09:11 -0700 | [diff] [blame] | 143 | public void prepare(Surface surface) throws CameraAccessException { |
| 144 | mDeviceImpl.prepare(surface); |
| 145 | } |
| 146 | |
| 147 | @Override |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 148 | public synchronized int capture(CaptureRequest request, CaptureCallback callback, |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 149 | Handler handler) throws CameraAccessException { |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 150 | if (request == null) { |
| 151 | throw new IllegalArgumentException("request must not be null"); |
Chien-Yu Chen | 8062d31 | 2015-05-12 14:24:10 -0700 | [diff] [blame] | 152 | } else if (request.isReprocess() && !isReprocessable()) { |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 153 | throw new IllegalArgumentException("this capture session cannot handle reprocess " + |
| 154 | "requests"); |
Chien-Yu Chen | 8062d31 | 2015-05-12 14:24:10 -0700 | [diff] [blame] | 155 | } else if (request.isReprocess() && request.getReprocessableSessionId() != mId) { |
Chien-Yu Chen | 7a316f6b | 2015-04-13 14:31:45 -0700 | [diff] [blame] | 156 | throw new IllegalArgumentException("capture request was created for another session"); |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 159 | checkNotClosed(); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 160 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 161 | handler = checkHandler(handler, callback); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 162 | |
| 163 | if (VERBOSE) { |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 164 | Log.v(TAG, mIdString + "capture - request " + request + ", callback " + callback + |
| 165 | " handler " + handler); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | return addPendingSequence(mDeviceImpl.capture(request, |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 169 | createCaptureCallbackProxy(handler, callback), mDeviceHandler)); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 173 | public synchronized int captureBurst(List<CaptureRequest> requests, CaptureCallback callback, |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 174 | Handler handler) throws CameraAccessException { |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 175 | if (requests == null) { |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 176 | throw new IllegalArgumentException("Requests must not be null"); |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 177 | } else if (requests.isEmpty()) { |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 178 | throw new IllegalArgumentException("Requests must have at least one element"); |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 181 | for (CaptureRequest request : requests) { |
| 182 | if (request.isReprocess()) { |
Chien-Yu Chen | 8062d31 | 2015-05-12 14:24:10 -0700 | [diff] [blame] | 183 | if (!isReprocessable()) { |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 184 | throw new IllegalArgumentException("This capture session cannot handle " + |
| 185 | "reprocess requests"); |
Chien-Yu Chen | 8062d31 | 2015-05-12 14:24:10 -0700 | [diff] [blame] | 186 | } else if (request.getReprocessableSessionId() != mId) { |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 187 | throw new IllegalArgumentException("Capture request was created for another " + |
| 188 | "session"); |
| 189 | } |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 193 | checkNotClosed(); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 194 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 195 | handler = checkHandler(handler, callback); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 196 | |
| 197 | if (VERBOSE) { |
| 198 | CaptureRequest[] requestArray = requests.toArray(new CaptureRequest[0]); |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 199 | Log.v(TAG, mIdString + "captureBurst - requests " + Arrays.toString(requestArray) + |
| 200 | ", callback " + callback + " handler " + handler); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | return addPendingSequence(mDeviceImpl.captureBurst(requests, |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 204 | createCaptureCallbackProxy(handler, callback), mDeviceHandler)); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 208 | public synchronized int setRepeatingRequest(CaptureRequest request, CaptureCallback callback, |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 209 | Handler handler) throws CameraAccessException { |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 210 | if (request == null) { |
| 211 | throw new IllegalArgumentException("request must not be null"); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 212 | } else if (request.isReprocess()) { |
| 213 | throw new IllegalArgumentException("repeating reprocess requests are not supported"); |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 216 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 217 | checkNotClosed(); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 218 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 219 | handler = checkHandler(handler, callback); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 220 | |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 221 | if (VERBOSE) { |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 222 | Log.v(TAG, mIdString + "setRepeatingRequest - request " + request + ", callback " + |
| 223 | callback + " handler" + " " + handler); |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 226 | return addPendingSequence(mDeviceImpl.setRepeatingRequest(request, |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 227 | createCaptureCallbackProxy(handler, callback), mDeviceHandler)); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public synchronized int setRepeatingBurst(List<CaptureRequest> requests, |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 232 | CaptureCallback callback, Handler handler) throws CameraAccessException { |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 233 | if (requests == null) { |
| 234 | throw new IllegalArgumentException("requests must not be null"); |
| 235 | } else if (requests.isEmpty()) { |
| 236 | throw new IllegalArgumentException("requests must have at least one element"); |
| 237 | } |
| 238 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 239 | for (CaptureRequest r : requests) { |
| 240 | if (r.isReprocess()) { |
| 241 | throw new IllegalArgumentException("repeating reprocess burst requests are not " + |
| 242 | "supported"); |
| 243 | } |
| 244 | } |
| 245 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 246 | checkNotClosed(); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 247 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 248 | handler = checkHandler(handler, callback); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 249 | |
| 250 | if (VERBOSE) { |
| 251 | CaptureRequest[] requestArray = requests.toArray(new CaptureRequest[0]); |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 252 | Log.v(TAG, mIdString + "setRepeatingBurst - requests " + Arrays.toString(requestArray) + |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 253 | ", callback " + callback + " handler" + "" + handler); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | return addPendingSequence(mDeviceImpl.setRepeatingBurst(requests, |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 257 | createCaptureCallbackProxy(handler, callback), mDeviceHandler)); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public synchronized void stopRepeating() throws CameraAccessException { |
| 262 | checkNotClosed(); |
| 263 | |
| 264 | if (VERBOSE) { |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 265 | Log.v(TAG, mIdString + "stopRepeating"); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | mDeviceImpl.stopRepeating(); |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public synchronized void abortCaptures() throws CameraAccessException { |
| 273 | checkNotClosed(); |
| 274 | |
| 275 | if (VERBOSE) { |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 276 | Log.v(TAG, mIdString + "abortCaptures"); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | if (mAborting) { |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 280 | Log.w(TAG, mIdString + "abortCaptures - Session is already aborting; doing nothing"); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 281 | return; |
| 282 | } |
| 283 | |
| 284 | mAborting = true; |
| 285 | mAbortDrainer.taskStarted(); |
| 286 | |
| 287 | mDeviceImpl.flush(); |
| 288 | // The next BUSY -> IDLE set of transitions will mark the end of the abort. |
| 289 | } |
| 290 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 291 | @Override |
Chien-Yu Chen | 8062d31 | 2015-05-12 14:24:10 -0700 | [diff] [blame] | 292 | public boolean isReprocessable() { |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 293 | return mInput != null; |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public Surface getInputSurface() { |
| 298 | return mInput; |
| 299 | } |
| 300 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 301 | /** |
| 302 | * Replace this session with another session. |
| 303 | * |
| 304 | * <p>This is an optimization to avoid unconfiguring and then immediately having to |
| 305 | * reconfigure again.</p> |
| 306 | * |
| 307 | * <p>The semantics are identical to {@link #close}, except that unconfiguring will be skipped. |
| 308 | * <p> |
| 309 | * |
Eino-Ville Talvala | d3b85f6 | 2014-08-05 15:02:31 -0700 | [diff] [blame] | 310 | * <p>After this call completes, the session will not call any further methods on the camera |
| 311 | * device.</p> |
| 312 | * |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 313 | * @see CameraCaptureSession#close |
| 314 | */ |
Eino-Ville Talvala | d3b85f6 | 2014-08-05 15:02:31 -0700 | [diff] [blame] | 315 | synchronized void replaceSessionClose() { |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 316 | /* |
| 317 | * In order for creating new sessions to be fast, the new session should be created |
| 318 | * before the old session is closed. |
| 319 | * |
| 320 | * Otherwise the old session will always unconfigure if there is no new session to |
| 321 | * replace it. |
| 322 | * |
| 323 | * Unconfiguring could add hundreds of milliseconds of delay. We could race and attempt |
| 324 | * to skip unconfigure if a new session is created before the captures are all drained, |
| 325 | * but this would introduce nondeterministic behavior. |
| 326 | */ |
| 327 | |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 328 | if (VERBOSE) Log.v(TAG, mIdString + "replaceSessionClose"); |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 329 | |
Eino-Ville Talvala | d3b85f6 | 2014-08-05 15:02:31 -0700 | [diff] [blame] | 330 | // Set up fast shutdown. Possible alternative paths: |
| 331 | // - This session is active, so close() below starts the shutdown drain |
| 332 | // - This session is mid-shutdown drain, and hasn't yet reached the idle drain listener. |
| 333 | // - This session is already closed and has executed the idle drain listener, and |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 334 | // configureOutputsChecked(null) has already been called. |
Eino-Ville Talvala | d3b85f6 | 2014-08-05 15:02:31 -0700 | [diff] [blame] | 335 | // |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 336 | // Do not call configureOutputsChecked(null) going forward, since it would race with the |
Eino-Ville Talvala | d3b85f6 | 2014-08-05 15:02:31 -0700 | [diff] [blame] | 337 | // configuration for the new session. If it was already called, then we don't care, since it |
| 338 | // won't get called again. |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 339 | mSkipUnconfigure = true; |
Eino-Ville Talvala | d3b85f6 | 2014-08-05 15:02:31 -0700 | [diff] [blame] | 340 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 341 | close(); |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public synchronized void close() { |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 346 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 347 | if (mClosed) { |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 348 | if (VERBOSE) Log.v(TAG, mIdString + "close - reentering"); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 349 | return; |
| 350 | } |
| 351 | |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 352 | if (VERBOSE) Log.v(TAG, mIdString + "close - first time"); |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 353 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 354 | mClosed = true; |
| 355 | |
| 356 | /* |
| 357 | * Flush out any repeating request. Since camera is closed, no new requests |
| 358 | * can be queued, and eventually the entire request queue will be drained. |
| 359 | * |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 360 | * If the camera device was already closed, short circuit and do nothing; since |
| 361 | * no more internal device callbacks will fire anyway. |
| 362 | * |
| 363 | * Otherwise, once stopRepeating is done, wait for camera to idle, then unconfigure the |
| 364 | * camera. Once that's done, fire #onClosed. |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 365 | */ |
| 366 | try { |
| 367 | mDeviceImpl.stopRepeating(); |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 368 | } catch (IllegalStateException e) { |
| 369 | // OK: Camera device may already be closed, nothing else to do |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 370 | Log.w(TAG, mIdString + "The camera device was already closed: ", e); |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 371 | |
| 372 | // TODO: Fire onClosed anytime we get the device onClosed or the ISE? |
| 373 | // or just suppress the ISE only and rely onClosed. |
| 374 | // Also skip any of the draining work if this is already closed. |
| 375 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 376 | // Short-circuit; queue callback immediately and return |
| 377 | mStateCallback.onClosed(this); |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 378 | return; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 379 | } catch (CameraAccessException e) { |
| 380 | // OK: close does not throw checked exceptions. |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 381 | Log.e(TAG, mIdString + "Exception while stopping repeating: ", e); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 382 | |
| 383 | // TODO: call onError instead of onClosed if this happens |
| 384 | } |
| 385 | |
| 386 | // If no sequences are pending, fire #onClosed immediately |
| 387 | mSequenceDrainer.beginDrain(); |
| 388 | } |
| 389 | |
| 390 | /** |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 391 | * Whether currently in mid-abort. |
| 392 | * |
| 393 | * <p>This is used by the implementation to set the capture failure |
| 394 | * reason, in lieu of more accurate error codes from the camera service. |
| 395 | * Unsynchronized to avoid deadlocks between simultaneous session->device, |
| 396 | * device->session calls.</p> |
| 397 | * |
| 398 | * <p>Package-private.</p> |
| 399 | */ |
| 400 | boolean isAborting() { |
| 401 | return mAborting; |
| 402 | } |
| 403 | |
| 404 | /** |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 405 | * Post calls into a CameraCaptureSession.StateCallback to the user-specified {@code handler}. |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 406 | */ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 407 | private StateCallback createUserStateCallbackProxy(Handler handler, StateCallback callback) { |
| 408 | InvokeDispatcher<StateCallback> userCallbackSink = new InvokeDispatcher<>(callback); |
| 409 | HandlerDispatcher<StateCallback> handlerPassthrough = |
| 410 | new HandlerDispatcher<>(userCallbackSink, handler); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 411 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 412 | return new CallbackProxies.SessionStateCallbackProxy(handlerPassthrough); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Forward callbacks from |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 417 | * CameraDeviceImpl.CaptureCallback to the CameraCaptureSession.CaptureCallback. |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 418 | * |
| 419 | * <p>In particular, all calls are automatically split to go both to our own |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 420 | * internal callback, and to the user-specified callback (by transparently posting |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 421 | * to the user-specified handler).</p> |
| 422 | * |
| 423 | * <p>When a capture sequence finishes, update the pending checked sequences set.</p> |
| 424 | */ |
| 425 | @SuppressWarnings("deprecation") |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 426 | private CameraDeviceImpl.CaptureCallback createCaptureCallbackProxy( |
| 427 | Handler handler, CaptureCallback callback) { |
| 428 | CameraDeviceImpl.CaptureCallback localCallback = new CameraDeviceImpl.CaptureCallback() { |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 429 | @Override |
| 430 | public void onCaptureSequenceCompleted(CameraDevice camera, |
| 431 | int sequenceId, long frameNumber) { |
| 432 | finishPendingSequence(sequenceId); |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public void onCaptureSequenceAborted(CameraDevice camera, |
| 437 | int sequenceId) { |
| 438 | finishPendingSequence(sequenceId); |
| 439 | } |
| 440 | }; |
| 441 | |
| 442 | /* |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 443 | * Split the calls from the device callback into local callback and the following chain: |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 444 | * - replace the first CameraDevice arg with a CameraCaptureSession |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 445 | * - duck type from device callback to session callback |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 446 | * - then forward the call to a handler |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 447 | * - then finally invoke the destination method on the session callback object |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 448 | */ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 449 | if (callback == null) { |
| 450 | // OK: API allows the user to not specify a callback, and the handler may |
Eino-Ville Talvala | 7875a88 | 2014-07-31 12:47:07 -0700 | [diff] [blame] | 451 | // also be null in that case. Collapse whole dispatch chain to only call the local |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 452 | // callback |
| 453 | return localCallback; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 456 | InvokeDispatcher<CameraDeviceImpl.CaptureCallback> localSink = |
| 457 | new InvokeDispatcher<>(localCallback); |
Eino-Ville Talvala | 7875a88 | 2014-07-31 12:47:07 -0700 | [diff] [blame] | 458 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 459 | InvokeDispatcher<CaptureCallback> userCallbackSink = |
| 460 | new InvokeDispatcher<>(callback); |
| 461 | HandlerDispatcher<CaptureCallback> handlerPassthrough = |
| 462 | new HandlerDispatcher<>(userCallbackSink, handler); |
| 463 | DuckTypingDispatcher<CameraDeviceImpl.CaptureCallback, CaptureCallback> duckToSession |
| 464 | = new DuckTypingDispatcher<>(handlerPassthrough, CaptureCallback.class); |
| 465 | ArgumentReplacingDispatcher<CameraDeviceImpl.CaptureCallback, CameraCaptureSessionImpl> |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 466 | replaceDeviceWithSession = new ArgumentReplacingDispatcher<>(duckToSession, |
| 467 | /*argumentIndex*/0, this); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 468 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 469 | BroadcastDispatcher<CameraDeviceImpl.CaptureCallback> broadcaster = |
| 470 | new BroadcastDispatcher<CameraDeviceImpl.CaptureCallback>( |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 471 | replaceDeviceWithSession, |
| 472 | localSink); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 473 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 474 | return new CallbackProxies.DeviceCaptureCallbackProxy(broadcaster); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /** |
| 478 | * |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 479 | * Create an internal state callback, to be invoked on the mDeviceHandler |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 480 | * |
| 481 | * <p>It has a few behaviors: |
| 482 | * <ul> |
| 483 | * <li>Convert device state changes into session state changes. |
| 484 | * <li>Keep track of async tasks that the session began (idle, abort). |
| 485 | * </ul> |
| 486 | * </p> |
| 487 | * */ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 488 | CameraDeviceImpl.StateCallbackKK getDeviceStateCallback() { |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 489 | final CameraCaptureSession session = this; |
| 490 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 491 | return new CameraDeviceImpl.StateCallbackKK() { |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 492 | private boolean mBusy = false; |
| 493 | private boolean mActive = false; |
| 494 | |
| 495 | @Override |
| 496 | public void onOpened(CameraDevice camera) { |
| 497 | throw new AssertionError("Camera must already be open before creating a session"); |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public void onDisconnected(CameraDevice camera) { |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 502 | if (VERBOSE) Log.v(TAG, mIdString + "onDisconnected"); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 503 | close(); |
| 504 | } |
| 505 | |
| 506 | @Override |
| 507 | public void onError(CameraDevice camera, int error) { |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 508 | // Should not be reached, handled by device code |
| 509 | Log.wtf(TAG, mIdString + "Got device error " + error); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | @Override |
| 513 | public void onActive(CameraDevice camera) { |
| 514 | mIdleDrainer.taskStarted(); |
| 515 | mActive = true; |
| 516 | |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 517 | if (VERBOSE) Log.v(TAG, mIdString + "onActive"); |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 518 | mStateCallback.onActive(session); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public void onIdle(CameraDevice camera) { |
| 523 | boolean isAborting; |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 524 | if (VERBOSE) Log.v(TAG, mIdString + "onIdle"); |
| 525 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 526 | synchronized (session) { |
| 527 | isAborting = mAborting; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Check which states we transitioned through: |
| 532 | * |
| 533 | * (ACTIVE -> IDLE) |
| 534 | * (BUSY -> IDLE) |
| 535 | * |
| 536 | * Note that this is also legal: |
| 537 | * (ACTIVE -> BUSY -> IDLE) |
| 538 | * |
| 539 | * and mark those tasks as finished |
| 540 | */ |
| 541 | if (mBusy && isAborting) { |
| 542 | mAbortDrainer.taskFinished(); |
| 543 | |
| 544 | synchronized (session) { |
| 545 | mAborting = false; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | if (mActive) { |
| 550 | mIdleDrainer.taskFinished(); |
| 551 | } |
| 552 | |
| 553 | mBusy = false; |
| 554 | mActive = false; |
| 555 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 556 | mStateCallback.onReady(session); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public void onBusy(CameraDevice camera) { |
| 561 | mBusy = true; |
| 562 | |
| 563 | // TODO: Queue captures during abort instead of failing them |
| 564 | // since the app won't be able to distinguish the two actives |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 565 | // Don't signal the application since there's no clean mapping here |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 566 | if (VERBOSE) Log.v(TAG, mIdString + "onBusy"); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | @Override |
| 570 | public void onUnconfigured(CameraDevice camera) { |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 571 | if (VERBOSE) Log.v(TAG, mIdString + "onUnconfigured"); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 572 | } |
Eino-Ville Talvala | ad916f7 | 2015-04-11 12:09:11 -0700 | [diff] [blame] | 573 | |
| 574 | @Override |
| 575 | public void onSurfacePrepared(Surface surface) { |
| 576 | if (VERBOSE) Log.v(TAG, mIdString + "onPrepared"); |
| 577 | mStateCallback.onSurfacePrepared(session, surface); |
| 578 | } |
| 579 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 580 | }; |
| 581 | |
| 582 | } |
| 583 | |
| 584 | @Override |
| 585 | protected void finalize() throws Throwable { |
| 586 | try { |
| 587 | close(); |
| 588 | } finally { |
| 589 | super.finalize(); |
| 590 | } |
| 591 | } |
| 592 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 593 | private void checkNotClosed() { |
| 594 | if (mClosed) { |
| 595 | throw new IllegalStateException( |
| 596 | "Session has been closed; further changes are illegal."); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Notify the session that a pending capture sequence has just been queued. |
| 602 | * |
| 603 | * <p>During a shutdown/close, the session waits until all pending sessions are finished |
| 604 | * before taking any further steps to shut down itself.</p> |
| 605 | * |
| 606 | * @see #finishPendingSequence |
| 607 | */ |
| 608 | private int addPendingSequence(int sequenceId) { |
| 609 | mSequenceDrainer.taskStarted(sequenceId); |
| 610 | return sequenceId; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Notify the session that a pending capture sequence is now finished. |
| 615 | * |
| 616 | * <p>During a shutdown/close, once all pending sequences finish, it is safe to |
| 617 | * close the camera further by unconfiguring and then firing {@code onClosed}.</p> |
| 618 | */ |
| 619 | private void finishPendingSequence(int sequenceId) { |
| 620 | mSequenceDrainer.taskFinished(sequenceId); |
| 621 | } |
| 622 | |
| 623 | private class SequenceDrainListener implements TaskDrainer.DrainListener { |
| 624 | @Override |
| 625 | public void onDrained() { |
| 626 | /* |
| 627 | * No repeating request is set; and the capture queue has fully drained. |
| 628 | * |
| 629 | * If no captures were queued to begin with, and an abort was queued, |
| 630 | * it's still possible to get another BUSY before the last IDLE. |
| 631 | * |
| 632 | * If the camera is already "IDLE" and no aborts are pending, |
| 633 | * then the drain immediately finishes. |
| 634 | */ |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 635 | if (VERBOSE) Log.v(TAG, mIdString + "onSequenceDrained"); |
Eino-Ville Talvala | 937c93c | 2015-05-18 16:16:44 -0700 | [diff] [blame^] | 636 | |
| 637 | |
| 638 | // Fire session close as soon as all sequences are complete. |
| 639 | // We may still need to unconfigure the device, but a new session might be created |
| 640 | // past this point, and notifications would then stop to this instance. |
| 641 | mStateCallback.onClosed(CameraCaptureSessionImpl.this); |
| 642 | |
| 643 | // Fast path: A new capture session has replaced this one; don't wait for abort/idle |
| 644 | // as we won't get state updates any more anyway. |
| 645 | if (mSkipUnconfigure) { |
| 646 | return; |
| 647 | } |
| 648 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 649 | mAbortDrainer.beginDrain(); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | private class AbortDrainListener implements TaskDrainer.DrainListener { |
| 654 | @Override |
| 655 | public void onDrained() { |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 656 | if (VERBOSE) Log.v(TAG, mIdString + "onAbortDrained"); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 657 | synchronized (CameraCaptureSessionImpl.this) { |
| 658 | /* |
| 659 | * Any queued aborts have now completed. |
| 660 | * |
| 661 | * It's now safe to wait to receive the final "IDLE" event, as the camera device |
| 662 | * will no longer again transition to "ACTIVE" by itself. |
| 663 | * |
| 664 | * If the camera is already "IDLE", then the drain immediately finishes. |
| 665 | */ |
Eino-Ville Talvala | 937c93c | 2015-05-18 16:16:44 -0700 | [diff] [blame^] | 666 | |
| 667 | // Fast path: A new capture session has replaced this one; don't wait for idle |
| 668 | // as we won't get state updates any more anyway. |
| 669 | if (mSkipUnconfigure) { |
| 670 | return; |
| 671 | } |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 672 | mIdleDrainer.beginDrain(); |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | private class IdleDrainListener implements TaskDrainer.DrainListener { |
| 678 | @Override |
| 679 | public void onDrained() { |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 680 | if (VERBOSE) Log.v(TAG, mIdString + "onIdleDrained"); |
Igor Murashkin | 51dcfd65 | 2014-09-25 16:55:01 -0700 | [diff] [blame] | 681 | |
| 682 | // Take device lock before session lock so that we can call back into device |
| 683 | // without causing a deadlock |
| 684 | synchronized (mDeviceImpl.mInterfaceLock) { |
| 685 | synchronized (CameraCaptureSessionImpl.this) { |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 686 | /* |
| 687 | * The device is now IDLE, and has settled. It will not transition to |
| 688 | * ACTIVE or BUSY again by itself. |
| 689 | * |
Eino-Ville Talvala | 937c93c | 2015-05-18 16:16:44 -0700 | [diff] [blame^] | 690 | * It's now safe to unconfigure the outputs. |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 691 | * |
| 692 | * This operation is idempotent; a session will not be closed twice. |
| 693 | */ |
Igor Murashkin | 51dcfd65 | 2014-09-25 16:55:01 -0700 | [diff] [blame] | 694 | if (VERBOSE) |
| 695 | Log.v(TAG, mIdString + "Session drain complete, skip unconfigure: " + |
| 696 | mSkipUnconfigure); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 697 | |
Eino-Ville Talvala | 937c93c | 2015-05-18 16:16:44 -0700 | [diff] [blame^] | 698 | // Fast path: A new capture session has replaced this one; don't wait for idle |
| 699 | // as we won't get state updates any more anyway. |
Igor Murashkin | 51dcfd65 | 2014-09-25 16:55:01 -0700 | [diff] [blame] | 700 | if (mSkipUnconfigure) { |
Igor Murashkin | 51dcfd65 | 2014-09-25 16:55:01 -0700 | [diff] [blame] | 701 | return; |
| 702 | } |
| 703 | |
Eino-Ville Talvala | 937c93c | 2015-05-18 16:16:44 -0700 | [diff] [blame^] | 704 | // Final slow path: unconfigure the camera, no session has replaced us and |
| 705 | // everything is idle. |
Igor Murashkin | 51dcfd65 | 2014-09-25 16:55:01 -0700 | [diff] [blame] | 706 | try { |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 707 | // begin transition to unconfigured |
| 708 | mDeviceImpl.configureStreamsChecked(null, null); |
Igor Murashkin | 51dcfd65 | 2014-09-25 16:55:01 -0700 | [diff] [blame] | 709 | } catch (CameraAccessException e) { |
| 710 | // OK: do not throw checked exceptions. |
Eino-Ville Talvala | 937c93c | 2015-05-18 16:16:44 -0700 | [diff] [blame^] | 711 | Log.e(TAG, mIdString + "Exception while unconfiguring outputs: ", e); |
Igor Murashkin | 51dcfd65 | 2014-09-25 16:55:01 -0700 | [diff] [blame] | 712 | |
| 713 | // TODO: call onError instead of onClosed if this happens |
Eino-Ville Talvala | 56fbbea | 2014-09-23 18:18:07 -0700 | [diff] [blame] | 714 | } catch (IllegalStateException e) { |
Eino-Ville Talvala | 937c93c | 2015-05-18 16:16:44 -0700 | [diff] [blame^] | 715 | // Camera is already closed, so nothing left to do |
Eino-Ville Talvala | 56fbbea | 2014-09-23 18:18:07 -0700 | [diff] [blame] | 716 | if (VERBOSE) Log.v(TAG, mIdString + |
| 717 | "Camera was already closed or busy, skipping unconfigure"); |
Igor Murashkin | 51dcfd65 | 2014-09-25 16:55:01 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 720 | } |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 725 | } |