Home | History | Annotate | Download | only in camera
      1 /*
      2  * Copyright (C) Texas Instruments - http://www.ti.com/
      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 /**
     18 * @file CameraHal.cpp
     19 *
     20 * This file maps the Camera Hardware Interface to V4L2.
     21 *
     22 */
     23 
     24 #define LOG_TAG "CameraHAL"
     25 
     26 #include "CameraHal.h"
     27 #include "ANativeWindowDisplayAdapter.h"
     28 #include "TICameraParameters.h"
     29 #include "CameraProperties.h"
     30 #include <cutils/properties.h>
     31 
     32 #include <poll.h>
     33 #include <math.h>
     34 
     35 namespace android {
     36 
     37 extern "C" CameraAdapter* CameraAdapter_Factory(size_t);
     38 
     39 /*****************************************************************************/
     40 
     41 ////Constant definitions and declarations
     42 ////@todo Have a CameraProperties class to store these parameters as constants for every camera
     43 ////       Currently, they are hard-coded
     44 
     45 const int CameraHal::NO_BUFFERS_PREVIEW = MAX_CAMERA_BUFFERS;
     46 const int CameraHal::NO_BUFFERS_IMAGE_CAPTURE = 2;
     47 
     48 const uint32_t MessageNotifier::EVENT_BIT_FIELD_POSITION = 0;
     49 const uint32_t MessageNotifier::FRAME_BIT_FIELD_POSITION = 0;
     50 
     51 /******************************************************************************/
     52 
     53 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
     54 
     55 struct timeval CameraHal::mStartPreview;
     56 struct timeval CameraHal::mStartFocus;
     57 struct timeval CameraHal::mStartCapture;
     58 
     59 #endif
     60 
     61 static void orientation_cb(uint32_t orientation, uint32_t tilt, void* cookie) {
     62     CameraHal *camera = NULL;
     63 
     64     if (cookie) {
     65         camera = (CameraHal*) cookie;
     66         camera->onOrientationEvent(orientation, tilt);
     67     }
     68 
     69 }
     70 /*-------------Camera Hal Interface Method definitions STARTS here--------------------*/
     71 
     72 /**
     73   Callback function to receive orientation events from SensorListener
     74  */
     75 void CameraHal::onOrientationEvent(uint32_t orientation, uint32_t tilt) {
     76     LOG_FUNCTION_NAME;
     77 
     78     if ( NULL != mCameraAdapter ) {
     79         mCameraAdapter->onOrientationEvent(orientation, tilt);
     80     }
     81 
     82     LOG_FUNCTION_NAME_EXIT;
     83 }
     84 
     85 /**
     86    @brief Set the notification and data callbacks
     87 
     88    @param[in] notify_cb Notify callback for notifying the app about events and errors
     89    @param[in] data_cb   Buffer callback for sending the preview/raw frames to the app
     90    @param[in] data_cb_timestamp Buffer callback for sending the video frames w/ timestamp
     91    @param[in] user  Callback cookie
     92    @return none
     93 
     94  */
     95 void CameraHal::setCallbacks(camera_notify_callback notify_cb,
     96                             camera_data_callback data_cb,
     97                             camera_data_timestamp_callback data_cb_timestamp,
     98                             camera_request_memory get_memory,
     99                             void *user)
    100 {
    101     LOG_FUNCTION_NAME;
    102 
    103     if ( NULL != mAppCallbackNotifier.get() )
    104     {
    105             mAppCallbackNotifier->setCallbacks(this,
    106                                                 notify_cb,
    107                                                 data_cb,
    108                                                 data_cb_timestamp,
    109                                                 get_memory,
    110                                                 user);
    111     }
    112 
    113     LOG_FUNCTION_NAME_EXIT;
    114 }
    115 
    116 /**
    117    @brief Enable a message, or set of messages.
    118 
    119    @param[in] msgtype Bitmask of the messages to enable (defined in include/ui/Camera.h)
    120    @return none
    121 
    122  */
    123 void CameraHal::enableMsgType(int32_t msgType)
    124 {
    125     LOG_FUNCTION_NAME;
    126 
    127     if ( ( msgType & CAMERA_MSG_SHUTTER ) && ( !mShutterEnabled ) )
    128         {
    129         msgType &= ~CAMERA_MSG_SHUTTER;
    130         }
    131 
    132     // ignoring enable focus message from camera service
    133     // we will enable internally in autoFocus call
    134     if (msgType & CAMERA_MSG_FOCUS) {
    135         msgType &= ~CAMERA_MSG_FOCUS;
    136     }
    137 
    138     {
    139     Mutex::Autolock lock(mLock);
    140     mMsgEnabled |= msgType;
    141     }
    142 
    143     if(mMsgEnabled & CAMERA_MSG_PREVIEW_FRAME)
    144     {
    145         if(mDisplayPaused)
    146         {
    147             CAMHAL_LOGDA("Preview currently paused...will enable preview callback when restarted");
    148             msgType &= ~CAMERA_MSG_PREVIEW_FRAME;
    149         }else
    150         {
    151             CAMHAL_LOGDA("Enabling Preview Callback");
    152         }
    153     }
    154     else
    155     {
    156         CAMHAL_LOGDB("Preview callback not enabled %x", msgType);
    157     }
    158 
    159 
    160     ///Configure app callback notifier with the message callback required
    161     mAppCallbackNotifier->enableMsgType (msgType);
    162 
    163     LOG_FUNCTION_NAME_EXIT;
    164 }
    165 
    166 /**
    167    @brief Disable a message, or set of messages.
    168 
    169    @param[in] msgtype Bitmask of the messages to disable (defined in include/ui/Camera.h)
    170    @return none
    171 
    172  */
    173 void CameraHal::disableMsgType(int32_t msgType)
    174 {
    175     LOG_FUNCTION_NAME;
    176 
    177         {
    178         Mutex::Autolock lock(mLock);
    179         mMsgEnabled &= ~msgType;
    180         }
    181 
    182     if( msgType & CAMERA_MSG_PREVIEW_FRAME)
    183         {
    184         CAMHAL_LOGDA("Disabling Preview Callback");
    185         }
    186 
    187     ///Configure app callback notifier
    188     mAppCallbackNotifier->disableMsgType (msgType);
    189 
    190     LOG_FUNCTION_NAME_EXIT;
    191 }
    192 
    193 /**
    194    @brief Query whether a message, or a set of messages, is enabled.
    195 
    196    Note that this is operates as an AND, if any of the messages queried are off, this will
    197    return false.
    198 
    199    @param[in] msgtype Bitmask of the messages to query (defined in include/ui/Camera.h)
    200    @return true If all message types are enabled
    201           false If any message type
    202 
    203  */
    204 int CameraHal::msgTypeEnabled(int32_t msgType)
    205 {
    206     LOG_FUNCTION_NAME;
    207     Mutex::Autolock lock(mLock);
    208     LOG_FUNCTION_NAME_EXIT;
    209     return (mMsgEnabled & msgType);
    210 }
    211 
    212 /**
    213    @brief Set the camera parameters.
    214 
    215    @param[in] params Camera parameters to configure the camera
    216    @return NO_ERROR
    217    @todo Define error codes
    218 
    219  */
    220 int CameraHal::setParameters(const char* parameters)
    221 {
    222 
    223    LOG_FUNCTION_NAME;
    224 
    225     CameraParameters params;
    226 
    227     String8 str_params(parameters);
    228     params.unflatten(str_params);
    229 
    230     LOG_FUNCTION_NAME_EXIT;
    231 
    232     return setParameters(params);
    233 }
    234 
    235 /**
    236    @brief Set the camera parameters.
    237 
    238    @param[in] params Camera parameters to configure the camera
    239    @return NO_ERROR
    240    @todo Define error codes
    241 
    242  */
    243 int CameraHal::setParameters(const CameraParameters& params)
    244 {
    245 
    246    LOG_FUNCTION_NAME;
    247 
    248     int w, h;
    249     int w_orig, h_orig;
    250     int framerate,minframerate;
    251     int maxFPS, minFPS;
    252     int error;
    253     int base;
    254     const char *valstr = NULL;
    255     const char *prevFormat;
    256     char *af_coord;
    257     TIUTILS::Message msg;
    258     status_t ret = NO_ERROR;
    259     // Needed for KEY_RECORDING_HINT
    260     bool restartPreviewRequired = false;
    261     bool updateRequired = false;
    262     CameraParameters oldParams(mParameters.flatten());
    263     bool videoMode = false;
    264     char range[MAX_PROP_VALUE_LENGTH];
    265 
    266     {
    267         Mutex::Autolock lock(mLock);
    268 
    269         ///Ensure that preview is not enabled when the below parameters are changed.
    270         if(!previewEnabled())
    271             {
    272 
    273             CAMHAL_LOGDB("PreviewFormat %s", params.getPreviewFormat());
    274 
    275             if ((valstr = params.getPreviewFormat()) != NULL) {
    276                 if ( isParameterValid(valstr, mCameraProperties->get(CameraProperties::SUPPORTED_PREVIEW_FORMATS))) {
    277                     mParameters.setPreviewFormat(valstr);
    278                 } else {
    279                     CAMHAL_LOGEB("Invalid preview format.Supported: %s",  mCameraProperties->get(CameraProperties::SUPPORTED_PREVIEW_FORMATS));
    280                     return -EINVAL;
    281                 }
    282             }
    283 
    284             if ((valstr = params.get(TICameraParameters::KEY_VNF)) != NULL) {
    285                 if ( (params.getInt(TICameraParameters::KEY_VNF)==0) || (params.getInt(TICameraParameters::KEY_VNF)==1) ) {
    286                     CAMHAL_LOGDB("VNF set %s", params.get(TICameraParameters::KEY_VNF));
    287                     mParameters.set(TICameraParameters::KEY_VNF, valstr);
    288                 } else {
    289                     CAMHAL_LOGEB("ERROR: Invalid VNF: %s", valstr);
    290                     ret = -EINVAL;
    291                 }
    292             }
    293 
    294             if ((valstr = params.get(CameraParameters::KEY_VIDEO_STABILIZATION)) != NULL) {
    295                 // make sure we support vstab...if we don't and application is trying to set
    296                 // vstab then return an error
    297                 if (strcmp(mCameraProperties->get(CameraProperties::VSTAB_SUPPORTED),
    298                            CameraParameters::TRUE) == 0) {
    299                     CAMHAL_LOGDB("VSTAB %s",
    300                                   params.get(CameraParameters::KEY_VIDEO_STABILIZATION));
    301                     mParameters.set(CameraParameters::KEY_VIDEO_STABILIZATION,
    302                                     params.get(CameraParameters::KEY_VIDEO_STABILIZATION));
    303                 } else if (strcmp(valstr, CameraParameters::TRUE) == 0) {
    304                     CAMHAL_LOGEB("ERROR: Invalid VSTAB: %s", valstr);
    305                     ret = -EINVAL;
    306                 } else {
    307                     mParameters.set(CameraParameters::KEY_VIDEO_STABILIZATION,
    308                                     CameraParameters::FALSE);
    309                 }
    310             }
    311 
    312             if( (valstr = params.get(TICameraParameters::KEY_CAP_MODE)) != NULL)
    313                 {
    314                 CAMHAL_LOGDB("Capture mode set %s", params.get(TICameraParameters::KEY_CAP_MODE));
    315                 mParameters.set(TICameraParameters::KEY_CAP_MODE, valstr);
    316                 }
    317 
    318             if ((valstr = params.get(TICameraParameters::KEY_IPP)) != NULL) {
    319                 if (isParameterValid(valstr,mCameraProperties->get(CameraProperties::SUPPORTED_IPP_MODES))) {
    320                     CAMHAL_LOGDB("IPP mode set %s", params.get(TICameraParameters::KEY_IPP));
    321                     mParameters.set(TICameraParameters::KEY_IPP, valstr);
    322                 } else {
    323                     CAMHAL_LOGEB("ERROR: Invalid IPP mode: %s", valstr);
    324                     ret = -EINVAL;
    325                 }
    326             }
    327 
    328             if((valstr = params.get(TICameraParameters::KEY_S3D2D_PREVIEW)) != NULL)
    329                 {
    330                 CAMHAL_LOGDB("Stereo 3D->2D Preview mode is %s", params.get(TICameraParameters::KEY_S3D2D_PREVIEW));
    331                 mParameters.set(TICameraParameters::KEY_S3D2D_PREVIEW, valstr);
    332                 }
    333 
    334             if((valstr = params.get(TICameraParameters::KEY_AUTOCONVERGENCE)) != NULL)
    335                 {
    336                 CAMHAL_LOGDB("AutoConvergence mode is %s", params.get(TICameraParameters::KEY_AUTOCONVERGENCE));
    337                 mParameters.set(TICameraParameters::KEY_AUTOCONVERGENCE, valstr);
    338                 }
    339 
    340             }
    341 
    342             params.getPreviewSize(&w, &h);
    343             if (w == -1 && h == -1) {
    344                 CAMHAL_LOGEA("Unable to get preview size");
    345                 return -EINVAL;
    346               }
    347 
    348             int oldWidth, oldHeight;
    349             mParameters.getPreviewSize(&oldWidth, &oldHeight);
    350 
    351             int orientation =0;
    352             if((valstr = params.get(TICameraParameters::KEY_SENSOR_ORIENTATION)) != NULL)
    353                 {
    354                 CAMHAL_LOGDB("Sensor Orientation is set to %s", params.get(TICameraParameters::KEY_SENSOR_ORIENTATION));
    355                 mParameters.set(TICameraParameters::KEY_SENSOR_ORIENTATION, valstr);
    356                 orientation = params.getInt(TICameraParameters::KEY_SENSOR_ORIENTATION);
    357                 }
    358 
    359             if(orientation ==90 || orientation ==270)
    360            {
    361               if ( !isResolutionValid(h,w, mCameraProperties->get(CameraProperties::SUPPORTED_PREVIEW_SIZES)))
    362                {
    363                 CAMHAL_LOGEB("Invalid preview resolution %d x %d", w, h);
    364                 return -EINVAL;
    365                }
    366               else
    367               {
    368                 mParameters.setPreviewSize(w, h);
    369                 mVideoWidth = w;
    370                 mVideoHeight = h;
    371                }
    372            }
    373            else
    374            {
    375             if ( !isResolutionValid(w, h, mCameraProperties->get(CameraProperties::SUPPORTED_PREVIEW_SIZES)))
    376                 {
    377                 CAMHAL_LOGEB("Invalid preview resolution %d x %d", w, h);
    378                 return -EINVAL;
    379                 }
    380             else
    381                 {
    382                 mParameters.setPreviewSize(w, h);
    383                 }
    384            }
    385 
    386             if ( ( oldWidth != w ) || ( oldHeight != h ) )
    387                 {
    388                 restartPreviewRequired |= true;
    389                 }
    390 
    391             CAMHAL_LOGDB("PreviewResolution by App %d x %d", w, h);
    392 
    393         // Handle RECORDING_HINT to Set/Reset Video Mode Parameters
    394         valstr = params.get(CameraParameters::KEY_RECORDING_HINT);
    395         if(valstr != NULL)
    396             {
    397             if(strcmp(valstr, CameraParameters::TRUE) == 0)
    398                 {
    399                 CAMHAL_LOGDB("Recording Hint is set to %s", valstr);
    400                 mParameters.set(CameraParameters::KEY_RECORDING_HINT, valstr);
    401                 videoMode = true;
    402                 int w, h;
    403 
    404                 params.getPreviewSize(&w, &h);
    405                 CAMHAL_LOGVB("%s Preview Width=%d Height=%d\n", __FUNCTION__, w, h);
    406                 //HACK FOR MMS
    407                 mVideoWidth = w;
    408                 mVideoHeight = h;
    409                 CAMHAL_LOGVB("%s Video Width=%d Height=%d\n", __FUNCTION__, mVideoWidth, mVideoHeight);
    410 
    411                 setPreferredPreviewRes(w, h);
    412                 mParameters.getPreviewSize(&w, &h);
    413                 CAMHAL_LOGVB("%s Preview Width=%d Height=%d\n", __FUNCTION__, w, h);
    414                 //Avoid restarting preview for MMS HACK
    415                 if ((w != mVideoWidth) && (h != mVideoHeight))
    416                     {
    417                     restartPreviewRequired = false;
    418                     }
    419 
    420                 restartPreviewRequired |= setVideoModeParameters(params);
    421                 }
    422             else if(strcmp(valstr, CameraParameters::FALSE) == 0)
    423                 {
    424                 CAMHAL_LOGDB("Recording Hint is set to %s", valstr);
    425                 mParameters.set(CameraParameters::KEY_RECORDING_HINT, valstr);
    426                 restartPreviewRequired |= resetVideoModeParameters();
    427                 params.getPreviewSize(&mVideoWidth, &mVideoHeight);
    428                 }
    429             else
    430                 {
    431                 CAMHAL_LOGEA("Invalid RECORDING_HINT");
    432                 return -EINVAL;
    433                 }
    434             }
    435         else
    436             {
    437             // This check is required in following case.
    438             // If VideoRecording activity sets KEY_RECORDING_HINT to TRUE and
    439             // ImageCapture activity doesnot set KEY_RECORDING_HINT to FALSE (i.e. simply NULL),
    440             // then Video Mode parameters may remain present in ImageCapture activity as well.
    441             CAMHAL_LOGDA("Recording Hint is set to NULL");
    442             mParameters.set(CameraParameters::KEY_RECORDING_HINT, "");
    443             restartPreviewRequired |= resetVideoModeParameters();
    444             params.getPreviewSize(&mVideoWidth, &mVideoHeight);
    445             }
    446 
    447         if ((valstr = params.get(CameraParameters::KEY_FOCUS_MODE)) != NULL) {
    448             if (isParameterValid(valstr, mCameraProperties->get(CameraProperties::SUPPORTED_FOCUS_MODES))) {
    449                 CAMHAL_LOGDB("Focus mode set %s", params.get(CameraParameters::KEY_FOCUS_MODE));
    450 
    451                 // we need to take a decision on the capture mode based on whether CAF picture or
    452                 // video is chosen so the behavior of each is consistent to the application
    453                 if(strcmp(valstr, CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE) == 0){
    454                     restartPreviewRequired |= resetVideoModeParameters();
    455                 } else if (strcmp(valstr, CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO) == 0){
    456                     restartPreviewRequired |= setVideoModeParameters(params);
    457                 }
    458 
    459                 mParameters.set(CameraParameters::KEY_FOCUS_MODE, valstr);
    460              } else {
    461                 CAMHAL_LOGEB("ERROR: Invalid FOCUS mode = %s", valstr);
    462                 ret = -EINVAL;
    463              }
    464         }
    465 
    466         ///Below parameters can be changed when the preview is running
    467         if ( (valstr = params.getPictureFormat()) != NULL ) {
    468             if (isParameterValid(params.getPictureFormat(),mCameraProperties->get(CameraProperties::SUPPORTED_PICTURE_FORMATS))) {
    469                 mParameters.setPictureFormat(valstr);
    470             } else {
    471                 CAMHAL_LOGEB("ERROR: Invalid picture format: %s",valstr);
    472                 ret = -EINVAL;
    473             }
    474             }
    475 
    476         params.getPictureSize(&w, &h);
    477         if ( isResolutionValid(w, h, mCameraProperties->get(CameraProperties::SUPPORTED_PICTURE_SIZES))) {
    478             mParameters.setPictureSize(w, h);
    479         } else {
    480             CAMHAL_LOGEB("ERROR: Invalid picture resolution %dx%d", w, h);
    481             ret = -EINVAL;
    482         }
    483 
    484         CAMHAL_LOGDB("Picture Size by App %d x %d", w, h);
    485 
    486         if ((valstr = params.get(TICameraParameters::KEY_BURST)) != NULL) {
    487             if (params.getInt(TICameraParameters::KEY_BURST) >=0) {
    488                 CAMHAL_LOGDB("Burst set %s", params.get(TICameraParameters::KEY_BURST));
    489                 mParameters.set(TICameraParameters::KEY_BURST, valstr);
    490             } else {
    491                 CAMHAL_LOGEB("ERROR: Invalid Burst value: %s",valstr);
    492                 ret = -EINVAL;
    493             }
    494         }
    495 
    496         framerate = params.getPreviewFrameRate();
    497         valstr = params.get(CameraParameters::KEY_PREVIEW_FPS_RANGE);
    498         CAMHAL_LOGDB("FRAMERATE %d", framerate);
    499 
    500         CAMHAL_LOGVB("Passed FRR: %s, Supported FRR %s", valstr
    501                         , mCameraProperties->get(CameraProperties::FRAMERATE_RANGE_SUPPORTED));
    502         CAMHAL_LOGVB("Passed FR: %d, Supported FR %s", framerate
    503                         , mCameraProperties->get(CameraProperties::SUPPORTED_PREVIEW_FRAME_RATES));
    504 
    505 
    506         //Perform parameter validation
    507         if(!isParameterValid(valstr
    508                         , mCameraProperties->get(CameraProperties::FRAMERATE_RANGE_SUPPORTED))
    509                         || !isParameterValid(framerate,
    510                                       mCameraProperties->get(CameraProperties::SUPPORTED_PREVIEW_FRAME_RATES)))
    511         {
    512             CAMHAL_LOGEA("Invalid frame rate range or frame rate");
    513             return -EINVAL;
    514         }
    515 
    516         // Variable framerate ranges have higher priority over
    517         // deprecated constant FPS. "KEY_PREVIEW_FPS_RANGE" should
    518         // be cleared by the client in order for constant FPS to get
    519         // applied.
    520         if ( strcmp(valstr, mCameraProperties->get(CameraProperties::FRAMERATE_RANGE))  != 0)
    521           {
    522             // APP wants to set FPS range
    523             //Set framerate = MAXFPS
    524             CAMHAL_LOGDA("APP IS CHANGING FRAME RATE RANGE");
    525             params.getPreviewFpsRange(&minFPS, &maxFPS);
    526 
    527             if ( ( 0 > minFPS ) || ( 0 > maxFPS ) )
    528               {
    529                 CAMHAL_LOGEA("ERROR: FPS Range is negative!");
    530                 return -EINVAL;
    531               }
    532 
    533             framerate = maxFPS /CameraHal::VFR_SCALE;
    534 
    535           }
    536         else
    537           {
    538               if ( framerate != atoi(mCameraProperties->get(CameraProperties::PREVIEW_FRAME_RATE)) )
    539               {
    540 
    541                 selectFPSRange(framerate, &minFPS, &maxFPS);
    542                 CAMHAL_LOGDB("Select FPS Range %d %d", minFPS, maxFPS);
    543               }
    544               else
    545                 {
    546                     if (videoMode) {
    547                         valstr = mCameraProperties->get(CameraProperties::FRAMERATE_RANGE_VIDEO);
    548                         CameraParameters temp;
    549                         temp.set(CameraParameters::KEY_PREVIEW_FPS_RANGE, valstr);
    550                         temp.getPreviewFpsRange(&minFPS, &maxFPS);
    551                     }
    552                     else {
    553                         valstr = mCameraProperties->get(CameraProperties::FRAMERATE_RANGE_IMAGE);
    554                         CameraParameters temp;
    555                         temp.set(CameraParameters::KEY_PREVIEW_FPS_RANGE, valstr);
    556                         temp.getPreviewFpsRange(&minFPS, &maxFPS);
    557                     }
    558 
    559                     framerate = maxFPS / CameraHal::VFR_SCALE;
    560                 }
    561 
    562           }
    563 
    564         CAMHAL_LOGDB("FPS Range = %s", valstr);
    565         CAMHAL_LOGDB("DEFAULT FPS Range = %s", mCameraProperties->get(CameraProperties::FRAMERATE_RANGE));
    566 
    567         minFPS /= CameraHal::VFR_SCALE;
    568         maxFPS /= CameraHal::VFR_SCALE;
    569 
    570         if ( ( 0 == minFPS ) || ( 0 == maxFPS ) )
    571           {
    572             CAMHAL_LOGEA("ERROR: FPS Range is invalid!");
    573             ret = -EINVAL;
    574           }
    575 
    576         if ( maxFPS < minFPS )
    577           {
    578             CAMHAL_LOGEA("ERROR: Max FPS is smaller than Min FPS!");
    579             ret = -EINVAL;
    580           }
    581         CAMHAL_LOGDB("SET FRAMERATE %d", framerate);
    582         mParameters.setPreviewFrameRate(framerate);
    583         mParameters.set(CameraParameters::KEY_PREVIEW_FPS_RANGE, params.get(CameraParameters::KEY_PREVIEW_FPS_RANGE));
    584 
    585         CAMHAL_LOGDB("FPS Range [%d, %d]", minFPS, maxFPS);
    586         mParameters.set(TICameraParameters::KEY_MINFRAMERATE, minFPS);
    587         mParameters.set(TICameraParameters::KEY_MAXFRAMERATE, maxFPS);
    588 
    589         if( ( valstr = params.get(TICameraParameters::KEY_GBCE) ) != NULL )
    590             {
    591             CAMHAL_LOGDB("GBCE Value = %s", valstr);
    592             mParameters.set(TICameraParameters::KEY_GBCE, valstr);
    593             }
    594 
    595         if( ( valstr = params.get(TICameraParameters::KEY_GLBCE) ) != NULL )
    596             {
    597             CAMHAL_LOGDB("GLBCE Value = %s", valstr);
    598             mParameters.set(TICameraParameters::KEY_GLBCE, valstr);
    599             }
    600 
    601         ///Update the current parameter set
    602         if( (valstr = params.get(TICameraParameters::KEY_AUTOCONVERGENCE)) != NULL)
    603             {
    604             CAMHAL_LOGDB("AutoConvergence Mode is set = %s", params.get(TICameraParameters::KEY_AUTOCONVERGENCE));
    605             mParameters.set(TICameraParameters::KEY_AUTOCONVERGENCE, valstr);
    606             }
    607 
    608         if( (valstr = params.get(TICameraParameters::KEY_MANUALCONVERGENCE_VALUES)) !=NULL )
    609             {
    610             CAMHAL_LOGDB("ManualConvergence Value = %s", params.get(TICameraParameters::KEY_MANUALCONVERGENCE_VALUES));
    611             mParameters.set(TICameraParameters::KEY_MANUALCONVERGENCE_VALUES, valstr);
    612             }
    613 
    614         if ((valstr = params.get(TICameraParameters::KEY_EXPOSURE_MODE)) != NULL) {
    615             if (isParameterValid(valstr, mCameraProperties->get(CameraProperties::SUPPORTED_EXPOSURE_MODES))) {
    616                 CAMHAL_LOGDB("Exposure set = %s", valstr);
    617                 mParameters.set(TICameraParameters::KEY_EXPOSURE_MODE, valstr);
    618             } else {
    619                 CAMHAL_LOGEB("ERROR: Invalid Exposure  = %s", valstr);
    620                 ret = -EINVAL;
    621             }
    622         }
    623 
    624         if ((valstr = params.get(CameraParameters::KEY_WHITE_BALANCE)) != NULL) {
    625            if ( isParameterValid(valstr, mCameraProperties->get(CameraProperties::SUPPORTED_WHITE_BALANCE))) {
    626                CAMHAL_LOGDB("White balance set %s", valstr);
    627                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, valstr);
    628             } else {
    629                CAMHAL_LOGEB("ERROR: Invalid white balance  = %s", valstr);
    630                ret = -EINVAL;
    631             }
    632         }
    633 
    634         if ((valstr = params.get(TICameraParameters::KEY_CONTRAST)) != NULL) {
    635             if (params.getInt(TICameraParameters::KEY_CONTRAST) >= 0 ) {
    636                 CAMHAL_LOGDB("Contrast set %s", valstr);
    637                 mParameters.set(TICameraParameters::KEY_CONTRAST, valstr);
    638             } else {
    639                 CAMHAL_LOGEB("ERROR: Invalid Contrast  = %s", valstr);
    640                 ret = -EINVAL;
    641             }
    642         }
    643 
    644         if ((valstr =params.get(TICameraParameters::KEY_SHARPNESS)) != NULL) {
    645             if (params.getInt(TICameraParameters::KEY_SHARPNESS) >= 0 ) {
    646                 CAMHAL_LOGDB("Sharpness set %s", valstr);
    647                 mParameters.set(TICameraParameters::KEY_SHARPNESS, valstr);
    648             } else {
    649                 CAMHAL_LOGEB("ERROR: Invalid Sharpness = %s", valstr);
    650                 ret = -EINVAL;
    651             }
    652         }
    653 
    654         if ((valstr = params.get(TICameraParameters::KEY_SATURATION)) != NULL) {
    655             if (params.getInt(TICameraParameters::KEY_SATURATION) >= 0 ) {
    656                 CAMHAL_LOGDB("Saturation set %s", valstr);
    657                 mParameters.set(TICameraParameters::KEY_SATURATION, valstr);
    658              } else {
    659                 CAMHAL_LOGEB("ERROR: Invalid Saturation = %s", valstr);
    660                 ret = -EINVAL;
    661             }
    662         }
    663 
    664         if ((valstr = params.get(TICameraParameters::KEY_BRIGHTNESS)) != NULL) {
    665             if (params.getInt(TICameraParameters::KEY_BRIGHTNESS) >= 0 ) {
    666                 CAMHAL_LOGDB("Brightness set %s", valstr);
    667                 mParameters.set(TICameraParameters::KEY_BRIGHTNESS, valstr);
    668             } else {
    669                 CAMHAL_LOGEB("ERROR: Invalid Brightness = %s", valstr);
    670                 ret = -EINVAL;
    671             }
    672          }
    673 
    674         if ((valstr = params.get(CameraParameters::KEY_ANTIBANDING)) != NULL) {
    675             if (isParameterValid(valstr, mCameraProperties->get(CameraProperties::SUPPORTED_ANTIBANDING))) {
    676                 CAMHAL_LOGDB("Antibanding set %s", valstr);
    677                 mParameters.set(CameraParameters::KEY_ANTIBANDING, valstr);
    678              } else {
    679                 CAMHAL_LOGEB("ERROR: Invalid Antibanding = %s", valstr);
    680                 ret = -EINVAL;
    681              }
    682          }
    683 
    684         if ((valstr = params.get(TICameraParameters::KEY_ISO)) != NULL) {
    685             if (isParameterValid(valstr, mCameraProperties->get(CameraProperties::SUPPORTED_ISO_VALUES))) {
    686                 CAMHAL_LOGDB("ISO set %s", valstr);
    687                 mParameters.set(TICameraParameters::KEY_ISO, valstr);
    688             } else {
    689                 CAMHAL_LOGEB("ERROR: Invalid ISO = %s", valstr);
    690                 ret = -EINVAL;
    691             }
    692         }
    693 
    694         if( (valstr = params.get(CameraParameters::KEY_FOCUS_AREAS)) != NULL )
    695             {
    696             CAMHAL_LOGEB("Focus areas position set %s", params.get(CameraParameters::KEY_FOCUS_AREAS));
    697             mParameters.set(CameraParameters::KEY_FOCUS_AREAS, valstr);
    698             }
    699 
    700         if( (valstr = params.get(TICameraParameters::KEY_MEASUREMENT_ENABLE)) != NULL )
    701             {
    702             CAMHAL_LOGDB("Measurements set to %s", params.get(TICameraParameters::KEY_MEASUREMENT_ENABLE));
    703             mParameters.set(TICameraParameters::KEY_MEASUREMENT_ENABLE, valstr);
    704 
    705             if (strcmp(valstr, (const char *) TICameraParameters::MEASUREMENT_ENABLE) == 0)
    706                 {
    707                 mMeasurementEnabled = true;
    708                 }
    709             else if (strcmp(valstr, (const char *) TICameraParameters::MEASUREMENT_DISABLE) == 0)
    710                 {
    711                 mMeasurementEnabled = false;
    712                 }
    713             else
    714                 {
    715                 mMeasurementEnabled = false;
    716                 }
    717 
    718             }
    719 
    720         if( (valstr = params.get(CameraParameters::KEY_EXPOSURE_COMPENSATION)) != NULL)
    721             {
    722             CAMHAL_LOGDB("Exposure compensation set %s", params.get(CameraParameters::KEY_EXPOSURE_COMPENSATION));
    723             mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, valstr);
    724             }
    725 
    726         if ((valstr = params.get(CameraParameters::KEY_SCENE_MODE)) != NULL) {
    727             if (isParameterValid(valstr, mCameraProperties->get(CameraProperties::SUPPORTED_SCENE_MODES))) {
    728                 CAMHAL_LOGDB("Scene mode set %s", valstr);
    729                 doesSetParameterNeedUpdate(valstr,
    730                                            mParameters.get(CameraParameters::KEY_SCENE_MODE),
    731                                            updateRequired);
    732                 mParameters.set(CameraParameters::KEY_SCENE_MODE, valstr);
    733             } else {
    734                 CAMHAL_LOGEB("ERROR: Invalid Scene mode = %s", valstr);
    735                 ret = -EINVAL;
    736             }
    737         }
    738 
    739         if ((valstr = params.get(CameraParameters::KEY_FLASH_MODE)) != NULL) {
    740             if (isParameterValid(valstr, mCameraProperties->get(CameraProperties::SUPPORTED_FLASH_MODES))) {
    741                 CAMHAL_LOGDB("Flash mode set %s", valstr);
    742                 mParameters.set(CameraParameters::KEY_FLASH_MODE, valstr);
    743             } else {
    744                 CAMHAL_LOGEB("ERROR: Invalid Flash mode = %s", valstr);
    745                 ret = -EINVAL;
    746             }
    747         }
    748 
    749         if ((valstr = params.get(CameraParameters::KEY_EFFECT)) != NULL) {
    750             if (isParameterValid(valstr, mCameraProperties->get(CameraProperties::SUPPORTED_EFFECTS))) {
    751                 CAMHAL_LOGDB("Effect set %s", valstr);
    752                 mParameters.set(CameraParameters::KEY_EFFECT, valstr);
    753              } else {
    754                 CAMHAL_LOGEB("ERROR: Invalid Effect = %s", valstr);
    755                 ret = -EINVAL;
    756              }
    757         }
    758 
    759         if(( (valstr = params.get(CameraParameters::KEY_ROTATION)) != NULL)
    760             && (params.getInt(CameraParameters::KEY_ROTATION) >=0))
    761             {
    762             CAMHAL_LOGDB("Rotation set %s", params.get(CameraParameters::KEY_ROTATION));
    763             mParameters.set(CameraParameters::KEY_ROTATION, valstr);
    764             }
    765 
    766         if(( (valstr = params.get(CameraParameters::KEY_JPEG_QUALITY)) != NULL)
    767             && (params.getInt(CameraParameters::KEY_JPEG_QUALITY) >=0))
    768             {
    769             CAMHAL_LOGDB("Jpeg quality set %s", params.get(CameraParameters::KEY_JPEG_QUALITY));
    770             mParameters.set(CameraParameters::KEY_JPEG_QUALITY, valstr);
    771             }
    772 
    773         if(( (valstr = params.get(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH)) != NULL)
    774             && (params.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH) >=0))
    775             {
    776             CAMHAL_LOGDB("Thumbnail width set %s", params.get(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH));
    777             mParameters.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH, valstr);
    778             }
    779 
    780         if(( (valstr = params.get(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT)) != NULL)
    781             && (params.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT) >=0))
    782             {
    783             CAMHAL_LOGDB("Thumbnail width set %s", params.get(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT));
    784             mParameters.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT, valstr);
    785             }
    786 
    787         if(( (valstr = params.get(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY)) != NULL )
    788             && (params.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY) >=0))
    789             {
    790             CAMHAL_LOGDB("Thumbnail quality set %s", params.get(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY));
    791             mParameters.set(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY, valstr);
    792             }
    793 
    794         if( (valstr = params.get(CameraParameters::KEY_GPS_LATITUDE)) != NULL )
    795             {
    796             CAMHAL_LOGDB("GPS latitude set %s", params.get(CameraParameters::KEY_GPS_LATITUDE));
    797             mParameters.set(CameraParameters::KEY_GPS_LATITUDE, valstr);
    798             }else{
    799                 mParameters.remove(CameraParameters::KEY_GPS_LATITUDE);
    800             }
    801 
    802         if( (valstr = params.get(CameraParameters::KEY_GPS_LONGITUDE)) != NULL )
    803             {
    804             CAMHAL_LOGDB("GPS longitude set %s", params.get(CameraParameters::KEY_GPS_LONGITUDE));
    805             mParameters.set(CameraParameters::KEY_GPS_LONGITUDE, valstr);
    806             }else{
    807                 mParameters.remove(CameraParameters::KEY_GPS_LONGITUDE);
    808             }
    809 
    810         if( (valstr = params.get(CameraParameters::KEY_GPS_ALTITUDE)) != NULL )
    811             {
    812             CAMHAL_LOGDB("GPS altitude set %s", params.get(CameraParameters::KEY_GPS_ALTITUDE));
    813             mParameters.set(CameraParameters::KEY_GPS_ALTITUDE, valstr);
    814             }else{
    815                 mParameters.remove(CameraParameters::KEY_GPS_ALTITUDE);
    816             }
    817 
    818         if( (valstr = params.get(CameraParameters::KEY_GPS_TIMESTAMP)) != NULL )
    819             {
    820             CAMHAL_LOGDB("GPS timestamp set %s", params.get(CameraParameters::KEY_GPS_TIMESTAMP));
    821             mParameters.set(CameraParameters::KEY_GPS_TIMESTAMP, valstr);
    822             }else{
    823                 mParameters.remove(CameraParameters::KEY_GPS_TIMESTAMP);
    824             }
    825 
    826         if( (valstr = params.get(TICameraParameters::KEY_GPS_DATESTAMP)) != NULL )
    827             {
    828             CAMHAL_LOGDB("GPS datestamp set %s", params.get(TICameraParameters::KEY_GPS_DATESTAMP));
    829             mParameters.set(TICameraParameters::KEY_GPS_DATESTAMP, valstr);
    830             }else{
    831                 mParameters.remove(TICameraParameters::KEY_GPS_DATESTAMP);
    832             }
    833 
    834         if( (valstr = params.get(CameraParameters::KEY_GPS_PROCESSING_METHOD)) != NULL )
    835             {
    836             CAMHAL_LOGDB("GPS processing method set %s", params.get(CameraParameters::KEY_GPS_PROCESSING_METHOD));
    837             mParameters.set(CameraParameters::KEY_GPS_PROCESSING_METHOD, valstr);
    838             }else{
    839                 mParameters.remove(CameraParameters::KEY_GPS_PROCESSING_METHOD);
    840             }
    841 
    842         if( (valstr = params.get(TICameraParameters::KEY_GPS_MAPDATUM )) != NULL )
    843             {
    844             CAMHAL_LOGDB("GPS MAPDATUM set %s", params.get(TICameraParameters::KEY_GPS_MAPDATUM));
    845             mParameters.set(TICameraParameters::KEY_GPS_MAPDATUM, valstr);
    846             }else{
    847                 mParameters.remove(TICameraParameters::KEY_GPS_MAPDATUM);
    848             }
    849 
    850         if( (valstr = params.get(TICameraParameters::KEY_GPS_VERSION)) != NULL )
    851             {
    852             CAMHAL_LOGDB("GPS MAPDATUM set %s", params.get(TICameraParameters::KEY_GPS_VERSION));
    853             mParameters.set(TICameraParameters::KEY_GPS_VERSION, valstr);
    854             }else{
    855                 mParameters.remove(TICameraParameters::KEY_GPS_VERSION);
    856             }
    857 
    858         if( (valstr = params.get(TICameraParameters::KEY_EXIF_MODEL)) != NULL )
    859             {
    860             CAMHAL_LOGDB("EXIF Model set %s", params.get(TICameraParameters::KEY_EXIF_MODEL));
    861             mParameters.set(TICameraParameters::KEY_EXIF_MODEL, valstr);
    862             }
    863 
    864         if( (valstr = params.get(TICameraParameters::KEY_EXIF_MAKE)) != NULL )
    865             {
    866             CAMHAL_LOGDB("EXIF Make set %s", params.get(TICameraParameters::KEY_EXIF_MAKE));
    867             mParameters.set(TICameraParameters::KEY_EXIF_MAKE, valstr);
    868             }
    869 
    870         if( (valstr = params.get(TICameraParameters::KEY_EXP_BRACKETING_RANGE)) != NULL )
    871             {
    872             CAMHAL_LOGDB("Exposure Bracketing set %s", params.get(TICameraParameters::KEY_EXP_BRACKETING_RANGE));
    873             mParameters.set(TICameraParameters::KEY_EXP_BRACKETING_RANGE, valstr);
    874             }
    875         else
    876             {
    877             mParameters.remove(TICameraParameters::KEY_EXP_BRACKETING_RANGE);
    878             }
    879 
    880         if ((valstr = params.get(CameraParameters::KEY_ZOOM)) != NULL ) {
    881             if ((params.getInt(CameraParameters::KEY_ZOOM) >= 0 ) &&
    882                 (params.getInt(CameraParameters::KEY_ZOOM) <= mMaxZoomSupported )) {
    883                 CAMHAL_LOGDB("Zoom set %s", valstr);
    884                 doesSetParameterNeedUpdate(valstr,
    885                                            mParameters.get(CameraParameters::KEY_ZOOM),
    886                                            updateRequired);
    887                 mParameters.set(CameraParameters::KEY_ZOOM, valstr);
    888              } else {
    889                 CAMHAL_LOGEB("ERROR: Invalid Zoom: %s", valstr);
    890                 ret = -EINVAL;
    891             }
    892         }
    893 
    894         if( (valstr = params.get(CameraParameters::KEY_AUTO_EXPOSURE_LOCK)) != NULL )
    895           {
    896             CAMHAL_LOGDB("Auto Exposure Lock set %s", params.get(CameraParameters::KEY_AUTO_EXPOSURE_LOCK));
    897             doesSetParameterNeedUpdate(valstr,
    898                                        mParameters.get(CameraParameters::KEY_AUTO_EXPOSURE_LOCK),
    899                                        updateRequired);
    900             mParameters.set(CameraParameters::KEY_AUTO_EXPOSURE_LOCK, valstr);
    901           }
    902 
    903         if( (valstr = params.get(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK)) != NULL )
    904           {
    905             CAMHAL_LOGDB("Auto WhiteBalance Lock set %s", params.get(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK));
    906             doesSetParameterNeedUpdate(valstr,
    907                                        mParameters.get(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK),
    908                                        updateRequired);
    909             mParameters.set(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK, valstr);
    910           }
    911         if( (valstr = params.get(CameraParameters::KEY_METERING_AREAS)) != NULL )
    912             {
    913             CAMHAL_LOGEB("Metering areas position set %s", params.get(CameraParameters::KEY_METERING_AREAS));
    914             mParameters.set(CameraParameters::KEY_METERING_AREAS, valstr);
    915             }
    916 
    917         CameraParameters adapterParams = mParameters;
    918 
    919         // Only send parameters to adapter if preview is already
    920         // enabled or doesSetParameterNeedUpdate says so. Initial setParameters to camera adapter,
    921         // will be called in startPreview()
    922         // TODO(XXX): Need to identify other parameters that need update from camera adapter
    923         if ( (NULL != mCameraAdapter) && (mPreviewEnabled || updateRequired) ) {
    924             ret |= mCameraAdapter->setParameters(adapterParams);
    925         }
    926 
    927         if( NULL != params.get(TICameraParameters::KEY_TEMP_BRACKETING_RANGE_POS) )
    928             {
    929             int posBracketRange = params.getInt(TICameraParameters::KEY_TEMP_BRACKETING_RANGE_POS);
    930             if ( 0 < posBracketRange )
    931                 {
    932                 mBracketRangePositive = posBracketRange;
    933                 }
    934             }
    935         CAMHAL_LOGDB("Positive bracketing range %d", mBracketRangePositive);
    936 
    937 
    938         if( NULL != params.get(TICameraParameters::KEY_TEMP_BRACKETING_RANGE_NEG) )
    939             {
    940             int negBracketRange = params.getInt(TICameraParameters::KEY_TEMP_BRACKETING_RANGE_NEG);
    941             if ( 0 < negBracketRange )
    942                 {
    943                 mBracketRangeNegative = negBracketRange;
    944                 }
    945             }
    946         CAMHAL_LOGDB("Negative bracketing range %d", mBracketRangeNegative);
    947 
    948         if( ( (valstr = params.get(TICameraParameters::KEY_TEMP_BRACKETING)) != NULL) &&
    949             ( strcmp(valstr, TICameraParameters::BRACKET_ENABLE) == 0 ))
    950             {
    951             if ( !mBracketingEnabled )
    952                 {
    953                 CAMHAL_LOGDA("Enabling bracketing");
    954                 mBracketingEnabled = true;
    955 
    956                 //Wait for AF events to enable bracketing
    957                 if ( NULL != mCameraAdapter )
    958                     {
    959                     setEventProvider( CameraHalEvent::ALL_EVENTS, mCameraAdapter );
    960                     }
    961                 }
    962             else
    963                 {
    964                 CAMHAL_LOGDA("Bracketing already enabled");
    965                 }
    966             }
    967         else if ( ( (valstr = params.get(TICameraParameters::KEY_TEMP_BRACKETING)) != NULL ) &&
    968             ( strcmp(valstr, TICameraParameters::BRACKET_DISABLE) == 0 ))
    969             {
    970             CAMHAL_LOGDA("Disabling bracketing");
    971 
    972             mBracketingEnabled = false;
    973             stopImageBracketing();
    974 
    975             //Remove AF events subscription
    976             if ( NULL != mEventProvider )
    977                 {
    978                 mEventProvider->disableEventNotification( CameraHalEvent::ALL_EVENTS );
    979                 delete mEventProvider;
    980                 mEventProvider = NULL;
    981                 }
    982 
    983             }
    984 
    985         if( ( (valstr = params.get(TICameraParameters::KEY_SHUTTER_ENABLE)) != NULL ) &&
    986             ( strcmp(valstr, TICameraParameters::SHUTTER_ENABLE) == 0 ))
    987             {
    988             CAMHAL_LOGDA("Enabling shutter sound");
    989 
    990             mShutterEnabled = true;
    991             mMsgEnabled |= CAMERA_MSG_SHUTTER;
    992             mParameters.set(TICameraParameters::KEY_SHUTTER_ENABLE, valstr);
    993             }
    994         else if ( ( (valstr = params.get(TICameraParameters::KEY_SHUTTER_ENABLE)) != NULL ) &&
    995             ( strcmp(valstr, TICameraParameters::SHUTTER_DISABLE) == 0 ))
    996             {
    997             CAMHAL_LOGDA("Disabling shutter sound");
    998 
    999             mShutterEnabled = false;
   1000             mMsgEnabled &= ~CAMERA_MSG_SHUTTER;
   1001             mParameters.set(TICameraParameters::KEY_SHUTTER_ENABLE, valstr);
   1002             }
   1003 
   1004     }
   1005 
   1006     //On fail restore old parameters
   1007     if ( NO_ERROR != ret ) {
   1008         mParameters.unflatten(oldParams.flatten());
   1009     }
   1010 
   1011     if ( NULL != mAppCallbackNotifier.get() ) {
   1012         mAppCallbackNotifier->setParameters(mParameters);
   1013     }
   1014 
   1015     // Restart Preview if needed by KEY_RECODING_HINT only if preview is already running.
   1016     // If preview is not started yet, Video Mode parameters will take effect on next startPreview()
   1017     if (restartPreviewRequired && previewEnabled() && !mRecordingEnabled) {
   1018         CAMHAL_LOGDA("Restarting Preview");
   1019         ret = restartPreview();
   1020     } else if (restartPreviewRequired && !previewEnabled() &&
   1021                 mDisplayPaused && !mRecordingEnabled) {
   1022         CAMHAL_LOGDA("Stopping Preview");
   1023         forceStopPreview();
   1024     }
   1025 
   1026     if (ret != NO_ERROR)
   1027         {
   1028         CAMHAL_LOGEA("Failed to restart Preview");
   1029         return ret;
   1030         }
   1031 
   1032     LOG_FUNCTION_NAME_EXIT;
   1033 
   1034     return ret;
   1035 }
   1036 
   1037 status_t CameraHal::allocPreviewBufs(int width, int height, const char* previewFormat,
   1038                                         unsigned int buffercount, unsigned int &max_queueable)
   1039 {
   1040     status_t ret = NO_ERROR;
   1041 
   1042     LOG_FUNCTION_NAME;
   1043 
   1044     if(mDisplayAdapter.get() == NULL)
   1045     {
   1046         // Memory allocation of preview buffers is now placed in gralloc
   1047         // CameraHal should not allocate preview buffers without DisplayAdapter
   1048         return NO_MEMORY;
   1049     }
   1050 
   1051     if(!mPreviewBufs)
   1052     {
   1053         ///@todo Pluralise the name of this method to allocateBuffers
   1054         mPreviewLength = 0;
   1055         mPreviewBufs = (int32_t *) mDisplayAdapter->allocateBuffer(width, height,
   1056                                                                     previewFormat,
   1057                                                                     mPreviewLength,
   1058                                                                     buffercount);
   1059 
   1060 	if (NULL == mPreviewBufs ) {
   1061             CAMHAL_LOGEA("Couldn't allocate preview buffers");
   1062             return NO_MEMORY;
   1063          }
   1064 
   1065         mPreviewOffsets = (uint32_t *) mDisplayAdapter->getOffsets();
   1066         if ( NULL == mPreviewOffsets ) {
   1067             CAMHAL_LOGEA("Buffer mapping failed");
   1068             return BAD_VALUE;
   1069          }
   1070 
   1071         mPreviewFd = mDisplayAdapter->getFd();
   1072         if ( -1 == mPreviewFd ) {
   1073             CAMHAL_LOGEA("Invalid handle");
   1074             return BAD_VALUE;
   1075           }
   1076 
   1077         mBufProvider = (BufferProvider*) mDisplayAdapter.get();
   1078 
   1079         ret = mDisplayAdapter->maxQueueableBuffers(max_queueable);
   1080         if (ret != NO_ERROR) {
   1081             return ret;
   1082          }
   1083 
   1084     }
   1085 
   1086     LOG_FUNCTION_NAME_EXIT;
   1087 
   1088     return ret;
   1089 
   1090 }
   1091 
   1092 status_t CameraHal::freePreviewBufs()
   1093 {
   1094     status_t ret = NO_ERROR;
   1095     LOG_FUNCTION_NAME;
   1096 
   1097     CAMHAL_LOGDB("mPreviewBufs = 0x%x", (unsigned int)mPreviewBufs);
   1098     if(mPreviewBufs)
   1099         {
   1100         ///@todo Pluralise the name of this method to freeBuffers
   1101         ret = mBufProvider->freeBuffer(mPreviewBufs);
   1102         mPreviewBufs = NULL;
   1103         LOG_FUNCTION_NAME_EXIT;
   1104         return ret;
   1105         }
   1106     LOG_FUNCTION_NAME_EXIT;
   1107     return ret;
   1108 }
   1109 
   1110 
   1111 status_t CameraHal::allocPreviewDataBufs(size_t size, size_t bufferCount)
   1112 {
   1113     status_t ret = NO_ERROR;
   1114     int bytes;
   1115 
   1116     LOG_FUNCTION_NAME;
   1117 
   1118     bytes = size;
   1119 
   1120     if ( NO_ERROR == ret )
   1121         {
   1122         if( NULL != mPreviewDataBufs )
   1123             {
   1124             ret = freePreviewDataBufs();
   1125             }
   1126         }
   1127 
   1128     if ( NO_ERROR == ret )
   1129         {
   1130         bytes = ((bytes+4095)/4096)*4096;
   1131         mPreviewDataBufs = (int32_t *)mMemoryManager->allocateBuffer(0, 0, NULL, bytes, bufferCount);
   1132 
   1133         CAMHAL_LOGDB("Size of Preview data buffer = %d", bytes);
   1134         if( NULL == mPreviewDataBufs )
   1135             {
   1136             CAMHAL_LOGEA("Couldn't allocate image buffers using memory manager");
   1137             ret = -NO_MEMORY;
   1138             }
   1139         else
   1140             {
   1141             bytes = size;
   1142             }
   1143         }
   1144 
   1145     if ( NO_ERROR == ret )
   1146         {
   1147         mPreviewDataFd = mMemoryManager->getFd();
   1148         mPreviewDataLength = bytes;
   1149         mPreviewDataOffsets = mMemoryManager->getOffsets();
   1150         }
   1151     else
   1152         {
   1153         mPreviewDataFd = -1;
   1154         mPreviewDataLength = 0;
   1155         mPreviewDataOffsets = NULL;
   1156         }
   1157 
   1158     LOG_FUNCTION_NAME;
   1159 
   1160     return ret;
   1161 }
   1162 
   1163 status_t CameraHal::freePreviewDataBufs()
   1164 {
   1165     status_t ret = NO_ERROR;
   1166 
   1167     LOG_FUNCTION_NAME;
   1168 
   1169     if ( NO_ERROR == ret )
   1170         {
   1171 
   1172         if( NULL != mPreviewDataBufs )
   1173             {
   1174 
   1175             ///@todo Pluralise the name of this method to freeBuffers
   1176             ret = mMemoryManager->freeBuffer(mPreviewDataBufs);
   1177             mPreviewDataBufs = NULL;
   1178 
   1179             }
   1180         }
   1181 
   1182     LOG_FUNCTION_NAME_EXIT;
   1183 
   1184     return ret;
   1185 }
   1186 
   1187 status_t CameraHal::allocImageBufs(unsigned int width, unsigned int height, size_t size, const char* previewFormat, unsigned int bufferCount)
   1188 {
   1189     status_t ret = NO_ERROR;
   1190     int bytes;
   1191 
   1192     LOG_FUNCTION_NAME;
   1193 
   1194     bytes = size;
   1195 
   1196     // allocate image buffers only if not already allocated
   1197     if(NULL != mImageBufs) {
   1198         return NO_ERROR;
   1199     }
   1200 
   1201     if ( NO_ERROR == ret )
   1202         {
   1203         bytes = ((bytes+4095)/4096)*4096;
   1204         mImageBufs = (int32_t *)mMemoryManager->allocateBuffer(0, 0, previewFormat, bytes, bufferCount);
   1205 
   1206         CAMHAL_LOGDB("Size of Image cap buffer = %d", bytes);
   1207         if( NULL == mImageBufs )
   1208             {
   1209             CAMHAL_LOGEA("Couldn't allocate image buffers using memory manager");
   1210             ret = -NO_MEMORY;
   1211             }
   1212         else
   1213             {
   1214             bytes = size;
   1215             }
   1216         }
   1217 
   1218     if ( NO_ERROR == ret )
   1219         {
   1220         mImageFd = mMemoryManager->getFd();
   1221         mImageLength = bytes;
   1222         mImageOffsets = mMemoryManager->getOffsets();
   1223         }
   1224     else
   1225         {
   1226         mImageFd = -1;
   1227         mImageLength = 0;
   1228         mImageOffsets = NULL;
   1229         }
   1230 
   1231     LOG_FUNCTION_NAME;
   1232 
   1233     return ret;
   1234 }
   1235 
   1236 status_t CameraHal::allocVideoBufs(uint32_t width, uint32_t height, uint32_t bufferCount)
   1237 {
   1238   status_t ret = NO_ERROR;
   1239   LOG_FUNCTION_NAME;
   1240 
   1241   if( NULL != mVideoBufs ){
   1242     ret = freeVideoBufs(mVideoBufs);
   1243     mVideoBufs = NULL;
   1244   }
   1245 
   1246   if ( NO_ERROR == ret ){
   1247     int32_t stride;
   1248     buffer_handle_t *bufsArr = new buffer_handle_t [bufferCount];
   1249 
   1250     if (bufsArr != NULL){
   1251       for (int i = 0; i< bufferCount; i++){
   1252         GraphicBufferAllocator &GrallocAlloc = GraphicBufferAllocator::get();
   1253         buffer_handle_t buf;
   1254         ret = GrallocAlloc.alloc(width, height, HAL_PIXEL_FORMAT_NV12, CAMHAL_GRALLOC_USAGE, &buf, &stride);
   1255         if (ret != NO_ERROR){
   1256           CAMHAL_LOGEA("Couldn't allocate video buffers using Gralloc");
   1257           ret = -NO_MEMORY;
   1258           for (int j=0; j< i; j++){
   1259             buf = (buffer_handle_t)bufsArr[j];
   1260             CAMHAL_LOGEB("Freeing Gralloc Buffer 0x%x", buf);
   1261             GrallocAlloc.free(buf);
   1262           }
   1263           delete [] bufsArr;
   1264           goto exit;
   1265         }
   1266         bufsArr[i] = buf;
   1267         CAMHAL_LOGVB("*** Gralloc Handle =0x%x ***", buf);
   1268       }
   1269 
   1270       mVideoBufs = (int32_t *)bufsArr;
   1271     }
   1272     else{
   1273       CAMHAL_LOGEA("Couldn't allocate video buffers ");
   1274       ret = -NO_MEMORY;
   1275     }
   1276   }
   1277 
   1278  exit:
   1279   LOG_FUNCTION_NAME;
   1280 
   1281   return ret;
   1282 }
   1283 
   1284 void endImageCapture( void *userData)
   1285 {
   1286     LOG_FUNCTION_NAME;
   1287 
   1288     if ( NULL != userData )
   1289         {
   1290         CameraHal *c = reinterpret_cast<CameraHal *>(userData);
   1291         c->signalEndImageCapture();
   1292         }
   1293 
   1294     LOG_FUNCTION_NAME_EXIT;
   1295 }
   1296 
   1297 void releaseImageBuffers(void *userData)
   1298 {
   1299     LOG_FUNCTION_NAME;
   1300 
   1301     if (NULL != userData) {
   1302         CameraHal *c = reinterpret_cast<CameraHal *>(userData);
   1303         c->freeImageBufs();
   1304     }
   1305 
   1306     LOG_FUNCTION_NAME_EXIT;
   1307 }
   1308 
   1309 status_t CameraHal::signalEndImageCapture()
   1310 {
   1311     status_t ret = NO_ERROR;
   1312     int w,h;
   1313     CameraParameters adapterParams = mParameters;
   1314     Mutex::Autolock lock(mLock);
   1315 
   1316     LOG_FUNCTION_NAME;
   1317 
   1318     if ( mBracketingRunning ) {
   1319         stopImageBracketing();
   1320     } else {
   1321         mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_IMAGE_CAPTURE);
   1322     }
   1323 
   1324     LOG_FUNCTION_NAME_EXIT;
   1325 
   1326     return ret;
   1327 }
   1328 
   1329 status_t CameraHal::freeImageBufs()
   1330 {
   1331     status_t ret = NO_ERROR;
   1332 
   1333     LOG_FUNCTION_NAME;
   1334 
   1335     if ( NO_ERROR == ret )
   1336         {
   1337 
   1338         if( NULL != mImageBufs )
   1339             {
   1340 
   1341             ///@todo Pluralise the name of this method to freeBuffers
   1342             ret = mMemoryManager->freeBuffer(mImageBufs);
   1343             mImageBufs = NULL;
   1344 
   1345             }
   1346         else
   1347             {
   1348             ret = -EINVAL;
   1349             }
   1350 
   1351         }
   1352 
   1353     LOG_FUNCTION_NAME_EXIT;
   1354 
   1355     return ret;
   1356 }
   1357 
   1358 status_t CameraHal::freeVideoBufs(void *bufs)
   1359 {
   1360   status_t ret = NO_ERROR;
   1361 
   1362   LOG_FUNCTION_NAME;
   1363 
   1364   buffer_handle_t *pBuf = (buffer_handle_t*)bufs;
   1365   int count = atoi(mCameraProperties->get(CameraProperties::REQUIRED_PREVIEW_BUFS));
   1366   if(pBuf == NULL)
   1367     {
   1368       CAMHAL_LOGEA("NULL pointer passed to freeVideoBuffer");
   1369       LOG_FUNCTION_NAME_EXIT;
   1370       return BAD_VALUE;
   1371     }
   1372 
   1373   GraphicBufferAllocator &GrallocAlloc = GraphicBufferAllocator::get();
   1374 
   1375   for(int i = 0; i < count; i++){
   1376     buffer_handle_t ptr = *pBuf++;
   1377     CAMHAL_LOGVB("Free Video Gralloc Handle 0x%x", ptr);
   1378     GrallocAlloc.free(ptr);
   1379   }
   1380 
   1381   LOG_FUNCTION_NAME_EXIT;
   1382 
   1383   return ret;
   1384 }
   1385 
   1386 /**
   1387    @brief Start preview mode.
   1388 
   1389    @param none
   1390    @return NO_ERROR Camera switched to VF mode
   1391    @todo Update function header with the different errors that are possible
   1392 
   1393  */
   1394 status_t CameraHal::startPreview()
   1395 {
   1396 
   1397     status_t ret = NO_ERROR;
   1398     CameraAdapter::BuffersDescriptor desc;
   1399     CameraFrame frame;
   1400     const char *valstr = NULL;
   1401     unsigned int required_buffer_count;
   1402     unsigned int max_queueble_buffers;
   1403 
   1404 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
   1405         gettimeofday(&mStartPreview, NULL);
   1406 #endif
   1407 
   1408     LOG_FUNCTION_NAME;
   1409 
   1410     if ( mPreviewEnabled ){
   1411       CAMHAL_LOGDA("Preview already running");
   1412       LOG_FUNCTION_NAME_EXIT;
   1413       return ALREADY_EXISTS;
   1414     }
   1415 
   1416     if ( NULL != mCameraAdapter ) {
   1417       ret = mCameraAdapter->setParameters(mParameters);
   1418     }
   1419 
   1420     if ((mPreviewStartInProgress == false) && (mDisplayPaused == false)){
   1421       ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_QUERY_RESOLUTION_PREVIEW,( int ) &frame);
   1422       if ( NO_ERROR != ret ){
   1423         CAMHAL_LOGEB("Error: CAMERA_QUERY_RESOLUTION_PREVIEW %d", ret);
   1424         return ret;
   1425       }
   1426 
   1427       ///Update the current preview width and height
   1428       mPreviewWidth = frame.mWidth;
   1429       mPreviewHeight = frame.mHeight;
   1430       //Update the padded width and height - required for VNF and VSTAB
   1431       mParameters.set(TICameraParameters::KEY_PADDED_WIDTH, mPreviewWidth);
   1432       mParameters.set(TICameraParameters::KEY_PADDED_HEIGHT, mPreviewHeight);
   1433 
   1434     }
   1435 
   1436     ///If we don't have the preview callback enabled and display adapter,
   1437     if(!mSetPreviewWindowCalled || (mDisplayAdapter.get() == NULL)){
   1438       CAMHAL_LOGEA("Preview not started. Preview in progress flag set");
   1439       mPreviewStartInProgress = true;
   1440       ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_SWITCH_TO_EXECUTING);
   1441       if ( NO_ERROR != ret ){
   1442         CAMHAL_LOGEB("Error: CAMERA_SWITCH_TO_EXECUTING %d", ret);
   1443         return ret;
   1444       }
   1445       return NO_ERROR;
   1446     }
   1447 
   1448     if( (mDisplayAdapter.get() != NULL) && ( !mPreviewEnabled ) && ( mDisplayPaused ) )
   1449         {
   1450         CAMHAL_LOGDA("Preview is in paused state");
   1451 
   1452         mDisplayPaused = false;
   1453         mPreviewEnabled = true;
   1454         if ( NO_ERROR == ret )
   1455             {
   1456             ret = mDisplayAdapter->pauseDisplay(mDisplayPaused);
   1457 
   1458             if ( NO_ERROR != ret )
   1459                 {
   1460                 CAMHAL_LOGEB("Display adapter resume failed %x", ret);
   1461                 }
   1462             }
   1463         //restart preview callbacks
   1464         if(mMsgEnabled & CAMERA_MSG_PREVIEW_FRAME)
   1465         {
   1466             mAppCallbackNotifier->enableMsgType (CAMERA_MSG_PREVIEW_FRAME);
   1467         }
   1468         return ret;
   1469         }
   1470 
   1471 
   1472     required_buffer_count = atoi(mCameraProperties->get(CameraProperties::REQUIRED_PREVIEW_BUFS));
   1473 
   1474     ///Allocate the preview buffers
   1475     ret = allocPreviewBufs(mPreviewWidth, mPreviewHeight, mParameters.getPreviewFormat(), required_buffer_count, max_queueble_buffers);
   1476 
   1477     if ( NO_ERROR != ret )
   1478         {
   1479         CAMHAL_LOGEA("Couldn't allocate buffers for Preview");
   1480         goto error;
   1481         }
   1482 
   1483     if ( mMeasurementEnabled )
   1484         {
   1485 
   1486         ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_QUERY_BUFFER_SIZE_PREVIEW_DATA,
   1487                                           ( int ) &frame,
   1488                                           required_buffer_count);
   1489         if ( NO_ERROR != ret )
   1490             {
   1491             return ret;
   1492             }
   1493 
   1494          ///Allocate the preview data buffers
   1495         ret = allocPreviewDataBufs(frame.mLength, required_buffer_count);
   1496         if ( NO_ERROR != ret ) {
   1497             CAMHAL_LOGEA("Couldn't allocate preview data buffers");
   1498             goto error;
   1499            }
   1500 
   1501         if ( NO_ERROR == ret )
   1502             {
   1503             desc.mBuffers = mPreviewDataBufs;
   1504             desc.mOffsets = mPreviewDataOffsets;
   1505             desc.mFd = mPreviewDataFd;
   1506             desc.mLength = mPreviewDataLength;
   1507             desc.mCount = ( size_t ) required_buffer_count;
   1508             desc.mMaxQueueable = (size_t) required_buffer_count;
   1509 
   1510             mCameraAdapter->sendCommand(CameraAdapter::CAMERA_USE_BUFFERS_PREVIEW_DATA,
   1511                                         ( int ) &desc);
   1512             }
   1513 
   1514         }
   1515 
   1516     ///Pass the buffers to Camera Adapter
   1517     desc.mBuffers = mPreviewBufs;
   1518     desc.mOffsets = mPreviewOffsets;
   1519     desc.mFd = mPreviewFd;
   1520     desc.mLength = mPreviewLength;
   1521     desc.mCount = ( size_t ) required_buffer_count;
   1522     desc.mMaxQueueable = (size_t) max_queueble_buffers;
   1523 
   1524     ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_USE_BUFFERS_PREVIEW,
   1525                                       ( int ) &desc);
   1526 
   1527     if ( NO_ERROR != ret )
   1528         {
   1529         CAMHAL_LOGEB("Failed to register preview buffers: 0x%x", ret);
   1530         freePreviewBufs();
   1531         return ret;
   1532         }
   1533 
   1534     mAppCallbackNotifier->startPreviewCallbacks(mParameters, mPreviewBufs, mPreviewOffsets, mPreviewFd, mPreviewLength, required_buffer_count);
   1535 
   1536     ///Start the callback notifier
   1537     ret = mAppCallbackNotifier->start();
   1538 
   1539     if( ALREADY_EXISTS == ret )
   1540         {
   1541         //Already running, do nothing
   1542         CAMHAL_LOGDA("AppCallbackNotifier already running");
   1543         ret = NO_ERROR;
   1544         }
   1545     else if ( NO_ERROR == ret ) {
   1546         CAMHAL_LOGDA("Started AppCallbackNotifier..");
   1547         mAppCallbackNotifier->setMeasurements(mMeasurementEnabled);
   1548         }
   1549     else
   1550         {
   1551         CAMHAL_LOGDA("Couldn't start AppCallbackNotifier");
   1552         goto error;
   1553         }
   1554 
   1555     ///Enable the display adapter if present, actual overlay enable happens when we post the buffer
   1556     if(mDisplayAdapter.get() != NULL)
   1557         {
   1558         CAMHAL_LOGDA("Enabling display");
   1559         bool isS3d = false;
   1560         DisplayAdapter::S3DParameters s3dParams;
   1561         int width, height;
   1562         mParameters.getPreviewSize(&width, &height);
   1563 #if 0 //TODO: s3d is not part of bringup...will reenable
   1564         if ( (valstr = mParameters.get(TICameraParameters::KEY_S3D_SUPPORTED)) != NULL) {
   1565             isS3d = (strcmp(valstr, "true") == 0);
   1566         }
   1567         if ( (valstr = mParameters.get(TICameraParameters::KEY_S3D2D_PREVIEW)) != NULL) {
   1568             if (strcmp(valstr, "off") == 0)
   1569                 {
   1570                 CAMHAL_LOGEA("STEREO 3D->2D PREVIEW MODE IS OFF");
   1571                 //TODO: obtain the frame packing configuration from camera or user settings
   1572                 //once side by side configuration is supported
   1573                 s3dParams.mode = OVERLAY_S3D_MODE_ON;
   1574                 s3dParams.framePacking = OVERLAY_S3D_FORMAT_OVERUNDER;
   1575                 s3dParams.order = OVERLAY_S3D_ORDER_LF;
   1576                 s3dParams.subSampling = OVERLAY_S3D_SS_NONE;
   1577                 }
   1578             else
   1579                 {
   1580                 CAMHAL_LOGEA("STEREO 3D->2D PREVIEW MODE IS ON");
   1581                 s3dParams.mode = OVERLAY_S3D_MODE_OFF;
   1582                 s3dParams.framePacking = OVERLAY_S3D_FORMAT_OVERUNDER;
   1583                 s3dParams.order = OVERLAY_S3D_ORDER_LF;
   1584                 s3dParams.subSampling = OVERLAY_S3D_SS_NONE;
   1585                 }
   1586         }
   1587 #endif //if 0
   1588 
   1589 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
   1590 
   1591         ret = mDisplayAdapter->enableDisplay(width, height, &mStartPreview, isS3d ? &s3dParams : NULL);
   1592 
   1593 #else
   1594 
   1595         ret = mDisplayAdapter->enableDisplay(width, height, NULL, isS3d ? &s3dParams : NULL);
   1596 
   1597 #endif
   1598 
   1599         if ( ret != NO_ERROR )
   1600             {
   1601             CAMHAL_LOGEA("Couldn't enable display");
   1602             goto error;
   1603             }
   1604 
   1605         }
   1606 
   1607     ///Send START_PREVIEW command to adapter
   1608     CAMHAL_LOGDA("Starting CameraAdapter preview mode");
   1609 
   1610     ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_PREVIEW);
   1611 
   1612     if(ret!=NO_ERROR)
   1613         {
   1614         CAMHAL_LOGEA("Couldn't start preview w/ CameraAdapter");
   1615         goto error;
   1616         }
   1617     CAMHAL_LOGDA("Started preview");
   1618 
   1619     mPreviewEnabled = true;
   1620     mPreviewStartInProgress = false;
   1621     return ret;
   1622 
   1623     error:
   1624 
   1625         CAMHAL_LOGEA("Performing cleanup after error");
   1626 
   1627         //Do all the cleanup
   1628         freePreviewBufs();
   1629         mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_PREVIEW);
   1630         if(mDisplayAdapter.get() != NULL)
   1631             {
   1632             mDisplayAdapter->disableDisplay(false);
   1633             }
   1634         mAppCallbackNotifier->stop();
   1635         mPreviewStartInProgress = false;
   1636         mPreviewEnabled = false;
   1637         LOG_FUNCTION_NAME_EXIT;
   1638 
   1639         return ret;
   1640 }
   1641 
   1642 /**
   1643    @brief Sets ANativeWindow object.
   1644 
   1645    Preview buffers provided to CameraHal via this object. DisplayAdapter will be interfacing with it
   1646    to render buffers to display.
   1647 
   1648    @param[in] window The ANativeWindow object created by Surface flinger
   1649    @return NO_ERROR If the ANativeWindow object passes validation criteria
   1650    @todo Define validation criteria for ANativeWindow object. Define error codes for scenarios
   1651 
   1652  */
   1653 status_t CameraHal::setPreviewWindow(struct preview_stream_ops *window)
   1654 {
   1655     status_t ret = NO_ERROR;
   1656     CameraAdapter::BuffersDescriptor desc;
   1657 
   1658     LOG_FUNCTION_NAME;
   1659     mSetPreviewWindowCalled = true;
   1660 
   1661    ///If the Camera service passes a null window, we destroy existing window and free the DisplayAdapter
   1662     if(!window)
   1663     {
   1664         if(mDisplayAdapter.get() != NULL)
   1665         {
   1666             ///NULL window passed, destroy the display adapter if present
   1667             CAMHAL_LOGEA("NULL window passed, destroying display adapter");
   1668             mDisplayAdapter.clear();
   1669             ///@remarks If there was a window previously existing, we usually expect another valid window to be passed by the client
   1670             ///@remarks so, we will wait until it passes a valid window to begin the preview again
   1671             mSetPreviewWindowCalled = false;
   1672         }
   1673         CAMHAL_LOGEA("NULL ANativeWindow passed to setPreviewWindow");
   1674         return NO_ERROR;
   1675     }else if(mDisplayAdapter.get() == NULL)
   1676     {
   1677         // Need to create the display adapter since it has not been created
   1678         // Create display adapter
   1679         mDisplayAdapter = new ANativeWindowDisplayAdapter();
   1680         ret = NO_ERROR;
   1681         if(!mDisplayAdapter.get() || ((ret=mDisplayAdapter->initialize())!=NO_ERROR))
   1682         {
   1683             if(ret!=NO_ERROR)
   1684             {
   1685                 mDisplayAdapter.clear();
   1686                 CAMHAL_LOGEA("DisplayAdapter initialize failed");
   1687                 LOG_FUNCTION_NAME_EXIT;
   1688                 return ret;
   1689             }
   1690             else
   1691             {
   1692                 CAMHAL_LOGEA("Couldn't create DisplayAdapter");
   1693                 LOG_FUNCTION_NAME_EXIT;
   1694                 return NO_MEMORY;
   1695             }
   1696         }
   1697 
   1698         // DisplayAdapter needs to know where to get the CameraFrames from inorder to display
   1699         // Since CameraAdapter is the one that provides the frames, set it as the frame provider for DisplayAdapter
   1700         mDisplayAdapter->setFrameProvider(mCameraAdapter);
   1701 
   1702         // Any dynamic errors that happen during the camera use case has to be propagated back to the application
   1703         // via CAMERA_MSG_ERROR. AppCallbackNotifier is the class that  notifies such errors to the application
   1704         // Set it as the error handler for the DisplayAdapter
   1705         mDisplayAdapter->setErrorHandler(mAppCallbackNotifier.get());
   1706 
   1707         // Update the display adapter with the new window that is passed from CameraService
   1708         ret  = mDisplayAdapter->setPreviewWindow(window);
   1709         if(ret!=NO_ERROR)
   1710             {
   1711             CAMHAL_LOGEB("DisplayAdapter setPreviewWindow returned error %d", ret);
   1712             }
   1713 
   1714         if(mPreviewStartInProgress)
   1715         {
   1716             CAMHAL_LOGDA("setPreviewWindow called when preview running");
   1717             // Start the preview since the window is now available
   1718             ret = startPreview();
   1719         }
   1720     }else
   1721     {
   1722         /* If mDisplayAdpater is already created. No need to do anything.
   1723          * We get a surface handle directly now, so we can reconfigure surface
   1724          * itself in DisplayAdapter if dimensions have changed
   1725          */
   1726     }
   1727     LOG_FUNCTION_NAME_EXIT;
   1728 
   1729     return ret;
   1730 
   1731 }
   1732 
   1733 
   1734 /**
   1735    @brief Stop a previously started preview.
   1736 
   1737    @param none
   1738    @return none
   1739 
   1740  */
   1741 void CameraHal::stopPreview()
   1742 {
   1743     LOG_FUNCTION_NAME;
   1744 
   1745     if( (!previewEnabled() && !mDisplayPaused) || mRecordingEnabled)
   1746         {
   1747         LOG_FUNCTION_NAME_EXIT;
   1748         return;
   1749         }
   1750 
   1751     bool imageCaptureRunning = (mCameraAdapter->getState() == CameraAdapter::CAPTURE_STATE) &&
   1752                                     (mCameraAdapter->getNextState() != CameraAdapter::PREVIEW_STATE);
   1753     if(mDisplayPaused && !imageCaptureRunning)
   1754         {
   1755         // Display is paused, which essentially means there is no preview active.
   1756         // Note: this is done so that when stopPreview is called by client after
   1757         // an image capture, we do not de-initialize the camera adapter and
   1758         // restart over again.
   1759 
   1760         return;
   1761         }
   1762 
   1763     forceStopPreview();
   1764 
   1765     // Reset Capture-Mode to default, so that when we switch from VideoRecording
   1766     // to ImageCapture, CAPTURE_MODE is not left to VIDEO_MODE.
   1767     CAMHAL_LOGDA("Resetting Capture-Mode to default");
   1768     mParameters.set(TICameraParameters::KEY_CAP_MODE, "");
   1769 
   1770     LOG_FUNCTION_NAME_EXIT;
   1771 }
   1772 
   1773 /**
   1774    @brief Returns true if preview is enabled
   1775 
   1776    @param none
   1777    @return true If preview is running currently
   1778          false If preview has been stopped
   1779 
   1780  */
   1781 bool CameraHal::previewEnabled()
   1782 {
   1783     LOG_FUNCTION_NAME;
   1784 
   1785     return (mPreviewEnabled || mPreviewStartInProgress);
   1786 }
   1787 
   1788 /**
   1789    @brief Start record mode.
   1790 
   1791   When a record image is available a CAMERA_MSG_VIDEO_FRAME message is sent with
   1792   the corresponding frame. Every record frame must be released by calling
   1793   releaseRecordingFrame().
   1794 
   1795    @param none
   1796    @return NO_ERROR If recording could be started without any issues
   1797    @todo Update the header with possible error values in failure scenarios
   1798 
   1799  */
   1800 status_t CameraHal::startRecording( )
   1801 {
   1802     int w, h;
   1803     const char *valstr = NULL;
   1804     bool restartPreviewRequired = false;
   1805     status_t ret = NO_ERROR;
   1806 
   1807     LOG_FUNCTION_NAME;
   1808 
   1809 
   1810 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
   1811 
   1812             gettimeofday(&mStartPreview, NULL);
   1813 
   1814 #endif
   1815 
   1816     if(!previewEnabled())
   1817         {
   1818         return NO_INIT;
   1819         }
   1820 
   1821     // set internal recording hint in case camera adapter needs to make some
   1822     // decisions....(will only be sent to camera adapter if camera restart is required)
   1823     mParameters.set(TICameraParameters::KEY_RECORDING_HINT, CameraParameters::TRUE);
   1824 
   1825     // if application starts recording in continuous focus picture mode...
   1826     // then we need to force default capture mode (as opposed to video mode)
   1827     if ( ((valstr = mParameters.get(CameraParameters::KEY_FOCUS_MODE)) != NULL) &&
   1828          (strcmp(valstr, CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE) == 0) ){
   1829         restartPreviewRequired = resetVideoModeParameters();
   1830     }
   1831 
   1832     // only need to check recording hint if preview restart is not already needed
   1833     valstr = mParameters.get(CameraParameters::KEY_RECORDING_HINT);
   1834     if ( !restartPreviewRequired &&
   1835          (!valstr || (valstr && (strcmp(valstr, CameraParameters::TRUE) != 0))) ) {
   1836         restartPreviewRequired = setVideoModeParameters(mParameters);
   1837     }
   1838 
   1839     if (restartPreviewRequired) {
   1840         ret = restartPreview();
   1841     }
   1842 
   1843     if ( NO_ERROR == ret )
   1844       {
   1845         int count = atoi(mCameraProperties->get(CameraProperties::REQUIRED_PREVIEW_BUFS));
   1846         mParameters.getPreviewSize(&w, &h);
   1847         CAMHAL_LOGDB("%s Video Width=%d Height=%d", __FUNCTION__, mVideoWidth, mVideoHeight);
   1848 
   1849         if ((w != mVideoWidth) && (h != mVideoHeight))
   1850           {
   1851             ret = allocVideoBufs(mVideoWidth, mVideoHeight, count);
   1852             if ( NO_ERROR != ret )
   1853               {
   1854                 CAMHAL_LOGEB("allocImageBufs returned error 0x%x", ret);
   1855                 mParameters.remove(TICameraParameters::KEY_RECORDING_HINT);
   1856                 return ret;
   1857               }
   1858 
   1859             mAppCallbackNotifier->useVideoBuffers(true);
   1860             mAppCallbackNotifier->setVideoRes(mVideoWidth, mVideoHeight);
   1861             ret = mAppCallbackNotifier->initSharedVideoBuffers(mPreviewBufs, mPreviewOffsets, mPreviewFd, mPreviewLength, count, mVideoBufs);
   1862           }
   1863         else
   1864           {
   1865             mAppCallbackNotifier->useVideoBuffers(false);
   1866             mAppCallbackNotifier->setVideoRes(mPreviewWidth, mPreviewHeight);
   1867             ret = mAppCallbackNotifier->initSharedVideoBuffers(mPreviewBufs, mPreviewOffsets, mPreviewFd, mPreviewLength, count, NULL);
   1868           }
   1869       }
   1870 
   1871     if ( NO_ERROR == ret )
   1872         {
   1873          ret = mAppCallbackNotifier->startRecording();
   1874         }
   1875 
   1876     if ( NO_ERROR == ret )
   1877         {
   1878         ///Buffers for video capture (if different from preview) are expected to be allocated within CameraAdapter
   1879          ret =  mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_VIDEO);
   1880         }
   1881 
   1882     if ( NO_ERROR == ret )
   1883         {
   1884         mRecordingEnabled = true;
   1885         }
   1886 
   1887     LOG_FUNCTION_NAME_EXIT;
   1888 
   1889     return ret;
   1890 }
   1891 
   1892 /**
   1893    @brief Set the camera parameters specific to Video Recording.
   1894 
   1895    This function checks for the camera parameters which have to be set for recording.
   1896    Video Recording needs CAPTURE_MODE to be VIDEO_MODE. This function sets it.
   1897    This function also enables Video Recording specific functions like VSTAB & VNF.
   1898 
   1899    @param none
   1900    @return true if preview needs to be restarted for VIDEO_MODE parameters to take effect.
   1901    @todo Modify the policies for enabling VSTAB & VNF usecase based later.
   1902 
   1903  */
   1904 bool CameraHal::setVideoModeParameters(const CameraParameters& params)
   1905 {
   1906     const char *valstr = NULL;
   1907     bool restartPreviewRequired = false;
   1908     status_t ret = NO_ERROR;
   1909 
   1910     LOG_FUNCTION_NAME;
   1911 
   1912     // Set CAPTURE_MODE to VIDEO_MODE, if not set already and Restart Preview
   1913     valstr = mParameters.get(TICameraParameters::KEY_CAP_MODE);
   1914     if ( (valstr == NULL) ||
   1915         ( (valstr != NULL) && (strcmp(valstr, (const char *) TICameraParameters::VIDEO_MODE) != 0) ) )
   1916         {
   1917         CAMHAL_LOGDA("Set CAPTURE_MODE to VIDEO_MODE");
   1918         mParameters.set(TICameraParameters::KEY_CAP_MODE, (const char *) TICameraParameters::VIDEO_MODE);
   1919         restartPreviewRequired = true;
   1920         }
   1921 
   1922     // Check if CAPTURE_MODE is VIDEO_MODE, since VSTAB & VNF work only in VIDEO_MODE.
   1923     valstr = mParameters.get(TICameraParameters::KEY_CAP_MODE);
   1924     if (strcmp(valstr, (const char *) TICameraParameters::VIDEO_MODE) == 0) {
   1925        // set VSTAB. restart is required if vstab value has changed
   1926        if (params.get(CameraParameters::KEY_VIDEO_STABILIZATION) != NULL) {
   1927             // make sure we support vstab
   1928             if (strcmp(mCameraProperties->get(CameraProperties::VSTAB_SUPPORTED),
   1929                        CameraParameters::TRUE) == 0) {
   1930                 valstr = mParameters.get(CameraParameters::KEY_VIDEO_STABILIZATION);
   1931                 // vstab value has changed
   1932                 if ((valstr != NULL) &&
   1933                      strcmp(valstr, params.get(CameraParameters::KEY_VIDEO_STABILIZATION)) != 0) {
   1934                     restartPreviewRequired = true;
   1935                 }
   1936                 mParameters.set(CameraParameters::KEY_VIDEO_STABILIZATION,
   1937                                 params.get(CameraParameters::KEY_VIDEO_STABILIZATION));
   1938             }
   1939         } else if (mParameters.get(CameraParameters::KEY_VIDEO_STABILIZATION)) {
   1940             // vstab was configured but now unset
   1941             restartPreviewRequired = true;
   1942             mParameters.remove(CameraParameters::KEY_VIDEO_STABILIZATION);
   1943         }
   1944 
   1945         // Set VNF
   1946         if (params.get(TICameraParameters::KEY_VNF) == NULL) {
   1947             CAMHAL_LOGDA("Enable VNF");
   1948             mParameters.set(TICameraParameters::KEY_VNF, "1");
   1949             restartPreviewRequired = true;
   1950         } else {
   1951             valstr = mParameters.get(TICameraParameters::KEY_VNF);
   1952             if (valstr && strcmp(valstr, params.get(TICameraParameters::KEY_VNF)) != 0) {
   1953                 restartPreviewRequired = true;
   1954             }
   1955             mParameters.set(TICameraParameters::KEY_VNF, params.get(TICameraParameters::KEY_VNF));
   1956         }
   1957 
   1958         // For VSTAB alone for 1080p resolution, padded width goes > 2048, which cannot be rendered by GPU.
   1959         // In such case, there is support in Ducati for combination of VSTAB & VNF requiring padded width < 2048.
   1960         // So we are forcefully enabling VNF, if VSTAB is enabled for 1080p resolution.
   1961         valstr = mParameters.get(CameraParameters::KEY_VIDEO_STABILIZATION);
   1962         if (valstr && (strcmp(valstr, CameraParameters::TRUE) == 0) && (mPreviewWidth == 1920)) {
   1963             CAMHAL_LOGDA("Force Enable VNF for 1080p");
   1964             mParameters.set(TICameraParameters::KEY_VNF, "1");
   1965             restartPreviewRequired = true;
   1966         }
   1967     }
   1968     LOG_FUNCTION_NAME_EXIT;
   1969 
   1970     return restartPreviewRequired;
   1971 }
   1972 
   1973 /**
   1974    @brief Reset the camera parameters specific to Video Recording.
   1975 
   1976    This function resets CAPTURE_MODE and disables Recording specific functions like VSTAB & VNF.
   1977 
   1978    @param none
   1979    @return true if preview needs to be restarted for VIDEO_MODE parameters to take effect.
   1980 
   1981  */
   1982 bool CameraHal::resetVideoModeParameters()
   1983 {
   1984     const char *valstr = NULL;
   1985     bool restartPreviewRequired = false;
   1986     status_t ret = NO_ERROR;
   1987 
   1988     LOG_FUNCTION_NAME;
   1989 
   1990     // ignore this if we are already recording
   1991     if (mRecordingEnabled) {
   1992         return false;
   1993     }
   1994 
   1995     // Set CAPTURE_MODE to VIDEO_MODE, if not set already and Restart Preview
   1996     valstr = mParameters.get(TICameraParameters::KEY_CAP_MODE);
   1997     if ((valstr != NULL) && (strcmp(valstr, TICameraParameters::VIDEO_MODE) == 0)) {
   1998         CAMHAL_LOGDA("Reset Capture-Mode to default");
   1999         mParameters.set(TICameraParameters::KEY_CAP_MODE, "");
   2000         restartPreviewRequired = true;
   2001     }
   2002 
   2003     LOG_FUNCTION_NAME_EXIT;
   2004 
   2005     return restartPreviewRequired;
   2006 }
   2007 
   2008 /**
   2009    @brief Restart the preview with setParameter.
   2010 
   2011    This function restarts preview, for some VIDEO_MODE parameters to take effect.
   2012 
   2013    @param none
   2014    @return NO_ERROR If recording parameters could be set without any issues
   2015 
   2016  */
   2017 status_t CameraHal::restartPreview()
   2018 {
   2019     const char *valstr = NULL;
   2020     char tmpvalstr[30];
   2021     status_t ret = NO_ERROR;
   2022 
   2023     LOG_FUNCTION_NAME;
   2024 
   2025     // Retain CAPTURE_MODE before calling stopPreview(), since it is reset in stopPreview().
   2026     tmpvalstr[0] = 0;
   2027     valstr = mParameters.get(TICameraParameters::KEY_CAP_MODE);
   2028     if(valstr != NULL)
   2029         {
   2030         if(sizeof(tmpvalstr) < (strlen(valstr)+1))
   2031             {
   2032             return -EINVAL;
   2033             }
   2034 
   2035         strncpy(tmpvalstr, valstr, sizeof(tmpvalstr));
   2036         tmpvalstr[sizeof(tmpvalstr)-1] = 0;
   2037         }
   2038 
   2039     forceStopPreview();
   2040 
   2041     {
   2042         Mutex::Autolock lock(mLock);
   2043         mParameters.set(TICameraParameters::KEY_CAP_MODE, tmpvalstr);
   2044         mCameraAdapter->setParameters(mParameters);
   2045     }
   2046 
   2047     ret = startPreview();
   2048 
   2049     LOG_FUNCTION_NAME_EXIT;
   2050 
   2051     return ret;
   2052 }
   2053 
   2054 /**
   2055    @brief Stop a previously started recording.
   2056 
   2057    @param none
   2058    @return none
   2059 
   2060  */
   2061 void CameraHal::stopRecording()
   2062 {
   2063     CameraAdapter::AdapterState currentState;
   2064 
   2065     LOG_FUNCTION_NAME;
   2066 
   2067     Mutex::Autolock lock(mLock);
   2068 
   2069     if (!mRecordingEnabled )
   2070         {
   2071         return;
   2072         }
   2073 
   2074     currentState = mCameraAdapter->getState();
   2075     if (currentState == CameraAdapter::VIDEO_CAPTURE_STATE) {
   2076         mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_IMAGE_CAPTURE);
   2077     }
   2078 
   2079     mAppCallbackNotifier->stopRecording();
   2080 
   2081     mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_VIDEO);
   2082 
   2083     mRecordingEnabled = false;
   2084 
   2085     if ( mAppCallbackNotifier->getUesVideoBuffers() ){
   2086       freeVideoBufs(mVideoBufs);
   2087       if (mVideoBufs){
   2088         CAMHAL_LOGVB(" FREEING mVideoBufs 0x%x", mVideoBufs);
   2089         delete [] mVideoBufs;
   2090       }
   2091       mVideoBufs = NULL;
   2092     }
   2093 
   2094     // reset internal recording hint in case camera adapter needs to make some
   2095     // decisions....(will only be sent to camera adapter if camera restart is required)
   2096     mParameters.remove(TICameraParameters::KEY_RECORDING_HINT);
   2097 
   2098     LOG_FUNCTION_NAME_EXIT;
   2099 }
   2100 
   2101 /**
   2102    @brief Returns true if recording is enabled.
   2103 
   2104    @param none
   2105    @return true If recording is currently running
   2106          false If recording has been stopped
   2107 
   2108  */
   2109 int CameraHal::recordingEnabled()
   2110 {
   2111     LOG_FUNCTION_NAME;
   2112 
   2113     LOG_FUNCTION_NAME_EXIT;
   2114 
   2115     return mRecordingEnabled;
   2116 }
   2117 
   2118 /**
   2119    @brief Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
   2120 
   2121    @param[in] mem MemoryBase pointer to the frame being released. Must be one of the buffers
   2122                previously given by CameraHal
   2123    @return none
   2124 
   2125  */
   2126 void CameraHal::releaseRecordingFrame(const void* mem)
   2127 {
   2128     LOG_FUNCTION_NAME;
   2129 
   2130     //CAMHAL_LOGDB(" 0x%x", mem->pointer());
   2131 
   2132     if ( ( mRecordingEnabled ) && mem != NULL)
   2133     {
   2134         mAppCallbackNotifier->releaseRecordingFrame(mem);
   2135     }
   2136 
   2137     LOG_FUNCTION_NAME_EXIT;
   2138 
   2139     return;
   2140 }
   2141 
   2142 /**
   2143    @brief Start auto focus
   2144 
   2145    This call asynchronous.
   2146    The notification callback routine is called with CAMERA_MSG_FOCUS once when
   2147    focusing is complete. autoFocus() will be called again if another auto focus is
   2148    needed.
   2149 
   2150    @param none
   2151    @return NO_ERROR
   2152    @todo Define the error codes if the focus is not locked
   2153 
   2154  */
   2155 status_t CameraHal::autoFocus()
   2156 {
   2157     status_t ret = NO_ERROR;
   2158 
   2159 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
   2160 
   2161     gettimeofday(&mStartFocus, NULL);
   2162 
   2163 #endif
   2164 
   2165 
   2166     LOG_FUNCTION_NAME;
   2167 
   2168     {
   2169     Mutex::Autolock lock(mLock);
   2170     mMsgEnabled |= CAMERA_MSG_FOCUS;
   2171     }
   2172 
   2173 
   2174     if ( NULL != mCameraAdapter )
   2175         {
   2176 
   2177 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
   2178 
   2179     //pass the autoFocus timestamp along with the command to camera adapter
   2180     ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_PERFORM_AUTOFOCUS, ( int ) &mStartFocus);
   2181 
   2182 #else
   2183 
   2184     ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_PERFORM_AUTOFOCUS);
   2185 
   2186 #endif
   2187 
   2188         }
   2189     else
   2190         {
   2191             ret = -1;
   2192         }
   2193 
   2194     LOG_FUNCTION_NAME_EXIT;
   2195 
   2196     return ret;
   2197 }
   2198 
   2199 /**
   2200    @brief Cancels auto-focus function.
   2201 
   2202    If the auto-focus is still in progress, this function will cancel it.
   2203    Whether the auto-focus is in progress or not, this function will return the
   2204    focus position to the default. If the camera does not support auto-focus, this is a no-op.
   2205 
   2206 
   2207    @param none
   2208    @return NO_ERROR If the cancel succeeded
   2209    @todo Define error codes if cancel didnt succeed
   2210 
   2211  */
   2212 status_t CameraHal::cancelAutoFocus()
   2213 {
   2214     LOG_FUNCTION_NAME;
   2215 
   2216     Mutex::Autolock lock(mLock);
   2217     CameraParameters adapterParams = mParameters;
   2218     mMsgEnabled &= ~CAMERA_MSG_FOCUS;
   2219 
   2220     if( NULL != mCameraAdapter )
   2221     {
   2222         adapterParams.set(TICameraParameters::KEY_AUTO_FOCUS_LOCK, CameraParameters::FALSE);
   2223         mCameraAdapter->setParameters(adapterParams);
   2224         mCameraAdapter->sendCommand(CameraAdapter::CAMERA_CANCEL_AUTOFOCUS);
   2225     }
   2226 
   2227     LOG_FUNCTION_NAME_EXIT;
   2228     return NO_ERROR;
   2229 }
   2230 
   2231 void CameraHal::setEventProvider(int32_t eventMask, MessageNotifier * eventNotifier)
   2232 {
   2233 
   2234     LOG_FUNCTION_NAME;
   2235 
   2236     if ( NULL != mEventProvider )
   2237         {
   2238         mEventProvider->disableEventNotification(CameraHalEvent::ALL_EVENTS);
   2239         delete mEventProvider;
   2240         mEventProvider = NULL;
   2241         }
   2242 
   2243     mEventProvider = new EventProvider(eventNotifier, this, eventCallbackRelay);
   2244     if ( NULL == mEventProvider )
   2245         {
   2246         CAMHAL_LOGEA("Error in creating EventProvider");
   2247         }
   2248     else
   2249         {
   2250         mEventProvider->enableEventNotification(eventMask);
   2251         }
   2252 
   2253     LOG_FUNCTION_NAME_EXIT;
   2254 }
   2255 
   2256 void CameraHal::eventCallbackRelay(CameraHalEvent* event)
   2257 {
   2258     LOG_FUNCTION_NAME;
   2259 
   2260     CameraHal *appcbn = ( CameraHal * ) (event->mCookie);
   2261     appcbn->eventCallback(event );
   2262 
   2263     LOG_FUNCTION_NAME_EXIT;
   2264 }
   2265 
   2266 void CameraHal::eventCallback(CameraHalEvent* event)
   2267 {
   2268     LOG_FUNCTION_NAME;
   2269 
   2270     if ( NULL != event )
   2271         {
   2272         switch( event->mEventType )
   2273             {
   2274             case CameraHalEvent::EVENT_FOCUS_LOCKED:
   2275             case CameraHalEvent::EVENT_FOCUS_ERROR:
   2276                 {
   2277                 if ( mBracketingEnabled )
   2278                     {
   2279                     startImageBracketing();
   2280                     }
   2281                 break;
   2282                 }
   2283             default:
   2284                 {
   2285                 break;
   2286                 }
   2287             };
   2288         }
   2289 
   2290     LOG_FUNCTION_NAME_EXIT;
   2291 }
   2292 
   2293 status_t CameraHal::startImageBracketing()
   2294 {
   2295         status_t ret = NO_ERROR;
   2296         CameraFrame frame;
   2297         CameraAdapter::BuffersDescriptor desc;
   2298 
   2299 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
   2300 
   2301         gettimeofday(&mStartCapture, NULL);
   2302 
   2303 #endif
   2304 
   2305         LOG_FUNCTION_NAME;
   2306 
   2307         if(!previewEnabled() && !mDisplayPaused)
   2308             {
   2309             LOG_FUNCTION_NAME_EXIT;
   2310             return NO_INIT;
   2311             }
   2312 
   2313         if ( !mBracketingEnabled )
   2314             {
   2315             return ret;
   2316             }
   2317 
   2318         if ( NO_ERROR == ret )
   2319             {
   2320             mBracketingRunning = true;
   2321             }
   2322 
   2323         if (  (NO_ERROR == ret) && ( NULL != mCameraAdapter ) )
   2324             {
   2325             ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE,
   2326                                               ( int ) &frame,
   2327                                               ( mBracketRangeNegative + 1 ));
   2328 
   2329             if ( NO_ERROR != ret )
   2330                 {
   2331                 CAMHAL_LOGEB("CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE returned error 0x%x", ret);
   2332                 }
   2333             }
   2334 
   2335         if ( NO_ERROR == ret )
   2336             {
   2337             if ( NULL != mAppCallbackNotifier.get() )
   2338                  {
   2339                  mAppCallbackNotifier->setBurst(true);
   2340                  }
   2341             }
   2342 
   2343         if ( NO_ERROR == ret )
   2344             {
   2345             mParameters.getPictureSize(( int * ) &frame.mWidth,
   2346                                        ( int * ) &frame.mHeight);
   2347 
   2348             ret = allocImageBufs(frame.mWidth,
   2349                                  frame.mHeight,
   2350                                  frame.mLength,
   2351                                  mParameters.getPictureFormat(),
   2352                                  ( mBracketRangeNegative + 1 ));
   2353             if ( NO_ERROR != ret )
   2354               {
   2355                 CAMHAL_LOGEB("allocImageBufs returned error 0x%x", ret);
   2356               }
   2357             }
   2358 
   2359         if (  (NO_ERROR == ret) && ( NULL != mCameraAdapter ) )
   2360             {
   2361 
   2362             desc.mBuffers = mImageBufs;
   2363             desc.mOffsets = mImageOffsets;
   2364             desc.mFd = mImageFd;
   2365             desc.mLength = mImageLength;
   2366             desc.mCount = ( size_t ) ( mBracketRangeNegative + 1 );
   2367             desc.mMaxQueueable = ( size_t ) ( mBracketRangeNegative + 1 );
   2368 
   2369             ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_USE_BUFFERS_IMAGE_CAPTURE,
   2370                                               ( int ) &desc);
   2371 
   2372             if ( NO_ERROR == ret )
   2373                 {
   2374 
   2375 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
   2376 
   2377                  //pass capture timestamp along with the camera adapter command
   2378                 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_BRACKET_CAPTURE,  ( mBracketRangePositive + 1 ),  (int) &mStartCapture);
   2379 
   2380 #else
   2381 
   2382                 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_BRACKET_CAPTURE, ( mBracketRangePositive + 1 ));
   2383 
   2384 #endif
   2385 
   2386                 }
   2387             }
   2388 
   2389         return ret;
   2390 }
   2391 
   2392 status_t CameraHal::stopImageBracketing()
   2393 {
   2394         status_t ret = NO_ERROR;
   2395 
   2396         LOG_FUNCTION_NAME;
   2397 
   2398         if( !previewEnabled() )
   2399             {
   2400             return NO_INIT;
   2401             }
   2402 
   2403         mBracketingRunning = false;
   2404 
   2405         ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_BRACKET_CAPTURE);
   2406 
   2407         LOG_FUNCTION_NAME_EXIT;
   2408 
   2409         return ret;
   2410 }
   2411 
   2412 /**
   2413    @brief Take a picture.
   2414 
   2415    @param none
   2416    @return NO_ERROR If able to switch to image capture
   2417    @todo Define error codes if unable to switch to image capture
   2418 
   2419  */
   2420 status_t CameraHal::takePicture( )
   2421 {
   2422     status_t ret = NO_ERROR;
   2423     CameraFrame frame;
   2424     CameraAdapter::BuffersDescriptor desc;
   2425     int burst;
   2426     const char *valstr = NULL;
   2427     unsigned int bufferCount = 1;
   2428 
   2429     Mutex::Autolock lock(mLock);
   2430 
   2431 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
   2432 
   2433     gettimeofday(&mStartCapture, NULL);
   2434 
   2435 #endif
   2436 
   2437     LOG_FUNCTION_NAME;
   2438 
   2439     if(!previewEnabled() && !mDisplayPaused)
   2440         {
   2441         LOG_FUNCTION_NAME_EXIT;
   2442         CAMHAL_LOGEA("Preview not started...");
   2443         return NO_INIT;
   2444         }
   2445 
   2446     // return error if we are already capturing
   2447     if ( (mCameraAdapter->getState() == CameraAdapter::CAPTURE_STATE &&
   2448           mCameraAdapter->getNextState() != CameraAdapter::PREVIEW_STATE) ||
   2449          (mCameraAdapter->getState() == CameraAdapter::VIDEO_CAPTURE_STATE &&
   2450           mCameraAdapter->getNextState() != CameraAdapter::VIDEO_STATE) ) {
   2451         CAMHAL_LOGEA("Already capturing an image...");
   2452         return NO_INIT;
   2453     }
   2454 
   2455     // we only support video snapshot if we are in video mode (recording hint is set)
   2456     valstr = mParameters.get(TICameraParameters::KEY_CAP_MODE);
   2457     if ( (mCameraAdapter->getState() == CameraAdapter::VIDEO_STATE) &&
   2458          (valstr && strcmp(valstr, TICameraParameters::VIDEO_MODE)) ) {
   2459         CAMHAL_LOGEA("Trying to capture while recording without recording hint set...");
   2460         return INVALID_OPERATION;
   2461     }
   2462 
   2463     if ( !mBracketingRunning )
   2464         {
   2465 
   2466          if ( NO_ERROR == ret )
   2467             {
   2468             burst = mParameters.getInt(TICameraParameters::KEY_BURST);
   2469             }
   2470 
   2471          //Allocate all buffers only in burst capture case
   2472          if ( burst > 1 )
   2473              {
   2474              bufferCount = CameraHal::NO_BUFFERS_IMAGE_CAPTURE;
   2475              if ( NULL != mAppCallbackNotifier.get() )
   2476                  {
   2477                  mAppCallbackNotifier->setBurst(true);
   2478                  }
   2479              }
   2480          else
   2481              {
   2482              if ( NULL != mAppCallbackNotifier.get() )
   2483                  {
   2484                  mAppCallbackNotifier->setBurst(false);
   2485                  }
   2486              }
   2487 
   2488         // pause preview during normal image capture
   2489         // do not pause preview if recording (video state)
   2490         if (NO_ERROR == ret &&
   2491                 NULL != mDisplayAdapter.get() &&
   2492                 burst < 1) {
   2493             if (mCameraAdapter->getState() != CameraAdapter::VIDEO_STATE) {
   2494                 mDisplayPaused = true;
   2495                 mPreviewEnabled = false;
   2496                 ret = mDisplayAdapter->pauseDisplay(mDisplayPaused);
   2497                 // since preview is paused we should stop sending preview frames too
   2498                 if(mMsgEnabled & CAMERA_MSG_PREVIEW_FRAME) {
   2499                     mAppCallbackNotifier->disableMsgType (CAMERA_MSG_PREVIEW_FRAME);
   2500                 }
   2501             }
   2502 
   2503 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
   2504             mDisplayAdapter->setSnapshotTimeRef(&mStartCapture);
   2505 #endif
   2506         }
   2507 
   2508         // if we taking video snapshot...
   2509         if ((NO_ERROR == ret) && (mCameraAdapter->getState() == CameraAdapter::VIDEO_STATE)) {
   2510             // enable post view frames if not already enabled so we can internally
   2511             // save snapshot frames for generating thumbnail
   2512             if((mMsgEnabled & CAMERA_MSG_POSTVIEW_FRAME) == 0) {
   2513                 mAppCallbackNotifier->enableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
   2514             }
   2515         }
   2516 
   2517         if ( (NO_ERROR == ret) && (NULL != mCameraAdapter) )
   2518             {
   2519             if ( NO_ERROR == ret )
   2520                 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE,
   2521                                                   ( int ) &frame,
   2522                                                   bufferCount);
   2523 
   2524             if ( NO_ERROR != ret )
   2525                 {
   2526                 CAMHAL_LOGEB("CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE returned error 0x%x", ret);
   2527                 }
   2528             }
   2529 
   2530         if ( NO_ERROR == ret )
   2531             {
   2532             mParameters.getPictureSize(( int * ) &frame.mWidth,
   2533                                        ( int * ) &frame.mHeight);
   2534 
   2535             ret = allocImageBufs(frame.mWidth,
   2536                                  frame.mHeight,
   2537                                  frame.mLength,
   2538                                  mParameters.getPictureFormat(),
   2539                                  bufferCount);
   2540             if ( NO_ERROR != ret )
   2541                 {
   2542                 CAMHAL_LOGEB("allocImageBufs returned error 0x%x", ret);
   2543                 }
   2544             }
   2545 
   2546         if (  (NO_ERROR == ret) && ( NULL != mCameraAdapter ) )
   2547             {
   2548             desc.mBuffers = mImageBufs;
   2549             desc.mOffsets = mImageOffsets;
   2550             desc.mFd = mImageFd;
   2551             desc.mLength = mImageLength;
   2552             desc.mCount = ( size_t ) bufferCount;
   2553             desc.mMaxQueueable = ( size_t ) bufferCount;
   2554 
   2555             ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_USE_BUFFERS_IMAGE_CAPTURE,
   2556                                               ( int ) &desc);
   2557             }
   2558         }
   2559 
   2560     if ( ( NO_ERROR == ret ) && ( NULL != mCameraAdapter ) )
   2561         {
   2562 
   2563 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
   2564 
   2565          //pass capture timestamp along with the camera adapter command
   2566         ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_IMAGE_CAPTURE,  (int) &mStartCapture);
   2567 
   2568 #else
   2569 
   2570         ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_IMAGE_CAPTURE);
   2571 
   2572 #endif
   2573 
   2574         }
   2575 
   2576     return ret;
   2577 }
   2578 
   2579 /**
   2580    @brief Cancel a picture that was started with takePicture.
   2581 
   2582    Calling this method when no picture is being taken is a no-op.
   2583 
   2584    @param none
   2585    @return NO_ERROR If cancel succeeded. Cancel can succeed if image callback is not sent
   2586    @todo Define error codes
   2587 
   2588  */
   2589 status_t CameraHal::cancelPicture( )
   2590 {
   2591     LOG_FUNCTION_NAME;
   2592 
   2593     Mutex::Autolock lock(mLock);
   2594 
   2595     mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_IMAGE_CAPTURE);
   2596 
   2597     return NO_ERROR;
   2598 }
   2599 
   2600 /**
   2601    @brief Return the camera parameters.
   2602 
   2603    @param none
   2604    @return Currently configured camera parameters
   2605 
   2606  */
   2607 char* CameraHal::getParameters()
   2608 {
   2609     String8 params_str8;
   2610     char* params_string;
   2611     const char * valstr = NULL;
   2612 
   2613     LOG_FUNCTION_NAME;
   2614 
   2615     if( NULL != mCameraAdapter )
   2616     {
   2617         mCameraAdapter->getParameters(mParameters);
   2618     }
   2619 
   2620     CameraParameters mParams = mParameters;
   2621 
   2622     // Handle RECORDING_HINT to Set/Reset Video Mode Parameters
   2623     valstr = mParameters.get(CameraParameters::KEY_RECORDING_HINT);
   2624     if(valstr != NULL)
   2625       {
   2626         if(strcmp(valstr, CameraParameters::TRUE) == 0)
   2627           {
   2628             //HACK FOR MMS MODE
   2629             resetPreviewRes(&mParams, mVideoWidth, mVideoHeight);
   2630           }
   2631       }
   2632 
   2633     // do not send internal parameters to upper layers
   2634     mParams.remove(TICameraParameters::KEY_RECORDING_HINT);
   2635     mParams.remove(TICameraParameters::KEY_AUTO_FOCUS_LOCK);
   2636 
   2637     params_str8 = mParams.flatten();
   2638 
   2639     // camera service frees this string...
   2640     params_string = (char*) malloc(sizeof(char) * (params_str8.length()+1));
   2641     strcpy(params_string, params_str8.string());
   2642 
   2643     LOG_FUNCTION_NAME_EXIT;
   2644 
   2645     ///Return the current set of parameters
   2646 
   2647     return params_string;
   2648 }
   2649 
   2650 void CameraHal::putParameters(char *parms)
   2651 {
   2652     free(parms);
   2653 }
   2654 
   2655 /**
   2656    @brief Send command to camera driver.
   2657 
   2658    @param none
   2659    @return NO_ERROR If the command succeeds
   2660    @todo Define the error codes that this function can return
   2661 
   2662  */
   2663 status_t CameraHal::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
   2664 {
   2665     status_t ret = NO_ERROR;
   2666 
   2667     LOG_FUNCTION_NAME;
   2668 
   2669 
   2670     if ( ( NO_ERROR == ret ) && ( NULL == mCameraAdapter ) )
   2671         {
   2672         CAMHAL_LOGEA("No CameraAdapter instance");
   2673         ret = -EINVAL;
   2674         }
   2675 
   2676     if ( ( NO_ERROR == ret ) && ( !previewEnabled() ))
   2677         {
   2678         CAMHAL_LOGEA("Preview is not running");
   2679         ret = -EINVAL;
   2680         }
   2681 
   2682     if ( NO_ERROR == ret )
   2683         {
   2684         switch(cmd)
   2685             {
   2686             case CAMERA_CMD_START_SMOOTH_ZOOM:
   2687 
   2688                 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_SMOOTH_ZOOM, arg1);
   2689 
   2690                 break;
   2691             case CAMERA_CMD_STOP_SMOOTH_ZOOM:
   2692 
   2693                 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_SMOOTH_ZOOM);
   2694 
   2695             case CAMERA_CMD_START_FACE_DETECTION:
   2696 
   2697                 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_FD);
   2698 
   2699                 break;
   2700 
   2701             case CAMERA_CMD_STOP_FACE_DETECTION:
   2702 
   2703                 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_FD);
   2704 
   2705                 break;
   2706 
   2707             default:
   2708                 break;
   2709             };
   2710         }
   2711 
   2712     LOG_FUNCTION_NAME_EXIT;
   2713 
   2714     return ret;
   2715 }
   2716 
   2717 /**
   2718    @brief Release the hardware resources owned by this object.
   2719 
   2720    Note that this is *not* done in the destructor.
   2721 
   2722    @param none
   2723    @return none
   2724 
   2725  */
   2726 void CameraHal::release()
   2727 {
   2728     LOG_FUNCTION_NAME;
   2729     ///@todo Investigate on how release is used by CameraService. Vaguely remember that this is called
   2730     ///just before CameraHal object destruction
   2731     deinitialize();
   2732     LOG_FUNCTION_NAME_EXIT;
   2733 }
   2734 
   2735 
   2736 /**
   2737    @brief Dump state of the camera hardware
   2738 
   2739    @param[in] fd    File descriptor
   2740    @param[in] args  Arguments
   2741    @return NO_ERROR Dump succeeded
   2742    @todo  Error codes for dump fail
   2743 
   2744  */
   2745 status_t  CameraHal::dump(int fd) const
   2746 {
   2747     LOG_FUNCTION_NAME;
   2748     ///Implement this method when the h/w dump function is supported on Ducati side
   2749     return NO_ERROR;
   2750 }
   2751 
   2752 /*-------------Camera Hal Interface Method definitions ENDS here--------------------*/
   2753 
   2754 
   2755 
   2756 
   2757 /*-------------Camera Hal Internal Method definitions STARTS here--------------------*/
   2758 
   2759 /**
   2760    @brief Constructor of CameraHal
   2761 
   2762    Member variables are initialized here.  No allocations should be done here as we
   2763    don't use c++ exceptions in the code.
   2764 
   2765  */
   2766 CameraHal::CameraHal(int cameraId)
   2767 {
   2768     LOG_FUNCTION_NAME;
   2769 
   2770     ///Initialize all the member variables to their defaults
   2771     mPreviewEnabled = false;
   2772     mPreviewBufs = NULL;
   2773     mImageBufs = NULL;
   2774     mBufProvider = NULL;
   2775     mPreviewStartInProgress = false;
   2776     mVideoBufs = NULL;
   2777     mVideoBufProvider = NULL;
   2778     mRecordingEnabled = false;
   2779     mDisplayPaused = false;
   2780     mSetPreviewWindowCalled = false;
   2781     mMsgEnabled = 0;
   2782     mAppCallbackNotifier = NULL;
   2783     mMemoryManager = NULL;
   2784     mCameraAdapter = NULL;
   2785     mBracketingEnabled = false;
   2786     mBracketingRunning = false;
   2787     mEventProvider = NULL;
   2788     mBracketRangePositive = 1;
   2789     mBracketRangeNegative = 1;
   2790     mMaxZoomSupported = 0;
   2791     mShutterEnabled = true;
   2792     mMeasurementEnabled = false;
   2793     mPreviewDataBufs = NULL;
   2794     mCameraProperties = NULL;
   2795     mCurrentTime = 0;
   2796     mFalsePreview = 0;
   2797     mImageOffsets = NULL;
   2798     mImageLength = 0;
   2799     mImageFd = 0;
   2800     mVideoOffsets = NULL;
   2801     mVideoFd = 0;
   2802     mVideoLength = 0;
   2803     mPreviewDataOffsets = NULL;
   2804     mPreviewDataFd = 0;
   2805     mPreviewDataLength = 0;
   2806     mPreviewFd = 0;
   2807     mPreviewWidth = 0;
   2808     mPreviewHeight = 0;
   2809     mPreviewLength = 0;
   2810     mPreviewOffsets = NULL;
   2811     mPreviewRunning = 0;
   2812     mPreviewStateOld = 0;
   2813     mRecordingEnabled = 0;
   2814     mRecordEnabled = 0;
   2815     mSensorListener = NULL;
   2816     mVideoWidth = 0;
   2817     mVideoHeight = 0;
   2818 
   2819 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
   2820 
   2821     //Initialize the CameraHAL constructor timestamp, which is used in the
   2822     // PPM() method as time reference if the user does not supply one.
   2823     gettimeofday(&ppm_start, NULL);
   2824 
   2825 #endif
   2826 
   2827     mCameraIndex = cameraId;
   2828 
   2829     LOG_FUNCTION_NAME_EXIT;
   2830 }
   2831 
   2832 /**
   2833    @brief Destructor of CameraHal
   2834 
   2835    This function simply calls deinitialize() to free up memory allocate during construct
   2836    phase
   2837  */
   2838 CameraHal::~CameraHal()
   2839 {
   2840     LOG_FUNCTION_NAME;
   2841 
   2842     ///Call de-initialize here once more - it is the last chance for us to relinquish all the h/w and s/w resources
   2843     deinitialize();
   2844 
   2845     if ( NULL != mEventProvider )
   2846         {
   2847         mEventProvider->disableEventNotification(CameraHalEvent::ALL_EVENTS);
   2848         delete mEventProvider;
   2849         mEventProvider = NULL;
   2850         }
   2851 
   2852     /// Free the callback notifier
   2853     mAppCallbackNotifier.clear();
   2854 
   2855     /// Free the display adapter
   2856     mDisplayAdapter.clear();
   2857 
   2858     if ( NULL != mCameraAdapter ) {
   2859         int strongCount = mCameraAdapter->getStrongCount();
   2860 
   2861         mCameraAdapter->decStrong(mCameraAdapter);
   2862 
   2863         mCameraAdapter = NULL;
   2864     }
   2865 
   2866     freeImageBufs();
   2867 
   2868     /// Free the memory manager
   2869     mMemoryManager.clear();
   2870 
   2871     LOG_FUNCTION_NAME_EXIT;
   2872 }
   2873 
   2874 /**
   2875    @brief Initialize the Camera HAL
   2876 
   2877    Creates CameraAdapter, AppCallbackNotifier, DisplayAdapter and MemoryManager
   2878 
   2879    @param None
   2880    @return NO_ERROR - On success
   2881          NO_MEMORY - On failure to allocate memory for any of the objects
   2882    @remarks Camera Hal internal function
   2883 
   2884  */
   2885 
   2886 status_t CameraHal::initialize(CameraProperties::Properties* properties)
   2887 {
   2888     LOG_FUNCTION_NAME;
   2889 
   2890     int sensor_index = 0;
   2891 
   2892     ///Initialize the event mask used for registering an event provider for AppCallbackNotifier
   2893     ///Currently, registering all events as to be coming from CameraAdapter
   2894     int32_t eventMask = CameraHalEvent::ALL_EVENTS;
   2895 
   2896     // Get my camera properties
   2897     mCameraProperties = properties;
   2898 
   2899     if(!mCameraProperties)
   2900     {
   2901         goto fail_loop;
   2902     }
   2903 
   2904     // Dump the properties of this Camera
   2905     // will only print if DEBUG macro is defined
   2906     mCameraProperties->dump();
   2907 
   2908     if (strcmp(CameraProperties::DEFAULT_VALUE, mCameraProperties->get(CameraProperties::CAMERA_SENSOR_INDEX)) != 0 )
   2909         {
   2910         sensor_index = atoi(mCameraProperties->get(CameraProperties::CAMERA_SENSOR_INDEX));
   2911         }
   2912 
   2913     CAMHAL_LOGDB("Sensor index %d", sensor_index);
   2914 
   2915     mCameraAdapter = CameraAdapter_Factory(sensor_index);
   2916     if ( ( NULL == mCameraAdapter ) || (mCameraAdapter->initialize(properties)!=NO_ERROR))
   2917         {
   2918         CAMHAL_LOGEA("Unable to create or initialize CameraAdapter");
   2919         mCameraAdapter = NULL;
   2920         goto fail_loop;
   2921         }
   2922 
   2923     mCameraAdapter->incStrong(mCameraAdapter);
   2924     mCameraAdapter->registerImageReleaseCallback(releaseImageBuffers, (void *) this);
   2925     mCameraAdapter->registerEndCaptureCallback(endImageCapture, (void *)this);
   2926 
   2927     if(!mAppCallbackNotifier.get())
   2928         {
   2929         /// Create the callback notifier
   2930         mAppCallbackNotifier = new AppCallbackNotifier();
   2931         if( ( NULL == mAppCallbackNotifier.get() ) || ( mAppCallbackNotifier->initialize() != NO_ERROR))
   2932             {
   2933             CAMHAL_LOGEA("Unable to create or initialize AppCallbackNotifier");
   2934             goto fail_loop;
   2935             }
   2936         }
   2937 
   2938     if(!mMemoryManager.get())
   2939         {
   2940         /// Create Memory Manager
   2941         mMemoryManager = new MemoryManager();
   2942         if( ( NULL == mMemoryManager.get() ) || ( mMemoryManager->initialize() != NO_ERROR))
   2943             {
   2944             CAMHAL_LOGEA("Unable to create or initialize MemoryManager");
   2945             goto fail_loop;
   2946             }
   2947         }
   2948 
   2949     ///Setup the class dependencies...
   2950 
   2951     ///AppCallbackNotifier has to know where to get the Camera frames and the events like auto focus lock etc from.
   2952     ///CameraAdapter is the one which provides those events
   2953     ///Set it as the frame and event providers for AppCallbackNotifier
   2954     ///@remarks  setEventProvider API takes in a bit mask of events for registering a provider for the different events
   2955     ///         That way, if events can come from DisplayAdapter in future, we will be able to add it as provider
   2956     ///         for any event
   2957     mAppCallbackNotifier->setEventProvider(eventMask, mCameraAdapter);
   2958     mAppCallbackNotifier->setFrameProvider(mCameraAdapter);
   2959 
   2960     ///Any dynamic errors that happen during the camera use case has to be propagated back to the application
   2961     ///via CAMERA_MSG_ERROR. AppCallbackNotifier is the class that  notifies such errors to the application
   2962     ///Set it as the error handler for CameraAdapter
   2963     mCameraAdapter->setErrorHandler(mAppCallbackNotifier.get());
   2964 
   2965     ///Start the callback notifier
   2966     if(mAppCallbackNotifier->start() != NO_ERROR)
   2967       {
   2968         CAMHAL_LOGEA("Couldn't start AppCallbackNotifier");
   2969         goto fail_loop;
   2970       }
   2971 
   2972     CAMHAL_LOGDA("Started AppCallbackNotifier..");
   2973     mAppCallbackNotifier->setMeasurements(mMeasurementEnabled);
   2974 
   2975     ///Initialize default parameters
   2976     initDefaultParameters();
   2977 
   2978 
   2979     if ( setParameters(mParameters) != NO_ERROR )
   2980         {
   2981         CAMHAL_LOGEA("Failed to set default parameters?!");
   2982         }
   2983 
   2984     // register for sensor events
   2985     mSensorListener = new SensorListener();
   2986     if (mSensorListener.get()) {
   2987         if (mSensorListener->initialize() == NO_ERROR) {
   2988             mSensorListener->setCallbacks(orientation_cb, this);
   2989             mSensorListener->enableSensor(SensorListener::SENSOR_ORIENTATION);
   2990         } else {
   2991             CAMHAL_LOGEA("Error initializing SensorListener. not fatal, continuing");
   2992             mSensorListener.clear();
   2993             mSensorListener = NULL;
   2994         }
   2995     }
   2996 
   2997     LOG_FUNCTION_NAME_EXIT;
   2998 
   2999     return NO_ERROR;
   3000 
   3001     fail_loop:
   3002 
   3003         ///Free up the resources because we failed somewhere up
   3004         deinitialize();
   3005         LOG_FUNCTION_NAME_EXIT;
   3006 
   3007         return NO_MEMORY;
   3008 
   3009 }
   3010 
   3011 bool CameraHal::isResolutionValid(unsigned int width, unsigned int height, const char *supportedResolutions)
   3012 {
   3013     bool ret = true;
   3014     status_t status = NO_ERROR;
   3015     char tmpBuffer[PARAM_BUFFER + 1];
   3016     char *pos = NULL;
   3017 
   3018     LOG_FUNCTION_NAME;
   3019 
   3020     if ( NULL == supportedResolutions )
   3021         {
   3022         CAMHAL_LOGEA("Invalid supported resolutions string");
   3023         ret = false;
   3024         goto exit;
   3025         }
   3026 
   3027     status = snprintf(tmpBuffer, PARAM_BUFFER, "%dx%d", width, height);
   3028     if ( 0 > status )
   3029         {
   3030         CAMHAL_LOGEA("Error encountered while generating validation string");
   3031         ret = false;
   3032         goto exit;
   3033         }
   3034 
   3035     pos = strstr(supportedResolutions, tmpBuffer);
   3036     if ( NULL == pos )
   3037         {
   3038         ret = false;
   3039         }
   3040     else
   3041         {
   3042         ret = true;
   3043         }
   3044 
   3045 exit:
   3046 
   3047     LOG_FUNCTION_NAME_EXIT;
   3048 
   3049     return ret;
   3050 }
   3051 
   3052 bool CameraHal::isParameterValid(const char *param, const char *supportedParams)
   3053 {
   3054     bool ret = true;
   3055     char *pos = NULL;
   3056 
   3057     LOG_FUNCTION_NAME;
   3058 
   3059     if ( NULL == supportedParams )
   3060         {
   3061         CAMHAL_LOGEA("Invalid supported parameters string");
   3062         ret = false;
   3063         goto exit;
   3064         }
   3065 
   3066     if ( NULL == param )
   3067         {
   3068         CAMHAL_LOGEA("Invalid parameter string");
   3069         ret = false;
   3070         goto exit;
   3071         }
   3072 
   3073     pos = strstr(supportedParams, param);
   3074     if ( NULL == pos )
   3075         {
   3076         ret = false;
   3077         }
   3078     else
   3079         {
   3080         ret = true;
   3081         }
   3082 
   3083 exit:
   3084 
   3085     LOG_FUNCTION_NAME_EXIT;
   3086 
   3087     return ret;
   3088 }
   3089 
   3090 bool CameraHal::isParameterValid(int param, const char *supportedParams)
   3091 {
   3092     bool ret = true;
   3093     char *pos = NULL;
   3094     status_t status;
   3095     char tmpBuffer[PARAM_BUFFER + 1];
   3096 
   3097     LOG_FUNCTION_NAME;
   3098 
   3099     if ( NULL == supportedParams )
   3100         {
   3101         CAMHAL_LOGEA("Invalid supported parameters string");
   3102         ret = false;
   3103         goto exit;
   3104         }
   3105 
   3106     status = snprintf(tmpBuffer, PARAM_BUFFER, "%d", param);
   3107     if ( 0 > status )
   3108         {
   3109         CAMHAL_LOGEA("Error encountered while generating validation string");
   3110         ret = false;
   3111         goto exit;
   3112         }
   3113 
   3114     pos = strstr(supportedParams, tmpBuffer);
   3115     if ( NULL == pos )
   3116         {
   3117         ret = false;
   3118         }
   3119     else
   3120         {
   3121         ret = true;
   3122         }
   3123 
   3124 exit:
   3125 
   3126     LOG_FUNCTION_NAME_EXIT;
   3127 
   3128     return ret;
   3129 }
   3130 
   3131 status_t CameraHal::doesSetParameterNeedUpdate(const char* new_param, const char* old_param, bool& update) {
   3132     if (!new_param || !old_param) {
   3133         return -EINVAL;
   3134     }
   3135 
   3136     // if params mismatch we should update parameters for camera adapter
   3137     if ((strcmp(new_param, old_param) != 0)) {
   3138        update = true;
   3139     }
   3140 
   3141    return NO_ERROR;
   3142 }
   3143 
   3144 status_t CameraHal::parseResolution(const char *resStr, int &width, int &height)
   3145 {
   3146     status_t ret = NO_ERROR;
   3147     char *ctx, *pWidth, *pHeight;
   3148     const char *sep = "x";
   3149     char *tmp = NULL;
   3150 
   3151     LOG_FUNCTION_NAME;
   3152 
   3153     if ( NULL == resStr )
   3154         {
   3155         return -EINVAL;
   3156         }
   3157 
   3158     //This fixes "Invalid input resolution"
   3159     char *resStr_copy = (char *)malloc(strlen(resStr) + 1);
   3160     if ( NULL!=resStr_copy ) {
   3161     if ( NO_ERROR == ret )
   3162         {
   3163         strcpy(resStr_copy, resStr);
   3164         pWidth = strtok_r( (char *) resStr_copy, sep, &ctx);
   3165 
   3166         if ( NULL != pWidth )
   3167             {
   3168             width = atoi(pWidth);
   3169             }
   3170         else
   3171             {
   3172             CAMHAL_LOGEB("Invalid input resolution %s", resStr);
   3173             ret = -EINVAL;
   3174             }
   3175         }
   3176 
   3177     if ( NO_ERROR == ret )
   3178         {
   3179         pHeight = strtok_r(NULL, sep, &ctx);
   3180 
   3181         if ( NULL != pHeight )
   3182             {
   3183             height = atoi(pHeight);
   3184             }
   3185         else
   3186             {
   3187             CAMHAL_LOGEB("Invalid input resolution %s", resStr);
   3188             ret = -EINVAL;
   3189             }
   3190         }
   3191 
   3192         free(resStr_copy);
   3193         resStr_copy = NULL;
   3194      }
   3195     LOG_FUNCTION_NAME_EXIT;
   3196 
   3197     return ret;
   3198 }
   3199 
   3200 void CameraHal::insertSupportedParams()
   3201 {
   3202     char tmpBuffer[PARAM_BUFFER + 1];
   3203 
   3204     LOG_FUNCTION_NAME;
   3205 
   3206     CameraParameters &p = mParameters;
   3207 
   3208     ///Set the name of the camera
   3209     p.set(TICameraParameters::KEY_CAMERA_NAME, mCameraProperties->get(CameraProperties::CAMERA_NAME));
   3210 
   3211     mMaxZoomSupported = atoi(mCameraProperties->get(CameraProperties::SUPPORTED_ZOOM_STAGES));
   3212 
   3213     p.set(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES, mCameraProperties->get(CameraProperties::SUPPORTED_PICTURE_SIZES));
   3214     p.set(CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS, mCameraProperties->get(CameraProperties::SUPPORTED_PICTURE_FORMATS));
   3215     p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES, mCameraProperties->get(CameraProperties::SUPPORTED_PREVIEW_SIZES));
   3216     p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS, mCameraProperties->get(CameraProperties::SUPPORTED_PREVIEW_FORMATS));
   3217     p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES, mCameraProperties->get(CameraProperties::SUPPORTED_PREVIEW_FRAME_RATES));
   3218     p.set(CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES, mCameraProperties->get(CameraProperties::SUPPORTED_THUMBNAIL_SIZES));
   3219     p.set(CameraParameters::KEY_SUPPORTED_WHITE_BALANCE, mCameraProperties->get(CameraProperties::SUPPORTED_WHITE_BALANCE));
   3220     p.set(CameraParameters::KEY_SUPPORTED_EFFECTS, mCameraProperties->get(CameraProperties::SUPPORTED_EFFECTS));
   3221     p.set(CameraParameters::KEY_SUPPORTED_SCENE_MODES, mCameraProperties->get(CameraProperties::SUPPORTED_SCENE_MODES));
   3222     p.set(CameraParameters::KEY_SUPPORTED_FLASH_MODES, mCameraProperties->get(CameraProperties::SUPPORTED_FLASH_MODES));
   3223     p.set(CameraParameters::KEY_SUPPORTED_FOCUS_MODES, mCameraProperties->get(CameraProperties::SUPPORTED_FOCUS_MODES));
   3224     p.set(CameraParameters::KEY_SUPPORTED_ANTIBANDING, mCameraProperties->get(CameraProperties::SUPPORTED_ANTIBANDING));
   3225     p.set(CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION, mCameraProperties->get(CameraProperties::SUPPORTED_EV_MAX));
   3226     p.set(CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION, mCameraProperties->get(CameraProperties::SUPPORTED_EV_MIN));
   3227     p.set(CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP, mCameraProperties->get(CameraProperties::SUPPORTED_EV_STEP));
   3228     p.set(CameraParameters::KEY_SUPPORTED_SCENE_MODES, mCameraProperties->get(CameraProperties::SUPPORTED_SCENE_MODES));
   3229     p.set(TICameraParameters::KEY_SUPPORTED_EXPOSURE, mCameraProperties->get(CameraProperties::SUPPORTED_EXPOSURE_MODES));
   3230     p.set(TICameraParameters::KEY_SUPPORTED_ISO_VALUES, mCameraProperties->get(CameraProperties::SUPPORTED_ISO_VALUES));
   3231     p.set(CameraParameters::KEY_ZOOM_RATIOS, mCameraProperties->get(CameraProperties::SUPPORTED_ZOOM_RATIOS));
   3232     p.set(CameraParameters::KEY_MAX_ZOOM, mCameraProperties->get(CameraProperties::SUPPORTED_ZOOM_STAGES));
   3233     p.set(CameraParameters::KEY_ZOOM_SUPPORTED, mCameraProperties->get(CameraProperties::ZOOM_SUPPORTED));
   3234     p.set(CameraParameters::KEY_SMOOTH_ZOOM_SUPPORTED, mCameraProperties->get(CameraProperties::SMOOTH_ZOOM_SUPPORTED));
   3235     p.set(TICameraParameters::KEY_SUPPORTED_IPP, mCameraProperties->get(CameraProperties::SUPPORTED_IPP_MODES));
   3236     p.set(TICameraParameters::KEY_S3D_SUPPORTED,mCameraProperties->get(CameraProperties::S3D_SUPPORTED));
   3237     p.set(TICameraParameters::KEY_S3D2D_PREVIEW_MODE,mCameraProperties->get(CameraProperties::S3D2D_PREVIEW_MODES));
   3238     p.set(TICameraParameters::KEY_AUTOCONVERGENCE_MODE, mCameraProperties->get(CameraProperties::AUTOCONVERGENCE_MODE));
   3239     p.set(TICameraParameters::KEY_MANUALCONVERGENCE_VALUES, mCameraProperties->get(CameraProperties::MANUALCONVERGENCE_VALUES));
   3240     p.set(CameraParameters::KEY_VIDEO_STABILIZATION_SUPPORTED, mCameraProperties->get(CameraProperties::VSTAB_SUPPORTED));
   3241     p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FPS_RANGE, mCameraProperties->get(CameraProperties::FRAMERATE_RANGE_SUPPORTED));
   3242     p.set(TICameraParameters::KEY_SENSOR_ORIENTATION, mCameraProperties->get(CameraProperties::SENSOR_ORIENTATION));
   3243     p.set(TICameraParameters::KEY_SENSOR_ORIENTATION_VALUES, mCameraProperties->get(CameraProperties::SENSOR_ORIENTATION_VALUES));
   3244     p.set(CameraParameters::KEY_AUTO_EXPOSURE_LOCK_SUPPORTED, mCameraProperties->get(CameraProperties::AUTO_EXPOSURE_LOCK_SUPPORTED));
   3245     p.set(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED, mCameraProperties->get(CameraProperties::AUTO_WHITEBALANCE_LOCK_SUPPORTED));
   3246     p.set(CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED, mCameraProperties->get(CameraProperties::VIDEO_SNAPSHOT_SUPPORTED));
   3247 
   3248     LOG_FUNCTION_NAME_EXIT;
   3249 
   3250 }
   3251 
   3252 void CameraHal::initDefaultParameters()
   3253 {
   3254     //Purpose of this function is to initialize the default current and supported parameters for the currently
   3255     //selected camera.
   3256 
   3257     CameraParameters &p = mParameters;
   3258     int currentRevision, adapterRevision;
   3259     status_t ret = NO_ERROR;
   3260     int width, height;
   3261 
   3262     LOG_FUNCTION_NAME;
   3263 
   3264     ret = parseResolution(mCameraProperties->get(CameraProperties::PREVIEW_SIZE), width, height);
   3265 
   3266     if ( NO_ERROR == ret )
   3267         {
   3268         p.setPreviewSize(width, height);
   3269         }
   3270     else
   3271         {
   3272         p.setPreviewSize(MIN_WIDTH, MIN_HEIGHT);
   3273         }
   3274 
   3275     ret = parseResolution(mCameraProperties->get(CameraProperties::PICTURE_SIZE), width, height);
   3276 
   3277     if ( NO_ERROR == ret )
   3278         {
   3279         p.setPictureSize(width, height);
   3280         }
   3281     else
   3282         {
   3283         p.setPictureSize(PICTURE_WIDTH, PICTURE_HEIGHT);
   3284         }
   3285 
   3286     ret = parseResolution(mCameraProperties->get(CameraProperties::JPEG_THUMBNAIL_SIZE), width, height);
   3287 
   3288     if ( NO_ERROR == ret )
   3289         {
   3290         p.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH, width);
   3291         p.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT, height);
   3292         }
   3293     else
   3294         {
   3295         p.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH, MIN_WIDTH);
   3296         p.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT, MIN_HEIGHT);
   3297         }
   3298 
   3299     insertSupportedParams();
   3300 
   3301     //Insert default values
   3302     p.setPreviewFrameRate(atoi(mCameraProperties->get(CameraProperties::PREVIEW_FRAME_RATE)));
   3303     p.setPreviewFormat(mCameraProperties->get(CameraProperties::PREVIEW_FORMAT));
   3304     p.setPictureFormat(mCameraProperties->get(CameraProperties::PICTURE_FORMAT));
   3305     p.set(CameraParameters::KEY_JPEG_QUALITY, mCameraProperties->get(CameraProperties::JPEG_QUALITY));
   3306     p.set(CameraParameters::KEY_WHITE_BALANCE, mCameraProperties->get(CameraProperties::WHITEBALANCE));
   3307     p.set(CameraParameters::KEY_EFFECT,  mCameraProperties->get(CameraProperties::EFFECT));
   3308     p.set(CameraParameters::KEY_ANTIBANDING, mCameraProperties->get(CameraProperties::ANTIBANDING));
   3309     p.set(CameraParameters::KEY_FLASH_MODE, mCameraProperties->get(CameraProperties::FLASH_MODE));
   3310     p.set(CameraParameters::KEY_FOCUS_MODE, mCameraProperties->get(CameraProperties::FOCUS_MODE));
   3311     p.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, mCameraProperties->get(CameraProperties::EV_COMPENSATION));
   3312     p.set(CameraParameters::KEY_SCENE_MODE, mCameraProperties->get(CameraProperties::SCENE_MODE));
   3313     p.set(CameraParameters::KEY_FLASH_MODE, mCameraProperties->get(CameraProperties::FLASH_MODE));
   3314     p.set(CameraParameters::KEY_ZOOM, mCameraProperties->get(CameraProperties::ZOOM));
   3315     p.set(TICameraParameters::KEY_CONTRAST, mCameraProperties->get(CameraProperties::CONTRAST));
   3316     p.set(TICameraParameters::KEY_SATURATION, mCameraProperties->get(CameraProperties::SATURATION));
   3317     p.set(TICameraParameters::KEY_BRIGHTNESS, mCameraProperties->get(CameraProperties::BRIGHTNESS));
   3318     p.set(TICameraParameters::KEY_SHARPNESS, mCameraProperties->get(CameraProperties::SHARPNESS));
   3319     p.set(TICameraParameters::KEY_EXPOSURE_MODE, mCameraProperties->get(CameraProperties::EXPOSURE_MODE));
   3320     p.set(TICameraParameters::KEY_ISO, mCameraProperties->get(CameraProperties::ISO_MODE));
   3321     p.set(TICameraParameters::KEY_IPP, mCameraProperties->get(CameraProperties::IPP));
   3322     p.set(TICameraParameters::KEY_GBCE, mCameraProperties->get(CameraProperties::GBCE));
   3323     p.set(TICameraParameters::KEY_S3D2D_PREVIEW, mCameraProperties->get(CameraProperties::S3D2D_PREVIEW));
   3324     p.set(TICameraParameters::KEY_AUTOCONVERGENCE, mCameraProperties->get(CameraProperties::AUTOCONVERGENCE));
   3325     p.set(TICameraParameters::KEY_MANUALCONVERGENCE_VALUES, mCameraProperties->get(CameraProperties::MANUALCONVERGENCE_VALUES));
   3326     p.set(CameraParameters::KEY_VIDEO_STABILIZATION, mCameraProperties->get(CameraProperties::VSTAB));
   3327     p.set(CameraParameters::KEY_FOCAL_LENGTH, mCameraProperties->get(CameraProperties::FOCAL_LENGTH));
   3328     p.set(CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE, mCameraProperties->get(CameraProperties::HOR_ANGLE));
   3329     p.set(CameraParameters::KEY_VERTICAL_VIEW_ANGLE, mCameraProperties->get(CameraProperties::VER_ANGLE));
   3330     p.set(CameraParameters::KEY_PREVIEW_FPS_RANGE,mCameraProperties->get(CameraProperties::FRAMERATE_RANGE));
   3331     p.set(TICameraParameters::KEY_SENSOR_ORIENTATION, mCameraProperties->get(CameraProperties::SENSOR_ORIENTATION));
   3332     p.set(TICameraParameters::KEY_SENSOR_ORIENTATION_VALUES, mCameraProperties->get(CameraProperties::SENSOR_ORIENTATION_VALUES));
   3333     p.set(TICameraParameters::KEY_EXIF_MAKE, mCameraProperties->get(CameraProperties::EXIF_MAKE));
   3334     p.set(TICameraParameters::KEY_EXIF_MODEL, mCameraProperties->get(CameraProperties::EXIF_MODEL));
   3335     p.set(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY, mCameraProperties->get(CameraProperties::JPEG_THUMBNAIL_QUALITY));
   3336     p.set(CameraParameters::KEY_VIDEO_FRAME_FORMAT, "OMX_TI_COLOR_FormatYUV420PackedSemiPlanar");
   3337     p.set(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_HW, mCameraProperties->get(CameraProperties::MAX_FD_HW_FACES));
   3338     p.set(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_SW, mCameraProperties->get(CameraProperties::MAX_FD_SW_FACES));
   3339 
   3340     // Only one area a.k.a Touch AF for now.
   3341     // TODO: Add support for multiple focus areas.
   3342     p.set(CameraParameters::KEY_MAX_NUM_FOCUS_AREAS, mCameraProperties->get(CameraProperties::MAX_FOCUS_AREAS));
   3343     p.set(CameraParameters::KEY_AUTO_EXPOSURE_LOCK, mCameraProperties->get(CameraProperties::AUTO_EXPOSURE_LOCK));
   3344     p.set(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK, mCameraProperties->get(CameraProperties::AUTO_WHITEBALANCE_LOCK));
   3345     p.set(CameraParameters::KEY_MAX_NUM_METERING_AREAS, mCameraProperties->get(CameraProperties::MAX_NUM_METERING_AREAS));
   3346 
   3347     LOG_FUNCTION_NAME_EXIT;
   3348 }
   3349 
   3350 /**
   3351    @brief Stop a previously started preview.
   3352    @param none
   3353    @return none
   3354 
   3355  */
   3356 void CameraHal::forceStopPreview()
   3357 {
   3358     LOG_FUNCTION_NAME;
   3359 
   3360     // stop bracketing if it is running
   3361     stopImageBracketing();
   3362 
   3363     if(mDisplayAdapter.get() != NULL) {
   3364         ///Stop the buffer display first
   3365         mDisplayAdapter->disableDisplay();
   3366     }
   3367 
   3368     if(mAppCallbackNotifier.get() != NULL) {
   3369         //Stop the callback sending
   3370         mAppCallbackNotifier->stop();
   3371         mAppCallbackNotifier->flushAndReturnFrames();
   3372         mAppCallbackNotifier->stopPreviewCallbacks();
   3373     }
   3374 
   3375     if ( NULL != mCameraAdapter ) {
   3376         CameraAdapter::AdapterState currentState;
   3377         CameraAdapter::AdapterState nextState;
   3378 
   3379         currentState = mCameraAdapter->getState();
   3380         nextState = mCameraAdapter->getNextState();
   3381 
   3382         // since prerequisite for capturing is for camera system
   3383         // to be previewing...cancel all captures before stopping
   3384         // preview
   3385         if ( (currentState == CameraAdapter::CAPTURE_STATE) &&
   3386              (nextState != CameraAdapter::PREVIEW_STATE)) {
   3387             mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_IMAGE_CAPTURE);
   3388         }
   3389 
   3390         // only need to send these control commands to state machine if we are
   3391         // passed the LOADED_PREVIEW_STATE
   3392         if (currentState > CameraAdapter::LOADED_PREVIEW_STATE) {
   3393            // according to javadoc...FD should be stopped in stopPreview
   3394            // and application needs to call startFaceDection again
   3395            // to restart FD
   3396            mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_FD);
   3397            mCameraAdapter->sendCommand(CameraAdapter::CAMERA_CANCEL_AUTOFOCUS);
   3398         }
   3399 
   3400         // only need to send these control commands to state machine if we are
   3401         // passed the INITIALIZED_STATE
   3402         if (currentState > CameraAdapter::INTIALIZED_STATE) {
   3403            //Stop the source of frames
   3404            mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_PREVIEW);
   3405         }
   3406     }
   3407 
   3408     freePreviewBufs();
   3409     freePreviewDataBufs();
   3410 
   3411     mPreviewEnabled = false;
   3412     mDisplayPaused = false;
   3413     mPreviewStartInProgress = false;
   3414 
   3415     LOG_FUNCTION_NAME_EXIT;
   3416 }
   3417 
   3418 /**
   3419    @brief Deallocates memory for all the resources held by Camera HAL.
   3420 
   3421    Frees the following objects- CameraAdapter, AppCallbackNotifier, DisplayAdapter,
   3422    and Memory Manager
   3423 
   3424    @param none
   3425    @return none
   3426 
   3427  */
   3428 void CameraHal::deinitialize()
   3429 {
   3430     LOG_FUNCTION_NAME;
   3431 
   3432     if ( mPreviewEnabled || mDisplayPaused ) {
   3433         forceStopPreview();
   3434     }
   3435 
   3436     mSetPreviewWindowCalled = false;
   3437 
   3438     if (mSensorListener.get()) {
   3439         mSensorListener->disableSensor(SensorListener::SENSOR_ORIENTATION);
   3440         mSensorListener.clear();
   3441         mSensorListener = NULL;
   3442     }
   3443 
   3444     LOG_FUNCTION_NAME_EXIT;
   3445 
   3446 }
   3447 
   3448 status_t CameraHal::storeMetaDataInBuffers(bool enable)
   3449 {
   3450     LOG_FUNCTION_NAME;
   3451 
   3452     return mAppCallbackNotifier->useMetaDataBufferMode(enable);
   3453 
   3454     LOG_FUNCTION_NAME_EXIT;
   3455 }
   3456 
   3457 void CameraHal::selectFPSRange(int framerate, int *min_fps, int *max_fps)
   3458 {
   3459   char * ptr;
   3460   char supported[MAX_PROP_VALUE_LENGTH];
   3461   int fpsrangeArray[2];
   3462   int i = 0;
   3463 
   3464   LOG_FUNCTION_NAME;
   3465   size_t size = strlen(mCameraProperties->get(CameraProperties::FRAMERATE_RANGE_SUPPORTED))+1;
   3466   strncpy(supported, mCameraProperties->get(CameraProperties::FRAMERATE_RANGE_SUPPORTED), size);
   3467 
   3468   ptr = strtok (supported," (,)");
   3469 
   3470   while (ptr != NULL)
   3471     {
   3472       fpsrangeArray[i]= atoi(ptr)/CameraHal::VFR_SCALE;
   3473       if (i == 1)
   3474         {
   3475           if (framerate == fpsrangeArray[i])
   3476             {
   3477               CAMHAL_LOGDB("SETTING FPS RANGE min = %d max = %d \n", fpsrangeArray[0], fpsrangeArray[1]);
   3478               *min_fps = fpsrangeArray[0]*CameraHal::VFR_SCALE;
   3479               *max_fps = fpsrangeArray[1]*CameraHal::VFR_SCALE;
   3480               break;
   3481             }
   3482         }
   3483       ptr = strtok (NULL, " (,)");
   3484       i++;
   3485       i%=2;
   3486     }
   3487 
   3488   LOG_FUNCTION_NAME_EXIT;
   3489 
   3490 }
   3491 
   3492 void CameraHal::setPreferredPreviewRes(int width, int height)
   3493 {
   3494   LOG_FUNCTION_NAME;
   3495 
   3496   if ( (width == 320) && (height == 240)){
   3497     mParameters.setPreviewSize(640,480);
   3498   }
   3499   if ( (width == 176) && (height == 144)){
   3500     mParameters.setPreviewSize(704,576);
   3501   }
   3502 
   3503   LOG_FUNCTION_NAME_EXIT;
   3504 }
   3505 
   3506 void CameraHal::resetPreviewRes(CameraParameters *mParams, int width, int height)
   3507 {
   3508   LOG_FUNCTION_NAME;
   3509 
   3510   if ( (width <= 320) && (height <= 240)){
   3511     mParams->setPreviewSize(mVideoWidth, mVideoHeight);
   3512   }
   3513 
   3514   LOG_FUNCTION_NAME_EXIT;
   3515 }
   3516 
   3517 };
   3518 
   3519 
   3520