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