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