summaryrefslogtreecommitdiffstats
path: root/camera/inc/V4LCameraAdapter/V4LCameraAdapter.h
blob: e1e7cad08da2feacab0fc0632d01a3652b0eb446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Copyright (C) Texas Instruments - http://www.ti.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



#ifndef V4L_CAMERA_ADAPTER_H
#define V4L_CAMERA_ADAPTER_H

#include <linux/videodev2.h>

#include "CameraHal.h"
#include "BaseCameraAdapter.h"
#include "DebugUtils.h"
#include "Decoder_libjpeg.h"
#include "FrameDecoder.h"


namespace Ti {
namespace Camera {

#ifndef V4L2_PIX_FMT_H264
#define V4L2_PIX_FMT_H264 0
#endif

#define DEFAULT_PIXEL_FORMAT V4L2_PIX_FMT_YUYV
#define DEFAULT_CAPTURE_FORMAT V4L2_PIX_FMT_YUYV

#define NB_BUFFER 10
#define DEVICE "/dev/videoxx"
#define DEVICE_PATH "/dev/"
#define DEVICE_NAME "videoxx"

typedef int V4L_HANDLETYPE;

struct CapPixelformat {
        uint32_t pixelformat;
        const char *param;
};

struct CapResolution {
        size_t width, height;
        char param[10];
};

struct CapU32 {
        uint32_t num;
        const char *param;
};

typedef CapU32 CapFramerate;

struct VideoInfo {
    struct v4l2_capability cap;
    struct v4l2_format format;
    struct v4l2_buffer buf;
    struct v4l2_requestbuffers rb;
    void *mem[NB_BUFFER];
    void *CaptureBuffers[NB_BUFFER];
    bool isStreaming;
    int width;
    int height;
    int formatIn;
    int framesizeIn;
};

typedef struct V4L_TI_CAPTYPE {
    uint16_t        ulPreviewFormatCount;   // supported preview pixelformat count
    uint32_t        ePreviewFormats[32];
    uint16_t        ulPreviewResCount;   // supported preview resolution sizes
    CapResolution   tPreviewRes[32];
    uint16_t        ulCaptureResCount;   // supported capture resolution sizes
    CapResolution   tCaptureRes[32];
    uint16_t        ulFrameRateCount;   // supported frame rate
    uint16_t        ulFrameRates[32];
}V4L_TI_CAPTYPE;

/**
  * Class which completely abstracts the camera hardware interaction from camera hal
  * TODO: Need to list down here, all the message types that will be supported by this class
                Need to implement BufferProvider interface to use AllocateBuffer of OMX if needed
  */
class V4LCameraAdapter : public BaseCameraAdapter
{
public:

    /*--------------------Constant declarations----------------------------------------*/
    static const int32_t MAX_NO_BUFFERS = 20;

    ///@remarks OMX Camera has six ports - buffer input, time input, preview, image, video, and meta data
    static const int MAX_NO_PORTS = 6;

    ///Five second timeout
    static const int CAMERA_ADAPTER_TIMEOUT = 5000*1000;

public:

    V4LCameraAdapter(size_t sensor_index, CameraHal* hal);
    ~V4LCameraAdapter();


    ///Initialzes the camera adapter creates any resources required
    virtual status_t initialize(CameraProperties::Properties*);

    //APIs to configure Camera adapter and get the current parameter set
    virtual status_t setParameters(const android::CameraParameters& params);
    virtual void getParameters(android::CameraParameters& params);

    // API
    virtual status_t UseBuffersPreview(CameraBuffer *bufArr, int num);
    virtual status_t UseBuffersCapture(CameraBuffer *bufArr, int num);

    static status_t getCaps(const int sensorId, CameraProperties::Properties* params, V4L_HANDLETYPE handle);

    void setupWorkingMode();

protected:

//----------Parent class method implementation------------------------------------
    virtual status_t startPreview();
    virtual status_t stopPreview();
    virtual status_t takePicture();
    virtual status_t stopImageCapture();
    virtual status_t autoFocus();
    virtual status_t useBuffers(CameraMode mode, CameraBuffer *bufArr, int num, size_t length, unsigned int queueable);
    virtual status_t fillThisBuffer(CameraBuffer *frameBuf, CameraFrame::FrameType frameType);
    virtual status_t getFrameSize(size_t &width, size_t &height);
    virtual status_t getPictureBufferSize(CameraFrame &frame, size_t bufferCount);
    virtual status_t getFrameDataSize(size_t &dataFrameSize, size_t bufferCount);
    virtual void onOrientationEvent(uint32_t orientation, uint32_t tilt);
//-----------------------------------------------------------------------------


private:

