blob: c52a4ab5806b0efb2556f28ca5c3b5ee933bc15d [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
Sauw Ming6e6c2152010-12-14 13:03:10 +000026#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
27# include "TargetConditionals.h"
28# if !TARGET_OS_IPHONE
29# define VID_DEV_TEST_MAC_OS 1
30# endif
31#endif
32
33#if VID_DEV_TEST_MAC_OS
34# include <Foundation/NSAutoreleasePool.h>
35# include <AppKit/NSApplication.h>
36#endif
37
Benny Prijonoc45d9512010-12-10 11:04:30 +000038#define THIS_FILE "vid_dev_test.c"
39
Sauw Mingb1b17d22010-12-20 11:02:48 +000040static pj_bool_t is_quitting = PJ_FALSE;
Benny Prijonoc45d9512010-12-10 11:04:30 +000041
42static int enum_devs(void)
43{
44 unsigned i, dev_cnt;
45 pj_status_t status;
46
47 PJ_LOG(3, (THIS_FILE, " device enums"));
48 dev_cnt = pjmedia_vid_dev_count();
49 for (i = 0; i < dev_cnt; ++i) {
50 pjmedia_vid_dev_info info;
51 status = pjmedia_vid_dev_get_info(i, &info);
52 if (status == PJ_SUCCESS) {
53 PJ_LOG(3, (THIS_FILE, "%3d: %s - %s", i, info.driver, info.name));
54 }
55 }
56
57 return PJ_SUCCESS;
58}
59
Sauw Mingb1b17d22010-12-20 11:02:48 +000060static pj_status_t vid_event_cb(pjmedia_vid_stream *stream,
Benny Prijonoc45d9512010-12-10 11:04:30 +000061 void *user_data,
62 pjmedia_vid_event *event)
63{
Benny Prijonoc45d9512010-12-10 11:04:30 +000064 PJ_UNUSED_ARG(stream);
Sauw Mingb1b17d22010-12-20 11:02:48 +000065 PJ_UNUSED_ARG(user_data);
Sauw Ming6e6c2152010-12-14 13:03:10 +000066
Sauw Mingb1b17d22010-12-20 11:02:48 +000067 if (event->event_type == PJMEDIA_EVENT_WINDOW_CLOSE)
68 is_quitting = PJ_TRUE;
Benny Prijonoc45d9512010-12-10 11:04:30 +000069
Sauw Mingb1b17d22010-12-20 11:02:48 +000070 /* We will handle the event on our own, so return non-PJ_SUCCESS here */
71 return -1;
Benny Prijonoc45d9512010-12-10 11:04:30 +000072}
73
74static int loopback_test(pj_pool_t *pool)
75{
76 pjmedia_vid_port *capture=NULL, *renderer=NULL;
77 pjmedia_vid_port_param param;
78 pjmedia_video_format_detail *vfd;
Sauw Mingb1b17d22010-12-20 11:02:48 +000079 pjmedia_vid_cb cb;
Benny Prijonoc45d9512010-12-10 11:04:30 +000080 pj_status_t status;
Sauw Mingb1b17d22010-12-20 11:02:48 +000081 int rc = 0, i;
Benny Prijonoc45d9512010-12-10 11:04:30 +000082
83 PJ_LOG(3, (THIS_FILE, " loopback test"));
84
85 pjmedia_vid_port_param_default(&param);
86
87 /* Create capture, set it to active (master) */
88 status = pjmedia_vid_dev_default_param(pool,
89 PJMEDIA_VID_DEFAULT_CAPTURE_DEV,
Benny Prijonoc45d9512010-12-10 11:04:30 +000090 &param.vidparam);
91 if (status != PJ_SUCCESS) {
92 rc = 100; goto on_return;
93 }
94 param.vidparam.dir = PJMEDIA_DIR_CAPTURE;
95 param.active = PJ_TRUE;
96
97 if (param.vidparam.fmt.detail_type != PJMEDIA_FORMAT_DETAIL_VIDEO) {
98 rc = 103; goto on_return;
99 }
100
101 vfd = pjmedia_format_get_video_format_detail(&param.vidparam.fmt, PJ_TRUE);
102 if (vfd == NULL) {
103 rc = 105; goto on_return;
104 }
105
106 status = pjmedia_vid_port_create(pool, &param, &capture);
107 if (status != PJ_SUCCESS) {
108 rc = 110; goto on_return;
109 }
110
111 /* Create renderer, set it to passive (slave) */
112 param.active = PJ_FALSE;
113 param.vidparam.dir = PJMEDIA_DIR_RENDER;
114 param.vidparam.rend_id = PJMEDIA_VID_DEFAULT_RENDER_DEV;
Benny Prijonoc45d9512010-12-10 11:04:30 +0000115 param.vidparam.disp_size = vfd->size;
116
117 status = pjmedia_vid_port_create(pool, &param, &renderer);
118 if (status != PJ_SUCCESS) {
119 rc = 130; goto on_return;
120 }
121
Sauw Mingb1b17d22010-12-20 11:02:48 +0000122 pj_bzero(&cb, sizeof(cb));
123 cb.on_event_cb = vid_event_cb;
124 pjmedia_vid_port_set_cb(renderer, &cb, NULL);
125
Benny Prijonoc45d9512010-12-10 11:04:30 +0000126 /* Connect capture to renderer */
Sauw Mingb1b17d22010-12-20 11:02:48 +0000127 status = pjmedia_vid_port_connect(
128 capture,
129 pjmedia_vid_port_get_passive_port(renderer),
130 PJ_FALSE);
Benny Prijonoc45d9512010-12-10 11:04:30 +0000131 if (status != PJ_SUCCESS) {
132 rc = 140; goto on_return;
133 }
134
135 /* Start streaming.. */
136 status = pjmedia_vid_port_start(renderer);
137 if (status != PJ_SUCCESS) {
138 rc = 150; goto on_return;
139 }
140 status = pjmedia_vid_port_start(capture);
141 if (status != PJ_SUCCESS) {
142 rc = 160; goto on_return;
143 }
144
145 /* Sleep while the webcam is being displayed... */
Sauw Mingb1b17d22010-12-20 11:02:48 +0000146 for (i = 0; i < 50 && (!is_quitting); i++) {
147#if VID_DEV_TEST_MAC_OS
148 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false);
149#endif
150 pj_thread_sleep(100);
151 }
Benny Prijonoc45d9512010-12-10 11:04:30 +0000152
153on_return:
154 PJ_PERROR(3, (THIS_FILE, status, " error"));
155 if (capture)
156 pjmedia_vid_port_destroy(capture);
157 if (renderer)
158 pjmedia_vid_port_destroy(renderer);
159
160 return rc;
161}
162
163int vid_dev_test(void)
164{
165 pj_pool_t *pool;
166 int rc = 0;
167 pj_status_t status;
168
Sauw Ming6e6c2152010-12-14 13:03:10 +0000169#if VID_DEV_TEST_MAC_OS
170 NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
171
172 [NSApplication sharedApplication];
173#endif
174
Benny Prijonoc45d9512010-12-10 11:04:30 +0000175 PJ_LOG(3, (THIS_FILE, "Video device tests.."));
176
177 pool = pj_pool_create(mem, "Viddev test", 256, 256, 0);
178
179 status = pjmedia_vid_subsys_init(mem);
180 if (status != PJ_SUCCESS)
181 return -10;
182
Benny Prijonoc45d9512010-12-10 11:04:30 +0000183 rc = enum_devs();
184 if (rc != 0)
185 goto on_return;
186
Benny Prijonoc45d9512010-12-10 11:04:30 +0000187 rc = loopback_test(pool);
188 if (rc != 0)
189 goto on_return;
190
191on_return:
Benny Prijonoc45d9512010-12-10 11:04:30 +0000192 pjmedia_vid_subsys_shutdown();
193 pj_pool_release(pool);
194
Sauw Ming6e6c2152010-12-14 13:03:10 +0000195#if VID_DEV_TEST_MAC_OS
196 [apool release];
197#endif
198
Benny Prijonoc45d9512010-12-10 11:04:30 +0000199 return rc;
200}