From edbfcc7dc35f75908de68bdd024d00e0d4617fbe Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 10 May 2009 17:46:40 +0200 Subject: [PATCH] Add new prebuffer filter. This could be handy for lossy networks that require a lot of buffering in addition to methods (like FEC) to cope with packet loss. --- NEWS | 1 + configure.ac | 15 +++-- error.h | 3 + ggo/prebuffer_filter.ggo | 24 ++++++++ prebuffer_filter.c | 130 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 ggo/prebuffer_filter.ggo create mode 100644 prebuffer_filter.c diff --git a/NEWS b/NEWS index c444464f..e51de1b7 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,7 @@ NEWS ------------------------------------------------- - the new oss writer (supported on *BSD and Linux) + - the new prebuffer filter - improved signal handling - variable fec output buffer size diff --git a/configure.ac b/configure.ac index d158fd3d..85a32729 100644 --- a/configure.ac +++ b/configure.ac @@ -84,7 +84,7 @@ dccp_send fd user_list chunk_queue afs osl aft mood score attribute blob ringbuf playlist sha1 rbtree sched audiod grab_client filter_common wav_filter compress_filter http_recv dccp_recv recv_common write_common file_write audiod_command client_common recv stdout filter stdin audioc write client fsck exec send_common ggo -udp_recv udp_send color fec fecdec_filter" +udp_recv udp_send color fec fecdec_filter prebuffer_filter" all_executables="server recv filter audioc write client fsck afh" @@ -96,11 +96,13 @@ recv_ldflags="" receivers=" http dccp udp" senders=" http dccp udp" -filter_cmdline_objs="filter.cmdline compress_filter.cmdline amp_filter.cmdline" +filter_cmdline_objs="filter.cmdline compress_filter.cmdline amp_filter.cmdline + prebuffer_filter.cmdline" filter_errlist_objs="filter_common wav_filter compress_filter filter string - stdin stdout sched fd amp_filter ggo fecdec_filter fec" + stdin stdout sched fd amp_filter ggo fecdec_filter fec + prebuffer_filter time" filter_ldflags="" -filters=" compress wav amp fecdec" +filters=" compress wav amp fecdec prebuffer" audioc_cmdline_objs="audioc.cmdline" audioc_errlist_objs="audioc string net fd" @@ -108,11 +110,12 @@ audioc_ldflags="" audiod_cmdline_objs="audiod.cmdline grab_client.cmdline compress_filter.cmdline http_recv.cmdline dccp_recv.cmdline file_write.cmdline client.cmdline - audiod_command_list amp_filter.cmdline udp_recv.cmdline" + audiod_command_list amp_filter.cmdline udp_recv.cmdline + prebuffer_filter.cmdline" audiod_errlist_objs="audiod signal string daemon stat net time grab_client filter_common wav_filter compress_filter amp_filter http_recv dccp_recv recv_common fd sched write_common file_write audiod_command crypt fecdec_filter - client_common ggo udp_recv color fec" + client_common ggo udp_recv color fec prebuffer_filter" audiod_ldflags="" audiod_audio_formats="" diff --git a/error.h b/error.h index fbc6c4de..c53ab89a 100644 --- a/error.h +++ b/error.h @@ -34,6 +34,9 @@ DEFINE_ERRLIST_OBJECT_ENUM; extern const char **para_errlist[]; +#define PREBUFFER_FILTER_ERRORS \ + PARA_ERROR(PREBUFFER_SYNTAX, "syntax error in prebuffer filter config"), \ + #define OSS_WRITE_ERRORS \ PARA_ERROR(BAD_SAMPLE_FORMAT, "sample format not supported"), \ PARA_ERROR(BAD_CHANNEL_COUNT, "channel count not supported"), \ diff --git a/ggo/prebuffer_filter.ggo b/ggo/prebuffer_filter.ggo new file mode 100644 index 00000000..7553e957 --- /dev/null +++ b/ggo/prebuffer_filter.ggo @@ -0,0 +1,24 @@ +option "duration" d +#~~~~~~~~~~~~~~~~~~ +"prebuffer time" +int typestr="milliseconds" +default="200" +optional +details=" + Wait that many milliseconds before letting data go through. + The time interval starts when the first data byte is seen by + this filter. +" + +option "size" s +#~~~~~~~~~~~~~~ +"amount of data to prebuffer" +int typestr="bytes" +default="0" +optional +details=" + Wait until that many data bytes are available in the input buffer. + The default value of zero means to not prebuffer by size at all. + If both --duration and --size options are given and non-zero, the + filter waits until both conditions are met. +" diff --git a/prebuffer_filter.c b/prebuffer_filter.c new file mode 100644 index 00000000..759991e6 --- /dev/null +++ b/prebuffer_filter.c @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2009 Andre Noll + * + * Licensed under the GPL v2. For licencing details see COPYING. + */ + +/** \file prebuffer_filter.c Paraslash's prebuffering filter. */ + +#include "para.h" +#include "prebuffer_filter.cmdline.h" +#include "list.h" +#include "sched.h" +#include "ggo.h" +#include "filter.h" +#include "string.h" +#include "error.h" + +/** Data specific to the prebuffer filter. */ +struct private_prebuffer_data { + /** The configuration data for this instance of the filter. */ + struct prebuffer_filter_args_info *conf; + /** Number of bytes prebuffered or -1 if no longer prebuffering. */ + int prebuffered; + /** End of prebuffering period. */ + struct timeval barrier; +}; + +static ssize_t prebuffer_convert(char *inbuf, size_t inbuf_len, + struct filter_node *fn) +{ + struct private_prebuffer_data *ppd = fn->private_data; + struct prebuffer_filter_args_info *conf = ppd->conf; + + if (inbuf_len == 0) { + if (*fn->fc->input_error < 0 && ppd->prebuffered >= 0) + goto prebuffer_end; + return 0; + } + if (ppd->prebuffered < 0) { + size_t copy = PARA_MIN(inbuf_len, fn->bufsize - fn->loaded); + memcpy(fn->buf + fn->loaded, inbuf, copy); + fn->loaded += copy; + return copy; + } + if (ppd->prebuffered + inbuf_len > fn->bufsize) { + fn->bufsize = PARA_MAX(2 * fn->bufsize, + ppd->prebuffered + inbuf_len); + fn->buf = para_realloc(fn->buf, fn->bufsize); + } + memcpy(fn->buf + ppd->prebuffered, inbuf, inbuf_len); + if (ppd->prebuffered == 0) { + struct timeval tv; + PARA_INFO_LOG("prebuffer period %dms\n", + conf->duration_arg); + ms2tv(conf->duration_arg, &tv); + tv_add(&tv, now, &ppd->barrier); + } + ppd->prebuffered += inbuf_len; + PARA_DEBUG_LOG("%zu bytes prebuffered\n", ppd->prebuffered); + if (*fn->fc->input_error >= 0) { + struct timeval diff; + if (tv_diff(now, &ppd->barrier, &diff) < 0) + goto out; + if (ppd->prebuffered < conf->size_arg) + goto out; + } +prebuffer_end: + fn->loaded = ppd->prebuffered; + ppd->prebuffered = -1; +out: + return inbuf_len; +} + +static void prebuffer_close(struct filter_node *fn) +{ + free(fn->private_data); + free(fn->buf); +} + +static int prebuffer_parse_config(int argc, char **argv, void **config) +{ + struct prebuffer_filter_args_info *prebuffer_conf + = para_calloc(sizeof(*prebuffer_conf)); + int ret = -E_PREBUFFER_SYNTAX; + + if (prebuffer_cmdline_parser(argc, argv, prebuffer_conf)) + goto err; + ret = -ERRNO_TO_PARA_ERROR(EINVAL); + if (prebuffer_conf->duration_arg < 0) + goto err; + if (prebuffer_conf->size_arg < 0) + goto err; + PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n", + prebuffer_conf->duration_arg, prebuffer_conf->size_arg); + *config = prebuffer_conf; + return 1; +err: + free(prebuffer_conf); + return ret; +} + +static void prebuffer_open(struct filter_node *fn) +{ + struct private_prebuffer_data *ppd = para_calloc(sizeof(*ppd)); + + ppd->conf = fn->conf; + fn->private_data = ppd; + fn->bufsize = 8192; /* gets increased on demand */ + fn->buf = para_malloc(fn->bufsize); +} + +/** + * The init function of the prebuffer filter. + * + * \param f Pointer to the struct to initialize. + */ +void prebuffer_filter_init(struct filter *f) +{ + struct prebuffer_filter_args_info dummy; + + prebuffer_cmdline_parser_init(&dummy); + f->open = prebuffer_open; + f->close = prebuffer_close; + f->convert = prebuffer_convert; + f->parse_config = prebuffer_parse_config; + f->help = (struct ggo_help) { + .short_help = prebuffer_filter_args_info_help, + .detailed_help = prebuffer_filter_args_info_detailed_help + }; +} -- 2.39.5