Debian Codegraph @

Xpjproject/pjmedia/src/test/vid_dev_test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 "test.h"
20 #include <pjmedia-audiodev/audiodev.h>
21 #include <pjmedia-codec/ffmpeg_vid_codecs.h>
22 #include <pjmedia/vid_codec.h>
23 #include <pjmedia_videodev.h>
24
25
26 #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
27
28
29 #define THIS_FILE "vid_dev_test.c"
30 #define LOOP_DURATION 10
31
32 static pj_bool_t is_quitting = PJ_FALSE;
33
34 static const char *vid_dir_name(pjmedia_dir dir)
35 {
36 switch (dir) {
37 case PJMEDIA_DIR_CAPTURE:
38 return "capture";
39 case PJMEDIA_DIR_RENDER:
40 return "render";
41 case PJMEDIA_DIR_CAPTURE_RENDER:
42 return "capture & render";
43 default:
44 return "unknown";
45 }
46 }
47
48 static int enum_devs(void)
49 {
50 unsigned i, dev_cnt;
51 pj_status_t status;
52
53 PJ_LOG(3, (THIS_FILE, " Enum video devices:"));
54 dev_cnt = pjmedia_vid_dev_count();
55 for (i = 0; i < dev_cnt; ++i) {
56 pjmedia_vid_dev_info di;
57 status = pjmedia_vid_dev_get_info(i, &di);
58 if (status == PJ_SUCCESS) {
59 unsigned j;
60
61 PJ_LOG(3, (THIS_FILE, " %3d: %s (%s) - %s", i, di.name, di.driver,
62 vid_dir_name(di.dir)));
63
64 PJ_LOG(3,(THIS_FILE, " Supported formats:"));
65 for (j=0; j<di.fmt_cnt; ++j) {
66 const pjmedia_video_format_info *vfi;
67
68 vfi = pjmedia_get_video_format_info(NULL, di.fmt[j].id);
69 PJ_LOG(3,(THIS_FILE, " %s",
70 (vfi ? vfi->name : "unknown")));
71 }
72 }
73 }
74
75 return PJ_SUCCESS;
76 }
77
78 static pj_status_t vid_event_cb(pjmedia_event *event,
79 void *user_data)
80 {
81 PJ_UNUSED_ARG(user_data);
82
83 if (event->type == PJMEDIA_EVENT_WND_CLOSED)
84 is_quitting = PJ_TRUE;
85
86 return PJ_SUCCESS;
87 }
88
89 static int capture_render_loopback(int cap_dev_id, int rend_dev_id,
90 const pjmedia_format *fmt)
91 {
92 pj_pool_t *pool;
93 pjmedia_vid_port *capture=NULL, *renderer=NULL;
94 pjmedia_vid_dev_info cdi, rdi;
95 pjmedia_vid_port_param param;
96 pjmedia_video_format_detail *vfd;
97 pj_status_t status;
98 int rc = 0, i;
99
100 pool = pj_pool_create(mem, "vidloop", 1000, 1000, NULL);
101
102 status = pjmedia_vid_dev_get_info(cap_dev_id, &cdi);
103 if (status != PJ_SUCCESS)
104 goto on_return;
105
106 status = pjmedia_vid_dev_get_info(rend_dev_id, &rdi);
107 if (status != PJ_SUCCESS)
108 goto on_return;
109
110 PJ_LOG(3,(THIS_FILE,
111 " %s (%s) ===> %s (%s)\t%s\t%dx%d\t@%d:%d fps",
112 cdi.name, cdi.driver, rdi.name, rdi.driver,
113 pjmedia_get_video_format_info(NULL, fmt->id)->name,
114 fmt->det.vid.size.w, fmt->det.vid.size.h,
115 fmt->det.vid.fps.num, fmt->det.vid.fps.denum));
116
117 pjmedia_vid_port_param_default(&param);
118
119 /* Create capture, set it to active (master) */
120 status = pjmedia_vid_dev_default_param(pool, cap_dev_id,
121 &param.vidparam);
122 if (status != PJ_SUCCESS) {
123 rc = 100; goto on_return;
124 }
125 param.vidparam.dir = PJMEDIA_DIR_CAPTURE;
126 param.vidparam.fmt = *fmt;
127 param.active = PJ_TRUE;
128
129 if (param.vidparam.fmt.detail_type != PJMEDIA_FORMAT_DETAIL_VIDEO) {
130 rc = 103; goto on_return;
131 }
132
133 vfd = pjmedia_format_get_video_format_detail(&param.vidparam.fmt, PJ_TRUE);
134 if (vfd == NULL) {
135 rc = 105; goto on_return;
136 }
137
138 status = pjmedia_vid_port_create(pool, &param, &capture);
139 if (status != PJ_SUCCESS) {
140 rc = 110; goto on_return;
141 }
142
143 /* Create renderer, set it to passive (slave) */
144 status = pjmedia_vid_dev_default_param(pool, rend_dev_id,
145 &param.vidparam);
146 if (status != PJ_SUCCESS) {
147 rc = 120; goto on_return;
148 }
149
150 param.active = PJ_FALSE;
151 param.vidparam.dir = PJMEDIA_DIR_RENDER;
152 param.vidparam.rend_id = rend_dev_id;
153 param.vidparam.fmt = *fmt;
154 param.vidparam.disp_size = vfd->size;
155
156 status = pjmedia_vid_port_create(pool, &param, &renderer);
157 if (status != PJ_SUCCESS) {
158 rc = 130; goto on_return;
159 }
160
161 /* Set event handler */
162 pjmedia_event_subscribe(NULL, &vid_event_cb, NULL, renderer);
163
164 /* Connect capture to renderer */
165 status = pjmedia_vid_port_connect(
166 capture,
167 pjmedia_vid_port_get_passive_port(renderer),
168 PJ_FALSE);
169 if (status != PJ_SUCCESS) {
170 rc = 140; goto on_return;
171 }
172
173 /* Start streaming.. */
174 status = pjmedia_vid_port_start(renderer);
175 if (status != PJ_SUCCESS) {
176 rc = 150; goto on_return;
177 }
178 status = pjmedia_vid_port_start(capture);
179 if (status != PJ_SUCCESS) {
180 rc = 160; goto on_return;
181 }
182
183 /* Sleep while the webcam is being displayed... */
184 for (i = 0; i < LOOP_DURATION*10 && (!is_quitting); i++) {
185 pj_thread_sleep(100);
186 }
187
188 on_return:
189 if (status != PJ_SUCCESS)
190 PJ_PERROR(3, (THIS_FILE, status, " error"));
191
192 if (capture)
193 pjmedia_vid_port_stop(capture);
194 if (renderer)
195 pjmedia_vid_port_stop(renderer);
196 if (capture)
197 pjmedia_vid_port_destroy(capture);
198 if (renderer) {
199 pjmedia_event_unsubscribe(NULL, &vid_event_cb, NULL, renderer);
200 pjmedia_vid_port_destroy(renderer);
201 }
202
203 pj_pool_release(pool);
204 return rc;
205 }
206
207 static int loopback_test(void)
208 {
209 unsigned count, i;
210 pjmedia_format_id test_fmts[] = {
211 #if defined(PJ_DARWINOS) && PJ_DARWINOS
212 PJMEDIA_FORMAT_I420
213 #else
214 PJMEDIA_FORMAT_YUY2
215 #endif
216 };
217 pjmedia_rect_size test_sizes[] = {
218 {176,144}, /* QCIF */
219 {352,288}, /* CIF */
220 {704,576} /* 4CIF */
221 };
222 pjmedia_ratio test_fpses[] = {
223 {25, 1},
224 {30, 1},
225 };
226 pj_status_t status;
227
228 PJ_LOG(3, (THIS_FILE, " Loopback tests (prepare you webcams):"));
229
230 count = pjmedia_vid_dev_count();
231 for (i=0; i<count; ++i) {
232 pjmedia_vid_dev_info cdi;
233 unsigned j;
234
235 status = pjmedia_vid_dev_get_info(i, &cdi);
236 if (status != PJ_SUCCESS)
237 return -300;
238
239 /* Only interested with capture device */
240 if ((cdi.dir & PJMEDIA_DIR_CAPTURE) == 0)
241 continue;
242
243 for (j=0; j<count; ++j) {
244 pjmedia_vid_dev_info rdi;
245 unsigned k;
246
247 status = pjmedia_vid_dev_get_info(j, &rdi);
248 if (status != PJ_SUCCESS)
249 return -310;
250
251 /* Only interested with render device */
252 if ((rdi.dir & PJMEDIA_DIR_RENDER) == 0)
253 continue;
254
255 /* Test with the format, size, and fps combinations */
256 for (k=0; k<PJ_ARRAY_SIZE(test_fmts); ++k) {
257 unsigned l;
258
259 for (l=0; l<PJ_ARRAY_SIZE(test_sizes); ++l) {
260 unsigned m;
261
262 for (m=0; m<PJ_ARRAY_SIZE(test_fpses); ++m) {
263 pjmedia_format fmt;
264
265 pjmedia_format_init_video(&fmt, test_fmts[k],
266 test_sizes[l].w,
267 test_sizes[l].h,
268 test_fpses[m].num,
269 test_fpses[m].denum);
270
271 capture_render_loopback(i, j, &fmt);
272 }
273 }
274 } /* k */
275
276 }
277 }
278
279 return 0;
280 }
281
282 int vid_dev_test(void)
283 {
284 int rc = 0;
285 pj_status_t status;
286
287 status = pjmedia_vid_dev_subsys_init(mem);
288 if (status != PJ_SUCCESS)
289 return -10;
290
291 rc = enum_devs();
292 if (rc != 0)
293 goto on_return;
294
295 rc = loopback_test();
296 if (rc != 0)
297 goto on_return;
298
299 on_return:
300 pjmedia_vid_dev_subsys_shutdown();
301
302 return rc;
303 }
304
305
306 #endif /* PJMEDIA_HAS_VIDEO */