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