para_server: Don't link with stat.o.
[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 "para.h"
10 #include "prebuffer_filter.cmdline.h"
11 #include "list.h"
12 #include "sched.h"
13 #include "ggo.h"
14 #include "filter.h"
15 #include "string.h"
16 #include "error.h"
17
18 /** Data specific to the prebuffer filter. */
19 struct private_prebuffer_data {
20 /** The configuration data for this instance of the filter. */
21 struct prebuffer_filter_args_info *conf;
22 /** Number of bytes prebuffered or -1 if no longer prebuffering. */
23 int prebuffered;
24 /** End of prebuffering period. */
25 struct timeval barrier;
26 };
27
28 static ssize_t prebuffer_convert(char *inbuf, size_t inbuf_len,
29 struct filter_node *fn)
30 {
31 struct private_prebuffer_data *ppd = fn->private_data;
32 struct prebuffer_filter_args_info *conf = ppd->conf;
33
34 if (inbuf_len == 0) {
35 if (*fn->fc->input_error < 0 && ppd->prebuffered >= 0)
36 goto prebuffer_end;
37 return 0;
38 }
39 if (ppd->prebuffered < 0) {
40 size_t copy = PARA_MIN(inbuf_len, fn->bufsize - fn->loaded);
41 memcpy(fn->buf + fn->loaded, inbuf, copy);
42 fn->loaded += copy;
43 return copy;
44 }
45 if (ppd->prebuffered + inbuf_len > fn->bufsize) {
46 fn->bufsize = PARA_MAX(2 * fn->bufsize,
47 ppd->prebuffered + inbuf_len);
48 fn->buf = para_realloc(fn->buf, fn->bufsize);
49 }
50 memcpy(fn->buf + ppd->prebuffered, inbuf, inbuf_len);
51 if (ppd->prebuffered == 0) {
52 struct timeval tv;
53 PARA_INFO_LOG("prebuffer period %dms\n",
54 conf->duration_arg);
55 ms2tv(conf->duration_arg, &tv);
56 tv_add(&tv, now, &ppd->barrier);
57 }
58 ppd->prebuffered += inbuf_len;
59 PARA_DEBUG_LOG("%d bytes prebuffered\n", ppd->prebuffered);
60 if (*fn->fc->input_error >= 0) {
61 struct timeval diff;
62 if (tv_diff(now, &ppd->barrier, &diff) < 0)
63 goto out;
64 if (ppd->prebuffered < conf->size_arg)
65 goto out;
66 }
67 prebuffer_end:
68 fn->loaded = ppd->prebuffered;
69 ppd->prebuffered = -1;
70 out:
71 return inbuf_len;
72 }
73
74 static void prebuffer_close(struct filter_node *fn)
75 {
76 free(fn->private_data);
77 free(fn->buf);
78 }
79
80 static int prebuffer_parse_config(int argc, char **argv, void **config)
81 {
82 struct prebuffer_filter_args_info *prebuffer_conf
83 = para_calloc(sizeof(*prebuffer_conf));
84 int ret = -E_PREBUFFER_SYNTAX;
85
86 if (prebuffer_cmdline_parser(argc, argv, prebuffer_conf))
87 goto err;
88 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
89 if (prebuffer_conf->duration_arg < 0)
90 goto err;
91 if (prebuffer_conf->size_arg < 0)
92 goto err;
93 PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n",
94 prebuffer_conf->duration_arg, prebuffer_conf->size_arg);
95 *config = prebuffer_conf;
96 return 1;
97 err:
98 free(prebuffer_conf);
99 return ret;
100 }
101
102 static void prebuffer_open(struct filter_node *fn)
103 {
104 struct private_prebuffer_data *ppd = para_calloc(sizeof(*ppd));
105
106 ppd->conf = fn->conf;
107 fn->private_data = ppd;
108 fn->bufsize = 8192; /* gets increased on demand */
109 fn->buf = para_malloc(fn->bufsize);
110 }
111
112 /**
113 * The init function of the prebuffer filter.
114 *
115 * \param f Pointer to the struct to initialize.
116 */
117 void prebuffer_filter_init(struct filter *f)
118 {
119 struct prebuffer_filter_args_info dummy;
120
121 prebuffer_cmdline_parser_init(&dummy);
122 f->open = prebuffer_open;
123 f->close = prebuffer_close;
124 f->convert = prebuffer_convert;
125 f->parse_config = prebuffer_parse_config;
126 f->help = (struct ggo_help) {
127 .short_help = prebuffer_filter_args_info_help,
128 .detailed_help = prebuffer_filter_args_info_detailed_help
129 };
130 }