Add btr support for the wav filter.
[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_close(struct filter_node *fn)
79 {
80         free(fn->private_data);
81         free(fn->buf);
82 }
83
84 static int prebuffer_parse_config(int argc, char **argv, void **config)
85 {
86         struct prebuffer_filter_args_info *prebuffer_conf
87                 = para_calloc(sizeof(*prebuffer_conf));
88         int ret = -E_PREBUFFER_SYNTAX;
89
90         if (prebuffer_cmdline_parser(argc, argv, prebuffer_conf))
91                 goto err;
92         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
93         if (prebuffer_conf->duration_arg < 0)
94                 goto err;
95         if (prebuffer_conf->size_arg < 0)
96                 goto err;
97         PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n",
98                 prebuffer_conf->duration_arg, prebuffer_conf->size_arg);
99         *config = prebuffer_conf;
100         return 1;
101 err:
102         free(prebuffer_conf);
103         return ret;
104 }
105
106 static void prebuffer_open(struct filter_node *fn)
107 {
108         struct private_prebuffer_data *ppd = para_calloc(sizeof(*ppd));
109
110         ppd->conf = fn->conf;
111         fn->private_data = ppd;
112         fn->bufsize = 8192; /* gets increased on demand */
113         fn->buf = para_malloc(fn->bufsize);
114 }
115
116 /**
117  * The init function of the prebuffer filter.
118  *
119  * \param f Pointer to the struct to initialize.
120  */
121 void prebuffer_filter_init(struct filter *f)
122 {
123         struct prebuffer_filter_args_info dummy;
124
125         prebuffer_cmdline_parser_init(&dummy);
126         f->open = prebuffer_open;
127         f->close = prebuffer_close;
128         f->convert = prebuffer_convert;
129         f->parse_config = prebuffer_parse_config;
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 }