blob: 69317dfde125acaf55f7b837bd44ae0bfe278ef3 [file] [log] [blame]
Benny Prijonoc45d9512010-12-10 11:04:30 +00001/* $Id$ */
2/*
3 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
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.
10 *
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.
15 *
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
19 */
20#include "test.h"
21#include <pjmedia-audiodev/audiodev.h>
22#include <pjmedia-codec/ffmpeg_codecs.h>
23#include <pjmedia/vid_codec.h>
24#include <pjmedia_videodev.h>
25
Benny Prijonof6bebc32011-03-25 08:23:12 +000026#define THIS_FILE "vid_dev_test.c"
27#define LOOP_DURATION 10
Benny Prijonoc45d9512010-12-10 11:04:30 +000028
Sauw Mingb1b17d22010-12-20 11:02:48 +000029static pj_bool_t is_quitting = PJ_FALSE;
Benny Prijonoc45d9512010-12-10 11:04:30 +000030
Benny Prijonof6bebc32011-03-25 08:23:12 +000031static const char *vid_dir_name(pjmedia_dir dir)
32{
33 switch (dir) {
34 case PJMEDIA_DIR_CAPTURE:
35 return "capture";
36 case PJMEDIA_DIR_RENDER:
37 return "render";
38 case PJMEDIA_DIR_CAPTURE_RENDER:
39 return "capture & render";
40 default:
41 return "unknown";
42 }
43}
44
Benny Prijonoc45d9512010-12-10 11:04:30 +000045static int enum_devs(void)
46{
47 unsigned i, dev_cnt;
48 pj_status_t status;
49
Benny Prijonof6bebc32011-03-25 08:23:12 +000050 PJ_LOG(3, (THIS_FILE, " Enum video devices:"));
Benny Prijonoc45d9512010-12-10 11:04:30 +000051 dev_cnt = pjmedia_vid_dev_count();
52 for (i = 0; i < dev_cnt; ++i) {
Benny Prijonof6bebc32011-03-25 08:23:12 +000053 pjmedia_vid_dev_info di;
54 status = pjmedia_vid_dev_get_info(i, &di);
Benny Prijonoc45d9512010-12-10 11:04:30 +000055 if (status == PJ_SUCCESS) {
Benny Prijonof6bebc32011-03-25 08:23:12 +000056 unsigned j;
57
58 PJ_LOG(3, (THIS_FILE, " %3d: %s (%s) - %s", i, di.name, di.driver,
59 vid_dir_name(di.dir)));
60
61 PJ_LOG(3,(THIS_FILE, " Supported formats:"));
62 for (j=0; j<di.fmt_cnt; ++j) {
63 const pjmedia_video_format_info *vfi;
64
65 vfi = pjmedia_get_video_format_info(NULL, di.fmt[j].id);
66 PJ_LOG(3,(THIS_FILE, " %s",
67 (vfi ? vfi->name : "unknown")));
68 }
Benny Prijonoc45d9512010-12-10 11:04:30 +000069 }
70 }
71
72 return PJ_SUCCESS;
73}
74
Nanang Izzuddin235e1b42011-02-28 18:59:47 +000075static pj_status_t vid_event_cb(pjmedia_vid_dev_stream *stream,
Benny Prijonoc45d9512010-12-10 11:04:30 +000076 void *user_data,
77 pjmedia_vid_event *event)
78{
Benny Prijonoc45d9512010-12-10 11:04:30 +000079 PJ_UNUSED_ARG(stream);
Sauw Mingb1b17d22010-12-20 11:02:48 +000080 PJ_UNUSED_ARG(user_data);
Sauw Ming6e6c2152010-12-14 13:03:10 +000081
Sauw Mingb1b17d22010-12-20 11:02:48 +000082 if (event->event_type == PJMEDIA_EVENT_WINDOW_CLOSE)
83 is_quitting = PJ_TRUE;
Benny Prijonoc45d9512010-12-10 11:04:30 +000084
Sauw Mingb1b17d22010-12-20 11:02:48 +000085 /* We will handle the event on our own, so return non-PJ_SUCCESS here */
86 return -1;
Benny Prijonoc45d9512010-12-10 11:04:30 +000087}
88
Benny Prijonof6bebc32011-03-25 08:23:12 +000089static int capture_render_loopback(int cap_dev_id, int rend_dev_id,
90 const pjmedia_format *fmt)
Benny Prijonoc45d9512010-12-10 11:04:30 +000091{
Benny Prijonof6bebc32011-03-25 08:23:12 +000092 pj_pool_t *pool;
Benny Prijonoc45d9512010-12-10 11:04:30 +000093 pjmedia_vid_port *capture=NULL, *renderer=NULL;
Benny Prijonof6bebc32011-03-25 08:23:12 +000094 pjmedia_vid_dev_info cdi, rdi;
Benny Prijonoc45d9512010-12-10 11:04:30 +000095 pjmedia_vid_port_param param;
96 pjmedia_video_format_detail *vfd;
Sauw Mingb1b17d22010-12-20 11:02:48 +000097 pjmedia_vid_cb cb;
Benny Prijonoc45d9512010-12-10 11:04:30 +000098 pj_status_t status;
Sauw Mingb1b17d22010-12-20 11:02:48 +000099 int rc = 0, i;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000100
Benny Prijonof6bebc32011-03-25 08:23:12 +0000101 pool = pj_pool_create(mem, "vidloop", 1000, 1000, NULL);
102
103 status = pjmedia_vid_dev_get_info(cap_dev_id, &cdi);
104 if (status != PJ_SUCCESS)
105 goto on_return;
106
107 status = pjmedia_vid_dev_get_info(rend_dev_id, &rdi);
108 if (status != PJ_SUCCESS)
109 goto on_return;
110
111 PJ_LOG(3,(THIS_FILE,
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));
Benny Prijonoc45d9512010-12-10 11:04:30 +0000117
118 pjmedia_vid_port_param_default(&param);
119
120 /* Create capture, set it to active (master) */
Benny Prijonof6bebc32011-03-25 08:23:12 +0000121 status = pjmedia_vid_dev_default_param(pool, cap_dev_id,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000122 &param.vidparam);
123 if (status != PJ_SUCCESS) {
124 rc = 100; goto on_return;
125 }
126 param.vidparam.dir = PJMEDIA_DIR_CAPTURE;
Benny Prijonof6bebc32011-03-25 08:23:12 +0000127 param.vidparam.fmt = *fmt;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000128 param.active = PJ_TRUE;
129
130 if (param.vidparam.fmt.detail_type != PJMEDIA_FORMAT_DETAIL_VIDEO) {
131 rc = 103; goto on_return;
132 }
133
134 vfd = pjmedia_format_get_video_format_detail(&param.vidparam.fmt, PJ_TRUE);
135 if (vfd == NULL) {
136 rc = 105; goto on_return;
137 }
138
139 status = pjmedia_vid_port_create(pool, &param, &capture);
140 if (status != PJ_SUCCESS) {
141 rc = 110; goto on_return;
142 }
143
144 /* Create renderer, set it to passive (slave) */
Benny Prijonof6bebc32011-03-25 08:23:12 +0000145 status = pjmedia_vid_dev_default_param(pool, rend_dev_id,
146 &param.vidparam);
147 if (status != PJ_SUCCESS) {
148 rc = 120; goto on_return;
149 }
150
Benny Prijonoc45d9512010-12-10 11:04:30 +0000151 param.active = PJ_FALSE;
152 param.vidparam.dir = PJMEDIA_DIR_RENDER;
Benny Prijonof6bebc32011-03-25 08:23:12 +0000153 param.vidparam.rend_id = rend_dev_id;
154 param.vidparam.fmt = *fmt;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000155 param.vidparam.disp_size = vfd->size;
156
157 status = pjmedia_vid_port_create(pool, &param, &renderer);
158 if (status != PJ_SUCCESS) {
159 rc = 130; goto on_return;
160 }
161
Benny Prijonof6bebc32011-03-25 08:23:12 +0000162 /* Set event handler */
Sauw Mingb1b17d22010-12-20 11:02:48 +0000163 pj_bzero(&cb, sizeof(cb));
164 cb.on_event_cb = vid_event_cb;
165 pjmedia_vid_port_set_cb(renderer, &cb, NULL);
166
Benny Prijonoc45d9512010-12-10 11:04:30 +0000167 /* Connect capture to renderer */
Sauw Mingb1b17d22010-12-20 11:02:48 +0000168 status = pjmedia_vid_port_connect(
169 capture,
170 pjmedia_vid_port_get_passive_port(renderer),
171 PJ_FALSE);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000172 if (status != PJ_SUCCESS) {
173 rc = 140; goto on_return;
174 }
175
176 /* Start streaming.. */
177 status = pjmedia_vid_port_start(renderer);
178 if (status != PJ_SUCCESS) {
179 rc = 150; goto on_return;
180 }
181 status = pjmedia_vid_port_start(capture);
182 if (status != PJ_SUCCESS) {
183 rc = 160; goto on_return;
184 }
185
186 /* Sleep while the webcam is being displayed... */
Benny Prijonof6bebc32011-03-25 08:23:12 +0000187 for (i = 0; i < LOOP_DURATION*10 && (!is_quitting); i++) {
188#if VID_DEV_TEST_MAC_OS
189 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false);
190#endif
Sauw Mingb1b17d22010-12-20 11:02:48 +0000191 pj_thread_sleep(100);
192 }
Benny Prijonoc45d9512010-12-10 11:04:30 +0000193
194on_return:
Benny Prijonof6bebc32011-03-25 08:23:12 +0000195 if (status != PJ_SUCCESS)
196 PJ_PERROR(3, (THIS_FILE, status, " error"));
197
Benny Prijonoc45d9512010-12-10 11:04:30 +0000198 if (capture)
199 pjmedia_vid_port_destroy(capture);
200 if (renderer)
201 pjmedia_vid_port_destroy(renderer);
202
Benny Prijonof6bebc32011-03-25 08:23:12 +0000203 pj_pool_release(pool);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000204 return rc;
205}
206
Benny Prijonof6bebc32011-03-25 08:23:12 +0000207static int loopback_test(void)
208{
209 unsigned count, i;
210 pjmedia_format_id test_fmts[] = {
211 PJMEDIA_FORMAT_YUY2
212 };
213 pjmedia_rect_size test_sizes[] = {
214 {176,144}, /* QCIF */
215 {352,288}, /* CIF */
216 {704,576} /* 4CIF */
217 };
218 pjmedia_ratio test_fpses[] = {
219 {25, 1},
220 {30, 1},
221 };
222 pj_status_t status;
223
224 PJ_LOG(3, (THIS_FILE, " Loopback tests (prepare you webcams):"));
225
226 count = pjmedia_vid_dev_count();
227 for (i=0; i<count; ++i) {
228 pjmedia_vid_dev_info cdi;
229 unsigned j;
230
231 status = pjmedia_vid_dev_get_info(i, &cdi);
232 if (status != PJ_SUCCESS)
233 return -300;
234
235 /* Only interested with capture device */
236 if ((cdi.dir & PJMEDIA_DIR_CAPTURE) == 0)
237 continue;
238
239 for (j=i+1; j<count; ++j) {
240 pjmedia_vid_dev_info rdi;
241 unsigned k;
242
243 status = pjmedia_vid_dev_get_info(j, &rdi);
244 if (status != PJ_SUCCESS)
245 return -310;
246
247 /* Only interested with render device */
248 if ((rdi.dir & PJMEDIA_DIR_RENDER) == 0)
249 continue;
250
251 /* Test with the format, size, and fps combinations */
252 for (k=0; k<PJ_ARRAY_SIZE(test_fmts); ++k) {
253 unsigned l;
254
255 for (l=0; l<PJ_ARRAY_SIZE(test_sizes); ++l) {
256 unsigned m;
257
258 for (m=0; m<PJ_ARRAY_SIZE(test_fpses); ++m) {
259 pjmedia_format fmt;
260
261 pjmedia_format_init_video(&fmt, test_fmts[k],
262 test_sizes[l].w,
263 test_sizes[l].h,
264 test_fpses[m].num,
265 test_fpses[m].denum);
266
267 capture_render_loopback(i, j, &fmt);
268 }
269 }
270 } /* k */
271
272 }
273 }
274
275 return 0;
276}
277
Benny Prijonoc45d9512010-12-10 11:04:30 +0000278int vid_dev_test(void)
279{
Benny Prijonoc45d9512010-12-10 11:04:30 +0000280 int rc = 0;
281 pj_status_t status;
Sauw Ming6e6c2152010-12-14 13:03:10 +0000282
Benny Prijonoc45d9512010-12-10 11:04:30 +0000283 status = pjmedia_vid_subsys_init(mem);
284 if (status != PJ_SUCCESS)
285 return -10;
286
Benny Prijonoc45d9512010-12-10 11:04:30 +0000287 rc = enum_devs();
288 if (rc != 0)
289 goto on_return;
290
Benny Prijonof6bebc32011-03-25 08:23:12 +0000291 rc = loopback_test();
Benny Prijonoc45d9512010-12-10 11:04:30 +0000292 if (rc != 0)
293 goto on_return;
294
295on_return:
Benny Prijonoc45d9512010-12-10 11:04:30 +0000296 pjmedia_vid_subsys_shutdown();
Sauw Ming6e6c2152010-12-14 13:03:10 +0000297
Benny Prijonoc45d9512010-12-10 11:04:30 +0000298 return rc;
299}