Debian Codegraph @

Xpjproject/pjmedia/src/test/vid_port_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 6
31
32 static pj_bool_t is_quitting = PJ_FALSE;
33
34 static pj_status_t vid_event_cb(pjmedia_event *event,
35 void *user_data)
36 {
37 PJ_UNUSED_ARG(user_data);
38
39 if (event->type == PJMEDIA_EVENT_WND_CLOSED)
40 is_quitting = PJ_TRUE;
41
42 return PJ_SUCCESS;
43 }
44
45 static int capture_render_loopback(pj_bool_t active,
46 int cap_dev_id, int rend_dev_id,
47 const pjmedia_format *fmt)
48 {
49 pj_pool_t *pool;
50 pjmedia_vid_port *capture=NULL, *renderer=NULL;
51 pjmedia_vid_dev_info cdi, rdi;
52 pjmedia_vid_port_param param;
53 pjmedia_video_format_detail *vfd;
54 pj_status_t status;
55 int rc = 0, i;
56
57 pool = pj_pool_create(mem, "vidportloop", 1000, 1000, NULL);
58
59 status = pjmedia_vid_dev_get_info(cap_dev_id, &cdi);
60 if (status != PJ_SUCCESS)
61 goto on_return;
62
63 status = pjmedia_vid_dev_get_info(rend_dev_id, &rdi);
64 if (status != PJ_SUCCESS)
65 goto on_return;
66
67 PJ_LOG(3,(THIS_FILE,
68 " %s (%s) ===> %s (%s)\t%s\t%dx%d\t@%d:%d fps",
69 cdi.name, cdi.driver, rdi.name, rdi.driver,
70 pjmedia_get_video_format_info(NULL, fmt->id)->name,
71 fmt->det.vid.size.w, fmt->det.vid.size.h,
72 fmt->det.vid.fps.num, fmt->det.vid.fps.denum));
73
74 pjmedia_vid_port_param_default(&param);
75
76 /* Create capture, set it to active (master) */
77 status = pjmedia_vid_dev_default_param(pool, cap_dev_id,
78 &param.vidparam);
79 if (status != PJ_SUCCESS) {
80 rc = 100; goto on_return;
81 }
82 param.vidparam.dir = PJMEDIA_DIR_CAPTURE;
83 param.vidparam.fmt = *fmt;
84 param.active = (active? PJ_TRUE: PJ_FALSE);
85
86 if (param.vidparam.fmt.detail_type != PJMEDIA_FORMAT_DETAIL_VIDEO) {
87 rc = 103; goto on_return;
88 }
89
90 vfd = pjmedia_format_get_video_format_detail(&param.vidparam.fmt, PJ_TRUE);
91 if (vfd == NULL) {
92 rc = 105; goto on_return;
93 }
94
95 status = pjmedia_vid_port_create(pool, &param, &capture);
96 if (status != PJ_SUCCESS) {
97 rc = 110; goto on_return;
98 }
99
100 /* Create renderer, set it to passive (slave) */
101 status = pjmedia_vid_dev_default_param(pool, rend_dev_id,
102 &param.vidparam);
103 if (status != PJ_SUCCESS) {
104 rc = 120; goto on_return;
105 }
106
107 param.active = (active? PJ_FALSE: PJ_TRUE);
108 param.vidparam.dir = PJMEDIA_DIR_RENDER;
109 param.vidparam.rend_id = rend_dev_id;
110 param.vidparam.fmt = *fmt;
111 param.vidparam.disp_size = vfd->size;
112
113 status = pjmedia_vid_port_create(pool, &param, &renderer);
114 if (status != PJ_SUCCESS) {
115 rc = 130; goto on_return;
116 }
117
118 /* Set event handler */
119 pjmedia_event_subscribe(NULL, &vid_event_cb, NULL, renderer);
120
121 /* Connect capture to renderer */
122 status = pjmedia_vid_port_connect(
123 (active? capture: renderer),
124 pjmedia_vid_port_get_passive_port(active? renderer: capture),
125 PJ_FALSE);
126 if (status != PJ_SUCCESS) {
127 rc = 140; goto on_return;
128 }
129
130 /* Start streaming.. */
131 status = pjmedia_vid_port_start(renderer);
132 if (status != PJ_SUCCESS) {
133 rc = 150; goto on_return;
134 }
135 status = pjmedia_vid_port_start(capture);
136 if (status != PJ_SUCCESS) {
137 rc = 160; goto on_return;
138 }
139
140 /* Sleep while the webcam is being displayed... */
141 for (i = 0; i < LOOP_DURATION*10 && (!is_quitting); i++) {
142 pj_thread_sleep(100);
143 }
144
145 on_return:
146 if (status != PJ_SUCCESS)
147 PJ_PERROR(3, (THIS_FILE, status, " error"));
148
149 if (capture)
150 pjmedia_vid_port_stop(capture);
151 if (renderer)
152 pjmedia_vid_port_stop(renderer);
153 if (capture)
154 pjmedia_vid_port_destroy(capture);
155 if (renderer) {
156 pjmedia_event_unsubscribe(NULL, &vid_event_cb, NULL, renderer);
157 pjmedia_vid_port_destroy(renderer);
158 }
159
160 pj_pool_release(pool);
161 return rc;
162 }
163
164 static int find_device(pjmedia_dir dir,
165 pj_bool_t has_callback)
166 {
167 unsigned i, count = pjmedia_vid_dev_count();
168
169 for (i = 0; i < count; ++i) {
170 pjmedia_vid_dev_info cdi;
171
172 if (pjmedia_vid_dev_get_info(i, &cdi) != PJ_SUCCESS)
173 continue;
174 if ((cdi.dir & dir) != 0 && cdi.has_callback == has_callback)
175 return i;
176 }
177
178 return -999;
179 }
180
181 static int vidport_test(void)
182 {
183 int i, j, k, l;
184 int cap_id, rend_id;
185 pjmedia_format_id test_fmts[] = {
186 PJMEDIA_FORMAT_RGBA,
187 PJMEDIA_FORMAT_I420
188 };
189
190 PJ_LOG(3, (THIS_FILE, " Video port tests:"));
191
192 /* Capturer's role: active/passive. */
193 for (i = 1; i >= 0; i--) {
194 /* Capturer's device has_callback: TRUE/FALSE. */
195 for (j = 1; j >= 0; j--) {
196 cap_id = find_device(PJMEDIA_DIR_CAPTURE, j);
197 if (cap_id < 0)
198 continue;
199
200 /* Renderer's device has callback: TRUE/FALSE. */
201 for (k = 1; k >= 0; k--) {
202 rend_id = find_device(PJMEDIA_DIR_RENDER, k);
203 if (rend_id < 0)
204 continue;
205
206 /* Check various formats to test format conversion. */
207 for (l = 0; l < (int)PJ_ARRAY_SIZE(test_fmts); ++l) {
208 pjmedia_format fmt;
209
210 PJ_LOG(3,(THIS_FILE,
211 "capturer %s (stream: %s) ===> "
212 "renderer %s (stream: %s)",
213 (i? "active": "passive"),
214 (j? "active": "passive"),
215 (i? "passive": "active"),
216 (k? "active": "passive")));
217
218 pjmedia_format_init_video(&fmt, test_fmts[l],
219 640, 480, 25, 1);
220 capture_render_loopback(i, cap_id, rend_id, &fmt);
221 }
222 }
223 }
224 }
225
226 return 0;
227 }
228
229 int vid_port_test(void)
230 {
231 int rc = 0;
232 pj_status_t status;
233
234 status = pjmedia_vid_dev_subsys_init(mem);
235 if (status != PJ_SUCCESS)
236 return -10;
237
238 rc = vidport_test();
239 if (rc != 0)
240 goto on_return;
241
242 on_return:
243 pjmedia_vid_dev_subsys_shutdown();
244
245 return rc;
246 }
247
248
249 #endif /* PJMEDIA_HAS_VIDEO */