Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef _ACAMERA_DEVICE_H |
| 17 | #define _ACAMERA_DEVICE_H |
| 18 | |
| 19 | #include <memory> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <set> |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 22 | #include <atomic> |
Yin-Chia Yeh | e081c59 | 2016-03-29 18:26:44 -0700 | [diff] [blame] | 23 | #include <utility> |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 24 | #include <vector> |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 25 | #include <utils/StrongPointer.h> |
| 26 | #include <utils/Mutex.h> |
| 27 | #include <utils/String8.h> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 28 | #include <utils/List.h> |
| 29 | #include <utils/Vector.h> |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 30 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 31 | #include <android/hardware/camera2/BnCameraDeviceCallbacks.h> |
| 32 | #include <android/hardware/camera2/ICameraDeviceUser.h> |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 33 | #include <media/stagefright/foundation/ALooper.h> |
| 34 | #include <media/stagefright/foundation/AHandler.h> |
| 35 | #include <media/stagefright/foundation/AMessage.h> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 36 | #include <camera/CaptureResult.h> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 37 | #include <camera/camera2/OutputConfiguration.h> |
Shuzhen Wang | 24810e7 | 2019-03-18 10:55:01 -0700 | [diff] [blame] | 38 | #include <camera/camera2/SessionConfiguration.h> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 39 | #include <camera/camera2/CaptureRequest.h> |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 40 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 41 | #include <camera/NdkCameraManager.h> |
| 42 | #include <camera/NdkCameraCaptureSession.h> |
Jayant Chowdhary | a8bf1c6 | 2019-09-26 08:50:17 -0700 | [diff] [blame] | 43 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 44 | #include "ACameraMetadata.h" |
| 45 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 46 | namespace android { |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 47 | namespace acam { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 48 | |
Yin-Chia Yeh | d21c46b | 2017-10-10 11:59:46 -0700 | [diff] [blame] | 49 | // Wrap ACameraCaptureFailure so it can be ref-counted |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 50 | struct CameraCaptureFailure : public RefBase, public ACameraCaptureFailure {}; |
| 51 | |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 52 | // Wrap PhysicalCaptureResultInfo so that it can be ref-counted |
| 53 | struct ACameraPhysicalCaptureResultInfo: public RefBase { |
| 54 | ACameraPhysicalCaptureResultInfo(const std::vector<PhysicalCaptureResultInfo>& info, |
| 55 | int64_t frameNumber) : |
| 56 | mPhysicalResultInfo(info), mFrameNumber(frameNumber) {} |
| 57 | |
| 58 | std::vector<PhysicalCaptureResultInfo> mPhysicalResultInfo; |
| 59 | int64_t mFrameNumber; |
| 60 | }; |
| 61 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 62 | class CameraDevice final : public RefBase { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 63 | public: |
| 64 | CameraDevice(const char* id, ACameraDevice_StateCallbacks* cb, |
Yin-Chia Yeh | dd045bf | 2018-08-20 12:39:19 -0700 | [diff] [blame] | 65 | sp<ACameraMetadata> chars, |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 66 | ACameraDevice* wrapper); |
| 67 | ~CameraDevice(); |
| 68 | |
Tomasz Wasilczyk | 12b04a5 | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 69 | inline const char* getId() const { return mCameraId.c_str(); } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 70 | |
| 71 | camera_status_t createCaptureRequest( |
| 72 | ACameraDevice_request_template templateId, |
Shuzhen Wang | 6c17e21 | 2019-02-19 14:51:47 -0800 | [diff] [blame] | 73 | const ACameraIdList* physicalIdList, |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 74 | ACaptureRequest** request) const; |
| 75 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 76 | camera_status_t createCaptureSession( |
| 77 | const ACaptureSessionOutputContainer* outputs, |
Emilian Peev | 5fbe0ba | 2017-10-20 15:45:45 +0100 | [diff] [blame] | 78 | const ACaptureRequest* sessionParameters, |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 79 | const ACameraCaptureSession_stateCallbacks* callbacks, |
| 80 | /*out*/ACameraCaptureSession** session); |
| 81 | |
Shuzhen Wang | 24810e7 | 2019-03-18 10:55:01 -0700 | [diff] [blame] | 82 | camera_status_t isSessionConfigurationSupported( |
| 83 | const ACaptureSessionOutputContainer* sessionOutputContainer) const; |
| 84 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 85 | // Callbacks from camera service |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 86 | class ServiceCallback : public hardware::camera2::BnCameraDeviceCallbacks { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 87 | public: |
Chih-Hung Hsieh | d19d994 | 2016-08-29 14:21:14 -0700 | [diff] [blame] | 88 | explicit ServiceCallback(CameraDevice* device) : mDevice(device) {} |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 89 | binder::Status onDeviceError(int32_t errorCode, |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 90 | const CaptureResultExtras& resultExtras) override; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 91 | binder::Status onDeviceIdle() override; |
| 92 | binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras, |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 93 | int64_t timestamp) override; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 94 | binder::Status onResultReceived(const CameraMetadata& metadata, |
Shuzhen Wang | 5c22c15 | 2017-12-31 17:12:25 -0800 | [diff] [blame] | 95 | const CaptureResultExtras& resultExtras, |
| 96 | const std::vector<PhysicalCaptureResultInfo>& physicalResultInfos) override; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 97 | binder::Status onPrepared(int streamId) override; |
Shuzhen Wang | 9d06601 | 2016-09-30 11:30:20 -0700 | [diff] [blame] | 98 | binder::Status onRequestQueueEmpty() override; |
Yin-Chia Yeh | 8ca23dc | 2017-09-05 18:15:56 -0700 | [diff] [blame] | 99 | binder::Status onRepeatingRequestError(int64_t lastFrameNumber, |
| 100 | int32_t stoppedSequenceId) override; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 101 | private: |
| 102 | const wp<CameraDevice> mDevice; |
| 103 | }; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 104 | inline sp<hardware::camera2::ICameraDeviceCallbacks> getServiceCallback() { |
| 105 | return mServiceCallback; |
| 106 | }; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 107 | |
| 108 | // Camera device is only functional after remote being set |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 109 | void setRemoteDevice(sp<hardware::camera2::ICameraDeviceUser> remote); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 110 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 111 | inline ACameraDevice* getWrapper() const { return mWrapper; }; |
| 112 | |
Jayant Chowdhary | a8488c9 | 2019-06-21 12:45:34 -0700 | [diff] [blame] | 113 | // Stop the looper thread and unregister the handler |
Jayant Chowdhary | a8bf1c6 | 2019-09-26 08:50:17 -0700 | [diff] [blame] | 114 | void stopLooperAndDisconnect(); |
Jayant Chowdhary | a8488c9 | 2019-06-21 12:45:34 -0700 | [diff] [blame] | 115 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 116 | private: |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 117 | friend ACameraCaptureSession; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 118 | camera_status_t checkCameraClosedOrErrorLocked() const; |
| 119 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 120 | // device goes into fatal error state after this |
| 121 | void setCameraDeviceErrorLocked(camera_status_t error); |
| 122 | |
Yin-Chia Yeh | 6e2353b | 2017-10-24 16:35:20 -0700 | [diff] [blame] | 123 | void disconnectLocked(sp<ACameraCaptureSession>& session); // disconnect from camera service |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 124 | |
| 125 | camera_status_t stopRepeatingLocked(); |
| 126 | |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame] | 127 | camera_status_t flushLocked(ACameraCaptureSession*); |
| 128 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 129 | camera_status_t waitUntilIdleLocked(); |
| 130 | |
| 131 | |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 132 | template<class T> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 133 | camera_status_t captureLocked(sp<ACameraCaptureSession> session, |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 134 | /*optional*/T* cbs, |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 135 | int numRequests, ACaptureRequest** requests, |
| 136 | /*optional*/int* captureSequenceId); |
| 137 | |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 138 | template<class T> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 139 | camera_status_t setRepeatingRequestsLocked(sp<ACameraCaptureSession> session, |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 140 | /*optional*/T* cbs, |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 141 | int numRequests, ACaptureRequest** requests, |
| 142 | /*optional*/int* captureSequenceId); |
| 143 | |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 144 | template<class T> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 145 | camera_status_t submitRequestsLocked( |
| 146 | sp<ACameraCaptureSession> session, |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 147 | /*optional*/T* cbs, |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 148 | int numRequests, ACaptureRequest** requests, |
| 149 | /*out*/int* captureSequenceId, |
| 150 | bool isRepeating); |
| 151 | |
Yin-Chia Yeh | 4dfa4cc | 2017-11-10 20:00:09 -0800 | [diff] [blame] | 152 | camera_status_t updateOutputConfigurationLocked(ACaptureSessionOutput *output); |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 153 | |
Avichal Rakesh | 8effe98 | 2023-11-13 18:53:40 -0800 | [diff] [blame^] | 154 | camera_status_t prepareLocked(ANativeWindow *window); |
Jayant Chowdhary | 09b368b | 2023-02-13 06:53:05 +0000 | [diff] [blame] | 155 | |
Yin-Chia Yeh | 4dfa4cc | 2017-11-10 20:00:09 -0800 | [diff] [blame] | 156 | camera_status_t allocateCaptureRequest( |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 157 | const ACaptureRequest* request, sp<CaptureRequest>& outReq); |
| 158 | |
Shuzhen Wang | 6c17e21 | 2019-02-19 14:51:47 -0800 | [diff] [blame] | 159 | static ACaptureRequest* allocateACaptureRequest(sp<CaptureRequest>& req, |
| 160 | const std::string& deviceId); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 161 | static void freeACaptureRequest(ACaptureRequest*); |
| 162 | |
| 163 | // only For session to hold device lock |
| 164 | // Always grab device lock before grabbing session lock |
| 165 | void lockDeviceForSessionOps() const { mDeviceLock.lock(); }; |
| 166 | void unlockDevice() const { mDeviceLock.unlock(); }; |
| 167 | |
| 168 | // For capture session to notify its end of life |
| 169 | void notifySessionEndOfLifeLocked(ACameraCaptureSession* session); |
| 170 | |
Emilian Peev | 5fbe0ba | 2017-10-20 15:45:45 +0100 | [diff] [blame] | 171 | camera_status_t configureStreamsLocked(const ACaptureSessionOutputContainer* outputs, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 172 | const ACaptureRequest* sessionParameters, nsecs_t startTimeNs); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 173 | |
Yin-Chia Yeh | 6e2353b | 2017-10-24 16:35:20 -0700 | [diff] [blame] | 174 | // Input message will be posted and cleared after this returns |
| 175 | void postSessionMsgAndCleanup(sp<AMessage>& msg); |
| 176 | |
Yin-Chia Yeh | e081c59 | 2016-03-29 18:26:44 -0700 | [diff] [blame] | 177 | static camera_status_t getIGBPfromAnw( |
| 178 | ANativeWindow* anw, sp<IGraphicBufferProducer>& out); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 179 | |
| 180 | static camera_status_t getSurfaceFromANativeWindow( |
| 181 | ANativeWindow* anw, sp<Surface>& out); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 182 | |
| 183 | mutable Mutex mDeviceLock; |
| 184 | const String8 mCameraId; // Camera ID |
| 185 | const ACameraDevice_StateCallbacks mAppCallbacks; // Callback to app |
Yin-Chia Yeh | dd045bf | 2018-08-20 12:39:19 -0700 | [diff] [blame] | 186 | const sp<ACameraMetadata> mChars; // Camera characteristics |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 187 | const sp<ServiceCallback> mServiceCallback; |
| 188 | ACameraDevice* mWrapper; |
| 189 | |
Yin-Chia Yeh | e081c59 | 2016-03-29 18:26:44 -0700 | [diff] [blame] | 190 | // stream id -> pair of (ANW* from application, OutputConfiguration used for camera service) |
| 191 | std::map<int, std::pair<ANativeWindow*, OutputConfiguration>> mConfiguredOutputs; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 192 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 193 | // TODO: maybe a bool will suffice for synchronous implementation? |
| 194 | std::atomic_bool mClosing; |
| 195 | inline bool isClosed() { return mClosing; } |
| 196 | |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame] | 197 | bool mInError = false; |
| 198 | camera_status_t mError = ACAMERA_OK; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 199 | void onCaptureErrorLocked( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 200 | int32_t errorCode, |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 201 | const CaptureResultExtras& resultExtras); |
| 202 | |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame] | 203 | bool mIdle = true; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 204 | // This will avoid a busy session being deleted before it's back to idle state |
| 205 | sp<ACameraCaptureSession> mBusySession; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 206 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 207 | sp<hardware::camera2::ICameraDeviceUser> mRemote; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 208 | |
| 209 | // Looper thread to handle callback to app |
| 210 | sp<ALooper> mCbLooper; |
| 211 | // definition of handler and message |
| 212 | enum { |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 213 | // Device state callbacks |
Yin-Chia Yeh | e081c59 | 2016-03-29 18:26:44 -0700 | [diff] [blame] | 214 | kWhatOnDisconnected, // onDisconnected |
| 215 | kWhatOnError, // onError |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 216 | // Session state callbacks |
Yin-Chia Yeh | e081c59 | 2016-03-29 18:26:44 -0700 | [diff] [blame] | 217 | kWhatSessionStateCb, // onReady, onActive |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 218 | // Capture callbacks |
Yin-Chia Yeh | e081c59 | 2016-03-29 18:26:44 -0700 | [diff] [blame] | 219 | kWhatCaptureStart, // onCaptureStarted |
Jayant Chowdhary | 04ba13f | 2022-01-14 00:21:19 +0000 | [diff] [blame] | 220 | kWhatCaptureStart2, // onCaptureStarted |
Yin-Chia Yeh | e081c59 | 2016-03-29 18:26:44 -0700 | [diff] [blame] | 221 | kWhatCaptureResult, // onCaptureProgressed, onCaptureCompleted |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 222 | kWhatLogicalCaptureResult, // onLogicalCameraCaptureCompleted |
Yin-Chia Yeh | e081c59 | 2016-03-29 18:26:44 -0700 | [diff] [blame] | 223 | kWhatCaptureFail, // onCaptureFailed |
Emilian Peev | edec62d | 2019-03-19 17:59:24 -0700 | [diff] [blame] | 224 | kWhatLogicalCaptureFail, // onLogicalCameraCaptureFailed |
Yin-Chia Yeh | e081c59 | 2016-03-29 18:26:44 -0700 | [diff] [blame] | 225 | kWhatCaptureSeqEnd, // onCaptureSequenceCompleted |
| 226 | kWhatCaptureSeqAbort, // onCaptureSequenceAborted |
Jayant Chowdhary | 09b368b | 2023-02-13 06:53:05 +0000 | [diff] [blame] | 227 | kWhatCaptureBufferLost, // onCaptureBufferLost |
| 228 | kWhatPreparedCb, // onWindowPrepared |
Yin-Chia Yeh | 6e2353b | 2017-10-24 16:35:20 -0700 | [diff] [blame] | 229 | // Internal cleanup |
| 230 | kWhatCleanUpSessions // Cleanup cached sp<ACameraCaptureSession> |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 231 | }; |
| 232 | static const char* kContextKey; |
| 233 | static const char* kDeviceKey; |
| 234 | static const char* kErrorCodeKey; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 235 | static const char* kCallbackFpKey; |
| 236 | static const char* kSessionSpKey; |
| 237 | static const char* kCaptureRequestKey; |
| 238 | static const char* kTimeStampKey; |
| 239 | static const char* kCaptureResultKey; |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 240 | static const char* kPhysicalCaptureResultKey; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 241 | static const char* kCaptureFailureKey; |
| 242 | static const char* kSequenceIdKey; |
| 243 | static const char* kFrameNumberKey; |
Yin-Chia Yeh | e081c59 | 2016-03-29 18:26:44 -0700 | [diff] [blame] | 244 | static const char* kAnwKey; |
Emilian Peev | edec62d | 2019-03-19 17:59:24 -0700 | [diff] [blame] | 245 | static const char* kFailingPhysicalCameraId; |
Yin-Chia Yeh | 6e2353b | 2017-10-24 16:35:20 -0700 | [diff] [blame] | 246 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 247 | class CallbackHandler : public AHandler { |
| 248 | public: |
Shuzhen Wang | 6c17e21 | 2019-02-19 14:51:47 -0800 | [diff] [blame] | 249 | explicit CallbackHandler(const char* id); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 250 | void onMessageReceived(const sp<AMessage> &msg) override; |
Yin-Chia Yeh | 6e2353b | 2017-10-24 16:35:20 -0700 | [diff] [blame] | 251 | |
| 252 | private: |
Shuzhen Wang | 6c17e21 | 2019-02-19 14:51:47 -0800 | [diff] [blame] | 253 | std::string mId; |
Yin-Chia Yeh | 6e2353b | 2017-10-24 16:35:20 -0700 | [diff] [blame] | 254 | // This handler will cache all capture session sp until kWhatCleanUpSessions |
| 255 | // is processed. This is used to guarantee the last session reference is always |
| 256 | // being removed in callback thread without holding camera device lock |
| 257 | Vector<sp<ACameraCaptureSession>> mCachedSessions; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 258 | }; |
| 259 | sp<CallbackHandler> mHandler; |
| 260 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 261 | /*********************************** |
| 262 | * Capture session related members * |
| 263 | ***********************************/ |
| 264 | // The current active session |
Yin-Chia Yeh | 6e2353b | 2017-10-24 16:35:20 -0700 | [diff] [blame] | 265 | wp<ACameraCaptureSession> mCurrentSession; |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame] | 266 | bool mFlushing = false; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 267 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 268 | int mNextSessionId = 0; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 269 | // TODO: might need another looper/handler to handle callbacks from service |
| 270 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 271 | static const int REQUEST_ID_NONE = -1; |
| 272 | int mRepeatingSequenceId = REQUEST_ID_NONE; |
| 273 | |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 274 | // sequence id -> last frame number holder map |
| 275 | struct RequestLastFrameNumberHolder { |
| 276 | int64_t lastFrameNumber; |
| 277 | // Whether the current sequence is completed (capture results are |
| 278 | // generated). May be set to true, but |
| 279 | // not removed from the map if not all inflight requests in the sequence |
| 280 | // have been completed. |
| 281 | bool isSequenceCompleted = false; |
| 282 | // Whether all inflight requests in the sequence are completed |
| 283 | // (capture results and buffers are generated). May be |
| 284 | // set to true, but not removed from the map yet if the capture results |
| 285 | // haven't been delivered to the app yet. |
| 286 | bool isInflightCompleted = false; |
| 287 | RequestLastFrameNumberHolder(int64_t lastFN) : |
| 288 | lastFrameNumber(lastFN) {} |
| 289 | }; |
| 290 | std::map<int, RequestLastFrameNumberHolder> mSequenceLastFrameNumberMap; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 291 | |
| 292 | struct CallbackHolder { |
| 293 | CallbackHolder(sp<ACameraCaptureSession> session, |
| 294 | const Vector<sp<CaptureRequest> >& requests, |
| 295 | bool isRepeating, |
| 296 | ACameraCaptureSession_captureCallbacks* cbs); |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 297 | CallbackHolder(sp<ACameraCaptureSession> session, |
| 298 | const Vector<sp<CaptureRequest> >& requests, |
| 299 | bool isRepeating, |
| 300 | ACameraCaptureSession_logicalCamera_captureCallbacks* lcbs); |
Jayant Chowdhary | 04ba13f | 2022-01-14 00:21:19 +0000 | [diff] [blame] | 301 | CallbackHolder(sp<ACameraCaptureSession> session, |
| 302 | const Vector<sp<CaptureRequest> >& requests, |
| 303 | bool isRepeating, |
| 304 | ACameraCaptureSession_captureCallbacksV2* cbs); |
| 305 | CallbackHolder(sp<ACameraCaptureSession> session, |
| 306 | const Vector<sp<CaptureRequest> >& requests, |
| 307 | bool isRepeating, |
| 308 | ACameraCaptureSession_logicalCamera_captureCallbacksV2* lcbs); |
| 309 | void clearCallbacks() { |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 310 | mContext = nullptr; |
| 311 | mOnCaptureStarted = nullptr; |
Jayant Chowdhary | 04ba13f | 2022-01-14 00:21:19 +0000 | [diff] [blame] | 312 | mOnCaptureStarted2 = nullptr; |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 313 | mOnCaptureProgressed = nullptr; |
| 314 | mOnCaptureCompleted = nullptr; |
| 315 | mOnLogicalCameraCaptureCompleted = nullptr; |
Emilian Peev | edec62d | 2019-03-19 17:59:24 -0700 | [diff] [blame] | 316 | mOnLogicalCameraCaptureFailed = nullptr; |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 317 | mOnCaptureFailed = nullptr; |
| 318 | mOnCaptureSequenceCompleted = nullptr; |
| 319 | mOnCaptureSequenceAborted = nullptr; |
| 320 | mOnCaptureBufferLost = nullptr; |
Jayant Chowdhary | 04ba13f | 2022-01-14 00:21:19 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | template <class T> |
| 324 | void initCaptureCallbacksV2(T* cbs) { |
| 325 | clearCallbacks(); |
| 326 | if (cbs != nullptr) { |
| 327 | mContext = cbs->context; |
| 328 | mOnCaptureStarted2 = cbs->onCaptureStarted; |
| 329 | mOnCaptureProgressed = cbs->onCaptureProgressed; |
| 330 | mOnCaptureSequenceCompleted = cbs->onCaptureSequenceCompleted; |
| 331 | mOnCaptureSequenceAborted = cbs->onCaptureSequenceAborted; |
| 332 | mOnCaptureBufferLost = cbs->onCaptureBufferLost; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | template <class T> |
| 337 | void initCaptureCallbacks(T* cbs) { |
| 338 | clearCallbacks(); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 339 | if (cbs != nullptr) { |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 340 | mContext = cbs->context; |
| 341 | mOnCaptureStarted = cbs->onCaptureStarted; |
| 342 | mOnCaptureProgressed = cbs->onCaptureProgressed; |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 343 | mOnCaptureSequenceCompleted = cbs->onCaptureSequenceCompleted; |
| 344 | mOnCaptureSequenceAborted = cbs->onCaptureSequenceAborted; |
| 345 | mOnCaptureBufferLost = cbs->onCaptureBufferLost; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 346 | } |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 347 | } |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 348 | sp<ACameraCaptureSession> mSession; |
| 349 | Vector<sp<CaptureRequest> > mRequests; |
| 350 | const bool mIsRepeating; |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 351 | const bool mIsLogicalCameraCallback; |
Jayant Chowdhary | 04ba13f | 2022-01-14 00:21:19 +0000 | [diff] [blame] | 352 | const bool mIs2Callback; |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 353 | |
| 354 | void* mContext; |
| 355 | ACameraCaptureSession_captureCallback_start mOnCaptureStarted; |
Jayant Chowdhary | 04ba13f | 2022-01-14 00:21:19 +0000 | [diff] [blame] | 356 | ACameraCaptureSession_captureCallback_startV2 mOnCaptureStarted2; |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 357 | ACameraCaptureSession_captureCallback_result mOnCaptureProgressed; |
| 358 | ACameraCaptureSession_captureCallback_result mOnCaptureCompleted; |
| 359 | ACameraCaptureSession_logicalCamera_captureCallback_result mOnLogicalCameraCaptureCompleted; |
Emilian Peev | edec62d | 2019-03-19 17:59:24 -0700 | [diff] [blame] | 360 | ACameraCaptureSession_logicalCamera_captureCallback_failed mOnLogicalCameraCaptureFailed; |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 361 | ACameraCaptureSession_captureCallback_failed mOnCaptureFailed; |
| 362 | ACameraCaptureSession_captureCallback_sequenceEnd mOnCaptureSequenceCompleted; |
| 363 | ACameraCaptureSession_captureCallback_sequenceAbort mOnCaptureSequenceAborted; |
| 364 | ACameraCaptureSession_captureCallback_bufferLost mOnCaptureBufferLost; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 365 | }; |
| 366 | // sequence id -> callbacks map |
| 367 | std::map<int, CallbackHolder> mSequenceCallbackMap; |
| 368 | |
| 369 | static const int64_t NO_FRAMES_CAPTURED = -1; |
| 370 | class FrameNumberTracker { |
| 371 | public: |
| 372 | // TODO: Called in onResultReceived and onCaptureErrorLocked |
| 373 | void updateTracker(int64_t frameNumber, bool isError); |
| 374 | inline int64_t getCompletedFrameNumber() { return mCompletedFrameNumber; } |
| 375 | private: |
| 376 | void update(); |
| 377 | void updateCompletedFrameNumber(int64_t frameNumber); |
| 378 | |
| 379 | int64_t mCompletedFrameNumber = NO_FRAMES_CAPTURED; |
| 380 | List<int64_t> mSkippedFrameNumbers; |
| 381 | std::set<int64_t> mFutureErrorSet; |
| 382 | }; |
| 383 | FrameNumberTracker mFrameNumberTracker; |
| 384 | |
| 385 | void checkRepeatingSequenceCompleteLocked(const int sequenceId, const int64_t lastFrameNumber); |
| 386 | void checkAndFireSequenceCompleteLocked(); |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 387 | void removeCompletedCallbackHolderLocked(int64_t lastCompletedRegularFrameNumber); |
Shuzhen Wang | acae264 | 2020-12-21 17:11:37 -0800 | [diff] [blame] | 388 | void sendCaptureSequenceCompletedLocked(int sequenceId, int64_t lastFrameNumber); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 389 | |
| 390 | // Misc variables |
| 391 | int32_t mShadingMapSize[2]; // const after constructor |
| 392 | int32_t mPartialResultCount; // const after constructor |
Shuzhen Wang | 6c17e21 | 2019-02-19 14:51:47 -0800 | [diff] [blame] | 393 | std::vector<std::string> mPhysicalIds; // const after constructor |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 394 | |
| 395 | }; |
| 396 | |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 397 | } // namespace acam; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 398 | } // namespace android; |
| 399 | |
| 400 | /** |
| 401 | * ACameraDevice opaque struct definition |
| 402 | * Leave outside of android namespace because it's NDK struct |
| 403 | */ |
| 404 | struct ACameraDevice { |
| 405 | ACameraDevice(const char* id, ACameraDevice_StateCallbacks* cb, |
Yin-Chia Yeh | dd045bf | 2018-08-20 12:39:19 -0700 | [diff] [blame] | 406 | sp<ACameraMetadata> chars) : |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 407 | mDevice(new android::acam::CameraDevice(id, cb, chars, this)) {} |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 408 | |
Jayant Chowdhary | a8488c9 | 2019-06-21 12:45:34 -0700 | [diff] [blame] | 409 | ~ACameraDevice(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 410 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 411 | /******************* |
| 412 | * NDK public APIs * |
| 413 | *******************/ |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 414 | inline const char* getId() const { return mDevice->getId(); } |
| 415 | |
| 416 | camera_status_t createCaptureRequest( |
| 417 | ACameraDevice_request_template templateId, |
Shuzhen Wang | 6c17e21 | 2019-02-19 14:51:47 -0800 | [diff] [blame] | 418 | const ACameraIdList* physicalCameraIdList, |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 419 | ACaptureRequest** request) const { |
Shuzhen Wang | 6c17e21 | 2019-02-19 14:51:47 -0800 | [diff] [blame] | 420 | return mDevice->createCaptureRequest(templateId, physicalCameraIdList, request); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 423 | camera_status_t createCaptureSession( |
| 424 | const ACaptureSessionOutputContainer* outputs, |
Emilian Peev | 5fbe0ba | 2017-10-20 15:45:45 +0100 | [diff] [blame] | 425 | const ACaptureRequest* sessionParameters, |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 426 | const ACameraCaptureSession_stateCallbacks* callbacks, |
| 427 | /*out*/ACameraCaptureSession** session) { |
Emilian Peev | 5fbe0ba | 2017-10-20 15:45:45 +0100 | [diff] [blame] | 428 | return mDevice->createCaptureSession(outputs, sessionParameters, callbacks, session); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Shuzhen Wang | 24810e7 | 2019-03-18 10:55:01 -0700 | [diff] [blame] | 431 | camera_status_t isSessionConfigurationSupported( |
| 432 | const ACaptureSessionOutputContainer* sessionOutputContainer) const { |
| 433 | return mDevice->isSessionConfigurationSupported(sessionOutputContainer); |
| 434 | } |
| 435 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 436 | /*********************** |
| 437 | * Device interal APIs * |
| 438 | ***********************/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 439 | inline android::sp<android::hardware::camera2::ICameraDeviceCallbacks> getServiceCallback() { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 440 | return mDevice->getServiceCallback(); |
| 441 | }; |
| 442 | |
| 443 | // Camera device is only functional after remote being set |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 444 | inline void setRemoteDevice(android::sp<android::hardware::camera2::ICameraDeviceUser> remote) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 445 | mDevice->setRemoteDevice(remote); |
| 446 | } |
| 447 | |
| 448 | private: |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 449 | android::sp<android::acam::CameraDevice> mDevice; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 450 | }; |
| 451 | |
| 452 | #endif // _ACAMERA_DEVICE_H |