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