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