diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2017-07-06 07:30:34 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2017-07-06 07:30:34 +0000 |
commit | 4bd0b3467dbf92ab087c3dc5f11a3324c3af6378 (patch) | |
tree | af14528679a8a26268ac9a9d6d6385d643a53978 | |
parent | a7d8836d3d78b4d7bfae4ae2d7397c3ce25237e1 (diff) | |
parent | 47d3951c90d97d5124d0d7f34c4b07106a2b6a59 (diff) | |
download | tinyalsa-oreo-dr3-release.tar.gz |
release-request-49fb627d-ebdd-4558-ae95-935e31212986-for-git_oc-dr1-release-4157297 snap-temp-L13700000079944578android-8.0.0_r34android-8.0.0_r33android-8.0.0_r27android-8.0.0_r26android-8.0.0_r25android-8.0.0_r24android-8.0.0_r23android-8.0.0_r22android-8.0.0_r21oreo-dr3-releaseoreo-dr2-releaseoreo-dr1-release
Change-Id: I3d3dc7b8309afbc844dd6f4b1ea8a95fa58e327f
-rw-r--r-- | tinycap.c | 32 | ||||
-rw-r--r-- | tinyhostless.c | 214 |
2 files changed, 207 insertions, 39 deletions
@@ -32,6 +32,7 @@ #include <stdint.h> #include <signal.h> #include <string.h> +#include <time.h> #define ID_RIFF 0x46464952 #define ID_WAVE 0x45564157 @@ -61,7 +62,7 @@ int capturing = 1; unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels, unsigned int rate, enum pcm_format format, unsigned int period_size, - unsigned int period_count); + unsigned int period_count, unsigned int cap_time); void sigint_handler(int sig __unused) { @@ -80,11 +81,13 @@ int main(int argc, char **argv) unsigned int frames; unsigned int period_size = 1024; unsigned int period_count = 4; + unsigned int cap_time = 0; enum pcm_format format; if (argc < 2) { - fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-c channels] " - "[-r rate] [-b bits] [-p period_size] [-n n_periods]\n", argv[0]); + fprintf(stderr, "Usage: %s file.wav [-D card] [-d device]" + " [-c channels] [-r rate] [-b bits] [-p period_size]" + " [-n n_periods] [-T capture time]\n", argv[0]); return 1; } @@ -125,6 +128,10 @@ int main(int argc, char **argv) argv++; if (*argv) period_count = atoi(*argv); + } else if (strcmp(*argv, "-T") == 0) { + argv++; + if (*argv) + cap_time = atoi(*argv); } if (*argv) argv++; @@ -164,9 +171,11 @@ int main(int argc, char **argv) /* install signal handler and begin capturing */ signal(SIGINT, sigint_handler); + signal(SIGHUP, sigint_handler); + signal(SIGTERM, sigint_handler); frames = capture_sample(file, card, device, header.num_channels, header.sample_rate, format, - period_size, period_count); + period_size, period_count, cap_time); printf("Captured %d frames\n", frames); /* write header now all information is known */ @@ -183,13 +192,15 @@ int main(int argc, char **argv) unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels, unsigned int rate, enum pcm_format format, unsigned int period_size, - unsigned int period_count) + unsigned int period_count, unsigned int cap_time) { struct pcm_config config; struct pcm *pcm; char *buffer; unsigned int size; unsigned int bytes_read = 0; + struct timespec end; + struct timespec now; memset(&config, 0, sizeof(config)); config.channels = channels; @@ -220,16 +231,25 @@ unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device, printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate, pcm_format_to_bits(format)); + clock_gettime(CLOCK_MONOTONIC, &now); + end.tv_sec = now.tv_sec + cap_time; + end.tv_nsec = now.tv_nsec; + while (capturing && !pcm_read(pcm, buffer, size)) { if (fwrite(buffer, 1, size, file) != size) { fprintf(stderr,"Error capturing sample\n"); break; } bytes_read += size; + if (cap_time) { + clock_gettime(CLOCK_MONOTONIC, &now); + if (now.tv_sec > end.tv_sec || + (now.tv_sec == end.tv_sec && now.tv_nsec >= end.tv_nsec)) + break; + } } free(buffer); pcm_close(pcm); return pcm_bytes_to_frames(pcm, bytes_read); } - diff --git a/tinyhostless.c b/tinyhostless.c index 3a63c0e..ba3c9c1 100644 --- a/tinyhostless.c +++ b/tinyhostless.c @@ -29,21 +29,27 @@ /* Playback data to a PCM device recorded from a capture PCM device. */ #include <tinyalsa/asoundlib.h> +#include <math.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <signal.h> -#include <math.h> +#include <time.h> #include <unistd.h> +/* Used when that particular device isn't opened. */ +#define TINYHOSTLESS_DEVICE_UNDEFINED 255 + static int close_h = 0; -void 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 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); -void stream_close(int sig) +static void stream_close(int sig) { /* allow the stream to be closed gracefully */ signal(sig, SIG_IGN); @@ -53,18 +59,25 @@ void stream_close(int sig) int main(int argc, char **argv) { unsigned int card = 0; - unsigned int p_device = 255; - unsigned int c_device = 255; - unsigned int period_size = 1024; + unsigned int p_device = TINYHOSTLESS_DEVICE_UNDEFINED; + unsigned int c_device = TINYHOSTLESS_DEVICE_UNDEFINED; + unsigned int period_size = 192; unsigned int period_count = 4; unsigned int number_bits = 16; unsigned int num_channels = 2; unsigned int sample_rate = 48000; unsigned int play_cap_time = 0; + unsigned int do_loopback = 0; if (argc < 2) { - fprintf(stderr, "Usage: %s [-D card] [-P Hostless Playback device] [-C Hostless Capture device] [-p period_size]" - " [-n n_periods] [-c num_channels] [-r sample_rate] [-T playback/capture time]\n", argv[0]); + fprintf(stderr, "Usage: %s [-D card] [-P playback device]" + " [-C capture device] [-p period_size] [-n n_periods]" + " [-c num_channels] [-r sample_rate] [-l]" + " [-T playback/capture time]\n\n" + "Used to enable 'hostless' mode for audio devices with a DSP back-end.\n" + "Alternatively, specify '-l' for loopback mode: this program will read\n" + "from the capture device and write to the playback device.\n", + argv[0]); return 1; } @@ -111,72 +124,207 @@ int main(int argc, char **argv) if (*argv) card = atoi(*argv); } + if (strcmp(*argv, "-l") == 0) { + do_loopback = 1; + } if (*argv) argv++; } - printf (" Hostless .. p_device =%d, c_device =%d, num_channel =%d, sample_rate =%d, number_bits =%d, play_cap_time =%d\n", p_device, c_device, num_channels, sample_rate, number_bits, play_cap_time); + if (p_device == TINYHOSTLESS_DEVICE_UNDEFINED && + c_device == TINYHOSTLESS_DEVICE_UNDEFINED) { + fprintf(stderr, "Specify at least one of -C (capture device) or -P (playback device).\n"); + return EINVAL; + } - play_sample(card, p_device, c_device, num_channels, sample_rate, number_bits, - period_size, period_count, play_cap_time); + if (do_loopback && (p_device == TINYHOSTLESS_DEVICE_UNDEFINED || + c_device == TINYHOSTLESS_DEVICE_UNDEFINED)) { + fprintf(stderr, "Loopback requires both playback and capture devices.\n"); + return EINVAL; + } - return 0; + return play_sample(card, p_device, c_device, num_channels, sample_rate, + number_bits, period_size, period_count, play_cap_time, + do_loopback); } -void 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) +static int check_param(struct pcm_params *params, unsigned int param, + unsigned int value, char *param_name, char *param_unit) +{ + unsigned int min; + unsigned int max; + int is_within_bounds = 1; + + min = pcm_params_get_min(params, param); + if (value < min) { + fprintf(stderr, "%s is %u%s, device only supports >= %u%s\n", param_name, value, + param_unit, min, param_unit); + is_within_bounds = 0; + } + + max = pcm_params_get_max(params, param); + if (value > max) { + fprintf(stderr, "%s is %u%s, device only supports <= %u%s\n", param_name, value, + param_unit, max, param_unit); + is_within_bounds = 0; + } + + return is_within_bounds; +} + +static int check_params(unsigned int card, unsigned int device, unsigned int direction, + const struct pcm_config *config) +{ + struct pcm_params *params; + int can_play; + int bits; + + params = pcm_params_get(card, device, direction); + if (params == NULL) { + fprintf(stderr, "Unable to open PCM %s device %u.\n", + direction == PCM_OUT ? "playback" : "capture", device); + return 0; + } + + switch (config->format) { + case PCM_FORMAT_S32_LE: + bits = 32; + break; + case PCM_FORMAT_S24_3LE: + bits = 24; + break; + case PCM_FORMAT_S16_LE: + bits = 16; + break; + default: + fprintf(stderr, "Invalid format: %u", config->format); + return 0; + } + + can_play = check_param(params, PCM_PARAM_RATE, config->rate, "Sample rate", "Hz"); + can_play &= check_param(params, PCM_PARAM_CHANNELS, config->channels, "Sample", " channels"); + can_play &= check_param(params, PCM_PARAM_SAMPLE_BITS, bits, "Bitrate", " bits"); + can_play &= check_param(params, PCM_PARAM_PERIOD_SIZE, config->period_size, "Period size", " frames"); + can_play &= check_param(params, PCM_PARAM_PERIODS, config->period_count, "Period count", " periods"); + + pcm_params_free(params); + + return can_play; +} + +int 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) { struct pcm_config config; struct pcm *pcm_play =NULL, *pcm_cap=NULL; - unsigned int count =0; + char *buffer = NULL; + int size = 0; + int rc = 0; + struct timespec end; + struct timespec now; + + memset(&config, 0, sizeof(config)); config.channels = channels; config.rate = rate; config.period_size = period_size; config.period_count = period_count; if (bits == 32) config.format = PCM_FORMAT_S32_LE; + else if (bits == 24) + config.format = PCM_FORMAT_S24_3LE; else if (bits == 16) config.format = PCM_FORMAT_S16_LE; config.start_threshold = 0; - config.stop_threshold = INT_MAX; + config.stop_threshold = 0; config.avail_min = 0; - if(p_device < 255 ) { + if(p_device < TINYHOSTLESS_DEVICE_UNDEFINED ) { + if (!check_params(card, p_device, PCM_OUT, &config)) + return EINVAL; pcm_play = pcm_open(card, p_device, PCM_OUT, &config); if (!pcm_play || !pcm_is_ready(pcm_play)) { - fprintf(stderr, "Unable to open PCM device %u (%s)\n", - p_device, pcm_get_error(pcm_play)); - return; + fprintf(stderr, "Unable to open PCM playback device %u (%s)\n", + p_device, pcm_get_error(pcm_play)); + return errno; } } - if (c_device < 255 ) { + if (c_device < TINYHOSTLESS_DEVICE_UNDEFINED ) { + if (!check_params(card, c_device, PCM_IN, &config)) + return EINVAL; pcm_cap = pcm_open(card, c_device, PCM_IN, &config); if (!pcm_cap || !pcm_is_ready(pcm_cap)) { - fprintf(stderr, "Unable to open PCM device %u (%s)\n", + fprintf(stderr, "Unable to open PCM capture device %u (%s)\n", c_device, pcm_get_error(pcm_cap)); if (pcm_play != NULL ) pcm_close(pcm_play); - return; + return errno; } } - printf("Hostless Playing device %u, Capture Device %u, sample: %u ch, %u hz, %u bit playback/capture duration in sec %u\n", p_device, c_device, channels, rate, bits, play_cap_time); + printf("%s: Playing device %u, Capture Device %u\n", + do_loopback ? "Loopback" : "Hostless", p_device, c_device); + printf("Sample: %u ch, %u hz, %u bit\n", channels, rate, bits); + if (play_cap_time) + printf("Duration in sec: %u\n", play_cap_time); + else + printf("Duration in sec: forever\n"); + + if (do_loopback) { + size = pcm_frames_to_bytes(pcm_cap, pcm_get_buffer_size(pcm_cap)); + buffer = malloc(size); + if (!buffer) { + fprintf(stderr, "Unable to allocate %d bytes\n", size); + pcm_close(pcm_play); + pcm_close(pcm_cap); + return ENOMEM; + } + } /* catch ctrl-c to shutdown cleanly */ signal(SIGINT, stream_close); + signal(SIGHUP, stream_close); + signal(SIGTERM, stream_close); if (pcm_cap != NULL) pcm_start(pcm_cap); if (pcm_play != NULL) pcm_start(pcm_play); + clock_gettime(CLOCK_MONOTONIC, &now); + end.tv_sec = now.tv_sec + play_cap_time; + end.tv_nsec = now.tv_nsec; + do { - count++; - usleep(10000); - if ( (play_cap_time > 0) && ((count/100) > play_cap_time)) break; + if (do_loopback) { + if (pcm_read(pcm_cap, buffer, size)) { + fprintf(stderr, "Unable to read from PCM capture device %u (%s)\n", + c_device, pcm_get_error(pcm_cap)); + rc = errno; + break; + } + if (pcm_write(pcm_play, buffer, size)) { + fprintf(stderr, "Unable to write to PCM playback device %u (%s)\n", + p_device, pcm_get_error(pcm_play)); + break; + } + } else { + usleep(100000); + } + if (play_cap_time) { + clock_gettime(CLOCK_MONOTONIC, &now); + if (now.tv_sec > end.tv_sec || + (now.tv_sec == end.tv_sec && now.tv_nsec >= end.tv_nsec)) + break; + } } while(!close_h); - if (pcm_play != NULL) pcm_close(pcm_play); - if (pcm_cap != NULL) pcm_close(pcm_cap); + if (buffer) + free(buffer); + if (pcm_play != NULL) + pcm_close(pcm_play); + if (pcm_cap != NULL) + pcm_close(pcm_cap); + return rc; } - |