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; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 22 | import android.hardware.camera2.CameraMetadata; |
Eino-Ville Talvala | 2f1a2e4 | 2013-07-25 17:12:05 -0700 | [diff] [blame] | 23 | import android.hardware.camera2.CameraProperties; |
| 24 | import android.hardware.camera2.CaptureRequest; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 25 | import android.hardware.camera2.CaptureResult; |
| 26 | import android.hardware.camera2.ICameraDeviceCallbacks; |
| 27 | import android.hardware.camera2.ICameraDeviceUser; |
Eino-Ville Talvala | 5afd3e9 | 2013-08-21 10:37:04 -0700 | [diff] [blame] | 28 | import android.hardware.camera2.utils.CameraBinderDecorator; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 29 | import android.hardware.camera2.utils.CameraRuntimeException; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 30 | import android.os.IBinder; |
| 31 | import android.os.RemoteException; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 32 | import android.os.Handler; |
| 33 | import android.os.Looper; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 34 | import android.util.Log; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 35 | import android.util.SparseArray; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 36 | import android.view.Surface; |
| 37 | |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 39 | import java.util.HashMap; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 40 | import java.util.HashSet; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 41 | import java.util.List; |
| 42 | import java.util.Stack; |
| 43 | |
| 44 | /** |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 45 | * HAL2.1+ implementation of CameraDevice. Use CameraManager#open to instantiate |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 46 | */ |
Eino-Ville Talvala | 2f1a2e4 | 2013-07-25 17:12:05 -0700 | [diff] [blame] | 47 | public class CameraDevice implements android.hardware.camera2.CameraDevice { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 48 | |
| 49 | private final String TAG; |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 50 | private final boolean DEBUG; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 51 | |
| 52 | // TODO: guard every function with if (!mRemoteDevice) check (if it was closed) |
| 53 | private ICameraDeviceUser mRemoteDevice; |
| 54 | |
| 55 | private final Object mLock = new Object(); |
| 56 | private final CameraDeviceCallbacks mCallbacks = new CameraDeviceCallbacks(); |
| 57 | |
Igor Murashkin | 5c9eaf6 | 2013-09-10 19:35:24 -0700 | [diff] [blame] | 58 | private StateListener mDeviceListener; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 59 | private Handler mDeviceHandler; |
| 60 | |
| 61 | private final SparseArray<CaptureListenerHolder> mCaptureListenerMap = |
| 62 | new SparseArray<CaptureListenerHolder>(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 63 | |
| 64 | private final Stack<Integer> mRepeatingRequestIdStack = new Stack<Integer>(); |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 65 | // Map stream IDs to Surfaces |
| 66 | private final SparseArray<Surface> mConfiguredOutputs = new SparseArray<Surface>(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 67 | |
| 68 | private final String mCameraId; |
| 69 | |
| 70 | public CameraDevice(String cameraId) { |
| 71 | mCameraId = cameraId; |
| 72 | TAG = String.format("CameraDevice-%s-JV", mCameraId); |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 73 | DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | public CameraDeviceCallbacks getCallbacks() { |
| 77 | return mCallbacks; |
| 78 | } |
| 79 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 80 | public void setRemoteDevice(ICameraDeviceUser remoteDevice) { |
Eino-Ville Talvala | 5afd3e9 | 2013-08-21 10:37:04 -0700 | [diff] [blame] | 81 | // TODO: Move from decorator to direct binder-mediated exceptions |
| 82 | mRemoteDevice = CameraBinderDecorator.newInstance(remoteDevice); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 86 | public String getId() { |
| 87 | return mCameraId; |
| 88 | } |
| 89 | |
| 90 | @Override |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 91 | public CameraProperties getProperties() throws CameraAccessException { |
Igor Murashkin | d7bf177 | 2013-07-12 18:01:31 -0700 | [diff] [blame] | 92 | |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 93 | CameraMetadataNative info = new CameraMetadataNative(); |
Igor Murashkin | d7bf177 | 2013-07-12 18:01:31 -0700 | [diff] [blame] | 94 | |
| 95 | try { |
| 96 | mRemoteDevice.getCameraInfo(/*out*/info); |
| 97 | } catch(CameraRuntimeException e) { |
| 98 | throw e.asChecked(); |
| 99 | } catch(RemoteException e) { |
| 100 | // impossible |
| 101 | return null; |
| 102 | } |
| 103 | |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 104 | CameraProperties properties = new CameraProperties(info); |
Igor Murashkin | d7bf177 | 2013-07-12 18:01:31 -0700 | [diff] [blame] | 105 | return properties; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void configureOutputs(List<Surface> outputs) throws CameraAccessException { |
| 110 | synchronized (mLock) { |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 111 | HashSet<Surface> addSet = new HashSet<Surface>(outputs); // Streams to create |
| 112 | List<Integer> deleteList = new ArrayList<Integer>(); // Streams to delete |
| 113 | |
| 114 | // Determine which streams need to be created, which to be deleted |
| 115 | for (int i = 0; i < mConfiguredOutputs.size(); ++i) { |
| 116 | int streamId = mConfiguredOutputs.keyAt(i); |
| 117 | Surface s = mConfiguredOutputs.valueAt(i); |
| 118 | |
| 119 | if (!outputs.contains(s)) { |
| 120 | deleteList.add(streamId); |
| 121 | } else { |
| 122 | addSet.remove(s); // Don't create a stream previously created |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | try { |
| 127 | // TODO: mRemoteDevice.beginConfigure |
| 128 | |
| 129 | // Delete all streams first (to free up HW resources) |
| 130 | for (Integer streamId : deleteList) { |
| 131 | mRemoteDevice.deleteStream(streamId); |
| 132 | mConfiguredOutputs.delete(streamId); |
| 133 | } |
| 134 | |
| 135 | // Add all new streams |
| 136 | for (Surface s : addSet) { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 137 | // TODO: remove width,height,format since we are ignoring |
| 138 | // it. |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 139 | int streamId = mRemoteDevice.createStream(0, 0, 0, s); |
| 140 | mConfiguredOutputs.put(streamId, s); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 141 | } |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 142 | |
| 143 | // TODO: mRemoteDevice.endConfigure |
| 144 | } catch (CameraRuntimeException e) { |
| 145 | if (e.getReason() == CAMERA_IN_USE) { |
| 146 | throw new IllegalStateException("The camera is currently busy." + |
| 147 | " You must call waitUntilIdle before trying to reconfigure."); |
| 148 | } |
| 149 | |
| 150 | throw e.asChecked(); |
| 151 | } catch (RemoteException e) { |
| 152 | // impossible |
| 153 | return; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | @Override |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 159 | public CaptureRequest.Builder createCaptureRequest(int templateType) |
| 160 | throws CameraAccessException { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 161 | synchronized (mLock) { |
| 162 | |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 163 | CameraMetadataNative templatedRequest = new CameraMetadataNative(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 164 | |
| 165 | try { |
| 166 | mRemoteDevice.createDefaultRequest(templateType, /* out */templatedRequest); |
| 167 | } catch (CameraRuntimeException e) { |
| 168 | throw e.asChecked(); |
| 169 | } catch (RemoteException e) { |
| 170 | // impossible |
| 171 | return null; |
| 172 | } |
| 173 | |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 174 | CaptureRequest.Builder builder = |
| 175 | new CaptureRequest.Builder(templatedRequest); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 176 | |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 177 | return builder; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | @Override |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 182 | public int capture(CaptureRequest request, CaptureListener listener, Handler handler) |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 183 | throws CameraAccessException { |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 184 | return submitCaptureRequest(request, listener, handler, /*streaming*/false); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 188 | public int captureBurst(List<CaptureRequest> requests, CaptureListener listener, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 189 | Handler handler) throws CameraAccessException { |
Zhijun He | fc19e2c | 2013-08-22 14:43:07 -0700 | [diff] [blame] | 190 | if (requests.isEmpty()) { |
| 191 | Log.w(TAG, "Capture burst request list is empty, do nothing!"); |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 192 | return -1; |
Zhijun He | fc19e2c | 2013-08-22 14:43:07 -0700 | [diff] [blame] | 193 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 194 | // TODO |
| 195 | throw new UnsupportedOperationException("Burst capture implemented yet"); |
| 196 | |
| 197 | } |
| 198 | |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 199 | private int submitCaptureRequest(CaptureRequest request, CaptureListener listener, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 200 | Handler handler, boolean repeating) throws CameraAccessException { |
| 201 | |
| 202 | // Need a valid handler, or current thread needs to have a looper, if |
| 203 | // listener is valid |
| 204 | if (handler == null && listener != null) { |
| 205 | Looper looper = Looper.myLooper(); |
| 206 | if (looper == null) { |
| 207 | throw new IllegalArgumentException( |
| 208 | "No handler given, and current thread has no looper!"); |
| 209 | } |
| 210 | handler = new Handler(looper); |
| 211 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 212 | |
| 213 | synchronized (mLock) { |
| 214 | |
| 215 | int requestId; |
| 216 | |
| 217 | try { |
| 218 | requestId = mRemoteDevice.submitRequest(request, repeating); |
| 219 | } catch (CameraRuntimeException e) { |
| 220 | throw e.asChecked(); |
| 221 | } catch (RemoteException e) { |
| 222 | // impossible |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 223 | return -1; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 224 | } |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 225 | if (listener != null) { |
| 226 | mCaptureListenerMap.put(requestId, new CaptureListenerHolder(listener, request, |
| 227 | handler, repeating)); |
| 228 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 229 | |
| 230 | if (repeating) { |
| 231 | mRepeatingRequestIdStack.add(requestId); |
| 232 | } |
| 233 | |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 234 | return requestId; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | @Override |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 239 | public int setRepeatingRequest(CaptureRequest request, CaptureListener listener, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 240 | Handler handler) throws CameraAccessException { |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 241 | return submitCaptureRequest(request, listener, handler, /*streaming*/true); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 245 | public int setRepeatingBurst(List<CaptureRequest> requests, CaptureListener listener, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 246 | Handler handler) throws CameraAccessException { |
Zhijun He | fc19e2c | 2013-08-22 14:43:07 -0700 | [diff] [blame] | 247 | if (requests.isEmpty()) { |
| 248 | Log.w(TAG, "Set Repeating burst request list is empty, do nothing!"); |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 249 | return -1; |
Zhijun He | fc19e2c | 2013-08-22 14:43:07 -0700 | [diff] [blame] | 250 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 251 | // TODO |
| 252 | throw new UnsupportedOperationException("Burst capture implemented yet"); |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public void stopRepeating() throws CameraAccessException { |
| 257 | |
| 258 | synchronized (mLock) { |
| 259 | |
| 260 | while (!mRepeatingRequestIdStack.isEmpty()) { |
| 261 | int requestId = mRepeatingRequestIdStack.pop(); |
| 262 | |
| 263 | try { |
| 264 | mRemoteDevice.cancelRequest(requestId); |
| 265 | } catch (CameraRuntimeException e) { |
| 266 | throw e.asChecked(); |
| 267 | } catch (RemoteException e) { |
| 268 | // impossible |
| 269 | return; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public void waitUntilIdle() throws CameraAccessException { |
Zhijun He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 277 | |
| 278 | synchronized (mLock) { |
| 279 | checkIfCameraClosed(); |
| 280 | if (!mRepeatingRequestIdStack.isEmpty()) { |
| 281 | throw new IllegalStateException("Active repeating request ongoing"); |
| 282 | } |
| 283 | |
| 284 | try { |
| 285 | mRemoteDevice.waitUntilIdle(); |
| 286 | } catch (CameraRuntimeException e) { |
| 287 | throw e.asChecked(); |
| 288 | } catch (RemoteException e) { |
| 289 | // impossible |
| 290 | return; |
| 291 | } |
| 292 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | @Override |
Igor Murashkin | 5c9eaf6 | 2013-09-10 19:35:24 -0700 | [diff] [blame] | 296 | public void setDeviceListener(StateListener listener, Handler handler) { |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 297 | synchronized (mLock) { |
| 298 | mDeviceListener = listener; |
| 299 | mDeviceHandler = handler; |
| 300 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Override |
Eino-Ville Talvala | 8ebd52b | 2013-08-13 12:09:44 -0700 | [diff] [blame] | 304 | public void flush() throws CameraAccessException { |
| 305 | synchronized (mLock) { |
| 306 | try { |
| 307 | mRemoteDevice.flush(); |
| 308 | } catch (CameraRuntimeException e) { |
| 309 | throw e.asChecked(); |
| 310 | } catch (RemoteException e) { |
| 311 | // impossible |
| 312 | return; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | @Override |
Igor Murashkin | 5c9eaf6 | 2013-09-10 19:35:24 -0700 | [diff] [blame] | 318 | public void close() { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 319 | |
| 320 | // TODO: every method should throw IllegalStateException after close has been called |
| 321 | |
| 322 | synchronized (mLock) { |
| 323 | |
| 324 | try { |
Igor Murashkin | 2a3eced | 2013-08-28 17:35:10 -0700 | [diff] [blame] | 325 | if (mRemoteDevice != null) { |
| 326 | mRemoteDevice.disconnect(); |
| 327 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 328 | } catch (CameraRuntimeException e) { |
Igor Murashkin | 5c9eaf6 | 2013-09-10 19:35:24 -0700 | [diff] [blame] | 329 | Log.e(TAG, "Exception while closing: ", e.asChecked()); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 330 | } catch (RemoteException e) { |
| 331 | // impossible |
| 332 | } |
| 333 | |
| 334 | mRemoteDevice = null; |
| 335 | |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | protected void finalize() throws Throwable { |
| 341 | try { |
| 342 | close(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 343 | } |
| 344 | finally { |
| 345 | super.finalize(); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static class CaptureListenerHolder { |
| 350 | |
| 351 | private final boolean mRepeating; |
| 352 | private final CaptureListener mListener; |
| 353 | private final CaptureRequest mRequest; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 354 | private final Handler mHandler; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 355 | |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 356 | CaptureListenerHolder(CaptureListener listener, CaptureRequest request, Handler handler, |
| 357 | boolean repeating) { |
| 358 | if (listener == null || handler == null) { |
| 359 | throw new UnsupportedOperationException( |
| 360 | "Must have a valid handler and a valid listener"); |
| 361 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 362 | mRepeating = repeating; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 363 | mHandler = handler; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 364 | mRequest = request; |
| 365 | mListener = listener; |
| 366 | } |
| 367 | |
| 368 | public boolean isRepeating() { |
| 369 | return mRepeating; |
| 370 | } |
| 371 | |
| 372 | public CaptureListener getListener() { |
| 373 | return mListener; |
| 374 | } |
| 375 | |
| 376 | public CaptureRequest getRequest() { |
| 377 | return mRequest; |
| 378 | } |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 379 | |
| 380 | public Handler getHandler() { |
| 381 | return mHandler; |
| 382 | } |
| 383 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // TODO: unit tests |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 387 | public class CameraDeviceCallbacks extends ICameraDeviceCallbacks.Stub { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 388 | |
| 389 | @Override |
| 390 | public IBinder asBinder() { |
| 391 | return this; |
| 392 | } |
| 393 | |
| 394 | // TODO: consider rename to onMessageReceived |
| 395 | @Override |
| 396 | public void notifyCallback(int msgType, int ext1, int ext2) throws RemoteException { |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 397 | if (DEBUG) { |
| 398 | Log.d(TAG, "Got message " + msgType + " ext1: " + ext1 + " , ext2: " + ext2); |
| 399 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 400 | // TODO implement rest |
| 401 | } |
| 402 | |
| 403 | @Override |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 404 | public void onResultReceived(int requestId, CameraMetadataNative result) |
| 405 | throws RemoteException { |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 406 | if (DEBUG) { |
| 407 | Log.d(TAG, "Received result for id " + requestId); |
| 408 | } |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 409 | final CaptureListenerHolder holder; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 410 | |
| 411 | synchronized (mLock) { |
| 412 | // TODO: move this whole map into this class to make it more testable, |
| 413 | // exposing the methods necessary like subscribeToRequest, unsubscribe.. |
| 414 | // TODO: make class static class |
| 415 | |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 416 | holder = CameraDevice.this.mCaptureListenerMap.get(requestId); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 417 | |
| 418 | // Clean up listener once we no longer expect to see it. |
| 419 | |
| 420 | // TODO: how to handle repeating listeners? |
| 421 | // we probably want cancelRequest to return # of times it already enqueued and |
| 422 | // keep a counter. |
| 423 | if (holder != null && !holder.isRepeating()) { |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 424 | CameraDevice.this.mCaptureListenerMap.remove(requestId); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 428 | // Check if we have a listener for this |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 429 | if (holder == null) { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 430 | return; |
| 431 | } |
| 432 | |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 433 | final CaptureRequest request = holder.getRequest(); |
| 434 | final CaptureResult resultAsCapture = new CaptureResult(result, request, requestId); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 435 | |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 436 | holder.getHandler().post( |
| 437 | new Runnable() { |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 438 | @Override |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 439 | public void run() { |
| 440 | holder.getListener().onCaptureCompleted( |
| 441 | CameraDevice.this, |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame^] | 442 | request, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 443 | resultAsCapture); |
| 444 | } |
| 445 | }); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | } |
| 449 | |
Zhijun He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 450 | private void checkIfCameraClosed() { |
| 451 | if (mRemoteDevice == null) { |
| 452 | throw new IllegalStateException("CameraDevice was already closed"); |
| 453 | } |
| 454 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 455 | } |