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 | |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 19 | import static com.android.internal.util.function.pooled.PooledLambda.obtainRunnable; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 20 | |
Shuzhen Wang | 23d2938 | 2017-11-26 17:24:56 -0800 | [diff] [blame] | 21 | import android.hardware.ICameraService; |
Eino-Ville Talvala | 2f1a2e4 | 2013-07-25 17:12:05 -0700 | [diff] [blame] | 22 | import android.hardware.camera2.CameraAccessException; |
Eino-Ville Talvala | cca00c6 | 2014-05-14 10:53:20 -0700 | [diff] [blame] | 23 | import android.hardware.camera2.CameraCaptureSession; |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 24 | import android.hardware.camera2.CameraCharacteristics; |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 25 | import android.hardware.camera2.CameraDevice; |
Shuzhen Wang | 23d2938 | 2017-11-26 17:24:56 -0800 | [diff] [blame] | 26 | import android.hardware.camera2.CaptureFailure; |
Eino-Ville Talvala | 2f1a2e4 | 2013-07-25 17:12:05 -0700 | [diff] [blame] | 27 | import android.hardware.camera2.CaptureRequest; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 28 | import android.hardware.camera2.CaptureResult; |
| 29 | import android.hardware.camera2.ICameraDeviceCallbacks; |
| 30 | import android.hardware.camera2.ICameraDeviceUser; |
Igor Murashkin | db075af | 2014-05-21 10:07:08 -0700 | [diff] [blame] | 31 | import android.hardware.camera2.TotalCaptureResult; |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 32 | import android.hardware.camera2.params.InputConfiguration; |
Yin-Chia Yeh | 49ea6ae | 2015-03-09 16:53:14 -0700 | [diff] [blame] | 33 | import android.hardware.camera2.params.OutputConfiguration; |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 34 | import android.hardware.camera2.params.SessionConfiguration; |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 35 | import android.hardware.camera2.params.StreamConfigurationMap; |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 36 | import android.hardware.camera2.utils.SubmitInfo; |
Zhijun He | a767772 | 2015-06-01 16:36:06 -0700 | [diff] [blame] | 37 | import android.hardware.camera2.utils.SurfaceUtils; |
Chien-Yu Chen | 3d2b5fe | 2017-04-05 16:42:31 -0700 | [diff] [blame] | 38 | import android.os.Build; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 39 | import android.os.Handler; |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 40 | import android.os.IBinder; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 41 | import android.os.Looper; |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 42 | import android.os.RemoteException; |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 43 | import android.os.ServiceSpecificException; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 44 | import android.util.Log; |
Zhijun He | a767772 | 2015-06-01 16:36:06 -0700 | [diff] [blame] | 45 | import android.util.Range; |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 46 | import android.util.Size; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 47 | import android.util.SparseArray; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 48 | import android.view.Surface; |
| 49 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 50 | import java.util.AbstractMap.SimpleEntry; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 51 | import java.util.ArrayList; |
Zhijun He | a767772 | 2015-06-01 16:36:06 -0700 | [diff] [blame] | 52 | import java.util.Collection; |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 53 | import java.util.HashMap; |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 54 | import java.util.HashSet; |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 55 | import java.util.Iterator; |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 56 | import java.util.LinkedList; |
Shuzhen Wang | 23d2938 | 2017-11-26 17:24:56 -0800 | [diff] [blame] | 57 | import java.util.List; |
Emilian Peev | 2100ae7 | 2018-01-12 16:56:25 +0000 | [diff] [blame] | 58 | import java.util.Set; |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 59 | import java.util.TreeMap; |
Shuzhen Wang | 23d2938 | 2017-11-26 17:24:56 -0800 | [diff] [blame] | 60 | import java.util.concurrent.atomic.AtomicBoolean; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 63 | * HAL2.1+ implementation of CameraDevice. Use CameraManager#open to instantiate |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 64 | */ |
Shuzhen Wang | 7839293 | 2016-03-04 15:26:01 -0800 | [diff] [blame] | 65 | public class CameraDeviceImpl extends CameraDevice |
| 66 | implements IBinder.DeathRecipient { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 67 | private final String TAG; |
Eino-Ville Talvala | a78791f | 2015-06-01 12:39:54 -0700 | [diff] [blame] | 68 | private final boolean DEBUG = false; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 69 | |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 70 | private static final int REQUEST_ID_NONE = -1; |
| 71 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 72 | // TODO: guard every function with if (!mRemoteDevice) check (if it was closed) |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 73 | private ICameraDeviceUserWrapper mRemoteDevice; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 74 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 75 | // Lock to synchronize cross-thread access to device public interface |
Igor Murashkin | 51dcfd65 | 2014-09-25 16:55:01 -0700 | [diff] [blame] | 76 | final Object mInterfaceLock = new Object(); // access from this class and Session only! |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 77 | private final CameraDeviceCallbacks mCallbacks = new CameraDeviceCallbacks(); |
| 78 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 79 | private final StateCallback mDeviceCallback; |
| 80 | private volatile StateCallbackKK mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 81 | private final Handler mDeviceHandler; |
| 82 | |
Ruben Brunk | 0953b64 | 2015-06-11 16:12:35 -0700 | [diff] [blame] | 83 | private final AtomicBoolean mClosing = new AtomicBoolean(); |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 84 | private boolean mInError = false; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 85 | private boolean mIdle = true; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 86 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 87 | /** map request IDs to callback/request data */ |
| 88 | private final SparseArray<CaptureCallbackHolder> mCaptureCallbackMap = |
| 89 | new SparseArray<CaptureCallbackHolder>(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 90 | |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 91 | private int mRepeatingRequestId = REQUEST_ID_NONE; |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 92 | // Map stream IDs to input/output configurations |
| 93 | private SimpleEntry<Integer, InputConfiguration> mConfiguredInput = |
| 94 | new SimpleEntry<>(REQUEST_ID_NONE, null); |
Yin-Chia Yeh | bfbbee7 | 2015-03-12 13:39:26 -0700 | [diff] [blame] | 95 | private final SparseArray<OutputConfiguration> mConfiguredOutputs = |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 96 | new SparseArray<>(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 97 | |
| 98 | private final String mCameraId; |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 99 | private final CameraCharacteristics mCharacteristics; |
Zhijun He | 8315915 | 2014-07-16 11:32:59 -0700 | [diff] [blame] | 100 | private final int mTotalPartialCount; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 101 | |
Shuzhen Wang | ae10eb9 | 2017-03-01 12:58:04 -0800 | [diff] [blame] | 102 | private static final long NANO_PER_SECOND = 1000000000; //ns |
| 103 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 104 | /** |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 105 | * A list tracking request and its expected last regular frame number and last reprocess frame |
| 106 | * number. Updated when calling ICameraDeviceUser methods. |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 107 | */ |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 108 | private final List<RequestLastFrameNumbersHolder> mRequestLastFrameNumbersList = |
| 109 | new ArrayList<>(); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * An object tracking received frame numbers. |
| 113 | * Updated when receiving callbacks from ICameraDeviceCallbacks. |
| 114 | */ |
| 115 | private final FrameNumberTracker mFrameNumberTracker = new FrameNumberTracker(); |
| 116 | |
Eino-Ville Talvala | 639fffe | 2015-06-30 10:34:48 -0700 | [diff] [blame] | 117 | private CameraCaptureSessionCore mCurrentSession; |
Eino-Ville Talvala | 9503785 | 2014-09-14 14:07:05 -0700 | [diff] [blame] | 118 | private int mNextSessionId = 0; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 119 | |
Chien-Yu Chen | 3d2b5fe | 2017-04-05 16:42:31 -0700 | [diff] [blame] | 120 | private final int mAppTargetSdkVersion; |
| 121 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 122 | // Runnables for all state transitions, except error, which needs the |
| 123 | // error code argument |
| 124 | |
| 125 | private final Runnable mCallOnOpened = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 126 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 127 | public void run() { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 128 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 129 | synchronized(mInterfaceLock) { |
| 130 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 131 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 132 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 133 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 134 | if (sessionCallback != null) { |
| 135 | sessionCallback.onOpened(CameraDeviceImpl.this); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 136 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 137 | mDeviceCallback.onOpened(CameraDeviceImpl.this); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 138 | } |
| 139 | }; |
| 140 | |
| 141 | private final Runnable mCallOnUnconfigured = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 142 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 143 | public void run() { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 144 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 145 | synchronized(mInterfaceLock) { |
| 146 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 147 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 148 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 149 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 150 | if (sessionCallback != null) { |
| 151 | sessionCallback.onUnconfigured(CameraDeviceImpl.this); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 152 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 153 | } |
| 154 | }; |
| 155 | |
| 156 | private final Runnable mCallOnActive = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 157 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 158 | public void run() { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 159 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 160 | synchronized(mInterfaceLock) { |
| 161 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 162 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 163 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 164 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 165 | if (sessionCallback != null) { |
| 166 | sessionCallback.onActive(CameraDeviceImpl.this); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 167 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 168 | } |
| 169 | }; |
| 170 | |
| 171 | private final Runnable mCallOnBusy = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 172 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 173 | public void run() { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 174 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 175 | synchronized(mInterfaceLock) { |
| 176 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 177 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 178 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 179 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 180 | if (sessionCallback != null) { |
| 181 | sessionCallback.onBusy(CameraDeviceImpl.this); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 182 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 183 | } |
| 184 | }; |
| 185 | |
| 186 | private final Runnable mCallOnClosed = new Runnable() { |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 187 | private boolean mClosedOnce = false; |
| 188 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 189 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 190 | public void run() { |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 191 | if (mClosedOnce) { |
| 192 | throw new AssertionError("Don't post #onClosed more than once"); |
| 193 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 194 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 195 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 196 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 197 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 198 | if (sessionCallback != null) { |
| 199 | sessionCallback.onClosed(CameraDeviceImpl.this); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 200 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 201 | mDeviceCallback.onClosed(CameraDeviceImpl.this); |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 202 | mClosedOnce = true; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 203 | } |
| 204 | }; |
| 205 | |
| 206 | private final Runnable mCallOnIdle = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 207 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 208 | public void run() { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 209 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 210 | synchronized(mInterfaceLock) { |
| 211 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 212 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 213 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 214 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 215 | if (sessionCallback != null) { |
| 216 | sessionCallback.onIdle(CameraDeviceImpl.this); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 217 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 218 | } |
| 219 | }; |
| 220 | |
| 221 | private final Runnable mCallOnDisconnected = new Runnable() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 222 | @Override |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 223 | public void run() { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 224 | StateCallbackKK sessionCallback = null; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 225 | synchronized(mInterfaceLock) { |
| 226 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 227 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 228 | sessionCallback = mSessionStateCallback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 229 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 230 | if (sessionCallback != null) { |
| 231 | sessionCallback.onDisconnected(CameraDeviceImpl.this); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 232 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 233 | mDeviceCallback.onDisconnected(CameraDeviceImpl.this); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 234 | } |
| 235 | }; |
| 236 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 237 | public CameraDeviceImpl(String cameraId, StateCallback callback, Handler handler, |
Chien-Yu Chen | 3d2b5fe | 2017-04-05 16:42:31 -0700 | [diff] [blame] | 238 | CameraCharacteristics characteristics, int appTargetSdkVersion) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 239 | if (cameraId == null || callback == null || handler == null || characteristics == null) { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 240 | throw new IllegalArgumentException("Null argument given"); |
| 241 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 242 | mCameraId = cameraId; |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 243 | mDeviceCallback = callback; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 244 | mDeviceHandler = handler; |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 245 | mCharacteristics = characteristics; |
Chien-Yu Chen | 3d2b5fe | 2017-04-05 16:42:31 -0700 | [diff] [blame] | 246 | mAppTargetSdkVersion = appTargetSdkVersion; |
Eino-Ville Talvala | b72d1ef | 2014-04-28 13:21:18 -0700 | [diff] [blame] | 247 | |
| 248 | final int MAX_TAG_LEN = 23; |
| 249 | String tag = String.format("CameraDevice-JV-%s", mCameraId); |
| 250 | if (tag.length() > MAX_TAG_LEN) { |
| 251 | tag = tag.substring(0, MAX_TAG_LEN); |
| 252 | } |
| 253 | TAG = tag; |
Zhijun He | 8315915 | 2014-07-16 11:32:59 -0700 | [diff] [blame] | 254 | |
| 255 | Integer partialCount = |
| 256 | mCharacteristics.get(CameraCharacteristics.REQUEST_PARTIAL_RESULT_COUNT); |
| 257 | if (partialCount == null) { |
| 258 | // 1 means partial result is not supported. |
| 259 | mTotalPartialCount = 1; |
| 260 | } else { |
| 261 | mTotalPartialCount = partialCount; |
| 262 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | public CameraDeviceCallbacks getCallbacks() { |
| 266 | return mCallbacks; |
| 267 | } |
| 268 | |
Shuzhen Wang | 7839293 | 2016-03-04 15:26:01 -0800 | [diff] [blame] | 269 | /** |
| 270 | * Set remote device, which triggers initial onOpened/onUnconfigured callbacks |
| 271 | * |
| 272 | * <p>This function may post onDisconnected and throw CAMERA_DISCONNECTED if remoteDevice dies |
| 273 | * during setup.</p> |
| 274 | * |
| 275 | */ |
| 276 | public void setRemoteDevice(ICameraDeviceUser remoteDevice) throws CameraAccessException { |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 277 | synchronized(mInterfaceLock) { |
| 278 | // TODO: Move from decorator to direct binder-mediated exceptions |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 279 | // If setRemoteFailure already called, do nothing |
| 280 | if (mInError) return; |
| 281 | |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 282 | mRemoteDevice = new ICameraDeviceUserWrapper(remoteDevice); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 283 | |
Shuzhen Wang | 7839293 | 2016-03-04 15:26:01 -0800 | [diff] [blame] | 284 | IBinder remoteDeviceBinder = remoteDevice.asBinder(); |
| 285 | // For legacy camera device, remoteDevice is in the same process, and |
| 286 | // asBinder returns NULL. |
| 287 | if (remoteDeviceBinder != null) { |
| 288 | try { |
| 289 | remoteDeviceBinder.linkToDeath(this, /*flag*/ 0); |
| 290 | } catch (RemoteException e) { |
| 291 | CameraDeviceImpl.this.mDeviceHandler.post(mCallOnDisconnected); |
| 292 | |
| 293 | throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, |
| 294 | "The camera device has encountered a serious error"); |
| 295 | } |
| 296 | } |
| 297 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 298 | mDeviceHandler.post(mCallOnOpened); |
| 299 | mDeviceHandler.post(mCallOnUnconfigured); |
| 300 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 303 | /** |
| 304 | * Call to indicate failed connection to a remote camera device. |
| 305 | * |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 306 | * <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] | 307 | * Use in place of setRemoteDevice() when startup fails.</p> |
| 308 | */ |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 309 | public void setRemoteFailure(final ServiceSpecificException failure) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 310 | int failureCode = StateCallback.ERROR_CAMERA_DEVICE; |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 311 | boolean failureIsError = true; |
| 312 | |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 313 | switch (failure.errorCode) { |
| 314 | case ICameraService.ERROR_CAMERA_IN_USE: |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 315 | failureCode = StateCallback.ERROR_CAMERA_IN_USE; |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 316 | break; |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 317 | case ICameraService.ERROR_MAX_CAMERAS_IN_USE: |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 318 | failureCode = StateCallback.ERROR_MAX_CAMERAS_IN_USE; |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 319 | break; |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 320 | case ICameraService.ERROR_DISABLED: |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 321 | failureCode = StateCallback.ERROR_CAMERA_DISABLED; |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 322 | break; |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 323 | case ICameraService.ERROR_DISCONNECTED: |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 324 | failureIsError = false; |
| 325 | break; |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 326 | case ICameraService.ERROR_INVALID_OPERATION: |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 327 | failureCode = StateCallback.ERROR_CAMERA_DEVICE; |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 328 | break; |
| 329 | default: |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 330 | Log.e(TAG, "Unexpected failure in opening camera device: " + failure.errorCode + |
| 331 | failure.getMessage()); |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 332 | break; |
| 333 | } |
| 334 | final int code = failureCode; |
| 335 | final boolean isError = failureIsError; |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 336 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 337 | mInError = true; |
| 338 | mDeviceHandler.post(new Runnable() { |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 339 | @Override |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 340 | public void run() { |
| 341 | if (isError) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 342 | mDeviceCallback.onError(CameraDeviceImpl.this, code); |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 343 | } else { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 344 | mDeviceCallback.onDisconnected(CameraDeviceImpl.this); |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | }); |
| 348 | } |
| 349 | } |
| 350 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 351 | @Override |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 352 | public String getId() { |
| 353 | return mCameraId; |
| 354 | } |
| 355 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 356 | public void configureOutputs(List<Surface> outputs) throws CameraAccessException { |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 357 | // Leave this here for backwards compatibility with older code using this directly |
Yin-Chia Yeh | bfbbee7 | 2015-03-12 13:39:26 -0700 | [diff] [blame] | 358 | ArrayList<OutputConfiguration> outputConfigs = new ArrayList<>(outputs.size()); |
| 359 | for (Surface s : outputs) { |
| 360 | outputConfigs.add(new OutputConfiguration(s)); |
| 361 | } |
Zhijun He | a767772 | 2015-06-01 16:36:06 -0700 | [diff] [blame] | 362 | configureStreamsChecked(/*inputConfig*/null, outputConfigs, |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 363 | /*operatingMode*/ICameraDeviceUser.NORMAL_MODE, /*sessionParams*/ null); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 364 | |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 368 | * Attempt to configure the input and outputs; the device goes to idle and then configures the |
| 369 | * new input and outputs if possible. |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 370 | * |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 371 | * <p>The configuration may gracefully fail, if input configuration is not supported, |
| 372 | * if there are too many outputs, if the formats are not supported, or if the sizes for that |
| 373 | * format is not supported. In this case this function will return {@code false} and the |
| 374 | * unconfigured callback will be fired.</p> |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 375 | * |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 376 | * <p>If the configuration succeeds (with 1 or more outputs with or without an input), |
| 377 | * then the idle callback is fired. Unconfiguring the device always fires the idle callback.</p> |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 378 | * |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 379 | * @param inputConfig input configuration or {@code null} for no input |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 380 | * @param outputs a list of one or more surfaces, or {@code null} to unconfigure |
Eino-Ville Talvala | 0e04e91 | 2017-02-28 17:49:41 -0800 | [diff] [blame] | 381 | * @param operatingMode If the stream configuration is for a normal session, |
| 382 | * a constrained high speed session, or something else. |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 383 | * @param sessionParams Session parameters. |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 384 | * @return whether or not the configuration was successful |
| 385 | * |
| 386 | * @throws CameraAccessException if there were any unexpected problems during configuration |
| 387 | */ |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 388 | public boolean configureStreamsChecked(InputConfiguration inputConfig, |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 389 | List<OutputConfiguration> outputs, int operatingMode, CaptureRequest sessionParams) |
Zhijun He | a767772 | 2015-06-01 16:36:06 -0700 | [diff] [blame] | 390 | throws CameraAccessException { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 391 | // Treat a null input the same an empty list |
| 392 | if (outputs == null) { |
Yin-Chia Yeh | bfbbee7 | 2015-03-12 13:39:26 -0700 | [diff] [blame] | 393 | outputs = new ArrayList<OutputConfiguration>(); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 394 | } |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 395 | if (outputs.size() == 0 && inputConfig != null) { |
| 396 | throw new IllegalArgumentException("cannot configure an input stream without " + |
| 397 | "any output streams"); |
| 398 | } |
| 399 | |
| 400 | checkInputConfiguration(inputConfig); |
| 401 | |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 402 | boolean success = false; |
| 403 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 404 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 405 | checkIfCameraClosedOrInError(); |
Yin-Chia Yeh | bfbbee7 | 2015-03-12 13:39:26 -0700 | [diff] [blame] | 406 | // Streams to create |
| 407 | HashSet<OutputConfiguration> addSet = new HashSet<OutputConfiguration>(outputs); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 408 | // Streams to delete |
Yin-Chia Yeh | bfbbee7 | 2015-03-12 13:39:26 -0700 | [diff] [blame] | 409 | List<Integer> deleteList = new ArrayList<Integer>(); |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 410 | |
| 411 | // Determine which streams need to be created, which to be deleted |
| 412 | for (int i = 0; i < mConfiguredOutputs.size(); ++i) { |
| 413 | int streamId = mConfiguredOutputs.keyAt(i); |
Yin-Chia Yeh | bfbbee7 | 2015-03-12 13:39:26 -0700 | [diff] [blame] | 414 | OutputConfiguration outConfig = mConfiguredOutputs.valueAt(i); |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 415 | |
Zhijun He | c8b181e | 2016-05-30 14:54:39 -0700 | [diff] [blame] | 416 | if (!outputs.contains(outConfig) || outConfig.isDeferredConfiguration()) { |
| 417 | // Always delete the deferred output configuration when the session |
| 418 | // is created, as the deferred output configuration doesn't have unique surface |
| 419 | // related identifies. |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 420 | deleteList.add(streamId); |
| 421 | } else { |
Yin-Chia Yeh | bfbbee7 | 2015-03-12 13:39:26 -0700 | [diff] [blame] | 422 | addSet.remove(outConfig); // Don't create a stream previously created |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 426 | mDeviceHandler.post(mCallOnBusy); |
| 427 | stopRepeating(); |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 428 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 429 | try { |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 430 | waitUntilIdle(); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 431 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 432 | mRemoteDevice.beginConfigure(); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 433 | |
| 434 | // reconfigure the input stream if the input configuration is different. |
| 435 | InputConfiguration currentInputConfig = mConfiguredInput.getValue(); |
| 436 | if (inputConfig != currentInputConfig && |
| 437 | (inputConfig == null || !inputConfig.equals(currentInputConfig))) { |
| 438 | if (currentInputConfig != null) { |
| 439 | mRemoteDevice.deleteStream(mConfiguredInput.getKey()); |
| 440 | mConfiguredInput = new SimpleEntry<Integer, InputConfiguration>( |
| 441 | REQUEST_ID_NONE, null); |
| 442 | } |
| 443 | if (inputConfig != null) { |
| 444 | int streamId = mRemoteDevice.createInputStream(inputConfig.getWidth(), |
| 445 | inputConfig.getHeight(), inputConfig.getFormat()); |
| 446 | mConfiguredInput = new SimpleEntry<Integer, InputConfiguration>( |
| 447 | streamId, inputConfig); |
| 448 | } |
| 449 | } |
| 450 | |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 451 | // Delete all streams first (to free up HW resources) |
| 452 | for (Integer streamId : deleteList) { |
| 453 | mRemoteDevice.deleteStream(streamId); |
| 454 | mConfiguredOutputs.delete(streamId); |
| 455 | } |
| 456 | |
| 457 | // Add all new streams |
Yin-Chia Yeh | bfbbee7 | 2015-03-12 13:39:26 -0700 | [diff] [blame] | 458 | for (OutputConfiguration outConfig : outputs) { |
| 459 | if (addSet.contains(outConfig)) { |
| 460 | int streamId = mRemoteDevice.createStream(outConfig); |
| 461 | mConfiguredOutputs.put(streamId, outConfig); |
| 462 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 463 | } |
Igor Murashkin | 57ea59b | 2013-08-23 16:55:57 -0700 | [diff] [blame] | 464 | |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 465 | if (sessionParams != null) { |
| 466 | mRemoteDevice.endConfigure(operatingMode, sessionParams.getNativeCopy()); |
| 467 | } else { |
| 468 | mRemoteDevice.endConfigure(operatingMode, null); |
| 469 | } |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 470 | |
| 471 | success = true; |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 472 | } catch (IllegalArgumentException e) { |
| 473 | // OK. camera service can reject stream config if it's not supported by HAL |
| 474 | // This is only the result of a programmer misusing the camera2 api. |
| 475 | Log.w(TAG, "Stream configuration failed due to: " + e.getMessage()); |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 476 | return false; |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 477 | } catch (CameraAccessException e) { |
| 478 | if (e.getReason() == CameraAccessException.CAMERA_IN_USE) { |
| 479 | throw new IllegalStateException("The camera is currently busy." + |
| 480 | " You must wait until the previous operation completes.", e); |
| 481 | } |
| 482 | throw e; |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 483 | } finally { |
| 484 | if (success && outputs.size() > 0) { |
| 485 | mDeviceHandler.post(mCallOnIdle); |
| 486 | } else { |
| 487 | // Always return to the 'unconfigured' state if we didn't hit a fatal error |
| 488 | mDeviceHandler.post(mCallOnUnconfigured); |
| 489 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 490 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 491 | } |
Igor Murashkin | 10fbbbb | 2014-08-19 16:19:30 -0700 | [diff] [blame] | 492 | |
| 493 | return success; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | @Override |
Eino-Ville Talvala | cca00c6 | 2014-05-14 10:53:20 -0700 | [diff] [blame] | 497 | public void createCaptureSession(List<Surface> outputs, |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 498 | CameraCaptureSession.StateCallback callback, Handler handler) |
Eino-Ville Talvala | cca00c6 | 2014-05-14 10:53:20 -0700 | [diff] [blame] | 499 | throws CameraAccessException { |
Yin-Chia Yeh | 49ea6ae | 2015-03-09 16:53:14 -0700 | [diff] [blame] | 500 | List<OutputConfiguration> outConfigurations = new ArrayList<>(outputs.size()); |
| 501 | for (Surface surface : outputs) { |
| 502 | outConfigurations.add(new OutputConfiguration(surface)); |
| 503 | } |
Zhijun He | a767772 | 2015-06-01 16:36:06 -0700 | [diff] [blame] | 504 | createCaptureSessionInternal(null, outConfigurations, callback, handler, |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 505 | /*operatingMode*/ICameraDeviceUser.NORMAL_MODE, /*sessionParams*/ null); |
Yin-Chia Yeh | 49ea6ae | 2015-03-09 16:53:14 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | @Override |
Zhijun He | 0034743 | 2016-04-07 17:34:10 -0700 | [diff] [blame] | 509 | public void createCaptureSessionByOutputConfigurations( |
Yin-Chia Yeh | 49ea6ae | 2015-03-09 16:53:14 -0700 | [diff] [blame] | 510 | List<OutputConfiguration> outputConfigurations, |
| 511 | CameraCaptureSession.StateCallback callback, Handler handler) |
| 512 | throws CameraAccessException { |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 513 | if (DEBUG) { |
Shuzhen Wang | 9c663d4 | 2016-10-28 23:25:02 -0700 | [diff] [blame] | 514 | Log.d(TAG, "createCaptureSessionByOutputConfigurations"); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Eino-Ville Talvala | fa5e2a1 | 2016-04-11 12:49:17 -0700 | [diff] [blame] | 517 | // OutputConfiguration objects are immutable, but need to have our own array |
| 518 | List<OutputConfiguration> currentOutputs = new ArrayList<>(outputConfigurations); |
Zhijun He | 445b316 | 2016-01-18 15:34:28 -0800 | [diff] [blame] | 519 | |
| 520 | createCaptureSessionInternal(null, currentOutputs, callback, handler, |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 521 | /*operatingMode*/ICameraDeviceUser.NORMAL_MODE, /*sessionParams*/null); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | @Override |
Chien-Yu Chen | 8062d31 | 2015-05-12 14:24:10 -0700 | [diff] [blame] | 525 | public void createReprocessableCaptureSession(InputConfiguration inputConfig, |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 526 | List<Surface> outputs, CameraCaptureSession.StateCallback callback, Handler handler) |
| 527 | throws CameraAccessException { |
| 528 | if (DEBUG) { |
Chien-Yu Chen | 8062d31 | 2015-05-12 14:24:10 -0700 | [diff] [blame] | 529 | Log.d(TAG, "createReprocessableCaptureSession"); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | if (inputConfig == null) { |
| 533 | throw new IllegalArgumentException("inputConfig cannot be null when creating a " + |
Chien-Yu Chen | 8062d31 | 2015-05-12 14:24:10 -0700 | [diff] [blame] | 534 | "reprocessable capture session"); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 535 | } |
| 536 | List<OutputConfiguration> outConfigurations = new ArrayList<>(outputs.size()); |
| 537 | for (Surface surface : outputs) { |
| 538 | outConfigurations.add(new OutputConfiguration(surface)); |
| 539 | } |
Zhijun He | a767772 | 2015-06-01 16:36:06 -0700 | [diff] [blame] | 540 | createCaptureSessionInternal(inputConfig, outConfigurations, callback, handler, |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 541 | /*operatingMode*/ICameraDeviceUser.NORMAL_MODE, /*sessionParams*/ null); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Eino-Ville Talvala | 639fffe | 2015-06-30 10:34:48 -0700 | [diff] [blame] | 544 | @Override |
Zhijun He | 0034743 | 2016-04-07 17:34:10 -0700 | [diff] [blame] | 545 | public void createReprocessableCaptureSessionByConfigurations(InputConfiguration inputConfig, |
Zhijun He | 445b316 | 2016-01-18 15:34:28 -0800 | [diff] [blame] | 546 | List<OutputConfiguration> outputs, |
| 547 | android.hardware.camera2.CameraCaptureSession.StateCallback callback, Handler handler) |
| 548 | throws CameraAccessException { |
| 549 | if (DEBUG) { |
| 550 | Log.d(TAG, "createReprocessableCaptureSessionWithConfigurations"); |
| 551 | } |
| 552 | |
| 553 | if (inputConfig == null) { |
| 554 | throw new IllegalArgumentException("inputConfig cannot be null when creating a " + |
| 555 | "reprocessable capture session"); |
| 556 | } |
| 557 | |
| 558 | if (outputs == null) { |
| 559 | throw new IllegalArgumentException("Output configurations cannot be null when " + |
| 560 | "creating a reprocessable capture session"); |
| 561 | } |
| 562 | |
| 563 | // OutputConfiguration objects aren't immutable, make a copy before using. |
| 564 | List<OutputConfiguration> currentOutputs = new ArrayList<OutputConfiguration>(); |
| 565 | for (OutputConfiguration output : outputs) { |
| 566 | currentOutputs.add(new OutputConfiguration(output)); |
| 567 | } |
| 568 | createCaptureSessionInternal(inputConfig, currentOutputs, |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 569 | callback, handler, /*operatingMode*/ICameraDeviceUser.NORMAL_MODE, |
| 570 | /*sessionParams*/ null); |
Zhijun He | 445b316 | 2016-01-18 15:34:28 -0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | @Override |
Eino-Ville Talvala | 639fffe | 2015-06-30 10:34:48 -0700 | [diff] [blame] | 574 | public void createConstrainedHighSpeedCaptureSession(List<Surface> outputs, |
| 575 | android.hardware.camera2.CameraCaptureSession.StateCallback callback, Handler handler) |
| 576 | throws CameraAccessException { |
| 577 | if (outputs == null || outputs.size() == 0 || outputs.size() > 2) { |
| 578 | throw new IllegalArgumentException( |
| 579 | "Output surface list must not be null and the size must be no more than 2"); |
| 580 | } |
Eino-Ville Talvala | 639fffe | 2015-06-30 10:34:48 -0700 | [diff] [blame] | 581 | List<OutputConfiguration> outConfigurations = new ArrayList<>(outputs.size()); |
| 582 | for (Surface surface : outputs) { |
| 583 | outConfigurations.add(new OutputConfiguration(surface)); |
| 584 | } |
| 585 | createCaptureSessionInternal(null, outConfigurations, callback, handler, |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 586 | /*operatingMode*/ICameraDeviceUser.CONSTRAINED_HIGH_SPEED_MODE, |
| 587 | /*sessionParams*/ null); |
Eino-Ville Talvala | 0e04e91 | 2017-02-28 17:49:41 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | @Override |
| 591 | public void createCustomCaptureSession(InputConfiguration inputConfig, |
| 592 | List<OutputConfiguration> outputs, |
| 593 | int operatingMode, |
| 594 | android.hardware.camera2.CameraCaptureSession.StateCallback callback, |
| 595 | Handler handler) throws CameraAccessException { |
| 596 | List<OutputConfiguration> currentOutputs = new ArrayList<OutputConfiguration>(); |
| 597 | for (OutputConfiguration output : outputs) { |
| 598 | currentOutputs.add(new OutputConfiguration(output)); |
| 599 | } |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 600 | createCaptureSessionInternal(inputConfig, currentOutputs, callback, handler, operatingMode, |
| 601 | /*sessionParams*/ null); |
| 602 | } |
| 603 | |
| 604 | @Override |
| 605 | public void createCaptureSession(SessionConfiguration config) |
| 606 | throws CameraAccessException { |
| 607 | if (config == null) { |
| 608 | throw new IllegalArgumentException("Invalid session configuration"); |
| 609 | } |
| 610 | |
| 611 | List<OutputConfiguration> outputConfigs = config.getOutputConfigurations(); |
| 612 | if (outputConfigs == null) { |
| 613 | throw new IllegalArgumentException("Invalid output configurations"); |
| 614 | } |
| 615 | createCaptureSessionInternal(config.getInputConfiguration(), outputConfigs, |
| 616 | config.getStateCallback(), config.getHandler(), config.getSessionType(), |
| 617 | config.getSessionParameters()); |
Eino-Ville Talvala | 639fffe | 2015-06-30 10:34:48 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 620 | private void createCaptureSessionInternal(InputConfiguration inputConfig, |
| 621 | List<OutputConfiguration> outputConfigurations, |
Zhijun He | a767772 | 2015-06-01 16:36:06 -0700 | [diff] [blame] | 622 | CameraCaptureSession.StateCallback callback, Handler handler, |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 623 | int operatingMode, CaptureRequest sessionParams) throws CameraAccessException { |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 624 | synchronized(mInterfaceLock) { |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 625 | if (DEBUG) { |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 626 | Log.d(TAG, "createCaptureSessionInternal"); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 629 | checkIfCameraClosedOrInError(); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 630 | |
Eino-Ville Talvala | 0e04e91 | 2017-02-28 17:49:41 -0800 | [diff] [blame] | 631 | boolean isConstrainedHighSpeed = |
| 632 | (operatingMode == ICameraDeviceUser.CONSTRAINED_HIGH_SPEED_MODE); |
Zhijun He | a767772 | 2015-06-01 16:36:06 -0700 | [diff] [blame] | 633 | if (isConstrainedHighSpeed && inputConfig != null) { |
| 634 | throw new IllegalArgumentException("Constrained high speed session doesn't support" |
| 635 | + " input configuration yet."); |
| 636 | } |
| 637 | |
Eino-Ville Talvala | d3b85f6 | 2014-08-05 15:02:31 -0700 | [diff] [blame] | 638 | // Notify current session that it's going away, before starting camera operations |
| 639 | // After this call completes, the session is not allowed to call into CameraDeviceImpl |
| 640 | if (mCurrentSession != null) { |
| 641 | mCurrentSession.replaceSessionClose(); |
| 642 | } |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 643 | |
| 644 | // TODO: dont block for this |
| 645 | boolean configureSuccess = true; |
| 646 | CameraAccessException pendingException = null; |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 647 | Surface input = null; |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 648 | try { |
Chien-Yu Chen | c58a602 | 2015-06-16 11:13:48 -0700 | [diff] [blame] | 649 | // configure streams and then block until IDLE |
Zhijun He | a767772 | 2015-06-01 16:36:06 -0700 | [diff] [blame] | 650 | configureSuccess = configureStreamsChecked(inputConfig, outputConfigurations, |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 651 | operatingMode, sessionParams); |
Chien-Yu Chen | c58a602 | 2015-06-16 11:13:48 -0700 | [diff] [blame] | 652 | if (configureSuccess == true && inputConfig != null) { |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 653 | input = mRemoteDevice.getInputSurface(); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 654 | } |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 655 | } catch (CameraAccessException e) { |
| 656 | configureSuccess = false; |
| 657 | pendingException = e; |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 658 | input = null; |
Igor Murashkin | e144220 | 2014-06-09 17:51:24 -0700 | [diff] [blame] | 659 | if (DEBUG) { |
| 660 | Log.v(TAG, "createCaptureSession - failed with exception ", e); |
| 661 | } |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | // Fire onConfigured if configureOutputs succeeded, fire onConfigureFailed otherwise. |
Eino-Ville Talvala | 639fffe | 2015-06-30 10:34:48 -0700 | [diff] [blame] | 665 | CameraCaptureSessionCore newSession = null; |
| 666 | if (isConstrainedHighSpeed) { |
Emilian Peev | 75a5570 | 2017-11-07 16:09:59 +0000 | [diff] [blame] | 667 | ArrayList<Surface> surfaces = new ArrayList<>(outputConfigurations.size()); |
| 668 | for (OutputConfiguration outConfig : outputConfigurations) { |
| 669 | surfaces.add(outConfig.getSurface()); |
| 670 | } |
| 671 | StreamConfigurationMap config = |
| 672 | getCharacteristics().get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); |
| 673 | SurfaceUtils.checkConstrainedHighSpeedSurfaces(surfaces, /*fpsRange*/null, config); |
| 674 | |
Eino-Ville Talvala | 639fffe | 2015-06-30 10:34:48 -0700 | [diff] [blame] | 675 | newSession = new CameraConstrainedHighSpeedCaptureSessionImpl(mNextSessionId++, |
Shuzhen Wang | 9c663d4 | 2016-10-28 23:25:02 -0700 | [diff] [blame] | 676 | callback, handler, this, mDeviceHandler, configureSuccess, |
Eino-Ville Talvala | 639fffe | 2015-06-30 10:34:48 -0700 | [diff] [blame] | 677 | mCharacteristics); |
| 678 | } else { |
| 679 | newSession = new CameraCaptureSessionImpl(mNextSessionId++, input, |
Shuzhen Wang | 9c663d4 | 2016-10-28 23:25:02 -0700 | [diff] [blame] | 680 | callback, handler, this, mDeviceHandler, |
Eino-Ville Talvala | 639fffe | 2015-06-30 10:34:48 -0700 | [diff] [blame] | 681 | configureSuccess); |
| 682 | } |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 683 | |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 684 | // TODO: wait until current session closes, then create the new session |
| 685 | mCurrentSession = newSession; |
| 686 | |
| 687 | if (pendingException != null) { |
| 688 | throw pendingException; |
| 689 | } |
| 690 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 691 | mSessionStateCallback = mCurrentSession.getDeviceStateCallback(); |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 692 | } |
Eino-Ville Talvala | cca00c6 | 2014-05-14 10:53:20 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 695 | /** |
| 696 | * For use by backwards-compatibility code only. |
| 697 | */ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 698 | public void setSessionListener(StateCallbackKK sessionCallback) { |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 699 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 700 | mSessionStateCallback = sessionCallback; |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Chien-Yu Chen | 3d2b5fe | 2017-04-05 16:42:31 -0700 | [diff] [blame] | 704 | private void overrideEnableZsl(CameraMetadataNative request, boolean newValue) { |
| 705 | Boolean enableZsl = request.get(CaptureRequest.CONTROL_ENABLE_ZSL); |
| 706 | if (enableZsl == null) { |
| 707 | // If enableZsl is not available, don't override. |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | request.set(CaptureRequest.CONTROL_ENABLE_ZSL, newValue); |
| 712 | } |
| 713 | |
Eino-Ville Talvala | cca00c6 | 2014-05-14 10:53:20 -0700 | [diff] [blame] | 714 | @Override |
Emilian Peev | 2100ae7 | 2018-01-12 16:56:25 +0000 | [diff] [blame] | 715 | public CaptureRequest.Builder createCaptureRequest(int templateType, |
| 716 | Set<String> physicalCameraIdSet) |
| 717 | throws CameraAccessException { |
| 718 | synchronized(mInterfaceLock) { |
| 719 | checkIfCameraClosedOrInError(); |
| 720 | |
| 721 | for (String physicalId : physicalCameraIdSet) { |
| 722 | if (physicalId == getId()) { |
| 723 | throw new IllegalStateException("Physical id matches the logical id!"); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | CameraMetadataNative templatedRequest = null; |
| 728 | |
| 729 | templatedRequest = mRemoteDevice.createDefaultRequest(templateType); |
| 730 | |
| 731 | // If app target SDK is older than O, or it's not a still capture template, enableZsl |
| 732 | // must be false in the default request. |
| 733 | if (mAppTargetSdkVersion < Build.VERSION_CODES.O || |
| 734 | templateType != TEMPLATE_STILL_CAPTURE) { |
| 735 | overrideEnableZsl(templatedRequest, false); |
| 736 | } |
| 737 | |
| 738 | CaptureRequest.Builder builder = new CaptureRequest.Builder( |
| 739 | templatedRequest, /*reprocess*/false, CameraCaptureSession.SESSION_ID_NONE, |
| 740 | getId(), physicalCameraIdSet); |
| 741 | |
| 742 | return builder; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | @Override |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 747 | public CaptureRequest.Builder createCaptureRequest(int templateType) |
| 748 | throws CameraAccessException { |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 749 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 750 | checkIfCameraClosedOrInError(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 751 | |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 752 | CameraMetadataNative templatedRequest = null; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 753 | |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 754 | templatedRequest = mRemoteDevice.createDefaultRequest(templateType); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 755 | |
Chien-Yu Chen | 3d2b5fe | 2017-04-05 16:42:31 -0700 | [diff] [blame] | 756 | // If app target SDK is older than O, or it's not a still capture template, enableZsl |
| 757 | // must be false in the default request. |
| 758 | if (mAppTargetSdkVersion < Build.VERSION_CODES.O || |
| 759 | templateType != TEMPLATE_STILL_CAPTURE) { |
| 760 | overrideEnableZsl(templatedRequest, false); |
| 761 | } |
| 762 | |
Chien-Yu Chen | 7a316f6b | 2015-04-13 14:31:45 -0700 | [diff] [blame] | 763 | CaptureRequest.Builder builder = new CaptureRequest.Builder( |
Emilian Peev | 2100ae7 | 2018-01-12 16:56:25 +0000 | [diff] [blame] | 764 | templatedRequest, /*reprocess*/false, CameraCaptureSession.SESSION_ID_NONE, |
| 765 | getId(), /*physicalCameraIdSet*/ null); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 766 | |
Eino-Ville Talvala | 70c2207 | 2013-08-27 12:09:04 -0700 | [diff] [blame] | 767 | return builder; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 771 | @Override |
| 772 | public CaptureRequest.Builder createReprocessCaptureRequest(TotalCaptureResult inputResult) |
| 773 | throws CameraAccessException { |
| 774 | synchronized(mInterfaceLock) { |
| 775 | checkIfCameraClosedOrInError(); |
| 776 | |
| 777 | CameraMetadataNative resultMetadata = new |
| 778 | CameraMetadataNative(inputResult.getNativeCopy()); |
| 779 | |
Chien-Yu Chen | 7a316f6b | 2015-04-13 14:31:45 -0700 | [diff] [blame] | 780 | return new CaptureRequest.Builder(resultMetadata, /*reprocess*/true, |
Emilian Peev | 2100ae7 | 2018-01-12 16:56:25 +0000 | [diff] [blame] | 781 | inputResult.getSessionId(), getId(), /*physicalCameraIdSet*/ null); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Eino-Ville Talvala | ad916f7 | 2015-04-11 12:09:11 -0700 | [diff] [blame] | 785 | public void prepare(Surface surface) throws CameraAccessException { |
Eino-Ville Talvala | 8b90557 | 2015-05-14 15:43:01 -0700 | [diff] [blame] | 786 | if (surface == null) throw new IllegalArgumentException("Surface is null"); |
| 787 | |
Eino-Ville Talvala | ad916f7 | 2015-04-11 12:09:11 -0700 | [diff] [blame] | 788 | synchronized(mInterfaceLock) { |
| 789 | int streamId = -1; |
| 790 | for (int i = 0; i < mConfiguredOutputs.size(); i++) { |
Shuzhen Wang | e0a6667 | 2017-03-21 12:05:14 -0700 | [diff] [blame] | 791 | final List<Surface> surfaces = mConfiguredOutputs.valueAt(i).getSurfaces(); |
| 792 | if (surfaces.contains(surface)) { |
Eino-Ville Talvala | ad916f7 | 2015-04-11 12:09:11 -0700 | [diff] [blame] | 793 | streamId = mConfiguredOutputs.keyAt(i); |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | if (streamId == -1) { |
| 798 | throw new IllegalArgumentException("Surface is not part of this session"); |
| 799 | } |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 800 | |
| 801 | mRemoteDevice.prepare(streamId); |
Eino-Ville Talvala | ad916f7 | 2015-04-11 12:09:11 -0700 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | |
Ruben Brunk | 7ed1aaa | 2015-08-13 17:57:14 -0700 | [diff] [blame] | 805 | public void prepare(int maxCount, Surface surface) throws CameraAccessException { |
| 806 | if (surface == null) throw new IllegalArgumentException("Surface is null"); |
| 807 | if (maxCount <= 0) throw new IllegalArgumentException("Invalid maxCount given: " + |
| 808 | maxCount); |
| 809 | |
| 810 | synchronized(mInterfaceLock) { |
| 811 | int streamId = -1; |
| 812 | for (int i = 0; i < mConfiguredOutputs.size(); i++) { |
| 813 | if (surface == mConfiguredOutputs.valueAt(i).getSurface()) { |
| 814 | streamId = mConfiguredOutputs.keyAt(i); |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | if (streamId == -1) { |
| 819 | throw new IllegalArgumentException("Surface is not part of this session"); |
| 820 | } |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 821 | |
| 822 | mRemoteDevice.prepare2(maxCount, streamId); |
Ruben Brunk | 7ed1aaa | 2015-08-13 17:57:14 -0700 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
Emilian Peev | 0323315 | 2017-10-27 16:01:20 +0100 | [diff] [blame] | 826 | public void updateOutputConfiguration(OutputConfiguration config) |
| 827 | throws CameraAccessException { |
| 828 | synchronized(mInterfaceLock) { |
| 829 | int streamId = -1; |
| 830 | for (int i = 0; i < mConfiguredOutputs.size(); i++) { |
| 831 | if (config.getSurface() == mConfiguredOutputs.valueAt(i).getSurface()) { |
| 832 | streamId = mConfiguredOutputs.keyAt(i); |
| 833 | break; |
| 834 | } |
| 835 | } |
| 836 | if (streamId == -1) { |
| 837 | throw new IllegalArgumentException("Invalid output configuration"); |
| 838 | } |
| 839 | |
| 840 | mRemoteDevice.updateOutputConfiguration(streamId, config); |
Yin-Chia Yeh | c60af90 | 2017-11-10 20:02:29 -0800 | [diff] [blame] | 841 | mConfiguredOutputs.put(streamId, config); |
Emilian Peev | 0323315 | 2017-10-27 16:01:20 +0100 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
Eino-Ville Talvala | 14c09fa | 2015-07-15 16:04:04 -0700 | [diff] [blame] | 845 | public void tearDown(Surface surface) throws CameraAccessException { |
| 846 | if (surface == null) throw new IllegalArgumentException("Surface is null"); |
| 847 | |
| 848 | synchronized(mInterfaceLock) { |
| 849 | int streamId = -1; |
| 850 | for (int i = 0; i < mConfiguredOutputs.size(); i++) { |
| 851 | if (surface == mConfiguredOutputs.valueAt(i).getSurface()) { |
| 852 | streamId = mConfiguredOutputs.keyAt(i); |
| 853 | break; |
| 854 | } |
| 855 | } |
| 856 | if (streamId == -1) { |
| 857 | throw new IllegalArgumentException("Surface is not part of this session"); |
| 858 | } |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 859 | |
| 860 | mRemoteDevice.tearDown(streamId); |
Eino-Ville Talvala | 14c09fa | 2015-07-15 16:04:04 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
Shuzhen Wang | 4bd7abe | 2017-01-11 09:58:58 -0800 | [diff] [blame] | 864 | public void finalizeOutputConfigs(List<OutputConfiguration> outputConfigs) |
Zhijun He | c8b181e | 2016-05-30 14:54:39 -0700 | [diff] [blame] | 865 | throws CameraAccessException { |
Shuzhen Wang | 4bd7abe | 2017-01-11 09:58:58 -0800 | [diff] [blame] | 866 | if (outputConfigs == null || outputConfigs.size() == 0) { |
Zhijun He | c8b181e | 2016-05-30 14:54:39 -0700 | [diff] [blame] | 867 | throw new IllegalArgumentException("deferred config is null or empty"); |
| 868 | } |
| 869 | |
| 870 | synchronized(mInterfaceLock) { |
Shuzhen Wang | 4bd7abe | 2017-01-11 09:58:58 -0800 | [diff] [blame] | 871 | for (OutputConfiguration config : outputConfigs) { |
Zhijun He | c8b181e | 2016-05-30 14:54:39 -0700 | [diff] [blame] | 872 | int streamId = -1; |
| 873 | for (int i = 0; i < mConfiguredOutputs.size(); i++) { |
| 874 | // Have to use equal here, as createCaptureSessionByOutputConfigurations() and |
| 875 | // createReprocessableCaptureSessionByConfigurations() do a copy of the configs. |
| 876 | if (config.equals(mConfiguredOutputs.valueAt(i))) { |
| 877 | streamId = mConfiguredOutputs.keyAt(i); |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | if (streamId == -1) { |
| 882 | throw new IllegalArgumentException("Deferred config is not part of this " |
| 883 | + "session"); |
| 884 | } |
| 885 | |
Shuzhen Wang | 4bd7abe | 2017-01-11 09:58:58 -0800 | [diff] [blame] | 886 | if (config.getSurfaces().size() == 0) { |
| 887 | throw new IllegalArgumentException("The final config for stream " + streamId |
| 888 | + " must have at least 1 surface"); |
Zhijun He | c8b181e | 2016-05-30 14:54:39 -0700 | [diff] [blame] | 889 | } |
Shuzhen Wang | 4bd7abe | 2017-01-11 09:58:58 -0800 | [diff] [blame] | 890 | mRemoteDevice.finalizeOutputConfigurations(streamId, config); |
Yin-Chia Yeh | c60af90 | 2017-11-10 20:02:29 -0800 | [diff] [blame] | 891 | mConfiguredOutputs.put(streamId, config); |
Zhijun He | c8b181e | 2016-05-30 14:54:39 -0700 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 896 | public int capture(CaptureRequest request, CaptureCallback callback, Handler handler) |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 897 | throws CameraAccessException { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 898 | if (DEBUG) { |
| 899 | Log.d(TAG, "calling capture"); |
| 900 | } |
| 901 | List<CaptureRequest> requestList = new ArrayList<CaptureRequest>(); |
| 902 | requestList.add(request); |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 903 | return submitCaptureRequest(requestList, callback, handler, /*streaming*/false); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 906 | public int captureBurst(List<CaptureRequest> requests, CaptureCallback callback, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 907 | Handler handler) throws CameraAccessException { |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 908 | if (requests == null || requests.isEmpty()) { |
| 909 | throw new IllegalArgumentException("At least one request must be given"); |
Zhijun He | fc19e2c | 2013-08-22 14:43:07 -0700 | [diff] [blame] | 910 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 911 | return submitCaptureRequest(requests, callback, handler, /*streaming*/false); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 914 | /** |
| 915 | * This method checks lastFrameNumber returned from ICameraDeviceUser methods for |
| 916 | * starting and stopping repeating request and flushing. |
| 917 | * |
| 918 | * <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] | 919 | * sent to HAL. Then onCaptureSequenceAborted is immediately triggered. |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 920 | * If lastFrameNumber is non-negative, then the requestId and lastFrameNumber as the last |
| 921 | * regular frame number will be added to the list mRequestLastFrameNumbersList.</p> |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 922 | * |
| 923 | * @param requestId the request ID of the current repeating request. |
| 924 | * |
| 925 | * @param lastFrameNumber last frame number returned from binder. |
| 926 | */ |
| 927 | private void checkEarlyTriggerSequenceComplete( |
| 928 | final int requestId, final long lastFrameNumber) { |
| 929 | // lastFrameNumber being equal to NO_FRAMES_CAPTURED means that the request |
Eino-Ville Talvala | ee37e30 | 2014-06-10 15:24:26 -0700 | [diff] [blame] | 930 | // was never sent to HAL. Should trigger onCaptureSequenceAborted immediately. |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 931 | if (lastFrameNumber == CaptureCallback.NO_FRAMES_CAPTURED) { |
| 932 | final CaptureCallbackHolder holder; |
| 933 | int index = mCaptureCallbackMap.indexOfKey(requestId); |
| 934 | holder = (index >= 0) ? mCaptureCallbackMap.valueAt(index) : null; |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 935 | if (holder != null) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 936 | mCaptureCallbackMap.removeAt(index); |
Jianing Wei | baf0c65 | 2014-04-18 17:35:00 -0700 | [diff] [blame] | 937 | if (DEBUG) { |
| 938 | Log.v(TAG, String.format( |
| 939 | "remove holder for requestId %d, " |
| 940 | + "because lastFrame is %d.", |
| 941 | requestId, lastFrameNumber)); |
| 942 | } |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | if (holder != null) { |
| 946 | if (DEBUG) { |
Eino-Ville Talvala | ee37e30 | 2014-06-10 15:24:26 -0700 | [diff] [blame] | 947 | Log.v(TAG, "immediately trigger onCaptureSequenceAborted because" |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 948 | + " request did not reach HAL"); |
| 949 | } |
| 950 | |
| 951 | Runnable resultDispatch = new Runnable() { |
| 952 | @Override |
| 953 | public void run() { |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 954 | if (!CameraDeviceImpl.this.isClosed()) { |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 955 | if (DEBUG) { |
| 956 | Log.d(TAG, String.format( |
| 957 | "early trigger sequence complete for request %d", |
| 958 | requestId)); |
| 959 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 960 | holder.getCallback().onCaptureSequenceAborted( |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 961 | CameraDeviceImpl.this, |
Eino-Ville Talvala | ee37e30 | 2014-06-10 15:24:26 -0700 | [diff] [blame] | 962 | requestId); |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | }; |
| 966 | holder.getHandler().post(resultDispatch); |
| 967 | } else { |
| 968 | Log.w(TAG, String.format( |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 969 | "did not register callback to request %d", |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 970 | requestId)); |
| 971 | } |
| 972 | } else { |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 973 | // This function is only called for regular request so lastFrameNumber is the last |
| 974 | // regular frame number. |
| 975 | mRequestLastFrameNumbersList.add(new RequestLastFrameNumbersHolder(requestId, |
| 976 | lastFrameNumber)); |
| 977 | |
Eino-Ville Talvala | 848fe73 | 2014-09-15 16:24:08 -0700 | [diff] [blame] | 978 | // It is possible that the last frame has already arrived, so we need to check |
| 979 | // for sequence completion right away |
| 980 | checkAndFireSequenceComplete(); |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 984 | private int submitCaptureRequest(List<CaptureRequest> requestList, CaptureCallback callback, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 985 | Handler handler, boolean repeating) throws CameraAccessException { |
| 986 | |
| 987 | // 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] | 988 | // callback is valid |
| 989 | handler = checkHandler(handler, callback); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 990 | |
Shuzhen Wang | 23d2938 | 2017-11-26 17:24:56 -0800 | [diff] [blame] | 991 | // Make sure that there all requests have at least 1 surface; all surfaces are non-null; |
| 992 | // the surface isn't a physical stream surface for reprocessing request |
Ruben Brunk | 7f2372b | 2014-07-02 11:05:08 -0700 | [diff] [blame] | 993 | for (CaptureRequest request : requestList) { |
| 994 | if (request.getTargets().isEmpty()) { |
| 995 | throw new IllegalArgumentException( |
| 996 | "Each request must have at least one Surface target"); |
| 997 | } |
| 998 | |
| 999 | for (Surface surface : request.getTargets()) { |
| 1000 | if (surface == null) { |
| 1001 | throw new IllegalArgumentException("Null Surface targets are not allowed"); |
| 1002 | } |
Shuzhen Wang | 23d2938 | 2017-11-26 17:24:56 -0800 | [diff] [blame] | 1003 | |
| 1004 | if (!request.isReprocess()) { |
| 1005 | continue; |
| 1006 | } |
| 1007 | for (int i = 0; i < mConfiguredOutputs.size(); i++) { |
| 1008 | OutputConfiguration configuration = mConfiguredOutputs.valueAt(i); |
| 1009 | if (configuration.isForPhysicalCamera() |
| 1010 | && configuration.getSurfaces().contains(surface)) { |
| 1011 | throw new IllegalArgumentException( |
| 1012 | "Reprocess request on physical stream is not allowed"); |
| 1013 | } |
| 1014 | } |
Ruben Brunk | 7f2372b | 2014-07-02 11:05:08 -0700 | [diff] [blame] | 1015 | } |
Shuzhen Wang | 23d2938 | 2017-11-26 17:24:56 -0800 | [diff] [blame] | 1016 | |
Ruben Brunk | 7f2372b | 2014-07-02 11:05:08 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1019 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 1020 | checkIfCameraClosedOrInError(); |
Ruben Brunk | e73b41b | 2013-11-07 19:30:43 -0800 | [diff] [blame] | 1021 | if (repeating) { |
| 1022 | stopRepeating(); |
| 1023 | } |
| 1024 | |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1025 | SubmitInfo requestInfo; |
| 1026 | |
| 1027 | CaptureRequest[] requestArray = requestList.toArray(new CaptureRequest[requestList.size()]); |
Yin-Chia Yeh | c60af90 | 2017-11-10 20:02:29 -0800 | [diff] [blame] | 1028 | // Convert Surface to streamIdx and surfaceIdx |
| 1029 | for (CaptureRequest request : requestArray) { |
| 1030 | request.convertSurfaceToStreamId(mConfiguredOutputs); |
| 1031 | } |
| 1032 | |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1033 | requestInfo = mRemoteDevice.submitRequestList(requestArray, repeating); |
| 1034 | if (DEBUG) { |
| 1035 | Log.v(TAG, "last frame number " + requestInfo.getLastFrameNumber()); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1036 | } |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 1037 | |
Yin-Chia Yeh | c60af90 | 2017-11-10 20:02:29 -0800 | [diff] [blame] | 1038 | for (CaptureRequest request : requestArray) { |
| 1039 | request.recoverStreamIdToSurface(); |
| 1040 | } |
| 1041 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1042 | if (callback != null) { |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1043 | mCaptureCallbackMap.put(requestInfo.getRequestId(), |
| 1044 | new CaptureCallbackHolder( |
| 1045 | callback, requestList, handler, repeating, mNextSessionId - 1)); |
Jianing Wei | baf0c65 | 2014-04-18 17:35:00 -0700 | [diff] [blame] | 1046 | } else { |
| 1047 | if (DEBUG) { |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1048 | Log.d(TAG, "Listen for request " + requestInfo.getRequestId() + " is null"); |
Jianing Wei | baf0c65 | 2014-04-18 17:35:00 -0700 | [diff] [blame] | 1049 | } |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 1050 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1051 | |
| 1052 | if (repeating) { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1053 | if (mRepeatingRequestId != REQUEST_ID_NONE) { |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1054 | checkEarlyTriggerSequenceComplete(mRepeatingRequestId, |
| 1055 | requestInfo.getLastFrameNumber()); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1056 | } |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1057 | mRepeatingRequestId = requestInfo.getRequestId(); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1058 | } else { |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1059 | mRequestLastFrameNumbersList.add( |
| 1060 | new RequestLastFrameNumbersHolder(requestList, requestInfo)); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1063 | if (mIdle) { |
| 1064 | mDeviceHandler.post(mCallOnActive); |
| 1065 | } |
| 1066 | mIdle = false; |
| 1067 | |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1068 | return requestInfo.getRequestId(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1072 | public int setRepeatingRequest(CaptureRequest request, CaptureCallback callback, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 1073 | Handler handler) throws CameraAccessException { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1074 | List<CaptureRequest> requestList = new ArrayList<CaptureRequest>(); |
| 1075 | requestList.add(request); |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1076 | return submitCaptureRequest(requestList, callback, handler, /*streaming*/true); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1079 | public int setRepeatingBurst(List<CaptureRequest> requests, CaptureCallback callback, |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 1080 | Handler handler) throws CameraAccessException { |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 1081 | if (requests == null || requests.isEmpty()) { |
| 1082 | throw new IllegalArgumentException("At least one request must be given"); |
Zhijun He | fc19e2c | 2013-08-22 14:43:07 -0700 | [diff] [blame] | 1083 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1084 | return submitCaptureRequest(requests, callback, handler, /*streaming*/true); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1087 | public void stopRepeating() throws CameraAccessException { |
| 1088 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1089 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 1090 | checkIfCameraClosedOrInError(); |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 1091 | if (mRepeatingRequestId != REQUEST_ID_NONE) { |
| 1092 | |
| 1093 | int requestId = mRepeatingRequestId; |
| 1094 | mRepeatingRequestId = REQUEST_ID_NONE; |
| 1095 | |
Chien-Yu Chen | 2da496f | 2016-04-14 13:33:00 -0700 | [diff] [blame] | 1096 | long lastFrameNumber; |
| 1097 | try { |
| 1098 | lastFrameNumber = mRemoteDevice.cancelRequest(requestId); |
| 1099 | } catch (IllegalArgumentException e) { |
| 1100 | if (DEBUG) { |
| 1101 | Log.v(TAG, "Repeating request was already stopped for request " + requestId); |
| 1102 | } |
| 1103 | // Repeating request was already stopped. Nothing more to do. |
| 1104 | return; |
| 1105 | } |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 1106 | |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1107 | checkEarlyTriggerSequenceComplete(requestId, lastFrameNumber); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | |
Zhijun He | d842fcd | 2013-12-26 14:14:04 -0800 | [diff] [blame] | 1112 | private void waitUntilIdle() throws CameraAccessException { |
Zhijun He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 1113 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1114 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 1115 | checkIfCameraClosedOrInError(); |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1116 | |
Ruben Brunk | decfe95 | 2013-10-29 11:00:32 -0700 | [diff] [blame] | 1117 | if (mRepeatingRequestId != REQUEST_ID_NONE) { |
Zhijun He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 1118 | throw new IllegalStateException("Active repeating request ongoing"); |
| 1119 | } |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1120 | |
| 1121 | mRemoteDevice.waitUntilIdle(); |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1122 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Eino-Ville Talvala | 8ebd52b | 2013-08-13 12:09:44 -0700 | [diff] [blame] | 1125 | public void flush() throws CameraAccessException { |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1126 | synchronized(mInterfaceLock) { |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 1127 | checkIfCameraClosedOrInError(); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1128 | |
| 1129 | mDeviceHandler.post(mCallOnBusy); |
Eino-Ville Talvala | c346bbf | 2014-08-27 17:05:29 -0700 | [diff] [blame] | 1130 | |
| 1131 | // If already idle, just do a busy->idle transition immediately, don't actually |
| 1132 | // flush. |
| 1133 | if (mIdle) { |
| 1134 | mDeviceHandler.post(mCallOnIdle); |
| 1135 | return; |
| 1136 | } |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1137 | |
| 1138 | long lastFrameNumber = mRemoteDevice.flush(); |
| 1139 | if (mRepeatingRequestId != REQUEST_ID_NONE) { |
| 1140 | checkEarlyTriggerSequenceComplete(mRepeatingRequestId, lastFrameNumber); |
| 1141 | mRepeatingRequestId = REQUEST_ID_NONE; |
Eino-Ville Talvala | 8ebd52b | 2013-08-13 12:09:44 -0700 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | @Override |
Igor Murashkin | 5c9eaf6 | 2013-09-10 19:35:24 -0700 | [diff] [blame] | 1147 | public void close() { |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1148 | synchronized (mInterfaceLock) { |
Ruben Brunk | 0953b64 | 2015-06-11 16:12:35 -0700 | [diff] [blame] | 1149 | if (mClosing.getAndSet(true)) { |
| 1150 | return; |
| 1151 | } |
| 1152 | |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1153 | if (mRemoteDevice != null) { |
| 1154 | mRemoteDevice.disconnect(); |
Eino-Ville Talvala | ee46b58 | 2016-04-04 12:26:40 -0700 | [diff] [blame] | 1155 | mRemoteDevice.unlinkToDeath(this, /*flags*/0); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 1158 | // Only want to fire the onClosed callback once; |
| 1159 | // either a normal close where the remote device is valid |
| 1160 | // or a close after a startup error (no remote device but in error state) |
| 1161 | if (mRemoteDevice != null || mInError) { |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1162 | mDeviceHandler.post(mCallOnClosed); |
| 1163 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1164 | |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1165 | mRemoteDevice = null; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | @Override |
| 1170 | protected void finalize() throws Throwable { |
| 1171 | try { |
| 1172 | close(); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1173 | } |
| 1174 | finally { |
| 1175 | super.finalize(); |
| 1176 | } |
| 1177 | } |
| 1178 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1179 | private void checkInputConfiguration(InputConfiguration inputConfig) { |
| 1180 | if (inputConfig != null) { |
| 1181 | StreamConfigurationMap configMap = mCharacteristics.get( |
| 1182 | CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); |
| 1183 | |
| 1184 | int[] inputFormats = configMap.getInputFormats(); |
| 1185 | boolean validFormat = false; |
| 1186 | for (int format : inputFormats) { |
| 1187 | if (format == inputConfig.getFormat()) { |
| 1188 | validFormat = true; |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | if (validFormat == false) { |
| 1193 | throw new IllegalArgumentException("input format " + inputConfig.getFormat() + |
| 1194 | " is not valid"); |
| 1195 | } |
| 1196 | |
| 1197 | boolean validSize = false; |
| 1198 | Size[] inputSizes = configMap.getInputSizes(inputConfig.getFormat()); |
| 1199 | for (Size s : inputSizes) { |
| 1200 | if (inputConfig.getWidth() == s.getWidth() && |
| 1201 | inputConfig.getHeight() == s.getHeight()) { |
| 1202 | validSize = true; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | if (validSize == false) { |
| 1207 | throw new IllegalArgumentException("input size " + inputConfig.getWidth() + "x" + |
| 1208 | inputConfig.getHeight() + " is not valid"); |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1213 | /** |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1214 | * <p>A callback for tracking the progress of a {@link CaptureRequest} |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1215 | * submitted to the camera device.</p> |
| 1216 | * |
Eino-Ville Talvala | cad1bfe | 2017-05-23 14:10:49 -0700 | [diff] [blame] | 1217 | * An interface instead of an abstract class because this is internal and |
| 1218 | * we want to make sure we always implement all its callbacks until we reach |
| 1219 | * the public layer. |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1220 | */ |
Eino-Ville Talvala | cad1bfe | 2017-05-23 14:10:49 -0700 | [diff] [blame] | 1221 | public interface CaptureCallback { |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1222 | |
| 1223 | /** |
| 1224 | * This constant is used to indicate that no images were captured for |
| 1225 | * the request. |
| 1226 | * |
| 1227 | * @hide |
| 1228 | */ |
| 1229 | public static final int NO_FRAMES_CAPTURED = -1; |
| 1230 | |
| 1231 | /** |
| 1232 | * This method is called when the camera device has started capturing |
| 1233 | * the output image for the request, at the beginning of image exposure. |
| 1234 | * |
| 1235 | * @see android.media.MediaActionSound |
| 1236 | */ |
| 1237 | public void onCaptureStarted(CameraDevice camera, |
Eino-Ville Talvala | cad1bfe | 2017-05-23 14:10:49 -0700 | [diff] [blame] | 1238 | CaptureRequest request, long timestamp, long frameNumber); |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1239 | |
| 1240 | /** |
| 1241 | * This method is called when some results from an image capture are |
| 1242 | * available. |
| 1243 | * |
| 1244 | * @hide |
| 1245 | */ |
| 1246 | public void onCapturePartial(CameraDevice camera, |
Eino-Ville Talvala | cad1bfe | 2017-05-23 14:10:49 -0700 | [diff] [blame] | 1247 | CaptureRequest request, CaptureResult result); |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1248 | |
| 1249 | /** |
| 1250 | * This method is called when an image capture makes partial forward progress; some |
| 1251 | * (but not all) results from an image capture are available. |
| 1252 | * |
| 1253 | */ |
| 1254 | public void onCaptureProgressed(CameraDevice camera, |
Eino-Ville Talvala | cad1bfe | 2017-05-23 14:10:49 -0700 | [diff] [blame] | 1255 | CaptureRequest request, CaptureResult partialResult); |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1256 | |
| 1257 | /** |
| 1258 | * This method is called when an image capture has fully completed and all the |
| 1259 | * result metadata is available. |
| 1260 | */ |
| 1261 | public void onCaptureCompleted(CameraDevice camera, |
Eino-Ville Talvala | cad1bfe | 2017-05-23 14:10:49 -0700 | [diff] [blame] | 1262 | CaptureRequest request, TotalCaptureResult result); |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1263 | |
| 1264 | /** |
| 1265 | * This method is called instead of {@link #onCaptureCompleted} when the |
| 1266 | * camera device failed to produce a {@link CaptureResult} for the |
| 1267 | * request. |
| 1268 | */ |
| 1269 | public void onCaptureFailed(CameraDevice camera, |
Eino-Ville Talvala | cad1bfe | 2017-05-23 14:10:49 -0700 | [diff] [blame] | 1270 | CaptureRequest request, CaptureFailure failure); |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1271 | |
| 1272 | /** |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1273 | * This method is called independently of the others in CaptureCallback, |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1274 | * when a capture sequence finishes and all {@link CaptureResult} |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1275 | * or {@link CaptureFailure} for it have been returned via this callback. |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1276 | */ |
| 1277 | public void onCaptureSequenceCompleted(CameraDevice camera, |
Eino-Ville Talvala | cad1bfe | 2017-05-23 14:10:49 -0700 | [diff] [blame] | 1278 | int sequenceId, long frameNumber); |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1279 | |
| 1280 | /** |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1281 | * This method is called independently of the others in CaptureCallback, |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1282 | * when a capture sequence aborts before any {@link CaptureResult} |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1283 | * or {@link CaptureFailure} for it have been returned via this callback. |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1284 | */ |
| 1285 | public void onCaptureSequenceAborted(CameraDevice camera, |
Eino-Ville Talvala | cad1bfe | 2017-05-23 14:10:49 -0700 | [diff] [blame] | 1286 | int sequenceId); |
Eino-Ville Talvala | 3e0023a | 2016-03-06 18:40:01 -0800 | [diff] [blame] | 1287 | |
Eino-Ville Talvala | cad1bfe | 2017-05-23 14:10:49 -0700 | [diff] [blame] | 1288 | /** |
| 1289 | * This method is called independently of the others in CaptureCallback, if an output buffer |
| 1290 | * is dropped for a particular capture request. |
| 1291 | * |
| 1292 | * Loss of metadata is communicated via onCaptureFailed, independently of any buffer loss. |
| 1293 | */ |
Eino-Ville Talvala | 3e0023a | 2016-03-06 18:40:01 -0800 | [diff] [blame] | 1294 | public void onCaptureBufferLost(CameraDevice camera, |
Eino-Ville Talvala | cad1bfe | 2017-05-23 14:10:49 -0700 | [diff] [blame] | 1295 | CaptureRequest request, Surface target, long frameNumber); |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | /** |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1299 | * 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] | 1300 | * were part of the earlier KK API design, but now only used internally. |
| 1301 | */ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1302 | public static abstract class StateCallbackKK extends StateCallback { |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1303 | /** |
| 1304 | * The method called when a camera device has no outputs configured. |
| 1305 | * |
| 1306 | */ |
| 1307 | public void onUnconfigured(CameraDevice camera) { |
| 1308 | // Default empty implementation |
| 1309 | } |
| 1310 | |
| 1311 | /** |
| 1312 | * The method called when a camera device begins processing |
| 1313 | * {@link CaptureRequest capture requests}. |
| 1314 | * |
| 1315 | */ |
| 1316 | public void onActive(CameraDevice camera) { |
| 1317 | // Default empty implementation |
| 1318 | } |
| 1319 | |
| 1320 | /** |
| 1321 | * The method called when a camera device is busy. |
| 1322 | * |
| 1323 | */ |
| 1324 | public void onBusy(CameraDevice camera) { |
| 1325 | // Default empty implementation |
| 1326 | } |
| 1327 | |
| 1328 | /** |
| 1329 | * The method called when a camera device has finished processing all |
| 1330 | * submitted capture requests and has reached an idle state. |
| 1331 | * |
| 1332 | */ |
| 1333 | public void onIdle(CameraDevice camera) { |
| 1334 | // Default empty implementation |
| 1335 | } |
Eino-Ville Talvala | ad916f7 | 2015-04-11 12:09:11 -0700 | [diff] [blame] | 1336 | |
| 1337 | /** |
Shuzhen Wang | 88f1af2 | 2016-09-30 10:29:28 -0700 | [diff] [blame] | 1338 | * This method is called when camera device's non-repeating request queue is empty, |
| 1339 | * and is ready to start capturing next image. |
| 1340 | */ |
| 1341 | public void onRequestQueueEmpty() { |
| 1342 | // Default empty implementation |
| 1343 | } |
| 1344 | |
| 1345 | /** |
Eino-Ville Talvala | ad916f7 | 2015-04-11 12:09:11 -0700 | [diff] [blame] | 1346 | * The method called when the camera device has finished preparing |
| 1347 | * an output Surface |
| 1348 | */ |
| 1349 | public void onSurfacePrepared(Surface surface) { |
| 1350 | // Default empty implementation |
| 1351 | } |
Eino-Ville Talvala | a6b5ba5 | 2014-07-02 16:30:53 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1354 | static class CaptureCallbackHolder { |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1355 | |
| 1356 | private final boolean mRepeating; |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1357 | private final CaptureCallback mCallback; |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1358 | private final List<CaptureRequest> mRequestList; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 1359 | private final Handler mHandler; |
Chien-Yu Chen | 7a316f6b | 2015-04-13 14:31:45 -0700 | [diff] [blame] | 1360 | private final int mSessionId; |
Shuzhen Wang | ae10eb9 | 2017-03-01 12:58:04 -0800 | [diff] [blame] | 1361 | /** |
| 1362 | * <p>Determine if the callback holder is for a constrained high speed request list that |
| 1363 | * expects batched capture results. Capture results will be batched if the request list |
| 1364 | * is interleaved with preview and video requests. Capture results won't be batched if the |
| 1365 | * request list only contains preview requests, or if the request doesn't belong to a |
| 1366 | * constrained high speed list. |
| 1367 | */ |
| 1368 | private final boolean mHasBatchedOutputs; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1369 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1370 | CaptureCallbackHolder(CaptureCallback callback, List<CaptureRequest> requestList, |
Chien-Yu Chen | 7a316f6b | 2015-04-13 14:31:45 -0700 | [diff] [blame] | 1371 | Handler handler, boolean repeating, int sessionId) { |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1372 | if (callback == null || handler == null) { |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 1373 | throw new UnsupportedOperationException( |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1374 | "Must have a valid handler and a valid callback"); |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 1375 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1376 | mRepeating = repeating; |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 1377 | mHandler = handler; |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1378 | mRequestList = new ArrayList<CaptureRequest>(requestList); |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1379 | mCallback = callback; |
Chien-Yu Chen | 7a316f6b | 2015-04-13 14:31:45 -0700 | [diff] [blame] | 1380 | mSessionId = sessionId; |
Shuzhen Wang | ae10eb9 | 2017-03-01 12:58:04 -0800 | [diff] [blame] | 1381 | |
| 1382 | // Check whether this callback holder is for batched outputs. |
| 1383 | // The logic here should match createHighSpeedRequestList. |
| 1384 | boolean hasBatchedOutputs = true; |
| 1385 | for (int i = 0; i < requestList.size(); i++) { |
| 1386 | CaptureRequest request = requestList.get(i); |
| 1387 | if (!request.isPartOfCRequestList()) { |
| 1388 | hasBatchedOutputs = false; |
| 1389 | break; |
| 1390 | } |
| 1391 | if (i == 0) { |
| 1392 | Collection<Surface> targets = request.getTargets(); |
| 1393 | if (targets.size() != 2) { |
| 1394 | hasBatchedOutputs = false; |
| 1395 | break; |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | mHasBatchedOutputs = hasBatchedOutputs; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | public boolean isRepeating() { |
| 1403 | return mRepeating; |
| 1404 | } |
| 1405 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1406 | public CaptureCallback getCallback() { |
| 1407 | return mCallback; |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1410 | public CaptureRequest getRequest(int subsequenceId) { |
| 1411 | if (subsequenceId >= mRequestList.size()) { |
| 1412 | throw new IllegalArgumentException( |
| 1413 | String.format( |
| 1414 | "Requested subsequenceId %d is larger than request list size %d.", |
| 1415 | subsequenceId, mRequestList.size())); |
| 1416 | } else { |
| 1417 | if (subsequenceId < 0) { |
| 1418 | throw new IllegalArgumentException(String.format( |
| 1419 | "Requested subsequenceId %d is negative", subsequenceId)); |
| 1420 | } else { |
| 1421 | return mRequestList.get(subsequenceId); |
| 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1426 | public CaptureRequest getRequest() { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1427 | return getRequest(0); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1428 | } |
Eino-Ville Talvala | 4af73c2 | 2013-08-14 10:35:46 -0700 | [diff] [blame] | 1429 | |
| 1430 | public Handler getHandler() { |
| 1431 | return mHandler; |
| 1432 | } |
| 1433 | |
Chien-Yu Chen | 7a316f6b | 2015-04-13 14:31:45 -0700 | [diff] [blame] | 1434 | public int getSessionId() { |
| 1435 | return mSessionId; |
| 1436 | } |
Shuzhen Wang | ae10eb9 | 2017-03-01 12:58:04 -0800 | [diff] [blame] | 1437 | |
| 1438 | public int getRequestCount() { |
| 1439 | return mRequestList.size(); |
| 1440 | } |
| 1441 | |
| 1442 | public boolean hasBatchedOutputs() { |
| 1443 | return mHasBatchedOutputs; |
| 1444 | } |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1445 | } |
Chien-Yu Chen | 7a316f6b | 2015-04-13 14:31:45 -0700 | [diff] [blame] | 1446 | |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1447 | /** |
| 1448 | * This class holds a capture ID and its expected last regular frame number and last reprocess |
| 1449 | * frame number. |
| 1450 | */ |
| 1451 | static class RequestLastFrameNumbersHolder { |
| 1452 | // request ID |
| 1453 | private final int mRequestId; |
| 1454 | // The last regular frame number for this request ID. It's |
| 1455 | // CaptureCallback.NO_FRAMES_CAPTURED if the request ID has no regular request. |
| 1456 | private final long mLastRegularFrameNumber; |
| 1457 | // The last reprocess frame number for this request ID. It's |
| 1458 | // CaptureCallback.NO_FRAMES_CAPTURED if the request ID has no reprocess request. |
| 1459 | private final long mLastReprocessFrameNumber; |
| 1460 | |
| 1461 | /** |
| 1462 | * Create a request-last-frame-numbers holder with a list of requests, request ID, and |
| 1463 | * the last frame number returned by camera service. |
| 1464 | */ |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1465 | public RequestLastFrameNumbersHolder(List<CaptureRequest> requestList, SubmitInfo requestInfo) { |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1466 | long lastRegularFrameNumber = CaptureCallback.NO_FRAMES_CAPTURED; |
| 1467 | long lastReprocessFrameNumber = CaptureCallback.NO_FRAMES_CAPTURED; |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1468 | long frameNumber = requestInfo.getLastFrameNumber(); |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1469 | |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1470 | if (requestInfo.getLastFrameNumber() < requestList.size() - 1) { |
| 1471 | throw new IllegalArgumentException( |
| 1472 | "lastFrameNumber: " + requestInfo.getLastFrameNumber() + |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1473 | " should be at least " + (requestList.size() - 1) + " for the number of " + |
| 1474 | " requests in the list: " + requestList.size()); |
| 1475 | } |
| 1476 | |
| 1477 | // find the last regular frame number and the last reprocess frame number |
| 1478 | for (int i = requestList.size() - 1; i >= 0; i--) { |
| 1479 | CaptureRequest request = requestList.get(i); |
| 1480 | if (request.isReprocess() && lastReprocessFrameNumber == |
| 1481 | CaptureCallback.NO_FRAMES_CAPTURED) { |
| 1482 | lastReprocessFrameNumber = frameNumber; |
| 1483 | } else if (!request.isReprocess() && lastRegularFrameNumber == |
| 1484 | CaptureCallback.NO_FRAMES_CAPTURED) { |
| 1485 | lastRegularFrameNumber = frameNumber; |
| 1486 | } |
| 1487 | |
| 1488 | if (lastReprocessFrameNumber != CaptureCallback.NO_FRAMES_CAPTURED && |
| 1489 | lastRegularFrameNumber != CaptureCallback.NO_FRAMES_CAPTURED) { |
| 1490 | break; |
| 1491 | } |
| 1492 | |
| 1493 | frameNumber--; |
| 1494 | } |
| 1495 | |
| 1496 | mLastRegularFrameNumber = lastRegularFrameNumber; |
| 1497 | mLastReprocessFrameNumber = lastReprocessFrameNumber; |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 1498 | mRequestId = requestInfo.getRequestId(); |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | /** |
| 1502 | * Create a request-last-frame-numbers holder with a request ID and last regular frame |
| 1503 | * number. |
| 1504 | */ |
| 1505 | public RequestLastFrameNumbersHolder(int requestId, long lastRegularFrameNumber) { |
| 1506 | mLastRegularFrameNumber = lastRegularFrameNumber; |
| 1507 | mLastReprocessFrameNumber = CaptureCallback.NO_FRAMES_CAPTURED; |
| 1508 | mRequestId = requestId; |
| 1509 | } |
| 1510 | |
| 1511 | /** |
| 1512 | * Return the last regular frame number. Return CaptureCallback.NO_FRAMES_CAPTURED if |
| 1513 | * it contains no regular request. |
| 1514 | */ |
| 1515 | public long getLastRegularFrameNumber() { |
| 1516 | return mLastRegularFrameNumber; |
| 1517 | } |
| 1518 | |
| 1519 | /** |
| 1520 | * Return the last reprocess frame number. Return CaptureCallback.NO_FRAMES_CAPTURED if |
| 1521 | * it contains no reprocess request. |
| 1522 | */ |
| 1523 | public long getLastReprocessFrameNumber() { |
| 1524 | return mLastReprocessFrameNumber; |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * Return the last frame number overall. |
| 1529 | */ |
| 1530 | public long getLastFrameNumber() { |
| 1531 | return Math.max(mLastRegularFrameNumber, mLastReprocessFrameNumber); |
| 1532 | } |
| 1533 | |
| 1534 | /** |
| 1535 | * Return the request ID. |
| 1536 | */ |
| 1537 | public int getRequestId() { |
| 1538 | return mRequestId; |
| 1539 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1542 | /** |
| 1543 | * This class tracks the last frame number for submitted requests. |
| 1544 | */ |
| 1545 | public class FrameNumberTracker { |
| 1546 | |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1547 | private long mCompletedFrameNumber = CaptureCallback.NO_FRAMES_CAPTURED; |
| 1548 | private long mCompletedReprocessFrameNumber = CaptureCallback.NO_FRAMES_CAPTURED; |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1549 | /** the skipped frame numbers that belong to regular results */ |
| 1550 | private final LinkedList<Long> mSkippedRegularFrameNumbers = new LinkedList<Long>(); |
| 1551 | /** the skipped frame numbers that belong to reprocess results */ |
| 1552 | private final LinkedList<Long> mSkippedReprocessFrameNumbers = new LinkedList<Long>(); |
| 1553 | /** frame number -> is reprocess */ |
| 1554 | private final TreeMap<Long, Boolean> mFutureErrorMap = new TreeMap<Long, Boolean>(); |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1555 | /** Map frame numbers to list of partial results */ |
| 1556 | private final HashMap<Long, List<CaptureResult>> mPartialResults = new HashMap<>(); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1557 | |
| 1558 | private void update() { |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1559 | Iterator iter = mFutureErrorMap.entrySet().iterator(); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1560 | while (iter.hasNext()) { |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1561 | TreeMap.Entry pair = (TreeMap.Entry)iter.next(); |
| 1562 | Long errorFrameNumber = (Long)pair.getKey(); |
| 1563 | Boolean reprocess = (Boolean)pair.getValue(); |
| 1564 | Boolean removeError = true; |
| 1565 | if (reprocess) { |
| 1566 | if (errorFrameNumber == mCompletedReprocessFrameNumber + 1) { |
| 1567 | mCompletedReprocessFrameNumber = errorFrameNumber; |
| 1568 | } else if (mSkippedReprocessFrameNumbers.isEmpty() != true && |
| 1569 | errorFrameNumber == mSkippedReprocessFrameNumbers.element()) { |
| 1570 | mCompletedReprocessFrameNumber = errorFrameNumber; |
| 1571 | mSkippedReprocessFrameNumbers.remove(); |
| 1572 | } else { |
| 1573 | removeError = false; |
| 1574 | } |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1575 | } else { |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1576 | if (errorFrameNumber == mCompletedFrameNumber + 1) { |
| 1577 | mCompletedFrameNumber = errorFrameNumber; |
| 1578 | } else if (mSkippedRegularFrameNumbers.isEmpty() != true && |
| 1579 | errorFrameNumber == mSkippedRegularFrameNumbers.element()) { |
| 1580 | mCompletedFrameNumber = errorFrameNumber; |
| 1581 | mSkippedRegularFrameNumbers.remove(); |
| 1582 | } else { |
| 1583 | removeError = false; |
| 1584 | } |
| 1585 | } |
| 1586 | if (removeError) { |
| 1587 | iter.remove(); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | /** |
| 1593 | * 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] | 1594 | * @param frameNumber the frame number corresponding to the result or error |
| 1595 | * @param isError true if it is an error, false if it is not an error |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1596 | * @param isReprocess true if it is a reprocess result, false if it is a regular result. |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1597 | */ |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1598 | public void updateTracker(long frameNumber, boolean isError, boolean isReprocess) { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1599 | if (isError) { |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1600 | mFutureErrorMap.put(frameNumber, isReprocess); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1601 | } else { |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1602 | try { |
| 1603 | if (isReprocess) { |
| 1604 | updateCompletedReprocessFrameNumber(frameNumber); |
| 1605 | } else { |
| 1606 | updateCompletedFrameNumber(frameNumber); |
| 1607 | } |
| 1608 | } catch (IllegalArgumentException e) { |
| 1609 | Log.e(TAG, e.getMessage()); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1610 | } |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1611 | } |
| 1612 | update(); |
| 1613 | } |
| 1614 | |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1615 | /** |
| 1616 | * This function is called every time a result has been completed. |
| 1617 | * |
| 1618 | * <p>It keeps a track of all the partial results already created for a particular |
| 1619 | * frame number.</p> |
| 1620 | * |
| 1621 | * @param frameNumber the frame number corresponding to the result |
| 1622 | * @param result the total or partial result |
| 1623 | * @param partial {@true} if the result is partial, {@code false} if total |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1624 | * @param isReprocess true if it is a reprocess result, false if it is a regular result. |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1625 | */ |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1626 | public void updateTracker(long frameNumber, CaptureResult result, boolean partial, |
| 1627 | boolean isReprocess) { |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1628 | if (!partial) { |
| 1629 | // Update the total result's frame status as being successful |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1630 | updateTracker(frameNumber, /*isError*/false, isReprocess); |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 1631 | // Don't keep a list of total results, we don't need to track them |
| 1632 | return; |
| 1633 | } |
| 1634 | |
| 1635 | if (result == null) { |
| 1636 | // Do not record blank results; this also means there will be no total result |
| 1637 | // so it doesn't matter that the partials were not recorded |
| 1638 | return; |
| 1639 | } |
| 1640 | |
| 1641 | // Partial results must be aggregated in-order for that frame number |
| 1642 | List<CaptureResult> partials = mPartialResults.get(frameNumber); |
| 1643 | if (partials == null) { |
| 1644 | partials = new ArrayList<>(); |
| 1645 | mPartialResults.put(frameNumber, partials); |
| 1646 | } |
| 1647 | |
| 1648 | partials.add(result); |
| 1649 | } |
| 1650 | |
| 1651 | /** |
| 1652 | * Attempt to pop off all of the partial results seen so far for the {@code frameNumber}. |
| 1653 | * |
| 1654 | * <p>Once popped-off, the partial results are forgotten (unless {@code updateTracker} |
| 1655 | * is called again with new partials for that frame number).</p> |
| 1656 | * |
| 1657 | * @param frameNumber the frame number corresponding to the result |
| 1658 | * @return a list of partial results for that frame with at least 1 element, |
| 1659 | * or {@code null} if there were no partials recorded for that frame |
| 1660 | */ |
| 1661 | public List<CaptureResult> popPartialResults(long frameNumber) { |
| 1662 | return mPartialResults.remove(frameNumber); |
| 1663 | } |
| 1664 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1665 | public long getCompletedFrameNumber() { |
| 1666 | return mCompletedFrameNumber; |
| 1667 | } |
| 1668 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1669 | public long getCompletedReprocessFrameNumber() { |
| 1670 | return mCompletedReprocessFrameNumber; |
| 1671 | } |
| 1672 | |
| 1673 | /** |
| 1674 | * Update the completed frame number for regular results. |
| 1675 | * |
| 1676 | * It validates that all previous frames have arrived except for reprocess frames. |
| 1677 | * |
| 1678 | * If there is a gap since previous regular frame number, assume the frames in the gap are |
| 1679 | * reprocess frames and store them in the skipped reprocess frame number queue to check |
| 1680 | * against when reprocess frames arrive. |
| 1681 | */ |
| 1682 | private void updateCompletedFrameNumber(long frameNumber) throws IllegalArgumentException { |
| 1683 | if (frameNumber <= mCompletedFrameNumber) { |
| 1684 | throw new IllegalArgumentException("frame number " + frameNumber + " is a repeat"); |
| 1685 | } else if (frameNumber <= mCompletedReprocessFrameNumber) { |
| 1686 | // if frame number is smaller than completed reprocess frame number, |
| 1687 | // it must be the head of mSkippedRegularFrameNumbers |
| 1688 | if (mSkippedRegularFrameNumbers.isEmpty() == true || |
| 1689 | frameNumber < mSkippedRegularFrameNumbers.element()) { |
| 1690 | throw new IllegalArgumentException("frame number " + frameNumber + |
| 1691 | " is a repeat"); |
| 1692 | } else if (frameNumber > mSkippedRegularFrameNumbers.element()) { |
| 1693 | throw new IllegalArgumentException("frame number " + frameNumber + |
| 1694 | " comes out of order. Expecting " + |
| 1695 | mSkippedRegularFrameNumbers.element()); |
| 1696 | } |
| 1697 | // frame number matches the head of the skipped frame number queue. |
| 1698 | mSkippedRegularFrameNumbers.remove(); |
| 1699 | } else { |
| 1700 | // there is a gap of unseen frame numbers which should belong to reprocess result |
| 1701 | // put all the skipped frame numbers in the queue |
| 1702 | for (long i = Math.max(mCompletedFrameNumber, mCompletedReprocessFrameNumber) + 1; |
| 1703 | i < frameNumber; i++) { |
| 1704 | mSkippedReprocessFrameNumbers.add(i); |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | mCompletedFrameNumber = frameNumber; |
| 1709 | } |
| 1710 | |
| 1711 | /** |
| 1712 | * Update the completed frame number for reprocess results. |
| 1713 | * |
| 1714 | * It validates that all previous frames have arrived except for regular frames. |
| 1715 | * |
| 1716 | * If there is a gap since previous reprocess frame number, assume the frames in the gap are |
| 1717 | * regular frames and store them in the skipped regular frame number queue to check |
| 1718 | * against when regular frames arrive. |
| 1719 | */ |
| 1720 | private void updateCompletedReprocessFrameNumber(long frameNumber) |
| 1721 | throws IllegalArgumentException { |
| 1722 | if (frameNumber < mCompletedReprocessFrameNumber) { |
| 1723 | throw new IllegalArgumentException("frame number " + frameNumber + " is a repeat"); |
| 1724 | } else if (frameNumber < mCompletedFrameNumber) { |
| 1725 | // if reprocess frame number is smaller than completed regular frame number, |
| 1726 | // it must be the head of the skipped reprocess frame number queue. |
| 1727 | if (mSkippedReprocessFrameNumbers.isEmpty() == true || |
| 1728 | frameNumber < mSkippedReprocessFrameNumbers.element()) { |
| 1729 | throw new IllegalArgumentException("frame number " + frameNumber + |
| 1730 | " is a repeat"); |
| 1731 | } else if (frameNumber > mSkippedReprocessFrameNumbers.element()) { |
| 1732 | throw new IllegalArgumentException("frame number " + frameNumber + |
| 1733 | " comes out of order. Expecting " + |
| 1734 | mSkippedReprocessFrameNumbers.element()); |
| 1735 | } |
| 1736 | // frame number matches the head of the skipped frame number queue. |
| 1737 | mSkippedReprocessFrameNumbers.remove(); |
| 1738 | } else { |
| 1739 | // put all the skipped frame numbers in the queue |
| 1740 | for (long i = Math.max(mCompletedFrameNumber, mCompletedReprocessFrameNumber) + 1; |
| 1741 | i < frameNumber; i++) { |
| 1742 | mSkippedRegularFrameNumbers.add(i); |
| 1743 | } |
| 1744 | } |
| 1745 | mCompletedReprocessFrameNumber = frameNumber; |
| 1746 | } |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | private void checkAndFireSequenceComplete() { |
| 1750 | long completedFrameNumber = mFrameNumberTracker.getCompletedFrameNumber(); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1751 | long completedReprocessFrameNumber = mFrameNumberTracker.getCompletedReprocessFrameNumber(); |
| 1752 | boolean isReprocess = false; |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1753 | Iterator<RequestLastFrameNumbersHolder> iter = mRequestLastFrameNumbersList.iterator(); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1754 | while (iter.hasNext()) { |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1755 | final RequestLastFrameNumbersHolder requestLastFrameNumbers = iter.next(); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1756 | boolean sequenceCompleted = false; |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1757 | final int requestId = requestLastFrameNumbers.getRequestId(); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1758 | final CaptureCallbackHolder holder; |
| 1759 | synchronized(mInterfaceLock) { |
| 1760 | if (mRemoteDevice == null) { |
| 1761 | Log.w(TAG, "Camera closed while checking sequences"); |
| 1762 | return; |
| 1763 | } |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1764 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1765 | int index = mCaptureCallbackMap.indexOfKey(requestId); |
| 1766 | holder = (index >= 0) ? |
| 1767 | mCaptureCallbackMap.valueAt(index) : null; |
| 1768 | if (holder != null) { |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1769 | long lastRegularFrameNumber = |
| 1770 | requestLastFrameNumbers.getLastRegularFrameNumber(); |
| 1771 | long lastReprocessFrameNumber = |
| 1772 | requestLastFrameNumbers.getLastReprocessFrameNumber(); |
| 1773 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1774 | // check if it's okay to remove request from mCaptureCallbackMap |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1775 | if (lastRegularFrameNumber <= completedFrameNumber && |
| 1776 | lastReprocessFrameNumber <= completedReprocessFrameNumber) { |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1777 | sequenceCompleted = true; |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1778 | mCaptureCallbackMap.removeAt(index); |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 1779 | if (DEBUG) { |
| 1780 | Log.v(TAG, String.format( |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1781 | "Remove holder for requestId %d, because lastRegularFrame %d " + |
| 1782 | "is <= %d and lastReprocessFrame %d is <= %d", requestId, |
| 1783 | lastRegularFrameNumber, completedFrameNumber, |
| 1784 | lastReprocessFrameNumber, completedReprocessFrameNumber)); |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 1785 | } |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1786 | } |
| 1787 | } |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | // If no callback is registered for this requestId or sequence completed, remove it |
| 1791 | // from the frame number->request pair because it's not needed anymore. |
| 1792 | if (holder == null || sequenceCompleted) { |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1793 | iter.remove(); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1794 | } |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1795 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1796 | // Call onCaptureSequenceCompleted |
| 1797 | if (sequenceCompleted) { |
| 1798 | Runnable resultDispatch = new Runnable() { |
| 1799 | @Override |
| 1800 | public void run() { |
| 1801 | if (!CameraDeviceImpl.this.isClosed()){ |
| 1802 | if (DEBUG) { |
| 1803 | Log.d(TAG, String.format( |
| 1804 | "fire sequence complete for request %d", |
| 1805 | requestId)); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1806 | } |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1807 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1808 | holder.getCallback().onCaptureSequenceCompleted( |
| 1809 | CameraDeviceImpl.this, |
| 1810 | requestId, |
Chien-Yu Chen | 4560df7 | 2015-04-27 17:21:31 -0700 | [diff] [blame] | 1811 | requestLastFrameNumbers.getLastFrameNumber()); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1812 | } |
| 1813 | } |
| 1814 | }; |
| 1815 | holder.getHandler().post(resultDispatch); |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1816 | } |
| 1817 | } |
| 1818 | } |
| 1819 | |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 1820 | public class CameraDeviceCallbacks extends ICameraDeviceCallbacks.Stub { |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1821 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1822 | @Override |
| 1823 | public IBinder asBinder() { |
| 1824 | return this; |
| 1825 | } |
| 1826 | |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1827 | @Override |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1828 | public void onDeviceError(final int errorCode, CaptureResultExtras resultExtras) { |
| 1829 | if (DEBUG) { |
| 1830 | Log.d(TAG, String.format( |
Ruben Brunk | e663cb77 | 2014-09-16 13:18:31 -0700 | [diff] [blame] | 1831 | "Device error received, code %d, frame number %d, request ID %d, subseq ID %d", |
| 1832 | errorCode, resultExtras.getFrameNumber(), resultExtras.getRequestId(), |
| 1833 | resultExtras.getSubsequenceId())); |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1834 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1835 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1836 | synchronized(mInterfaceLock) { |
| 1837 | if (mRemoteDevice == null) { |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1838 | return; // Camera already closed |
| 1839 | } |
| 1840 | |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1841 | switch (errorCode) { |
| 1842 | case ERROR_CAMERA_DISCONNECTED: |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1843 | CameraDeviceImpl.this.mDeviceHandler.post(mCallOnDisconnected); |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1844 | break; |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1845 | case ERROR_CAMERA_REQUEST: |
| 1846 | case ERROR_CAMERA_RESULT: |
| 1847 | case ERROR_CAMERA_BUFFER: |
| 1848 | onCaptureErrorLocked(errorCode, resultExtras); |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1849 | break; |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 1850 | case ERROR_CAMERA_DEVICE: |
| 1851 | scheduleNotifyError(StateCallback.ERROR_CAMERA_DEVICE); |
| 1852 | break; |
| 1853 | case ERROR_CAMERA_DISABLED: |
| 1854 | scheduleNotifyError(StateCallback.ERROR_CAMERA_DISABLED); |
| 1855 | break; |
| 1856 | default: |
| 1857 | Log.e(TAG, "Unknown error from camera device: " + errorCode); |
| 1858 | scheduleNotifyError(StateCallback.ERROR_CAMERA_SERVICE); |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1859 | } |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 1860 | } |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 1863 | private void scheduleNotifyError(int code) { |
| 1864 | mInError = true; |
| 1865 | CameraDeviceImpl.this.mDeviceHandler.post(obtainRunnable( |
| 1866 | CameraDeviceCallbacks::notifyError, this, code)); |
| 1867 | } |
| 1868 | |
| 1869 | private void notifyError(int code) { |
| 1870 | if (!CameraDeviceImpl.this.isClosed()) { |
| 1871 | mDeviceCallback.onError(CameraDeviceImpl.this, code); |
| 1872 | } |
| 1873 | } |
| 1874 | |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1875 | @Override |
Yin-Chia Yeh | 8cd12e9 | 2017-09-05 18:14:21 -0700 | [diff] [blame] | 1876 | public void onRepeatingRequestError(long lastFrameNumber, int repeatingRequestId) { |
Chien-Yu Chen | 2da496f | 2016-04-14 13:33:00 -0700 | [diff] [blame] | 1877 | if (DEBUG) { |
| 1878 | Log.d(TAG, "Repeating request error received. Last frame number is " + |
| 1879 | lastFrameNumber); |
| 1880 | } |
| 1881 | |
| 1882 | synchronized(mInterfaceLock) { |
| 1883 | // Camera is already closed or no repeating request is present. |
| 1884 | if (mRemoteDevice == null || mRepeatingRequestId == REQUEST_ID_NONE) { |
| 1885 | return; // Camera already closed |
| 1886 | } |
| 1887 | |
| 1888 | checkEarlyTriggerSequenceComplete(mRepeatingRequestId, lastFrameNumber); |
Yin-Chia Yeh | 8cd12e9 | 2017-09-05 18:14:21 -0700 | [diff] [blame] | 1889 | // Check if there is already a new repeating request |
| 1890 | if (mRepeatingRequestId == repeatingRequestId) { |
| 1891 | mRepeatingRequestId = REQUEST_ID_NONE; |
| 1892 | } |
Chien-Yu Chen | 2da496f | 2016-04-14 13:33:00 -0700 | [diff] [blame] | 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | @Override |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 1897 | public void onDeviceIdle() { |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1898 | if (DEBUG) { |
| 1899 | Log.d(TAG, "Camera now idle"); |
| 1900 | } |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1901 | synchronized(mInterfaceLock) { |
| 1902 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1903 | |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 1904 | if (!CameraDeviceImpl.this.mIdle) { |
| 1905 | CameraDeviceImpl.this.mDeviceHandler.post(mCallOnIdle); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1906 | } |
Igor Murashkin | 21547d6 | 2014-06-04 15:21:42 -0700 | [diff] [blame] | 1907 | CameraDeviceImpl.this.mIdle = true; |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | @Override |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1912 | public void onCaptureStarted(final CaptureResultExtras resultExtras, final long timestamp) { |
| 1913 | int requestId = resultExtras.getRequestId(); |
Eino-Ville Talvala | e8df309 | 2014-09-08 14:30:50 -0700 | [diff] [blame] | 1914 | final long frameNumber = resultExtras.getFrameNumber(); |
| 1915 | |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1916 | if (DEBUG) { |
Eino-Ville Talvala | e8df309 | 2014-09-08 14:30:50 -0700 | [diff] [blame] | 1917 | Log.d(TAG, "Capture started for id " + requestId + " frame number " + frameNumber); |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1918 | } |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1919 | final CaptureCallbackHolder holder; |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1920 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1921 | synchronized(mInterfaceLock) { |
| 1922 | if (mRemoteDevice == null) return; // Camera already closed |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1923 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1924 | // Get the callback for this frame ID, if there is one |
| 1925 | holder = CameraDeviceImpl.this.mCaptureCallbackMap.get(requestId); |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1926 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1927 | if (holder == null) { |
| 1928 | return; |
| 1929 | } |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 1930 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1931 | if (isClosed()) return; |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1932 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1933 | // Dispatch capture start notice |
| 1934 | holder.getHandler().post( |
| 1935 | new Runnable() { |
| 1936 | @Override |
| 1937 | public void run() { |
| 1938 | if (!CameraDeviceImpl.this.isClosed()) { |
Shuzhen Wang | ae10eb9 | 2017-03-01 12:58:04 -0800 | [diff] [blame] | 1939 | final int subsequenceId = resultExtras.getSubsequenceId(); |
| 1940 | final CaptureRequest request = holder.getRequest(subsequenceId); |
| 1941 | |
| 1942 | if (holder.hasBatchedOutputs()) { |
| 1943 | // Send derived onCaptureStarted for requests within the batch |
| 1944 | final Range<Integer> fpsRange = |
| 1945 | request.get(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE); |
| 1946 | for (int i = 0; i < holder.getRequestCount(); i++) { |
| 1947 | holder.getCallback().onCaptureStarted( |
| 1948 | CameraDeviceImpl.this, |
| 1949 | holder.getRequest(i), |
| 1950 | timestamp - (subsequenceId - i) * |
| 1951 | NANO_PER_SECOND/fpsRange.getUpper(), |
| 1952 | frameNumber - (subsequenceId - i)); |
| 1953 | } |
| 1954 | } else { |
| 1955 | holder.getCallback().onCaptureStarted( |
| 1956 | CameraDeviceImpl.this, |
| 1957 | holder.getRequest(resultExtras.getSubsequenceId()), |
| 1958 | timestamp, frameNumber); |
| 1959 | } |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1960 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 1961 | } |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1962 | }); |
| 1963 | |
| 1964 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | @Override |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1968 | public void onResultReceived(CameraMetadataNative result, |
| 1969 | CaptureResultExtras resultExtras) throws RemoteException { |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 1970 | |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 1971 | int requestId = resultExtras.getRequestId(); |
Igor Murashkin | bdf366c | 2014-07-25 16:54:20 -0700 | [diff] [blame] | 1972 | long frameNumber = resultExtras.getFrameNumber(); |
| 1973 | |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 1974 | if (DEBUG) { |
Igor Murashkin | bdf366c | 2014-07-25 16:54:20 -0700 | [diff] [blame] | 1975 | Log.v(TAG, "Received result frame " + frameNumber + " for id " |
Jianing Wei | baf0c65 | 2014-04-18 17:35:00 -0700 | [diff] [blame] | 1976 | + requestId); |
Zhijun He | ecb323e | 2013-07-31 09:40:27 -0700 | [diff] [blame] | 1977 | } |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 1978 | |
Eino-Ville Talvala | 2f75e7d | 2014-07-15 10:31:54 -0700 | [diff] [blame] | 1979 | synchronized(mInterfaceLock) { |
| 1980 | if (mRemoteDevice == null) return; // Camera already closed |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 1981 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1982 | // TODO: Handle CameraCharacteristics access from CaptureResult correctly. |
| 1983 | result.set(CameraCharacteristics.LENS_INFO_SHADING_MAP_SIZE, |
| 1984 | getCharacteristics().get(CameraCharacteristics.LENS_INFO_SHADING_MAP_SIZE)); |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 1985 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1986 | final CaptureCallbackHolder holder = |
| 1987 | CameraDeviceImpl.this.mCaptureCallbackMap.get(requestId); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1988 | final CaptureRequest request = holder.getRequest(resultExtras.getSubsequenceId()); |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 1989 | |
Zhijun He | 8315915 | 2014-07-16 11:32:59 -0700 | [diff] [blame] | 1990 | boolean isPartialResult = |
| 1991 | (resultExtras.getPartialResultCount() < mTotalPartialCount); |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 1992 | boolean isReprocess = request.isReprocess(); |
Eino-Ville Talvala | 7a31310 | 2013-11-07 14:45:06 -0800 | [diff] [blame] | 1993 | |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 1994 | // Check if we have a callback for this |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 1995 | if (holder == null) { |
| 1996 | if (DEBUG) { |
| 1997 | Log.d(TAG, |
| 1998 | "holder is null, early return at frame " |
Igor Murashkin | bdf366c | 2014-07-25 16:54:20 -0700 | [diff] [blame] | 1999 | + frameNumber); |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2000 | } |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 2001 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 2002 | mFrameNumberTracker.updateTracker(frameNumber, /*result*/null, isPartialResult, |
| 2003 | isReprocess); |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 2004 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2005 | return; |
Jianing Wei | 5a4b02b | 2014-04-11 09:59:24 -0700 | [diff] [blame] | 2006 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 2007 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2008 | if (isClosed()) { |
| 2009 | if (DEBUG) { |
| 2010 | Log.d(TAG, |
| 2011 | "camera is closed, early return at frame " |
Igor Murashkin | bdf366c | 2014-07-25 16:54:20 -0700 | [diff] [blame] | 2012 | + frameNumber); |
Eino-Ville Talvala | 7a31310 | 2013-11-07 14:45:06 -0800 | [diff] [blame] | 2013 | } |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 2014 | |
Chien-Yu Chen | 5398a67 | 2015-03-19 14:48:43 -0700 | [diff] [blame] | 2015 | mFrameNumberTracker.updateTracker(frameNumber, /*result*/null, isPartialResult, |
| 2016 | isReprocess); |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2017 | return; |
| 2018 | } |
Igor Murashkin | db075af | 2014-05-21 10:07:08 -0700 | [diff] [blame] | 2019 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2020 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2021 | Runnable resultDispatch = null; |
| 2022 | |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 2023 | CaptureResult finalResult; |
Shuzhen Wang | ae10eb9 | 2017-03-01 12:58:04 -0800 | [diff] [blame] | 2024 | // Make a copy of the native metadata before it gets moved to a CaptureResult |
| 2025 | // object. |
| 2026 | final CameraMetadataNative resultCopy; |
| 2027 | if (holder.hasBatchedOutputs()) { |
| 2028 | resultCopy = new CameraMetadataNative(result); |
| 2029 | } else { |
| 2030 | resultCopy = null; |
| 2031 | } |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 2032 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2033 | // Either send a partial result or the final capture completed result |
Zhijun He | 8315915 | 2014-07-16 11:32:59 -0700 | [diff] [blame] | 2034 | if (isPartialResult) { |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2035 | final CaptureResult resultAsCapture = |
Igor Murashkin | bdf366c | 2014-07-25 16:54:20 -0700 | [diff] [blame] | 2036 | new CaptureResult(result, request, resultExtras); |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2037 | // Partial result |
| 2038 | resultDispatch = new Runnable() { |
| 2039 | @Override |
| 2040 | public void run() { |
Shuzhen Wang | ae10eb9 | 2017-03-01 12:58:04 -0800 | [diff] [blame] | 2041 | if (!CameraDeviceImpl.this.isClosed()) { |
| 2042 | if (holder.hasBatchedOutputs()) { |
| 2043 | // Send derived onCaptureProgressed for requests within |
| 2044 | // the batch. |
| 2045 | for (int i = 0; i < holder.getRequestCount(); i++) { |
| 2046 | CameraMetadataNative resultLocal = |
| 2047 | new CameraMetadataNative(resultCopy); |
| 2048 | CaptureResult resultInBatch = new CaptureResult( |
| 2049 | resultLocal, holder.getRequest(i), resultExtras); |
| 2050 | |
| 2051 | holder.getCallback().onCaptureProgressed( |
| 2052 | CameraDeviceImpl.this, |
| 2053 | holder.getRequest(i), |
| 2054 | resultInBatch); |
| 2055 | } |
| 2056 | } else { |
| 2057 | holder.getCallback().onCaptureProgressed( |
| 2058 | CameraDeviceImpl.this, |
| 2059 | request, |
| 2060 | resultAsCapture); |
| 2061 | } |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2062 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 2063 | } |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2064 | }; |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 2065 | finalResult = resultAsCapture; |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2066 | } else { |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 2067 | List<CaptureResult> partialResults = |
| 2068 | mFrameNumberTracker.popPartialResults(frameNumber); |
| 2069 | |
Shuzhen Wang | ae10eb9 | 2017-03-01 12:58:04 -0800 | [diff] [blame] | 2070 | final long sensorTimestamp = |
| 2071 | result.get(CaptureResult.SENSOR_TIMESTAMP); |
| 2072 | final Range<Integer> fpsRange = |
| 2073 | request.get(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE); |
| 2074 | final int subsequenceId = resultExtras.getSubsequenceId(); |
Chien-Yu Chen | 7a316f6b | 2015-04-13 14:31:45 -0700 | [diff] [blame] | 2075 | final TotalCaptureResult resultAsCapture = new TotalCaptureResult(result, |
| 2076 | request, resultExtras, partialResults, holder.getSessionId()); |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2077 | // Final capture result |
| 2078 | resultDispatch = new Runnable() { |
| 2079 | @Override |
| 2080 | public void run() { |
| 2081 | if (!CameraDeviceImpl.this.isClosed()){ |
Shuzhen Wang | ae10eb9 | 2017-03-01 12:58:04 -0800 | [diff] [blame] | 2082 | if (holder.hasBatchedOutputs()) { |
| 2083 | // Send derived onCaptureCompleted for requests within |
| 2084 | // the batch. |
| 2085 | for (int i = 0; i < holder.getRequestCount(); i++) { |
| 2086 | resultCopy.set(CaptureResult.SENSOR_TIMESTAMP, |
| 2087 | sensorTimestamp - (subsequenceId - i) * |
| 2088 | NANO_PER_SECOND/fpsRange.getUpper()); |
| 2089 | CameraMetadataNative resultLocal = |
| 2090 | new CameraMetadataNative(resultCopy); |
| 2091 | TotalCaptureResult resultInBatch = new TotalCaptureResult( |
| 2092 | resultLocal, holder.getRequest(i), resultExtras, |
| 2093 | partialResults, holder.getSessionId()); |
| 2094 | |
| 2095 | holder.getCallback().onCaptureCompleted( |
| 2096 | CameraDeviceImpl.this, |
| 2097 | holder.getRequest(i), |
| 2098 | resultInBatch); |
| 2099 | } |
| 2100 | } else { |
| 2101 | holder.getCallback().onCaptureCompleted( |
| 2102 | CameraDeviceImpl.this, |
| 2103 | request, |
| 2104 | resultAsCapture); |
| 2105 | } |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | }; |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 2109 | finalResult = resultAsCapture; |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2110 | } |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 2111 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2112 | holder.getHandler().post(resultDispatch); |
| 2113 | |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 2114 | // Collect the partials for a total result; or mark the frame as totally completed |
Chien-Yu Chen | 7a316f6b | 2015-04-13 14:31:45 -0700 | [diff] [blame] | 2115 | mFrameNumberTracker.updateTracker(frameNumber, finalResult, isPartialResult, |
| 2116 | isReprocess); |
Igor Murashkin | 1e854c5 | 2014-08-28 15:21:49 -0700 | [diff] [blame] | 2117 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2118 | // Fire onCaptureSequenceCompleted |
Zhijun He | 8315915 | 2014-07-16 11:32:59 -0700 | [diff] [blame] | 2119 | if (!isPartialResult) { |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2120 | checkAndFireSequenceComplete(); |
| 2121 | } |
Jianing Wei | d2c3a82 | 2014-03-27 18:27:43 -0700 | [diff] [blame] | 2122 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Eino-Ville Talvala | ad916f7 | 2015-04-11 12:09:11 -0700 | [diff] [blame] | 2125 | @Override |
| 2126 | public void onPrepared(int streamId) { |
| 2127 | final OutputConfiguration output; |
| 2128 | final StateCallbackKK sessionCallback; |
| 2129 | |
| 2130 | if (DEBUG) { |
| 2131 | Log.v(TAG, "Stream " + streamId + " is prepared"); |
| 2132 | } |
| 2133 | |
| 2134 | synchronized(mInterfaceLock) { |
| 2135 | output = mConfiguredOutputs.get(streamId); |
| 2136 | sessionCallback = mSessionStateCallback; |
| 2137 | } |
| 2138 | |
| 2139 | if (sessionCallback == null) return; |
| 2140 | |
| 2141 | if (output == null) { |
| 2142 | Log.w(TAG, "onPrepared invoked for unknown output Surface"); |
| 2143 | return; |
| 2144 | } |
Shuzhen Wang | e0a6667 | 2017-03-21 12:05:14 -0700 | [diff] [blame] | 2145 | final List<Surface> surfaces = output.getSurfaces(); |
| 2146 | for (Surface surface : surfaces) { |
| 2147 | sessionCallback.onSurfacePrepared(surface); |
| 2148 | } |
Eino-Ville Talvala | ad916f7 | 2015-04-11 12:09:11 -0700 | [diff] [blame] | 2149 | } |
| 2150 | |
Shuzhen Wang | 88f1af2 | 2016-09-30 10:29:28 -0700 | [diff] [blame] | 2151 | @Override |
| 2152 | public void onRequestQueueEmpty() { |
| 2153 | final StateCallbackKK sessionCallback; |
| 2154 | |
| 2155 | if (DEBUG) { |
| 2156 | Log.v(TAG, "Request queue becomes empty"); |
| 2157 | } |
| 2158 | |
| 2159 | synchronized(mInterfaceLock) { |
| 2160 | sessionCallback = mSessionStateCallback; |
| 2161 | } |
| 2162 | |
| 2163 | if (sessionCallback == null) return; |
| 2164 | |
| 2165 | sessionCallback.onRequestQueueEmpty(); |
| 2166 | } |
| 2167 | |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 2168 | /** |
| 2169 | * Called by onDeviceError for handling single-capture failures. |
| 2170 | */ |
| 2171 | private void onCaptureErrorLocked(int errorCode, CaptureResultExtras resultExtras) { |
| 2172 | |
| 2173 | final int requestId = resultExtras.getRequestId(); |
| 2174 | final int subsequenceId = resultExtras.getSubsequenceId(); |
| 2175 | final long frameNumber = resultExtras.getFrameNumber(); |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 2176 | final CaptureCallbackHolder holder = |
| 2177 | CameraDeviceImpl.this.mCaptureCallbackMap.get(requestId); |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 2178 | |
| 2179 | final CaptureRequest request = holder.getRequest(subsequenceId); |
| 2180 | |
Eino-Ville Talvala | 3e0023a | 2016-03-06 18:40:01 -0800 | [diff] [blame] | 2181 | Runnable failureDispatch = null; |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 2182 | if (errorCode == ERROR_CAMERA_BUFFER) { |
Shuzhen Wang | 9c663d4 | 2016-10-28 23:25:02 -0700 | [diff] [blame] | 2183 | // Because 1 stream id could map to multiple surfaces, we need to specify both |
| 2184 | // streamId and surfaceId. |
| 2185 | List<Surface> surfaces = |
| 2186 | mConfiguredOutputs.get(resultExtras.getErrorStreamId()).getSurfaces(); |
| 2187 | for (Surface surface : surfaces) { |
| 2188 | if (!request.containsTarget(surface)) { |
| 2189 | continue; |
Eino-Ville Talvala | 3e0023a | 2016-03-06 18:40:01 -0800 | [diff] [blame] | 2190 | } |
Shuzhen Wang | 9c663d4 | 2016-10-28 23:25:02 -0700 | [diff] [blame] | 2191 | if (DEBUG) { |
| 2192 | Log.v(TAG, String.format("Lost output buffer reported for frame %d, target %s", |
| 2193 | frameNumber, surface)); |
| 2194 | } |
| 2195 | failureDispatch = new Runnable() { |
| 2196 | @Override |
| 2197 | public void run() { |
| 2198 | if (!CameraDeviceImpl.this.isClosed()){ |
| 2199 | holder.getCallback().onCaptureBufferLost( |
| 2200 | CameraDeviceImpl.this, |
| 2201 | request, |
| 2202 | surface, |
| 2203 | frameNumber); |
| 2204 | } |
| 2205 | } |
| 2206 | }; |
| 2207 | // Dispatch the failure callback |
| 2208 | holder.getHandler().post(failureDispatch); |
| 2209 | } |
Eino-Ville Talvala | 3e0023a | 2016-03-06 18:40:01 -0800 | [diff] [blame] | 2210 | } else { |
| 2211 | boolean mayHaveBuffers = (errorCode == ERROR_CAMERA_RESULT); |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 2212 | |
Eino-Ville Talvala | 3e0023a | 2016-03-06 18:40:01 -0800 | [diff] [blame] | 2213 | // This is only approximate - exact handling needs the camera service and HAL to |
| 2214 | // disambiguate between request failures to due abort and due to real errors. For |
| 2215 | // now, assume that if the session believes we're mid-abort, then the error is due |
| 2216 | // to abort. |
| 2217 | int reason = (mCurrentSession != null && mCurrentSession.isAborting()) ? |
| 2218 | CaptureFailure.REASON_FLUSHED : |
| 2219 | CaptureFailure.REASON_ERROR; |
| 2220 | |
| 2221 | final CaptureFailure failure = new CaptureFailure( |
| 2222 | request, |
| 2223 | reason, |
| 2224 | /*dropped*/ mayHaveBuffers, |
| 2225 | requestId, |
| 2226 | frameNumber); |
| 2227 | |
| 2228 | failureDispatch = new Runnable() { |
| 2229 | @Override |
| 2230 | public void run() { |
| 2231 | if (!CameraDeviceImpl.this.isClosed()){ |
| 2232 | holder.getCallback().onCaptureFailed( |
| 2233 | CameraDeviceImpl.this, |
| 2234 | request, |
| 2235 | failure); |
| 2236 | } |
| 2237 | } |
| 2238 | }; |
| 2239 | |
| 2240 | // Fire onCaptureSequenceCompleted if appropriate |
| 2241 | if (DEBUG) { |
| 2242 | Log.v(TAG, String.format("got error frame %d", frameNumber)); |
| 2243 | } |
| 2244 | mFrameNumberTracker.updateTracker(frameNumber, /*error*/true, request.isReprocess()); |
| 2245 | checkAndFireSequenceComplete(); |
Shuzhen Wang | 9c663d4 | 2016-10-28 23:25:02 -0700 | [diff] [blame] | 2246 | |
| 2247 | // Dispatch the failure callback |
| 2248 | holder.getHandler().post(failureDispatch); |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 2249 | } |
Eino-Ville Talvala | 3e0023a | 2016-03-06 18:40:01 -0800 | [diff] [blame] | 2250 | |
Eino-Ville Talvala | acc0095 | 2014-08-06 14:31:08 -0700 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | } // public class CameraDeviceCallbacks |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 2254 | |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 2255 | /** |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 2256 | * Default handler management. |
| 2257 | * |
| 2258 | * <p> |
| 2259 | * If handler is null, get the current thread's |
| 2260 | * Looper to create a Handler with. If no looper exists, throw {@code IllegalArgumentException}. |
| 2261 | * </p> |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 2262 | */ |
Igor Murashkin | 0b27d34 | 2014-05-30 09:45:05 -0700 | [diff] [blame] | 2263 | static Handler checkHandler(Handler handler) { |
Eino-Ville Talvala | e841d4e | 2013-09-05 09:04:08 -0700 | [diff] [blame] | 2264 | if (handler == null) { |
| 2265 | Looper looper = Looper.myLooper(); |
| 2266 | if (looper == null) { |
| 2267 | throw new IllegalArgumentException( |
| 2268 | "No handler given, and current thread has no looper!"); |
| 2269 | } |
| 2270 | handler = new Handler(looper); |
| 2271 | } |
| 2272 | return handler; |
| 2273 | } |
| 2274 | |
Eino-Ville Talvala | 7875a88 | 2014-07-31 12:47:07 -0700 | [diff] [blame] | 2275 | /** |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 2276 | * Default handler management, conditional on there being a callback. |
Eino-Ville Talvala | 7875a88 | 2014-07-31 12:47:07 -0700 | [diff] [blame] | 2277 | * |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 2278 | * <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] | 2279 | */ |
Eino-Ville Talvala | fd88743 | 2014-09-04 13:07:40 -0700 | [diff] [blame] | 2280 | static <T> Handler checkHandler(Handler handler, T callback) { |
| 2281 | if (callback != null) { |
Eino-Ville Talvala | 7875a88 | 2014-07-31 12:47:07 -0700 | [diff] [blame] | 2282 | return checkHandler(handler); |
| 2283 | } |
| 2284 | return handler; |
| 2285 | } |
| 2286 | |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 2287 | private void checkIfCameraClosedOrInError() throws CameraAccessException { |
Ruben Brunk | a45aa0d | 2015-06-03 19:40:22 -0700 | [diff] [blame] | 2288 | if (mRemoteDevice == null) { |
| 2289 | throw new IllegalStateException("CameraDevice was already closed"); |
| 2290 | } |
Eino-Ville Talvala | 7fcb3578 | 2014-06-03 18:30:27 -0700 | [diff] [blame] | 2291 | if (mInError) { |
| 2292 | throw new CameraAccessException(CameraAccessException.CAMERA_ERROR, |
| 2293 | "The camera device has encountered a serious error"); |
| 2294 | } |
Zhijun He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 2295 | } |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 2296 | |
Igor Murashkin | 49b2b13 | 2014-06-18 19:03:00 -0700 | [diff] [blame] | 2297 | /** 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] | 2298 | private boolean isClosed() { |
Ruben Brunk | 0953b64 | 2015-06-11 16:12:35 -0700 | [diff] [blame] | 2299 | return mClosing.get(); |
Eino-Ville Talvala | 868d904 | 2013-10-03 11:15:21 -0700 | [diff] [blame] | 2300 | } |
Ruben Brunk | 5749368 | 2014-05-27 18:58:08 -0700 | [diff] [blame] | 2301 | |
| 2302 | private CameraCharacteristics getCharacteristics() { |
| 2303 | return mCharacteristics; |
| 2304 | } |
Zhijun He | b1300e3 | 2015-05-28 12:51:52 -0700 | [diff] [blame] | 2305 | |
Shuzhen Wang | 7839293 | 2016-03-04 15:26:01 -0800 | [diff] [blame] | 2306 | /** |
| 2307 | * Listener for binder death. |
| 2308 | * |
| 2309 | * <p> Handle binder death for ICameraDeviceUser. Trigger onError.</p> |
| 2310 | */ |
Zhijun He | c8b181e | 2016-05-30 14:54:39 -0700 | [diff] [blame] | 2311 | @Override |
Shuzhen Wang | 7839293 | 2016-03-04 15:26:01 -0800 | [diff] [blame] | 2312 | public void binderDied() { |
| 2313 | Log.w(TAG, "CameraDevice " + mCameraId + " died unexpectedly"); |
| 2314 | |
| 2315 | if (mRemoteDevice == null) { |
| 2316 | return; // Camera already closed |
| 2317 | } |
| 2318 | |
| 2319 | mInError = true; |
| 2320 | Runnable r = new Runnable() { |
| 2321 | @Override |
| 2322 | public void run() { |
| 2323 | if (!isClosed()) { |
| 2324 | mDeviceCallback.onError(CameraDeviceImpl.this, |
Eino-Ville Talvala | 50eebe0 | 2016-06-09 17:07:13 -0700 | [diff] [blame] | 2325 | StateCallback.ERROR_CAMERA_SERVICE); |
Shuzhen Wang | 7839293 | 2016-03-04 15:26:01 -0800 | [diff] [blame] | 2326 | } |
| 2327 | } |
| 2328 | }; |
| 2329 | CameraDeviceImpl.this.mDeviceHandler.post(r); |
| 2330 | } |
Igor Murashkin | 7072550 | 2013-06-25 20:27:06 +0000 | [diff] [blame] | 2331 | } |