2 * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file prebuffer_filter.c Paraslash's prebuffering filter. */
13 #include "prebuffer_filter.cmdline.h"
17 #include "buffer_tree.h"
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. */
28 /** End of prebuffering period. */
29 struct timeval barrier
;
32 static ssize_t
prebuffer_convert(char *inbuf
, size_t inbuf_len
,
33 struct filter_node
*fn
)
35 struct private_prebuffer_data
*ppd
= fn
->private_data
;
36 struct prebuffer_filter_args_info
*conf
= ppd
->conf
;
39 if (*fn
->fc
->input_error
< 0 && ppd
->prebuffered
>= 0)
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
);
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
);
54 memcpy(fn
->buf
+ ppd
->prebuffered
, inbuf
, inbuf_len
);
55 if (ppd
->prebuffered
== 0) {
57 PARA_INFO_LOG("prebuffer period %dms\n",
59 ms2tv(conf
->duration_arg
, &tv
);
60 tv_add(&tv
, now
, &ppd
->barrier
);
62 ppd
->prebuffered
+= inbuf_len
;
63 PARA_DEBUG_LOG("%d bytes prebuffered\n", ppd
->prebuffered
);
64 if (*fn
->fc
->input_error
>= 0) {
66 if (tv_diff(now
, &ppd
->barrier
, &diff
) < 0)
68 if (ppd
->prebuffered
< conf
->size_arg
)
72 fn
->loaded
= ppd
->prebuffered
;
73 ppd
->prebuffered
= -1;
78 static void prebuffer_pre_select(struct sched
*s
, struct task
*t
)
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
;
90 if (ppd
->barrier
.tv_sec
== 0) {
92 PARA_INFO_LOG("prebuffer period %dms\n",
94 ms2tv(conf
->duration_arg
, &tv
);
95 tv_add(&tv
, now
, &ppd
->barrier
);
97 if (tv_diff(&ppd
->barrier
, now
, &diff
) < 0)
99 if (tv_diff(&diff
, &s
->timeout
, NULL
) < 0)
103 s
->timeout
.tv_sec
= 0;
104 s
->timeout
.tv_usec
= 1;
107 static void prebuffer_close(struct filter_node
*fn
)
109 free(fn
->private_data
);
113 static void prebuffer_post_select(__a_unused
struct sched
*s
, struct task
*t
)
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
;
122 if (ppd
->barrier
.tv_sec
== 0)
124 if (tv_diff(now
, &ppd
->barrier
, NULL
) < 0)
126 if (iqs
< conf
->size_arg
)
128 btr_splice_out_node(btrn
);
130 t
->error
= -E_PREBUFFER_SUCCESS
;
133 static int prebuffer_parse_config(int argc
, char **argv
, void **config
)
135 struct prebuffer_filter_args_info
*prebuffer_conf
136 = para_calloc(sizeof(*prebuffer_conf
));
137 int ret
= -E_PREBUFFER_SYNTAX
;
139 if (prebuffer_cmdline_parser(argc
, argv
, prebuffer_conf
))
141 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
142 if (prebuffer_conf
->duration_arg
< 0)
144 if (prebuffer_conf
->size_arg
< 0)
146 PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n",
147 prebuffer_conf
->duration_arg
, prebuffer_conf
->size_arg
);
148 *config
= prebuffer_conf
;
151 free(prebuffer_conf
);
155 static void prebuffer_open(struct filter_node
*fn
)
157 struct private_prebuffer_data
*ppd
= para_calloc(sizeof(*ppd
));
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
);
165 static void prebuffer_free_config(void *conf
)
167 prebuffer_cmdline_parser_free(conf
);
171 * The init function of the prebuffer filter.
173 * \param f Pointer to the struct to initialize.
175 void prebuffer_filter_init(struct filter
*f
)
177 struct prebuffer_filter_args_info dummy
;
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