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