Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 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 | |
Benny Prijono | 1ec70b3 | 2006-06-20 15:39:07 +0000 | [diff] [blame] | 21 | /** |
| 22 | * \page page_pjmedia_samples_sndtest_c Samples: Sound Card Benchmark |
| 23 | * |
| 24 | * This example can be used to benchmark the quality of the sound card |
| 25 | * installed in the system. At the end of the test, it will report |
| 26 | * the jitter and clock drifts of the device. |
| 27 | * |
| 28 | * This file is pjsip-apps/src/samples/sndtest.c |
| 29 | * |
| 30 | * Screenshots on WinXP: \image html sndtest.jpg |
| 31 | * |
| 32 | * \includelineno sndtest.c |
| 33 | */ |
| 34 | |
| 35 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 36 | #include <pjmedia.h> |
| 37 | #include <pjlib.h> |
| 38 | #include <pjlib-util.h> |
| 39 | |
| 40 | #include <stdlib.h> /* atoi() */ |
| 41 | #include <stdio.h> |
| 42 | |
| 43 | |
| 44 | |
| 45 | #define THIS_FILE "sndtest.c" |
| 46 | |
| 47 | /* Warn (print log with yellow color) if frame jitter is larger than |
| 48 | * this value (in usec). |
| 49 | */ |
| 50 | #define WARN_JITTER_USEC 1000 |
| 51 | |
| 52 | /* Test duration in msec */ |
| 53 | #define DURATION 10000 |
| 54 | |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 55 | /* Skip the first msec from the calculation */ |
| 56 | #define SKIP_DURATION 1000 |
| 57 | |
| 58 | /* Max frames per sec (to calculate number of delays to keep). */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 59 | #define MAX_FRAMES_PER_SEC 100 |
| 60 | |
| 61 | /* Number of frame durations to keep */ |
| 62 | #define MAX_DELAY_COUNTER (((DURATION/1000)+1)*MAX_FRAMES_PER_SEC) |
| 63 | |
| 64 | |
| 65 | struct stream_data |
| 66 | { |
| 67 | pj_uint32_t first_timestamp; |
| 68 | pj_uint32_t last_timestamp; |
| 69 | pj_timestamp last_called; |
| 70 | unsigned counter; |
| 71 | unsigned min_delay; |
| 72 | unsigned max_delay; |
| 73 | unsigned delay[MAX_DELAY_COUNTER]; |
| 74 | }; |
| 75 | |
| 76 | struct test_data { |
| 77 | pjmedia_dir dir; |
| 78 | unsigned clock_rate; |
| 79 | unsigned samples_per_frame; |
| 80 | unsigned channel_count; |
| 81 | pj_bool_t running; |
| 82 | pj_bool_t has_error; |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 83 | pj_mutex_t *mutex; |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 84 | |
| 85 | struct stream_data capture_data; |
| 86 | struct stream_data playback_data; |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | |
| 91 | static const char *desc = |
| 92 | " sndtest.c \n" |
| 93 | " \n" |
| 94 | " PURPOSE: \n" |
| 95 | " Test the performance of sound device. \n" |
| 96 | " \n" |
| 97 | " USAGE: \n" |
| 98 | " sndtest --help \n" |
| 99 | " sndtest [options] \n" |
| 100 | " \n" |
| 101 | " where options: \n" |
| 102 | " --id=ID -i Use device ID (default is -1) \n" |
| 103 | " --rate=HZ -r Set test clock rate (default=8000)\n" |
| 104 | " --frame=SAMPLES -f Set number of samples per frame\n" |
| 105 | " --channel=CH -n Set number of channels (default=1)\n" |
| 106 | " --verbose -v Show verbose result \n" |
| 107 | " --help -h Show this screen \n" |
| 108 | ; |
| 109 | |
| 110 | |
| 111 | |
| 112 | static void enum_devices(void) |
| 113 | { |
| 114 | int i, count; |
| 115 | |
| 116 | count = pjmedia_snd_get_dev_count(); |
| 117 | if (count == 0) { |
| 118 | PJ_LOG(3,(THIS_FILE, "No devices found")); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | PJ_LOG(3,(THIS_FILE, "Found %d devices:", count)); |
| 123 | for (i=0; i<count; ++i) { |
| 124 | const pjmedia_snd_dev_info *info; |
| 125 | |
| 126 | info = pjmedia_snd_get_dev_info(i); |
| 127 | pj_assert(info != NULL); |
| 128 | |
| 129 | PJ_LOG(3,(THIS_FILE," %d: %s (capture=%d, playback=%d)", |
| 130 | i, info->name, info->input_count, info->output_count)); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 135 | static pj_status_t play_cb(void *user_data, pj_uint32_t timestamp, |
| 136 | void *output, unsigned size) |
| 137 | { |
| 138 | struct test_data *test_data = user_data; |
| 139 | struct stream_data *strm_data = &test_data->playback_data; |
| 140 | |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 141 | pj_mutex_lock(test_data->mutex); |
| 142 | |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 143 | /* Skip frames when test is not started or test has finished */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 144 | if (!test_data->running) { |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 145 | pj_bzero(output, size); |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 146 | |
| 147 | pj_mutex_unlock(test_data->mutex); |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 148 | return PJ_SUCCESS; |
| 149 | } |
| 150 | |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 151 | /* Save last timestamp seen (to calculate drift) */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 152 | strm_data->last_timestamp = timestamp; |
| 153 | |
| 154 | if (strm_data->last_called.u64 == 0) { |
| 155 | pj_get_timestamp(&strm_data->last_called); |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 156 | /* Init min_delay to one frame */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 157 | strm_data->min_delay = test_data->samples_per_frame * 1000000 / |
| 158 | test_data->clock_rate; |
| 159 | strm_data->first_timestamp = timestamp; |
| 160 | |
| 161 | } else if (strm_data->counter <= MAX_DELAY_COUNTER) { |
| 162 | pj_timestamp now; |
| 163 | unsigned delay; |
| 164 | |
| 165 | pj_get_timestamp(&now); |
| 166 | |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 167 | /* Calculate frame interval */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 168 | delay = pj_elapsed_usec(&strm_data->last_called, &now); |
| 169 | if (delay < strm_data->min_delay) |
| 170 | strm_data->min_delay = delay; |
| 171 | if (delay > strm_data->max_delay) |
| 172 | strm_data->max_delay = delay; |
| 173 | |
| 174 | strm_data->last_called = now; |
| 175 | |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 176 | /* Save the frame interval for later calculation */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 177 | strm_data->delay[strm_data->counter] = delay; |
| 178 | ++strm_data->counter; |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 179 | |
| 180 | } else { |
| 181 | |
| 182 | /* No space, can't take anymore frames */ |
| 183 | test_data->running = 0; |
| 184 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 187 | pj_bzero(output, size); |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 188 | |
| 189 | pj_mutex_unlock(test_data->mutex); |
| 190 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 191 | return PJ_SUCCESS; |
| 192 | } |
| 193 | |
| 194 | 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] | 195 | void *input, unsigned size) |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 196 | { |
| 197 | |
| 198 | struct test_data *test_data = user_data; |
| 199 | struct stream_data *strm_data = &test_data->capture_data; |
| 200 | |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 201 | pj_mutex_lock(test_data->mutex); |
| 202 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 203 | PJ_UNUSED_ARG(input); |
| 204 | PJ_UNUSED_ARG(size); |
| 205 | |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 206 | /* Skip frames when test is not started or test has finished */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 207 | if (!test_data->running) { |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 208 | pj_mutex_unlock(test_data->mutex); |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 209 | return PJ_SUCCESS; |
| 210 | } |
| 211 | |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 212 | /* Save last timestamp seen (to calculate drift) */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 213 | strm_data->last_timestamp = timestamp; |
| 214 | |
| 215 | if (strm_data->last_called.u64 == 0) { |
| 216 | pj_get_timestamp(&strm_data->last_called); |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 217 | /* Init min_delay to one frame */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 218 | strm_data->min_delay = test_data->samples_per_frame * 1000000 / |
| 219 | test_data->clock_rate; |
| 220 | strm_data->first_timestamp = timestamp; |
| 221 | |
| 222 | } else if (strm_data->counter <= MAX_DELAY_COUNTER) { |
| 223 | pj_timestamp now; |
| 224 | unsigned delay; |
| 225 | |
| 226 | pj_get_timestamp(&now); |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 227 | |
| 228 | /* Calculate frame interval */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 229 | delay = pj_elapsed_usec(&strm_data->last_called, &now); |
| 230 | if (delay < strm_data->min_delay) |
| 231 | strm_data->min_delay = delay; |
| 232 | if (delay > strm_data->max_delay) |
| 233 | strm_data->max_delay = delay; |
| 234 | |
| 235 | strm_data->last_called = now; |
| 236 | |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 237 | /* Save the frame interval for later calculation */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 238 | strm_data->delay[strm_data->counter] = delay; |
| 239 | ++strm_data->counter; |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 240 | |
| 241 | } else { |
| 242 | |
| 243 | /* No space, can't take anymore frames */ |
| 244 | test_data->running = 0; |
| 245 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 248 | pj_mutex_unlock(test_data->mutex); |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 249 | return PJ_SUCCESS; |
| 250 | } |
| 251 | |
| 252 | static void app_perror(const char *title, pj_status_t status) |
| 253 | { |
| 254 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 255 | |
| 256 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 257 | printf( "%s: %s (err=%d)\n", |
| 258 | title, errmsg, status); |
| 259 | } |
| 260 | |
| 261 | |
| 262 | static void print_stream_data(const char *title, |
| 263 | struct test_data *test_data, |
| 264 | struct stream_data *strm_data, |
| 265 | int verbose) |
| 266 | { |
| 267 | unsigned i, dur; |
Benny Prijono | 959df2a | 2006-05-22 10:48:11 +0000 | [diff] [blame] | 268 | int ptime; |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 269 | unsigned min_jitter, max_jitter, sum_jitter, avg_jitter=0; |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 270 | |
| 271 | PJ_LOG(3,(THIS_FILE, " %s stream report:", title)); |
| 272 | |
| 273 | /* Check that frames are captured/played */ |
| 274 | if (strm_data->counter == 0) { |
| 275 | PJ_LOG(1,(THIS_FILE, " Error: no frames are captured/played!")); |
| 276 | test_data->has_error = 1; |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | /* Duration */ |
| 281 | dur = (strm_data->counter+1) * test_data->samples_per_frame * 1000 / |
| 282 | test_data->clock_rate; |
| 283 | PJ_LOG(3,(THIS_FILE, " Duration: %ds.%03d", |
| 284 | dur/1000, dur%1000)); |
| 285 | |
| 286 | /* Frame interval */ |
| 287 | if (strm_data->max_delay - strm_data->min_delay < WARN_JITTER_USEC) { |
| 288 | PJ_LOG(3,(THIS_FILE, |
| 289 | " Frame interval: min=%d.%03dms, max=%d.%03dms", |
| 290 | strm_data->min_delay/1000, strm_data->min_delay%1000, |
| 291 | strm_data->max_delay/1000, strm_data->max_delay%1000)); |
| 292 | } else { |
| 293 | test_data->has_error = 1; |
| 294 | PJ_LOG(2,(THIS_FILE, |
| 295 | " Frame interval: min=%d.%03dms, max=%d.%03dms", |
| 296 | strm_data->min_delay/1000, strm_data->min_delay%1000, |
| 297 | strm_data->max_delay/1000, strm_data->max_delay%1000)); |
| 298 | } |
| 299 | |
| 300 | if (verbose) { |
| 301 | unsigned i; |
| 302 | unsigned decor = pj_log_get_decor(); |
| 303 | |
| 304 | PJ_LOG(3,(THIS_FILE, " Dumping frame delays:")); |
| 305 | |
| 306 | pj_log_set_decor(0); |
| 307 | for (i=0; i<strm_data->counter; ++i) |
| 308 | PJ_LOG(3,(THIS_FILE, " %d.%03d", strm_data->delay[i]/1000, |
| 309 | strm_data->delay[i]%1000)); |
| 310 | PJ_LOG(3,(THIS_FILE, "\r\n")); |
| 311 | pj_log_set_decor(decor); |
| 312 | } |
| 313 | |
Benny Prijono | 959df2a | 2006-05-22 10:48:11 +0000 | [diff] [blame] | 314 | /* Calculate frame ptime in usec */ |
| 315 | ptime = test_data->samples_per_frame * 1000000 / |
| 316 | test_data->clock_rate; |
| 317 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 318 | /* Calculate jitter */ |
| 319 | min_jitter = 0xFFFFF; |
| 320 | max_jitter = 0; |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 321 | sum_jitter = 0; |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 322 | |
| 323 | for (i=1; i<strm_data->counter; ++i) { |
Benny Prijono | 959df2a | 2006-05-22 10:48:11 +0000 | [diff] [blame] | 324 | int jitter1, jitter2, jitter; |
| 325 | |
| 326 | /* jitter1 is interarrival difference */ |
| 327 | jitter1 = strm_data->delay[i] - strm_data->delay[i-1]; |
| 328 | if (jitter1 < 0) jitter1 = -jitter1; |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 329 | |
Benny Prijono | 959df2a | 2006-05-22 10:48:11 +0000 | [diff] [blame] | 330 | /* jitter2 is difference between actual and scheduled arrival. |
| 331 | * This is intended to capture situation when frames are coming |
| 332 | * instantaneously, which will calculate as zero jitter with |
| 333 | * jitter1 calculation. |
| 334 | */ |
| 335 | jitter2 = ptime - strm_data->delay[i]; |
| 336 | if (jitter2 < 0) jitter2 = -jitter2; |
| 337 | |
| 338 | /* Set jitter as the maximum of the two jitter calculations. |
| 339 | * This is intended to show the worst result. |
| 340 | */ |
| 341 | jitter = (jitter1>jitter2) ? jitter1 : jitter2; |
| 342 | |
| 343 | /* Calculate min, max, avg jitter */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 344 | if (jitter < (int)min_jitter) min_jitter = jitter; |
| 345 | if (jitter > (int)max_jitter) max_jitter = jitter; |
| 346 | |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 347 | sum_jitter += jitter; |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 348 | } |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 349 | |
| 350 | avg_jitter = (sum_jitter) / (strm_data->counter - 1); |
| 351 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 352 | if (max_jitter < WARN_JITTER_USEC) { |
| 353 | PJ_LOG(3,(THIS_FILE, |
| 354 | " Jitter: min=%d.%03dms, avg=%d.%03dms, max=%d.%03dms", |
| 355 | min_jitter/1000, min_jitter%1000, |
| 356 | avg_jitter/1000, avg_jitter%1000, |
| 357 | max_jitter/1000, max_jitter%1000)); |
| 358 | } else { |
| 359 | test_data->has_error = 1; |
| 360 | PJ_LOG(2,(THIS_FILE, |
| 361 | " Jitter: min=%d.%03dms, avg=%d.%03dms, max=%d.%03dms", |
| 362 | min_jitter/1000, min_jitter%1000, |
| 363 | avg_jitter/1000, avg_jitter%1000, |
| 364 | max_jitter/1000, max_jitter%1000)); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 369 | static int perform_test(pj_pool_t *pool, int dev_id, pjmedia_dir dir, |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 370 | unsigned clock_rate, unsigned samples_per_frame, |
| 371 | unsigned nchannel, int verbose) |
| 372 | { |
| 373 | pj_status_t status = PJ_SUCCESS; |
| 374 | pjmedia_snd_stream *strm; |
| 375 | struct test_data test_data; |
Benny Prijono | 5807f2c | 2006-11-29 23:12:26 +0000 | [diff] [blame] | 376 | pjmedia_snd_stream_info si; |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 377 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 378 | /* |
| 379 | * Init test parameters |
| 380 | */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 381 | pj_bzero(&test_data, sizeof(test_data)); |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 382 | test_data.dir = dir; |
| 383 | test_data.clock_rate = clock_rate; |
| 384 | test_data.samples_per_frame = samples_per_frame; |
| 385 | test_data.channel_count = nchannel; |
| 386 | |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 387 | pj_mutex_create_simple(pool, "sndtest", &test_data.mutex); |
| 388 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 389 | /* |
| 390 | * Open device. |
| 391 | */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 392 | if (dir == PJMEDIA_DIR_CAPTURE) { |
| 393 | status = pjmedia_snd_open_rec( dev_id, clock_rate, nchannel, |
| 394 | samples_per_frame, 16, &rec_cb, |
| 395 | &test_data, &strm); |
| 396 | } else if (dir == PJMEDIA_DIR_PLAYBACK) { |
| 397 | status = pjmedia_snd_open_player( dev_id, clock_rate, nchannel, |
| 398 | samples_per_frame, 16, &play_cb, |
| 399 | &test_data, &strm); |
| 400 | } else { |
| 401 | status = pjmedia_snd_open( dev_id, dev_id, clock_rate, nchannel, |
| 402 | samples_per_frame, 16, &rec_cb, &play_cb, |
| 403 | &test_data, &strm); |
| 404 | } |
| 405 | |
| 406 | if (status != PJ_SUCCESS) { |
| 407 | app_perror("Unable to open device for capture", status); |
| 408 | return status; |
| 409 | } |
| 410 | |
Benny Prijono | 5807f2c | 2006-11-29 23:12:26 +0000 | [diff] [blame] | 411 | pjmedia_snd_stream_get_info(strm, &si); |
| 412 | if (si.play_id >= 0) { |
| 413 | PJ_LOG(3,(THIS_FILE, "Testing playback device %s", |
| 414 | pjmedia_snd_get_dev_info(si.play_id)->name)); |
| 415 | } |
| 416 | if (si.rec_id >= 0) { |
| 417 | PJ_LOG(3,(THIS_FILE, "Testing capture device %s", |
| 418 | pjmedia_snd_get_dev_info(si.rec_id)->name)); |
| 419 | } |
| 420 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 421 | /* Sleep for a while to let sound device "settles" */ |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 422 | pj_thread_sleep(200); |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 423 | |
| 424 | |
| 425 | /* |
| 426 | * Start the stream. |
| 427 | */ |
| 428 | status = pjmedia_snd_stream_start(strm); |
| 429 | if (status != PJ_SUCCESS) { |
| 430 | app_perror("Unable to start capture stream", status); |
| 431 | return status; |
| 432 | } |
| 433 | |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 434 | PJ_LOG(3,(THIS_FILE, |
| 435 | " Please wait while test is in progress (~%d secs)..", |
| 436 | (DURATION+SKIP_DURATION)/1000)); |
| 437 | |
| 438 | /* Let the stream runs for few msec/sec to get stable result. |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 439 | * (capture normally begins with frames available simultaneously). |
| 440 | */ |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 441 | pj_thread_sleep(SKIP_DURATION); |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 442 | |
| 443 | |
| 444 | /* Begin gather data */ |
| 445 | test_data.running = 1; |
| 446 | |
| 447 | /* |
| 448 | * Let the test runs for a while. |
| 449 | */ |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 450 | pj_thread_sleep(DURATION); |
| 451 | |
| 452 | |
| 453 | /* |
| 454 | * Close stream. |
| 455 | */ |
| 456 | test_data.running = 0; |
| 457 | pjmedia_snd_stream_close(strm); |
| 458 | |
| 459 | |
| 460 | /* |
| 461 | * Print results. |
| 462 | */ |
| 463 | PJ_LOG(3,(THIS_FILE, " Dumping results:")); |
| 464 | |
| 465 | PJ_LOG(3,(THIS_FILE, " Parameters: clock rate=%dHz, %d samples/frame", |
| 466 | clock_rate, samples_per_frame)); |
| 467 | |
| 468 | if (dir & PJMEDIA_DIR_PLAYBACK) |
| 469 | print_stream_data("Playback", &test_data, &test_data.playback_data, |
| 470 | verbose); |
| 471 | if (dir & PJMEDIA_DIR_CAPTURE) |
| 472 | print_stream_data("Capture", &test_data, &test_data.capture_data, |
| 473 | verbose); |
| 474 | |
| 475 | /* Check drifting */ |
| 476 | if (dir == PJMEDIA_DIR_CAPTURE_PLAYBACK) { |
| 477 | int end_diff, start_diff, drift; |
| 478 | |
| 479 | end_diff = test_data.capture_data.last_timestamp - |
| 480 | test_data.playback_data.last_timestamp; |
| 481 | start_diff = test_data.capture_data.first_timestamp- |
| 482 | test_data.playback_data.first_timestamp; |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 483 | drift = end_diff > start_diff? end_diff - start_diff : |
| 484 | start_diff - end_diff; |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 485 | |
| 486 | PJ_LOG(3,(THIS_FILE, " Checking for clock drifts:")); |
| 487 | |
| 488 | /* Allow one frame tolerance for clock drift detection */ |
| 489 | if (drift < (int)samples_per_frame) { |
| 490 | PJ_LOG(3,(THIS_FILE, " No clock drifts is detected")); |
| 491 | } else { |
| 492 | const char *which = (drift<0 ? "slower" : "faster"); |
| 493 | unsigned msec_dur; |
| 494 | |
| 495 | if (drift < 0) drift = -drift; |
| 496 | |
| 497 | |
| 498 | msec_dur = (test_data.capture_data.last_timestamp - |
| 499 | test_data.capture_data.first_timestamp) * 1000 / |
| 500 | test_data.clock_rate; |
| 501 | |
| 502 | PJ_LOG(2,(THIS_FILE, |
| 503 | " Sound capture is %d samples %s than playback " |
Benny Prijono | 6a61c22 | 2006-05-22 10:28:44 +0000 | [diff] [blame] | 504 | "at the end of the test (average is %d samples" |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 505 | " per second)", |
| 506 | drift, which, |
| 507 | drift * 1000 / msec_dur)); |
| 508 | |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | if (test_data.has_error == 0) { |
| 513 | PJ_LOG(3,(THIS_FILE, " Test completed, sound device looks okay.")); |
| 514 | return 0; |
| 515 | } else { |
| 516 | PJ_LOG(2,(THIS_FILE, " Test completed with some warnings")); |
| 517 | return 1; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | |
| 522 | int main(int argc, char *argv[]) |
| 523 | { |
| 524 | pj_caching_pool cp; |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 525 | pj_pool_t *pool; |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 526 | pjmedia_endpt *med_endpt; |
| 527 | int id = -1, verbose = 0; |
| 528 | int clock_rate = 8000; |
| 529 | int frame = -1; |
| 530 | int channel = 1; |
| 531 | struct pj_getopt_option long_options[] = { |
| 532 | { "id", 1, 0, 'i' }, |
| 533 | { "rate", 1, 0, 'r' }, |
| 534 | { "frame", 1, 0, 'f' }, |
| 535 | { "channel", 1, 0, 'n' }, |
| 536 | { "verbose", 0, 0, 'v' }, |
| 537 | { "help", 0, 0, 'h' }, |
| 538 | { NULL, 0, 0, 0 } |
| 539 | }; |
| 540 | int c, option_index; |
| 541 | |
| 542 | |
| 543 | pj_status_t status; |
| 544 | |
| 545 | /* Init pjlib */ |
| 546 | status = pj_init(); |
| 547 | PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1); |
| 548 | |
| 549 | /* Must create a pool factory before we can allocate any memory. */ |
| 550 | pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 551 | |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 552 | /* Also create pool for misc purposes */ |
| 553 | pool = pj_pool_create(&cp.factory, "sndtest", 1000, 1000, NULL); |
| 554 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 555 | /* |
| 556 | * Initialize media endpoint. |
| 557 | * This will implicitly initialize PJMEDIA too. |
| 558 | */ |
| 559 | status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
| 560 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 561 | |
| 562 | /* Print devices */ |
| 563 | enum_devices(); |
| 564 | |
| 565 | /* Parse options */ |
| 566 | pj_optind = 0; |
| 567 | while((c=pj_getopt_long(argc,argv, "i:r:f:n:vh", |
| 568 | long_options, &option_index))!=-1) |
| 569 | { |
| 570 | switch (c) { |
| 571 | case 'i': |
| 572 | id = atoi(pj_optarg); |
| 573 | break; |
| 574 | case 'r': |
| 575 | clock_rate = atoi(pj_optarg); |
| 576 | break; |
| 577 | case 'f': |
| 578 | frame = atoi(pj_optarg); |
| 579 | break; |
| 580 | case 'n': |
| 581 | channel = atoi(pj_optarg); |
| 582 | break; |
| 583 | case 'v': |
| 584 | verbose = 1; |
| 585 | break; |
| 586 | case 'h': |
| 587 | puts(desc); |
| 588 | return 0; |
| 589 | break; |
| 590 | default: |
| 591 | printf("Error: invalid options %s\n", argv[pj_optind-1]); |
| 592 | puts(desc); |
| 593 | return 1; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | if (pj_optind != argc) { |
| 598 | printf("Error: invalid options\n"); |
| 599 | puts(desc); |
| 600 | return 1; |
| 601 | } |
| 602 | |
| 603 | if (!verbose) |
| 604 | pj_log_set_level(3); |
| 605 | |
| 606 | if (frame == -1) |
| 607 | frame = 10 * clock_rate / 1000; |
| 608 | |
| 609 | |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 610 | status = perform_test(pool, id, PJMEDIA_DIR_CAPTURE_PLAYBACK, |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 611 | clock_rate, frame, channel, verbose); |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 612 | |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 613 | pjmedia_endpt_destroy(med_endpt); |
| 614 | pj_pool_release(pool); |
| 615 | pj_caching_pool_destroy(&cp); |
| 616 | pj_shutdown(); |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 617 | |
Benny Prijono | d744393 | 2008-01-04 18:00:11 +0000 | [diff] [blame] | 618 | return status == PJ_SUCCESS ? 0 : 1; |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | |