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