Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-videodev/ffmpeg_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
19 /* Video device with ffmpeg backend, currently only capture devices are
20 * implemented.
21 *
22 * Issues:
23 * - no device enumeration (ffmpeg limitation), so this uses "host API" enum
24 * instead
25 * - need stricter filter on "host API" enum, currently audio capture devs are
26 * still listed.
27 * - no format enumeration, currently hardcoded to PJMEDIA_FORMAT_RGB24 only
28 * - tested on Vista only (vfw backend) with virtual cam
29 * - vfw backend produce bottom up pictures
30 * - using VS IDE, this cannot run under debugger!
31 */
32
33 #include <pjmedia-videodev/videodev_imp.h>
34 #include <pj/assert.h>
35 #include <pj/log.h>
36 #include <pj/os.h>
37 #include <pj/unicode.h>
38
39
40 #if defined(PJMEDIA_HAS_VIDEO) && PJMEDIA_HAS_VIDEO != 0 && \
41 defined(PJMEDIA_HAS_LIBAVDEVICE) && PJMEDIA_HAS_LIBAVDEVICE != 0 && \
42 defined(PJMEDIA_VIDEO_DEV_HAS_FFMPEG) && PJMEDIA_VIDEO_DEV_HAS_FFMPEG != 0
43
44
45 #define THIS_FILE "ffmpeg.c"
46
47 #define LIBAVFORMAT_VER_AT_LEAST(major,minor) (LIBAVFORMAT_VERSION_MAJOR > major || \
48 (LIBAVFORMAT_VERSION_MAJOR == major && \
49 LIBAVFORMAT_VERSION_MINOR >= minor))
50
51 #include "../pjmedia/ffmpeg_util.h"
52 #include <libavdevice/avdevice.h>
53 #include <libavformat/avformat.h>
54 #if LIBAVFORMAT_VER_AT_LEAST(53,2)
55 # include <libavutil/pixdesc.h>
56 #endif
57
58 #define MAX_DEV_CNT 8
59 #define MAX_DEV_NAME_LEN 80
60
61 #ifndef PJMEDIA_USE_OLD_FFMPEG
62 # define av_free_packet av_packet_unref
63 # define av_close_input_stream(ctx) avformat_close_input(&ctx)
64 #endif
65
66
67 typedef struct ffmpeg_dev_info
68 {
69 pjmedia_vid_dev_info base;
70 const AVInputFormat *host_api;
71 const char *def_devname;
72 } ffmpeg_dev_info;
73
74
75 typedef struct ffmpeg_factory
76 {
77 pjmedia_vid_dev_factory base;
78 pj_pool_factory *pf;
79 pj_pool_t *pool;
80 pj_pool_t *dev_pool;
81 unsigned dev_count;
82 ffmpeg_dev_info dev_info[MAX_DEV_CNT];
83 } ffmpeg_factory;
84
85
86 typedef struct ffmpeg_stream
87 {
88 pjmedia_vid_dev_stream base;
89 ffmpeg_factory *factory;
90 pj_pool_t *pool;
91 pjmedia_vid_dev_param param;
92 AVFormatContext *ff_fmt_ctx;
93 void *frame_buf;
94 } ffmpeg_stream;
95
96
97 /* Prototypes */
98 static pj_status_t ffmpeg_factory_init(pjmedia_vid_dev_factory *f);
99 static pj_status_t ffmpeg_factory_destroy(pjmedia_vid_dev_factory *f);
100 static pj_status_t ffmpeg_factory_refresh(pjmedia_vid_dev_factory *f);
101 static unsigned ffmpeg_factory_get_dev_count(pjmedia_vid_dev_factory *f);
102 static pj_status_t ffmpeg_factory_get_dev_info(pjmedia_vid_dev_factory *f,
103 unsigned index,
104 pjmedia_vid_dev_info *info);
105 static pj_status_t ffmpeg_factory_default_param(pj_pool_t *pool,
106 pjmedia_vid_dev_factory *f,
107 unsigned index,
108 pjmedia_vid_dev_param *param);
109 static pj_status_t ffmpeg_factory_create_stream(
110 pjmedia_vid_dev_factory *f,
111 pjmedia_vid_dev_param *param,
112 const pjmedia_vid_dev_cb *cb,
113 void *user_data,
114 pjmedia_vid_dev_stream **p_vid_strm);
115
116 static pj_status_t ffmpeg_stream_get_param(pjmedia_vid_dev_stream *strm,
117 pjmedia_vid_dev_param *param);
118 static pj_status_t ffmpeg_stream_get_cap(pjmedia_vid_dev_stream *strm,
119 pjmedia_vid_dev_cap cap,
120 void *value);
121 static pj_status_t ffmpeg_stream_set_cap(pjmedia_vid_dev_stream *strm,
122 pjmedia_vid_dev_cap cap,
123 const void *value);
124 static pj_status_t ffmpeg_stream_start(pjmedia_vid_dev_stream *strm);
125 static pj_status_t ffmpeg_stream_get_frame(pjmedia_vid_dev_stream *s,
126 pjmedia_frame *frame);
127 static pj_status_t ffmpeg_stream_stop(pjmedia_vid_dev_stream *strm);
128 static pj_status_t ffmpeg_stream_destroy(pjmedia_vid_dev_stream *strm);
129
130 /* Operations */
131 static pjmedia_vid_dev_factory_op factory_op =
132 {
133 &ffmpeg_factory_init,
134 &ffmpeg_factory_destroy,
135 &ffmpeg_factory_get_dev_count,
136 &ffmpeg_factory_get_dev_info,
137 &ffmpeg_factory_default_param,
138 &ffmpeg_factory_create_stream,
139 &ffmpeg_factory_refresh
140 };
141
142 static pjmedia_vid_dev_stream_op stream_op =
143 {
144 &ffmpeg_stream_get_param,
145 &ffmpeg_stream_get_cap,
146 &ffmpeg_stream_set_cap,
147 &ffmpeg_stream_start,
148 &ffmpeg_stream_get_frame,
149 NULL,
150 &ffmpeg_stream_stop,
151 &ffmpeg_stream_destroy
152 };
153
154
155 static void print_ffmpeg_err(int err)
156 {
157 char errbuf[512];
158 if (av_strerror(err, errbuf, sizeof(errbuf)) >= 0)
159 PJ_LOG(1, (THIS_FILE, "ffmpeg err %d: %s", err, errbuf));
160
161 }
162
163 static void print_ffmpeg_log(void* ptr, int level, const char* fmt, va_list vl)
164 {
165 PJ_UNUSED_ARG(ptr);
166
167 /* Custom callback needs to filter log level by itself */
168 if (level > av_log_get_level())
169 return;
170
171 vfprintf(stdout, fmt, vl);
172 }
173
174
175 static pj_status_t ffmpeg_capture_open(AVFormatContext **ctx,
176 const AVInputFormat *ifmt,
177 const char *dev_name,
178 const pjmedia_vid_dev_param *param)
179 {
180 #if LIBAVFORMAT_VER_AT_LEAST(53,2)
181 AVDictionary *format_opts = NULL;
182 char buf[128];
183 enum AVPixelFormat av_fmt;
184
185 PJ_UNUSED_ARG(buf);
186 #else
187 AVFormatParameters fp;
188 #endif
189 pjmedia_video_format_detail *vfd;
190 pj_status_t status;
191 int err;
192
193 PJ_ASSERT_RETURN(ctx && ifmt && dev_name && param, PJ_EINVAL);
194 PJ_ASSERT_RETURN(param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO,
195 PJ_EINVAL);
196
197 status = pjmedia_format_id_to_PixelFormat(param->fmt.id, &av_fmt);
198 if (status != PJ_SUCCESS) {
199 avformat_free_context(*ctx);
200 return status;
201 }
202
203 vfd = pjmedia_format_get_video_format_detail(&param->fmt, PJ_TRUE);
204
205 /* Init ffmpeg format context */
206 *ctx = avformat_alloc_context();
207
208 #if LIBAVFORMAT_VER_AT_LEAST(53,2)
209 /* Init ffmpeg dictionary */
210 /*
211 snprintf(buf, sizeof(buf), "%d/%d", vfd->fps.num, vfd->fps.denum);
212 av_dict_set(&format_opts, "framerate", buf, 0);
213 snprintf(buf, sizeof(buf), "%dx%d", vfd->size.w, vfd->size.h);
214 av_dict_set(&format_opts, "video_size", buf, 0);
215 av_dict_set(&format_opts, "pixel_format", av_get_pix_fmt_name(av_fmt), 0);
216 */
217 /* Open capture stream */
218 err = avformat_open_input(ctx, dev_name, ifmt, &format_opts);
219 #else
220 /* Init ffmpeg format param */
221 pj_bzero(&fp, sizeof(fp));
222 fp.prealloced_context = 1;
223 fp.width = vfd->size.w;
224 fp.height = vfd->size.h;
225 fp.pix_fmt = av_fmt;
226 fp.time_base.num = vfd->fps.denum;
227 fp.time_base.den = vfd->fps.num;
228
229 /* Open capture stream */
230 err = av_open_input_stream(ctx, NULL, dev_name, ifmt, &fp);
231 #endif
232 if (err < 0) {
233 *ctx = NULL; /* ffmpeg freed its states on failure, do we must too */
234 print_ffmpeg_err(err);
235 return PJ_EUNKNOWN;
236 }
237
238 return PJ_SUCCESS;
239 }
240
241 static void ffmpeg_capture_close(AVFormatContext *ctx)
242 {
243 if (ctx)
244 av_close_input_stream(ctx);
245 }
246
247
248 /****************************************************************************
249 * Factory operations
250 */
251 /*
252 * Init ffmpeg_ video driver.
253 */
254 pjmedia_vid_dev_factory* pjmedia_ffmpeg_factory(pj_pool_factory *pf)
255 {
256 ffmpeg_factory *f;
257 pj_pool_t *pool;
258
259 pool = pj_pool_create(pf, "ffmpeg_cap_dev", 1000, 1000, NULL);
260 f = PJ_POOL_ZALLOC_T(pool, ffmpeg_factory);
261
262 f->pool = pool;
263 f->pf = pf;
264 f->base.op = &factory_op;
265
266 avdevice_register_all();
267
268 return &f->base;
269 }
270
271
272 /* API: init factory */
273 static pj_status_t ffmpeg_factory_init(pjmedia_vid_dev_factory *f)
274 {
275 return ffmpeg_factory_refresh(f);
276 }
277
278 /* API: destroy factory */
279 static pj_status_t ffmpeg_factory_destroy(pjmedia_vid_dev_factory *f)
280 {
281 ffmpeg_factory *ff = (ffmpeg_factory*)f;
282 pj_pool_t *pool = ff->pool;
283
284 ff->dev_count = 0;
285 ff->pool = NULL;
286 if (ff->dev_pool)
287 pj_pool_release(ff->dev_pool);
288 if (pool)
289 pj_pool_release(pool);
290
291 return PJ_SUCCESS;
292 }
293
294
295 #if (defined(PJ_WIN32) && PJ_WIN32!=0) || \
296 (defined(PJ_WIN64) && PJ_WIN64!=0)
297
298 #ifdef _MSC_VER
299 # pragma warning(push, 3)
300 #endif
301
302 #define COBJMACROS
303 #include <DShow.h>
304 #pragma comment(lib, "Strmiids.lib")
305
306 #ifdef _MSC_VER
307 # pragma warning(pop)
308 #endif
309
310 static pj_status_t dshow_enum_devices(unsigned *dev_cnt,
311 char dev_names[][MAX_DEV_NAME_LEN])
312 {
313 unsigned max_cnt = *dev_cnt;
314 ICreateDevEnum *dev_enum = NULL;
315 IEnumMoniker *enum_cat = NULL;
316 IMoniker *moniker = NULL;
317 HRESULT hr;
318 ULONG fetched;
319 unsigned i = 0;
320
321 CoInitialize(0);
322
323 *dev_cnt = 0;
324 hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL,
325 CLSCTX_INPROC_SERVER, &IID_ICreateDevEnum,
326 (void**)&dev_enum);
327 if (FAILED(hr) ||
328 ICreateDevEnum_CreateClassEnumerator(dev_enum,
329 &CLSID_VideoInputDeviceCategory, &enum_cat, 0) != S_OK)
330 {
331 PJ_LOG(4,(THIS_FILE, "Windows found no video input devices"));
332 if (dev_enum)
333 ICreateDevEnum_Release(dev_enum);
334
335 return PJ_SUCCESS;
336 }
337
338 while (IEnumMoniker_Next(enum_cat, 1, &moniker, &fetched) == S_OK &&
339 *dev_cnt < max_cnt)
340 {
341 (*dev_cnt)++;
342 }
343
344 if (*dev_cnt == 0) {
345 IEnumMoniker_Release(enum_cat);
346 ICreateDevEnum_Release(dev_enum);
347 return PJ_SUCCESS;
348 }
349
350 IEnumMoniker_Reset(enum_cat);
351 while (i < max_cnt &&
352 IEnumMoniker_Next(enum_cat, 1, &moniker, &fetched) == S_OK)
353 {
354 IPropertyBag *prop_bag;
355
356 hr = IMoniker_BindToStorage(moniker, 0, 0, &IID_IPropertyBag,
357 (void**)&prop_bag);
358 if (SUCCEEDED(hr)) {
359 VARIANT var_name;
360
361 VariantInit(&var_name);
362 hr = IPropertyBag_Read(prop_bag, L"FriendlyName", &var_name,
363 NULL);
364 if (SUCCEEDED(hr) && var_name.bstrVal) {
365 char tmp[MAX_DEV_NAME_LEN] = {0};
366 WideCharToMultiByte(CP_ACP, 0, var_name.bstrVal,
367 (int)wcslen(var_name.bstrVal),
368 tmp, MAX_DEV_NAME_LEN, NULL, NULL);
369 pj_ansi_snprintf(dev_names[i++], MAX_DEV_NAME_LEN,
370 "video=%s", tmp);
371 }
372 VariantClear(&var_name);
373 IPropertyBag_Release(prop_bag);
374 }
375 IMoniker_Release(moniker);
376 }
377
378 IEnumMoniker_Release(enum_cat);
379 ICreateDevEnum_Release(dev_enum);
380
381 PJ_LOG(4, (THIS_FILE, "DShow has %d devices:", *dev_cnt));
382 for (i = 0; i < *dev_cnt; ++i) {
383 PJ_LOG(4, (THIS_FILE, " %d: %s", (i+1), dev_names[i]));
384 }
385
386 return PJ_SUCCESS;
387 }
388
389 #endif /* PJ_WIN32 or PJ_WIN64 */
390
391
392 /* API: refresh the list of devices */
393 static pj_status_t ffmpeg_factory_refresh(pjmedia_vid_dev_factory *f)
394 {
395 ffmpeg_factory *ff = (ffmpeg_factory*)f;
396 const AVInputFormat *p;
397
398 av_log_set_callback(&print_ffmpeg_log);
399 av_log_set_level(AV_LOG_ERROR);
400
401 if (ff->dev_pool) {
402 pj_pool_release(ff->dev_pool);
403 ff->dev_pool = NULL;
404 }
405
406 ff->dev_count = 0;
407 ff->dev_pool = pj_pool_create(ff->pf, "ffmpeg_cap_dev", 500, 500, NULL);
408
409 /* Iterate host APIs */
410 p = av_input_video_device_next(NULL);
411 while (p && ff->dev_count < MAX_DEV_CNT) {
412 char dev_names[MAX_DEV_CNT][MAX_DEV_NAME_LEN];
413 unsigned dev_cnt = MAX_DEV_CNT;
414 unsigned dev_idx;
415
416 #if LIBAVFORMAT_VER_AT_LEAST(60, 21)
417 if ((p->flags & AVFMT_NOFILE)==0) {
418 #else
419 if ((p->flags & AVFMT_NOFILE)==0 || p->read_probe) {
420 #endif
421 goto next_format;
422 }
423
424 #if (defined(PJ_WIN32) && PJ_WIN32!=0) || \
425 (defined(PJ_WIN64) && PJ_WIN64!=0)
426 if (pj_ansi_strcmp(p->name, "dshow") == 0) {
427 dshow_enum_devices(&dev_cnt, dev_names);
428 } else if (pj_ansi_strcmp(p->name, "vfwcap") == 0) {
429 dev_cnt = 1;
430 pj_ansi_snprintf(dev_names[0], MAX_DEV_NAME_LEN, "0");
431 } else {
432 dev_cnt = 0;
433 }
434 #elif defined(PJ_LINUX) && PJ_LINUX!=0
435 dev_cnt = 1;
436 pj_ansi_snprintf(dev_names[0], MAX_DEV_NAME_LEN, "/dev/video0");
437 #else
438 dev_cnt = 0;
439 #endif
440
441 /* Iterate devices (only DirectShow devices for now) */
442 for (dev_idx = 0; dev_idx < dev_cnt && ff->dev_count < MAX_DEV_CNT;
443 ++dev_idx)
444 {
445 ffmpeg_dev_info *info;
446 AVFormatContext *ctx;
447 AVCodecContext *codec = NULL;
448 pjmedia_format_id fmt_id;
449 pj_str_t dev_name;
450 pj_status_t status;
451 unsigned i;
452
453 ctx = avformat_alloc_context();
454 if (!ctx || avformat_open_input(&ctx, dev_names[dev_idx], p, NULL)!=0)
455 continue;
456
457 for(i = 0; i < ctx->nb_streams; i++) {
458 #if LIBAVFORMAT_VER_AT_LEAST(59,0)
459 if (ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
460 codec = ctx->streams[i]->codecpar;
461 break;
462 }
463 #else
464 if (ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
465 codec = ctx->streams[i]->codec;
466 break;
467 }
468 #endif
469 }
470 if (!codec) {
471 av_close_input_stream(ctx);
472 continue;
473 }
474
475 status = PixelFormat_to_pjmedia_format_id(codec->pix_fmt, &fmt_id);
476 if (status != PJ_SUCCESS) {
477 av_close_input_stream(ctx);
478 continue;
479 }
480
481 info = &ff->dev_info[ff->dev_count++];
482 pj_bzero(info, sizeof(*info));
483 pj_ansi_strxcpy(info->base.name, "default",
484 sizeof(info->base.name));
485 pj_ansi_snprintf(info->base.driver, sizeof(info->base.driver),
486 "ffmpeg %s", p->name);
487
488 pj_strdup2_with_null(ff->pool, &dev_name, dev_names[dev_idx]);
489 info->def_devname = dev_name.ptr;
490 info->base.dir = PJMEDIA_DIR_CAPTURE;
491 info->base.has_callback = PJ_FALSE;
492
493 info->host_api = p;
494
495 /* Set supported formats */
496 info->base.caps = PJMEDIA_VID_DEV_CAP_FORMAT;
497 info->base.fmt_cnt = 1;
498 for (i = 0; i < info->base.fmt_cnt; ++i) {
499 pjmedia_format *fmt = &info->base.fmt[i];
500 pjmedia_format_init_video(fmt, fmt_id,
501 codec->width, codec->height, 15, 1);
502 }
503
504 av_close_input_stream(ctx);
505 }
506
507 next_format:
508 p = av_input_video_device_next(p);
509 }
510
511 return PJ_SUCCESS;
512 }
513
514 /* API: get number of devices */
515 static unsigned ffmpeg_factory_get_dev_count(pjmedia_vid_dev_factory *f)
516 {
517 ffmpeg_factory *ff = (ffmpeg_factory*)f;
518 return ff->dev_count;
519 }
520
521 /* API: get device info */
522 static pj_status_t ffmpeg_factory_get_dev_info(pjmedia_vid_dev_factory *f,
523 unsigned index,
524 pjmedia_vid_dev_info *info)
525 {
526 ffmpeg_factory *ff = (ffmpeg_factory*)f;
527
528 PJ_ASSERT_RETURN(index < ff->dev_count, PJMEDIA_EVID_INVDEV);
529
530 pj_memcpy(info, &ff->dev_info[index].base, sizeof(*info));
531
532 return PJ_SUCCESS;
533 }
534
535 /* API: create default device parameter */
536 static pj_status_t ffmpeg_factory_default_param(pj_pool_t *pool,
537 pjmedia_vid_dev_factory *f,
538 unsigned index,
539 pjmedia_vid_dev_param *param)
540 {
541 ffmpeg_factory *ff = (ffmpeg_factory*)f;
542 ffmpeg_dev_info *info;
543
544 PJ_ASSERT_RETURN(index < ff->dev_count, PJMEDIA_EVID_INVDEV);
545
546 PJ_UNUSED_ARG(pool);
547
548 info = &ff->dev_info[index];
549
550 pj_bzero(param, sizeof(*param));
551 param->dir = PJMEDIA_DIR_CAPTURE;
552 param->cap_id = index;
553 param->rend_id = PJMEDIA_VID_INVALID_DEV;
554 param->clock_rate = 0;
555
556 /* Set the device capabilities here */
557 param->flags = PJMEDIA_VID_DEV_CAP_FORMAT;
558 param->clock_rate = 90000;
559 pjmedia_format_init_video(&param->fmt, 0, 320, 240, 25, 1);
560 param->fmt.id = info->base.fmt[0].id;
561
562 return PJ_SUCCESS;
563 }
564
565
566
567 /* API: create stream */
568 static pj_status_t ffmpeg_factory_create_stream(
569 pjmedia_vid_dev_factory *f,
570 pjmedia_vid_dev_param *param,
571 const pjmedia_vid_dev_cb *cb,
572 void *user_data,
573 pjmedia_vid_dev_stream **p_vid_strm)
574 {
575 ffmpeg_factory *ff = (ffmpeg_factory*)f;
576 pj_pool_t *pool;
577 ffmpeg_stream *strm;
578
579 PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL);
580 PJ_ASSERT_RETURN(param->dir == PJMEDIA_DIR_CAPTURE, PJ_EINVAL);
581 PJ_ASSERT_RETURN((unsigned)param->cap_id < ff->dev_count, PJ_EINVAL);
582 PJ_ASSERT_RETURN(param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO,
583 PJ_EINVAL);
584
585 PJ_UNUSED_ARG(cb);
586 PJ_UNUSED_ARG(user_data);
587
588 /* Create and Initialize stream descriptor */
589 pool = pj_pool_create(ff->pf, "ffmpeg-dev", 1000, 1000, NULL);
590 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
591
592 strm = PJ_POOL_ZALLOC_T(pool, struct ffmpeg_stream);
593 strm->factory = (ffmpeg_factory*)f;
594 strm->pool = pool;
595 pj_memcpy(&strm->param, param, sizeof(*param));
596
597 /* Allocate frame buffer */
598 {
599 const pjmedia_video_format_info *vfi;
600 pjmedia_video_apply_fmt_param vafp;
601
602 vfi = pjmedia_get_video_format_info(NULL, param->fmt.id);
603 if (!vfi) goto on_error;
604
605 pj_bzero(&vafp, sizeof(vafp));
606 vafp.size = param->fmt.det.vid.size;
607 vfi->apply_fmt(vfi, &vafp);
608
609 strm->frame_buf = pj_pool_alloc(pool, vafp.framebytes);
610 }
611
612 /* Done */
613 strm->base.op = &stream_op;
614 *p_vid_strm = &strm->base;
615
616 return PJ_SUCCESS;
617
618 on_error:
619 pj_pool_release(pool);
620 return PJMEDIA_EVID_INVCAP;
621 }
622
623 /* API: Get stream info. */
624 static pj_status_t ffmpeg_stream_get_param(pjmedia_vid_dev_stream *s,
625 pjmedia_vid_dev_param *pi)
626 {
627 ffmpeg_stream *strm = (ffmpeg_stream*)s;
628
629 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
630
631 pj_memcpy(pi, &strm->param, sizeof(*pi));
632
633 return PJ_SUCCESS;
634 }
635
636 /* API: get capability */
637 static pj_status_t ffmpeg_stream_get_cap(pjmedia_vid_dev_stream *s,
638 pjmedia_vid_dev_cap cap,
639 void *pval)
640 {
641 ffmpeg_stream *strm = (ffmpeg_stream*)s;
642
643 PJ_UNUSED_ARG(strm);
644 PJ_UNUSED_ARG(cap);
645 PJ_UNUSED_ARG(pval);
646
647 return PJMEDIA_EVID_INVCAP;
648 }
649
650 /* API: set capability */
651 static pj_status_t ffmpeg_stream_set_cap(pjmedia_vid_dev_stream *s,
652 pjmedia_vid_dev_cap cap,
653 const void *pval)
654 {
655 ffmpeg_stream *strm = (ffmpeg_stream*)s;
656
657 PJ_UNUSED_ARG(strm);
658 PJ_UNUSED_ARG(cap);
659 PJ_UNUSED_ARG(pval);
660
661 return PJMEDIA_EVID_INVCAP;
662 }
663
664
665 /* API: Start stream. */
666 static pj_status_t ffmpeg_stream_start(pjmedia_vid_dev_stream *s)
667 {
668 ffmpeg_stream *strm = (ffmpeg_stream*)s;
669 ffmpeg_dev_info *info;
670 pj_status_t status;
671
672 info = &strm->factory->dev_info[strm->param.cap_id];
673
674 PJ_LOG(4, (THIS_FILE, "Starting ffmpeg capture stream"));
675
676 status = ffmpeg_capture_open(&strm->ff_fmt_ctx, info->host_api,
677 info->def_devname, &strm->param);
678 if (status != PJ_SUCCESS) {
679 /* must set ffmpeg states to NULL on any failure */
680 strm->ff_fmt_ctx = NULL;
681 }
682
683 return status;
684 }
685
686
687 /* API: Get frame from stream */
688 static pj_status_t ffmpeg_stream_get_frame(pjmedia_vid_dev_stream *s,
689 pjmedia_frame *frame)
690 {
691 ffmpeg_stream *strm = (ffmpeg_stream*)s;
692 AVPacket p = {0};
693 int err;
694
695 err = av_read_frame(strm->ff_fmt_ctx, &p);
696 if (err < 0) {
697 print_ffmpeg_err(err);
698 return PJ_EUNKNOWN;
699 }
700
701 pj_bzero(frame, sizeof(*frame));
702 frame->type = PJMEDIA_FRAME_TYPE_VIDEO;
703 frame->buf = strm->frame_buf;
704 frame->size = p.size;
705 pj_memcpy(frame->buf, p.data, p.size);
706 av_free_packet(&p);
707
708 return PJ_SUCCESS;
709 }
710
711
712 /* API: Stop stream. */
713 static pj_status_t ffmpeg_stream_stop(pjmedia_vid_dev_stream *s)
714 {
715 ffmpeg_stream *strm = (ffmpeg_stream*)s;
716
717 PJ_LOG(4, (THIS_FILE, "Stopping ffmpeg capture stream"));
718
719 ffmpeg_capture_close(strm->ff_fmt_ctx);
720 strm->ff_fmt_ctx = NULL;
721
722 return PJ_SUCCESS;
723 }
724
725
726 /* API: Destroy stream. */
727 static pj_status_t ffmpeg_stream_destroy(pjmedia_vid_dev_stream *s)
728 {
729 ffmpeg_stream *strm = (ffmpeg_stream*)s;
730
731 PJ_ASSERT_RETURN(strm != NULL, PJ_EINVAL);
732
733 ffmpeg_stream_stop(s);
734
735 pj_pool_release(strm->pool);
736
737 return PJ_SUCCESS;
738 }
739
740 #ifdef _MSC_VER
741 # pragma comment( lib, "avdevice.lib")
742 # pragma comment( lib, "avformat.lib")
743 # pragma comment( lib, "avutil.lib")
744 #endif
745
746
747 #endif /* PJMEDIA_VIDEO_DEV_HAS_FFMPEG */