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 "CameraHal.h" 23 #include "BaseCameraAdapter.h" 24 #include "DebugUtils.h" 25 26 namespace android { 27 28 #define DEFAULT_PIXEL_FORMAT V4L2_PIX_FMT_YUYV 29 #define NB_BUFFER 10 30 #define DEVICE "/dev/video4" 31 32 33 struct VideoInfo { 34 struct v4l2_capability cap; 35 struct v4l2_format format; 36 struct v4l2_buffer buf; 37 struct v4l2_requestbuffers rb; 38 void *mem[NB_BUFFER]; 39 bool isStreaming; 40 int width; 41 int height; 42 int formatIn; 43 int framesizeIn; 44 }; 45 46 47 /** 48 * Class which completely abstracts the camera hardware interaction from camera hal 49 * TODO: Need to list down here, all the message types that will be supported by this class 50 Need to implement BufferProvider interface to use AllocateBuffer of OMX if needed 51 */ 52 class V4LCameraAdapter : public BaseCameraAdapter 53 { 54 public: 55 56 /*--------------------Constant declarations----------------------------------------*/ 57 static const int32_t MAX_NO_BUFFERS = 20; 58 59 ///@remarks OMX Camera has six ports - buffer input, time input, preview, image, video, and meta data 60 static const int MAX_NO_PORTS = 6; 61 62 ///Five second timeout 63 static const int CAMERA_ADAPTER_TIMEOUT = 5000*1000; 64 65 public: 66 67 V4LCameraAdapter(); 68 ~V4LCameraAdapter(); 69 70 71 ///Initialzes the camera adapter creates any resources required 72 virtual status_t initialize(CameraProperties::Properties*, int sensor_index=0); 73 74 //APIs to configure Camera adapter and get the current parameter set 75 virtual status_t setParameters(const CameraParameters& params); 76 virtual void getParameters(CameraParameters& params); 77 78 // API 79 virtual status_t UseBuffersPreview(void* bufArr, int num); 80 81 //API to flush the buffers for preview 82 status_t flushBuffers(); 83 84 protected: 85 86 //----------Parent class method implementation------------------------------------ 87 virtual status_t startPreview(); 88 virtual status_t stopPreview(); 89 virtual status_t useBuffers(CameraMode mode, void* bufArr, int num, size_t length, unsigned int queueable); 90 virtual status_t fillThisBuffer(void* frameBuf, CameraFrame::FrameType frameType); 91 virtual status_t getFrameSize(size_t &width, size_t &height); 92 virtual status_t getPictureBufferSize(size_t &length, size_t bufferCount); 93 virtual status_t getFrameDataSize(size_t &dataFrameSize, size_t bufferCount); 94 virtual void onOrientationEvent(uint32_t orientation, uint32_t tilt); 95 //----------------------------------------------------------------------------- 96 97 98 private: 99 100 class PreviewThread : public Thread { 101 V4LCameraAdapter* mAdapter; 102 public: PreviewThread(V4LCameraAdapter * hw)103 PreviewThread(V4LCameraAdapter* hw) : 104 Thread(false), mAdapter(hw) { } onFirstRef()105 virtual void onFirstRef() { 106 run("CameraPreviewThread", PRIORITY_URGENT_DISPLAY); 107 } threadLoop()108 virtual bool threadLoop() { 109 mAdapter->previewThread(); 110 // loop until we need to quit 111 return true; 112 } 113 }; 114 115 //Used for calculation of the average frame rate during preview 116 status_t recalculateFPS(); 117 118 char * GetFrame(int &index); 119 120 int previewThread(); 121 122 public: 123 124 private: 125 int mPreviewBufferCount; 126 KeyedVector<int, int> mPreviewBufs; 127 mutable Mutex mPreviewBufsLock; 128 129 CameraParameters mParams; 130 131 bool mPreviewing; 132 bool mCapturing; 133 Mutex mLock; 134 135 int mFrameCount; 136 int mLastFrameCount; 137 unsigned int mIter; 138 nsecs_t mLastFPSTime; 139 140 //variables holding the estimated framerate 141 float mFPS, mLastFPS; 142 143 int mSensorIndex; 144 145 // protected by mLock 146 sp<PreviewThread> mPreviewThread; 147 148 struct VideoInfo *mVideoInfo; 149 int mCameraHandle; 150 151 152 int nQueued; 153 int nDequeued; 154 155 }; 156 }; //// namespace 157 #endif //V4L_CAMERA_ADAPTER_H 158 159