blob: e1260486823208a68a0cdaa526e9708bfd5a988b [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{
Benny Prijono6c7e95e2011-03-15 11:22:04 +000093 pjmedia_vid_dev_stream base; /**< Base stream */
Benny Prijonoc45d9512010-12-10 11:04:30 +000094 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,
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000134 pjmedia_vid_dev_stream **p);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000135
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000136static pj_status_t vid4lin_stream_get_param(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000137 pjmedia_vid_param *param);
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000138static pj_status_t vid4lin_stream_get_cap(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000139 pjmedia_vid_dev_cap cap,
140 void *value);
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000141static pj_status_t vid4lin_stream_set_cap(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000142 pjmedia_vid_dev_cap cap,
143 const void *value);
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000144static pj_status_t vid4lin_stream_get_frame(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000145 pjmedia_frame *frame);
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000146static pj_status_t vid4lin_stream_start(pjmedia_vid_dev_stream *strm);
147static pj_status_t vid4lin_stream_stop(pjmedia_vid_dev_stream *strm);
148static pj_status_t vid4lin_stream_destroy(pjmedia_vid_dev_stream *strm);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000149
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
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000161static pjmedia_vid_dev_stream_op stream_op =
Benny Prijonoc45d9512010-12-10 11:04:30 +0000162{
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;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000392 pjmedia_format_copy(&param->fmt, &cf->dev_info[index].info.fmt[0]);
393
394 return PJ_SUCCESS;
395}
396
397static vid4lin_fmt_map* get_v4l2_format_info(pjmedia_format_id id)
398{
399 unsigned i;
400
401 for (i = 0; i < PJ_ARRAY_SIZE(v4l2_fmt_maps); i++) {
402 if (v4l2_fmt_maps[i].pjmedia_fmt_id == id)
403 return &v4l2_fmt_maps[i];
404 }
405
406 return NULL;
407}
408
409/* util: setup format */
410static pj_status_t vid4lin_stream_init_fmt(vid4lin_stream *stream,
411 const pjmedia_vid_param *param,
412 pj_uint32_t pix_fmt)
413{
414 const pjmedia_video_format_detail *vfd;
415 struct v4l2_format v4l2_fmt;
416 pj_status_t status;
417
418 vfd = pjmedia_format_get_video_format_detail(&param->fmt, PJ_TRUE);
419 if (vfd == NULL)
420 return PJMEDIA_EVID_BADFORMAT;
421
422 pj_bzero(&v4l2_fmt, sizeof(v4l2_fmt));
423 v4l2_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
424 v4l2_fmt.fmt.pix.width = vfd->size.w;
425 v4l2_fmt.fmt.pix.height = vfd->size.h;
426 v4l2_fmt.fmt.pix.pixelformat = pix_fmt;
427 v4l2_fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
428 status = xioctl(stream->fd, VIDIOC_S_FMT, &v4l2_fmt);
429 if (status != PJ_SUCCESS)
430 return status;
431
432 if (v4l2_fmt.fmt.pix.pixelformat != pix_fmt) {
433 status = PJMEDIA_EVID_BADFORMAT;
434 return status;
435 }
436
437 if ((v4l2_fmt.fmt.pix.width != vfd->size.w) ||
438 (v4l2_fmt.fmt.pix.height != vfd->size.h))
439 {
440 status = PJMEDIA_EVID_BADSIZE;
441 return status;
442 }
443
444 return PJ_SUCCESS;
445}
446
447/* Util: initiate v4l2 streaming via mmap */
448static pj_status_t vid4lin_stream_init_streaming(vid4lin_stream *stream)
449{
450 struct v4l2_requestbuffers req;
451 unsigned i;
452 pj_status_t status;
453
454 pj_bzero(&req, sizeof(req));
455 req.count = BUFFER_CNT;
456 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
457 req.memory = V4L2_MEMORY_MMAP;
458 status = xioctl(stream->fd, VIDIOC_REQBUFS, &req);
459 if (status != PJ_SUCCESS)
460 return status;
461
462 stream->buffers = pj_pool_calloc(stream->pool, req.count,
463 sizeof(*stream->buffers));
464 stream->buf_cnt = 0;
465
466 for (i = 0; i < req.count; ++i) {
467 struct v4l2_buffer buf;
468
469 pj_bzero(&buf, sizeof(buf));
470
471 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
472 buf.memory = V4L2_MEMORY_MMAP;
473 buf.index = i;
474
475 status = xioctl(stream->fd, VIDIOC_QUERYBUF, &buf);
476 if (status != PJ_SUCCESS)
477 goto on_error;
478
479 stream->buffers[i].length = buf.length;
480 stream->buffers[i].start = v4l2_mmap(NULL, buf.length,
481 PROT_READ | PROT_WRITE,
482 MAP_SHARED, stream->fd,
483 buf.m.offset);
484
485 if (MAP_FAILED == stream->buffers[i].start) {
486 status = pj_get_os_error();
487 goto on_error;
488 }
489
490 stream->buf_cnt++;
491 }
492
493 PJ_LOG(5,(THIS_FILE, " mmap streaming initialized"));
494
495 stream->io_type = IO_TYPE_MMAP;
496 return PJ_SUCCESS;
497
498on_error:
499 return status;
500}
501
502/* init streaming with user pointer */
503static pj_status_t vid4lin_stream_init_streaming_user(vid4lin_stream *stream)
504{
505 return PJ_ENOTSUP;
506}
507
508/* init streaming with read() */
509static pj_status_t vid4lin_stream_init_read_write(vid4lin_stream *stream)
510{
511 return PJ_ENOTSUP;
512}
513
514/* API: create stream */
515static pj_status_t vid4lin_factory_create_stream(pjmedia_vid_dev_factory *f,
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000516 const pjmedia_vid_param *param,
517 const pjmedia_vid_cb *cb,
518 void *user_data,
519 pjmedia_vid_dev_stream **p_vid_strm)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000520{
521 vid4lin_factory *cf = (vid4lin_factory*)f;
522 pj_pool_t *pool;
523 vid4lin_stream *stream;
524 vid4lin_dev_info *vdi;
525 const vid4lin_fmt_map *fmt_map;
526 const pjmedia_video_format_info *fmt_info;
527 pj_status_t status = PJ_SUCCESS;
528
529 PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL);
530 PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO &&
531 param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO,
532 PJ_EINVAL);
533 PJ_ASSERT_RETURN(param->cap_id >= 0 && param->cap_id < cf->dev_count,
534 PJMEDIA_EVID_INVDEV);
535
536 fmt_info = pjmedia_get_video_format_info(NULL, param->fmt.id);
537 if (!fmt_info || (fmt_map=get_v4l2_format_info(param->fmt.id))==NULL)
538 return PJMEDIA_EVID_BADFORMAT;
539
540 vdi = &cf->dev_info[param->cap_id];
541
542 /* Create and Initialize stream descriptor */
543 pool = pj_pool_create(cf->pf, vdi->info.name, 512, 512, NULL);
544 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
545
546 stream = PJ_POOL_ZALLOC_T(pool, vid4lin_stream);
547 pj_memcpy(&stream->param, param, sizeof(*param));
548 stream->pool = pool;
549 pj_memcpy(&stream->vid_cb, cb, sizeof(*cb));
550 strncpy(stream->name, vdi->info.name, sizeof(stream->name));
551 stream->name[sizeof(stream->name)-1] = '\0';
552 stream->user_data = user_data;
553 stream->fd = INVALID_FD;
554
555 PJ_LOG(4,(THIS_FILE, "Opening video4linux2 device %s: format=%s..",
556 stream->name, fmt_info->name));
557
558 stream->fd = v4l2_open(vdi->dev_name, O_RDWR | O_NONBLOCK, 0);
559 if (stream->fd < 0)
560 goto on_error;
561
562 status = vid4lin_stream_init_fmt(stream, param, fmt_map->v4l2_fmt_id);
563 if (status != PJ_SUCCESS)
564 goto on_error;
565
566 if (vdi->v4l2_cap.capabilities & V4L2_CAP_STREAMING)
567 status = vid4lin_stream_init_streaming(stream);
568
569 if (status!=PJ_SUCCESS && vdi->v4l2_cap.capabilities & V4L2_CAP_STREAMING)
570 status = vid4lin_stream_init_streaming_user(stream);
571
572 if (status!=PJ_SUCCESS && vdi->v4l2_cap.capabilities & V4L2_CAP_READWRITE)
573 status = vid4lin_stream_init_read_write(stream);
574
575 if (status != PJ_SUCCESS) {
576 PJ_LOG(1,(THIS_FILE, "Error: unable to initiate I/O on %s",
577 stream->name));
578 goto on_error;
579 }
580
581 /* Done */
582 stream->base.op = &stream_op;
583 *p_vid_strm = &stream->base;
584
585 return PJ_SUCCESS;
586
587on_error:
588 if (status == PJ_SUCCESS)
589 status = PJ_RETURN_OS_ERROR(errno);
590
591 vid4lin_stream_destroy(&stream->base);
592 return status;
593}
594
595/* API: Get stream info. */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000596static pj_status_t vid4lin_stream_get_param(pjmedia_vid_dev_stream *s,
597 pjmedia_vid_param *pi)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000598{
599 vid4lin_stream *strm = (vid4lin_stream*)s;
600
601 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
602
603 pj_memcpy(pi, &strm->param, sizeof(*pi));
604
605 return PJ_SUCCESS;
606}
607
608/* API: get capability */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000609static pj_status_t vid4lin_stream_get_cap(pjmedia_vid_dev_stream *s,
610 pjmedia_vid_dev_cap cap,
611 void *pval)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000612{
613 vid4lin_stream *strm = (vid4lin_stream*)s;
614
615 PJ_UNUSED_ARG(strm);
616
617 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
618
619 if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
620 {
621 return PJMEDIA_EVID_INVCAP;
622// return PJ_SUCCESS;
623 } else {
624 return PJMEDIA_EVID_INVCAP;
625 }
626}
627
628/* API: set capability */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000629static pj_status_t vid4lin_stream_set_cap(pjmedia_vid_dev_stream *s,
630 pjmedia_vid_dev_cap cap,
631 const void *pval)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000632{
633 vid4lin_stream *strm = (vid4lin_stream*)s;
634
635
636 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
637
638 /*
639 if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
640 {
641 return PJ_SUCCESS;
642 }
643 */
644 PJ_UNUSED_ARG(strm);
645 PJ_UNUSED_ARG(cap);
646 PJ_UNUSED_ARG(pval);
647
648 return PJMEDIA_EVID_INVCAP;
649}
650
651/* get frame from mmap */
652static pj_status_t vid4lin_stream_get_frame_mmap(vid4lin_stream *stream,
653 pjmedia_frame *frame)
654{
655 struct v4l2_buffer buf;
656 pj_time_val time;
657 pj_status_t status = PJ_SUCCESS;
658
659 pj_bzero(&buf, sizeof(buf));
660 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
661 buf.memory = V4L2_MEMORY_MMAP;
662 status = xioctl(stream->fd, VIDIOC_DQBUF, &buf);
663 if (status != PJ_SUCCESS)
664 return status;
665
666 if (frame->size < buf.bytesused) {
667 /* supplied buffer is too small */
668 pj_assert(!"frame buffer is too small for v4l2");
669 status = PJ_ETOOSMALL;
670 goto on_return;
671 }
672
673 time.sec = buf.timestamp.tv_sec;
674 time.msec = buf.timestamp.tv_usec / 1000;
675 PJ_TIME_VAL_SUB(time, stream->start_time);
676
677 frame->type = PJMEDIA_FRAME_TYPE_VIDEO;
678 frame->size = buf.bytesused;
Benny Prijono349037b2011-03-17 11:25:19 +0000679 frame->timestamp.u64 = PJ_UINT64(1) * PJ_TIME_VAL_MSEC(time) *
680 stream->param.clock_rate / PJ_UINT64(1000);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000681 pj_memcpy(frame->buf, stream->buffers[buf.index].start, buf.bytesused);
682
683on_return:
684 pj_bzero(&buf, sizeof(buf));
685 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
686 buf.memory = V4L2_MEMORY_MMAP;
687 xioctl(stream->fd, VIDIOC_QBUF, &buf);
688
689 return status;
690}
691
692/* API: Get frame from stream */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000693static pj_status_t vid4lin_stream_get_frame(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000694 pjmedia_frame *frame)
695{
696 vid4lin_stream *stream = (vid4lin_stream*)strm;
697
698 if (stream->io_type == IO_TYPE_MMAP)
699 return vid4lin_stream_get_frame_mmap(stream, frame);
700 else {
701 pj_assert(!"Unsupported i/o type");
702 return PJ_EINVALIDOP;
703 }
704}
705
706/* API: Start stream. */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000707static pj_status_t vid4lin_stream_start(pjmedia_vid_dev_stream *strm)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000708{
709 vid4lin_stream *stream = (vid4lin_stream*)strm;
710 struct v4l2_buffer buf;
711 enum v4l2_buf_type type;
712 unsigned i;
713 pj_status_t status;
714
715 PJ_ASSERT_RETURN(stream->fd != -1, PJ_EINVALIDOP);
716
717 PJ_LOG(4, (THIS_FILE, "Starting v4l2 video stream %s", stream->name));
718
719 pj_gettimeofday(&stream->start_time);
720
721 for (i = 0; i < stream->buf_cnt; ++i) {
722 pj_bzero(&buf, sizeof(buf));
723 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
724 buf.memory = V4L2_MEMORY_MMAP;
725 buf.index = i;
726 status = xioctl(stream->fd, VIDIOC_QBUF, &buf);
727 if (status != PJ_SUCCESS)
728 goto on_error;
729 }
730 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
731
732 status = xioctl(stream->fd, VIDIOC_STREAMON, &type);
733 if (status != PJ_SUCCESS)
734 goto on_error;
735
736 return PJ_SUCCESS;
737
738on_error:
739 if (i > 0) {
740 /* Dequeue already enqueued buffers. Can we do this while streaming
741 * is not started?
742 */
743 unsigned n = i;
744 for (i=0; i<n; ++i) {
745 pj_bzero(&buf, sizeof(buf));
746 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
747 buf.memory = V4L2_MEMORY_MMAP;
748 xioctl(stream->fd, VIDIOC_DQBUF, &buf);
749 }
750 }
751 return status;
752}
753
754/* API: Stop stream. */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000755static pj_status_t vid4lin_stream_stop(pjmedia_vid_dev_stream *strm)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000756{
757 vid4lin_stream *stream = (vid4lin_stream*)strm;
758 enum v4l2_buf_type type;
759 pj_status_t status;
760
761 if (stream->fd < 0)
762 return PJ_SUCCESS;
763
764 PJ_LOG(4, (THIS_FILE, "Stopping v4l2 video stream %s", stream->name));
765
766 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
767 status = xioctl(stream->fd, VIDIOC_STREAMOFF, &type);
768 if (status != PJ_SUCCESS)
769 return status;
770
771 return PJ_SUCCESS;
772}
773
774
775/* API: Destroy stream. */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000776static pj_status_t vid4lin_stream_destroy(pjmedia_vid_dev_stream *strm)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000777{
778 vid4lin_stream *stream = (vid4lin_stream*)strm;
779 unsigned i;
780
781 PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);
782
783 vid4lin_stream_stop(strm);
784
785 PJ_LOG(4, (THIS_FILE, "Destroying v4l2 video stream %s", stream->name));
786
787 for (i=0; i<stream->buf_cnt; ++i) {
788 if (stream->buffers[i].start != MAP_FAILED) {
789 v4l2_munmap(stream->buffers[i].start, stream->buffers[i].length);
790 stream->buffers[i].start = MAP_FAILED;
791 }
792 }
793
794 if (stream->fd >= 0) {
795 v4l2_close(stream->fd);
796 stream->fd = -1;
797 }
798 pj_pool_release(stream->pool);
799
800 return PJ_SUCCESS;
801}
802
803#endif /* PJMEDIA_VIDEO_DEV_HAS_V4L2 */