Merge branch 't/wma_improvements'
[paraslash.git] / prebuffer_filter.c
1 /*
2  * Copyright (C) 2009-2012 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file prebuffer_filter.c Paraslash's prebuffering filter. */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "prebuffer_filter.cmdline.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "buffer_tree.h"
17 #include "filter.h"
18 #include "string.h"
19 #include "error.h"
20
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. */
26         int prebuffered;
27         /** End of prebuffering period. */
28         struct timeval barrier;
29 };
30
31 static void prebuffer_pre_select(struct sched *s, struct task *t)
32 {
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;
38         struct timeval diff;
39
40         t->error = 0;
41         if (iqs == 0)
42                 return;
43         if (ppd->barrier.tv_sec == 0) {
44                 struct timeval tv;
45                 PARA_INFO_LOG("prebuffer period %dms\n",
46                         conf->duration_arg);
47                 ms2tv(conf->duration_arg, &tv);
48                 tv_add(&tv, now, &ppd->barrier);
49         }
50         if (tv_diff(&ppd->barrier, now, &diff) < 0)
51                 return sched_min_delay(s);
52         sched_request_timeout(&diff, s);
53 }
54
55 static void prebuffer_close(struct filter_node *fn)
56 {
57         free(fn->private_data);
58 }
59
60 static void prebuffer_post_select(__a_unused struct sched *s, struct task *t)
61 {
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;
67
68         t->error = 0;
69         if (ppd->barrier.tv_sec == 0)
70                 return;
71         if (tv_diff(now, &ppd->barrier, NULL) < 0)
72                 return;
73         if (iqs < conf->size_arg)
74                 return;
75         btr_splice_out_node(btrn);
76         t->error = -E_PREBUFFER_SUCCESS;
77 }
78
79 static int prebuffer_parse_config(int argc, char **argv, void **config)
80 {
81         struct prebuffer_filter_args_info *prebuffer_conf
82                 = para_calloc(sizeof(*prebuffer_conf));
83         int ret = -E_PREBUFFER_SYNTAX;
84
85         if (prebuffer_cmdline_parser(argc, argv, prebuffer_conf))
86                 goto err;
87         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
88         if (prebuffer_conf->duration_arg < 0)
89                 goto err;
90         if (prebuffer_conf->size_arg < 0)
91                 goto err;
92         PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n",
93                 prebuffer_conf->duration_arg, prebuffer_conf->size_arg);
94         *config = prebuffer_conf;
95         return 1;
96 err:
97         free(prebuffer_conf);
98         return ret;
99 }
100
101 static void prebuffer_open(struct filter_node *fn)
102 {
103         struct private_prebuffer_data *ppd = para_calloc(sizeof(*ppd));
104
105         ppd->conf = fn->conf;
106         fn->private_data = ppd;
107 }
108
109 static void prebuffer_free_config(void *conf)
110 {
111         prebuffer_cmdline_parser_free(conf);
112 }
113
114 /**
115  * The init function of the prebuffer filter.
116  *
117  * \param f Pointer to the struct to initialize.
118  */
119 void prebuffer_filter_init(struct filter *f)
120 {
121         struct prebuffer_filter_args_info dummy;
122
123         prebuffer_cmdline_parser_init(&dummy);
124         f->open = prebuffer_open;
125         f->close = prebuffer_close;
126         f->parse_config = prebuffer_parse_config;
127         f->free_config = prebuffer_free_config;
128         f->pre_select = prebuffer_pre_select;
129         f->post_select = prebuffer_post_select;
130         f->help = (struct ggo_help) {
131                 .short_help = prebuffer_filter_args_info_help,
132                 .detailed_help = prebuffer_filter_args_info_detailed_help
133         };
134 }