1 /*
2 * Copyright (C) 2012 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_TAG "Camera2-CaptureSequencer"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20
21 #include <inttypes.h>
22
23 #include <utils/Log.h>
24 #include <utils/Trace.h>
25 #include <utils/Vector.h>
26
27 #include "api1/Camera2Client.h"
28 #include "api1/client2/CaptureSequencer.h"
29 #include "api1/client2/Parameters.h"
30 #include "api1/client2/ZslProcessor.h"
31
32 namespace android {
33 namespace camera2 {
34
35 /** Public members */
36
CaptureSequencer(wp<Camera2Client> client)37 CaptureSequencer::CaptureSequencer(wp<Camera2Client> client):
38 Thread(false),
39 mStartCapture(false),
40 mBusy(false),
41 mNewAEState(false),
42 mNewFrameReceived(false),
43 mNewCaptureReceived(false),
44 mNewCaptureErrorCnt(0),
45 mShutterNotified(false),
46 mHalNotifiedShutter(false),
47 mShutterCaptureId(-1),
48 mClient(client),
49 mCaptureState(IDLE),
50 mStateTransitionCount(0),
51 mTriggerId(0),
52 mTimeoutCount(0),
53 mCaptureId(Camera2Client::kCaptureRequestIdStart),
54 mMsgType(0) {
55 ALOGV("%s", __FUNCTION__);
56 }
57
~CaptureSequencer()58 CaptureSequencer::~CaptureSequencer() {
59 ALOGV("%s: Exit", __FUNCTION__);
60 }
61
setZslProcessor(const wp<ZslProcessor> & processor)62 void CaptureSequencer::setZslProcessor(const wp<ZslProcessor>& processor) {
63 Mutex::Autolock l(mInputMutex);
64 mZslProcessor = processor;
65 }
66
startCapture(int msgType)67 status_t CaptureSequencer::startCapture(int msgType) {
68 ALOGV("%s", __FUNCTION__);
69 ATRACE_CALL();
70 Mutex::Autolock l(mInputMutex);
71 if (mBusy) {
72 ALOGE("%s: Already busy capturing!", __FUNCTION__);
73 return INVALID_OPERATION;
74 }
75 if (!mStartCapture) {
76 mMsgType = msgType;
77 mStartCapture = true;
78 mStartCaptureSignal.signal();
79 }
80 return OK;
81 }
82
waitUntilIdle(nsecs_t timeout)83 status_t CaptureSequencer::waitUntilIdle(nsecs_t timeout) {
84 ATRACE_CALL();
85 ALOGV("%s: Waiting for idle", __FUNCTION__);
86 Mutex::Autolock l(mStateMutex);
87 status_t res = -1;
88 while (mCaptureState != IDLE) {
89 nsecs_t startTime = systemTime();
90
91 res = mStateChanged.waitRelative(mStateMutex, timeout);
92 if (res != OK) return res;
93
94 timeout -= (systemTime() - startTime);
95 }
96 ALOGV("%s: Now idle", __FUNCTION__);
97 return OK;
98 }
99
notifyAutoExposure(uint8_t newState,int triggerId)100 void CaptureSequencer::notifyAutoExposure(uint8_t newState, int triggerId) {
101 ATRACE_CALL();
102 Mutex::Autolock l(mInputMutex);
103 mAEState = newState;
104 mAETriggerId = triggerId;
105 if (!mNewAEState) {
106 mNewAEState = true;
107 mNewNotifySignal.signal();
108 }
109 }
110
notifyShutter(const CaptureResultExtras & resultExtras,nsecs_t timestamp)111 void CaptureSequencer::notifyShutter(const CaptureResultExtras& resultExtras,
112 nsecs_t timestamp) {
113 ATRACE_CALL();
114 (void) timestamp;
115 Mutex::Autolock l(mInputMutex);
116 if (!mHalNotifiedShutter && resultExtras.requestId == mShutterCaptureId) {
117 mHalNotifiedShutter = true;
118 mShutterNotifySignal.signal();
119 }
120 }
121
onResultAvailable(const CaptureResult & result)122 void CaptureSequencer::onResultAvailable(const CaptureResult &result) {
123 ATRACE_CALL();
124 ALOGV("%s: New result available.", __FUNCTION__);
125 Mutex::Autolock l(mInputMutex);
126 mNewFrameId = result.mResultExtras.requestId;
127 mNewFrame = result.mMetadata;
128 if (!mNewFrameReceived) {
129 mNewFrameReceived = true;
130 mNewFrameSignal.signal();
131 }
132 }
133
onCaptureAvailable(nsecs_t timestamp,const sp<MemoryBase> & captureBuffer,bool captureError)134 void CaptureSequencer::onCaptureAvailable(nsecs_t timestamp,
135 const sp<MemoryBase>& captureBuffer, bool captureError) {
136 ATRACE_CALL();
137 ALOGV("%s", __FUNCTION__);
138 Mutex::Autolock l(mInputMutex);
139 mCaptureTimestamp = timestamp;
140 mCaptureBuffer = captureBuffer;
141 if (!mNewCaptureReceived) {
142 mNewCaptureReceived = true;
143 if (captureError) {
144 mNewCaptureErrorCnt++;
145 } else {
146 mNewCaptureErrorCnt = 0;
147 }
148 mNewCaptureSignal.signal();
149 }
150 }
151
152
dump(int fd,const Vector<String16> &)153 void CaptureSequencer::dump(int fd, const Vector<String16>& /*args*/) {
154 String8 result;
155 if (mCaptureRequest.entryCount() != 0) {
156 result = " Capture request:\n";
157 write(fd, result.string(), result.size());
158 mCaptureRequest.dump(fd, 2, 6);
159 } else {
160 result = " Capture request: undefined\n";
161 write(fd, result.string(), result.size());
162 }
163 result = String8::format(" Current capture state: %s\n",
164 kStateNames[mCaptureState]);
165 result.append(" Latest captured frame:\n");
166 write(fd, result.string(), result.size());
167 mNewFrame.dump(fd, 2, 6);
168 }
169
170 /** Private members */
171
172 const char* CaptureSequencer::kStateNames[CaptureSequencer::NUM_CAPTURE_STATES+1] =
173 {
174 "IDLE",
175 "START",
176 "ZSL_START",
177 "ZSL_WAITING",
178 "ZSL_REPROCESSING",
179 "STANDARD_START",
180 "STANDARD_PRECAPTURE_WAIT",
181 "STANDARD_CAPTURE",
182 "STANDARD_CAPTURE_WAIT",
183 "DONE",
184 "ERROR",
185 "UNKNOWN"
186 };
187
188 const CaptureSequencer::StateManager
189 CaptureSequencer::kStateManagers[CaptureSequencer::NUM_CAPTURE_STATES-1] = {
190 &CaptureSequencer::manageIdle,
191 &CaptureSequencer::manageStart,
192 &CaptureSequencer::manageZslStart,
193 &CaptureSequencer::manageZslWaiting,
194 &CaptureSequencer::manageZslReprocessing,
195 &CaptureSequencer::manageStandardStart,
196 &CaptureSequencer::manageStandardPrecaptureWait,
197 &CaptureSequencer::manageStandardCapture,
198 &CaptureSequencer::manageStandardCaptureWait,
199 &CaptureSequencer::manageDone,
200 };
201
threadLoop()202 bool CaptureSequencer::threadLoop() {
203
204 sp<Camera2Client> client = mClient.promote();
205 if (client == 0) return false;
206
207 CaptureState currentState;
208 {
209 Mutex::Autolock l(mStateMutex);
210 currentState = mCaptureState;
211 }
212
213 currentState = (this->*kStateManagers[currentState])(client);
214
215 Mutex::Autolock l(mStateMutex);
216 if (currentState != mCaptureState) {
217 if (mCaptureState != IDLE) {
218 ATRACE_ASYNC_END(kStateNames[mCaptureState], mStateTransitionCount);
219 }
220 mCaptureState = currentState;
221 mStateTransitionCount++;
222 if (mCaptureState != IDLE) {
223 ATRACE_ASYNC_BEGIN(kStateNames[mCaptureState], mStateTransitionCount);
224 }
225 ALOGV("Camera %d: New capture state %s",
226 client->getCameraId(), kStateNames[mCaptureState]);
227 mStateChanged.signal();
228 }
229
230 if (mCaptureState == ERROR) {
231 ALOGE("Camera %d: Stopping capture sequencer due to error",
232 client->getCameraId());
233 return false;
234 }
235
236 return true;
237 }
238
manageIdle(sp<Camera2Client> &)239 CaptureSequencer::CaptureState CaptureSequencer::manageIdle(
240 sp<Camera2Client> &/*client*/) {
241 status_t res;
242 Mutex::Autolock l(mInputMutex);
243 while (!mStartCapture) {
244 res = mStartCaptureSignal.waitRelative(mInputMutex,
245 kWaitDuration);
246 if (res == TIMED_OUT) break;
247 }
248 if (mStartCapture) {
249 mStartCapture = false;
250 mBusy = true;
251 return START;
252 }
253 return IDLE;
254 }
255
manageDone(sp<Camera2Client> & client)256 CaptureSequencer::CaptureState CaptureSequencer::manageDone(sp<Camera2Client> &client) {
257 status_t res = OK;
258 ATRACE_CALL();
259 mCaptureId++;
260 if (mCaptureId >= Camera2Client::kCaptureRequestIdEnd) {
261 mCaptureId = Camera2Client::kCaptureRequestIdStart;
262 }
263 {
264 Mutex::Autolock l(mInputMutex);
265 mBusy = false;
266 }
267
268 int takePictureCounter = 0;
269 {
270 SharedParameters::Lock l(client->getParameters());
271 switch (l.mParameters.state) {
272 case Parameters::DISCONNECTED:
273 ALOGW("%s: Camera %d: Discarding image data during shutdown ",
274 __FUNCTION__, client->getCameraId());
275 res = INVALID_OPERATION;
276 break;
277 case Parameters::STILL_CAPTURE:
278 res = client->getCameraDevice()->waitUntilDrained();
279 if (res != OK) {
280 ALOGE("%s: Camera %d: Can't idle after still capture: "
281 "%s (%d)", __FUNCTION__, client->getCameraId(),
282 strerror(-res), res);
283 }
284 l.mParameters.state = Parameters::STOPPED;
285 break;
286 case Parameters::VIDEO_SNAPSHOT:
287 l.mParameters.state = Parameters::RECORD;
288 break;
289 default:
290 ALOGE("%s: Camera %d: Still image produced unexpectedly "
291 "in state %s!",
292 __FUNCTION__, client->getCameraId(),
293 Parameters::getStateName(l.mParameters.state));
294 res = INVALID_OPERATION;
295 }
296 takePictureCounter = l.mParameters.takePictureCounter;
297 }
298 sp<ZslProcessor> processor = mZslProcessor.promote();
299 if (processor != 0) {
300 ALOGV("%s: Memory optimization, clearing ZSL queue",
301 __FUNCTION__);
302 processor->clearZslQueue();
303 }
304
305 /**
306 * Fire the jpegCallback in Camera#takePicture(..., jpegCallback)
307 */
308 if (mCaptureBuffer != 0 && res == OK) {
309 ATRACE_ASYNC_END(Camera2Client::kTakepictureLabel, takePictureCounter);
310
311 Camera2Client::SharedCameraCallbacks::Lock
312 l(client->mSharedCameraCallbacks);
313 ALOGV("%s: Sending still image to client", __FUNCTION__);
314 if (l.mRemoteCallback != 0) {
315 l.mRemoteCallback->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE,
316 mCaptureBuffer, NULL);
317 } else {
318 ALOGV("%s: No client!", __FUNCTION__);
319 }
320 }
321 mCaptureBuffer.clear();
322
323 return IDLE;
324 }
325
manageStart(sp<Camera2Client> & client)326 CaptureSequencer::CaptureState CaptureSequencer::manageStart(
327 sp<Camera2Client> &client) {
328 ALOGV("%s", __FUNCTION__);
329 status_t res;
330 ATRACE_CALL();
331 SharedParameters::Lock l(client->getParameters());
332 CaptureState nextState = DONE;
333
334 res = updateCaptureRequest(l.mParameters, client);
335 if (res != OK ) {
336 ALOGE("%s: Camera %d: Can't update still image capture request: %s (%d)",
337 __FUNCTION__, client->getCameraId(), strerror(-res), res);
338 return DONE;
339 }
340
341 else if (l.mParameters.useZeroShutterLag() &&
342 l.mParameters.state == Parameters::STILL_CAPTURE &&
343 l.mParameters.flashMode != Parameters::FLASH_MODE_ON) {
344 nextState = ZSL_START;
345 } else {
346 nextState = STANDARD_START;
347 }
348 {
349 Mutex::Autolock l(mInputMutex);
350 mShutterCaptureId = mCaptureId;
351 mHalNotifiedShutter = false;
352 }
353 mShutterNotified = false;
354
355 return nextState;
356 }
357
manageZslStart(sp<Camera2Client> & client)358 CaptureSequencer::CaptureState CaptureSequencer::manageZslStart(
359 sp<Camera2Client> &client) {
360 ALOGV("%s", __FUNCTION__);
361 status_t res;
362 sp<ZslProcessor> processor = mZslProcessor.promote();
363 if (processor == 0) {
364 ALOGE("%s: No ZSL queue to use!", __FUNCTION__);
365 return DONE;
366 }
367
368 // We don't want to get partial results for ZSL capture.
369 client->registerFrameListener(mCaptureId, mCaptureId + 1,
370 this,
371 /*sendPartials*/false);
372
373 // TODO: Actually select the right thing here.
374 res = processor->pushToReprocess(mCaptureId);
375 if (res != OK) {
376 if (res == NOT_ENOUGH_DATA) {
377 ALOGV("%s: Camera %d: ZSL queue doesn't have good frame, "
378 "falling back to normal capture", __FUNCTION__,
379 client->getCameraId());
380 } else {
381 ALOGE("%s: Camera %d: Error in ZSL queue: %s (%d)",
382 __FUNCTION__, client->getCameraId(), strerror(-res), res);
383 }
384 return STANDARD_START;
385 }
386
387 SharedParameters::Lock l(client->getParameters());
388 /* warning: this also locks a SharedCameraCallbacks */
389 shutterNotifyLocked(l.mParameters, client, mMsgType);
390 mShutterNotified = true;
391 mTimeoutCount = kMaxTimeoutsForCaptureEnd;
392 return STANDARD_CAPTURE_WAIT;
393 }
394
manageZslWaiting(sp<Camera2Client> &)395 CaptureSequencer::CaptureState CaptureSequencer::manageZslWaiting(
396 sp<Camera2Client> &/*client*/) {
397 ALOGV("%s", __FUNCTION__);
398 return DONE;
399 }
400
manageZslReprocessing(sp<Camera2Client> &)401 CaptureSequencer::CaptureState CaptureSequencer::manageZslReprocessing(
402 sp<Camera2Client> &/*client*/) {
403 ALOGV("%s", __FUNCTION__);
404 return START;
405 }
406
manageStandardStart(sp<Camera2Client> & client)407 CaptureSequencer::CaptureState CaptureSequencer::manageStandardStart(
408 sp<Camera2Client> &client) {
409 ATRACE_CALL();
410
411 bool isAeConverged = false;
412 // Get the onFrameAvailable callback when the requestID == mCaptureId
413 // We don't want to get partial results for normal capture, as we need
414 // Get ANDROID_SENSOR_TIMESTAMP from the capture result, but partial
415 // result doesn't have to have this metadata available.
416 // TODO: Update to use the HALv3 shutter notification for remove the
417 // need for this listener and make it faster. see bug 12530628.
418 client->registerFrameListener(mCaptureId, mCaptureId + 1,
419 this,
420 /*sendPartials*/false);
421
422 {
423 Mutex::Autolock l(mInputMutex);
424 isAeConverged = (mAEState == ANDROID_CONTROL_AE_STATE_CONVERGED);
425 }
426
427 {
428 SharedParameters::Lock l(client->getParameters());
429 // Skip AE precapture when it is already converged and not in force flash mode.
430 if (l.mParameters.flashMode != Parameters::FLASH_MODE_ON && isAeConverged) {
431 return STANDARD_CAPTURE;
432 }
433
434 mTriggerId = l.mParameters.precaptureTriggerCounter++;
435 }
436 client->getCameraDevice()->triggerPrecaptureMetering(mTriggerId);
437
438 mAeInPrecapture = false;
439 mTimeoutCount = kMaxTimeoutsForPrecaptureStart;
440 return STANDARD_PRECAPTURE_WAIT;
441 }
442
manageStandardPrecaptureWait(sp<Camera2Client> &)443 CaptureSequencer::CaptureState CaptureSequencer::manageStandardPrecaptureWait(
444 sp<Camera2Client> &/*client*/) {
445 status_t res;
446 ATRACE_CALL();
447 Mutex::Autolock l(mInputMutex);
448 while (!mNewAEState) {
449 res = mNewNotifySignal.waitRelative(mInputMutex, kWaitDuration);
450 if (res == TIMED_OUT) {
451 mTimeoutCount--;
452 break;
453 }
454 }
455 if (mTimeoutCount <= 0) {
456 ALOGW("Timed out waiting for precapture %s",
457 mAeInPrecapture ? "end" : "start");
458 return STANDARD_CAPTURE;
459 }
460 if (mNewAEState) {
461 if (!mAeInPrecapture) {
462 // Waiting to see PRECAPTURE state
463 if (mAETriggerId == mTriggerId) {
464 if (mAEState == ANDROID_CONTROL_AE_STATE_PRECAPTURE) {
465 ALOGV("%s: Got precapture start", __FUNCTION__);
466 mAeInPrecapture = true;
467 mTimeoutCount = kMaxTimeoutsForPrecaptureEnd;
468 } else if (mAEState == ANDROID_CONTROL_AE_STATE_CONVERGED ||
469 mAEState == ANDROID_CONTROL_AE_STATE_FLASH_REQUIRED) {
470 // It is legal to transit to CONVERGED or FLASH_REQUIRED
471 // directly after a trigger.
472 ALOGV("%s: AE is already in good state, start capture", __FUNCTION__);
473 return STANDARD_CAPTURE;
474 }
475 }
476 } else {
477 // Waiting to see PRECAPTURE state end
478 if (mAETriggerId == mTriggerId &&
479 mAEState != ANDROID_CONTROL_AE_STATE_PRECAPTURE) {
480 ALOGV("%s: Got precapture end", __FUNCTION__);
481 return STANDARD_CAPTURE;
482 }
483 }
484 mNewAEState = false;
485 }
486 return STANDARD_PRECAPTURE_WAIT;
487 }
488
manageStandardCapture(sp<Camera2Client> & client)489 CaptureSequencer::CaptureState CaptureSequencer::manageStandardCapture(
490 sp<Camera2Client> &client) {
491 status_t res;
492 ATRACE_CALL();
493 SharedParameters::Lock l(client->getParameters());
494 Vector<int32_t> outputStreams;
495 uint8_t captureIntent = static_cast<uint8_t>(ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE);
496
497 /**
498 * Set up output streams in the request
499 * - preview
500 * - capture/jpeg
501 * - callback (if preview callbacks enabled)
502 * - recording (if recording enabled)
503 */
504 outputStreams.push(client->getPreviewStreamId());
505
506 int captureStreamId = client->getCaptureStreamId();
507 if (captureStreamId == Camera2Client::NO_STREAM) {
508 res = client->createJpegStreamL(l.mParameters);
509 if (res != OK || client->getCaptureStreamId() == Camera2Client::NO_STREAM) {
510 ALOGE("%s: Camera %d: cannot create jpeg stream for slowJpeg mode: %s (%d)",
511 __FUNCTION__, client->getCameraId(), strerror(-res), res);
512 return DONE;
513 }
514 }
515
516 outputStreams.push(client->getCaptureStreamId());
517
518 if (l.mParameters.previewCallbackFlags &
519 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
520 outputStreams.push(client->getCallbackStreamId());
521 }
522
523 if (l.mParameters.state == Parameters::VIDEO_SNAPSHOT) {
524 outputStreams.push(client->getRecordingStreamId());
525 captureIntent = static_cast<uint8_t>(ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT);
526 }
527
528 res = mCaptureRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS,
529 outputStreams);
530 if (res == OK) {
531 res = mCaptureRequest.update(ANDROID_REQUEST_ID,
532 &mCaptureId, 1);
533 }
534 if (res == OK) {
535 res = mCaptureRequest.update(ANDROID_CONTROL_CAPTURE_INTENT,
536 &captureIntent, 1);
537 }
538 if (res == OK) {
539 res = mCaptureRequest.sort();
540 }
541
542 if (res != OK) {
543 ALOGE("%s: Camera %d: Unable to set up still capture request: %s (%d)",
544 __FUNCTION__, client->getCameraId(), strerror(-res), res);
545 return DONE;
546 }
547
548 // Create a capture copy since CameraDeviceBase#capture takes ownership
549 CameraMetadata captureCopy = mCaptureRequest;
550 if (captureCopy.entryCount() == 0) {
551 ALOGE("%s: Camera %d: Unable to copy capture request for HAL device",
552 __FUNCTION__, client->getCameraId());
553 return DONE;
554 }
555
556 if (l.mParameters.isDeviceZslSupported) {
557 // If device ZSL is supported, drop all pending preview buffers to reduce the chance of
558 // rendering preview frames newer than the still frame.
559 client->getCameraDevice()->dropStreamBuffers(true, client->getPreviewStreamId());
560 }
561
562 /**
563 * Clear the streaming request for still-capture pictures
564 * (as opposed to i.e. video snapshots)
565 */
566 if (l.mParameters.state == Parameters::STILL_CAPTURE) {
567 // API definition of takePicture() - stop preview before taking pic
568 res = client->stopStream();
569 if (res != OK) {
570 ALOGE("%s: Camera %d: Unable to stop preview for still capture: "
571 "%s (%d)",
572 __FUNCTION__, client->getCameraId(), strerror(-res), res);
573 return DONE;
574 }
575 }
576
577 // TODO: Capture should be atomic with setStreamingRequest here
578 res = client->getCameraDevice()->capture(captureCopy);
579 if (res != OK) {
580 ALOGE("%s: Camera %d: Unable to submit still image capture request: "
581 "%s (%d)",
582 __FUNCTION__, client->getCameraId(), strerror(-res), res);
583 return DONE;
584 }
585
586 mTimeoutCount = kMaxTimeoutsForCaptureEnd;
587 return STANDARD_CAPTURE_WAIT;
588 }
589
manageStandardCaptureWait(sp<Camera2Client> & client)590 CaptureSequencer::CaptureState CaptureSequencer::manageStandardCaptureWait(
591 sp<Camera2Client> &client) {
592 status_t res;
593 ATRACE_CALL();
594 Mutex::Autolock l(mInputMutex);
595
596
597 // Wait for shutter callback
598 while (!mHalNotifiedShutter) {
599 if (mTimeoutCount <= 0) {
600 break;
601 }
602 res = mShutterNotifySignal.waitRelative(mInputMutex, kWaitDuration);
603 if (res == TIMED_OUT) {
604 mTimeoutCount--;
605 return STANDARD_CAPTURE_WAIT;
606 }
607 }
608
609 if (mHalNotifiedShutter) {
610 if (!mShutterNotified) {
611 SharedParameters::Lock l(client->getParameters());
612 /* warning: this also locks a SharedCameraCallbacks */
613 shutterNotifyLocked(l.mParameters, client, mMsgType);
614 mShutterNotified = true;
615 }
616 } else if (mTimeoutCount <= 0) {
617 ALOGW("Timed out waiting for shutter notification");
618 return DONE;
619 }
620
621 // Wait for new metadata result (mNewFrame)
622 while (!mNewFrameReceived) {
623 res = mNewFrameSignal.waitRelative(mInputMutex, kWaitDuration);
624 if (res == TIMED_OUT) {
625 mTimeoutCount--;
626 break;
627 }
628 }
629
630 // Wait until jpeg was captured by JpegProcessor
631 while (mNewFrameReceived && !mNewCaptureReceived) {
632 res = mNewCaptureSignal.waitRelative(mInputMutex, kWaitDuration);
633 if (res == TIMED_OUT) {
634 mTimeoutCount--;
635 break;
636 }
637 }
638 if (mNewCaptureReceived) {
639 if (mNewCaptureErrorCnt > kMaxRetryCount) {
640 ALOGW("Exceeding multiple retry limit of %d due to buffer drop", kMaxRetryCount);
641 return DONE;
642 } else if (mNewCaptureErrorCnt > 0) {
643 ALOGW("Capture error happened, retry %d...", mNewCaptureErrorCnt);
644 mNewCaptureReceived = false;
645 return STANDARD_CAPTURE;
646 }
647 }
648
649 if (mTimeoutCount <= 0) {
650 ALOGW("Timed out waiting for capture to complete");
651 return DONE;
652 }
653 if (mNewFrameReceived && mNewCaptureReceived) {
654
655 if (mNewFrameId != mCaptureId) {
656 ALOGW("Mismatched capture frame IDs: Expected %d, got %d",
657 mCaptureId, mNewFrameId);
658 }
659 camera_metadata_entry_t entry;
660 entry = mNewFrame.find(ANDROID_SENSOR_TIMESTAMP);
661 if (entry.count == 0) {
662 ALOGE("No timestamp field in capture frame!");
663 } else if (entry.count == 1) {
664 if (entry.data.i64[0] != mCaptureTimestamp) {
665 ALOGW("Mismatched capture timestamps: Metadata frame %" PRId64 ","
666 " captured buffer %" PRId64,
667 entry.data.i64[0],
668 mCaptureTimestamp);
669 }
670 } else {
671 ALOGE("Timestamp metadata is malformed!");
672 }
673 client->removeFrameListener(mCaptureId, mCaptureId + 1, this);
674
675 mNewFrameReceived = false;
676 mNewCaptureReceived = false;
677 return DONE;
678 }
679 return STANDARD_CAPTURE_WAIT;
680 }
681
updateCaptureRequest(const Parameters & params,sp<Camera2Client> & client)682 status_t CaptureSequencer::updateCaptureRequest(const Parameters ¶ms,
683 sp<Camera2Client> &client) {
684 ATRACE_CALL();
685 status_t res;
686 if (mCaptureRequest.entryCount() == 0) {
687 res = client->getCameraDevice()->createDefaultRequest(
688 CAMERA2_TEMPLATE_STILL_CAPTURE,
689 &mCaptureRequest);
690 if (res != OK) {
691 ALOGE("%s: Camera %d: Unable to create default still image request:"
692 " %s (%d)", __FUNCTION__, client->getCameraId(),
693 strerror(-res), res);
694 return res;
695 }
696 }
697
698 res = params.updateRequest(&mCaptureRequest);
699 if (res != OK) {
700 ALOGE("%s: Camera %d: Unable to update common entries of capture "
701 "request: %s (%d)", __FUNCTION__, client->getCameraId(),
702 strerror(-res), res);
703 return res;
704 }
705
706 res = params.updateRequestJpeg(&mCaptureRequest);
707 if (res != OK) {
708 ALOGE("%s: Camera %d: Unable to update JPEG entries of capture "
709 "request: %s (%d)", __FUNCTION__, client->getCameraId(),
710 strerror(-res), res);
711 return res;
712 }
713
714 return OK;
715 }
716
shutterNotifyLocked(const Parameters & params,const sp<Camera2Client> & client,int msgType)717 /*static*/ void CaptureSequencer::shutterNotifyLocked(const Parameters ¶ms,
718 const sp<Camera2Client>& client, int msgType) {
719 ATRACE_CALL();
720
721 if (params.state == Parameters::STILL_CAPTURE
722 && params.playShutterSound
723 && (msgType & CAMERA_MSG_SHUTTER)) {
724 client->getCameraService()->playSound(CameraService::SOUND_SHUTTER);
725 }
726
727 {
728 Camera2Client::SharedCameraCallbacks::Lock
729 l(client->mSharedCameraCallbacks);
730
731 ALOGV("%s: Notifying of shutter close to client", __FUNCTION__);
732 if (l.mRemoteCallback != 0) {
733 // ShutterCallback
734 l.mRemoteCallback->notifyCallback(CAMERA_MSG_SHUTTER,
735 /*ext1*/0, /*ext2*/0);
736
737 // RawCallback with null buffer
738 l.mRemoteCallback->notifyCallback(CAMERA_MSG_RAW_IMAGE_NOTIFY,
739 /*ext1*/0, /*ext2*/0);
740 } else {
741 ALOGV("%s: No client!", __FUNCTION__);
742 }
743 }
744 }
745
746
747 }; // namespace camera2
748 }; // namespace android
749