a83eacc9d7e53f627985f6cfdfc86eccb588fa47
[paraslash.git] / prebuffer_filter.c
1 /*
2  * Copyright (C) 2009 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 #include <stdbool.h>
11
12 #include "para.h"
13 #include "prebuffer_filter.cmdline.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "filter.h"
19 #include "string.h"
20 #include "error.h"
21
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. */
27         int prebuffered;
28         /** End of prebuffering period. */
29         struct timeval barrier;
30 };
31
32 static void prebuffer_pre_select(struct sched *s, struct task *t)
33 {
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;
39         struct timeval diff;
40
41         t->error = 0;
42         if (iqs == 0)
43                 return;
44         if (ppd->barrier.tv_sec == 0) {
45                 struct timeval tv;
46                 PARA_INFO_LOG("prebuffer period %dms\n",
47                         conf->duration_arg);
48                 ms2tv(conf->duration_arg, &tv);
49                 tv_add(&tv, now, &ppd->barrier);
50         }
51         if (tv_diff(&ppd->barrier, now, &diff) < 0)
52                 goto min_delay;
53         if (tv_diff(&diff, &s->timeout, NULL) < 0)
54                 s->timeout = diff;
55         return;
56 min_delay:
57         s->timeout.tv_sec = 0;
58         s->timeout.tv_usec = 1;
59 }
60
61 static void prebuffer_close(struct filter_node *fn)
62 {
63         free(fn->private_data);
64         free(fn->buf);
65 }
66
67 static void prebuffer_post_select(__a_unused struct sched *s, struct task *t)
68 {
69         struct filter_node *fn = container_of(t, struct filter_node, task);
70         struct btr_node *btrn = fn->btrn;
71         size_t iqs = btr_get_input_queue_size(btrn);
72         struct private_prebuffer_data *ppd = fn->private_data;
73         struct prebuffer_filter_args_info *conf = ppd->conf;
74
75         t->error = 0;
76         if (ppd->barrier.tv_sec == 0)
77                 return;
78         if (tv_diff(now, &ppd->barrier, NULL) < 0)
79                 return;
80         if (iqs < conf->size_arg)
81                 return;
82         btr_splice_out_node(btrn);
83         prebuffer_close(fn);
84         t->error = -E_PREBUFFER_SUCCESS;
85 }
86
87 static int prebuffer_parse_config(int argc, char **argv, void **config)
88 {
89         struct prebuffer_filter_args_info *prebuffer_conf
90                 = para_calloc(sizeof(*prebuffer_conf));
91         int ret = -E_PREBUFFER_SYNTAX;
92
93         if (prebuffer_cmdline_parser(argc, argv, prebuffer_conf))
94                 goto err;
95         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
96         if (prebuffer_conf->duration_arg < 0)
97                 goto err;
98         if (prebuffer_conf->size_arg < 0)
99                 goto err;
100         PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n",
101                 prebuffer_conf->duration_arg, prebuffer_conf->size_arg);
102         *config = prebuffer_conf;
103         return 1;
104 err:
105         free(prebuffer_conf);
106         return ret;
107 }
108
109 static void prebuffer_open(struct filter_node *fn)
110 {
111         struct private_prebuffer_data *ppd = para_calloc(sizeof(*ppd));
112
113         ppd->conf = fn->conf;
114         fn->private_data = ppd;
115         fn->bufsize = 8192; /* gets increased on demand */
116         fn->buf = para_malloc(fn->bufsize);
117 }
118
119 static void prebuffer_free_config(void *conf)
120 {
121         prebuffer_cmdline_parser_free(conf);
122 }
123
124 /**
125  * The init function of the prebuffer filter.
126  *
127  * \param f Pointer to the struct to initialize.
128  */
129 void prebuffer_filter_init(struct filter *f)
130 {
131         struct prebuffer_filter_args_info dummy;
132
133         prebuffer_cmdline_parser_init(&dummy);
134         f->open = prebuffer_open;
135         f->close = prebuffer_close;
136         f->parse_config = prebuffer_parse_config;
137         f->free_config = prebuffer_free_config;
138         f->pre_select = prebuffer_pre_select;
139         f->post_select = prebuffer_post_select;
140         f->help = (struct ggo_help) {
141                 .short_help = prebuffer_filter_args_info_help,
142                 .detailed_help = prebuffer_filter_args_info_detailed_help
143         };
144 }