1
/* $Id: vid_dev_test.c 4084 2012-04-25 07:13:05Z ming $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include <pjmedia-audiodev/audiodev.h>
22
#include <pjmedia-codec/ffmpeg_vid_codecs.h>
23
#include <pjmedia/vid_codec.h>
24
#include <pjmedia_videodev.h>
27
#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
30
#define THIS_FILE "vid_dev_test.c"
31
#define LOOP_DURATION 10
33
static pj_bool_t is_quitting = PJ_FALSE;
35
static const char *vid_dir_name(pjmedia_dir dir)
38
case PJMEDIA_DIR_CAPTURE:
40
case PJMEDIA_DIR_RENDER:
42
case PJMEDIA_DIR_CAPTURE_RENDER:
43
return "capture & render";
49
static int enum_devs(void)
54
PJ_LOG(3, (THIS_FILE, " Enum video devices:"));
55
dev_cnt = pjmedia_vid_dev_count();
56
for (i = 0; i < dev_cnt; ++i) {
57
pjmedia_vid_dev_info di;
58
status = pjmedia_vid_dev_get_info(i, &di);
59
if (status == PJ_SUCCESS) {
62
PJ_LOG(3, (THIS_FILE, " %3d: %s (%s) - %s", i, di.name, di.driver,
63
vid_dir_name(di.dir)));
65
PJ_LOG(3,(THIS_FILE, " Supported formats:"));
66
for (j=0; j<di.fmt_cnt; ++j) {
67
const pjmedia_video_format_info *vfi;
69
vfi = pjmedia_get_video_format_info(NULL, di.fmt[j].id);
70
PJ_LOG(3,(THIS_FILE, " %s",
71
(vfi ? vfi->name : "unknown")));
79
static pj_status_t vid_event_cb(pjmedia_event *event,
82
PJ_UNUSED_ARG(user_data);
84
if (event->type == PJMEDIA_EVENT_WND_CLOSED)
85
is_quitting = PJ_TRUE;
90
static int capture_render_loopback(int cap_dev_id, int rend_dev_id,
91
const pjmedia_format *fmt)
94
pjmedia_vid_port *capture=NULL, *renderer=NULL;
95
pjmedia_vid_dev_info cdi, rdi;
96
pjmedia_vid_port_param param;
97
pjmedia_video_format_detail *vfd;
101
pool = pj_pool_create(mem, "vidloop", 1000, 1000, NULL);
103
status = pjmedia_vid_dev_get_info(cap_dev_id, &cdi);
104
if (status != PJ_SUCCESS)
107
status = pjmedia_vid_dev_get_info(rend_dev_id, &rdi);
108
if (status != PJ_SUCCESS)
112
" %s (%s) ===> %s (%s)\t%s\t%dx%d\t@%d:%d fps",
113
cdi.name, cdi.driver, rdi.name, rdi.driver,
114
pjmedia_get_video_format_info(NULL, fmt->id)->name,
115
fmt->det.vid.size.w, fmt->det.vid.size.h,
116
fmt->det.vid.fps.num, fmt->det.vid.fps.denum));
118
pjmedia_vid_port_param_default(¶m);
120
/* Create capture, set it to active (master) */
121
status = pjmedia_vid_dev_default_param(pool, cap_dev_id,
123
if (status != PJ_SUCCESS) {
124
rc = 100; goto on_return;
126
param.vidparam.dir = PJMEDIA_DIR_CAPTURE;
127
param.vidparam.fmt = *fmt;
128
param.active = PJ_TRUE;
130
if (param.vidparam.fmt.detail_type != PJMEDIA_FORMAT_DETAIL_VIDEO) {
131
rc = 103; goto on_return;
134
vfd = pjmedia_format_get_video_format_detail(¶m.vidparam.fmt, PJ_TRUE);
136
rc = 105; goto on_return;
139
status = pjmedia_vid_port_create(pool, ¶m, &capture);
140
if (status != PJ_SUCCESS) {
141
rc = 110; goto on_return;
144
/* Create renderer, set it to passive (slave) */
145
status = pjmedia_vid_dev_default_param(pool, rend_dev_id,
147
if (status != PJ_SUCCESS) {
148
rc = 120; goto on_return;
151
param.active = PJ_FALSE;
152
param.vidparam.dir = PJMEDIA_DIR_RENDER;
153
param.vidparam.rend_id = rend_dev_id;
154
param.vidparam.fmt = *fmt;
155
param.vidparam.disp_size = vfd->size;
157
status = pjmedia_vid_port_create(pool, ¶m, &renderer);
158
if (status != PJ_SUCCESS) {
159
rc = 130; goto on_return;
162
/* Set event handler */
163
pjmedia_event_subscribe(NULL, &vid_event_cb, NULL, renderer);
165
/* Connect capture to renderer */
166
status = pjmedia_vid_port_connect(
168
pjmedia_vid_port_get_passive_port(renderer),
170
if (status != PJ_SUCCESS) {
171
rc = 140; goto on_return;
174
/* Start streaming.. */
175
status = pjmedia_vid_port_start(renderer);
176
if (status != PJ_SUCCESS) {
177
rc = 150; goto on_return;
179
status = pjmedia_vid_port_start(capture);
180
if (status != PJ_SUCCESS) {
181
rc = 160; goto on_return;
184
/* Sleep while the webcam is being displayed... */
185
for (i = 0; i < LOOP_DURATION*10 && (!is_quitting); i++) {
186
pj_thread_sleep(100);
190
if (status != PJ_SUCCESS)
191
PJ_PERROR(3, (THIS_FILE, status, " error"));
194
pjmedia_vid_port_stop(capture);
196
pjmedia_vid_port_stop(renderer);
198
pjmedia_vid_port_destroy(capture);
200
pjmedia_event_unsubscribe(NULL, &vid_event_cb, NULL, renderer);
201
pjmedia_vid_port_destroy(renderer);
204
pj_pool_release(pool);
208
static int loopback_test(void)
211
pjmedia_format_id test_fmts[] = {
214
pjmedia_rect_size test_sizes[] = {
215
{176,144}, /* QCIF */
219
pjmedia_ratio test_fpses[] = {
225
PJ_LOG(3, (THIS_FILE, " Loopback tests (prepare you webcams):"));
227
count = pjmedia_vid_dev_count();
228
for (i=0; i<count; ++i) {
229
pjmedia_vid_dev_info cdi;
232
status = pjmedia_vid_dev_get_info(i, &cdi);
233
if (status != PJ_SUCCESS)
236
/* Only interested with capture device */
237
if ((cdi.dir & PJMEDIA_DIR_CAPTURE) == 0)
240
for (j=i+1; j<count; ++j) {
241
pjmedia_vid_dev_info rdi;
244
status = pjmedia_vid_dev_get_info(j, &rdi);
245
if (status != PJ_SUCCESS)
248
/* Only interested with render device */
249
if ((rdi.dir & PJMEDIA_DIR_RENDER) == 0)
252
/* Test with the format, size, and fps combinations */
253
for (k=0; k<PJ_ARRAY_SIZE(test_fmts); ++k) {
256
for (l=0; l<PJ_ARRAY_SIZE(test_sizes); ++l) {
259
for (m=0; m<PJ_ARRAY_SIZE(test_fpses); ++m) {
262
pjmedia_format_init_video(&fmt, test_fmts[k],
266
test_fpses[m].denum);
268
capture_render_loopback(i, j, &fmt);
279
int vid_dev_test(void)
284
status = pjmedia_vid_dev_subsys_init(mem);
285
if (status != PJ_SUCCESS)
292
rc = loopback_test();
297
pjmedia_vid_dev_subsys_shutdown();
303
#endif /* PJMEDIA_HAS_VIDEO */