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