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
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "ACameraDevice"
19
20 #include <vector>
21 #include <inttypes.h>
22 #include <android/hardware/ICameraService.h>
23 #include <camera2/SubmitInfo.h>
24 #include <gui/Surface.h>
25 #include "ACameraDevice.h"
26 #include "ACameraMetadata.h"
27 #include "ACaptureRequest.h"
28 #include "ACameraCaptureSession.h"
29
30 using namespace android;
31
32 namespace android {
33 // Static member definitions
34 const char* CameraDevice::kContextKey = "Context";
35 const char* CameraDevice::kDeviceKey = "Device";
36 const char* CameraDevice::kErrorCodeKey = "ErrorCode";
37 const char* CameraDevice::kCallbackFpKey = "Callback";
38 const char* CameraDevice::kSessionSpKey = "SessionSp";
39 const char* CameraDevice::kCaptureRequestKey = "CaptureRequest";
40 const char* CameraDevice::kTimeStampKey = "TimeStamp";
41 const char* CameraDevice::kCaptureResultKey = "CaptureResult";
42 const char* CameraDevice::kCaptureFailureKey = "CaptureFailure";
43 const char* CameraDevice::kSequenceIdKey = "SequenceId";
44 const char* CameraDevice::kFrameNumberKey = "FrameNumber";
45 const char* CameraDevice::kAnwKey = "Anw";
46
47 /**
48 * CameraDevice Implementation
49 */
CameraDevice(const char * id,ACameraDevice_StateCallbacks * cb,std::unique_ptr<ACameraMetadata> chars,ACameraDevice * wrapper)50 CameraDevice::CameraDevice(
51 const char* id,
52 ACameraDevice_StateCallbacks* cb,
53 std::unique_ptr<ACameraMetadata> chars,
54 ACameraDevice* wrapper) :
55 mCameraId(id),
56 mAppCallbacks(*cb),
57 mChars(std::move(chars)),
58 mServiceCallback(new ServiceCallback(this)),
59 mWrapper(wrapper),
60 mInError(false),
61 mError(ACAMERA_OK),
62 mIdle(true) {
63 mClosing = false;
64 // Setup looper thread to perfrom device callbacks to app
65 mCbLooper = new ALooper;
66 mCbLooper->setName("C2N-dev-looper");
67 status_t err = mCbLooper->start(
68 /*runOnCallingThread*/false,
69 /*canCallJava*/ true,
70 PRIORITY_DEFAULT);
71 if (err != OK) {
72 ALOGE("%s: Unable to start camera device callback looper: %s (%d)",
73 __FUNCTION__, strerror(-err), err);
74 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
75 }
76 mHandler = new CallbackHandler();
77 mCbLooper->registerHandler(mHandler);
78
79 const CameraMetadata& metadata = mChars->getInternalData();
80 camera_metadata_ro_entry entry = metadata.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
81 if (entry.count != 1) {
82 ALOGW("%s: bad count %zu for partial result count", __FUNCTION__, entry.count);
83 mPartialResultCount = 1;
84 } else {
85 mPartialResultCount = entry.data.i32[0];
86 }
87
88 entry = metadata.find(ANDROID_LENS_INFO_SHADING_MAP_SIZE);
89 if (entry.count != 2) {
90 ALOGW("%s: bad count %zu for shading map size", __FUNCTION__, entry.count);
91 mShadingMapSize[0] = 0;
92 mShadingMapSize[1] = 0;
93 } else {
94 mShadingMapSize[0] = entry.data.i32[0];
95 mShadingMapSize[1] = entry.data.i32[1];
96 }
97 }
98
99 // Device close implementaiton
~CameraDevice()100 CameraDevice::~CameraDevice() {
101 Mutex::Autolock _l(mDeviceLock);
102 if (!isClosed()) {
103 disconnectLocked();
104 }
105 if (mCbLooper != nullptr) {
106 mCbLooper->unregisterHandler(mHandler->id());
107 mCbLooper->stop();
108 }
109 mCbLooper.clear();
110 mHandler.clear();
111 }
112
113 // TODO: cached created request?
114 camera_status_t
createCaptureRequest(ACameraDevice_request_template templateId,ACaptureRequest ** request) const115 CameraDevice::createCaptureRequest(
116 ACameraDevice_request_template templateId,
117 ACaptureRequest** request) const {
118 Mutex::Autolock _l(mDeviceLock);
119 camera_status_t ret = checkCameraClosedOrErrorLocked();
120 if (ret != ACAMERA_OK) {
121 return ret;
122 }
123 if (mRemote == nullptr) {
124 return ACAMERA_ERROR_CAMERA_DISCONNECTED;
125 }
126 CameraMetadata rawRequest;
127 binder::Status remoteRet = mRemote->createDefaultRequest(templateId, &rawRequest);
128 if (remoteRet.serviceSpecificErrorCode() ==
129 hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT) {
130 ALOGW("Create capture request failed! template %d is not supported on this device",
131 templateId);
132 return ACAMERA_ERROR_INVALID_PARAMETER;
133 } else if (!remoteRet.isOk()) {
134 ALOGE("Create capture request failed: %s", remoteRet.toString8().string());
135 return ACAMERA_ERROR_UNKNOWN;
136 }
137 ACaptureRequest* outReq = new ACaptureRequest();
138 outReq->settings = new ACameraMetadata(rawRequest.release(), ACameraMetadata::ACM_REQUEST);
139 outReq->targets = new ACameraOutputTargets();
140 *request = outReq;
141 return ACAMERA_OK;
142 }
143
144 camera_status_t
createCaptureSession(const ACaptureSessionOutputContainer * outputs,const ACameraCaptureSession_stateCallbacks * callbacks,ACameraCaptureSession ** session)145 CameraDevice::createCaptureSession(
146 const ACaptureSessionOutputContainer* outputs,
147 const ACameraCaptureSession_stateCallbacks* callbacks,
148 /*out*/ACameraCaptureSession** session) {
149 Mutex::Autolock _l(mDeviceLock);
150 camera_status_t ret = checkCameraClosedOrErrorLocked();
151 if (ret != ACAMERA_OK) {
152 return ret;
153 }
154
155 if (mCurrentSession != nullptr) {
156 mCurrentSession->closeByDevice();
157 stopRepeatingLocked();
158 }
159
160 // Create new session
161 ret = configureStreamsLocked(outputs);
162 if (ret != ACAMERA_OK) {
163 ALOGE("Fail to create new session. cannot configure streams");
164 return ret;
165 }
166
167 ACameraCaptureSession* newSession = new ACameraCaptureSession(
168 mNextSessionId++, outputs, callbacks, this);
169
170 // set new session as current session
171 newSession->incStrong((void *) ACameraDevice_createCaptureSession);
172 mCurrentSession = newSession;
173 mFlushing = false;
174 *session = newSession;
175 return ACAMERA_OK;
176 }
177
178 camera_status_t
captureLocked(sp<ACameraCaptureSession> session,ACameraCaptureSession_captureCallbacks * cbs,int numRequests,ACaptureRequest ** requests,int * captureSequenceId)179 CameraDevice::captureLocked(
180 sp<ACameraCaptureSession> session,
181 /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
182 int numRequests, ACaptureRequest** requests,
183 /*optional*/int* captureSequenceId) {
184 return submitRequestsLocked(
185 session, cbs, numRequests, requests, captureSequenceId, /*isRepeating*/false);
186 }
187
188 camera_status_t
setRepeatingRequestsLocked(sp<ACameraCaptureSession> session,ACameraCaptureSession_captureCallbacks * cbs,int numRequests,ACaptureRequest ** requests,int * captureSequenceId)189 CameraDevice::setRepeatingRequestsLocked(
190 sp<ACameraCaptureSession> session,
191 /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
192 int numRequests, ACaptureRequest** requests,
193 /*optional*/int* captureSequenceId) {
194 return submitRequestsLocked(
195 session, cbs, numRequests, requests, captureSequenceId, /*isRepeating*/true);
196 }
197
198 camera_status_t
submitRequestsLocked(sp<ACameraCaptureSession> session,ACameraCaptureSession_captureCallbacks * cbs,int numRequests,ACaptureRequest ** requests,int * captureSequenceId,bool isRepeating)199 CameraDevice::submitRequestsLocked(
200 sp<ACameraCaptureSession> session,
201 /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
202 int numRequests, ACaptureRequest** requests,
203 /*optional*/int* captureSequenceId,
204 bool isRepeating) {
205 camera_status_t ret = checkCameraClosedOrErrorLocked();
206 if (ret != ACAMERA_OK) {
207 ALOGE("Camera %s submit capture request failed! ret %d", getId(), ret);
208 return ret;
209 }
210
211 // Form two vectors of capture request, one for internal tracking
212 std::vector<hardware::camera2::CaptureRequest> requestList;
213 Vector<sp<CaptureRequest> > requestsV;
214 requestsV.setCapacity(numRequests);
215 for (int i = 0; i < numRequests; i++) {
216 sp<CaptureRequest> req;
217 ret = allocateCaptureRequest(requests[i], req);
218 if (ret != ACAMERA_OK) {
219 ALOGE("Convert capture request to internal format failure! ret %d", ret);
220 return ret;
221 }
222 if (req->mSurfaceList.empty()) {
223 ALOGE("Capture request without output target cannot be submitted!");
224 return ACAMERA_ERROR_INVALID_PARAMETER;
225 }
226 requestList.push_back(*(req.get()));
227 requestsV.push_back(req);
228 }
229
230 if (isRepeating) {
231 ret = stopRepeatingLocked();
232 if (ret != ACAMERA_OK) {
233 ALOGE("Camera %s stop repeating failed! ret %d", getId(), ret);
234 return ret;
235 }
236 }
237
238 binder::Status remoteRet;
239 hardware::camera2::utils::SubmitInfo info;
240 remoteRet = mRemote->submitRequestList(requestList, isRepeating, &info);
241 int sequenceId = info.mRequestId;
242 int64_t lastFrameNumber = info.mLastFrameNumber;
243 if (sequenceId < 0) {
244 ALOGE("Camera %s submit request remote failure: ret %d", getId(), sequenceId);
245 return ACAMERA_ERROR_UNKNOWN;
246 }
247
248 CallbackHolder cbHolder(session, requestsV, isRepeating, cbs);
249 mSequenceCallbackMap.insert(std::make_pair(sequenceId, cbHolder));
250
251 if (isRepeating) {
252 // stopRepeating above should have cleanup repeating sequence id
253 if (mRepeatingSequenceId != REQUEST_ID_NONE) {
254 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
255 return ACAMERA_ERROR_CAMERA_DEVICE;
256 }
257 mRepeatingSequenceId = sequenceId;
258 } else {
259 mSequenceLastFrameNumberMap.insert(std::make_pair(sequenceId, lastFrameNumber));
260 }
261
262 if (mIdle) {
263 sp<AMessage> msg = new AMessage(kWhatSessionStateCb, mHandler);
264 msg->setPointer(kContextKey, session->mUserSessionCallback.context);
265 msg->setObject(kSessionSpKey, session);
266 msg->setPointer(kCallbackFpKey, (void*) session->mUserSessionCallback.onActive);
267 msg->post();
268 }
269 mIdle = false;
270 mBusySession = session;
271
272 if (captureSequenceId) {
273 *captureSequenceId = sequenceId;
274 }
275 return ACAMERA_OK;
276 }
277
278 camera_status_t
allocateCaptureRequest(const ACaptureRequest * request,sp<CaptureRequest> & outReq)279 CameraDevice::allocateCaptureRequest(
280 const ACaptureRequest* request, /*out*/sp<CaptureRequest>& outReq) {
281 camera_status_t ret;
282 sp<CaptureRequest> req(new CaptureRequest());
283 req->mMetadata = request->settings->getInternalData();
284 req->mIsReprocess = false; // NDK does not support reprocessing yet
285
286 for (auto outputTarget : request->targets->mOutputs) {
287 ANativeWindow* anw = outputTarget.mWindow;
288 sp<Surface> surface;
289 ret = getSurfaceFromANativeWindow(anw, surface);
290 if (ret != ACAMERA_OK) {
291 ALOGE("Bad output target in capture request! ret %d", ret);
292 return ret;
293 }
294 req->mSurfaceList.push_back(surface);
295 }
296 outReq = req;
297 return ACAMERA_OK;
298 }
299
300 ACaptureRequest*
allocateACaptureRequest(sp<CaptureRequest> & req)301 CameraDevice::allocateACaptureRequest(sp<CaptureRequest>& req) {
302 ACaptureRequest* pRequest = new ACaptureRequest();
303 CameraMetadata clone = req->mMetadata;
304 pRequest->settings = new ACameraMetadata(clone.release(), ACameraMetadata::ACM_REQUEST);
305 pRequest->targets = new ACameraOutputTargets();
306 for (size_t i = 0; i < req->mSurfaceList.size(); i++) {
307 ANativeWindow* anw = static_cast<ANativeWindow*>(req->mSurfaceList[i].get());
308 ACameraOutputTarget outputTarget(anw);
309 pRequest->targets->mOutputs.insert(outputTarget);
310 }
311 return pRequest;
312 }
313
314 void
freeACaptureRequest(ACaptureRequest * req)315 CameraDevice::freeACaptureRequest(ACaptureRequest* req) {
316 if (req == nullptr) {
317 return;
318 }
319 delete req->settings;
320 delete req->targets;
321 delete req;
322 }
323
324 void
notifySessionEndOfLifeLocked(ACameraCaptureSession * session)325 CameraDevice::notifySessionEndOfLifeLocked(ACameraCaptureSession* session) {
326 if (isClosed()) {
327 // Device is closing already. do nothing
328 return;
329 }
330
331 if (session != mCurrentSession) {
332 // Session has been replaced by other seesion or device is closed
333 return;
334 }
335 mCurrentSession = nullptr;
336
337 // Should not happen
338 if (!session->mIsClosed) {
339 ALOGE("Error: unclosed session %p reaches end of life!", session);
340 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
341 return;
342 }
343
344 // No new session, unconfigure now
345 camera_status_t ret = configureStreamsLocked(nullptr);
346 if (ret != ACAMERA_OK) {
347 ALOGE("Unconfigure stream failed. Device might still be configured! ret %d", ret);
348 }
349 }
350
351 void
disconnectLocked()352 CameraDevice::disconnectLocked() {
353 if (mClosing.exchange(true)) {
354 // Already closing, just return
355 ALOGW("Camera device %s is already closing.", getId());
356 return;
357 }
358
359 if (mRemote != nullptr) {
360 mRemote->disconnect();
361 }
362 mRemote = nullptr;
363
364 if (mCurrentSession != nullptr) {
365 mCurrentSession->closeByDevice();
366 mCurrentSession = nullptr;
367 }
368 }
369
370 camera_status_t
stopRepeatingLocked()371 CameraDevice::stopRepeatingLocked() {
372 camera_status_t ret = checkCameraClosedOrErrorLocked();
373 if (ret != ACAMERA_OK) {
374 ALOGE("Camera %s stop repeating failed! ret %d", getId(), ret);
375 return ret;
376 }
377 if (mRepeatingSequenceId != REQUEST_ID_NONE) {
378 int repeatingSequenceId = mRepeatingSequenceId;
379 mRepeatingSequenceId = REQUEST_ID_NONE;
380
381 int64_t lastFrameNumber;
382 binder::Status remoteRet = mRemote->cancelRequest(repeatingSequenceId, &lastFrameNumber);
383 if (remoteRet.serviceSpecificErrorCode() ==
384 hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT) {
385 ALOGV("Repeating request is already stopped.");
386 return ACAMERA_OK;
387 } else if (!remoteRet.isOk()) {
388 ALOGE("Stop repeating request fails in remote: %s", remoteRet.toString8().string());
389 return ACAMERA_ERROR_UNKNOWN;
390 }
391 checkRepeatingSequenceCompleteLocked(repeatingSequenceId, lastFrameNumber);
392 }
393 return ACAMERA_OK;
394 }
395
396 camera_status_t
flushLocked(ACameraCaptureSession * session)397 CameraDevice::flushLocked(ACameraCaptureSession* session) {
398 camera_status_t ret = checkCameraClosedOrErrorLocked();
399 if (ret != ACAMERA_OK) {
400 ALOGE("Camera %s abort captures failed! ret %d", getId(), ret);
401 return ret;
402 }
403
404 // This should never happen because creating a new session will close
405 // previous one and thus reject any API call from previous session.
406 // But still good to check here in case something unexpected happen.
407 if (session != mCurrentSession) {
408 ALOGE("Camera %s session %p is not current active session!", getId(), session);
409 return ACAMERA_ERROR_INVALID_OPERATION;
410 }
411
412 if (mFlushing) {
413 ALOGW("Camera %s is already aborting captures", getId());
414 return ACAMERA_OK;
415 }
416
417 mFlushing = true;
418 // Send onActive callback to guarantee there is always active->ready transition
419 sp<AMessage> msg = new AMessage(kWhatSessionStateCb, mHandler);
420 msg->setPointer(kContextKey, session->mUserSessionCallback.context);
421 msg->setObject(kSessionSpKey, session);
422 msg->setPointer(kCallbackFpKey, (void*) session->mUserSessionCallback.onActive);
423 msg->post();
424
425 // If device is already idling, send callback and exit early
426 if (mIdle) {
427 sp<AMessage> msg = new AMessage(kWhatSessionStateCb, mHandler);
428 msg->setPointer(kContextKey, session->mUserSessionCallback.context);
429 msg->setObject(kSessionSpKey, session);
430 msg->setPointer(kCallbackFpKey, (void*) session->mUserSessionCallback.onReady);
431 msg->post();
432 mFlushing = false;
433 return ACAMERA_OK;
434 }
435
436 int64_t lastFrameNumber;
437 binder::Status remoteRet = mRemote->flush(&lastFrameNumber);
438 if (!remoteRet.isOk()) {
439 ALOGE("Abort captures fails in remote: %s", remoteRet.toString8().string());
440 return ACAMERA_ERROR_UNKNOWN;
441 }
442 if (mRepeatingSequenceId != REQUEST_ID_NONE) {
443 checkRepeatingSequenceCompleteLocked(mRepeatingSequenceId, lastFrameNumber);
444 }
445 return ACAMERA_OK;
446 }
447
448 camera_status_t
waitUntilIdleLocked()449 CameraDevice::waitUntilIdleLocked() {
450 camera_status_t ret = checkCameraClosedOrErrorLocked();
451 if (ret != ACAMERA_OK) {
452 ALOGE("Wait until camera %s idle failed! ret %d", getId(), ret);
453 return ret;
454 }
455
456 if (mRepeatingSequenceId != REQUEST_ID_NONE) {
457 ALOGE("Camera device %s won't go to idle when there is repeating request!", getId());
458 return ACAMERA_ERROR_INVALID_OPERATION;
459 }
460
461 binder::Status remoteRet = mRemote->waitUntilIdle();
462 if (!remoteRet.isOk()) {
463 ALOGE("Camera device %s waitUntilIdle failed: %s", getId(), remoteRet.toString8().string());
464 // TODO: define a function to convert status_t -> camera_status_t
465 return ACAMERA_ERROR_UNKNOWN;
466 }
467
468 return ACAMERA_OK;
469 }
470
471 camera_status_t
getIGBPfromAnw(ANativeWindow * anw,sp<IGraphicBufferProducer> & out)472 CameraDevice::getIGBPfromAnw(
473 ANativeWindow* anw,
474 sp<IGraphicBufferProducer>& out) {
475 if (anw == nullptr) {
476 ALOGE("Error: output ANativeWindow is null");
477 return ACAMERA_ERROR_INVALID_PARAMETER;
478 }
479 int value;
480 int err = (*anw->query)(anw, NATIVE_WINDOW_CONCRETE_TYPE, &value);
481 if (err != OK || value != NATIVE_WINDOW_SURFACE) {
482 ALOGE("Error: ANativeWindow is not backed by Surface!");
483 return ACAMERA_ERROR_INVALID_PARAMETER;
484 }
485 const sp<Surface> surface(static_cast<Surface*>(anw));
486 out = surface->getIGraphicBufferProducer();
487 return ACAMERA_OK;
488 }
489
490 camera_status_t
getSurfaceFromANativeWindow(ANativeWindow * anw,sp<Surface> & out)491 CameraDevice::getSurfaceFromANativeWindow(
492 ANativeWindow* anw, sp<Surface>& out) {
493 if (anw == nullptr) {
494 ALOGE("Error: output ANativeWindow is null");
495 return ACAMERA_ERROR_INVALID_PARAMETER;
496 }
497 int value;
498 int err = (*anw->query)(anw, NATIVE_WINDOW_CONCRETE_TYPE, &value);
499 if (err != OK || value != NATIVE_WINDOW_SURFACE) {
500 ALOGE("Error: ANativeWindow is not backed by Surface!");
501 return ACAMERA_ERROR_INVALID_PARAMETER;
502 }
503 sp<Surface> surface(static_cast<Surface*>(anw));
504 out = surface;
505 return ACAMERA_OK;
506 }
507
508 camera_status_t
configureStreamsLocked(const ACaptureSessionOutputContainer * outputs)509 CameraDevice::configureStreamsLocked(const ACaptureSessionOutputContainer* outputs) {
510 ACaptureSessionOutputContainer emptyOutput;
511 if (outputs == nullptr) {
512 outputs = &emptyOutput;
513 }
514
515 camera_status_t ret = checkCameraClosedOrErrorLocked();
516 if (ret != ACAMERA_OK) {
517 return ret;
518 }
519
520 std::set<std::pair<ANativeWindow*, OutputConfiguration>> outputSet;
521 for (auto outConfig : outputs->mOutputs) {
522 ANativeWindow* anw = outConfig.mWindow;
523 sp<IGraphicBufferProducer> iGBP(nullptr);
524 ret = getIGBPfromAnw(anw, iGBP);
525 if (ret != ACAMERA_OK) {
526 return ret;
527 }
528 outputSet.insert(std::make_pair(
529 anw, OutputConfiguration(iGBP, outConfig.mRotation)));
530 }
531 auto addSet = outputSet;
532 std::vector<int> deleteList;
533
534 // Determine which streams need to be created, which to be deleted
535 for (auto& kvPair : mConfiguredOutputs) {
536 int streamId = kvPair.first;
537 auto& outputPair = kvPair.second;
538 if (outputSet.count(outputPair) == 0) {
539 deleteList.push_back(streamId); // Need to delete a no longer needed stream
540 } else {
541 addSet.erase(outputPair); // No need to add already existing stream
542 }
543 }
544
545 ret = stopRepeatingLocked();
546 if (ret != ACAMERA_OK) {
547 ALOGE("Camera device %s stop repeating failed, ret %d", getId(), ret);
548 return ret;
549 }
550
551 ret = waitUntilIdleLocked();
552 if (ret != ACAMERA_OK) {
553 ALOGE("Camera device %s wait until idle failed, ret %d", getId(), ret);
554 return ret;
555 }
556
557 // Send onReady to previous session
558 // CurrentSession will be updated after configureStreamLocked, so here
559 // mCurrentSession is the session to be replaced by a new session
560 if (!mIdle && mCurrentSession != nullptr) {
561 if (mBusySession != mCurrentSession) {
562 ALOGE("Current session != busy session");
563 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
564 return ACAMERA_ERROR_CAMERA_DEVICE;
565 }
566 sp<AMessage> msg = new AMessage(kWhatSessionStateCb, mHandler);
567 msg->setPointer(kContextKey, mBusySession->mUserSessionCallback.context);
568 msg->setObject(kSessionSpKey, mBusySession);
569 msg->setPointer(kCallbackFpKey, (void*) mBusySession->mUserSessionCallback.onReady);
570 mBusySession.clear();
571 msg->post();
572 }
573 mIdle = true;
574
575 binder::Status remoteRet = mRemote->beginConfigure();
576 if (!remoteRet.isOk()) {
577 ALOGE("Camera device %s begin configure failed: %s", getId(), remoteRet.toString8().string());
578 return ACAMERA_ERROR_UNKNOWN;
579 }
580
581 // delete to-be-deleted streams
582 for (auto streamId : deleteList) {
583 remoteRet = mRemote->deleteStream(streamId);
584 if (!remoteRet.isOk()) {
585 ALOGE("Camera device %s failed to remove stream %d: %s", getId(), streamId,
586 remoteRet.toString8().string());
587 return ACAMERA_ERROR_UNKNOWN;
588 }
589 mConfiguredOutputs.erase(streamId);
590 }
591
592 // add new streams
593 for (auto outputPair : addSet) {
594 int streamId;
595 remoteRet = mRemote->createStream(outputPair.second, &streamId);
596 if (!remoteRet.isOk()) {
597 ALOGE("Camera device %s failed to create stream: %s", getId(),
598 remoteRet.toString8().string());
599 return ACAMERA_ERROR_UNKNOWN;
600 }
601 mConfiguredOutputs.insert(std::make_pair(streamId, outputPair));
602 }
603
604 remoteRet = mRemote->endConfigure(/*isConstrainedHighSpeed*/ false);
605 if (remoteRet.serviceSpecificErrorCode() == hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT) {
606 ALOGE("Camera device %s cannnot support app output configuration: %s", getId(),
607 remoteRet.toString8().string());
608 return ACAMERA_ERROR_STREAM_CONFIGURE_FAIL;
609 } else if (!remoteRet.isOk()) {
610 ALOGE("Camera device %s end configure failed: %s", getId(), remoteRet.toString8().string());
611 return ACAMERA_ERROR_UNKNOWN;
612 }
613
614 return ACAMERA_OK;
615 }
616
617 void
setRemoteDevice(sp<hardware::camera2::ICameraDeviceUser> remote)618 CameraDevice::setRemoteDevice(sp<hardware::camera2::ICameraDeviceUser> remote) {
619 Mutex::Autolock _l(mDeviceLock);
620 mRemote = remote;
621 }
622
623 camera_status_t
checkCameraClosedOrErrorLocked() const624 CameraDevice::checkCameraClosedOrErrorLocked() const {
625 if (mRemote == nullptr) {
626 ALOGE("%s: camera device already closed", __FUNCTION__);
627 return ACAMERA_ERROR_CAMERA_DISCONNECTED;
628 }
629 if (mInError) {// triggered by onDeviceError
630 ALOGE("%s: camera device has encountered a serious error", __FUNCTION__);
631 return mError;
632 }
633 return ACAMERA_OK;
634 }
635
636 void
setCameraDeviceErrorLocked(camera_status_t error)637 CameraDevice::setCameraDeviceErrorLocked(camera_status_t error) {
638 mInError = true;
639 mError = error;
640 return;
641 }
642
643 void
updateTracker(int64_t frameNumber,bool isError)644 CameraDevice::FrameNumberTracker::updateTracker(int64_t frameNumber, bool isError) {
645 ALOGV("updateTracker frame %" PRId64 " isError %d", frameNumber, isError);
646 if (isError) {
647 mFutureErrorSet.insert(frameNumber);
648 } else if (frameNumber <= mCompletedFrameNumber) {
649 ALOGE("Frame number %" PRId64 " decreased! current fn %" PRId64,
650 frameNumber, mCompletedFrameNumber);
651 return;
652 } else {
653 if (frameNumber != mCompletedFrameNumber + 1) {
654 ALOGE("Frame number out of order. Expect %" PRId64 " but get %" PRId64,
655 mCompletedFrameNumber + 1, frameNumber);
656 // Do not assert as in java implementation
657 }
658 mCompletedFrameNumber = frameNumber;
659 }
660 update();
661 }
662
663 void
update()664 CameraDevice::FrameNumberTracker::update() {
665 for (auto it = mFutureErrorSet.begin(); it != mFutureErrorSet.end();) {
666 int64_t errorFrameNumber = *it;
667 if (errorFrameNumber == mCompletedFrameNumber + 1) {
668 mCompletedFrameNumber++;
669 it = mFutureErrorSet.erase(it);
670 } else if (errorFrameNumber <= mCompletedFrameNumber) {
671 // This should not happen, but deal with it anyway
672 ALOGE("Completd frame number passed through current frame number!");
673 // erase the old error since it's no longer useful
674 it = mFutureErrorSet.erase(it);
675 } else {
676 // Normal requests hasn't catched up error frames, just break
677 break;
678 }
679 }
680 ALOGV("Update complete frame %" PRId64, mCompletedFrameNumber);
681 }
682
683 void
onCaptureErrorLocked(int32_t errorCode,const CaptureResultExtras & resultExtras)684 CameraDevice::onCaptureErrorLocked(
685 int32_t errorCode,
686 const CaptureResultExtras& resultExtras) {
687 int sequenceId = resultExtras.requestId;
688 int64_t frameNumber = resultExtras.frameNumber;
689 int32_t burstId = resultExtras.burstId;
690 auto it = mSequenceCallbackMap.find(sequenceId);
691 if (it == mSequenceCallbackMap.end()) {
692 ALOGE("%s: Error: capture sequence index %d not found!",
693 __FUNCTION__, sequenceId);
694 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
695 return;
696 }
697
698 CallbackHolder cbh = (*it).second;
699 sp<ACameraCaptureSession> session = cbh.mSession;
700 if ((size_t) burstId >= cbh.mRequests.size()) {
701 ALOGE("%s: Error: request index %d out of bound (size %zu)",
702 __FUNCTION__, burstId, cbh.mRequests.size());
703 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
704 return;
705 }
706 sp<CaptureRequest> request = cbh.mRequests[burstId];
707
708 // Handle buffer error
709 if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER) {
710 int32_t streamId = resultExtras.errorStreamId;
711 ACameraCaptureSession_captureCallback_bufferLost onBufferLost =
712 cbh.mCallbacks.onCaptureBufferLost;
713 auto outputPairIt = mConfiguredOutputs.find(streamId);
714 if (outputPairIt == mConfiguredOutputs.end()) {
715 ALOGE("%s: Error: stream id %d does not exist", __FUNCTION__, streamId);
716 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
717 return;
718 }
719 ANativeWindow* anw = outputPairIt->second.first;
720
721 ALOGV("Camera %s Lost output buffer for ANW %p frame %" PRId64,
722 getId(), anw, frameNumber);
723
724 sp<AMessage> msg = new AMessage(kWhatCaptureBufferLost, mHandler);
725 msg->setPointer(kContextKey, cbh.mCallbacks.context);
726 msg->setObject(kSessionSpKey, session);
727 msg->setPointer(kCallbackFpKey, (void*) onBufferLost);
728 msg->setObject(kCaptureRequestKey, request);
729 msg->setPointer(kAnwKey, (void*) anw);
730 msg->setInt64(kFrameNumberKey, frameNumber);
731 msg->post();
732 } else { // Handle other capture failures
733 // Fire capture failure callback if there is one registered
734 ACameraCaptureSession_captureCallback_failed onError = cbh.mCallbacks.onCaptureFailed;
735 sp<CameraCaptureFailure> failure(new CameraCaptureFailure());
736 failure->frameNumber = frameNumber;
737 // TODO: refine this when implementing flush
738 failure->reason = CAPTURE_FAILURE_REASON_ERROR;
739 failure->sequenceId = sequenceId;
740 failure->wasImageCaptured = (errorCode ==
741 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT);
742
743 sp<AMessage> msg = new AMessage(kWhatCaptureFail, mHandler);
744 msg->setPointer(kContextKey, cbh.mCallbacks.context);
745 msg->setObject(kSessionSpKey, session);
746 msg->setPointer(kCallbackFpKey, (void*) onError);
747 msg->setObject(kCaptureRequestKey, request);
748 msg->setObject(kCaptureFailureKey, failure);
749 msg->post();
750
751 // Update tracker
752 mFrameNumberTracker.updateTracker(frameNumber, /*isError*/true);
753 checkAndFireSequenceCompleteLocked();
754 }
755 return;
756 }
757
onMessageReceived(const sp<AMessage> & msg)758 void CameraDevice::CallbackHandler::onMessageReceived(
759 const sp<AMessage> &msg) {
760 switch (msg->what()) {
761 case kWhatOnDisconnected:
762 case kWhatOnError:
763 case kWhatSessionStateCb:
764 case kWhatCaptureStart:
765 case kWhatCaptureResult:
766 case kWhatCaptureFail:
767 case kWhatCaptureSeqEnd:
768 case kWhatCaptureSeqAbort:
769 case kWhatCaptureBufferLost:
770 ALOGV("%s: Received msg %d", __FUNCTION__, msg->what());
771 break;
772 default:
773 ALOGE("%s:Error: unknown device callback %d", __FUNCTION__, msg->what());
774 return;
775 }
776 // Check the common part of all message
777 void* context;
778 bool found = msg->findPointer(kContextKey, &context);
779 if (!found) {
780 ALOGE("%s: Cannot find callback context!", __FUNCTION__);
781 return;
782 }
783 switch (msg->what()) {
784 case kWhatOnDisconnected:
785 {
786 ACameraDevice* dev;
787 found = msg->findPointer(kDeviceKey, (void**) &dev);
788 if (!found || dev == nullptr) {
789 ALOGE("%s: Cannot find device pointer!", __FUNCTION__);
790 return;
791 }
792 ACameraDevice_StateCallback onDisconnected;
793 found = msg->findPointer(kCallbackFpKey, (void**) &onDisconnected);
794 if (!found) {
795 ALOGE("%s: Cannot find onDisconnected!", __FUNCTION__);
796 return;
797 }
798 if (onDisconnected == nullptr) {
799 return;
800 }
801 (*onDisconnected)(context, dev);
802 break;
803 }
804 case kWhatOnError:
805 {
806 ACameraDevice* dev;
807 found = msg->findPointer(kDeviceKey, (void**) &dev);
808 if (!found || dev == nullptr) {
809 ALOGE("%s: Cannot find device pointer!", __FUNCTION__);
810 return;
811 }
812 ACameraDevice_ErrorStateCallback onError;
813 found = msg->findPointer(kCallbackFpKey, (void**) &onError);
814 if (!found) {
815 ALOGE("%s: Cannot find onError!", __FUNCTION__);
816 return;
817 }
818 int errorCode;
819 found = msg->findInt32(kErrorCodeKey, &errorCode);
820 if (!found) {
821 ALOGE("%s: Cannot find error code!", __FUNCTION__);
822 return;
823 }
824 if (onError == nullptr) {
825 return;
826 }
827 (*onError)(context, dev, errorCode);
828 break;
829 }
830 case kWhatSessionStateCb:
831 case kWhatCaptureStart:
832 case kWhatCaptureResult:
833 case kWhatCaptureFail:
834 case kWhatCaptureSeqEnd:
835 case kWhatCaptureSeqAbort:
836 case kWhatCaptureBufferLost:
837 {
838 sp<RefBase> obj;
839 found = msg->findObject(kSessionSpKey, &obj);
840 if (!found || obj == nullptr) {
841 ALOGE("%s: Cannot find session pointer!", __FUNCTION__);
842 return;
843 }
844 sp<ACameraCaptureSession> session(static_cast<ACameraCaptureSession*>(obj.get()));
845 sp<CaptureRequest> requestSp = nullptr;
846 switch (msg->what()) {
847 case kWhatCaptureStart:
848 case kWhatCaptureResult:
849 case kWhatCaptureFail:
850 case kWhatCaptureBufferLost:
851 found = msg->findObject(kCaptureRequestKey, &obj);
852 if (!found) {
853 ALOGE("%s: Cannot find capture request!", __FUNCTION__);
854 return;
855 }
856 requestSp = static_cast<CaptureRequest*>(obj.get());
857 break;
858 }
859
860 switch (msg->what()) {
861 case kWhatSessionStateCb:
862 {
863 ACameraCaptureSession_stateCallback onState;
864 found = msg->findPointer(kCallbackFpKey, (void**) &onState);
865 if (!found) {
866 ALOGE("%s: Cannot find state callback!", __FUNCTION__);
867 return;
868 }
869 if (onState == nullptr) {
870 return;
871 }
872 (*onState)(context, session.get());
873 break;
874 }
875 case kWhatCaptureStart:
876 {
877 ACameraCaptureSession_captureCallback_start onStart;
878 found = msg->findPointer(kCallbackFpKey, (void**) &onStart);
879 if (!found) {
880 ALOGE("%s: Cannot find capture start callback!", __FUNCTION__);
881 return;
882 }
883 if (onStart == nullptr) {
884 return;
885 }
886 int64_t timestamp;
887 found = msg->findInt64(kTimeStampKey, ×tamp);
888 if (!found) {
889 ALOGE("%s: Cannot find timestamp!", __FUNCTION__);
890 return;
891 }
892 ACaptureRequest* request = allocateACaptureRequest(requestSp);
893 (*onStart)(context, session.get(), request, timestamp);
894 freeACaptureRequest(request);
895 break;
896 }
897 case kWhatCaptureResult:
898 {
899 ACameraCaptureSession_captureCallback_result onResult;
900 found = msg->findPointer(kCallbackFpKey, (void**) &onResult);
901 if (!found) {
902 ALOGE("%s: Cannot find capture result callback!", __FUNCTION__);
903 return;
904 }
905 if (onResult == nullptr) {
906 return;
907 }
908
909 found = msg->findObject(kCaptureResultKey, &obj);
910 if (!found) {
911 ALOGE("%s: Cannot find capture result!", __FUNCTION__);
912 return;
913 }
914 sp<ACameraMetadata> result(static_cast<ACameraMetadata*>(obj.get()));
915 ACaptureRequest* request = allocateACaptureRequest(requestSp);
916 (*onResult)(context, session.get(), request, result.get());
917 freeACaptureRequest(request);
918 break;
919 }
920 case kWhatCaptureFail:
921 {
922 ACameraCaptureSession_captureCallback_failed onFail;
923 found = msg->findPointer(kCallbackFpKey, (void**) &onFail);
924 if (!found) {
925 ALOGE("%s: Cannot find capture fail callback!", __FUNCTION__);
926 return;
927 }
928 if (onFail == nullptr) {
929 return;
930 }
931
932 found = msg->findObject(kCaptureFailureKey, &obj);
933 if (!found) {
934 ALOGE("%s: Cannot find capture failure!", __FUNCTION__);
935 return;
936 }
937 sp<CameraCaptureFailure> failureSp(
938 static_cast<CameraCaptureFailure*>(obj.get()));
939 ACameraCaptureFailure* failure =
940 static_cast<ACameraCaptureFailure*>(failureSp.get());
941 ACaptureRequest* request = allocateACaptureRequest(requestSp);
942 (*onFail)(context, session.get(), request, failure);
943 freeACaptureRequest(request);
944 delete failure;
945 break;
946 }
947 case kWhatCaptureSeqEnd:
948 {
949 ACameraCaptureSession_captureCallback_sequenceEnd onSeqEnd;
950 found = msg->findPointer(kCallbackFpKey, (void**) &onSeqEnd);
951 if (!found) {
952 ALOGE("%s: Cannot find sequence end callback!", __FUNCTION__);
953 return;
954 }
955 if (onSeqEnd == nullptr) {
956 return;
957 }
958 int seqId;
959 found = msg->findInt32(kSequenceIdKey, &seqId);
960 if (!found) {
961 ALOGE("%s: Cannot find frame number!", __FUNCTION__);
962 return;
963 }
964 int64_t frameNumber;
965 found = msg->findInt64(kFrameNumberKey, &frameNumber);
966 if (!found) {
967 ALOGE("%s: Cannot find frame number!", __FUNCTION__);
968 return;
969 }
970 (*onSeqEnd)(context, session.get(), seqId, frameNumber);
971 break;
972 }
973 case kWhatCaptureSeqAbort:
974 {
975 ACameraCaptureSession_captureCallback_sequenceAbort onSeqAbort;
976 found = msg->findPointer(kCallbackFpKey, (void**) &onSeqAbort);
977 if (!found) {
978 ALOGE("%s: Cannot find sequence end callback!", __FUNCTION__);
979 return;
980 }
981 if (onSeqAbort == nullptr) {
982 return;
983 }
984 int seqId;
985 found = msg->findInt32(kSequenceIdKey, &seqId);
986 if (!found) {
987 ALOGE("%s: Cannot find frame number!", __FUNCTION__);
988 return;
989 }
990 (*onSeqAbort)(context, session.get(), seqId);
991 break;
992 }
993 case kWhatCaptureBufferLost:
994 {
995 ACameraCaptureSession_captureCallback_bufferLost onBufferLost;
996 found = msg->findPointer(kCallbackFpKey, (void**) &onBufferLost);
997 if (!found) {
998 ALOGE("%s: Cannot find buffer lost callback!", __FUNCTION__);
999 return;
1000 }
1001 if (onBufferLost == nullptr) {
1002 return;
1003 }
1004
1005 ANativeWindow* anw;
1006 found = msg->findPointer(kAnwKey, (void**) &anw);
1007 if (!found) {
1008 ALOGE("%s: Cannot find ANativeWindow!", __FUNCTION__);
1009 return;
1010 }
1011
1012 int64_t frameNumber;
1013 found = msg->findInt64(kFrameNumberKey, &frameNumber);
1014 if (!found) {
1015 ALOGE("%s: Cannot find frame number!", __FUNCTION__);
1016 return;
1017 }
1018
1019 ACaptureRequest* request = allocateACaptureRequest(requestSp);
1020 (*onBufferLost)(context, session.get(), request, anw, frameNumber);
1021 freeACaptureRequest(request);
1022 break;
1023 }
1024 }
1025 break;
1026 }
1027 }
1028 }
1029
CallbackHolder(sp<ACameraCaptureSession> session,const Vector<sp<CaptureRequest>> & requests,bool isRepeating,ACameraCaptureSession_captureCallbacks * cbs)1030 CameraDevice::CallbackHolder::CallbackHolder(
1031 sp<ACameraCaptureSession> session,
1032 const Vector<sp<CaptureRequest> >& requests,
1033 bool isRepeating,
1034 ACameraCaptureSession_captureCallbacks* cbs) :
1035 mSession(session), mRequests(requests),
1036 mIsRepeating(isRepeating), mCallbacks(fillCb(cbs)) {}
1037
1038 void
checkRepeatingSequenceCompleteLocked(const int sequenceId,const int64_t lastFrameNumber)1039 CameraDevice::checkRepeatingSequenceCompleteLocked(
1040 const int sequenceId, const int64_t lastFrameNumber) {
1041 ALOGV("Repeating seqId %d lastFrameNumer %" PRId64, sequenceId, lastFrameNumber);
1042 if (lastFrameNumber == NO_FRAMES_CAPTURED) {
1043 if (mSequenceCallbackMap.count(sequenceId) == 0) {
1044 ALOGW("No callback found for sequenceId %d", sequenceId);
1045 return;
1046 }
1047 // remove callback holder from callback map
1048 auto cbIt = mSequenceCallbackMap.find(sequenceId);
1049 CallbackHolder cbh = cbIt->second;
1050 mSequenceCallbackMap.erase(cbIt);
1051 // send seq aborted callback
1052 sp<AMessage> msg = new AMessage(kWhatCaptureSeqAbort, mHandler);
1053 msg->setPointer(kContextKey, cbh.mCallbacks.context);
1054 msg->setObject(kSessionSpKey, cbh.mSession);
1055 msg->setPointer(kCallbackFpKey, (void*) cbh.mCallbacks.onCaptureSequenceAborted);
1056 msg->setInt32(kSequenceIdKey, sequenceId);
1057 msg->post();
1058 } else {
1059 // Use mSequenceLastFrameNumberMap to track
1060 mSequenceLastFrameNumberMap.insert(std::make_pair(sequenceId, lastFrameNumber));
1061
1062 // Last frame might have arrived. Check now
1063 checkAndFireSequenceCompleteLocked();
1064 }
1065 }
1066
1067 void
checkAndFireSequenceCompleteLocked()1068 CameraDevice::checkAndFireSequenceCompleteLocked() {
1069 int64_t completedFrameNumber = mFrameNumberTracker.getCompletedFrameNumber();
1070 //std::map<int, int64_t> mSequenceLastFrameNumberMap;
1071 auto it = mSequenceLastFrameNumberMap.begin();
1072 while (it != mSequenceLastFrameNumberMap.end()) {
1073 int sequenceId = it->first;
1074 int64_t lastFrameNumber = it->second;
1075 bool seqCompleted = false;
1076 bool hasCallback = true;
1077
1078 if (mRemote == nullptr) {
1079 ALOGW("Camera %s closed while checking sequence complete", getId());
1080 return;
1081 }
1082
1083 // Check if there is callback for this sequence
1084 // This should not happen because we always register callback (with nullptr inside)
1085 if (mSequenceCallbackMap.count(sequenceId) == 0) {
1086 ALOGW("No callback found for sequenceId %d", sequenceId);
1087 hasCallback = false;
1088 }
1089
1090 if (lastFrameNumber <= completedFrameNumber) {
1091 ALOGV("seq %d reached last frame %" PRId64 ", completed %" PRId64,
1092 sequenceId, lastFrameNumber, completedFrameNumber);
1093 seqCompleted = true;
1094 }
1095
1096 if (seqCompleted && hasCallback) {
1097 // remove callback holder from callback map
1098 auto cbIt = mSequenceCallbackMap.find(sequenceId);
1099 CallbackHolder cbh = cbIt->second;
1100 mSequenceCallbackMap.erase(cbIt);
1101 // send seq complete callback
1102 sp<AMessage> msg = new AMessage(kWhatCaptureSeqEnd, mHandler);
1103 msg->setPointer(kContextKey, cbh.mCallbacks.context);
1104 msg->setObject(kSessionSpKey, cbh.mSession);
1105 msg->setPointer(kCallbackFpKey, (void*) cbh.mCallbacks.onCaptureSequenceCompleted);
1106 msg->setInt32(kSequenceIdKey, sequenceId);
1107 msg->setInt64(kFrameNumberKey, lastFrameNumber);
1108
1109 // Clear the session sp before we send out the message
1110 // This will guarantee the rare case where the message is processed
1111 // before cbh goes out of scope and causing we call the session
1112 // destructor while holding device lock
1113 cbh.mSession.clear();
1114 msg->post();
1115 }
1116
1117 // No need to track sequence complete if there is no callback registered
1118 if (seqCompleted || !hasCallback) {
1119 it = mSequenceLastFrameNumberMap.erase(it);
1120 } else {
1121 ++it;
1122 }
1123 }
1124 }
1125
1126 /**
1127 * Camera service callback implementation
1128 */
1129 binder::Status
onDeviceError(int32_t errorCode,const CaptureResultExtras & resultExtras)1130 CameraDevice::ServiceCallback::onDeviceError(
1131 int32_t errorCode,
1132 const CaptureResultExtras& resultExtras) {
1133 ALOGD("Device error received, code %d, frame number %" PRId64 ", request ID %d, subseq ID %d",
1134 errorCode, resultExtras.frameNumber, resultExtras.requestId, resultExtras.burstId);
1135 binder::Status ret = binder::Status::ok();
1136 sp<CameraDevice> dev = mDevice.promote();
1137 if (dev == nullptr) {
1138 return ret; // device has been closed
1139 }
1140
1141 Mutex::Autolock _l(dev->mDeviceLock);
1142 if (dev->mRemote == nullptr) {
1143 return ret; // device has been closed
1144 }
1145 switch (errorCode) {
1146 case ERROR_CAMERA_DISCONNECTED:
1147 {
1148 // Camera is disconnected, close the session and expect no more callbacks
1149 if (dev->mCurrentSession != nullptr) {
1150 dev->mCurrentSession->closeByDevice();
1151 dev->mCurrentSession = nullptr;
1152 }
1153 sp<AMessage> msg = new AMessage(kWhatOnDisconnected, dev->mHandler);
1154 msg->setPointer(kContextKey, dev->mAppCallbacks.context);
1155 msg->setPointer(kDeviceKey, (void*) dev->getWrapper());
1156 msg->setPointer(kCallbackFpKey, (void*) dev->mAppCallbacks.onDisconnected);
1157 msg->post();
1158 break;
1159 }
1160 default:
1161 ALOGE("Unknown error from camera device: %d", errorCode);
1162 // no break
1163 case ERROR_CAMERA_DEVICE:
1164 case ERROR_CAMERA_SERVICE:
1165 {
1166 switch (errorCode) {
1167 case ERROR_CAMERA_DEVICE:
1168 dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
1169 break;
1170 case ERROR_CAMERA_SERVICE:
1171 dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
1172 break;
1173 default:
1174 dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_UNKNOWN);
1175 break;
1176 }
1177 sp<AMessage> msg = new AMessage(kWhatOnError, dev->mHandler);
1178 msg->setPointer(kContextKey, dev->mAppCallbacks.context);
1179 msg->setPointer(kDeviceKey, (void*) dev->getWrapper());
1180 msg->setPointer(kCallbackFpKey, (void*) dev->mAppCallbacks.onError);
1181 msg->setInt32(kErrorCodeKey, errorCode);
1182 msg->post();
1183 break;
1184 }
1185 case ERROR_CAMERA_REQUEST:
1186 case ERROR_CAMERA_RESULT:
1187 case ERROR_CAMERA_BUFFER:
1188 dev->onCaptureErrorLocked(errorCode, resultExtras);
1189 break;
1190 }
1191 return ret;
1192 }
1193
1194 binder::Status
onDeviceIdle()1195 CameraDevice::ServiceCallback::onDeviceIdle() {
1196 ALOGV("Camera is now idle");
1197 binder::Status ret = binder::Status::ok();
1198 sp<CameraDevice> dev = mDevice.promote();
1199 if (dev == nullptr) {
1200 return ret; // device has been closed
1201 }
1202
1203 Mutex::Autolock _l(dev->mDeviceLock);
1204 if (dev->isClosed() || dev->mRemote == nullptr) {
1205 return ret;
1206 }
1207
1208 if (dev->mIdle) {
1209 // Already in idle state. Possibly other thread did waitUntilIdle
1210 return ret;
1211 }
1212
1213 if (dev->mCurrentSession != nullptr) {
1214 ALOGE("onDeviceIdle sending state cb");
1215 if (dev->mBusySession != dev->mCurrentSession) {
1216 ALOGE("Current session != busy session");
1217 dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
1218 return ret;
1219 }
1220 sp<AMessage> msg = new AMessage(kWhatSessionStateCb, dev->mHandler);
1221 msg->setPointer(kContextKey, dev->mBusySession->mUserSessionCallback.context);
1222 msg->setObject(kSessionSpKey, dev->mBusySession);
1223 msg->setPointer(kCallbackFpKey, (void*) dev->mBusySession->mUserSessionCallback.onReady);
1224 // Make sure we clear the sp first so the session destructor can
1225 // only happen on handler thread (where we don't hold device/session lock)
1226 dev->mBusySession.clear();
1227 msg->post();
1228 }
1229 dev->mIdle = true;
1230 dev->mFlushing = false;
1231 return ret;
1232 }
1233
1234 binder::Status
onCaptureStarted(const CaptureResultExtras & resultExtras,int64_t timestamp)1235 CameraDevice::ServiceCallback::onCaptureStarted(
1236 const CaptureResultExtras& resultExtras,
1237 int64_t timestamp) {
1238 binder::Status ret = binder::Status::ok();
1239
1240 sp<CameraDevice> dev = mDevice.promote();
1241 if (dev == nullptr) {
1242 return ret; // device has been closed
1243 }
1244 Mutex::Autolock _l(dev->mDeviceLock);
1245 if (dev->isClosed() || dev->mRemote == nullptr) {
1246 return ret;
1247 }
1248
1249 int sequenceId = resultExtras.requestId;
1250 int32_t burstId = resultExtras.burstId;
1251
1252 auto it = dev->mSequenceCallbackMap.find(sequenceId);
1253 if (it != dev->mSequenceCallbackMap.end()) {
1254 CallbackHolder cbh = (*it).second;
1255 ACameraCaptureSession_captureCallback_start onStart = cbh.mCallbacks.onCaptureStarted;
1256 sp<ACameraCaptureSession> session = cbh.mSession;
1257 if ((size_t) burstId >= cbh.mRequests.size()) {
1258 ALOGE("%s: Error: request index %d out of bound (size %zu)",
1259 __FUNCTION__, burstId, cbh.mRequests.size());
1260 dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
1261 }
1262 sp<CaptureRequest> request = cbh.mRequests[burstId];
1263 sp<AMessage> msg = new AMessage(kWhatCaptureStart, dev->mHandler);
1264 msg->setPointer(kContextKey, cbh.mCallbacks.context);
1265 msg->setObject(kSessionSpKey, session);
1266 msg->setPointer(kCallbackFpKey, (void*) onStart);
1267 msg->setObject(kCaptureRequestKey, request);
1268 msg->setInt64(kTimeStampKey, timestamp);
1269 msg->post();
1270 }
1271 return ret;
1272 }
1273
1274 binder::Status
onResultReceived(const CameraMetadata & metadata,const CaptureResultExtras & resultExtras)1275 CameraDevice::ServiceCallback::onResultReceived(
1276 const CameraMetadata& metadata,
1277 const CaptureResultExtras& resultExtras) {
1278 binder::Status ret = binder::Status::ok();
1279
1280 sp<CameraDevice> dev = mDevice.promote();
1281 if (dev == nullptr) {
1282 return ret; // device has been closed
1283 }
1284 int sequenceId = resultExtras.requestId;
1285 int64_t frameNumber = resultExtras.frameNumber;
1286 int32_t burstId = resultExtras.burstId;
1287 bool isPartialResult = (resultExtras.partialResultCount < dev->mPartialResultCount);
1288
1289 if (!isPartialResult) {
1290 ALOGV("SeqId %d frame %" PRId64 " result arrive.", sequenceId, frameNumber);
1291 }
1292
1293 Mutex::Autolock _l(dev->mDeviceLock);
1294 if (dev->mRemote == nullptr) {
1295 return ret; // device has been disconnected
1296 }
1297
1298 if (dev->isClosed()) {
1299 if (!isPartialResult) {
1300 dev->mFrameNumberTracker.updateTracker(frameNumber, /*isError*/false);
1301 }
1302 // early return to avoid callback sent to closed devices
1303 return ret;
1304 }
1305
1306 CameraMetadata metadataCopy = metadata;
1307 metadataCopy.update(ANDROID_LENS_INFO_SHADING_MAP_SIZE, dev->mShadingMapSize, /*data_count*/2);
1308 metadataCopy.update(ANDROID_SYNC_FRAME_NUMBER, &frameNumber, /*data_count*/1);
1309
1310 auto it = dev->mSequenceCallbackMap.find(sequenceId);
1311 if (it != dev->mSequenceCallbackMap.end()) {
1312 CallbackHolder cbh = (*it).second;
1313 ACameraCaptureSession_captureCallback_result onResult = isPartialResult ?
1314 cbh.mCallbacks.onCaptureProgressed :
1315 cbh.mCallbacks.onCaptureCompleted;
1316 sp<ACameraCaptureSession> session = cbh.mSession;
1317 if ((size_t) burstId >= cbh.mRequests.size()) {
1318 ALOGE("%s: Error: request index %d out of bound (size %zu)",
1319 __FUNCTION__, burstId, cbh.mRequests.size());
1320 dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
1321 }
1322 sp<CaptureRequest> request = cbh.mRequests[burstId];
1323 sp<ACameraMetadata> result(new ACameraMetadata(
1324 metadataCopy.release(), ACameraMetadata::ACM_RESULT));
1325
1326 sp<AMessage> msg = new AMessage(kWhatCaptureResult, dev->mHandler);
1327 msg->setPointer(kContextKey, cbh.mCallbacks.context);
1328 msg->setObject(kSessionSpKey, session);
1329 msg->setPointer(kCallbackFpKey, (void*) onResult);
1330 msg->setObject(kCaptureRequestKey, request);
1331 msg->setObject(kCaptureResultKey, result);
1332 msg->post();
1333 }
1334
1335 if (!isPartialResult) {
1336 dev->mFrameNumberTracker.updateTracker(frameNumber, /*isError*/false);
1337 dev->checkAndFireSequenceCompleteLocked();
1338 }
1339
1340 return ret;
1341 }
1342
1343 binder::Status
onPrepared(int)1344 CameraDevice::ServiceCallback::onPrepared(int) {
1345 // Prepare not yet implemented in NDK
1346 return binder::Status::ok();
1347 }
1348
1349 binder::Status
onRepeatingRequestError(int64_t lastFrameNumber)1350 CameraDevice::ServiceCallback::onRepeatingRequestError(int64_t lastFrameNumber) {
1351 binder::Status ret = binder::Status::ok();
1352
1353 sp<CameraDevice> dev = mDevice.promote();
1354 if (dev == nullptr) {
1355 return ret; // device has been closed
1356 }
1357
1358 Mutex::Autolock _l(dev->mDeviceLock);
1359
1360 int repeatingSequenceId = dev->mRepeatingSequenceId;
1361 dev->mRepeatingSequenceId = REQUEST_ID_NONE;
1362
1363 dev->checkRepeatingSequenceCompleteLocked(repeatingSequenceId, lastFrameNumber);
1364
1365 return ret;
1366 }
1367
1368
1369 } // namespace android
1370