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