blob: 6ed3881932bf0e0c2a240898e3512a2bfce20f99 [file] [log] [blame]
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001/*
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 Yehead91462016-01-06 16:45:08 -080020#include <map>
21#include <set>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080022#include <atomic>
Yin-Chia Yehe081c592016-03-29 18:26:44 -070023#include <utility>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080024#include <utils/StrongPointer.h>
25#include <utils/Mutex.h>
26#include <utils/String8.h>
Yin-Chia Yehead91462016-01-06 16:45:08 -080027#include <utils/List.h>
28#include <utils/Vector.h>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080029
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080030#include <android/hardware/camera2/BnCameraDeviceCallbacks.h>
31#include <android/hardware/camera2/ICameraDeviceUser.h>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080032#include <media/stagefright/foundation/ALooper.h>
33#include <media/stagefright/foundation/AHandler.h>
34#include <media/stagefright/foundation/AMessage.h>
Yin-Chia Yehead91462016-01-06 16:45:08 -080035#include <camera/CaptureResult.h>
Yin-Chia Yehead91462016-01-06 16:45:08 -080036#include <camera/camera2/OutputConfiguration.h>
37#include <camera/camera2/CaptureRequest.h>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080038
Colin Cross7e8d4ba2017-05-04 16:17:42 -070039#include <camera/NdkCameraDevice.h>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080040#include "ACameraMetadata.h"
41
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080042namespace android {
43
Yin-Chia Yehd21c46b2017-10-10 11:59:46 -070044// Wrap ACameraCaptureFailure so it can be ref-counted
Yin-Chia Yehead91462016-01-06 16:45:08 -080045struct CameraCaptureFailure : public RefBase, public ACameraCaptureFailure {};
46
47class CameraDevice final : public RefBase {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080048 public:
49 CameraDevice(const char* id, ACameraDevice_StateCallbacks* cb,
50 std::unique_ptr<ACameraMetadata> chars,
51 ACameraDevice* wrapper);
52 ~CameraDevice();
53
54 inline const char* getId() const { return mCameraId.string(); }
55
56 camera_status_t createCaptureRequest(
57 ACameraDevice_request_template templateId,
58 ACaptureRequest** request) const;
59
Yin-Chia Yehead91462016-01-06 16:45:08 -080060 camera_status_t createCaptureSession(
61 const ACaptureSessionOutputContainer* outputs,
62 const ACameraCaptureSession_stateCallbacks* callbacks,
63 /*out*/ACameraCaptureSession** session);
64
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080065 // Callbacks from camera service
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080066 class ServiceCallback : public hardware::camera2::BnCameraDeviceCallbacks {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080067 public:
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -070068 explicit ServiceCallback(CameraDevice* device) : mDevice(device) {}
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080069 binder::Status onDeviceError(int32_t errorCode,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080070 const CaptureResultExtras& resultExtras) override;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080071 binder::Status onDeviceIdle() override;
72 binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080073 int64_t timestamp) override;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080074 binder::Status onResultReceived(const CameraMetadata& metadata,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080075 const CaptureResultExtras& resultExtras) override;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080076 binder::Status onPrepared(int streamId) override;
Shuzhen Wang9d066012016-09-30 11:30:20 -070077 binder::Status onRequestQueueEmpty() override;
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -070078 binder::Status onRepeatingRequestError(int64_t lastFrameNumber,
79 int32_t stoppedSequenceId) override;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080080 private:
81 const wp<CameraDevice> mDevice;
82 };
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080083 inline sp<hardware::camera2::ICameraDeviceCallbacks> getServiceCallback() {
84 return mServiceCallback;
85 };
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080086
87 // Camera device is only functional after remote being set
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080088 void setRemoteDevice(sp<hardware::camera2::ICameraDeviceUser> remote);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080089
Yin-Chia Yehead91462016-01-06 16:45:08 -080090 inline ACameraDevice* getWrapper() const { return mWrapper; };
91
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080092 private:
Yin-Chia Yehead91462016-01-06 16:45:08 -080093 friend ACameraCaptureSession;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080094 camera_status_t checkCameraClosedOrErrorLocked() const;
95
Yin-Chia Yehead91462016-01-06 16:45:08 -080096 // device goes into fatal error state after this
97 void setCameraDeviceErrorLocked(camera_status_t error);
98
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -070099 void disconnectLocked(sp<ACameraCaptureSession>& session); // disconnect from camera service
Yin-Chia Yehead91462016-01-06 16:45:08 -0800100
101 camera_status_t stopRepeatingLocked();
102
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700103 camera_status_t flushLocked(ACameraCaptureSession*);
104
Yin-Chia Yehead91462016-01-06 16:45:08 -0800105 camera_status_t waitUntilIdleLocked();
106
107
108 camera_status_t captureLocked(sp<ACameraCaptureSession> session,
109 /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
110 int numRequests, ACaptureRequest** requests,
111 /*optional*/int* captureSequenceId);
112
113 camera_status_t setRepeatingRequestsLocked(sp<ACameraCaptureSession> session,
114 /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
115 int numRequests, ACaptureRequest** requests,
116 /*optional*/int* captureSequenceId);
117
118 camera_status_t submitRequestsLocked(
119 sp<ACameraCaptureSession> session,
120 /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
121 int numRequests, ACaptureRequest** requests,
122 /*out*/int* captureSequenceId,
123 bool isRepeating);
124
125 static camera_status_t allocateCaptureRequest(
126 const ACaptureRequest* request, sp<CaptureRequest>& outReq);
127
128 static ACaptureRequest* allocateACaptureRequest(sp<CaptureRequest>& req);
129 static void freeACaptureRequest(ACaptureRequest*);
130
131 // only For session to hold device lock
132 // Always grab device lock before grabbing session lock
133 void lockDeviceForSessionOps() const { mDeviceLock.lock(); };
134 void unlockDevice() const { mDeviceLock.unlock(); };
135
136 // For capture session to notify its end of life
137 void notifySessionEndOfLifeLocked(ACameraCaptureSession* session);
138
139 camera_status_t configureStreamsLocked(const ACaptureSessionOutputContainer* outputs);
140
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700141 // Input message will be posted and cleared after this returns
142 void postSessionMsgAndCleanup(sp<AMessage>& msg);
143
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700144 static camera_status_t getIGBPfromAnw(
145 ANativeWindow* anw, sp<IGraphicBufferProducer>& out);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800146
147 static camera_status_t getSurfaceFromANativeWindow(
148 ANativeWindow* anw, sp<Surface>& out);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800149
150 mutable Mutex mDeviceLock;
151 const String8 mCameraId; // Camera ID
152 const ACameraDevice_StateCallbacks mAppCallbacks; // Callback to app
153 const std::unique_ptr<ACameraMetadata> mChars; // Camera characteristics
154 const sp<ServiceCallback> mServiceCallback;
155 ACameraDevice* mWrapper;
156
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700157 // stream id -> pair of (ANW* from application, OutputConfiguration used for camera service)
158 std::map<int, std::pair<ANativeWindow*, OutputConfiguration>> mConfiguredOutputs;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800159
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800160 // TODO: maybe a bool will suffice for synchronous implementation?
161 std::atomic_bool mClosing;
162 inline bool isClosed() { return mClosing; }
163
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700164 bool mInError = false;
165 camera_status_t mError = ACAMERA_OK;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800166 void onCaptureErrorLocked(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800167 int32_t errorCode,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800168 const CaptureResultExtras& resultExtras);
169
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700170 bool mIdle = true;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800171 // This will avoid a busy session being deleted before it's back to idle state
172 sp<ACameraCaptureSession> mBusySession;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800173
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800174 sp<hardware::camera2::ICameraDeviceUser> mRemote;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800175
176 // Looper thread to handle callback to app
177 sp<ALooper> mCbLooper;
178 // definition of handler and message
179 enum {
Yin-Chia Yehead91462016-01-06 16:45:08 -0800180 // Device state callbacks
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700181 kWhatOnDisconnected, // onDisconnected
182 kWhatOnError, // onError
Yin-Chia Yehead91462016-01-06 16:45:08 -0800183 // Session state callbacks
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700184 kWhatSessionStateCb, // onReady, onActive
Yin-Chia Yehead91462016-01-06 16:45:08 -0800185 // Capture callbacks
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700186 kWhatCaptureStart, // onCaptureStarted
187 kWhatCaptureResult, // onCaptureProgressed, onCaptureCompleted
188 kWhatCaptureFail, // onCaptureFailed
189 kWhatCaptureSeqEnd, // onCaptureSequenceCompleted
190 kWhatCaptureSeqAbort, // onCaptureSequenceAborted
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700191 kWhatCaptureBufferLost,// onCaptureBufferLost
192 // Internal cleanup
193 kWhatCleanUpSessions // Cleanup cached sp<ACameraCaptureSession>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800194 };
195 static const char* kContextKey;
196 static const char* kDeviceKey;
197 static const char* kErrorCodeKey;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800198 static const char* kCallbackFpKey;
199 static const char* kSessionSpKey;
200 static const char* kCaptureRequestKey;
201 static const char* kTimeStampKey;
202 static const char* kCaptureResultKey;
203 static const char* kCaptureFailureKey;
204 static const char* kSequenceIdKey;
205 static const char* kFrameNumberKey;
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700206 static const char* kAnwKey;
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700207
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800208 class CallbackHandler : public AHandler {
209 public:
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800210 void onMessageReceived(const sp<AMessage> &msg) override;
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700211
212 private:
213 // This handler will cache all capture session sp until kWhatCleanUpSessions
214 // is processed. This is used to guarantee the last session reference is always
215 // being removed in callback thread without holding camera device lock
216 Vector<sp<ACameraCaptureSession>> mCachedSessions;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800217 };
218 sp<CallbackHandler> mHandler;
219
Yin-Chia Yehead91462016-01-06 16:45:08 -0800220 /***********************************
221 * Capture session related members *
222 ***********************************/
223 // The current active session
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700224 wp<ACameraCaptureSession> mCurrentSession;
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700225 bool mFlushing = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800226
Yin-Chia Yehead91462016-01-06 16:45:08 -0800227 int mNextSessionId = 0;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800228 // TODO: might need another looper/handler to handle callbacks from service
229
Yin-Chia Yehead91462016-01-06 16:45:08 -0800230 static const int REQUEST_ID_NONE = -1;
231 int mRepeatingSequenceId = REQUEST_ID_NONE;
232
233 // sequence id -> last frame number map
234 std::map<int, int64_t> mSequenceLastFrameNumberMap;
235
236 struct CallbackHolder {
237 CallbackHolder(sp<ACameraCaptureSession> session,
238 const Vector<sp<CaptureRequest> >& requests,
239 bool isRepeating,
240 ACameraCaptureSession_captureCallbacks* cbs);
241
242 static ACameraCaptureSession_captureCallbacks fillCb(
243 ACameraCaptureSession_captureCallbacks* cbs) {
244 if (cbs != nullptr) {
245 return *cbs;
246 }
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700247 return { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
Yin-Chia Yehead91462016-01-06 16:45:08 -0800248 }
249
250 sp<ACameraCaptureSession> mSession;
251 Vector<sp<CaptureRequest> > mRequests;
252 const bool mIsRepeating;
253 ACameraCaptureSession_captureCallbacks mCallbacks;
254 };
255 // sequence id -> callbacks map
256 std::map<int, CallbackHolder> mSequenceCallbackMap;
257
258 static const int64_t NO_FRAMES_CAPTURED = -1;
259 class FrameNumberTracker {
260 public:
261 // TODO: Called in onResultReceived and onCaptureErrorLocked
262 void updateTracker(int64_t frameNumber, bool isError);
263 inline int64_t getCompletedFrameNumber() { return mCompletedFrameNumber; }
264 private:
265 void update();
266 void updateCompletedFrameNumber(int64_t frameNumber);
267
268 int64_t mCompletedFrameNumber = NO_FRAMES_CAPTURED;
269 List<int64_t> mSkippedFrameNumbers;
270 std::set<int64_t> mFutureErrorSet;
271 };
272 FrameNumberTracker mFrameNumberTracker;
273
274 void checkRepeatingSequenceCompleteLocked(const int sequenceId, const int64_t lastFrameNumber);
275 void checkAndFireSequenceCompleteLocked();
276
277 // Misc variables
278 int32_t mShadingMapSize[2]; // const after constructor
279 int32_t mPartialResultCount; // const after constructor
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800280
281};
282
283} // namespace android;
284
285/**
286 * ACameraDevice opaque struct definition
287 * Leave outside of android namespace because it's NDK struct
288 */
289struct ACameraDevice {
290 ACameraDevice(const char* id, ACameraDevice_StateCallbacks* cb,
291 std::unique_ptr<ACameraMetadata> chars) :
292 mDevice(new CameraDevice(id, cb, std::move(chars), this)) {}
293
294 ~ACameraDevice() {};
295
Yin-Chia Yehead91462016-01-06 16:45:08 -0800296 /*******************
297 * NDK public APIs *
298 *******************/
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800299 inline const char* getId() const { return mDevice->getId(); }
300
301 camera_status_t createCaptureRequest(
302 ACameraDevice_request_template templateId,
303 ACaptureRequest** request) const {
304 return mDevice->createCaptureRequest(templateId, request);
305 }
306
Yin-Chia Yehead91462016-01-06 16:45:08 -0800307 camera_status_t createCaptureSession(
308 const ACaptureSessionOutputContainer* outputs,
309 const ACameraCaptureSession_stateCallbacks* callbacks,
310 /*out*/ACameraCaptureSession** session) {
311 return mDevice->createCaptureSession(outputs, callbacks, session);
312 }
313
314 /***********************
315 * Device interal APIs *
316 ***********************/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800317 inline android::sp<android::hardware::camera2::ICameraDeviceCallbacks> getServiceCallback() {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800318 return mDevice->getServiceCallback();
319 };
320
321 // Camera device is only functional after remote being set
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800322 inline void setRemoteDevice(android::sp<android::hardware::camera2::ICameraDeviceUser> remote) {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800323 mDevice->setRemoteDevice(remote);
324 }
325
326 private:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800327 android::sp<android::CameraDevice> mDevice;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800328};
329
330#endif // _ACAMERA_DEVICE_H