blob: 23cc1a128368d45d3c85a01f0b04fa53593d2dd9 [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
Emilian Peev40ead602017-09-26 15:46:36 +010039#include <camera/NdkCameraManager.h>
40#include <camera/NdkCameraCaptureSession.h>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080041#include "ACameraMetadata.h"
42
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080043namespace android {
44
Yin-Chia Yehd21c46b2017-10-10 11:59:46 -070045// Wrap ACameraCaptureFailure so it can be ref-counted
Yin-Chia Yehead91462016-01-06 16:45:08 -080046struct CameraCaptureFailure : public RefBase, public ACameraCaptureFailure {};
47
48class CameraDevice final : public RefBase {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080049 public:
50 CameraDevice(const char* id, ACameraDevice_StateCallbacks* cb,
51 std::unique_ptr<ACameraMetadata> chars,
52 ACameraDevice* wrapper);
53 ~CameraDevice();
54
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
Yin-Chia Yehead91462016-01-06 16:45:08 -080061 camera_status_t createCaptureSession(
62 const ACaptureSessionOutputContainer* outputs,
63 const ACameraCaptureSession_stateCallbacks* callbacks,
64 /*out*/ACameraCaptureSession** session);
65
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080066 // Callbacks from camera service
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080067 class ServiceCallback : public hardware::camera2::BnCameraDeviceCallbacks {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080068 public:
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -070069 explicit ServiceCallback(CameraDevice* device) : mDevice(device) {}
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080070 binder::Status onDeviceError(int32_t errorCode,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080071 const CaptureResultExtras& resultExtras) override;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080072 binder::Status onDeviceIdle() override;
73 binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080074 int64_t timestamp) override;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080075 binder::Status onResultReceived(const CameraMetadata& metadata,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080076 const CaptureResultExtras& resultExtras) override;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080077 binder::Status onPrepared(int streamId) override;
Shuzhen Wang9d066012016-09-30 11:30:20 -070078 binder::Status onRequestQueueEmpty() override;
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -070079 binder::Status onRepeatingRequestError(int64_t lastFrameNumber,
80 int32_t stoppedSequenceId) override;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080081 private:
82 const wp<CameraDevice> mDevice;
83 };
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080084 inline sp<hardware::camera2::ICameraDeviceCallbacks> getServiceCallback() {
85 return mServiceCallback;
86 };
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080087
88 // Camera device is only functional after remote being set
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080089 void setRemoteDevice(sp<hardware::camera2::ICameraDeviceUser> remote);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080090
Yin-Chia Yehead91462016-01-06 16:45:08 -080091 inline ACameraDevice* getWrapper() const { return mWrapper; };
92
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080093 private:
Yin-Chia Yehead91462016-01-06 16:45:08 -080094 friend ACameraCaptureSession;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080095 camera_status_t checkCameraClosedOrErrorLocked() const;
96
Yin-Chia Yehead91462016-01-06 16:45:08 -080097 // device goes into fatal error state after this
98 void setCameraDeviceErrorLocked(camera_status_t error);
99
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700100 void disconnectLocked(sp<ACameraCaptureSession>& session); // disconnect from camera service
Yin-Chia Yehead91462016-01-06 16:45:08 -0800101
102 camera_status_t stopRepeatingLocked();
103
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700104 camera_status_t flushLocked(ACameraCaptureSession*);
105
Yin-Chia Yehead91462016-01-06 16:45:08 -0800106 camera_status_t waitUntilIdleLocked();
107
108
109 camera_status_t captureLocked(sp<ACameraCaptureSession> session,
110 /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
111 int numRequests, ACaptureRequest** requests,
112 /*optional*/int* captureSequenceId);
113
114 camera_status_t setRepeatingRequestsLocked(sp<ACameraCaptureSession> session,
115 /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
116 int numRequests, ACaptureRequest** requests,
117 /*optional*/int* captureSequenceId);
118
119 camera_status_t submitRequestsLocked(
120 sp<ACameraCaptureSession> session,
121 /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
122 int numRequests, ACaptureRequest** requests,
123 /*out*/int* captureSequenceId,
124 bool isRepeating);
125
Emilian Peev40ead602017-09-26 15:46:36 +0100126 camera_status_t updateOutputConfiguration(ACaptureSessionOutput *output);
127
Yin-Chia Yehead91462016-01-06 16:45:08 -0800128 static camera_status_t allocateCaptureRequest(
129 const ACaptureRequest* request, sp<CaptureRequest>& outReq);
130
131 static ACaptureRequest* allocateACaptureRequest(sp<CaptureRequest>& req);
132 static void freeACaptureRequest(ACaptureRequest*);
133
134 // only For session to hold device lock
135 // Always grab device lock before grabbing session lock
136 void lockDeviceForSessionOps() const { mDeviceLock.lock(); };
137 void unlockDevice() const { mDeviceLock.unlock(); };
138
139 // For capture session to notify its end of life
140 void notifySessionEndOfLifeLocked(ACameraCaptureSession* session);
141
142 camera_status_t configureStreamsLocked(const ACaptureSessionOutputContainer* outputs);
143
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700144 // Input message will be posted and cleared after this returns
145 void postSessionMsgAndCleanup(sp<AMessage>& msg);
146
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700147 static camera_status_t getIGBPfromAnw(
148 ANativeWindow* anw, sp<IGraphicBufferProducer>& out);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800149
150 static camera_status_t getSurfaceFromANativeWindow(
151 ANativeWindow* anw, sp<Surface>& out);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800152
153 mutable Mutex mDeviceLock;
154 const String8 mCameraId; // Camera ID
155 const ACameraDevice_StateCallbacks mAppCallbacks; // Callback to app
156 const std::unique_ptr<ACameraMetadata> mChars; // Camera characteristics
157 const sp<ServiceCallback> mServiceCallback;
158 ACameraDevice* mWrapper;
159
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700160 // stream id -> pair of (ANW* from application, OutputConfiguration used for camera service)
161 std::map<int, std::pair<ANativeWindow*, OutputConfiguration>> mConfiguredOutputs;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800162
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800163 // TODO: maybe a bool will suffice for synchronous implementation?
164 std::atomic_bool mClosing;
165 inline bool isClosed() { return mClosing; }
166
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700167 bool mInError = false;
168 camera_status_t mError = ACAMERA_OK;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800169 void onCaptureErrorLocked(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800170 int32_t errorCode,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800171 const CaptureResultExtras& resultExtras);
172
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700173 bool mIdle = true;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800174 // This will avoid a busy session being deleted before it's back to idle state
175 sp<ACameraCaptureSession> mBusySession;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800176
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800177 sp<hardware::camera2::ICameraDeviceUser> mRemote;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800178
179 // Looper thread to handle callback to app
180 sp<ALooper> mCbLooper;
181 // definition of handler and message
182 enum {
Yin-Chia Yehead91462016-01-06 16:45:08 -0800183 // Device state callbacks
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700184 kWhatOnDisconnected, // onDisconnected
185 kWhatOnError, // onError
Yin-Chia Yehead91462016-01-06 16:45:08 -0800186 // Session state callbacks
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700187 kWhatSessionStateCb, // onReady, onActive
Yin-Chia Yehead91462016-01-06 16:45:08 -0800188 // Capture callbacks
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700189 kWhatCaptureStart, // onCaptureStarted
190 kWhatCaptureResult, // onCaptureProgressed, onCaptureCompleted
191 kWhatCaptureFail, // onCaptureFailed
192 kWhatCaptureSeqEnd, // onCaptureSequenceCompleted
193 kWhatCaptureSeqAbort, // onCaptureSequenceAborted
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700194 kWhatCaptureBufferLost,// onCaptureBufferLost
195 // Internal cleanup
196 kWhatCleanUpSessions // Cleanup cached sp<ACameraCaptureSession>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800197 };
198 static const char* kContextKey;
199 static const char* kDeviceKey;
200 static const char* kErrorCodeKey;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800201 static const char* kCallbackFpKey;
202 static const char* kSessionSpKey;
203 static const char* kCaptureRequestKey;
204 static const char* kTimeStampKey;
205 static const char* kCaptureResultKey;
206 static const char* kCaptureFailureKey;
207 static const char* kSequenceIdKey;
208 static const char* kFrameNumberKey;
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700209 static const char* kAnwKey;
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700210
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800211 class CallbackHandler : public AHandler {
212 public:
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800213 void onMessageReceived(const sp<AMessage> &msg) override;
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700214
215 private:
216 // This handler will cache all capture session sp until kWhatCleanUpSessions
217 // is processed. This is used to guarantee the last session reference is always
218 // being removed in callback thread without holding camera device lock
219 Vector<sp<ACameraCaptureSession>> mCachedSessions;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800220 };
221 sp<CallbackHandler> mHandler;
222
Yin-Chia Yehead91462016-01-06 16:45:08 -0800223 /***********************************
224 * Capture session related members *
225 ***********************************/
226 // The current active session
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700227 wp<ACameraCaptureSession> mCurrentSession;
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700228 bool mFlushing = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800229
Yin-Chia Yehead91462016-01-06 16:45:08 -0800230 int mNextSessionId = 0;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800231 // TODO: might need another looper/handler to handle callbacks from service
232
Yin-Chia Yehead91462016-01-06 16:45:08 -0800233 static const int REQUEST_ID_NONE = -1;
234 int mRepeatingSequenceId = REQUEST_ID_NONE;
235
236 // sequence id -> last frame number map
237 std::map<int, int64_t> mSequenceLastFrameNumberMap;
238
239 struct CallbackHolder {
240 CallbackHolder(sp<ACameraCaptureSession> session,
241 const Vector<sp<CaptureRequest> >& requests,
242 bool isRepeating,
243 ACameraCaptureSession_captureCallbacks* cbs);
244
245 static ACameraCaptureSession_captureCallbacks fillCb(
246 ACameraCaptureSession_captureCallbacks* cbs) {
247 if (cbs != nullptr) {
248 return *cbs;
249 }
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700250 return { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
Yin-Chia Yehead91462016-01-06 16:45:08 -0800251 }
252
253 sp<ACameraCaptureSession> mSession;
254 Vector<sp<CaptureRequest> > mRequests;
255 const bool mIsRepeating;
256 ACameraCaptureSession_captureCallbacks mCallbacks;
257 };
258 // sequence id -> callbacks map
259 std::map<int, CallbackHolder> mSequenceCallbackMap;
260
261 static const int64_t NO_FRAMES_CAPTURED = -1;
262 class FrameNumberTracker {
263 public:
264 // TODO: Called in onResultReceived and onCaptureErrorLocked
265 void updateTracker(int64_t frameNumber, bool isError);
266 inline int64_t getCompletedFrameNumber() { return mCompletedFrameNumber; }
267 private:
268 void update();
269 void updateCompletedFrameNumber(int64_t frameNumber);
270
271 int64_t mCompletedFrameNumber = NO_FRAMES_CAPTURED;
272 List<int64_t> mSkippedFrameNumbers;
273 std::set<int64_t> mFutureErrorSet;
274 };
275 FrameNumberTracker mFrameNumberTracker;
276
277 void checkRepeatingSequenceCompleteLocked(const int sequenceId, const int64_t lastFrameNumber);
278 void checkAndFireSequenceCompleteLocked();
279
280 // Misc variables
281 int32_t mShadingMapSize[2]; // const after constructor
282 int32_t mPartialResultCount; // const after constructor
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800283
284};
285
286} // namespace android;
287
288/**
289 * ACameraDevice opaque struct definition
290 * Leave outside of android namespace because it's NDK struct
291 */
292struct ACameraDevice {
293 ACameraDevice(const char* id, ACameraDevice_StateCallbacks* cb,
294 std::unique_ptr<ACameraMetadata> chars) :
295 mDevice(new CameraDevice(id, cb, std::move(chars), this)) {}
296
297 ~ACameraDevice() {};
298
Yin-Chia Yehead91462016-01-06 16:45:08 -0800299 /*******************
300 * NDK public APIs *
301 *******************/
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800302 inline const char* getId() const { return mDevice->getId(); }
303
304 camera_status_t createCaptureRequest(
305 ACameraDevice_request_template templateId,
306 ACaptureRequest** request) const {
307 return mDevice->createCaptureRequest(templateId, request);
308 }
309
Yin-Chia Yehead91462016-01-06 16:45:08 -0800310 camera_status_t createCaptureSession(
311 const ACaptureSessionOutputContainer* outputs,
312 const ACameraCaptureSession_stateCallbacks* callbacks,
313 /*out*/ACameraCaptureSession** session) {
314 return mDevice->createCaptureSession(outputs, callbacks, session);
315 }
316
317 /***********************
318 * Device interal APIs *
319 ***********************/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800320 inline android::sp<android::hardware::camera2::ICameraDeviceCallbacks> getServiceCallback() {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800321 return mDevice->getServiceCallback();
322 };
323
324 // Camera device is only functional after remote being set
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800325 inline void setRemoteDevice(android::sp<android::hardware::camera2::ICameraDeviceUser> remote) {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800326 mDevice->setRemoteDevice(remote);
327 }
328
329 private:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800330 android::sp<android::CameraDevice> mDevice;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800331};
332
333#endif // _ACAMERA_DEVICE_H