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