-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideocapture.cpp
375 lines (321 loc) · 9.94 KB
/
videocapture.cpp
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#define LOG_TAG "RearCamera"
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <errno.h>
#include <memory.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <cutils/log.h>
#include "assert.h"
#include "videocapture.h"
VideoCapture::VideoCapture() : mRunMode(STOPPED), mFrameReady(false),
mCameraWidth(CAMERA_WIDTH), mCameraHeight(CAMERA_HEIGHT), mCameraFourCC(CAMERA_FOURCC), mNbrBuffers(6)
{
}
VideoCapture::~VideoCapture()
{
if (mRawCamera != nullptr)
{
delete[] mRawCamera;
mRawCamera = nullptr;
}
if (mRawColorCamera != nullptr)
{
delete[] mRawColorCamera;
mRawColorCamera = nullptr;
}
}
bool VideoCapture::open(const char *deviceName)
{
mDeviceFd = ::open(deviceName, O_RDWR, 0);
if (mDeviceFd < 0)
{
ALOGD("failed to open device %s (%d = %s)", deviceName, errno, strerror(errno));
return false;
}
allocateBuffers(deviceName);
return true;
}
void VideoCapture::allocateBuffers(const char *deviceName)
{
v4l2_capability caps;
{
int result = ioctl(mDeviceFd, VIDIOC_QUERYCAP, &caps);
if (result < 0)
{
ALOGD("failed to get device caps for %s (%d = %s)", deviceName, errno, strerror(errno));
return;
}
}
ALOGD("Open Device: %s (fd=%d)", deviceName, mDeviceFd);
ALOGD(" Driver: %s", caps.driver);
ALOGD(" Card: %s", caps.card);
ALOGD(" Version: %u.%u.%u", (caps.version >> 16) & 0xFF, (caps.version >> 8) & 0xFF, (caps.version) & 0xFF);
ALOGD(" All Caps: %08X", caps.capabilities);
ALOGD(" Dev Caps: %08X", caps.device_caps);
int ret = prepare();
if (ret)
{
queueAllBuffers(CAMERA_CAPTURE_MODE);
}
}
int VideoCapture::prepare()
{
struct v4l2_format fmt;
struct v4l2_requestbuffers reqbuf;
struct v4l2_buffer buffer;
struct v4l2_plane buf_planes[2];
int ret = -1;
memset(&fmt, 0, sizeof(fmt));
fmt.type = CAMERA_CAPTURE_MODE;
fmt.fmt.pix_mp.width = mCameraWidth;
fmt.fmt.pix_mp.height = mCameraHeight;
fmt.fmt.pix_mp.pixelformat = mCameraFourCC;
fmt.fmt.pix_mp.colorspace = V4L2_COLORSPACE_SMPTE170M;
fmt.fmt.pix_mp.field = V4L2_FIELD_ANY;
ret = ioctl(mDeviceFd, VIDIOC_S_FMT, &fmt);
if (ret < 0)
{
ALOGD("Cant set format");
return 0;
}
mYBufferSize = fmt.fmt.pix_mp.plane_fmt[0].sizeimage;
mUVBufferSize = fmt.fmt.pix_mp.plane_fmt[1].sizeimage;
ALOGD("Current output format: fmt=0x%X, %dx%d, pixels bytes per line=%d",
fmt.fmt.pix.pixelformat, fmt.fmt.pix.width, fmt.fmt.pix.height, fmt.fmt.pix.bytesperline);
memset(&reqbuf, 0, sizeof(reqbuf));
reqbuf.count = mNbrBuffers;
reqbuf.type = CAMERA_CAPTURE_MODE;
reqbuf.memory = V4L2_MEMORY_MMAP;
ret = ioctl(mDeviceFd, VIDIOC_REQBUFS, &reqbuf);
if (ret < 0)
{
ALOGD("Cant request buffers");
return 0;
}
else
{
mNbrBuffers = reqbuf.count;
}
ALOGD(" %dx%d flags=%08x numbuffers:%d", fmt.fmt.pix.width, fmt.fmt.pix.height, fmt.fmt.pix.flags, mNbrBuffers);
for (int i = 0; i < mNbrBuffers; i++)
{
memset(&buffer, 0, sizeof(buffer));
buffer.type = CAMERA_CAPTURE_MODE;
buffer.memory = V4L2_MEMORY_MMAP;
buffer.index = i;
buffer.m.planes = buf_planes;
buffer.length = 2;
ret = ioctl(mDeviceFd, VIDIOC_QUERYBUF, &buffer);
if (ret < 0)
{
ALOGD("Cant query buffers");
return 0;
}
ALOGD("query buf, plane 0 = %d, offset 0 = %d", buffer.m.planes[0].length, buffer.m.planes[0].m.mem_offset);
mPointerBuffersY[i] = mmap(NULL, buffer.m.planes[0].length, PROT_READ | PROT_WRITE, MAP_SHARED,
mDeviceFd, buffer.m.planes[0].m.mem_offset);
if (MAP_FAILED == mPointerBuffersY[i])
{
while (i >= 0)
{
i--;
munmap(mPointerBuffersY[i], mYBufferSize);
mPointerBuffersY[i] = NULL;
}
ALOGD("Cant mmap buffers Y");
return 0;
}
ALOGD("query buf, plane 1 = %d, offset 1 = %d, sizeimage_uv %d", buffer.m.planes[1].length, buffer.m.planes[1].m.mem_offset, mYBufferSize);
mPointerBuffersUV[i] = mmap(NULL, buffer.m.planes[1].length, PROT_READ | PROT_WRITE,
MAP_SHARED, mDeviceFd, buffer.m.planes[1].m.mem_offset);
if (MAP_FAILED == mPointerBuffersUV[i])
{
while (i >= 0)
{
i--;
munmap(mPointerBuffersUV[i], mUVBufferSize);
mPointerBuffersY[i] = NULL;
}
ALOGD("Cant mmap buffers UV");
return 0;
}
}
if (mRawColorCamera != nullptr)
{
delete[] mRawColorCamera;
mRawColorCamera = nullptr;
}
if (mRawCamera != nullptr)
{
delete[] mRawCamera;
mRawCamera = nullptr;
}
mRawCamera = new unsigned char[mCameraWidth * mCameraHeight];
mRawColorCamera = new unsigned char[mCameraWidth * mCameraHeight / 2];
memset(mRawCamera, 0, mCameraWidth * mCameraHeight);
memset(mRawColorCamera, 0, mCameraWidth * mCameraHeight / 2);
ALOGD("Output Buffers = %d each of size %d\n", mNbrBuffers, mYBufferSize);
return 1;
}
int VideoCapture::queueAllBuffers(int type)
{
struct v4l2_buffer buffer;
struct v4l2_plane buf_planes[2];
int i = 0;
int ret = -1;
int lastqueued = -1;
for (i = 0; i < mNbrBuffers; i++)
{
memset(&buffer, 0, sizeof(buffer));
buffer.type = type;
buffer.memory = V4L2_MEMORY_MMAP;
buffer.index = i;
buffer.m.planes = buf_planes;
buffer.length = 2;
gettimeofday(&buffer.timestamp, NULL);
ret = ioctl(mDeviceFd, VIDIOC_QBUF, &buffer);
if (-1 == ret)
{
break;
}
else
{
lastqueued = i;
}
}
return lastqueued;
}
void VideoCapture::startV4Lstream(int type)
{
int ret = -1;
ret = ioctl(mDeviceFd, VIDIOC_STREAMON, &type);
if (-1 == ret)
{
ALOGD("Cant Stream on\n");
}
}
void VideoCapture::stopV4Lstream(int type)
{
int ret = -1;
ret = ioctl(mDeviceFd, VIDIOC_STREAMOFF, &type);
if (-1 == ret)
{
ALOGD("Cant Stream on\n");
}
}
void VideoCapture::close()
{
ALOGD("VideoCapture::close");
assert(mRunMode == STOPPED);
if (isOpen())
{
ALOGD("closing video device file handled %d", mDeviceFd);
::close(mDeviceFd);
mDeviceFd = -1;
}
}
bool VideoCapture::startStream()
{
ALOGD("Starting stream...");
int prevRunMode = mRunMode.fetch_or(RUN);
if (prevRunMode & RUN)
{
ALOGD("Already in RUN state, so we can't start a new streaming thread");
return false;
}
startV4Lstream(CAMERA_CAPTURE_MODE);
mCaptureThread = std::thread([this]() { collectFrames(); });
ALOGD("Stream started.");
return true;
}
void VideoCapture::stopStream()
{
int prevRunMode = mRunMode.fetch_or(STOPPING);
if (prevRunMode == STOPPED)
{
mRunMode = STOPPED;
}
else if (prevRunMode & STOPPING)
{
ALOGD("stopStream called while stream is already stopping. Reentrancy is not supported!");
return;
}
else
{
if (mCaptureThread.joinable())
{
mCaptureThread.join();
}
ALOGD("Capture thread stopped.");
}
}
void VideoCapture::collectFrames()
{
struct v4l2_buffer buf;
struct v4l2_plane buf_planes[2];
while (mRunMode == RUN)
{
dequeueFrame(CAMERA_CAPTURE_MODE, &buf, buf_planes);
ALOGD("dequeued buffer %d (flags:%08x, bytesused:%d, "
"offset main: %u mplaneOffset: %d buf.length: %d, buf.sequence:%d, buf.m.planes[0].length:%d buf.field %d buf.m.planes[buf.index].bytesused:%d",
buf.index, buf.flags, buf.m.planes[0].bytesused, buf.m.offset, buf.m.planes[0].data_offset, buf.length,
buf.sequence, buf.m.planes[0].length, buf.field, buf.m.planes[buf.index].bytesused);
{
const std::lock_guard<std::mutex> lock(mSafeMutex);
memcpy(mRawCamera, mPointerBuffersY[buf.index], mCameraWidth * mCameraHeight);
memcpy(mRawColorCamera, mPointerBuffersUV[buf.index], mCameraWidth * mCameraHeight / 2);
}
queueFrame(CAMERA_CAPTURE_MODE, buf.index, V4L2_FIELD_NONE, mYBufferSize, mUVBufferSize);
}
ALOGD("VideoCapture thread ending");
mRunMode = STOPPED;
}
int VideoCapture::queueFrame(int type, int index, int field, int size_y, int size_uv)
{
struct v4l2_buffer buffer;
struct v4l2_plane buf_planes[2];
int ret = -1;
buf_planes[0].length = buf_planes[0].bytesused = size_y;
buf_planes[1].length = buf_planes[1].bytesused = size_uv;
buf_planes[0].data_offset = buf_planes[1].data_offset = 0;
memset(&buffer, 0, sizeof(buffer));
buffer.type = type;
buffer.memory = V4L2_MEMORY_MMAP;
buffer.index = index;
buffer.m.planes = buf_planes;
buffer.field = field;
buffer.length = 2;
gettimeofday(&buffer.timestamp, NULL);
ret = ioctl(mDeviceFd, VIDIOC_QBUF, &buffer);
if (-1 == ret)
{
ALOGD("Failed to queueFrame\n");
return -1;
}
return ret;
}
int VideoCapture::dequeueFrame(int type, struct v4l2_buffer *buf, struct v4l2_plane *buf_planes)
{
int ret = -1;
memset(buf, 0, sizeof(*buf));
buf->type = type;
buf->memory = V4L2_MEMORY_MMAP;
buf->m.planes = buf_planes;
buf->length = 2;
ret = ioctl(mDeviceFd, VIDIOC_DQBUF, buf);
if (-1 == ret)
{
ALOGD("Failed to dequeueFrame\n");
return -1;
}
return ret;
}
std::tuple<const unsigned char *, const unsigned char *> VideoCapture::getRawBufferCamera()
{
const std::lock_guard<std::mutex> lock(mSafeMutex);
return std::make_tuple(mRawCamera, mRawColorCamera);
}