Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 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 | |
| 20 | static const char *desc = |
| 21 | " sndinfo.c \n" |
| 22 | " \n" |
| 23 | " PURPOSE: \n" |
| 24 | " Print sound device info and test open device. \n" |
| 25 | " \n" |
| 26 | " USAGE: \n" |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 27 | " sndinfo [id rec/play/both clockrate nchan bits] \n" |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 28 | " \n" |
| 29 | " DESCRIPTION: \n" |
| 30 | " When invoked without any arguments, it displays information about all \n" |
| 31 | " sound devices in the system. \n" |
| 32 | " \n" |
| 33 | " When invoked with arguments, the program tests if device can be opened \n" |
| 34 | " with the specified arguments. All these arguments must be specified: \n" |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 35 | " - id The device ID (-1 for the first capable device) \n" |
| 36 | " - rec/play/both Specify which streams to open. \n" |
| 37 | " - clockrate Specify clock rate (e.g. 8000, 11025, etc.) \n" |
| 38 | " - nchan Number of channels (1=mono, 2=stereo). \n" |
| 39 | " - bits Number of bits per sample (normally 16). \n"; |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 40 | |
| 41 | #include <pjmedia.h> |
| 42 | #include <pjlib.h> |
| 43 | |
| 44 | #include <stdlib.h> /* atoi() */ |
| 45 | #include <stdio.h> |
| 46 | |
| 47 | |
| 48 | static void enum_devices(void) |
| 49 | { |
| 50 | int i, count; |
| 51 | |
| 52 | count = pjmedia_snd_get_dev_count(); |
| 53 | if (count == 0) { |
| 54 | puts("No devices found"); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | for (i=0; i<count; ++i) { |
| 59 | const pjmedia_snd_dev_info *info; |
| 60 | |
| 61 | info = pjmedia_snd_get_dev_info(i); |
| 62 | pj_assert(info != NULL); |
| 63 | |
| 64 | printf( "Device #%02d: \n" |
| 65 | " Name : %s\n" |
| 66 | " # of input channels : %d\n" |
| 67 | " # of output channels: %d\n" |
| 68 | " Default clock rate : %d Hz\n\n", |
| 69 | i, info->name, info->input_count, info->output_count, |
| 70 | info->default_samples_per_sec); |
| 71 | } |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 72 | puts(""); |
| 73 | puts("Run with -h to get more options"); |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 76 | static unsigned clock_rate; |
| 77 | static unsigned play_counter; |
| 78 | static unsigned rec_counter; |
| 79 | static unsigned min_delay = 0xFFFF, max_delay; |
Benny Prijono | e452dda | 2006-04-03 09:43:36 +0000 | [diff] [blame] | 80 | static char play_delays[1000]; |
Benny Prijono | 5012da8 | 2006-04-25 10:02:40 +0000 | [diff] [blame] | 81 | static pj_uint32_t last_play_timestamp, last_rec_timestamp; |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 82 | |
| 83 | static pj_status_t play_cb(void *user_data, pj_uint32_t timestamp, |
| 84 | void *output, unsigned size) |
| 85 | { |
Benny Prijono | c708f93 | 2006-04-03 11:42:56 +0000 | [diff] [blame] | 86 | static pj_timestamp last_cb; |
Benny Prijono | e452dda | 2006-04-03 09:43:36 +0000 | [diff] [blame] | 87 | |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 88 | |
| 89 | PJ_UNUSED_ARG(user_data); |
| 90 | PJ_UNUSED_ARG(output); |
| 91 | PJ_UNUSED_ARG(size); |
| 92 | |
| 93 | |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 94 | ++play_counter; |
Benny Prijono | 5012da8 | 2006-04-25 10:02:40 +0000 | [diff] [blame] | 95 | last_play_timestamp = timestamp; |
Benny Prijono | e452dda | 2006-04-03 09:43:36 +0000 | [diff] [blame] | 96 | |
Benny Prijono | c708f93 | 2006-04-03 11:42:56 +0000 | [diff] [blame] | 97 | if (last_cb.u64 == 0) { |
| 98 | pj_get_timestamp(&last_cb); |
Benny Prijono | 5012da8 | 2006-04-25 10:02:40 +0000 | [diff] [blame] | 99 | } else if (play_counter <= PJ_ARRAY_SIZE(play_delays)) { |
Benny Prijono | c708f93 | 2006-04-03 11:42:56 +0000 | [diff] [blame] | 100 | pj_timestamp now; |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 101 | unsigned delay; |
Benny Prijono | e452dda | 2006-04-03 09:43:36 +0000 | [diff] [blame] | 102 | |
Benny Prijono | c708f93 | 2006-04-03 11:42:56 +0000 | [diff] [blame] | 103 | pj_get_timestamp(&now); |
| 104 | |
| 105 | delay = pj_elapsed_msec(&last_cb, &now); |
Benny Prijono | e452dda | 2006-04-03 09:43:36 +0000 | [diff] [blame] | 106 | if (delay < min_delay) |
| 107 | min_delay = delay; |
| 108 | if (delay > max_delay) |
| 109 | max_delay = delay; |
| 110 | |
Benny Prijono | c708f93 | 2006-04-03 11:42:56 +0000 | [diff] [blame] | 111 | last_cb = now; |
Benny Prijono | e452dda | 2006-04-03 09:43:36 +0000 | [diff] [blame] | 112 | |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 113 | play_delays[play_counter-1] = (char)delay; |
Benny Prijono | e452dda | 2006-04-03 09:43:36 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 116 | return PJ_SUCCESS; |
| 117 | } |
| 118 | |
| 119 | static pj_status_t rec_cb(void *user_data, pj_uint32_t timestamp, |
Benny Prijono | f521eb0 | 2006-08-06 23:07:25 +0000 | [diff] [blame] | 120 | void *input, unsigned size) |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 121 | { |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 122 | |
| 123 | PJ_UNUSED_ARG(size); |
| 124 | PJ_UNUSED_ARG(input); |
| 125 | PJ_UNUSED_ARG(user_data); |
| 126 | |
| 127 | |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 128 | ++rec_counter; |
Benny Prijono | 5012da8 | 2006-04-25 10:02:40 +0000 | [diff] [blame] | 129 | |
Benny Prijono | ed8c0fc | 2006-05-14 21:29:53 +0000 | [diff] [blame] | 130 | if (timestamp - last_rec_timestamp >= clock_rate && last_play_timestamp) { |
Benny Prijono | 5012da8 | 2006-04-25 10:02:40 +0000 | [diff] [blame] | 131 | int diff; |
| 132 | diff = last_play_timestamp - timestamp; |
| 133 | printf("Play timestamp=%u, capture timestamp=%u, diff=%d\n", |
| 134 | last_play_timestamp, timestamp, diff); |
| 135 | last_rec_timestamp = timestamp; |
| 136 | } |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 137 | return PJ_SUCCESS; |
| 138 | } |
| 139 | |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 140 | static void app_perror(const char *title, pj_status_t status) |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 141 | { |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 142 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 143 | |
| 144 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 145 | printf( "%s: %s (err=%d)\n", |
| 146 | title, errmsg, status); |
| 147 | } |
Benny Prijono | e452dda | 2006-04-03 09:43:36 +0000 | [diff] [blame] | 148 | |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 149 | static int open_device(int dev_id, pjmedia_dir dir, |
Benny Prijono | 5012da8 | 2006-04-25 10:02:40 +0000 | [diff] [blame] | 150 | int nchannel, int bits) |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 151 | { |
| 152 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 153 | unsigned nsamples; |
| 154 | pjmedia_snd_stream *strm; |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 155 | const char *dirtype; |
Benny Prijono | 5012da8 | 2006-04-25 10:02:40 +0000 | [diff] [blame] | 156 | char tmp[10]; |
Benny Prijono | e452dda | 2006-04-03 09:43:36 +0000 | [diff] [blame] | 157 | unsigned i; |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 158 | |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 159 | switch (dir) { |
| 160 | case PJMEDIA_DIR_CAPTURE: |
| 161 | dirtype = "capture"; break; |
| 162 | case PJMEDIA_DIR_PLAYBACK: |
| 163 | dirtype = "playback"; break; |
| 164 | case PJMEDIA_DIR_CAPTURE_PLAYBACK: |
| 165 | dirtype = "capture/playback"; break; |
| 166 | default: |
| 167 | return 1; |
| 168 | } |
| 169 | |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 170 | nsamples = clock_rate * 20 / 1000; |
| 171 | |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 172 | printf( "Opening device %d for %s: clockrate=%d, nchannel=%d, " |
| 173 | "bits=%d, nsamples=%d..\n", |
| 174 | dev_id, dirtype, clock_rate, nchannel, bits, nsamples); |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 175 | |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 176 | if (dir == PJMEDIA_DIR_CAPTURE) { |
| 177 | status = pjmedia_snd_open_rec( dev_id, clock_rate, nchannel, |
| 178 | nsamples, bits, &rec_cb, NULL, |
| 179 | &strm); |
| 180 | } else if (dir == PJMEDIA_DIR_PLAYBACK) { |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 181 | status = pjmedia_snd_open_player( dev_id, clock_rate, nchannel, |
| 182 | nsamples, bits, &play_cb, NULL, |
| 183 | &strm); |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 184 | } else { |
| 185 | status = pjmedia_snd_open( dev_id, dev_id, clock_rate, nchannel, |
| 186 | nsamples, bits, &rec_cb, &play_cb, NULL, |
| 187 | &strm); |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if (status != PJ_SUCCESS) { |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 191 | app_perror("Unable to open device for capture", status); |
| 192 | return 1; |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 195 | status = pjmedia_snd_stream_start(strm); |
| 196 | if (status != PJ_SUCCESS) { |
| 197 | app_perror("Unable to start capture stream", status); |
| 198 | return 1; |
| 199 | } |
| 200 | |
| 201 | /* Let playback/capture runs for a while */ |
Benny Prijono | 5012da8 | 2006-04-25 10:02:40 +0000 | [diff] [blame] | 202 | //pj_thread_sleep(1000); |
| 203 | puts("Press <ENTER> to stop"); |
| 204 | fgets(tmp, sizeof(tmp), stdin); |
| 205 | |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 206 | |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 207 | pjmedia_snd_stream_close(strm); |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 208 | |
| 209 | if ((dir & PJMEDIA_DIR_CAPTURE) && rec_counter==0) { |
| 210 | printf("Error: capture stream was not running\n"); |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | if ((dir & PJMEDIA_DIR_PLAYBACK) && play_counter==0) { |
| 215 | printf("Error: playback stream was not running\n"); |
| 216 | return 1; |
| 217 | } |
| 218 | |
| 219 | puts("Success."); |
Benny Prijono | e452dda | 2006-04-03 09:43:36 +0000 | [diff] [blame] | 220 | |
| 221 | printf("Delay: "); |
| 222 | for (i=0; i<play_counter; ++i) |
| 223 | printf("%d ", play_delays[i]); |
| 224 | |
| 225 | puts(""); |
| 226 | if (dir & PJMEDIA_DIR_PLAYBACK) { |
| 227 | printf("Callback interval: min interval=%d ms, max interval=%d ms\n", |
| 228 | min_delay, max_delay); |
| 229 | } |
| 230 | |
| 231 | |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | int main(int argc, char *argv[]) |
| 237 | { |
| 238 | pj_caching_pool cp; |
| 239 | pjmedia_endpt *med_endpt; |
| 240 | pj_status_t status; |
| 241 | |
| 242 | /* Init pjlib */ |
| 243 | status = pj_init(); |
| 244 | PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1); |
| 245 | |
| 246 | /* Must create a pool factory before we can allocate any memory. */ |
| 247 | pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 248 | |
| 249 | /* |
| 250 | * Initialize media endpoint. |
| 251 | * This will implicitly initialize PJMEDIA too. |
| 252 | */ |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 253 | status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 254 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 255 | |
| 256 | |
| 257 | if (argc == 1) { |
| 258 | enum_devices(); |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 259 | } else if (argc == 6) { |
| 260 | |
| 261 | int dev_id; |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 262 | pjmedia_dir dir = PJMEDIA_DIR_NONE; |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 263 | int nchannel; |
| 264 | int bits; |
| 265 | |
| 266 | dev_id = atoi(argv[1]); |
Benny Prijono | a8df654 | 2006-03-23 14:20:42 +0000 | [diff] [blame] | 267 | |
| 268 | if (strcmp(argv[2], "rec")==0) |
| 269 | dir = PJMEDIA_DIR_CAPTURE; |
| 270 | else if (strcmp(argv[2], "play")==0) |
| 271 | dir = PJMEDIA_DIR_PLAYBACK; |
| 272 | else if (strcmp(argv[2], "both")==0) |
| 273 | dir = PJMEDIA_DIR_CAPTURE_PLAYBACK; |
| 274 | |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 275 | clock_rate = atoi(argv[3]); |
| 276 | nchannel = atoi(argv[4]); |
| 277 | bits = atoi(argv[5]); |
| 278 | |
Benny Prijono | 5012da8 | 2006-04-25 10:02:40 +0000 | [diff] [blame] | 279 | return open_device(dev_id, dir, nchannel, bits); |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 280 | |
| 281 | } else { |
| 282 | puts("Error: invalid arguments"); |
| 283 | puts(desc); |
| 284 | return 1; |
| 285 | } |
| 286 | |
Benny Prijono | af1bb1e | 2006-11-21 12:39:31 +0000 | [diff] [blame] | 287 | /* Shutdown PJLIB */ |
| 288 | pj_shutdown(); |
| 289 | |
Benny Prijono | 204ce5e | 2006-03-20 16:59:23 +0000 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | |