blob: 199377c815a0cca3bcab9318fd659c9aee3e85f5 [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;
Sauw Ming7f7c5bd2011-06-21 09:33:01 +000084 pj_pool_t *dev_pool;
Benny Prijonoc45d9512010-12-10 11:04:30 +000085 pj_pool_factory *pf;
86
87 unsigned dev_count;
88 vid4lin_dev_info *dev_info;
89} vid4lin_factory;
90
91/* Video stream. */
92typedef struct vid4lin_stream
93{
Benny Prijono6c7e95e2011-03-15 11:22:04 +000094 pjmedia_vid_dev_stream base; /**< Base stream */
Benny Prijonoc45d9512010-12-10 11:04:30 +000095 pjmedia_vid_param param; /**< Settings */
96 pj_pool_t *pool; /**< Memory pool. */
97
98 int fd; /**< Video fd. */
99 char name[64]; /**< Name for log */
100 enum vid4lin_io_type io_type; /**< I/O method. */
101 unsigned buf_cnt; /**< MMap buf cnt. */
102 vid4lin_buffer *buffers; /**< MMap buffers. */
103 pj_time_val start_time; /**< Time when started */
104
105 pjmedia_vid_cb vid_cb; /**< Stream callback */
106 void *user_data; /**< Application data */
107} vid4lin_stream;
108
109/* Use this to convert between pjmedia_format_id and V4L2 fourcc */
110static vid4lin_fmt_map v4l2_fmt_maps[] =
111{
112 { PJMEDIA_FORMAT_RGB24, V4L2_PIX_FMT_BGR24 },
113 { PJMEDIA_FORMAT_RGBA, V4L2_PIX_FMT_BGR32 },
114 { PJMEDIA_FORMAT_RGB32, V4L2_PIX_FMT_BGR32 },
115 { PJMEDIA_FORMAT_AYUV, V4L2_PIX_FMT_YUV32 },
116 { PJMEDIA_FORMAT_YUY2, V4L2_PIX_FMT_YUYV },
117 { PJMEDIA_FORMAT_UYVY, V4L2_PIX_FMT_UYVY }
118};
119
120/* Prototypes */
121static pj_status_t vid4lin_factory_init(pjmedia_vid_dev_factory *f);
122static pj_status_t vid4lin_factory_destroy(pjmedia_vid_dev_factory *f);
Sauw Ming7f7c5bd2011-06-21 09:33:01 +0000123static pj_status_t vid4lin_factory_refresh(pjmedia_vid_dev_factory *f);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000124static unsigned vid4lin_factory_get_dev_count(pjmedia_vid_dev_factory *f);
125static pj_status_t vid4lin_factory_get_dev_info(pjmedia_vid_dev_factory *f,
126 unsigned index,
127 pjmedia_vid_dev_info *info);
128static pj_status_t vid4lin_factory_default_param(pj_pool_t *pool,
129 pjmedia_vid_dev_factory *f,
130 unsigned index,
131 pjmedia_vid_param *param);
132static pj_status_t vid4lin_factory_create_stream(pjmedia_vid_dev_factory *f,
Benny Prijonoe9f70d82011-03-25 08:38:26 +0000133 pjmedia_vid_param *prm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000134 const pjmedia_vid_cb *cb,
135 void *user_data,
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000136 pjmedia_vid_dev_stream **p);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000137
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000138static pj_status_t vid4lin_stream_get_param(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000139 pjmedia_vid_param *param);
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000140static pj_status_t vid4lin_stream_get_cap(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000141 pjmedia_vid_dev_cap cap,
142 void *value);
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000143static pj_status_t vid4lin_stream_set_cap(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000144 pjmedia_vid_dev_cap cap,
145 const void *value);
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000146static pj_status_t vid4lin_stream_get_frame(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000147 pjmedia_frame *frame);
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000148static pj_status_t vid4lin_stream_start(pjmedia_vid_dev_stream *strm);
149static pj_status_t vid4lin_stream_stop(pjmedia_vid_dev_stream *strm);
150static pj_status_t vid4lin_stream_destroy(pjmedia_vid_dev_stream *strm);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000151
152/* Operations */
153static pjmedia_vid_dev_factory_op factory_op =
154{
155 &vid4lin_factory_init,
156 &vid4lin_factory_destroy,
157 &vid4lin_factory_get_dev_count,
158 &vid4lin_factory_get_dev_info,
159 &vid4lin_factory_default_param,
Sauw Ming7f7c5bd2011-06-21 09:33:01 +0000160 &vid4lin_factory_create_stream,
161 &vid4lin_factory_refresh
Benny Prijonoc45d9512010-12-10 11:04:30 +0000162};
163
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000164static pjmedia_vid_dev_stream_op stream_op =
Benny Prijonoc45d9512010-12-10 11:04:30 +0000165{
166 &vid4lin_stream_get_param,
167 &vid4lin_stream_get_cap,
168 &vid4lin_stream_set_cap,
169 &vid4lin_stream_start,
170 &vid4lin_stream_get_frame,
171 NULL,
172 &vid4lin_stream_stop,
173 &vid4lin_stream_destroy
174};
175
176
177/****************************************************************************
178 * Factory operations
179 */
180/*
181 * Factory creation function.
182 */
183pjmedia_vid_dev_factory* pjmedia_v4l2_factory(pj_pool_factory *pf)
184{
185 vid4lin_factory *f;
186 pj_pool_t *pool;
187
188 pool = pj_pool_create(pf, DRIVER_NAME, 512, 512, NULL);
189 f = PJ_POOL_ZALLOC_T(pool, vid4lin_factory);
190 f->pf = pf;
191 f->pool = pool;
192 f->base.op = &factory_op;
193
194 return &f->base;
195}
196
197/* util: ioctl that tries harder. */
198static pj_status_t xioctl(int fh, int request, void *arg)
199{
200 enum { RETRY = MAX_IOCTL_RETRY };
201 int r, c=0;
202
203 do {
204 r = v4l2_ioctl(fh, request, arg);
205 } while (r==-1 && c++<RETRY && ((errno==EINTR) || (errno==EAGAIN)));
206
207 return (r == -1) ? pj_get_os_error() : PJ_SUCCESS;
208}
209
210/* Scan V4L2 devices */
211static pj_status_t v4l2_scan_devs(vid4lin_factory *f)
212{
213 vid4lin_dev_info vdi[V4L2_MAX_DEVS];
214 char dev_name[32];
215 unsigned i, old_count;
216 pj_status_t status;
217
Sauw Ming7f7c5bd2011-06-21 09:33:01 +0000218 if (f->dev_pool) {
219 pj_pool_release(f->dev_pool);
220 f->dev_pool = NULL;
221 }
222
Benny Prijonoc45d9512010-12-10 11:04:30 +0000223 pj_bzero(vdi, sizeof(vdi));
224 old_count = f->dev_count;
225 f->dev_count = 0;
Sauw Ming7f7c5bd2011-06-21 09:33:01 +0000226 f->dev_pool = pj_pool_create(f->pf, DRIVER_NAME, 500, 500, NULL);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000227
228 for (i=0; i<V4L2_MAX_DEVS && f->dev_count < V4L2_MAX_DEVS; ++i) {
229 int fd;
230 vid4lin_dev_info *pdi;
Sauw Ming7f7c5bd2011-06-21 09:33:01 +0000231 pj_pool_t *pool = f->dev_pool;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000232 pj_uint32_t fmt_cap[8];
233 int j, fmt_cnt=0;
234
235 pdi = &vdi[f->dev_count];
236
237 snprintf(dev_name, sizeof(dev_name), "/dev/video%d", i);
238 if (!pj_file_exists(dev_name))
239 continue;
240
241 fd = v4l2_open(dev_name, O_RDWR, 0);
242 if (fd == -1)
243 continue;
244
245 status = xioctl(fd, VIDIOC_QUERYCAP, &pdi->v4l2_cap);
246 if (status != PJ_SUCCESS) {
247 PJ_PERROR(4,(THIS_FILE, status, "Error querying %s", dev_name));
248 v4l2_close(fd);
249 continue;
250 }
251
252 if ((pdi->v4l2_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
253 v4l2_close(fd);
254 continue;
255 }
256
257 PJ_LOG(5,(THIS_FILE, "Found capture device %s", pdi->v4l2_cap.card));
258 PJ_LOG(5,(THIS_FILE, " Enumerating formats:"));
259 for (j=0; fmt_cnt<PJ_ARRAY_SIZE(fmt_cap); ++j) {
260 struct v4l2_fmtdesc fdesc;
261 unsigned k;
262
263 pj_bzero(&fdesc, sizeof(fdesc));
264 fdesc.index = j;
265 fdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
266
267 status = xioctl(fd, VIDIOC_ENUM_FMT, &fdesc);
268 if (status != PJ_SUCCESS)
269 break;
270
271 for (k=0; k<PJ_ARRAY_SIZE(v4l2_fmt_maps); ++k) {
272 if (v4l2_fmt_maps[k].v4l2_fmt_id == fdesc.pixelformat) {
273 fmt_cap[fmt_cnt++] = v4l2_fmt_maps[k].pjmedia_fmt_id;
274 PJ_LOG(5,(THIS_FILE, " Supported: %s",
275 fdesc.description));
276 break;
277 }
278 }
279 if (k==PJ_ARRAY_SIZE(v4l2_fmt_maps)) {
280 PJ_LOG(5,(THIS_FILE, " Unsupported: %s", fdesc.description));
281 }
282 }
283
284 v4l2_close(fd);
285
286 if (fmt_cnt==0) {
287 PJ_LOG(5,(THIS_FILE, " Found no common format"));
288 continue;
289 }
290
291 strncpy(pdi->dev_name, dev_name, sizeof(pdi->dev_name));
292 pdi->dev_name[sizeof(pdi->dev_name)-1] = '\0';
293 strncpy(pdi->info.name, (char*)pdi->v4l2_cap.card,
294 sizeof(pdi->info.name));
295 pdi->info.name[sizeof(pdi->info.name)-1] = '\0';
296 strncpy(pdi->info.driver, DRIVER_NAME, sizeof(pdi->info.driver));
297 pdi->info.driver[sizeof(pdi->info.driver)-1] = '\0';
298 pdi->info.dir = PJMEDIA_DIR_CAPTURE;
299 pdi->info.has_callback = PJ_FALSE;
300 pdi->info.caps = PJMEDIA_VID_DEV_CAP_FORMAT;
301
302 pdi->info.fmt_cnt = fmt_cnt;
303 pdi->info.fmt = (pjmedia_format*)
304 pj_pool_calloc(pool, sizeof(pjmedia_format), fmt_cnt);
305
306 for (j=0; j<fmt_cnt; ++j) {
307 pjmedia_format_init_video(&pdi->info.fmt[j],
308 fmt_cap[j],
309 DEFAULT_WIDTH,
310 DEFAULT_HEIGHT,
311 DEFAULT_FPS, 1);
312 }
313 if (j < fmt_cnt)
314 continue;
315
316 f->dev_count++;
317 }
318
319 if (f->dev_count == 0)
320 return PJ_SUCCESS;
321
322 if (f->dev_count > old_count || f->dev_info == NULL) {
323 f->dev_info = (vid4lin_dev_info*)
Sauw Ming7f7c5bd2011-06-21 09:33:01 +0000324 pj_pool_calloc(f->dev_pool,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000325 f->dev_count,
326 sizeof(vid4lin_dev_info));
327 }
328 pj_memcpy(f->dev_info, vdi, f->dev_count * sizeof(vid4lin_dev_info));
329
330 return PJ_SUCCESS;
331}
332
333
334/* API: init factory */
335static pj_status_t vid4lin_factory_init(pjmedia_vid_dev_factory *f)
336{
Sauw Ming7f7c5bd2011-06-21 09:33:01 +0000337 return vid4lin_factory_refresh(f);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000338}
339
340/* API: destroy factory */
341static pj_status_t vid4lin_factory_destroy(pjmedia_vid_dev_factory *f)
342{
343 vid4lin_factory *cf = (vid4lin_factory*)f;
344 pj_pool_t *pool = cf->pool;
345
Sauw Ming7f7c5bd2011-06-21 09:33:01 +0000346 if (cf->dev_pool)
347 pj_pool_release(cf->dev_pool);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000348 if (cf->pool) {
349 cf->pool = NULL;
350 pj_pool_release(pool);
351 }
352
353 return PJ_SUCCESS;
354}
355
Sauw Ming7f7c5bd2011-06-21 09:33:01 +0000356/* API: refresh the list of devices */
357static pj_status_t vid4lin_factory_refresh(pjmedia_vid_dev_factory *f)
358{
359 vid4lin_factory *cf = (vid4lin_factory*)f;
360 pj_status_t status;
361
362 status = v4l2_scan_devs(cf);
363 if (status != PJ_SUCCESS)
364 return status;
365
366 PJ_LOG(4, (THIS_FILE, "Video4Linux2 has %d devices",
367 cf->dev_count));
368
369 return PJ_SUCCESS;
370}
371
Benny Prijonoc45d9512010-12-10 11:04:30 +0000372/* API: get number of devices */
373static unsigned vid4lin_factory_get_dev_count(pjmedia_vid_dev_factory *f)
374{
375 vid4lin_factory *cf = (vid4lin_factory*)f;
376 return cf->dev_count;
377}
378
379/* API: get device info */
380static pj_status_t vid4lin_factory_get_dev_info(pjmedia_vid_dev_factory *f,
381 unsigned index,
382 pjmedia_vid_dev_info *info)
383{
384 vid4lin_factory *cf = (vid4lin_factory*)f;
385
386 PJ_ASSERT_RETURN(index < cf->dev_count, PJMEDIA_EVID_INVDEV);
387
388 pj_memcpy(info, &cf->dev_info[index].info, sizeof(*info));
389
390 return PJ_SUCCESS;
391}
392
393/* API: create default device parameter */
394static pj_status_t vid4lin_factory_default_param(pj_pool_t *pool,
395 pjmedia_vid_dev_factory *f,
396 unsigned index,
397 pjmedia_vid_param *param)
398{
399 vid4lin_factory *cf = (vid4lin_factory*)f;
400
401 PJ_ASSERT_RETURN(index < cf->dev_count, PJMEDIA_EVID_INVDEV);
402
403 pj_bzero(param, sizeof(*param));
404 param->dir = PJMEDIA_DIR_CAPTURE;
405 param->cap_id = index;
406 param->rend_id = PJMEDIA_VID_INVALID_DEV;
407 param->flags = PJMEDIA_VID_DEV_CAP_FORMAT;
408 param->clock_rate = DEFAULT_CLOCK_RATE;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000409 pjmedia_format_copy(&param->fmt, &cf->dev_info[index].info.fmt[0]);
410
411 return PJ_SUCCESS;
412}
413
414static vid4lin_fmt_map* get_v4l2_format_info(pjmedia_format_id id)
415{
416 unsigned i;
417
418 for (i = 0; i < PJ_ARRAY_SIZE(v4l2_fmt_maps); i++) {
419 if (v4l2_fmt_maps[i].pjmedia_fmt_id == id)
420 return &v4l2_fmt_maps[i];
421 }
422
423 return NULL;
424}
425
426/* util: setup format */
427static pj_status_t vid4lin_stream_init_fmt(vid4lin_stream *stream,
428 const pjmedia_vid_param *param,
429 pj_uint32_t pix_fmt)
430{
Benny Prijonoe9f70d82011-03-25 08:38:26 +0000431 pjmedia_video_format_detail *vfd;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000432 struct v4l2_format v4l2_fmt;
433 pj_status_t status;
434
435 vfd = pjmedia_format_get_video_format_detail(&param->fmt, PJ_TRUE);
436 if (vfd == NULL)
437 return PJMEDIA_EVID_BADFORMAT;
438
439 pj_bzero(&v4l2_fmt, sizeof(v4l2_fmt));
440 v4l2_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
441 v4l2_fmt.fmt.pix.width = vfd->size.w;
442 v4l2_fmt.fmt.pix.height = vfd->size.h;
443 v4l2_fmt.fmt.pix.pixelformat = pix_fmt;
444 v4l2_fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
445 status = xioctl(stream->fd, VIDIOC_S_FMT, &v4l2_fmt);
446 if (status != PJ_SUCCESS)
447 return status;
448
449 if (v4l2_fmt.fmt.pix.pixelformat != pix_fmt) {
450 status = PJMEDIA_EVID_BADFORMAT;
451 return status;
452 }
453
454 if ((v4l2_fmt.fmt.pix.width != vfd->size.w) ||
455 (v4l2_fmt.fmt.pix.height != vfd->size.h))
456 {
Benny Prijonoe9f70d82011-03-25 08:38:26 +0000457 /* Size has changed */
458 vfd->size.w = v4l2_fmt.fmt.pix.width;
459 vfd->size.h = v4l2_fmt.fmt.pix.height;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000460 }
461
462 return PJ_SUCCESS;
463}
464
465/* Util: initiate v4l2 streaming via mmap */
466static pj_status_t vid4lin_stream_init_streaming(vid4lin_stream *stream)
467{
468 struct v4l2_requestbuffers req;
469 unsigned i;
470 pj_status_t status;
471
472 pj_bzero(&req, sizeof(req));
473 req.count = BUFFER_CNT;
474 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
475 req.memory = V4L2_MEMORY_MMAP;
476 status = xioctl(stream->fd, VIDIOC_REQBUFS, &req);
477 if (status != PJ_SUCCESS)
478 return status;
479
480 stream->buffers = pj_pool_calloc(stream->pool, req.count,
481 sizeof(*stream->buffers));
482 stream->buf_cnt = 0;
483
484 for (i = 0; i < req.count; ++i) {
485 struct v4l2_buffer buf;
486
487 pj_bzero(&buf, sizeof(buf));
488
489 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
490 buf.memory = V4L2_MEMORY_MMAP;
491 buf.index = i;
492
493 status = xioctl(stream->fd, VIDIOC_QUERYBUF, &buf);
494 if (status != PJ_SUCCESS)
495 goto on_error;
496
497 stream->buffers[i].length = buf.length;
498 stream->buffers[i].start = v4l2_mmap(NULL, buf.length,
499 PROT_READ | PROT_WRITE,
500 MAP_SHARED, stream->fd,
501 buf.m.offset);
502
503 if (MAP_FAILED == stream->buffers[i].start) {
504 status = pj_get_os_error();
505 goto on_error;
506 }
507
508 stream->buf_cnt++;
509 }
510
511 PJ_LOG(5,(THIS_FILE, " mmap streaming initialized"));
512
513 stream->io_type = IO_TYPE_MMAP;
514 return PJ_SUCCESS;
515
516on_error:
517 return status;
518}
519
520/* init streaming with user pointer */
521static pj_status_t vid4lin_stream_init_streaming_user(vid4lin_stream *stream)
522{
523 return PJ_ENOTSUP;
524}
525
526/* init streaming with read() */
527static pj_status_t vid4lin_stream_init_read_write(vid4lin_stream *stream)
528{
529 return PJ_ENOTSUP;
530}
531
532/* API: create stream */
533static pj_status_t vid4lin_factory_create_stream(pjmedia_vid_dev_factory *f,
Benny Prijonoe9f70d82011-03-25 08:38:26 +0000534 pjmedia_vid_param *param,
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000535 const pjmedia_vid_cb *cb,
536 void *user_data,
537 pjmedia_vid_dev_stream **p_vid_strm)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000538{
539 vid4lin_factory *cf = (vid4lin_factory*)f;
540 pj_pool_t *pool;
541 vid4lin_stream *stream;
542 vid4lin_dev_info *vdi;
543 const vid4lin_fmt_map *fmt_map;
544 const pjmedia_video_format_info *fmt_info;
Benny Prijonoe9f70d82011-03-25 08:38:26 +0000545 pjmedia_video_format_detail *vfd;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000546 pj_status_t status = PJ_SUCCESS;
547
Benny Prijonoe9f70d82011-03-25 08:38:26 +0000548
Benny Prijonoc45d9512010-12-10 11:04:30 +0000549 PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL);
550 PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO &&
Sauw Ming83db7d62011-06-09 04:08:47 +0000551 param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO &&
552 param->dir == PJMEDIA_DIR_CAPTURE,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000553 PJ_EINVAL);
554 PJ_ASSERT_RETURN(param->cap_id >= 0 && param->cap_id < cf->dev_count,
555 PJMEDIA_EVID_INVDEV);
556
557 fmt_info = pjmedia_get_video_format_info(NULL, param->fmt.id);
558 if (!fmt_info || (fmt_map=get_v4l2_format_info(param->fmt.id))==NULL)
559 return PJMEDIA_EVID_BADFORMAT;
560
561 vdi = &cf->dev_info[param->cap_id];
Benny Prijonoe9f70d82011-03-25 08:38:26 +0000562 vfd = pjmedia_format_get_video_format_detail(&param->fmt, PJ_TRUE);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000563
564 /* Create and Initialize stream descriptor */
565 pool = pj_pool_create(cf->pf, vdi->info.name, 512, 512, NULL);
566 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
567
568 stream = PJ_POOL_ZALLOC_T(pool, vid4lin_stream);
569 pj_memcpy(&stream->param, param, sizeof(*param));
570 stream->pool = pool;
571 pj_memcpy(&stream->vid_cb, cb, sizeof(*cb));
572 strncpy(stream->name, vdi->info.name, sizeof(stream->name));
573 stream->name[sizeof(stream->name)-1] = '\0';
574 stream->user_data = user_data;
575 stream->fd = INVALID_FD;
Benny Prijonofcf5db32011-07-14 04:56:08 +0000576 pjmedia_event_publisher_init(&stream->base.epub, PJMEDIA_SIG_VID_DEV_V4L2);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000577
Benny Prijonoc45d9512010-12-10 11:04:30 +0000578 stream->fd = v4l2_open(vdi->dev_name, O_RDWR | O_NONBLOCK, 0);
579 if (stream->fd < 0)
580 goto on_error;
581
582 status = vid4lin_stream_init_fmt(stream, param, fmt_map->v4l2_fmt_id);
583 if (status != PJ_SUCCESS)
584 goto on_error;
585
586 if (vdi->v4l2_cap.capabilities & V4L2_CAP_STREAMING)
587 status = vid4lin_stream_init_streaming(stream);
588
589 if (status!=PJ_SUCCESS && vdi->v4l2_cap.capabilities & V4L2_CAP_STREAMING)
590 status = vid4lin_stream_init_streaming_user(stream);
591
592 if (status!=PJ_SUCCESS && vdi->v4l2_cap.capabilities & V4L2_CAP_READWRITE)
593 status = vid4lin_stream_init_read_write(stream);
594
595 if (status != PJ_SUCCESS) {
596 PJ_LOG(1,(THIS_FILE, "Error: unable to initiate I/O on %s",
597 stream->name));
598 goto on_error;
599 }
600
601 /* Done */
602 stream->base.op = &stream_op;
603 *p_vid_strm = &stream->base;
604
605 return PJ_SUCCESS;
606
607on_error:
608 if (status == PJ_SUCCESS)
609 status = PJ_RETURN_OS_ERROR(errno);
610
611 vid4lin_stream_destroy(&stream->base);
612 return status;
613}
614
615/* API: Get stream info. */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000616static pj_status_t vid4lin_stream_get_param(pjmedia_vid_dev_stream *s,
617 pjmedia_vid_param *pi)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000618{
619 vid4lin_stream *strm = (vid4lin_stream*)s;
620
621 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
622
623 pj_memcpy(pi, &strm->param, sizeof(*pi));
624
625 return PJ_SUCCESS;
626}
627
628/* API: get capability */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000629static pj_status_t vid4lin_stream_get_cap(pjmedia_vid_dev_stream *s,
630 pjmedia_vid_dev_cap cap,
631 void *pval)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000632{
633 vid4lin_stream *strm = (vid4lin_stream*)s;
634
635 PJ_UNUSED_ARG(strm);
636
637 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
638
639 if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
640 {
641 return PJMEDIA_EVID_INVCAP;
642// return PJ_SUCCESS;
643 } else {
644 return PJMEDIA_EVID_INVCAP;
645 }
646}
647
648/* API: set capability */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000649static pj_status_t vid4lin_stream_set_cap(pjmedia_vid_dev_stream *s,
650 pjmedia_vid_dev_cap cap,
651 const void *pval)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000652{
653 vid4lin_stream *strm = (vid4lin_stream*)s;
654
655
656 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
657
658 /*
659 if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
660 {
661 return PJ_SUCCESS;
662 }
663 */
664 PJ_UNUSED_ARG(strm);
665 PJ_UNUSED_ARG(cap);
666 PJ_UNUSED_ARG(pval);
667
668 return PJMEDIA_EVID_INVCAP;
669}
670
671/* get frame from mmap */
672static pj_status_t vid4lin_stream_get_frame_mmap(vid4lin_stream *stream,
673 pjmedia_frame *frame)
674{
675 struct v4l2_buffer buf;
676 pj_time_val time;
677 pj_status_t status = PJ_SUCCESS;
678
679 pj_bzero(&buf, sizeof(buf));
680 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
681 buf.memory = V4L2_MEMORY_MMAP;
682 status = xioctl(stream->fd, VIDIOC_DQBUF, &buf);
683 if (status != PJ_SUCCESS)
684 return status;
685
686 if (frame->size < buf.bytesused) {
687 /* supplied buffer is too small */
688 pj_assert(!"frame buffer is too small for v4l2");
689 status = PJ_ETOOSMALL;
690 goto on_return;
691 }
692
693 time.sec = buf.timestamp.tv_sec;
694 time.msec = buf.timestamp.tv_usec / 1000;
695 PJ_TIME_VAL_SUB(time, stream->start_time);
696
697 frame->type = PJMEDIA_FRAME_TYPE_VIDEO;
698 frame->size = buf.bytesused;
Benny Prijono349037b2011-03-17 11:25:19 +0000699 frame->timestamp.u64 = PJ_UINT64(1) * PJ_TIME_VAL_MSEC(time) *
700 stream->param.clock_rate / PJ_UINT64(1000);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000701 pj_memcpy(frame->buf, stream->buffers[buf.index].start, buf.bytesused);
702
703on_return:
704 pj_bzero(&buf, sizeof(buf));
705 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
706 buf.memory = V4L2_MEMORY_MMAP;
707 xioctl(stream->fd, VIDIOC_QBUF, &buf);
708
709 return status;
710}
711
712/* API: Get frame from stream */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000713static pj_status_t vid4lin_stream_get_frame(pjmedia_vid_dev_stream *strm,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000714 pjmedia_frame *frame)
715{
716 vid4lin_stream *stream = (vid4lin_stream*)strm;
717
718 if (stream->io_type == IO_TYPE_MMAP)
719 return vid4lin_stream_get_frame_mmap(stream, frame);
720 else {
721 pj_assert(!"Unsupported i/o type");
722 return PJ_EINVALIDOP;
723 }
724}
725
726/* API: Start stream. */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000727static pj_status_t vid4lin_stream_start(pjmedia_vid_dev_stream *strm)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000728{
729 vid4lin_stream *stream = (vid4lin_stream*)strm;
730 struct v4l2_buffer buf;
731 enum v4l2_buf_type type;
732 unsigned i;
733 pj_status_t status;
734
735 PJ_ASSERT_RETURN(stream->fd != -1, PJ_EINVALIDOP);
736
737 PJ_LOG(4, (THIS_FILE, "Starting v4l2 video stream %s", stream->name));
738
739 pj_gettimeofday(&stream->start_time);
740
741 for (i = 0; i < stream->buf_cnt; ++i) {
742 pj_bzero(&buf, sizeof(buf));
743 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
744 buf.memory = V4L2_MEMORY_MMAP;
745 buf.index = i;
746 status = xioctl(stream->fd, VIDIOC_QBUF, &buf);
747 if (status != PJ_SUCCESS)
748 goto on_error;
749 }
750 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
751
752 status = xioctl(stream->fd, VIDIOC_STREAMON, &type);
753 if (status != PJ_SUCCESS)
754 goto on_error;
755
756 return PJ_SUCCESS;
757
758on_error:
759 if (i > 0) {
760 /* Dequeue already enqueued buffers. Can we do this while streaming
761 * is not started?
762 */
763 unsigned n = i;
764 for (i=0; i<n; ++i) {
765 pj_bzero(&buf, sizeof(buf));
766 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
767 buf.memory = V4L2_MEMORY_MMAP;
768 xioctl(stream->fd, VIDIOC_DQBUF, &buf);
769 }
770 }
771 return status;
772}
773
774/* API: Stop stream. */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000775static pj_status_t vid4lin_stream_stop(pjmedia_vid_dev_stream *strm)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000776{
777 vid4lin_stream *stream = (vid4lin_stream*)strm;
778 enum v4l2_buf_type type;
779 pj_status_t status;
780
781 if (stream->fd < 0)
782 return PJ_SUCCESS;
783
784 PJ_LOG(4, (THIS_FILE, "Stopping v4l2 video stream %s", stream->name));
785
786 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
787 status = xioctl(stream->fd, VIDIOC_STREAMOFF, &type);
788 if (status != PJ_SUCCESS)
789 return status;
790
791 return PJ_SUCCESS;
792}
793
794
795/* API: Destroy stream. */
Benny Prijono6c7e95e2011-03-15 11:22:04 +0000796static pj_status_t vid4lin_stream_destroy(pjmedia_vid_dev_stream *strm)
Benny Prijonoc45d9512010-12-10 11:04:30 +0000797{
798 vid4lin_stream *stream = (vid4lin_stream*)strm;
799 unsigned i;
800
801 PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);
802
803 vid4lin_stream_stop(strm);
804
805 PJ_LOG(4, (THIS_FILE, "Destroying v4l2 video stream %s", stream->name));
806
807 for (i=0; i<stream->buf_cnt; ++i) {
808 if (stream->buffers[i].start != MAP_FAILED) {
809 v4l2_munmap(stream->buffers[i].start, stream->buffers[i].length);
810 stream->buffers[i].start = MAP_FAILED;
811 }
812 }
813
814 if (stream->fd >= 0) {
815 v4l2_close(stream->fd);
816 stream->fd = -1;
817 }
818 pj_pool_release(stream->pool);
819
820 return PJ_SUCCESS;
821}
822
823#endif /* PJMEDIA_VIDEO_DEV_HAS_V4L2 */