blob: ee9a385cb3ba1a14f473ca1f9dc158ac7530e376 [file] [log] [blame]
Benny Prijono204ce5e2006-03-20 16:59:23 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
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
20static 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 Prijonoa8df6542006-03-23 14:20:42 +000027 " sndinfo [id rec/play/both clockrate nchan bits] \n"
Benny Prijono204ce5e2006-03-20 16:59:23 +000028 " \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 Prijonoa8df6542006-03-23 14:20:42 +000035 " - 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 Prijono204ce5e2006-03-20 16:59:23 +000040
41#include <pjmedia.h>
42#include <pjlib.h>
43
44#include <stdlib.h> /* atoi() */
45#include <stdio.h>
46
47
48static 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 Prijonoa8df6542006-03-23 14:20:42 +000072 puts("");
73 puts("Run with -h to get more options");
Benny Prijono204ce5e2006-03-20 16:59:23 +000074}
75
Benny Prijono5012da82006-04-25 10:02:40 +000076static int clock_rate;
Benny Prijonoa8df6542006-03-23 14:20:42 +000077static int play_counter;
78static int rec_counter;
Benny Prijonoe452dda2006-04-03 09:43:36 +000079static int min_delay = 0xFFFF, max_delay;
80static char play_delays[1000];
Benny Prijono5012da82006-04-25 10:02:40 +000081static pj_uint32_t last_play_timestamp, last_rec_timestamp;
Benny Prijono204ce5e2006-03-20 16:59:23 +000082
83static pj_status_t play_cb(void *user_data, pj_uint32_t timestamp,
84 void *output, unsigned size)
85{
Benny Prijonoc708f932006-04-03 11:42:56 +000086 static pj_timestamp last_cb;
Benny Prijonoe452dda2006-04-03 09:43:36 +000087
Benny Prijonoa8df6542006-03-23 14:20:42 +000088 ++play_counter;
Benny Prijono5012da82006-04-25 10:02:40 +000089 last_play_timestamp = timestamp;
Benny Prijonoe452dda2006-04-03 09:43:36 +000090
Benny Prijonoc708f932006-04-03 11:42:56 +000091 if (last_cb.u64 == 0) {
92 pj_get_timestamp(&last_cb);
Benny Prijono5012da82006-04-25 10:02:40 +000093 } else if (play_counter <= PJ_ARRAY_SIZE(play_delays)) {
Benny Prijonoc708f932006-04-03 11:42:56 +000094 pj_timestamp now;
Benny Prijonoe452dda2006-04-03 09:43:36 +000095 int delay;
96
Benny Prijonoc708f932006-04-03 11:42:56 +000097 pj_get_timestamp(&now);
98
99 delay = pj_elapsed_msec(&last_cb, &now);
Benny Prijonoe452dda2006-04-03 09:43:36 +0000100 if (delay < min_delay)
101 min_delay = delay;
102 if (delay > max_delay)
103 max_delay = delay;
104
Benny Prijonoc708f932006-04-03 11:42:56 +0000105 last_cb = now;
Benny Prijonoe452dda2006-04-03 09:43:36 +0000106
107 play_delays[play_counter-1] = delay;
108 }
109
Benny Prijono204ce5e2006-03-20 16:59:23 +0000110 return PJ_SUCCESS;
111}
112
113static pj_status_t rec_cb(void *user_data, pj_uint32_t timestamp,
114 const void *input, unsigned size)
115{
Benny Prijonoa8df6542006-03-23 14:20:42 +0000116 ++rec_counter;
Benny Prijono5012da82006-04-25 10:02:40 +0000117
118 if (timestamp - last_rec_timestamp >= clock_rate) {
119 int diff;
120 diff = last_play_timestamp - timestamp;
121 printf("Play timestamp=%u, capture timestamp=%u, diff=%d\n",
122 last_play_timestamp, timestamp, diff);
123 last_rec_timestamp = timestamp;
124 }
Benny Prijono204ce5e2006-03-20 16:59:23 +0000125 return PJ_SUCCESS;
126}
127
Benny Prijonoa8df6542006-03-23 14:20:42 +0000128static void app_perror(const char *title, pj_status_t status)
Benny Prijono204ce5e2006-03-20 16:59:23 +0000129{
Benny Prijonoa8df6542006-03-23 14:20:42 +0000130 char errmsg[PJ_ERR_MSG_SIZE];
131
132 pj_strerror(status, errmsg, sizeof(errmsg));
133 printf( "%s: %s (err=%d)\n",
134 title, errmsg, status);
135}
Benny Prijonoe452dda2006-04-03 09:43:36 +0000136
Benny Prijonoa8df6542006-03-23 14:20:42 +0000137static int open_device(int dev_id, pjmedia_dir dir,
Benny Prijono5012da82006-04-25 10:02:40 +0000138 int nchannel, int bits)
Benny Prijonoa8df6542006-03-23 14:20:42 +0000139{
140 pj_status_t status = PJ_SUCCESS;
Benny Prijono204ce5e2006-03-20 16:59:23 +0000141 unsigned nsamples;
142 pjmedia_snd_stream *strm;
Benny Prijonoa8df6542006-03-23 14:20:42 +0000143 const char *dirtype;
Benny Prijono5012da82006-04-25 10:02:40 +0000144 char tmp[10];
Benny Prijonoe452dda2006-04-03 09:43:36 +0000145 unsigned i;
Benny Prijono204ce5e2006-03-20 16:59:23 +0000146
Benny Prijonoa8df6542006-03-23 14:20:42 +0000147 switch (dir) {
148 case PJMEDIA_DIR_CAPTURE:
149 dirtype = "capture"; break;
150 case PJMEDIA_DIR_PLAYBACK:
151 dirtype = "playback"; break;
152 case PJMEDIA_DIR_CAPTURE_PLAYBACK:
153 dirtype = "capture/playback"; break;
154 default:
155 return 1;
156 }
157
Benny Prijono204ce5e2006-03-20 16:59:23 +0000158 nsamples = clock_rate * 20 / 1000;
159
Benny Prijonoa8df6542006-03-23 14:20:42 +0000160 printf( "Opening device %d for %s: clockrate=%d, nchannel=%d, "
161 "bits=%d, nsamples=%d..\n",
162 dev_id, dirtype, clock_rate, nchannel, bits, nsamples);
Benny Prijono204ce5e2006-03-20 16:59:23 +0000163
Benny Prijonoa8df6542006-03-23 14:20:42 +0000164 if (dir == PJMEDIA_DIR_CAPTURE) {
165 status = pjmedia_snd_open_rec( dev_id, clock_rate, nchannel,
166 nsamples, bits, &rec_cb, NULL,
167 &strm);
168 } else if (dir == PJMEDIA_DIR_PLAYBACK) {
Benny Prijono204ce5e2006-03-20 16:59:23 +0000169 status = pjmedia_snd_open_player( dev_id, clock_rate, nchannel,
170 nsamples, bits, &play_cb, NULL,
171 &strm);
Benny Prijonoa8df6542006-03-23 14:20:42 +0000172 } else {
173 status = pjmedia_snd_open( dev_id, dev_id, clock_rate, nchannel,
174 nsamples, bits, &rec_cb, &play_cb, NULL,
175 &strm);
Benny Prijono204ce5e2006-03-20 16:59:23 +0000176 }
177
178 if (status != PJ_SUCCESS) {
Benny Prijonoa8df6542006-03-23 14:20:42 +0000179 app_perror("Unable to open device for capture", status);
180 return 1;
Benny Prijono204ce5e2006-03-20 16:59:23 +0000181 }
182
Benny Prijonoa8df6542006-03-23 14:20:42 +0000183 status = pjmedia_snd_stream_start(strm);
184 if (status != PJ_SUCCESS) {
185 app_perror("Unable to start capture stream", status);
186 return 1;
187 }
188
189 /* Let playback/capture runs for a while */
Benny Prijono5012da82006-04-25 10:02:40 +0000190 //pj_thread_sleep(1000);
191 puts("Press <ENTER> to stop");
192 fgets(tmp, sizeof(tmp), stdin);
193
Benny Prijonoa8df6542006-03-23 14:20:42 +0000194
Benny Prijono204ce5e2006-03-20 16:59:23 +0000195 pjmedia_snd_stream_close(strm);
Benny Prijonoa8df6542006-03-23 14:20:42 +0000196
197 if ((dir & PJMEDIA_DIR_CAPTURE) && rec_counter==0) {
198 printf("Error: capture stream was not running\n");
199 return 1;
200 }
201
202 if ((dir & PJMEDIA_DIR_PLAYBACK) && play_counter==0) {
203 printf("Error: playback stream was not running\n");
204 return 1;
205 }
206
207 puts("Success.");
Benny Prijonoe452dda2006-04-03 09:43:36 +0000208
209 printf("Delay: ");
210 for (i=0; i<play_counter; ++i)
211 printf("%d ", play_delays[i]);
212
213 puts("");
214 if (dir & PJMEDIA_DIR_PLAYBACK) {
215 printf("Callback interval: min interval=%d ms, max interval=%d ms\n",
216 min_delay, max_delay);
217 }
218
219
Benny Prijono204ce5e2006-03-20 16:59:23 +0000220 return 0;
221}
222
223
224int main(int argc, char *argv[])
225{
226 pj_caching_pool cp;
227 pjmedia_endpt *med_endpt;
228 pj_status_t status;
229
230 /* Init pjlib */
231 status = pj_init();
232 PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
233
234 /* Must create a pool factory before we can allocate any memory. */
235 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
236
237 /*
238 * Initialize media endpoint.
239 * This will implicitly initialize PJMEDIA too.
240 */
Benny Prijono275fd682006-03-22 11:59:11 +0000241 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
Benny Prijono204ce5e2006-03-20 16:59:23 +0000242 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
243
244
245 if (argc == 1) {
246 enum_devices();
247 return 0;
248 } else if (argc == 6) {
249
250 int dev_id;
Benny Prijonoa8df6542006-03-23 14:20:42 +0000251 pjmedia_dir dir;
Benny Prijono204ce5e2006-03-20 16:59:23 +0000252 int nchannel;
253 int bits;
254
255 dev_id = atoi(argv[1]);
Benny Prijonoa8df6542006-03-23 14:20:42 +0000256
257 if (strcmp(argv[2], "rec")==0)
258 dir = PJMEDIA_DIR_CAPTURE;
259 else if (strcmp(argv[2], "play")==0)
260 dir = PJMEDIA_DIR_PLAYBACK;
261 else if (strcmp(argv[2], "both")==0)
262 dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
263
Benny Prijono204ce5e2006-03-20 16:59:23 +0000264 clock_rate = atoi(argv[3]);
265 nchannel = atoi(argv[4]);
266 bits = atoi(argv[5]);
267
Benny Prijono5012da82006-04-25 10:02:40 +0000268 return open_device(dev_id, dir, nchannel, bits);
Benny Prijono204ce5e2006-03-20 16:59:23 +0000269
270 } else {
271 puts("Error: invalid arguments");
272 puts(desc);
273 return 1;
274 }
275
276 return 0;
277}
278
279