OMXCameraAdapter.cpp revision 70e04f3b54d5100621ffa631dbc81579d2cc3c8c
1c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*
2c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * Copyright (C) Texas Instruments - http://www.ti.com/
3c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev *
4c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * Licensed under the Apache License, Version 2.0 (the "License");
5c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * you may not use this file except in compliance with the License.
6c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * You may obtain a copy of the License at
7c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev *
8c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev *      http://www.apache.org/licenses/LICENSE-2.0
9c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev *
10c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * Unless required by applicable law or agreed to in writing, software
11c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * distributed under the License is distributed on an "AS IS" BASIS,
12c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * See the License for the specific language governing permissions and
14c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * limitations under the License.
15c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev */
16c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
17c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/**
18c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev* @file OMXCameraAdapter.cpp
19c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev*
20c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev* This file maps the Camera Hardware Interface to OMX.
21c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev*
22c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev*/
23c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
24c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include "CameraHal.h"
25c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include "OMXCameraAdapter.h"
26c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include "ErrorUtils.h"
27c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include "TICameraParameters.h"
28c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <signal.h>
29c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <math.h>
30c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
31c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <cutils/properties.h>
32c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
33c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatic int mDebugFps = 0;
34c493114006e0e136c8c88c2fc9865994054959bfSundar Ramanstatic int mDebugFcs = 0;
35c493114006e0e136c8c88c2fc9865994054959bfSundar Raman
36c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
37c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#define HERE(Msg) {CAMHAL_LOGEB("--===line %d, %s===--\n", __LINE__, Msg);}
38c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
39c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevnamespace android {
40c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
41c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#undef LOG_TAG
42c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev///Maintain a separate tag for OMXCameraAdapter logs to isolate issues OMX specific
43c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#define LOG_TAG "CameraHAL"
44c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
45c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev//frames skipped before recalculating the framerate
46c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#define FPS_PERIOD 30
47c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
48c322989ae6ff6769490828de1b5eda12b749cce9Iliyan MalchevMutex gAdapterLock;
49c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*--------------------Camera Adapter Class STARTS here-----------------------------*/
50c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
519a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luustatus_t OMXCameraAdapter::initialize(CameraProperties::Properties* caps)
52c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
53c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
54c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
55c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    char value[PROPERTY_VALUE_MAX];
56c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    property_get("debug.camera.showfps", value, "0");
57c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mDebugFps = atoi(value);
58c493114006e0e136c8c88c2fc9865994054959bfSundar Raman    property_get("debug.camera.framecounts", value, "0");
59c493114006e0e136c8c88c2fc9865994054959bfSundar Raman    mDebugFcs = atoi(value);
60c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
61c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    TIMM_OSAL_ERRORTYPE osalError = OMX_ErrorNone;
62c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
63c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
64c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
65c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
66c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mLocalVersionParam.s.nVersionMajor = 0x1;
67c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mLocalVersionParam.s.nVersionMinor = 0x1;
68c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mLocalVersionParam.s.nRevision = 0x0 ;
69c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mLocalVersionParam.s.nStep =  0x0;
70c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
71c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mPending3Asettings = 0;//E3AsettingsAll;
72b5abb7cbdf48ecccbf0e6d5e61a1846c0c4b28a5Tyler Luu    mPendingCaptureSettings = 0;
73c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
74c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( 0 != mInitSem.Count() )
75c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
76c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error mInitSem semaphore count %d", mInitSem.Count());
77c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOG_FUNCTION_NAME_EXIT;
78c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return NO_INIT;
79c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
80c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
81c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (mComponentState != OMX_StateLoaded && mComponentState != OMX_StateInvalid) {
8241471bd18fc9a28371c4d89283f2082b513f2226Iliyan Malchev       CAMHAL_LOGEB("Error mComponentState %d is invalid!", mComponentState);
83c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev       LOG_FUNCTION_NAME_EXIT;
84c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev       return NO_INIT;
85c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
86c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
879a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    ///Update the preview and image capture port indexes
889a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mCameraAdapterParameters.mPrevPortIndex = OMX_CAMERA_PORT_VIDEO_OUT_PREVIEW;
899a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    // temp changed in order to build OMX_CAMERA_PORT_VIDEO_OUT_IMAGE;
909a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mCameraAdapterParameters.mImagePortIndex = OMX_CAMERA_PORT_IMAGE_OUT_IMAGE;
919a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mCameraAdapterParameters.mMeasurementPortIndex = OMX_CAMERA_PORT_VIDEO_OUT_MEASUREMENT;
929a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    //currently not supported use preview port instead
939a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mCameraAdapterParameters.mVideoPortIndex = OMX_CAMERA_PORT_VIDEO_OUT_PREVIEW;
949a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu
959a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    ///Get the handle to the OMX Component
969a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    eError = OMXCameraAdapter::OMXCameraGetHandle(&mCameraAdapterParameters.mHandleComp, (OMX_PTR)this);
979a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if(eError != OMX_ErrorNone) {
989a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        CAMHAL_LOGEB("OMX_GetHandle -0x%x", eError);
999a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    }
1009a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    GOTO_EXIT_IF((eError != OMX_ErrorNone), eError);
101c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
102e552120201f0e53a6e5c9f816e91739dc4ec1e0bTyler Luu    CAMHAL_LOGVB("OMX_GetHandle -0x%x sensor_index = %lu", eError, mSensorIndex);
1039a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    eError = OMX_SendCommand(mCameraAdapterParameters.mHandleComp,
1049a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                                  OMX_CommandPortDisable,
1059a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                                  OMX_ALL,
1069a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                                  NULL);
107c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1089a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if(eError != OMX_ErrorNone) {
1099a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu         CAMHAL_LOGEB("OMX_SendCommand(OMX_CommandPortDisable) -0x%x", eError);
1109a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    }
1119a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    GOTO_EXIT_IF((eError != OMX_ErrorNone), eError);
112c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1139a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    // Register for port enable event
1149a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
1159a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                                 OMX_EventCmdComplete,
1169a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                                 OMX_CommandPortEnable,
1179a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                                 mCameraAdapterParameters.mPrevPortIndex,
1189a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                                 mInitSem);
1199a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if(ret != NO_ERROR) {
1209a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu         CAMHAL_LOGEB("Error in registering for event %d", ret);
1219a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu         goto EXIT;
1229a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    }
123c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1249a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    // Enable PREVIEW Port
1259a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    eError = OMX_SendCommand(mCameraAdapterParameters.mHandleComp,
1269a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                                 OMX_CommandPortEnable,
1279a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                                 mCameraAdapterParameters.mPrevPortIndex,
1289a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                                 NULL);
1299a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if(eError != OMX_ErrorNone) {
1309a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        CAMHAL_LOGEB("OMX_SendCommand(OMX_CommandPortEnable) -0x%x", eError);
1319a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    }
1329a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
133c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1349a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    // Wait for the port enable event to occur
1359a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    ret = mInitSem.WaitTimeout(OMX_CMD_TIMEOUT);
1369a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if ( NO_ERROR == ret ) {
1379a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu         CAMHAL_LOGDA("-Port enable event arrived");
1389a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    } else {
1399a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu         ret |= RemoveEvent(mCameraAdapterParameters.mHandleComp,
1409a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                            OMX_EventCmdComplete,
1419a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                            OMX_CommandPortEnable,
1429a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                            mCameraAdapterParameters.mPrevPortIndex,
1439a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                            NULL);
1449a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu         CAMHAL_LOGEA("Timeout for enabling preview port expired!");
1459a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu         goto EXIT;
1469a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu     }
147c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1489a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    // Select the sensor
149c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_CONFIG_SENSORSELECTTYPE sensorSelect;
150c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_INIT_STRUCT_PTR (&sensorSelect, OMX_CONFIG_SENSORSELECTTYPE);
1519a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    sensorSelect.eSensor = (OMX_SENSORSELECT) mSensorIndex;
152c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    eError = OMX_SetConfig(mCameraAdapterParameters.mHandleComp, ( OMX_INDEXTYPE ) OMX_TI_IndexConfigSensorSelect, &sensorSelect);
1539a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if ( OMX_ErrorNone != eError ) {
1549a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        CAMHAL_LOGEB("Error while selecting the sensor index as %d - 0x%x", mSensorIndex, eError);
155c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return BAD_VALUE;
1569a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    } else {
1579a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        CAMHAL_LOGDB("Sensor %d selected successfully", mSensorIndex);
1589a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    }
159c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
160c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    printComponentVersion(mCameraAdapterParameters.mHandleComp);
161c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
162c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mBracketingEnabled = false;
163c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mBracketingBuffersQueuedCount = 0;
164c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mBracketingRange = 1;
165c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mLastBracetingBufferIdx = 0;
166c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mOMXStateSwitch = false;
167c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1689a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mCaptureSignalled = false;
1699a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mCaptureConfigured = false;
1709a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mRecording = false;
1719a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mWaitingForSnapshot = false;
1729a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mSnapshotCount = 0;
1739a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mComponentState = OMX_StateLoaded;
174c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1759a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mCapMode = HIGH_QUALITY;
1769a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mIPP = IPP_NULL;
1779a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mVstabEnabled = false;
1789a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mVnfEnabled = false;
1799a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mBurstFrames = 1;
1809a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mCapturedFrames = 0;
1819a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mPictureQuality = 100;
1829a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mCurrentZoomIdx = 0;
1839a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mTargetZoomIdx = 0;
1849a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mPreviousZoomIndx = 0;
1859a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mReturnZoomStatus = false;
1869a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mZoomInc = 1;
1879a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mZoomParameterIdx = 0;
1889a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mExposureBracketingValidEntries = 0;
1899a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mSensorOverclock = false;
1909a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu
1919a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mDeviceOrientation = 0;
1929a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mCapabilities = caps;
1931c7383822ebd9f8e70d7eec6dd204bb691d29c47Sundar Raman    mZoomUpdating = false;
1941c7383822ebd9f8e70d7eec6dd204bb691d29c47Sundar Raman    mZoomUpdate = false;
1959a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu
1969a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mEXIFData.mGPSData.mAltitudeValid = false;
1979a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mEXIFData.mGPSData.mDatestampValid = false;
1989a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mEXIFData.mGPSData.mLatValid = false;
1999a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mEXIFData.mGPSData.mLongValid = false;
2009a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mEXIFData.mGPSData.mMapDatumValid = false;
2019a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mEXIFData.mGPSData.mProcMethodValid = false;
2029a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mEXIFData.mGPSData.mVersionIdValid = false;
2039a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mEXIFData.mGPSData.mTimeStampValid = false;
2049a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mEXIFData.mModelValid = false;
2059a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mEXIFData.mMakeValid = false;
2069a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu
2079a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    // initialize command handling thread
2089a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if(mCommandHandler.get() == NULL)
2099a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        mCommandHandler = new CommandHandler(this);
2109a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu
2119a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if ( NULL == mCommandHandler.get() )
2129a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    {
2139a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        CAMHAL_LOGEA("Couldn't create command handler");
2149a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        return NO_MEMORY;
2159a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    }
216c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2179a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    ret = mCommandHandler->run("CallbackThread", PRIORITY_URGENT_DISPLAY);
2189a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if ( ret != NO_ERROR )
2199a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    {
2209a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        if( ret == INVALID_OPERATION){
2219a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu            CAMHAL_LOGDA("command handler thread already runnning!!");
22241471bd18fc9a28371c4d89283f2082b513f2226Iliyan Malchev	    ret = NO_ERROR;
22341471bd18fc9a28371c4d89283f2082b513f2226Iliyan Malchev        } else
224c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2259a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu            CAMHAL_LOGEA("Couldn't run command handlerthread");
2269a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu            return ret;
227c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2289a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    }
229c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2309a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    // initialize omx callback handling thread
2319a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if(mOMXCallbackHandler.get() == NULL)
2329a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        mOMXCallbackHandler = new OMXCallbackHandler(this);
2339a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu
2349a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if ( NULL == mOMXCallbackHandler.get() )
2359a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    {
2369a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        CAMHAL_LOGEA("Couldn't create omx callback handler");
2379a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        return NO_MEMORY;
2389a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    }
2399a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu
2409a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    ret = mOMXCallbackHandler->run("OMXCallbackThread", PRIORITY_URGENT_DISPLAY);
2419a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if ( ret != NO_ERROR )
2429a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    {
2439a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        if( ret == INVALID_OPERATION){
2449a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu            CAMHAL_LOGDA("omx callback handler thread already runnning!!");
24541471bd18fc9a28371c4d89283f2082b513f2226Iliyan Malchev	    ret = NO_ERROR;
2469a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        }else
247c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2489a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu            CAMHAL_LOGEA("Couldn't run omx callback handler thread");
2499a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu            return ret;
250c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2519a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    }
252c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2539a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    //Remove any unhandled events
2549a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if (!mEventSignalQ.isEmpty()) {
2559a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        for (unsigned int i = 0 ;i < mEventSignalQ.size(); i++ ) {
2569a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu            TIUTILS::Message *msg = mEventSignalQ.itemAt(i);
2579a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu            //remove from queue and free msg
2589a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu            mEventSignalQ.removeAt(i);
2599a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu            if ( NULL != msg ) {
2609a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu                free(msg);
261c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2629a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        }
2639a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    }
264c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2659a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    OMX_INIT_STRUCT_PTR (&mRegionPriority, OMX_TI_CONFIG_3A_REGION_PRIORITY);
2669a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    OMX_INIT_STRUCT_PTR (&mFacePriority, OMX_TI_CONFIG_3A_FACE_PRIORITY);
2679a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mRegionPriority.nPortIndex = OMX_ALL;
2689a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mFacePriority.nPortIndex = OMX_ALL;
26900479a8f1d791824870513b8b0b9edd67d2560ffSundar Raman
2709a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    //Setting this flag will that the first setParameter call will apply all 3A settings
2719a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    //and will not conditionally apply based on current values.
2729a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mFirstTimeInit = true;
273c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2749a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    memset(mExposureBracketingValues, 0, EXP_BRACKET_RANGE*sizeof(int));
2759a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mMeasurementEnabled = false;
2769a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mFaceDetectionRunning = false;
2779a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mFaceDetectionPaused = false;
278c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2799a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    memset(&mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mImagePortIndex], 0, sizeof(OMXCameraPortParameters));
2809a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    memset(&mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex], 0, sizeof(OMXCameraPortParameters));
281024e97a1213041f9d266ee09f7148a0c9b007260Tyler Luu
2829a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    //Initialize 3A defaults
2839a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    ret = apply3ADefaults(mParameters3A);
2849a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if ( NO_ERROR != ret ) {
28541471bd18fc9a28371c4d89283f2082b513f2226Iliyan Malchev        CAMHAL_LOGEA("Couldn't apply 3A defaults!");
2869a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        goto EXIT;
287c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2889a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu
289c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
290c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ErrorUtils::omxToAndroidError(eError);
291c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
292c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    EXIT:
293c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
294c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
295b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    performCleanupAfterError();
296b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    LOG_FUNCTION_NAME_EXIT;
297b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    return ErrorUtils::omxToAndroidError(eError);
298b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman}
299c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
300b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Ramanvoid OMXCameraAdapter::performCleanupAfterError()
301b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman{
302c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(mCameraAdapterParameters.mHandleComp)
303c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
304c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ///Free the OMX component handle in case of error
305c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        OMX_FreeHandle(mCameraAdapterParameters.mHandleComp);
306b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman        mCameraAdapterParameters.mHandleComp = NULL;
307c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
308c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
309c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///De-init the OMX
310c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_Deinit();
311b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    mComponentState = OMX_StateInvalid;
312c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
313c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
314c322989ae6ff6769490828de1b5eda12b749cce9Iliyan MalchevOMXCameraAdapter::OMXCameraPortParameters *OMXCameraAdapter::getPortParams(CameraFrame::FrameType frameType)
315c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
316c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraAdapter::OMXCameraPortParameters *ret = NULL;
317c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
318c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    switch ( frameType )
319c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    {
320c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case CameraFrame::IMAGE_FRAME:
321c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case CameraFrame::RAW_FRAME:
322c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mImagePortIndex];
323c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
324c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case CameraFrame::PREVIEW_FRAME_SYNC:
325c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case CameraFrame::SNAPSHOT_FRAME:
326c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case CameraFrame::VIDEO_FRAME_SYNC:
327c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex];
328c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
329c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    case CameraFrame::FRAME_DATA_SYNC:
330c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mMeasurementPortIndex];
331c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
332c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    default:
333c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
334c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    };
335c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
336c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
337c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
338c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
339c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::fillThisBuffer(void* frameBuf, CameraFrame::FrameType frameType)
340c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
341c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
342c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraPortParameters *port = NULL;
343c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
344c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    BaseCameraAdapter::AdapterState state;
345c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    BaseCameraAdapter::getState(state);
346c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
347c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( ( PREVIEW_ACTIVE & state ) != PREVIEW_ACTIVE )
348c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
349c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return NO_INIT;
350c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
351c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
352c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NULL == frameBuf )
353c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
354c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return -EINVAL;
355c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
356c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
357c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( (NO_ERROR == ret) &&
358c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         ((CameraFrame::IMAGE_FRAME == frameType) || (CameraFrame::RAW_FRAME == frameType)) &&
359c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         (1 > mCapturedFrames) &&
360c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         (!mBracketingEnabled)) {
361c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        // Signal end of image capture
362c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NULL != mEndImageCaptureCallback) {
363c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mEndImageCaptureCallback(mEndCaptureData);
364c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
365c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return NO_ERROR;
366c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev     }
367c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
368c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
369c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
370c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        port = getPortParams(frameType);
371c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NULL == port )
372c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
373c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("Invalid frameType 0x%x", frameType);
374c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = -EINVAL;
375c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
376c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
377c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
378c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
379c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
380c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
381c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        for ( int i = 0 ; i < port->mNumBufs ; i++)
382c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
383c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( port->mBufferHeader[i]->pBuffer == frameBuf )
384c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
385c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                eError = OMX_FillThisBuffer(mCameraAdapterParameters.mHandleComp, port->mBufferHeader[i]);
386c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                if ( eError != OMX_ErrorNone )
387c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    {
388b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman                    CAMHAL_LOGEB("OMX_FillThisBuffer 0x%x", eError);
389b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman                    goto EXIT;
390c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    }
391c493114006e0e136c8c88c2fc9865994054959bfSundar Raman                mFramesWithDucati++;
392c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                break;
393c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
394c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
395c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
396c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
397c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
398b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    LOG_FUNCTION_NAME_EXIT;
399c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
400b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman
401b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar RamanEXIT:
402b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
403b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    performCleanupAfterError();
404b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    //Since fillthisbuffer is called asynchronously, make sure to signal error to the app
405b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    mErrorNotifier->errorNotify(CAMERA_ERROR_HARD);
406b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    LOG_FUNCTION_NAME_EXIT;
407b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    return (ret | ErrorUtils::omxToAndroidError(eError));
408c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
409c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
410c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::setParameters(const CameraParameters &params)
411c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
412c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
413c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
414c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    const char * str = NULL;
415c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int mode = 0;
416c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
417c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    bool updateImagePortParams = false;
418c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int minFramerate, maxFramerate, frameRate;
419c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    const char *valstr = NULL;
420c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    const char *oldstr = NULL;
421c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int w, h;
422c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_COLOR_FORMATTYPE pixFormat;
423c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    BaseCameraAdapter::AdapterState state;
424c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    BaseCameraAdapter::getState(state);
425c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
426c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///@todo Include more camera parameters
427c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( (valstr = params.getPreviewFormat()) != NULL )
428c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
429c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if (strcmp(valstr, (const char *) CameraParameters::PIXEL_FORMAT_YUV422I) == 0)
430c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
431c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDA("CbYCrY format selected");
432c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            pixFormat = OMX_COLOR_FormatCbYCrY;
433c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
434c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else if(strcmp(valstr, (const char *) CameraParameters::PIXEL_FORMAT_YUV420SP) == 0 ||
435c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                strcmp(valstr, (const char *) CameraParameters::PIXEL_FORMAT_YUV420P) == 0)
436c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
437c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDA("YUV420SP format selected");
438c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            pixFormat = OMX_COLOR_FormatYUV420SemiPlanar;
439c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
440c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else if(strcmp(valstr, (const char *) CameraParameters::PIXEL_FORMAT_RGB565) == 0)
441c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
442c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDA("RGB565 format selected");
443c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            pixFormat = OMX_COLOR_Format16bitRGB565;
444c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
445c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
446c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
447c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDA("Invalid format, CbYCrY format selected as default");
448c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            pixFormat = OMX_COLOR_FormatCbYCrY;
449c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
450c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
451c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
452c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
453c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Preview format is NULL, defaulting to CbYCrY");
454c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        pixFormat = OMX_COLOR_FormatCbYCrY;
455c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
456c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
457c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraPortParameters *cap;
458c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    cap = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex];
459c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
460c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    params.getPreviewSize(&w, &h);
461c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    frameRate = params.getPreviewFrameRate();
462c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    minFramerate = params.getInt(TICameraParameters::KEY_MINFRAMERATE);
463c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    maxFramerate = params.getInt(TICameraParameters::KEY_MAXFRAMERATE);
464c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( ( 0 < minFramerate ) &&
465c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         ( 0 < maxFramerate ) )
466c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
467c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( minFramerate > maxFramerate )
468c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
469c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev             CAMHAL_LOGEA(" Min FPS set higher than MAX. So setting MIN and MAX to the higher value");
470c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev             maxFramerate = minFramerate;
471c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
472c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
473c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( 0 >= frameRate )
474c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
475c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            frameRate = maxFramerate;
476c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
477c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
478c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if( ( cap->mMinFrameRate != minFramerate ) ||
479c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ( cap->mMaxFrameRate != maxFramerate ) )
480c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
481c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            cap->mMinFrameRate = minFramerate;
482c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            cap->mMaxFrameRate = maxFramerate;
483c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            setVFramerate(cap->mMinFrameRate, cap->mMaxFrameRate);
484c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
485c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
486c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
487c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( 0 < frameRate )
488c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
489c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        cap->mColorFormat = pixFormat;
490c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        cap->mWidth = w;
491c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        cap->mHeight = h;
492c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        cap->mFrameRate = frameRate;
493c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
494c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGVB("Prev: cap.mColorFormat = %d", (int)cap->mColorFormat);
495c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGVB("Prev: cap.mWidth = %d", (int)cap->mWidth);
496c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGVB("Prev: cap.mHeight = %d", (int)cap->mHeight);
497c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGVB("Prev: cap.mFrameRate = %d", (int)cap->mFrameRate);
498c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
499c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        //TODO: Add an additional parameter for video resolution
500c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev       //use preview resolution for now
501c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        cap = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex];
502c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        cap->mColorFormat = pixFormat;
503c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        cap->mWidth = w;
504c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        cap->mHeight = h;
505c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        cap->mFrameRate = frameRate;
506c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
507c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGVB("Video: cap.mColorFormat = %d", (int)cap->mColorFormat);
508c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGVB("Video: cap.mWidth = %d", (int)cap->mWidth);
509c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGVB("Video: cap.mHeight = %d", (int)cap->mHeight);
510c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGVB("Video: cap.mFrameRate = %d", (int)cap->mFrameRate);
511c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
512c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ///mStride is set from setBufs() while passing the APIs
513c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        cap->mStride = 4096;
514c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        cap->mBufSize = cap->mStride * cap->mHeight;
515c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
516c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
517c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( ( cap->mWidth >= 1920 ) &&
518c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         ( cap->mHeight >= 1080 ) &&
519c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         ( cap->mFrameRate >= FRAME_RATE_FULL_HD ) &&
520c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         ( !mSensorOverclock ) )
521c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
522c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mOMXStateSwitch = true;
523c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
524c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else if ( ( ( cap->mWidth < 1920 ) ||
525c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev               ( cap->mHeight < 1080 ) ||
526c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev               ( cap->mFrameRate < FRAME_RATE_FULL_HD ) ) &&
527c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev               ( mSensorOverclock ) )
528c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
529c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mOMXStateSwitch = true;
530c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
531c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
532c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( (valstr = params.get(TICameraParameters::KEY_MEASUREMENT_ENABLE)) != NULL )
533c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
534c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if (strcmp(valstr, (const char *) TICameraParameters::MEASUREMENT_ENABLE) == 0)
535c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
536c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mMeasurementEnabled = true;
537c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
538c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else if (strcmp(valstr, (const char *) TICameraParameters::MEASUREMENT_DISABLE) == 0)
539c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
540c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mMeasurementEnabled = false;
541c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
542c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
543c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
544c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mMeasurementEnabled = false;
545c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
546c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
547c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
548c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
549c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        //Disable measurement data by default
550c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mMeasurementEnabled = false;
551c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
552c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
553782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev    ret |= setParametersCapture(params, state);
554c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
555782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev    ret |= setParameters3A(params, state);
556c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
557782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev    ret |= setParametersAlgo(params, state);
558c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
559782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev    ret |= setParametersFocus(params, state);
560c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
561782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev    ret |= setParametersFD(params, state);
562c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
563782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev    ret |= setParametersZoom(params, state);
564c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
565782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev    ret |= setParametersEXIF(params, state);
566c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
567c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mParams = params;
568782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev    mFirstTimeInit = false;
569c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
570c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
571c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
572c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
573c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
574c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevvoid saveFile(unsigned char   *buff, int width, int height, int format) {
575c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    static int      counter = 1;
576c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int             fd = -1;
577c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    char            fn[256];
578c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
579c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
580c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
581c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    fn[0] = 0;
582c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    sprintf(fn, "/preview%03d.yuv", counter);
583c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    fd = open(fn, O_CREAT | O_WRONLY | O_SYNC | O_TRUNC, 0777);
584c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(fd < 0) {
585c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOGE("Unable to open file %s: %s", fn, strerror(fd));
586c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return;
587c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
588c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
589c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGVB("Copying from 0x%x, size=%d x %d", buff, width, height);
590c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
591c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //method currently supports only nv12 dumping
592c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int stride = width;
593c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    uint8_t *bf = (uint8_t*) buff;
594c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    for(int i=0;i<height;i++)
595c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
596c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        write(fd, bf, width);
597c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        bf += 4096;
598c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
599c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
600c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    for(int i=0;i<height/2;i++)
601c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
602c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        write(fd, bf, stride);
603c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        bf += 4096;
604c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
605c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
606c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    close(fd);
607c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
608c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
609c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    counter++;
610c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
611c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
612c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
613c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
614c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevvoid OMXCameraAdapter::getParameters(CameraParameters& params)
615c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
616c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
617c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_CONFIG_EXPOSUREVALUETYPE exp;
618c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
619c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    BaseCameraAdapter::AdapterState state;
620c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    BaseCameraAdapter::getState(state);
62180c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman    const char *valstr = NULL;
622c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
623c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
62439ab34daa6770e0995b7184ae7fd9c9213136f52Heechan Park    if( mParameters3A.SceneMode != OMX_Manual ) {
62511be32ce333557c09434611f69830e19aca55e72Tyler Luu       const char *valstr_supported = NULL;
62611be32ce333557c09434611f69830e19aca55e72Tyler Luu
62739ab34daa6770e0995b7184ae7fd9c9213136f52Heechan Park       valstr = getLUTvalue_OMXtoHAL(mParameters3A.WhiteBallance, WBalLUT);
62811be32ce333557c09434611f69830e19aca55e72Tyler Luu       valstr_supported = mParams.get(CameraParameters::KEY_SUPPORTED_WHITE_BALANCE);
62911be32ce333557c09434611f69830e19aca55e72Tyler Luu       if (valstr && valstr_supported && strstr(valstr_supported, valstr))
63011be32ce333557c09434611f69830e19aca55e72Tyler Luu           params.set(CameraParameters::KEY_WHITE_BALANCE , valstr);
631c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
63239ab34daa6770e0995b7184ae7fd9c9213136f52Heechan Park       valstr = getLUTvalue_OMXtoHAL(mParameters3A.FlashMode, FlashLUT);
63311be32ce333557c09434611f69830e19aca55e72Tyler Luu       valstr_supported = mParams.get(CameraParameters::KEY_SUPPORTED_FLASH_MODES);
63411be32ce333557c09434611f69830e19aca55e72Tyler Luu       if (valstr && valstr_supported && strstr(valstr_supported, valstr))
63511be32ce333557c09434611f69830e19aca55e72Tyler Luu           params.set(CameraParameters::KEY_FLASH_MODE, valstr);
636c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
63770e04f3b54d5100621ffa631dbc81579d2cc3c8cTyler Luu       if ((mParameters3A.Focus == OMX_IMAGE_FocusControlAuto) &&
63870e04f3b54d5100621ffa631dbc81579d2cc3c8cTyler Luu           (mCapMode != OMXCameraAdapter::VIDEO_MODE)) {
63970e04f3b54d5100621ffa631dbc81579d2cc3c8cTyler Luu           valstr = CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE;
64070e04f3b54d5100621ffa631dbc81579d2cc3c8cTyler Luu       } else {
64170e04f3b54d5100621ffa631dbc81579d2cc3c8cTyler Luu           valstr = getLUTvalue_OMXtoHAL(mParameters3A.Focus, FocusLUT);
64270e04f3b54d5100621ffa631dbc81579d2cc3c8cTyler Luu       }
64311be32ce333557c09434611f69830e19aca55e72Tyler Luu       valstr_supported = mParams.get(CameraParameters::KEY_SUPPORTED_FOCUS_MODES);
64411be32ce333557c09434611f69830e19aca55e72Tyler Luu       if (valstr && valstr_supported && strstr(valstr_supported, valstr))
64511be32ce333557c09434611f69830e19aca55e72Tyler Luu           params.set(CameraParameters::KEY_FOCUS_MODE, valstr);
64639ab34daa6770e0995b7184ae7fd9c9213136f52Heechan Park    }
647c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
648c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //Query focus distances only during CAF, Infinity
649c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //or when focus is running
650c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( ( AF_ACTIVE & state ) ||
651c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         ( mParameters3A.Focus == OMX_IMAGE_FocusControlAuto )  ||
652c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         ( mParameters3A.Focus == OMX_IMAGE_FocusControlAutoInfinity ) ||
653c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         ( NULL == mParameters.get(CameraParameters::KEY_FOCUS_DISTANCES) ) )
654c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
655c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        updateFocusDistances(params);
656c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
657c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
658c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
659c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        params.set(CameraParameters::KEY_FOCUS_DISTANCES,
660c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                   mParameters.get(CameraParameters::KEY_FOCUS_DISTANCES));
661c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
662c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
663c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_INIT_STRUCT_PTR (&exp, OMX_CONFIG_EXPOSUREVALUETYPE);
664c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    exp.nPortIndex = OMX_ALL;
665c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
666c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    eError = OMX_GetConfig(mCameraAdapterParameters.mHandleComp,
667c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_IndexConfigCommonExposureValue,
668c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           &exp);
669c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( OMX_ErrorNone == eError )
670c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
671c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        params.set(TICameraParameters::KEY_CURRENT_ISO, exp.nSensitivity);
672c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
673c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
674c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
675c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("OMX error 0x%x, while retrieving current ISO value", eError);
676c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
677c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
678c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    {
679c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    Mutex::Autolock lock(mZoomLock);
680c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //Immediate zoom should not be avaialable while smooth zoom is running
681c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( ZOOM_ACTIVE & state )
682c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
683c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( mZoomParameterIdx != mCurrentZoomIdx )
684c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
685c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mZoomParameterIdx += mZoomInc;
686c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
687c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        params.set( CameraParameters::KEY_ZOOM, mZoomParameterIdx);
688c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( ( mCurrentZoomIdx == mTargetZoomIdx ) &&
689c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev             ( mZoomParameterIdx == mCurrentZoomIdx ) )
690c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
691c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
692c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR == ret )
693c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
694c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
695c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ret =  BaseCameraAdapter::setState(CAMERA_STOP_SMOOTH_ZOOM);
696c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
697c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                if ( NO_ERROR == ret )
698c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    {
699c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    ret = BaseCameraAdapter::commitState();
700c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    }
701c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                else
702c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    {
703c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    ret |= BaseCameraAdapter::rollbackState();
704c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    }
705c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
706c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
707c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
708c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
709c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
710c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("CameraParameters Zoom = %d", mCurrentZoomIdx);
711c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
712c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
713c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
714c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        params.set( CameraParameters::KEY_ZOOM, mCurrentZoomIdx);
715c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
716c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
717c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
71880c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman    //Populate current lock status
71980c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman    if( (valstr = mParams.get(CameraParameters::KEY_AUTO_EXPOSURE_LOCK)) != NULL )
72080c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman      {
72180c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman        CAMHAL_LOGDB("Auto Exposure Lock get %s", mParams.get(CameraParameters::KEY_AUTO_EXPOSURE_LOCK));
72280c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman        params.set(CameraParameters::KEY_AUTO_EXPOSURE_LOCK, valstr);
72380c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman      }
7248cb48e582f4f3c93aa25159c1116a7c83bfa388aSundar Raman
72580c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman    if( (valstr = mParams.get(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK)) != NULL )
72680c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman      {
72780c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman        CAMHAL_LOGDB("Auto WhiteBalance Lock get %s", mParams.get(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK));
72880c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman        params.set(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK, valstr);
72980c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman      }
73080c50ac56bfa97be0cf04332ec1d314637c8770fSundar Raman
731c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
732c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
733c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
734c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
735c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::setFormat(OMX_U32 port, OMXCameraPortParameters &portParams)
736c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
737c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    size_t bufferCount;
738c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
739c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
740c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
741c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
742c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_PARAM_PORTDEFINITIONTYPE portCheck;
743c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
744c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_INIT_STRUCT_PTR (&portCheck, OMX_PARAM_PORTDEFINITIONTYPE);
745c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
746c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    portCheck.nPortIndex = port;
747c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
748c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    eError = OMX_GetParameter (mCameraAdapterParameters.mHandleComp,
749c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                OMX_IndexParamPortDefinition, &portCheck);
750c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(eError!=OMX_ErrorNone)
751c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
752c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("OMX_GetParameter - %x", eError);
753c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
754c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
755c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
756c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( OMX_CAMERA_PORT_VIDEO_OUT_PREVIEW == port )
757c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
758c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.format.video.nFrameWidth      = portParams.mWidth;
759c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.format.video.nFrameHeight     = portParams.mHeight;
760c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.format.video.eColorFormat     = portParams.mColorFormat;
761c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.format.video.nStride          = portParams.mStride;
762c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if( ( portCheck.format.video.nFrameWidth >= 1920 ) &&
763c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ( portCheck.format.video.nFrameHeight >= 1080 ) &&
764c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ( portParams.mFrameRate >= FRAME_RATE_FULL_HD ) )
765c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
766c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            setSensorOverclock(true);
767c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
768c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
769c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
770c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            setSensorOverclock(false);
771c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
772c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
773c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.format.video.xFramerate       = portParams.mFrameRate<<16;
774c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.nBufferSize                   = portParams.mStride * portParams.mHeight;
775c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.nBufferCountActual = portParams.mNumBufs;
776c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mFocusThreshold = FOCUS_THRESHOLD * portParams.mFrameRate;
777c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
778c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else if ( OMX_CAMERA_PORT_IMAGE_OUT_IMAGE == port )
779c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
780c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.format.image.nFrameWidth      = portParams.mWidth;
781c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.format.image.nFrameHeight     = portParams.mHeight;
782c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( OMX_COLOR_FormatUnused == portParams.mColorFormat && mCodingMode == CodingNone )
783c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
784c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            portCheck.format.image.eColorFormat     = OMX_COLOR_FormatCbYCrY;
785c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            portCheck.format.image.eCompressionFormat = OMX_IMAGE_CodingJPEG;
786c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
787c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else if ( OMX_COLOR_FormatUnused == portParams.mColorFormat && mCodingMode == CodingJPS )
788c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
789c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            portCheck.format.image.eColorFormat       = OMX_COLOR_FormatCbYCrY;
790c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            portCheck.format.image.eCompressionFormat = (OMX_IMAGE_CODINGTYPE) OMX_TI_IMAGE_CodingJPS;
791c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
792c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else if ( OMX_COLOR_FormatUnused == portParams.mColorFormat && mCodingMode == CodingMPO )
793c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
794c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            portCheck.format.image.eColorFormat       = OMX_COLOR_FormatCbYCrY;
795c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            portCheck.format.image.eCompressionFormat = (OMX_IMAGE_CODINGTYPE) OMX_TI_IMAGE_CodingMPO;
796c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
797c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else if ( OMX_COLOR_FormatUnused == portParams.mColorFormat && mCodingMode == CodingRAWJPEG )
798c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
799c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            //TODO: OMX_IMAGE_CodingJPEG should be changed to OMX_IMAGE_CodingRAWJPEG when
800c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            // RAW format is supported
801c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            portCheck.format.image.eColorFormat       = OMX_COLOR_FormatCbYCrY;
802c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            portCheck.format.image.eCompressionFormat = OMX_IMAGE_CodingJPEG;
803c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
804c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else if ( OMX_COLOR_FormatUnused == portParams.mColorFormat && mCodingMode == CodingRAWMPO )
805c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
806c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            //TODO: OMX_IMAGE_CodingJPEG should be changed to OMX_IMAGE_CodingRAWMPO when
807c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            // RAW format is supported
808c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            portCheck.format.image.eColorFormat       = OMX_COLOR_FormatCbYCrY;
809c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            portCheck.format.image.eCompressionFormat = OMX_IMAGE_CodingJPEG;
810c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
811c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
812c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
813c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            portCheck.format.image.eColorFormat     = portParams.mColorFormat;
814c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            portCheck.format.image.eCompressionFormat = OMX_IMAGE_CodingUnused;
815c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
816c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
817c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        //Stride for 1D tiler buffer is zero
818c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.format.image.nStride          =  0;
819c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.nBufferSize                   =  portParams.mStride * portParams.mWidth * portParams.mHeight;
820c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.nBufferCountActual = portParams.mNumBufs;
821c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
822c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
823c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
824c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Unsupported port index 0x%x", (unsigned int)port);
825c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
826c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
827c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    eError = OMX_SetParameter(mCameraAdapterParameters.mHandleComp,
828c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                            OMX_IndexParamPortDefinition, &portCheck);
829c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(eError!=OMX_ErrorNone)
830c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
831c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("OMX_SetParameter - %x", eError);
832c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
833c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
834c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
835c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    /* check if parameters are set correctly by calling GetParameter() */
836c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    eError = OMX_GetParameter(mCameraAdapterParameters.mHandleComp,
837c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                        OMX_IndexParamPortDefinition, &portCheck);
838c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(eError!=OMX_ErrorNone)
839c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
840c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("OMX_GetParameter - %x", eError);
841c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
842c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
843c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
844c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    portParams.mBufSize = portCheck.nBufferSize;
845c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
846c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( OMX_CAMERA_PORT_IMAGE_OUT_IMAGE == port )
847c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
848c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n *** IMG Width = %ld", portCheck.format.image.nFrameWidth);
849c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n ***IMG Height = %ld", portCheck.format.image.nFrameHeight);
850c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
851c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n ***IMG IMG FMT = %x", portCheck.format.image.eColorFormat);
852c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n ***IMG portCheck.nBufferSize = %ld\n",portCheck.nBufferSize);
853c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n ***IMG portCheck.nBufferCountMin = %ld\n",
854c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                                portCheck.nBufferCountMin);
855c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n ***IMG portCheck.nBufferCountActual = %ld\n",
856c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                                portCheck.nBufferCountActual);
857c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n ***IMG portCheck.format.image.nStride = %ld\n",
858c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                                portCheck.format.image.nStride);
859c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
860c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
861c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
862c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n *** PRV Width = %ld", portCheck.format.video.nFrameWidth);
863c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n ***PRV Height = %ld", portCheck.format.video.nFrameHeight);
864c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
865c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n ***PRV IMG FMT = %x", portCheck.format.video.eColorFormat);
866c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n ***PRV portCheck.nBufferSize = %ld\n",portCheck.nBufferSize);
867c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n ***PRV portCheck.nBufferCountMin = %ld\n",
868c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                                portCheck.nBufferCountMin);
869c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n ***PRV portCheck.nBufferCountActual = %ld\n",
870c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                                portCheck.nBufferCountActual);
871c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("\n ***PRV portCheck.format.video.nStride = %ld\n",
872c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                                portCheck.format.video.nStride);
873c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
874c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
875c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
876c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
877c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ErrorUtils::omxToAndroidError(eError);
878c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
879c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    EXIT:
880c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
881c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGEB("Exiting function %s because of eError=%x", __FUNCTION__, eError);
882c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
883c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
884c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
885c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ErrorUtils::omxToAndroidError(eError);
886c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
887c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
888c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::flushBuffers()
889c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
890c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
891c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
892c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    TIMM_OSAL_ERRORTYPE err;
893c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    TIMM_OSAL_U32 uRequestedEvents = OMXCameraAdapter::CAMERA_PORT_FLUSH;
894c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    TIMM_OSAL_U32 pRetrievedEvents;
895c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
896c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( 0 != mFlushSem.Count() )
897c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
898c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error mFlushSem semaphore count %d", mFlushSem.Count());
899c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOG_FUNCTION_NAME_EXIT;
900c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return NO_INIT;
901c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
902c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
903c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
904c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
905c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraPortParameters * mPreviewData = NULL;
906c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mPreviewData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex];
907c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
908c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Register for the FLUSH event
909c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///This method just inserts a message in Event Q, which is checked in the callback
910c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///The sempahore passed is signalled by the callback
911c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
912c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                OMX_EventCmdComplete,
913c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                OMX_CommandFlush,
914c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                OMX_CAMERA_PORT_VIDEO_OUT_PREVIEW,
915c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                mFlushSem);
916c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(ret!=NO_ERROR)
917c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
918c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error in registering for event %d", ret);
919c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
920c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
921c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
922c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Send FLUSH command to preview port
923c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    eError = OMX_SendCommand (mCameraAdapterParameters.mHandleComp,
924c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                              OMX_CommandFlush,
925c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                              mCameraAdapterParameters.mPrevPortIndex,
926c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                              NULL);
927c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
928c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(eError!=OMX_ErrorNone)
929c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
930c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("OMX_SendCommand(OMX_CommandFlush)-0x%x", eError);
931c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
932c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
933c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
934c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGDA("Waiting for flush event");
935c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
936c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Wait for the FLUSH event to occur
937c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = mFlushSem.WaitTimeout(OMX_CMD_TIMEOUT);
9386b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
9396b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    //If somethiing bad happened while we wait
9406b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    if (mComponentState == OMX_StateInvalid)
9416b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng      {
9426b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        CAMHAL_LOGEA("Invalid State after Flush Exitting!!!");
9436b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        goto EXIT;
9446b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng      }
9456b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
946c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
947c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
948c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDA("Flush event received");
949c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
950c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
951c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
9529b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng        ret |= RemoveEvent(mCameraAdapterParameters.mHandleComp,
953c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_EventCmdComplete,
954c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_CommandFlush,
955c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_CAMERA_PORT_VIDEO_OUT_PREVIEW,
956c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           NULL);
957c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDA("Flush event timeout expired");
958c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
959c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
960c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
961c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
962c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
963c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return (ret | ErrorUtils::omxToAndroidError(eError));
964c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
965c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    EXIT:
966c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
967b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    performCleanupAfterError();
968c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
969c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return (ret | ErrorUtils::omxToAndroidError(eError));
970c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
971c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
972c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev///API to give the buffers to Adapter
973c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::useBuffers(CameraMode mode, void* bufArr, int num, size_t length, unsigned int queueable)
974c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
975c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
976c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
977c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
978c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
979c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
980c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    switch(mode)
981c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
982c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case CAMERA_PREVIEW:
983c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex].mNumBufs =  num;
984c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex].mMaxQueueable = queueable;
985c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = UseBuffersPreview(bufArr, num);
986c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            break;
987c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
988c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case CAMERA_IMAGE_CAPTURE:
989c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mImagePortIndex].mNumBufs = num;
990c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mImagePortIndex].mMaxQueueable = queueable;
991c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = UseBuffersCapture(bufArr, num);
992c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            break;
993c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
994c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case CAMERA_VIDEO:
995c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex].mNumBufs =  num;
996c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex].mMaxQueueable = queueable;
997c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = UseBuffersPreview(bufArr, num);
998c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            break;
999c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1000c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case CAMERA_MEASUREMENT:
1001c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mMeasurementPortIndex].mNumBufs = num;
1002c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mMeasurementPortIndex].mMaxQueueable = queueable;
1003c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = UseBuffersPreviewData(bufArr, num);
1004c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            break;
1005c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1006c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1007c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1008c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
1009c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1010c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
1011c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
1012c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1013c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::UseBuffersPreviewData(void* bufArr, int num)
1014c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
1015c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
1016c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
1017c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraPortParameters * measurementData = NULL;
1018c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    uint32_t *buffers;
1019c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    Mutex::Autolock lock( mPreviewDataBufferLock);
1020c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1021c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
1022c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1023c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( mComponentState != OMX_StateLoaded )
1024c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1025c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Calling UseBuffersPreviewData() when not in LOADED state");
1026b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman        return BAD_VALUE;
1027c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1028c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1029c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NULL == bufArr )
1030c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1031c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("NULL pointer passed for buffArr");
1032b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman        return BAD_VALUE;
1033c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1034c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1035c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( 0 != mUsePreviewDataSem.Count() )
1036c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1037c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error mUsePreviewDataSem semaphore count %d", mUsePreviewDataSem.Count());
1038c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOG_FUNCTION_NAME_EXIT;
1039c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return NO_INIT;
1040c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1041c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1042c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
1043c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1044c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        measurementData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mMeasurementPortIndex];
1045c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        measurementData->mNumBufs = num ;
1046c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        buffers= (uint32_t*) bufArr;
1047c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1048c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1049c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
1050c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1051c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         ///Register for port enable event on measurement port
1052c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
1053c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                      OMX_EventCmdComplete,
1054c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                      OMX_CommandPortEnable,
1055c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                      mCameraAdapterParameters.mMeasurementPortIndex,
1056c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                      mUsePreviewDataSem);
1057c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1058c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( ret == NO_ERROR )
1059c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1060c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDB("Registering for event %d", ret);
1061c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1062c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
1063c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1064c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("Error in registering for event %d", ret);
1065b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            goto EXIT;
1066c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1067c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1068c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1069c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
1070c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1071c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         ///Enable MEASUREMENT Port
1072c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev         eError = OMX_SendCommand(mCameraAdapterParameters.mHandleComp,
1073c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                      OMX_CommandPortEnable,
1074c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                      mCameraAdapterParameters.mMeasurementPortIndex,
1075c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                      NULL);
1076c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1077c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( eError == OMX_ErrorNone )
1078c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
1079c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGDB("OMX_SendCommand(OMX_CommandPortEnable) -0x%x", eError);
1080c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
1081c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            else
1082c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
1083c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEB("OMX_SendCommand(OMX_CommandPortEnable) -0x%x", eError);
1084b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman                goto EXIT;
1085c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
1086c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1087c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1088c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
1089c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1090c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = mUsePreviewDataSem.WaitTimeout(OMX_CMD_TIMEOUT);
1091c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
10926b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        //If somethiing bad happened while we wait
10936b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        if (mComponentState == OMX_StateInvalid)
10946b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng          {
10956b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng            CAMHAL_LOGEA("Invalid State after measurement port enable Exitting!!!");
1096b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            goto EXIT;
10976b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng          }
10986b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
1099c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NO_ERROR == ret )
1100c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1101c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDA("Port enable event arrived on measurement port");
1102c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1103c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
1104c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
11059b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng            ret |= RemoveEvent(mCameraAdapterParameters.mHandleComp,
1106c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_EventCmdComplete,
1107c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_CommandPortEnable,
1108c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               mCameraAdapterParameters.mMeasurementPortIndex,
1109c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               NULL);
1110c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEA("Timeout expoired during port enable on measurement port");
1111b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            goto EXIT;
1112c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1113c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1114c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDA("Port enable event arrived on measurement port");
1115c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1116c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1117c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
1118c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1119c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
1120b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar RamanEXIT:
1121b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
1122b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    performCleanupAfterError();
1123b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    LOG_FUNCTION_NAME_EXIT;
1124b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    return (ret | ErrorUtils::omxToAndroidError(eError));
1125c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
1126c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
11277016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boatengstatus_t OMXCameraAdapter::switchToExecuting()
11287016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng{
11297016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  status_t ret = NO_ERROR;
11307016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  TIUTILS::Message msg;
11317016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
11327016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  LOG_FUNCTION_NAME;
11337016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
1134312fa6b1a184b5e3aaedf66e6a348d17d9e38bd8Tyler Luu  mStateSwitchLock.lock();
11357016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  msg.command = CommandHandler::CAMERA_SWITCH_TO_EXECUTING;
11367016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  msg.arg1 = mErrorNotifier;
11377016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  ret = mCommandHandler->put(&msg);
11387016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
11397016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  LOG_FUNCTION_NAME;
11407016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
11417016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  return ret;
11427016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng}
11437016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
11447016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boatengstatus_t OMXCameraAdapter::doSwitchToExecuting()
11457016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng{
11467016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  status_t ret = NO_ERROR;
11477016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  OMX_ERRORTYPE eError = OMX_ErrorNone;
11487016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  LOG_FUNCTION_NAME;
11497016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
11507016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  if ( (mComponentState == OMX_StateExecuting) || (mComponentState == OMX_StateInvalid) ){
11517016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    CAMHAL_LOGDA("Already in OMX_Executing state or OMX_StateInvalid state");
1152312fa6b1a184b5e3aaedf66e6a348d17d9e38bd8Tyler Luu    mStateSwitchLock.unlock();
11537016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    return NO_ERROR;
11547016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  }
11557016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
11567016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  if ( 0 != mSwitchToExecSem.Count() ){
11577016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    CAMHAL_LOGEB("Error mSwitchToExecSem semaphore count %d", mSwitchToExecSem.Count());
11587016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    goto EXIT;
11597016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  }
11607016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
11617016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  ///Register for Preview port DISABLE  event
11627016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
11637016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                         OMX_EventCmdComplete,
11647016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                         OMX_CommandPortDisable,
11657016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                         mCameraAdapterParameters.mPrevPortIndex,
11667016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                         mSwitchToExecSem);
11677016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  if ( NO_ERROR != ret ){
11687016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    CAMHAL_LOGEB("Error in registering Port Disable for event %d", ret);
11697016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    goto EXIT;
11707016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  }
11717016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  ///Disable Preview Port
11727016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  eError = OMX_SendCommand(mCameraAdapterParameters.mHandleComp,
11737016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                           OMX_CommandPortDisable,
11747016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                           mCameraAdapterParameters.mPrevPortIndex,
11757016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                           NULL);
11767016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  ret = mSwitchToExecSem.WaitTimeout(OMX_CMD_TIMEOUT);
11777016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  if (ret != NO_ERROR){
11787016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    CAMHAL_LOGEB("Timeout PREVIEW PORT DISABLE %d", ret);
11797016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  }
11807016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
11817016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  CAMHAL_LOGVB("PREV PORT DISABLED %d", ret);
11827016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
11837016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  ///Register for IDLE state switch event
11847016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
11857016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                         OMX_EventCmdComplete,
11867016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                         OMX_CommandStateSet,
11877016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                         OMX_StateIdle,
11887016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                         mSwitchToExecSem);
11897016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  if(ret!=NO_ERROR)
11907016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    {
11917016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng      CAMHAL_LOGEB("Error in IDLE STATE SWITCH %d", ret);
11927016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng      goto EXIT;
11937016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    }
11947016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  eError = OMX_SendCommand (mCameraAdapterParameters.mHandleComp ,
11957016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                            OMX_CommandStateSet,
11967016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                            OMX_StateIdle,
11977016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                            NULL);
11987016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
11997016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  ret = mSwitchToExecSem.WaitTimeout(OMX_CMD_TIMEOUT);
12007016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  if (ret != NO_ERROR){
12017016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    CAMHAL_LOGEB("Timeout IDLE STATE SWITCH %d", ret);
12027016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    goto EXIT;
12037016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  }
12047016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  mComponentState = OMX_StateIdle;
12057016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  CAMHAL_LOGVB("OMX_SendCommand(OMX_StateIdle) 0x%x", eError);
12067016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
12077016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  ///Register for EXECUTING state switch event
12087016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
12097016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                         OMX_EventCmdComplete,
12107016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                         OMX_CommandStateSet,
12117016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                         OMX_StateExecuting,
12127016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                         mSwitchToExecSem);
12137016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  if(ret!=NO_ERROR)
12147016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    {
12157016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng      CAMHAL_LOGEB("Error in EXECUTING STATE SWITCH %d", ret);
12167016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng      goto EXIT;
12177016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    }
12187016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  eError = OMX_SendCommand (mCameraAdapterParameters.mHandleComp ,
12197016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                            OMX_CommandStateSet,
12207016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                            OMX_StateExecuting,
12217016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng                            NULL);
12227016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
12237016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  ret = mSwitchToExecSem.WaitTimeout(OMX_CMD_TIMEOUT);
12247016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  if (ret != NO_ERROR){
12257016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    CAMHAL_LOGEB("Timeout EXEC STATE SWITCH %d", ret);
12267016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    goto EXIT;
12277016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  }
12287016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  mComponentState = OMX_StateExecuting;
12297016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  CAMHAL_LOGVB("OMX_SendCommand(OMX_StateExecuting) 0x%x", eError);
12307016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
1231312fa6b1a184b5e3aaedf66e6a348d17d9e38bd8Tyler Luu  mStateSwitchLock.unlock();
12327016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
12337016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  LOG_FUNCTION_NAME_EXIT;
12347016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  return ret;
12357016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
12367016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng EXIT:
12377016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
12387016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  performCleanupAfterError();
1239312fa6b1a184b5e3aaedf66e6a348d17d9e38bd8Tyler Luu  mStateSwitchLock.unlock();
12407016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  LOG_FUNCTION_NAME_EXIT;
12417016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng  return (ret | ErrorUtils::omxToAndroidError(eError));
12427016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng}
12437016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
1244c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::switchToLoaded()
1245c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
1246c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
1247c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
1248c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1249c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
1250c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1251312fa6b1a184b5e3aaedf66e6a348d17d9e38bd8Tyler Luu    Mutex::Autolock lock(mStateSwitchLock);
1252312fa6b1a184b5e3aaedf66e6a348d17d9e38bd8Tyler Luu
12536b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    if ( mComponentState == OMX_StateLoaded  || mComponentState == OMX_StateInvalid)
1254c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
12556b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        CAMHAL_LOGDA("Already in OMX_Loaded state or OMX_StateInvalid state");
1256b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman        return NO_ERROR;
1257c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1258c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1259c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( 0 != mSwitchToLoadedSem.Count() )
1260c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1261c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error mSwitchToLoadedSem semaphore count %d", mSwitchToLoadedSem.Count());
1262c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
1263c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1264c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1265c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Register for EXECUTING state transition.
1266c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///This method just inserts a message in Event Q, which is checked in the callback
1267c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///The sempahore passed is signalled by the callback
1268c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
1269c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_EventCmdComplete,
1270c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_CommandStateSet,
1271c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_StateIdle,
1272c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           mSwitchToLoadedSem);
1273c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1274c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(ret!=NO_ERROR)
1275c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1276c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error in registering for event %d", ret);
1277c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
1278c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1279c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1280c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    eError = OMX_SendCommand (mCameraAdapterParameters.mHandleComp,
1281c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                              OMX_CommandStateSet,
1282c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                              OMX_StateIdle,
1283c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                              NULL);
1284c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1285c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(eError!=OMX_ErrorNone)
1286c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1287c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("OMX_SendCommand(OMX_StateIdle) - %x", eError);
1288c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1289c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1290c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
1291c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1292c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Wait for the EXECUTING ->IDLE transition to arrive
1293c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1294c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGDA("EXECUTING->IDLE state changed");
1295c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = mSwitchToLoadedSem.WaitTimeout(OMX_CMD_TIMEOUT);
12966b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
12976b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    //If somethiing bad happened while we wait
12986b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    if (mComponentState == OMX_StateInvalid)
12996b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng      {
13006b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        CAMHAL_LOGEA("Invalid State after EXECUTING->IDLE Exitting!!!");
13016b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        goto EXIT;
13026b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng      }
13036b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
1304c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
1305c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1306c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDA("EXECUTING->IDLE state changed");
1307c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1308c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
1309c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
13109b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng        ret |= RemoveEvent(mCameraAdapterParameters.mHandleComp,
1311c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_EventCmdComplete,
1312c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_CommandStateSet,
1313c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_StateIdle,
1314c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           NULL);
1315c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Timeout expired on EXECUTING->IDLE state change");
1316c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
1317c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1318c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1319c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Register for LOADED state transition.
1320c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///This method just inserts a message in Event Q, which is checked in the callback
1321c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///The sempahore passed is signalled by the callback
1322c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
1323c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_EventCmdComplete,
1324c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_CommandStateSet,
1325c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_StateLoaded,
1326c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           mSwitchToLoadedSem);
1327c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1328c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(ret!=NO_ERROR)
1329c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1330c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error in registering for event %d", ret);
1331c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
1332c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1333c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1334c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    eError = OMX_SendCommand (mCameraAdapterParameters.mHandleComp,
1335c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                              OMX_CommandStateSet,
1336c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                              OMX_StateLoaded,
1337c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                              NULL);
1338c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1339c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(eError!=OMX_ErrorNone)
1340c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1341c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("OMX_SendCommand(OMX_StateLoaded) - %x", eError);
1342c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1343c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
1344c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1345c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGDA("Switching IDLE->LOADED state");
1346c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = mSwitchToLoadedSem.WaitTimeout(OMX_CMD_TIMEOUT);
13476b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
13486b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    //If somethiing bad happened while we wait
13496b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    if (mComponentState == OMX_StateInvalid)
13506b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng      {
13516b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        CAMHAL_LOGEA("Invalid State after IDLE->LOADED Exitting!!!");
13526b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        goto EXIT;
13536b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng      }
13546b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
1355c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
1356c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1357c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDA("IDLE->LOADED state changed");
1358c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1359c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
1360c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
13619b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng        ret |= RemoveEvent(mCameraAdapterParameters.mHandleComp,
1362c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_EventCmdComplete,
1363c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_CommandStateSet,
1364c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_StateLoaded,
1365c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           NULL);
1366c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Timeout expired on IDLE->LOADED state change");
1367c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
1368c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1369c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1370c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mComponentState = OMX_StateLoaded;
1371c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1372c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Register for Preview port ENABLE event
1373c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
1374c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_EventCmdComplete,
1375c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_CommandPortEnable,
1376c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           mCameraAdapterParameters.mPrevPortIndex,
1377c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           mSwitchToLoadedSem);
1378c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1379c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR != ret )
1380c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1381c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error in registering for event %d", ret);
1382c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
1383c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1384c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1385c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Enable Preview Port
1386c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    eError = OMX_SendCommand(mCameraAdapterParameters.mHandleComp,
1387c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                             OMX_CommandPortEnable,
1388c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                             mCameraAdapterParameters.mPrevPortIndex,
1389c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                             NULL);
1390c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1391c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
13927016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    CAMHAL_LOGDB("OMX_SendCommand(OMX_CommandStateSet) 0x%x", eError);
1393c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
1394c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1395c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGDA("Enabling Preview port");
1396c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Wait for state to switch to idle
1397c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = mSwitchToLoadedSem.WaitTimeout(OMX_CMD_TIMEOUT);
13986b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
13996b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    //If somethiing bad happened while we wait
14006b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    if (mComponentState == OMX_StateInvalid)
14016b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng      {
14026b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        CAMHAL_LOGEA("Invalid State after Enabling Preview port Exitting!!!");
14036b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        goto EXIT;
14046b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng      }
14056b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
1406c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
1407c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1408c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDA("Preview port enabled!");
1409c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1410c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
1411c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
14129b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng        ret |= RemoveEvent(mCameraAdapterParameters.mHandleComp,
1413c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_EventCmdComplete,
1414c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_CommandPortEnable,
1415c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           mCameraAdapterParameters.mPrevPortIndex,
1416c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           NULL);
1417c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Preview enable timedout");
1418b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman
1419c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
1420c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1421c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1422b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    return (ret | ErrorUtils::omxToAndroidError(eError));
1423c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1424b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar RamanEXIT:
1425b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
1426b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    performCleanupAfterError();
1427c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
1428b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    return (ret | ErrorUtils::omxToAndroidError(eError));
1429c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
1430c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1431c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::UseBuffersPreview(void* bufArr, int num)
1432c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
1433c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
1434c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
1435c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int tmpHeight, tmpWidth;
1436c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1437c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
1438c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1439c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Flag to determine whether it is 3D camera or not
1440c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    bool isS3d = false;
1441c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    const char *valstr = NULL;
1442c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( (valstr = mParams.get(TICameraParameters::KEY_S3D_SUPPORTED)) != NULL) {
1443c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        isS3d = (strcmp(valstr, "true") == 0);
1444c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
1445c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1446c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(!bufArr)
1447c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1448c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("NULL pointer passed for buffArr");
1449c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOG_FUNCTION_NAME_EXIT;
1450c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return BAD_VALUE;
1451c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1452c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1453c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraPortParameters * mPreviewData = NULL;
1454c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraPortParameters *measurementData = NULL;
1455c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mPreviewData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex];
1456c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    measurementData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mMeasurementPortIndex];
1457c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mPreviewData->mNumBufs = num ;
1458c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    uint32_t *buffers = (uint32_t*)bufArr;
1459c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1460c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( 0 != mUsePreviewSem.Count() )
1461c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1462c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error mUsePreviewSem semaphore count %d", mUsePreviewSem.Count());
1463c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOG_FUNCTION_NAME_EXIT;
1464c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return NO_INIT;
1465c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1466c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1467c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(mPreviewData->mNumBufs != num)
1468c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1469c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Current number of buffers doesnt equal new num of buffers passed!");
1470c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOG_FUNCTION_NAME_EXIT;
1471c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return BAD_VALUE;
1472c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1473c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1474312fa6b1a184b5e3aaedf66e6a348d17d9e38bd8Tyler Luu    mStateSwitchLock.lock();
14757016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
1476c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( mComponentState == OMX_StateLoaded )
1477c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1478c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1479c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = setLDC(mIPP);
1480c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NO_ERROR != ret )
1481c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1482c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("setLDC() failed %d", ret);
1483c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            LOG_FUNCTION_NAME_EXIT;
1484c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            return ret;
1485c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1486c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1487c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = setNSF(mIPP);
1488c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NO_ERROR != ret )
1489c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1490c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("setNSF() failed %d", ret);
1491c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            LOG_FUNCTION_NAME_EXIT;
1492c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            return ret;
1493c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1494c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1495c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = setCaptureMode(mCapMode);
1496c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NO_ERROR != ret )
1497c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1498c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("setCaptureMode() failed %d", ret);
1499c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            LOG_FUNCTION_NAME_EXIT;
1500c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            return ret;
1501c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1502c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1503c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("Camera Mode = %d", mCapMode);
1504c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1505c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if( ( mCapMode == OMXCameraAdapter::VIDEO_MODE ) ||
1506c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ( isS3d && (mCapMode == OMXCameraAdapter::HIGH_QUALITY)) )
1507c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1508c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ///Enable/Disable Video Noise Filter
1509c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = enableVideoNoiseFilter(mVnfEnabled);
1510c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR != ret)
1511c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
1512c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEB("Error configuring VNF %x", ret);
1513c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                return ret;
1514c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
1515c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1516c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ///Enable/Disable Video Stabilization
1517c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = enableVideoStabilization(mVstabEnabled);
1518c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR != ret)
1519c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
1520c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEB("Error configuring VSTAB %x", ret);
1521c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                return ret;
1522c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
1523c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1524c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
1525c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1526c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = enableVideoNoiseFilter(false);
1527c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR != ret)
1528c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
1529c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEB("Error configuring VNF %x", ret);
1530c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                return ret;
1531c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
1532c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ///Enable/Disable Video Stabilization
1533c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = enableVideoStabilization(false);
1534c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR != ret)
1535c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
1536c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEB("Error configuring VSTAB %x", ret);
1537c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                return ret;
1538c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
1539c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1540c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1541c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1542c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = setSensorOrientation(mSensorOrientation);
1543c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR != ret )
1544c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1545c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error configuring Sensor Orientation %x", ret);
1546c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mSensorOrientation = 0;
1547c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1548c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1549c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = setVFramerate(mPreviewData->mMinFrameRate, mPreviewData->mMaxFrameRate);
1550c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( ret != NO_ERROR )
1551c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1552c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("VFR configuration failed 0x%x", ret);
1553c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOG_FUNCTION_NAME_EXIT;
1554c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return ret;
1555c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1556c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1557c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( mComponentState == OMX_StateLoaded )
1558c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1559c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ///Register for IDLE state switch event
1560c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
1561c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_EventCmdComplete,
1562c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_CommandStateSet,
1563c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_StateIdle,
1564c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               mUsePreviewSem);
1565c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1566c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if(ret!=NO_ERROR)
1567c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1568c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("Error in registering for event %d", ret);
1569c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            goto EXIT;
1570c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1571c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1572c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ///Once we get the buffers, move component state to idle state and pass the buffers to OMX comp using UseBuffer
1573c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        eError = OMX_SendCommand (mCameraAdapterParameters.mHandleComp ,
1574c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                  OMX_CommandStateSet,
1575c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                  OMX_StateIdle,
1576c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                  NULL);
1577c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1578c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("OMX_SendCommand(OMX_CommandStateSet) 0x%x", eError);
1579c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1580c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
1581c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1582c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mComponentState = OMX_StateIdle;
1583c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1584c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
1585c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1586c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ///Register for Preview port ENABLE event
1587c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
1588c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   OMX_EventCmdComplete,
1589c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   OMX_CommandPortEnable,
1590c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   mCameraAdapterParameters.mPrevPortIndex,
1591c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   mUsePreviewSem);
1592c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1593c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR != ret )
1594c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
1595c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEB("Error in registering for event %d", ret);
1596c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                goto EXIT;
1597c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
1598c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1599c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ///Enable Preview Port
1600c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            eError = OMX_SendCommand(mCameraAdapterParameters.mHandleComp,
1601c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                     OMX_CommandPortEnable,
1602c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                     mCameraAdapterParameters.mPrevPortIndex,
1603c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                     NULL);
1604c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1605c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1606c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1607c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Configure DOMX to use either gralloc handles or vptrs
1608c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_TI_PARAMUSENATIVEBUFFER domxUseGrallocHandles;
1609c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_INIT_STRUCT_PTR (&domxUseGrallocHandles, OMX_TI_PARAMUSENATIVEBUFFER);
1610c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1611c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    domxUseGrallocHandles.nPortIndex = mCameraAdapterParameters.mPrevPortIndex;
1612c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    domxUseGrallocHandles.bEnable = OMX_TRUE;
1613c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1614c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    eError = OMX_SetParameter(mCameraAdapterParameters.mHandleComp,
1615c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                            (OMX_INDEXTYPE)OMX_TI_IndexUseNativeBuffers, &domxUseGrallocHandles);
1616c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(eError!=OMX_ErrorNone)
1617c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1618c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("OMX_SetParameter - %x", eError);
1619c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1620c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
1621c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1622c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_BUFFERHEADERTYPE *pBufferHdr;
1623c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    for(int index=0;index<num;index++) {
1624c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1625c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("OMX_UseBuffer(0x%x)", buffers[index]);
1626c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        eError = OMX_UseBuffer( mCameraAdapterParameters.mHandleComp,
1627c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                &pBufferHdr,
1628c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                mCameraAdapterParameters.mPrevPortIndex,
1629c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                0,
1630c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                mPreviewData->mBufSize,
1631c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                (OMX_U8*)buffers[index]);
1632c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if(eError!=OMX_ErrorNone)
1633c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1634c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("OMX_UseBuffer-0x%x", eError);
1635c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1636c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
1637c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1638c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        //pBufferHdr->pAppPrivate =  (OMX_PTR)pBufferHdr;
1639c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        pBufferHdr->nSize = sizeof(OMX_BUFFERHEADERTYPE);
1640c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        pBufferHdr->nVersion.s.nVersionMajor = 1 ;
1641c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        pBufferHdr->nVersion.s.nVersionMinor = 1 ;
1642c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        pBufferHdr->nVersion.s.nRevision = 0 ;
1643c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        pBufferHdr->nVersion.s.nStep =  0;
1644c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mPreviewData->mBufferHeader[index] = pBufferHdr;
1645c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
1646c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1647c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( mMeasurementEnabled )
1648c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1649c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1650c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        for( int i = 0; i < num; i++ )
1651c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1652c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            OMX_BUFFERHEADERTYPE *pBufHdr;
1653c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            eError = OMX_UseBuffer( mCameraAdapterParameters.mHandleComp,
1654c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                    &pBufHdr,
1655c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                    mCameraAdapterParameters.mMeasurementPortIndex,
1656c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                    0,
1657c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                    measurementData->mBufSize,
1658c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                    (OMX_U8*)(mPreviewDataBuffers[i]));
1659c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1660c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev             if ( eError == OMX_ErrorNone )
1661c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
1662c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                pBufHdr->nSize = sizeof(OMX_BUFFERHEADERTYPE);
1663c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                pBufHdr->nVersion.s.nVersionMajor = 1 ;
1664c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                pBufHdr->nVersion.s.nVersionMinor = 1 ;
1665c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                pBufHdr->nVersion.s.nRevision = 0 ;
1666c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                pBufHdr->nVersion.s.nStep =  0;
1667c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                measurementData->mBufferHeader[i] = pBufHdr;
1668c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
1669c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            else
1670c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
1671c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEB("OMX_UseBuffer -0x%x", eError);
1672c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ret = BAD_VALUE;
1673c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                break;
1674c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
1675c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1676c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1677c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1678c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1679c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGDA("Registering preview buffers");
1680c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1681c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = mUsePreviewSem.WaitTimeout(OMX_CMD_TIMEOUT);
16826b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
16836b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    //If somethiing bad happened while we wait
16846b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    if (mComponentState == OMX_StateInvalid)
16856b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng      {
16866b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        CAMHAL_LOGEA("Invalid State after Registering preview buffers Exitting!!!");
16876b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        goto EXIT;
16886b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng      }
16896b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
1690c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
1691c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1692c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDA("Preview buffer registration successfull");
1693c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1694c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
1695c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1696c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( mComponentState == OMX_StateLoaded )
1697c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
16989b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng            ret |= RemoveEvent(mCameraAdapterParameters.mHandleComp,
1699c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_EventCmdComplete,
1700c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_CommandStateSet,
1701c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_StateIdle,
1702c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               NULL);
1703c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1704c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
1705c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1706c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret |= SignalEvent(mCameraAdapterParameters.mHandleComp,
1707c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_EventCmdComplete,
1708c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_CommandPortEnable,
1709c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               mCameraAdapterParameters.mPrevPortIndex,
1710c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               NULL);
1711c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1712c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Timeout expired on preview buffer registration");
1713c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
1714c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1715c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1716c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
1717c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1718c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return (ret | ErrorUtils::omxToAndroidError(eError));
1719c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1720c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///If there is any failure, we reach here.
1721c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Here, we do any resource freeing and convert from OMX error code to Camera Hal error code
1722b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar RamanEXIT:
1723b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
1724b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    performCleanupAfterError();
1725c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
1726c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1727c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
1728c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1729c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return (ret | ErrorUtils::omxToAndroidError(eError));
1730c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
1731c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1732c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::startPreview()
1733c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
1734c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
1735c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
1736c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraPortParameters *mPreviewData = NULL;
1737c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraPortParameters *measurementData = NULL;
1738c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1739c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
1740c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1741c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if( 0 != mStartPreviewSem.Count() )
1742c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1743c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error mStartPreviewSem semaphore count %d", mStartPreviewSem.Count());
1744c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOG_FUNCTION_NAME_EXIT;
1745c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return NO_INIT;
1746c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1747c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1748c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mPreviewData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex];
1749c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    measurementData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mMeasurementPortIndex];
1750c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1751c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if( OMX_StateIdle == mComponentState )
1752c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1753c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ///Register for EXECUTING state transition.
1754c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ///This method just inserts a message in Event Q, which is checked in the callback
1755c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ///The sempahore passed is signalled by the callback
1756c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
1757c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_EventCmdComplete,
1758c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_CommandStateSet,
1759c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_StateExecuting,
1760c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               mStartPreviewSem);
1761c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1762c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if(ret!=NO_ERROR)
1763c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1764c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("Error in registering for event %d", ret);
1765c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            goto EXIT;
1766c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1767c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1768c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ///Switch to EXECUTING state
1769c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        eError = OMX_SendCommand(mCameraAdapterParameters.mHandleComp,
1770c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                 OMX_CommandStateSet,
1771c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                 OMX_StateExecuting,
1772c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                 NULL);
1773c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1774c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if(eError!=OMX_ErrorNone)
1775c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1776c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("OMX_SendCommand(OMX_StateExecuting)-0x%x", eError);
1777c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1778c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1779c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDA("+Waiting for component to go into EXECUTING state");
1780c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = mStartPreviewSem.WaitTimeout(OMX_CMD_TIMEOUT);
17816b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
17826b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        //If somethiing bad happened while we wait
17836b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        if (mComponentState == OMX_StateInvalid)
17846b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng          {
17856b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng            CAMHAL_LOGEA("Invalid State after IDLE_EXECUTING Exitting!!!");
17866b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng            goto EXIT;
17876b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng          }
17886b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
1789c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NO_ERROR == ret )
1790c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1791c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDA("+Great. Component went into executing state!!");
1792c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1793c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
1794c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
17959b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng            ret |= RemoveEvent(mCameraAdapterParameters.mHandleComp,
1796c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_EventCmdComplete,
1797c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_CommandStateSet,
1798c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               OMX_StateExecuting,
1799c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                               NULL);
1800c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDA("Timeout expired on executing state switch!");
1801c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            goto EXIT;
1802c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1803c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1804c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mComponentState = OMX_StateExecuting;
1805c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1806c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1807c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1808312fa6b1a184b5e3aaedf66e6a348d17d9e38bd8Tyler Luu    mStateSwitchLock.unlock();
18097016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
1810c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //Queue all the buffers on preview port
1811c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    for(int index=0;index< mPreviewData->mMaxQueueable;index++)
1812c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1813c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("Queuing buffer on Preview port - 0x%x", (uint32_t)mPreviewData->mBufferHeader[index]->pBuffer);
1814c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        eError = OMX_FillThisBuffer(mCameraAdapterParameters.mHandleComp,
1815c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    (OMX_BUFFERHEADERTYPE*)mPreviewData->mBufferHeader[index]);
1816c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if(eError!=OMX_ErrorNone)
1817c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1818c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("OMX_FillThisBuffer-0x%x", eError);
1819c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1820c493114006e0e136c8c88c2fc9865994054959bfSundar Raman        mFramesWithDucati++;
1821c493114006e0e136c8c88c2fc9865994054959bfSundar Raman#ifdef DEGUG_LOG
1822c493114006e0e136c8c88c2fc9865994054959bfSundar Raman        mBuffersWithDucati.add((uint32_t)mPreviewData->mBufferHeader[index]->pBuffer,1);
1823c493114006e0e136c8c88c2fc9865994054959bfSundar Raman#endif
1824c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
1825c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1826c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1827c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( mMeasurementEnabled )
1828c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1829c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1830c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        for(int index=0;index< mPreviewData->mNumBufs;index++)
1831c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1832c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDB("Queuing buffer on Measurement port - 0x%x", (uint32_t) measurementData->mBufferHeader[index]->pBuffer);
1833c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            eError = OMX_FillThisBuffer(mCameraAdapterParameters.mHandleComp,
1834c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                            (OMX_BUFFERHEADERTYPE*) measurementData->mBufferHeader[index]);
1835c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if(eError!=OMX_ErrorNone)
1836c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
1837c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEB("OMX_FillThisBuffer-0x%x", eError);
1838c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
1839c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
1840c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1841c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1842c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1843c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1844782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev    if ( mPending3Asettings )
1845782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev        apply3Asettings(mParameters3A);
1846782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev
1847c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //Query current focus distance after
1848c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //starting the preview
1849c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    updateFocusDistances(mParameters);
1850c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1851c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //reset frame rate estimates
1852c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mFPS = 0.0f;
1853c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mLastFPS = 0.0f;
1854c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mFrameCount = 0;
1855c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mLastFrameCount = 0;
1856c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mIter = 1;
1857c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mLastFPSTime = systemTime();
1858c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1859c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
1860c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1861b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    return (ret | ErrorUtils::omxToAndroidError(eError));
1862c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1863c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    EXIT:
1864c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1865c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
1866b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    performCleanupAfterError();
1867312fa6b1a184b5e3aaedf66e6a348d17d9e38bd8Tyler Luu    mStateSwitchLock.unlock();
1868c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
1869c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1870c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return (ret | ErrorUtils::omxToAndroidError(eError));
1871c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1872c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
1873c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1874c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::stopPreview()
1875c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
1876c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
1877c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1878c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
1879c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
1880c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1881c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraPortParameters *mCaptureData , *mPreviewData, *measurementData;
1882c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mCaptureData = mPreviewData = measurementData = NULL;
1883c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1884c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mPreviewData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex];
1885c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mCaptureData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mImagePortIndex];
1886c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    measurementData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mMeasurementPortIndex];
1887c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1888c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( mComponentState != OMX_StateExecuting )
1889c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1890c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Calling StopPreview() when not in EXECUTING state");
1891c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOG_FUNCTION_NAME_EXIT;
1892c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return NO_INIT;
1893c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1894c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1895c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = cancelAutoFocus();
1896c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(ret!=NO_ERROR)
1897c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    {
1898c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error canceling autofocus %d", ret);
1899c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        // Error, but we probably still want to continue to stop preview
1900c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
1901c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1902c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( 0 != mStopPreviewSem.Count() )
1903c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1904c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error mStopPreviewSem semaphore count %d", mStopPreviewSem.Count());
1905c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOG_FUNCTION_NAME_EXIT;
1906c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return NO_INIT;
1907c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1908c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1909fab72086a1fa5d93037ec1355bc8b01355db0f41Tyler Luu    ret = disableImagePort();
1910fab72086a1fa5d93037ec1355bc8b01355db0f41Tyler Luu    if ( NO_ERROR != ret ) {
1911fab72086a1fa5d93037ec1355bc8b01355db0f41Tyler Luu        CAMHAL_LOGEB("disable image port failed 0x%x", ret);
1912fab72086a1fa5d93037ec1355bc8b01355db0f41Tyler Luu        goto EXIT;
1913fab72086a1fa5d93037ec1355bc8b01355db0f41Tyler Luu    }
1914fab72086a1fa5d93037ec1355bc8b01355db0f41Tyler Luu
1915c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGDB("Average framerate: %f", mFPS);
1916c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1917c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //Avoid state switching of the OMX Component
1918c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = flushBuffers();
1919c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR != ret )
1920c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1921c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Flush Buffers failed 0x%x", ret);
1922c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
1923c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1924c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1925c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Register for Preview port Disable event
1926c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
1927c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_EventCmdComplete,
1928c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_CommandPortDisable,
1929c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           mCameraAdapterParameters.mPrevPortIndex,
1930c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           mStopPreviewSem);
1931c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1932c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Disable Preview Port
1933c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    eError = OMX_SendCommand(mCameraAdapterParameters.mHandleComp,
1934c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                             OMX_CommandPortDisable,
1935c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                             mCameraAdapterParameters.mPrevPortIndex,
1936c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                             NULL);
1937c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1938c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Free the OMX Buffers
1939c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    for ( int i = 0 ; i < mPreviewData->mNumBufs ; i++ )
1940c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1941c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        eError = OMX_FreeBuffer(mCameraAdapterParameters.mHandleComp,
1942c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                mCameraAdapterParameters.mPrevPortIndex,
1943c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                mPreviewData->mBufferHeader[i]);
1944c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1945c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if(eError!=OMX_ErrorNone)
1946c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1947c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("OMX_FreeBuffer - %x", eError);
1948c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1949c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
1950c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1951c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1952c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( mMeasurementEnabled )
1953c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1954c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1955c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            for ( int i = 0 ; i < measurementData->mNumBufs ; i++ )
1956c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
1957c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                eError = OMX_FreeBuffer(mCameraAdapterParameters.mHandleComp,
1958c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                        mCameraAdapterParameters.mMeasurementPortIndex,
1959c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                        measurementData->mBufferHeader[i]);
1960c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                if(eError!=OMX_ErrorNone)
1961c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    {
1962c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    CAMHAL_LOGEB("OMX_FreeBuffer - %x", eError);
1963c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    }
1964c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);
1965c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
1966c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1967c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
1968c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            Mutex::Autolock lock(mPreviewDataBufferLock);
1969c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mPreviewDataBuffersAvailable.clear();
1970c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
1971c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1972c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1973c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1974c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGDA("Disabling preview port");
1975c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = mStopPreviewSem.WaitTimeout(OMX_CMD_TIMEOUT);
19766b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
19776b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    //If somethiing bad happened while we wait
19786b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng    if (mComponentState == OMX_StateInvalid)
19796b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng      {
19806b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        CAMHAL_LOGEA("Invalid State after Disabling preview port Exitting!!!");
19816b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng        goto EXIT;
19826b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng      }
19836b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
1984c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
1985c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
1986c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDA("Preview port disabled");
1987c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1988c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
1989c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
19909b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng        ret |= RemoveEvent(mCameraAdapterParameters.mHandleComp,
1991c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_EventCmdComplete,
1992c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           OMX_CommandPortDisable,
1993c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           mCameraAdapterParameters.mPrevPortIndex,
1994c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                           NULL);
1995c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Timeout expired on preview port disable");
1996c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
1997c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
1998c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
1999c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2000c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        Mutex::Autolock lock(mPreviewBufferLock);
2001c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ///Clear all the available preview buffers
2002c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mPreviewBuffersAvailable.clear();
2003c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2004c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2005c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    switchToLoaded();
2006c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
20078cb48e582f4f3c93aa25159c1116a7c83bfa388aSundar Raman
20088cb48e582f4f3c93aa25159c1116a7c83bfa388aSundar Raman    mFirstTimeInit = true;
2009b5abb7cbdf48ecccbf0e6d5e61a1846c0c4b28a5Tyler Luu    mPendingCaptureSettings = 0;
2010c493114006e0e136c8c88c2fc9865994054959bfSundar Raman    mFramesWithDucati = 0;
2011c493114006e0e136c8c88c2fc9865994054959bfSundar Raman    mFramesWithDisplay = 0;
2012c493114006e0e136c8c88c2fc9865994054959bfSundar Raman    mFramesWithEncoder = 0;
20138cb48e582f4f3c93aa25159c1116a7c83bfa388aSundar Raman
2014c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2015c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2016c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return (ret | ErrorUtils::omxToAndroidError(eError));
2017c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2018b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar RamanEXIT:
2019b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, ret, eError);
2020c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    {
2021c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    Mutex::Autolock lock(mPreviewBufferLock);
2022c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Clear all the available preview buffers
2023c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mPreviewBuffersAvailable.clear();
2024c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2025b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    performCleanupAfterError();
2026c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2027c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return (ret | ErrorUtils::omxToAndroidError(eError));
2028c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2029c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2030c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2031c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::setSensorOverclock(bool enable)
2032c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2033c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
2034c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
2035c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_CONFIG_BOOLEANTYPE bOMX;
2036c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2037c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2038c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2039c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( OMX_StateLoaded != mComponentState )
2040c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2041c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDA("OMX component is not in loaded state");
2042c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return ret;
2043c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2044c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2045c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
2046c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2047c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        OMX_INIT_STRUCT_PTR (&bOMX, OMX_CONFIG_BOOLEANTYPE);
2048c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2049c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( enable )
2050c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2051c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            bOMX.bEnabled = OMX_TRUE;
2052c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2053c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
2054c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2055c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            bOMX.bEnabled = OMX_FALSE;
2056c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2057c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2058c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("Configuring Sensor overclock mode 0x%x", bOMX.bEnabled);
2059c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        eError = OMX_SetParameter(mCameraAdapterParameters.mHandleComp, ( OMX_INDEXTYPE ) OMX_TI_IndexParamSensorOverClockMode, &bOMX);
2060c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( OMX_ErrorNone != eError )
2061c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2062c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("Error while setting Sensor overclock 0x%x", eError);
2063c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = BAD_VALUE;
2064c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2065c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
2066c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2067c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mSensorOverclock = enable;
2068c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2069c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2070c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2071c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2072c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2073c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
2074c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2075c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2076c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::printComponentVersion(OMX_HANDLETYPE handle)
2077c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2078c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
2079c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
2080c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_VERSIONTYPE compVersion;
2081c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    char compName[OMX_MAX_STRINGNAME_SIZE];
2082c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    char *currentUUID = NULL;
2083c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    size_t offset = 0;
2084c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2085c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2086c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2087c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NULL == handle )
2088c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2089c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Invalid OMX Handle =0x%x",  ( unsigned int ) handle);
2090c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = -EINVAL;
2091c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2092c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2093c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mCompUUID[0] = 0;
2094c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2095c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
2096c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2097c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        eError = OMX_GetComponentVersion(handle,
2098c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                      compName,
2099c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                      &compVersion,
2100c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                      &mCompRevision,
2101c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                      &mCompUUID
2102c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                    );
2103c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( OMX_ErrorNone != eError )
2104c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2105c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("OMX_GetComponentVersion returned 0x%x", eError);
2106c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = BAD_VALUE;
2107c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2108c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2109c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2110c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
2111c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2112c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGVB("OMX Component name: [%s]", compName);
2113c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGVB("OMX Component version: [%u]", ( unsigned int ) compVersion.nVersion);
2114c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGVB("Spec version: [%u]", ( unsigned int ) mCompRevision.nVersion);
2115c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGVB("Git Commit ID: [%s]", mCompUUID);
2116c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        currentUUID = ( char * ) mCompUUID;
2117c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2118c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2119c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NULL != currentUUID )
2120c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2121c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        offset = strlen( ( const char * ) mCompUUID) + 1;
2122c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( (int)currentUUID + (int)offset - (int)mCompUUID < OMX_MAX_STRINGNAME_SIZE )
2123c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2124c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            currentUUID += offset;
2125c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGVB("Git Branch: [%s]", currentUUID);
2126c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2127c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
2128c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2129c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = BAD_VALUE;
2130c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2131c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2132c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2133c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
2134c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2135c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        offset = strlen( ( const char * ) currentUUID) + 1;
2136c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2137c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( (int)currentUUID + (int)offset - (int)mCompUUID < OMX_MAX_STRINGNAME_SIZE )
2138c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2139c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            currentUUID += offset;
2140c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGVB("Build date and time: [%s]", currentUUID);
2141c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2142c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
2143c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2144c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = BAD_VALUE;
2145c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2146c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2147c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2148c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
2149c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2150c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        offset = strlen( ( const char * ) currentUUID) + 1;
2151c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2152c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( (int)currentUUID + (int)offset - (int)mCompUUID < OMX_MAX_STRINGNAME_SIZE )
2153c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2154c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            currentUUID += offset;
2155c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGVB("Build description: [%s]", currentUUID);
2156c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2157c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
2158c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2159c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = BAD_VALUE;
2160c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2161c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2162c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2163c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2164c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2165c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
2166c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2167c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2168c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::autoFocus()
2169c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2170c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
2171c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    TIUTILS::Message msg;
2172c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2173c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2174c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2175c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    msg.command = CommandHandler::CAMERA_PERFORM_AUTOFOCUS;
2176c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    msg.arg1 = mErrorNotifier;
2177c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = mCommandHandler->put(&msg);
2178c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2179c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2180c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2181c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
2182c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2183c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2184c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::takePicture()
2185c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2186c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
2187c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    TIUTILS::Message msg;
2188c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2189c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2190c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2191c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    msg.command = CommandHandler::CAMERA_START_IMAGE_CAPTURE;
2192c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    msg.arg1 = mErrorNotifier;
2193c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = mCommandHandler->put(&msg);
2194c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2195c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2196c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2197c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
2198c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2199c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2200c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::startVideoCapture()
2201c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2202c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return BaseCameraAdapter::startVideoCapture();
2203c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2204c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2205c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::stopVideoCapture()
2206c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2207c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return BaseCameraAdapter::stopVideoCapture();
2208c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2209c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2210c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev//API to get the frame size required to be allocated. This size is used to override the size passed
2211c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev//by camera service when VSTAB/VNF is turned ON for example
2212c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::getFrameSize(size_t &width, size_t &height)
2213c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2214c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
2215c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
2216c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_CONFIG_RECTTYPE tFrameDim;
2217c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2218c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2219c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2220c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_INIT_STRUCT_PTR (&tFrameDim, OMX_CONFIG_RECTTYPE);
2221c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    tFrameDim.nPortIndex = mCameraAdapterParameters.mPrevPortIndex;
2222c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2223c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( mOMXStateSwitch )
2224c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2225c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = switchToLoaded();
2226c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NO_ERROR != ret )
2227c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2228c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("switchToLoaded() failed 0x%x", ret);
2229c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            goto exit;
2230c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2231c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2232c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mOMXStateSwitch = false;
2233c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2234c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2235c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( OMX_StateLoaded == mComponentState )
2236c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2237c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2238c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = setLDC(mIPP);
2239c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NO_ERROR != ret )
2240c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2241c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("setLDC() failed %d", ret);
2242c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            LOG_FUNCTION_NAME_EXIT;
2243c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            goto exit;
2244c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2245c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2246c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = setNSF(mIPP);
2247c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NO_ERROR != ret )
2248c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2249c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("setNSF() failed %d", ret);
2250c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            LOG_FUNCTION_NAME_EXIT;
2251c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            goto exit;
2252c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2253c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2254c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = setCaptureMode(mCapMode);
2255c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NO_ERROR != ret )
2256c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2257c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("setCaptureMode() failed %d", ret);
2258c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2259c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2260c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if(mCapMode == OMXCameraAdapter::VIDEO_MODE)
2261c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2262c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR == ret )
2263c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
2264c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ///Enable/Disable Video Noise Filter
2265c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ret = enableVideoNoiseFilter(mVnfEnabled);
2266c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2267c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2268c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR != ret)
2269c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
2270c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEB("Error configuring VNF %x", ret);
2271c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2272c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2273c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR == ret )
2274c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
2275c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ///Enable/Disable Video Stabilization
2276c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ret = enableVideoStabilization(mVstabEnabled);
2277c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2278c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2279c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR != ret)
2280c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
2281c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEB("Error configuring VSTAB %x", ret);
2282c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2283c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev             }
2284c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
2285c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2286c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR == ret )
2287c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
2288c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ///Enable/Disable Video Noise Filter
2289c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ret = enableVideoNoiseFilter(false);
2290c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2291c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2292c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR != ret)
2293c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
2294c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEB("Error configuring VNF %x", ret);
2295c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2296c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2297c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR == ret )
2298c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
2299c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ///Enable/Disable Video Stabilization
2300c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ret = enableVideoStabilization(false);
2301c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2302c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2303c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NO_ERROR != ret)
2304c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
2305c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEB("Error configuring VSTAB %x", ret);
2306c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2307c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2308c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2309c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2310c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2311c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = setSensorOrientation(mSensorOrientation);
2312c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR != ret )
2313c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2314c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("Error configuring Sensor Orientation %x", ret);
2315c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mSensorOrientation = 0;
2316c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2317c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2318c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
2319c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2320c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        eError = OMX_GetParameter(mCameraAdapterParameters.mHandleComp, ( OMX_INDEXTYPE ) OMX_TI_IndexParam2DBufferAllocDimension, &tFrameDim);
2321c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( OMX_ErrorNone == eError)
2322c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2323c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            width = tFrameDim.nWidth;
2324c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            height = tFrameDim.nHeight;
2325c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2326c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2327c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2328c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevexit:
2329c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2330c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGDB("Required frame size %dx%d", width, height);
2331c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2332c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2333c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
2334c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2335c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2336c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::getFrameDataSize(size_t &dataFrameSize, size_t bufferCount)
2337c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2338c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
2339c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_PARAM_PORTDEFINITIONTYPE portCheck;
2340c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
2341c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2342c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2343c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2344c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( OMX_StateLoaded != mComponentState )
2345c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2346c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Calling getFrameDataSize() when not in LOADED state");
2347c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        dataFrameSize = 0;
2348c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = BAD_VALUE;
2349c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2350c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2351c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret  )
2352c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2353c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        OMX_INIT_STRUCT_PTR(&portCheck, OMX_PARAM_PORTDEFINITIONTYPE);
2354c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.nPortIndex = mCameraAdapterParameters.mMeasurementPortIndex;
2355c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2356c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        eError = OMX_GetParameter(mCameraAdapterParameters.mHandleComp, OMX_IndexParamPortDefinition, &portCheck);
2357c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( OMX_ErrorNone != eError )
2358c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2359c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("OMX_GetParameter on OMX_IndexParamPortDefinition returned: 0x%x", eError);
2360c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            dataFrameSize = 0;
2361c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = BAD_VALUE;
2362c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2363c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2364c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2365c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
2366c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2367c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        portCheck.nBufferCountActual = bufferCount;
2368c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        eError = OMX_SetParameter(mCameraAdapterParameters.mHandleComp, OMX_IndexParamPortDefinition, &portCheck);
2369c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( OMX_ErrorNone != eError )
2370c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2371c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("OMX_SetParameter on OMX_IndexParamPortDefinition returned: 0x%x", eError);
2372c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            dataFrameSize = 0;
2373c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = BAD_VALUE;
2374c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2375c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2376c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2377c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret  )
2378c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2379c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        eError = OMX_GetParameter(mCameraAdapterParameters.mHandleComp, OMX_IndexParamPortDefinition, &portCheck);
2380c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( OMX_ErrorNone != eError )
2381c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2382c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEB("OMX_GetParameter on OMX_IndexParamPortDefinition returned: 0x%x", eError);
2383c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = BAD_VALUE;
2384c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2385c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
2386c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2387c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mCameraAdapterParameters.mCameraPortParams[portCheck.nPortIndex].mBufSize = portCheck.nBufferSize;
2388c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            dataFrameSize = portCheck.nBufferSize;
2389c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2390c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2391c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2392c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2393c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2394c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
2395c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2396c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2397c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevvoid OMXCameraAdapter::onOrientationEvent(uint32_t orientation, uint32_t tilt)
2398c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2399c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2400c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2401348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu    static const unsigned int DEGREES_TILT_IGNORE = 45;
2402c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int device_orientation = 0;
2403c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int mount_orientation = 0;
2404c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    const char *facing_direction = NULL;
2405c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2406c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    // if tilt angle is greater than DEGREES_TILT_IGNORE
2407c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    // we are going to ignore the orientation returned from
2408c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    // sensor. the orientation returned from sensor is not
2409c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    // reliable. Value of DEGREES_TILT_IGNORE may need adjusting
2410c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (tilt > DEGREES_TILT_IGNORE) {
2411c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return;
2412c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2413c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2414c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (mCapabilities) {
2415c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if (mCapabilities->get(CameraProperties::ORIENTATION_INDEX)) {
2416c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mount_orientation = atoi(mCapabilities->get(CameraProperties::ORIENTATION_INDEX));
2417c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2418c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        facing_direction = mCapabilities->get(CameraProperties::FACING_INDEX);
2419c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2420c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2421c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    // calculate device orientation relative to the sensor orientation
242200fd87fc096d092bfbe3579cd7fe2c1824e6eac0Tyler Luu    // front camera display is mirrored...needs to be accounted for when orientation
242300fd87fc096d092bfbe3579cd7fe2c1824e6eac0Tyler Luu    // is 90 or 270...since this will result in a flip on orientation otherwise
242400fd87fc096d092bfbe3579cd7fe2c1824e6eac0Tyler Luu    if (facing_direction && !strcmp(facing_direction, TICameraParameters::FACING_FRONT) &&
242500fd87fc096d092bfbe3579cd7fe2c1824e6eac0Tyler Luu        (orientation == 90 || orientation == 270)) {
2426c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        device_orientation = (orientation - mount_orientation + 360) % 360;
2427c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    } else {  // back-facing camera
2428c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        device_orientation = (orientation + mount_orientation) % 360;
2429c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2430c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2431c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (device_orientation != mDeviceOrientation) {
2432c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mDeviceOrientation = device_orientation;
2433c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2434c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mFaceDetectionLock.lock();
2435c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if (mFaceDetectionRunning) {
2436c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            // restart face detection with new rotation
2437c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            setFaceDetection(true, mDeviceOrientation);
2438c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2439c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mFaceDetectionLock.unlock();
2440c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2441c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGVB("orientation = %d tilt = %d device_orientation = %d", orientation, tilt, mDeviceOrientation);
2442c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2443c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2444c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2445c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2446c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/* Application callback Functions */
2447c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*========================================================*/
2448c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/* @ fn SampleTest_EventHandler :: Application callback   */
2449c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*========================================================*/
2450c322989ae6ff6769490828de1b5eda12b749cce9Iliyan MalchevOMX_ERRORTYPE OMXCameraAdapterEventHandler(OMX_IN OMX_HANDLETYPE hComponent,
2451c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_PTR pAppData,
2452c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_EVENTTYPE eEvent,
2453c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_U32 nData1,
2454c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_U32 nData2,
2455c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_PTR pEventData)
2456c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2457c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2458c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2459c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGDB("Event %d", eEvent);
2460c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2461c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE ret = OMX_ErrorNone;
2462c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraAdapter *oca = (OMXCameraAdapter*)pAppData;
2463c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ret = oca->OMXCameraAdapterEventHandler(hComponent, eEvent, nData1, nData2, pEventData);
2464c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2465c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2466c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
2467c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2468c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2469c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/* Application callback Functions */
2470c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*========================================================*/
2471c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/* @ fn SampleTest_EventHandler :: Application callback   */
2472c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*========================================================*/
2473c322989ae6ff6769490828de1b5eda12b749cce9Iliyan MalchevOMX_ERRORTYPE OMXCameraAdapter::OMXCameraAdapterEventHandler(OMX_IN OMX_HANDLETYPE hComponent,
2474c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_EVENTTYPE eEvent,
2475c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_U32 nData1,
2476c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_U32 nData2,
2477c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_PTR pEventData)
2478c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2479c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2480c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2481c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2482c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
2483c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGDB("+OMX_Event %x, %d %d", eEvent, (int)nData1, (int)nData2);
2484c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2485c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    switch (eEvent) {
2486c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case OMX_EventCmdComplete:
2487c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDB("+OMX_EventCmdComplete %d %d", (int)nData1, (int)nData2);
2488c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2489c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if (OMX_CommandStateSet == nData1) {
2490c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                mCameraAdapterParameters.mState = (OMX_STATETYPE) nData2;
2491c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2492c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            } else if (OMX_CommandFlush == nData1) {
2493c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGDB("OMX_CommandFlush received for port %d", (int)nData2);
2494c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2495c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            } else if (OMX_CommandPortDisable == nData1) {
2496c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGDB("OMX_CommandPortDisable received for port %d", (int)nData2);
2497c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2498c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            } else if (OMX_CommandPortEnable == nData1) {
2499c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGDB("OMX_CommandPortEnable received for port %d", (int)nData2);
2500c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2501c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            } else if (OMX_CommandMarkBuffer == nData1) {
2502c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ///This is not used currently
2503c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2504c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2505c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDA("-OMX_EventCmdComplete");
2506c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
2507c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2508c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case OMX_EventIndexSettingChanged:
2509c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDB("OMX_EventIndexSettingChanged event received data1 0x%x, data2 0x%x",
2510c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                            ( unsigned int ) nData1, ( unsigned int ) nData2);
2511c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            break;
2512c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2513c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case OMX_EventError:
2514c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDB("OMX interface failed to execute OMX command %d", (int)nData1);
2515c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGDA("See OMX_INDEXTYPE for reference");
2516c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NULL != mErrorNotifier && ( ( OMX_U32 ) OMX_ErrorHardware == nData1 ) && mComponentState != OMX_StateInvalid)
2517c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev              {
2518c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEA("***Got Fatal Error Notification***\n");
2519c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                mComponentState = OMX_StateInvalid;
25206b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                /*
25216b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                Remove any unhandled events and
25226b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                unblock any waiting semaphores
25236b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                */
25246b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                if ( !mEventSignalQ.isEmpty() )
25256b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                  {
2526348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu                    for (unsigned int i = 0 ; i < mEventSignalQ.size(); i++ )
25276b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                      {
25286b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                        CAMHAL_LOGEB("***Removing %d EVENTS***** \n", mEventSignalQ.size());
25296b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                        //remove from queue and free msg
25306b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                        TIUTILS::Message *msg = mEventSignalQ.itemAt(i);
25316b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                        if ( NULL != msg )
25326b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                          {
25336b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                            Semaphore *sem  = (Semaphore*) msg->arg3;
25346b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                            mEventSignalQ.removeAt(i);
25356b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                            if ( sem )
25366b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                              {
25376b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                                sem->Signal();
25386b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                              }
25396b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                            free(msg);
25406b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                          }
25416b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                      }
25426b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                  }
2543c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ///Report Error to App
25440ff71809874c00d540af83c123f3dc34759f1871Sundar Raman                mErrorNotifier->errorNotify(CAMERA_ERROR_FATAL);
2545c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev              }
2546c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            break;
2547c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2548c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case OMX_EventMark:
2549c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
2550c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2551c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case OMX_EventPortSettingsChanged:
2552c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
2553c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2554c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case OMX_EventBufferFlag:
2555c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
2556c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2557c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case OMX_EventResourcesAcquired:
2558c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
2559c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2560c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case OMX_EventComponentResumed:
2561c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
2562c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2563c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case OMX_EventDynamicResourcesAvailable:
2564c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
2565c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2566c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        case OMX_EventPortFormatDetected:
2567c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
2568c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2569c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        default:
2570c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        break;
2571c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2572c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2573c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///Signal to the thread(s) waiting that the event has occured
2574c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    SignalEvent(hComponent, eEvent, nData1, nData2, pEventData);
2575c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2576c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev   LOG_FUNCTION_NAME_EXIT;
2577c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev   return eError;
2578c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2579c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    EXIT:
2580c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2581c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGEB("Exiting function %s because of eError=%x", __FUNCTION__, eError);
2582c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2583c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return eError;
2584c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2585c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2586c322989ae6ff6769490828de1b5eda12b749cce9Iliyan MalchevOMX_ERRORTYPE OMXCameraAdapter::SignalEvent(OMX_IN OMX_HANDLETYPE hComponent,
2587c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_EVENTTYPE eEvent,
2588c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_U32 nData1,
2589c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_U32 nData2,
2590c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_PTR pEventData)
2591c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2592c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    Mutex::Autolock lock(mEventLock);
2593c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    TIUTILS::Message *msg;
2594c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2595c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2596c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2597c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( !mEventSignalQ.isEmpty() )
2598c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2599c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDA("Event queue not empty");
2600c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2601c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        for ( unsigned int i = 0 ; i < mEventSignalQ.size() ; i++ )
2602c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2603c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            msg = mEventSignalQ.itemAt(i);
2604c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( NULL != msg )
2605c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
2606c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                if( ( msg->command != 0 || msg->command == ( unsigned int ) ( eEvent ) )
2607c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    && ( !msg->arg1 || ( OMX_U32 ) msg->arg1 == nData1 )
2608c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    && ( !msg->arg2 || ( OMX_U32 ) msg->arg2 == nData2 )
2609c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    && msg->arg3)
2610c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    {
2611c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    Semaphore *sem  = (Semaphore*) msg->arg3;
2612c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    CAMHAL_LOGDA("Event matched, signalling sem");
2613c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    mEventSignalQ.removeAt(i);
2614c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    //Signal the semaphore provided
2615c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    sem->Signal();
2616c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    free(msg);
2617c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    break;
2618c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    }
2619c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2620c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2621c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2622c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
2623c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2624c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Event queue empty!!!");
2625c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2626c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2627c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2628c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2629c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return OMX_ErrorNone;
2630c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2631c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
26329b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi BoatengOMX_ERRORTYPE OMXCameraAdapter::RemoveEvent(OMX_IN OMX_HANDLETYPE hComponent,
26339b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                                            OMX_IN OMX_EVENTTYPE eEvent,
26349b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                                            OMX_IN OMX_U32 nData1,
26359b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                                            OMX_IN OMX_U32 nData2,
26369b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                                            OMX_IN OMX_PTR pEventData)
26379b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng{
26389b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng  Mutex::Autolock lock(mEventLock);
26399b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng  TIUTILS::Message *msg;
26409b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng  LOG_FUNCTION_NAME;
26419b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng
26429b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng  if ( !mEventSignalQ.isEmpty() )
26439b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng    {
26449b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng      CAMHAL_LOGDA("Event queue not empty");
26459b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng
26469b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng      for ( unsigned int i = 0 ; i < mEventSignalQ.size() ; i++ )
26479b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng        {
26489b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng          msg = mEventSignalQ.itemAt(i);
26499b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng          if ( NULL != msg )
26509b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng            {
26519b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng              if( ( msg->command != 0 || msg->command == ( unsigned int ) ( eEvent ) )
26529b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                  && ( !msg->arg1 || ( OMX_U32 ) msg->arg1 == nData1 )
26539b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                  && ( !msg->arg2 || ( OMX_U32 ) msg->arg2 == nData2 )
26549b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                  && msg->arg3)
26559b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                {
26569b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                  Semaphore *sem  = (Semaphore*) msg->arg3;
26579b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                  CAMHAL_LOGDA("Event matched, signalling sem");
26589b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                  mEventSignalQ.removeAt(i);
26599b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                  free(msg);
26609b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                  break;
26619b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng                }
26629b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng            }
26639b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng        }
26649b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng    }
26659b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng  else
26669b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng    {
26679b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng      CAMHAL_LOGEA("Event queue empty!!!");
26689b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng    }
26699b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng  LOG_FUNCTION_NAME_EXIT;
26709b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng
26719b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng  return OMX_ErrorNone;
26729b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng}
26739b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng
26749b981b1785b6758b2dc8b147587188e5cf0d893dAkwasi Boateng
2675c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::RegisterForEvent(OMX_IN OMX_HANDLETYPE hComponent,
2676c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_EVENTTYPE eEvent,
2677c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_U32 nData1,
2678c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN OMX_U32 nData2,
2679c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          OMX_IN Semaphore &semaphore)
2680c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2681c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
2682c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ssize_t res;
2683c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    Mutex::Autolock lock(mEventLock);
2684c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2685c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2686c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    TIUTILS::Message * msg = ( struct TIUTILS::Message * ) malloc(sizeof(struct TIUTILS::Message));
2687c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NULL != msg )
2688c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2689c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        msg->command = ( unsigned int ) eEvent;
2690c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        msg->arg1 = ( void * ) nData1;
2691c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        msg->arg2 = ( void * ) nData2;
2692c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        msg->arg3 = ( void * ) &semaphore;
2693c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        msg->arg4 =  ( void * ) hComponent;
2694c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        res = mEventSignalQ.add(msg);
2695c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NO_MEMORY == res )
2696c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2697c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            CAMHAL_LOGEA("No ressources for inserting OMX events");
2698fdc26e9268306a90ca884934ca328d465545a303Tyler Luu            free(msg);
2699c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            ret = -ENOMEM;
2700c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2701c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2702c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2703c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2704c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2705c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
2706c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2707c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2708c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*========================================================*/
2709c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/* @ fn SampleTest_EmptyBufferDone :: Application callback*/
2710c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*========================================================*/
2711c322989ae6ff6769490828de1b5eda12b749cce9Iliyan MalchevOMX_ERRORTYPE OMXCameraAdapterEmptyBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
2712c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   OMX_IN OMX_PTR pAppData,
2713c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   OMX_IN OMX_BUFFERHEADERTYPE* pBuffHeader)
2714c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2715c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
2716c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2717c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
2718c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2719c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraAdapter *oca = (OMXCameraAdapter*)pAppData;
2720c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    eError = oca->OMXCameraAdapterEmptyBufferDone(hComponent, pBuffHeader);
2721c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2722c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
2723c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return eError;
2724c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2725c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2726c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2727c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*========================================================*/
2728c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/* @ fn SampleTest_EmptyBufferDone :: Application callback*/
2729c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*========================================================*/
2730c322989ae6ff6769490828de1b5eda12b749cce9Iliyan MalchevOMX_ERRORTYPE OMXCameraAdapter::OMXCameraAdapterEmptyBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
2731c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   OMX_IN OMX_BUFFERHEADERTYPE* pBuffHeader)
2732c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2733c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2734c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev   LOG_FUNCTION_NAME;
2735c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2736c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev   LOG_FUNCTION_NAME_EXIT;
2737c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2738c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev   return OMX_ErrorNone;
2739c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2740c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2741c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatic void debugShowFPS()
2742c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2743c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    static int mFrameCount = 0;
2744c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    static int mLastFrameCount = 0;
2745c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    static nsecs_t mLastFpsTime = 0;
2746c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    static float mFps = 0;
2747c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mFrameCount++;
2748c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (!(mFrameCount & 0x1F)) {
2749c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        nsecs_t now = systemTime();
2750c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        nsecs_t diff = now - mLastFpsTime;
2751c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
2752c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mLastFpsTime = now;
2753c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mLastFrameCount = mFrameCount;
2754c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOGD("Camera %d Frames, %f FPS", mFrameCount, mFps);
2755c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2756c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    // XXX: mFPS has the value we want
2757c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2758c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2759c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*========================================================*/
2760c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/* @ fn SampleTest_FillBufferDone ::  Application callback*/
2761c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*========================================================*/
2762c322989ae6ff6769490828de1b5eda12b749cce9Iliyan MalchevOMX_ERRORTYPE OMXCameraAdapterFillBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
2763c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   OMX_IN OMX_PTR pAppData,
2764c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   OMX_IN OMX_BUFFERHEADERTYPE* pBuffHeader)
2765c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2766c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    TIUTILS::Message msg;
2767c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
2768c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2769c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (UNLIKELY(mDebugFps)) {
2770c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        debugShowFPS();
2771c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
2772c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2773c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraAdapter *adapter =  ( OMXCameraAdapter * ) pAppData;
2774c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NULL != adapter )
2775c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2776c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        msg.command = OMXCameraAdapter::OMXCallbackHandler::CAMERA_FILL_BUFFER_DONE;
2777c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        msg.arg1 = ( void * ) hComponent;
2778c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        msg.arg2 = ( void * ) pBuffHeader;
2779c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        adapter->mOMXCallbackHandler->put(&msg);
2780c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2781c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2782c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return eError;
2783c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
2784c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2785c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*========================================================*/
2786c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/* @ fn SampleTest_FillBufferDone ::  Application callback*/
2787c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*========================================================*/
2788c322989ae6ff6769490828de1b5eda12b749cce9Iliyan MalchevOMX_ERRORTYPE OMXCameraAdapter::OMXCameraAdapterFillBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
2789c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                   OMX_IN OMX_BUFFERHEADERTYPE* pBuffHeader)
2790c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
2791c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2792c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t  stat = NO_ERROR;
2793c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t  res1, res2;
2794c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMXCameraPortParameters  *pPortParam;
2795c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
2796c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CameraFrame::FrameType typeOfFrame = CameraFrame::ALL_FRAMES;
2797c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    unsigned int refCount = 0;
2798470b7353f563741371ebd464f6c4a662f45e65c4Tyler Luu    BaseCameraAdapter::AdapterState state, nextState;
2799c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    BaseCameraAdapter::getState(state);
2800470b7353f563741371ebd464f6c4a662f45e65c4Tyler Luu    BaseCameraAdapter::getNextState(nextState);
2801c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    sp<CameraFDResult> fdResult = NULL;
28025e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng    unsigned int mask = 0xFFFF;
28035e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng    CameraFrame cameraFrame;
2804c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2805c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    res1 = res2 = NO_ERROR;
2806c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    pPortParam = &(mCameraAdapterParameters.mCameraPortParams[pBuffHeader->nOutputPortIndex]);
2807348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu
2808348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu    if ( !pBuffHeader || !pBuffHeader->pBuffer ) {
2809348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu        CAMHAL_LOGEA("NULL Buffer from OMX");
2810348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu        return OMX_ErrorNone;
2811348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu    }
2812348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu
2813c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (pBuffHeader->nOutputPortIndex == OMX_CAMERA_PORT_VIDEO_OUT_PREVIEW)
2814c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2815c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2816c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( ( PREVIEW_ACTIVE & state ) != PREVIEW_ACTIVE )
2817c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2818c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            return OMX_ErrorNone;
2819c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2820c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2821c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        recalculateFPS();
2822c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2823c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2824c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            Mutex::Autolock lock(mFaceDetectionLock);
2825aa6e62e279cb54ae76c55ba9f8d02da230ce34e7Tyler Luu            if ( mFaceDetectionRunning && !mFaceDetectionPaused ) {
2826c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                detectFaces(pBuffHeader, fdResult, pPortParam->mWidth, pPortParam->mHeight);
2827c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                if ( NULL != fdResult.get() ) {
2828c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    notifyFaceSubscribers(fdResult);
2829c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                    fdResult.clear();
2830c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2831c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2832c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2833c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2834c78fa2adcbe6ab50b751a45adde4483de528c5e6Milen Mitkov        if ( (nextState & CAPTURE_ACTIVE) )
2835c78fa2adcbe6ab50b751a45adde4483de528c5e6Milen Mitkov            {
2836c78fa2adcbe6ab50b751a45adde4483de528c5e6Milen Mitkov            mPending3Asettings |= SetFocus;
2837c78fa2adcbe6ab50b751a45adde4483de528c5e6Milen Mitkov            }
2838c78fa2adcbe6ab50b751a45adde4483de528c5e6Milen Mitkov
2839c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ///Prepare the frames to be sent - initialize CameraFrame object and reference count
2840ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu        if( mWaitingForSnapshot &&  (mCapturedFrames > 0) )
2841c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2842c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            typeOfFrame = CameraFrame::SNAPSHOT_FRAME;
28435e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng            mask = (unsigned int)CameraFrame::SNAPSHOT_FRAME;
2844c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2845c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
2846c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
28475e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng              typeOfFrame = CameraFrame::PREVIEW_FRAME_SYNC;
28485e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng              mask = (unsigned int)CameraFrame::PREVIEW_FRAME_SYNC;
28494f29332694e8222b3f1fae39d40edcdec1dec1bdTyler Luu            }
28504f29332694e8222b3f1fae39d40edcdec1dec1bdTyler Luu
28514f29332694e8222b3f1fae39d40edcdec1dec1bdTyler Luu        if (mRecording)
28524f29332694e8222b3f1fae39d40edcdec1dec1bdTyler Luu            {
28534f29332694e8222b3f1fae39d40edcdec1dec1bdTyler Luu            mask |= (unsigned int)CameraFrame::VIDEO_FRAME_SYNC;
28544f29332694e8222b3f1fae39d40edcdec1dec1bdTyler Luu            mFramesWithEncoder++;
2855c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2856c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2857c493114006e0e136c8c88c2fc9865994054959bfSundar Raman        //LOGV("FBD pBuffer = 0x%x", pBuffHeader->pBuffer);
2858c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2859c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if( mWaitingForSnapshot )
28605e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng          {
2861c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mSnapshotCount++;
2862c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2863ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu            if ( (mSnapshotCount == 1) &&
2864ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu                 ((HIGH_SPEED == mCapMode) || (VIDEO_MODE == mCapMode)) )
28655e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng              {
2866c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                notifyShutterSubscribers();
28675e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng              }
28685e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng          }
2869c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
28705e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng        stat = sendCallBacks(cameraFrame, pBuffHeader, mask, pPortParam);
2871c493114006e0e136c8c88c2fc9865994054959bfSundar Raman        mFramesWithDisplay++;
2872c493114006e0e136c8c88c2fc9865994054959bfSundar Raman
2873c493114006e0e136c8c88c2fc9865994054959bfSundar Raman        mFramesWithDucati--;
2874c493114006e0e136c8c88c2fc9865994054959bfSundar Raman
2875c493114006e0e136c8c88c2fc9865994054959bfSundar Raman#ifdef DEBUG_LOG
2876c493114006e0e136c8c88c2fc9865994054959bfSundar Raman        if(mBuffersWithDucati.indexOfKey((int)pBuffHeader->pBuffer)<0)
2877c493114006e0e136c8c88c2fc9865994054959bfSundar Raman            {
2878c493114006e0e136c8c88c2fc9865994054959bfSundar Raman            LOGE("Buffer was never with Ducati!! 0x%x", pBuffHeader->pBuffer);
2879c493114006e0e136c8c88c2fc9865994054959bfSundar Raman            for(int i=0;i<mBuffersWithDucati.size();i++) LOGE("0x%x", mBuffersWithDucati.keyAt(i));
2880c493114006e0e136c8c88c2fc9865994054959bfSundar Raman            }
2881c493114006e0e136c8c88c2fc9865994054959bfSundar Raman        mBuffersWithDucati.removeItem((int)pBuffHeader->pBuffer);
2882c493114006e0e136c8c88c2fc9865994054959bfSundar Raman#endif
2883c493114006e0e136c8c88c2fc9865994054959bfSundar Raman
2884c493114006e0e136c8c88c2fc9865994054959bfSundar Raman        if(mDebugFcs)
2885c493114006e0e136c8c88c2fc9865994054959bfSundar Raman            CAMHAL_LOGEB("C[%d] D[%d] E[%d]", mFramesWithDucati, mFramesWithDisplay, mFramesWithEncoder);
2886c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
28879c37a78ac28cf4a6ee966d29dbbf3468e5ad74c4Sundar Raman        stat |= advanceZoom();
28889c37a78ac28cf4a6ee966d29dbbf3468e5ad74c4Sundar Raman
28899c37a78ac28cf4a6ee966d29dbbf3468e5ad74c4Sundar Raman        // On the fly update to 3A settings not working
28909c37a78ac28cf4a6ee966d29dbbf3468e5ad74c4Sundar Raman        // Do not update 3A here if we are in the middle of a capture
28919c37a78ac28cf4a6ee966d29dbbf3468e5ad74c4Sundar Raman        // or in the middle of transitioning to it
28929c37a78ac28cf4a6ee966d29dbbf3468e5ad74c4Sundar Raman        if( mPending3Asettings && ((nextState & CAPTURE_ACTIVE) == 0))
28939c37a78ac28cf4a6ee966d29dbbf3468e5ad74c4Sundar Raman            {
28949c37a78ac28cf4a6ee966d29dbbf3468e5ad74c4Sundar Raman            apply3Asettings(mParameters3A);
28959c37a78ac28cf4a6ee966d29dbbf3468e5ad74c4Sundar Raman            }
28969c37a78ac28cf4a6ee966d29dbbf3468e5ad74c4Sundar Raman
2897c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2898c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else if( pBuffHeader->nOutputPortIndex == OMX_CAMERA_PORT_VIDEO_OUT_MEASUREMENT )
2899c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2900c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        typeOfFrame = CameraFrame::FRAME_DATA_SYNC;
29015e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng        mask = (unsigned int)CameraFrame::FRAME_DATA_SYNC;
29025e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng
29035e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng        stat = sendCallBacks(cameraFrame, pBuffHeader, mask, pPortParam);
2904c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev       }
2905c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else if( pBuffHeader->nOutputPortIndex == OMX_CAMERA_PORT_IMAGE_OUT_IMAGE )
2906c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2907ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu        OMX_COLOR_FORMATTYPE pixFormat;
2908ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu        const char *valstr = NULL;
2909c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2910ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu        pixFormat = mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mImagePortIndex].mColorFormat;
2911ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu        valstr = mParams.getPictureFormat();
2912ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu
2913ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu        if ( OMX_COLOR_FormatUnused == pixFormat )
2914c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2915c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            typeOfFrame = CameraFrame::IMAGE_FRAME;
29165e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng            mask = (unsigned int) CameraFrame::IMAGE_FRAME;
2917c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2918ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu        else if ( pixFormat == OMX_COLOR_FormatCbYCrY &&
2919ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu                  ((valstr && !strcmp(valstr, CameraParameters::PIXEL_FORMAT_JPEG)) ||
2920ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu                   !valstr) )
2921ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu            {
2922ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu            // signals to callbacks that this needs to be coverted to jpeg
2923ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu            // before returning to framework
2924ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu            typeOfFrame = CameraFrame::IMAGE_FRAME;
29255e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng            mask = (unsigned int) CameraFrame::IMAGE_FRAME;
2926ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu            cameraFrame.mQuirks |= CameraFrame::ENCODE_RAW_YUV422I_TO_JPEG;
292736e9bdd56757ff8048e08f6e52f234480c44f122Tyler Luu
292836e9bdd56757ff8048e08f6e52f234480c44f122Tyler Luu            // populate exif data and pass to subscribers via quirk
292936e9bdd56757ff8048e08f6e52f234480c44f122Tyler Luu            // subscriber is in charge of freeing exif data
293036e9bdd56757ff8048e08f6e52f234480c44f122Tyler Luu            ExifElementsTable* exif = new ExifElementsTable();
293136e9bdd56757ff8048e08f6e52f234480c44f122Tyler Luu            setupEXIF_libjpeg(exif);
293236e9bdd56757ff8048e08f6e52f234480c44f122Tyler Luu            cameraFrame.mQuirks |= CameraFrame::HAS_EXIF_DATA;
293336e9bdd56757ff8048e08f6e52f234480c44f122Tyler Luu            cameraFrame.mCookie2 = (void*) exif;
2934ee6bb64f60c228d711dc1d6875d8f4b0ed88b6cfTyler Luu            }
2935c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
29365e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng          {
2937c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            typeOfFrame = CameraFrame::RAW_FRAME;
29385e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng            mask = (unsigned int) CameraFrame::RAW_FRAME;
29395e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng          }
2940c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2941c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            pPortParam->mImageType = typeOfFrame;
2942c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2943c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if((mCapturedFrames>0) && !mCaptureSignalled)
2944c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
2945c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                mCaptureSignalled = true;
2946c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                mCaptureSem.Signal();
2947c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2948c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2949c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if( ( CAPTURE_ACTIVE & state ) != CAPTURE_ACTIVE )
2950c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
2951c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                goto EXIT;
2952c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2953c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2954c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2955c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            Mutex::Autolock lock(mBracketingLock);
2956c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            if ( mBracketingEnabled )
2957c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
2958c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                doBracketing(pBuffHeader, typeOfFrame);
2959c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                return eError;
2960c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
2961c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2962c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2963c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( 1 > mCapturedFrames )
2964c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2965c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            goto EXIT;
2966c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2967c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2968c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("Captured Frames: %d", mCapturedFrames);
2969c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2970c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mCapturedFrames--;
2971c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
29725e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng        stat = sendCallBacks(cameraFrame, pBuffHeader, mask, pPortParam);
29735e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng
2974c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2975c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    else
2976c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2977c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Frame received for non-(preview/capture/measure) port. This is yet to be supported");
2978c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
2979c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2980c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2981c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR != stat )
2982c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2983c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDB("sendFrameToSubscribers error: %d", stat);
2984c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        returnFrame(pBuffHeader->pBuffer, typeOfFrame);
2985c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
2986c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2987c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return eError;
2988c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2989c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    EXIT:
2990c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2991c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CAMHAL_LOGEB("Exiting function %s because of ret %d eError=%x", __FUNCTION__, stat, eError);
2992c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
2993c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR != stat )
2994c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
2995c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( NULL != mErrorNotifier )
2996c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
2997c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mErrorNotifier->errorNotify(CAMERA_ERROR_UNKNOWN);
2998c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
2999c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
3000c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3001c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return eError;
3002c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
3003c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3004c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::recalculateFPS()
3005c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
3006c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    float currentFPS;
3007c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3008c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mFrameCount++;
3009c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3010c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( ( mFrameCount % FPS_PERIOD ) == 0 )
3011c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
3012c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        nsecs_t now = systemTime();
3013c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        nsecs_t diff = now - mLastFPSTime;
3014c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        currentFPS =  ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
3015c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mLastFPSTime = now;
3016c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mLastFrameCount = mFrameCount;
3017c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3018c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( 1 == mIter )
3019c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
3020c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mFPS = currentFPS;
3021c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
3022c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        else
3023c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
3024c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            //cumulative moving average
3025c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mFPS = mLastFPS + (currentFPS - mLastFPS)/mIter;
3026c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
3027c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3028c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mLastFPS = mFPS;
3029c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mIter++;
3030c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
3031c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3032c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return NO_ERROR;
3033c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
3034c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3035c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::sendFrame(CameraFrame &frame)
3036c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
3037c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
3038c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3039c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
3040c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3041c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3042c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NO_ERROR == ret )
3043c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
3044c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ret = sendFrameToSubscribers(&frame);
3045c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
3046c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3047c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
3048c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3049c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
3050c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
3051c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
30525e02dd977e79ee977ee063496033bbc363a70780Akwasi Boatengstatus_t OMXCameraAdapter::sendCallBacks(CameraFrame frame, OMX_IN OMX_BUFFERHEADERTYPE *pBuffHeader, unsigned int mask, OMXCameraPortParameters *port)
30535e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng{
30545e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  status_t ret = NO_ERROR;
30555e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng
30565e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  LOG_FUNCTION_NAME;
30575e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng
30585e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  if ( NULL == port)
30595e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng    {
30605e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng      CAMHAL_LOGEA("Invalid portParam");
30615e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng      return -EINVAL;
30625e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng    }
30635e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng
30645e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  if ( NULL == pBuffHeader )
30655e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng    {
30665e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng      CAMHAL_LOGEA("Invalid Buffer header");
30675e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng      return -EINVAL;
30685e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng    }
30695e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng
30705e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  Mutex::Autolock lock(mSubscriberLock);
30715e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng
30725e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  //frame.mFrameType = typeOfFrame;
30735e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  frame.mFrameMask = mask;
30745e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  frame.mBuffer = pBuffHeader->pBuffer;
30755e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  frame.mLength = pBuffHeader->nFilledLen;
30765e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  frame.mAlignment = port->mStride;
30775e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  frame.mOffset = pBuffHeader->nOffset;
30785e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  frame.mWidth = port->mWidth;
30795e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  frame.mHeight = port->mHeight;
30808e88af31129b8cc09675279ca2eac88256d8526bAkwasi Boateng  frame.mYuv[0] = NULL;
30818e88af31129b8cc09675279ca2eac88256d8526bAkwasi Boateng  frame.mYuv[1] = NULL;
30825e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng
30835e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  if ( onlyOnce && mRecording )
30845e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng    {
30855e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng      mTimeSourceDelta = (pBuffHeader->nTimeStamp * 1000) - systemTime(SYSTEM_TIME_MONOTONIC);
30865e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng      onlyOnce = false;
30875e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng    }
30885e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng
30895e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  frame.mTimestamp = (pBuffHeader->nTimeStamp * 1000) - mTimeSourceDelta;
3090348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu
3091348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu  ret = setInitFrameRefCount(frame.mBuffer, mask);
3092348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu
3093348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu  if (ret != NO_ERROR) {
30947b4990d47f1765a2264cdf925730e486791c2d1cTyler Luu     CAMHAL_LOGDB("Error in setInitFrameRefCount %d", ret);
3095348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu  } else {
3096348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu      ret = sendFrameToSubscribers(&frame);
3097348e4eaaf2f88810f2a3c55fd1b0df540a5104edTyler Luu  }
30985e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng
3099c493114006e0e136c8c88c2fc9865994054959bfSundar Raman  CAMHAL_LOGVB("B 0x%x T %llu", frame.mBuffer, pBuffHeader->nTimeStamp);
3100c493114006e0e136c8c88c2fc9865994054959bfSundar Raman
31015e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  LOG_FUNCTION_NAME_EXIT;
31025e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng
31035e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng  return ret;
31045e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng}
31055e02dd977e79ee977ee063496033bbc363a70780Akwasi Boateng
3106c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevstatus_t OMXCameraAdapter::initCameraFrame( CameraFrame &frame,
3107c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                            OMX_IN OMX_BUFFERHEADERTYPE *pBuffHeader,
3108c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                            int typeOfFrame,
3109c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                            OMXCameraPortParameters *port)
3110c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
3111c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
3112c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3113c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
3114c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3115c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NULL == port)
3116c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
3117c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Invalid portParam");
3118c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return -EINVAL;
3119c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
3120c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3121c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NULL == pBuffHeader )
3122c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
3123c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEA("Invalid Buffer header");
3124c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return -EINVAL;
3125c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
3126c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3127c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    frame.mFrameType = typeOfFrame;
3128c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    frame.mBuffer = pBuffHeader->pBuffer;
3129c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    frame.mLength = pBuffHeader->nFilledLen;
3130c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    frame.mAlignment = port->mStride;
3131c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    frame.mOffset = pBuffHeader->nOffset;
3132c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    frame.mWidth = port->mWidth;
3133c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    frame.mHeight = port->mHeight;
3134c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3135b8923d57614400f11c61a524e15f040dd68d25bcDevaraj Rangasamy    // Timestamp in pBuffHeader->nTimeStamp is derived on DUCATI side, which is
3136b8923d57614400f11c61a524e15f040dd68d25bcDevaraj Rangasamy    // is not  same time value as derived using systemTime. It would be ideal to use
3137b8923d57614400f11c61a524e15f040dd68d25bcDevaraj Rangasamy    // exactly same time source across Android and Ducati, which is limited by
3138b8923d57614400f11c61a524e15f040dd68d25bcDevaraj Rangasamy    // system now. So, workaround for now is to find the time offset between the two
3139b8923d57614400f11c61a524e15f040dd68d25bcDevaraj Rangasamy    // time sources and compensate the difference, along with the latency involved
3140b8923d57614400f11c61a524e15f040dd68d25bcDevaraj Rangasamy    // in camera buffer reaching CameraHal. Also, Do timeset offset calculation only
3141b8923d57614400f11c61a524e15f040dd68d25bcDevaraj Rangasamy    // when recording is in progress, when nTimestamp will be populated by Camera
314296e5c60fc857eea34bb76b0334844b229b59506aDevaraj Rangasamy    if ( onlyOnce && mRecording )
3143c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
3144c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mTimeSourceDelta = (pBuffHeader->nTimeStamp * 1000) - systemTime(SYSTEM_TIME_MONOTONIC);
3145b8923d57614400f11c61a524e15f040dd68d25bcDevaraj Rangasamy        mTimeSourceDelta += kCameraBufferLatencyNs;
3146c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        onlyOnce = false;
3147c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
3148c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3149c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    // Calculating the new video timestamp based on offset from ducati source.
3150c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    frame.mTimestamp = (pBuffHeader->nTimeStamp * 1000) - mTimeSourceDelta;
3151c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3152c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOG_FUNCTION_NAME_EXIT;
3153c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3154c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return ret;
3155c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
3156c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3157c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevbool OMXCameraAdapter::CommandHandler::Handler()
3158c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
3159c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    TIUTILS::Message msg;
3160c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    volatile int forever = 1;
3161c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t stat;
3162c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ErrorNotifier *errorNotify = NULL;
3163c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3164c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
3165c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3166c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    while ( forever )
3167782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev        {
3168c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        stat = NO_ERROR;
3169c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGDA("Handler: waiting for messsage...");
3170c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        TIUTILS::MessageQueue::waitForMsg(&mCommandMsgQ, NULL, NULL, -1);
3171782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev        mCommandMsgQ.get(&msg);
3172782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev        CAMHAL_LOGDB("msg.command = %d", msg.command);
3173782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev        switch ( msg.command ) {
3174782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev            case CommandHandler::CAMERA_START_IMAGE_CAPTURE:
3175782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev            {
3176782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev                stat = mCameraAdapter->startImageCapture();
3177782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev                break;
3178782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev            }
3179782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev            case CommandHandler::CAMERA_PERFORM_AUTOFOCUS:
3180782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev            {
3181782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev                stat = mCameraAdapter->doAutoFocus();
3182782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev                break;
3183782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev            }
3184782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev            case CommandHandler::COMMAND_EXIT:
3185782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev            {
3186782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev                CAMHAL_LOGEA("Exiting command handler");
3187782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev                forever = 0;
3188782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev                break;
3189c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
31907016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng            case CommandHandler::CAMERA_SWITCH_TO_EXECUTING:
31917016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng            {
31927016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng              stat = mCameraAdapter->doSwitchToExecuting();
31937016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng              break;
31947016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng            }
3195782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev        }
3196c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3197782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev        if ( NO_ERROR != stat )
3198c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
3199782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev            errorNotify = ( ErrorNotifier * ) msg.arg1;
3200782a084e5fec691d3985b31c3ab7be1584d879b3Iliyan Malchev            if ( NULL != errorNotify )
3201c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                {
32020ff71809874c00d540af83c123f3dc34759f1871Sundar Raman                errorNotify->errorNotify(CAMERA_ERROR_HARD);
3203c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                }
3204c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
3205c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
3206c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3207c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
3208c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3209c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return false;
3210c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
3211c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3212c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevbool OMXCameraAdapter::OMXCallbackHandler::Handler()
3213c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
3214c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    TIUTILS::Message msg;
3215c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    volatile int forever = 1;
3216c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t ret = NO_ERROR;
3217c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3218c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
3219c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3220c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    while(forever){
3221c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        TIUTILS::MessageQueue::waitForMsg(&mCommandMsgQ, NULL, NULL, -1);
3222c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mCommandMsgQ.get(&msg);
3223c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        switch ( msg.command ) {
3224c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            case OMXCallbackHandler::CAMERA_FILL_BUFFER_DONE:
3225c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
3226c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                ret = mCameraAdapter->OMXCameraAdapterFillBufferDone(( OMX_HANDLETYPE ) msg.arg1,
3227c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                                                     ( OMX_BUFFERHEADERTYPE *) msg.arg2);
3228c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                break;
3229c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
3230c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            case CommandHandler::COMMAND_EXIT:
3231c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            {
3232c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                CAMHAL_LOGEA("Exiting OMX callback handler");
3233c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                forever = 0;
3234c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                break;
3235c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            }
3236c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
3237c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
3238c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3239c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
3240c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return false;
3241c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
3242c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
32439a412956d5327ec851ff8adc3e9804640fa30c7fTyler LuuOMXCameraAdapter::OMXCameraAdapter(size_t sensor_index): mComponentState (OMX_StateLoaded)
3244c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
3245c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
3246c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
32479a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    mSensorIndex = sensor_index;
3248c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mPictureRotation = 0;
3249c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    // Initial values
3250c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mTimeSourceDelta = 0;
3251c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    onlyOnce = true;
3252c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3253c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mDoAFSem.Create(0);
3254c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mInitSem.Create(0);
3255c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mFlushSem.Create(0);
3256c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mUsePreviewDataSem.Create(0);
3257c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mUsePreviewSem.Create(0);
3258c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mUseCaptureSem.Create(0);
3259c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mStartPreviewSem.Create(0);
3260c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mStopPreviewSem.Create(0);
3261c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mStartCaptureSem.Create(0);
3262c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mStopCaptureSem.Create(0);
3263c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mSwitchToLoadedSem.Create(0);
3264c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mCaptureSem.Create(0);
3265c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
32667016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng    mSwitchToExecSem.Create(0);
32677016577b173fc73f5b43e76cbc122bb0a9346613Akwasi Boateng
3268c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    mCameraAdapterParameters.mHandleComp = 0;
3269c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3270ba027fc89ec3950e1ac2f95fcc3f1002442ddabbSundar Raman    mUserSetExpLock = OMX_FALSE;
3271ba027fc89ec3950e1ac2f95fcc3f1002442ddabbSundar Raman    mUserSetWbLock = OMX_FALSE;
3272ba027fc89ec3950e1ac2f95fcc3f1002442ddabbSundar Raman
3273c493114006e0e136c8c88c2fc9865994054959bfSundar Raman    mFramesWithDucati = 0;
3274c493114006e0e136c8c88c2fc9865994054959bfSundar Raman    mFramesWithDisplay = 0;
3275c493114006e0e136c8c88c2fc9865994054959bfSundar Raman    mFramesWithEncoder = 0;
3276c493114006e0e136c8c88c2fc9865994054959bfSundar Raman
3277c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
3278c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
3279c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3280c322989ae6ff6769490828de1b5eda12b749cce9Iliyan MalchevOMXCameraAdapter::~OMXCameraAdapter()
3281c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
3282c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
3283c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3284ce8e32eddf46a966b6f637c30ad0c464631c1d86Tyler Luu    Mutex::Autolock lock(gAdapterLock);
3285ce8e32eddf46a966b6f637c30ad0c464631c1d86Tyler Luu
3286c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //Return to OMX Loaded state
3287c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    switchToLoaded();
3288c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3289c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ///De-init the OMX
3290c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if( (mComponentState==OMX_StateLoaded) || (mComponentState==OMX_StateInvalid))
3291c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        {
32920ff71809874c00d540af83c123f3dc34759f1871Sundar Raman        ///Free the handle for the Camera component
32930ff71809874c00d540af83c123f3dc34759f1871Sundar Raman        if(mCameraAdapterParameters.mHandleComp)
32940ff71809874c00d540af83c123f3dc34759f1871Sundar Raman            {
32950ff71809874c00d540af83c123f3dc34759f1871Sundar Raman            OMX_FreeHandle(mCameraAdapterParameters.mHandleComp);
3296b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman             mCameraAdapterParameters.mHandleComp = NULL;
32970ff71809874c00d540af83c123f3dc34759f1871Sundar Raman            }
32980ff71809874c00d540af83c123f3dc34759f1871Sundar Raman
3299c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        OMX_Deinit();
3300c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
3301c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3302b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman
3303fdc26e9268306a90ca884934ca328d465545a303Tyler Luu    //Remove any unhandled events
3304fdc26e9268306a90ca884934ca328d465545a303Tyler Luu    if ( !mEventSignalQ.isEmpty() )
3305fdc26e9268306a90ca884934ca328d465545a303Tyler Luu      {
3306fdc26e9268306a90ca884934ca328d465545a303Tyler Luu        for (unsigned int i = 0 ; i < mEventSignalQ.size() ; i++ )
3307fdc26e9268306a90ca884934ca328d465545a303Tyler Luu          {
3308fdc26e9268306a90ca884934ca328d465545a303Tyler Luu            TIUTILS::Message *msg = mEventSignalQ.itemAt(i);
3309fdc26e9268306a90ca884934ca328d465545a303Tyler Luu            //remove from queue and free msg
3310fdc26e9268306a90ca884934ca328d465545a303Tyler Luu            if ( NULL != msg )
3311fdc26e9268306a90ca884934ca328d465545a303Tyler Luu              {
33126b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                Semaphore *sem  = (Semaphore*) msg->arg3;
33136b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                mEventSignalQ.removeAt(i);
33146b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng                sem->Signal();
3315fdc26e9268306a90ca884934ca328d465545a303Tyler Luu                free(msg);
33166b5eaf29c3d17a24731bf9271bd0d199d433813eAkwasi Boateng
3317fdc26e9268306a90ca884934ca328d465545a303Tyler Luu              }
3318fdc26e9268306a90ca884934ca328d465545a303Tyler Luu          }
3319fdc26e9268306a90ca884934ca328d465545a303Tyler Luu      }
3320fdc26e9268306a90ca884934ca328d465545a303Tyler Luu
3321c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //Exit and free ref to command handling thread
3322c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NULL != mCommandHandler.get() )
3323c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    {
3324c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        TIUTILS::Message msg;
3325c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        msg.command = CommandHandler::COMMAND_EXIT;
3326c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        msg.arg1 = mErrorNotifier;
3327c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mCommandHandler->put(&msg);
3328c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mCommandHandler->requestExitAndWait();
3329c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mCommandHandler.clear();
3330c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
3331c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3332c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    //Exit and free ref to callback handling thread
3333c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if ( NULL != mOMXCallbackHandler.get() )
3334c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    {
3335c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        TIUTILS::Message msg;
3336c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        msg.command = OMXCallbackHandler::COMMAND_EXIT;
3337c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mOMXCallbackHandler->put(&msg);
3338c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mOMXCallbackHandler->requestExitAndWait();
3339c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        mOMXCallbackHandler.clear();
3340c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
3341c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3342c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
3343c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
3344c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
33459a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luuextern "C" CameraAdapter* CameraAdapter_Factory(size_t sensor_index)
3346c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
33479a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    CameraAdapter *adapter = NULL;
3348c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    Mutex::Autolock lock(gAdapterLock);
3349c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3350c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
3351c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
33529a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    adapter = new OMXCameraAdapter(sensor_index);
33539a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    if ( adapter ) {
33549a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        CAMHAL_LOGDB("New OMX Camera adapter instance created for sensor %d",sensor_index);
33559a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    } else {
33569a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu        CAMHAL_LOGEA("Camera adapter create failed!");
33579a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    }
3358c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3359c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
3360c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
33619a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    return adapter;
3362c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
3363c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3364b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar RamanOMX_ERRORTYPE OMXCameraAdapter::OMXCameraGetHandle(OMX_HANDLETYPE *handle, OMX_PTR pAppData )
3365b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman{
3366b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    OMX_ERRORTYPE eError = OMX_ErrorUndefined;
3367b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman
3368b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    int retries = 5;
3369b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    while(eError!=OMX_ErrorNone && --retries>=0) {
3370b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman        // OMX_Init
3371b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman        eError = OMX_Init();
3372b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman        if (eError != OMX_ErrorNone) {
3373b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            CAMHAL_LOGEB("OMX_Init -0x%x", eError);
3374b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman        } else {
3375b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            // Setup key parameters to send to Ducati during init
3376b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            OMX_CALLBACKTYPE oCallbacks;
3377b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman
3378b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            // Initialize the callback handles
3379b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            oCallbacks.EventHandler    = android::OMXCameraAdapterEventHandler;
3380b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            oCallbacks.EmptyBufferDone = android::OMXCameraAdapterEmptyBufferDone;
3381b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            oCallbacks.FillBufferDone  = android::OMXCameraAdapterFillBufferDone;
3382b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman
3383b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            // Get Handle
3384b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            eError = OMX_GetHandle(handle, (OMX_STRING)"OMX.TI.DUCATI1.VIDEO.CAMERA", pAppData, &oCallbacks);
3385b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            if (eError != OMX_ErrorNone) {
3386b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman                CAMHAL_LOGEB("OMX_GetHandle -0x%x", eError);
3387b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman                //Deinit here as we will init again above
3388b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman                //Note that we need to deinit because an erro recovery
3389b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman                //might have rendered the currently open rpmsg-omx device
3390b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman                //useless. so we might need to re-open it again
3391b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman                OMX_Deinit();
33929c37a78ac28cf4a6ee966d29dbbf3468e5ad74c4Sundar Raman            } else {
33939c37a78ac28cf4a6ee966d29dbbf3468e5ad74c4Sundar Raman                break;
3394b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman            }
3395b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman        }
3396b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman        //Sleep for 100 mS
3397b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman        usleep(100000);
3398b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    }
3399b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman
3400b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    return eError;
3401b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman
3402b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman}
3403b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman
3404c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevextern "C" int CameraAdapter_Capabilities(CameraProperties::Properties* properties_array,
3405c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          const unsigned int starting_camera,
3406c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev                                          const unsigned int max_camera) {
3407c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int num_cameras_supported = 0;
3408c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    CameraProperties::Properties* properties = NULL;
3409c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_ERRORTYPE eError = OMX_ErrorNone;
3410c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_HANDLETYPE handle = NULL;
3411c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_TI_CAPTYPE caps;
3412c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3413c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME;
3414c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
34159a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu    Mutex::Autolock lock(gAdapterLock);
34169a412956d5327ec851ff8adc3e9804640fa30c7fTyler Luu
3417c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (!properties_array) {
3418c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("invalid param: properties = 0x%p", properties_array);
3419c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        LOG_FUNCTION_NAME_EXIT;
3420c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        return -EINVAL;
3421c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
3422c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3423b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman    eError = OMXCameraAdapter::OMXCameraGetHandle(&handle);
3424c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if (eError != OMX_ErrorNone) {
3425c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        CAMHAL_LOGEB("OMX_GetHandle -0x%x", eError);
3426c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        goto EXIT;
3427c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
3428c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3429c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    // Continue selecting sensor and then querying OMX Camera for it's capabilities
3430c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    // When sensor select returns an error, we know to break and stop
3431c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    while (eError == OMX_ErrorNone &&
3432c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev           (starting_camera + num_cameras_supported) < max_camera) {
3433c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        // sensor select
3434c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        OMX_CONFIG_SENSORSELECTTYPE sensorSelect;
3435c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        OMX_INIT_STRUCT_PTR (&sensorSelect, OMX_CONFIG_SENSORSELECTTYPE);
3436c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        sensorSelect.eSensor = (OMX_SENSORSELECT) num_cameras_supported;
3437c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        eError = OMX_SetConfig(handle, ( OMX_INDEXTYPE ) OMX_TI_IndexConfigSensorSelect, &sensorSelect);
3438c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3439c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if ( OMX_ErrorNone != eError ) {
3440c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            break;
3441c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
3442c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3443c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        // get and fill capabilities
3444c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        properties = properties_array + starting_camera + num_cameras_supported;
3445c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        OMXCameraAdapter::getCaps(properties, handle);
3446c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3447c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        // need to fill facing information
3448c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        // assume that only sensor 0 is back facing
3449c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        if (num_cameras_supported == 0) {
3450c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            properties->set(CameraProperties::FACING_INDEX, TICameraParameters::FACING_BACK);
3451c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        } else {
3452c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            properties->set(CameraProperties::FACING_INDEX, TICameraParameters::FACING_FRONT);
3453c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
3454c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3455c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        num_cameras_supported++;
3456c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
3457c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3458c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev EXIT:
3459c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    // clean up
3460c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    if(handle) {
3461c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        OMX_FreeHandle(handle);
3462b3fb1a45ce906ab0365f247caf596665ea0c9f39Sundar Raman        handle=NULL;
3463c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    }
3464c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    OMX_Deinit();
3465c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3466c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    LOG_FUNCTION_NAME_EXIT;
3467c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3468c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    return num_cameras_supported;
3469c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
3470c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3471c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev};
3472c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3473c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3474c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*--------------------Camera Adapter Class ENDS here-----------------------------*/
3475c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
3476