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