DSP

Android Camera OMXCameraAdapter.cpp初始化分析

2019-07-13 20:16发布

这几天一直在研究android 的omx机制,我针对的android是4.0.3,主要是TI 的4430,4460的omx方式实现,这里还是简单的说一下为什么要研究这个文件
之前有一篇文章已经比较详细的说过了OMAP4系列芯片了,这里这个OMXCameraAdapter其实就是omap4 A9端的omx client,通过这个client与ducati端(也就是omap4的DSP端)的omx进行通信,可以把ducati端的这个omx简单理解为omx servicer,之前已经说了很多了andriod camera了,但是之前文章都是针对V4LCameraAdapter这个适配器进行的,这是大家都比较了解的,就是app通过V4L2这种方式与kernel camera driver实现交互,进而实现camera 的使用,包括数据回调等过程,但是这里的OMX方法完全颠覆了我之前的想法,开始在研究OMX的时候自己就是调用这个死胡同里了,一直在试图在OMX的实现中找到他到底是在哪么访问设备的,本来我是以后最终他还是通过V4L2这种方式,事实证明我错了,OMX是一种完全不同于V4L2 的访问camera的策略,他通过A9端的omx client和DSP端的omx通信,而且最终访问camera的方法是在DSP端omx server接到到A9端的omx client配置后按照指定方法实现camera 的控制的,这里ducati端到底是怎么处理的我暂时没有关注,暂时精力有限啊!
以上是我自己的见解,也许是完全错误的,待确认,这里只是总结我的分析过程
经过上面我的概述,自然A9端的这个OMXCameraAdapter对于我来说就是一切了,分析它是必经之路啊
现在就开始:
首先要知道在哪里实例化了这个类的对象,这个在之前已经说到过了,这个不在说明,重点看看他的initialize方法都干了些甚么

  1. /*--------------------Camera Adapter Class STARTS here-----------------------------*/

  2. status_t OMXCameraAdapter::initialize(CameraProperties::Properties* caps)
  3. {
  4.     LOG_FUNCTION_NAME;

  5.     char value[PROPERTY_VALUE_MAX];
  6.     const char *mountOrientationString = NULL;

  7.     property_get("debug.camera.showfps", value, "0");
  8.     mDebugFps = atoi(value);
  9.     property_get("debug.camera.framecounts", value, "0");
  10.     mDebugFcs = atoi(value);

  11. #ifdef CAMERAHAL_OMX_PROFILING

  12.     property_get("debug.camera.profile", value, "0");
  13.     mDebugProfile = atoi(value);

  14. #endif

  15.     TIMM_OSAL_ERRORTYPE osalError = OMX_ErrorNone;
  16.     OMX_ERRORTYPE eError = OMX_ErrorNone;
  17.     status_t ret = NO_ERROR;

  18.     mLocalVersionParam.s.nVersionMajor = 0x1;
  19.     mLocalVersionParam.s.nVersionMinor = 0x1;
  20.     mLocalVersionParam.s.nRevision = 0x0 ;
  21.     mLocalVersionParam.s.nStep = 0x0;

  22.     mPending3Asettings = 0;//E3AsettingsAll;
  23.     mPendingCaptureSettings = 0;
  24.     mPendingPreviewSettings = 0;

  25.     if ( 0 != mInitSem.Count() )
  26.         {
  27.         CAMHAL_LOGEB("Error mInitSem semaphore count %d", mInitSem.Count());
  28.         LOG_FUNCTION_NAME_EXIT;
  29.         return NO_INIT;
  30.         }

  31.     ///Update the preview and image capture port indexes
  32.     mCameraAdapterParameters.mPrevPortIndex = OMX_CAMERA_PORT_VIDEO_OUT_PREVIEW;
  33.     // temp changed in order to build OMX_CAMERA_PORT_VIDEO_OUT_IMAGE;
  34.     mCameraAdapterParameters.mImagePortIndex = OMX_CAMERA_PORT_IMAGE_OUT_IMAGE;
  35.     mCameraAdapterParameters.mMeasurementPortIndex = OMX_CAMERA_PORT_VIDEO_OUT_MEASUREMENT;
  36.     //currently not supported use preview port instead
  37.     mCameraAdapterParameters.mVideoPortIndex = OMX_CAMERA_PORT_VIDEO_OUT_VIDEO;
  38.     mCameraAdapterParameters.mVideoInPortIndex = OMX_CAMERA_PORT_VIDEO_IN_VIDEO;
  39.     // 1.OMX_Init
  40.     eError = OMX_Init();
  41.     if (eError != OMX_ErrorNone) {
  42.         CAMHAL_LOGEB("OMX_Init() failed, error: 0x%x", eError);
  43.         return ErrorUtils::omxToAndroidError(eError);
  44.     }
  45.     mOmxInitialized = true;

  46.     // 2.Initialize the callback handles
  47.     OMX_CALLBACKTYPE callbacks;
  48.     callbacks.EventHandler = android::OMXCameraAdapterEventHandler;
  49.     callbacks.EmptyBufferDone = android::OMXCameraAdapterEmptyBufferDone;
  50.     callbacks.FillBufferDone = android::OMXCameraAdapterFillBufferDone;

  51.     // 3.Get the handle to the OMX Component
  52.     eError = OMXCameraAdapter::OMXCameraGetHandle(&mCameraAdapterParameters.mHandleComp, this, callbacks);
  53.     if(eError != OMX_ErrorNone) {
  54.         CAMHAL_LOGEB("OMX_GetHandle -0x%x", eError);
  55.     }
  56.     GOTO_EXIT_IF((eError != OMX_ErrorNone), eError);

  57.     // 4.设置component的初始状态
  58.     mComponentState = OMX_StateLoaded;

  59.     CAMHAL_LOGVB("OMX_GetHandle -0x%x sensor_index = %lu", eError, mSensorIndex);
  60.     initDccFileDataSave(&mCameraAdapterParameters.mHandleComp, mCameraAdapterParameters.mPrevPortIndex);

  61.     // 5.disable所以的component
  62.     eError = OMX_SendCommand(mCameraAdapterParameters.mHandleComp,
  63.                                   OMX_CommandPortDisable,
  64.                                   OMX_ALL,
  65.                                   NULL);

  66.     if(eError != OMX_ErrorNone) {
  67.          CAMHAL_LOGEB("OMX_SendCommand(OMX_CommandPortDisable) -0x%x", eError);
  68.     }
  69.     GOTO_EXIT_IF((eError != OMX_ErrorNone), eError);

  70.     // 6.Register for port enable event
  71.     ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
  72.                                  OMX_EventCmdComplete,
  73.                                  OMX_CommandPortEnable,
  74.                                  mCameraAdapterParameters.mPrevPortIndex,
  75.                                  mInitSem);
  76.     if(ret != NO_ERROR) {
  77.          CAMHAL_LOGEB("Error in registering for event %d", ret);
  78.          goto EXIT;
  79.     }

  80.     // 7.Enable PREVIEW Port
  81.     eError = OMX_SendCommand(mCameraAdapterParameters.mHandleComp,
  82.                                  OMX_CommandPortEnable,
  83.                                  mCameraAdapterParameters.mPrevPortIndex,
  84.                                  NULL);
  85.     if(eError != OMX_ErrorNone) {
  86.         CAMHAL_LOGEB("OMX_SendCommand(OMX_CommandPortEnable) -0x%x", eError);
  87.     }
  88.     GOTO_EXIT_IF((eError!=OMX_ErrorNone), eError);

  89.     // 8.Wait for the port enable event to occur
  90.     ret = mInitSem.WaitTimeout(OMX_CMD_TIMEOUT);
  91.     if ( NO_ERROR == ret ) {
  92.          CAMHAL_LOGDA("-Port enable event arrived");
  93.     } else {
  94.          ret |= RemoveEvent(mCameraAdapterParameters.mHandleComp,
  95.                             OMX_EventCmdComplete,
  96.                             OMX_CommandPortEnable,
  97.                             mCameraAdapterParameters.mPrevPortIndex,
  98.                             NULL);
  99.          CAMHAL_LOGEA("Timeout for enabling preview port expired!");
  100.          goto EXIT;
  101.      }

  102.     // 9.Select the sensor
  103.     OMX_CONFIG_SENSORSELECTTYPE sensorSelect;
  104.     OMX_INIT_STRUCT_PTR (&sensorSelect, OMX_CONFIG_SENSORSELECTTYPE);
  105.     sensorSelect.eSensor = (OMX_SENSORSELECT) mSensorIndex;
  106.     eError = OMX_SetConfig(mCameraAdapterParameters.mHandleComp, ( OMX_INDEXTYPE ) OMX_TI_IndexConfigSensorSelect, &sensorSelect);
  107.     if ( OMX_ErrorNone != eError ) {
  108.         CAMHAL_LOGEB("Error while selecting the sensor index as %d - 0x%x", mSensorIndex, eError);
  109.         return BAD_VALUE;
  110.     } else {
  111.         CAMHAL_LOGDB("Sensor %d selected successfully", mSensorIndex);
  112.     }

  113. #ifdef CAMERAHAL_DEBUG

  114.     printComponentVersion(mCameraAdapterParameters.mHandleComp);

  115. #endif
  116.     //