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 "Camera2Client"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20
21 #include <inttypes.h>
22 #include <utils/Log.h>
23 #include <utils/Trace.h>
24
25 #include <cutils/properties.h>
26 #include <gui/Surface.h>
27
28 #include "api1/Camera2Client.h"
29
30 #include "api1/client2/StreamingProcessor.h"
31 #include "api1/client2/JpegProcessor.h"
32 #include "api1/client2/CaptureSequencer.h"
33 #include "api1/client2/CallbackProcessor.h"
34 #include "api1/client2/ZslProcessor.h"
35 #include "api1/client2/ZslProcessor3.h"
36
37 #define ALOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
38 #define ALOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
39
40 namespace android {
41 using namespace camera2;
42
getCallingPid()43 static int getCallingPid() {
44 return IPCThreadState::self()->getCallingPid();
45 }
46
47 // Interface used by CameraService
48
Camera2Client(const sp<CameraService> & cameraService,const sp<ICameraClient> & cameraClient,const String16 & clientPackageName,int cameraId,int cameraFacing,int clientPid,uid_t clientUid,int servicePid,bool legacyMode)49 Camera2Client::Camera2Client(const sp<CameraService>& cameraService,
50 const sp<ICameraClient>& cameraClient,
51 const String16& clientPackageName,
52 int cameraId,
53 int cameraFacing,
54 int clientPid,
55 uid_t clientUid,
56 int servicePid,
57 bool legacyMode):
58 Camera2ClientBase(cameraService, cameraClient, clientPackageName,
59 cameraId, cameraFacing, clientPid, clientUid, servicePid),
60 mParameters(cameraId, cameraFacing)
61 {
62 ATRACE_CALL();
63
64 SharedParameters::Lock l(mParameters);
65 l.mParameters.state = Parameters::DISCONNECTED;
66
67 mLegacyMode = legacyMode;
68 }
69
initialize(camera_module_t * module)70 status_t Camera2Client::initialize(camera_module_t *module)
71 {
72 ATRACE_CALL();
73 ALOGV("%s: Initializing client for camera %d", __FUNCTION__, mCameraId);
74 status_t res;
75
76 res = Camera2ClientBase::initialize(module);
77 if (res != OK) {
78 return res;
79 }
80
81 {
82 SharedParameters::Lock l(mParameters);
83
84 res = l.mParameters.initialize(&(mDevice->info()), mDeviceVersion);
85 if (res != OK) {
86 ALOGE("%s: Camera %d: unable to build defaults: %s (%d)",
87 __FUNCTION__, mCameraId, strerror(-res), res);
88 return NO_INIT;
89 }
90 }
91
92 String8 threadName;
93
94 mStreamingProcessor = new StreamingProcessor(this);
95 threadName = String8::format("C2-%d-StreamProc",
96 mCameraId);
97 mStreamingProcessor->run(threadName.string());
98
99 mFrameProcessor = new FrameProcessor(mDevice, this);
100 threadName = String8::format("C2-%d-FrameProc",
101 mCameraId);
102 mFrameProcessor->run(threadName.string());
103
104 mCaptureSequencer = new CaptureSequencer(this);
105 threadName = String8::format("C2-%d-CaptureSeq",
106 mCameraId);
107 mCaptureSequencer->run(threadName.string());
108
109 mJpegProcessor = new JpegProcessor(this, mCaptureSequencer);
110 threadName = String8::format("C2-%d-JpegProc",
111 mCameraId);
112 mJpegProcessor->run(threadName.string());
113
114 switch (mDeviceVersion) {
115 case CAMERA_DEVICE_API_VERSION_2_0: {
116 sp<ZslProcessor> zslProc =
117 new ZslProcessor(this, mCaptureSequencer);
118 mZslProcessor = zslProc;
119 mZslProcessorThread = zslProc;
120 break;
121 }
122 case CAMERA_DEVICE_API_VERSION_3_0:
123 case CAMERA_DEVICE_API_VERSION_3_1:
124 case CAMERA_DEVICE_API_VERSION_3_2: {
125 sp<ZslProcessor3> zslProc =
126 new ZslProcessor3(this, mCaptureSequencer);
127 mZslProcessor = zslProc;
128 mZslProcessorThread = zslProc;
129 break;
130 }
131 default:
132 break;
133 }
134 threadName = String8::format("C2-%d-ZslProc",
135 mCameraId);
136 mZslProcessorThread->run(threadName.string());
137
138 mCallbackProcessor = new CallbackProcessor(this);
139 threadName = String8::format("C2-%d-CallbkProc",
140 mCameraId);
141 mCallbackProcessor->run(threadName.string());
142
143 if (gLogLevel >= 1) {
144 SharedParameters::Lock l(mParameters);
145 ALOGD("%s: Default parameters converted from camera %d:", __FUNCTION__,
146 mCameraId);
147 ALOGD("%s", l.mParameters.paramsFlattened.string());
148 }
149
150 return OK;
151 }
152
~Camera2Client()153 Camera2Client::~Camera2Client() {
154 ATRACE_CALL();
155 ALOGV("~Camera2Client");
156
157 mDestructionStarted = true;
158
159 disconnect();
160
161 ALOGI("Camera %d: Closed", mCameraId);
162 }
163
dump(int fd,const Vector<String16> & args)164 status_t Camera2Client::dump(int fd, const Vector<String16>& args) {
165 String8 result;
166 result.appendFormat("Client2[%d] (%p) Client: %s PID: %d, dump:\n",
167 mCameraId,
168 getRemoteCallback()->asBinder().get(),
169 String8(mClientPackageName).string(),
170 mClientPid);
171 result.append(" State: ");
172 #define CASE_APPEND_ENUM(x) case x: result.append(#x "\n"); break;
173
174 const Parameters& p = mParameters.unsafeAccess();
175
176 result.append(Parameters::getStateName(p.state));
177
178 result.append("\n Current parameters:\n");
179 result.appendFormat(" Preview size: %d x %d\n",
180 p.previewWidth, p.previewHeight);
181 result.appendFormat(" Preview FPS range: %d - %d\n",
182 p.previewFpsRange[0], p.previewFpsRange[1]);
183 result.appendFormat(" Preview HAL pixel format: 0x%x\n",
184 p.previewFormat);
185 result.appendFormat(" Preview transform: %x\n",
186 p.previewTransform);
187 result.appendFormat(" Picture size: %d x %d\n",
188 p.pictureWidth, p.pictureHeight);
189 result.appendFormat(" Jpeg thumbnail size: %d x %d\n",
190 p.jpegThumbSize[0], p.jpegThumbSize[1]);
191 result.appendFormat(" Jpeg quality: %d, thumbnail quality: %d\n",
192 p.jpegQuality, p.jpegThumbQuality);
193 result.appendFormat(" Jpeg rotation: %d\n", p.jpegRotation);
194 result.appendFormat(" GPS tags %s\n",
195 p.gpsEnabled ? "enabled" : "disabled");
196 if (p.gpsEnabled) {
197 result.appendFormat(" GPS lat x long x alt: %f x %f x %f\n",
198 p.gpsCoordinates[0], p.gpsCoordinates[1],
199 p.gpsCoordinates[2]);
200 result.appendFormat(" GPS timestamp: %" PRId64 "\n",
201 p.gpsTimestamp);
202 result.appendFormat(" GPS processing method: %s\n",
203 p.gpsProcessingMethod.string());
204 }
205
206 result.append(" White balance mode: ");
207 switch (p.wbMode) {
208 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_AUTO)
209 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_INCANDESCENT)
210 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_FLUORESCENT)
211 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_WARM_FLUORESCENT)
212 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_DAYLIGHT)
213 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_CLOUDY_DAYLIGHT)
214 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_TWILIGHT)
215 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_SHADE)
216 default: result.append("UNKNOWN\n");
217 }
218
219 result.append(" Effect mode: ");
220 switch (p.effectMode) {
221 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_OFF)
222 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_MONO)
223 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_NEGATIVE)
224 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_SOLARIZE)
225 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_SEPIA)
226 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_POSTERIZE)
227 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_WHITEBOARD)
228 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_BLACKBOARD)
229 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_AQUA)
230 default: result.append("UNKNOWN\n");
231 }
232
233 result.append(" Antibanding mode: ");
234 switch (p.antibandingMode) {
235 CASE_APPEND_ENUM(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO)
236 CASE_APPEND_ENUM(ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF)
237 CASE_APPEND_ENUM(ANDROID_CONTROL_AE_ANTIBANDING_MODE_50HZ)
238 CASE_APPEND_ENUM(ANDROID_CONTROL_AE_ANTIBANDING_MODE_60HZ)
239 default: result.append("UNKNOWN\n");
240 }
241
242 result.append(" Scene mode: ");
243 switch (p.sceneMode) {
244 case ANDROID_CONTROL_SCENE_MODE_DISABLED:
245 result.append("AUTO\n"); break;
246 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_ACTION)
247 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_PORTRAIT)
248 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_LANDSCAPE)
249 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_NIGHT)
250 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_NIGHT_PORTRAIT)
251 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_THEATRE)
252 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_BEACH)
253 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_SNOW)
254 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_SUNSET)
255 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_STEADYPHOTO)
256 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_FIREWORKS)
257 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_SPORTS)
258 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_PARTY)
259 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_CANDLELIGHT)
260 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_BARCODE)
261 default: result.append("UNKNOWN\n");
262 }
263
264 result.append(" Flash mode: ");
265 switch (p.flashMode) {
266 CASE_APPEND_ENUM(Parameters::FLASH_MODE_OFF)
267 CASE_APPEND_ENUM(Parameters::FLASH_MODE_AUTO)
268 CASE_APPEND_ENUM(Parameters::FLASH_MODE_ON)
269 CASE_APPEND_ENUM(Parameters::FLASH_MODE_TORCH)
270 CASE_APPEND_ENUM(Parameters::FLASH_MODE_RED_EYE)
271 CASE_APPEND_ENUM(Parameters::FLASH_MODE_INVALID)
272 default: result.append("UNKNOWN\n");
273 }
274
275 result.append(" Focus mode: ");
276 switch (p.focusMode) {
277 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_AUTO)
278 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_MACRO)
279 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_CONTINUOUS_VIDEO)
280 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_CONTINUOUS_PICTURE)
281 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_EDOF)
282 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_INFINITY)
283 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_FIXED)
284 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_INVALID)
285 default: result.append("UNKNOWN\n");
286 }
287
288 result.append(" Focus state: ");
289 switch (p.focusState) {
290 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_INACTIVE)
291 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN)
292 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED)
293 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_PASSIVE_UNFOCUSED)
294 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN)
295 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED)
296 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED)
297 default: result.append("UNKNOWN\n");
298 }
299
300 result.append(" Focusing areas:\n");
301 for (size_t i = 0; i < p.focusingAreas.size(); i++) {
302 result.appendFormat(" [ (%d, %d, %d, %d), weight %d ]\n",
303 p.focusingAreas[i].left,
304 p.focusingAreas[i].top,
305 p.focusingAreas[i].right,
306 p.focusingAreas[i].bottom,
307 p.focusingAreas[i].weight);
308 }
309
310 result.appendFormat(" Exposure compensation index: %d\n",
311 p.exposureCompensation);
312
313 result.appendFormat(" AE lock %s, AWB lock %s\n",
314 p.autoExposureLock ? "enabled" : "disabled",
315 p.autoWhiteBalanceLock ? "enabled" : "disabled" );
316
317 result.appendFormat(" Metering areas:\n");
318 for (size_t i = 0; i < p.meteringAreas.size(); i++) {
319 result.appendFormat(" [ (%d, %d, %d, %d), weight %d ]\n",
320 p.meteringAreas[i].left,
321 p.meteringAreas[i].top,
322 p.meteringAreas[i].right,
323 p.meteringAreas[i].bottom,
324 p.meteringAreas[i].weight);
325 }
326
327 result.appendFormat(" Zoom index: %d\n", p.zoom);
328 result.appendFormat(" Video size: %d x %d\n", p.videoWidth,
329 p.videoHeight);
330
331 result.appendFormat(" Recording hint is %s\n",
332 p.recordingHint ? "set" : "not set");
333
334 result.appendFormat(" Video stabilization is %s\n",
335 p.videoStabilization ? "enabled" : "disabled");
336
337 result.appendFormat(" Selected still capture FPS range: %d - %d\n",
338 p.fastInfo.bestStillCaptureFpsRange[0],
339 p.fastInfo.bestStillCaptureFpsRange[1]);
340
341 result.append(" Current streams:\n");
342 result.appendFormat(" Preview stream ID: %d\n",
343 getPreviewStreamId());
344 result.appendFormat(" Capture stream ID: %d\n",
345 getCaptureStreamId());
346 result.appendFormat(" Recording stream ID: %d\n",
347 getRecordingStreamId());
348
349 result.append(" Quirks for this camera:\n");
350 bool haveQuirk = false;
351 if (p.quirks.triggerAfWithAuto) {
352 result.appendFormat(" triggerAfWithAuto\n");
353 haveQuirk = true;
354 }
355 if (p.quirks.useZslFormat) {
356 result.appendFormat(" useZslFormat\n");
357 haveQuirk = true;
358 }
359 if (p.quirks.meteringCropRegion) {
360 result.appendFormat(" meteringCropRegion\n");
361 haveQuirk = true;
362 }
363 if (p.quirks.partialResults) {
364 result.appendFormat(" usePartialResult\n");
365 haveQuirk = true;
366 }
367 if (!haveQuirk) {
368 result.appendFormat(" none\n");
369 }
370
371 write(fd, result.string(), result.size());
372
373 mStreamingProcessor->dump(fd, args);
374
375 mCaptureSequencer->dump(fd, args);
376
377 mFrameProcessor->dump(fd, args);
378
379 mZslProcessor->dump(fd, args);
380
381 return dumpDevice(fd, args);
382 #undef CASE_APPEND_ENUM
383 }
384
385 // ICamera interface
386
disconnect()387 void Camera2Client::disconnect() {
388 ATRACE_CALL();
389 Mutex::Autolock icl(mBinderSerializationLock);
390
391 // Allow both client and the media server to disconnect at all times
392 int callingPid = getCallingPid();
393 if (callingPid != mClientPid && callingPid != mServicePid) return;
394
395 if (mDevice == 0) return;
396
397 ALOGV("Camera %d: Shutting down", mCameraId);
398
399 /**
400 * disconnect() cannot call any methods that might need to promote a
401 * wp<Camera2Client>, since disconnect can be called from the destructor, at
402 * which point all such promotions will fail.
403 */
404
405 stopPreviewL();
406
407 {
408 SharedParameters::Lock l(mParameters);
409 if (l.mParameters.state == Parameters::DISCONNECTED) return;
410 l.mParameters.state = Parameters::DISCONNECTED;
411 }
412
413 mStreamingProcessor->requestExit();
414 mFrameProcessor->requestExit();
415 mCaptureSequencer->requestExit();
416 mJpegProcessor->requestExit();
417 mZslProcessorThread->requestExit();
418 mCallbackProcessor->requestExit();
419
420 ALOGV("Camera %d: Waiting for threads", mCameraId);
421
422 {
423 // Don't wait with lock held, in case the other threads need to
424 // complete callbacks that re-enter Camera2Client
425 mBinderSerializationLock.unlock();
426
427 mStreamingProcessor->join();
428 mFrameProcessor->join();
429 mCaptureSequencer->join();
430 mJpegProcessor->join();
431 mZslProcessorThread->join();
432 mCallbackProcessor->join();
433
434 mBinderSerializationLock.lock();
435 }
436
437 ALOGV("Camera %d: Deleting streams", mCameraId);
438
439 mStreamingProcessor->deletePreviewStream();
440 mStreamingProcessor->deleteRecordingStream();
441 mJpegProcessor->deleteStream();
442 mCallbackProcessor->deleteStream();
443 mZslProcessor->deleteStream();
444
445 // Remove all ZSL stream state before disconnect; needed to work around b/15408128.
446 mZslProcessor->disconnect();
447
448 ALOGV("Camera %d: Disconnecting device", mCameraId);
449
450 mDevice->disconnect();
451
452 mDevice.clear();
453
454 CameraService::Client::disconnect();
455 }
456
connect(const sp<ICameraClient> & client)457 status_t Camera2Client::connect(const sp<ICameraClient>& client) {
458 ATRACE_CALL();
459 ALOGV("%s: E", __FUNCTION__);
460 Mutex::Autolock icl(mBinderSerializationLock);
461
462 if (mClientPid != 0 && getCallingPid() != mClientPid) {
463 ALOGE("%s: Camera %d: Connection attempt from pid %d; "
464 "current locked to pid %d", __FUNCTION__,
465 mCameraId, getCallingPid(), mClientPid);
466 return BAD_VALUE;
467 }
468
469 mClientPid = getCallingPid();
470
471 mRemoteCallback = client;
472 mSharedCameraCallbacks = client;
473
474 return OK;
475 }
476
lock()477 status_t Camera2Client::lock() {
478 ATRACE_CALL();
479 ALOGV("%s: E", __FUNCTION__);
480 Mutex::Autolock icl(mBinderSerializationLock);
481 ALOGV("%s: Camera %d: Lock call from pid %d; current client pid %d",
482 __FUNCTION__, mCameraId, getCallingPid(), mClientPid);
483
484 if (mClientPid == 0) {
485 mClientPid = getCallingPid();
486 return OK;
487 }
488
489 if (mClientPid != getCallingPid()) {
490 ALOGE("%s: Camera %d: Lock call from pid %d; currently locked to pid %d",
491 __FUNCTION__, mCameraId, getCallingPid(), mClientPid);
492 return EBUSY;
493 }
494
495 return OK;
496 }
497
unlock()498 status_t Camera2Client::unlock() {
499 ATRACE_CALL();
500 ALOGV("%s: E", __FUNCTION__);
501 Mutex::Autolock icl(mBinderSerializationLock);
502 ALOGV("%s: Camera %d: Unlock call from pid %d; current client pid %d",
503 __FUNCTION__, mCameraId, getCallingPid(), mClientPid);
504
505 if (mClientPid == getCallingPid()) {
506 SharedParameters::Lock l(mParameters);
507 if (l.mParameters.state == Parameters::RECORD ||
508 l.mParameters.state == Parameters::VIDEO_SNAPSHOT) {
509 ALOGD("Not allowed to unlock camera during recording.");
510 return INVALID_OPERATION;
511 }
512 mClientPid = 0;
513 mRemoteCallback.clear();
514 mSharedCameraCallbacks.clear();
515 return OK;
516 }
517
518 ALOGE("%s: Camera %d: Unlock call from pid %d; currently locked to pid %d",
519 __FUNCTION__, mCameraId, getCallingPid(), mClientPid);
520 return EBUSY;
521 }
522
setPreviewTarget(const sp<IGraphicBufferProducer> & bufferProducer)523 status_t Camera2Client::setPreviewTarget(
524 const sp<IGraphicBufferProducer>& bufferProducer) {
525 ATRACE_CALL();
526 ALOGV("%s: E", __FUNCTION__);
527 Mutex::Autolock icl(mBinderSerializationLock);
528 status_t res;
529 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
530
531 sp<IBinder> binder;
532 sp<ANativeWindow> window;
533 if (bufferProducer != 0) {
534 binder = bufferProducer->asBinder();
535 // Using controlledByApp flag to ensure that the buffer queue remains in
536 // async mode for the old camera API, where many applications depend
537 // on that behavior.
538 window = new Surface(bufferProducer, /*controlledByApp*/ true);
539 }
540 return setPreviewWindowL(binder, window);
541 }
542
setPreviewWindowL(const sp<IBinder> & binder,sp<ANativeWindow> window)543 status_t Camera2Client::setPreviewWindowL(const sp<IBinder>& binder,
544 sp<ANativeWindow> window) {
545 ATRACE_CALL();
546 status_t res;
547
548 if (binder == mPreviewSurface) {
549 ALOGV("%s: Camera %d: New window is same as old window",
550 __FUNCTION__, mCameraId);
551 return NO_ERROR;
552 }
553
554 Parameters::State state;
555 {
556 SharedParameters::Lock l(mParameters);
557 state = l.mParameters.state;
558 }
559 switch (state) {
560 case Parameters::DISCONNECTED:
561 case Parameters::RECORD:
562 case Parameters::STILL_CAPTURE:
563 case Parameters::VIDEO_SNAPSHOT:
564 ALOGE("%s: Camera %d: Cannot set preview display while in state %s",
565 __FUNCTION__, mCameraId,
566 Parameters::getStateName(state));
567 return INVALID_OPERATION;
568 case Parameters::STOPPED:
569 case Parameters::WAITING_FOR_PREVIEW_WINDOW:
570 // OK
571 break;
572 case Parameters::PREVIEW:
573 // Already running preview - need to stop and create a new stream
574 res = stopStream();
575 if (res != OK) {
576 ALOGE("%s: Unable to stop preview to swap windows: %s (%d)",
577 __FUNCTION__, strerror(-res), res);
578 return res;
579 }
580 state = Parameters::WAITING_FOR_PREVIEW_WINDOW;
581 break;
582 }
583
584 mPreviewSurface = binder;
585 res = mStreamingProcessor->setPreviewWindow(window);
586 if (res != OK) {
587 ALOGE("%s: Unable to set new preview window: %s (%d)",
588 __FUNCTION__, strerror(-res), res);
589 return res;
590 }
591
592 if (state == Parameters::WAITING_FOR_PREVIEW_WINDOW) {
593 SharedParameters::Lock l(mParameters);
594 l.mParameters.state = state;
595 return startPreviewL(l.mParameters, false);
596 }
597
598 return OK;
599 }
600
setPreviewCallbackFlag(int flag)601 void Camera2Client::setPreviewCallbackFlag(int flag) {
602 ATRACE_CALL();
603 ALOGV("%s: Camera %d: Flag 0x%x", __FUNCTION__, mCameraId, flag);
604 Mutex::Autolock icl(mBinderSerializationLock);
605
606 if ( checkPid(__FUNCTION__) != OK) return;
607
608 SharedParameters::Lock l(mParameters);
609 setPreviewCallbackFlagL(l.mParameters, flag);
610 }
611
setPreviewCallbackFlagL(Parameters & params,int flag)612 void Camera2Client::setPreviewCallbackFlagL(Parameters ¶ms, int flag) {
613 status_t res = OK;
614
615 switch(params.state) {
616 case Parameters::STOPPED:
617 case Parameters::WAITING_FOR_PREVIEW_WINDOW:
618 case Parameters::PREVIEW:
619 case Parameters::STILL_CAPTURE:
620 // OK
621 break;
622 default:
623 if (flag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
624 ALOGE("%s: Camera %d: Can't use preview callbacks "
625 "in state %d", __FUNCTION__, mCameraId, params.state);
626 return;
627 }
628 }
629
630 if (flag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK) {
631 ALOGV("%s: setting oneshot", __FUNCTION__);
632 params.previewCallbackOneShot = true;
633 }
634 if (params.previewCallbackFlags != (uint32_t)flag) {
635
636 if (params.previewCallbackSurface && flag != CAMERA_FRAME_CALLBACK_FLAG_NOOP) {
637 // Disable any existing preview callback window when enabling
638 // preview callback flags
639 res = mCallbackProcessor->setCallbackWindow(NULL);
640 if (res != OK) {
641 ALOGE("%s: Camera %d: Unable to clear preview callback surface:"
642 " %s (%d)", __FUNCTION__, mCameraId, strerror(-res), res);
643 return;
644 }
645 params.previewCallbackSurface = false;
646 }
647
648 params.previewCallbackFlags = flag;
649
650 if (params.state == Parameters::PREVIEW) {
651 res = startPreviewL(params, true);
652 if (res != OK) {
653 ALOGE("%s: Camera %d: Unable to refresh request in state %s",
654 __FUNCTION__, mCameraId,
655 Parameters::getStateName(params.state));
656 }
657 }
658 }
659 }
660
setPreviewCallbackTarget(const sp<IGraphicBufferProducer> & callbackProducer)661 status_t Camera2Client::setPreviewCallbackTarget(
662 const sp<IGraphicBufferProducer>& callbackProducer) {
663 ATRACE_CALL();
664 ALOGV("%s: E", __FUNCTION__);
665 Mutex::Autolock icl(mBinderSerializationLock);
666 status_t res;
667 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
668
669 sp<ANativeWindow> window;
670 if (callbackProducer != 0) {
671 window = new Surface(callbackProducer);
672 }
673
674 res = mCallbackProcessor->setCallbackWindow(window);
675 if (res != OK) {
676 ALOGE("%s: Camera %d: Unable to set preview callback surface: %s (%d)",
677 __FUNCTION__, mCameraId, strerror(-res), res);
678 return res;
679 }
680
681 SharedParameters::Lock l(mParameters);
682
683 if (window != NULL) {
684 // Disable traditional callbacks when a valid callback target is given
685 l.mParameters.previewCallbackFlags = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
686 l.mParameters.previewCallbackOneShot = false;
687 l.mParameters.previewCallbackSurface = true;
688 } else {
689 // Disable callback target if given a NULL interface.
690 l.mParameters.previewCallbackSurface = false;
691 }
692
693 switch(l.mParameters.state) {
694 case Parameters::PREVIEW:
695 res = startPreviewL(l.mParameters, true);
696 break;
697 case Parameters::RECORD:
698 case Parameters::VIDEO_SNAPSHOT:
699 res = startRecordingL(l.mParameters, true);
700 break;
701 default:
702 break;
703 }
704 if (res != OK) {
705 ALOGE("%s: Camera %d: Unable to refresh request in state %s",
706 __FUNCTION__, mCameraId,
707 Parameters::getStateName(l.mParameters.state));
708 }
709
710 return OK;
711 }
712
713
startPreview()714 status_t Camera2Client::startPreview() {
715 ATRACE_CALL();
716 ALOGV("%s: E", __FUNCTION__);
717 Mutex::Autolock icl(mBinderSerializationLock);
718 status_t res;
719 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
720 SharedParameters::Lock l(mParameters);
721 return startPreviewL(l.mParameters, false);
722 }
723
startPreviewL(Parameters & params,bool restart)724 status_t Camera2Client::startPreviewL(Parameters ¶ms, bool restart) {
725 ATRACE_CALL();
726 status_t res;
727
728 ALOGV("%s: state == %d, restart = %d", __FUNCTION__, params.state, restart);
729
730 if ( (params.state == Parameters::PREVIEW ||
731 params.state == Parameters::RECORD ||
732 params.state == Parameters::VIDEO_SNAPSHOT)
733 && !restart) {
734 // Succeed attempt to re-enter a streaming state
735 ALOGI("%s: Camera %d: Preview already active, ignoring restart",
736 __FUNCTION__, mCameraId);
737 return OK;
738 }
739 if (params.state > Parameters::PREVIEW && !restart) {
740 ALOGE("%s: Can't start preview in state %s",
741 __FUNCTION__,
742 Parameters::getStateName(params.state));
743 return INVALID_OPERATION;
744 }
745
746 if (!mStreamingProcessor->haveValidPreviewWindow()) {
747 params.state = Parameters::WAITING_FOR_PREVIEW_WINDOW;
748 return OK;
749 }
750 params.state = Parameters::STOPPED;
751 int lastPreviewStreamId = mStreamingProcessor->getPreviewStreamId();
752
753 res = mStreamingProcessor->updatePreviewStream(params);
754 if (res != OK) {
755 ALOGE("%s: Camera %d: Unable to update preview stream: %s (%d)",
756 __FUNCTION__, mCameraId, strerror(-res), res);
757 return res;
758 }
759
760 bool previewStreamChanged = mStreamingProcessor->getPreviewStreamId() != lastPreviewStreamId;
761
762 // We could wait to create the JPEG output stream until first actual use
763 // (first takePicture call). However, this would substantially increase the
764 // first capture latency on HAL3 devices, and potentially on some HAL2
765 // devices. So create it unconditionally at preview start. As a drawback,
766 // this increases gralloc memory consumption for applications that don't
767 // ever take a picture.
768 // TODO: Find a better compromise, though this likely would involve HAL
769 // changes.
770 int lastJpegStreamId = mJpegProcessor->getStreamId();
771 res = updateProcessorStream(mJpegProcessor, params);
772 if (res != OK) {
773 ALOGE("%s: Camera %d: Can't pre-configure still image "
774 "stream: %s (%d)",
775 __FUNCTION__, mCameraId, strerror(-res), res);
776 return res;
777 }
778 bool jpegStreamChanged = mJpegProcessor->getStreamId() != lastJpegStreamId;
779
780 Vector<int32_t> outputStreams;
781 bool callbacksEnabled = (params.previewCallbackFlags &
782 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) ||
783 params.previewCallbackSurface;
784
785 if (callbacksEnabled) {
786 // Can't have recording stream hanging around when enabling callbacks,
787 // since it exceeds the max stream count on some devices.
788 if (mStreamingProcessor->getRecordingStreamId() != NO_STREAM) {
789 ALOGV("%s: Camera %d: Clearing out recording stream before "
790 "creating callback stream", __FUNCTION__, mCameraId);
791 res = mStreamingProcessor->stopStream();
792 if (res != OK) {
793 ALOGE("%s: Camera %d: Can't stop streaming to delete "
794 "recording stream", __FUNCTION__, mCameraId);
795 return res;
796 }
797 res = mStreamingProcessor->deleteRecordingStream();
798 if (res != OK) {
799 ALOGE("%s: Camera %d: Unable to delete recording stream before "
800 "enabling callbacks: %s (%d)", __FUNCTION__, mCameraId,
801 strerror(-res), res);
802 return res;
803 }
804 }
805
806 res = mCallbackProcessor->updateStream(params);
807 if (res != OK) {
808 ALOGE("%s: Camera %d: Unable to update callback stream: %s (%d)",
809 __FUNCTION__, mCameraId, strerror(-res), res);
810 return res;
811 }
812 outputStreams.push(getCallbackStreamId());
813 } else if (previewStreamChanged && mCallbackProcessor->getStreamId() != NO_STREAM) {
814 /**
815 * Delete the unused callback stream when preview stream is changed and
816 * preview is not enabled. Don't need stop preview stream as preview is in
817 * STOPPED state now.
818 */
819 ALOGV("%s: Camera %d: Delete unused preview callback stream.", __FUNCTION__, mCameraId);
820 res = mCallbackProcessor->deleteStream();
821 if (res != OK) {
822 ALOGE("%s: Camera %d: Unable to delete callback stream %s (%d)",
823 __FUNCTION__, mCameraId, strerror(-res), res);
824 return res;
825 }
826 }
827
828 if (params.zslMode && !params.recordingHint &&
829 getRecordingStreamId() == NO_STREAM) {
830 res = updateProcessorStream(mZslProcessor, params);
831 if (res != OK) {
832 ALOGE("%s: Camera %d: Unable to update ZSL stream: %s (%d)",
833 __FUNCTION__, mCameraId, strerror(-res), res);
834 return res;
835 }
836
837 if (jpegStreamChanged) {
838 ALOGV("%s: Camera %d: Clear ZSL buffer queue when Jpeg size is changed",
839 __FUNCTION__, mCameraId);
840 mZslProcessor->clearZslQueue();
841 }
842 outputStreams.push(getZslStreamId());
843 } else {
844 mZslProcessor->deleteStream();
845 }
846
847 outputStreams.push(getPreviewStreamId());
848
849 if (!params.recordingHint) {
850 if (!restart) {
851 res = mStreamingProcessor->updatePreviewRequest(params);
852 if (res != OK) {
853 ALOGE("%s: Camera %d: Can't set up preview request: "
854 "%s (%d)", __FUNCTION__, mCameraId,
855 strerror(-res), res);
856 return res;
857 }
858 }
859 res = mStreamingProcessor->startStream(StreamingProcessor::PREVIEW,
860 outputStreams);
861 } else {
862 if (!restart) {
863 res = mStreamingProcessor->updateRecordingRequest(params);
864 if (res != OK) {
865 ALOGE("%s: Camera %d: Can't set up preview request with "
866 "record hint: %s (%d)", __FUNCTION__, mCameraId,
867 strerror(-res), res);
868 return res;
869 }
870 }
871 res = mStreamingProcessor->startStream(StreamingProcessor::RECORD,
872 outputStreams);
873 }
874 if (res != OK) {
875 ALOGE("%s: Camera %d: Unable to start streaming preview: %s (%d)",
876 __FUNCTION__, mCameraId, strerror(-res), res);
877 return res;
878 }
879
880 params.state = Parameters::PREVIEW;
881 return OK;
882 }
883
stopPreview()884 void Camera2Client::stopPreview() {
885 ATRACE_CALL();
886 ALOGV("%s: E", __FUNCTION__);
887 Mutex::Autolock icl(mBinderSerializationLock);
888 status_t res;
889 if ( (res = checkPid(__FUNCTION__) ) != OK) return;
890 stopPreviewL();
891 }
892
stopPreviewL()893 void Camera2Client::stopPreviewL() {
894 ATRACE_CALL();
895 status_t res;
896 const nsecs_t kStopCaptureTimeout = 3000000000LL; // 3 seconds
897 Parameters::State state;
898 {
899 SharedParameters::Lock l(mParameters);
900 state = l.mParameters.state;
901 }
902
903 switch (state) {
904 case Parameters::DISCONNECTED:
905 // Nothing to do.
906 break;
907 case Parameters::STOPPED:
908 case Parameters::VIDEO_SNAPSHOT:
909 case Parameters::STILL_CAPTURE:
910 mCaptureSequencer->waitUntilIdle(kStopCaptureTimeout);
911 // no break
912 case Parameters::RECORD:
913 case Parameters::PREVIEW:
914 syncWithDevice();
915 res = stopStream();
916 if (res != OK) {
917 ALOGE("%s: Camera %d: Can't stop streaming: %s (%d)",
918 __FUNCTION__, mCameraId, strerror(-res), res);
919 }
920
921 // Flush all in-process captures and buffer in order to stop
922 // preview faster.
923 res = mDevice->flush();
924 if (res != OK) {
925 ALOGE("%s: Camera %d: Unable to flush pending requests: %s (%d)",
926 __FUNCTION__, mCameraId, strerror(-res), res);
927 }
928
929 res = mDevice->waitUntilDrained();
930 if (res != OK) {
931 ALOGE("%s: Camera %d: Waiting to stop streaming failed: %s (%d)",
932 __FUNCTION__, mCameraId, strerror(-res), res);
933 }
934 // Clean up recording stream
935 res = mStreamingProcessor->deleteRecordingStream();
936 if (res != OK) {
937 ALOGE("%s: Camera %d: Unable to delete recording stream before "
938 "stop preview: %s (%d)",
939 __FUNCTION__, mCameraId, strerror(-res), res);
940 }
941 // no break
942 case Parameters::WAITING_FOR_PREVIEW_WINDOW: {
943 SharedParameters::Lock l(mParameters);
944 l.mParameters.state = Parameters::STOPPED;
945 commandStopFaceDetectionL(l.mParameters);
946 break;
947 }
948 default:
949 ALOGE("%s: Camera %d: Unknown state %d", __FUNCTION__, mCameraId,
950 state);
951 }
952 }
953
previewEnabled()954 bool Camera2Client::previewEnabled() {
955 ATRACE_CALL();
956 Mutex::Autolock icl(mBinderSerializationLock);
957 status_t res;
958 if ( (res = checkPid(__FUNCTION__) ) != OK) return false;
959
960 SharedParameters::Lock l(mParameters);
961 return l.mParameters.state == Parameters::PREVIEW;
962 }
963
storeMetaDataInBuffers(bool enabled)964 status_t Camera2Client::storeMetaDataInBuffers(bool enabled) {
965 ATRACE_CALL();
966 Mutex::Autolock icl(mBinderSerializationLock);
967 status_t res;
968 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
969
970 SharedParameters::Lock l(mParameters);
971 switch (l.mParameters.state) {
972 case Parameters::RECORD:
973 case Parameters::VIDEO_SNAPSHOT:
974 ALOGE("%s: Camera %d: Can't be called in state %s",
975 __FUNCTION__, mCameraId,
976 Parameters::getStateName(l.mParameters.state));
977 return INVALID_OPERATION;
978 default:
979 // OK
980 break;
981 }
982
983 l.mParameters.storeMetadataInBuffers = enabled;
984
985 return OK;
986 }
987
startRecording()988 status_t Camera2Client::startRecording() {
989 ATRACE_CALL();
990 ALOGV("%s: E", __FUNCTION__);
991 Mutex::Autolock icl(mBinderSerializationLock);
992 status_t res;
993 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
994 SharedParameters::Lock l(mParameters);
995
996 return startRecordingL(l.mParameters, false);
997 }
998
startRecordingL(Parameters & params,bool restart)999 status_t Camera2Client::startRecordingL(Parameters ¶ms, bool restart) {
1000 status_t res;
1001
1002 ALOGV("%s: state == %d, restart = %d", __FUNCTION__, params.state, restart);
1003
1004 switch (params.state) {
1005 case Parameters::STOPPED:
1006 res = startPreviewL(params, false);
1007 if (res != OK) return res;
1008 // Make sure first preview request is submitted to the HAL device to avoid
1009 // two consecutive set of configure_streams being called into the HAL.
1010 // TODO: Refactor this to avoid initial preview configuration.
1011 syncWithDevice();
1012 break;
1013 case Parameters::PREVIEW:
1014 // Ready to go
1015 break;
1016 case Parameters::RECORD:
1017 case Parameters::VIDEO_SNAPSHOT:
1018 // OK to call this when recording is already on, just skip unless
1019 // we're looking to restart
1020 if (!restart) return OK;
1021 break;
1022 default:
1023 ALOGE("%s: Camera %d: Can't start recording in state %s",
1024 __FUNCTION__, mCameraId,
1025 Parameters::getStateName(params.state));
1026 return INVALID_OPERATION;
1027 };
1028
1029 if (!params.storeMetadataInBuffers) {
1030 ALOGE("%s: Camera %d: Recording only supported in metadata mode, but "
1031 "non-metadata recording mode requested!", __FUNCTION__,
1032 mCameraId);
1033 return INVALID_OPERATION;
1034 }
1035
1036 if (!restart) {
1037 mCameraService->playSound(CameraService::SOUND_RECORDING);
1038 mStreamingProcessor->updateRecordingRequest(params);
1039 if (res != OK) {
1040 ALOGE("%s: Camera %d: Unable to update recording request: %s (%d)",
1041 __FUNCTION__, mCameraId, strerror(-res), res);
1042 return res;
1043 }
1044 }
1045
1046 // Not all devices can support a preview callback stream and a recording
1047 // stream at the same time, so assume none of them can.
1048 if (mCallbackProcessor->getStreamId() != NO_STREAM) {
1049 ALOGV("%s: Camera %d: Clearing out callback stream before "
1050 "creating recording stream", __FUNCTION__, mCameraId);
1051 res = mStreamingProcessor->stopStream();
1052 if (res != OK) {
1053 ALOGE("%s: Camera %d: Can't stop streaming to delete callback stream",
1054 __FUNCTION__, mCameraId);
1055 return res;
1056 }
1057 res = mCallbackProcessor->deleteStream();
1058 if (res != OK) {
1059 ALOGE("%s: Camera %d: Unable to delete callback stream before "
1060 "record: %s (%d)", __FUNCTION__, mCameraId,
1061 strerror(-res), res);
1062 return res;
1063 }
1064 }
1065
1066 // On current HALs, clean up ZSL before transitioning into recording
1067 if (mDeviceVersion != CAMERA_DEVICE_API_VERSION_2_0) {
1068 if (mZslProcessor->getStreamId() != NO_STREAM) {
1069 ALOGV("%s: Camera %d: Clearing out zsl stream before "
1070 "creating recording stream", __FUNCTION__, mCameraId);
1071 res = mStreamingProcessor->stopStream();
1072 if (res != OK) {
1073 ALOGE("%s: Camera %d: Can't stop streaming to delete callback stream",
1074 __FUNCTION__, mCameraId);
1075 return res;
1076 }
1077 res = mDevice->waitUntilDrained();
1078 if (res != OK) {
1079 ALOGE("%s: Camera %d: Waiting to stop streaming failed: %s (%d)",
1080 __FUNCTION__, mCameraId, strerror(-res), res);
1081 }
1082 res = mZslProcessor->clearZslQueue();
1083 if (res != OK) {
1084 ALOGE("%s: Camera %d: Can't clear zsl queue",
1085 __FUNCTION__, mCameraId);
1086 return res;
1087 }
1088 res = mZslProcessor->deleteStream();
1089 if (res != OK) {
1090 ALOGE("%s: Camera %d: Unable to delete zsl stream before "
1091 "record: %s (%d)", __FUNCTION__, mCameraId,
1092 strerror(-res), res);
1093 return res;
1094 }
1095 }
1096 }
1097
1098 // Disable callbacks if they're enabled; can't record and use callbacks,
1099 // and we can't fail record start without stagefright asserting.
1100 params.previewCallbackFlags = 0;
1101
1102 if (mDeviceVersion != CAMERA_DEVICE_API_VERSION_2_0) {
1103 // For newer devices, may need to reconfigure video snapshot JPEG sizes
1104 // during recording startup, so need a more complex sequence here to
1105 // ensure an early stream reconfiguration doesn't happen
1106 bool recordingStreamNeedsUpdate;
1107 res = mStreamingProcessor->recordingStreamNeedsUpdate(params, &recordingStreamNeedsUpdate);
1108 if (res != OK) {
1109 ALOGE("%s: Camera %d: Can't query recording stream",
1110 __FUNCTION__, mCameraId);
1111 return res;
1112 }
1113
1114 if (recordingStreamNeedsUpdate) {
1115 // Need to stop stream here so updateProcessorStream won't trigger configureStream
1116 // Right now camera device cannot handle configureStream failure gracefully
1117 // when device is streaming
1118 res = mStreamingProcessor->stopStream();
1119 if (res != OK) {
1120 ALOGE("%s: Camera %d: Can't stop streaming to update record "
1121 "stream", __FUNCTION__, mCameraId);
1122 return res;
1123 }
1124 res = mDevice->waitUntilDrained();
1125 if (res != OK) {
1126 ALOGE("%s: Camera %d: Waiting to stop streaming failed: "
1127 "%s (%d)", __FUNCTION__, mCameraId,
1128 strerror(-res), res);
1129 }
1130
1131 res = updateProcessorStream<
1132 StreamingProcessor,
1133 &StreamingProcessor::updateRecordingStream>(
1134 mStreamingProcessor,
1135 params);
1136 if (res != OK) {
1137 ALOGE("%s: Camera %d: Unable to update recording stream: "
1138 "%s (%d)", __FUNCTION__, mCameraId,
1139 strerror(-res), res);
1140 return res;
1141 }
1142 }
1143 } else {
1144 // Maintain call sequencing for HALv2 devices.
1145 res = updateProcessorStream<
1146 StreamingProcessor,
1147 &StreamingProcessor::updateRecordingStream>(mStreamingProcessor,
1148 params);
1149 if (res != OK) {
1150 ALOGE("%s: Camera %d: Unable to update recording stream: %s (%d)",
1151 __FUNCTION__, mCameraId, strerror(-res), res);
1152 return res;
1153 }
1154 }
1155
1156 Vector<int32_t> outputStreams;
1157 outputStreams.push(getPreviewStreamId());
1158 outputStreams.push(getRecordingStreamId());
1159
1160 res = mStreamingProcessor->startStream(StreamingProcessor::RECORD,
1161 outputStreams);
1162
1163 // startStream might trigger a configureStream call and device might fail
1164 // configureStream due to jpeg size > video size. Try again with jpeg size overridden
1165 // to video size.
1166 if (res == BAD_VALUE) {
1167 overrideVideoSnapshotSize(params);
1168 res = mStreamingProcessor->startStream(StreamingProcessor::RECORD,
1169 outputStreams);
1170 }
1171
1172 if (res != OK) {
1173 ALOGE("%s: Camera %d: Unable to start recording stream: %s (%d)",
1174 __FUNCTION__, mCameraId, strerror(-res), res);
1175 return res;
1176 }
1177
1178 if (params.state < Parameters::RECORD) {
1179 params.state = Parameters::RECORD;
1180 }
1181
1182 return OK;
1183 }
1184
stopRecording()1185 void Camera2Client::stopRecording() {
1186 ATRACE_CALL();
1187 ALOGV("%s: E", __FUNCTION__);
1188 Mutex::Autolock icl(mBinderSerializationLock);
1189 SharedParameters::Lock l(mParameters);
1190
1191 status_t res;
1192 if ( (res = checkPid(__FUNCTION__) ) != OK) return;
1193
1194 switch (l.mParameters.state) {
1195 case Parameters::RECORD:
1196 // OK to stop
1197 break;
1198 case Parameters::STOPPED:
1199 case Parameters::PREVIEW:
1200 case Parameters::STILL_CAPTURE:
1201 case Parameters::VIDEO_SNAPSHOT:
1202 default:
1203 ALOGE("%s: Camera %d: Can't stop recording in state %s",
1204 __FUNCTION__, mCameraId,
1205 Parameters::getStateName(l.mParameters.state));
1206 return;
1207 };
1208
1209 mCameraService->playSound(CameraService::SOUND_RECORDING);
1210
1211 // Remove recording stream to prevent it from slowing down takePicture later
1212 if (!l.mParameters.recordingHint && l.mParameters.isJpegSizeOverridden()) {
1213 res = stopStream();
1214 if (res != OK) {
1215 ALOGE("%s: Camera %d: Can't stop streaming: %s (%d)",
1216 __FUNCTION__, mCameraId, strerror(-res), res);
1217 }
1218 res = mDevice->waitUntilDrained();
1219 if (res != OK) {
1220 ALOGE("%s: Camera %d: Waiting to stop streaming failed: %s (%d)",
1221 __FUNCTION__, mCameraId, strerror(-res), res);
1222 }
1223 // Clean up recording stream
1224 res = mStreamingProcessor->deleteRecordingStream();
1225 if (res != OK) {
1226 ALOGE("%s: Camera %d: Unable to delete recording stream before "
1227 "stop preview: %s (%d)",
1228 __FUNCTION__, mCameraId, strerror(-res), res);
1229 }
1230 l.mParameters.recoverOverriddenJpegSize();
1231 }
1232
1233 res = startPreviewL(l.mParameters, true);
1234 if (res != OK) {
1235 ALOGE("%s: Camera %d: Unable to return to preview",
1236 __FUNCTION__, mCameraId);
1237 }
1238 }
1239
recordingEnabled()1240 bool Camera2Client::recordingEnabled() {
1241 ATRACE_CALL();
1242 Mutex::Autolock icl(mBinderSerializationLock);
1243
1244 if ( checkPid(__FUNCTION__) != OK) return false;
1245
1246 return recordingEnabledL();
1247 }
1248
recordingEnabledL()1249 bool Camera2Client::recordingEnabledL() {
1250 ATRACE_CALL();
1251 SharedParameters::Lock l(mParameters);
1252
1253 return (l.mParameters.state == Parameters::RECORD
1254 || l.mParameters.state == Parameters::VIDEO_SNAPSHOT);
1255 }
1256
releaseRecordingFrame(const sp<IMemory> & mem)1257 void Camera2Client::releaseRecordingFrame(const sp<IMemory>& mem) {
1258 ATRACE_CALL();
1259 Mutex::Autolock icl(mBinderSerializationLock);
1260 if ( checkPid(__FUNCTION__) != OK) return;
1261
1262 mStreamingProcessor->releaseRecordingFrame(mem);
1263 }
1264
autoFocus()1265 status_t Camera2Client::autoFocus() {
1266 ATRACE_CALL();
1267 Mutex::Autolock icl(mBinderSerializationLock);
1268 ALOGV("%s: Camera %d", __FUNCTION__, mCameraId);
1269 status_t res;
1270 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
1271
1272 int triggerId;
1273 bool notifyImmediately = false;
1274 bool notifySuccess = false;
1275 {
1276 SharedParameters::Lock l(mParameters);
1277 if (l.mParameters.state < Parameters::PREVIEW) {
1278 ALOGE("%s: Camera %d: Call autoFocus when preview is inactive (state = %d).",
1279 __FUNCTION__, mCameraId, l.mParameters.state);
1280 return INVALID_OPERATION;
1281 }
1282
1283 /**
1284 * If the camera does not support auto-focus, it is a no-op and
1285 * onAutoFocus(boolean, Camera) callback will be called immediately
1286 * with a fake value of success set to true.
1287 *
1288 * Similarly, if focus mode is set to INFINITY, there's no reason to
1289 * bother the HAL.
1290 */
1291 if (l.mParameters.focusMode == Parameters::FOCUS_MODE_FIXED ||
1292 l.mParameters.focusMode == Parameters::FOCUS_MODE_INFINITY) {
1293 notifyImmediately = true;
1294 notifySuccess = true;
1295 }
1296 /**
1297 * If we're in CAF mode, and AF has already been locked, just fire back
1298 * the callback right away; the HAL would not send a notification since
1299 * no state change would happen on a AF trigger.
1300 */
1301 if ( (l.mParameters.focusMode == Parameters::FOCUS_MODE_CONTINUOUS_PICTURE ||
1302 l.mParameters.focusMode == Parameters::FOCUS_MODE_CONTINUOUS_VIDEO) &&
1303 l.mParameters.focusState == ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED ) {
1304 notifyImmediately = true;
1305 notifySuccess = true;
1306 }
1307 /**
1308 * Send immediate notification back to client
1309 */
1310 if (notifyImmediately) {
1311 SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
1312 if (l.mRemoteCallback != 0) {
1313 l.mRemoteCallback->notifyCallback(CAMERA_MSG_FOCUS,
1314 notifySuccess ? 1 : 0, 0);
1315 }
1316 return OK;
1317 }
1318 /**
1319 * Handle quirk mode for AF in scene modes
1320 */
1321 if (l.mParameters.quirks.triggerAfWithAuto &&
1322 l.mParameters.sceneMode != ANDROID_CONTROL_SCENE_MODE_DISABLED &&
1323 l.mParameters.focusMode != Parameters::FOCUS_MODE_AUTO &&
1324 !l.mParameters.focusingAreas[0].isEmpty()) {
1325 ALOGV("%s: Quirk: Switching from focusMode %d to AUTO",
1326 __FUNCTION__, l.mParameters.focusMode);
1327 l.mParameters.shadowFocusMode = l.mParameters.focusMode;
1328 l.mParameters.focusMode = Parameters::FOCUS_MODE_AUTO;
1329 updateRequests(l.mParameters);
1330 }
1331
1332 l.mParameters.currentAfTriggerId = ++l.mParameters.afTriggerCounter;
1333 triggerId = l.mParameters.currentAfTriggerId;
1334 }
1335 ATRACE_ASYNC_BEGIN(kAutofocusLabel, triggerId);
1336
1337 syncWithDevice();
1338
1339 mDevice->triggerAutofocus(triggerId);
1340
1341 return OK;
1342 }
1343
cancelAutoFocus()1344 status_t Camera2Client::cancelAutoFocus() {
1345 ATRACE_CALL();
1346 Mutex::Autolock icl(mBinderSerializationLock);
1347 ALOGV("%s: Camera %d", __FUNCTION__, mCameraId);
1348 status_t res;
1349 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
1350
1351 int triggerId;
1352 {
1353 SharedParameters::Lock l(mParameters);
1354 // Canceling does nothing in FIXED or INFINITY modes
1355 if (l.mParameters.focusMode == Parameters::FOCUS_MODE_FIXED ||
1356 l.mParameters.focusMode == Parameters::FOCUS_MODE_INFINITY) {
1357 return OK;
1358 }
1359
1360 // An active AF trigger is canceled
1361 if (l.mParameters.afTriggerCounter == l.mParameters.currentAfTriggerId) {
1362 ATRACE_ASYNC_END(kAutofocusLabel, l.mParameters.currentAfTriggerId);
1363 }
1364
1365 triggerId = ++l.mParameters.afTriggerCounter;
1366
1367 // When using triggerAfWithAuto quirk, may need to reset focus mode to
1368 // the real state at this point. No need to cancel explicitly if
1369 // changing the AF mode.
1370 if (l.mParameters.shadowFocusMode != Parameters::FOCUS_MODE_INVALID) {
1371 ALOGV("%s: Quirk: Restoring focus mode to %d", __FUNCTION__,
1372 l.mParameters.shadowFocusMode);
1373 l.mParameters.focusMode = l.mParameters.shadowFocusMode;
1374 l.mParameters.shadowFocusMode = Parameters::FOCUS_MODE_INVALID;
1375 updateRequests(l.mParameters);
1376
1377 return OK;
1378 }
1379 if (l.mParameters.zslMode) {
1380 mZslProcessor->clearZslQueue();
1381 }
1382 }
1383 syncWithDevice();
1384
1385 mDevice->triggerCancelAutofocus(triggerId);
1386
1387 return OK;
1388 }
1389
takePicture(int msgType)1390 status_t Camera2Client::takePicture(int msgType) {
1391 ATRACE_CALL();
1392 Mutex::Autolock icl(mBinderSerializationLock);
1393 status_t res;
1394 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
1395
1396 int takePictureCounter;
1397 {
1398 SharedParameters::Lock l(mParameters);
1399 switch (l.mParameters.state) {
1400 case Parameters::DISCONNECTED:
1401 case Parameters::STOPPED:
1402 case Parameters::WAITING_FOR_PREVIEW_WINDOW:
1403 ALOGE("%s: Camera %d: Cannot take picture without preview enabled",
1404 __FUNCTION__, mCameraId);
1405 return INVALID_OPERATION;
1406 case Parameters::PREVIEW:
1407 // Good to go for takePicture
1408 res = commandStopFaceDetectionL(l.mParameters);
1409 if (res != OK) {
1410 ALOGE("%s: Camera %d: Unable to stop face detection for still capture",
1411 __FUNCTION__, mCameraId);
1412 return res;
1413 }
1414 l.mParameters.state = Parameters::STILL_CAPTURE;
1415
1416 // Remove recording stream to prevent video snapshot jpeg logic kicking in
1417 if (l.mParameters.isJpegSizeOverridden() &&
1418 mStreamingProcessor->getRecordingStreamId() != NO_STREAM) {
1419 res = mStreamingProcessor->togglePauseStream(/*pause*/true);
1420 if (res != OK) {
1421 ALOGE("%s: Camera %d: Can't pause streaming: %s (%d)",
1422 __FUNCTION__, mCameraId, strerror(-res), res);
1423 }
1424 res = mDevice->waitUntilDrained();
1425 if (res != OK) {
1426 ALOGE("%s: Camera %d: Waiting to stop streaming failed: %s (%d)",
1427 __FUNCTION__, mCameraId, strerror(-res), res);
1428 }
1429 // Clean up recording stream
1430 res = mStreamingProcessor->deleteRecordingStream();
1431 if (res != OK) {
1432 ALOGE("%s: Camera %d: Unable to delete recording stream before "
1433 "stop preview: %s (%d)",
1434 __FUNCTION__, mCameraId, strerror(-res), res);
1435 }
1436 res = mStreamingProcessor->togglePauseStream(/*pause*/false);
1437 if (res != OK) {
1438 ALOGE("%s: Camera %d: Can't unpause streaming: %s (%d)",
1439 __FUNCTION__, mCameraId, strerror(-res), res);
1440 }
1441 l.mParameters.recoverOverriddenJpegSize();
1442 }
1443 break;
1444 case Parameters::RECORD:
1445 // Good to go for video snapshot
1446 l.mParameters.state = Parameters::VIDEO_SNAPSHOT;
1447 break;
1448 case Parameters::STILL_CAPTURE:
1449 case Parameters::VIDEO_SNAPSHOT:
1450 ALOGE("%s: Camera %d: Already taking a picture",
1451 __FUNCTION__, mCameraId);
1452 return INVALID_OPERATION;
1453 }
1454
1455 ALOGV("%s: Camera %d: Starting picture capture", __FUNCTION__, mCameraId);
1456
1457 int lastJpegStreamId = mJpegProcessor->getStreamId();
1458 res = updateProcessorStream(mJpegProcessor, l.mParameters);
1459 // If video snapshot fail to configureStream, try override video snapshot size to
1460 // video size
1461 if (res == BAD_VALUE && l.mParameters.state == Parameters::VIDEO_SNAPSHOT) {
1462 overrideVideoSnapshotSize(l.mParameters);
1463 res = updateProcessorStream(mJpegProcessor, l.mParameters);
1464 }
1465 if (res != OK) {
1466 ALOGE("%s: Camera %d: Can't set up still image stream: %s (%d)",
1467 __FUNCTION__, mCameraId, strerror(-res), res);
1468 return res;
1469 }
1470 takePictureCounter = ++l.mParameters.takePictureCounter;
1471
1472 // Clear ZSL buffer queue when Jpeg size is changed.
1473 bool jpegStreamChanged = mJpegProcessor->getStreamId() != lastJpegStreamId;
1474 if (l.mParameters.zslMode && jpegStreamChanged) {
1475 ALOGV("%s: Camera %d: Clear ZSL buffer queue when Jpeg size is changed",
1476 __FUNCTION__, mCameraId);
1477 mZslProcessor->clearZslQueue();
1478 }
1479 }
1480
1481 ATRACE_ASYNC_BEGIN(kTakepictureLabel, takePictureCounter);
1482
1483 // Need HAL to have correct settings before (possibly) triggering precapture
1484 syncWithDevice();
1485
1486 res = mCaptureSequencer->startCapture(msgType);
1487 if (res != OK) {
1488 ALOGE("%s: Camera %d: Unable to start capture: %s (%d)",
1489 __FUNCTION__, mCameraId, strerror(-res), res);
1490 }
1491
1492 return res;
1493 }
1494
setParameters(const String8 & params)1495 status_t Camera2Client::setParameters(const String8& params) {
1496 ATRACE_CALL();
1497 ALOGV("%s: Camera %d", __FUNCTION__, mCameraId);
1498 Mutex::Autolock icl(mBinderSerializationLock);
1499 status_t res;
1500 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
1501
1502 SharedParameters::Lock l(mParameters);
1503
1504 Parameters::focusMode_t focusModeBefore = l.mParameters.focusMode;
1505 res = l.mParameters.set(params);
1506 if (res != OK) return res;
1507 Parameters::focusMode_t focusModeAfter = l.mParameters.focusMode;
1508
1509 if (l.mParameters.zslMode && focusModeAfter != focusModeBefore) {
1510 mZslProcessor->clearZslQueue();
1511 }
1512
1513 res = updateRequests(l.mParameters);
1514
1515 return res;
1516 }
1517
getParameters() const1518 String8 Camera2Client::getParameters() const {
1519 ATRACE_CALL();
1520 ALOGV("%s: Camera %d", __FUNCTION__, mCameraId);
1521 Mutex::Autolock icl(mBinderSerializationLock);
1522 // The camera service can unconditionally get the parameters at all times
1523 if (getCallingPid() != mServicePid && checkPid(__FUNCTION__) != OK) return String8();
1524
1525 SharedParameters::ReadLock l(mParameters);
1526
1527 return l.mParameters.get();
1528 }
1529
sendCommand(int32_t cmd,int32_t arg1,int32_t arg2)1530 status_t Camera2Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
1531 ATRACE_CALL();
1532 Mutex::Autolock icl(mBinderSerializationLock);
1533 status_t res;
1534 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
1535
1536 ALOGV("%s: Camera %d: Command %d (%d, %d)", __FUNCTION__, mCameraId,
1537 cmd, arg1, arg2);
1538
1539 switch (cmd) {
1540 case CAMERA_CMD_START_SMOOTH_ZOOM:
1541 return commandStartSmoothZoomL();
1542 case CAMERA_CMD_STOP_SMOOTH_ZOOM:
1543 return commandStopSmoothZoomL();
1544 case CAMERA_CMD_SET_DISPLAY_ORIENTATION:
1545 return commandSetDisplayOrientationL(arg1);
1546 case CAMERA_CMD_ENABLE_SHUTTER_SOUND:
1547 return commandEnableShutterSoundL(arg1 == 1);
1548 case CAMERA_CMD_PLAY_RECORDING_SOUND:
1549 return commandPlayRecordingSoundL();
1550 case CAMERA_CMD_START_FACE_DETECTION:
1551 return commandStartFaceDetectionL(arg1);
1552 case CAMERA_CMD_STOP_FACE_DETECTION: {
1553 SharedParameters::Lock l(mParameters);
1554 return commandStopFaceDetectionL(l.mParameters);
1555 }
1556 case CAMERA_CMD_ENABLE_FOCUS_MOVE_MSG:
1557 return commandEnableFocusMoveMsgL(arg1 == 1);
1558 case CAMERA_CMD_PING:
1559 return commandPingL();
1560 case CAMERA_CMD_SET_VIDEO_BUFFER_COUNT:
1561 return commandSetVideoBufferCountL(arg1);
1562 default:
1563 ALOGE("%s: Unknown command %d (arguments %d, %d)",
1564 __FUNCTION__, cmd, arg1, arg2);
1565 return BAD_VALUE;
1566 }
1567 }
1568
commandStartSmoothZoomL()1569 status_t Camera2Client::commandStartSmoothZoomL() {
1570 ALOGE("%s: Unimplemented!", __FUNCTION__);
1571 return OK;
1572 }
1573
commandStopSmoothZoomL()1574 status_t Camera2Client::commandStopSmoothZoomL() {
1575 ALOGE("%s: Unimplemented!", __FUNCTION__);
1576 return OK;
1577 }
1578
commandSetDisplayOrientationL(int degrees)1579 status_t Camera2Client::commandSetDisplayOrientationL(int degrees) {
1580 int transform = Parameters::degToTransform(degrees,
1581 mCameraFacing == CAMERA_FACING_FRONT);
1582 if (transform == -1) {
1583 ALOGE("%s: Camera %d: Error setting %d as display orientation value",
1584 __FUNCTION__, mCameraId, degrees);
1585 return BAD_VALUE;
1586 }
1587 SharedParameters::Lock l(mParameters);
1588 if (transform != l.mParameters.previewTransform &&
1589 getPreviewStreamId() != NO_STREAM) {
1590 mDevice->setStreamTransform(getPreviewStreamId(), transform);
1591 }
1592 l.mParameters.previewTransform = transform;
1593 return OK;
1594 }
1595
commandEnableShutterSoundL(bool enable)1596 status_t Camera2Client::commandEnableShutterSoundL(bool enable) {
1597 SharedParameters::Lock l(mParameters);
1598 if (enable) {
1599 l.mParameters.playShutterSound = true;
1600 return OK;
1601 }
1602
1603 // the camera2 api legacy mode can unconditionally disable the shutter sound
1604 if (mLegacyMode) {
1605 ALOGV("%s: Disable shutter sound in legacy mode", __FUNCTION__);
1606 l.mParameters.playShutterSound = false;
1607 return OK;
1608 }
1609
1610 // Disabling shutter sound may not be allowed. In that case only
1611 // allow the mediaserver process to disable the sound.
1612 char value[PROPERTY_VALUE_MAX];
1613 property_get("ro.camera.sound.forced", value, "0");
1614 if (strncmp(value, "0", 2) != 0) {
1615 // Disabling shutter sound is not allowed. Deny if the current
1616 // process is not mediaserver.
1617 if (getCallingPid() != getpid()) {
1618 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)",
1619 getCallingPid());
1620 return PERMISSION_DENIED;
1621 }
1622 }
1623
1624 l.mParameters.playShutterSound = false;
1625 return OK;
1626 }
1627
commandPlayRecordingSoundL()1628 status_t Camera2Client::commandPlayRecordingSoundL() {
1629 mCameraService->playSound(CameraService::SOUND_RECORDING);
1630 return OK;
1631 }
1632
commandStartFaceDetectionL(int)1633 status_t Camera2Client::commandStartFaceDetectionL(int /*type*/) {
1634 ALOGV("%s: Camera %d: Starting face detection",
1635 __FUNCTION__, mCameraId);
1636 status_t res;
1637 SharedParameters::Lock l(mParameters);
1638 switch (l.mParameters.state) {
1639 case Parameters::DISCONNECTED:
1640 case Parameters::STOPPED:
1641 case Parameters::WAITING_FOR_PREVIEW_WINDOW:
1642 case Parameters::STILL_CAPTURE:
1643 ALOGE("%s: Camera %d: Cannot start face detection without preview active",
1644 __FUNCTION__, mCameraId);
1645 return INVALID_OPERATION;
1646 case Parameters::PREVIEW:
1647 case Parameters::RECORD:
1648 case Parameters::VIDEO_SNAPSHOT:
1649 // Good to go for starting face detect
1650 break;
1651 }
1652 // Ignoring type
1653 if (l.mParameters.fastInfo.bestFaceDetectMode ==
1654 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF) {
1655 ALOGE("%s: Camera %d: Face detection not supported",
1656 __FUNCTION__, mCameraId);
1657 return BAD_VALUE;
1658 }
1659 if (l.mParameters.enableFaceDetect) return OK;
1660
1661 l.mParameters.enableFaceDetect = true;
1662
1663 res = updateRequests(l.mParameters);
1664
1665 return res;
1666 }
1667
commandStopFaceDetectionL(Parameters & params)1668 status_t Camera2Client::commandStopFaceDetectionL(Parameters ¶ms) {
1669 status_t res = OK;
1670 ALOGV("%s: Camera %d: Stopping face detection",
1671 __FUNCTION__, mCameraId);
1672
1673 if (!params.enableFaceDetect) return OK;
1674
1675 params.enableFaceDetect = false;
1676
1677 if (params.state == Parameters::PREVIEW
1678 || params.state == Parameters::RECORD
1679 || params.state == Parameters::VIDEO_SNAPSHOT) {
1680 res = updateRequests(params);
1681 }
1682
1683 return res;
1684 }
1685
commandEnableFocusMoveMsgL(bool enable)1686 status_t Camera2Client::commandEnableFocusMoveMsgL(bool enable) {
1687 SharedParameters::Lock l(mParameters);
1688 l.mParameters.enableFocusMoveMessages = enable;
1689
1690 return OK;
1691 }
1692
commandPingL()1693 status_t Camera2Client::commandPingL() {
1694 // Always ping back if access is proper and device is alive
1695 SharedParameters::Lock l(mParameters);
1696 if (l.mParameters.state != Parameters::DISCONNECTED) {
1697 return OK;
1698 } else {
1699 return NO_INIT;
1700 }
1701 }
1702
commandSetVideoBufferCountL(size_t count)1703 status_t Camera2Client::commandSetVideoBufferCountL(size_t count) {
1704 if (recordingEnabledL()) {
1705 ALOGE("%s: Camera %d: Error setting video buffer count after "
1706 "recording was started", __FUNCTION__, mCameraId);
1707 return INVALID_OPERATION;
1708 }
1709
1710 return mStreamingProcessor->setRecordingBufferCount(count);
1711 }
1712
1713 /** Device-related methods */
notifyAutoFocus(uint8_t newState,int triggerId)1714 void Camera2Client::notifyAutoFocus(uint8_t newState, int triggerId) {
1715 ALOGV("%s: Autofocus state now %d, last trigger %d",
1716 __FUNCTION__, newState, triggerId);
1717 bool sendCompletedMessage = false;
1718 bool sendMovingMessage = false;
1719
1720 bool success = false;
1721 bool afInMotion = false;
1722 {
1723 SharedParameters::Lock l(mParameters);
1724 // Trace end of AF state
1725 char tmp[32];
1726 if (l.mParameters.afStateCounter > 0) {
1727 camera_metadata_enum_snprint(
1728 ANDROID_CONTROL_AF_STATE, l.mParameters.focusState, tmp, sizeof(tmp));
1729 ATRACE_ASYNC_END(tmp, l.mParameters.afStateCounter);
1730 }
1731
1732 // Update state
1733 l.mParameters.focusState = newState;
1734 l.mParameters.afStateCounter++;
1735
1736 // Trace start of AF state
1737
1738 camera_metadata_enum_snprint(
1739 ANDROID_CONTROL_AF_STATE, l.mParameters.focusState, tmp, sizeof(tmp));
1740 ATRACE_ASYNC_BEGIN(tmp, l.mParameters.afStateCounter);
1741
1742 switch (l.mParameters.focusMode) {
1743 case Parameters::FOCUS_MODE_AUTO:
1744 case Parameters::FOCUS_MODE_MACRO:
1745 // Don't send notifications upstream if they're not for the current AF
1746 // trigger. For example, if cancel was called in between, or if we
1747 // already sent a notification about this AF call.
1748 if (triggerId != l.mParameters.currentAfTriggerId) break;
1749 switch (newState) {
1750 case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED:
1751 success = true;
1752 // no break
1753 case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
1754 sendCompletedMessage = true;
1755 l.mParameters.currentAfTriggerId = -1;
1756 break;
1757 case ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN:
1758 // Just starting focusing, ignore
1759 break;
1760 case ANDROID_CONTROL_AF_STATE_INACTIVE:
1761 case ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN:
1762 case ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED:
1763 case ANDROID_CONTROL_AF_STATE_PASSIVE_UNFOCUSED:
1764 default:
1765 // Unexpected in AUTO/MACRO mode
1766 ALOGE("%s: Unexpected AF state transition in AUTO/MACRO mode: %d",
1767 __FUNCTION__, newState);
1768 break;
1769 }
1770 break;
1771 case Parameters::FOCUS_MODE_CONTINUOUS_VIDEO:
1772 case Parameters::FOCUS_MODE_CONTINUOUS_PICTURE:
1773 switch (newState) {
1774 case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED:
1775 success = true;
1776 // no break
1777 case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
1778 // Don't send notifications upstream if they're not for
1779 // the current AF trigger. For example, if cancel was
1780 // called in between, or if we already sent a
1781 // notification about this AF call.
1782 // Send both a 'AF done' callback and a 'AF move' callback
1783 if (triggerId != l.mParameters.currentAfTriggerId) break;
1784 sendCompletedMessage = true;
1785 afInMotion = false;
1786 if (l.mParameters.enableFocusMoveMessages &&
1787 l.mParameters.afInMotion) {
1788 sendMovingMessage = true;
1789 }
1790 l.mParameters.currentAfTriggerId = -1;
1791 break;
1792 case ANDROID_CONTROL_AF_STATE_INACTIVE:
1793 // Cancel was called, or we switched state; care if
1794 // currently moving
1795 afInMotion = false;
1796 if (l.mParameters.enableFocusMoveMessages &&
1797 l.mParameters.afInMotion) {
1798 sendMovingMessage = true;
1799 }
1800 break;
1801 case ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN:
1802 // Start passive scan, inform upstream
1803 afInMotion = true;
1804 // no break
1805 case ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED:
1806 case ANDROID_CONTROL_AF_STATE_PASSIVE_UNFOCUSED:
1807 // Stop passive scan, inform upstream
1808 if (l.mParameters.enableFocusMoveMessages) {
1809 sendMovingMessage = true;
1810 }
1811 break;
1812 }
1813 l.mParameters.afInMotion = afInMotion;
1814 break;
1815 case Parameters::FOCUS_MODE_EDOF:
1816 case Parameters::FOCUS_MODE_INFINITY:
1817 case Parameters::FOCUS_MODE_FIXED:
1818 default:
1819 if (newState != ANDROID_CONTROL_AF_STATE_INACTIVE) {
1820 ALOGE("%s: Unexpected AF state change %d "
1821 "(ID %d) in focus mode %d",
1822 __FUNCTION__, newState, triggerId,
1823 l.mParameters.focusMode);
1824 }
1825 }
1826 }
1827 if (sendMovingMessage) {
1828 SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
1829 if (l.mRemoteCallback != 0) {
1830 l.mRemoteCallback->notifyCallback(CAMERA_MSG_FOCUS_MOVE,
1831 afInMotion ? 1 : 0, 0);
1832 }
1833 }
1834 if (sendCompletedMessage) {
1835 ATRACE_ASYNC_END(kAutofocusLabel, triggerId);
1836 SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
1837 if (l.mRemoteCallback != 0) {
1838 l.mRemoteCallback->notifyCallback(CAMERA_MSG_FOCUS,
1839 success ? 1 : 0, 0);
1840 }
1841 }
1842 }
1843
notifyAutoExposure(uint8_t newState,int triggerId)1844 void Camera2Client::notifyAutoExposure(uint8_t newState, int triggerId) {
1845 ALOGV("%s: Autoexposure state now %d, last trigger %d",
1846 __FUNCTION__, newState, triggerId);
1847 mCaptureSequencer->notifyAutoExposure(newState, triggerId);
1848 }
1849
getParameters()1850 camera2::SharedParameters& Camera2Client::getParameters() {
1851 return mParameters;
1852 }
1853
getPreviewStreamId() const1854 int Camera2Client::getPreviewStreamId() const {
1855 return mStreamingProcessor->getPreviewStreamId();
1856 }
1857
getCaptureStreamId() const1858 int Camera2Client::getCaptureStreamId() const {
1859 return mJpegProcessor->getStreamId();
1860 }
1861
getCallbackStreamId() const1862 int Camera2Client::getCallbackStreamId() const {
1863 return mCallbackProcessor->getStreamId();
1864 }
1865
getRecordingStreamId() const1866 int Camera2Client::getRecordingStreamId() const {
1867 return mStreamingProcessor->getRecordingStreamId();
1868 }
1869
getZslStreamId() const1870 int Camera2Client::getZslStreamId() const {
1871 return mZslProcessor->getStreamId();
1872 }
1873
registerFrameListener(int32_t minId,int32_t maxId,wp<camera2::FrameProcessor::FilteredListener> listener,bool sendPartials)1874 status_t Camera2Client::registerFrameListener(int32_t minId, int32_t maxId,
1875 wp<camera2::FrameProcessor::FilteredListener> listener, bool sendPartials) {
1876 return mFrameProcessor->registerListener(minId, maxId, listener, sendPartials);
1877 }
1878
removeFrameListener(int32_t minId,int32_t maxId,wp<camera2::FrameProcessor::FilteredListener> listener)1879 status_t Camera2Client::removeFrameListener(int32_t minId, int32_t maxId,
1880 wp<camera2::FrameProcessor::FilteredListener> listener) {
1881 return mFrameProcessor->removeListener(minId, maxId, listener);
1882 }
1883
stopStream()1884 status_t Camera2Client::stopStream() {
1885 return mStreamingProcessor->stopStream();
1886 }
1887
1888 const int32_t Camera2Client::kPreviewRequestIdStart;
1889 const int32_t Camera2Client::kPreviewRequestIdEnd;
1890 const int32_t Camera2Client::kRecordingRequestIdStart;
1891 const int32_t Camera2Client::kRecordingRequestIdEnd;
1892 const int32_t Camera2Client::kCaptureRequestIdStart;
1893 const int32_t Camera2Client::kCaptureRequestIdEnd;
1894
1895 /** Utility methods */
1896
updateRequests(Parameters & params)1897 status_t Camera2Client::updateRequests(Parameters ¶ms) {
1898 status_t res;
1899
1900 ALOGV("%s: Camera %d: state = %d", __FUNCTION__, getCameraId(), params.state);
1901
1902 res = mStreamingProcessor->incrementStreamingIds();
1903 if (res != OK) {
1904 ALOGE("%s: Camera %d: Unable to increment request IDs: %s (%d)",
1905 __FUNCTION__, mCameraId, strerror(-res), res);
1906 return res;
1907 }
1908
1909 res = mStreamingProcessor->updatePreviewRequest(params);
1910 if (res != OK) {
1911 ALOGE("%s: Camera %d: Unable to update preview request: %s (%d)",
1912 __FUNCTION__, mCameraId, strerror(-res), res);
1913 return res;
1914 }
1915 res = mStreamingProcessor->updateRecordingRequest(params);
1916 if (res != OK) {
1917 ALOGE("%s: Camera %d: Unable to update recording request: %s (%d)",
1918 __FUNCTION__, mCameraId, strerror(-res), res);
1919 return res;
1920 }
1921
1922 if (params.state == Parameters::PREVIEW) {
1923 res = startPreviewL(params, true);
1924 if (res != OK) {
1925 ALOGE("%s: Camera %d: Error streaming new preview request: %s (%d)",
1926 __FUNCTION__, mCameraId, strerror(-res), res);
1927 return res;
1928 }
1929 } else if (params.state == Parameters::RECORD ||
1930 params.state == Parameters::VIDEO_SNAPSHOT) {
1931 res = startRecordingL(params, true);
1932 if (res != OK) {
1933 ALOGE("%s: Camera %d: Error streaming new record request: %s (%d)",
1934 __FUNCTION__, mCameraId, strerror(-res), res);
1935 return res;
1936 }
1937 }
1938 return res;
1939 }
1940
1941
calculateBufferSize(int width,int height,int format,int stride)1942 size_t Camera2Client::calculateBufferSize(int width, int height,
1943 int format, int stride) {
1944 switch (format) {
1945 case HAL_PIXEL_FORMAT_YCbCr_422_SP: // NV16
1946 return width * height * 2;
1947 case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21
1948 return width * height * 3 / 2;
1949 case HAL_PIXEL_FORMAT_YCbCr_422_I: // YUY2
1950 return width * height * 2;
1951 case HAL_PIXEL_FORMAT_YV12: { // YV12
1952 size_t ySize = stride * height;
1953 size_t uvStride = (stride / 2 + 0xF) & ~0xF;
1954 size_t uvSize = uvStride * height / 2;
1955 return ySize + uvSize * 2;
1956 }
1957 case HAL_PIXEL_FORMAT_RGB_565:
1958 return width * height * 2;
1959 case HAL_PIXEL_FORMAT_RGBA_8888:
1960 return width * height * 4;
1961 case HAL_PIXEL_FORMAT_RAW_SENSOR:
1962 return width * height * 2;
1963 default:
1964 ALOGE("%s: Unknown preview format: %x",
1965 __FUNCTION__, format);
1966 return 0;
1967 }
1968 }
1969
syncWithDevice()1970 status_t Camera2Client::syncWithDevice() {
1971 ATRACE_CALL();
1972 const nsecs_t kMaxSyncTimeout = 500000000; // 500 ms
1973 status_t res;
1974
1975 int32_t activeRequestId = mStreamingProcessor->getActiveRequestId();
1976 if (activeRequestId == 0) return OK;
1977
1978 res = mDevice->waitUntilRequestReceived(activeRequestId, kMaxSyncTimeout);
1979 if (res == TIMED_OUT) {
1980 ALOGE("%s: Camera %d: Timed out waiting sync with HAL",
1981 __FUNCTION__, mCameraId);
1982 } else if (res != OK) {
1983 ALOGE("%s: Camera %d: Error while waiting to sync with HAL",
1984 __FUNCTION__, mCameraId);
1985 }
1986 return res;
1987 }
1988
1989 template <typename ProcessorT>
updateProcessorStream(sp<ProcessorT> processor,camera2::Parameters params)1990 status_t Camera2Client::updateProcessorStream(sp<ProcessorT> processor,
1991 camera2::Parameters params) {
1992 // No default template arguments until C++11, so we need this overload
1993 return updateProcessorStream<ProcessorT, &ProcessorT::updateStream>(
1994 processor, params);
1995 }
1996
1997 template <typename ProcessorT,
1998 status_t (ProcessorT::*updateStreamF)(const Parameters &)>
updateProcessorStream(sp<ProcessorT> processor,Parameters params)1999 status_t Camera2Client::updateProcessorStream(sp<ProcessorT> processor,
2000 Parameters params) {
2001 status_t res;
2002
2003 // Get raw pointer since sp<T> doesn't have operator->*
2004 ProcessorT *processorPtr = processor.get();
2005 res = (processorPtr->*updateStreamF)(params);
2006
2007 /**
2008 * Can't update the stream if it's busy?
2009 *
2010 * Then we need to stop the device (by temporarily clearing the request
2011 * queue) and then try again. Resume streaming once we're done.
2012 */
2013 if (res == -EBUSY) {
2014 ALOGV("%s: Camera %d: Pausing to update stream", __FUNCTION__,
2015 mCameraId);
2016 res = mStreamingProcessor->togglePauseStream(/*pause*/true);
2017 if (res != OK) {
2018 ALOGE("%s: Camera %d: Can't pause streaming: %s (%d)",
2019 __FUNCTION__, mCameraId, strerror(-res), res);
2020 }
2021
2022 res = mDevice->waitUntilDrained();
2023 if (res != OK) {
2024 ALOGE("%s: Camera %d: Waiting to stop streaming failed: %s (%d)",
2025 __FUNCTION__, mCameraId, strerror(-res), res);
2026 }
2027
2028 res = (processorPtr->*updateStreamF)(params);
2029 if (res != OK) {
2030 ALOGE("%s: Camera %d: Failed to update processing stream "
2031 " despite having halted streaming first: %s (%d)",
2032 __FUNCTION__, mCameraId, strerror(-res), res);
2033 }
2034
2035 res = mStreamingProcessor->togglePauseStream(/*pause*/false);
2036 if (res != OK) {
2037 ALOGE("%s: Camera %d: Can't unpause streaming: %s (%d)",
2038 __FUNCTION__, mCameraId, strerror(-res), res);
2039 }
2040 }
2041
2042 return res;
2043 }
2044
overrideVideoSnapshotSize(Parameters & params)2045 status_t Camera2Client::overrideVideoSnapshotSize(Parameters ¶ms) {
2046 ALOGV("%s: Camera %d: configure still size to video size before recording"
2047 , __FUNCTION__, mCameraId);
2048 params.overrideJpegSizeByVideoSize();
2049 status_t res = updateProcessorStream(mJpegProcessor, params);
2050 if (res != OK) {
2051 ALOGE("%s: Camera %d: Can't override video snapshot size to video size: %s (%d)",
2052 __FUNCTION__, mCameraId, strerror(-res), res);
2053 }
2054 return res;
2055 }
2056
2057 const char* Camera2Client::kAutofocusLabel = "autofocus";
2058 const char* Camera2Client::kTakepictureLabel = "take_picture";
2059
2060 } // namespace android
2061