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