Home | History | Annotate | Download | only in V4LCameraAdapter
      1 /*
      2  * Copyright (C) Texas Instruments - http://www.ti.com/
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 
     18 
     19 #ifndef V4L_CAMERA_ADAPTER_H
     20 #define V4L_CAMERA_ADAPTER_H
     21 
     22 #include <linux/videodev2.h>
     23 
     24 #include "CameraHal.h"
     25 #include "BaseCameraAdapter.h"
     26 #include "DebugUtils.h"
     27 
     28 namespace Ti {
     29 namespace Camera {
     30 
     31 #define DEFAULT_PIXEL_FORMAT V4L2_PIX_FMT_YUYV
     32 
     33 #define NB_BUFFER 10
     34 #define DEVICE "/dev/videoxx"
     35 #define DEVICE_PATH "/dev/"
     36 #define DEVICE_NAME "videoxx"
     37 
     38 typedef int V4L_HANDLETYPE;
     39 
     40 struct CapPixelformat {
     41         uint32_t pixelformat;
     42         const char *param;
     43 };
     44 
     45 struct CapResolution {
     46         size_t width, height;
     47         char param[10];
     48 };
     49 
     50 struct CapU32 {
     51         uint32_t num;
     52         const char *param;
     53 };
     54 
     55 typedef CapU32 CapFramerate;
     56 
     57 struct VideoInfo {
     58     struct v4l2_capability cap;
     59     struct v4l2_format format;
     60     struct v4l2_buffer buf;
     61     struct v4l2_requestbuffers rb;
     62     void *mem[NB_BUFFER];
     63     void *CaptureBuffers[NB_BUFFER];
     64     bool isStreaming;
     65     int width;
     66     int height;
     67     int formatIn;
     68     int framesizeIn;
     69 };
     70 
     71 typedef struct V4L_TI_CAPTYPE {
     72     uint16_t        ulPreviewFormatCount;   // supported preview pixelformat count
     73     uint32_t        ePreviewFormats[32];
     74     uint16_t        ulPreviewResCount;   // supported preview resolution sizes
     75     CapResolution   tPreviewRes[32];
     76     uint16_t        ulCaptureResCount;   // supported capture resolution sizes
     77     CapResolution   tCaptureRes[32];
     78     uint16_t        ulFrameRateCount;   // supported frame rate
     79     uint16_t        ulFrameRates[32];
     80 }V4L_TI_CAPTYPE;
     81 
     82 /**
     83   * Class which completely abstracts the camera hardware interaction from camera hal
     84   * TODO: Need to list down here, all the message types that will be supported by this class
     85                 Need to implement BufferProvider interface to use AllocateBuffer of OMX if needed
     86   */
     87 class V4LCameraAdapter : public BaseCameraAdapter
     88 {
     89 public:
     90 
     91     /*--------------------Constant declarations----------------------------------------*/
     92     static const int32_t MAX_NO_BUFFERS = 20;
     93 
     94     ///@remarks OMX Camera has six ports - buffer input, time input, preview, image, video, and meta data
     95     static const int MAX_NO_PORTS = 6;
     96 
     97     ///Five second timeout
     98     static const int CAMERA_ADAPTER_TIMEOUT = 5000*1000;
     99 
    100 public:
    101 
    102     V4LCameraAdapter(size_t sensor_index);
    103     ~V4LCameraAdapter();
    104 
    105 
    106     ///Initialzes the camera adapter creates any resources required
    107     virtual status_t initialize(CameraProperties::Properties*);
    108 
    109     //APIs to configure Camera adapter and get the current parameter set
    110     virtual status_t setParameters(const android::CameraParameters& params);
    111     virtual void getParameters(android::CameraParameters& params);
    112 
    113     // API
    114     virtual status_t UseBuffersPreview(CameraBuffer *bufArr, int num);
    115     virtual status_t UseBuffersCapture(CameraBuffer *bufArr, int num);
    116 
    117     static status_t getCaps(const int sensorId, CameraProperties::Properties* params, V4L_HANDLETYPE handle);
    118 
    119 protected:
    120 
    121 //----------Parent class method implementation------------------------------------
    122     virtual status_t startPreview();
    123     virtual status_t stopPreview();
    124     virtual status_t takePicture();
    125     virtual status_t stopImageCapture();
    126     virtual status_t autoFocus();
    127     virtual status_t useBuffers(CameraMode mode, CameraBuffer *bufArr, int num, size_t length, unsigned int queueable);
    128     virtual status_t fillThisBuffer(CameraBuffer *frameBuf, CameraFrame::FrameType frameType);
    129     virtual status_t getFrameSize(size_t &width, size_t &height);
    130     virtual status_t getPictureBufferSize(CameraFrame &frame, size_t bufferCount);
    131     virtual status_t getFrameDataSize(size_t &dataFrameSize, size_t bufferCount);
    132     virtual void onOrientationEvent(uint32_t orientation, uint32_t tilt);
    133 //-----------------------------------------------------------------------------
    134 
    135 
    136 private:
    137 
    138     class PreviewThread : public android::Thread {
    139             V4LCameraAdapter* mAdapter;
    140         public:
    141             PreviewThread(V4LCameraAdapter* hw) :
    142                     Thread(false), mAdapter(hw) { }
    143             virtual void onFirstRef() {
    144                 run("CameraPreviewThread", android::PRIORITY_URGENT_DISPLAY);
    145             }
    146             virtual bool threadLoop() {
    147                 mAdapter->previewThread();
    148                 // loop until we need to quit
    149                 return true;
    150             }
    151         };
    152 
    153     //Used for calculation of the average frame rate during preview
    154     status_t recalculateFPS();
    155 
    156     char * GetFrame(int &index);
    157 
    158     int previewThread();
    159 
    160 public:
    161 
    162 private:
    163     //capabilities data
    164     static const CapPixelformat mPixelformats [];
    165     static const CapResolution mPreviewRes [];
    166     static const CapFramerate mFramerates [];
    167     static const CapResolution mImageCapRes [];
    168 
    169     //camera defaults
    170     static const char DEFAULT_PREVIEW_FORMAT[];
    171     static const char DEFAULT_PREVIEW_SIZE[];
    172     static const char DEFAULT_FRAMERATE[];
    173     static const char DEFAULT_NUM_PREV_BUFS[];
    174 
    175     static const char DEFAULT_PICTURE_FORMAT[];
    176     static const char DEFAULT_PICTURE_SIZE[];
    177     static const char DEFAULT_FOCUS_MODE[];
    178     static const char * DEFAULT_VSTAB;
    179     static const char * DEFAULT_VNF;
    180 
    181     static status_t insertDefaults(CameraProperties::Properties*, V4L_TI_CAPTYPE&);
    182     static status_t insertCapabilities(CameraProperties::Properties*, V4L_TI_CAPTYPE&);
    183     static status_t insertPreviewFormats(CameraProperties::Properties* , V4L_TI_CAPTYPE&);
    184     static status_t insertPreviewSizes(CameraProperties::Properties* , V4L_TI_CAPTYPE&);
    185     static status_t insertImageSizes(CameraProperties::Properties* , V4L_TI_CAPTYPE&);
    186     static status_t insertFrameRates(CameraProperties::Properties* , V4L_TI_CAPTYPE&);
    187     static status_t sortAscend(V4L_TI_CAPTYPE&, uint16_t ) ;
    188 
    189     status_t v4lIoctl(int, int, void*);
    190     status_t v4lInitMmap(int&);
    191     status_t v4lInitUsrPtr(int&);
    192     status_t v4lStartStreaming();
    193     status_t v4lStopStreaming(int nBufferCount);
    194     status_t v4lSetFormat(int, int, uint32_t);
    195     status_t restartPreview();
    196 
    197 
    198     int mPreviewBufferCount;
    199     int mPreviewBufferCountQueueable;
    200     int mCaptureBufferCount;
    201     int mCaptureBufferCountQueueable;
    202     android::KeyedVector<CameraBuffer *, int> mPreviewBufs;
    203     android::KeyedVector<CameraBuffer *, int> mCaptureBufs;
    204     mutable android::Mutex mPreviewBufsLock;
    205     mutable android::Mutex mCaptureBufsLock;
    206     mutable android::Mutex mStopPreviewLock;
    207 
    208     android::CameraParameters mParams;
    209 
    210     bool mPreviewing;
    211     bool mCapturing;
    212     android::Mutex mLock;
    213 
    214     int mFrameCount;
    215     int mLastFrameCount;
    216     unsigned int mIter;
    217     nsecs_t mLastFPSTime;
    218 
    219     //variables holding the estimated framerate
    220     float mFPS, mLastFPS;
    221 
    222     int mSensorIndex;
    223 
    224     // protected by mLock
    225     android::sp<PreviewThread>   mPreviewThread;
    226 
    227     struct VideoInfo *mVideoInfo;
    228     int mCameraHandle;
    229 
    230     int nQueued;
    231     int nDequeued;
    232 
    233 };
    234 
    235 } // namespace Camera
    236 } // namespace Ti
    237 
    238 #endif //V4L_CAMERA_ADAPTER_H
    239