From 1dacdee696b7591a6dcc0b3c1d0f41573e473168 Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Thu, 12 Mar 2009 18:11:37 +0000 Subject: (Major) Task #737 and #738: integration of APS-Direct and Audiodev from aps-direct branch to trunk. git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@2506 74dad513-b988-da41-8d7b-12977e46ad98 --- pjsip-apps/src/samples/auddemo.c | 544 ++++++++++++++++++++++++++++++++++ pjsip-apps/src/samples/debug.c | 2 +- pjsip-apps/src/samples/sndinfo.c | 295 ------------------- pjsip-apps/src/samples/sndtest.c | 621 --------------------------------------- 4 files changed, 545 insertions(+), 917 deletions(-) create mode 100644 pjsip-apps/src/samples/auddemo.c delete mode 100644 pjsip-apps/src/samples/sndinfo.c delete mode 100644 pjsip-apps/src/samples/sndtest.c (limited to 'pjsip-apps/src/samples') diff --git a/pjsip-apps/src/samples/auddemo.c b/pjsip-apps/src/samples/auddemo.c new file mode 100644 index 00000000..e805f176 --- /dev/null +++ b/pjsip-apps/src/samples/auddemo.c @@ -0,0 +1,544 @@ +/* $Id$ */ +/* + * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) + * Copyright (C) 2003-2008 Benny Prijono + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include +#include +#include +#include +#include + +#define THIS_FILE "auddemo.c" +#define MAX_DEVICES 64 +#define WAV_FILE "auddemo.wav" + + +static unsigned dev_count; + +static void app_perror(const char *title, pj_status_t status) +{ + char errmsg[PJ_ERR_MSG_SIZE]; + + pj_strerror(status, errmsg, sizeof(errmsg)); + printf( "%s: %s (err=%d)\n", + title, errmsg, status); +} + +static void list_devices(void) +{ + unsigned i; + pj_status_t status; + + dev_count = pjmedia_aud_dev_count(); + if (dev_count == 0) { + PJ_LOG(3,(THIS_FILE, "No devices found")); + return; + } + + PJ_LOG(3,(THIS_FILE, "Found %d devices:", dev_count)); + + for (i=0; i= dev_count) { + PJ_LOG(1,(THIS_FILE, "Error: invalid index %u", index)); + return; + } + + status = pjmedia_aud_dev_get_info(index, &info); + if (status != PJ_SUCCESS) { + app_perror("pjmedia_aud_dev_get_info() error", status); + return; + } + + PJ_LOG(3, (THIS_FILE, "Device at index %u:", index)); + PJ_LOG(3, (THIS_FILE, "-------------------------")); + + PJ_LOG(3, (THIS_FILE, H": %u (0x%x)", "ID", index, index)); + PJ_LOG(3, (THIS_FILE, H": %s", "Name", info.name)); + PJ_LOG(3, (THIS_FILE, H": %s", "Driver", info.driver)); + PJ_LOG(3, (THIS_FILE, H": %u", "Input channels", info.input_count)); + PJ_LOG(3, (THIS_FILE, H": %u", "Output channels", info.output_count)); + PJ_LOG(3, (THIS_FILE, H": %s", "Capabilities", decode_caps(info.caps))); + + formats[0] = '\0'; + if (info.caps & PJMEDIA_AUD_DEV_CAP_EXT_FORMAT) { + unsigned i; + + for (i=0; i=0 ? "faster" : "slower"; + unsigned drift = result.rec_drift_per_sec>=0 ? + result.rec_drift_per_sec : + -result.rec_drift_per_sec; + + PJ_LOG(3,(THIS_FILE, " Clock drifts detected. Capture device " + "is running %d samples per second %s " + "than the playback device", + drift, which)); + } + } +} + + +static pj_status_t wav_rec_cb(void *user_data, pjmedia_frame *frame) +{ + return pjmedia_port_put_frame((pjmedia_port*)user_data, frame); +} + +static void record(unsigned rec_index, const char *filename) +{ + pj_pool_t *pool = NULL; + pjmedia_port *wav = NULL; + pjmedia_aud_param param; + pjmedia_aud_stream *strm = NULL; + char line[10]; + pj_status_t status; + + if (filename == NULL) + filename = WAV_FILE; + + pool = pj_pool_create(pjmedia_aud_subsys_get_pool_factory(), "wav", + 1000, 1000, NULL); + + status = pjmedia_wav_writer_port_create(pool, filename, 16000, + 1, 320, 16, 0, 0, &wav); + if (status != PJ_SUCCESS) { + app_perror("Error creating WAV file", status); + goto on_return; + } + + status = pjmedia_aud_dev_default_param(rec_index, ¶m); + if (status != PJ_SUCCESS) { + app_perror("pjmedia_aud_dev_default_param()", status); + goto on_return; + } + + param.dir = PJMEDIA_DIR_CAPTURE; + param.clock_rate = wav->info.clock_rate; + param.samples_per_frame = wav->info.samples_per_frame; + param.channel_count = wav->info.channel_count; + param.bits_per_sample = wav->info.bits_per_sample; + + status = pjmedia_aud_stream_create(¶m, &wav_rec_cb, NULL, wav, + &strm); + if (status != PJ_SUCCESS) { + app_perror("Error opening the sound device", status); + goto on_return; + } + + status = pjmedia_aud_stream_start(strm); + if (status != PJ_SUCCESS) { + app_perror("Error starting the sound device", status); + goto on_return; + } + + PJ_LOG(3,(THIS_FILE, "Recording started, press ENTER to stop")); + fgets(line, sizeof(line), stdin); + +on_return: + if (strm) { + pjmedia_aud_stream_stop(strm); + pjmedia_aud_stream_destroy(strm); + } + if (wav) + pjmedia_port_destroy(wav); + if (pool) + pj_pool_release(pool); +} + + +static pj_status_t wav_play_cb(void *user_data, pjmedia_frame *frame) +{ + return pjmedia_port_get_frame((pjmedia_port*)user_data, frame); +} + + +static void play_file(unsigned play_index, const char *filename) +{ + pj_pool_t *pool = NULL; + pjmedia_port *wav = NULL; + pjmedia_aud_param param; + pjmedia_aud_stream *strm = NULL; + char line[10]; + pj_status_t status; + + if (filename == NULL) + filename = WAV_FILE; + + pool = pj_pool_create(pjmedia_aud_subsys_get_pool_factory(), "wav", + 1000, 1000, NULL); + + status = pjmedia_wav_player_port_create(pool, filename, 20, 0, 0, &wav); + if (status != PJ_SUCCESS) { + app_perror("Error opening WAV file", status); + goto on_return; + } + + status = pjmedia_aud_dev_default_param(play_index, ¶m); + if (status != PJ_SUCCESS) { + app_perror("pjmedia_aud_dev_default_param()", status); + goto on_return; + } + + param.dir = PJMEDIA_DIR_PLAYBACK; + param.clock_rate = wav->info.clock_rate; + param.samples_per_frame = wav->info.samples_per_frame; + param.channel_count = wav->info.channel_count; + param.bits_per_sample = wav->info.bits_per_sample; + + status = pjmedia_aud_stream_create(¶m, NULL, &wav_play_cb, wav, + &strm); + if (status != PJ_SUCCESS) { + app_perror("Error opening the sound device", status); + goto on_return; + } + + status = pjmedia_aud_stream_start(strm); + if (status != PJ_SUCCESS) { + app_perror("Error starting the sound device", status); + goto on_return; + } + + PJ_LOG(3,(THIS_FILE, "Playback started, press ENTER to stop")); + fgets(line, sizeof(line), stdin); + +on_return: + if (strm) { + pjmedia_aud_stream_stop(strm); + pjmedia_aud_stream_destroy(strm); + } + if (wav) + pjmedia_port_destroy(wav); + if (pool) + pj_pool_release(pool); +} + + +static void print_menu(void) +{ + puts(""); + puts("Audio demo menu:"); + puts("-------------------------------"); + puts(" l List devices"); + puts(" i ID Show device info for device ID"); + puts(" t RID PID CR PTIM [CH] Perform test on the device:"); + puts(" RID: record device ID (-1 for no)"); + puts(" PID: playback device ID (-1 for no)"); + puts(" CR: clock rate"); + puts(" PTIM: ptime in ms"); + puts(" CH: # of channels"); + puts(" r RID [FILE] Record capture device RID to WAV file"); + puts(" p PID [FILE] Playback WAV file to device ID PID"); + puts(" v Toggle log verbosity"); + puts(" q Quit"); + puts(""); + printf("Enter selection: "); + fflush(stdout); +} + +int main() +{ + pj_caching_pool cp; + pj_bool_t done = PJ_FALSE; + pj_status_t status; + + /* Init pjlib */ + status = pj_init(); + PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1); + + pj_log_set_decor(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_COLOR); + + /* Must create a pool factory before we can allocate any memory. */ + pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); + + status = pjmedia_aud_subsys_init(&cp.factory); + if (status != PJ_SUCCESS) { + app_perror("pjmedia_aud_subsys_init()", status); + pj_caching_pool_destroy(&cp); + pj_shutdown(); + return 1; + } + + list_devices(); + + while (!done) { + char line[80]; + + print_menu(); + + if (fgets(line, sizeof(line), stdin)==NULL) + break; + + switch (line[0]) { + case 'l': + list_devices(); + break; + + case 'i': + { + unsigned dev_index; + if (sscanf(line+2, "%u", &dev_index) != 1) { + puts("error: device ID required"); + break; + } + show_dev_info(dev_index); + } + break; + + case 't': + { + pjmedia_dir dir; + int rec_id, play_id; + unsigned clock_rate, ptime, chnum; + int cnt; + + cnt = sscanf(line+2, "%d %d %u %u %u", &rec_id, &play_id, + &clock_rate, &ptime, &chnum); + if (cnt < 4) { + puts("error: not enough parameters"); + break; + } + if (clock_rate < 8000 || clock_rate > 128000) { + puts("error: invalid clock rate"); + break; + } + if (ptime < 10 || ptime > 500) { + puts("error: invalid ptime"); + break; + } + if (cnt==5) { + if (chnum < 1 || chnum > 4) { + puts("error: invalid number of channels"); + break; + } + } else { + chnum = 1; + } + + if (rec_id >= 0 && rec_id < (int)dev_count) { + if (play_id >= 0 && play_id < (int)dev_count) + dir = PJMEDIA_DIR_CAPTURE_PLAYBACK; + else + dir = PJMEDIA_DIR_CAPTURE; + } else if (play_id >= 0 && play_id < (int)dev_count) { + dir = PJMEDIA_DIR_PLAYBACK; + } else { + puts("error: at least one valid device index required"); + break; + } + + test_device(dir, rec_id, play_id, clock_rate, ptime, chnum); + + } + break; + + case 'r': + /* record */ + { + int index; + char filename[80]; + int count; + + count = sscanf(line+2, "%d %s", &index, filename); + if (count==1) + record(index, NULL); + else if (count==2) + record(index, filename); + else + puts("error: invalid command syntax"); + } + break; + + case 'p': + /* playback */ + { + int index; + char filename[80]; + int count; + + count = sscanf(line+2, "%d %s", &index, filename); + if (count==1) + play_file(index, NULL); + else if (count==2) + play_file(index, filename); + else + puts("error: invalid command syntax"); + } + break; + + case 'v': + if (pj_log_get_level() <= 3) { + pj_log_set_level(5); + puts("Logging set to detail"); + } else { + pj_log_set_level(3); + puts("Logging set to quiet"); + } + break; + + case 'q': + done = PJ_TRUE; + break; + } + } + + pj_caching_pool_destroy(&cp); + pj_shutdown(); + return 0; +} + + diff --git a/pjsip-apps/src/samples/debug.c b/pjsip-apps/src/samples/debug.c index c7453d93..1a145b9a 100644 --- a/pjsip-apps/src/samples/debug.c +++ b/pjsip-apps/src/samples/debug.c @@ -28,5 +28,5 @@ * E.g.: * #include "playfile.c" */ -#include "aectest.c" +#include "auddemo.c" diff --git a/pjsip-apps/src/samples/sndinfo.c b/pjsip-apps/src/samples/sndinfo.c deleted file mode 100644 index 9f282d1a..00000000 --- a/pjsip-apps/src/samples/sndinfo.c +++ /dev/null @@ -1,295 +0,0 @@ -/* $Id$ */ -/* - * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) - * Copyright (C) 2003-2008 Benny Prijono - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -static const char *desc = - " sndinfo.c \n" - " \n" - " PURPOSE: \n" - " Print sound device info and test open device. \n" - " \n" - " USAGE: \n" - " sndinfo [id rec/play/both clockrate nchan bits] \n" - " \n" - " DESCRIPTION: \n" - " When invoked without any arguments, it displays information about all \n" - " sound devices in the system. \n" - " \n" - " When invoked with arguments, the program tests if device can be opened \n" - " with the specified arguments. All these arguments must be specified: \n" - " - id The device ID (-1 for the first capable device) \n" - " - rec/play/both Specify which streams to open. \n" - " - clockrate Specify clock rate (e.g. 8000, 11025, etc.) \n" - " - nchan Number of channels (1=mono, 2=stereo). \n" - " - bits Number of bits per sample (normally 16). \n"; - -#include -#include - -#include /* atoi() */ -#include - - -static void enum_devices(void) -{ - int i, count; - - count = pjmedia_snd_get_dev_count(); - if (count == 0) { - puts("No devices found"); - return; - } - - for (i=0; iname, info->input_count, info->output_count, - info->default_samples_per_sec); - } - puts(""); - puts("Run with -h to get more options"); -} - -static unsigned clock_rate; -static unsigned play_counter; -static unsigned rec_counter; -static unsigned min_delay = 0xFFFF, max_delay; -static char play_delays[1000]; -static pj_uint32_t last_play_timestamp, last_rec_timestamp; - -static pj_status_t play_cb(void *user_data, pj_uint32_t timestamp, - void *output, unsigned size) -{ - static pj_timestamp last_cb; - - - PJ_UNUSED_ARG(user_data); - PJ_UNUSED_ARG(output); - PJ_UNUSED_ARG(size); - - - ++play_counter; - last_play_timestamp = timestamp; - - if (last_cb.u64 == 0) { - pj_get_timestamp(&last_cb); - } else if (play_counter <= PJ_ARRAY_SIZE(play_delays)) { - pj_timestamp now; - unsigned delay; - - pj_get_timestamp(&now); - - delay = pj_elapsed_msec(&last_cb, &now); - if (delay < min_delay) - min_delay = delay; - if (delay > max_delay) - max_delay = delay; - - last_cb = now; - - play_delays[play_counter-1] = (char)delay; - } - - return PJ_SUCCESS; -} - -static pj_status_t rec_cb(void *user_data, pj_uint32_t timestamp, - void *input, unsigned size) -{ - - PJ_UNUSED_ARG(size); - PJ_UNUSED_ARG(input); - PJ_UNUSED_ARG(user_data); - - - ++rec_counter; - - if (timestamp - last_rec_timestamp >= clock_rate && last_play_timestamp) { - int diff; - diff = last_play_timestamp - timestamp; - printf("Play timestamp=%u, capture timestamp=%u, diff=%d\n", - last_play_timestamp, timestamp, diff); - last_rec_timestamp = timestamp; - } - return PJ_SUCCESS; -} - -static void app_perror(const char *title, pj_status_t status) -{ - char errmsg[PJ_ERR_MSG_SIZE]; - - pj_strerror(status, errmsg, sizeof(errmsg)); - printf( "%s: %s (err=%d)\n", - title, errmsg, status); -} - -static int open_device(int dev_id, pjmedia_dir dir, - int nchannel, int bits) -{ - pj_status_t status = PJ_SUCCESS; - unsigned nsamples; - pjmedia_snd_stream *strm; - const char *dirtype; - char tmp[10]; - unsigned i; - - switch (dir) { - case PJMEDIA_DIR_CAPTURE: - dirtype = "capture"; break; - case PJMEDIA_DIR_PLAYBACK: - dirtype = "playback"; break; - case PJMEDIA_DIR_CAPTURE_PLAYBACK: - dirtype = "capture/playback"; break; - default: - return 1; - } - - nsamples = clock_rate * 20 / 1000; - - printf( "Opening device %d for %s: clockrate=%d, nchannel=%d, " - "bits=%d, nsamples=%d..\n", - dev_id, dirtype, clock_rate, nchannel, bits, nsamples); - - if (dir == PJMEDIA_DIR_CAPTURE) { - status = pjmedia_snd_open_rec( dev_id, clock_rate, nchannel, - nsamples, bits, &rec_cb, NULL, - &strm); - } else if (dir == PJMEDIA_DIR_PLAYBACK) { - status = pjmedia_snd_open_player( dev_id, clock_rate, nchannel, - nsamples, bits, &play_cb, NULL, - &strm); - } else { - status = pjmedia_snd_open( dev_id, dev_id, clock_rate, nchannel, - nsamples, bits, &rec_cb, &play_cb, NULL, - &strm); - } - - if (status != PJ_SUCCESS) { - app_perror("Unable to open device for capture", status); - return 1; - } - - status = pjmedia_snd_stream_start(strm); - if (status != PJ_SUCCESS) { - app_perror("Unable to start capture stream", status); - return 1; - } - - /* Let playback/capture runs for a while */ - //pj_thread_sleep(1000); - puts("Press to stop"); - if (fgets(tmp, sizeof(tmp), stdin) == NULL) { - puts("EOF while reading stdin, will quit now.."); - } - - pjmedia_snd_stream_close(strm); - - if ((dir & PJMEDIA_DIR_CAPTURE) && rec_counter==0) { - printf("Error: capture stream was not running\n"); - return 1; - } - - if ((dir & PJMEDIA_DIR_PLAYBACK) && play_counter==0) { - printf("Error: playback stream was not running\n"); - return 1; - } - - puts("Success."); - - printf("Delay: "); - for (i=0; i - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/** - * \page page_pjmedia_samples_sndtest_c Samples: Sound Card Benchmark - * - * This example can be used to benchmark the quality of the sound card - * installed in the system. At the end of the test, it will report - * the jitter and clock drifts of the device. - * - * This file is pjsip-apps/src/samples/sndtest.c - * - * Screenshots on WinXP: \image html sndtest.jpg - * - * \includelineno sndtest.c - */ - - -#include -#include -#include - -#include /* atoi() */ -#include - - - -#define THIS_FILE "sndtest.c" - -/* Warn (print log with yellow color) if frame jitter is larger than - * this value (in usec). - */ -#define WARN_JITTER_USEC 1000 - -/* Test duration in msec */ -#define DURATION 10000 - -/* Skip the first msec from the calculation */ -#define SKIP_DURATION 1000 - -/* Max frames per sec (to calculate number of delays to keep). */ -#define MAX_FRAMES_PER_SEC 100 - -/* Number of frame durations to keep */ -#define MAX_DELAY_COUNTER (((DURATION/1000)+1)*MAX_FRAMES_PER_SEC) - - -struct stream_data -{ - pj_uint32_t first_timestamp; - pj_uint32_t last_timestamp; - pj_timestamp last_called; - unsigned counter; - unsigned min_delay; - unsigned max_delay; - unsigned delay[MAX_DELAY_COUNTER]; -}; - -struct test_data { - pjmedia_dir dir; - unsigned clock_rate; - unsigned samples_per_frame; - unsigned channel_count; - pj_bool_t running; - pj_bool_t has_error; - pj_mutex_t *mutex; - - struct stream_data capture_data; - struct stream_data playback_data; -}; - - - -static const char *desc = - " sndtest.c \n" - " \n" - " PURPOSE: \n" - " Test the performance of sound device. \n" - " \n" - " USAGE: \n" - " sndtest --help \n" - " sndtest [options] \n" - " \n" - " where options: \n" - " --id=ID -i Use device ID (default is -1) \n" - " --rate=HZ -r Set test clock rate (default=8000)\n" - " --frame=SAMPLES -f Set number of samples per frame\n" - " --channel=CH -n Set number of channels (default=1)\n" - " --verbose -v Show verbose result \n" - " --help -h Show this screen \n" -; - - - -static void enum_devices(void) -{ - int i, count; - - count = pjmedia_snd_get_dev_count(); - if (count == 0) { - PJ_LOG(3,(THIS_FILE, "No devices found")); - return; - } - - PJ_LOG(3,(THIS_FILE, "Found %d devices:", count)); - for (i=0; iname, info->input_count, info->output_count)); - } -} - - -static pj_status_t play_cb(void *user_data, pj_uint32_t timestamp, - void *output, unsigned size) -{ - struct test_data *test_data = user_data; - struct stream_data *strm_data = &test_data->playback_data; - - pj_mutex_lock(test_data->mutex); - - /* Skip frames when test is not started or test has finished */ - if (!test_data->running) { - pj_bzero(output, size); - - pj_mutex_unlock(test_data->mutex); - return PJ_SUCCESS; - } - - /* Save last timestamp seen (to calculate drift) */ - strm_data->last_timestamp = timestamp; - - if (strm_data->last_called.u64 == 0) { - pj_get_timestamp(&strm_data->last_called); - /* Init min_delay to one frame */ - strm_data->min_delay = test_data->samples_per_frame * 1000000 / - test_data->clock_rate; - strm_data->first_timestamp = timestamp; - - } else if (strm_data->counter <= MAX_DELAY_COUNTER) { - pj_timestamp now; - unsigned delay; - - pj_get_timestamp(&now); - - /* Calculate frame interval */ - delay = pj_elapsed_usec(&strm_data->last_called, &now); - if (delay < strm_data->min_delay) - strm_data->min_delay = delay; - if (delay > strm_data->max_delay) - strm_data->max_delay = delay; - - strm_data->last_called = now; - - /* Save the frame interval for later calculation */ - strm_data->delay[strm_data->counter] = delay; - ++strm_data->counter; - - } else { - - /* No space, can't take anymore frames */ - test_data->running = 0; - - } - - pj_bzero(output, size); - - pj_mutex_unlock(test_data->mutex); - - return PJ_SUCCESS; -} - -static pj_status_t rec_cb(void *user_data, pj_uint32_t timestamp, - void *input, unsigned size) -{ - - struct test_data *test_data = user_data; - struct stream_data *strm_data = &test_data->capture_data; - - pj_mutex_lock(test_data->mutex); - - PJ_UNUSED_ARG(input); - PJ_UNUSED_ARG(size); - - /* Skip frames when test is not started or test has finished */ - if (!test_data->running) { - pj_mutex_unlock(test_data->mutex); - return PJ_SUCCESS; - } - - /* Save last timestamp seen (to calculate drift) */ - strm_data->last_timestamp = timestamp; - - if (strm_data->last_called.u64 == 0) { - pj_get_timestamp(&strm_data->last_called); - /* Init min_delay to one frame */ - strm_data->min_delay = test_data->samples_per_frame * 1000000 / - test_data->clock_rate; - strm_data->first_timestamp = timestamp; - - } else if (strm_data->counter <= MAX_DELAY_COUNTER) { - pj_timestamp now; - unsigned delay; - - pj_get_timestamp(&now); - - /* Calculate frame interval */ - delay = pj_elapsed_usec(&strm_data->last_called, &now); - if (delay < strm_data->min_delay) - strm_data->min_delay = delay; - if (delay > strm_data->max_delay) - strm_data->max_delay = delay; - - strm_data->last_called = now; - - /* Save the frame interval for later calculation */ - strm_data->delay[strm_data->counter] = delay; - ++strm_data->counter; - - } else { - - /* No space, can't take anymore frames */ - test_data->running = 0; - - } - - pj_mutex_unlock(test_data->mutex); - return PJ_SUCCESS; -} - -static void app_perror(const char *title, pj_status_t status) -{ - char errmsg[PJ_ERR_MSG_SIZE]; - - pj_strerror(status, errmsg, sizeof(errmsg)); - printf( "%s: %s (err=%d)\n", - title, errmsg, status); -} - - -static void print_stream_data(const char *title, - struct test_data *test_data, - struct stream_data *strm_data, - int verbose) -{ - unsigned i, dur; - int ptime; - unsigned min_jitter, max_jitter, sum_jitter, avg_jitter=0; - - PJ_LOG(3,(THIS_FILE, " %s stream report:", title)); - - /* Check that frames are captured/played */ - if (strm_data->counter == 0) { - PJ_LOG(1,(THIS_FILE, " Error: no frames are captured/played!")); - test_data->has_error = 1; - return; - } - - /* Duration */ - dur = (strm_data->counter+1) * test_data->samples_per_frame * 1000 / - test_data->clock_rate; - PJ_LOG(3,(THIS_FILE, " Duration: %ds.%03d", - dur/1000, dur%1000)); - - /* Frame interval */ - if (strm_data->max_delay - strm_data->min_delay < WARN_JITTER_USEC) { - PJ_LOG(3,(THIS_FILE, - " Frame interval: min=%d.%03dms, max=%d.%03dms", - strm_data->min_delay/1000, strm_data->min_delay%1000, - strm_data->max_delay/1000, strm_data->max_delay%1000)); - } else { - test_data->has_error = 1; - PJ_LOG(2,(THIS_FILE, - " Frame interval: min=%d.%03dms, max=%d.%03dms", - strm_data->min_delay/1000, strm_data->min_delay%1000, - strm_data->max_delay/1000, strm_data->max_delay%1000)); - } - - if (verbose) { - unsigned i; - unsigned decor = pj_log_get_decor(); - - PJ_LOG(3,(THIS_FILE, " Dumping frame delays:")); - - pj_log_set_decor(0); - for (i=0; icounter; ++i) - PJ_LOG(3,(THIS_FILE, " %d.%03d", strm_data->delay[i]/1000, - strm_data->delay[i]%1000)); - PJ_LOG(3,(THIS_FILE, "\r\n")); - pj_log_set_decor(decor); - } - - /* Calculate frame ptime in usec */ - ptime = test_data->samples_per_frame * 1000000 / - test_data->clock_rate; - - /* Calculate jitter */ - min_jitter = 0xFFFFF; - max_jitter = 0; - sum_jitter = 0; - - for (i=1; icounter; ++i) { - int jitter1, jitter2, jitter; - - /* jitter1 is interarrival difference */ - jitter1 = strm_data->delay[i] - strm_data->delay[i-1]; - if (jitter1 < 0) jitter1 = -jitter1; - - /* jitter2 is difference between actual and scheduled arrival. - * This is intended to capture situation when frames are coming - * instantaneously, which will calculate as zero jitter with - * jitter1 calculation. - */ - jitter2 = ptime - strm_data->delay[i]; - if (jitter2 < 0) jitter2 = -jitter2; - - /* Set jitter as the maximum of the two jitter calculations. - * This is intended to show the worst result. - */ - jitter = (jitter1>jitter2) ? jitter1 : jitter2; - - /* Calculate min, max, avg jitter */ - if (jitter < (int)min_jitter) min_jitter = jitter; - if (jitter > (int)max_jitter) max_jitter = jitter; - - sum_jitter += jitter; - } - - avg_jitter = (sum_jitter) / (strm_data->counter - 1); - - if (max_jitter < WARN_JITTER_USEC) { - PJ_LOG(3,(THIS_FILE, - " Jitter: min=%d.%03dms, avg=%d.%03dms, max=%d.%03dms", - min_jitter/1000, min_jitter%1000, - avg_jitter/1000, avg_jitter%1000, - max_jitter/1000, max_jitter%1000)); - } else { - test_data->has_error = 1; - PJ_LOG(2,(THIS_FILE, - " Jitter: min=%d.%03dms, avg=%d.%03dms, max=%d.%03dms", - min_jitter/1000, min_jitter%1000, - avg_jitter/1000, avg_jitter%1000, - max_jitter/1000, max_jitter%1000)); - } -} - - -static int perform_test(pj_pool_t *pool, int dev_id, pjmedia_dir dir, - unsigned clock_rate, unsigned samples_per_frame, - unsigned nchannel, int verbose) -{ - pj_status_t status = PJ_SUCCESS; - pjmedia_snd_stream *strm; - struct test_data test_data; - pjmedia_snd_stream_info si; - - /* - * Init test parameters - */ - pj_bzero(&test_data, sizeof(test_data)); - test_data.dir = dir; - test_data.clock_rate = clock_rate; - test_data.samples_per_frame = samples_per_frame; - test_data.channel_count = nchannel; - - pj_mutex_create_simple(pool, "sndtest", &test_data.mutex); - - /* - * Open device. - */ - if (dir == PJMEDIA_DIR_CAPTURE) { - status = pjmedia_snd_open_rec( dev_id, clock_rate, nchannel, - samples_per_frame, 16, &rec_cb, - &test_data, &strm); - } else if (dir == PJMEDIA_DIR_PLAYBACK) { - status = pjmedia_snd_open_player( dev_id, clock_rate, nchannel, - samples_per_frame, 16, &play_cb, - &test_data, &strm); - } else { - status = pjmedia_snd_open( dev_id, dev_id, clock_rate, nchannel, - samples_per_frame, 16, &rec_cb, &play_cb, - &test_data, &strm); - } - - if (status != PJ_SUCCESS) { - app_perror("Unable to open device for capture", status); - return status; - } - - pjmedia_snd_stream_get_info(strm, &si); - if (si.play_id >= 0) { - PJ_LOG(3,(THIS_FILE, "Testing playback device %s", - pjmedia_snd_get_dev_info(si.play_id)->name)); - } - if (si.rec_id >= 0) { - PJ_LOG(3,(THIS_FILE, "Testing capture device %s", - pjmedia_snd_get_dev_info(si.rec_id)->name)); - } - - /* Sleep for a while to let sound device "settles" */ - pj_thread_sleep(200); - - - /* - * Start the stream. - */ - status = pjmedia_snd_stream_start(strm); - if (status != PJ_SUCCESS) { - app_perror("Unable to start capture stream", status); - return status; - } - - PJ_LOG(3,(THIS_FILE, - " Please wait while test is in progress (~%d secs)..", - (DURATION+SKIP_DURATION)/1000)); - - /* Let the stream runs for few msec/sec to get stable result. - * (capture normally begins with frames available simultaneously). - */ - pj_thread_sleep(SKIP_DURATION); - - - /* Begin gather data */ - test_data.running = 1; - - /* - * Let the test runs for a while. - */ - pj_thread_sleep(DURATION); - - - /* - * Close stream. - */ - test_data.running = 0; - pjmedia_snd_stream_close(strm); - - - /* - * Print results. - */ - PJ_LOG(3,(THIS_FILE, " Dumping results:")); - - PJ_LOG(3,(THIS_FILE, " Parameters: clock rate=%dHz, %d samples/frame", - clock_rate, samples_per_frame)); - - if (dir & PJMEDIA_DIR_PLAYBACK) - print_stream_data("Playback", &test_data, &test_data.playback_data, - verbose); - if (dir & PJMEDIA_DIR_CAPTURE) - print_stream_data("Capture", &test_data, &test_data.capture_data, - verbose); - - /* Check drifting */ - if (dir == PJMEDIA_DIR_CAPTURE_PLAYBACK) { - int end_diff, start_diff, drift; - - end_diff = test_data.capture_data.last_timestamp - - test_data.playback_data.last_timestamp; - start_diff = test_data.capture_data.first_timestamp- - test_data.playback_data.first_timestamp; - drift = end_diff > start_diff? end_diff - start_diff : - start_diff - end_diff; - - PJ_LOG(3,(THIS_FILE, " Checking for clock drifts:")); - - /* Allow one frame tolerance for clock drift detection */ - if (drift < (int)samples_per_frame) { - PJ_LOG(3,(THIS_FILE, " No clock drifts is detected")); - } else { - const char *which = (drift<0 ? "slower" : "faster"); - unsigned msec_dur; - - if (drift < 0) drift = -drift; - - - msec_dur = (test_data.capture_data.last_timestamp - - test_data.capture_data.first_timestamp) * 1000 / - test_data.clock_rate; - - PJ_LOG(2,(THIS_FILE, - " Sound capture is %d samples %s than playback " - "at the end of the test (average is %d samples" - " per second)", - drift, which, - drift * 1000 / msec_dur)); - - } - } - - if (test_data.has_error == 0) { - PJ_LOG(3,(THIS_FILE, " Test completed, sound device looks okay.")); - return 0; - } else { - PJ_LOG(2,(THIS_FILE, " Test completed with some warnings")); - return 1; - } -} - - -int main(int argc, char *argv[]) -{ - pj_caching_pool cp; - pj_pool_t *pool; - pjmedia_endpt *med_endpt; - int id = -1, verbose = 0; - int clock_rate = 8000; - int frame = -1; - int channel = 1; - struct pj_getopt_option long_options[] = { - { "id", 1, 0, 'i' }, - { "rate", 1, 0, 'r' }, - { "frame", 1, 0, 'f' }, - { "channel", 1, 0, 'n' }, - { "verbose", 0, 0, 'v' }, - { "help", 0, 0, 'h' }, - { NULL, 0, 0, 0 } - }; - int c, option_index; - - - pj_status_t status; - - /* Init pjlib */ - status = pj_init(); - PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1); - - /* Must create a pool factory before we can allocate any memory. */ - pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); - - /* Also create pool for misc purposes */ - pool = pj_pool_create(&cp.factory, "sndtest", 1000, 1000, NULL); - - /* - * Initialize media endpoint. - * This will implicitly initialize PJMEDIA too. - */ - status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); - PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); - - /* Print devices */ - enum_devices(); - - /* Parse options */ - pj_optind = 0; - while((c=pj_getopt_long(argc,argv, "i:r:f:n:vh", - long_options, &option_index))!=-1) - { - switch (c) { - case 'i': - id = atoi(pj_optarg); - break; - case 'r': - clock_rate = atoi(pj_optarg); - break; - case 'f': - frame = atoi(pj_optarg); - break; - case 'n': - channel = atoi(pj_optarg); - break; - case 'v': - verbose = 1; - break; - case 'h': - puts(desc); - return 0; - break; - default: - printf("Error: invalid options %s\n", argv[pj_optind-1]); - puts(desc); - return 1; - } - } - - if (pj_optind != argc) { - printf("Error: invalid options\n"); - puts(desc); - return 1; - } - - if (!verbose) - pj_log_set_level(3); - - if (frame == -1) - frame = 10 * clock_rate / 1000; - - - status = perform_test(pool, id, PJMEDIA_DIR_CAPTURE_PLAYBACK, - clock_rate, frame, channel, verbose); - - pjmedia_endpt_destroy(med_endpt); - pj_pool_release(pool); - pj_caching_pool_destroy(&cp); - pj_shutdown(); - - return status == PJ_SUCCESS ? 0 : 1; -} - - -- cgit v1.2.3