    class PreviewThread : public android::Thread {
            V4LCameraAdapter* mAdapter;
        public:
            PreviewThread(V4LCameraAdapter* hw) :
                    Thread(false), mAdapter(hw) { }
            virtual void onFirstRef() {
                run("CameraPreviewThread", android::PRIORITY_URGENT_DISPLAY);
            }
            virtual bool threadLoop() {
                mAdapter->previewThread();
                // loop until we need to quit
                return true;
            }
        };

    //Used for calculation of the average frame rate during preview
    status_t recalculateFPS();

    char * GetFrame(int &index, int &filledLen);

    int previewThread();

private:
    //capabilities data
    static const CapPixelformat mPixelformats [];
    static const CapResolution mPreviewRes [];
    static const CapFramerate mFramerates [];
    static const CapResolution mImageCapRes [];

    //camera defaults
    static const char DEFAULT_PREVIEW_FORMAT[];
    static const char DEFAULT_PREVIEW_SIZE[];
    static const char DEFAULT_FRAMERATE[];
    static const char DEFAULT_NUM_PREV_BUFS[];

    static const char DEFAULT_PICTURE_FORMAT[];
    static const char DEFAULT_PICTURE_SIZE[];
    static const char DEFAULT_FOCUS_MODE[];
    static const char DEFAULT_FRAMERATE_RANGE[];
    static const char * DEFAULT_VSTAB;
    static const char * DEFAULT_VNF;

    static status_t insertDefaults(CameraProperties::Properties*, V4L_TI_CAPTYPE&);
    static status_t insertCapabilities(CameraProperties::Properties*, V4L_TI_CAPTYPE&);
    static status_t insertPreviewFormats(CameraProperties::Properties* , V4L_TI_CAPTYPE&);
    static status_t insertPreviewSizes(CameraProperties::Properties* , V4L_TI_CAPTYPE&);
    static status_t insertImageSizes(CameraProperties::Properties* , V4L_TI_CAPTYPE&);
    static status_t insertFrameRates(CameraProperties::Properties* , V4L_TI_CAPTYPE&);
    static status_t sortAscend(V4L_TI_CAPTYPE&, uint16_t ) ;

    status_t v4lIoctl(int, int, void*);
    status_t v4lInitMmap(int& count, int width, int height);
    status_t v4lInitUsrPtr(int&);
    status_t v4lStartStreaming();
    status_t v4lStopStreaming(int nBufferCount);
    status_t v4lSetFormat(int, int, uint32_t);
    status_t restartPreview();
    status_t applyFpsValue();
    status_t returnBufferToV4L(int id);
    void returnOutputBuffer(int index);
    bool isNeedToUseDecoder() const;

    int mPreviewBufferCount;
    int mPreviewBufferCountQueueable;
    int mCaptureBufferCount;
    int mCaptureBufferCountQueueable;
    CameraBuffer *mPreviewBufs[NB_BUFFER];
    android::KeyedVector<CameraBuffer *, int> mCaptureBufs;

    android::CameraParameters mParams;

    bool mPreviewing;
    bool mCapturing;
    mutable android::Mutex mLock;

    int mFrameCount;
    int mLastFrameCount;
    unsigned int mIter;
    nsecs_t mLastFPSTime;

    //variables holding the estimated framerate
    float mFPS, mLastFPS;

    int mSensorIndex;

    // protected by mLock
    android::sp<PreviewThread>   mPreviewThread;

    struct VideoInfo *mVideoInfo;
    int mCameraHandle;

    int nQueued;
    int nDequeued;
    int mQueuedOutputBuffers;

    FrameDecoder* mDecoder;
    android::Vector< android::sp<MediaBuffer> > mInBuffers;
    android::Vector< android::sp<MediaBuffer> > mOutBuffers;

    android::Mutex mV4LLock;

    int mPixelFormat;
    int mFrameRate;

    android::Mutex mStopLock;
    android::Condition mStopCondition;

    CameraHal* mCameraHal;
    int mSkipFramesCount;
};

} // namespace Camera
} // namespace Ti

#endif //V4L_CAMERA_ADAPTER_H