blob: 50c26fc0d92080da07fa4979e0b3b7daadc368d9 [file] [log] [blame]
Frank Barchard4163c2f2017-06-28 15:44:40 -07001/* 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>
Dan Albertcddc0f02017-10-12 13:27:04 -070032#include <errno.h>
John Muir43ea4842017-07-05 15:30:01 -070033#include <math.h>
Frank Barchard4163c2f2017-06-28 15:44:40 -070034#include <stdio.h>
35#include <stdlib.h>
36#include <stdint.h>
37#include <string.h>
38#include <signal.h>
John Muir43ea4842017-07-05 15:30:01 -070039#include <time.h>
Frank Barchard4163c2f2017-06-28 15:44:40 -070040#include <unistd.h>
41
John Muir43ea4842017-07-05 15:30:01 -070042/* Used when that particular device isn't opened. */
43#define TINYHOSTLESS_DEVICE_UNDEFINED 255
44
Frank Barchard4163c2f2017-06-28 15:44:40 -070045static int close_h = 0;
46
John Muir43ea4842017-07-05 15:30:01 -070047int 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);
Frank Barchard4163c2f2017-06-28 15:44:40 -070052
John Muir43ea4842017-07-05 15:30:01 -070053static void stream_close(int sig)
Frank Barchard4163c2f2017-06-28 15:44:40 -070054{
55 /* allow the stream to be closed gracefully */
56 signal(sig, SIG_IGN);
57 close_h = 1;
58}
59
60int main(int argc, char **argv)
61{
62 unsigned int card = 0;
John Muir43ea4842017-07-05 15:30:01 -070063 unsigned int p_device = TINYHOSTLESS_DEVICE_UNDEFINED;
64 unsigned int c_device = TINYHOSTLESS_DEVICE_UNDEFINED;
65 unsigned int period_size = 192;
Frank Barchard4163c2f2017-06-28 15:44:40 -070066 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;
John Muir43ea4842017-07-05 15:30:01 -070071 unsigned int do_loopback = 0;
Frank Barchard4163c2f2017-06-28 15:44:40 -070072
73 if (argc < 2) {
John Muir43ea4842017-07-05 15:30:01 -070074 fprintf(stderr, "Usage: %s [-D card] [-P playback device]"
75 " [-C capture device] [-p period_size] [-n n_periods]"
Xinhui Zhou1a11a732021-10-13 22:22:53 -070076 " [-c num_channels] [-b number_bits] [-r sample_rate] [-l]"
John Muir43ea4842017-07-05 15:30:01 -070077 " [-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]);
Frank Barchard4163c2f2017-06-28 15:44:40 -070082 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 }
Xinhui Zhou1a11a732021-10-13 22:22:53 -0700113 if (strcmp(*argv, "-b") == 0) {
114 argv++;
115 if (*argv)
116 number_bits = atoi(*argv);
117 }
Frank Barchard4163c2f2017-06-28 15:44:40 -0700118 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 }
John Muir43ea4842017-07-05 15:30:01 -0700133 if (strcmp(*argv, "-l") == 0) {
134 do_loopback = 1;
135 }
Frank Barchard4163c2f2017-06-28 15:44:40 -0700136 if (*argv)
137 argv++;
138 }
139
John Muir43ea4842017-07-05 15:30:01 -0700140 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 }
Frank Barchard4163c2f2017-06-28 15:44:40 -0700145
John Muir43ea4842017-07-05 15:30:01 -0700146 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 }
Frank Barchard4163c2f2017-06-28 15:44:40 -0700151
John Muir43ea4842017-07-05 15:30:01 -0700152 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);
Frank Barchard4163c2f2017-06-28 15:44:40 -0700155}
156
John Muir43ea4842017-07-05 15:30:01 -0700157static 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
181static 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");
Xinhui Zhouacd55992021-10-13 22:53:10 -0700212 can_play &= check_param(params, PCM_PARAM_SAMPLE_BITS, bits, "Bitwidth", " bits");
John Muir43ea4842017-07-05 15:30:01 -0700213 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
221int 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)
Frank Barchard4163c2f2017-06-28 15:44:40 -0700226{
227 struct pcm_config config;
228 struct pcm *pcm_play =NULL, *pcm_cap=NULL;
Frank Barchard4163c2f2017-06-28 15:44:40 -0700229 unsigned int count =0;
John Muir43ea4842017-07-05 15:30:01 -0700230 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));
Frank Barchard4163c2f2017-06-28 15:44:40 -0700237 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;
John Muir43ea4842017-07-05 15:30:01 -0700243 else if (bits == 24)
244 config.format = PCM_FORMAT_S24_3LE;
Frank Barchard4163c2f2017-06-28 15:44:40 -0700245 else if (bits == 16)
246 config.format = PCM_FORMAT_S16_LE;
247 config.start_threshold = 0;
John Muir43ea4842017-07-05 15:30:01 -0700248 config.stop_threshold = 0;
Frank Barchard4163c2f2017-06-28 15:44:40 -0700249 config.avail_min = 0;
250
John Muir43ea4842017-07-05 15:30:01 -0700251 if(p_device < TINYHOSTLESS_DEVICE_UNDEFINED ) {
252 if (!check_params(card, p_device, PCM_OUT, &config))
253 return EINVAL;
Frank Barchard4163c2f2017-06-28 15:44:40 -0700254 pcm_play = pcm_open(card, p_device, PCM_OUT, &config);
255 if (!pcm_play || !pcm_is_ready(pcm_play)) {
John Muir43ea4842017-07-05 15:30:01 -0700256 fprintf(stderr, "Unable to open PCM playback device %u (%s)\n",
257 p_device, pcm_get_error(pcm_play));
258 return errno;
Frank Barchard4163c2f2017-06-28 15:44:40 -0700259 }
260 }
261
John Muir43ea4842017-07-05 15:30:01 -0700262 if (c_device < TINYHOSTLESS_DEVICE_UNDEFINED ) {
263 if (!check_params(card, c_device, PCM_IN, &config))
264 return EINVAL;
Frank Barchard4163c2f2017-06-28 15:44:40 -0700265 pcm_cap = pcm_open(card, c_device, PCM_IN, &config);
266 if (!pcm_cap || !pcm_is_ready(pcm_cap)) {
John Muir43ea4842017-07-05 15:30:01 -0700267 fprintf(stderr, "Unable to open PCM capture device %u (%s)\n",
Frank Barchard4163c2f2017-06-28 15:44:40 -0700268 c_device, pcm_get_error(pcm_cap));
269 if (pcm_play != NULL ) pcm_close(pcm_play);
John Muir43ea4842017-07-05 15:30:01 -0700270 return errno;
Frank Barchard4163c2f2017-06-28 15:44:40 -0700271 }
272 }
273
John Muir43ea4842017-07-05 15:30:01 -0700274 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 }
Frank Barchard4163c2f2017-06-28 15:44:40 -0700292
293 /* catch ctrl-c to shutdown cleanly */
294 signal(SIGINT, stream_close);
John Muir43ea4842017-07-05 15:30:01 -0700295 signal(SIGHUP, stream_close);
296 signal(SIGTERM, stream_close);
Frank Barchard4163c2f2017-06-28 15:44:40 -0700297
298 if (pcm_cap != NULL) pcm_start(pcm_cap);
299 if (pcm_play != NULL) pcm_start(pcm_play);
300
John Muir43ea4842017-07-05 15:30:01 -0700301 clock_gettime(CLOCK_MONOTONIC, &now);
302 end.tv_sec = now.tv_sec + play_cap_time;
303 end.tv_nsec = now.tv_nsec;
304
Frank Barchard4163c2f2017-06-28 15:44:40 -0700305 do {
John Muir43ea4842017-07-05 15:30:01 -0700306 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 }
Frank Barchard4163c2f2017-06-28 15:44:40 -0700327 } while(!close_h);
328
John Muir43ea4842017-07-05 15:30:01 -0700329 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;
Frank Barchard4163c2f2017-06-28 15:44:40 -0700336}