stdin: Use buffer pools.
[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 ssize_t prebuffer_convert(char *inbuf, size_t inbuf_len,
33                 struct filter_node *fn)
34 {
35         struct private_prebuffer_data *ppd = fn->private_data;
36         struct prebuffer_filter_args_info *conf = ppd->conf;
37
38         if (inbuf_len == 0) {
39                 if (*fn->fc->input_error < 0 && ppd->prebuffered >= 0)
40                         goto prebuffer_end;
41                 return 0;
42         }
43         if (ppd->prebuffered < 0) {
44                 size_t copy = PARA_MIN(inbuf_len, fn->bufsize - fn->loaded);
45                 memcpy(fn->buf + fn->loaded, inbuf, copy);
46                 fn->loaded += copy;
47                 return copy;
48         }
49         if (ppd->prebuffered + inbuf_len > fn->bufsize) {
50                 fn->bufsize = PARA_MAX(2 * fn->bufsize,
51                         ppd->prebuffered + inbuf_len);
52                 fn->buf = para_realloc(fn->buf, fn->bufsize);
53         }
54         memcpy(fn->buf + ppd->prebuffered, inbuf, inbuf_len);
55         if (ppd->prebuffered == 0) {
56                 struct timeval tv;
57                 PARA_INFO_LOG("prebuffer period %dms\n",
58                         conf->duration_arg);
59                 ms2tv(conf->duration_arg, &tv);
60                 tv_add(&tv, now, &ppd->barrier);
61         }
62         ppd->prebuffered += inbuf_len;
63         PARA_DEBUG_LOG("%d bytes prebuffered\n", ppd->prebuffered);
64         if (*fn->fc->input_error >= 0) {
65                 struct timeval diff;
66                 if (tv_diff(now, &ppd->barrier, &diff) < 0)
67                         goto out;
68                 if (ppd->prebuffered < conf->size_arg)
69                         goto out;
70         }
71 prebuffer_end:
72         fn->loaded = ppd->prebuffered;
73         ppd->prebuffered = -1;
74 out:
75         return inbuf_len;
76 }
77
78 static void prebuffer_pre_select(struct sched *s, struct task *t)
79 {
80         struct filter_node *fn = container_of(t, struct filter_node, task);
81         struct btr_node *btrn = fn->btrn;
82         size_t iqs = btr_get_input_queue_size(btrn);
83         struct private_prebuffer_data *ppd = fn->private_data;
84         struct prebuffer_filter_args_info *conf = ppd->conf;
85         struct timeval diff;
86
87         t->error = 0;
88         if (iqs == 0)
89                 return;
90         if (ppd->barrier.tv_sec == 0) {
91                 struct timeval tv;
92                 PARA_INFO_LOG("prebuffer period %dms\n",
93                         conf->duration_arg);
94                 ms2tv(conf->duration_arg, &tv);
95                 tv_add(&tv, now, &ppd->barrier);
96         }
97         if (tv_diff(&ppd->barrier, now, &diff) < 0)
98                 goto min_delay;
99         if (tv_diff(&diff, &s->timeout, NULL) < 0)
100                 s->timeout = diff;
101         return;
102 min_delay:
103         s->timeout.tv_sec = 0;
104         s->timeout.tv_usec = 1;
105 }
106
107 static void prebuffer_close(struct filter_node *fn)
108 {
109         free(fn->private_data);
110         free(fn->buf);
111 }
112
113 static void prebuffer_post_select(__a_unused struct sched *s, struct task *t)
114 {
115         struct filter_node *fn = container_of(t, struct filter_node, task);
116         struct btr_node *btrn = fn->btrn;
117         size_t iqs = btr_get_input_queue_size(btrn);
118         struct private_prebuffer_data *ppd = fn->private_data;
119         struct prebuffer_filter_args_info *conf = ppd->conf;
120
121         t->error = 0;
122         if (ppd->barrier.tv_sec == 0)
123                 return;
124         if (tv_diff(now, &ppd->barrier, NULL) < 0)
125                 return;
126         if (iqs < conf->size_arg)
127                 return;
128         btr_splice_out_node(btrn);
129         prebuffer_close(fn);
130         t->error = -E_PREBUFFER_SUCCESS;
131 }
132
133 static int prebuffer_parse_config(int argc, char **argv, void **config)
134 {
135         struct prebuffer_filter_args_info *prebuffer_conf
136                 = para_calloc(sizeof(*prebuffer_conf));
137         int ret = -E_PREBUFFER_SYNTAX;
138
139         if (prebuffer_cmdline_parser(argc, argv, prebuffer_conf))
140                 goto err;
141         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
142         if (prebuffer_conf->duration_arg < 0)
143                 goto err;
144         if (prebuffer_conf->size_arg < 0)
145                 goto err;
146         PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n",
147                 prebuffer_conf->duration_arg, prebuffer_conf->size_arg);
148         *config = prebuffer_conf;
149         return 1;
150 err:
151         free(prebuffer_conf);
152         return ret;
153 }
154
155 static void prebuffer_open(struct filter_node *fn)
156 {
157         struct private_prebuffer_data *ppd = para_calloc(sizeof(*ppd));
158
159         ppd->conf = fn->conf;
160         fn->private_data = ppd;
161         fn->bufsize = 8192; /* gets increased on demand */
162         fn->buf = para_malloc(fn->bufsize);
163 }
164
165 static void prebuffer_free_config(void *conf)
166 {
167         prebuffer_cmdline_parser_free(conf);
168 }
169
170 /**
171  * The init function of the prebuffer filter.
172  *
173  * \param f Pointer to the struct to initialize.
174  */
175 void prebuffer_filter_init(struct filter *f)
176 {
177         struct prebuffer_filter_args_info dummy;
178
179         prebuffer_cmdline_parser_init(&dummy);
180         f->open = prebuffer_open;
181         f->close = prebuffer_close;
182         f->convert = prebuffer_convert;
183         f->parse_config = prebuffer_parse_config;
184         f->free_config = prebuffer_free_config;
185         f->pre_select = prebuffer_pre_select;
186         f->post_select = prebuffer_post_select;
187         f->help = (struct ggo_help) {
188                 .short_help = prebuffer_filter_args_info_help,
189                 .detailed_help = prebuffer_filter_args_info_detailed_help
190         };
191 }