2 * Copyright (C) 2009-2012 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. */
12 #include "prebuffer_filter.cmdline.h"
16 #include "buffer_tree.h"
21 /** Data specific to the prebuffer filter. */
22 struct private_prebuffer_data {
23 /** The configuration data for this instance of the filter. */
24 struct prebuffer_filter_args_info *conf;
25 /** Number of bytes prebuffered or -1 if no longer prebuffering. */
27 /** End of prebuffering period. */
28 struct timeval barrier;
31 static void prebuffer_pre_select(struct sched *s, struct task *t)
33 struct filter_node *fn = container_of(t, struct filter_node, task);
34 struct btr_node *btrn = fn->btrn;
35 size_t iqs = btr_get_input_queue_size(btrn);
36 struct private_prebuffer_data *ppd = fn->private_data;
37 struct prebuffer_filter_args_info *conf = ppd->conf;
43 if (ppd->barrier.tv_sec == 0) {
45 PARA_INFO_LOG("prebuffer period %dms\n",
47 ms2tv(conf->duration_arg, &tv);
48 tv_add(&tv, now, &ppd->barrier);
50 if (tv_diff(&ppd->barrier, now, &diff) < 0)
51 return sched_min_delay(s);
52 sched_request_timeout(&diff, s);
55 static void prebuffer_close(struct filter_node *fn)
57 free(fn->private_data);
60 static void prebuffer_post_select(__a_unused struct sched *s, struct task *t)
62 struct filter_node *fn = container_of(t, struct filter_node, task);
63 struct btr_node *btrn = fn->btrn;
64 size_t iqs = btr_get_input_queue_size(btrn);
65 struct private_prebuffer_data *ppd = fn->private_data;
66 struct prebuffer_filter_args_info *conf = ppd->conf;
69 if (ppd->barrier.tv_sec == 0)
71 if (tv_diff(now, &ppd->barrier, NULL) < 0)
73 if (iqs < conf->size_arg)
75 btr_splice_out_node(btrn);
76 t->error = -E_PREBUFFER_SUCCESS;
79 static int prebuffer_parse_config(int argc, char **argv, void **config)
81 struct prebuffer_filter_args_info *conf = para_calloc(sizeof(*conf));
82 int ret = -E_PREBUFFER_SYNTAX;
84 prebuffer_filter_cmdline_parser(argc, argv, conf);
85 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
86 if (conf->duration_arg < 0)
88 if (conf->size_arg < 0)
90 PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n", conf->duration_arg,
99 static void prebuffer_open(struct filter_node *fn)
101 struct private_prebuffer_data *ppd = para_calloc(sizeof(*ppd));
103 ppd->conf = fn->conf;
104 fn->private_data = ppd;
107 static void prebuffer_free_config(void *conf)
109 prebuffer_filter_cmdline_parser_free(conf);
113 * The init function of the prebuffer filter.
115 * \param f Pointer to the struct to initialize.
117 void prebuffer_filter_init(struct filter *f)
119 struct prebuffer_filter_args_info dummy;
121 prebuffer_filter_cmdline_parser_init(&dummy);
122 f->open = prebuffer_open;
123 f->close = prebuffer_close;
124 f->parse_config = prebuffer_parse_config;
125 f->free_config = prebuffer_free_config;
126 f->pre_select = prebuffer_pre_select;
127 f->post_select = prebuffer_post_select;
128 f->help = (struct ggo_help) {
129 .short_help = prebuffer_filter_args_info_help,
130 .detailed_help = prebuffer_filter_args_info_detailed_help