blob: 77d63a2728fb5cb3a75997eaf21836bc40ce0b5f [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 Mingd49b17e2011-06-06 11:11:35 +000085 return PJ_SUCCESS;
Benny Prijonoc45d9512010-12-10 11:04:30 +000086}
87
Benny Prijonof6bebc32011-03-25 08:23:12 +000088static int capture_render_loopback(int cap_dev_id, int rend_dev_id,
89 const pjmedia_format *fmt)
Benny Prijonoc45d9512010-12-10 11:04:30 +000090{
Benny Prijonof6bebc32011-03-25 08:23:12 +000091 pj_pool_t *pool;
Benny Prijonoc45d9512010-12-10 11:04:30 +000092 pjmedia_vid_port *capture=NULL, *renderer=NULL;
Benny Prijonof6bebc32011-03-25 08:23:12 +000093 pjmedia_vid_dev_info cdi, rdi;
Benny Prijonoc45d9512010-12-10 11:04:30 +000094 pjmedia_vid_port_param param;
95 pjmedia_video_format_detail *vfd;
Sauw Mingb1b17d22010-12-20 11:02:48 +000096 pjmedia_vid_cb cb;
Benny Prijonoc45d9512010-12-10 11:04:30 +000097 pj_status_t status;
Sauw Mingb1b17d22010-12-20 11:02:48 +000098 int rc = 0, i;
Benny Prijonoc45d9512010-12-10 11:04:30 +000099
Benny Prijonof6bebc32011-03-25 08:23:12 +0000100 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));
Benny Prijonoc45d9512010-12-10 11:04:30 +0000116
117 pjmedia_vid_port_param_default(&param);
118
119 /* Create capture, set it to active (master) */
Benny Prijonof6bebc32011-03-25 08:23:12 +0000120 status = pjmedia_vid_dev_default_param(pool, cap_dev_id,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000121 &param.vidparam);
122 if (status != PJ_SUCCESS) {
123 rc = 100; goto on_return;
124 }
125 param.vidparam.dir = PJMEDIA_DIR_CAPTURE;
Benny Prijonof6bebc32011-03-25 08:23:12 +0000126 param.vidparam.fmt = *fmt;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000127 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) */
Benny Prijonof6bebc32011-03-25 08:23:12 +0000144 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
Benny Prijonoc45d9512010-12-10 11:04:30 +0000150 param.active = PJ_FALSE;
151 param.vidparam.dir = PJMEDIA_DIR_RENDER;
Benny Prijonof6bebc32011-03-25 08:23:12 +0000152 param.vidparam.rend_id = rend_dev_id;
153 param.vidparam.fmt = *fmt;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000154 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
Benny Prijonof6bebc32011-03-25 08:23:12 +0000161 /* Set event handler */
Sauw Mingb1b17d22010-12-20 11:02:48 +0000162 pj_bzero(&cb, sizeof(cb));
163 cb.on_event_cb = vid_event_cb;
164 pjmedia_vid_port_set_cb(renderer, &cb, NULL);
165
Benny Prijonoc45d9512010-12-10 11:04:30 +0000166 /* Connect capture to renderer */
Sauw Mingb1b17d22010-12-20 11:02:48 +0000167 status = pjmedia_vid_port_connect(
168 capture,
169 pjmedia_vid_port_get_passive_port(renderer),
170 PJ_FALSE);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000171 if (status != PJ_SUCCESS) {
172 rc = 140; goto on_return;
173 }
174
175 /* Start streaming.. */
176 status = pjmedia_vid_port_start(renderer);
177 if (status != PJ_SUCCESS) {
178 rc = 150; goto on_return;
179 }
180 status = pjmedia_vid_port_start(capture);
181 if (status != PJ_SUCCESS) {
182 rc = 160; goto on_return;
183 }
184
185 /* Sleep while the webcam is being displayed... */
Benny Prijonof6bebc32011-03-25 08:23:12 +0000186 for (i = 0; i < LOOP_DURATION*10 && (!is_quitting); i++) {
Sauw Mingb1b17d22010-12-20 11:02:48 +0000187 pj_thread_sleep(100);
188 }
Benny Prijonoc45d9512010-12-10 11:04:30 +0000189
190on_return:
Benny Prijonof6bebc32011-03-25 08:23:12 +0000191 if (status != PJ_SUCCESS)
192 PJ_PERROR(3, (THIS_FILE, status, " error"));
193
Benny Prijonoc45d9512010-12-10 11:04:30 +0000194 if (capture)
195 pjmedia_vid_port_destroy(capture);
196 if (renderer)
197 pjmedia_vid_port_destroy(renderer);
198
Benny Prijonof6bebc32011-03-25 08:23:12 +0000199 pj_pool_release(pool);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000200 return rc;
201}
202
Benny Prijonof6bebc32011-03-25 08:23:12 +0000203static int loopback_test(void)
204{
205 unsigned count, i;
206 pjmedia_format_id test_fmts[] = {
207 PJMEDIA_FORMAT_YUY2
208 };
209 pjmedia_rect_size test_sizes[] = {
210 {176,144}, /* QCIF */
211 {352,288}, /* CIF */
212 {704,576} /* 4CIF */
213 };
214 pjmedia_ratio test_fpses[] = {
215 {25, 1},
216 {30, 1},
217 };
218 pj_status_t status;
219
220 PJ_LOG(3, (THIS_FILE, " Loopback tests (prepare you webcams):"));
221
222 count = pjmedia_vid_dev_count();
223 for (i=0; i<count; ++i) {
224 pjmedia_vid_dev_info cdi;
225 unsigned j;
226
227 status = pjmedia_vid_dev_get_info(i, &cdi);
228 if (status != PJ_SUCCESS)
229 return -300;
230
231 /* Only interested with capture device */
232 if ((cdi.dir & PJMEDIA_DIR_CAPTURE) == 0)
233 continue;
234
235 for (j=i+1; j<count; ++j) {
236 pjmedia_vid_dev_info rdi;
237 unsigned k;
238
239 status = pjmedia_vid_dev_get_info(j, &rdi);
240 if (status != PJ_SUCCESS)
241 return -310;
242
243 /* Only interested with render device */
244 if ((rdi.dir & PJMEDIA_DIR_RENDER) == 0)
245 continue;
246
247 /* Test with the format, size, and fps combinations */
248 for (k=0; k<PJ_ARRAY_SIZE(test_fmts); ++k) {
249 unsigned l;
250
251 for (l=0; l<PJ_ARRAY_SIZE(test_sizes); ++l) {
252 unsigned m;
253
254 for (m=0; m<PJ_ARRAY_SIZE(test_fpses); ++m) {
255 pjmedia_format fmt;
256
257 pjmedia_format_init_video(&fmt, test_fmts[k],
258 test_sizes[l].w,
259 test_sizes[l].h,
260 test_fpses[m].num,
261 test_fpses[m].denum);
262
263 capture_render_loopback(i, j, &fmt);
264 }
265 }
266 } /* k */
267
268 }
269 }
270
271 return 0;
272}
273
Benny Prijonoc45d9512010-12-10 11:04:30 +0000274int vid_dev_test(void)
275{
Benny Prijonoc45d9512010-12-10 11:04:30 +0000276 int rc = 0;
277 pj_status_t status;
Sauw Ming6e6c2152010-12-14 13:03:10 +0000278
Benny Prijonoc45d9512010-12-10 11:04:30 +0000279 status = pjmedia_vid_subsys_init(mem);
280 if (status != PJ_SUCCESS)
281 return -10;
282
Benny Prijonoc45d9512010-12-10 11:04:30 +0000283 rc = enum_devs();
284 if (rc != 0)
285 goto on_return;
286
Benny Prijonof6bebc32011-03-25 08:23:12 +0000287 rc = loopback_test();
Benny Prijonoc45d9512010-12-10 11:04:30 +0000288 if (rc != 0)
289 goto on_return;
290
291on_return:
Benny Prijonoc45d9512010-12-10 11:04:30 +0000292 pjmedia_vid_subsys_shutdown();
Sauw Ming6e6c2152010-12-14 13:03:10 +0000293
Benny Prijonoc45d9512010-12-10 11:04:30 +0000294 return rc;
295}