From 1bf35a6162d6199df6a27f53cf3be6609ee2311c Mon Sep 17 00:00:00 2001 From: Mikhail Kupchik Date: Wed, 8 Jun 2022 18:28:42 +0300 Subject: mmap support in tinycap Added mmap (-M option) support to tinycap utility. This is necessary for some hardware to work (e.g. Plugable HDMI capture card with USB ID 1bcf:2c99). --- utils/tinycap.1 | 5 +++++ utils/tinycap.c | 26 ++++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/utils/tinycap.1 b/utils/tinycap.1 index ad60a2e..d18dd12 100644 --- a/utils/tinycap.1 +++ b/utils/tinycap.1 @@ -23,6 +23,11 @@ The default is 0. Device number of the PCM. The default is 0. +.TP +\fB\-M\fR +Use memory-mapped I/O method. +If this option is not specified, then read and write I/O method will be used. + .TP \fB\-c\fR \fIchannels\fR Number of channels the PCM will have. diff --git a/utils/tinycap.c b/utils/tinycap.c index 7d4b8a4..617d16a 100644 --- a/utils/tinycap.c +++ b/utils/tinycap.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -64,7 +65,7 @@ int capturing = 1; int prinfo = 1; unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device, - unsigned int channels, unsigned int rate, + bool use_mmap, unsigned int channels, unsigned int rate, enum pcm_format format, unsigned int period_size, unsigned int period_count, unsigned int capture_time); @@ -88,12 +89,13 @@ int main(int argc, char **argv) unsigned int period_size = 1024; unsigned int period_count = 4; unsigned int capture_time = UINT_MAX; + bool use_mmap = false; enum pcm_format format; int no_header = 0, c; struct optparse opts; if (argc < 2) { - fprintf(stderr, "Usage: %s {file.wav | --} [-D card] [-d device] [-c channels] " + fprintf(stderr, "Usage: %s {file.wav | --} [-D card] [-d device] [-M] [-c channels] " "[-r rate] [-b bits] [-p period_size] [-n n_periods] [-t time_in_seconds]\n\n" "Use -- for filename to send raw PCM to stdout\n", argv[0]); return 1; @@ -113,7 +115,7 @@ int main(int argc, char **argv) /* parse command line arguments */ optparse_init(&opts, argv + 1); - while ((c = optparse(&opts, "D:d:c:r:b:p:n:t:")) != -1) { + while ((c = optparse(&opts, "D:d:c:r:b:p:n:t:M")) != -1) { switch (c) { case 'd': device = atoi(opts.optarg); @@ -139,6 +141,9 @@ int main(int argc, char **argv) case 't': capture_time = atoi(opts.optarg); break; + case 'M': + use_mmap = true; + break; case '?': fprintf(stderr, "%s\n", opts.errmsg); return EXIT_FAILURE; @@ -182,9 +187,9 @@ int main(int argc, char **argv) /* install signal handler and begin capturing */ signal(SIGINT, sigint_handler); - frames = capture_sample(file, card, device, header.num_channels, - header.sample_rate, format, - period_size, period_count, capture_time); + frames = capture_sample(file, card, device, use_mmap, + header.num_channels, header.sample_rate, + format, period_size, period_count, capture_time); if (prinfo) { printf("Captured %u frames\n", frames); } @@ -203,11 +208,12 @@ int main(int argc, char **argv) } unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device, - unsigned int channels, unsigned int rate, + bool use_mmap, unsigned int channels, unsigned int rate, enum pcm_format format, unsigned int period_size, unsigned int period_count, unsigned int capture_time) { struct pcm_config config; + unsigned int pcm_open_flags; struct pcm *pcm; char *buffer; unsigned int size; @@ -225,7 +231,11 @@ unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device, config.stop_threshold = 0; config.silence_threshold = 0; - pcm = pcm_open(card, device, PCM_IN, &config); + pcm_open_flags = PCM_IN; + if (use_mmap) + pcm_open_flags |= PCM_MMAP; + + pcm = pcm_open(card, device, pcm_open_flags, &config); if (!pcm || !pcm_is_ready(pcm)) { fprintf(stderr, "Unable to open PCM device (%s)\n", pcm_get_error(pcm)); -- cgit v1.2.3