• 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 <camera2/SubmitInfo.h>
24 #include <gui/Surface.h>
25 #include "ACameraDevice.h"
26 #include "ACameraMetadata.h"
27 #include "ACaptureRequest.h"
28 #include "ACameraCaptureSession.h"
29 
30 using namespace android;
31 
32 namespace android {
33 // Static member definitions
34 const char* CameraDevice::kContextKey        = "Context";
35 const char* CameraDevice::kDeviceKey         = "Device";
36 const char* CameraDevice::kErrorCodeKey      = "ErrorCode";
37 const char* CameraDevice::kCallbackFpKey     = "Callback";
38 const char* CameraDevice::kSessionSpKey      = "SessionSp";
39 const char* CameraDevice::kCaptureRequestKey = "CaptureRequest";
40 const char* CameraDevice::kTimeStampKey      = "TimeStamp";
41 const char* CameraDevice::kCaptureResultKey  = "CaptureResult";
42 const char* CameraDevice::kCaptureFailureKey = "CaptureFailure";
43 const char* CameraDevice::kSequenceIdKey     = "SequenceId";
44 const char* CameraDevice::kFrameNumberKey    = "FrameNumber";
45 const char* CameraDevice::kAnwKey            = "Anw";
46 
47 /**
48  * CameraDevice Implementation
49  */
CameraDevice(const char * id,ACameraDevice_StateCallbacks * cb,std::unique_ptr<ACameraMetadata> chars,ACameraDevice * wrapper)50 CameraDevice::CameraDevice(
51         const char* id,
52         ACameraDevice_StateCallbacks* cb,
53         std::unique_ptr<ACameraMetadata> chars,
54         ACameraDevice* wrapper) :
55         mCameraId(id),
56         mAppCallbacks(*cb),
57         mChars(std::move(chars)),
58         mServiceCallback(new ServiceCallback(this)),
59         mWrapper(wrapper),
60         mInError(false),
61         mError(ACAMERA_OK),
62         mIdle(true) {
63     mClosing = false;
64     // Setup looper thread to perfrom device callbacks to app
65     mCbLooper = new ALooper;
66     mCbLooper->setName("C2N-dev-looper");
67     status_t err = mCbLooper->start(
68             /*runOnCallingThread*/false,
69             /*canCallJava*/       true,
70             PRIORITY_DEFAULT);
71     if (err != OK) {
72         ALOGE("%s: Unable to start camera device callback looper: %s (%d)",
73                 __FUNCTION__, strerror(-err), err);
74         setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
75     }
76     mHandler = new CallbackHandler();
77     mCbLooper->registerHandler(mHandler);
78 
79     const CameraMetadata& metadata = mChars->getInternalData();
80     camera_metadata_ro_entry entry = metadata.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
81     if (entry.count != 1) {
82         ALOGW("%s: bad count %zu for partial result count", __FUNCTION__, entry.count);
83         mPartialResultCount = 1;
84     } else {
85         mPartialResultCount = entry.data.i32[0];
86     }
87 
88     entry = metadata.find(ANDROID_LENS_INFO_SHADING_MAP_SIZE);
89     if (entry.count != 2) {
90         ALOGW("%s: bad count %zu for shading map size", __FUNCTION__, entry.count);
91         mShadingMapSize[0] = 0;
92         mShadingMapSize[1] = 0;
93     } else {
94         mShadingMapSize[0] = entry.data.i32[0];
95         mShadingMapSize[1] = entry.data.i32[1];
96     }
97 }
98 
99 // Device close implementaiton
~CameraDevice()100 CameraDevice::~CameraDevice() {
101     Mutex::Autolock _l(mDeviceLock);
102     if (!isClosed()) {
103         disconnectLocked();
104     }
105     if (mCbLooper != nullptr) {
106         mCbLooper->unregisterHandler(mHandler->id());
107         mCbLooper->stop();
108     }
109     mCbLooper.clear();
110     mHandler.clear();
111 }
112 
113 // TODO: cached created request?
114 camera_status_t
createCaptureRequest(ACameraDevice_request_template templateId,ACaptureRequest ** request) const115 CameraDevice::createCaptureRequest(
116         ACameraDevice_request_template templateId,
117         ACaptureRequest** request) const {
118     Mutex::Autolock _l(mDeviceLock);
119     camera_status_t ret = checkCameraClosedOrErrorLocked();
120     if (ret != ACAMERA_OK) {
121         return ret;
122     }
123     if (mRemote == nullptr) {
124         return ACAMERA_ERROR_CAMERA_DISCONNECTED;
125     }
126     CameraMetadata rawRequest;
127     binder::Status remoteRet = mRemote->createDefaultRequest(templateId, &rawRequest);
128     if (remoteRet.serviceSpecificErrorCode() ==
129             hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT) {
130         ALOGW("Create capture request failed! template %d is not supported on this device",
131             templateId);
132         return ACAMERA_ERROR_INVALID_PARAMETER;
133     } else if (!remoteRet.isOk()) {
134         ALOGE("Create capture request failed: %s", remoteRet.toString8().string());
135         return ACAMERA_ERROR_UNKNOWN;
136     }
137     ACaptureRequest* outReq = new ACaptureRequest();
138     outReq->settings = new ACameraMetadata(rawRequest.release(), ACameraMetadata::ACM_REQUEST);
139     outReq->targets  = new ACameraOutputTargets();
140     *request = outReq;
141     return ACAMERA_OK;
142 }
143 
144 camera_status_t
createCaptureSession(const ACaptureSessionOutputContainer * outputs,const ACameraCaptureSession_stateCallbacks * callbacks,ACameraCaptureSession ** session)145 CameraDevice::createCaptureSession(
146         const ACaptureSessionOutputContainer*       outputs,
147         const ACameraCaptureSession_stateCallbacks* callbacks,
148         /*out*/ACameraCaptureSession** session) {
149     Mutex::Autolock _l(mDeviceLock);
150     camera_status_t ret = checkCameraClosedOrErrorLocked();
151     if (ret != ACAMERA_OK) {
152         return ret;
153     }
154 
155     if (mCurrentSession != nullptr) {
156         mCurrentSession->closeByDevice();
157         stopRepeatingLocked();
158     }
159 
160     // Create new session
161     ret = configureStreamsLocked(outputs);
162     if (ret != ACAMERA_OK) {
163         ALOGE("Fail to create new session. cannot configure streams");
164         return ret;
165     }
166 
167     ACameraCaptureSession* newSession = new ACameraCaptureSession(
168             mNextSessionId++, outputs, callbacks, this);
169 
170     // set new session as current session
171     newSession->incStrong((void *) ACameraDevice_createCaptureSession);
172     mCurrentSession = newSession;
173     mFlushing = false;
174     *session = newSession;
175     return ACAMERA_OK;
176 }
177 
178 camera_status_t
captureLocked(sp<ACameraCaptureSession> session,ACameraCaptureSession_captureCallbacks * cbs,int numRequests,ACaptureRequest ** requests,int * captureSequenceId)179 CameraDevice::captureLocked(
180         sp<ACameraCaptureSession> session,
181         /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
182         int numRequests, ACaptureRequest** requests,
183         /*optional*/int* captureSequenceId) {
184     return submitRequestsLocked(
185             session, cbs, numRequests, requests, captureSequenceId, /*isRepeating*/false);
186 }
187 
188 camera_status_t
setRepeatingRequestsLocked(sp<ACameraCaptureSession> session,ACameraCaptureSession_captureCallbacks * cbs,int numRequests,ACaptureRequest ** requests,int * captureSequenceId)189 CameraDevice::setRepeatingRequestsLocked(
190         sp<ACameraCaptureSession> session,
191         /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
192         int numRequests, ACaptureRequest** requests,
193         /*optional*/int* captureSequenceId) {
194     return submitRequestsLocked(
195             session, cbs, numRequests, requests, captureSequenceId, /*isRepeating*/true);
196 }
197 
198 camera_status_t
submitRequestsLocked(sp<ACameraCaptureSession> session,ACameraCaptureSession_captureCallbacks * cbs,int numRequests,ACaptureRequest ** requests,int * captureSequenceId,bool isRepeating)199 CameraDevice::submitRequestsLocked(
200         sp<ACameraCaptureSession> session,
201         /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
202         int numRequests, ACaptureRequest** requests,
203         /*optional*/int* captureSequenceId,
204         bool isRepeating) {
205     camera_status_t ret = checkCameraClosedOrErrorLocked();
206     if (ret != ACAMERA_OK) {
207         ALOGE("Camera %s submit capture request failed! ret %d", getId(), ret);
208         return ret;
209     }
210 
211     // Form two vectors of capture request, one for internal tracking
212     std::vector<hardware::camera2::CaptureRequest> requestList;
213     Vector<sp<CaptureRequest> > requestsV;
214     requestsV.setCapacity(numRequests);
215     for (int i = 0; i < numRequests; i++) {
216         sp<CaptureRequest> req;
217         ret = allocateCaptureRequest(requests[i], req);
218         if (ret != ACAMERA_OK) {
219             ALOGE("Convert capture request to internal format failure! ret %d", ret);
220             return ret;
221         }
222         if (req->mSurfaceList.empty()) {
223             ALOGE("Capture request without output target cannot be submitted!");
224             return ACAMERA_ERROR_INVALID_PARAMETER;
225         }
226         requestList.push_back(*(req.get()));
227         requestsV.push_back(req);
228     }
229 
230     if (isRepeating) {
231         ret = stopRepeatingLocked();
232         if (ret != ACAMERA_OK) {
233             ALOGE("Camera %s stop repeating failed! ret %d", getId(), ret);
234             return ret;
235         }
236     }
237 
238     binder::Status remoteRet;
239     hardware::camera2::utils::SubmitInfo info;
240     remoteRet = mRemote->submitRequestList(requestList, isRepeating, &info);
241     int sequenceId = info.mRequestId;
242     int64_t lastFrameNumber = info.mLastFrameNumber;
243     if (sequenceId < 0) {
244         ALOGE("Camera %s submit request remote failure: ret %d", getId(), sequenceId);
245         return ACAMERA_ERROR_UNKNOWN;
246     }
247 
248     CallbackHolder cbHolder(session, requestsV, isRepeating, cbs);
249     mSequenceCallbackMap.insert(std::make_pair(sequenceId, cbHolder));
250 
251     if (isRepeating) {
252         // stopRepeating above should have cleanup repeating sequence id
253         if (mRepeatingSequenceId != REQUEST_ID_NONE) {
254             setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
255             return ACAMERA_ERROR_CAMERA_DEVICE;
256         }
257         mRepeatingSequenceId = sequenceId;
258     } else {
259         mSequenceLastFrameNumberMap.insert(std::make_pair(sequenceId, lastFrameNumber));
260     }
261 
262     if (mIdle) {
263         sp<AMessage> msg = new AMessage(kWhatSessionStateCb, mHandler);
264         msg->setPointer(kContextKey, session->mUserSessionCallback.context);
265         msg->setObject(kSessionSpKey, session);
266         msg->setPointer(kCallbackFpKey, (void*) session->mUserSessionCallback.onActive);
267         msg->post();
268     }
269     mIdle = false;
270     mBusySession = session;
271 
272     if (captureSequenceId) {
273         *captureSequenceId = sequenceId;
274     }
275     return ACAMERA_OK;
276 }
277 
278 camera_status_t
allocateCaptureRequest(const ACaptureRequest * request,sp<CaptureRequest> & outReq)279 CameraDevice::allocateCaptureRequest(
280         const ACaptureRequest* request, /*out*/sp<CaptureRequest>& outReq) {
281     camera_status_t ret;
282     sp<CaptureRequest> req(new CaptureRequest());
283     req->mMetadata = request->settings->getInternalData();
284     req->mIsReprocess = false; // NDK does not support reprocessing yet
285 
286     for (auto outputTarget : request->targets->mOutputs) {
287         ANativeWindow* anw = outputTarget.mWindow;
288         sp<Surface> surface;
289         ret = getSurfaceFromANativeWindow(anw, surface);
290         if (ret != ACAMERA_OK) {
291             ALOGE("Bad output target in capture request! ret %d", ret);
292             return ret;
293         }
294         req->mSurfaceList.push_back(surface);
295     }
296     outReq = req;
297     return ACAMERA_OK;
298 }
299 
300 ACaptureRequest*
allocateACaptureRequest(sp<CaptureRequest> & req)301 CameraDevice::allocateACaptureRequest(sp<CaptureRequest>& req) {
302     ACaptureRequest* pRequest = new ACaptureRequest();
303     CameraMetadata clone = req->mMetadata;
304     pRequest->settings = new ACameraMetadata(clone.release(), ACameraMetadata::ACM_REQUEST);
305     pRequest->targets  = new ACameraOutputTargets();
306     for (size_t i = 0; i < req->mSurfaceList.size(); i++) {
307         ANativeWindow* anw = static_cast<ANativeWindow*>(req->mSurfaceList[i].get());
308         ACameraOutputTarget outputTarget(anw);
309         pRequest->targets->mOutputs.insert(outputTarget);
310     }
311     return pRequest;
312 }
313 
314 void
freeACaptureRequest(ACaptureRequest * req)315 CameraDevice::freeACaptureRequest(ACaptureRequest* req) {
316     if (req == nullptr) {
317         return;
318     }
319     delete req->settings;
320     delete req->targets;
321     delete req;
322 }
323 
324 void
notifySessionEndOfLifeLocked(ACameraCaptureSession * session)325 CameraDevice::notifySessionEndOfLifeLocked(ACameraCaptureSession* session) {
326     if (isClosed()) {
327         // Device is closing already. do nothing
328         return;
329     }
330 
331     if (session != mCurrentSession) {
332         // Session has been replaced by other seesion or device is closed
333         return;
334     }
335     mCurrentSession = nullptr;
336 
337     // Should not happen
338     if (!session->mIsClosed) {
339         ALOGE("Error: unclosed session %p reaches end of life!", session);
340         setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
341         return;
342     }
343 
344     // No new session, unconfigure now
345     camera_status_t ret = configureStreamsLocked(nullptr);
346     if (ret != ACAMERA_OK) {
347         ALOGE("Unconfigure stream failed. Device might still be configured! ret %d", ret);
348     }
349 }
350 
351 void
disconnectLocked()352 CameraDevice::disconnectLocked() {
353     if (mClosing.exchange(true)) {
354         // Already closing, just return
355         ALOGW("Camera device %s is already closing.", getId());
356         return;
357     }
358 
359     if (mRemote != nullptr) {
360         mRemote->disconnect();
361     }
362     mRemote = nullptr;
363 
364     if (mCurrentSession != nullptr) {
365         mCurrentSession->closeByDevice();
366         mCurrentSession = nullptr;
367     }
368 }
369 
370 camera_status_t
stopRepeatingLocked()371 CameraDevice::stopRepeatingLocked() {
372     camera_status_t ret = checkCameraClosedOrErrorLocked();
373     if (ret != ACAMERA_OK) {
374         ALOGE("Camera %s stop repeating failed! ret %d", getId(), ret);
375         return ret;
376     }
377     if (mRepeatingSequenceId != REQUEST_ID_NONE) {
378         int repeatingSequenceId = mRepeatingSequenceId;
379         mRepeatingSequenceId = REQUEST_ID_NONE;
380 
381         int64_t lastFrameNumber;
382         binder::Status remoteRet = mRemote->cancelRequest(repeatingSequenceId, &lastFrameNumber);
383         if (remoteRet.serviceSpecificErrorCode() ==
384                 hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT) {
385             ALOGV("Repeating request is already stopped.");
386             return ACAMERA_OK;
387         } else if (!remoteRet.isOk()) {
388             ALOGE("Stop repeating request fails in remote: %s", remoteRet.toString8().string());
389             return ACAMERA_ERROR_UNKNOWN;
390         }
391         checkRepeatingSequenceCompleteLocked(repeatingSequenceId, lastFrameNumber);
392     }
393     return ACAMERA_OK;
394 }
395 
396 camera_status_t
flushLocked(ACameraCaptureSession * session)397 CameraDevice::flushLocked(ACameraCaptureSession* session) {
398     camera_status_t ret = checkCameraClosedOrErrorLocked();
399     if (ret != ACAMERA_OK) {
400         ALOGE("Camera %s abort captures failed! ret %d", getId(), ret);
401         return ret;
402     }
403 
404     // This should never happen because creating a new session will close
405     // previous one and thus reject any API call from previous session.
406     // But still good to check here in case something unexpected happen.
407     if (session != mCurrentSession) {
408         ALOGE("Camera %s session %p is not current active session!", getId(), session);
409         return ACAMERA_ERROR_INVALID_OPERATION;
410     }
411 
412     if (mFlushing) {
413         ALOGW("Camera %s is already aborting captures", getId());
414         return ACAMERA_OK;
415     }
416 
417     mFlushing = true;
418     // Send onActive callback to guarantee there is always active->ready transition
419     sp<AMessage> msg = new AMessage(kWhatSessionStateCb, mHandler);
420     msg->setPointer(kContextKey, session->mUserSessionCallback.context);
421     msg->setObject(kSessionSpKey, session);
422     msg->setPointer(kCallbackFpKey, (void*) session->mUserSessionCallback.onActive);
423     msg->post();
424 
425     // If device is already idling, send callback and exit early
426     if (mIdle) {
427         sp<AMessage> msg = new AMessage(kWhatSessionStateCb, mHandler);
428         msg->setPointer(kContextKey, session->mUserSessionCallback.context);
429         msg->setObject(kSessionSpKey, session);
430         msg->setPointer(kCallbackFpKey, (void*) session->mUserSessionCallback.onReady);
431         msg->post();
432         mFlushing = false;
433         return ACAMERA_OK;
434     }
435 
436     int64_t lastFrameNumber;
437     binder::Status remoteRet = mRemote->flush(&lastFrameNumber);
438     if (!remoteRet.isOk()) {
439         ALOGE("Abort captures fails in remote: %s", remoteRet.toString8().string());
440         return ACAMERA_ERROR_UNKNOWN;
441     }
442     if (mRepeatingSequenceId != REQUEST_ID_NONE) {
443         checkRepeatingSequenceCompleteLocked(mRepeatingSequenceId, lastFrameNumber);
444     }
445     return ACAMERA_OK;
446 }
447 
448 camera_status_t
waitUntilIdleLocked()449 CameraDevice::waitUntilIdleLocked() {
450     camera_status_t ret = checkCameraClosedOrErrorLocked();
451     if (ret != ACAMERA_OK) {
452         ALOGE("Wait until camera %s idle failed! ret %d", getId(), ret);
453         return ret;
454     }
455 
456     if (mRepeatingSequenceId != REQUEST_ID_NONE) {
457         ALOGE("Camera device %s won't go to idle when there is repeating request!", getId());
458         return ACAMERA_ERROR_INVALID_OPERATION;
459     }
460 
461     binder::Status remoteRet = mRemote->waitUntilIdle();
462     if (!remoteRet.isOk()) {
463         ALOGE("Camera device %s waitUntilIdle failed: %s", getId(), remoteRet.toString8().string());
464         // TODO: define a function to convert status_t -> camera_status_t
465         return ACAMERA_ERROR_UNKNOWN;
466     }
467 
468     return ACAMERA_OK;
469 }
470 
471 camera_status_t
getIGBPfromAnw(ANativeWindow * anw,sp<IGraphicBufferProducer> & out)472 CameraDevice::getIGBPfromAnw(
473         ANativeWindow* anw,
474         sp<IGraphicBufferProducer>& out) {
475     if (anw == nullptr) {
476         ALOGE("Error: output ANativeWindow is null");
477         return ACAMERA_ERROR_INVALID_PARAMETER;
478     }
479     int value;
480     int err = (*anw->query)(anw, NATIVE_WINDOW_CONCRETE_TYPE, &value);
481     if (err != OK || value != NATIVE_WINDOW_SURFACE) {
482         ALOGE("Error: ANativeWindow is not backed by Surface!");
483         return ACAMERA_ERROR_INVALID_PARAMETER;
484     }
485     const sp<Surface> surface(static_cast<Surface*>(anw));
486     out = surface->getIGraphicBufferProducer();
487     return ACAMERA_OK;
488 }
489 
490 camera_status_t
getSurfaceFromANativeWindow(ANativeWindow * anw,sp<Surface> & out)491 CameraDevice::getSurfaceFromANativeWindow(
492         ANativeWindow* anw, sp<Surface>& out) {
493     if (anw == nullptr) {
494         ALOGE("Error: output ANativeWindow is null");
495         return ACAMERA_ERROR_INVALID_PARAMETER;
496     }
497     int value;
498     int err = (*anw->query)(anw, NATIVE_WINDOW_CONCRETE_TYPE, &value);
499     if (err != OK || value != NATIVE_WINDOW_SURFACE) {
500         ALOGE("Error: ANativeWindow is not backed by Surface!");
501         return ACAMERA_ERROR_INVALID_PARAMETER;
502     }
503     sp<Surface> surface(static_cast<Surface*>(anw));
504     out = surface;
505     return ACAMERA_OK;
506 }
507 
508 camera_status_t
configureStreamsLocked(const ACaptureSessionOutputContainer * outputs)509 CameraDevice::configureStreamsLocked(const ACaptureSessionOutputContainer* outputs) {
510     ACaptureSessionOutputContainer emptyOutput;
511     if (outputs == nullptr) {
512         outputs = &emptyOutput;
513     }
514 
515     camera_status_t ret = checkCameraClosedOrErrorLocked();
516     if (ret != ACAMERA_OK) {
517         return ret;
518     }
519 
520     std::set<std::pair<ANativeWindow*, OutputConfiguration>> outputSet;
521     for (auto outConfig : outputs->mOutputs) {
522         ANativeWindow* anw = outConfig.mWindow;
523         sp<IGraphicBufferProducer> iGBP(nullptr);
524         ret = getIGBPfromAnw(anw, iGBP);
525         if (ret != ACAMERA_OK) {
526             return ret;
527         }
528         outputSet.insert(std::make_pair(
529                 anw, OutputConfiguration(iGBP, outConfig.mRotation)));
530     }
531     auto addSet = outputSet;
532     std::vector<int> deleteList;
533 
534     // Determine which streams need to be created, which to be deleted
535     for (auto& kvPair : mConfiguredOutputs) {
536         int streamId = kvPair.first;
537         auto& outputPair = kvPair.second;
538         if (outputSet.count(outputPair) == 0) {
539             deleteList.push_back(streamId); // Need to delete a no longer needed stream
540         } else {
541             addSet.erase(outputPair);        // No need to add already existing stream
542         }
543     }
544 
545     ret = stopRepeatingLocked();
546     if (ret != ACAMERA_OK) {
547         ALOGE("Camera device %s stop repeating failed, ret %d", getId(), ret);
548         return ret;
549     }
550 
551     ret = waitUntilIdleLocked();
552     if (ret != ACAMERA_OK) {
553         ALOGE("Camera device %s wait until idle failed, ret %d", getId(), ret);
554         return ret;
555     }
556 
557     // Send onReady to previous session
558     // CurrentSession will be updated after configureStreamLocked, so here
559     // mCurrentSession is the session to be replaced by a new session
560     if (!mIdle && mCurrentSession != nullptr) {
561         if (mBusySession != mCurrentSession) {
562             ALOGE("Current session != busy session");
563             setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
564             return ACAMERA_ERROR_CAMERA_DEVICE;
565         }
566         sp<AMessage> msg = new AMessage(kWhatSessionStateCb, mHandler);
567         msg->setPointer(kContextKey, mBusySession->mUserSessionCallback.context);
568         msg->setObject(kSessionSpKey, mBusySession);
569         msg->setPointer(kCallbackFpKey, (void*) mBusySession->mUserSessionCallback.onReady);
570         mBusySession.clear();
571         msg->post();
572     }
573     mIdle = true;
574 
575     binder::Status remoteRet = mRemote->beginConfigure();
576     if (!remoteRet.isOk()) {
577         ALOGE("Camera device %s begin configure failed: %s", getId(), remoteRet.toString8().string());
578         return ACAMERA_ERROR_UNKNOWN;
579     }
580 
581     // delete to-be-deleted streams
582     for (auto streamId : deleteList) {
583         remoteRet = mRemote->deleteStream(streamId);
584         if (!remoteRet.isOk()) {
585             ALOGE("Camera device %s failed to remove stream %d: %s", getId(), streamId,
586                     remoteRet.toString8().string());
587             return ACAMERA_ERROR_UNKNOWN;
588         }
589         mConfiguredOutputs.erase(streamId);
590     }
591 
592     // add new streams
593     for (auto outputPair : addSet) {
594         int streamId;
595         remoteRet = mRemote->createStream(outputPair.second, &streamId);
596         if (!remoteRet.isOk()) {
597             ALOGE("Camera device %s failed to create stream: %s", getId(),
598                     remoteRet.toString8().string());
599             return ACAMERA_ERROR_UNKNOWN;
600         }
601         mConfiguredOutputs.insert(std::make_pair(streamId, outputPair));
602     }
603 
604     remoteRet = mRemote->endConfigure(/*isConstrainedHighSpeed*/ false);
605     if (remoteRet.serviceSpecificErrorCode() == hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT) {
606         ALOGE("Camera device %s cannnot support app output configuration: %s", getId(),
607                 remoteRet.toString8().string());
608         return ACAMERA_ERROR_STREAM_CONFIGURE_FAIL;
609     } else if (!remoteRet.isOk()) {
610         ALOGE("Camera device %s end configure failed: %s", getId(), remoteRet.toString8().string());
611         return ACAMERA_ERROR_UNKNOWN;
612     }
613 
614     return ACAMERA_OK;
615 }
616 
617 void
setRemoteDevice(sp<hardware::camera2::ICameraDeviceUser> remote)618 CameraDevice::setRemoteDevice(sp<hardware::camera2::ICameraDeviceUser> remote) {
619     Mutex::Autolock _l(mDeviceLock);
620     mRemote = remote;
621 }
622 
623 camera_status_t
checkCameraClosedOrErrorLocked() const624 CameraDevice::checkCameraClosedOrErrorLocked() const {
625     if (mRemote == nullptr) {
626         ALOGE("%s: camera device already closed", __FUNCTION__);
627         return ACAMERA_ERROR_CAMERA_DISCONNECTED;
628     }
629     if (mInError) {// triggered by onDeviceError
630         ALOGE("%s: camera device has encountered a serious error", __FUNCTION__);
631         return mError;
632     }
633     return ACAMERA_OK;
634 }
635 
636 void
setCameraDeviceErrorLocked(camera_status_t error)637 CameraDevice::setCameraDeviceErrorLocked(camera_status_t error) {
638     mInError = true;
639     mError = error;
640     return;
641 }
642 
643 void
updateTracker(int64_t frameNumber,bool isError)644 CameraDevice::FrameNumberTracker::updateTracker(int64_t frameNumber, bool isError) {
645     ALOGV("updateTracker frame %" PRId64 " isError %d", frameNumber, isError);
646     if (isError) {
647         mFutureErrorSet.insert(frameNumber);
648     } else if (frameNumber <= mCompletedFrameNumber) {
649         ALOGE("Frame number %" PRId64 " decreased! current fn %" PRId64,
650                 frameNumber, mCompletedFrameNumber);
651         return;
652     } else {
653         if (frameNumber != mCompletedFrameNumber + 1) {
654             ALOGE("Frame number out of order. Expect %" PRId64 " but get %" PRId64,
655                     mCompletedFrameNumber + 1, frameNumber);
656             // Do not assert as in java implementation
657         }
658         mCompletedFrameNumber = frameNumber;
659     }
660     update();
661 }
662 
663 void
update()664 CameraDevice::FrameNumberTracker::update() {
665     for (auto it = mFutureErrorSet.begin(); it != mFutureErrorSet.end();) {
666         int64_t errorFrameNumber = *it;
667         if (errorFrameNumber == mCompletedFrameNumber + 1) {
668             mCompletedFrameNumber++;
669             it = mFutureErrorSet.erase(it);
670         } else if (errorFrameNumber <= mCompletedFrameNumber) {
671             // This should not happen, but deal with it anyway
672             ALOGE("Completd frame number passed through current frame number!");
673             // erase the old error since it's no longer useful
674             it = mFutureErrorSet.erase(it);
675         } else {
676             // Normal requests hasn't catched up error frames, just break
677             break;
678         }
679     }
680     ALOGV("Update complete frame %" PRId64, mCompletedFrameNumber);
681 }
682 
683 void
onCaptureErrorLocked(int32_t errorCode,const CaptureResultExtras & resultExtras)684 CameraDevice::onCaptureErrorLocked(
685         int32_t errorCode,
686         const CaptureResultExtras& resultExtras) {
687     int sequenceId = resultExtras.requestId;
688     int64_t frameNumber = resultExtras.frameNumber;
689     int32_t burstId = resultExtras.burstId;
690     auto it = mSequenceCallbackMap.find(sequenceId);
691     if (it == mSequenceCallbackMap.end()) {
692         ALOGE("%s: Error: capture sequence index %d not found!",
693                 __FUNCTION__, sequenceId);
694         setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
695         return;
696     }
697 
698     CallbackHolder cbh = (*it).second;
699     sp<ACameraCaptureSession> session = cbh.mSession;
700     if ((size_t) burstId >= cbh.mRequests.size()) {
701         ALOGE("%s: Error: request index %d out of bound (size %zu)",
702                 __FUNCTION__, burstId, cbh.mRequests.size());
703         setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
704         return;
705     }
706     sp<CaptureRequest> request = cbh.mRequests[burstId];
707 
708     // Handle buffer error
709     if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER) {
710         int32_t streamId = resultExtras.errorStreamId;
711         ACameraCaptureSession_captureCallback_bufferLost onBufferLost =
712                 cbh.mCallbacks.onCaptureBufferLost;
713         auto outputPairIt = mConfiguredOutputs.find(streamId);
714         if (outputPairIt == mConfiguredOutputs.end()) {
715             ALOGE("%s: Error: stream id %d does not exist", __FUNCTION__, streamId);
716             setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
717             return;
718         }
719         ANativeWindow* anw = outputPairIt->second.first;
720 
721         ALOGV("Camera %s Lost output buffer for ANW %p frame %" PRId64,
722                 getId(), anw, frameNumber);
723 
724         sp<AMessage> msg = new AMessage(kWhatCaptureBufferLost, mHandler);
725         msg->setPointer(kContextKey, cbh.mCallbacks.context);
726         msg->setObject(kSessionSpKey, session);
727         msg->setPointer(kCallbackFpKey, (void*) onBufferLost);
728         msg->setObject(kCaptureRequestKey, request);
729         msg->setPointer(kAnwKey, (void*) anw);
730         msg->setInt64(kFrameNumberKey, frameNumber);
731         msg->post();
732     } else { // Handle other capture failures
733         // Fire capture failure callback if there is one registered
734         ACameraCaptureSession_captureCallback_failed onError = cbh.mCallbacks.onCaptureFailed;
735         sp<CameraCaptureFailure> failure(new CameraCaptureFailure());
736         failure->frameNumber = frameNumber;
737         // TODO: refine this when implementing flush
738         failure->reason      = CAPTURE_FAILURE_REASON_ERROR;
739         failure->sequenceId  = sequenceId;
740         failure->wasImageCaptured = (errorCode ==
741                 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT);
742 
743         sp<AMessage> msg = new AMessage(kWhatCaptureFail, mHandler);
744         msg->setPointer(kContextKey, cbh.mCallbacks.context);
745         msg->setObject(kSessionSpKey, session);
746         msg->setPointer(kCallbackFpKey, (void*) onError);
747         msg->setObject(kCaptureRequestKey, request);
748         msg->setObject(kCaptureFailureKey, failure);
749         msg->post();
750 
751         // Update tracker
752         mFrameNumberTracker.updateTracker(frameNumber, /*isError*/true);
753         checkAndFireSequenceCompleteLocked();
754     }
755     return;
756 }
757 
onMessageReceived(const sp<AMessage> & msg)758 void CameraDevice::CallbackHandler::onMessageReceived(
759         const sp<AMessage> &msg) {
760     switch (msg->what()) {
761         case kWhatOnDisconnected:
762         case kWhatOnError:
763         case kWhatSessionStateCb:
764         case kWhatCaptureStart:
765         case kWhatCaptureResult:
766         case kWhatCaptureFail:
767         case kWhatCaptureSeqEnd:
768         case kWhatCaptureSeqAbort:
769         case kWhatCaptureBufferLost:
770             ALOGV("%s: Received msg %d", __FUNCTION__, msg->what());
771             break;
772         default:
773             ALOGE("%s:Error: unknown device callback %d", __FUNCTION__, msg->what());
774             return;
775     }
776     // Check the common part of all message
777     void* context;
778     bool found = msg->findPointer(kContextKey, &context);
779     if (!found) {
780         ALOGE("%s: Cannot find callback context!", __FUNCTION__);
781         return;
782     }
783     switch (msg->what()) {
784         case kWhatOnDisconnected:
785         {
786             ACameraDevice* dev;
787             found = msg->findPointer(kDeviceKey, (void**) &dev);
788             if (!found || dev == nullptr) {
789                 ALOGE("%s: Cannot find device pointer!", __FUNCTION__);
790                 return;
791             }
792             ACameraDevice_StateCallback onDisconnected;
793             found = msg->findPointer(kCallbackFpKey, (void**) &onDisconnected);
794             if (!found) {
795                 ALOGE("%s: Cannot find onDisconnected!", __FUNCTION__);
796                 return;
797             }
798             if (onDisconnected == nullptr) {
799                 return;
800             }
801             (*onDisconnected)(context, dev);
802             break;
803         }
804         case kWhatOnError:
805         {
806             ACameraDevice* dev;
807             found = msg->findPointer(kDeviceKey, (void**) &dev);
808             if (!found || dev == nullptr) {
809                 ALOGE("%s: Cannot find device pointer!", __FUNCTION__);
810                 return;
811             }
812             ACameraDevice_ErrorStateCallback onError;
813             found = msg->findPointer(kCallbackFpKey, (void**) &onError);
814             if (!found) {
815                 ALOGE("%s: Cannot find onError!", __FUNCTION__);
816                 return;
817             }
818             int errorCode;
819             found = msg->findInt32(kErrorCodeKey, &errorCode);
820             if (!found) {
821                 ALOGE("%s: Cannot find error code!", __FUNCTION__);
822                 return;
823             }
824             if (onError == nullptr) {
825                 return;
826             }
827             (*onError)(context, dev, errorCode);
828             break;
829         }
830         case kWhatSessionStateCb:
831         case kWhatCaptureStart:
832         case kWhatCaptureResult:
833         case kWhatCaptureFail:
834         case kWhatCaptureSeqEnd:
835         case kWhatCaptureSeqAbort:
836         case kWhatCaptureBufferLost:
837         {
838             sp<RefBase> obj;
839             found = msg->findObject(kSessionSpKey, &obj);
840             if (!found || obj == nullptr) {
841                 ALOGE("%s: Cannot find session pointer!", __FUNCTION__);
842                 return;
843             }
844             sp<ACameraCaptureSession> session(static_cast<ACameraCaptureSession*>(obj.get()));
845             sp<CaptureRequest> requestSp = nullptr;
846             switch (msg->what()) {
847                 case kWhatCaptureStart:
848                 case kWhatCaptureResult:
849                 case kWhatCaptureFail:
850                 case kWhatCaptureBufferLost:
851                     found = msg->findObject(kCaptureRequestKey, &obj);
852                     if (!found) {
853                         ALOGE("%s: Cannot find capture request!", __FUNCTION__);
854                         return;
855                     }
856                     requestSp = static_cast<CaptureRequest*>(obj.get());
857                     break;
858             }
859 
860             switch (msg->what()) {
861                 case kWhatSessionStateCb:
862                 {
863                     ACameraCaptureSession_stateCallback onState;
864                     found = msg->findPointer(kCallbackFpKey, (void**) &onState);
865                     if (!found) {
866                         ALOGE("%s: Cannot find state callback!", __FUNCTION__);
867                         return;
868                     }
869                     if (onState == nullptr) {
870                         return;
871                     }
872                     (*onState)(context, session.get());
873                     break;
874                 }
875                 case kWhatCaptureStart:
876                 {
877                     ACameraCaptureSession_captureCallback_start onStart;
878                     found = msg->findPointer(kCallbackFpKey, (void**) &onStart);
879                     if (!found) {
880                         ALOGE("%s: Cannot find capture start callback!", __FUNCTION__);
881                         return;
882                     }
883                     if (onStart == nullptr) {
884                         return;
885                     }
886                     int64_t timestamp;
887                     found = msg->findInt64(kTimeStampKey, &timestamp);
888                     if (!found) {
889                         ALOGE("%s: Cannot find timestamp!", __FUNCTION__);
890                         return;
891                     }
892                     ACaptureRequest* request = allocateACaptureRequest(requestSp);
893                     (*onStart)(context, session.get(), request, timestamp);
894                     freeACaptureRequest(request);
895                     break;
896                 }
897                 case kWhatCaptureResult:
898                 {
899                     ACameraCaptureSession_captureCallback_result onResult;
900                     found = msg->findPointer(kCallbackFpKey, (void**) &onResult);
901                     if (!found) {
902                         ALOGE("%s: Cannot find capture result callback!", __FUNCTION__);
903                         return;
904                     }
905                     if (onResult == nullptr) {
906                         return;
907                     }
908 
909                     found = msg->findObject(kCaptureResultKey, &obj);
910                     if (!found) {
911                         ALOGE("%s: Cannot find capture result!", __FUNCTION__);
912                         return;
913                     }
914                     sp<ACameraMetadata> result(static_cast<ACameraMetadata*>(obj.get()));
915                     ACaptureRequest* request = allocateACaptureRequest(requestSp);
916                     (*onResult)(context, session.get(), request, result.get());
917                     freeACaptureRequest(request);
918                     break;
919                 }
920                 case kWhatCaptureFail:
921                 {
922                     ACameraCaptureSession_captureCallback_failed onFail;
923                     found = msg->findPointer(kCallbackFpKey, (void**) &onFail);
924                     if (!found) {
925                         ALOGE("%s: Cannot find capture fail callback!", __FUNCTION__);
926                         return;
927                     }
928                     if (onFail == nullptr) {
929                         return;
930                     }
931 
932                     found = msg->findObject(kCaptureFailureKey, &obj);
933                     if (!found) {
934                         ALOGE("%s: Cannot find capture failure!", __FUNCTION__);
935                         return;
936                     }
937                     sp<CameraCaptureFailure> failureSp(
938                             static_cast<CameraCaptureFailure*>(obj.get()));
939                     ACameraCaptureFailure* failure =
940                             static_cast<ACameraCaptureFailure*>(failureSp.get());
941                     ACaptureRequest* request = allocateACaptureRequest(requestSp);
942                     (*onFail)(context, session.get(), request, failure);
943                     freeACaptureRequest(request);
944                     break;
945                 }
946                 case kWhatCaptureSeqEnd:
947                 {
948                     ACameraCaptureSession_captureCallback_sequenceEnd onSeqEnd;
949                     found = msg->findPointer(kCallbackFpKey, (void**) &onSeqEnd);
950                     if (!found) {
951                         ALOGE("%s: Cannot find sequence end callback!", __FUNCTION__);
952                         return;
953                     }
954                     if (onSeqEnd == nullptr) {
955                         return;
956                     }
957                     int seqId;
958                     found = msg->findInt32(kSequenceIdKey, &seqId);
959                     if (!found) {
960                         ALOGE("%s: Cannot find frame number!", __FUNCTION__);
961                         return;
962                     }
963                     int64_t frameNumber;
964                     found = msg->findInt64(kFrameNumberKey, &frameNumber);
965                     if (!found) {
966                         ALOGE("%s: Cannot find frame number!", __FUNCTION__);
967                         return;
968                     }
969                     (*onSeqEnd)(context, session.get(), seqId, frameNumber);
970                     break;
971                 }
972                 case kWhatCaptureSeqAbort:
973                 {
974                     ACameraCaptureSession_captureCallback_sequenceAbort onSeqAbort;
975                     found = msg->findPointer(kCallbackFpKey, (void**) &onSeqAbort);
976                     if (!found) {
977                         ALOGE("%s: Cannot find sequence end callback!", __FUNCTION__);
978                         return;
979                     }
980                     if (onSeqAbort == nullptr) {
981                         return;
982                     }
983                     int seqId;
984                     found = msg->findInt32(kSequenceIdKey, &seqId);
985                     if (!found) {
986                         ALOGE("%s: Cannot find frame number!", __FUNCTION__);
987                         return;
988                     }
989                     (*onSeqAbort)(context, session.get(), seqId);
990                     break;
991                 }
992                 case kWhatCaptureBufferLost:
993                 {
994                     ACameraCaptureSession_captureCallback_bufferLost onBufferLost;
995                     found = msg->findPointer(kCallbackFpKey, (void**) &onBufferLost);
996                     if (!found) {
997                         ALOGE("%s: Cannot find buffer lost callback!", __FUNCTION__);
998                         return;
999                     }
1000                     if (onBufferLost == nullptr) {
1001                         return;
1002                     }
1003 
1004                     ANativeWindow* anw;
1005                     found = msg->findPointer(kAnwKey, (void**) &anw);
1006                     if (!found) {
1007                         ALOGE("%s: Cannot find ANativeWindow!", __FUNCTION__);
1008                         return;
1009                     }
1010 
1011                     int64_t frameNumber;
1012                     found = msg->findInt64(kFrameNumberKey, &frameNumber);
1013                     if (!found) {
1014                         ALOGE("%s: Cannot find frame number!", __FUNCTION__);
1015                         return;
1016                     }
1017 
1018                     ACaptureRequest* request = allocateACaptureRequest(requestSp);
1019                     (*onBufferLost)(context, session.get(), request, anw, frameNumber);
1020                     freeACaptureRequest(request);
1021                     break;
1022                 }
1023             }
1024             break;
1025         }
1026     }
1027 }
1028 
CallbackHolder(sp<ACameraCaptureSession> session,const Vector<sp<CaptureRequest>> & requests,bool isRepeating,ACameraCaptureSession_captureCallbacks * cbs)1029 CameraDevice::CallbackHolder::CallbackHolder(
1030     sp<ACameraCaptureSession>          session,
1031     const Vector<sp<CaptureRequest> >& requests,
1032     bool                               isRepeating,
1033     ACameraCaptureSession_captureCallbacks* cbs) :
1034     mSession(session), mRequests(requests),
1035     mIsRepeating(isRepeating), mCallbacks(fillCb(cbs)) {}
1036 
1037 void
checkRepeatingSequenceCompleteLocked(const int sequenceId,const int64_t lastFrameNumber)1038 CameraDevice::checkRepeatingSequenceCompleteLocked(
1039     const int sequenceId, const int64_t lastFrameNumber) {
1040     ALOGV("Repeating seqId %d lastFrameNumer %" PRId64, sequenceId, lastFrameNumber);
1041     if (lastFrameNumber == NO_FRAMES_CAPTURED) {
1042         if (mSequenceCallbackMap.count(sequenceId) == 0) {
1043             ALOGW("No callback found for sequenceId %d", sequenceId);
1044             return;
1045         }
1046         // remove callback holder from callback map
1047         auto cbIt = mSequenceCallbackMap.find(sequenceId);
1048         CallbackHolder cbh = cbIt->second;
1049         mSequenceCallbackMap.erase(cbIt);
1050         // send seq aborted callback
1051         sp<AMessage> msg = new AMessage(kWhatCaptureSeqAbort, mHandler);
1052         msg->setPointer(kContextKey, cbh.mCallbacks.context);
1053         msg->setObject(kSessionSpKey, cbh.mSession);
1054         msg->setPointer(kCallbackFpKey, (void*) cbh.mCallbacks.onCaptureSequenceAborted);
1055         msg->setInt32(kSequenceIdKey, sequenceId);
1056         msg->post();
1057     } else {
1058         // Use mSequenceLastFrameNumberMap to track
1059         mSequenceLastFrameNumberMap.insert(std::make_pair(sequenceId, lastFrameNumber));
1060 
1061         // Last frame might have arrived. Check now
1062         checkAndFireSequenceCompleteLocked();
1063     }
1064 }
1065 
1066 void
checkAndFireSequenceCompleteLocked()1067 CameraDevice::checkAndFireSequenceCompleteLocked() {
1068     int64_t completedFrameNumber = mFrameNumberTracker.getCompletedFrameNumber();
1069     //std::map<int, int64_t> mSequenceLastFrameNumberMap;
1070     auto it = mSequenceLastFrameNumberMap.begin();
1071     while (it != mSequenceLastFrameNumberMap.end()) {
1072         int sequenceId = it->first;
1073         int64_t lastFrameNumber = it->second;
1074         bool seqCompleted = false;
1075         bool hasCallback  = true;
1076 
1077         if (mRemote == nullptr) {
1078             ALOGW("Camera %s closed while checking sequence complete", getId());
1079             return;
1080         }
1081 
1082         // Check if there is callback for this sequence
1083         // This should not happen because we always register callback (with nullptr inside)
1084         if (mSequenceCallbackMap.count(sequenceId) == 0) {
1085             ALOGW("No callback found for sequenceId %d", sequenceId);
1086             hasCallback = false;
1087         }
1088 
1089         if (lastFrameNumber <= completedFrameNumber) {
1090             ALOGV("seq %d reached last frame %" PRId64 ", completed %" PRId64,
1091                     sequenceId, lastFrameNumber, completedFrameNumber);
1092             seqCompleted = true;
1093         }
1094 
1095         if (seqCompleted && hasCallback) {
1096             // remove callback holder from callback map
1097             auto cbIt = mSequenceCallbackMap.find(sequenceId);
1098             CallbackHolder cbh = cbIt->second;
1099             mSequenceCallbackMap.erase(cbIt);
1100             // send seq complete callback
1101             sp<AMessage> msg = new AMessage(kWhatCaptureSeqEnd, mHandler);
1102             msg->setPointer(kContextKey, cbh.mCallbacks.context);
1103             msg->setObject(kSessionSpKey, cbh.mSession);
1104             msg->setPointer(kCallbackFpKey, (void*) cbh.mCallbacks.onCaptureSequenceCompleted);
1105             msg->setInt32(kSequenceIdKey, sequenceId);
1106             msg->setInt64(kFrameNumberKey, lastFrameNumber);
1107 
1108             // Clear the session sp before we send out the message
1109             // This will guarantee the rare case where the message is processed
1110             // before cbh goes out of scope and causing we call the session
1111             // destructor while holding device lock
1112             cbh.mSession.clear();
1113             msg->post();
1114         }
1115 
1116         // No need to track sequence complete if there is no callback registered
1117         if (seqCompleted || !hasCallback) {
1118             it = mSequenceLastFrameNumberMap.erase(it);
1119         } else {
1120             ++it;
1121         }
1122     }
1123 }
1124 
1125 /**
1126   * Camera service callback implementation
1127   */
1128 binder::Status
onDeviceError(int32_t errorCode,const CaptureResultExtras & resultExtras)1129 CameraDevice::ServiceCallback::onDeviceError(
1130         int32_t errorCode,
1131         const CaptureResultExtras& resultExtras) {
1132     ALOGD("Device error received, code %d, frame number %" PRId64 ", request ID %d, subseq ID %d",
1133             errorCode, resultExtras.frameNumber, resultExtras.requestId, resultExtras.burstId);
1134     binder::Status ret = binder::Status::ok();
1135     sp<CameraDevice> dev = mDevice.promote();
1136     if (dev == nullptr) {
1137         return ret; // device has been closed
1138     }
1139 
1140     Mutex::Autolock _l(dev->mDeviceLock);
1141     if (dev->mRemote == nullptr) {
1142         return ret; // device has been closed
1143     }
1144     switch (errorCode) {
1145         case ERROR_CAMERA_DISCONNECTED:
1146         {
1147             // Camera is disconnected, close the session and expect no more callbacks
1148             if (dev->mCurrentSession != nullptr) {
1149                 dev->mCurrentSession->closeByDevice();
1150                 dev->mCurrentSession = nullptr;
1151             }
1152             sp<AMessage> msg = new AMessage(kWhatOnDisconnected, dev->mHandler);
1153             msg->setPointer(kContextKey, dev->mAppCallbacks.context);
1154             msg->setPointer(kDeviceKey, (void*) dev->getWrapper());
1155             msg->setPointer(kCallbackFpKey, (void*) dev->mAppCallbacks.onDisconnected);
1156             msg->post();
1157             break;
1158         }
1159         default:
1160             ALOGE("Unknown error from camera device: %d", errorCode);
1161             // no break
1162         case ERROR_CAMERA_DEVICE:
1163         case ERROR_CAMERA_SERVICE:
1164         {
1165             switch (errorCode) {
1166                 case ERROR_CAMERA_DEVICE:
1167                     dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
1168                     break;
1169                 case ERROR_CAMERA_SERVICE:
1170                     dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
1171                     break;
1172                 default:
1173                     dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_UNKNOWN);
1174                     break;
1175             }
1176             sp<AMessage> msg = new AMessage(kWhatOnError, dev->mHandler);
1177             msg->setPointer(kContextKey, dev->mAppCallbacks.context);
1178             msg->setPointer(kDeviceKey, (void*) dev->getWrapper());
1179             msg->setPointer(kCallbackFpKey, (void*) dev->mAppCallbacks.onError);
1180             msg->setInt32(kErrorCodeKey, errorCode);
1181             msg->post();
1182             break;
1183         }
1184         case ERROR_CAMERA_REQUEST:
1185         case ERROR_CAMERA_RESULT:
1186         case ERROR_CAMERA_BUFFER:
1187             dev->onCaptureErrorLocked(errorCode, resultExtras);
1188             break;
1189     }
1190     return ret;
1191 }
1192 
1193 binder::Status
onDeviceIdle()1194 CameraDevice::ServiceCallback::onDeviceIdle() {
1195     ALOGV("Camera is now idle");
1196     binder::Status ret = binder::Status::ok();
1197     sp<CameraDevice> dev = mDevice.promote();
1198     if (dev == nullptr) {
1199         return ret; // device has been closed
1200     }
1201 
1202     Mutex::Autolock _l(dev->mDeviceLock);
1203     if (dev->isClosed() || dev->mRemote == nullptr) {
1204         return ret;
1205     }
1206 
1207     if (dev->mIdle) {
1208         // Already in idle state. Possibly other thread did waitUntilIdle
1209         return ret;
1210     }
1211 
1212     if (dev->mCurrentSession != nullptr) {
1213         ALOGE("onDeviceIdle sending state cb");
1214         if (dev->mBusySession != dev->mCurrentSession) {
1215             ALOGE("Current session != busy session");
1216             dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
1217             return ret;
1218         }
1219         sp<AMessage> msg = new AMessage(kWhatSessionStateCb, dev->mHandler);
1220         msg->setPointer(kContextKey, dev->mBusySession->mUserSessionCallback.context);
1221         msg->setObject(kSessionSpKey, dev->mBusySession);
1222         msg->setPointer(kCallbackFpKey, (void*) dev->mBusySession->mUserSessionCallback.onReady);
1223         // Make sure we clear the sp first so the session destructor can
1224         // only happen on handler thread (where we don't hold device/session lock)
1225         dev->mBusySession.clear();
1226         msg->post();
1227     }
1228     dev->mIdle = true;
1229     dev->mFlushing = false;
1230     return ret;
1231 }
1232 
1233 binder::Status
onCaptureStarted(const CaptureResultExtras & resultExtras,int64_t timestamp)1234 CameraDevice::ServiceCallback::onCaptureStarted(
1235         const CaptureResultExtras& resultExtras,
1236         int64_t timestamp) {
1237     binder::Status ret = binder::Status::ok();
1238 
1239     sp<CameraDevice> dev = mDevice.promote();
1240     if (dev == nullptr) {
1241         return ret; // device has been closed
1242     }
1243     Mutex::Autolock _l(dev->mDeviceLock);
1244     if (dev->isClosed() || dev->mRemote == nullptr) {
1245         return ret;
1246     }
1247 
1248     int sequenceId = resultExtras.requestId;
1249     int32_t burstId = resultExtras.burstId;
1250 
1251     auto it = dev->mSequenceCallbackMap.find(sequenceId);
1252     if (it != dev->mSequenceCallbackMap.end()) {
1253         CallbackHolder cbh = (*it).second;
1254         ACameraCaptureSession_captureCallback_start onStart = cbh.mCallbacks.onCaptureStarted;
1255         sp<ACameraCaptureSession> session = cbh.mSession;
1256         if ((size_t) burstId >= cbh.mRequests.size()) {
1257             ALOGE("%s: Error: request index %d out of bound (size %zu)",
1258                     __FUNCTION__, burstId, cbh.mRequests.size());
1259             dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
1260         }
1261         sp<CaptureRequest> request = cbh.mRequests[burstId];
1262         sp<AMessage> msg = new AMessage(kWhatCaptureStart, dev->mHandler);
1263         msg->setPointer(kContextKey, cbh.mCallbacks.context);
1264         msg->setObject(kSessionSpKey, session);
1265         msg->setPointer(kCallbackFpKey, (void*) onStart);
1266         msg->setObject(kCaptureRequestKey, request);
1267         msg->setInt64(kTimeStampKey, timestamp);
1268         msg->post();
1269     }
1270     return ret;
1271 }
1272 
1273 binder::Status
onResultReceived(const CameraMetadata & metadata,const CaptureResultExtras & resultExtras)1274 CameraDevice::ServiceCallback::onResultReceived(
1275         const CameraMetadata& metadata,
1276         const CaptureResultExtras& resultExtras) {
1277     binder::Status ret = binder::Status::ok();
1278 
1279     sp<CameraDevice> dev = mDevice.promote();
1280     if (dev == nullptr) {
1281         return ret; // device has been closed
1282     }
1283     int sequenceId = resultExtras.requestId;
1284     int64_t frameNumber = resultExtras.frameNumber;
1285     int32_t burstId = resultExtras.burstId;
1286     bool    isPartialResult = (resultExtras.partialResultCount < dev->mPartialResultCount);
1287 
1288     if (!isPartialResult) {
1289         ALOGV("SeqId %d frame %" PRId64 " result arrive.", sequenceId, frameNumber);
1290     }
1291 
1292     Mutex::Autolock _l(dev->mDeviceLock);
1293     if (dev->mRemote == nullptr) {
1294         return ret; // device has been disconnected
1295     }
1296 
1297     if (dev->isClosed()) {
1298         if (!isPartialResult) {
1299             dev->mFrameNumberTracker.updateTracker(frameNumber, /*isError*/false);
1300         }
1301         // early return to avoid callback sent to closed devices
1302         return ret;
1303     }
1304 
1305     CameraMetadata metadataCopy = metadata;
1306     metadataCopy.update(ANDROID_LENS_INFO_SHADING_MAP_SIZE, dev->mShadingMapSize, /*data_count*/2);
1307     metadataCopy.update(ANDROID_SYNC_FRAME_NUMBER, &frameNumber, /*data_count*/1);
1308 
1309     auto it = dev->mSequenceCallbackMap.find(sequenceId);
1310     if (it != dev->mSequenceCallbackMap.end()) {
1311         CallbackHolder cbh = (*it).second;
1312         ACameraCaptureSession_captureCallback_result onResult = isPartialResult ?
1313                 cbh.mCallbacks.onCaptureProgressed :
1314                 cbh.mCallbacks.onCaptureCompleted;
1315         sp<ACameraCaptureSession> session = cbh.mSession;
1316         if ((size_t) burstId >= cbh.mRequests.size()) {
1317             ALOGE("%s: Error: request index %d out of bound (size %zu)",
1318                     __FUNCTION__, burstId, cbh.mRequests.size());
1319             dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
1320         }
1321         sp<CaptureRequest> request = cbh.mRequests[burstId];
1322         sp<ACameraMetadata> result(new ACameraMetadata(
1323                 metadataCopy.release(), ACameraMetadata::ACM_RESULT));
1324 
1325         sp<AMessage> msg = new AMessage(kWhatCaptureResult, dev->mHandler);
1326         msg->setPointer(kContextKey, cbh.mCallbacks.context);
1327         msg->setObject(kSessionSpKey, session);
1328         msg->setPointer(kCallbackFpKey, (void*) onResult);
1329         msg->setObject(kCaptureRequestKey, request);
1330         msg->setObject(kCaptureResultKey, result);
1331         msg->post();
1332     }
1333 
1334     if (!isPartialResult) {
1335         dev->mFrameNumberTracker.updateTracker(frameNumber, /*isError*/false);
1336         dev->checkAndFireSequenceCompleteLocked();
1337     }
1338 
1339     return ret;
1340 }
1341 
1342 binder::Status
onPrepared(int)1343 CameraDevice::ServiceCallback::onPrepared(int) {
1344     // Prepare not yet implemented in NDK
1345     return binder::Status::ok();
1346 }
1347 
1348 binder::Status
onRequestQueueEmpty()1349 CameraDevice::ServiceCallback::onRequestQueueEmpty() {
1350     // onRequestQueueEmpty not yet implemented in NDK
1351     return binder::Status::ok();
1352 }
1353 
1354 binder::Status
onRepeatingRequestError(int64_t lastFrameNumber,int32_t stoppedSequenceId)1355 CameraDevice::ServiceCallback::onRepeatingRequestError(
1356         int64_t lastFrameNumber, int32_t stoppedSequenceId) {
1357     binder::Status ret = binder::Status::ok();
1358 
1359     sp<CameraDevice> dev = mDevice.promote();
1360     if (dev == nullptr) {
1361         return ret; // device has been closed
1362     }
1363 
1364     Mutex::Autolock _l(dev->mDeviceLock);
1365 
1366     int repeatingSequenceId = dev->mRepeatingSequenceId;
1367     if (stoppedSequenceId == repeatingSequenceId) {
1368         dev->mRepeatingSequenceId = REQUEST_ID_NONE;
1369     }
1370 
1371     dev->checkRepeatingSequenceCompleteLocked(repeatingSequenceId, lastFrameNumber);
1372 
1373     return ret;
1374 }
1375 
1376 
1377 } // namespace android
1378