Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 1 | /* $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 Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 26 | #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 Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 38 | #define THIS_FILE "vid_dev_test.c" |
| 39 | |
Sauw Ming | b1b17d2 | 2010-12-20 11:02:48 +0000 | [diff] [blame] | 40 | static pj_bool_t is_quitting = PJ_FALSE; |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 41 | |
| 42 | static 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 Ming | b1b17d2 | 2010-12-20 11:02:48 +0000 | [diff] [blame] | 60 | static pj_status_t vid_event_cb(pjmedia_vid_stream *stream, |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 61 | void *user_data, |
| 62 | pjmedia_vid_event *event) |
| 63 | { |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 64 | PJ_UNUSED_ARG(stream); |
Sauw Ming | b1b17d2 | 2010-12-20 11:02:48 +0000 | [diff] [blame] | 65 | PJ_UNUSED_ARG(user_data); |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 66 | |
Sauw Ming | b1b17d2 | 2010-12-20 11:02:48 +0000 | [diff] [blame] | 67 | if (event->event_type == PJMEDIA_EVENT_WINDOW_CLOSE) |
| 68 | is_quitting = PJ_TRUE; |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 69 | |
Sauw Ming | b1b17d2 | 2010-12-20 11:02:48 +0000 | [diff] [blame] | 70 | /* We will handle the event on our own, so return non-PJ_SUCCESS here */ |
| 71 | return -1; |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static 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 Ming | b1b17d2 | 2010-12-20 11:02:48 +0000 | [diff] [blame] | 79 | pjmedia_vid_cb cb; |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 80 | pj_status_t status; |
Sauw Ming | b1b17d2 | 2010-12-20 11:02:48 +0000 | [diff] [blame] | 81 | int rc = 0, i; |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 82 | |
| 83 | PJ_LOG(3, (THIS_FILE, " loopback test")); |
| 84 | |
| 85 | pjmedia_vid_port_param_default(¶m); |
| 86 | |
| 87 | /* Create capture, set it to active (master) */ |
| 88 | status = pjmedia_vid_dev_default_param(pool, |
| 89 | PJMEDIA_VID_DEFAULT_CAPTURE_DEV, |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 90 | ¶m.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(¶m.vidparam.fmt, PJ_TRUE); |
| 102 | if (vfd == NULL) { |
| 103 | rc = 105; goto on_return; |
| 104 | } |
| 105 | |
| 106 | status = pjmedia_vid_port_create(pool, ¶m, &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 Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 115 | param.vidparam.disp_size = vfd->size; |
| 116 | |
| 117 | status = pjmedia_vid_port_create(pool, ¶m, &renderer); |
| 118 | if (status != PJ_SUCCESS) { |
| 119 | rc = 130; goto on_return; |
| 120 | } |
| 121 | |
Sauw Ming | b1b17d2 | 2010-12-20 11:02:48 +0000 | [diff] [blame] | 122 | pj_bzero(&cb, sizeof(cb)); |
| 123 | cb.on_event_cb = vid_event_cb; |
| 124 | pjmedia_vid_port_set_cb(renderer, &cb, NULL); |
| 125 | |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 126 | /* Connect capture to renderer */ |
Sauw Ming | b1b17d2 | 2010-12-20 11:02:48 +0000 | [diff] [blame] | 127 | status = pjmedia_vid_port_connect( |
| 128 | capture, |
| 129 | pjmedia_vid_port_get_passive_port(renderer), |
| 130 | PJ_FALSE); |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 131 | 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 Ming | b1b17d2 | 2010-12-20 11:02:48 +0000 | [diff] [blame] | 146 | 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 Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 152 | |
| 153 | on_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 | |
| 163 | int vid_dev_test(void) |
| 164 | { |
| 165 | pj_pool_t *pool; |
| 166 | int rc = 0; |
| 167 | pj_status_t status; |
| 168 | |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 169 | #if VID_DEV_TEST_MAC_OS |
| 170 | NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init]; |
| 171 | |
| 172 | [NSApplication sharedApplication]; |
| 173 | #endif |
| 174 | |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 175 | 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 Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 183 | rc = enum_devs(); |
| 184 | if (rc != 0) |
| 185 | goto on_return; |
| 186 | |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 187 | rc = loopback_test(pool); |
| 188 | if (rc != 0) |
| 189 | goto on_return; |
| 190 | |
| 191 | on_return: |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 192 | pjmedia_vid_subsys_shutdown(); |
| 193 | pj_pool_release(pool); |
| 194 | |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 195 | #if VID_DEV_TEST_MAC_OS |
| 196 | [apool release]; |
| 197 | #endif |
| 198 | |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 199 | return rc; |
| 200 | } |