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