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