blob: 88ef6114011719cc3cfed0d180aec29641ec94cf [file] [log] [blame]
Benny Prijonoc45d9512010-12-10 11:04:30 +00001/* $Id$ */
2/*
3 * Copyright (C) 2008-2010 Teluu Inc. (http://www.teluu.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjmedia-videodev/videodev_imp.h>
20#include <pjmedia/errno.h>
21#include <pj/assert.h>
22#include <pj/errno.h>
23#include <pj/file_access.h>
24#include <pj/log.h>
25#include <pj/os.h>
26#include <pj/rand.h>
27
28#if PJMEDIA_VIDEO_DEV_HAS_V4L2
29
30#include <linux/videodev2.h>
31#include <libv4l2.h>
32#include <fcntl.h>
33#include <errno.h>
34#include <sys/mman.h>
35
36#define THIS_FILE "v4l2_dev.c"
37#define DRIVER_NAME "v4l2"
38#define V4L2_MAX_DEVS 4
39#define DEFAULT_WIDTH 640
40#define DEFAULT_HEIGHT 480
41#define DEFAULT_FPS 25
42#define DEFAULT_CLOCK_RATE 90000
43#define INVALID_FD -1
44#define BUFFER_CNT 2
45#define MAX_IOCTL_RETRY 20
46
47
48/* mapping between pjmedia_fmt_id and v4l2 pixel format */
49typedef struct vid4lin_fmt_map
50{
51 pj_uint32_t pjmedia_fmt_id;
52 pj_uint32_t v4l2_fmt_id;
53} vid4lin_fmt_map;
54
55/* I/O type being used */
56enum vid4lin_io_type
57{
58 IO_TYPE_NONE,
59 IO_TYPE_READ,
60 IO_TYPE_MMAP,
61 IO_TYPE_MMAP_USER
62};
63
64/* descriptor for each mmap-ed buffer */
65typedef struct vid4lin_buffer
66{
67 void *start;
68 size_t length;
69} vid4lin_buffer;
70
71/* v4l2 device info */
72typedef struct vid4lin_dev_info
73{
74 pjmedia_vid_dev_info info;
75 char dev_name[32];
76 struct v4l2_capability v4l2_cap;
77} vid4lin_dev_info;
78
79/* v4l2 factory */
80typedef struct vid4lin_factory
81{
82 pjmedia_vid_dev_factory base;
83 pj_pool_t *pool;
84 pj_pool_factory *pf;
85
86 unsigned dev_count;
87 vid4lin_dev_info *dev_info;
88} vid4lin_factory;
89
90/* Video stream. */
91typedef struct vid4lin_stream
92{
93 pjmedia_vid_stream base; /**< Base stream */
94 pjmedia_vid_param param; /**< Settings */
95 pj_pool_t *pool; /**< Memory pool. */
96
97 int fd; /**< Video fd. */
98 char name[64]; /**< Name for log */
99 enum vid4lin_io_type io_type; /**< I/O method. */
100 unsigned buf_cnt; /**< MMap buf cnt. */
101 vid4lin_buffer *buffers; /**< MMap buffers. */
102 pj_time_val start_time; /**< Time when started */
103
104 pjmedia_vid_cb vid_cb; /**< Stream callback */
105 void *user_data; /**< Application data */
106} vid4lin_stream;
107
108/* Use this to convert between pjmedia_format_id and V4L2 fourcc */
109static vid4lin_fmt_map v4l2_fmt_maps[] =
110{
111 { PJMEDIA_FORMAT_RGB24, V4L2_PIX_FMT_BGR24 },
112 { PJMEDIA_FORMAT_RGBA, V4L2_PIX_FMT_BGR32 },
113 { PJMEDIA_FORMAT_RGB32, V4L2_PIX_FMT_BGR32 },
114 { PJMEDIA_FORMAT_AYUV, V4L2_PIX_FMT_YUV32 },
115 { PJMEDIA_FORMAT_YUY2, V4L2_PIX_FMT_YUYV },
116 { PJMEDIA_FORMAT_UYVY, V4L2_PIX_FMT_UYVY }
117};
118
119/* Prototypes */
120static pj_status_t vid4lin_factory_init(pjmedia_vid_dev_factory *f);
121static pj_status_t vid4lin_factory_destroy(pjmedia_vid_dev_factory *f);
122static unsigned vid4lin_factory_get_dev_count(pjmedia_vid_dev_factory *f);
123static pj_status_t vid4lin_factory_get_dev_info(pjmedia_vid_dev_factory *f,
124 unsigned index,
125 pjmedia_vid_dev_info *info);
126static pj_status_t vid4lin_factory_default_param(pj_pool_t *pool,
127 pjmedia_vid_dev_factory *f,
128 unsigned index,
129 pjmedia_vid_param *param);
130static pj_status_t vid4lin_factory_create_stream(pjmedia_vid_dev_factory *f,
131 const pjmedia_vid_param *prm,
132 const pjmedia_vid_cb *cb,
133 void *user_data,
134 pjmedia_vid_stream **p_strm);
135
136static pj_status_t vid4lin_stream_get_param(pjmedia_vid_stream *strm,
137 pjmedia_vid_param *param);
138static pj_status_t vid4lin_stream_get_cap(pjmedia_vid_stream *strm,
139 pjmedia_vid_dev_cap cap,
140 void *value);
141static pj_status_t vid4lin_stream_set_cap(pjmedia_vid_stream *strm,
142 pjmedia_vid_dev_cap cap,
143 const void *value);
144static pj_status_t vid4lin_stream_get_frame(pjmedia_vid_stream *strm,
145 pjmedia_frame *frame);
146static pj_status_t vid4lin_stream_start(pjmedia_vid_stream *strm);
147static pj_status_t vid4lin_stream_stop(pjmedia_vid_stream *strm);
148static pj_status_t vid4lin_stream_destroy(pjmedia_vid_stream *strm);
149
150/* Operations */
151static pjmedia_vid_dev_factory_op factory_op =
152{
153 &vid4lin_factory_init,
154 &vid4lin_factory_destroy,
155 &vid4lin_factory_get_dev_count,
156 &vid4lin_factory_get_dev_info,
157 &vid4lin_factory_default_param,
158 &vid4lin_factory_create_stream
159};
160
161static pjmedia_vid_stream_op stream_op =
162{
163 &vid4lin_stream_get_param,
164 &vid4lin_stream_get_cap,
165 &vid4lin_stream_set_cap,
166 &vid4lin_stream_start,
167 &vid4lin_stream_get_frame,
168 NULL,
169 &vid4lin_stream_stop,
170 &vid4lin_stream_destroy
171};
172
173
174/****************************************************************************
175 * Factory operations
176 */
177/*
178 * Factory creation function.
179 */
180pjmedia_vid_dev_factory* pjmedia_v4l2_factory(pj_pool_factory *pf)
181{
182 vid4lin_factory *f;
183 pj_pool_t *pool;
184
185 pool = pj_pool_create(pf, DRIVER_NAME, 512, 512, NULL);
186 f = PJ_POOL_ZALLOC_T(pool, vid4lin_factory);
187 f->pf = pf;
188 f->pool = pool;
189 f->base.op = &factory_op;
190
191 return &f->base;
192}
193
194/* util: ioctl that tries harder. */
195static pj_status_t xioctl(int fh, int request, void *arg)
196{
197 enum { RETRY = MAX_IOCTL_RETRY };
198 int r, c=0;
199
200 do {
201 r = v4l2_ioctl(fh, request, arg);
202 } while (r==-1 && c++<RETRY && ((errno==EINTR) || (errno==EAGAIN)));
203
204 return (r == -1) ? pj_get_os_error() : PJ_SUCCESS;
205}
206
207/* Scan V4L2 devices */
208static pj_status_t v4l2_scan_devs(vid4lin_factory *f)
209{
210 vid4lin_dev_info vdi[V4L2_MAX_DEVS];
211 char dev_name[32];
212 unsigned i, old_count;
213 pj_status_t status;
214
215 pj_bzero(vdi, sizeof(vdi));
216 old_count = f->dev_count;
217 f->dev_count = 0;
218
219 for (i=0; i<V4L2_MAX_DEVS && f->dev_count < V4L2_MAX_DEVS; ++i) {
220 int fd;
221 vid4lin_dev_info *pdi;
222 pj_pool_t *pool = f->pool;
223 pj_uint32_t fmt_cap[8];
224 int j, fmt_cnt=0;
225
226 pdi = &vdi[f->dev_count];
227
228 snprintf(dev_name, sizeof(dev_name), "/dev/video%d", i);
229 if (!pj_file_exists(dev_name))
230 continue;
231
232 fd = v4l2_open(dev_name, O_RDWR, 0);
233 if (fd == -1)
234 continue;
235
236 status = xioctl(fd, VIDIOC_QUERYCAP, &pdi->v4l2_cap);
237 if (status != PJ_SUCCESS) {
238 PJ_PERROR(4,(THIS_FILE, status, "Error querying %s", dev_name));
239 v4l2_close(fd);
240 continue;
241 }
242
243 if ((pdi->v4l2_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
244 v4l2_close(fd);
245 continue;
246 }
247
248 PJ_LOG(5,(THIS_FILE, "Found capture device %s", pdi->v4l2_cap.card));
249 PJ_LOG(5,(THIS_FILE, " Enumerating formats:"));
250 for (j=0; fmt_cnt<PJ_ARRAY_SIZE(fmt_cap); ++j) {
251 struct v4l2_fmtdesc fdesc;
252 unsigned k;
253
254 pj_bzero(&fdesc, sizeof(fdesc));
255 fdesc.index = j;
256 fdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
257
258 status = xioctl(fd, VIDIOC_ENUM_FMT, &fdesc);
259 if (status != PJ_SUCCESS)
260 break;
261
262 for (k=0; k<PJ_ARRAY_SIZE(v4l2_fmt_maps); ++k) {
263 if (v4l2_fmt_maps[k].v4l2_fmt_id == fdesc.pixelformat) {
264 fmt_cap[fmt_cnt++] = v4l2_fmt_maps[k].pjmedia_fmt_id;
265 PJ_LOG(5,(THIS_FILE, " Supported: %s",
266 fdesc.description));
267 break;
268 }
269 }
270 if (k==PJ_ARRAY_SIZE(v4l2_fmt_maps)) {
271 PJ_LOG(5,(THIS_FILE, " Unsupported: %s", fdesc.description));
272 }
273 }
274
275 v4l2_close(fd);
276
277 if (fmt_cnt==0) {
278 PJ_LOG(5,(THIS_FILE, " Found no common format"));
279 continue;
280 }
281
282 strncpy(pdi->dev_name, dev_name, sizeof(pdi->dev_name));
283 pdi->dev_name[sizeof(pdi->dev_name)-1] = '\0';
284 strncpy(pdi->info.name, (char*)pdi->v4l2_cap.card,
285 sizeof(pdi->info.name));
286 pdi->info.name[sizeof(pdi->info.name)-1] = '\0';
287 strncpy(pdi->info.driver, DRIVER_NAME, sizeof(pdi->info.driver));
288 pdi->info.driver[sizeof(pdi->info.driver)-1] = '\0';
289 pdi->info.dir = PJMEDIA_DIR_CAPTURE;
290 pdi->info.has_callback = PJ_FALSE;
291 pdi->info.caps = PJMEDIA_VID_DEV_CAP_FORMAT;
292
293 pdi->info.fmt_cnt = fmt_cnt;
294 pdi->info.fmt = (pjmedia_format*)
295 pj_pool_calloc(pool, sizeof(pjmedia_format), fmt_cnt);
296
297 for (j=0; j<fmt_cnt; ++j) {
298 pjmedia_format_init_video(&pdi->info.fmt[j],
299 fmt_cap[j],
300 DEFAULT_WIDTH,
301 DEFAULT_HEIGHT,
302 DEFAULT_FPS, 1);
303 }
304 if (j < fmt_cnt)
305 continue;
306
307 f->dev_count++;
308 }
309
310 if (f->dev_count == 0)
311 return PJ_SUCCESS;
312
313 if (f->dev_count > old_count || f->dev_info == NULL) {
314 f->dev_info = (vid4lin_dev_info*)
315 pj_pool_calloc(f->pool,
316 f->dev_count,
317 sizeof(vid4lin_dev_info));
318 }
319 pj_memcpy(f->dev_info, vdi, f->dev_count * sizeof(vid4lin_dev_info));
320
321 return PJ_SUCCESS;
322}
323
324
325/* API: init factory */
326static pj_status_t vid4lin_factory_init(pjmedia_vid_dev_factory *f)
327{
328 vid4lin_factory *cf = (vid4lin_factory*)f;
329 pj_status_t status;
330
331 status = v4l2_scan_devs(cf);
332 if (status != PJ_SUCCESS)
333 return status;
334
335 PJ_LOG(4, (THIS_FILE, "Video4Linux2 initialized with %d devices",
336 cf->dev_count));
337
338 return PJ_SUCCESS;
339}
340
341/* API: destroy factory */
342static pj_status_t vid4lin_factory_destroy(pjmedia_vid_dev_factory *f)
343{
344 vid4lin_factory *cf = (vid4lin_factory*)f;
345 pj_pool_t *pool = cf->pool;
346
347 if (cf->pool) {
348 cf->pool = NULL;
349 pj_pool_release(pool);
350 }
351
352 return PJ_SUCCESS;
353}
354
355/* API: get number of devices */
356static unsigned vid4lin_factory_get_dev_count(pjmedia_vid_dev_factory *f)
357{
358 vid4lin_factory *cf = (vid4lin_factory*)f;
359 return cf->dev_count;
360}
361
362/* API: get device info */
363static pj_status_t vid4lin_factory_get_dev_info(pjmedia_vid_dev_factory *f,
364 unsigned index,
365 pjmedia_vid_dev_info *info)
366{
367 vid4lin_factory *cf = (vid4lin_factory*)f;
368
369 PJ_ASSERT_RETURN(index < cf->dev_count, PJMEDIA_EVID_INVDEV);
370
371 pj_memcpy(info, &cf->dev_info[index].info, sizeof(*info));
372
373 return PJ_SUCCESS;
374}
375
376/* API: create default device parameter */
377static pj_status_t vid4lin_factory_default_param(pj_pool_t *pool,
378 pjmedia_vid_dev_factory *f,
379 unsigned index,
380 pjmedia_vid_param *param)
381{
382 vid4lin_factory *cf = (vid4lin_factory*)f;
383
384 PJ_ASSERT_RETURN(index < cf->dev_count, PJMEDIA_EVID_INVDEV);
385
386 pj_bzero(param, sizeof(*param));
387 param->dir = PJMEDIA_DIR_CAPTURE;
388 param->cap_id = index;
389 param->rend_id = PJMEDIA_VID_INVALID_DEV;
390 param->flags = PJMEDIA_VID_DEV_CAP_FORMAT;
391 param->clock_rate = DEFAULT_CLOCK_RATE;
392 param->frame_rate.num = DEFAULT_FPS;
393 param->frame_rate.denum = 1;
394 pjmedia_format_copy(&param->fmt, &cf->dev_info[index].info.fmt[0]);
395
396 return PJ_SUCCESS;
397}
398
399static vid4lin_fmt_map* get_v4l2_format_info(pjmedia_format_id id)
400{
401 unsigned i;
402
403 for (i = 0; i < PJ_ARRAY_SIZE(v4l2_fmt_maps); i++) {
404 if (v4l2_fmt_maps[i].pjmedia_fmt_id == id)
405 return &v4l2_fmt_maps[i];
406 }
407
408 return NULL;
409}
410
411/* util: setup format */
412static pj_status_t vid4lin_stream_init_fmt(vid4lin_stream *stream,
413 const pjmedia_vid_param *param,
414 pj_uint32_t pix_fmt)
415{
416 const pjmedia_video_format_detail *vfd;
417 struct v4l2_format v4l2_fmt;
418 pj_status_t status;
419
420 vfd = pjmedia_format_get_video_format_detail(&param->fmt, PJ_TRUE);
421 if (vfd == NULL)
422 return PJMEDIA_EVID_BADFORMAT;
423
424 pj_bzero(&v4l2_fmt, sizeof(v4l2_fmt));
425 v4l2_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
426 v4l2_fmt.fmt.pix.width = vfd->size.w;
427 v4l2_fmt.fmt.pix.height = vfd->size.h;
428 v4l2_fmt.fmt.pix.pixelformat = pix_fmt;
429 v4l2_fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
430 status = xioctl(stream->fd, VIDIOC_S_FMT, &v4l2_fmt);
431 if (status != PJ_SUCCESS)
432 return status;
433
434 if (v4l2_fmt.fmt.pix.pixelformat != pix_fmt) {
435 status = PJMEDIA_EVID_BADFORMAT;
436 return status;
437 }
438
439 if ((v4l2_fmt.fmt.pix.width != vfd->size.w) ||
440 (v4l2_fmt.fmt.pix.height != vfd->size.h))
441 {
442 status = PJMEDIA_EVID_BADSIZE;
443 return status;
444 }
445
446 return PJ_SUCCESS;
447}
448
449/* Util: initiate v4l2 streaming via mmap */
450static pj_status_t vid4lin_stream_init_streaming(vid4lin_stream *stream)
451{
452 struct v4l2_requestbuffers req;
453 unsigned i;
454 pj_status_t status;
455
456 pj_bzero(&req, sizeof(req));
457 req.count = BUFFER_CNT;
458 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
459 req.memory = V4L2_MEMORY_MMAP;
460 status = xioctl(stream->fd, VIDIOC_REQBUFS, &req);
461 if (status != PJ_SUCCESS)
462 return status;
463
464 stream->buffers = pj_pool_calloc(stream->pool, req.count,
465 sizeof(*stream->buffers));
466 stream->buf_cnt = 0;
467
468 for (i = 0; i < req.count; ++i) {
469 struct v4l2_buffer buf;
470
471 pj_bzero(&buf, sizeof(buf));
472
473 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
474 buf.memory = V4L2_MEMORY_MMAP;
475 buf.index = i;
476
477 status = xioctl(stream->fd, VIDIOC_QUERYBUF, &buf);
478 if (status != PJ_SUCCESS)
479 goto on_error;
480
481 stream->buffers[i].length = buf.length;
482 stream->buffers[i].start = v4l2_mmap(NULL, buf.length,
483 PROT_READ | PROT_WRITE,
484 MAP_SHARED, stream->fd,
485 buf.m.offset);
486
487 if (MAP_FAILED == stream->buffers[i].start) {
488 status = pj_get_os_error();
489 goto on_error;
490 }
491
492 stream->buf_cnt++;
493 }
494
495 PJ_LOG(5,(THIS_FILE, " mmap streaming initialized"));
496
497 stream->io_type = IO_TYPE_MMAP;
498 return PJ_SUCCESS;
499
500on_error:
501 return status;
502}
503
504/* init streaming with user pointer */
505static pj_status_t vid4lin_stream_init_streaming_user(vid4lin_stream *stream)
506{
507 return PJ_ENOTSUP;
508}
509
510/* init streaming with read() */
511static pj_status_t vid4lin_stream_init_read_write(vid4lin_stream *stream)
512{
513 return PJ_ENOTSUP;
514}
515
516/* API: create stream */
517static pj_status_t vid4lin_factory_create_stream(pjmedia_vid_dev_factory *f,
518 const pjmedia_vid_param *param,
519 const pjmedia_vid_cb *cb,
520 void *user_data,
521 pjmedia_vid_stream **p_vid_strm)
522{
523 vid4lin_factory *cf = (vid4lin_factory*)f;
524 pj_pool_t *pool;
525 vid4lin_stream *stream;
526 vid4lin_dev_info *vdi;
527 const vid4lin_fmt_map *fmt_map;
528 const pjmedia_video_format_info *fmt_info;
529 pj_status_t status = PJ_SUCCESS;
530
531 PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL);
532 PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO &&
533 param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO,
534 PJ_EINVAL);
535 PJ_ASSERT_RETURN(param->cap_id >= 0 && param->cap_id < cf->dev_count,
536 PJMEDIA_EVID_INVDEV);
537
538 fmt_info = pjmedia_get_video_format_info(NULL, param->fmt.id);
539 if (!fmt_info || (fmt_map=get_v4l2_format_info(param->fmt.id))==NULL)
540 return PJMEDIA_EVID_BADFORMAT;
541
542 vdi = &cf->dev_info[param->cap_id];
543
544 /* Create and Initialize stream descriptor */
545 pool = pj_pool_create(cf->pf, vdi->info.name, 512, 512, NULL);
546 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
547
548 stream = PJ_POOL_ZALLOC_T(pool, vid4lin_stream);
549 pj_memcpy(&stream->param, param, sizeof(*param));
550 stream->pool = pool;
551 pj_memcpy(&stream->vid_cb, cb, sizeof(*cb));
552 strncpy(stream->name, vdi->info.name, sizeof(stream->name));
553 stream->name[sizeof(stream->name)-1] = '\0';
554 stream->user_data = user_data;
555 stream->fd = INVALID_FD;
556
557 PJ_LOG(4,(THIS_FILE, "Opening video4linux2 device %s: format=%s..",
558 stream->name, fmt_info->name));
559
560 stream->fd = v4l2_open(vdi->dev_name, O_RDWR | O_NONBLOCK, 0);
561 if (stream->fd < 0)
562 goto on_error;
563
564 status = vid4lin_stream_init_fmt(stream, param, fmt_map->v4l2_fmt_id);
565 if (status != PJ_SUCCESS)
566 goto on_error;
567
568 if (vdi->v4l2_cap.capabilities & V4L2_CAP_STREAMING)
569 status = vid4lin_stream_init_streaming(stream);
570
571 if (status!=PJ_SUCCESS && vdi->v4l2_cap.capabilities & V4L2_CAP_STREAMING)
572 status = vid4lin_stream_init_streaming_user(stream);
573
574 if (status!=PJ_SUCCESS && vdi->v4l2_cap.capabilities & V4L2_CAP_READWRITE)
575 status = vid4lin_stream_init_read_write(stream);
576
577 if (status != PJ_SUCCESS) {
578 PJ_LOG(1,(THIS_FILE, "Error: unable to initiate I/O on %s",
579 stream->name));
580 goto on_error;
581 }
582
583 /* Done */
584 stream->base.op = &stream_op;
585 *p_vid_strm = &stream->base;
586
587 return PJ_SUCCESS;
588
589on_error:
590 if (status == PJ_SUCCESS)
591 status = PJ_RETURN_OS_ERROR(errno);
592
593 vid4lin_stream_destroy(&stream->base);
594 return status;
595}
596
597/* API: Get stream info. */
598static pj_status_t vid4lin_stream_get_param(pjmedia_vid_stream *s,
599 pjmedia_vid_param *pi)
600{
601 vid4lin_stream *strm = (vid4lin_stream*)s;
602
603 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
604
605 pj_memcpy(pi, &strm->param, sizeof(*pi));
606
607 return PJ_SUCCESS;
608}
609
610/* API: get capability */
611static pj_status_t vid4lin_stream_get_cap(pjmedia_vid_stream *s,
612 pjmedia_vid_dev_cap cap,
613 void *pval)
614{
615 vid4lin_stream *strm = (vid4lin_stream*)s;
616
617 PJ_UNUSED_ARG(strm);
618
619 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
620
621 if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
622 {
623 return PJMEDIA_EVID_INVCAP;
624// return PJ_SUCCESS;
625 } else {
626 return PJMEDIA_EVID_INVCAP;
627 }
628}
629
630/* API: set capability */
631static pj_status_t vid4lin_stream_set_cap(pjmedia_vid_stream *s,
632 pjmedia_vid_dev_cap cap,
633 const void *pval)
634{
635 vid4lin_stream *strm = (vid4lin_stream*)s;
636
637
638 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
639
640 /*
641 if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
642 {
643 return PJ_SUCCESS;
644 }
645 */
646 PJ_UNUSED_ARG(strm);
647 PJ_UNUSED_ARG(cap);
648 PJ_UNUSED_ARG(pval);
649
650 return PJMEDIA_EVID_INVCAP;
651}
652
653/* get frame from mmap */
654static pj_status_t vid4lin_stream_get_frame_mmap(vid4lin_stream *stream,
655 pjmedia_frame *frame)
656{
657 struct v4l2_buffer buf;
658 pj_time_val time;
659 pj_status_t status = PJ_SUCCESS;
660
661 pj_bzero(&buf, sizeof(buf));
662 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
663 buf.memory = V4L2_MEMORY_MMAP;
664 status = xioctl(stream->fd, VIDIOC_DQBUF, &buf);
665 if (status != PJ_SUCCESS)
666 return status;
667
668 if (frame->size < buf.bytesused) {
669 /* supplied buffer is too small */
670 pj_assert(!"frame buffer is too small for v4l2");
671 status = PJ_ETOOSMALL;
672 goto on_return;
673 }
674
675 time.sec = buf.timestamp.tv_sec;
676 time.msec = buf.timestamp.tv_usec / 1000;
677 PJ_TIME_VAL_SUB(time, stream->start_time);
678
679 frame->type = PJMEDIA_FRAME_TYPE_VIDEO;
680 frame->size = buf.bytesused;
681 frame->timestamp.u64 = PJ_TIME_VAL_MSEC(time) * stream->param.clock_rate
682 / PJ_UINT64(1000);
683 pj_memcpy(frame->buf, stream->buffers[buf.index].start, buf.bytesused);
684
685on_return:
686 pj_bzero(&buf, sizeof(buf));
687 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
688 buf.memory = V4L2_MEMORY_MMAP;
689 xioctl(stream->fd, VIDIOC_QBUF, &buf);
690
691 return status;
692}
693
694/* API: Get frame from stream */
695static pj_status_t vid4lin_stream_get_frame(pjmedia_vid_stream *strm,
696 pjmedia_frame *frame)
697{
698 vid4lin_stream *stream = (vid4lin_stream*)strm;
699
700 if (stream->io_type == IO_TYPE_MMAP)
701 return vid4lin_stream_get_frame_mmap(stream, frame);
702 else {
703 pj_assert(!"Unsupported i/o type");
704 return PJ_EINVALIDOP;
705 }
706}
707
708/* API: Start stream. */
709static pj_status_t vid4lin_stream_start(pjmedia_vid_stream *strm)
710{
711 vid4lin_stream *stream = (vid4lin_stream*)strm;
712 struct v4l2_buffer buf;
713 enum v4l2_buf_type type;
714 unsigned i;
715 pj_status_t status;
716
717 PJ_ASSERT_RETURN(stream->fd != -1, PJ_EINVALIDOP);
718
719 PJ_LOG(4, (THIS_FILE, "Starting v4l2 video stream %s", stream->name));
720
721 pj_gettimeofday(&stream->start_time);
722
723 for (i = 0; i < stream->buf_cnt; ++i) {
724 pj_bzero(&buf, sizeof(buf));
725 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
726 buf.memory = V4L2_MEMORY_MMAP;
727 buf.index = i;
728 status = xioctl(stream->fd, VIDIOC_QBUF, &buf);
729 if (status != PJ_SUCCESS)
730 goto on_error;
731 }
732 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
733
734 status = xioctl(stream->fd, VIDIOC_STREAMON, &type);
735 if (status != PJ_SUCCESS)
736 goto on_error;
737
738 return PJ_SUCCESS;
739
740on_error:
741 if (i > 0) {
742 /* Dequeue already enqueued buffers. Can we do this while streaming
743 * is not started?
744 */
745 unsigned n = i;
746 for (i=0; i<n; ++i) {
747 pj_bzero(&buf, sizeof(buf));
748 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
749 buf.memory = V4L2_MEMORY_MMAP;
750 xioctl(stream->fd, VIDIOC_DQBUF, &buf);
751 }
752 }
753 return status;
754}
755
756/* API: Stop stream. */
757static pj_status_t vid4lin_stream_stop(pjmedia_vid_stream *strm)
758{
759 vid4lin_stream *stream = (vid4lin_stream*)strm;
760 enum v4l2_buf_type type;
761 pj_status_t status;
762
763 if (stream->fd < 0)
764 return PJ_SUCCESS;
765
766 PJ_LOG(4, (THIS_FILE, "Stopping v4l2 video stream %s", stream->name));
767
768 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
769 status = xioctl(stream->fd, VIDIOC_STREAMOFF, &type);
770 if (status != PJ_SUCCESS)
771 return status;
772
773 return PJ_SUCCESS;
774}
775
776
777/* API: Destroy stream. */
778static pj_status_t vid4lin_stream_destroy(pjmedia_vid_stream *strm)
779{
780 vid4lin_stream *stream = (vid4lin_stream*)strm;
781 unsigned i;
782
783 PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);
784
785 vid4lin_stream_stop(strm);
786
787 PJ_LOG(4, (THIS_FILE, "Destroying v4l2 video stream %s", stream->name));
788
789 for (i=0; i<stream->buf_cnt; ++i) {
790 if (stream->buffers[i].start != MAP_FAILED) {
791 v4l2_munmap(stream->buffers[i].start, stream->buffers[i].length);
792 stream->buffers[i].start = MAP_FAILED;
793 }
794 }
795
796 if (stream->fd >= 0) {
797 v4l2_close(stream->fd);
798 stream->fd = -1;
799 }
800 pj_pool_release(stream->pool);
801
802 return PJ_SUCCESS;
803}
804
805#endif /* PJMEDIA_VIDEO_DEV_HAS_V4L2 */