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