2 * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file prebuffer_filter.c Paraslash's prebuffering filter. */
13 #include "prebuffer_filter.cmdline.h"
17 #include "buffer_tree.h"
22 /** Data specific to the prebuffer filter. */
23 struct private_prebuffer_data {
24 /** The configuration data for this instance of the filter. */
25 struct prebuffer_filter_args_info *conf;
26 /** Number of bytes prebuffered or -1 if no longer prebuffering. */
28 /** End of prebuffering period. */
29 struct timeval barrier;
32 static void prebuffer_pre_select(struct sched *s, struct task *t)
34 struct filter_node *fn = container_of(t, struct filter_node, task);
35 struct btr_node *btrn = fn->btrn;
36 size_t iqs = btr_get_input_queue_size(btrn);
37 struct private_prebuffer_data *ppd = fn->private_data;
38 struct prebuffer_filter_args_info *conf = ppd->conf;
44 if (ppd->barrier.tv_sec == 0) {
46 PARA_INFO_LOG("prebuffer period %dms\n",
48 ms2tv(conf->duration_arg, &tv);
49 tv_add(&tv, now, &ppd->barrier);
51 if (tv_diff(&ppd->barrier, now, &diff) < 0)
52 return sched_min_delay(s);
53 sched_request_timeout(&diff, s);
56 static void prebuffer_close(struct filter_node *fn)
58 free(fn->private_data);
62 static void prebuffer_post_select(__a_unused struct sched *s, struct task *t)
64 struct filter_node *fn = container_of(t, struct filter_node, task);
65 struct btr_node *btrn = fn->btrn;
66 size_t iqs = btr_get_input_queue_size(btrn);
67 struct private_prebuffer_data *ppd = fn->private_data;
68 struct prebuffer_filter_args_info *conf = ppd->conf;
71 if (ppd->barrier.tv_sec == 0)
73 if (tv_diff(now, &ppd->barrier, NULL) < 0)
75 if (iqs < conf->size_arg)
77 btr_splice_out_node(btrn);
79 t->error = -E_PREBUFFER_SUCCESS;
82 static int prebuffer_parse_config(int argc, char **argv, void **config)
84 struct prebuffer_filter_args_info *prebuffer_conf
85 = para_calloc(sizeof(*prebuffer_conf));
86 int ret = -E_PREBUFFER_SYNTAX;
88 if (prebuffer_cmdline_parser(argc, argv, prebuffer_conf))
90 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
91 if (prebuffer_conf->duration_arg < 0)
93 if (prebuffer_conf->size_arg < 0)
95 PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n",
96 prebuffer_conf->duration_arg, prebuffer_conf->size_arg);
97 *config = prebuffer_conf;
100 free(prebuffer_conf);
104 static void prebuffer_open(struct filter_node *fn)
106 struct private_prebuffer_data *ppd = para_calloc(sizeof(*ppd));
108 ppd->conf = fn->conf;
109 fn->private_data = ppd;
110 fn->bufsize = 8192; /* gets increased on demand */
111 fn->buf = para_malloc(fn->bufsize);
114 static void prebuffer_free_config(void *conf)
116 prebuffer_cmdline_parser_free(conf);
120 * The init function of the prebuffer filter.
122 * \param f Pointer to the struct to initialize.
124 void prebuffer_filter_init(struct filter *f)
126 struct prebuffer_filter_args_info dummy;
128 prebuffer_cmdline_parser_init(&dummy);
129 f->open = prebuffer_open;
130 f->close = prebuffer_close;
131 f->parse_config = prebuffer_parse_config;
132 f->free_config = prebuffer_free_config;
133 f->pre_select = prebuffer_pre_select;
134 f->post_select = prebuffer_post_select;
135 f->help = (struct ggo_help) {
136 .short_help = prebuffer_filter_args_info_help,
137 .detailed_help = prebuffer_filter_args_info_detailed_help