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> |
| 23 | #include <utils/StrongPointer.h> |
| 24 | #include <utils/Mutex.h> |
| 25 | #include <utils/String8.h> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 26 | #include <utils/List.h> |
| 27 | #include <utils/Vector.h> |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 28 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 29 | #include <android/hardware/camera2/BnCameraDeviceCallbacks.h> |
| 30 | #include <android/hardware/camera2/ICameraDeviceUser.h> |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 31 | #include <media/stagefright/foundation/ALooper.h> |
| 32 | #include <media/stagefright/foundation/AHandler.h> |
| 33 | #include <media/stagefright/foundation/AMessage.h> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 34 | #include <camera/CaptureResult.h> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 35 | #include <camera/camera2/OutputConfiguration.h> |
| 36 | #include <camera/camera2/CaptureRequest.h> |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 37 | |
| 38 | #include <NdkCameraDevice.h> |
| 39 | #include "ACameraMetadata.h" |
| 40 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 41 | namespace android { |
| 42 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 43 | // Wrap ACameraCaptureFailure so it can be ref-counter |
| 44 | struct CameraCaptureFailure : public RefBase, public ACameraCaptureFailure {}; |
| 45 | |
| 46 | class CameraDevice final : public RefBase { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 47 | public: |
| 48 | CameraDevice(const char* id, ACameraDevice_StateCallbacks* cb, |
| 49 | std::unique_ptr<ACameraMetadata> chars, |
| 50 | ACameraDevice* wrapper); |
| 51 | ~CameraDevice(); |
| 52 | |
| 53 | inline const char* getId() const { return mCameraId.string(); } |
| 54 | |
| 55 | camera_status_t createCaptureRequest( |
| 56 | ACameraDevice_request_template templateId, |
| 57 | ACaptureRequest** request) const; |
| 58 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 59 | camera_status_t createCaptureSession( |
| 60 | const ACaptureSessionOutputContainer* outputs, |
| 61 | const ACameraCaptureSession_stateCallbacks* callbacks, |
| 62 | /*out*/ACameraCaptureSession** session); |
| 63 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 64 | // Callbacks from camera service |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 65 | class ServiceCallback : public hardware::camera2::BnCameraDeviceCallbacks { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 66 | public: |
| 67 | ServiceCallback(CameraDevice* device) : mDevice(device) {} |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 68 | binder::Status onDeviceError(int32_t errorCode, |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 69 | const CaptureResultExtras& resultExtras) override; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 70 | binder::Status onDeviceIdle() override; |
| 71 | binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras, |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 72 | int64_t timestamp) override; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 73 | binder::Status onResultReceived(const CameraMetadata& metadata, |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 74 | const CaptureResultExtras& resultExtras) override; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 75 | binder::Status onPrepared(int streamId) override; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 76 | private: |
| 77 | const wp<CameraDevice> mDevice; |
| 78 | }; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 79 | inline sp<hardware::camera2::ICameraDeviceCallbacks> getServiceCallback() { |
| 80 | return mServiceCallback; |
| 81 | }; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 82 | |
| 83 | // Camera device is only functional after remote being set |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 84 | void setRemoteDevice(sp<hardware::camera2::ICameraDeviceUser> remote); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 85 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 86 | inline ACameraDevice* getWrapper() const { return mWrapper; }; |
| 87 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 88 | private: |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 89 | friend ACameraCaptureSession; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 90 | camera_status_t checkCameraClosedOrErrorLocked() const; |
| 91 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 92 | // device goes into fatal error state after this |
| 93 | void setCameraDeviceErrorLocked(camera_status_t error); |
| 94 | |
| 95 | void disconnectLocked(); // disconnect from camera service |
| 96 | |
| 97 | camera_status_t stopRepeatingLocked(); |
| 98 | |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame^] | 99 | camera_status_t flushLocked(ACameraCaptureSession*); |
| 100 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 101 | camera_status_t waitUntilIdleLocked(); |
| 102 | |
| 103 | |
| 104 | camera_status_t captureLocked(sp<ACameraCaptureSession> session, |
| 105 | /*optional*/ACameraCaptureSession_captureCallbacks* cbs, |
| 106 | int numRequests, ACaptureRequest** requests, |
| 107 | /*optional*/int* captureSequenceId); |
| 108 | |
| 109 | camera_status_t setRepeatingRequestsLocked(sp<ACameraCaptureSession> session, |
| 110 | /*optional*/ACameraCaptureSession_captureCallbacks* cbs, |
| 111 | int numRequests, ACaptureRequest** requests, |
| 112 | /*optional*/int* captureSequenceId); |
| 113 | |
| 114 | camera_status_t submitRequestsLocked( |
| 115 | sp<ACameraCaptureSession> session, |
| 116 | /*optional*/ACameraCaptureSession_captureCallbacks* cbs, |
| 117 | int numRequests, ACaptureRequest** requests, |
| 118 | /*out*/int* captureSequenceId, |
| 119 | bool isRepeating); |
| 120 | |
| 121 | static camera_status_t allocateCaptureRequest( |
| 122 | const ACaptureRequest* request, sp<CaptureRequest>& outReq); |
| 123 | |
| 124 | static ACaptureRequest* allocateACaptureRequest(sp<CaptureRequest>& req); |
| 125 | static void freeACaptureRequest(ACaptureRequest*); |
| 126 | |
| 127 | // only For session to hold device lock |
| 128 | // Always grab device lock before grabbing session lock |
| 129 | void lockDeviceForSessionOps() const { mDeviceLock.lock(); }; |
| 130 | void unlockDevice() const { mDeviceLock.unlock(); }; |
| 131 | |
| 132 | // For capture session to notify its end of life |
| 133 | void notifySessionEndOfLifeLocked(ACameraCaptureSession* session); |
| 134 | |
| 135 | camera_status_t configureStreamsLocked(const ACaptureSessionOutputContainer* outputs); |
| 136 | |
| 137 | static camera_status_t getIGBPfromSessionOutput( |
| 138 | const ACaptureSessionOutput& config, sp<IGraphicBufferProducer>& out); |
| 139 | |
| 140 | static camera_status_t getSurfaceFromANativeWindow( |
| 141 | ANativeWindow* anw, sp<Surface>& out); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 142 | |
| 143 | mutable Mutex mDeviceLock; |
| 144 | const String8 mCameraId; // Camera ID |
| 145 | const ACameraDevice_StateCallbacks mAppCallbacks; // Callback to app |
| 146 | const std::unique_ptr<ACameraMetadata> mChars; // Camera characteristics |
| 147 | const sp<ServiceCallback> mServiceCallback; |
| 148 | ACameraDevice* mWrapper; |
| 149 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 150 | // stream id -> OutputConfiguration map |
| 151 | std::map<int, OutputConfiguration> mConfiguredOutputs; |
| 152 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 153 | // TODO: maybe a bool will suffice for synchronous implementation? |
| 154 | std::atomic_bool mClosing; |
| 155 | inline bool isClosed() { return mClosing; } |
| 156 | |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame^] | 157 | bool mInError = false; |
| 158 | camera_status_t mError = ACAMERA_OK; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 159 | void onCaptureErrorLocked( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 160 | int32_t errorCode, |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 161 | const CaptureResultExtras& resultExtras); |
| 162 | |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame^] | 163 | bool mIdle = true; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 164 | // This will avoid a busy session being deleted before it's back to idle state |
| 165 | sp<ACameraCaptureSession> mBusySession; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 166 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 167 | sp<hardware::camera2::ICameraDeviceUser> mRemote; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 168 | |
| 169 | // Looper thread to handle callback to app |
| 170 | sp<ALooper> mCbLooper; |
| 171 | // definition of handler and message |
| 172 | enum { |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 173 | // Device state callbacks |
| 174 | kWhatOnDisconnected, // onDisconnected |
| 175 | kWhatOnError, // onError |
| 176 | // Session state callbacks |
| 177 | kWhatSessionStateCb, // onReady, onActive |
| 178 | // Capture callbacks |
| 179 | kWhatCaptureStart, // onCaptureStarted |
| 180 | kWhatCaptureResult, // onCaptureProgressed, onCaptureCompleted |
| 181 | kWhatCaptureFail, // onCaptureFailed |
| 182 | kWhatCaptureSeqEnd, // onCaptureSequenceCompleted |
| 183 | kWhatCaptureSeqAbort // onCaptureSequenceAborted |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 184 | }; |
| 185 | static const char* kContextKey; |
| 186 | static const char* kDeviceKey; |
| 187 | static const char* kErrorCodeKey; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 188 | static const char* kCallbackFpKey; |
| 189 | static const char* kSessionSpKey; |
| 190 | static const char* kCaptureRequestKey; |
| 191 | static const char* kTimeStampKey; |
| 192 | static const char* kCaptureResultKey; |
| 193 | static const char* kCaptureFailureKey; |
| 194 | static const char* kSequenceIdKey; |
| 195 | static const char* kFrameNumberKey; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 196 | class CallbackHandler : public AHandler { |
| 197 | public: |
| 198 | CallbackHandler() {} |
| 199 | void onMessageReceived(const sp<AMessage> &msg) override; |
| 200 | }; |
| 201 | sp<CallbackHandler> mHandler; |
| 202 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 203 | /*********************************** |
| 204 | * Capture session related members * |
| 205 | ***********************************/ |
| 206 | // The current active session |
| 207 | ACameraCaptureSession* mCurrentSession = nullptr; |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame^] | 208 | bool mFlushing = false; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 209 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 210 | int mNextSessionId = 0; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 211 | // TODO: might need another looper/handler to handle callbacks from service |
| 212 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 213 | static const int REQUEST_ID_NONE = -1; |
| 214 | int mRepeatingSequenceId = REQUEST_ID_NONE; |
| 215 | |
| 216 | // sequence id -> last frame number map |
| 217 | std::map<int, int64_t> mSequenceLastFrameNumberMap; |
| 218 | |
| 219 | struct CallbackHolder { |
| 220 | CallbackHolder(sp<ACameraCaptureSession> session, |
| 221 | const Vector<sp<CaptureRequest> >& requests, |
| 222 | bool isRepeating, |
| 223 | ACameraCaptureSession_captureCallbacks* cbs); |
| 224 | |
| 225 | static ACameraCaptureSession_captureCallbacks fillCb( |
| 226 | ACameraCaptureSession_captureCallbacks* cbs) { |
| 227 | if (cbs != nullptr) { |
| 228 | return *cbs; |
| 229 | } |
| 230 | return { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr }; |
| 231 | } |
| 232 | |
| 233 | sp<ACameraCaptureSession> mSession; |
| 234 | Vector<sp<CaptureRequest> > mRequests; |
| 235 | const bool mIsRepeating; |
| 236 | ACameraCaptureSession_captureCallbacks mCallbacks; |
| 237 | }; |
| 238 | // sequence id -> callbacks map |
| 239 | std::map<int, CallbackHolder> mSequenceCallbackMap; |
| 240 | |
| 241 | static const int64_t NO_FRAMES_CAPTURED = -1; |
| 242 | class FrameNumberTracker { |
| 243 | public: |
| 244 | // TODO: Called in onResultReceived and onCaptureErrorLocked |
| 245 | void updateTracker(int64_t frameNumber, bool isError); |
| 246 | inline int64_t getCompletedFrameNumber() { return mCompletedFrameNumber; } |
| 247 | private: |
| 248 | void update(); |
| 249 | void updateCompletedFrameNumber(int64_t frameNumber); |
| 250 | |
| 251 | int64_t mCompletedFrameNumber = NO_FRAMES_CAPTURED; |
| 252 | List<int64_t> mSkippedFrameNumbers; |
| 253 | std::set<int64_t> mFutureErrorSet; |
| 254 | }; |
| 255 | FrameNumberTracker mFrameNumberTracker; |
| 256 | |
| 257 | void checkRepeatingSequenceCompleteLocked(const int sequenceId, const int64_t lastFrameNumber); |
| 258 | void checkAndFireSequenceCompleteLocked(); |
| 259 | |
| 260 | // Misc variables |
| 261 | int32_t mShadingMapSize[2]; // const after constructor |
| 262 | int32_t mPartialResultCount; // const after constructor |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 263 | |
| 264 | }; |
| 265 | |
| 266 | } // namespace android; |
| 267 | |
| 268 | /** |
| 269 | * ACameraDevice opaque struct definition |
| 270 | * Leave outside of android namespace because it's NDK struct |
| 271 | */ |
| 272 | struct ACameraDevice { |
| 273 | ACameraDevice(const char* id, ACameraDevice_StateCallbacks* cb, |
| 274 | std::unique_ptr<ACameraMetadata> chars) : |
| 275 | mDevice(new CameraDevice(id, cb, std::move(chars), this)) {} |
| 276 | |
| 277 | ~ACameraDevice() {}; |
| 278 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 279 | /******************* |
| 280 | * NDK public APIs * |
| 281 | *******************/ |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 282 | inline const char* getId() const { return mDevice->getId(); } |
| 283 | |
| 284 | camera_status_t createCaptureRequest( |
| 285 | ACameraDevice_request_template templateId, |
| 286 | ACaptureRequest** request) const { |
| 287 | return mDevice->createCaptureRequest(templateId, request); |
| 288 | } |
| 289 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 290 | camera_status_t createCaptureSession( |
| 291 | const ACaptureSessionOutputContainer* outputs, |
| 292 | const ACameraCaptureSession_stateCallbacks* callbacks, |
| 293 | /*out*/ACameraCaptureSession** session) { |
| 294 | return mDevice->createCaptureSession(outputs, callbacks, session); |
| 295 | } |
| 296 | |
| 297 | /*********************** |
| 298 | * Device interal APIs * |
| 299 | ***********************/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 300 | inline android::sp<android::hardware::camera2::ICameraDeviceCallbacks> getServiceCallback() { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 301 | return mDevice->getServiceCallback(); |
| 302 | }; |
| 303 | |
| 304 | // Camera device is only functional after remote being set |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 305 | inline void setRemoteDevice(android::sp<android::hardware::camera2::ICameraDeviceUser> remote) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 306 | mDevice->setRemoteDevice(remote); |
| 307 | } |
| 308 | |
| 309 | private: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 310 | android::sp<android::CameraDevice> mDevice; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | #endif // _ACAMERA_DEVICE_H |