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