diff options
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | include/gapk/procqueue.h | 3 | ||||
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/main.c | 46 | ||||
-rw-r--r-- | src/pq_alsa.c | 164 |
5 files changed, 214 insertions, 6 deletions
diff --git a/configure.ac b/configure.ac index 1d78fea..c24e8f0 100644 --- a/configure.ac +++ b/configure.ac @@ -52,6 +52,7 @@ AC_PROG_CC # libosmocore (codec module) PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore >= 0.3.2) PKG_CHECK_MODULES(LIBOSMOCODEC, libosmocodec >= 0.1.25) +PKG_CHECK_MODULES(LIBALSA, alsa, [AC_DEFINE([HAVE_ALSA], [1], [We have ALSA])]) # opencore-amrnb for AMR and EFR decoding found_opencore_amrnb=yes diff --git a/include/gapk/procqueue.h b/include/gapk/procqueue.h index 93893ca..68d6f52 100644 --- a/include/gapk/procqueue.h +++ b/include/gapk/procqueue.h @@ -48,6 +48,9 @@ int pq_queue_file_output(struct pq *pq, FILE *dst, unsigned int block_len); int pq_queue_rtp_input(struct pq *pq, int rtp_fd, unsigned int block_len); int pq_queue_rtp_output(struct pq *pq, int rtp_fd, unsigned int block_len); +/* ALSA */ +int pq_queue_alsa_input(struct pq *pq, const char *hwdev, unsigned int blk_len); +int pq_queue_alsa_output(struct pq *pq, const char *hwdev, unsigned int blk_len); /* Format */ struct format_desc; diff --git a/src/Makefile.am b/src/Makefile.am index 788d425..e33a490 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,10 +1,10 @@ AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir) AM_CFLAGS=-Wall $(LIBOSMOCODEC_CFLAGS) $(LIBOSMOCORE_CFLAGS) \ - ${OPENCORE_AMRNB_CFLAGS} + ${OPENCORE_AMRNB_CFLAGS} $(LIBALSA_CFLAGS) AM_LDFLAGS=$(LIBOSMOCODEC_LIBS) $(LIBOSMOCORE_LIBS) \ - ${OPENCORE_AMRNB_LIBS} ${LIBGSM_LIBS} + ${OPENCORE_AMRNB_LIBS} ${LIBGSM_LIBS} $(LIBALSA_LIBS) -COM_SOURCES = procqueue.c pq_file.c pq_format.c pq_codec.c pq_rtp.c \ +COM_SOURCES = procqueue.c pq_file.c pq_format.c pq_codec.c pq_rtp.c pq_alsa.c \ formats.c fmt_amr.c fmt_gsm.c fmt_hr_ref.c fmt_racal.c \ fmt_rawpcm.c fmt_ti.c benchmark.c \ codecs.c codec_pcm.c codec_hr.c codec_fr.c codec_efr.c @@ -17,6 +17,8 @@ * along with gapk. If not, see <http://www.gnu.org/licenses/>. */ +#include "config.h" + #include <errno.h> #include <stdio.h> #include <stdlib.h> @@ -44,6 +46,7 @@ struct gapk_options const char *hostname; uint16_t port; } rtp_in; + const char *alsa_in; const struct format_desc *fmt_in; const char *fname_out; @@ -51,6 +54,7 @@ struct gapk_options const char *hostname; uint16_t port; } rtp_out; + const char *alsa_out; const struct format_desc *fmt_out; }; @@ -93,6 +97,10 @@ print_help(char *progname) fprintf(stdout, " -I, --input-rtp=HOST/PORT\tInput RTP stream\n"); fprintf(stdout, " -o, --output-file=FILE\tOutput file\n"); fprintf(stdout, " -O, --output-rtp=HOST/PORT\tOutput RTP stream\n"); +#ifdef HAVE_ALSA + fprintf(stdout, " -a, --input-alsa=dev\t\tInput ALSA stream\n"); + fprintf(stdout, " -A, --output-alsa=dev\t\tOutput ALSA stream\n"); +#endif fprintf(stdout, " -f, --input-format=FMT\tInput format (see below)\n"); fprintf(stdout, " -g, --output-format=FMT\tOutput format (see below)\n"); fprintf(stdout, "\n"); @@ -159,11 +167,19 @@ parse_options(struct gapk_state *state, int argc, char *argv[]) {"output-file", 1, 0, 'o'}, {"input-rtp", 1, 0, 'I'}, {"output-rtp", 1, 0, 'O'}, +#ifdef HAVE_ALSA + {"input-alsa", 1, 0, 'a'}, + {"output-alsa", 1, 0, 'A'}, +#endif {"input-format", 1, 0, 'f'}, {"output-format", 1, 0, 'g'}, {"help", 0, 0, 'h'}, }; - const char *short_options = "i:o:I:O:f:g:h"; + const char *short_options = "i:o:I:O:f:g:h" +#ifdef HAVE_ALSA + "a:A:" +#endif + ; struct gapk_options *opt = &state->opts; @@ -197,7 +213,15 @@ parse_options(struct gapk_state *state, int argc, char *argv[]) } opt->rtp_in.port = rv; break; +#ifdef HAVE_ALSA + case 'a': + opt->alsa_in = optarg; + break; + case 'A': + opt->alsa_out = optarg; + break; +#endif case 'O': rv = parse_host_port(optarg, &opt->rtp_out.hostname); if (rv < 0 || rv > 0xffff) { @@ -307,6 +331,8 @@ files_open(struct gapk_state *gs) fprintf(stderr, "[!] Error while opening input socket\n"); return gs->in.rtp.fd; } + } else if (gs->opts.alsa_in) { + printf("alsa_in, not stdin\n"); } else gs->in.file.fh = stdin; @@ -327,6 +353,8 @@ files_open(struct gapk_state *gs) fprintf(stderr, "[!] Error while opening output socket\n"); return gs->out.rtp.fd; } + } else if (gs->opts.alsa_out) { + printf("alsa_out, not stdout\n"); } else gs->out.file.fh = stdout; @@ -406,8 +434,14 @@ make_processing_chain(struct gapk_state *gs) /* File read */ if (gs->in.file.fh) pq_queue_file_input(gs->pq, gs->in.file.fh, fmt_in->frame_len); - else + else if (gs->in.rtp.fd != -1) pq_queue_rtp_input(gs->pq, gs->in.rtp.fd, fmt_in->frame_len); +#ifdef HAVE_ALSA + else if (gs->opts.alsa_in) + pq_queue_alsa_input(gs->pq, gs->opts.alsa_in, fmt_in->frame_len); +#endif + else + return -1; /* Decoding to PCM ? */ if (need_dec) @@ -462,8 +496,14 @@ make_processing_chain(struct gapk_state *gs) /* File write */ if (gs->out.file.fh) pq_queue_file_output(gs->pq, gs->out.file.fh, fmt_out->frame_len); - else + else if (gs->out.rtp.fd != -1) pq_queue_rtp_output(gs->pq, gs->out.rtp.fd, fmt_out->frame_len); +#ifdef HAVE_ALSA + else if (gs->opts.alsa_out) + pq_queue_alsa_output(gs->pq, gs->opts.alsa_out, fmt_out->frame_len); +#endif + else + return -1; return 0; } diff --git a/src/pq_alsa.c b/src/pq_alsa.c new file mode 100644 index 0000000..b17faa8 --- /dev/null +++ b/src/pq_alsa.c @@ -0,0 +1,164 @@ +/* Process Queue: ALSA PCM output */ +/* (C) 2017 by Harald Welte <laforge@gnumonks.org> */ + +/* + * This file is part of gapk (GSM Audio Pocket Knife). + * + * gapk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gapk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gapk. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> + +#include <gapk/codecs.h> +#include <gapk/formats.h> +#include <gapk/procqueue.h> + +#include "config.h" + +#ifdef HAVE_ALSA + +#include <alsa/asoundlib.h> + + +struct pq_state_alsa { + snd_pcm_t *pcm_handle; + unsigned int blk_len; +}; + +static int +pq_cb_alsa_input(void *_state, uint8_t *out, const uint8_t *in) +{ + struct pq_state_alsa *state = _state; + unsigned int num_samples = state->blk_len/2; + int rv; + rv = snd_pcm_readi(state->pcm_handle, out, num_samples); + return rv == num_samples ? 0 : -1; +} + +static int +pq_cb_alsa_output(void *_state, uint8_t *out, const uint8_t *in) +{ + struct pq_state_alsa *state = _state; + unsigned int num_samples = state->blk_len/2; + int rv; + rv = snd_pcm_writei(state->pcm_handle, in, num_samples); + return rv == num_samples ? 0 : -1; +} + +static void +pq_cb_alsa_exit(void *_state) +{ + struct pq_state_alsa *state = _state; + snd_pcm_close(state->pcm_handle); +} + +static int +pq_queue_alsa_op(struct pq *pq, const char *alsa_dev, unsigned int blk_len, int in_out_n) +{ + struct pq_item *item; + struct pq_state_alsa *state; + snd_pcm_hw_params_t *hw_params; + int rc = -1; + + state = calloc(1, sizeof(struct pq_state_alsa)); + if (!state) + return -ENOMEM; + + rc = snd_pcm_open(&state->pcm_handle, alsa_dev, + in_out_n ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK, 0); + if (rc < 0) + return rc; + state->blk_len = blk_len; + + rc = snd_pcm_hw_params_malloc(&hw_params); + if (rc < 0) + goto out_close; + + rc = snd_pcm_hw_params_any(state->pcm_handle, hw_params); + if (rc < 0) + goto out_free_par; + + rc = snd_pcm_hw_params_set_access(state->pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); + if (rc < 0) + goto out_free_par; + + rc = snd_pcm_hw_params_set_format(state->pcm_handle, hw_params, SND_PCM_FORMAT_S16_LE); + if (rc < 0) + goto out_free_par; + + rc = snd_pcm_hw_params_set_rate(state->pcm_handle, hw_params, 8000, 0); + if (rc < 0) + goto out_free_par; + + rc = snd_pcm_hw_params_set_channels (state->pcm_handle, hw_params, 1); + if (rc < 0) + goto out_free_par; + + rc = snd_pcm_hw_params(state->pcm_handle, hw_params); + if (rc < 0) + goto out_free_par; + + snd_pcm_hw_params_free(hw_params); + + item = pq_add_item(pq); + if (!item) { + rc = -ENOMEM; + goto out_close; + } + + item->len_in = in_out_n ? 0 : blk_len; + item->len_out = in_out_n ? blk_len : 0; + item->state = state; + item->proc = in_out_n ? pq_cb_alsa_input : pq_cb_alsa_output; + item->exit = pq_cb_alsa_exit; + + return 0; + +out_free_par: + snd_pcm_hw_params_free(hw_params); +out_close: + snd_pcm_close(state->pcm_handle); + free(state); + return rc; +} + + +/*! Add ALSA input to given processing queue + * This usually only makes sense as first item in the queue + * \param pq Processing Queue to add the ALSA input to + * \param[in] hwdev ALSA hardware device to use + * \param[in] blk_len block length to be read from device + * \returns 0 on sucess; negative on error */ +int +pq_queue_alsa_input(struct pq *pq, const char *hwdev, unsigned int blk_len) +{ + return pq_queue_alsa_op(pq, hwdev, blk_len, 1); +} + +/*! Add ALSA output to given processing queue + * This usually only makes sense as first item in the queue + * \param pq Processing Queue to add the ALSA output to + * \param[in] hwdev ALSA hardware device to use + * \param[in] blk_len block length to be written to device + * \returns 0 on sucess; negative on error */ +int +pq_queue_alsa_output(struct pq *pq, const char *hwdev, unsigned int blk_len) +{ + return pq_queue_alsa_op(pq, hwdev, blk_len, 0); +} + +#endif /* HAVE_ALSA */ |