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