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