hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//#define LOG_NDEBUG 0
#define LOG_TAG "CamCaptureHelper"
#include <utils/Log.h>
 
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
 
#include <sys/select.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
 
#include "CamCaptureHelper.h"
 
#define FMT_NUM_PLANES 1
 
static enum v4l2_buf_type m_cur_type;
 
CamCaptureHelper::CamCaptureHelper() : mDevfd(-1)
{
    ALOGD("CamCaptureHelper enter");
    // default v4l2 request buffer count
    mPixBufCnt = 4;
}
 
CamCaptureHelper::~CamCaptureHelper()
{
    ALOGD("~CamCaptureHelper enter");
    deinit();
}
 
// Wrap ioctl() to spin on EINTR
int CamCaptureHelper::v4l2Ioctl(int fd, int req, void* arg)
{
    struct timespec poll_time;
    int ret;
 
    while ((ret = ioctl(fd, req, arg))) {
        if (ret == -1 && (EINTR != errno && EAGAIN != errno)) {
            break;
        }
        // 10 milliseconds
        poll_time.tv_sec = 0;
        poll_time.tv_nsec = 10000000;
        nanosleep(&poll_time, NULL);
    }
 
    return ret;
}
 
bool CamCaptureHelper::init(MetaInfo* meta)
{
    struct v4l2_capability cap;
    struct v4l2_format vfmt;
    struct v4l2_requestbuffers req;
    struct v4l2_buffer buf;
    enum v4l2_buf_type type;
 
    int i, buf_len;
 
    if (meta == NULL) {
        ALOGE("Failed to get metaData");
        return false;
    }
 
    ALOGD("camera capture init start: dev %s, wh %dx%d, format %d, bufcnt %d", meta->video_dev, meta->width,
          meta->height, meta->format, mPixBufCnt);
 
    mDevfd = open(meta->video_dev, O_RDWR, 0);
    if (mDevfd < 0) {
        ALOGE("Cannot open device");
        goto _FAIL;
    }
 
    // Determine if fd is a V4L2 Device
    if (0 != v4l2Ioctl(mDevfd, VIDIOC_QUERYCAP, &cap)) {
        ALOGE("Not v4l2 compatible");
        goto _FAIL;
    }
 
    if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) && !(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)) {
        ALOGE("Capture not supported");
        goto _FAIL;
    }
 
    if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
        ALOGE("Streaming IO Not Supported");
        goto _FAIL;
    }
 
    // Preserve original settings as set by v4l2-ctl for example
    // vfmt = (struct v4l2_format) {0};
    vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 
    if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
        vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
 
    vfmt.fmt.pix.width = meta->width;
    vfmt.fmt.pix.height = meta->height;
 
    // vfmt.fmt.pix.pixelformat = meta->format;
    vfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
 
    type = (v4l2_buf_type)vfmt.type;
    m_cur_type = (v4l2_buf_type)vfmt.type;
 
    if (-1 == v4l2Ioctl(mDevfd, VIDIOC_S_FMT, &vfmt)) {
        ALOGE("VIDIOC_S_FMT");
        goto _FAIL;
    }
 
    if (-1 == v4l2Ioctl(mDevfd, VIDIOC_G_FMT, &vfmt)) {
        ALOGE("VIDIOC_G_FMT");
        goto _FAIL;
    }
 
    ALOGD("VIDIOC_G_FMT w %d h %d", vfmt.fmt.pix.width, vfmt.fmt.pix.height);
 
    // Request memory-mapped buffers
    // req = (struct v4l2_requestbuffers) {0};
    req.count = mPixBufCnt;
    req.type = type;
    req.memory = V4L2_MEMORY_MMAP;
    if (-1 == v4l2Ioctl(mDevfd, VIDIOC_REQBUFS, &req)) {
        ALOGE("Device does not support mmap");
        goto _FAIL;
    }
 
    if (req.count != mPixBufCnt) {
        ALOGE("Device buffer count mismatch");
        goto _FAIL;
    }
 
    // mmap() the buffers into userspace memory
    for (i = 0; i < mPixBufCnt; i++) {
        // buf = (struct v4l2_buffer) {0};
        buf.type = type;
        buf.memory = V4L2_MEMORY_MMAP;
        buf.index = i;
        struct v4l2_plane planes[FMT_NUM_PLANES];
        buf.memory = V4L2_MEMORY_MMAP;
        if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
            buf.m.planes = planes;
            buf.length = FMT_NUM_PLANES;
        }
 
        if (-1 == v4l2Ioctl(mDevfd, VIDIOC_QUERYBUF, &buf)) {
            ALOGE("ERROR: VIDIOC_QUERYBUF");
            goto _FAIL;
        }
 
        if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == buf.type) {
            // tmp_buffers[n_buffers].length = buf.m.planes[0].length;
            buf_len = buf.m.planes[0].length;
            mCambuf[i].start =
                mmap(NULL /* start anywhere */, buf.m.planes[0].length, PROT_READ | PROT_WRITE /* required */,
                     MAP_SHARED /* recommended */, mDevfd, buf.m.planes[0].m.mem_offset);
        } else {
            buf_len = buf.length;
            mCambuf[i].start = mmap(NULL /* start anywhere */, buf.length, PROT_READ | PROT_WRITE /* required */,
                                    MAP_SHARED /* recommended */, mDevfd, buf.m.offset);
        }
        if (MAP_FAILED == mCambuf[i].start) {
            ALOGE("ERROR: Failed to map device frame buffers");
            goto _FAIL;
        }
        mCambuf[i].length = buf_len;
 
        struct v4l2_exportbuffer expbuf;
        // struct v4l2_exportbuffer expbuf = (struct v4l2_exportbuffer) {0} ;
        // xcam_mem_clear (expbuf);
        expbuf.type = type;
        expbuf.index = i;
        expbuf.flags = O_CLOEXEC;
        if (v4l2Ioctl(mDevfd, VIDIOC_EXPBUF, &expbuf) < 0) {
            ALOGE("get dma buf failed");
            goto _FAIL;
        } else {
            ALOGD("get dma buf(%d)-fd: %d", i, expbuf.fd);
        }
        mCambuf[i].export_fd = expbuf.fd;
    }
 
    for (i = 0; i < mPixBufCnt; i++) {
        struct v4l2_plane planes[FMT_NUM_PLANES];
 
        // buf = (struct v4l2_buffer) {0};
        buf.type = type;
        buf.memory = V4L2_MEMORY_MMAP;
        buf.index = i;
 
        if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
            buf.m.planes = planes;
            buf.length = FMT_NUM_PLANES;
        }
 
        if (-1 == v4l2Ioctl(mDevfd, VIDIOC_QBUF, &buf)) {
            ALOGE("ERROR: VIDIOC_QBUF %d", i);
            goto _FAIL;
        }
    }
 
    // Start capturing
    if (-1 == v4l2Ioctl(mDevfd, VIDIOC_STREAMON, &m_cur_type)) {
        ALOGE("ERROR: VIDIOC_STREAMON");
        return false;
    }
 
    return true;
 
