Home | History | Annotate | Download | only in api1
      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 
     43 static int getCallingPid() {
     44     return IPCThreadState::self()->getCallingPid();
     45 }
     46 
     47 // Interface used by CameraService
     48 
     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 
     70 status_t Camera2Client::initialize(camera_module_t *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             sp<ZslProcessor3> zslProc =
    126                     new ZslProcessor3(this, mCaptureSequencer);
    127             mZslProcessor = zslProc;
    128             mZslProcessorThread = zslProc;
    129             break;
    130         }
    131         default:
    132             break;
    133     }
    134     threadName = String8::format("C2-%d-ZslProc",
    135             mCameraId);
    136     mZslProcessorThread->run(threadName.string());
    137 
    138     mCallbackProcessor = new CallbackProcessor(this);
    139     threadName = String8::format("C2-%d-CallbkProc",
    140             mCameraId);
    141     mCallbackProcessor->run(threadName.string());
    142 
    143     if (gLogLevel >= 1) {
    144         SharedParameters::Lock l(mParameters);
    145         ALOGD("%s: Default parameters converted from camera %d:", __FUNCTION__,
    146               mCameraId);
    147         ALOGD("%s", l.mParameters.paramsFlattened.string());
    148     }
    149 
    150     return OK;
    151 }
    152 
    153 Camera2Client::~Camera2Client() {
    154     ATRACE_CALL();
    155     ALOGV("~Camera2Client");
    156 
    157     mDestructionStarted = true;
    158 
    159     disconnect();
    160 
    161     ALOGI("Camera %d: Closed", mCameraId);
    162 }
    163 
    164 status_t Camera2Client::dump(int fd, const Vector<String16>& args) {
    165     String8 result;
    166     result.appendFormat("Client2[%d] (%p) Client: %s PID: %d, dump:\n",
    167             mCameraId,
    168             getRemoteCallback()->asBinder().get(),
    169             String8(mClientPackageName).string(),
    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 
    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 
    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 
    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 
    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 
    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<ANativeWindow> window;
    533     if (bufferProducer != 0) {
    534         binder = bufferProducer->asBinder();
    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 
    543 status_t Camera2Client::setPreviewWindowL(const sp<IBinder>& binder,
    544         sp<ANativeWindow> 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 
    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 
    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 
    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<ANativeWindow> 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 
    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 
    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.
    768     // TODO: Find a better compromise, though this likely would involve HAL
    769     // changes.
    770     int lastJpegStreamId = mJpegProcessor->getStreamId();
    771     res = updateProcessorStream(mJpegProcessor, params);
    772     if (res != OK) {
    773         ALOGE("%s: Camera %d: Can't pre-configure still image "
    774                 "stream: %s (%d)",
    775                 __FUNCTION__, mCameraId, strerror(-res), res);
    776         return res;
    777     }
    778     bool jpegStreamChanged = mJpegProcessor->getStreamId() != lastJpegStreamId;
    779 
    780     Vector<int32_t> outputStreams;
    781     bool callbacksEnabled = (params.previewCallbackFlags &
    782             CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) ||
    783             params.previewCallbackSurface;
    784 
    785     if (callbacksEnabled) {
    786         // Can't have recording stream hanging around when enabling callbacks,
    787         // since it exceeds the max stream count on some devices.
    788         if (mStreamingProcessor->getRecordingStreamId() != NO_STREAM) {
    789             ALOGV("%s: Camera %d: Clearing out recording stream before "
    790                     "creating callback stream", __FUNCTION__, mCameraId);
    791             res = mStreamingProcessor->stopStream();
    792             if (res != OK) {
    793                 ALOGE("%s: Camera %d: Can't stop streaming to delete "
    794                         "recording stream", __FUNCTION__, mCameraId);
    795                 return res;
    796             }
    797             res = mStreamingProcessor->deleteRecordingStream();
    798             if (res != OK) {
    799                 ALOGE("%s: Camera %d: Unable to delete recording stream before "
    800                         "enabling callbacks: %s (%d)", __FUNCTION__, mCameraId,
    801                         strerror(-res), res);
    802                 return res;
    803             }
    804         }
    805 
    806         res = mCallbackProcessor->updateStream(params);
    807         if (res != OK) {
    808             ALOGE("%s: Camera %d: Unable to update callback stream: %s (%d)",
    809                     __FUNCTION__, mCameraId, strerror(-res), res);
    810             return res;
    811         }
    812         outputStreams.push(getCallbackStreamId());
    813     } else if (previewStreamChanged && mCallbackProcessor->getStreamId() != NO_STREAM) {
    814         /**
    815          * Delete the unused callback stream when preview stream is changed and
    816          * preview is not enabled. Don't need stop preview stream as preview is in
    817          * STOPPED state now.
    818          */
    819         ALOGV("%s: Camera %d: Delete unused preview callback stream.",  __FUNCTION__, mCameraId);
    820         res = mCallbackProcessor->deleteStream();
    821         if (res != OK) {
    822             ALOGE("%s: Camera %d: Unable to delete callback stream %s (%d)",
    823                     __FUNCTION__, mCameraId, strerror(-res), res);
    824             return res;
    825         }
    826     }
    827 
    828     if (params.zslMode && !params.recordingHint &&
    829             getRecordingStreamId() == NO_STREAM) {
    830         res = updateProcessorStream(mZslProcessor, params);
    831         if (res != OK) {
    832             ALOGE("%s: Camera %d: Unable to update ZSL stream: %s (%d)",
    833                     __FUNCTION__, mCameraId, strerror(-res), res);
    834             return res;
    835         }
    836 
    837         if (jpegStreamChanged) {
    838             ALOGV("%s: Camera %d: Clear ZSL buffer queue when Jpeg size is changed",
    839                     __FUNCTION__, mCameraId);
    840             mZslProcessor->clearZslQueue();
    841         }
    842         outputStreams.push(getZslStreamId());
    843     } else {
    844         mZslProcessor->deleteStream();
    845     }
    846 
    847     outputStreams.push(getPreviewStreamId());
    848 
    849     if (!params.recordingHint) {
    850         if (!restart) {
    851             res = mStreamingProcessor->updatePreviewRequest(params);
    852             if (res != OK) {
    853                 ALOGE("%s: Camera %d: Can't set up preview request: "
    854                         "%s (%d)", __FUNCTION__, mCameraId,
    855                         strerror(-res), res);
    856                 return res;
    857             }
    858         }
    859         res = mStreamingProcessor->startStream(StreamingProcessor::PREVIEW,
    860                 outputStreams);
    861     } else {
    862         if (!restart) {
    863             res = mStreamingProcessor->updateRecordingRequest(params);
    864             if (res != OK) {
    865                 ALOGE("%s: Camera %d: Can't set up preview request with "
    866                         "record hint: %s (%d)", __FUNCTION__, mCameraId,
    867                         strerror(-res), res);
    868                 return res;
    869             }
    870         }
    871         res = mStreamingProcessor->startStream(StreamingProcessor::RECORD,
    872                 outputStreams);
    873     }
    874     if (res != OK) {
    875         ALOGE("%s: Camera %d: Unable to start streaming preview: %s (%d)",
    876                 __FUNCTION__, mCameraId, strerror(-res), res);
    877         return res;
    878     }
    879 
    880     params.state = Parameters::PREVIEW;
    881     return OK;
    882 }
    883 
    884 void Camera2Client::stopPreview() {
    885     ATRACE_CALL();
    886     ALOGV("%s: E", __FUNCTION__);
    887     Mutex::Autolock icl(mBinderSerializationLock);
    888     status_t res;
    889     if ( (res = checkPid(__FUNCTION__) ) != OK) return;
    890     stopPreviewL();
    891 }
    892 
    893 void Camera2Client::stopPreviewL() {
    894     ATRACE_CALL();
    895     status_t res;
    896     const nsecs_t kStopCaptureTimeout = 3000000000LL; // 3 seconds
    897     Parameters::State state;
    898     {
    899         SharedParameters::Lock l(mParameters);
    900         state = l.mParameters.state;
    901     }
    902 
    903     switch (state) {
    904         case Parameters::DISCONNECTED:
    905             // Nothing to do.
    906             break;
    907         case Parameters::STOPPED:
    908         case Parameters::VIDEO_SNAPSHOT:
    909         case Parameters::STILL_CAPTURE:
    910             mCaptureSequencer->waitUntilIdle(kStopCaptureTimeout);
    911             // no break
    912         case Parameters::RECORD:
    913         case Parameters::PREVIEW:
    914             syncWithDevice();
    915             res = stopStream();
    916             if (res != OK) {
    917                 ALOGE("%s: Camera %d: Can't stop streaming: %s (%d)",
    918                         __FUNCTION__, mCameraId, strerror(-res), res);
    919             }
    920             res = mDevice->waitUntilDrained();
    921             if (res != OK) {
    922                 ALOGE("%s: Camera %d: Waiting to stop streaming failed: %s (%d)",
    923                         __FUNCTION__, mCameraId, strerror(-res), res);
    924             }
    925             // Clean up recording stream
    926             res = mStreamingProcessor->deleteRecordingStream();
    927             if (res != OK) {
    928                 ALOGE("%s: Camera %d: Unable to delete recording stream before "
    929                         "stop preview: %s (%d)",
    930                         __FUNCTION__, mCameraId, strerror(-res), res);
    931             }
    932             {
    933                 // Ideally we should recover the override after recording stopped, but
    934                 // right now recording stream will live until here, so we are forced to
    935                 // recover here. TODO: find a better way to handle that (b/17495165)
    936                 SharedParameters::Lock l(mParameters);
    937                 l.mParameters.recoverOverriddenJpegSize();
    938             }
    939             // no break
    940         case Parameters::WAITING_FOR_PREVIEW_WINDOW: {
    941             SharedParameters::Lock l(mParameters);
    942             l.mParameters.state = Parameters::STOPPED;
    943             commandStopFaceDetectionL(l.mParameters);
    944             break;
    945         }
    946         default:
    947             ALOGE("%s: Camera %d: Unknown state %d", __FUNCTION__, mCameraId,
    948                     state);
    949     }
    950 }
    951 
    952 bool Camera2Client::previewEnabled() {
    953     ATRACE_CALL();
    954     Mutex::Autolock icl(mBinderSerializationLock);
    955     status_t res;
    956     if ( (res = checkPid(__FUNCTION__) ) != OK) return false;
    957 
    958     SharedParameters::Lock l(mParameters);
    959     return l.mParameters.state == Parameters::PREVIEW;
    960 }
    961 
    962 status_t Camera2Client::storeMetaDataInBuffers(bool enabled) {
    963     ATRACE_CALL();
    964     Mutex::Autolock icl(mBinderSerializationLock);
    965     status_t res;
    966     if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
    967 
    968     SharedParameters::Lock l(mParameters);
    969     switch (l.mParameters.state) {
    970         case Parameters::RECORD:
    971         case Parameters::VIDEO_SNAPSHOT:
    972             ALOGE("%s: Camera %d: Can't be called in state %s",
    973                     __FUNCTION__, mCameraId,
    974                     Parameters::getStateName(l.mParameters.state));
    975             return INVALID_OPERATION;
    976         default:
    977             // OK
    978             break;
    979     }
    980 
    981     l.mParameters.storeMetadataInBuffers = enabled;
    982 
    983     return OK;
    984 }
    985 
    986 status_t Camera2Client::startRecording() {
    987     ATRACE_CALL();
    988     ALOGV("%s: E", __FUNCTION__);
    989     Mutex::Autolock icl(mBinderSerializationLock);
    990     status_t res;
    991     if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
    992     SharedParameters::Lock l(mParameters);
    993 
    994     return startRecordingL(l.mParameters, false);
    995 }
    996 
    997 status_t Camera2Client::startRecordingL(Parameters &params, bool restart) {
    998     status_t res;
    999 
   1000     ALOGV("%s: state == %d, restart = %d", __FUNCTION__, params.state, restart);
   1001 
   1002     switch (params.state) {
   1003         case Parameters::STOPPED:
   1004             res = startPreviewL(params, false);
   1005             if (res != OK) return res;
   1006             // Make sure first preview request is submitted to the HAL device to avoid
   1007             // two consecutive set of configure_streams being called into the HAL.
   1008             // TODO: Refactor this to avoid initial preview configuration.
   1009             syncWithDevice();
   1010             break;
   1011         case Parameters::PREVIEW:
   1012             // Ready to go
   1013             break;
   1014         case Parameters::RECORD:
   1015         case Parameters::VIDEO_SNAPSHOT:
   1016             // OK to call this when recording is already on, just skip unless
   1017             // we're looking to restart
   1018             if (!restart) return OK;
   1019             break;
   1020         default:
   1021             ALOGE("%s: Camera %d: Can't start recording in state %s",
   1022                     __FUNCTION__, mCameraId,
   1023                     Parameters::getStateName(params.state));
   1024             return INVALID_OPERATION;
   1025     };
   1026 
   1027     if (!params.storeMetadataInBuffers) {
   1028         ALOGE("%s: Camera %d: Recording only supported in metadata mode, but "
   1029                 "non-metadata recording mode requested!", __FUNCTION__,
   1030                 mCameraId);
   1031         return INVALID_OPERATION;
   1032     }
   1033 
   1034     if (!restart) {
   1035         mCameraService->playSound(CameraService::SOUND_RECORDING);
   1036         mStreamingProcessor->updateRecordingRequest(params);
   1037         if (res != OK) {
   1038             ALOGE("%s: Camera %d: Unable to update recording request: %s (%d)",
   1039                     __FUNCTION__, mCameraId, strerror(-res), res);
   1040             return res;
   1041         }
   1042     }
   1043 
   1044     // Not all devices can support a preview callback stream and a recording
   1045     // stream at the same time, so assume none of them can.
   1046     if (mCallbackProcessor->getStreamId() != NO_STREAM) {
   1047         ALOGV("%s: Camera %d: Clearing out callback stream before "
   1048                 "creating recording stream", __FUNCTION__, mCameraId);
   1049         res = mStreamingProcessor->stopStream();
   1050         if (res != OK) {
   1051             ALOGE("%s: Camera %d: Can't stop streaming to delete callback stream",
   1052                     __FUNCTION__, mCameraId);
   1053             return res;
   1054         }
   1055         res = mCallbackProcessor->deleteStream();
   1056         if (res != OK) {
   1057             ALOGE("%s: Camera %d: Unable to delete callback stream before "
   1058                     "record: %s (%d)", __FUNCTION__, mCameraId,
   1059                     strerror(-res), res);
   1060             return res;
   1061         }
   1062     }
   1063 
   1064     // On current HALs, clean up ZSL before transitioning into recording
   1065     if (mDeviceVersion != CAMERA_DEVICE_API_VERSION_2_0) {
   1066         if (mZslProcessor->getStreamId() != NO_STREAM) {
   1067             ALOGV("%s: Camera %d: Clearing out zsl stream before "
   1068                     "creating recording stream", __FUNCTION__, mCameraId);
   1069             res = mStreamingProcessor->stopStream();
   1070             if (res != OK) {
   1071                 ALOGE("%s: Camera %d: Can't stop streaming to delete callback stream",
   1072                         __FUNCTION__, mCameraId);
   1073                 return res;
   1074             }
   1075             res = mDevice->waitUntilDrained();
   1076             if (res != OK) {
   1077                 ALOGE("%s: Camera %d: Waiting to stop streaming failed: %s (%d)",
   1078                         __FUNCTION__, mCameraId, strerror(-res), res);
   1079             }
   1080             res = mZslProcessor->clearZslQueue();
   1081             if (res != OK) {
   1082                 ALOGE("%s: Camera %d: Can't clear zsl queue",
   1083                         __FUNCTION__, mCameraId);
   1084                 return res;
   1085             }
   1086             res = mZslProcessor->deleteStream();
   1087             if (res != OK) {
   1088                 ALOGE("%s: Camera %d: Unable to delete zsl stream before "
   1089                         "record: %s (%d)", __FUNCTION__, mCameraId,
   1090                         strerror(-res), res);
   1091                 return res;
   1092             }
   1093         }
   1094     }
   1095 
   1096     // Disable callbacks if they're enabled; can't record and use callbacks,
   1097     // and we can't fail record start without stagefright asserting.
   1098     params.previewCallbackFlags = 0;
   1099 
   1100     if (mDeviceVersion != CAMERA_DEVICE_API_VERSION_2_0) {
   1101         // For newer devices, may need to reconfigure video snapshot JPEG sizes
   1102         // during recording startup, so need a more complex sequence here to
   1103         // ensure an early stream reconfiguration doesn't happen
   1104         bool recordingStreamNeedsUpdate;
   1105         res = mStreamingProcessor->recordingStreamNeedsUpdate(params, &recordingStreamNeedsUpdate);
   1106         if (res != OK) {
   1107             ALOGE("%s: Camera %d: Can't query recording stream",
   1108                     __FUNCTION__, mCameraId);
   1109             return res;
   1110         }
   1111 
   1112         if (recordingStreamNeedsUpdate) {
   1113             // Need to stop stream here so updateProcessorStream won't trigger configureStream
   1114             // Right now camera device cannot handle configureStream failure gracefully
   1115             // when device is streaming
   1116             res = mStreamingProcessor->stopStream();
   1117             if (res != OK) {
   1118                 ALOGE("%s: Camera %d: Can't stop streaming to update record "
   1119                         "stream", __FUNCTION__, mCameraId);
   1120                 return res;
   1121             }
   1122             res = mDevice->waitUntilDrained();
   1123             if (res != OK) {
   1124                 ALOGE("%s: Camera %d: Waiting to stop streaming failed: "
   1125                         "%s (%d)", __FUNCTION__, mCameraId,
   1126                         strerror(-res), res);
   1127             }
   1128 
   1129             res = updateProcessorStream<
   1130                     StreamingProcessor,
   1131                     &StreamingProcessor::updateRecordingStream>(
   1132                         mStreamingProcessor,
   1133                         params);
   1134             if (res != OK) {
   1135                 ALOGE("%s: Camera %d: Unable to update recording stream: "
   1136                         "%s (%d)", __FUNCTION__, mCameraId,
   1137                         strerror(-res), res);
   1138                 return res;
   1139             }
   1140         }
   1141     } else {
   1142         // Maintain call sequencing for HALv2 devices.
   1143         res = updateProcessorStream<
   1144                 StreamingProcessor,
   1145                 &StreamingProcessor::updateRecordingStream>(mStreamingProcessor,
   1146                     params);
   1147         if (res != OK) {
   1148             ALOGE("%s: Camera %d: Unable to update recording stream: %s (%d)",
   1149                     __FUNCTION__, mCameraId, strerror(-res), res);
   1150             return res;
   1151         }
   1152     }
   1153 
   1154     Vector<int32_t> outputStreams;
   1155     outputStreams.push(getPreviewStreamId());
   1156     outputStreams.push(getRecordingStreamId());
   1157 
   1158     res = mStreamingProcessor->startStream(StreamingProcessor::RECORD,
   1159             outputStreams);
   1160 
   1161     // startStream might trigger a configureStream call and device might fail
   1162     // configureStream due to jpeg size > video size. Try again with jpeg size overridden
   1163     // to video size.
   1164     if (res == BAD_VALUE) {
   1165         overrideVideoSnapshotSize(params);
   1166         res = mStreamingProcessor->startStream(StreamingProcessor::RECORD,
   1167                 outputStreams);
   1168     }
   1169 
   1170     if (res != OK) {
   1171         ALOGE("%s: Camera %d: Unable to start recording stream: %s (%d)",
   1172                 __FUNCTION__, mCameraId, strerror(-res), res);
   1173         return res;
   1174     }
   1175 
   1176     if (params.state < Parameters::RECORD) {
   1177         params.state = Parameters::RECORD;
   1178     }
   1179 
   1180     return OK;
   1181 }
   1182 
   1183 void Camera2Client::stopRecording() {
   1184     ATRACE_CALL();
   1185     ALOGV("%s: E", __FUNCTION__);
   1186     Mutex::Autolock icl(mBinderSerializationLock);
   1187     SharedParameters::Lock l(mParameters);
   1188 
   1189     status_t res;
   1190     if ( (res = checkPid(__FUNCTION__) ) != OK) return;
   1191 
   1192     switch (l.mParameters.state) {
   1193         case Parameters::RECORD:
   1194             // OK to stop
   1195             break;
   1196         case Parameters::STOPPED:
   1197         case Parameters::PREVIEW:
   1198         case Parameters::STILL_CAPTURE:
   1199         case Parameters::VIDEO_SNAPSHOT:
   1200         default:
   1201             ALOGE("%s: Camera %d: Can't stop recording in state %s",
   1202                     __FUNCTION__, mCameraId,
   1203                     Parameters::getStateName(l.mParameters.state));
   1204             return;
   1205     };
   1206 
   1207     mCameraService->playSound(CameraService::SOUND_RECORDING);
   1208 
   1209     res = startPreviewL(l.mParameters, true);
   1210     if (res != OK) {
   1211         ALOGE("%s: Camera %d: Unable to return to preview",
   1212                 __FUNCTION__, mCameraId);
   1213     }
   1214 }
   1215 
   1216 bool Camera2Client::recordingEnabled() {
   1217     ATRACE_CALL();
   1218     Mutex::Autolock icl(mBinderSerializationLock);
   1219 
   1220     if ( checkPid(__FUNCTION__) != OK) return false;
   1221 
   1222     return recordingEnabledL();
   1223 }
   1224 
   1225 bool Camera2Client::recordingEnabledL() {
   1226     ATRACE_CALL();
   1227     SharedParameters::Lock l(mParameters);
   1228 
   1229     return (l.mParameters.state == Parameters::RECORD
   1230             || l.mParameters.state == Parameters::VIDEO_SNAPSHOT);
   1231 }
   1232 
   1233 void Camera2Client::releaseRecordingFrame(const sp<IMemory>& mem) {
   1234     ATRACE_CALL();
   1235     Mutex::Autolock icl(mBinderSerializationLock);
   1236     if ( checkPid(__FUNCTION__) != OK) return;
   1237 
   1238     mStreamingProcessor->releaseRecordingFrame(mem);
   1239 }
   1240 
   1241 status_t Camera2Client::autoFocus() {
   1242     ATRACE_CALL();
   1243     Mutex::Autolock icl(mBinderSerializationLock);
   1244     ALOGV("%s: Camera %d", __FUNCTION__, mCameraId);
   1245     status_t res;
   1246     if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
   1247 
   1248     int triggerId;
   1249     bool notifyImmediately = false;
   1250     bool notifySuccess = false;
   1251     {
   1252         SharedParameters::Lock l(mParameters);
   1253         if (l.mParameters.state < Parameters::PREVIEW) {
   1254             ALOGE("%s: Camera %d: Call autoFocus when preview is inactive (state = %d).",
   1255                     __FUNCTION__, mCameraId, l.mParameters.state);
   1256             return INVALID_OPERATION;
   1257         }
   1258 
   1259         /**
   1260           * If the camera does not support auto-focus, it is a no-op and
   1261           * onAutoFocus(boolean, Camera) callback will be called immediately
   1262           * with a fake value of success set to true.
   1263           *
   1264           * Similarly, if focus mode is set to INFINITY, there's no reason to
   1265           * bother the HAL.
   1266           */
   1267         if (l.mParameters.focusMode == Parameters::FOCUS_MODE_FIXED ||
   1268                 l.mParameters.focusMode == Parameters::FOCUS_MODE_INFINITY) {
   1269             notifyImmediately = true;
   1270             notifySuccess = true;
   1271         }
   1272         /**
   1273          * If we're in CAF mode, and AF has already been locked, just fire back
   1274          * the callback right away; the HAL would not send a notification since
   1275          * no state change would happen on a AF trigger.
   1276          */
   1277         if ( (l.mParameters.focusMode == Parameters::FOCUS_MODE_CONTINUOUS_PICTURE ||
   1278                 l.mParameters.focusMode == Parameters::FOCUS_MODE_CONTINUOUS_VIDEO) &&
   1279                 l.mParameters.focusState == ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED ) {
   1280             notifyImmediately = true;
   1281             notifySuccess = true;
   1282         }
   1283         /**
   1284          * Send immediate notification back to client
   1285          */
   1286         if (notifyImmediately) {
   1287             SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
   1288             if (l.mRemoteCallback != 0) {
   1289                 l.mRemoteCallback->notifyCallback(CAMERA_MSG_FOCUS,
   1290                         notifySuccess ? 1 : 0, 0);
   1291             }
   1292             return OK;
   1293         }
   1294         /**
   1295          * Handle quirk mode for AF in scene modes
   1296          */
   1297         if (l.mParameters.quirks.triggerAfWithAuto &&
   1298                 l.mParameters.sceneMode != ANDROID_CONTROL_SCENE_MODE_DISABLED &&
   1299                 l.mParameters.focusMode != Parameters::FOCUS_MODE_AUTO &&
   1300                 !l.mParameters.focusingAreas[0].isEmpty()) {
   1301             ALOGV("%s: Quirk: Switching from focusMode %d to AUTO",
   1302                     __FUNCTION__, l.mParameters.focusMode);
   1303             l.mParameters.shadowFocusMode = l.mParameters.focusMode;
   1304             l.mParameters.focusMode = Parameters::FOCUS_MODE_AUTO;
   1305             updateRequests(l.mParameters);
   1306         }
   1307 
   1308         l.mParameters.currentAfTriggerId = ++l.mParameters.afTriggerCounter;
   1309         triggerId = l.mParameters.currentAfTriggerId;
   1310     }
   1311     ATRACE_ASYNC_BEGIN(kAutofocusLabel, triggerId);
   1312 
   1313     syncWithDevice();
   1314 
   1315     mDevice->triggerAutofocus(triggerId);
   1316 
   1317     return OK;
   1318 }
   1319 
   1320 status_t Camera2Client::cancelAutoFocus() {
   1321     ATRACE_CALL();
   1322     Mutex::Autolock icl(mBinderSerializationLock);
   1323     ALOGV("%s: Camera %d", __FUNCTION__, mCameraId);
   1324     status_t res;
   1325     if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
   1326 
   1327     int triggerId;
   1328     {
   1329         SharedParameters::Lock l(mParameters);
   1330         // Canceling does nothing in FIXED or INFINITY modes
   1331         if (l.mParameters.focusMode == Parameters::FOCUS_MODE_FIXED ||
   1332                 l.mParameters.focusMode == Parameters::FOCUS_MODE_INFINITY) {
   1333             return OK;
   1334         }
   1335 
   1336         // An active AF trigger is canceled
   1337         if (l.mParameters.afTriggerCounter == l.mParameters.currentAfTriggerId) {
   1338             ATRACE_ASYNC_END(kAutofocusLabel, l.mParameters.currentAfTriggerId);
   1339         }
   1340 
   1341         triggerId = ++l.mParameters.afTriggerCounter;
   1342 
   1343         // When using triggerAfWithAuto quirk, may need to reset focus mode to
   1344         // the real state at this point. No need to cancel explicitly if
   1345         // changing the AF mode.
   1346         if (l.mParameters.shadowFocusMode != Parameters::FOCUS_MODE_INVALID) {
   1347             ALOGV("%s: Quirk: Restoring focus mode to %d", __FUNCTION__,
   1348                     l.mParameters.shadowFocusMode);
   1349             l.mParameters.focusMode = l.mParameters.shadowFocusMode;
   1350             l.mParameters.shadowFocusMode = Parameters::FOCUS_MODE_INVALID;
   1351             updateRequests(l.mParameters);
   1352 
   1353             return OK;
   1354         }
   1355         if (l.mParameters.zslMode) {
   1356             mZslProcessor->clearZslQueue();
   1357         }
   1358     }
   1359     syncWithDevice();
   1360 
   1361     mDevice->triggerCancelAutofocus(triggerId);
   1362 
   1363     return OK;
   1364 }
   1365 
   1366 status_t Camera2Client::takePicture(int msgType) {
   1367     ATRACE_CALL();
   1368     Mutex::Autolock icl(mBinderSerializationLock);
   1369     status_t res;
   1370     if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
   1371 
   1372     int takePictureCounter;
   1373     {
   1374         SharedParameters::Lock l(mParameters);
   1375         switch (l.mParameters.state) {
   1376             case Parameters::DISCONNECTED:
   1377             case Parameters::STOPPED:
   1378             case Parameters::WAITING_FOR_PREVIEW_WINDOW:
   1379                 ALOGE("%s: Camera %d: Cannot take picture without preview enabled",
   1380                         __FUNCTION__, mCameraId);
   1381                 return INVALID_OPERATION;
   1382             case Parameters::PREVIEW:
   1383                 // Good to go for takePicture
   1384                 res = commandStopFaceDetectionL(l.mParameters);
   1385                 if (res != OK) {
   1386                     ALOGE("%s: Camera %d: Unable to stop face detection for still capture",
   1387                             __FUNCTION__, mCameraId);
   1388                     return res;
   1389                 }
   1390                 l.mParameters.state = Parameters::STILL_CAPTURE;
   1391                 break;
   1392             case Parameters::RECORD:
   1393                 // Good to go for video snapshot
   1394                 l.mParameters.state = Parameters::VIDEO_SNAPSHOT;
   1395                 break;
   1396             case Parameters::STILL_CAPTURE:
   1397             case Parameters::VIDEO_SNAPSHOT:
   1398                 ALOGE("%s: Camera %d: Already taking a picture",
   1399                         __FUNCTION__, mCameraId);
   1400                 return INVALID_OPERATION;
   1401         }
   1402 
   1403         ALOGV("%s: Camera %d: Starting picture capture", __FUNCTION__, mCameraId);
   1404 
   1405         int lastJpegStreamId = mJpegProcessor->getStreamId();
   1406         res = updateProcessorStream(mJpegProcessor, l.mParameters);
   1407         // If video snapshot fail to configureStream, try override video snapshot size to
   1408         // video size
   1409         if (res == BAD_VALUE && l.mParameters.state == Parameters::VIDEO_SNAPSHOT) {
   1410             overrideVideoSnapshotSize(l.mParameters);
   1411             res = updateProcessorStream(mJpegProcessor, l.mParameters);
   1412         }
   1413         if (res != OK) {
   1414             ALOGE("%s: Camera %d: Can't set up still image stream: %s (%d)",
   1415                     __FUNCTION__, mCameraId, strerror(-res), res);
   1416             return res;
   1417         }
   1418         takePictureCounter = ++l.mParameters.takePictureCounter;
   1419 
   1420         // Clear ZSL buffer queue when Jpeg size is changed.
   1421         bool jpegStreamChanged = mJpegProcessor->getStreamId() != lastJpegStreamId;
   1422         if (l.mParameters.zslMode && jpegStreamChanged) {
   1423             ALOGV("%s: Camera %d: Clear ZSL buffer queue when Jpeg size is changed",
   1424                     __FUNCTION__, mCameraId);
   1425             mZslProcessor->clearZslQueue();
   1426         }
   1427     }
   1428 
   1429     ATRACE_ASYNC_BEGIN(kTakepictureLabel, takePictureCounter);
   1430 
   1431     // Need HAL to have correct settings before (possibly) triggering precapture
   1432     syncWithDevice();
   1433 
   1434     res = mCaptureSequencer->startCapture(msgType);
   1435     if (res != OK) {
   1436         ALOGE("%s: Camera %d: Unable to start capture: %s (%d)",
   1437                 __FUNCTION__, mCameraId, strerror(-res), res);
   1438     }
   1439 
   1440     return res;
   1441 }
   1442 
   1443 status_t Camera2Client::setParameters(const String8& params) {
   1444     ATRACE_CALL();
   1445     ALOGV("%s: Camera %d", __FUNCTION__, mCameraId);
   1446     Mutex::Autolock icl(mBinderSerializationLock);
   1447     status_t res;
   1448     if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
   1449 
   1450     SharedParameters::Lock l(mParameters);
   1451 
   1452     Parameters::focusMode_t focusModeBefore = l.mParameters.focusMode;
   1453     res = l.mParameters.set(params);
   1454     if (res != OK) return res;
   1455     Parameters::focusMode_t focusModeAfter = l.mParameters.focusMode;
   1456 
   1457     if (l.mParameters.zslMode && focusModeAfter != focusModeBefore) {
   1458         mZslProcessor->clearZslQueue();
   1459     }
   1460 
   1461     res = updateRequests(l.mParameters);
   1462 
   1463     return res;
   1464 }
   1465 
   1466 String8 Camera2Client::getParameters() const {
   1467     ATRACE_CALL();
   1468     ALOGV("%s: Camera %d", __FUNCTION__, mCameraId);
   1469     Mutex::Autolock icl(mBinderSerializationLock);
   1470     // The camera service can unconditionally get the parameters at all times
   1471     if (getCallingPid() != mServicePid && checkPid(__FUNCTION__) != OK) return String8();
   1472 
   1473     SharedParameters::ReadLock l(mParameters);
   1474 
   1475     return l.mParameters.get();
   1476 }
   1477 
   1478 status_t Camera2Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
   1479     ATRACE_CALL();
   1480     Mutex::Autolock icl(mBinderSerializationLock);
   1481     status_t res;
   1482     if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
   1483 
   1484     ALOGV("%s: Camera %d: Command %d (%d, %d)", __FUNCTION__, mCameraId,
   1485             cmd, arg1, arg2);
   1486 
   1487     switch (cmd) {
   1488         case CAMERA_CMD_START_SMOOTH_ZOOM:
   1489             return commandStartSmoothZoomL();
   1490         case CAMERA_CMD_STOP_SMOOTH_ZOOM:
   1491             return commandStopSmoothZoomL();
   1492         case CAMERA_CMD_SET_DISPLAY_ORIENTATION:
   1493             return commandSetDisplayOrientationL(arg1);
   1494         case CAMERA_CMD_ENABLE_SHUTTER_SOUND:
   1495             return commandEnableShutterSoundL(arg1 == 1);
   1496         case CAMERA_CMD_PLAY_RECORDING_SOUND:
   1497             return commandPlayRecordingSoundL();
   1498         case CAMERA_CMD_START_FACE_DETECTION:
   1499             return commandStartFaceDetectionL(arg1);
   1500         case CAMERA_CMD_STOP_FACE_DETECTION: {
   1501             SharedParameters::Lock l(mParameters);
   1502             return commandStopFaceDetectionL(l.mParameters);
   1503         }
   1504         case CAMERA_CMD_ENABLE_FOCUS_MOVE_MSG:
   1505             return commandEnableFocusMoveMsgL(arg1 == 1);
   1506         case CAMERA_CMD_PING:
   1507             return commandPingL();
   1508         case CAMERA_CMD_SET_VIDEO_BUFFER_COUNT:
   1509             return commandSetVideoBufferCountL(arg1);
   1510         default:
   1511             ALOGE("%s: Unknown command %d (arguments %d, %d)",
   1512                     __FUNCTION__, cmd, arg1, arg2);
   1513             return BAD_VALUE;
   1514     }
   1515 }
   1516 
   1517 status_t Camera2Client::commandStartSmoothZoomL() {
   1518     ALOGE("%s: Unimplemented!", __FUNCTION__);
   1519     return OK;
   1520 }
   1521 
   1522 status_t Camera2Client::commandStopSmoothZoomL() {
   1523     ALOGE("%s: Unimplemented!", __FUNCTION__);
   1524     return OK;
   1525 }
   1526 
   1527 status_t Camera2Client::commandSetDisplayOrientationL(int degrees) {
   1528     int transform = Parameters::degToTransform(degrees,
   1529             mCameraFacing == CAMERA_FACING_FRONT);
   1530     if (transform == -1) {
   1531         ALOGE("%s: Camera %d: Error setting %d as display orientation value",
   1532                 __FUNCTION__, mCameraId, degrees);
   1533         return BAD_VALUE;
   1534     }
   1535     SharedParameters::Lock l(mParameters);
   1536     if (transform != l.mParameters.previewTransform &&
   1537             getPreviewStreamId() != NO_STREAM) {
   1538         mDevice->setStreamTransform(getPreviewStreamId(), transform);
   1539     }
   1540     l.mParameters.previewTransform = transform;
   1541     return OK;
   1542 }
   1543 
   1544 status_t Camera2Client::commandEnableShutterSoundL(bool enable) {
   1545     SharedParameters::Lock l(mParameters);
   1546     if (enable) {
   1547         l.mParameters.playShutterSound = true;
   1548         return OK;
   1549     }
   1550 
   1551     // the camera2 api legacy mode can unconditionally disable the shutter sound
   1552     if (mLegacyMode) {
   1553         ALOGV("%s: Disable shutter sound in legacy mode", __FUNCTION__);
   1554         l.mParameters.playShutterSound = false;
   1555         return OK;
   1556     }
   1557 
   1558     // Disabling shutter sound may not be allowed. In that case only
   1559     // allow the mediaserver process to disable the sound.
   1560     char value[PROPERTY_VALUE_MAX];
   1561     property_get("ro.camera.sound.forced", value, "0");
   1562     if (strncmp(value, "0", 2) != 0) {
   1563         // Disabling shutter sound is not allowed. Deny if the current
   1564         // process is not mediaserver.
   1565         if (getCallingPid() != getpid()) {
   1566             ALOGE("Failed to disable shutter sound. Permission denied (pid %d)",
   1567                     getCallingPid());
   1568             return PERMISSION_DENIED;
   1569         }
   1570     }
   1571 
   1572     l.mParameters.playShutterSound = false;
   1573     return OK;
   1574 }
   1575 
   1576 status_t Camera2Client::commandPlayRecordingSoundL() {
   1577     mCameraService->playSound(CameraService::SOUND_RECORDING);
   1578     return OK;
   1579 }
   1580 
   1581 status_t Camera2Client::commandStartFaceDetectionL(int /*type*/) {
   1582     ALOGV("%s: Camera %d: Starting face detection",
   1583           __FUNCTION__, mCameraId);
   1584     status_t res;
   1585     SharedParameters::Lock l(mParameters);
   1586     switch (l.mParameters.state) {
   1587         case Parameters::DISCONNECTED:
   1588         case Parameters::STOPPED:
   1589         case Parameters::WAITING_FOR_PREVIEW_WINDOW:
   1590         case Parameters::STILL_CAPTURE:
   1591             ALOGE("%s: Camera %d: Cannot start face detection without preview active",
   1592                     __FUNCTION__, mCameraId);
   1593             return INVALID_OPERATION;
   1594         case Parameters::PREVIEW:
   1595         case Parameters::RECORD:
   1596         case Parameters::VIDEO_SNAPSHOT:
   1597             // Good to go for starting face detect
   1598             break;
   1599     }
   1600     // Ignoring type
   1601     if (l.mParameters.fastInfo.bestFaceDetectMode ==
   1602             ANDROID_STATISTICS_FACE_DETECT_MODE_OFF) {
   1603         ALOGE("%s: Camera %d: Face detection not supported",
   1604                 __FUNCTION__, mCameraId);
   1605         return BAD_VALUE;
   1606     }
   1607     if (l.mParameters.enableFaceDetect) return OK;
   1608 
   1609     l.mParameters.enableFaceDetect = true;
   1610 
   1611     res = updateRequests(l.mParameters);
   1612 
   1613     return res;
   1614 }
   1615 
   1616 status_t Camera2Client::commandStopFaceDetectionL(Parameters &params) {
   1617     status_t res = OK;
   1618     ALOGV("%s: Camera %d: Stopping face detection",
   1619           __FUNCTION__, mCameraId);
   1620 
   1621     if (!params.enableFaceDetect) return OK;
   1622 
   1623     params.enableFaceDetect = false;
   1624 
   1625     if (params.state == Parameters::PREVIEW
   1626             || params.state == Parameters::RECORD
   1627             || params.state == Parameters::VIDEO_SNAPSHOT) {
   1628         res = updateRequests(params);
   1629     }
   1630 
   1631     return res;
   1632 }
   1633 
   1634 status_t Camera2Client::commandEnableFocusMoveMsgL(bool enable) {
   1635     SharedParameters::Lock l(mParameters);
   1636     l.mParameters.enableFocusMoveMessages = enable;
   1637 
   1638     return OK;
   1639 }
   1640 
   1641 status_t Camera2Client::commandPingL() {
   1642     // Always ping back if access is proper and device is alive
   1643     SharedParameters::Lock l(mParameters);
   1644     if (l.mParameters.state != Parameters::DISCONNECTED) {
   1645         return OK;
   1646     } else {
   1647         return NO_INIT;
   1648     }
   1649 }
   1650 
   1651 status_t Camera2Client::commandSetVideoBufferCountL(size_t count) {
   1652     if (recordingEnabledL()) {
   1653         ALOGE("%s: Camera %d: Error setting video buffer count after "
   1654                 "recording was started", __FUNCTION__, mCameraId);
   1655         return INVALID_OPERATION;
   1656     }
   1657 
   1658     return mStreamingProcessor->setRecordingBufferCount(count);
   1659 }
   1660 
   1661 /** Device-related methods */
   1662 void Camera2Client::notifyAutoFocus(uint8_t newState, int triggerId) {
   1663     ALOGV("%s: Autofocus state now %d, last trigger %d",
   1664             __FUNCTION__, newState, triggerId);
   1665     bool sendCompletedMessage = false;
   1666     bool sendMovingMessage = false;
   1667 
   1668     bool success = false;
   1669     bool afInMotion = false;
   1670     {
   1671         SharedParameters::Lock l(mParameters);
   1672         // Trace end of AF state
   1673         char tmp[32];
   1674         if (l.mParameters.afStateCounter > 0) {
   1675             camera_metadata_enum_snprint(
   1676                 ANDROID_CONTROL_AF_STATE, l.mParameters.focusState, tmp, sizeof(tmp));
   1677             ATRACE_ASYNC_END(tmp, l.mParameters.afStateCounter);
   1678         }
   1679 
   1680         // Update state
   1681         l.mParameters.focusState = newState;
   1682         l.mParameters.afStateCounter++;
   1683 
   1684         // Trace start of AF state
   1685 
   1686         camera_metadata_enum_snprint(
   1687             ANDROID_CONTROL_AF_STATE, l.mParameters.focusState, tmp, sizeof(tmp));
   1688         ATRACE_ASYNC_BEGIN(tmp, l.mParameters.afStateCounter);
   1689 
   1690         switch (l.mParameters.focusMode) {
   1691             case Parameters::FOCUS_MODE_AUTO:
   1692             case Parameters::FOCUS_MODE_MACRO:
   1693                 // Don't send notifications upstream if they're not for the current AF
   1694                 // trigger. For example, if cancel was called in between, or if we
   1695                 // already sent a notification about this AF call.
   1696                 if (triggerId != l.mParameters.currentAfTriggerId) break;
   1697                 switch (newState) {
   1698                     case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED:
   1699                         success = true;
   1700                         // no break
   1701                     case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
   1702                         sendCompletedMessage = true;
   1703                         l.mParameters.currentAfTriggerId = -1;
   1704                         break;
   1705                     case ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN:
   1706                         // Just starting focusing, ignore
   1707                         break;
   1708                     case ANDROID_CONTROL_AF_STATE_INACTIVE:
   1709                     case ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN:
   1710                     case ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED:
   1711                     case ANDROID_CONTROL_AF_STATE_PASSIVE_UNFOCUSED:
   1712                     default:
   1713                         // Unexpected in AUTO/MACRO mode
   1714                         ALOGE("%s: Unexpected AF state transition in AUTO/MACRO mode: %d",
   1715                                 __FUNCTION__, newState);
   1716                         break;
   1717                 }
   1718                 break;
   1719             case Parameters::FOCUS_MODE_CONTINUOUS_VIDEO:
   1720             case Parameters::FOCUS_MODE_CONTINUOUS_PICTURE:
   1721                 switch (newState) {
   1722                     case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED:
   1723                         success = true;
   1724                         // no break
   1725                     case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
   1726                         // Don't send notifications upstream if they're not for
   1727                         // the current AF trigger. For example, if cancel was
   1728                         // called in between, or if we already sent a
   1729                         // notification about this AF call.
   1730                         // Send both a 'AF done' callback and a 'AF move' callback
   1731                         if (triggerId != l.mParameters.currentAfTriggerId) break;
   1732                         sendCompletedMessage = true;
   1733                         afInMotion = false;
   1734                         if (l.mParameters.enableFocusMoveMessages &&
   1735                                 l.mParameters.afInMotion) {
   1736                             sendMovingMessage = true;
   1737                         }
   1738                         l.mParameters.currentAfTriggerId = -1;
   1739                         break;
   1740                     case ANDROID_CONTROL_AF_STATE_INACTIVE:
   1741                         // Cancel was called, or we switched state; care if
   1742                         // currently moving
   1743                         afInMotion = false;
   1744                         if (l.mParameters.enableFocusMoveMessages &&
   1745                                 l.mParameters.afInMotion) {
   1746                             sendMovingMessage = true;
   1747                         }
   1748                         break;
   1749                     case ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN:
   1750                         // Start passive scan, inform upstream
   1751                         afInMotion = true;
   1752                         // no break
   1753                     case ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED:
   1754                     case ANDROID_CONTROL_AF_STATE_PASSIVE_UNFOCUSED:
   1755                         // Stop passive scan, inform upstream
   1756                         if (l.mParameters.enableFocusMoveMessages) {
   1757                             sendMovingMessage = true;
   1758                         }
   1759                         break;
   1760                 }
   1761                 l.mParameters.afInMotion = afInMotion;
   1762                 break;
   1763             case Parameters::FOCUS_MODE_EDOF:
   1764             case Parameters::FOCUS_MODE_INFINITY:
   1765             case Parameters::FOCUS_MODE_FIXED:
   1766             default:
   1767                 if (newState != ANDROID_CONTROL_AF_STATE_INACTIVE) {
   1768                     ALOGE("%s: Unexpected AF state change %d "
   1769                             "(ID %d) in focus mode %d",
   1770                           __FUNCTION__, newState, triggerId,
   1771                             l.mParameters.focusMode);
   1772                 }
   1773         }
   1774     }
   1775     if (sendMovingMessage) {
   1776         SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
   1777         if (l.mRemoteCallback != 0) {
   1778             l.mRemoteCallback->notifyCallback(CAMERA_MSG_FOCUS_MOVE,
   1779                     afInMotion ? 1 : 0, 0);
   1780         }
   1781     }
   1782     if (sendCompletedMessage) {
   1783         ATRACE_ASYNC_END(kAutofocusLabel, triggerId);
   1784         SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
   1785         if (l.mRemoteCallback != 0) {
   1786             l.mRemoteCallback->notifyCallback(CAMERA_MSG_FOCUS,
   1787                     success ? 1 : 0, 0);
   1788         }
   1789     }
   1790 }
   1791 
   1792 void Camera2Client::notifyAutoExposure(uint8_t newState, int triggerId) {
   1793     ALOGV("%s: Autoexposure state now %d, last trigger %d",
   1794             __FUNCTION__, newState, triggerId);
   1795     mCaptureSequencer->notifyAutoExposure(newState, triggerId);
   1796 }
   1797 
   1798 camera2::SharedParameters& Camera2Client::getParameters() {
   1799     return mParameters;
   1800 }
   1801 
   1802 int Camera2Client::getPreviewStreamId() const {
   1803     return mStreamingProcessor->getPreviewStreamId();
   1804 }
   1805 
   1806 int Camera2Client::getCaptureStreamId() const {
   1807     return mJpegProcessor->getStreamId();
   1808 }
   1809 
   1810 int Camera2Client::getCallbackStreamId() const {
   1811     return mCallbackProcessor->getStreamId();
   1812 }
   1813 
   1814 int Camera2Client::getRecordingStreamId() const {
   1815     return mStreamingProcessor->getRecordingStreamId();
   1816 }
   1817 
   1818 int Camera2Client::getZslStreamId() const {
   1819     return mZslProcessor->getStreamId();
   1820 }
   1821 
   1822 status_t Camera2Client::registerFrameListener(int32_t minId, int32_t maxId,
   1823         wp<camera2::FrameProcessor::FilteredListener> listener, bool sendPartials) {
   1824     return mFrameProcessor->registerListener(minId, maxId, listener, sendPartials);
   1825 }
   1826 
   1827 status_t Camera2Client::removeFrameListener(int32_t minId, int32_t maxId,
   1828         wp<camera2::FrameProcessor::FilteredListener> listener) {
   1829     return mFrameProcessor->removeListener(minId, maxId, listener);
   1830 }
   1831 
   1832 status_t Camera2Client::stopStream() {
   1833     return mStreamingProcessor->stopStream();
   1834 }
   1835 
   1836 const int32_t Camera2Client::kPreviewRequestIdStart;
   1837 const int32_t Camera2Client::kPreviewRequestIdEnd;
   1838 const int32_t Camera2Client::kRecordingRequestIdStart;
   1839 const int32_t Camera2Client::kRecordingRequestIdEnd;
   1840 const int32_t Camera2Client::kCaptureRequestIdStart;
   1841 const int32_t Camera2Client::kCaptureRequestIdEnd;
   1842 
   1843 /** Utility methods */
   1844 
   1845 status_t Camera2Client::updateRequests(Parameters &params) {
   1846     status_t res;
   1847 
   1848     ALOGV("%s: Camera %d: state = %d", __FUNCTION__, getCameraId(), params.state);
   1849 
   1850     res = mStreamingProcessor->incrementStreamingIds();
   1851     if (res != OK) {
   1852         ALOGE("%s: Camera %d: Unable to increment request IDs: %s (%d)",
   1853                 __FUNCTION__, mCameraId, strerror(-res), res);
   1854         return res;
   1855     }
   1856 
   1857     res = mStreamingProcessor->updatePreviewRequest(params);
   1858     if (res != OK) {
   1859         ALOGE("%s: Camera %d: Unable to update preview request: %s (%d)",
   1860                 __FUNCTION__, mCameraId, strerror(-res), res);
   1861         return res;
   1862     }
   1863     res = mStreamingProcessor->updateRecordingRequest(params);
   1864     if (res != OK) {
   1865         ALOGE("%s: Camera %d: Unable to update recording request: %s (%d)",
   1866                 __FUNCTION__, mCameraId, strerror(-res), res);
   1867         return res;
   1868     }
   1869 
   1870     if (params.state == Parameters::PREVIEW) {
   1871         res = startPreviewL(params, true);
   1872         if (res != OK) {
   1873             ALOGE("%s: Camera %d: Error streaming new preview request: %s (%d)",
   1874                     __FUNCTION__, mCameraId, strerror(-res), res);
   1875             return res;
   1876         }
   1877     } else if (params.state == Parameters::RECORD ||
   1878             params.state == Parameters::VIDEO_SNAPSHOT) {
   1879         res = startRecordingL(params, true);
   1880         if (res != OK) {
   1881             ALOGE("%s: Camera %d: Error streaming new record request: %s (%d)",
   1882                     __FUNCTION__, mCameraId, strerror(-res), res);
   1883             return res;
   1884         }
   1885     }
   1886     return res;
   1887 }
   1888 
   1889 
   1890 size_t Camera2Client::calculateBufferSize(int width, int height,
   1891         int format, int stride) {
   1892     switch (format) {
   1893         case HAL_PIXEL_FORMAT_YCbCr_422_SP: // NV16
   1894             return width * height * 2;
   1895         case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21
   1896             return width * height * 3 / 2;
   1897         case HAL_PIXEL_FORMAT_YCbCr_422_I: // YUY2
   1898             return width * height * 2;
   1899         case HAL_PIXEL_FORMAT_YV12: {      // YV12
   1900             size_t ySize = stride * height;
   1901             size_t uvStride = (stride / 2 + 0xF) & ~0xF;
   1902             size_t uvSize = uvStride * height / 2;
   1903             return ySize + uvSize * 2;
   1904         }
   1905         case HAL_PIXEL_FORMAT_RGB_565:
   1906             return width * height * 2;
   1907         case HAL_PIXEL_FORMAT_RGBA_8888:
   1908             return width * height * 4;
   1909         case HAL_PIXEL_FORMAT_RAW_SENSOR:
   1910             return width * height * 2;
   1911         default:
   1912             ALOGE("%s: Unknown preview format: %x",
   1913                     __FUNCTION__,  format);
   1914             return 0;
   1915     }
   1916 }
   1917 
   1918 status_t Camera2Client::syncWithDevice() {
   1919     ATRACE_CALL();
   1920     const nsecs_t kMaxSyncTimeout = 500000000; // 500 ms
   1921     status_t res;
   1922 
   1923     int32_t activeRequestId = mStreamingProcessor->getActiveRequestId();
   1924     if (activeRequestId == 0) return OK;
   1925 
   1926     res = mDevice->waitUntilRequestReceived(activeRequestId, kMaxSyncTimeout);
   1927     if (res == TIMED_OUT) {
   1928         ALOGE("%s: Camera %d: Timed out waiting sync with HAL",
   1929                 __FUNCTION__, mCameraId);
   1930     } else if (res != OK) {
   1931         ALOGE("%s: Camera %d: Error while waiting to sync with HAL",
   1932                 __FUNCTION__, mCameraId);
   1933     }
   1934     return res;
   1935 }
   1936 
   1937 template <typename ProcessorT>
   1938 status_t Camera2Client::updateProcessorStream(sp<ProcessorT> processor,
   1939                                               camera2::Parameters params) {
   1940     // No default template arguments until C++11, so we need this overload
   1941     return updateProcessorStream<ProcessorT, &ProcessorT::updateStream>(
   1942             processor, params);
   1943 }
   1944 
   1945 template <typename ProcessorT,
   1946           status_t (ProcessorT::*updateStreamF)(const Parameters &)>
   1947 status_t Camera2Client::updateProcessorStream(sp<ProcessorT> processor,
   1948                                               Parameters params) {
   1949     status_t res;
   1950 
   1951     // Get raw pointer since sp<T> doesn't have operator->*
   1952     ProcessorT *processorPtr = processor.get();
   1953     res = (processorPtr->*updateStreamF)(params);
   1954 
   1955     /**
   1956      * Can't update the stream if it's busy?
   1957      *
   1958      * Then we need to stop the device (by temporarily clearing the request
   1959      * queue) and then try again. Resume streaming once we're done.
   1960      */
   1961     if (res == -EBUSY) {
   1962         ALOGV("%s: Camera %d: Pausing to update stream", __FUNCTION__,
   1963                 mCameraId);
   1964         res = mStreamingProcessor->togglePauseStream(/*pause*/true);
   1965         if (res != OK) {
   1966             ALOGE("%s: Camera %d: Can't pause streaming: %s (%d)",
   1967                     __FUNCTION__, mCameraId, strerror(-res), res);
   1968         }
   1969 
   1970         res = mDevice->waitUntilDrained();
   1971         if (res != OK) {
   1972             ALOGE("%s: Camera %d: Waiting to stop streaming failed: %s (%d)",
   1973                     __FUNCTION__, mCameraId, strerror(-res), res);
   1974         }
   1975 
   1976         res = (processorPtr->*updateStreamF)(params);
   1977         if (res != OK) {
   1978             ALOGE("%s: Camera %d: Failed to update processing stream "
   1979                   " despite having halted streaming first: %s (%d)",
   1980                   __FUNCTION__, mCameraId, strerror(-res), res);
   1981         }
   1982 
   1983         res = mStreamingProcessor->togglePauseStream(/*pause*/false);
   1984         if (res != OK) {
   1985             ALOGE("%s: Camera %d: Can't unpause streaming: %s (%d)",
   1986                     __FUNCTION__, mCameraId, strerror(-res), res);
   1987         }
   1988     }
   1989 
   1990     return res;
   1991 }
   1992 
   1993 status_t Camera2Client::overrideVideoSnapshotSize(Parameters &params) {
   1994     ALOGV("%s: Camera %d: configure still size to video size before recording"
   1995             , __FUNCTION__, mCameraId);
   1996     params.overrideJpegSizeByVideoSize();
   1997     status_t res = updateProcessorStream(mJpegProcessor, params);
   1998     if (res != OK) {
   1999         ALOGE("%s: Camera %d: Can't override video snapshot size to video size: %s (%d)",
   2000                 __FUNCTION__, mCameraId, strerror(-res), res);
   2001     }
   2002     return res;
   2003 }
   2004 
   2005 const char* Camera2Client::kAutofocusLabel = "autofocus";
   2006 const char* Camera2Client::kTakepictureLabel = "take_picture";
   2007 
   2008 } // namespace android
   2009