Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-videodev/v4l2_dev.c

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