_FAIL:
    deinit();
    return false;
}
 
void CamCaptureHelper::deinit()
{
    enum v4l2_buf_type type;
    int i;
 
    if (mDevfd < 0)
        return;
 
    // Stop capturing
    type = m_cur_type;
 
    v4l2Ioctl(mDevfd, VIDIOC_STREAMOFF, &type);
 
    // un-mmap() buffers
    for (i = 0; i < mPixBufCnt; i++) {
        munmap(mCambuf[i].start, mCambuf[i].length);
        close(mCambuf[i].export_fd);
    }
 
    // Close v4l2 device
    close(mDevfd);
    mDevfd = -1;
}
 
// Returns a pointer to a captured frame and its meta-data. NOT thread-safe.
int CamCaptureHelper::v4l2DequeueBuf()
{
    struct v4l2_buffer buf;
    enum v4l2_buf_type type;
 
    if (!mDevfd) {
        ALOGE("Init first");
        return -1;
    }
 
    type = m_cur_type;
    // buf = (struct v4l2_buffer) {0};
    buf.type = type;
    buf.memory = V4L2_MEMORY_MMAP;
 
    struct v4l2_plane planes[FMT_NUM_PLANES];
    if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
        buf.m.planes = planes;
        buf.length = FMT_NUM_PLANES;
    }
 
    if (-1 == v4l2Ioctl(mDevfd, VIDIOC_DQBUF, &buf)) {
        ALOGE("VIDIOC_DQBUF");
        return -1;
    }
 
    if (buf.index > mPixBufCnt) {
        ALOGE("buffer index out of bounds");
        return -1;
    }
 
    if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type)
        buf.bytesused = buf.m.planes[0].bytesused;
 
    return buf.index;
}
 
// It's OK to capture into this framebuffer now
void CamCaptureHelper::v4l2QueueBuf(int idx)
{
    struct v4l2_buffer buf;
    enum v4l2_buf_type type;
 
    if (!mDevfd) {
        ALOGE("Init first");
        return;
    }
 
    if (idx < 0)
        return;
 
    type = m_cur_type;
    // buf = (struct v4l2_buffer) {0};
    buf.type = type;
    buf.memory = V4L2_MEMORY_MMAP;
    buf.index = idx;
 
    struct v4l2_plane planes[FMT_NUM_PLANES];
    if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
        buf.m.planes = planes;
        buf.length = FMT_NUM_PLANES;
    }
 
    // Tell kernel it's ok to overwrite this frame
    if (-1 == v4l2Ioctl(mDevfd, VIDIOC_QBUF, &buf)) {
        ALOGE("VIDIOC_QBUF");
    }
}
 
bool CamCaptureHelper::getCameraBuffer(QMediaBuffer* buffer)
{
    int idx = v4l2DequeueBuf();
    if (idx < 0)
        return false;
 
    if (buffer == NULL) {
        ALOGE("getCameraBuffer: buffer NULL");
        return false;
    }
 
    buffer->setData(mCambuf[idx].start, mCambuf[idx].length);
    buffer->setFd(mCambuf[idx].export_fd);
    buffer->setBufferID(idx);
 
    return true;
}
 
bool CamCaptureHelper::putCameraBuffer(QMediaBuffer* buffer)
{
    if (buffer->getBufferID() > 0) {
        v4l2QueueBuf(buffer->getBufferID());
    }
    return true;
}