blob: 07e5df66c0aa406926cd3d4e83e66a34f08e5de4 [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 Prijonoa8df6542006-03-23 14:20:42 +000076static int play_counter;
77static int rec_counter;
Benny Prijonoe452dda2006-04-03 09:43:36 +000078static int min_delay = 0xFFFF, max_delay;
79static char play_delays[1000];
Benny Prijono204ce5e2006-03-20 16:59:23 +000080
81static pj_status_t play_cb(void *user_data, pj_uint32_t timestamp,
82 void *output, unsigned size)
83{
Benny Prijonoe452dda2006-04-03 09:43:36 +000084 static pj_time_val last_cb;
85
Benny Prijonoa8df6542006-03-23 14:20:42 +000086 ++play_counter;
Benny Prijonoe452dda2006-04-03 09:43:36 +000087
88 if (last_cb.sec == 0 && last_cb.msec == 0) {
89 pj_gettimeofday(&last_cb);
90 } else {
91 pj_time_val now, saved;
92 int delay;
93
94 pj_gettimeofday(&now);
95 saved = now;
96 PJ_TIME_VAL_SUB(now, last_cb);
97 delay = PJ_TIME_VAL_MSEC(now);
98
99 if (delay < min_delay)
100 min_delay = delay;
101 if (delay > max_delay)
102 max_delay = delay;
103
104 last_cb = saved;
105
106 play_delays[play_counter-1] = delay;
107 }
108
Benny Prijono204ce5e2006-03-20 16:59:23 +0000109 return PJ_SUCCESS;
110}
111
112static pj_status_t rec_cb(void *user_data, pj_uint32_t timestamp,
113 const void *input, unsigned size)
114{
Benny Prijonoa8df6542006-03-23 14:20:42 +0000115 ++rec_counter;
Benny Prijono204ce5e2006-03-20 16:59:23 +0000116 return PJ_SUCCESS;
117}
118
Benny Prijonoa8df6542006-03-23 14:20:42 +0000119static void app_perror(const char *title, pj_status_t status)
Benny Prijono204ce5e2006-03-20 16:59:23 +0000120{
Benny Prijonoa8df6542006-03-23 14:20:42 +0000121 char errmsg[PJ_ERR_MSG_SIZE];
122
123 pj_strerror(status, errmsg, sizeof(errmsg));
124 printf( "%s: %s (err=%d)\n",
125 title, errmsg, status);
126}
Benny Prijonoe452dda2006-04-03 09:43:36 +0000127
Benny Prijonoa8df6542006-03-23 14:20:42 +0000128static int open_device(int dev_id, pjmedia_dir dir,
129 int clock_rate, int nchannel, int bits)
130{
131 pj_status_t status = PJ_SUCCESS;
Benny Prijono204ce5e2006-03-20 16:59:23 +0000132 unsigned nsamples;
133 pjmedia_snd_stream *strm;
Benny Prijonoa8df6542006-03-23 14:20:42 +0000134 const char *dirtype;
Benny Prijonoe452dda2006-04-03 09:43:36 +0000135 unsigned i;
Benny Prijono204ce5e2006-03-20 16:59:23 +0000136
Benny Prijonoa8df6542006-03-23 14:20:42 +0000137 switch (dir) {
138 case PJMEDIA_DIR_CAPTURE:
139 dirtype = "capture"; break;
140 case PJMEDIA_DIR_PLAYBACK:
141 dirtype = "playback"; break;
142 case PJMEDIA_DIR_CAPTURE_PLAYBACK:
143 dirtype = "capture/playback"; break;
144 default:
145 return 1;
146 }
147
Benny Prijono204ce5e2006-03-20 16:59:23 +0000148 nsamples = clock_rate * 20 / 1000;
149
Benny Prijonoa8df6542006-03-23 14:20:42 +0000150 printf( "Opening device %d for %s: clockrate=%d, nchannel=%d, "
151 "bits=%d, nsamples=%d..\n",
152 dev_id, dirtype, clock_rate, nchannel, bits, nsamples);
Benny Prijono204ce5e2006-03-20 16:59:23 +0000153
Benny Prijonoa8df6542006-03-23 14:20:42 +0000154 if (dir == PJMEDIA_DIR_CAPTURE) {
155 status = pjmedia_snd_open_rec( dev_id, clock_rate, nchannel,
156 nsamples, bits, &rec_cb, NULL,
157 &strm);
158 } else if (dir == PJMEDIA_DIR_PLAYBACK) {
Benny Prijono204ce5e2006-03-20 16:59:23 +0000159 status = pjmedia_snd_open_player( dev_id, clock_rate, nchannel,
160 nsamples, bits, &play_cb, NULL,
161 &strm);
Benny Prijonoa8df6542006-03-23 14:20:42 +0000162 } else {
163 status = pjmedia_snd_open( dev_id, dev_id, clock_rate, nchannel,
164 nsamples, bits, &rec_cb, &play_cb, NULL,
165 &strm);
Benny Prijono204ce5e2006-03-20 16:59:23 +0000166 }
167
168 if (status != PJ_SUCCESS) {
Benny Prijonoa8df6542006-03-23 14:20:42 +0000169 app_perror("Unable to open device for capture", status);
170 return 1;
Benny Prijono204ce5e2006-03-20 16:59:23 +0000171 }
172
Benny Prijonoa8df6542006-03-23 14:20:42 +0000173 status = pjmedia_snd_stream_start(strm);
174 if (status != PJ_SUCCESS) {
175 app_perror("Unable to start capture stream", status);
176 return 1;
177 }
178
179 /* Let playback/capture runs for a while */
180 pj_thread_sleep(1000);
181
Benny Prijono204ce5e2006-03-20 16:59:23 +0000182 pjmedia_snd_stream_close(strm);
Benny Prijonoa8df6542006-03-23 14:20:42 +0000183
184 if ((dir & PJMEDIA_DIR_CAPTURE) && rec_counter==0) {
185 printf("Error: capture stream was not running\n");
186 return 1;
187 }
188
189 if ((dir & PJMEDIA_DIR_PLAYBACK) && play_counter==0) {
190 printf("Error: playback stream was not running\n");
191 return 1;
192 }
193
194 puts("Success.");
Benny Prijonoe452dda2006-04-03 09:43:36 +0000195
196 printf("Delay: ");
197 for (i=0; i<play_counter; ++i)
198 printf("%d ", play_delays[i]);
199
200 puts("");
201 if (dir & PJMEDIA_DIR_PLAYBACK) {
202 printf("Callback interval: min interval=%d ms, max interval=%d ms\n",
203 min_delay, max_delay);
204 }
205
206
Benny Prijono204ce5e2006-03-20 16:59:23 +0000207 return 0;
208}
209
210
211int main(int argc, char *argv[])
212{
213 pj_caching_pool cp;
214 pjmedia_endpt *med_endpt;
215 pj_status_t status;
216
217 /* Init pjlib */
218 status = pj_init();
219 PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
220
221 /* Must create a pool factory before we can allocate any memory. */
222 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
223
224 /*
225 * Initialize media endpoint.
226 * This will implicitly initialize PJMEDIA too.
227 */
Benny Prijono275fd682006-03-22 11:59:11 +0000228 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
Benny Prijono204ce5e2006-03-20 16:59:23 +0000229 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
230
231
232 if (argc == 1) {
233 enum_devices();
234 return 0;
235 } else if (argc == 6) {
236
237 int dev_id;
Benny Prijonoa8df6542006-03-23 14:20:42 +0000238 pjmedia_dir dir;
Benny Prijono204ce5e2006-03-20 16:59:23 +0000239 int clock_rate;
240 int nchannel;
241 int bits;
242
243 dev_id = atoi(argv[1]);
Benny Prijonoa8df6542006-03-23 14:20:42 +0000244
245 if (strcmp(argv[2], "rec")==0)
246 dir = PJMEDIA_DIR_CAPTURE;
247 else if (strcmp(argv[2], "play")==0)
248 dir = PJMEDIA_DIR_PLAYBACK;
249 else if (strcmp(argv[2], "both")==0)
250 dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
251
Benny Prijono204ce5e2006-03-20 16:59:23 +0000252 clock_rate = atoi(argv[3]);
253 nchannel = atoi(argv[4]);
254 bits = atoi(argv[5]);
255
Benny Prijonoa8df6542006-03-23 14:20:42 +0000256 return open_device(dev_id, dir, clock_rate, nchannel, bits);
Benny Prijono204ce5e2006-03-20 16:59:23 +0000257
258 } else {
259 puts("Error: invalid arguments");
260 puts(desc);
261 return 1;
262 }
263
264 return 0;
265}
266
267