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