blob: eb9eb5b1c9decabc63a5221cc9348a93bdba235e [file] [log] [blame]
Eino-Ville Talvala69230df2012-08-29 17:37:16 -07001/*
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 "Camera2Client::CaptureSequencer"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
21#include <utils/Log.h>
22#include <utils/Trace.h>
23#include <utils/Vector.h>
24
25#include "CaptureSequencer.h"
James Painterc3dbf1a2012-09-05 18:02:32 -070026#include "BurstCapture.h"
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070027#include "../Camera2Device.h"
28#include "../Camera2Client.h"
29#include "Parameters.h"
30
31namespace android {
32namespace camera2 {
33
34/** Public members */
35
36CaptureSequencer::CaptureSequencer(wp<Camera2Client> client):
37 Thread(false),
38 mStartCapture(false),
39 mBusy(false),
40 mNewAEState(false),
41 mNewFrameReceived(false),
42 mNewCaptureReceived(false),
43 mClient(client),
44 mCaptureState(IDLE),
45 mTriggerId(0),
46 mTimeoutCount(0),
47 mCaptureId(Camera2Client::kFirstCaptureRequestId) {
James Painterc3dbf1a2012-09-05 18:02:32 -070048 ALOGV("%s", __FUNCTION__);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070049}
50
51CaptureSequencer::~CaptureSequencer() {
52 ALOGV("%s: Exit", __FUNCTION__);
53}
54
55void CaptureSequencer::setZslProcessor(wp<ZslProcessor> processor) {
56 Mutex::Autolock l(mInputMutex);
57 mZslProcessor = processor;
58}
59
60status_t CaptureSequencer::startCapture() {
James Painterc3dbf1a2012-09-05 18:02:32 -070061 ALOGV("%s", __FUNCTION__);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070062 ATRACE_CALL();
63 Mutex::Autolock l(mInputMutex);
64 if (mBusy) {
65 ALOGE("%s: Already busy capturing!", __FUNCTION__);
66 return INVALID_OPERATION;
67 }
68 if (!mStartCapture) {
69 mStartCapture = true;
70 mStartCaptureSignal.signal();
71 }
72 return OK;
73}
74
75void CaptureSequencer::notifyAutoExposure(uint8_t newState, int triggerId) {
76 ATRACE_CALL();
77 Mutex::Autolock l(mInputMutex);
78 mAEState = newState;
79 mAETriggerId = triggerId;
80 if (!mNewAEState) {
81 mNewAEState = true;
82 mNewNotifySignal.signal();
83 }
84}
85
86void CaptureSequencer::onFrameAvailable(int32_t frameId,
87 CameraMetadata &frame) {
James Painterc3dbf1a2012-09-05 18:02:32 -070088 ALOGV("%s: Listener found new frame", __FUNCTION__);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070089 ATRACE_CALL();
90 Mutex::Autolock l(mInputMutex);
91 mNewFrameId = frameId;
92 mNewFrame.acquire(frame);
93 if (!mNewFrameReceived) {
94 mNewFrameReceived = true;
95 mNewFrameSignal.signal();
96 }
97}
98
99void CaptureSequencer::onCaptureAvailable(nsecs_t timestamp) {
100 ATRACE_CALL();
James Painterc3dbf1a2012-09-05 18:02:32 -0700101 ALOGV("%s", __FUNCTION__);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700102 Mutex::Autolock l(mInputMutex);
103 mCaptureTimestamp = timestamp;
104 if (!mNewCaptureReceived) {
105 mNewCaptureReceived = true;
106 mNewCaptureSignal.signal();
107 }
108}
109
110
111void CaptureSequencer::dump(int fd, const Vector<String16>& args) {
112 String8 result;
113 if (mCaptureRequest.entryCount() != 0) {
114 result = " Capture request:\n";
115 write(fd, result.string(), result.size());
116 mCaptureRequest.dump(fd, 2, 6);
117 } else {
118 result = " Capture request: undefined\n";
119 write(fd, result.string(), result.size());
120 }
121 result = String8::format(" Current capture state: %s\n",
122 kStateNames[mCaptureState]);
123 result.append(" Latest captured frame:\n");
124 write(fd, result.string(), result.size());
125 mNewFrame.dump(fd, 2, 6);
126}
127
128/** Private members */
129
130const char* CaptureSequencer::kStateNames[CaptureSequencer::NUM_CAPTURE_STATES+1] =
131{
132 "IDLE",
133 "START",
134 "ZSL_START",
135 "ZSL_WAITING",
136 "ZSL_REPROCESSING",
137 "STANDARD_START",
138 "STANDARD_PRECAPTURE",
139 "STANDARD_CAPTURING",
James Painterc3dbf1a2012-09-05 18:02:32 -0700140 "BURST_CAPTURE_START",
141 "BURST_CAPTURE_WAIT",
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700142 "DONE",
143 "ERROR",
144 "UNKNOWN"
145};
146
147const CaptureSequencer::StateManager
148 CaptureSequencer::kStateManagers[CaptureSequencer::NUM_CAPTURE_STATES-1] = {
149 &CaptureSequencer::manageIdle,
150 &CaptureSequencer::manageStart,
151 &CaptureSequencer::manageZslStart,
152 &CaptureSequencer::manageZslWaiting,
153 &CaptureSequencer::manageZslReprocessing,
154 &CaptureSequencer::manageStandardStart,
155 &CaptureSequencer::manageStandardPrecaptureWait,
156 &CaptureSequencer::manageStandardCapture,
157 &CaptureSequencer::manageStandardCaptureWait,
James Painterc3dbf1a2012-09-05 18:02:32 -0700158 &CaptureSequencer::manageBurstCaptureStart,
159 &CaptureSequencer::manageBurstCaptureWait,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700160 &CaptureSequencer::manageDone,
161};
162
163bool CaptureSequencer::threadLoop() {
164 status_t res;
165
166 sp<Camera2Client> client = mClient.promote();
167 if (client == 0) return false;
168
169 if (mCaptureState < ERROR) {
170 mCaptureState = (this->*kStateManagers[mCaptureState])(client);
171 } else {
172 ALOGE("%s: Bad capture state: %s",
173 __FUNCTION__, kStateNames[mCaptureState]);
174 return false;
175 }
176
177 return true;
178}
179
180CaptureSequencer::CaptureState CaptureSequencer::manageIdle(sp<Camera2Client> &client) {
181 status_t res;
182 ATRACE_CALL();
183 Mutex::Autolock l(mInputMutex);
184 while (!mStartCapture) {
185 res = mStartCaptureSignal.waitRelative(mInputMutex,
186 kWaitDuration);
187 if (res == TIMED_OUT) break;
188 }
189 if (mStartCapture) {
190 mStartCapture = false;
191 mBusy = true;
192 return START;
193 }
194 return IDLE;
195}
196
197CaptureSequencer::CaptureState CaptureSequencer::manageDone(sp<Camera2Client> &client) {
198 status_t res;
199 ATRACE_CALL();
200 mCaptureId++;
201
202 {
203 Mutex::Autolock l(mInputMutex);
204 mBusy = false;
205 }
206
207 SharedParameters::Lock l(client->getParameters());
208 switch (l.mParameters.state) {
209 case Parameters::STILL_CAPTURE:
210 l.mParameters.state = Parameters::STOPPED;
211 break;
212 case Parameters::VIDEO_SNAPSHOT:
213 l.mParameters.state = Parameters::RECORD;
214 break;
215 default:
216 ALOGE("%s: Camera %d: Still image produced unexpectedly "
217 "in state %s!",
218 __FUNCTION__, client->getCameraId(),
219 Parameters::getStateName(l.mParameters.state));
220 }
221
222 return IDLE;
223}
224
225CaptureSequencer::CaptureState CaptureSequencer::manageStart(
226 sp<Camera2Client> &client) {
James Painterc3dbf1a2012-09-05 18:02:32 -0700227 ALOGV("%s", __FUNCTION__);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700228 status_t res;
229 ATRACE_CALL();
230 SharedParameters::Lock l(client->getParameters());
231 CaptureState nextState = DONE;
232
233 res = updateCaptureRequest(l.mParameters, client);
234 if (res != OK ) {
235 ALOGE("%s: Camera %d: Can't update still image capture request: %s (%d)",
236 __FUNCTION__, client->getCameraId(), strerror(-res), res);
237 return DONE;
238 }
239
James Painterc3dbf1a2012-09-05 18:02:32 -0700240 if(l.mParameters.lightFx != Parameters::LIGHTFX_NONE &&
241 l.mParameters.state == Parameters::STILL_CAPTURE) {
242 nextState = BURST_CAPTURE_START;
243 }
244 else if (l.mParameters.zslMode &&
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700245 l.mParameters.state == Parameters::STILL_CAPTURE) {
246 nextState = ZSL_START;
247 } else {
248 nextState = STANDARD_START;
249 }
250
251 return nextState;
252}
253
254CaptureSequencer::CaptureState CaptureSequencer::manageZslStart(
255 sp<Camera2Client> &client) {
Eino-Ville Talvala2954fe92012-09-12 10:42:10 -0700256 ALOGV("%s", __FUNCTION__);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700257 status_t res;
258 sp<ZslProcessor> processor = mZslProcessor.promote();
259 if (processor == 0) {
260 ALOGE("%s: No ZSL queue to use!", __FUNCTION__);
261 return DONE;
262 }
263
264 client->registerFrameListener(mCaptureId,
265 this);
266
267 res = client->getCameraDevice()->clearStreamingRequest();
268 if (res != OK) {
269 ALOGE("%s: Camera %d: Unable to stop preview for ZSL capture: "
270 "%s (%d)",
271 __FUNCTION__, client->getCameraId(), strerror(-res), res);
272 return DONE;
273 }
274 // TODO: Actually select the right thing here.
Eino-Ville Talvala2954fe92012-09-12 10:42:10 -0700275 res = processor->pushToReprocess(mCaptureId);
276 if (res != OK) {
Eino-Ville Talvala22745492012-09-17 17:55:07 -0700277 if (res == NOT_ENOUGH_DATA) {
278 ALOGV("%s: Camera %d: ZSL queue doesn't have good frame, "
279 "falling back to normal capture", __FUNCTION__,
280 client->getCameraId());
281 } else {
282 ALOGE("%s: Camera %d: Error in ZSL queue: %s (%d)",
283 __FUNCTION__, client->getCameraId(), strerror(-res), res);
284 }
Eino-Ville Talvala2954fe92012-09-12 10:42:10 -0700285 return STANDARD_START;
286 }
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700287
288 mTimeoutCount = kMaxTimeoutsForCaptureEnd;
289 return STANDARD_CAPTURE_WAIT;
290}
291
292CaptureSequencer::CaptureState CaptureSequencer::manageZslWaiting(
293 sp<Camera2Client> &client) {
Eino-Ville Talvala2954fe92012-09-12 10:42:10 -0700294 ALOGV("%s", __FUNCTION__);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700295 return DONE;
296}
297
298CaptureSequencer::CaptureState CaptureSequencer::manageZslReprocessing(
299 sp<Camera2Client> &client) {
Eino-Ville Talvala2954fe92012-09-12 10:42:10 -0700300 ALOGV("%s", __FUNCTION__);
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700301 return START;
302}
303
304CaptureSequencer::CaptureState CaptureSequencer::manageStandardStart(
305 sp<Camera2Client> &client) {
306 ATRACE_CALL();
307 client->registerFrameListener(mCaptureId,
308 this);
309 {
310 SharedParameters::Lock l(client->getParameters());
311 mTriggerId = l.mParameters.precaptureTriggerCounter++;
312 }
313 client->getCameraDevice()->triggerPrecaptureMetering(mTriggerId);
314
315 mAeInPrecapture = false;
316 mTimeoutCount = kMaxTimeoutsForPrecaptureStart;
317 return STANDARD_PRECAPTURE_WAIT;
318}
319
320CaptureSequencer::CaptureState CaptureSequencer::manageStandardPrecaptureWait(
321 sp<Camera2Client> &client) {
322 status_t res;
323 ATRACE_CALL();
324 Mutex::Autolock l(mInputMutex);
325 while (!mNewAEState) {
326 res = mNewNotifySignal.waitRelative(mInputMutex, kWaitDuration);
327 if (res == TIMED_OUT) {
328 mTimeoutCount--;
329 break;
330 }
331 }
332 if (mTimeoutCount <= 0) {
333 ALOGW("Timed out waiting for precapture %s",
334 mAeInPrecapture ? "end" : "start");
335 return STANDARD_CAPTURE;
336 }
337 if (mNewAEState) {
338 if (!mAeInPrecapture) {
339 // Waiting to see PRECAPTURE state
340 if (mAETriggerId == mTriggerId &&
341 mAEState == ANDROID_CONTROL_AE_STATE_PRECAPTURE) {
342 ALOGV("%s: Got precapture start", __FUNCTION__);
343 mAeInPrecapture = true;
344 mTimeoutCount = kMaxTimeoutsForPrecaptureEnd;
345 }
346 } else {
347 // Waiting to see PRECAPTURE state end
348 if (mAETriggerId == mTriggerId &&
349 mAEState != ANDROID_CONTROL_AE_STATE_PRECAPTURE) {
350 ALOGV("%s: Got precapture end", __FUNCTION__);
351 return STANDARD_CAPTURE;
352 }
353 }
354 mNewAEState = false;
355 }
356 return STANDARD_PRECAPTURE_WAIT;
357}
358
359CaptureSequencer::CaptureState CaptureSequencer::manageStandardCapture(
360 sp<Camera2Client> &client) {
361 status_t res;
362 ATRACE_CALL();
363 SharedParameters::Lock l(client->getParameters());
364 Vector<uint8_t> outputStreams;
365
366 outputStreams.push(client->getPreviewStreamId());
367 outputStreams.push(client->getCaptureStreamId());
368
369 if (l.mParameters.previewCallbackFlags &
370 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
371 outputStreams.push(client->getCallbackStreamId());
372 }
373
374 if (l.mParameters.state == Parameters::VIDEO_SNAPSHOT) {
375 outputStreams.push(client->getRecordingStreamId());
376 }
377
378 res = mCaptureRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS,
379 outputStreams);
380 if (res == OK) {
381 res = mCaptureRequest.update(ANDROID_REQUEST_ID,
382 &mCaptureId, 1);
383 }
384 if (res == OK) {
385 res = mCaptureRequest.sort();
386 }
387
388 if (res != OK) {
389 ALOGE("%s: Camera %d: Unable to set up still capture request: %s (%d)",
390 __FUNCTION__, client->getCameraId(), strerror(-res), res);
391 return DONE;
392 }
393
394 CameraMetadata captureCopy = mCaptureRequest;
395 if (captureCopy.entryCount() == 0) {
396 ALOGE("%s: Camera %d: Unable to copy capture request for HAL device",
397 __FUNCTION__, client->getCameraId());
398 return DONE;
399 }
400
401 if (l.mParameters.state == Parameters::STILL_CAPTURE) {
402 res = client->getCameraDevice()->clearStreamingRequest();
403 if (res != OK) {
404 ALOGE("%s: Camera %d: Unable to stop preview for still capture: "
405 "%s (%d)",
406 __FUNCTION__, client->getCameraId(), strerror(-res), res);
407 return DONE;
408 }
409 }
410 // TODO: Capture should be atomic with setStreamingRequest here
411 res = client->getCameraDevice()->capture(captureCopy);
412 if (res != OK) {
413 ALOGE("%s: Camera %d: Unable to submit still image capture request: "
414 "%s (%d)",
415 __FUNCTION__, client->getCameraId(), strerror(-res), res);
416 return DONE;
417 }
418
Eino-Ville Talvalabd3a8162012-09-15 13:27:52 -0700419 if (l.mParameters.playShutterSound &&
420 l.mParameters.state == Parameters::STILL_CAPTURE) {
Eino-Ville Talvala33578832012-09-06 18:26:58 -0700421 client->getCameraService()->playSound(CameraService::SOUND_SHUTTER);
422 }
423
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700424 mTimeoutCount = kMaxTimeoutsForCaptureEnd;
425 return STANDARD_CAPTURE_WAIT;
426}
427
428CaptureSequencer::CaptureState CaptureSequencer::manageStandardCaptureWait(
429 sp<Camera2Client> &client) {
430 status_t res;
431 ATRACE_CALL();
432 Mutex::Autolock l(mInputMutex);
433 while (!mNewFrameReceived) {
434 res = mNewFrameSignal.waitRelative(mInputMutex, kWaitDuration);
435 if (res == TIMED_OUT) {
436 mTimeoutCount--;
437 break;
438 }
439 }
440 while (!mNewCaptureReceived) {
441 res = mNewCaptureSignal.waitRelative(mInputMutex, kWaitDuration);
442 if (res == TIMED_OUT) {
443 mTimeoutCount--;
444 break;
445 }
446 }
447 if (mTimeoutCount <= 0) {
448 ALOGW("Timed out waiting for capture to complete");
449 return DONE;
450 }
451 if (mNewFrameReceived && mNewCaptureReceived) {
452 if (mNewFrameId != mCaptureId) {
453 ALOGW("Mismatched capture frame IDs: Expected %d, got %d",
454 mCaptureId, mNewFrameId);
455 }
456 camera_metadata_entry_t entry;
457 entry = mNewFrame.find(ANDROID_SENSOR_TIMESTAMP);
458 if (entry.count == 0) {
459 ALOGE("No timestamp field in capture frame!");
460 }
461 if (entry.data.i64[0] != mCaptureTimestamp) {
462 ALOGW("Mismatched capture timestamps: Metadata frame %lld,"
463 " captured buffer %lld", entry.data.i64[0], mCaptureTimestamp);
464 }
465 client->removeFrameListener(mCaptureId);
466
467 mNewFrameReceived = false;
468 mNewCaptureReceived = false;
469 return DONE;
470 }
471 return STANDARD_CAPTURE_WAIT;
472}
473
James Painterc3dbf1a2012-09-05 18:02:32 -0700474CaptureSequencer::CaptureState CaptureSequencer::manageBurstCaptureStart(
475 sp<Camera2Client> &client) {
476 ALOGV("%s", __FUNCTION__);
477 status_t res;
478 ATRACE_CALL();
479
480 // check which burst mode is set, create respective burst object
481 {
482 SharedParameters::Lock l(client->getParameters());
483
484 res = updateCaptureRequest(l.mParameters, client);
485 if(res != OK) {
486 return DONE;
487 }
488
489 //
490 // check for burst mode type in mParameters here
491 //
492 mBurstCapture = new BurstCapture(client, this);
493 }
494
495 res = mCaptureRequest.update(ANDROID_REQUEST_ID, &mCaptureId, 1);
496 if (res == OK) {
497 res = mCaptureRequest.sort();
498 }
499 if (res != OK) {
500 ALOGE("%s: Camera %d: Unable to set up still capture request: %s (%d)",
501 __FUNCTION__, client->getCameraId(), strerror(-res), res);
502 return DONE;
503 }
504
505 CameraMetadata captureCopy = mCaptureRequest;
506 if (captureCopy.entryCount() == 0) {
507 ALOGE("%s: Camera %d: Unable to copy capture request for HAL device",
508 __FUNCTION__, client->getCameraId());
509 return DONE;
510 }
511
512 Vector<CameraMetadata> requests;
513 requests.push(mCaptureRequest);
514 res = mBurstCapture->start(requests, mCaptureId);
515 mTimeoutCount = kMaxTimeoutsForCaptureEnd * 10;
516 return BURST_CAPTURE_WAIT;
517}
518
519CaptureSequencer::CaptureState CaptureSequencer::manageBurstCaptureWait(
520 sp<Camera2Client> &client) {
521 status_t res;
522 ATRACE_CALL();
523
524 while (!mNewCaptureReceived) {
525 res = mNewCaptureSignal.waitRelative(mInputMutex, kWaitDuration);
526 if (res == TIMED_OUT) {
527 mTimeoutCount--;
528 break;
529 }
530 }
531
532 if (mTimeoutCount <= 0) {
533 ALOGW("Timed out waiting for burst capture to complete");
534 return DONE;
535 }
536 if (mNewCaptureReceived) {
537 mNewCaptureReceived = false;
538 // TODO: update mCaptureId to last burst's capture ID + 1?
539 return DONE;
540 }
541
542 return BURST_CAPTURE_WAIT;
543}
544
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700545status_t CaptureSequencer::updateCaptureRequest(const Parameters &params,
546 sp<Camera2Client> &client) {
547 ATRACE_CALL();
548 status_t res;
549 if (mCaptureRequest.entryCount() == 0) {
550 res = client->getCameraDevice()->createDefaultRequest(
551 CAMERA2_TEMPLATE_STILL_CAPTURE,
552 &mCaptureRequest);
553 if (res != OK) {
554 ALOGE("%s: Camera %d: Unable to create default still image request:"
555 " %s (%d)", __FUNCTION__, client->getCameraId(),
556 strerror(-res), res);
557 return res;
558 }
559 }
560
561 res = params.updateRequest(&mCaptureRequest);
562 if (res != OK) {
563 ALOGE("%s: Camera %d: Unable to update common entries of capture "
564 "request: %s (%d)", __FUNCTION__, client->getCameraId(),
565 strerror(-res), res);
566 return res;
567 }
568
569 res = mCaptureRequest.update(ANDROID_JPEG_THUMBNAIL_SIZE,
570 params.jpegThumbSize, 2);
571 if (res != OK) return res;
572 res = mCaptureRequest.update(ANDROID_JPEG_THUMBNAIL_QUALITY,
573 &params.jpegThumbQuality, 1);
574 if (res != OK) return res;
575 res = mCaptureRequest.update(ANDROID_JPEG_QUALITY,
576 &params.jpegQuality, 1);
577 if (res != OK) return res;
578 res = mCaptureRequest.update(
579 ANDROID_JPEG_ORIENTATION,
580 &params.jpegRotation, 1);
581 if (res != OK) return res;
582
583 if (params.gpsEnabled) {
584 res = mCaptureRequest.update(
585 ANDROID_JPEG_GPS_COORDINATES,
586 params.gpsCoordinates, 3);
587 if (res != OK) return res;
588 res = mCaptureRequest.update(
589 ANDROID_JPEG_GPS_TIMESTAMP,
590 &params.gpsTimestamp, 1);
591 if (res != OK) return res;
592 res = mCaptureRequest.update(
593 ANDROID_JPEG_GPS_PROCESSING_METHOD,
594 params.gpsProcessingMethod);
595 if (res != OK) return res;
596 } else {
597 res = mCaptureRequest.erase(ANDROID_JPEG_GPS_COORDINATES);
598 if (res != OK) return res;
599 res = mCaptureRequest.erase(ANDROID_JPEG_GPS_TIMESTAMP);
600 if (res != OK) return res;
601 res = mCaptureRequest.erase(ANDROID_JPEG_GPS_PROCESSING_METHOD);
602 if (res != OK) return res;
603 }
604
605 return OK;
606}
607
608
609}; // namespace camera2
610}; // namespace android