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