-
Notifications
You must be signed in to change notification settings - Fork 27
/
camdev.cpp
executable file
·686 lines (590 loc) · 23.1 KB
/
camdev.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
#define LOG_TAG "camdev"
// 包含头文件
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <utils/Log.h>
#include "ffutils.h"
#include "ffjpeg.h"
#include "watermark.h"
#include "camdev.h"
extern "C" {
#include "libswscale/swscale.h"
}
#ifdef PLATFORM_ALLWINNER_A33
extern "C" int ion_alloc_open (void);
extern "C" int ion_alloc_close(void);
extern "C" void ion_flush_cache(void *pbuf, int size);
#endif
// 内部常量定义
#define DO_USE_VAR(v) do { v = v; } while (0)
#define VIDEO_CAPTURE_BUFFER_COUNT 3
#define NATIVE_WIN_BUFFER_COUNT 3
#define DEF_WIN_PIX_FMT HAL_PIXEL_FORMAT_YCrCb_420_SP // HAL_PIXEL_FORMAT_RGBX_8888 or HAL_PIXEL_FORMAT_YCrCb_420_SP
#define CAMDEV_GRALLOC_USAGE (GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_NEVER | GRALLOC_USAGE_HW_TEXTURE)
// 内部类型定义
struct video_buffer {
void *addr;
unsigned len;
};
// camdev context
typedef struct {
struct v4l2_buffer buf;
struct video_buffer vbs[VIDEO_CAPTURE_BUFFER_COUNT];
int fd;
sp<ANativeWindow> new_win;
sp<ANativeWindow> cur_win;
int win_w;
int win_h;
#define CAMDEV_TS_EXIT (1 << 0) // exit threads
#define CAMDEV_TS_PAUSE_C (1 << 1) // pause capture
#define CAMDEV_TS_PAUSE_P (1 << 2) // pause preview
#define CAMDEV_TS_PREVIEW (1 << 3) // enable preview
sem_t sem_render;
pthread_t thread_id_render;
pthread_t thread_id_capture;
int thread_state;
int update_flag;
int cam_pixfmt;
int cam_stride;
int cam_w;
int cam_h;
int cam_frate_num; // camdev frame rate num get from v4l2 interface
int cam_frate_den; // camdev frame rate den get from v4l2 interface
SwsContext *swsctxt;
CAMDEV_CAPTURE_CALLBACK callback;
void *recorder;
void *jpegdec;
#define MAX_WATERMARK_SIZE 64
int watermark_x;
int watermark_y;
char watermark_str[MAX_WATERMARK_SIZE];
} CAMDEV;
// 内部函数实现
static void render_v4l2(CAMDEV *cam,
void *dstbuf, int dstlen, int dstfmt, int dstw, int dsth,
void *srcbuf, int srclen, int srcfmt, int srcw, int srch)
{
if (dstfmt != DEF_WIN_PIX_FMT) {
return;
}
// src fmt
AVPixelFormat sws_src_fmt = (AVPixelFormat)v4l2dev_pixfmt_to_ffmpeg_pixfmt(srcfmt);
// dst fmt
AVPixelFormat sws_dst_fmt = (AVPixelFormat)android_pixfmt_to_ffmpeg_pixfmt(dstfmt);
// dst len
if (dstlen == -1) {
switch (dstfmt) {
case HAL_PIXEL_FORMAT_RGB_565: dstlen = dstw * dsth * 2; break;
case HAL_PIXEL_FORMAT_RGBX_8888: dstlen = dstw * dsth * 4; break;
case HAL_PIXEL_FORMAT_YV12: dstlen = dstw * dsth + dstw * dsth / 2; break;
case HAL_PIXEL_FORMAT_YCrCb_420_SP: dstlen = dstw * dsth + dstw * dsth / 2; break;
default: dstlen = 0; break;
}
}
// ALOGD("srcfmt = 0x%0x, srcw = %d, srch = %d, srclen = %d, sws_src_fmt = 0x%0x\n", srcfmt, srcw, srch, srclen, sws_src_fmt);
// ALOGD("dstfmt = 0x%0x, dstw = %d, dsth = %d, dstlen = %d, sws_dst_fmt = 0x%0x\n", dstfmt, dstw, dsth, dstlen, sws_dst_fmt);
// memcpy if same fmt and size
if (sws_src_fmt == sws_dst_fmt && srcw == dstw && srch == dsth) {
// ALOGD("===ck=== sws_src_fmt = 0x%0x, sws_dst_fmt = 0x%0x", sws_src_fmt, sws_dst_fmt);
// memcpy(dstbuf, srcbuf, dstlen < srclen ? dstlen : srclen);
memcpy(dstbuf, srcbuf, srcw * srch);
memcpy((uint8_t*)dstbuf + srcw * srch, (uint8_t*)srcbuf + srcw * srch + 1, srcw * srch / 2 - 1);
return;
}
uint8_t* dst_data[AV_NUM_DATA_POINTERS] = { (uint8_t*)dstbuf, (uint8_t*)dstbuf + dstw * dsth, (uint8_t*)dstbuf + dstw * dsth };
uint8_t* src_data[AV_NUM_DATA_POINTERS] = { (uint8_t*)srcbuf, (uint8_t*)srcbuf + srcw * srch, (uint8_t*)srcbuf + srcw * srch };
int dst_linesize[AV_NUM_DATA_POINTERS] = { dstw, dstw / 1, dstw / 1 };
int src_linesize[AV_NUM_DATA_POINTERS] = { srcw, srcw / 1, srcw / 1 };
if (srcfmt == V4L2_PIX_FMT_YUYV) {
src_linesize[0] = srcw * 2;
}
if (srcfmt == V4L2_PIX_FMT_MJPEG) {
AVFrame *pic = ffjpeg_decoder_decode(cam->jpegdec, srcbuf, srclen);
sws_src_fmt = (AVPixelFormat)pic->format;
memcpy(src_data , pic->data , sizeof(src_data ));
memcpy(src_linesize, pic->linesize, sizeof(src_linesize));
}
if (dstfmt == HAL_PIXEL_FORMAT_RGBX_8888) {
dst_linesize[0] = dstw * 4;
}
//++ do sws scale
if (cam->win_w != dstw || cam->win_h != dsth) {
if (cam->swsctxt) {
sws_freeContext(cam->swsctxt);
}
cam->swsctxt = sws_getContext(srcw, srch, sws_src_fmt, dstw, dsth, sws_dst_fmt, SWS_POINT, 0, 0, 0);
cam->win_w = dstw;
cam->win_h = dsth;
}
sws_scale(cam->swsctxt, src_data, src_linesize, 0, srch, dst_data, dst_linesize);
//-- do sws scale
}
static void* camdev_capture_thread_proc(void *param)
{
CAMDEV *cam = (CAMDEV*)param;
int err;
//++ for select
fd_set fds;
struct timeval tv;
//-- for select
while (!(cam->thread_state & CAMDEV_TS_EXIT)) {
FD_ZERO(&fds);
FD_SET (cam->fd, &fds);
tv.tv_sec = 1;
tv.tv_usec = 0;
if (select(cam->fd + 1, &fds, NULL, NULL, &tv) <= 0) {
ALOGD("select error or timeout !\n");
continue;
}
if (cam->thread_state & CAMDEV_TS_PAUSE_C) {
cam->thread_state |= (CAMDEV_TS_PAUSE_C << 16);
usleep(10*1000);
continue;
}
// dequeue camera video buffer
if (-1 == ioctl(cam->fd, VIDIOC_DQBUF, &cam->buf)) {
ALOGD("failed to de-queue buffer !\n");
continue;
}
if (cam->watermark_str[0]) {
watermark_putstring(cam->vbs[cam->buf.index].addr, cam->cam_w, cam->cam_h, cam->watermark_x, cam->watermark_y, cam->watermark_str);
#ifdef PLATFORM_ALLWINNER_A33
ion_flush_cache(cam->vbs[cam->buf.index].addr, cam->buf.bytesused);
#endif
}
// ALOGD("%d. bytesused: %d, sequence: %d, length = %d\n", cam->buf.index, cam->buf.bytesused,
// cam->buf.sequence, cam->buf.length);
// ALOGD("timestamp: %ld, %ld\n", cam->buf.timestamp.tv_sec, cam->buf.timestamp.tv_usec);
if (cam->thread_state & CAMDEV_TS_PREVIEW) {
sem_post(&cam->sem_render);
}
if (cam->callback) {
int camw = cam->cam_w;
int camh = cam->cam_h;
uint8_t *cbuf = (uint8_t*)cam->vbs[cam->buf.index].addr;
void *data[AV_NUM_DATA_POINTERS] = { (uint8_t*)cbuf, (uint8_t*)cbuf + camw * camh, (uint8_t*)cbuf + camw * camh };
int linesize[AV_NUM_DATA_POINTERS] = { camw, camw / 1, camw / 1 };
int64_t pts = cam->buf.timestamp.tv_sec * 1000000 + cam->buf.timestamp.tv_usec;
if (cam->cam_pixfmt == V4L2_PIX_FMT_YUYV) {
linesize[0] = camw * 2;
}
linesize[AV_NUM_DATA_POINTERS-1] = cam->buf.bytesused; // store the buffer size
data [AV_NUM_DATA_POINTERS-2] = (uint8_t*)cam->buf.m.offset; // store the physic addr1
data [AV_NUM_DATA_POINTERS-1] = (uint8_t*)cam->buf.m.offset + camw * camh; // store the physic addr2
cam->callback(cam->recorder, data, linesize, pts);
}
// requeue camera video buffer
if (-1 == ioctl(cam->fd, VIDIOC_QBUF , &cam->buf)) {
ALOGD("failed to en-queue buffer !\n");
}
}
// ALOGD("camdev_capture_thread_proc exited !");
return NULL;
}
static void* camdev_render_thread_proc(void *param)
{
CAMDEV *cam = (CAMDEV*)param;
int err;
while (!(cam->thread_state & CAMDEV_TS_EXIT)) {
if (0 != sem_wait(&cam->sem_render)) {
ALOGD("failed to wait sem_render !\n");
break;
}
if (cam->thread_state & CAMDEV_TS_PAUSE_P) {
cam->thread_state |= (CAMDEV_TS_PAUSE_P << 16);
usleep(10*1000);
continue;
}
if (cam->update_flag) {
cam->cur_win = cam->new_win;
if (cam->cur_win != NULL) {
native_window_set_usage (cam->cur_win.get(), CAMDEV_GRALLOC_USAGE);
native_window_set_scaling_mode (cam->cur_win.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
native_window_set_buffer_count (cam->cur_win.get(), NATIVE_WIN_BUFFER_COUNT);
native_window_set_buffers_format (cam->cur_win.get(), DEF_WIN_PIX_FMT);
native_window_set_buffers_dimensions(cam->cur_win.get(), cam->cam_w, cam->cam_h);
}
cam->update_flag = 0;
}
char *data = (char*)cam->vbs[cam->buf.index].addr;
int len = cam->buf.bytesused;
ANativeWindowBuffer *buf;
if (cam->cur_win != NULL && 0 == native_window_dequeue_buffer_and_wait(cam->cur_win.get(), &buf)) {
GraphicBufferMapper &mapper = GraphicBufferMapper::get();
Rect bounds(buf->width, buf->height);
void *dst = NULL;
if (0 == mapper.lock(buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, &dst)) {
render_v4l2(cam,
dst , -1 , buf->format , buf->width, buf->height,
data, len, cam->cam_pixfmt, cam->cam_w, cam->cam_h);
mapper.unlock(buf->handle);
}
if ((err = cam->cur_win->queueBuffer(cam->cur_win.get(), buf, -1)) != 0) {
ALOGW("Surface::queueBuffer returned error %d\n", err);
}
}
}
// ALOGD("camdev_render_thread_proc exited !");
return NULL;
}
static int v4l2_try_fmt_size(int fd, int fmt, int *width, int *height)
{
struct v4l2_fmtdesc fmtdesc;
struct v4l2_format v4l2fmt;
int find = 0;
int ret = 0;
fmtdesc.index = 0;
fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
#if 0
ALOGD("\n");
ALOGD("VIDIOC_ENUM_FMT \n");
ALOGD("------------------\n");
#endif
while (ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) != -1) {
#if 0
ALOGD("%d. flags: %d, description: %-16s, pixelfmt: 0x%0x\n",
fmtdesc.index, fmtdesc.flags,
fmtdesc.description,
fmtdesc.pixelformat);
#endif
if (fmt == (int)fmtdesc.pixelformat) {
find = 1;
}
fmtdesc.index++;
}
ALOGD("\n");
if (!find) {
ALOGD("video device can't support pixel format: 0x%0x\n", fmt);
return -1;
}
// try format
v4l2fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
v4l2fmt.fmt.pix.width = *width;
v4l2fmt.fmt.pix.height = *height;
v4l2fmt.fmt.pix.pixelformat = fmt;
v4l2fmt.fmt.pix.field = V4L2_FIELD_NONE;
ret = ioctl(fd, VIDIOC_TRY_FMT, &v4l2fmt);
if (ret < 0)
{
ALOGE("VIDIOC_TRY_FMT Failed: %s\n", strerror(errno));
return ret;
}
// driver surpport this size
*width = v4l2fmt.fmt.pix.width;
*height = v4l2fmt.fmt.pix.height;
ALOGD("using pixel format: 0x%0x\n", fmt);
ALOGD("using frame size: %d, %d\n\n", *width, *height);
return 0;
}
// 函数实现
void* camdev_init(const char *dev, int sub, int w, int h, int frate)
{
CAMDEV *cam = new CAMDEV();
if (!cam) {
return NULL;
} else {
memset(cam, 0, sizeof(CAMDEV));
}
#ifdef PLATFORM_ALLWINNER_A33
ion_alloc_open();
#endif
// open camera device
cam->fd = open(dev, O_RDWR | O_NONBLOCK);
if (cam->fd < 0) {
ALOGW("failed to open video device: %s\n", dev);
delete cam;
return NULL;
}
struct v4l2_capability cap;
ioctl(cam->fd, VIDIOC_QUERYCAP, &cap);
ALOGD("\n");
ALOGD("video device caps \n");
ALOGD("------------------\n");
ALOGD("driver: %s\n" , cap.driver );
ALOGD("card: %s\n" , cap.card );
ALOGD("bus_info: %s\n" , cap.bus_info );
ALOGD("version: %0x\n", cap.version );
ALOGD("capabilities: %0x\n", cap.capabilities);
ALOGD("\n");
if (strcmp((char*)cap.driver, "uvcvideo") != 0) {
struct v4l2_input input;
input.index = sub;
ioctl(cam->fd, VIDIOC_S_INPUT, &input);
}
if (0 == v4l2_try_fmt_size(cam->fd, V4L2_PIX_FMT_MJPEG, &w, &h)) {
cam->cam_pixfmt = V4L2_PIX_FMT_MJPEG;
cam->cam_w = w;
cam->cam_h = h;
}
else if (0 == v4l2_try_fmt_size(cam->fd, V4L2_PIX_FMT_NV12, &w, &h)) {
// ALOGD("===ck=== V4L2_PIX_FMT_NV12");
cam->cam_pixfmt = V4L2_PIX_FMT_NV12;
cam->cam_w = w;
cam->cam_h = h;
}
else if (0 == v4l2_try_fmt_size(cam->fd, V4L2_PIX_FMT_NV21, &w, &h)) {
// ALOGD("===ck=== V4L2_PIX_FMT_NV21");
cam->cam_pixfmt = V4L2_PIX_FMT_NV21;
cam->cam_w = w;
cam->cam_h = h;
}
else if (0 == v4l2_try_fmt_size(cam->fd, V4L2_PIX_FMT_YUYV, &w, &h)) {
// ALOGD("===ck=== V4L2_PIX_FMT_YUYV");
cam->cam_pixfmt = V4L2_PIX_FMT_YUYV;
cam->cam_w = w;
cam->cam_h = h;
}
struct v4l2_format v4l2fmt;
v4l2fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ioctl(cam->fd, VIDIOC_G_FMT, &v4l2fmt);
v4l2fmt.fmt.pix.pixelformat = cam->cam_pixfmt;
v4l2fmt.fmt.pix.width = cam->cam_w;
v4l2fmt.fmt.pix.height = cam->cam_h;
if (ioctl(cam->fd, VIDIOC_S_FMT, &v4l2fmt) != -1) {
ALOGD("VIDIOC_S_FMT \n");
ALOGD("------------------\n");
ALOGD("width: %d\n", v4l2fmt.fmt.pix.width );
ALOGD("height: %d\n", v4l2fmt.fmt.pix.height );
ALOGD("pixfmt: 0x%0x\n", v4l2fmt.fmt.pix.pixelformat );
ALOGD("field: %d\n", v4l2fmt.fmt.pix.field );
ALOGD("bytesperline: %d\n", v4l2fmt.fmt.pix.bytesperline);
ALOGD("sizeimage: %d\n", v4l2fmt.fmt.pix.sizeimage );
ALOGD("colorspace: %d\n", v4l2fmt.fmt.pix.colorspace );
cam->cam_stride = v4l2fmt.fmt.pix.bytesperline;
}
else {
ALOGW("failed to set camera preview size and pixel format !\n");
close(cam->fd);
delete cam;
return NULL;
}
struct v4l2_streamparm streamparam;
streamparam.parm.capture.timeperframe.numerator = 1;
streamparam.parm.capture.timeperframe.denominator = frate;
streamparam.parm.capture.capturemode = 0;
streamparam.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl(cam->fd, VIDIOC_S_PARM, &streamparam) != -1) {
ioctl(cam->fd, VIDIOC_G_PARM, &streamparam);
ALOGD("current camera frame rate: %d/%d !\n",
streamparam.parm.capture.timeperframe.denominator,
streamparam.parm.capture.timeperframe.numerator );
cam->cam_frate_num = streamparam.parm.capture.timeperframe.denominator;
cam->cam_frate_den = streamparam.parm.capture.timeperframe.numerator;
}
else {
ALOGW("failed to set camera frame rate !\n");
}
struct v4l2_requestbuffers req;
req.count = VIDEO_CAPTURE_BUFFER_COUNT;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
ioctl(cam->fd, VIDIOC_REQBUFS, &req);
for (int i=0; i<VIDEO_CAPTURE_BUFFER_COUNT; i++) {
cam->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
cam->buf.memory = V4L2_MEMORY_MMAP;
cam->buf.index = i;
ioctl(cam->fd, VIDIOC_QUERYBUF, &cam->buf);
cam->vbs[i].addr= mmap(NULL, cam->buf.length, PROT_READ | PROT_WRITE, MAP_SHARED,
cam->fd, cam->buf.m.offset);
cam->vbs[i].len = cam->buf.length;
ioctl(cam->fd, VIDIOC_QBUF, &cam->buf);
}
if (cam->cam_pixfmt == V4L2_PIX_FMT_MJPEG) {
cam->jpegdec = ffjpeg_decoder_init();
}
// set capture & preview pause flags
cam->thread_state |= (CAMDEV_TS_PAUSE_C | CAMDEV_TS_PAUSE_P);
// create sem_render
sem_init(&cam->sem_render, 0, 0);
// create capture thread
pthread_create(&cam->thread_id_capture, NULL, camdev_capture_thread_proc, cam);
// create render thread
pthread_create(&cam->thread_id_render , NULL, camdev_render_thread_proc , cam);
return cam;
}
void camdev_close(void *ctxt)
{
CAMDEV *cam = (CAMDEV*)ctxt;
if (!cam) return;
// wait thread safely exited
cam->thread_state |= CAMDEV_TS_EXIT; sem_post(&cam->sem_render);
pthread_join(cam->thread_id_capture, NULL);
pthread_join(cam->thread_id_render , NULL);
if (cam->cam_pixfmt == V4L2_PIX_FMT_MJPEG) {
ffjpeg_decoder_free(cam->jpegdec);
}
// unmap buffers
for (int i=0; i<VIDEO_CAPTURE_BUFFER_COUNT; i++) {
munmap(cam->vbs[i].addr, cam->vbs[i].len);
}
// free sws context
if (cam->swsctxt) {
sws_freeContext(cam->swsctxt);
}
// close & free
close(cam->fd);
delete cam;
#ifdef PLATFORM_ALLWINNER_A33
ion_alloc_close();
#endif
}
void camdev_reset(void *ctxt, int w, int h, int frate)
{
CAMDEV *cam = (CAMDEV*)ctxt;
int old_thread_state = cam->thread_state;
// stop preview thread
cam->thread_state &=~(CAMDEV_TS_PAUSE_P << 16);
cam->thread_state |= (CAMDEV_TS_PAUSE_P << 0 );
sem_post(&cam->sem_render);
while ((cam->thread_state & (CAMDEV_TS_PAUSE_P << 16)) == 0) usleep(10000);
// stop capture thread
camdev_capture_stop(cam);
// unmap buffers
for (int i=0; i<VIDEO_CAPTURE_BUFFER_COUNT; i++) {
munmap(cam->vbs[i].addr, cam->vbs[i].len);
}
cam->cam_w = w;
cam->cam_h = h;
v4l2_try_fmt_size(cam->fd, cam->cam_pixfmt, &cam->cam_w, &cam->cam_h);
struct v4l2_format v4l2fmt;
v4l2fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ioctl(cam->fd, VIDIOC_G_FMT, &v4l2fmt);
v4l2fmt.fmt.pix.pixelformat = cam->cam_pixfmt;
v4l2fmt.fmt.pix.width = cam->cam_w;
v4l2fmt.fmt.pix.height = cam->cam_h;
ioctl(cam->fd, VIDIOC_S_FMT, &v4l2fmt);
struct v4l2_streamparm streamparam;
streamparam.parm.capture.timeperframe.numerator = 1;
streamparam.parm.capture.timeperframe.denominator = frate;
streamparam.parm.capture.capturemode = 0;
streamparam.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ioctl(cam->fd, VIDIOC_S_PARM, &streamparam);
ioctl(cam->fd, VIDIOC_G_PARM, &streamparam);
cam->cam_frate_num = streamparam.parm.capture.timeperframe.denominator;
cam->cam_frate_den = streamparam.parm.capture.timeperframe.numerator;
// remap buffers
struct v4l2_requestbuffers req;
req.count = VIDEO_CAPTURE_BUFFER_COUNT;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
ioctl(cam->fd, VIDIOC_REQBUFS, &req);
for (int i=0; i<VIDEO_CAPTURE_BUFFER_COUNT; i++) {
cam->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
cam->buf.memory = V4L2_MEMORY_MMAP;
cam->buf.index = i;
ioctl(cam->fd, VIDIOC_QUERYBUF, &cam->buf);
cam->vbs[i].addr= mmap(NULL, cam->buf.length, PROT_READ | PROT_WRITE, MAP_SHARED,
cam->fd, cam->buf.m.offset);
cam->vbs[i].len = cam->buf.length;
ioctl(cam->fd, VIDIOC_QBUF, &cam->buf);
}
// set update_flag
cam->update_flag = 1;
// start capture if needed
if (!(old_thread_state & CAMDEV_TS_PAUSE_C)) {
camdev_capture_start(ctxt);
}
// clear preview pause flags
if (!(old_thread_state & CAMDEV_TS_PAUSE_P)) {
cam->thread_state &= ~CAMDEV_TS_PAUSE_P;
}
}
void camdev_set_preview_window(void *ctxt, const sp<ANativeWindow> win)
{
CAMDEV *cam = (CAMDEV*)ctxt;
if (!cam) return;
cam->new_win = win;
cam->update_flag = 1;
}
void camdev_set_preview_target(void *ctxt, const sp<IGraphicBufferProducer>& gbp)
{
CAMDEV *cam = (CAMDEV*)ctxt;
if (!cam) return;
sp<ANativeWindow> win;
if (gbp != 0) {
// Using controlledByApp flag to ensure that the buffer queue remains in
// async mode for the old camera API, where many applications depend
// on that behavior.
win = new Surface(gbp, /*controlledByApp*/ true);
}
camdev_set_preview_window(cam, win);
}
void camdev_capture_start(void *ctxt)
{
CAMDEV *cam = (CAMDEV*)ctxt;
// check fd valid
if (!cam || cam->fd <= 0) return;
// turn on stream
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ioctl(cam->fd, VIDIOC_STREAMON, &type);
// resume thread
cam->thread_state &= ~CAMDEV_TS_PAUSE_C;
}
void camdev_capture_stop(void *ctxt)
{
CAMDEV *cam = (CAMDEV*)ctxt;
// check fd valid
if (!cam || cam->fd <= 0) return;
// pause thread
cam->thread_state |= CAMDEV_TS_PAUSE_C;
// turn off stream
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ioctl(cam->fd, VIDIOC_STREAMOFF, &type);
}
void camdev_preview_start(void *ctxt)
{
CAMDEV *cam = (CAMDEV*)ctxt;
if (!cam) return;
// set start prevew flag
cam->thread_state &=~CAMDEV_TS_PAUSE_P;
cam->thread_state |= CAMDEV_TS_PREVIEW;
}
void camdev_preview_stop(void *ctxt)
{
CAMDEV *cam = (CAMDEV*)ctxt;
if (!cam) return;
// set stop prevew flag
cam->thread_state |= CAMDEV_TS_PAUSE_P;
cam->thread_state &=~CAMDEV_TS_PREVIEW;
}
void camdev_set_watermark(void *ctxt, int x, int y, const char *wm)
{
CAMDEV *cam = (CAMDEV*)ctxt;
if (!cam) return;
cam->watermark_x = x;
cam->watermark_y = y;
strncpy(cam->watermark_str, wm, MAX_WATERMARK_SIZE - 1);
cam->watermark_str[MAX_WATERMARK_SIZE - 1] = '\0';
}
void camdev_set_callback(void *ctxt, void *callback, void *recorder)
{
CAMDEV *cam = (CAMDEV*)ctxt;
if (!cam) return;
cam->callback = (CAMDEV_CAPTURE_CALLBACK)callback;
cam->recorder = recorder;
}
void camdev_set_param(void *ctxt, int id, int value)
{
// todo...
}
int camdev_get_param(void *ctxt, int id)
{
CAMDEV *cam = (CAMDEV*)ctxt;
if (!cam) return 0;
switch (id) {
case CAMDEV_PARAM_VIDEO_WIDTH:
return cam->cam_w;
case CAMDEV_PARAM_VIDEO_HEIGHT:
return cam->cam_h;
case CAMDEV_PARAM_VIDEO_PIXFMT:
return cam->cam_pixfmt;
case CAMDEV_PARAM_VIDEO_FRATE_NUM:
return cam->cam_frate_num;
case CAMDEV_PARAM_VIDEO_FRATE_DEN:
return cam->cam_frate_den;
}
return 0;
}