Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1 | /* |
| 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 Talvala | 2f1a2e4 | 2013-07-25 17:12:05 -0700 | [diff] [blame] | 17 | package android.hardware.camera2.impl; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 18 | |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 19 | import static android.hardware.camera2.CameraAccessException.CAMERA_IN_USE; |
| 20 | |
Eino-Ville Talvala | 2f1a2e4 | 2013-07-25 17:12:05 -0700 | [diff] [blame] | 21 | import android.hardware.camera2.CameraAccessException; |
Eino-Ville Talvala | 2f1a2e4 | 2013-07-25 17:12:05 -0700 | [diff] [blame] | 22 | import android.hardware.camera2.CaptureRequest; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 23 | import android.hardware.camera2.CaptureResult; |
| 24 | import android.hardware.camera2.ICameraDeviceCallbacks; |
| 25 | import android.hardware.camera2.ICameraDeviceUser; |
Eino-Ville Talvala | 5afd3e9 | 2013-08-21 10:37:04 -0700 | [diff] [blame] | 26 | import android.hardware.camera2.utils.CameraBinderDecorator; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 27 | import android.hardware.camera2.utils.CameraRuntimeException; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 28 | import android.os.Handler; |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 29 | import android.os.IBinder; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 30 | import android.os.Looper; |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 31 | import android.os.RemoteException; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 32 | import android.util.Log; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 33 | import android.util.SparseArray; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 34 | import android.view.Surface; |
| 35 | |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 37 | import java.util.HashSet; |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 38 | import java.util.Iterator; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 39 | import java.util.List; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 42 | * HAL2.1+ implementation of CameraDevice. Use CameraManager#open to instantiate |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 43 | */ |
Eino-Ville Talvala | 2f1a2e4 | 2013-07-25 17:12:05 -0700 | [diff] [blame] | 44 | public class CameraDevice implements android.hardware.camera2.CameraDevice { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 45 | |
| 46 | private final String TAG; |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 47 | private final boolean DEBUG; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 48 | |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 49 | private static final int REQUEST_ID_NONE = -1; |
| 50 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 51 | // TODO: guard every function with if (!mRemoteDevice) check (if it was closed) |
| 52 | private ICameraDeviceUser mRemoteDevice; |
| 53 | |
| 54 | private final Object mLock = new Object(); |
| 55 | private final CameraDeviceCallbacks mCallbacks = new CameraDeviceCallbacks(); |
| 56 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 57 | private final StateListener mDeviceListener; |
| 58 | private final Handler mDeviceHandler; |
| 59 | |
| 60 | private boolean mIdle = true; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 61 | |
| 62 | private final SparseArray<CaptureListenerHolder> mCaptureListenerMap = |
| 63 | new SparseArray<CaptureListenerHolder>(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 64 | |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 65 | private int mRepeatingRequestId = REQUEST_ID_NONE; |
| 66 | private final ArrayList<Integer> mRepeatingRequestIdDeletedList = new ArrayList<Integer>(); |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 67 | // Map stream IDs to Surfaces |
| 68 | private final SparseArray<Surface> mConfiguredOutputs = new SparseArray<Surface>(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 69 | |
| 70 | private final String mCameraId; |
| 71 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 72 | // Runnables for all state transitions, except error, which needs the |
| 73 | // error code argument |
| 74 | |
| 75 | private final Runnable mCallOnOpened = new Runnable() { |
| 76 | public void run() { |
| 77 | if (!CameraDevice.this.isClosed()) { |
| 78 | mDeviceListener.onOpened(CameraDevice.this); |
| 79 | } |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | private final Runnable mCallOnUnconfigured = new Runnable() { |
| 84 | public void run() { |
| 85 | if (!CameraDevice.this.isClosed()) { |
| 86 | mDeviceListener.onUnconfigured(CameraDevice.this); |
| 87 | } |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | private final Runnable mCallOnActive = new Runnable() { |
| 92 | public void run() { |
| 93 | if (!CameraDevice.this.isClosed()) { |
| 94 | mDeviceListener.onActive(CameraDevice.this); |
| 95 | } |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | private final Runnable mCallOnBusy = new Runnable() { |
| 100 | public void run() { |
| 101 | if (!CameraDevice.this.isClosed()) { |
| 102 | mDeviceListener.onBusy(CameraDevice.this); |
| 103 | } |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | private final Runnable mCallOnClosed = new Runnable() { |
| 108 | public void run() { |
Zhijun He | 2db5602 | 2014-01-02 15:12:00 -0800 | [diff] [blame] | 109 | mDeviceListener.onClosed(CameraDevice.this); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 110 | } |
| 111 | }; |
| 112 | |
| 113 | private final Runnable mCallOnIdle = new Runnable() { |
| 114 | public void run() { |
| 115 | if (!CameraDevice.this.isClosed()) { |
| 116 | mDeviceListener.onIdle(CameraDevice.this); |
| 117 | } |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | private final Runnable mCallOnDisconnected = new Runnable() { |
| 122 | public void run() { |
| 123 | if (!CameraDevice.this.isClosed()) { |
| 124 | mDeviceListener.onDisconnected(CameraDevice.this); |
| 125 | } |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | public CameraDevice(String cameraId, StateListener listener, Handler handler) { |
| 130 | if (cameraId == null || listener == null || handler == null) { |
| 131 | throw new IllegalArgumentException("Null argument given"); |
| 132 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 133 | mCameraId = cameraId; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 134 | mDeviceListener = listener; |
| 135 | mDeviceHandler = handler; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 136 | TAG = String.format("CameraDevice-%s-JV", mCameraId); |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 137 | DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | public CameraDeviceCallbacks getCallbacks() { |
| 141 | return mCallbacks; |
| 142 | } |
| 143 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 144 | public void setRemoteDevice(ICameraDeviceUser remoteDevice) { |
Eino-Ville Talvala | 5afd3e9 | 2013-08-21 10:37:04 -0700 | [diff] [blame] | 145 | // TODO: Move from decorator to direct binder-mediated exceptions |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 146 | synchronized(mLock) { |
| 147 | mRemoteDevice = CameraBinderDecorator.newInstance(remoteDevice); |
| 148 | |
| 149 | mDeviceHandler.post(mCallOnOpened); |
| 150 | mDeviceHandler.post(mCallOnUnconfigured); |
| 151 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 155 | public String getId() { |
| 156 | return mCameraId; |
| 157 | } |
| 158 | |
| 159 | @Override |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 160 | public void configureOutputs(List<Surface> outputs) throws CameraAccessException { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 161 | // Treat a null input the same an empty list |
| 162 | if (outputs == null) { |
| 163 | outputs = new ArrayList<Surface>(); |
| 164 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 165 | synchronized (mLock) { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 166 | checkIfCameraClosed(); |
| 167 | |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 168 | HashSet<Surface> addSet = new HashSet<Surface>(outputs); // Streams to create |
| 169 | List<Integer> deleteList = new ArrayList<Integer>(); // Streams to delete |
| 170 | |
| 171 | // Determine which streams need to be created, which to be deleted |
| 172 | for (int i = 0; i < mConfiguredOutputs.size(); ++i) { |
| 173 | int streamId = mConfiguredOutputs.keyAt(i); |
| 174 | Surface s = mConfiguredOutputs.valueAt(i); |
| 175 | |
| 176 | if (!outputs.contains(s)) { |
| 177 | deleteList.add(streamId); |
| 178 | } else { |
| 179 | addSet.remove(s); // Don't create a stream previously created |
| 180 | } |
| 181 | } |
| 182 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 183 | mDeviceHandler.post(mCallOnBusy); |
| 184 | stopRepeating(); |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 185 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 186 | try { |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 187 | waitUntilIdle(); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 188 | |
| 189 | // TODO: mRemoteDevice.beginConfigure |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 190 | // Delete all streams first (to free up HW resources) |
| 191 | for (Integer streamId : deleteList) { |
| 192 | mRemoteDevice.deleteStream(streamId); |
| 193 | mConfiguredOutputs.delete(streamId); |
| 194 | } |
| 195 | |
| 196 | // Add all new streams |
| 197 | for (Surface s : addSet) { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 198 | // TODO: remove width,height,format since we are ignoring |
| 199 | // it. |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 200 | int streamId = mRemoteDevice.createStream(0, 0, 0, s); |
| 201 | mConfiguredOutputs.put(streamId, s); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 202 | } |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 203 | |
| 204 | // TODO: mRemoteDevice.endConfigure |
| 205 | } catch (CameraRuntimeException e) { |
| 206 | if (e.getReason() == CAMERA_IN_USE) { |
| 207 | throw new IllegalStateException("The camera is currently busy." + |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 208 | " You must wait until the previous operation completes."); |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | throw e.asChecked(); |
| 212 | } catch (RemoteException e) { |
| 213 | // impossible |
| 214 | return; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 215 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 216 | |
| 217 | if (outputs.size() > 0) { |
| 218 | mDeviceHandler.post(mCallOnIdle); |
| 219 | } else { |
| 220 | mDeviceHandler.post(mCallOnUnconfigured); |
| 221 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | @Override |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 226 | public CaptureRequest.Builder createCaptureRequest(int templateType) |
| 227 | throws CameraAccessException { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 228 | synchronized (mLock) { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 229 | checkIfCameraClosed(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 230 | |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 231 | CameraMetadataNative templatedRequest = new CameraMetadataNative(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 232 | |
| 233 | try { |
| 234 | mRemoteDevice.createDefaultRequest(templateType, /* out */templatedRequest); |
| 235 | } catch (CameraRuntimeException e) { |
| 236 | throw e.asChecked(); |
| 237 | } catch (RemoteException e) { |
| 238 | // impossible |
| 239 | return null; |
| 240 | } |
| 241 | |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 242 | CaptureRequest.Builder builder = |
| 243 | new CaptureRequest.Builder(templatedRequest); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 244 | |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 245 | return builder; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | @Override |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 250 | public int capture(CaptureRequest request, CaptureListener listener, Handler handler) |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 251 | throws CameraAccessException { |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 252 | return submitCaptureRequest(request, listener, handler, /*streaming*/false); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Override |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 256 | public int captureBurst(List<CaptureRequest> requests, CaptureListener listener, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 257 | Handler handler) throws CameraAccessException { |
Zhijun He | fc19e2c | 2013-08-22 14:43:07 -0700 | [diff] [blame] | 258 | if (requests.isEmpty()) { |
| 259 | Log.w(TAG, "Capture burst request list is empty, do nothing!"); |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 260 | return -1; |
Zhijun He | fc19e2c | 2013-08-22 14:43:07 -0700 | [diff] [blame] | 261 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 262 | // TODO |
| 263 | throw new UnsupportedOperationException("Burst capture implemented yet"); |
| 264 | |
| 265 | } |
| 266 | |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 267 | private int submitCaptureRequest(CaptureRequest request, CaptureListener listener, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 268 | Handler handler, boolean repeating) throws CameraAccessException { |
| 269 | |
| 270 | // Need a valid handler, or current thread needs to have a looper, if |
| 271 | // listener is valid |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 272 | if (listener != null) { |
| 273 | handler = checkHandler(handler); |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 274 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 275 | |
| 276 | synchronized (mLock) { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 277 | checkIfCameraClosed(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 278 | int requestId; |
| 279 | |
Ruben Brunk | e73b41b | 2013-11-07 19:30:43 -0800 | [diff] [blame] | 280 | if (repeating) { |
| 281 | stopRepeating(); |
| 282 | } |
| 283 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 284 | try { |
| 285 | requestId = mRemoteDevice.submitRequest(request, repeating); |
| 286 | } catch (CameraRuntimeException e) { |
| 287 | throw e.asChecked(); |
| 288 | } catch (RemoteException e) { |
| 289 | // impossible |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 290 | return -1; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 291 | } |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 292 | if (listener != null) { |
| 293 | mCaptureListenerMap.put(requestId, new CaptureListenerHolder(listener, request, |
| 294 | handler, repeating)); |
| 295 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 296 | |
| 297 | if (repeating) { |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 298 | mRepeatingRequestId = requestId; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 301 | if (mIdle) { |
| 302 | mDeviceHandler.post(mCallOnActive); |
| 303 | } |
| 304 | mIdle = false; |
| 305 | |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 306 | return requestId; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | @Override |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 311 | public int setRepeatingRequest(CaptureRequest request, CaptureListener listener, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 312 | Handler handler) throws CameraAccessException { |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 313 | return submitCaptureRequest(request, listener, handler, /*streaming*/true); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Override |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 317 | public int setRepeatingBurst(List<CaptureRequest> requests, CaptureListener listener, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 318 | Handler handler) throws CameraAccessException { |
Zhijun He | fc19e2c | 2013-08-22 14:43:07 -0700 | [diff] [blame] | 319 | if (requests.isEmpty()) { |
| 320 | Log.w(TAG, "Set Repeating burst request list is empty, do nothing!"); |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 321 | return -1; |
Zhijun He | fc19e2c | 2013-08-22 14:43:07 -0700 | [diff] [blame] | 322 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 323 | // TODO |
| 324 | throw new UnsupportedOperationException("Burst capture implemented yet"); |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public void stopRepeating() throws CameraAccessException { |
| 329 | |
| 330 | synchronized (mLock) { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 331 | checkIfCameraClosed(); |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 332 | if (mRepeatingRequestId != REQUEST_ID_NONE) { |
| 333 | |
| 334 | int requestId = mRepeatingRequestId; |
| 335 | mRepeatingRequestId = REQUEST_ID_NONE; |
| 336 | |
| 337 | // Queue for deletion after in-flight requests finish |
| 338 | mRepeatingRequestIdDeletedList.add(requestId); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 339 | |
| 340 | try { |
| 341 | mRemoteDevice.cancelRequest(requestId); |
| 342 | } catch (CameraRuntimeException e) { |
| 343 | throw e.asChecked(); |
| 344 | } catch (RemoteException e) { |
| 345 | // impossible |
| 346 | return; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
Zhijun He | d842fcd | 2013-12-26 14:14:04 -0800 | [diff] [blame] | 352 | private void waitUntilIdle() throws CameraAccessException { |
Zhijun He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 353 | |
| 354 | synchronized (mLock) { |
| 355 | checkIfCameraClosed(); |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 356 | if (mRepeatingRequestId != REQUEST_ID_NONE) { |
Zhijun He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 357 | throw new IllegalStateException("Active repeating request ongoing"); |
| 358 | } |
| 359 | |
| 360 | try { |
| 361 | mRemoteDevice.waitUntilIdle(); |
| 362 | } catch (CameraRuntimeException e) { |
| 363 | throw e.asChecked(); |
| 364 | } catch (RemoteException e) { |
| 365 | // impossible |
| 366 | return; |
| 367 | } |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 368 | |
| 369 | mRepeatingRequestId = REQUEST_ID_NONE; |
| 370 | mRepeatingRequestIdDeletedList.clear(); |
| 371 | mCaptureListenerMap.clear(); |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 372 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | @Override |
Eino-Ville Talvala | 8ebd52b | 2013-08-13 12:09:44 -0700 | [diff] [blame] | 376 | public void flush() throws CameraAccessException { |
| 377 | synchronized (mLock) { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 378 | checkIfCameraClosed(); |
| 379 | |
| 380 | mDeviceHandler.post(mCallOnBusy); |
Eino-Ville Talvala | 8ebd52b | 2013-08-13 12:09:44 -0700 | [diff] [blame] | 381 | try { |
| 382 | mRemoteDevice.flush(); |
| 383 | } catch (CameraRuntimeException e) { |
| 384 | throw e.asChecked(); |
| 385 | } catch (RemoteException e) { |
| 386 | // impossible |
| 387 | return; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | @Override |
Igor Murashkin | 5c9eaf6 | 2013-09-10 19:35:24 -0700 | [diff] [blame] | 393 | public void close() { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 394 | synchronized (mLock) { |
| 395 | |
| 396 | try { |
Igor Murashkin | 2a3eced | 2013-08-28 17:35:10 -0700 | [diff] [blame] | 397 | if (mRemoteDevice != null) { |
| 398 | mRemoteDevice.disconnect(); |
| 399 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 400 | } catch (CameraRuntimeException e) { |
Igor Murashkin | 5c9eaf6 | 2013-09-10 19:35:24 -0700 | [diff] [blame] | 401 | Log.e(TAG, "Exception while closing: ", e.asChecked()); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 402 | } catch (RemoteException e) { |
| 403 | // impossible |
| 404 | } |
| 405 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 406 | if (mRemoteDevice != null) { |
| 407 | mDeviceHandler.post(mCallOnClosed); |
| 408 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 409 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 410 | mRemoteDevice = null; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | @Override |
| 415 | protected void finalize() throws Throwable { |
| 416 | try { |
| 417 | close(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 418 | } |
| 419 | finally { |
| 420 | super.finalize(); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | static class CaptureListenerHolder { |
| 425 | |
| 426 | private final boolean mRepeating; |
| 427 | private final CaptureListener mListener; |
| 428 | private final CaptureRequest mRequest; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 429 | private final Handler mHandler; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 430 | |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 431 | CaptureListenerHolder(CaptureListener listener, CaptureRequest request, Handler handler, |
| 432 | boolean repeating) { |
| 433 | if (listener == null || handler == null) { |
| 434 | throw new UnsupportedOperationException( |
| 435 | "Must have a valid handler and a valid listener"); |
| 436 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 437 | mRepeating = repeating; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 438 | mHandler = handler; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 439 | mRequest = request; |
| 440 | mListener = listener; |
| 441 | } |
| 442 | |
| 443 | public boolean isRepeating() { |
| 444 | return mRepeating; |
| 445 | } |
| 446 | |
| 447 | public CaptureListener getListener() { |
| 448 | return mListener; |
| 449 | } |
| 450 | |
| 451 | public CaptureRequest getRequest() { |
| 452 | return mRequest; |
| 453 | } |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 454 | |
| 455 | public Handler getHandler() { |
| 456 | return mHandler; |
| 457 | } |
| 458 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 461 | public class CameraDeviceCallbacks extends ICameraDeviceCallbacks.Stub { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 462 | |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 463 | // |
| 464 | // Constants below need to be kept up-to-date with |
| 465 | // frameworks/av/include/camera/camera2/ICameraDeviceCallbacks.h |
| 466 | // |
| 467 | |
| 468 | // |
| 469 | // Error codes for onCameraError |
| 470 | // |
| 471 | |
| 472 | /** |
| 473 | * Camera has been disconnected |
| 474 | */ |
| 475 | static final int ERROR_CAMERA_DISCONNECTED = 0; |
| 476 | |
| 477 | /** |
| 478 | * Camera has encountered a device-level error |
| 479 | * Matches CameraDevice.StateListener#ERROR_CAMERA_DEVICE |
| 480 | */ |
| 481 | static final int ERROR_CAMERA_DEVICE = 1; |
| 482 | |
| 483 | /** |
| 484 | * Camera has encountered a service-level error |
| 485 | * Matches CameraDevice.StateListener#ERROR_CAMERA_SERVICE |
| 486 | */ |
| 487 | static final int ERROR_CAMERA_SERVICE = 2; |
| 488 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 489 | @Override |
| 490 | public IBinder asBinder() { |
| 491 | return this; |
| 492 | } |
| 493 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 494 | @Override |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 495 | public void onCameraError(final int errorCode) { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 496 | Runnable r = null; |
| 497 | if (isClosed()) return; |
| 498 | |
| 499 | synchronized(mLock) { |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 500 | switch (errorCode) { |
| 501 | case ERROR_CAMERA_DISCONNECTED: |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 502 | r = mCallOnDisconnected; |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 503 | break; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 504 | default: |
| 505 | Log.e(TAG, "Unknown error from camera device: " + errorCode); |
| 506 | // no break |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 507 | case ERROR_CAMERA_DEVICE: |
| 508 | case ERROR_CAMERA_SERVICE: |
| 509 | r = new Runnable() { |
| 510 | public void run() { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 511 | if (!CameraDevice.this.isClosed()) { |
| 512 | mDeviceListener.onError(CameraDevice.this, errorCode); |
| 513 | } |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 514 | } |
| 515 | }; |
| 516 | break; |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 517 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 518 | CameraDevice.this.mDeviceHandler.post(r); |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 519 | } |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | @Override |
| 523 | public void onCameraIdle() { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 524 | if (isClosed()) return; |
| 525 | |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 526 | if (DEBUG) { |
| 527 | Log.d(TAG, "Camera now idle"); |
| 528 | } |
| 529 | synchronized (mLock) { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 530 | if (!CameraDevice.this.mIdle) { |
| 531 | CameraDevice.this.mDeviceHandler.post(mCallOnIdle); |
| 532 | } |
| 533 | CameraDevice.this.mIdle = true; |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public void onCaptureStarted(int requestId, final long timestamp) { |
| 539 | if (DEBUG) { |
| 540 | Log.d(TAG, "Capture started for id " + requestId); |
| 541 | } |
| 542 | final CaptureListenerHolder holder; |
| 543 | |
| 544 | // Get the listener for this frame ID, if there is one |
| 545 | synchronized (mLock) { |
| 546 | holder = CameraDevice.this.mCaptureListenerMap.get(requestId); |
| 547 | } |
| 548 | |
| 549 | if (holder == null) { |
| 550 | return; |
| 551 | } |
| 552 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 553 | if (isClosed()) return; |
| 554 | |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 555 | // Dispatch capture start notice |
| 556 | holder.getHandler().post( |
| 557 | new Runnable() { |
| 558 | public void run() { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 559 | if (!CameraDevice.this.isClosed()) { |
| 560 | holder.getListener().onCaptureStarted( |
| 561 | CameraDevice.this, |
| 562 | holder.getRequest(), |
| 563 | timestamp); |
| 564 | } |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 565 | } |
| 566 | }); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | @Override |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 570 | public void onResultReceived(int requestId, CameraMetadataNative result) |
| 571 | throws RemoteException { |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 572 | if (DEBUG) { |
| 573 | Log.d(TAG, "Received result for id " + requestId); |
| 574 | } |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 575 | final CaptureListenerHolder holder; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 576 | |
Eino-Ville Talvala | 7a31310 | 2013-11-07 14:45:06 -0800 | [diff] [blame] | 577 | Boolean quirkPartial = result.get(CaptureResult.QUIRKS_PARTIAL_RESULT); |
| 578 | boolean quirkIsPartialResult = (quirkPartial != null && quirkPartial); |
| 579 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 580 | synchronized (mLock) { |
| 581 | // TODO: move this whole map into this class to make it more testable, |
| 582 | // exposing the methods necessary like subscribeToRequest, unsubscribe.. |
| 583 | // TODO: make class static class |
| 584 | |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 585 | holder = CameraDevice.this.mCaptureListenerMap.get(requestId); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 586 | |
| 587 | // Clean up listener once we no longer expect to see it. |
Eino-Ville Talvala | 7a31310 | 2013-11-07 14:45:06 -0800 | [diff] [blame] | 588 | if (holder != null && !holder.isRepeating() && !quirkIsPartialResult) { |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 589 | CameraDevice.this.mCaptureListenerMap.remove(requestId); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 590 | } |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 591 | |
| 592 | // TODO: add 'capture sequence completed' callback to the |
| 593 | // service, and clean up repeating requests there instead. |
| 594 | |
| 595 | // If we received a result for a repeating request and have |
| 596 | // prior repeating requests queued for deletion, remove those |
| 597 | // requests from mCaptureListenerMap. |
Eino-Ville Talvala | 7a31310 | 2013-11-07 14:45:06 -0800 | [diff] [blame] | 598 | if (holder != null && holder.isRepeating() && !quirkIsPartialResult |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 599 | && mRepeatingRequestIdDeletedList.size() > 0) { |
| 600 | Iterator<Integer> iter = mRepeatingRequestIdDeletedList.iterator(); |
| 601 | while (iter.hasNext()) { |
| 602 | int deletedRequestId = iter.next(); |
| 603 | if (deletedRequestId < requestId) { |
| 604 | CameraDevice.this.mCaptureListenerMap.remove(deletedRequestId); |
| 605 | iter.remove(); |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 612 | // Check if we have a listener for this |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 613 | if (holder == null) { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 617 | if (isClosed()) return; |
| 618 | |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 619 | final CaptureRequest request = holder.getRequest(); |
| 620 | final CaptureResult resultAsCapture = new CaptureResult(result, request, requestId); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 621 | |
Eino-Ville Talvala | 7a31310 | 2013-11-07 14:45:06 -0800 | [diff] [blame] | 622 | Runnable resultDispatch = null; |
| 623 | |
| 624 | // Either send a partial result or the final capture completed result |
| 625 | if (quirkIsPartialResult) { |
| 626 | // Partial result |
| 627 | resultDispatch = new Runnable() { |
| 628 | @Override |
| 629 | public void run() { |
| 630 | if (!CameraDevice.this.isClosed()){ |
| 631 | holder.getListener().onCapturePartial( |
| 632 | CameraDevice.this, |
| 633 | request, |
| 634 | resultAsCapture); |
| 635 | } |
| 636 | } |
| 637 | }; |
| 638 | } else { |
| 639 | // Final capture result |
| 640 | resultDispatch = new Runnable() { |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 641 | @Override |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 642 | public void run() { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 643 | if (!CameraDevice.this.isClosed()){ |
| 644 | holder.getListener().onCaptureCompleted( |
| 645 | CameraDevice.this, |
| 646 | request, |
| 647 | resultAsCapture); |
| 648 | } |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 649 | } |
Eino-Ville Talvala | 7a31310 | 2013-11-07 14:45:06 -0800 | [diff] [blame] | 650 | }; |
| 651 | } |
| 652 | |
| 653 | holder.getHandler().post(resultDispatch); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | } |
| 657 | |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 658 | /** |
| 659 | * Default handler management. If handler is null, get the current thread's |
| 660 | * Looper to create a Handler with. If no looper exists, throw exception. |
| 661 | */ |
| 662 | private Handler checkHandler(Handler handler) { |
| 663 | if (handler == null) { |
| 664 | Looper looper = Looper.myLooper(); |
| 665 | if (looper == null) { |
| 666 | throw new IllegalArgumentException( |
| 667 | "No handler given, and current thread has no looper!"); |
| 668 | } |
| 669 | handler = new Handler(looper); |
| 670 | } |
| 671 | return handler; |
| 672 | } |
| 673 | |
Zhijun He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 674 | private void checkIfCameraClosed() { |
| 675 | if (mRemoteDevice == null) { |
| 676 | throw new IllegalStateException("CameraDevice was already closed"); |
| 677 | } |
| 678 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 679 | |
| 680 | private boolean isClosed() { |
| 681 | synchronized(mLock) { |
| 682 | return (mRemoteDevice == null); |
| 683 | } |
| 684 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 685 | } |