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 | cca00c6 | 2014-05-14 10:53:20 -0700 | [diff] [blame] | 22 | import android.hardware.camera2.CameraCaptureSession; |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 23 | import android.hardware.camera2.CameraCharacteristics; |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 24 | import android.hardware.camera2.CameraDevice; |
Eino-Ville Talvala | 2f1a2e4 | 2013-07-25 17:12:05 -0700 | [diff] [blame] | 25 | import android.hardware.camera2.CaptureRequest; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 26 | import android.hardware.camera2.CaptureResult; |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 27 | import android.hardware.camera2.CaptureFailure; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 28 | import android.hardware.camera2.ICameraDeviceCallbacks; |
| 29 | import android.hardware.camera2.ICameraDeviceUser; |
Igor Murashkin | db075af | 2014-05-21 10:07:08 -0700 | [diff] [blame] | 30 | import android.hardware.camera2.TotalCaptureResult; |
Eino-Ville Talvala | 5afd3e9 | 2013-08-21 10:37:04 -0700 | [diff] [blame] | 31 | import android.hardware.camera2.utils.CameraBinderDecorator; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 32 | import android.hardware.camera2.utils.CameraRuntimeException; |
Igor Murashkin | 9c59517 | 2014-05-12 13:56:20 -0700 | [diff] [blame] | 33 | import android.hardware.camera2.utils.LongParcelable; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 34 | import android.os.Handler; |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 35 | import android.os.IBinder; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 36 | import android.os.Looper; |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 37 | import android.os.RemoteException; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 38 | import android.util.Log; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 39 | import android.util.SparseArray; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 40 | import android.view.Surface; |
| 41 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 42 | import java.util.AbstractMap.SimpleEntry; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 43 | import java.util.ArrayList; |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 44 | import java.util.HashMap; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 45 | import java.util.HashSet; |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 46 | import java.util.Iterator; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 47 | import java.util.List; |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 48 | import java.util.TreeSet; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 49 | |
| 50 | /** |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 51 | * HAL2.1+ implementation of CameraDevice. Use CameraManager#open to instantiate |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 52 | */ |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 53 | public class CameraDeviceImpl extends CameraDevice { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 54 | |
| 55 | private final String TAG; |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 56 | private final boolean DEBUG; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 57 | |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 58 | private static final int REQUEST_ID_NONE = -1; |
| 59 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 60 | // TODO: guard every function with if (!mRemoteDevice) check (if it was closed) |
| 61 | private ICameraDeviceUser mRemoteDevice; |
| 62 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 63 | // Lock to synchronize cross-thread access to device public interface |
| 64 | private final Object mInterfaceLock = new Object(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 65 | private final CameraDeviceCallbacks mCallbacks = new CameraDeviceCallbacks(); |
| 66 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 67 | private final StateCallback mDeviceCallback; |
| 68 | private volatile StateCallbackKK mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 69 | private final Handler mDeviceHandler; |
| 70 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 71 | private volatile boolean mClosing = false; |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 72 | private boolean mInError = false; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 73 | private boolean mIdle = true; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 74 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 75 | /** map request IDs to callback/request data */ |
| 76 | private final SparseArray<CaptureCallbackHolder> mCaptureCallbackMap = |
| 77 | new SparseArray<CaptureCallbackHolder>(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 78 | |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 79 | private int mRepeatingRequestId = REQUEST_ID_NONE; |
| 80 | private final ArrayList<Integer> mRepeatingRequestIdDeletedList = new ArrayList<Integer>(); |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 81 | // Map stream IDs to Surfaces |
| 82 | private final SparseArray<Surface> mConfiguredOutputs = new SparseArray<Surface>(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 83 | |
| 84 | private final String mCameraId; |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 85 | private final CameraCharacteristics mCharacteristics; |
Zhijun He | 8315915 | 2014-07-16 11:32:59 -0700 | [diff] [blame] | 86 | private final int mTotalPartialCount; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 87 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 88 | /** |
| 89 | * A list tracking request and its expected last frame. |
| 90 | * Updated when calling ICameraDeviceUser methods. |
| 91 | */ |
| 92 | private final List<SimpleEntry</*frameNumber*/Long, /*requestId*/Integer>> |
| 93 | mFrameNumberRequestPairs = new ArrayList<SimpleEntry<Long, Integer>>(); |
| 94 | |
| 95 | /** |
| 96 | * An object tracking received frame numbers. |
| 97 | * Updated when receiving callbacks from ICameraDeviceCallbacks. |
| 98 | */ |
| 99 | private final FrameNumberTracker mFrameNumberTracker = new FrameNumberTracker(); |
| 100 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 101 | private CameraCaptureSessionImpl mCurrentSession; |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 102 | private int mNextSessionId = 0; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 103 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 104 | // Runnables for all state transitions, except error, which needs the |
| 105 | // error code argument |
| 106 | |
| 107 | private final Runnable mCallOnOpened = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 108 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 109 | public void run() { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 110 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 111 | synchronized(mInterfaceLock) { |
| 112 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 113 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 114 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 115 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 116 | if (sessionCallback != null) { |
| 117 | sessionCallback.onOpened(CameraDeviceImpl.this); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 118 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 119 | mDeviceCallback.onOpened(CameraDeviceImpl.this); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 120 | } |
| 121 | }; |
| 122 | |
| 123 | private final Runnable mCallOnUnconfigured = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 124 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 125 | public void run() { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 126 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 127 | synchronized(mInterfaceLock) { |
| 128 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 129 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 130 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 131 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 132 | if (sessionCallback != null) { |
| 133 | sessionCallback.onUnconfigured(CameraDeviceImpl.this); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 134 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 135 | } |
| 136 | }; |
| 137 | |
| 138 | private final Runnable mCallOnActive = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 139 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 140 | public void run() { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 141 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 142 | synchronized(mInterfaceLock) { |
| 143 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 144 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 145 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 146 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 147 | if (sessionCallback != null) { |
| 148 | sessionCallback.onActive(CameraDeviceImpl.this); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 149 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 150 | } |
| 151 | }; |
| 152 | |
| 153 | private final Runnable mCallOnBusy = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 154 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 155 | public void run() { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 156 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 157 | synchronized(mInterfaceLock) { |
| 158 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 159 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 160 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 161 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 162 | if (sessionCallback != null) { |
| 163 | sessionCallback.onBusy(CameraDeviceImpl.this); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 164 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 165 | } |
| 166 | }; |
| 167 | |
| 168 | private final Runnable mCallOnClosed = new Runnable() { |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 169 | private boolean mClosedOnce = false; |
| 170 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 171 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 172 | public void run() { |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 173 | if (mClosedOnce) { |
| 174 | throw new AssertionError("Don't post #onClosed more than once"); |
| 175 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 176 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 177 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 178 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 179 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 180 | if (sessionCallback != null) { |
| 181 | sessionCallback.onClosed(CameraDeviceImpl.this); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 182 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 183 | mDeviceCallback.onClosed(CameraDeviceImpl.this); |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 184 | mClosedOnce = true; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 185 | } |
| 186 | }; |
| 187 | |
| 188 | private final Runnable mCallOnIdle = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 189 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 190 | public void run() { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 191 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 192 | synchronized(mInterfaceLock) { |
| 193 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 194 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 195 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 196 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 197 | if (sessionCallback != null) { |
| 198 | sessionCallback.onIdle(CameraDeviceImpl.this); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 199 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 200 | } |
| 201 | }; |
| 202 | |
| 203 | private final Runnable mCallOnDisconnected = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 204 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 205 | public void run() { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 206 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 207 | synchronized(mInterfaceLock) { |
| 208 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 209 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 210 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 211 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 212 | if (sessionCallback != null) { |
| 213 | sessionCallback.onDisconnected(CameraDeviceImpl.this); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 214 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 215 | mDeviceCallback.onDisconnected(CameraDeviceImpl.this); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 216 | } |
| 217 | }; |
| 218 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 219 | public CameraDeviceImpl(String cameraId, StateCallback callback, Handler handler, |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 220 | CameraCharacteristics characteristics) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 221 | if (cameraId == null || callback == null || handler == null || characteristics == null) { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 222 | throw new IllegalArgumentException("Null argument given"); |
| 223 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 224 | mCameraId = cameraId; |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 225 | mDeviceCallback = callback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 226 | mDeviceHandler = handler; |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 227 | mCharacteristics = characteristics; |
Eino-Ville Talvala | b72d1ef | 2014-04-28 13:21:18 -0700 | [diff] [blame] | 228 | |
| 229 | final int MAX_TAG_LEN = 23; |
| 230 | String tag = String.format("CameraDevice-JV-%s", mCameraId); |
| 231 | if (tag.length() > MAX_TAG_LEN) { |
| 232 | tag = tag.substring(0, MAX_TAG_LEN); |
| 233 | } |
| 234 | TAG = tag; |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 235 | DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Zhijun He | 8315915 | 2014-07-16 11:32:59 -0700 | [diff] [blame] | 236 | |
| 237 | Integer partialCount = |
| 238 | mCharacteristics.get(CameraCharacteristics.REQUEST_PARTIAL_RESULT_COUNT); |
| 239 | if (partialCount == null) { |
| 240 | // 1 means partial result is not supported. |
| 241 | mTotalPartialCount = 1; |
| 242 | } else { |
| 243 | mTotalPartialCount = partialCount; |
| 244 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | public CameraDeviceCallbacks getCallbacks() { |
| 248 | return mCallbacks; |
| 249 | } |
| 250 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 251 | public void setRemoteDevice(ICameraDeviceUser remoteDevice) { |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 252 | synchronized(mInterfaceLock) { |
| 253 | // TODO: Move from decorator to direct binder-mediated exceptions |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 254 | // If setRemoteFailure already called, do nothing |
| 255 | if (mInError) return; |
| 256 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 257 | mRemoteDevice = CameraBinderDecorator.newInstance(remoteDevice); |
| 258 | |
| 259 | mDeviceHandler.post(mCallOnOpened); |
| 260 | mDeviceHandler.post(mCallOnUnconfigured); |
| 261 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 264 | /** |
| 265 | * Call to indicate failed connection to a remote camera device. |
| 266 | * |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 267 | * <p>This places the camera device in the error state and informs the callback. |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 268 | * Use in place of setRemoteDevice() when startup fails.</p> |
| 269 | */ |
| 270 | public void setRemoteFailure(final CameraRuntimeException failure) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 271 | int failureCode = StateCallback.ERROR_CAMERA_DEVICE; |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 272 | boolean failureIsError = true; |
| 273 | |
| 274 | switch (failure.getReason()) { |
| 275 | case CameraAccessException.CAMERA_IN_USE: |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 276 | failureCode = StateCallback.ERROR_CAMERA_IN_USE; |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 277 | break; |
| 278 | case CameraAccessException.MAX_CAMERAS_IN_USE: |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 279 | failureCode = StateCallback.ERROR_MAX_CAMERAS_IN_USE; |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 280 | break; |
| 281 | case CameraAccessException.CAMERA_DISABLED: |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 282 | failureCode = StateCallback.ERROR_CAMERA_DISABLED; |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 283 | break; |
| 284 | case CameraAccessException.CAMERA_DISCONNECTED: |
| 285 | failureIsError = false; |
| 286 | break; |
| 287 | case CameraAccessException.CAMERA_ERROR: |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 288 | failureCode = StateCallback.ERROR_CAMERA_DEVICE; |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 289 | break; |
| 290 | default: |
| 291 | Log.wtf(TAG, "Unknown failure in opening camera device: " + failure.getReason()); |
| 292 | break; |
| 293 | } |
| 294 | final int code = failureCode; |
| 295 | final boolean isError = failureIsError; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 296 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 297 | mInError = true; |
| 298 | mDeviceHandler.post(new Runnable() { |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 299 | @Override |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 300 | public void run() { |
| 301 | if (isError) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 302 | mDeviceCallback.onError(CameraDeviceImpl.this, code); |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 303 | } else { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 304 | mDeviceCallback.onDisconnected(CameraDeviceImpl.this); |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | }); |
| 308 | } |
| 309 | } |
| 310 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 311 | @Override |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 312 | public String getId() { |
| 313 | return mCameraId; |
| 314 | } |
| 315 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 316 | public void configureOutputs(List<Surface> outputs) throws CameraAccessException { |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 317 | // Leave this here for backwards compatibility with older code using this directly |
| 318 | configureOutputsChecked(outputs); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Attempt to configure the outputs; the device goes to idle and then configures the |
| 323 | * new outputs if possible. |
| 324 | * |
| 325 | * <p>The configuration may gracefully fail, if there are too many outputs, if the formats |
| 326 | * are not supported, or if the sizes for that format is not supported. In this case this |
| 327 | * function will return {@code false} and the unconfigured callback will be fired.</p> |
| 328 | * |
| 329 | * <p>If the configuration succeeds (with 1 or more outputs), then the idle callback is fired. |
| 330 | * Unconfiguring the device always fires the idle callback.</p> |
| 331 | * |
| 332 | * @param outputs a list of one or more surfaces, or {@code null} to unconfigure |
| 333 | * @return whether or not the configuration was successful |
| 334 | * |
| 335 | * @throws CameraAccessException if there were any unexpected problems during configuration |
| 336 | */ |
| 337 | public boolean configureOutputsChecked(List<Surface> outputs) throws CameraAccessException { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 338 | // Treat a null input the same an empty list |
| 339 | if (outputs == null) { |
| 340 | outputs = new ArrayList<Surface>(); |
| 341 | } |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 342 | boolean success = false; |
| 343 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 344 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 345 | checkIfCameraClosedOrInError(); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 346 | |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 347 | HashSet<Surface> addSet = new HashSet<Surface>(outputs); // Streams to create |
| 348 | List<Integer> deleteList = new ArrayList<Integer>(); // Streams to delete |
| 349 | |
| 350 | // Determine which streams need to be created, which to be deleted |
| 351 | for (int i = 0; i < mConfiguredOutputs.size(); ++i) { |
| 352 | int streamId = mConfiguredOutputs.keyAt(i); |
| 353 | Surface s = mConfiguredOutputs.valueAt(i); |
| 354 | |
| 355 | if (!outputs.contains(s)) { |
| 356 | deleteList.add(streamId); |
| 357 | } else { |
| 358 | addSet.remove(s); // Don't create a stream previously created |
| 359 | } |
| 360 | } |
| 361 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 362 | mDeviceHandler.post(mCallOnBusy); |
| 363 | stopRepeating(); |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 364 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 365 | try { |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 366 | waitUntilIdle(); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 367 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 368 | mRemoteDevice.beginConfigure(); |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 369 | // Delete all streams first (to free up HW resources) |
| 370 | for (Integer streamId : deleteList) { |
| 371 | mRemoteDevice.deleteStream(streamId); |
| 372 | mConfiguredOutputs.delete(streamId); |
| 373 | } |
| 374 | |
| 375 | // Add all new streams |
| 376 | for (Surface s : addSet) { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 377 | // TODO: remove width,height,format since we are ignoring |
| 378 | // it. |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 379 | int streamId = mRemoteDevice.createStream(0, 0, 0, s); |
| 380 | mConfiguredOutputs.put(streamId, s); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 381 | } |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 382 | |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 383 | try { |
| 384 | mRemoteDevice.endConfigure(); |
| 385 | } |
| 386 | catch (IllegalArgumentException e) { |
| 387 | // OK. camera service can reject stream config if it's not supported by HAL |
| 388 | // This is only the result of a programmer misusing the camera2 api. |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 389 | Log.w(TAG, "Stream configuration failed"); |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 390 | return false; |
| 391 | } |
| 392 | |
| 393 | success = true; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 394 | } catch (CameraRuntimeException e) { |
| 395 | if (e.getReason() == CAMERA_IN_USE) { |
| 396 | throw new IllegalStateException("The camera is currently busy." + |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 397 | " You must wait until the previous operation completes."); |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | throw e.asChecked(); |
| 401 | } catch (RemoteException e) { |
| 402 | // impossible |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 403 | return false; |
| 404 | } finally { |
| 405 | if (success && outputs.size() > 0) { |
| 406 | mDeviceHandler.post(mCallOnIdle); |
| 407 | } else { |
| 408 | // Always return to the 'unconfigured' state if we didn't hit a fatal error |
| 409 | mDeviceHandler.post(mCallOnUnconfigured); |
| 410 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 411 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 412 | } |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 413 | |
| 414 | return success; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | @Override |
Eino-Ville Talvala | cca00c6 | 2014-05-14 10:53:20 -0700 | [diff] [blame] | 418 | public void createCaptureSession(List<Surface> outputs, |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 419 | CameraCaptureSession.StateCallback callback, Handler handler) |
Eino-Ville Talvala | cca00c6 | 2014-05-14 10:53:20 -0700 | [diff] [blame] | 420 | throws CameraAccessException { |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 421 | synchronized(mInterfaceLock) { |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 422 | if (DEBUG) { |
| 423 | Log.d(TAG, "createCaptureSession"); |
| 424 | } |
| 425 | |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 426 | checkIfCameraClosedOrInError(); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 427 | |
Eino-Ville Talvala | d3b85f6 | 2014-08-05 15:02:31 -0700 | [diff] [blame] | 428 | // Notify current session that it's going away, before starting camera operations |
| 429 | // After this call completes, the session is not allowed to call into CameraDeviceImpl |
| 430 | if (mCurrentSession != null) { |
| 431 | mCurrentSession.replaceSessionClose(); |
| 432 | } |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 433 | |
| 434 | // TODO: dont block for this |
| 435 | boolean configureSuccess = true; |
| 436 | CameraAccessException pendingException = null; |
| 437 | try { |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 438 | configureSuccess = configureOutputsChecked(outputs); // and then block until IDLE |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 439 | } catch (CameraAccessException e) { |
| 440 | configureSuccess = false; |
| 441 | pendingException = e; |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 442 | if (DEBUG) { |
| 443 | Log.v(TAG, "createCaptureSession - failed with exception ", e); |
| 444 | } |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | // Fire onConfigured if configureOutputs succeeded, fire onConfigureFailed otherwise. |
| 448 | CameraCaptureSessionImpl newSession = |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 449 | new CameraCaptureSessionImpl(mNextSessionId++, |
| 450 | outputs, callback, handler, this, mDeviceHandler, |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 451 | configureSuccess); |
| 452 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 453 | // TODO: wait until current session closes, then create the new session |
| 454 | mCurrentSession = newSession; |
| 455 | |
| 456 | if (pendingException != null) { |
| 457 | throw pendingException; |
| 458 | } |
| 459 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 460 | mSessionStateCallback = mCurrentSession.getDeviceStateCallback(); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 461 | } |
Eino-Ville Talvala | cca00c6 | 2014-05-14 10:53:20 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 464 | /** |
| 465 | * For use by backwards-compatibility code only. |
| 466 | */ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 467 | public void setSessionListener(StateCallbackKK sessionCallback) { |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 468 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 469 | mSessionStateCallback = sessionCallback; |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Eino-Ville Talvala | cca00c6 | 2014-05-14 10:53:20 -0700 | [diff] [blame] | 473 | @Override |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 474 | public CaptureRequest.Builder createCaptureRequest(int templateType) |
| 475 | throws CameraAccessException { |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 476 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 477 | checkIfCameraClosedOrInError(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 478 | |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 479 | CameraMetadataNative templatedRequest = new CameraMetadataNative(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 480 | |
| 481 | try { |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 482 | mRemoteDevice.createDefaultRequest(templateType, /*out*/templatedRequest); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 483 | } catch (CameraRuntimeException e) { |
| 484 | throw e.asChecked(); |
| 485 | } catch (RemoteException e) { |
| 486 | // impossible |
| 487 | return null; |
| 488 | } |
| 489 | |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 490 | CaptureRequest.Builder builder = |
| 491 | new CaptureRequest.Builder(templatedRequest); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 492 | |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 493 | return builder; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 497 | public int capture(CaptureRequest request, CaptureCallback callback, Handler handler) |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 498 | throws CameraAccessException { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 499 | if (DEBUG) { |
| 500 | Log.d(TAG, "calling capture"); |
| 501 | } |
| 502 | List<CaptureRequest> requestList = new ArrayList<CaptureRequest>(); |
| 503 | requestList.add(request); |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 504 | return submitCaptureRequest(requestList, callback, handler, /*streaming*/false); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 507 | public int captureBurst(List<CaptureRequest> requests, CaptureCallback callback, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 508 | Handler handler) throws CameraAccessException { |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 509 | if (requests == null || requests.isEmpty()) { |
| 510 | throw new IllegalArgumentException("At least one request must be given"); |
Zhijun He | fc19e2c | 2013-08-22 14:43:07 -0700 | [diff] [blame] | 511 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 512 | return submitCaptureRequest(requests, callback, handler, /*streaming*/false); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 515 | /** |
| 516 | * This method checks lastFrameNumber returned from ICameraDeviceUser methods for |
| 517 | * starting and stopping repeating request and flushing. |
| 518 | * |
| 519 | * <p>If lastFrameNumber is NO_FRAMES_CAPTURED, it means that the request was never |
Eino-Ville Talvala | ee37e30 | 2014-06-10 15:24:26 -0700 | [diff] [blame] | 520 | * sent to HAL. Then onCaptureSequenceAborted is immediately triggered. |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 521 | * If lastFrameNumber is non-negative, then the requestId and lastFrameNumber pair |
| 522 | * is added to the list mFrameNumberRequestPairs.</p> |
| 523 | * |
| 524 | * @param requestId the request ID of the current repeating request. |
| 525 | * |
| 526 | * @param lastFrameNumber last frame number returned from binder. |
| 527 | */ |
| 528 | private void checkEarlyTriggerSequenceComplete( |
| 529 | final int requestId, final long lastFrameNumber) { |
| 530 | // lastFrameNumber being equal to NO_FRAMES_CAPTURED means that the request |
Eino-Ville Talvala | ee37e30 | 2014-06-10 15:24:26 -0700 | [diff] [blame] | 531 | // was never sent to HAL. Should trigger onCaptureSequenceAborted immediately. |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 532 | if (lastFrameNumber == CaptureCallback.NO_FRAMES_CAPTURED) { |
| 533 | final CaptureCallbackHolder holder; |
| 534 | int index = mCaptureCallbackMap.indexOfKey(requestId); |
| 535 | holder = (index >= 0) ? mCaptureCallbackMap.valueAt(index) : null; |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 536 | if (holder != null) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 537 | mCaptureCallbackMap.removeAt(index); |
Jianing Wei | baf0c65 | 2014-04-18 17:35:00 -0700 | [diff] [blame] | 538 | if (DEBUG) { |
| 539 | Log.v(TAG, String.format( |
| 540 | "remove holder for requestId %d, " |
| 541 | + "because lastFrame is %d.", |
| 542 | requestId, lastFrameNumber)); |
| 543 | } |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | if (holder != null) { |
| 547 | if (DEBUG) { |
Eino-Ville Talvala | ee37e30 | 2014-06-10 15:24:26 -0700 | [diff] [blame] | 548 | Log.v(TAG, "immediately trigger onCaptureSequenceAborted because" |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 549 | + " request did not reach HAL"); |
| 550 | } |
| 551 | |
| 552 | Runnable resultDispatch = new Runnable() { |
| 553 | @Override |
| 554 | public void run() { |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 555 | if (!CameraDeviceImpl.this.isClosed()) { |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 556 | if (DEBUG) { |
| 557 | Log.d(TAG, String.format( |
| 558 | "early trigger sequence complete for request %d", |
| 559 | requestId)); |
| 560 | } |
| 561 | if (lastFrameNumber < Integer.MIN_VALUE |
| 562 | || lastFrameNumber > Integer.MAX_VALUE) { |
| 563 | throw new AssertionError(lastFrameNumber + " cannot be cast to int"); |
| 564 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 565 | holder.getCallback().onCaptureSequenceAborted( |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 566 | CameraDeviceImpl.this, |
Eino-Ville Talvala | ee37e30 | 2014-06-10 15:24:26 -0700 | [diff] [blame] | 567 | requestId); |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | }; |
| 571 | holder.getHandler().post(resultDispatch); |
| 572 | } else { |
| 573 | Log.w(TAG, String.format( |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 574 | "did not register callback to request %d", |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 575 | requestId)); |
| 576 | } |
| 577 | } else { |
| 578 | mFrameNumberRequestPairs.add( |
| 579 | new SimpleEntry<Long, Integer>(lastFrameNumber, |
| 580 | requestId)); |
Eino-Ville Talvala | 848fe73 | 2014-09-15 16:24:08 -0700 | [diff] [blame^] | 581 | // It is possible that the last frame has already arrived, so we need to check |
| 582 | // for sequence completion right away |
| 583 | checkAndFireSequenceComplete(); |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 587 | private int submitCaptureRequest(List<CaptureRequest> requestList, CaptureCallback callback, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 588 | Handler handler, boolean repeating) throws CameraAccessException { |
| 589 | |
| 590 | // Need a valid handler, or current thread needs to have a looper, if |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 591 | // callback is valid |
| 592 | handler = checkHandler(handler, callback); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 593 | |
Ruben Brunk | 7f2372b | 2014-07-02 11:05:08 -0700 | [diff] [blame] | 594 | // Make sure that there all requests have at least 1 surface; all surfaces are non-null |
| 595 | for (CaptureRequest request : requestList) { |
| 596 | if (request.getTargets().isEmpty()) { |
| 597 | throw new IllegalArgumentException( |
| 598 | "Each request must have at least one Surface target"); |
| 599 | } |
| 600 | |
| 601 | for (Surface surface : request.getTargets()) { |
| 602 | if (surface == null) { |
| 603 | throw new IllegalArgumentException("Null Surface targets are not allowed"); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 608 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 609 | checkIfCameraClosedOrInError(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 610 | int requestId; |
| 611 | |
Ruben Brunk | e73b41b | 2013-11-07 19:30:43 -0800 | [diff] [blame] | 612 | if (repeating) { |
| 613 | stopRepeating(); |
| 614 | } |
| 615 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 616 | LongParcelable lastFrameNumberRef = new LongParcelable(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 617 | try { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 618 | requestId = mRemoteDevice.submitRequestList(requestList, repeating, |
| 619 | /*out*/lastFrameNumberRef); |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 620 | if (DEBUG) { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 621 | Log.v(TAG, "last frame number " + lastFrameNumberRef.getNumber()); |
| 622 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 623 | } catch (CameraRuntimeException e) { |
| 624 | throw e.asChecked(); |
| 625 | } catch (RemoteException e) { |
| 626 | // impossible |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 627 | return -1; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 628 | } |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 629 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 630 | if (callback != null) { |
| 631 | mCaptureCallbackMap.put(requestId, new CaptureCallbackHolder(callback, |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 632 | requestList, handler, repeating)); |
Jianing Wei | baf0c65 | 2014-04-18 17:35:00 -0700 | [diff] [blame] | 633 | } else { |
| 634 | if (DEBUG) { |
| 635 | Log.d(TAG, "Listen for request " + requestId + " is null"); |
| 636 | } |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 637 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 638 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 639 | long lastFrameNumber = lastFrameNumberRef.getNumber(); |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 640 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 641 | if (repeating) { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 642 | if (mRepeatingRequestId != REQUEST_ID_NONE) { |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 643 | checkEarlyTriggerSequenceComplete(mRepeatingRequestId, lastFrameNumber); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 644 | } |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 645 | mRepeatingRequestId = requestId; |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 646 | } else { |
| 647 | mFrameNumberRequestPairs.add( |
| 648 | new SimpleEntry<Long, Integer>(lastFrameNumber, requestId)); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 651 | if (mIdle) { |
| 652 | mDeviceHandler.post(mCallOnActive); |
| 653 | } |
| 654 | mIdle = false; |
| 655 | |
Igor Murashkin | 6bbf9dc | 2013-09-05 12:22:00 -0700 | [diff] [blame] | 656 | return requestId; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 660 | public int setRepeatingRequest(CaptureRequest request, CaptureCallback callback, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 661 | Handler handler) throws CameraAccessException { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 662 | List<CaptureRequest> requestList = new ArrayList<CaptureRequest>(); |
| 663 | requestList.add(request); |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 664 | return submitCaptureRequest(requestList, callback, handler, /*streaming*/true); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 667 | public int setRepeatingBurst(List<CaptureRequest> requests, CaptureCallback callback, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 668 | Handler handler) throws CameraAccessException { |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 669 | if (requests == null || requests.isEmpty()) { |
| 670 | throw new IllegalArgumentException("At least one request must be given"); |
Zhijun He | fc19e2c | 2013-08-22 14:43:07 -0700 | [diff] [blame] | 671 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 672 | return submitCaptureRequest(requests, callback, handler, /*streaming*/true); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 675 | public void stopRepeating() throws CameraAccessException { |
| 676 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 677 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 678 | checkIfCameraClosedOrInError(); |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 679 | if (mRepeatingRequestId != REQUEST_ID_NONE) { |
| 680 | |
| 681 | int requestId = mRepeatingRequestId; |
| 682 | mRepeatingRequestId = REQUEST_ID_NONE; |
| 683 | |
| 684 | // Queue for deletion after in-flight requests finish |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 685 | if (mCaptureCallbackMap.get(requestId) != null) { |
Zhijun He | 1a9b646 | 2014-03-31 16:11:33 -0700 | [diff] [blame] | 686 | mRepeatingRequestIdDeletedList.add(requestId); |
| 687 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 688 | |
| 689 | try { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 690 | LongParcelable lastFrameNumberRef = new LongParcelable(); |
| 691 | mRemoteDevice.cancelRequest(requestId, /*out*/lastFrameNumberRef); |
| 692 | long lastFrameNumber = lastFrameNumberRef.getNumber(); |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 693 | |
| 694 | checkEarlyTriggerSequenceComplete(requestId, lastFrameNumber); |
| 695 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 696 | } catch (CameraRuntimeException e) { |
| 697 | throw e.asChecked(); |
| 698 | } catch (RemoteException e) { |
| 699 | // impossible |
| 700 | return; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
Zhijun He | d842fcd | 2013-12-26 14:14:04 -0800 | [diff] [blame] | 706 | private void waitUntilIdle() throws CameraAccessException { |
Zhijun He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 707 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 708 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 709 | checkIfCameraClosedOrInError(); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 710 | |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 711 | if (mRepeatingRequestId != REQUEST_ID_NONE) { |
Zhijun He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 712 | throw new IllegalStateException("Active repeating request ongoing"); |
| 713 | } |
Zhijun He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 714 | try { |
| 715 | mRemoteDevice.waitUntilIdle(); |
| 716 | } catch (CameraRuntimeException e) { |
| 717 | throw e.asChecked(); |
| 718 | } catch (RemoteException e) { |
| 719 | // impossible |
| 720 | return; |
| 721 | } |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 722 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Eino-Ville Talvala | 8ebd52b | 2013-08-13 12:09:44 -0700 | [diff] [blame] | 725 | public void flush() throws CameraAccessException { |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 726 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 727 | checkIfCameraClosedOrInError(); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 728 | |
| 729 | mDeviceHandler.post(mCallOnBusy); |
Eino-Ville Talvala | c346bbf | 2014-08-27 17:05:29 -0700 | [diff] [blame] | 730 | |
| 731 | // If already idle, just do a busy->idle transition immediately, don't actually |
| 732 | // flush. |
| 733 | if (mIdle) { |
| 734 | mDeviceHandler.post(mCallOnIdle); |
| 735 | return; |
| 736 | } |
Eino-Ville Talvala | 8ebd52b | 2013-08-13 12:09:44 -0700 | [diff] [blame] | 737 | try { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 738 | LongParcelable lastFrameNumberRef = new LongParcelable(); |
| 739 | mRemoteDevice.flush(/*out*/lastFrameNumberRef); |
| 740 | if (mRepeatingRequestId != REQUEST_ID_NONE) { |
| 741 | long lastFrameNumber = lastFrameNumberRef.getNumber(); |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 742 | checkEarlyTriggerSequenceComplete(mRepeatingRequestId, lastFrameNumber); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 743 | mRepeatingRequestId = REQUEST_ID_NONE; |
| 744 | } |
Eino-Ville Talvala | 8ebd52b | 2013-08-13 12:09:44 -0700 | [diff] [blame] | 745 | } catch (CameraRuntimeException e) { |
| 746 | throw e.asChecked(); |
| 747 | } catch (RemoteException e) { |
| 748 | // impossible |
| 749 | return; |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | @Override |
Igor Murashkin | 5c9eaf6 | 2013-09-10 19:35:24 -0700 | [diff] [blame] | 755 | public void close() { |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 756 | synchronized (mInterfaceLock) { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 757 | try { |
Igor Murashkin | 2a3eced | 2013-08-28 17:35:10 -0700 | [diff] [blame] | 758 | if (mRemoteDevice != null) { |
| 759 | mRemoteDevice.disconnect(); |
| 760 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 761 | } catch (CameraRuntimeException e) { |
Igor Murashkin | 5c9eaf6 | 2013-09-10 19:35:24 -0700 | [diff] [blame] | 762 | Log.e(TAG, "Exception while closing: ", e.asChecked()); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 763 | } catch (RemoteException e) { |
| 764 | // impossible |
| 765 | } |
| 766 | |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 767 | // Only want to fire the onClosed callback once; |
| 768 | // either a normal close where the remote device is valid |
| 769 | // or a close after a startup error (no remote device but in error state) |
| 770 | if (mRemoteDevice != null || mInError) { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 771 | mDeviceHandler.post(mCallOnClosed); |
| 772 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 773 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 774 | mRemoteDevice = null; |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 775 | mInError = false; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
| 779 | @Override |
| 780 | protected void finalize() throws Throwable { |
| 781 | try { |
| 782 | close(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 783 | } |
| 784 | finally { |
| 785 | super.finalize(); |
| 786 | } |
| 787 | } |
| 788 | |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 789 | /** |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 790 | * <p>A callback for tracking the progress of a {@link CaptureRequest} |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 791 | * submitted to the camera device.</p> |
| 792 | * |
| 793 | */ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 794 | public static abstract class CaptureCallback { |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 795 | |
| 796 | /** |
| 797 | * This constant is used to indicate that no images were captured for |
| 798 | * the request. |
| 799 | * |
| 800 | * @hide |
| 801 | */ |
| 802 | public static final int NO_FRAMES_CAPTURED = -1; |
| 803 | |
| 804 | /** |
| 805 | * This method is called when the camera device has started capturing |
| 806 | * the output image for the request, at the beginning of image exposure. |
| 807 | * |
| 808 | * @see android.media.MediaActionSound |
| 809 | */ |
| 810 | public void onCaptureStarted(CameraDevice camera, |
Eino-Ville Talvala | e8df309 | 2014-09-08 14:30:50 -0700 | [diff] [blame] | 811 | CaptureRequest request, long timestamp, long frameNumber) { |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 812 | // default empty implementation |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * This method is called when some results from an image capture are |
| 817 | * available. |
| 818 | * |
| 819 | * @hide |
| 820 | */ |
| 821 | public void onCapturePartial(CameraDevice camera, |
| 822 | CaptureRequest request, CaptureResult result) { |
| 823 | // default empty implementation |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * This method is called when an image capture makes partial forward progress; some |
| 828 | * (but not all) results from an image capture are available. |
| 829 | * |
| 830 | */ |
| 831 | public void onCaptureProgressed(CameraDevice camera, |
| 832 | CaptureRequest request, CaptureResult partialResult) { |
| 833 | // default empty implementation |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * This method is called when an image capture has fully completed and all the |
| 838 | * result metadata is available. |
| 839 | */ |
| 840 | public void onCaptureCompleted(CameraDevice camera, |
| 841 | CaptureRequest request, TotalCaptureResult result) { |
| 842 | // default empty implementation |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * This method is called instead of {@link #onCaptureCompleted} when the |
| 847 | * camera device failed to produce a {@link CaptureResult} for the |
| 848 | * request. |
| 849 | */ |
| 850 | public void onCaptureFailed(CameraDevice camera, |
| 851 | CaptureRequest request, CaptureFailure failure) { |
| 852 | // default empty implementation |
| 853 | } |
| 854 | |
| 855 | /** |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 856 | * This method is called independently of the others in CaptureCallback, |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 857 | * when a capture sequence finishes and all {@link CaptureResult} |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 858 | * or {@link CaptureFailure} for it have been returned via this callback. |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 859 | */ |
| 860 | public void onCaptureSequenceCompleted(CameraDevice camera, |
| 861 | int sequenceId, long frameNumber) { |
| 862 | // default empty implementation |
| 863 | } |
| 864 | |
| 865 | /** |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 866 | * This method is called independently of the others in CaptureCallback, |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 867 | * when a capture sequence aborts before any {@link CaptureResult} |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 868 | * or {@link CaptureFailure} for it have been returned via this callback. |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 869 | */ |
| 870 | public void onCaptureSequenceAborted(CameraDevice camera, |
| 871 | int sequenceId) { |
| 872 | // default empty implementation |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | /** |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 877 | * A callback for notifications about the state of a camera device, adding in the callbacks that |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 878 | * were part of the earlier KK API design, but now only used internally. |
| 879 | */ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 880 | public static abstract class StateCallbackKK extends StateCallback { |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 881 | /** |
| 882 | * The method called when a camera device has no outputs configured. |
| 883 | * |
| 884 | */ |
| 885 | public void onUnconfigured(CameraDevice camera) { |
| 886 | // Default empty implementation |
| 887 | } |
| 888 | |
| 889 | /** |
| 890 | * The method called when a camera device begins processing |
| 891 | * {@link CaptureRequest capture requests}. |
| 892 | * |
| 893 | */ |
| 894 | public void onActive(CameraDevice camera) { |
| 895 | // Default empty implementation |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * The method called when a camera device is busy. |
| 900 | * |
| 901 | */ |
| 902 | public void onBusy(CameraDevice camera) { |
| 903 | // Default empty implementation |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * The method called when a camera device has finished processing all |
| 908 | * submitted capture requests and has reached an idle state. |
| 909 | * |
| 910 | */ |
| 911 | public void onIdle(CameraDevice camera) { |
| 912 | // Default empty implementation |
| 913 | } |
| 914 | } |
| 915 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 916 | static class CaptureCallbackHolder { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 917 | |
| 918 | private final boolean mRepeating; |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 919 | private final CaptureCallback mCallback; |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 920 | private final List<CaptureRequest> mRequestList; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 921 | private final Handler mHandler; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 922 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 923 | CaptureCallbackHolder(CaptureCallback callback, List<CaptureRequest> requestList, |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 924 | Handler handler, boolean repeating) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 925 | if (callback == null || handler == null) { |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 926 | throw new UnsupportedOperationException( |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 927 | "Must have a valid handler and a valid callback"); |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 928 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 929 | mRepeating = repeating; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 930 | mHandler = handler; |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 931 | mRequestList = new ArrayList<CaptureRequest>(requestList); |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 932 | mCallback = callback; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | public boolean isRepeating() { |
| 936 | return mRepeating; |
| 937 | } |
| 938 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 939 | public CaptureCallback getCallback() { |
| 940 | return mCallback; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 943 | public CaptureRequest getRequest(int subsequenceId) { |
| 944 | if (subsequenceId >= mRequestList.size()) { |
| 945 | throw new IllegalArgumentException( |
| 946 | String.format( |
| 947 | "Requested subsequenceId %d is larger than request list size %d.", |
| 948 | subsequenceId, mRequestList.size())); |
| 949 | } else { |
| 950 | if (subsequenceId < 0) { |
| 951 | throw new IllegalArgumentException(String.format( |
| 952 | "Requested subsequenceId %d is negative", subsequenceId)); |
| 953 | } else { |
| 954 | return mRequestList.get(subsequenceId); |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 959 | public CaptureRequest getRequest() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 960 | return getRequest(0); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 961 | } |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 962 | |
| 963 | public Handler getHandler() { |
| 964 | return mHandler; |
| 965 | } |
| 966 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 969 | /** |
| 970 | * This class tracks the last frame number for submitted requests. |
| 971 | */ |
| 972 | public class FrameNumberTracker { |
| 973 | |
| 974 | private long mCompletedFrameNumber = -1; |
| 975 | private final TreeSet<Long> mFutureErrorSet = new TreeSet<Long>(); |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 976 | /** Map frame numbers to list of partial results */ |
| 977 | private final HashMap<Long, List<CaptureResult>> mPartialResults = new HashMap<>(); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 978 | |
| 979 | private void update() { |
| 980 | Iterator<Long> iter = mFutureErrorSet.iterator(); |
| 981 | while (iter.hasNext()) { |
| 982 | long errorFrameNumber = iter.next(); |
| 983 | if (errorFrameNumber == mCompletedFrameNumber + 1) { |
| 984 | mCompletedFrameNumber++; |
| 985 | iter.remove(); |
| 986 | } else { |
| 987 | break; |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | /** |
| 993 | * This function is called every time when a result or an error is received. |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 994 | * @param frameNumber the frame number corresponding to the result or error |
| 995 | * @param isError true if it is an error, false if it is not an error |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 996 | */ |
| 997 | public void updateTracker(long frameNumber, boolean isError) { |
| 998 | if (isError) { |
| 999 | mFutureErrorSet.add(frameNumber); |
| 1000 | } else { |
| 1001 | /** |
| 1002 | * HAL cannot send an OnResultReceived for frame N unless it knows for |
| 1003 | * sure that all frames prior to N have either errored out or completed. |
| 1004 | * So if the current frame is not an error, then all previous frames |
| 1005 | * should have arrived. The following line checks whether this holds. |
| 1006 | */ |
| 1007 | if (frameNumber != mCompletedFrameNumber + 1) { |
Zhijun He | 22589b4 | 2014-05-01 10:37:36 -0700 | [diff] [blame] | 1008 | Log.e(TAG, String.format( |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 1009 | "result frame number %d comes out of order, should be %d + 1", |
| 1010 | frameNumber, mCompletedFrameNumber)); |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 1011 | // Continue on to set the completed frame number to this frame anyway, |
| 1012 | // to be robust to lower-level errors and allow for clean shutdowns. |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1013 | } |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 1014 | mCompletedFrameNumber = frameNumber; |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1015 | } |
| 1016 | update(); |
| 1017 | } |
| 1018 | |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1019 | /** |
| 1020 | * This function is called every time a result has been completed. |
| 1021 | * |
| 1022 | * <p>It keeps a track of all the partial results already created for a particular |
| 1023 | * frame number.</p> |
| 1024 | * |
| 1025 | * @param frameNumber the frame number corresponding to the result |
| 1026 | * @param result the total or partial result |
| 1027 | * @param partial {@true} if the result is partial, {@code false} if total |
| 1028 | */ |
| 1029 | public void updateTracker(long frameNumber, CaptureResult result, boolean partial) { |
| 1030 | |
| 1031 | if (!partial) { |
| 1032 | // Update the total result's frame status as being successful |
| 1033 | updateTracker(frameNumber, /*isError*/false); |
| 1034 | // Don't keep a list of total results, we don't need to track them |
| 1035 | return; |
| 1036 | } |
| 1037 | |
| 1038 | if (result == null) { |
| 1039 | // Do not record blank results; this also means there will be no total result |
| 1040 | // so it doesn't matter that the partials were not recorded |
| 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | // Partial results must be aggregated in-order for that frame number |
| 1045 | List<CaptureResult> partials = mPartialResults.get(frameNumber); |
| 1046 | if (partials == null) { |
| 1047 | partials = new ArrayList<>(); |
| 1048 | mPartialResults.put(frameNumber, partials); |
| 1049 | } |
| 1050 | |
| 1051 | partials.add(result); |
| 1052 | } |
| 1053 | |
| 1054 | /** |
| 1055 | * Attempt to pop off all of the partial results seen so far for the {@code frameNumber}. |
| 1056 | * |
| 1057 | * <p>Once popped-off, the partial results are forgotten (unless {@code updateTracker} |
| 1058 | * is called again with new partials for that frame number).</p> |
| 1059 | * |
| 1060 | * @param frameNumber the frame number corresponding to the result |
| 1061 | * @return a list of partial results for that frame with at least 1 element, |
| 1062 | * or {@code null} if there were no partials recorded for that frame |
| 1063 | */ |
| 1064 | public List<CaptureResult> popPartialResults(long frameNumber) { |
| 1065 | return mPartialResults.remove(frameNumber); |
| 1066 | } |
| 1067 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1068 | public long getCompletedFrameNumber() { |
| 1069 | return mCompletedFrameNumber; |
| 1070 | } |
| 1071 | |
| 1072 | } |
| 1073 | |
| 1074 | private void checkAndFireSequenceComplete() { |
| 1075 | long completedFrameNumber = mFrameNumberTracker.getCompletedFrameNumber(); |
| 1076 | Iterator<SimpleEntry<Long, Integer> > iter = mFrameNumberRequestPairs.iterator(); |
| 1077 | while (iter.hasNext()) { |
| 1078 | final SimpleEntry<Long, Integer> frameNumberRequestPair = iter.next(); |
| 1079 | if (frameNumberRequestPair.getKey() <= completedFrameNumber) { |
| 1080 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1081 | // remove request from mCaptureCallbackMap |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1082 | final int requestId = frameNumberRequestPair.getValue(); |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1083 | final CaptureCallbackHolder holder; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1084 | synchronized(mInterfaceLock) { |
| 1085 | if (mRemoteDevice == null) { |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1086 | Log.w(TAG, "Camera closed while checking sequences"); |
| 1087 | return; |
| 1088 | } |
| 1089 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1090 | int index = mCaptureCallbackMap.indexOfKey(requestId); |
| 1091 | holder = (index >= 0) ? mCaptureCallbackMap.valueAt(index) |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1092 | : null; |
| 1093 | if (holder != null) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1094 | mCaptureCallbackMap.removeAt(index); |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 1095 | if (DEBUG) { |
| 1096 | Log.v(TAG, String.format( |
| 1097 | "remove holder for requestId %d, " |
| 1098 | + "because lastFrame %d is <= %d", |
| 1099 | requestId, frameNumberRequestPair.getKey(), |
| 1100 | completedFrameNumber)); |
| 1101 | } |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | iter.remove(); |
| 1105 | |
| 1106 | // Call onCaptureSequenceCompleted |
| 1107 | if (holder != null) { |
| 1108 | Runnable resultDispatch = new Runnable() { |
| 1109 | @Override |
| 1110 | public void run() { |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 1111 | if (!CameraDeviceImpl.this.isClosed()){ |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1112 | if (DEBUG) { |
| 1113 | Log.d(TAG, String.format( |
| 1114 | "fire sequence complete for request %d", |
| 1115 | requestId)); |
| 1116 | } |
| 1117 | |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 1118 | long lastFrameNumber = frameNumberRequestPair.getKey(); |
| 1119 | if (lastFrameNumber < Integer.MIN_VALUE |
| 1120 | || lastFrameNumber > Integer.MAX_VALUE) { |
| 1121 | throw new AssertionError(lastFrameNumber |
| 1122 | + " cannot be cast to int"); |
| 1123 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1124 | holder.getCallback().onCaptureSequenceCompleted( |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 1125 | CameraDeviceImpl.this, |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1126 | requestId, |
Igor Murashkin | db075af | 2014-05-21 10:07:08 -0700 | [diff] [blame] | 1127 | lastFrameNumber); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | }; |
| 1131 | holder.getHandler().post(resultDispatch); |
| 1132 | } |
| 1133 | |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 1138 | public class CameraDeviceCallbacks extends ICameraDeviceCallbacks.Stub { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1139 | |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1140 | // |
| 1141 | // Constants below need to be kept up-to-date with |
| 1142 | // frameworks/av/include/camera/camera2/ICameraDeviceCallbacks.h |
| 1143 | // |
| 1144 | |
| 1145 | // |
| 1146 | // Error codes for onCameraError |
| 1147 | // |
| 1148 | |
| 1149 | /** |
| 1150 | * Camera has been disconnected |
| 1151 | */ |
| 1152 | static final int ERROR_CAMERA_DISCONNECTED = 0; |
| 1153 | |
| 1154 | /** |
| 1155 | * Camera has encountered a device-level error |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1156 | * Matches CameraDevice.StateCallback#ERROR_CAMERA_DEVICE |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1157 | */ |
| 1158 | static final int ERROR_CAMERA_DEVICE = 1; |
| 1159 | |
| 1160 | /** |
| 1161 | * Camera has encountered a service-level error |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1162 | * Matches CameraDevice.StateCallback#ERROR_CAMERA_SERVICE |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1163 | */ |
| 1164 | static final int ERROR_CAMERA_SERVICE = 2; |
| 1165 | |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1166 | /** |
| 1167 | * Camera has encountered an error processing a single request. |
| 1168 | */ |
| 1169 | static final int ERROR_CAMERA_REQUEST = 3; |
| 1170 | |
| 1171 | /** |
| 1172 | * Camera has encountered an error producing metadata for a single capture |
| 1173 | */ |
| 1174 | static final int ERROR_CAMERA_RESULT = 4; |
| 1175 | |
| 1176 | /** |
| 1177 | * Camera has encountered an error producing an image buffer for a single capture |
| 1178 | */ |
| 1179 | static final int ERROR_CAMERA_BUFFER = 5; |
| 1180 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1181 | @Override |
| 1182 | public IBinder asBinder() { |
| 1183 | return this; |
| 1184 | } |
| 1185 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1186 | @Override |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1187 | public void onDeviceError(final int errorCode, CaptureResultExtras resultExtras) { |
| 1188 | if (DEBUG) { |
| 1189 | Log.d(TAG, String.format( |
| 1190 | "Device error received, code %d, frame number %d, request ID %d, subseq ID %d", |
| 1191 | errorCode, resultExtras.getFrameNumber(), resultExtras.getRequestId(), |
| 1192 | resultExtras.getSubsequenceId())); |
| 1193 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1194 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1195 | synchronized(mInterfaceLock) { |
| 1196 | if (mRemoteDevice == null) { |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1197 | return; // Camera already closed |
| 1198 | } |
| 1199 | |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1200 | switch (errorCode) { |
| 1201 | case ERROR_CAMERA_DISCONNECTED: |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1202 | CameraDeviceImpl.this.mDeviceHandler.post(mCallOnDisconnected); |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1203 | break; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1204 | default: |
| 1205 | Log.e(TAG, "Unknown error from camera device: " + errorCode); |
| 1206 | // no break |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1207 | case ERROR_CAMERA_DEVICE: |
| 1208 | case ERROR_CAMERA_SERVICE: |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1209 | mInError = true; |
| 1210 | Runnable r = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1211 | @Override |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1212 | public void run() { |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 1213 | if (!CameraDeviceImpl.this.isClosed()) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1214 | mDeviceCallback.onError(CameraDeviceImpl.this, errorCode); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1215 | } |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1216 | } |
| 1217 | }; |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1218 | CameraDeviceImpl.this.mDeviceHandler.post(r); |
| 1219 | break; |
| 1220 | case ERROR_CAMERA_REQUEST: |
| 1221 | case ERROR_CAMERA_RESULT: |
| 1222 | case ERROR_CAMERA_BUFFER: |
| 1223 | onCaptureErrorLocked(errorCode, resultExtras); |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1224 | break; |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1225 | } |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 1226 | } |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | @Override |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1230 | public void onDeviceIdle() { |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1231 | if (DEBUG) { |
| 1232 | Log.d(TAG, "Camera now idle"); |
| 1233 | } |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1234 | synchronized(mInterfaceLock) { |
| 1235 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1236 | |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 1237 | if (!CameraDeviceImpl.this.mIdle) { |
| 1238 | CameraDeviceImpl.this.mDeviceHandler.post(mCallOnIdle); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1239 | } |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 1240 | CameraDeviceImpl.this.mIdle = true; |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | @Override |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1245 | public void onCaptureStarted(final CaptureResultExtras resultExtras, final long timestamp) { |
| 1246 | int requestId = resultExtras.getRequestId(); |
Eino-Ville Talvala | e8df309 | 2014-09-08 14:30:50 -0700 | [diff] [blame] | 1247 | final long frameNumber = resultExtras.getFrameNumber(); |
| 1248 | |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1249 | if (DEBUG) { |
Eino-Ville Talvala | e8df309 | 2014-09-08 14:30:50 -0700 | [diff] [blame] | 1250 | Log.d(TAG, "Capture started for id " + requestId + " frame number " + frameNumber); |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1251 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1252 | final CaptureCallbackHolder holder; |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1253 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1254 | synchronized(mInterfaceLock) { |
| 1255 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1256 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1257 | // Get the callback for this frame ID, if there is one |
| 1258 | holder = CameraDeviceImpl.this.mCaptureCallbackMap.get(requestId); |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1259 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1260 | if (holder == null) { |
| 1261 | return; |
| 1262 | } |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1263 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1264 | if (isClosed()) return; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1265 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1266 | // Dispatch capture start notice |
| 1267 | holder.getHandler().post( |
| 1268 | new Runnable() { |
| 1269 | @Override |
| 1270 | public void run() { |
| 1271 | if (!CameraDeviceImpl.this.isClosed()) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1272 | holder.getCallback().onCaptureStarted( |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1273 | CameraDeviceImpl.this, |
| 1274 | holder.getRequest(resultExtras.getSubsequenceId()), |
Eino-Ville Talvala | e8df309 | 2014-09-08 14:30:50 -0700 | [diff] [blame] | 1275 | timestamp, frameNumber); |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1276 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1277 | } |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1278 | }); |
| 1279 | |
| 1280 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | @Override |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1284 | public void onResultReceived(CameraMetadataNative result, |
| 1285 | CaptureResultExtras resultExtras) throws RemoteException { |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 1286 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1287 | int requestId = resultExtras.getRequestId(); |
Igor Murashkin | bdf366c | 2014-07-25 16:54:20 -0700 | [diff] [blame] | 1288 | long frameNumber = resultExtras.getFrameNumber(); |
| 1289 | |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 1290 | if (DEBUG) { |
Igor Murashkin | bdf366c | 2014-07-25 16:54:20 -0700 | [diff] [blame] | 1291 | Log.v(TAG, "Received result frame " + frameNumber + " for id " |
Jianing Wei | baf0c65 | 2014-04-18 17:35:00 -0700 | [diff] [blame] | 1292 | + requestId); |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 1293 | } |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 1294 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1295 | synchronized(mInterfaceLock) { |
| 1296 | if (mRemoteDevice == null) return; // Camera already closed |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 1297 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1298 | // TODO: Handle CameraCharacteristics access from CaptureResult correctly. |
| 1299 | result.set(CameraCharacteristics.LENS_INFO_SHADING_MAP_SIZE, |
| 1300 | getCharacteristics().get(CameraCharacteristics.LENS_INFO_SHADING_MAP_SIZE)); |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 1301 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1302 | final CaptureCallbackHolder holder = |
| 1303 | CameraDeviceImpl.this.mCaptureCallbackMap.get(requestId); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1304 | |
Zhijun He | 8315915 | 2014-07-16 11:32:59 -0700 | [diff] [blame] | 1305 | boolean isPartialResult = |
| 1306 | (resultExtras.getPartialResultCount() < mTotalPartialCount); |
Eino-Ville Talvala | 7a31310 | 2013-11-07 14:45:06 -0800 | [diff] [blame] | 1307 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1308 | // Check if we have a callback for this |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1309 | if (holder == null) { |
| 1310 | if (DEBUG) { |
| 1311 | Log.d(TAG, |
| 1312 | "holder is null, early return at frame " |
Igor Murashkin | bdf366c | 2014-07-25 16:54:20 -0700 | [diff] [blame] | 1313 | + frameNumber); |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1314 | } |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1315 | |
| 1316 | mFrameNumberTracker.updateTracker(frameNumber, /*result*/null, isPartialResult); |
| 1317 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1318 | return; |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 1319 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1320 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1321 | if (isClosed()) { |
| 1322 | if (DEBUG) { |
| 1323 | Log.d(TAG, |
| 1324 | "camera is closed, early return at frame " |
Igor Murashkin | bdf366c | 2014-07-25 16:54:20 -0700 | [diff] [blame] | 1325 | + frameNumber); |
Eino-Ville Talvala | 7a31310 | 2013-11-07 14:45:06 -0800 | [diff] [blame] | 1326 | } |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1327 | |
| 1328 | mFrameNumberTracker.updateTracker(frameNumber, /*result*/null, isPartialResult); |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1329 | return; |
| 1330 | } |
Igor Murashkin | db075af | 2014-05-21 10:07:08 -0700 | [diff] [blame] | 1331 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1332 | final CaptureRequest request = holder.getRequest(resultExtras.getSubsequenceId()); |
| 1333 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1334 | Runnable resultDispatch = null; |
| 1335 | |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1336 | CaptureResult finalResult; |
| 1337 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1338 | // Either send a partial result or the final capture completed result |
Zhijun He | 8315915 | 2014-07-16 11:32:59 -0700 | [diff] [blame] | 1339 | if (isPartialResult) { |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1340 | final CaptureResult resultAsCapture = |
Igor Murashkin | bdf366c | 2014-07-25 16:54:20 -0700 | [diff] [blame] | 1341 | new CaptureResult(result, request, resultExtras); |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1342 | |
| 1343 | // Partial result |
| 1344 | resultDispatch = new Runnable() { |
| 1345 | @Override |
| 1346 | public void run() { |
| 1347 | if (!CameraDeviceImpl.this.isClosed()){ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1348 | holder.getCallback().onCaptureProgressed( |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1349 | CameraDeviceImpl.this, |
| 1350 | request, |
| 1351 | resultAsCapture); |
| 1352 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1353 | } |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1354 | }; |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1355 | |
| 1356 | finalResult = resultAsCapture; |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1357 | } else { |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1358 | List<CaptureResult> partialResults = |
| 1359 | mFrameNumberTracker.popPartialResults(frameNumber); |
| 1360 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1361 | final TotalCaptureResult resultAsCapture = |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1362 | new TotalCaptureResult(result, request, resultExtras, partialResults); |
Eino-Ville Talvala | 7a31310 | 2013-11-07 14:45:06 -0800 | [diff] [blame] | 1363 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1364 | // Final capture result |
| 1365 | resultDispatch = new Runnable() { |
| 1366 | @Override |
| 1367 | public void run() { |
| 1368 | if (!CameraDeviceImpl.this.isClosed()){ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1369 | holder.getCallback().onCaptureCompleted( |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1370 | CameraDeviceImpl.this, |
| 1371 | request, |
| 1372 | resultAsCapture); |
| 1373 | } |
| 1374 | } |
| 1375 | }; |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1376 | |
| 1377 | finalResult = resultAsCapture; |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1378 | } |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1379 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1380 | holder.getHandler().post(resultDispatch); |
| 1381 | |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1382 | // Collect the partials for a total result; or mark the frame as totally completed |
| 1383 | mFrameNumberTracker.updateTracker(frameNumber, finalResult, isPartialResult); |
| 1384 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1385 | // Fire onCaptureSequenceCompleted |
Zhijun He | 8315915 | 2014-07-16 11:32:59 -0700 | [diff] [blame] | 1386 | if (!isPartialResult) { |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1387 | checkAndFireSequenceComplete(); |
| 1388 | } |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1389 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1392 | /** |
| 1393 | * Called by onDeviceError for handling single-capture failures. |
| 1394 | */ |
| 1395 | private void onCaptureErrorLocked(int errorCode, CaptureResultExtras resultExtras) { |
| 1396 | |
| 1397 | final int requestId = resultExtras.getRequestId(); |
| 1398 | final int subsequenceId = resultExtras.getSubsequenceId(); |
| 1399 | final long frameNumber = resultExtras.getFrameNumber(); |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1400 | final CaptureCallbackHolder holder = |
| 1401 | CameraDeviceImpl.this.mCaptureCallbackMap.get(requestId); |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1402 | |
| 1403 | final CaptureRequest request = holder.getRequest(subsequenceId); |
| 1404 | |
| 1405 | // No way to report buffer errors right now |
| 1406 | if (errorCode == ERROR_CAMERA_BUFFER) { |
| 1407 | Log.e(TAG, String.format("Lost output buffer reported for frame %d", frameNumber)); |
| 1408 | return; |
| 1409 | } |
| 1410 | |
| 1411 | boolean mayHaveBuffers = (errorCode == ERROR_CAMERA_RESULT); |
| 1412 | |
| 1413 | // This is only approximate - exact handling needs the camera service and HAL to |
| 1414 | // disambiguate between request failures to due abort and due to real errors. |
| 1415 | // For now, assume that if the session believes we're mid-abort, then the error |
| 1416 | // is due to abort. |
| 1417 | int reason = (mCurrentSession != null && mCurrentSession.isAborting()) ? |
| 1418 | CaptureFailure.REASON_FLUSHED : |
| 1419 | CaptureFailure.REASON_ERROR; |
| 1420 | |
| 1421 | final CaptureFailure failure = new CaptureFailure( |
| 1422 | request, |
| 1423 | reason, |
| 1424 | /*dropped*/ mayHaveBuffers, |
| 1425 | requestId, |
| 1426 | frameNumber); |
| 1427 | |
| 1428 | Runnable failureDispatch = new Runnable() { |
| 1429 | @Override |
| 1430 | public void run() { |
| 1431 | if (!CameraDeviceImpl.this.isClosed()){ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1432 | holder.getCallback().onCaptureFailed( |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1433 | CameraDeviceImpl.this, |
| 1434 | request, |
| 1435 | failure); |
| 1436 | } |
| 1437 | } |
| 1438 | }; |
| 1439 | holder.getHandler().post(failureDispatch); |
| 1440 | |
| 1441 | // Fire onCaptureSequenceCompleted if appropriate |
| 1442 | if (DEBUG) { |
| 1443 | Log.v(TAG, String.format("got error frame %d", frameNumber)); |
| 1444 | } |
| 1445 | mFrameNumberTracker.updateTracker(frameNumber, /*error*/true); |
| 1446 | checkAndFireSequenceComplete(); |
| 1447 | } |
| 1448 | |
| 1449 | } // public class CameraDeviceCallbacks |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1450 | |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1451 | /** |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 1452 | * Default handler management. |
| 1453 | * |
| 1454 | * <p> |
| 1455 | * If handler is null, get the current thread's |
| 1456 | * Looper to create a Handler with. If no looper exists, throw {@code IllegalArgumentException}. |
| 1457 | * </p> |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1458 | */ |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 1459 | static Handler checkHandler(Handler handler) { |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1460 | if (handler == null) { |
| 1461 | Looper looper = Looper.myLooper(); |
| 1462 | if (looper == null) { |
| 1463 | throw new IllegalArgumentException( |
| 1464 | "No handler given, and current thread has no looper!"); |
| 1465 | } |
| 1466 | handler = new Handler(looper); |
| 1467 | } |
| 1468 | return handler; |
| 1469 | } |
| 1470 | |
Eino-Ville Talvala | 7875a88 | 2014-07-31 12:47:07 -0700 | [diff] [blame] | 1471 | /** |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1472 | * Default handler management, conditional on there being a callback. |
Eino-Ville Talvala | 7875a88 | 2014-07-31 12:47:07 -0700 | [diff] [blame] | 1473 | * |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1474 | * <p>If the callback isn't null, check the handler, otherwise pass it through.</p> |
Eino-Ville Talvala | 7875a88 | 2014-07-31 12:47:07 -0700 | [diff] [blame] | 1475 | */ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1476 | static <T> Handler checkHandler(Handler handler, T callback) { |
| 1477 | if (callback != null) { |
Eino-Ville Talvala | 7875a88 | 2014-07-31 12:47:07 -0700 | [diff] [blame] | 1478 | return checkHandler(handler); |
| 1479 | } |
| 1480 | return handler; |
| 1481 | } |
| 1482 | |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 1483 | private void checkIfCameraClosedOrInError() throws CameraAccessException { |
| 1484 | if (mInError) { |
| 1485 | throw new CameraAccessException(CameraAccessException.CAMERA_ERROR, |
| 1486 | "The camera device has encountered a serious error"); |
| 1487 | } |
Zhijun He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 1488 | if (mRemoteDevice == null) { |
| 1489 | throw new IllegalStateException("CameraDevice was already closed"); |
| 1490 | } |
| 1491 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1492 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1493 | /** Whether the camera device has started to close (may not yet have finished) */ |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1494 | private boolean isClosed() { |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1495 | return mClosing; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1496 | } |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 1497 | |
| 1498 | private CameraCharacteristics getCharacteristics() { |
| 1499 | return mCharacteristics; |
| 1500 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1501 | } |