Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 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> |
Dan Albert | cddc0f0 | 2017-10-12 13:27:04 -0700 | [diff] [blame] | 32 | #include <errno.h> |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 33 | #include <math.h> |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <stdint.h> |
| 37 | #include <string.h> |
| 38 | #include <signal.h> |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 39 | #include <time.h> |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 40 | #include <unistd.h> |
| 41 | |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 42 | /* Used when that particular device isn't opened. */ |
| 43 | #define TINYHOSTLESS_DEVICE_UNDEFINED 255 |
| 44 | |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 45 | static int close_h = 0; |
| 46 | |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 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); |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 52 | |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 53 | static void stream_close(int sig) |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 54 | { |
| 55 | /* allow the stream to be closed gracefully */ |
| 56 | signal(sig, SIG_IGN); |
| 57 | close_h = 1; |
| 58 | } |
| 59 | |
| 60 | int main(int argc, char **argv) |
| 61 | { |
| 62 | unsigned int card = 0; |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 63 | unsigned int p_device = TINYHOSTLESS_DEVICE_UNDEFINED; |
| 64 | unsigned int c_device = TINYHOSTLESS_DEVICE_UNDEFINED; |
| 65 | unsigned int period_size = 192; |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 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; |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 71 | unsigned int do_loopback = 0; |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 72 | |
| 73 | if (argc < 2) { |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 74 | fprintf(stderr, "Usage: %s [-D card] [-P playback device]" |
| 75 | " [-C capture device] [-p period_size] [-n n_periods]" |
| 76 | " [-c num_channels] [-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]); |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 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, "-r") == 0) { |
| 114 | argv++; |
| 115 | if (*argv) |
| 116 | sample_rate = atoi(*argv); |
| 117 | } |
| 118 | if (strcmp(*argv, "-T") == 0) { |
| 119 | argv++; |
| 120 | if (*argv) |
| 121 | play_cap_time = atoi(*argv); |
| 122 | } |
| 123 | if (strcmp(*argv, "-D") == 0) { |
| 124 | argv++; |
| 125 | if (*argv) |
| 126 | card = atoi(*argv); |
| 127 | } |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 128 | if (strcmp(*argv, "-l") == 0) { |
| 129 | do_loopback = 1; |
| 130 | } |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 131 | if (*argv) |
| 132 | argv++; |
| 133 | } |
| 134 | |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 135 | if (p_device == TINYHOSTLESS_DEVICE_UNDEFINED && |
| 136 | c_device == TINYHOSTLESS_DEVICE_UNDEFINED) { |
| 137 | fprintf(stderr, "Specify at least one of -C (capture device) or -P (playback device).\n"); |
| 138 | return EINVAL; |
| 139 | } |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 140 | |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 141 | if (do_loopback && (p_device == TINYHOSTLESS_DEVICE_UNDEFINED || |
| 142 | c_device == TINYHOSTLESS_DEVICE_UNDEFINED)) { |
| 143 | fprintf(stderr, "Loopback requires both playback and capture devices.\n"); |
| 144 | return EINVAL; |
| 145 | } |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 146 | |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 147 | return play_sample(card, p_device, c_device, num_channels, sample_rate, |
| 148 | number_bits, period_size, period_count, play_cap_time, |
| 149 | do_loopback); |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 150 | } |
| 151 | |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 152 | static int check_param(struct pcm_params *params, unsigned int param, |
| 153 | unsigned int value, char *param_name, char *param_unit) |
| 154 | { |
| 155 | unsigned int min; |
| 156 | unsigned int max; |
| 157 | int is_within_bounds = 1; |
| 158 | |
| 159 | min = pcm_params_get_min(params, param); |
| 160 | if (value < min) { |
| 161 | fprintf(stderr, "%s is %u%s, device only supports >= %u%s\n", param_name, value, |
| 162 | param_unit, min, param_unit); |
| 163 | is_within_bounds = 0; |
| 164 | } |
| 165 | |
| 166 | max = pcm_params_get_max(params, param); |
| 167 | if (value > max) { |
| 168 | fprintf(stderr, "%s is %u%s, device only supports <= %u%s\n", param_name, value, |
| 169 | param_unit, max, param_unit); |
| 170 | is_within_bounds = 0; |
| 171 | } |
| 172 | |
| 173 | return is_within_bounds; |
| 174 | } |
| 175 | |
| 176 | static int check_params(unsigned int card, unsigned int device, unsigned int direction, |
| 177 | const struct pcm_config *config) |
| 178 | { |
| 179 | struct pcm_params *params; |
| 180 | int can_play; |
| 181 | int bits; |
| 182 | |
| 183 | params = pcm_params_get(card, device, direction); |
| 184 | if (params == NULL) { |
| 185 | fprintf(stderr, "Unable to open PCM %s device %u.\n", |
| 186 | direction == PCM_OUT ? "playback" : "capture", device); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | switch (config->format) { |
| 191 | case PCM_FORMAT_S32_LE: |
| 192 | bits = 32; |
| 193 | break; |
| 194 | case PCM_FORMAT_S24_3LE: |
| 195 | bits = 24; |
| 196 | break; |
| 197 | case PCM_FORMAT_S16_LE: |
| 198 | bits = 16; |
| 199 | break; |
| 200 | default: |
| 201 | fprintf(stderr, "Invalid format: %u", config->format); |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | can_play = check_param(params, PCM_PARAM_RATE, config->rate, "Sample rate", "Hz"); |
| 206 | can_play &= check_param(params, PCM_PARAM_CHANNELS, config->channels, "Sample", " channels"); |
| 207 | can_play &= check_param(params, PCM_PARAM_SAMPLE_BITS, bits, "Bitrate", " bits"); |
| 208 | can_play &= check_param(params, PCM_PARAM_PERIOD_SIZE, config->period_size, "Period size", " frames"); |
| 209 | can_play &= check_param(params, PCM_PARAM_PERIODS, config->period_count, "Period count", " periods"); |
| 210 | |
| 211 | pcm_params_free(params); |
| 212 | |
| 213 | return can_play; |
| 214 | } |
| 215 | |
| 216 | int play_sample(unsigned int card, unsigned int p_device, |
| 217 | unsigned int c_device, unsigned int channels, |
| 218 | unsigned int rate, unsigned int bits, unsigned int period_size, |
| 219 | unsigned int period_count, unsigned int play_cap_time, |
| 220 | int do_loopback) |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 221 | { |
| 222 | struct pcm_config config; |
| 223 | struct pcm *pcm_play =NULL, *pcm_cap=NULL; |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 224 | unsigned int count =0; |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 225 | char *buffer = NULL; |
| 226 | int size = 0; |
| 227 | int rc = 0; |
| 228 | struct timespec end; |
| 229 | struct timespec now; |
| 230 | |
| 231 | memset(&config, 0, sizeof(config)); |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 232 | config.channels = channels; |
| 233 | config.rate = rate; |
| 234 | config.period_size = period_size; |
| 235 | config.period_count = period_count; |
| 236 | if (bits == 32) |
| 237 | config.format = PCM_FORMAT_S32_LE; |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 238 | else if (bits == 24) |
| 239 | config.format = PCM_FORMAT_S24_3LE; |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 240 | else if (bits == 16) |
| 241 | config.format = PCM_FORMAT_S16_LE; |
| 242 | config.start_threshold = 0; |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 243 | config.stop_threshold = 0; |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 244 | config.avail_min = 0; |
| 245 | |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 246 | if(p_device < TINYHOSTLESS_DEVICE_UNDEFINED ) { |
| 247 | if (!check_params(card, p_device, PCM_OUT, &config)) |
| 248 | return EINVAL; |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 249 | pcm_play = pcm_open(card, p_device, PCM_OUT, &config); |
| 250 | if (!pcm_play || !pcm_is_ready(pcm_play)) { |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 251 | fprintf(stderr, "Unable to open PCM playback device %u (%s)\n", |
| 252 | p_device, pcm_get_error(pcm_play)); |
| 253 | return errno; |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 257 | if (c_device < TINYHOSTLESS_DEVICE_UNDEFINED ) { |
| 258 | if (!check_params(card, c_device, PCM_IN, &config)) |
| 259 | return EINVAL; |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 260 | pcm_cap = pcm_open(card, c_device, PCM_IN, &config); |
| 261 | if (!pcm_cap || !pcm_is_ready(pcm_cap)) { |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 262 | fprintf(stderr, "Unable to open PCM capture device %u (%s)\n", |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 263 | c_device, pcm_get_error(pcm_cap)); |
| 264 | if (pcm_play != NULL ) pcm_close(pcm_play); |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 265 | return errno; |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 269 | printf("%s: Playing device %u, Capture Device %u\n", |
| 270 | do_loopback ? "Loopback" : "Hostless", p_device, c_device); |
| 271 | printf("Sample: %u ch, %u hz, %u bit\n", channels, rate, bits); |
| 272 | if (play_cap_time) |
| 273 | printf("Duration in sec: %u\n", play_cap_time); |
| 274 | else |
| 275 | printf("Duration in sec: forever\n"); |
| 276 | |
| 277 | if (do_loopback) { |
| 278 | size = pcm_frames_to_bytes(pcm_cap, pcm_get_buffer_size(pcm_cap)); |
| 279 | buffer = malloc(size); |
| 280 | if (!buffer) { |
| 281 | fprintf(stderr, "Unable to allocate %d bytes\n", size); |
| 282 | pcm_close(pcm_play); |
| 283 | pcm_close(pcm_cap); |
| 284 | return ENOMEM; |
| 285 | } |
| 286 | } |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 287 | |
| 288 | /* catch ctrl-c to shutdown cleanly */ |
| 289 | signal(SIGINT, stream_close); |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 290 | signal(SIGHUP, stream_close); |
| 291 | signal(SIGTERM, stream_close); |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 292 | |
| 293 | if (pcm_cap != NULL) pcm_start(pcm_cap); |
| 294 | if (pcm_play != NULL) pcm_start(pcm_play); |
| 295 | |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 296 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 297 | end.tv_sec = now.tv_sec + play_cap_time; |
| 298 | end.tv_nsec = now.tv_nsec; |
| 299 | |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 300 | do { |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 301 | if (do_loopback) { |
| 302 | if (pcm_read(pcm_cap, buffer, size)) { |
| 303 | fprintf(stderr, "Unable to read from PCM capture device %u (%s)\n", |
| 304 | c_device, pcm_get_error(pcm_cap)); |
| 305 | rc = errno; |
| 306 | break; |
| 307 | } |
| 308 | if (pcm_write(pcm_play, buffer, size)) { |
| 309 | fprintf(stderr, "Unable to write to PCM playback device %u (%s)\n", |
| 310 | p_device, pcm_get_error(pcm_play)); |
| 311 | break; |
| 312 | } |
| 313 | } else { |
| 314 | usleep(100000); |
| 315 | } |
| 316 | if (play_cap_time) { |
| 317 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 318 | if (now.tv_sec > end.tv_sec || |
| 319 | (now.tv_sec == end.tv_sec && now.tv_nsec >= end.tv_nsec)) |
| 320 | break; |
| 321 | } |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 322 | } while(!close_h); |
| 323 | |
John Muir | 43ea484 | 2017-07-05 15:30:01 -0700 | [diff] [blame] | 324 | if (buffer) |
| 325 | free(buffer); |
| 326 | if (pcm_play != NULL) |
| 327 | pcm_close(pcm_play); |
| 328 | if (pcm_cap != NULL) |
| 329 | pcm_close(pcm_cap); |
| 330 | return rc; |
Frank Barchard | 4163c2f | 2017-06-28 15:44:40 -0700 | [diff] [blame] | 331 | } |