From e889a38a274c81daecd972b0a233e1c14e961eba Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 1 Aug 2020 19:01:03 +0200 Subject: [PATCH] gapk: Implemnet optional looping/rewinding of input files Together with the recently-added throttling, this can be useful to generate a continuous stream of RTP data from an input file. Change-Id: I2d552695dfb4cc96039838e79e0f5ae25a6737c8 --- include/osmocom/gapk/procqueue.h | 3 ++- src/app_osmo_gapk.c | 11 +++++++++-- src/pq_file.c | 15 +++++++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/include/osmocom/gapk/procqueue.h b/include/osmocom/gapk/procqueue.h index 82c1cf9..be10f7a 100644 --- a/include/osmocom/gapk/procqueue.h +++ b/include/osmocom/gapk/procqueue.h @@ -20,6 +20,7 @@ #pragma once #include +#include #include /* for FILE */ #include @@ -85,7 +86,7 @@ char *osmo_gapk_pq_describe(struct osmo_gapk_pq *pq); struct osmo_gapk_pq_item *osmo_gapk_pq_add_item(struct osmo_gapk_pq *pq); /* File */ -int osmo_gapk_pq_queue_file_input(struct osmo_gapk_pq *pq, FILE *src, unsigned int block_len); +int osmo_gapk_pq_queue_file_input(struct osmo_gapk_pq *pq, FILE *src, unsigned int block_len, bool loop); int osmo_gapk_pq_queue_file_output(struct osmo_gapk_pq *pq, FILE *dst, unsigned int block_len); /* RTP */ diff --git a/src/app_osmo_gapk.c b/src/app_osmo_gapk.c index 1e4e9a0..5e85548 100644 --- a/src/app_osmo_gapk.c +++ b/src/app_osmo_gapk.c @@ -68,6 +68,7 @@ struct gapk_options /* RTP payload type */ uint8_t rtp_pt_in, rtp_pt_out; + bool loop_input; int throttle; int benchmark; int verbose; @@ -144,6 +145,7 @@ print_help(char *progname) fprintf(stdout, " -P --rtp-pt-out=TYPE\t\tRTP payload type for outgoing frames\n"); fprintf(stdout, " -b, --enable-benchmark\tEnable codec benchmarking\n"); fprintf(stdout, " -t, --throttle\tEnable throttling (one codec frame every 20ms)\n"); + fprintf(stdout, " -l, --loop-input\tEnable looping of the input file\n"); fprintf(stdout, " -v, --verbose\t\t\tEnable debug messages\n"); fprintf(stdout, "\n"); @@ -217,10 +219,11 @@ parse_options(struct gapk_state *state, int argc, char *argv[]) {"rtp-pt-out", 1, 0, 'P'}, {"enable-benchmark", 0, 0, 'b'}, {"throttle", 0, 0, 't'}, + {"loop-input", 0, 0, 'l'}, {"verbose", 0, 0, 'v'}, {"help", 0, 0, 'h'}, }; - const char *short_options = "i:o:I:O:f:g:p:P:btvh" + const char *short_options = "i:o:I:O:f:g:p:P:btlvh" #ifdef HAVE_ALSA "a:A:" #endif @@ -322,6 +325,10 @@ parse_options(struct gapk_state *state, int argc, char *argv[]) opt->throttle = 1; break; + case 'l': + opt->loop_input = true; + break; + case 'v': log_parse_category_mask(osmo_stderr_target, "DAPP"); opt->verbose = 1; @@ -577,7 +584,7 @@ make_processing_chain(struct gapk_state *gs) /* File read */ if (gs->in.file.fh) - osmo_gapk_pq_queue_file_input(gs->pq, gs->in.file.fh, fmt_in->frame_len); + osmo_gapk_pq_queue_file_input(gs->pq, gs->in.file.fh, fmt_in->frame_len, gs->opts.loop_input); else if (gs->in.rtp.fd != -1) osmo_gapk_pq_queue_rtp_input(gs->pq, gs->in.rtp.fd, fmt_in->frame_len, gs->opts.rtp_pt_in); diff --git a/src/pq_file.c b/src/pq_file.c index d496eec..d214096 100644 --- a/src/pq_file.c +++ b/src/pq_file.c @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -30,6 +31,7 @@ struct pq_state_file { FILE *fh; unsigned int blk_len; + bool loop_input; }; @@ -39,6 +41,10 @@ pq_cb_file_input(void *_state, uint8_t *out, const uint8_t *in, unsigned int in_ struct pq_state_file *state = _state; int rv; rv = fread(out, state->blk_len, 1, state->fh); + if (rv == 0 && state->loop_input && feof(state->fh)) { + rewind(state->fh); + rv = fread(out, state->blk_len, 1, state->fh); + } if (rv <= 0) return -1; return rv * state->blk_len; @@ -60,7 +66,7 @@ pq_cb_file_exit(void *_state) } static int -pq_queue_file_op(struct osmo_gapk_pq *pq, FILE *fh, unsigned int blk_len, int in_out_n) +pq_queue_file_op(struct osmo_gapk_pq *pq, FILE *fh, unsigned int blk_len, int in_out_n, bool loop_input) { struct osmo_gapk_pq_item *item; struct pq_state_file *state; @@ -71,6 +77,7 @@ pq_queue_file_op(struct osmo_gapk_pq *pq, FILE *fh, unsigned int blk_len, int in state->fh = fh; state->blk_len = blk_len; + state->loop_input = loop_input; item = osmo_gapk_pq_add_item(pq); if (!item) { @@ -105,11 +112,11 @@ pq_queue_file_op(struct osmo_gapk_pq *pq, FILE *fh, unsigned int blk_len, int in * \param[in] blk_len block length to be read from file * \returns 0 on success; negative on error */ int -osmo_gapk_pq_queue_file_input(struct osmo_gapk_pq *pq, FILE *src, unsigned int blk_len) +osmo_gapk_pq_queue_file_input(struct osmo_gapk_pq *pq, FILE *src, unsigned int blk_len, bool loop) { LOGPGAPK(LOGL_DEBUG, "PQ '%s': Adding file input (blk_len=%u)\n", pq->name, blk_len); - return pq_queue_file_op(pq, src, blk_len, 1); + return pq_queue_file_op(pq, src, blk_len, 1, loop); } /*! Add file output to given processing queue @@ -123,5 +130,5 @@ osmo_gapk_pq_queue_file_output(struct osmo_gapk_pq *pq, FILE *dst, unsigned int { LOGPGAPK(LOGL_DEBUG, "PQ '%s': Adding file output (blk_len=%u)\n", pq->name, blk_len); - return pq_queue_file_op(pq, dst, blk_len, 0); + return pq_queue_file_op(pq, dst, blk_len, 0, false); }