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