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_LOGI("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_LOGI("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 // Restart Preview if needed by KEY_RECODING_HINT only if preview is already running. 1012 // If preview is not started yet, Video Mode parameters will take effect on next startPreview() 1013 if (restartPreviewRequired && previewEnabled() && !mRecordingEnabled) { 1014 CAMHAL_LOGDA("Restarting Preview"); 1015 ret = restartPreview(); 1016 } else if (restartPreviewRequired && !previewEnabled() && 1017 mDisplayPaused && !mRecordingEnabled) { 1018 CAMHAL_LOGDA("Stopping Preview"); 1019 forceStopPreview(); 1020 } 1021 1022 if (ret != NO_ERROR) 1023 { 1024 CAMHAL_LOGEA("Failed to restart Preview"); 1025 return ret; 1026 } 1027 1028 LOG_FUNCTION_NAME_EXIT; 1029 1030 return ret; 1031 } 1032 1033 status_t CameraHal::allocPreviewBufs(int width, int height, const char* previewFormat, 1034 unsigned int buffercount, unsigned int &max_queueable) 1035 { 1036 status_t ret = NO_ERROR; 1037 1038 LOG_FUNCTION_NAME; 1039 1040 if(mDisplayAdapter.get() == NULL) 1041 { 1042 // Memory allocation of preview buffers is now placed in gralloc 1043 // CameraHal should not allocate preview buffers without DisplayAdapter 1044 return NO_MEMORY; 1045 } 1046 1047 if(!mPreviewBufs) 1048 { 1049 ///@todo Pluralise the name of this method to allocateBuffers 1050 mPreviewLength = 0; 1051 mPreviewBufs = (int32_t *) mDisplayAdapter->allocateBuffer(width, height, 1052 previewFormat, 1053 mPreviewLength, 1054 buffercount); 1055 1056 if (NULL == mPreviewBufs ) { 1057 CAMHAL_LOGEA("Couldn't allocate preview buffers"); 1058 return NO_MEMORY; 1059 } 1060 1061 mPreviewOffsets = (uint32_t *) mDisplayAdapter->getOffsets(); 1062 if ( NULL == mPreviewOffsets ) { 1063 CAMHAL_LOGEA("Buffer mapping failed"); 1064 return BAD_VALUE; 1065 } 1066 1067 mPreviewFd = mDisplayAdapter->getFd(); 1068 if ( -1 == mPreviewFd ) { 1069 CAMHAL_LOGEA("Invalid handle"); 1070 return BAD_VALUE; 1071 } 1072 1073 mBufProvider = (BufferProvider*) mDisplayAdapter.get(); 1074 1075 ret = mDisplayAdapter->maxQueueableBuffers(max_queueable); 1076 if (ret != NO_ERROR) { 1077 return ret; 1078 } 1079 1080 } 1081 1082 LOG_FUNCTION_NAME_EXIT; 1083 1084 return ret; 1085 1086 } 1087 1088 status_t CameraHal::freePreviewBufs() 1089 { 1090 status_t ret = NO_ERROR; 1091 LOG_FUNCTION_NAME; 1092 1093 CAMHAL_LOGDB("mPreviewBufs = 0x%x", (unsigned int)mPreviewBufs); 1094 if(mPreviewBufs) 1095 { 1096 ///@todo Pluralise the name of this method to freeBuffers 1097 ret = mBufProvider->freeBuffer(mPreviewBufs); 1098 mPreviewBufs = NULL; 1099 LOG_FUNCTION_NAME_EXIT; 1100 return ret; 1101 } 1102 LOG_FUNCTION_NAME_EXIT; 1103 return ret; 1104 } 1105 1106 1107 status_t CameraHal::allocPreviewDataBufs(size_t size, size_t bufferCount) 1108 { 1109 status_t ret = NO_ERROR; 1110 int bytes; 1111 1112 LOG_FUNCTION_NAME; 1113 1114 bytes = size; 1115 1116 if ( NO_ERROR == ret ) 1117 { 1118 if( NULL != mPreviewDataBufs ) 1119 { 1120 ret = freePreviewDataBufs(); 1121 } 1122 } 1123 1124 if ( NO_ERROR == ret ) 1125 { 1126 bytes = ((bytes+4095)/4096)*4096; 1127 mPreviewDataBufs = (int32_t *)mMemoryManager->allocateBuffer(0, 0, NULL, bytes, bufferCount); 1128 1129 CAMHAL_LOGDB("Size of Preview data buffer = %d", bytes); 1130 if( NULL == mPreviewDataBufs ) 1131 { 1132 CAMHAL_LOGEA("Couldn't allocate image buffers using memory manager"); 1133 ret = -NO_MEMORY; 1134 } 1135 else 1136 { 1137 bytes = size; 1138 } 1139 } 1140 1141 if ( NO_ERROR == ret ) 1142 { 1143 mPreviewDataFd = mMemoryManager->getFd(); 1144 mPreviewDataLength = bytes; 1145 mPreviewDataOffsets = mMemoryManager->getOffsets(); 1146 } 1147 else 1148 { 1149 mPreviewDataFd = -1; 1150 mPreviewDataLength = 0; 1151 mPreviewDataOffsets = NULL; 1152 } 1153 1154 LOG_FUNCTION_NAME; 1155 1156 return ret; 1157 } 1158 1159 status_t CameraHal::freePreviewDataBufs() 1160 { 1161 status_t ret = NO_ERROR; 1162 1163 LOG_FUNCTION_NAME; 1164 1165 if ( NO_ERROR == ret ) 1166 { 1167 1168 if( NULL != mPreviewDataBufs ) 1169 { 1170 1171 ///@todo Pluralise the name of this method to freeBuffers 1172 ret = mMemoryManager->freeBuffer(mPreviewDataBufs); 1173 mPreviewDataBufs = NULL; 1174 1175 } 1176 } 1177 1178 LOG_FUNCTION_NAME_EXIT; 1179 1180 return ret; 1181 } 1182 1183 status_t CameraHal::allocImageBufs(unsigned int width, unsigned int height, size_t size, const char* previewFormat, unsigned int bufferCount) 1184 { 1185 status_t ret = NO_ERROR; 1186 int bytes; 1187 1188 LOG_FUNCTION_NAME; 1189 1190 bytes = size; 1191 1192 // allocate image buffers only if not already allocated 1193 if(NULL != mImageBufs) { 1194 return NO_ERROR; 1195 } 1196 1197 if ( NO_ERROR == ret ) 1198 { 1199 bytes = ((bytes+4095)/4096)*4096; 1200 mImageBufs = (int32_t *)mMemoryManager->allocateBuffer(0, 0, previewFormat, bytes, bufferCount); 1201 1202 CAMHAL_LOGDB("Size of Image cap buffer = %d", bytes); 1203 if( NULL == mImageBufs ) 1204 { 1205 CAMHAL_LOGEA("Couldn't allocate image buffers using memory manager"); 1206 ret = -NO_MEMORY; 1207 } 1208 else 1209 { 1210 bytes = size; 1211 } 1212 } 1213 1214 if ( NO_ERROR == ret ) 1215 { 1216 mImageFd = mMemoryManager->getFd(); 1217 mImageLength = bytes; 1218 mImageOffsets = mMemoryManager->getOffsets(); 1219 } 1220 else 1221 { 1222 mImageFd = -1; 1223 mImageLength = 0; 1224 mImageOffsets = NULL; 1225 } 1226 1227 LOG_FUNCTION_NAME; 1228 1229 return ret; 1230 } 1231 1232 status_t CameraHal::allocVideoBufs(uint32_t width, uint32_t height, uint32_t bufferCount) 1233 { 1234 status_t ret = NO_ERROR; 1235 LOG_FUNCTION_NAME; 1236 1237 if( NULL != mVideoBufs ){ 1238 ret = freeVideoBufs(mVideoBufs); 1239 mVideoBufs = NULL; 1240 } 1241 1242 if ( NO_ERROR == ret ){ 1243 int32_t stride; 1244 buffer_handle_t *bufsArr = new buffer_handle_t [bufferCount]; 1245 1246 if (bufsArr != NULL){ 1247 for (int i = 0; i< bufferCount; i++){ 1248 GraphicBufferAllocator &GrallocAlloc = GraphicBufferAllocator::get(); 1249 buffer_handle_t buf; 1250 ret = GrallocAlloc.alloc(width, height, HAL_PIXEL_FORMAT_NV12, CAMHAL_GRALLOC_USAGE, &buf, &stride); 1251 if (ret != NO_ERROR){ 1252 CAMHAL_LOGEA("Couldn't allocate video buffers using Gralloc"); 1253 ret = -NO_MEMORY; 1254 for (int j=0; j< i; j++){ 1255 buf = (buffer_handle_t)bufsArr[j]; 1256 CAMHAL_LOGEB("Freeing Gralloc Buffer 0x%x", buf); 1257 GrallocAlloc.free(buf); 1258 } 1259 delete [] bufsArr; 1260 goto exit; 1261 } 1262 bufsArr[i] = buf; 1263 CAMHAL_LOGVB("*** Gralloc Handle =0x%x ***", buf); 1264 } 1265 1266 mVideoBufs = (int32_t *)bufsArr; 1267 } 1268 else{ 1269 CAMHAL_LOGEA("Couldn't allocate video buffers "); 1270 ret = -NO_MEMORY; 1271 } 1272 } 1273 1274 exit: 1275 LOG_FUNCTION_NAME; 1276 1277 return ret; 1278 } 1279 1280 void endImageCapture( void *userData) 1281 { 1282 LOG_FUNCTION_NAME; 1283 1284 if ( NULL != userData ) 1285 { 1286 CameraHal *c = reinterpret_cast<CameraHal *>(userData); 1287 c->signalEndImageCapture(); 1288 } 1289 1290 LOG_FUNCTION_NAME_EXIT; 1291 } 1292 1293 void releaseImageBuffers(void *userData) 1294 { 1295 LOG_FUNCTION_NAME; 1296 1297 if (NULL != userData) { 1298 CameraHal *c = reinterpret_cast<CameraHal *>(userData); 1299 c->freeImageBufs(); 1300 } 1301 1302 LOG_FUNCTION_NAME_EXIT; 1303 } 1304 1305 status_t CameraHal::signalEndImageCapture() 1306 { 1307 status_t ret = NO_ERROR; 1308 int w,h; 1309 CameraParameters adapterParams = mParameters; 1310 Mutex::Autolock lock(mLock); 1311 1312 LOG_FUNCTION_NAME; 1313 1314 if ( mBracketingRunning ) { 1315 stopImageBracketing(); 1316 } else { 1317 mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_IMAGE_CAPTURE); 1318 } 1319 1320 LOG_FUNCTION_NAME_EXIT; 1321 1322 return ret; 1323 } 1324 1325 status_t CameraHal::freeImageBufs() 1326 { 1327 status_t ret = NO_ERROR; 1328 1329 LOG_FUNCTION_NAME; 1330 1331 if ( NO_ERROR == ret ) 1332 { 1333 1334 if( NULL != mImageBufs ) 1335 { 1336 1337 ///@todo Pluralise the name of this method to freeBuffers 1338 ret = mMemoryManager->freeBuffer(mImageBufs); 1339 mImageBufs = NULL; 1340 1341 } 1342 else 1343 { 1344 ret = -EINVAL; 1345 } 1346 1347 } 1348 1349 LOG_FUNCTION_NAME_EXIT; 1350 1351 return ret; 1352 } 1353 1354 status_t CameraHal::freeVideoBufs(void *bufs) 1355 { 1356 status_t ret = NO_ERROR; 1357 1358 LOG_FUNCTION_NAME; 1359 1360 buffer_handle_t *pBuf = (buffer_handle_t*)bufs; 1361 int count = atoi(mCameraProperties->get(CameraProperties::REQUIRED_PREVIEW_BUFS)); 1362 if(pBuf == NULL) 1363 { 1364 CAMHAL_LOGEA("NULL pointer passed to freeVideoBuffer"); 1365 LOG_FUNCTION_NAME_EXIT; 1366 return BAD_VALUE; 1367 } 1368 1369 GraphicBufferAllocator &GrallocAlloc = GraphicBufferAllocator::get(); 1370 1371 for(int i = 0; i < count; i++){ 1372 buffer_handle_t ptr = *pBuf++; 1373 CAMHAL_LOGVB("Free Video Gralloc Handle 0x%x", ptr); 1374 GrallocAlloc.free(ptr); 1375 } 1376 1377 LOG_FUNCTION_NAME_EXIT; 1378 1379 return ret; 1380 } 1381 1382 /** 1383 @brief Start preview mode. 1384 1385 @param none 1386 @return NO_ERROR Camera switched to VF mode 1387 @todo Update function header with the different errors that are possible 1388 1389 */ 1390 status_t CameraHal::startPreview() 1391 { 1392 1393 status_t ret = NO_ERROR; 1394 CameraAdapter::BuffersDescriptor desc; 1395 CameraFrame frame; 1396 const char *valstr = NULL; 1397 unsigned int required_buffer_count; 1398 unsigned int max_queueble_buffers; 1399 1400 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 1401 gettimeofday(&mStartPreview, NULL); 1402 #endif 1403 1404 LOG_FUNCTION_NAME; 1405 1406 if ( mPreviewEnabled ){ 1407 CAMHAL_LOGDA("Preview already running"); 1408 LOG_FUNCTION_NAME_EXIT; 1409 return ALREADY_EXISTS; 1410 } 1411 1412 if ( NULL != mCameraAdapter ) { 1413 ret = mCameraAdapter->setParameters(mParameters); 1414 } 1415 1416 if ((mPreviewStartInProgress == false) && (mDisplayPaused == false)){ 1417 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_QUERY_RESOLUTION_PREVIEW,( int ) &frame); 1418 if ( NO_ERROR != ret ){ 1419 CAMHAL_LOGEB("Error: CAMERA_QUERY_RESOLUTION_PREVIEW %d", ret); 1420 return ret; 1421 } 1422 1423 ///Update the current preview width and height 1424 mPreviewWidth = frame.mWidth; 1425 mPreviewHeight = frame.mHeight; 1426 //Update the padded width and height - required for VNF and VSTAB 1427 mParameters.set(TICameraParameters::KEY_PADDED_WIDTH, mPreviewWidth); 1428 mParameters.set(TICameraParameters::KEY_PADDED_HEIGHT, mPreviewHeight); 1429 1430 } 1431 1432 ///If we don't have the preview callback enabled and display adapter, 1433 if(!mSetPreviewWindowCalled || (mDisplayAdapter.get() == NULL)){ 1434 CAMHAL_LOGI("Preview not started. Preview in progress flag set"); 1435 mPreviewStartInProgress = true; 1436 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_SWITCH_TO_EXECUTING); 1437 if ( NO_ERROR != ret ){ 1438 CAMHAL_LOGEB("Error: CAMERA_SWITCH_TO_EXECUTING %d", ret); 1439 return ret; 1440 } 1441 return NO_ERROR; 1442 } 1443 1444 if( (mDisplayAdapter.get() != NULL) && ( !mPreviewEnabled ) && ( mDisplayPaused ) ) 1445 { 1446 CAMHAL_LOGDA("Preview is in paused state"); 1447 1448 mDisplayPaused = false; 1449 mPreviewEnabled = true; 1450 if ( NO_ERROR == ret ) 1451 { 1452 ret = mDisplayAdapter->pauseDisplay(mDisplayPaused); 1453 1454 if ( NO_ERROR != ret ) 1455 { 1456 CAMHAL_LOGEB("Display adapter resume failed %x", ret); 1457 } 1458 } 1459 //restart preview callbacks 1460 if(mMsgEnabled & CAMERA_MSG_PREVIEW_FRAME) 1461 { 1462 mAppCallbackNotifier->enableMsgType (CAMERA_MSG_PREVIEW_FRAME); 1463 } 1464 return ret; 1465 } 1466 1467 1468 required_buffer_count = atoi(mCameraProperties->get(CameraProperties::REQUIRED_PREVIEW_BUFS)); 1469 1470 ///Allocate the preview buffers 1471 ret = allocPreviewBufs(mPreviewWidth, mPreviewHeight, mParameters.getPreviewFormat(), required_buffer_count, max_queueble_buffers); 1472 1473 if ( NO_ERROR != ret ) 1474 { 1475 CAMHAL_LOGEA("Couldn't allocate buffers for Preview"); 1476 goto error; 1477 } 1478 1479 if ( mMeasurementEnabled ) 1480 { 1481 1482 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_QUERY_BUFFER_SIZE_PREVIEW_DATA, 1483 ( int ) &frame, 1484 required_buffer_count); 1485 if ( NO_ERROR != ret ) 1486 { 1487 return ret; 1488 } 1489 1490 ///Allocate the preview data buffers 1491 ret = allocPreviewDataBufs(frame.mLength, required_buffer_count); 1492 if ( NO_ERROR != ret ) { 1493 CAMHAL_LOGEA("Couldn't allocate preview data buffers"); 1494 goto error; 1495 } 1496 1497 if ( NO_ERROR == ret ) 1498 { 1499 desc.mBuffers = mPreviewDataBufs; 1500 desc.mOffsets = mPreviewDataOffsets; 1501 desc.mFd = mPreviewDataFd; 1502 desc.mLength = mPreviewDataLength; 1503 desc.mCount = ( size_t ) required_buffer_count; 1504 desc.mMaxQueueable = (size_t) required_buffer_count; 1505 1506 mCameraAdapter->sendCommand(CameraAdapter::CAMERA_USE_BUFFERS_PREVIEW_DATA, 1507 ( int ) &desc); 1508 } 1509 1510 } 1511 1512 ///Pass the buffers to Camera Adapter 1513 desc.mBuffers = mPreviewBufs; 1514 desc.mOffsets = mPreviewOffsets; 1515 desc.mFd = mPreviewFd; 1516 desc.mLength = mPreviewLength; 1517 desc.mCount = ( size_t ) required_buffer_count; 1518 desc.mMaxQueueable = (size_t) max_queueble_buffers; 1519 1520 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_USE_BUFFERS_PREVIEW, 1521 ( int ) &desc); 1522 1523 if ( NO_ERROR != ret ) 1524 { 1525 CAMHAL_LOGEB("Failed to register preview buffers: 0x%x", ret); 1526 freePreviewBufs(); 1527 return ret; 1528 } 1529 1530 mAppCallbackNotifier->startPreviewCallbacks(mParameters, mPreviewBufs, mPreviewOffsets, mPreviewFd, mPreviewLength, required_buffer_count); 1531 1532 ///Start the callback notifier 1533 ret = mAppCallbackNotifier->start(); 1534 1535 if( ALREADY_EXISTS == ret ) 1536 { 1537 //Already running, do nothing 1538 CAMHAL_LOGDA("AppCallbackNotifier already running"); 1539 ret = NO_ERROR; 1540 } 1541 else if ( NO_ERROR == ret ) { 1542 CAMHAL_LOGDA("Started AppCallbackNotifier.."); 1543 mAppCallbackNotifier->setMeasurements(mMeasurementEnabled); 1544 } 1545 else 1546 { 1547 CAMHAL_LOGDA("Couldn't start AppCallbackNotifier"); 1548 goto error; 1549 } 1550 1551 ///Enable the display adapter if present, actual overlay enable happens when we post the buffer 1552 if(mDisplayAdapter.get() != NULL) 1553 { 1554 CAMHAL_LOGDA("Enabling display"); 1555 bool isS3d = false; 1556 DisplayAdapter::S3DParameters s3dParams; 1557 int width, height; 1558 mParameters.getPreviewSize(&width, &height); 1559 #if 0 //TODO: s3d is not part of bringup...will reenable 1560 if ( (valstr = mParameters.get(TICameraParameters::KEY_S3D_SUPPORTED)) != NULL) { 1561 isS3d = (strcmp(valstr, "true") == 0); 1562 } 1563 if ( (valstr = mParameters.get(TICameraParameters::KEY_S3D2D_PREVIEW)) != NULL) { 1564 if (strcmp(valstr, "off") == 0) 1565 { 1566 CAMHAL_LOGEA("STEREO 3D->2D PREVIEW MODE IS OFF"); 1567 //TODO: obtain the frame packing configuration from camera or user settings 1568 //once side by side configuration is supported 1569 s3dParams.mode = OVERLAY_S3D_MODE_ON; 1570 s3dParams.framePacking = OVERLAY_S3D_FORMAT_OVERUNDER; 1571 s3dParams.order = OVERLAY_S3D_ORDER_LF; 1572 s3dParams.subSampling = OVERLAY_S3D_SS_NONE; 1573 } 1574 else 1575 { 1576 CAMHAL_LOGEA("STEREO 3D->2D PREVIEW MODE IS ON"); 1577 s3dParams.mode = OVERLAY_S3D_MODE_OFF; 1578 s3dParams.framePacking = OVERLAY_S3D_FORMAT_OVERUNDER; 1579 s3dParams.order = OVERLAY_S3D_ORDER_LF; 1580 s3dParams.subSampling = OVERLAY_S3D_SS_NONE; 1581 } 1582 } 1583 #endif //if 0 1584 1585 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 1586 1587 ret = mDisplayAdapter->enableDisplay(width, height, &mStartPreview, isS3d ? &s3dParams : NULL); 1588 1589 #else 1590 1591 ret = mDisplayAdapter->enableDisplay(width, height, NULL, isS3d ? &s3dParams : NULL); 1592 1593 #endif 1594 1595 if ( ret != NO_ERROR ) 1596 { 1597 CAMHAL_LOGEA("Couldn't enable display"); 1598 goto error; 1599 } 1600 1601 } 1602 1603 ///Send START_PREVIEW command to adapter 1604 CAMHAL_LOGDA("Starting CameraAdapter preview mode"); 1605 1606 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_PREVIEW); 1607 1608 if(ret!=NO_ERROR) 1609 { 1610 CAMHAL_LOGEA("Couldn't start preview w/ CameraAdapter"); 1611 goto error; 1612 } 1613 CAMHAL_LOGDA("Started preview"); 1614 1615 mPreviewEnabled = true; 1616 mPreviewStartInProgress = false; 1617 return ret; 1618 1619 error: 1620 1621 CAMHAL_LOGEA("Performing cleanup after error"); 1622 1623 //Do all the cleanup 1624 freePreviewBufs(); 1625 mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_PREVIEW); 1626 if(mDisplayAdapter.get() != NULL) 1627 { 1628 mDisplayAdapter->disableDisplay(false); 1629 } 1630 mAppCallbackNotifier->stop(); 1631 mPreviewStartInProgress = false; 1632 mPreviewEnabled = false; 1633 LOG_FUNCTION_NAME_EXIT; 1634 1635 return ret; 1636 } 1637 1638 /** 1639 @brief Sets ANativeWindow object. 1640 1641 Preview buffers provided to CameraHal via this object. DisplayAdapter will be interfacing with it 1642 to render buffers to display. 1643 1644 @param[in] window The ANativeWindow object created by Surface flinger 1645 @return NO_ERROR If the ANativeWindow object passes validation criteria 1646 @todo Define validation criteria for ANativeWindow object. Define error codes for scenarios 1647 1648 */ 1649 status_t CameraHal::setPreviewWindow(struct preview_stream_ops *window) 1650 { 1651 status_t ret = NO_ERROR; 1652 CameraAdapter::BuffersDescriptor desc; 1653 1654 LOG_FUNCTION_NAME; 1655 mSetPreviewWindowCalled = true; 1656 1657 ///If the Camera service passes a null window, we destroy existing window and free the DisplayAdapter 1658 if(!window) 1659 { 1660 if(mDisplayAdapter.get() != NULL) 1661 { 1662 ///NULL window passed, destroy the display adapter if present 1663 CAMHAL_LOGI("NULL window passed, destroying display adapter"); 1664 mDisplayAdapter.clear(); 1665 ///@remarks If there was a window previously existing, we usually expect another valid window to be passed by the client 1666 ///@remarks so, we will wait until it passes a valid window to begin the preview again 1667 mSetPreviewWindowCalled = false; 1668 } 1669 CAMHAL_LOGI("NULL ANativeWindow passed to setPreviewWindow"); 1670 return NO_ERROR; 1671 }else if(mDisplayAdapter.get() == NULL) 1672 { 1673 // Need to create the display adapter since it has not been created 1674 // Create display adapter 1675 mDisplayAdapter = new ANativeWindowDisplayAdapter(); 1676 ret = NO_ERROR; 1677 if(!mDisplayAdapter.get() || ((ret=mDisplayAdapter->initialize())!=NO_ERROR)) 1678 { 1679 if(ret!=NO_ERROR) 1680 { 1681 mDisplayAdapter.clear(); 1682 CAMHAL_LOGEA("DisplayAdapter initialize failed"); 1683 LOG_FUNCTION_NAME_EXIT; 1684 return ret; 1685 } 1686 else 1687 { 1688 CAMHAL_LOGEA("Couldn't create DisplayAdapter"); 1689 LOG_FUNCTION_NAME_EXIT; 1690 return NO_MEMORY; 1691 } 1692 } 1693 1694 // DisplayAdapter needs to know where to get the CameraFrames from inorder to display 1695 // Since CameraAdapter is the one that provides the frames, set it as the frame provider for DisplayAdapter 1696 mDisplayAdapter->setFrameProvider(mCameraAdapter); 1697 1698 // Any dynamic errors that happen during the camera use case has to be propagated back to the application 1699 // via CAMERA_MSG_ERROR. AppCallbackNotifier is the class that notifies such errors to the application 1700 // Set it as the error handler for the DisplayAdapter 1701 mDisplayAdapter->setErrorHandler(mAppCallbackNotifier.get()); 1702 1703 // Update the display adapter with the new window that is passed from CameraService 1704 ret = mDisplayAdapter->setPreviewWindow(window); 1705 if(ret!=NO_ERROR) 1706 { 1707 CAMHAL_LOGEB("DisplayAdapter setPreviewWindow returned error %d", ret); 1708 } 1709 1710 if(mPreviewStartInProgress) 1711 { 1712 CAMHAL_LOGDA("setPreviewWindow called when preview running"); 1713 // Start the preview since the window is now available 1714 ret = startPreview(); 1715 } 1716 }else 1717 { 1718 /* If mDisplayAdpater is already created. No need to do anything. 1719 * We get a surface handle directly now, so we can reconfigure surface 1720 * itself in DisplayAdapter if dimensions have changed 1721 */ 1722 } 1723 LOG_FUNCTION_NAME_EXIT; 1724 1725 return ret; 1726 1727 } 1728 1729 1730 /** 1731 @brief Stop a previously started preview. 1732 1733 @param none 1734 @return none 1735 1736 */ 1737 void CameraHal::stopPreview() 1738 { 1739 LOG_FUNCTION_NAME; 1740 1741 if( (!previewEnabled() && !mDisplayPaused) || mRecordingEnabled) 1742 { 1743 LOG_FUNCTION_NAME_EXIT; 1744 return; 1745 } 1746 1747 bool imageCaptureRunning = (mCameraAdapter->getState() == CameraAdapter::CAPTURE_STATE) && 1748 (mCameraAdapter->getNextState() != CameraAdapter::PREVIEW_STATE); 1749 if(mDisplayPaused && !imageCaptureRunning) 1750 { 1751 // Display is paused, which essentially means there is no preview active. 1752 // Note: this is done so that when stopPreview is called by client after 1753 // an image capture, we do not de-initialize the camera adapter and 1754 // restart over again. 1755 1756 return; 1757 } 1758 1759 forceStopPreview(); 1760 1761 // Reset Capture-Mode to default, so that when we switch from VideoRecording 1762 // to ImageCapture, CAPTURE_MODE is not left to VIDEO_MODE. 1763 CAMHAL_LOGDA("Resetting Capture-Mode to default"); 1764 mParameters.set(TICameraParameters::KEY_CAP_MODE, ""); 1765 1766 LOG_FUNCTION_NAME_EXIT; 1767 } 1768 1769 /** 1770 @brief Returns true if preview is enabled 1771 1772 @param none 1773 @return true If preview is running currently 1774 false If preview has been stopped 1775 1776 */ 1777 bool CameraHal::previewEnabled() 1778 { 1779 LOG_FUNCTION_NAME; 1780 1781 return (mPreviewEnabled || mPreviewStartInProgress); 1782 } 1783 1784 /** 1785 @brief Start record mode. 1786 1787 When a record image is available a CAMERA_MSG_VIDEO_FRAME message is sent with 1788 the corresponding frame. Every record frame must be released by calling 1789 releaseRecordingFrame(). 1790 1791 @param none 1792 @return NO_ERROR If recording could be started without any issues 1793 @todo Update the header with possible error values in failure scenarios 1794 1795 */ 1796 status_t CameraHal::startRecording( ) 1797 { 1798 int w, h; 1799 const char *valstr = NULL; 1800 bool restartPreviewRequired = false; 1801 status_t ret = NO_ERROR; 1802 1803 LOG_FUNCTION_NAME; 1804 1805 1806 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 1807 1808 gettimeofday(&mStartPreview, NULL); 1809 1810 #endif 1811 1812 if(!previewEnabled()) 1813 { 1814 return NO_INIT; 1815 } 1816 1817 // set internal recording hint in case camera adapter needs to make some 1818 // decisions....(will only be sent to camera adapter if camera restart is required) 1819 mParameters.set(TICameraParameters::KEY_RECORDING_HINT, CameraParameters::TRUE); 1820 1821 // if application starts recording in continuous focus picture mode... 1822 // then we need to force default capture mode (as opposed to video mode) 1823 if ( ((valstr = mParameters.get(CameraParameters::KEY_FOCUS_MODE)) != NULL) && 1824 (strcmp(valstr, CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE) == 0) ){ 1825 restartPreviewRequired = resetVideoModeParameters(); 1826 } 1827 1828 // only need to check recording hint if preview restart is not already needed 1829 valstr = mParameters.get(CameraParameters::KEY_RECORDING_HINT); 1830 if ( !restartPreviewRequired && 1831 (!valstr || (valstr && (strcmp(valstr, CameraParameters::TRUE) != 0))) ) { 1832 restartPreviewRequired = setVideoModeParameters(mParameters); 1833 } 1834 1835 if (restartPreviewRequired) { 1836 ret = restartPreview(); 1837 } 1838 1839 if ( NO_ERROR == ret ) 1840 { 1841 int count = atoi(mCameraProperties->get(CameraProperties::REQUIRED_PREVIEW_BUFS)); 1842 mParameters.getPreviewSize(&w, &h); 1843 CAMHAL_LOGDB("%s Video Width=%d Height=%d", __FUNCTION__, mVideoWidth, mVideoHeight); 1844 1845 if ((w != mVideoWidth) && (h != mVideoHeight)) 1846 { 1847 ret = allocVideoBufs(mVideoWidth, mVideoHeight, count); 1848 if ( NO_ERROR != ret ) 1849 { 1850 CAMHAL_LOGEB("allocImageBufs returned error 0x%x", ret); 1851 mParameters.remove(TICameraParameters::KEY_RECORDING_HINT); 1852 return ret; 1853 } 1854 1855 mAppCallbackNotifier->useVideoBuffers(true); 1856 mAppCallbackNotifier->setVideoRes(mVideoWidth, mVideoHeight); 1857 ret = mAppCallbackNotifier->initSharedVideoBuffers(mPreviewBufs, mPreviewOffsets, mPreviewFd, mPreviewLength, count, mVideoBufs); 1858 } 1859 else 1860 { 1861 mAppCallbackNotifier->useVideoBuffers(false); 1862 mAppCallbackNotifier->setVideoRes(mPreviewWidth, mPreviewHeight); 1863 ret = mAppCallbackNotifier->initSharedVideoBuffers(mPreviewBufs, mPreviewOffsets, mPreviewFd, mPreviewLength, count, NULL); 1864 } 1865 } 1866 1867 if ( NO_ERROR == ret ) 1868 { 1869 ret = mAppCallbackNotifier->startRecording(); 1870 } 1871 1872 if ( NO_ERROR == ret ) 1873 { 1874 ///Buffers for video capture (if different from preview) are expected to be allocated within CameraAdapter 1875 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_VIDEO); 1876 } 1877 1878 if ( NO_ERROR == ret ) 1879 { 1880 mRecordingEnabled = true; 1881 } 1882 1883 LOG_FUNCTION_NAME_EXIT; 1884 1885 return ret; 1886 } 1887 1888 /** 1889 @brief Set the camera parameters specific to Video Recording. 1890 1891 This function checks for the camera parameters which have to be set for recording. 1892 Video Recording needs CAPTURE_MODE to be VIDEO_MODE. This function sets it. 1893 This function also enables Video Recording specific functions like VSTAB & VNF. 1894 1895 @param none 1896 @return true if preview needs to be restarted for VIDEO_MODE parameters to take effect. 1897 @todo Modify the policies for enabling VSTAB & VNF usecase based later. 1898 1899 */ 1900 bool CameraHal::setVideoModeParameters(const CameraParameters& params) 1901 { 1902 const char *valstr = NULL; 1903 bool restartPreviewRequired = false; 1904 status_t ret = NO_ERROR; 1905 1906 LOG_FUNCTION_NAME; 1907 1908 // Set CAPTURE_MODE to VIDEO_MODE, if not set already and Restart Preview 1909 valstr = mParameters.get(TICameraParameters::KEY_CAP_MODE); 1910 if ( (valstr == NULL) || 1911 ( (valstr != NULL) && (strcmp(valstr, (const char *) TICameraParameters::VIDEO_MODE) != 0) ) ) 1912 { 1913 CAMHAL_LOGDA("Set CAPTURE_MODE to VIDEO_MODE"); 1914 mParameters.set(TICameraParameters::KEY_CAP_MODE, (const char *) TICameraParameters::VIDEO_MODE); 1915 restartPreviewRequired = true; 1916 } 1917 1918 // Check if CAPTURE_MODE is VIDEO_MODE, since VSTAB & VNF work only in VIDEO_MODE. 1919 valstr = mParameters.get(TICameraParameters::KEY_CAP_MODE); 1920 if (strcmp(valstr, (const char *) TICameraParameters::VIDEO_MODE) == 0) { 1921 // set VSTAB. restart is required if vstab value has changed 1922 if (params.get(CameraParameters::KEY_VIDEO_STABILIZATION) != NULL) { 1923 // make sure we support vstab 1924 if (strcmp(mCameraProperties->get(CameraProperties::VSTAB_SUPPORTED), 1925 CameraParameters::TRUE) == 0) { 1926 valstr = mParameters.get(CameraParameters::KEY_VIDEO_STABILIZATION); 1927 // vstab value has changed 1928 if ((valstr != NULL) && 1929 strcmp(valstr, params.get(CameraParameters::KEY_VIDEO_STABILIZATION)) != 0) { 1930 restartPreviewRequired = true; 1931 } 1932 mParameters.set(CameraParameters::KEY_VIDEO_STABILIZATION, 1933 params.get(CameraParameters::KEY_VIDEO_STABILIZATION)); 1934 } 1935 } else if (mParameters.get(CameraParameters::KEY_VIDEO_STABILIZATION)) { 1936 // vstab was configured but now unset 1937 restartPreviewRequired = true; 1938 mParameters.remove(CameraParameters::KEY_VIDEO_STABILIZATION); 1939 } 1940 1941 // Set VNF 1942 if (params.get(TICameraParameters::KEY_VNF) == NULL) { 1943 CAMHAL_LOGDA("Enable VNF"); 1944 mParameters.set(TICameraParameters::KEY_VNF, "1"); 1945 restartPreviewRequired = true; 1946 } else { 1947 valstr = mParameters.get(TICameraParameters::KEY_VNF); 1948 if (valstr && strcmp(valstr, params.get(TICameraParameters::KEY_VNF)) != 0) { 1949 restartPreviewRequired = true; 1950 } 1951 mParameters.set(TICameraParameters::KEY_VNF, params.get(TICameraParameters::KEY_VNF)); 1952 } 1953 1954 // For VSTAB alone for 1080p resolution, padded width goes > 2048, which cannot be rendered by GPU. 1955 // In such case, there is support in Ducati for combination of VSTAB & VNF requiring padded width < 2048. 1956 // So we are forcefully enabling VNF, if VSTAB is enabled for 1080p resolution. 1957 valstr = mParameters.get(CameraParameters::KEY_VIDEO_STABILIZATION); 1958 if (valstr && (strcmp(valstr, CameraParameters::TRUE) == 0) && (mPreviewWidth == 1920)) { 1959 CAMHAL_LOGDA("Force Enable VNF for 1080p"); 1960 mParameters.set(TICameraParameters::KEY_VNF, "1"); 1961 restartPreviewRequired = true; 1962 } 1963 } 1964 LOG_FUNCTION_NAME_EXIT; 1965 1966 return restartPreviewRequired; 1967 } 1968 1969 /** 1970 @brief Reset the camera parameters specific to Video Recording. 1971 1972 This function resets CAPTURE_MODE and disables Recording specific functions like VSTAB & VNF. 1973 1974 @param none 1975 @return true if preview needs to be restarted for VIDEO_MODE parameters to take effect. 1976 1977 */ 1978 bool CameraHal::resetVideoModeParameters() 1979 { 1980 const char *valstr = NULL; 1981 bool restartPreviewRequired = false; 1982 status_t ret = NO_ERROR; 1983 1984 LOG_FUNCTION_NAME; 1985 1986 // ignore this if we are already recording 1987 if (mRecordingEnabled) { 1988 return false; 1989 } 1990 1991 // Set CAPTURE_MODE to VIDEO_MODE, if not set already and Restart Preview 1992 valstr = mParameters.get(TICameraParameters::KEY_CAP_MODE); 1993 if ((valstr != NULL) && (strcmp(valstr, TICameraParameters::VIDEO_MODE) == 0)) { 1994 CAMHAL_LOGDA("Reset Capture-Mode to default"); 1995 mParameters.set(TICameraParameters::KEY_CAP_MODE, ""); 1996 restartPreviewRequired = true; 1997 } 1998 1999 LOG_FUNCTION_NAME_EXIT; 2000 2001 return restartPreviewRequired; 2002 } 2003 2004 /** 2005 @brief Restart the preview with setParameter. 2006 2007 This function restarts preview, for some VIDEO_MODE parameters to take effect. 2008 2009 @param none 2010 @return NO_ERROR If recording parameters could be set without any issues 2011 2012 */ 2013 status_t CameraHal::restartPreview() 2014 { 2015 const char *valstr = NULL; 2016 char tmpvalstr[30]; 2017 status_t ret = NO_ERROR; 2018 2019 LOG_FUNCTION_NAME; 2020 2021 // Retain CAPTURE_MODE before calling stopPreview(), since it is reset in stopPreview(). 2022 tmpvalstr[0] = 0; 2023 valstr = mParameters.get(TICameraParameters::KEY_CAP_MODE); 2024 if(valstr != NULL) 2025 { 2026 if(sizeof(tmpvalstr) < (strlen(valstr)+1)) 2027 { 2028 return -EINVAL; 2029 } 2030 2031 strncpy(tmpvalstr, valstr, sizeof(tmpvalstr)); 2032 tmpvalstr[sizeof(tmpvalstr)-1] = 0; 2033 } 2034 2035 forceStopPreview(); 2036 2037 { 2038 Mutex::Autolock lock(mLock); 2039 mParameters.set(TICameraParameters::KEY_CAP_MODE, tmpvalstr); 2040 mCameraAdapter->setParameters(mParameters); 2041 } 2042 2043 ret = startPreview(); 2044 2045 LOG_FUNCTION_NAME_EXIT; 2046 2047 return ret; 2048 } 2049 2050 /** 2051 @brief Stop a previously started recording. 2052 2053 @param none 2054 @return none 2055 2056 */ 2057 void CameraHal::stopRecording() 2058 { 2059 CameraAdapter::AdapterState currentState; 2060 2061 LOG_FUNCTION_NAME; 2062 2063 Mutex::Autolock lock(mLock); 2064 2065 if (!mRecordingEnabled ) 2066 { 2067 return; 2068 } 2069 2070 currentState = mCameraAdapter->getState(); 2071 if (currentState == CameraAdapter::VIDEO_CAPTURE_STATE) { 2072 mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_IMAGE_CAPTURE); 2073 } 2074 2075 mAppCallbackNotifier->stopRecording(); 2076 2077 mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_VIDEO); 2078 2079 mRecordingEnabled = false; 2080 2081 if ( mAppCallbackNotifier->getUesVideoBuffers() ){ 2082 freeVideoBufs(mVideoBufs); 2083 if (mVideoBufs){ 2084 CAMHAL_LOGVB(" FREEING mVideoBufs 0x%x", mVideoBufs); 2085 delete [] mVideoBufs; 2086 } 2087 mVideoBufs = NULL; 2088 } 2089 2090 // reset internal recording hint in case camera adapter needs to make some 2091 // decisions....(will only be sent to camera adapter if camera restart is required) 2092 mParameters.remove(TICameraParameters::KEY_RECORDING_HINT); 2093 2094 LOG_FUNCTION_NAME_EXIT; 2095 } 2096 2097 /** 2098 @brief Returns true if recording is enabled. 2099 2100 @param none 2101 @return true If recording is currently running 2102 false If recording has been stopped 2103 2104 */ 2105 int CameraHal::recordingEnabled() 2106 { 2107 LOG_FUNCTION_NAME; 2108 2109 LOG_FUNCTION_NAME_EXIT; 2110 2111 return mRecordingEnabled; 2112 } 2113 2114 /** 2115 @brief Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME. 2116 2117 @param[in] mem MemoryBase pointer to the frame being released. Must be one of the buffers 2118 previously given by CameraHal 2119 @return none 2120 2121 */ 2122 void CameraHal::releaseRecordingFrame(const void* mem) 2123 { 2124 LOG_FUNCTION_NAME; 2125 2126 //CAMHAL_LOGDB(" 0x%x", mem->pointer()); 2127 2128 if ( ( mRecordingEnabled ) && mem != NULL) 2129 { 2130 mAppCallbackNotifier->releaseRecordingFrame(mem); 2131 } 2132 2133 LOG_FUNCTION_NAME_EXIT; 2134 2135 return; 2136 } 2137 2138 /** 2139 @brief Start auto focus 2140 2141 This call asynchronous. 2142 The notification callback routine is called with CAMERA_MSG_FOCUS once when 2143 focusing is complete. autoFocus() will be called again if another auto focus is 2144 needed. 2145 2146 @param none 2147 @return NO_ERROR 2148 @todo Define the error codes if the focus is not locked 2149 2150 */ 2151 status_t CameraHal::autoFocus() 2152 { 2153 status_t ret = NO_ERROR; 2154 2155 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 2156 2157 gettimeofday(&mStartFocus, NULL); 2158 2159 #endif 2160 2161 LOG_FUNCTION_NAME; 2162 2163 Mutex::Autolock lock(mLock); 2164 2165 mMsgEnabled |= CAMERA_MSG_FOCUS; 2166 2167 if ( NULL == mCameraAdapter ) 2168 { 2169 ret = -1; 2170 goto EXIT; 2171 } 2172 2173 CameraAdapter::AdapterState state; 2174 ret = mCameraAdapter->getState(state); 2175 if (ret != NO_ERROR) 2176 { 2177 goto EXIT; 2178 } 2179 2180 if (state == CameraAdapter::AF_STATE) 2181 { 2182 CAMHAL_LOGI("Ignoring start-AF (already in progress)"); 2183 goto EXIT; 2184 } 2185 2186 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 2187 2188 //pass the autoFocus timestamp along with the command to camera adapter 2189 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_PERFORM_AUTOFOCUS, ( int ) &mStartFocus); 2190 2191 #else 2192 2193 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_PERFORM_AUTOFOCUS); 2194 2195 #endif 2196 2197 EXIT: 2198 LOG_FUNCTION_NAME_EXIT; 2199 2200 return ret; 2201 } 2202 2203 /** 2204 @brief Cancels auto-focus function. 2205 2206 If the auto-focus is still in progress, this function will cancel it. 2207 Whether the auto-focus is in progress or not, this function will return the 2208 focus position to the default. If the camera does not support auto-focus, this is a no-op. 2209 2210 2211 @param none 2212 @return NO_ERROR If the cancel succeeded 2213 @todo Define error codes if cancel didnt succeed 2214 2215 */ 2216 status_t CameraHal::cancelAutoFocus() 2217 { 2218 LOG_FUNCTION_NAME; 2219 2220 Mutex::Autolock lock(mLock); 2221 CameraParameters adapterParams = mParameters; 2222 mMsgEnabled &= ~CAMERA_MSG_FOCUS; 2223 2224 if( NULL != mCameraAdapter ) 2225 { 2226 adapterParams.set(TICameraParameters::KEY_AUTO_FOCUS_LOCK, CameraParameters::FALSE); 2227 mCameraAdapter->setParameters(adapterParams); 2228 mCameraAdapter->sendCommand(CameraAdapter::CAMERA_CANCEL_AUTOFOCUS); 2229 mAppCallbackNotifier->flushEventQueue(); 2230 } 2231 2232 LOG_FUNCTION_NAME_EXIT; 2233 return NO_ERROR; 2234 } 2235 2236 void CameraHal::setEventProvider(int32_t eventMask, MessageNotifier * eventNotifier) 2237 { 2238 2239 LOG_FUNCTION_NAME; 2240 2241 if ( NULL != mEventProvider ) 2242 { 2243 mEventProvider->disableEventNotification(CameraHalEvent::ALL_EVENTS); 2244 delete mEventProvider; 2245 mEventProvider = NULL; 2246 } 2247 2248 mEventProvider = new EventProvider(eventNotifier, this, eventCallbackRelay); 2249 if ( NULL == mEventProvider ) 2250 { 2251 CAMHAL_LOGEA("Error in creating EventProvider"); 2252 } 2253 else 2254 { 2255 mEventProvider->enableEventNotification(eventMask); 2256 } 2257 2258 LOG_FUNCTION_NAME_EXIT; 2259 } 2260 2261 void CameraHal::eventCallbackRelay(CameraHalEvent* event) 2262 { 2263 LOG_FUNCTION_NAME; 2264 2265 CameraHal *appcbn = ( CameraHal * ) (event->mCookie); 2266 appcbn->eventCallback(event ); 2267 2268 LOG_FUNCTION_NAME_EXIT; 2269 } 2270 2271 void CameraHal::eventCallback(CameraHalEvent* event) 2272 { 2273 LOG_FUNCTION_NAME; 2274 2275 if ( NULL != event ) 2276 { 2277 switch( event->mEventType ) 2278 { 2279 case CameraHalEvent::EVENT_FOCUS_LOCKED: 2280 case CameraHalEvent::EVENT_FOCUS_ERROR: 2281 { 2282 if ( mBracketingEnabled ) 2283 { 2284 startImageBracketing(); 2285 } 2286 break; 2287 } 2288 default: 2289 { 2290 break; 2291 } 2292 }; 2293 } 2294 2295 LOG_FUNCTION_NAME_EXIT; 2296 } 2297 2298 status_t CameraHal::startImageBracketing() 2299 { 2300 status_t ret = NO_ERROR; 2301 CameraFrame frame; 2302 CameraAdapter::BuffersDescriptor desc; 2303 2304 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 2305 2306 gettimeofday(&mStartCapture, NULL); 2307 2308 #endif 2309 2310 LOG_FUNCTION_NAME; 2311 2312 if(!previewEnabled() && !mDisplayPaused) 2313 { 2314 LOG_FUNCTION_NAME_EXIT; 2315 return NO_INIT; 2316 } 2317 2318 if ( !mBracketingEnabled ) 2319 { 2320 return ret; 2321 } 2322 2323 if ( NO_ERROR == ret ) 2324 { 2325 mBracketingRunning = true; 2326 } 2327 2328 if ( (NO_ERROR == ret) && ( NULL != mCameraAdapter ) ) 2329 { 2330 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE, 2331 ( int ) &frame, 2332 ( mBracketRangeNegative + 1 )); 2333 2334 if ( NO_ERROR != ret ) 2335 { 2336 CAMHAL_LOGEB("CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE returned error 0x%x", ret); 2337 } 2338 } 2339 2340 if ( NO_ERROR == ret ) 2341 { 2342 if ( NULL != mAppCallbackNotifier.get() ) 2343 { 2344 mAppCallbackNotifier->setBurst(true); 2345 } 2346 } 2347 2348 if ( NO_ERROR == ret ) 2349 { 2350 mParameters.getPictureSize(( int * ) &frame.mWidth, 2351 ( int * ) &frame.mHeight); 2352 2353 ret = allocImageBufs(frame.mWidth, 2354 frame.mHeight, 2355 frame.mLength, 2356 mParameters.getPictureFormat(), 2357 ( mBracketRangeNegative + 1 )); 2358 if ( NO_ERROR != ret ) 2359 { 2360 CAMHAL_LOGEB("allocImageBufs returned error 0x%x", ret); 2361 } 2362 } 2363 2364 if ( (NO_ERROR == ret) && ( NULL != mCameraAdapter ) ) 2365 { 2366 2367 desc.mBuffers = mImageBufs; 2368 desc.mOffsets = mImageOffsets; 2369 desc.mFd = mImageFd; 2370 desc.mLength = mImageLength; 2371 desc.mCount = ( size_t ) ( mBracketRangeNegative + 1 ); 2372 desc.mMaxQueueable = ( size_t ) ( mBracketRangeNegative + 1 ); 2373 2374 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_USE_BUFFERS_IMAGE_CAPTURE, 2375 ( int ) &desc); 2376 2377 if ( NO_ERROR == ret ) 2378 { 2379 2380 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 2381 2382 //pass capture timestamp along with the camera adapter command 2383 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_BRACKET_CAPTURE, ( mBracketRangePositive + 1 ), (int) &mStartCapture); 2384 2385 #else 2386 2387 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_BRACKET_CAPTURE, ( mBracketRangePositive + 1 )); 2388 2389 #endif 2390 2391 } 2392 } 2393 2394 return ret; 2395 } 2396 2397 status_t CameraHal::stopImageBracketing() 2398 { 2399 status_t ret = NO_ERROR; 2400 2401 LOG_FUNCTION_NAME; 2402 2403 if( !previewEnabled() ) 2404 { 2405 return NO_INIT; 2406 } 2407 2408 mBracketingRunning = false; 2409 2410 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_BRACKET_CAPTURE); 2411 2412 LOG_FUNCTION_NAME_EXIT; 2413 2414 return ret; 2415 } 2416 2417 /** 2418 @brief Take a picture. 2419 2420 @param none 2421 @return NO_ERROR If able to switch to image capture 2422 @todo Define error codes if unable to switch to image capture 2423 2424 */ 2425 status_t CameraHal::takePicture( ) 2426 { 2427 status_t ret = NO_ERROR; 2428 CameraFrame frame; 2429 CameraAdapter::BuffersDescriptor desc; 2430 int burst; 2431 const char *valstr = NULL; 2432 unsigned int bufferCount = 1; 2433 2434 Mutex::Autolock lock(mLock); 2435 2436 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 2437 2438 gettimeofday(&mStartCapture, NULL); 2439 2440 #endif 2441 2442 LOG_FUNCTION_NAME; 2443 2444 if(!previewEnabled() && !mDisplayPaused) 2445 { 2446 LOG_FUNCTION_NAME_EXIT; 2447 CAMHAL_LOGEA("Preview not started..."); 2448 return NO_INIT; 2449 } 2450 2451 // return error if we are already capturing 2452 if ( (mCameraAdapter->getState() == CameraAdapter::CAPTURE_STATE && 2453 mCameraAdapter->getNextState() != CameraAdapter::PREVIEW_STATE) || 2454 (mCameraAdapter->getState() == CameraAdapter::VIDEO_CAPTURE_STATE && 2455 mCameraAdapter->getNextState() != CameraAdapter::VIDEO_STATE) ) { 2456 CAMHAL_LOGEA("Already capturing an image..."); 2457 return NO_INIT; 2458 } 2459 2460 // we only support video snapshot if we are in video mode (recording hint is set) 2461 valstr = mParameters.get(TICameraParameters::KEY_CAP_MODE); 2462 if ( (mCameraAdapter->getState() == CameraAdapter::VIDEO_STATE) && 2463 (valstr && strcmp(valstr, TICameraParameters::VIDEO_MODE)) ) { 2464 CAMHAL_LOGEA("Trying to capture while recording without recording hint set..."); 2465 return INVALID_OPERATION; 2466 } 2467 2468 if ( !mBracketingRunning ) 2469 { 2470 2471 if ( NO_ERROR == ret ) 2472 { 2473 burst = mParameters.getInt(TICameraParameters::KEY_BURST); 2474 } 2475 2476 //Allocate all buffers only in burst capture case 2477 if ( burst > 1 ) 2478 { 2479 bufferCount = CameraHal::NO_BUFFERS_IMAGE_CAPTURE; 2480 if ( NULL != mAppCallbackNotifier.get() ) 2481 { 2482 mAppCallbackNotifier->setBurst(true); 2483 } 2484 } 2485 else 2486 { 2487 if ( NULL != mAppCallbackNotifier.get() ) 2488 { 2489 mAppCallbackNotifier->setBurst(false); 2490 } 2491 } 2492 2493 // pause preview during normal image capture 2494 // do not pause preview if recording (video state) 2495 if (NO_ERROR == ret && 2496 NULL != mDisplayAdapter.get() && 2497 burst < 1) { 2498 if (mCameraAdapter->getState() != CameraAdapter::VIDEO_STATE) { 2499 mDisplayPaused = true; 2500 mPreviewEnabled = false; 2501 ret = mDisplayAdapter->pauseDisplay(mDisplayPaused); 2502 // since preview is paused we should stop sending preview frames too 2503 if(mMsgEnabled & CAMERA_MSG_PREVIEW_FRAME) { 2504 mAppCallbackNotifier->disableMsgType (CAMERA_MSG_PREVIEW_FRAME); 2505 } 2506 } 2507 2508 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 2509 mDisplayAdapter->setSnapshotTimeRef(&mStartCapture); 2510 #endif 2511 } 2512 2513 // if we taking video snapshot... 2514 if ((NO_ERROR == ret) && (mCameraAdapter->getState() == CameraAdapter::VIDEO_STATE)) { 2515 // enable post view frames if not already enabled so we can internally 2516 // save snapshot frames for generating thumbnail 2517 if((mMsgEnabled & CAMERA_MSG_POSTVIEW_FRAME) == 0) { 2518 mAppCallbackNotifier->enableMsgType(CAMERA_MSG_POSTVIEW_FRAME); 2519 } 2520 } 2521 2522 if ( (NO_ERROR == ret) && (NULL != mCameraAdapter) ) 2523 { 2524 if ( NO_ERROR == ret ) 2525 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE, 2526 ( int ) &frame, 2527 bufferCount); 2528 2529 if ( NO_ERROR != ret ) 2530 { 2531 CAMHAL_LOGEB("CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE returned error 0x%x", ret); 2532 } 2533 } 2534 2535 if ( NO_ERROR == ret ) 2536 { 2537 mParameters.getPictureSize(( int * ) &frame.mWidth, 2538 ( int * ) &frame.mHeight); 2539 2540 ret = allocImageBufs(frame.mWidth, 2541 frame.mHeight, 2542 frame.mLength, 2543 mParameters.getPictureFormat(), 2544 bufferCount); 2545 if ( NO_ERROR != ret ) 2546 { 2547 CAMHAL_LOGEB("allocImageBufs returned error 0x%x", ret); 2548 } 2549 } 2550 2551 if ( (NO_ERROR == ret) && ( NULL != mCameraAdapter ) ) 2552 { 2553 desc.mBuffers = mImageBufs; 2554 desc.mOffsets = mImageOffsets; 2555 desc.mFd = mImageFd; 2556 desc.mLength = mImageLength; 2557 desc.mCount = ( size_t ) bufferCount; 2558 desc.mMaxQueueable = ( size_t ) bufferCount; 2559 2560 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_USE_BUFFERS_IMAGE_CAPTURE, 2561 ( int ) &desc); 2562 } 2563 } 2564 2565 if ( ( NO_ERROR == ret ) && ( NULL != mCameraAdapter ) ) 2566 { 2567 2568 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 2569 2570 //pass capture timestamp along with the camera adapter command 2571 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_IMAGE_CAPTURE, (int) &mStartCapture); 2572 2573 #else 2574 2575 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_IMAGE_CAPTURE); 2576 2577 #endif 2578 2579 } 2580 2581 return ret; 2582 } 2583 2584 /** 2585 @brief Cancel a picture that was started with takePicture. 2586 2587 Calling this method when no picture is being taken is a no-op. 2588 2589 @param none 2590 @return NO_ERROR If cancel succeeded. Cancel can succeed if image callback is not sent 2591 @todo Define error codes 2592 2593 */ 2594 status_t CameraHal::cancelPicture( ) 2595 { 2596 LOG_FUNCTION_NAME; 2597 2598 Mutex::Autolock lock(mLock); 2599 2600 mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_IMAGE_CAPTURE); 2601 2602 return NO_ERROR; 2603 } 2604 2605 /** 2606 @brief Return the camera parameters. 2607 2608 @param none 2609 @return Currently configured camera parameters 2610 2611 */ 2612 char* CameraHal::getParameters() 2613 { 2614 String8 params_str8; 2615 char* params_string; 2616 const char * valstr = NULL; 2617 2618 LOG_FUNCTION_NAME; 2619 2620 if( NULL != mCameraAdapter ) 2621 { 2622 mCameraAdapter->getParameters(mParameters); 2623 } 2624 2625 CameraParameters mParams = mParameters; 2626 2627 // Handle RECORDING_HINT to Set/Reset Video Mode Parameters 2628 valstr = mParameters.get(CameraParameters::KEY_RECORDING_HINT); 2629 if(valstr != NULL) 2630 { 2631 if(strcmp(valstr, CameraParameters::TRUE) == 0) 2632 { 2633 //HACK FOR MMS MODE 2634 resetPreviewRes(&mParams, mVideoWidth, mVideoHeight); 2635 } 2636 } 2637 2638 // do not send internal parameters to upper layers 2639 mParams.remove(TICameraParameters::KEY_RECORDING_HINT); 2640 mParams.remove(TICameraParameters::KEY_AUTO_FOCUS_LOCK); 2641 2642 params_str8 = mParams.flatten(); 2643 2644 // camera service frees this string... 2645 params_string = (char*) malloc(sizeof(char) * (params_str8.length()+1)); 2646 strcpy(params_string, params_str8.string()); 2647 2648 LOG_FUNCTION_NAME_EXIT; 2649 2650 ///Return the current set of parameters 2651 2652 return params_string; 2653 } 2654 2655 void CameraHal::putParameters(char *parms) 2656 { 2657 free(parms); 2658 } 2659 2660 /** 2661 @brief Send command to camera driver. 2662 2663 @param none 2664 @return NO_ERROR If the command succeeds 2665 @todo Define the error codes that this function can return 2666 2667 */ 2668 status_t CameraHal::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) 2669 { 2670 status_t ret = NO_ERROR; 2671 2672 LOG_FUNCTION_NAME; 2673 2674 2675 if ( ( NO_ERROR == ret ) && ( NULL == mCameraAdapter ) ) 2676 { 2677 CAMHAL_LOGEA("No CameraAdapter instance"); 2678 ret = -EINVAL; 2679 } 2680 2681 if ( ( NO_ERROR == ret ) && ( !previewEnabled() )) 2682 { 2683 CAMHAL_LOGEA("Preview is not running"); 2684 ret = -EINVAL; 2685 } 2686 2687 if ( NO_ERROR == ret ) 2688 { 2689 switch(cmd) 2690 { 2691 case CAMERA_CMD_START_SMOOTH_ZOOM: 2692 2693 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_SMOOTH_ZOOM, arg1); 2694 2695 break; 2696 case CAMERA_CMD_STOP_SMOOTH_ZOOM: 2697 2698 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_SMOOTH_ZOOM); 2699 2700 case CAMERA_CMD_START_FACE_DETECTION: 2701 2702 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_FD); 2703 2704 break; 2705 2706 case CAMERA_CMD_STOP_FACE_DETECTION: 2707 2708 ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_FD); 2709 2710 break; 2711 2712 default: 2713 break; 2714 }; 2715 } 2716 2717 LOG_FUNCTION_NAME_EXIT; 2718 2719 return ret; 2720 } 2721 2722 /** 2723 @brief Release the hardware resources owned by this object. 2724 2725 Note that this is *not* done in the destructor. 2726 2727 @param none 2728 @return none 2729 2730 */ 2731 void CameraHal::release() 2732 { 2733 LOG_FUNCTION_NAME; 2734 ///@todo Investigate on how release is used by CameraService. Vaguely remember that this is called 2735 ///just before CameraHal object destruction 2736 deinitialize(); 2737 LOG_FUNCTION_NAME_EXIT; 2738 } 2739 2740 2741 /** 2742 @brief Dump state of the camera hardware 2743 2744 @param[in] fd File descriptor 2745 @param[in] args Arguments 2746 @return NO_ERROR Dump succeeded 2747 @todo Error codes for dump fail 2748 2749 */ 2750 status_t CameraHal::dump(int fd) const 2751 { 2752 LOG_FUNCTION_NAME; 2753 ///Implement this method when the h/w dump function is supported on Ducati side 2754 return NO_ERROR; 2755 } 2756 2757 /*-------------Camera Hal Interface Method definitions ENDS here--------------------*/ 2758 2759 2760 2761 2762 /*-------------Camera Hal Internal Method definitions STARTS here--------------------*/ 2763 2764 /** 2765 @brief Constructor of CameraHal 2766 2767 Member variables are initialized here. No allocations should be done here as we 2768 don't use c++ exceptions in the code. 2769 2770 */ 2771 CameraHal::CameraHal(int cameraId) 2772 { 2773 LOG_FUNCTION_NAME; 2774 2775 ///Initialize all the member variables to their defaults 2776 mPreviewEnabled = false; 2777 mPreviewBufs = NULL; 2778 mImageBufs = NULL; 2779 mBufProvider = NULL; 2780 mPreviewStartInProgress = false; 2781 mVideoBufs = NULL; 2782 mVideoBufProvider = NULL; 2783 mRecordingEnabled = false; 2784 mDisplayPaused = false; 2785 mSetPreviewWindowCalled = false; 2786 mMsgEnabled = 0; 2787 mAppCallbackNotifier = NULL; 2788 mMemoryManager = NULL; 2789 mCameraAdapter = NULL; 2790 mBracketingEnabled = false; 2791 mBracketingRunning = false; 2792 mEventProvider = NULL; 2793 mBracketRangePositive = 1; 2794 mBracketRangeNegative = 1; 2795 mMaxZoomSupported = 0; 2796 mShutterEnabled = true; 2797 mMeasurementEnabled = false; 2798 mPreviewDataBufs = NULL; 2799 mCameraProperties = NULL; 2800 mCurrentTime = 0; 2801 mFalsePreview = 0; 2802 mImageOffsets = NULL; 2803 mImageLength = 0; 2804 mImageFd = 0; 2805 mVideoOffsets = NULL; 2806 mVideoFd = 0; 2807 mVideoLength = 0; 2808 mPreviewDataOffsets = NULL; 2809 mPreviewDataFd = 0; 2810 mPreviewDataLength = 0; 2811 mPreviewFd = 0; 2812 mPreviewWidth = 0; 2813 mPreviewHeight = 0; 2814 mPreviewLength = 0; 2815 mPreviewOffsets = NULL; 2816 mPreviewRunning = 0; 2817 mPreviewStateOld = 0; 2818 mRecordingEnabled = 0; 2819 mRecordEnabled = 0; 2820 mSensorListener = NULL; 2821 mVideoWidth = 0; 2822 mVideoHeight = 0; 2823 2824 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 2825 2826 //Initialize the CameraHAL constructor timestamp, which is used in the 2827 // PPM() method as time reference if the user does not supply one. 2828 gettimeofday(&ppm_start, NULL); 2829 2830 #endif 2831 2832 mCameraIndex = cameraId; 2833 2834 LOG_FUNCTION_NAME_EXIT; 2835 } 2836 2837 /** 2838 @brief Destructor of CameraHal 2839 2840 This function simply calls deinitialize() to free up memory allocate during construct 2841 phase 2842 */ 2843 CameraHal::~CameraHal() 2844 { 2845 LOG_FUNCTION_NAME; 2846 2847 ///Call de-initialize here once more - it is the last chance for us to relinquish all the h/w and s/w resources 2848 deinitialize(); 2849 2850 if ( NULL != mEventProvider ) 2851 { 2852 mEventProvider->disableEventNotification(CameraHalEvent::ALL_EVENTS); 2853 delete mEventProvider; 2854 mEventProvider = NULL; 2855 } 2856 2857 /// Free the callback notifier 2858 mAppCallbackNotifier.clear(); 2859 2860 /// Free the display adapter 2861 mDisplayAdapter.clear(); 2862 2863 if ( NULL != mCameraAdapter ) { 2864 int strongCount = mCameraAdapter->getStrongCount(); 2865 2866 mCameraAdapter->decStrong(mCameraAdapter); 2867 2868 mCameraAdapter = NULL; 2869 } 2870 2871 freeImageBufs(); 2872 2873 /// Free the memory manager 2874 mMemoryManager.clear(); 2875 2876 LOG_FUNCTION_NAME_EXIT; 2877 } 2878 2879 /** 2880 @brief Initialize the Camera HAL 2881 2882 Creates CameraAdapter, AppCallbackNotifier, DisplayAdapter and MemoryManager 2883 2884 @param None 2885 @return NO_ERROR - On success 2886 NO_MEMORY - On failure to allocate memory for any of the objects 2887 @remarks Camera Hal internal function 2888 2889 */ 2890 2891 status_t CameraHal::initialize(CameraProperties::Properties* properties) 2892 { 2893 LOG_FUNCTION_NAME; 2894 2895 int sensor_index = 0; 2896 2897 ///Initialize the event mask used for registering an event provider for AppCallbackNotifier 2898 ///Currently, registering all events as to be coming from CameraAdapter 2899 int32_t eventMask = CameraHalEvent::ALL_EVENTS; 2900 2901 // Get my camera properties 2902 mCameraProperties = properties; 2903 2904 if(!mCameraProperties) 2905 { 2906 goto fail_loop; 2907 } 2908 2909 // Dump the properties of this Camera 2910 // will only print if DEBUG macro is defined 2911 mCameraProperties->dump(); 2912 2913 if (strcmp(CameraProperties::DEFAULT_VALUE, mCameraProperties->get(CameraProperties::CAMERA_SENSOR_INDEX)) != 0 ) 2914 { 2915 sensor_index = atoi(mCameraProperties->get(CameraProperties::CAMERA_SENSOR_INDEX)); 2916 } 2917 2918 CAMHAL_LOGDB("Sensor index %d", sensor_index); 2919 2920 mCameraAdapter = CameraAdapter_Factory(sensor_index); 2921 if ( ( NULL == mCameraAdapter ) || (mCameraAdapter->initialize(properties)!=NO_ERROR)) 2922 { 2923 CAMHAL_LOGEA("Unable to create or initialize CameraAdapter"); 2924 mCameraAdapter = NULL; 2925 goto fail_loop; 2926 } 2927 2928 mCameraAdapter->incStrong(mCameraAdapter); 2929 mCameraAdapter->registerImageReleaseCallback(releaseImageBuffers, (void *) this); 2930 mCameraAdapter->registerEndCaptureCallback(endImageCapture, (void *)this); 2931 2932 if(!mAppCallbackNotifier.get()) 2933 { 2934 /// Create the callback notifier 2935 mAppCallbackNotifier = new AppCallbackNotifier(); 2936 if( ( NULL == mAppCallbackNotifier.get() ) || ( mAppCallbackNotifier->initialize() != NO_ERROR)) 2937 { 2938 CAMHAL_LOGEA("Unable to create or initialize AppCallbackNotifier"); 2939 goto fail_loop; 2940 } 2941 } 2942 2943 if(!mMemoryManager.get()) 2944 { 2945 /// Create Memory Manager 2946 mMemoryManager = new MemoryManager(); 2947 if( ( NULL == mMemoryManager.get() ) || ( mMemoryManager->initialize() != NO_ERROR)) 2948 { 2949 CAMHAL_LOGEA("Unable to create or initialize MemoryManager"); 2950 goto fail_loop; 2951 } 2952 } 2953 2954 ///Setup the class dependencies... 2955 2956 ///AppCallbackNotifier has to know where to get the Camera frames and the events like auto focus lock etc from. 2957 ///CameraAdapter is the one which provides those events 2958 ///Set it as the frame and event providers for AppCallbackNotifier 2959 ///@remarks setEventProvider API takes in a bit mask of events for registering a provider for the different events 2960 /// That way, if events can come from DisplayAdapter in future, we will be able to add it as provider 2961 /// for any event 2962 mAppCallbackNotifier->setEventProvider(eventMask, mCameraAdapter); 2963 mAppCallbackNotifier->setFrameProvider(mCameraAdapter); 2964 2965 ///Any dynamic errors that happen during the camera use case has to be propagated back to the application 2966 ///via CAMERA_MSG_ERROR. AppCallbackNotifier is the class that notifies such errors to the application 2967 ///Set it as the error handler for CameraAdapter 2968 mCameraAdapter->setErrorHandler(mAppCallbackNotifier.get()); 2969 2970 ///Start the callback notifier 2971 if(mAppCallbackNotifier->start() != NO_ERROR) 2972 { 2973 CAMHAL_LOGEA("Couldn't start AppCallbackNotifier"); 2974 goto fail_loop; 2975 } 2976 2977 CAMHAL_LOGDA("Started AppCallbackNotifier.."); 2978 mAppCallbackNotifier->setMeasurements(mMeasurementEnabled); 2979 2980 ///Initialize default parameters 2981 initDefaultParameters(); 2982 2983 2984 if ( setParameters(mParameters) != NO_ERROR ) 2985 { 2986 CAMHAL_LOGEA("Failed to set default parameters?!"); 2987 } 2988 2989 // register for sensor events 2990 mSensorListener = new SensorListener(); 2991 if (mSensorListener.get()) { 2992 if (mSensorListener->initialize() == NO_ERROR) { 2993 mSensorListener->setCallbacks(orientation_cb, this); 2994 mSensorListener->enableSensor(SensorListener::SENSOR_ORIENTATION); 2995 } else { 2996 CAMHAL_LOGEA("Error initializing SensorListener. not fatal, continuing"); 2997 mSensorListener.clear(); 2998 mSensorListener = NULL; 2999 } 3000 } 3001 3002 LOG_FUNCTION_NAME_EXIT; 3003 3004 return NO_ERROR; 3005 3006 fail_loop: 3007 3008 ///Free up the resources because we failed somewhere up 3009 deinitialize(); 3010 LOG_FUNCTION_NAME_EXIT; 3011 3012 return NO_MEMORY; 3013 3014 } 3015 3016 bool CameraHal::isResolutionValid(unsigned int width, unsigned int height, const char *supportedResolutions) 3017 { 3018 bool ret = true; 3019 status_t status = NO_ERROR; 3020 char tmpBuffer[PARAM_BUFFER + 1]; 3021 char *pos = NULL; 3022 3023 LOG_FUNCTION_NAME; 3024 3025 if ( NULL == supportedResolutions ) 3026 { 3027 CAMHAL_LOGEA("Invalid supported resolutions string"); 3028 ret = false; 3029 goto exit; 3030 } 3031 3032 status = snprintf(tmpBuffer, PARAM_BUFFER, "%dx%d", width, height); 3033 if ( 0 > status ) 3034 { 3035 CAMHAL_LOGEA("Error encountered while generating validation string"); 3036 ret = false; 3037 goto exit; 3038 } 3039 3040 pos = strstr(supportedResolutions, tmpBuffer); 3041 if ( NULL == pos ) 3042 { 3043 ret = false; 3044 } 3045 else 3046 { 3047 ret = true; 3048 } 3049 3050 exit: 3051 3052 LOG_FUNCTION_NAME_EXIT; 3053 3054 return ret; 3055 } 3056 3057 bool CameraHal::isParameterValid(const char *param, const char *supportedParams) 3058 { 3059 bool ret = true; 3060 char *pos = NULL; 3061 3062 LOG_FUNCTION_NAME; 3063 3064 if ( NULL == supportedParams ) 3065 { 3066 CAMHAL_LOGEA("Invalid supported parameters string"); 3067 ret = false; 3068 goto exit; 3069 } 3070 3071 if ( NULL == param ) 3072 { 3073 CAMHAL_LOGEA("Invalid parameter string"); 3074 ret = false; 3075 goto exit; 3076 } 3077 3078 pos = strstr(supportedParams, param); 3079 if ( NULL == pos ) 3080 { 3081 ret = false; 3082 } 3083 else 3084 { 3085 ret = true; 3086 } 3087 3088 exit: 3089 3090 LOG_FUNCTION_NAME_EXIT; 3091 3092 return ret; 3093 } 3094 3095 bool CameraHal::isParameterValid(int param, const char *supportedParams) 3096 { 3097 bool ret = true; 3098 char *pos = NULL; 3099 status_t status; 3100 char tmpBuffer[PARAM_BUFFER + 1]; 3101 3102 LOG_FUNCTION_NAME; 3103 3104 if ( NULL == supportedParams ) 3105 { 3106 CAMHAL_LOGEA("Invalid supported parameters string"); 3107 ret = false; 3108 goto exit; 3109 } 3110 3111 status = snprintf(tmpBuffer, PARAM_BUFFER, "%d", param); 3112 if ( 0 > status ) 3113 { 3114 CAMHAL_LOGEA("Error encountered while generating validation string"); 3115 ret = false; 3116 goto exit; 3117 } 3118 3119 pos = strstr(supportedParams, tmpBuffer); 3120 if ( NULL == pos ) 3121 { 3122 ret = false; 3123 } 3124 else 3125 { 3126 ret = true; 3127 } 3128 3129 exit: 3130 3131 LOG_FUNCTION_NAME_EXIT; 3132 3133 return ret; 3134 } 3135 3136 status_t CameraHal::doesSetParameterNeedUpdate(const char* new_param, const char* old_param, bool& update) { 3137 if (!new_param || !old_param) { 3138 return -EINVAL; 3139 } 3140 3141 // if params mismatch we should update parameters for camera adapter 3142 if ((strcmp(new_param, old_param) != 0)) { 3143 update = true; 3144 } 3145 3146 return NO_ERROR; 3147 } 3148 3149 status_t CameraHal::parseResolution(const char *resStr, int &width, int &height) 3150 { 3151 status_t ret = NO_ERROR; 3152 char *ctx, *pWidth, *pHeight; 3153 const char *sep = "x"; 3154 char *tmp = NULL; 3155 3156 LOG_FUNCTION_NAME; 3157 3158 if ( NULL == resStr ) 3159 { 3160 return -EINVAL; 3161 } 3162 3163 //This fixes "Invalid input resolution" 3164 char *resStr_copy = (char *)malloc(strlen(resStr) + 1); 3165 if ( NULL!=resStr_copy ) { 3166 if ( NO_ERROR == ret ) 3167 { 3168 strcpy(resStr_copy, resStr); 3169 pWidth = strtok_r( (char *) resStr_copy, sep, &ctx); 3170 3171 if ( NULL != pWidth ) 3172 { 3173 width = atoi(pWidth); 3174 } 3175 else 3176 { 3177 CAMHAL_LOGEB("Invalid input resolution %s", resStr); 3178 ret = -EINVAL; 3179 } 3180 } 3181 3182 if ( NO_ERROR == ret ) 3183 { 3184 pHeight = strtok_r(NULL, sep, &ctx); 3185 3186 if ( NULL != pHeight ) 3187 { 3188 height = atoi(pHeight); 3189 } 3190 else 3191 { 3192 CAMHAL_LOGEB("Invalid input resolution %s", resStr); 3193 ret = -EINVAL; 3194 } 3195 } 3196 3197 free(resStr_copy); 3198 resStr_copy = NULL; 3199 } 3200 LOG_FUNCTION_NAME_EXIT; 3201 3202 return ret; 3203 } 3204 3205 void CameraHal::insertSupportedParams() 3206 { 3207 char tmpBuffer[PARAM_BUFFER + 1]; 3208 3209 LOG_FUNCTION_NAME; 3210 3211 CameraParameters &p = mParameters; 3212 3213 ///Set the name of the camera 3214 p.set(TICameraParameters::KEY_CAMERA_NAME, mCameraProperties->get(CameraProperties::CAMERA_NAME)); 3215 3216 mMaxZoomSupported = atoi(mCameraProperties->get(CameraProperties::SUPPORTED_ZOOM_STAGES)); 3217 3218 p.set(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES, mCameraProperties->get(CameraProperties::SUPPORTED_PICTURE_SIZES)); 3219 p.set(CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS, mCameraProperties->get(CameraProperties::SUPPORTED_PICTURE_FORMATS)); 3220 p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES, mCameraProperties->get(CameraProperties::SUPPORTED_PREVIEW_SIZES)); 3221 p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS, mCameraProperties->get(CameraProperties::SUPPORTED_PREVIEW_FORMATS)); 3222 p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES, mCameraProperties->get(CameraProperties::SUPPORTED_PREVIEW_FRAME_RATES)); 3223 p.set(CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES, mCameraProperties->get(CameraProperties::SUPPORTED_THUMBNAIL_SIZES)); 3224 p.set(CameraParameters::KEY_SUPPORTED_WHITE_BALANCE, mCameraProperties->get(CameraProperties::SUPPORTED_WHITE_BALANCE)); 3225 p.set(CameraParameters::KEY_SUPPORTED_EFFECTS, mCameraProperties->get(CameraProperties::SUPPORTED_EFFECTS)); 3226 p.set(CameraParameters::KEY_SUPPORTED_SCENE_MODES, mCameraProperties->get(CameraProperties::SUPPORTED_SCENE_MODES)); 3227 p.set(CameraParameters::KEY_SUPPORTED_FLASH_MODES, mCameraProperties->get(CameraProperties::SUPPORTED_FLASH_MODES)); 3228 p.set(CameraParameters::KEY_SUPPORTED_FOCUS_MODES, mCameraProperties->get(CameraProperties::SUPPORTED_FOCUS_MODES)); 3229 p.set(CameraParameters::KEY_SUPPORTED_ANTIBANDING, mCameraProperties->get(CameraProperties::SUPPORTED_ANTIBANDING)); 3230 p.set(CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION, mCameraProperties->get(CameraProperties::SUPPORTED_EV_MAX)); 3231 p.set(CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION, mCameraProperties->get(CameraProperties::SUPPORTED_EV_MIN)); 3232 p.set(CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP, mCameraProperties->get(CameraProperties::SUPPORTED_EV_STEP)); 3233 p.set(CameraParameters::KEY_SUPPORTED_SCENE_MODES, mCameraProperties->get(CameraProperties::SUPPORTED_SCENE_MODES)); 3234 p.set(TICameraParameters::KEY_SUPPORTED_EXPOSURE, mCameraProperties->get(CameraProperties::SUPPORTED_EXPOSURE_MODES)); 3235 p.set(TICameraParameters::KEY_SUPPORTED_ISO_VALUES, mCameraProperties->get(CameraProperties::SUPPORTED_ISO_VALUES)); 3236 p.set(CameraParameters::KEY_ZOOM_RATIOS, mCameraProperties->get(CameraProperties::SUPPORTED_ZOOM_RATIOS)); 3237 p.set(CameraParameters::KEY_MAX_ZOOM, mCameraProperties->get(CameraProperties::SUPPORTED_ZOOM_STAGES)); 3238 p.set(CameraParameters::KEY_ZOOM_SUPPORTED, mCameraProperties->get(CameraProperties::ZOOM_SUPPORTED)); 3239 p.set(CameraParameters::KEY_SMOOTH_ZOOM_SUPPORTED, mCameraProperties->get(CameraProperties::SMOOTH_ZOOM_SUPPORTED)); 3240 p.set(TICameraParameters::KEY_SUPPORTED_IPP, mCameraProperties->get(CameraProperties::SUPPORTED_IPP_MODES)); 3241 p.set(TICameraParameters::KEY_S3D_SUPPORTED,mCameraProperties->get(CameraProperties::S3D_SUPPORTED)); 3242 p.set(TICameraParameters::KEY_S3D2D_PREVIEW_MODE,mCameraProperties->get(CameraProperties::S3D2D_PREVIEW_MODES)); 3243 p.set(TICameraParameters::KEY_AUTOCONVERGENCE_MODE, mCameraProperties->get(CameraProperties::AUTOCONVERGENCE_MODE)); 3244 p.set(TICameraParameters::KEY_MANUALCONVERGENCE_VALUES, mCameraProperties->get(CameraProperties::MANUALCONVERGENCE_VALUES)); 3245 p.set(CameraParameters::KEY_VIDEO_STABILIZATION_SUPPORTED, mCameraProperties->get(CameraProperties::VSTAB_SUPPORTED)); 3246 p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FPS_RANGE, mCameraProperties->get(CameraProperties::FRAMERATE_RANGE_SUPPORTED)); 3247 p.set(TICameraParameters::KEY_SENSOR_ORIENTATION, mCameraProperties->get(CameraProperties::SENSOR_ORIENTATION)); 3248 p.set(TICameraParameters::KEY_SENSOR_ORIENTATION_VALUES, mCameraProperties->get(CameraProperties::SENSOR_ORIENTATION_VALUES)); 3249 p.set(CameraParameters::KEY_AUTO_EXPOSURE_LOCK_SUPPORTED, mCameraProperties->get(CameraProperties::AUTO_EXPOSURE_LOCK_SUPPORTED)); 3250 p.set(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED, mCameraProperties->get(CameraProperties::AUTO_WHITEBALANCE_LOCK_SUPPORTED)); 3251 p.set(CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED, mCameraProperties->get(CameraProperties::VIDEO_SNAPSHOT_SUPPORTED)); 3252 3253 LOG_FUNCTION_NAME_EXIT; 3254 3255 } 3256 3257 void CameraHal::initDefaultParameters() 3258 { 3259 //Purpose of this function is to initialize the default current and supported parameters for the currently 3260 //selected camera. 3261 3262 CameraParameters &p = mParameters; 3263 int currentRevision, adapterRevision; 3264 status_t ret = NO_ERROR; 3265 int width, height; 3266 3267 LOG_FUNCTION_NAME; 3268 3269 ret = parseResolution(mCameraProperties->get(CameraProperties::PREVIEW_SIZE), width, height); 3270 3271 if ( NO_ERROR == ret ) 3272 { 3273 p.setPreviewSize(width, height); 3274 } 3275 else 3276 { 3277 p.setPreviewSize(MIN_WIDTH, MIN_HEIGHT); 3278 } 3279 3280 ret = parseResolution(mCameraProperties->get(CameraProperties::PICTURE_SIZE), width, height); 3281 3282 if ( NO_ERROR == ret ) 3283 { 3284 p.setPictureSize(width, height); 3285 } 3286 else 3287 { 3288 p.setPictureSize(PICTURE_WIDTH, PICTURE_HEIGHT); 3289 } 3290 3291 ret = parseResolution(mCameraProperties->get(CameraProperties::JPEG_THUMBNAIL_SIZE), width, height); 3292 3293 if ( NO_ERROR == ret ) 3294 { 3295 p.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH, width); 3296 p.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT, height); 3297 } 3298 else 3299 { 3300 p.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH, MIN_WIDTH); 3301 p.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT, MIN_HEIGHT); 3302 } 3303 3304 insertSupportedParams(); 3305 3306 //Insert default values 3307 p.setPreviewFrameRate(atoi(mCameraProperties->get(CameraProperties::PREVIEW_FRAME_RATE))); 3308 p.setPreviewFormat(mCameraProperties->get(CameraProperties::PREVIEW_FORMAT)); 3309 p.setPictureFormat(mCameraProperties->get(CameraProperties::PICTURE_FORMAT)); 3310 p.set(CameraParameters::KEY_JPEG_QUALITY, mCameraProperties->get(CameraProperties::JPEG_QUALITY)); 3311 p.set(CameraParameters::KEY_WHITE_BALANCE, mCameraProperties->get(CameraProperties::WHITEBALANCE)); 3312 p.set(CameraParameters::KEY_EFFECT, mCameraProperties->get(CameraProperties::EFFECT)); 3313 p.set(CameraParameters::KEY_ANTIBANDING, mCameraProperties->get(CameraProperties::ANTIBANDING)); 3314 p.set(CameraParameters::KEY_FLASH_MODE, mCameraProperties->get(CameraProperties::FLASH_MODE)); 3315 p.set(CameraParameters::KEY_FOCUS_MODE, mCameraProperties->get(CameraProperties::FOCUS_MODE)); 3316 p.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, mCameraProperties->get(CameraProperties::EV_COMPENSATION)); 3317 p.set(CameraParameters::KEY_SCENE_MODE, mCameraProperties->get(CameraProperties::SCENE_MODE)); 3318 p.set(CameraParameters::KEY_FLASH_MODE, mCameraProperties->get(CameraProperties::FLASH_MODE)); 3319 p.set(CameraParameters::KEY_ZOOM, mCameraProperties->get(CameraProperties::ZOOM)); 3320 p.set(TICameraParameters::KEY_CONTRAST, mCameraProperties->get(CameraProperties::CONTRAST)); 3321 p.set(TICameraParameters::KEY_SATURATION, mCameraProperties->get(CameraProperties::SATURATION)); 3322 p.set(TICameraParameters::KEY_BRIGHTNESS, mCameraProperties->get(CameraProperties::BRIGHTNESS)); 3323 p.set(TICameraParameters::KEY_SHARPNESS, mCameraProperties->get(CameraProperties::SHARPNESS)); 3324 p.set(TICameraParameters::KEY_EXPOSURE_MODE, mCameraProperties->get(CameraProperties::EXPOSURE_MODE)); 3325 p.set(TICameraParameters::KEY_ISO, mCameraProperties->get(CameraProperties::ISO_MODE)); 3326 p.set(TICameraParameters::KEY_IPP, mCameraProperties->get(CameraProperties::IPP)); 3327 p.set(TICameraParameters::KEY_GBCE, mCameraProperties->get(CameraProperties::GBCE)); 3328 p.set(TICameraParameters::KEY_S3D2D_PREVIEW, mCameraProperties->get(CameraProperties::S3D2D_PREVIEW)); 3329 p.set(TICameraParameters::KEY_AUTOCONVERGENCE, mCameraProperties->get(CameraProperties::AUTOCONVERGENCE)); 3330 p.set(TICameraParameters::KEY_MANUALCONVERGENCE_VALUES, mCameraProperties->get(CameraProperties::MANUALCONVERGENCE_VALUES)); 3331 p.set(CameraParameters::KEY_VIDEO_STABILIZATION, mCameraProperties->get(CameraProperties::VSTAB)); 3332 p.set(CameraParameters::KEY_FOCAL_LENGTH, mCameraProperties->get(CameraProperties::FOCAL_LENGTH)); 3333 p.set(CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE, mCameraProperties->get(CameraProperties::HOR_ANGLE)); 3334 p.set(CameraParameters::KEY_VERTICAL_VIEW_ANGLE, mCameraProperties->get(CameraProperties::VER_ANGLE)); 3335 p.set(CameraParameters::KEY_PREVIEW_FPS_RANGE,mCameraProperties->get(CameraProperties::FRAMERATE_RANGE)); 3336 p.set(TICameraParameters::KEY_SENSOR_ORIENTATION, mCameraProperties->get(CameraProperties::SENSOR_ORIENTATION)); 3337 p.set(TICameraParameters::KEY_SENSOR_ORIENTATION_VALUES, mCameraProperties->get(CameraProperties::SENSOR_ORIENTATION_VALUES)); 3338 p.set(TICameraParameters::KEY_EXIF_MAKE, mCameraProperties->get(CameraProperties::EXIF_MAKE)); 3339 p.set(TICameraParameters::KEY_EXIF_MODEL, mCameraProperties->get(CameraProperties::EXIF_MODEL)); 3340 p.set(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY, mCameraProperties->get(CameraProperties::JPEG_THUMBNAIL_QUALITY)); 3341 p.set(CameraParameters::KEY_VIDEO_FRAME_FORMAT, "OMX_TI_COLOR_FormatYUV420PackedSemiPlanar"); 3342 p.set(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_HW, mCameraProperties->get(CameraProperties::MAX_FD_HW_FACES)); 3343 p.set(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_SW, mCameraProperties->get(CameraProperties::MAX_FD_SW_FACES)); 3344 3345 // Only one area a.k.a Touch AF for now. 3346 // TODO: Add support for multiple focus areas. 3347 p.set(CameraParameters::KEY_MAX_NUM_FOCUS_AREAS, mCameraProperties->get(CameraProperties::MAX_FOCUS_AREAS)); 3348 p.set(CameraParameters::KEY_AUTO_EXPOSURE_LOCK, mCameraProperties->get(CameraProperties::AUTO_EXPOSURE_LOCK)); 3349 p.set(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK, mCameraProperties->get(CameraProperties::AUTO_WHITEBALANCE_LOCK)); 3350 p.set(CameraParameters::KEY_MAX_NUM_METERING_AREAS, mCameraProperties->get(CameraProperties::MAX_NUM_METERING_AREAS)); 3351 3352 LOG_FUNCTION_NAME_EXIT; 3353 } 3354 3355 /** 3356 @brief Stop a previously started preview. 3357 @param none 3358 @return none 3359 3360 */ 3361 void CameraHal::forceStopPreview() 3362 { 3363 LOG_FUNCTION_NAME; 3364 3365 // stop bracketing if it is running 3366 stopImageBracketing(); 3367 3368 if(mDisplayAdapter.get() != NULL) { 3369 ///Stop the buffer display first 3370 mDisplayAdapter->disableDisplay(); 3371 } 3372 3373 if(mAppCallbackNotifier.get() != NULL) { 3374 //Stop the callback sending 3375 mAppCallbackNotifier->stop(); 3376 mAppCallbackNotifier->flushAndReturnFrames(); 3377 mAppCallbackNotifier->stopPreviewCallbacks(); 3378 } 3379 3380 if ( NULL != mCameraAdapter ) { 3381 // only need to send these control commands to state machine if we are 3382 // passed the LOADED_PREVIEW_STATE 3383 if (mCameraAdapter->getState() > CameraAdapter::LOADED_PREVIEW_STATE) { 3384 // according to javadoc...FD should be stopped in stopPreview 3385 // and application needs to call startFaceDection again 3386 // to restart FD 3387 mCameraAdapter->sendCommand(CameraAdapter::CAMERA_STOP_FD); 3388 } 3389 3390 mCameraAdapter->rollbackToInitializedState(); 3391 3392 } 3393 3394 freePreviewBufs(); 3395 freePreviewDataBufs(); 3396 3397 mPreviewEnabled = false; 3398 mDisplayPaused = false; 3399 mPreviewStartInProgress = false; 3400 3401 LOG_FUNCTION_NAME_EXIT; 3402 } 3403 3404 /** 3405 @brief Deallocates memory for all the resources held by Camera HAL. 3406 3407 Frees the following objects- CameraAdapter, AppCallbackNotifier, DisplayAdapter, 3408 and Memory Manager 3409 3410 @param none 3411 @return none 3412 3413 */ 3414 void CameraHal::deinitialize() 3415 { 3416 LOG_FUNCTION_NAME; 3417 3418 if ( mPreviewEnabled || mDisplayPaused ) { 3419 forceStopPreview(); 3420 } 3421 3422 mSetPreviewWindowCalled = false; 3423 3424 if (mSensorListener.get()) { 3425 mSensorListener->disableSensor(SensorListener::SENSOR_ORIENTATION); 3426 mSensorListener.clear(); 3427 mSensorListener = NULL; 3428 } 3429 3430 LOG_FUNCTION_NAME_EXIT; 3431 3432 } 3433 3434 status_t CameraHal::storeMetaDataInBuffers(bool enable) 3435 { 3436 LOG_FUNCTION_NAME; 3437 3438 return mAppCallbackNotifier->useMetaDataBufferMode(enable); 3439 3440 LOG_FUNCTION_NAME_EXIT; 3441 } 3442 3443 void CameraHal::selectFPSRange(int framerate, int *min_fps, int *max_fps) 3444 { 3445 char * ptr; 3446 char supported[MAX_PROP_VALUE_LENGTH]; 3447 int fpsrangeArray[2]; 3448 int i = 0; 3449 3450 LOG_FUNCTION_NAME; 3451 size_t size = strlen(mCameraProperties->get(CameraProperties::FRAMERATE_RANGE_SUPPORTED))+1; 3452 strncpy(supported, mCameraProperties->get(CameraProperties::FRAMERATE_RANGE_SUPPORTED), size); 3453 3454 ptr = strtok (supported," (,)"); 3455 3456 while (ptr != NULL) 3457 { 3458 fpsrangeArray[i]= atoi(ptr)/CameraHal::VFR_SCALE; 3459 if (i == 1) 3460 { 3461 if (framerate == fpsrangeArray[i]) 3462 { 3463 CAMHAL_LOGDB("SETTING FPS RANGE min = %d max = %d \n", fpsrangeArray[0], fpsrangeArray[1]); 3464 *min_fps = fpsrangeArray[0]*CameraHal::VFR_SCALE; 3465 *max_fps = fpsrangeArray[1]*CameraHal::VFR_SCALE; 3466 break; 3467 } 3468 } 3469 ptr = strtok (NULL, " (,)"); 3470 i++; 3471 i%=2; 3472 } 3473 3474 LOG_FUNCTION_NAME_EXIT; 3475 3476 } 3477 3478 void CameraHal::setPreferredPreviewRes(int width, int height) 3479 { 3480 LOG_FUNCTION_NAME; 3481 3482 if ( (width == 320) && (height == 240)){ 3483 mParameters.setPreviewSize(640,480); 3484 } 3485 if ( (width == 176) && (height == 144)){ 3486 mParameters.setPreviewSize(704,576); 3487 } 3488 3489 LOG_FUNCTION_NAME_EXIT; 3490 } 3491 3492 void CameraHal::resetPreviewRes(CameraParameters *mParams, int width, int height) 3493 { 3494 LOG_FUNCTION_NAME; 3495 3496 if ( (width <= 320) && (height <= 240)){ 3497 mParams->setPreviewSize(mVideoWidth, mVideoHeight); 3498 } 3499 3500 LOG_FUNCTION_NAME_EXIT; 3501 } 3502 3503 }; 3504 3505 3506