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