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