1 /* tinyhostless.c
2 **
3 ** Copyright 2011, The Android Open Source Project
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions are met:
7 ** * Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
9 ** * Redistributions in binary form must reproduce the above copyright
10 ** notice, this list of conditions and the following disclaimer in the
11 ** documentation and/or other materials provided with the distribution.
12 ** * Neither the name of The Android Open Source Project nor the names of
13 ** its contributors may be used to endorse or promote products derived
14 ** from this software without specific prior written permission.
15 **
16 ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 ** DAMAGE.
27 */
28
29 /* Playback data to a PCM device recorded from a capture PCM device. */
30
31 #include <tinyalsa/asoundlib.h>
32 #include <errno.h>
33 #include <math.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <string.h>
38 #include <signal.h>
39 #include <time.h>
40 #include <unistd.h>
41
42 /* Used when that particular device isn't opened. */
43 #define TINYHOSTLESS_DEVICE_UNDEFINED 255
44
45 static int close_h = 0;
46
47 int play_sample(unsigned int card, unsigned int p_device,
48 unsigned int c_device, unsigned int channels,
49 unsigned int rate, unsigned int bits, unsigned int period_size,
50 unsigned int period_count, unsigned int play_cap_time,
51 int do_loopback);
52
stream_close(int sig)53 static void stream_close(int sig)
54 {
55 /* allow the stream to be closed gracefully */
56 signal(sig, SIG_IGN);
57 close_h = 1;
58 }
59
main(int argc,char ** argv)60 int main(int argc, char **argv)
61 {
62 unsigned int card = 0;
63 unsigned int p_device = TINYHOSTLESS_DEVICE_UNDEFINED;
64 unsigned int c_device = TINYHOSTLESS_DEVICE_UNDEFINED;
65 unsigned int period_size = 192;
66 unsigned int period_count = 4;
67 unsigned int number_bits = 16;
68 unsigned int num_channels = 2;
69 unsigned int sample_rate = 48000;
70 unsigned int play_cap_time = 0;
71 unsigned int do_loopback = 0;
72
73 if (argc < 2) {
74 fprintf(stderr, "Usage: %s [-D card] [-P playback device]"
75 " [-C capture device] [-p period_size] [-n n_periods]"
76 " [-c num_channels] [-b number_bits] [-r sample_rate] [-l]"
77 " [-T playback/capture time]\n\n"
78 "Used to enable 'hostless' mode for audio devices with a DSP back-end.\n"
79 "Alternatively, specify '-l' for loopback mode: this program will read\n"
80 "from the capture device and write to the playback device.\n",
81 argv[0]);
82 return 1;
83 }
84
85 /* parse command line arguments */
86 argv += 1;
87 while (*argv) {
88 if (strcmp(*argv, "-P") == 0) {
89 argv++;
90 if (*argv)
91 p_device = atoi(*argv);
92 }
93 if (strcmp(*argv, "-C") == 0) {
94 argv++;
95 if (*argv)
96 c_device = atoi(*argv);
97 }
98 if (strcmp(*argv, "-p") == 0) {
99 argv++;
100 if (*argv)
101 period_size = atoi(*argv);
102 }
103 if (strcmp(*argv, "-n") == 0) {
104 argv++;
105 if (*argv)
106 period_count = atoi(*argv);
107 }
108 if (strcmp(*argv, "-c") == 0) {
109 argv++;
110 if (*argv)
111 num_channels = atoi(*argv);
112 }
113 if (strcmp(*argv, "-b") == 0) {
114 argv++;
115 if (*argv)
116 number_bits = atoi(*argv);
117 }
118 if (strcmp(*argv, "-r") == 0) {
119 argv++;
120 if (*argv)
121 sample_rate = atoi(*argv);
122 }
123 if (strcmp(*argv, "-T") == 0) {
124 argv++;
125 if (*argv)
126 play_cap_time = atoi(*argv);
127 }
128 if (strcmp(*argv, "-D") == 0) {
129 argv++;
130 if (*argv)
131 card = atoi(*argv);
132 }
133 if (strcmp(*argv, "-l") == 0) {
134 do_loopback = 1;
135 }
136 if (*argv)
137 argv++;
138 }
139
140 if (p_device == TINYHOSTLESS_DEVICE_UNDEFINED &&
141 c_device == TINYHOSTLESS_DEVICE_UNDEFINED) {
142 fprintf(stderr, "Specify at least one of -C (capture device) or -P (playback device).\n");
143 return EINVAL;
144 }
145
146 if (do_loopback && (p_device == TINYHOSTLESS_DEVICE_UNDEFINED ||
147 c_device == TINYHOSTLESS_DEVICE_UNDEFINED)) {
148 fprintf(stderr, "Loopback requires both playback and capture devices.\n");
149 return EINVAL;
150 }
151
152 return play_sample(card, p_device, c_device, num_channels, sample_rate,
153 number_bits, period_size, period_count, play_cap_time,
154 do_loopback);
155 }
156
check_param(struct pcm_params * params,unsigned int param,unsigned int value,char * param_name,char * param_unit)157 static int check_param(struct pcm_params *params, unsigned int param,
158 unsigned int value, char *param_name, char *param_unit)
159 {
160 unsigned int min;
161 unsigned int max;
162 int is_within_bounds = 1;
163
164 min = pcm_params_get_min(params, param);
165 if (value < min) {
166 fprintf(stderr, "%s is %u%s, device only supports >= %u%s\n", param_name, value,
167 param_unit, min, param_unit);
168 is_within_bounds = 0;
169 }
170
171 max = pcm_params_get_max(params, param);
172 if (value > max) {
173 fprintf(stderr, "%s is %u%s, device only supports <= %u%s\n", param_name, value,
174 param_unit, max, param_unit);
175 is_within_bounds = 0;
176 }
177
178 return is_within_bounds;
179 }
180
check_params(unsigned int card,unsigned int device,unsigned int direction,const struct pcm_config * config)181 static int check_params(unsigned int card, unsigned int device, unsigned int direction,
182 const struct pcm_config *config)
183 {
184 struct pcm_params *params;
185 int can_play;
186 int bits;
187
188 params = pcm_params_get(card, device, direction);
189 if (params == NULL) {
190 fprintf(stderr, "Unable to open PCM %s device %u.\n",
191 direction == PCM_OUT ? "playback" : "capture", device);
192 return 0;
193 }
194
195 switch (config->format) {
196 case PCM_FORMAT_S32_LE:
197 bits = 32;
198 break;
199 case PCM_FORMAT_S24_3LE:
200 bits = 24;
201 break;
202 case PCM_FORMAT_S16_LE:
203 bits = 16;
204 break;
205 default:
206 fprintf(stderr, "Invalid format: %u", config->format);
207 return 0;
208 }
209
210 can_play = check_param(params, PCM_PARAM_RATE, config->rate, "Sample rate", "Hz");
211 can_play &= check_param(params, PCM_PARAM_CHANNELS, config->channels, "Sample", " channels");
212 can_play &= check_param(params, PCM_PARAM_SAMPLE_BITS, bits, "Bitwidth", " bits");
213 can_play &= check_param(params, PCM_PARAM_PERIOD_SIZE, config->period_size, "Period size", " frames");
214 can_play &= check_param(params, PCM_PARAM_PERIODS, config->period_count, "Period count", " periods");
215
216 pcm_params_free(params);
217
218 return can_play;
219 }
220
play_sample(unsigned int card,unsigned int p_device,unsigned int c_device,unsigned int channels,unsigned int rate,unsigned int bits,unsigned int period_size,unsigned int period_count,unsigned int play_cap_time,int do_loopback)221 int play_sample(unsigned int card, unsigned int p_device,
222 unsigned int c_device, unsigned int channels,
223 unsigned int rate, unsigned int bits, unsigned int period_size,
224 unsigned int period_count, unsigned int play_cap_time,
225 int do_loopback)
226 {
227 struct pcm_config config;
228 struct pcm *pcm_play =NULL, *pcm_cap=NULL;
229 unsigned int count =0;
230 char *buffer = NULL;
231 int size = 0;
232 int rc = 0;
233 struct timespec end;
234 struct timespec now;
235
236 memset(&config, 0, sizeof(config));
237 config.channels = channels;
238 config.rate = rate;
239 config.period_size = period_size;
240 config.period_count = period_count;
241 if (bits == 32)
242 config.format = PCM_FORMAT_S32_LE;
243 else if (bits == 24)
244 config.format = PCM_FORMAT_S24_3LE;
245 else if (bits == 16)
246 config.format = PCM_FORMAT_S16_LE;
247 config.start_threshold = 0;
248 config.stop_threshold = 0;
249 config.avail_min = 0;
250
251 if(p_device < TINYHOSTLESS_DEVICE_UNDEFINED ) {
252 if (!check_params(card, p_device, PCM_OUT, &config))
253 return EINVAL;
254 pcm_play = pcm_open(card, p_device, PCM_OUT, &config);
255 if (!pcm_play || !pcm_is_ready(pcm_play)) {
256 fprintf(stderr, "Unable to open PCM playback device %u (%s)\n",
257 p_device, pcm_get_error(pcm_play));
258 return errno;
259 }
260 }
261
262 if (c_device < TINYHOSTLESS_DEVICE_UNDEFINED ) {
263 if (!check_params(card, c_device, PCM_IN, &config))
264 return EINVAL;
265 pcm_cap = pcm_open(card, c_device, PCM_IN, &config);
266 if (!pcm_cap || !pcm_is_ready(pcm_cap)) {
267 fprintf(stderr, "Unable to open PCM capture device %u (%s)\n",
268 c_device, pcm_get_error(pcm_cap));
269 if (pcm_play != NULL ) pcm_close(pcm_play);
270 return errno;
271 }
272 }
273
274 printf("%s: Playing device %u, Capture Device %u\n",
275 do_loopback ? "Loopback" : "Hostless", p_device, c_device);
276 printf("Sample: %u ch, %u hz, %u bit\n", channels, rate, bits);
277 if (play_cap_time)
278 printf("Duration in sec: %u\n", play_cap_time);
279 else
280 printf("Duration in sec: forever\n");
281
282 if (do_loopback) {
283 size = pcm_frames_to_bytes(pcm_cap, pcm_get_buffer_size(pcm_cap));
284 buffer = malloc(size);
285 if (!buffer) {
286 fprintf(stderr, "Unable to allocate %d bytes\n", size);
287 pcm_close(pcm_play);
288 pcm_close(pcm_cap);
289 return ENOMEM;
290 }
291 }
292
293 /* catch ctrl-c to shutdown cleanly */
294 signal(SIGINT, stream_close);
295 signal(SIGHUP, stream_close);
296 signal(SIGTERM, stream_close);
297
298 if (pcm_cap != NULL) pcm_start(pcm_cap);
299 if (pcm_play != NULL) pcm_start(pcm_play);
300
301 clock_gettime(CLOCK_MONOTONIC, &now);
302 end.tv_sec = now.tv_sec + play_cap_time;
303 end.tv_nsec = now.tv_nsec;
304
305 do {
306 if (do_loopback) {
307 if (pcm_read(pcm_cap, buffer, size)) {
308 fprintf(stderr, "Unable to read from PCM capture device %u (%s)\n",
309 c_device, pcm_get_error(pcm_cap));
310 rc = errno;
311 break;
312 }
313 if (pcm_write(pcm_play, buffer, size)) {
314 fprintf(stderr, "Unable to write to PCM playback device %u (%s)\n",
315 p_device, pcm_get_error(pcm_play));
316 break;
317 }
318 } else {
319 usleep(100000);
320 }
321 if (play_cap_time) {
322 clock_gettime(CLOCK_MONOTONIC, &now);
323 if (now.tv_sec > end.tv_sec ||
324 (now.tv_sec == end.tv_sec && now.tv_nsec >= end.tv_nsec))
325 break;
326 }
327 } while(!close_h);
328
329 if (buffer)
330 free(buffer);
331 if (pcm_play != NULL)
332 pcm_close(pcm_play);
333 if (pcm_cap != NULL)
334 pcm_close(pcm_cap);
335 return rc;
336 }
337