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. */
12 #include "prebuffer_filter.cmdline.h"
20 /** Data specific to the prebuffer filter. */
21 struct private_prebuffer_data
{
22 /** The configuration data for this instance of the filter. */
23 struct prebuffer_filter_args_info
*conf
;
24 /** Number of bytes prebuffered or -1 if no longer prebuffering. */
26 /** End of prebuffering period. */
27 struct timeval barrier
;
30 static ssize_t
prebuffer_convert(char *inbuf
, size_t inbuf_len
,
31 struct filter_node
*fn
)
33 struct private_prebuffer_data
*ppd
= fn
->private_data
;
34 struct prebuffer_filter_args_info
*conf
= ppd
->conf
;
37 if (*fn
->fc
->input_error
< 0 && ppd
->prebuffered
>= 0)
41 if (ppd
->prebuffered
< 0) {
42 size_t copy
= PARA_MIN(inbuf_len
, fn
->bufsize
- fn
->loaded
);
43 memcpy(fn
->buf
+ fn
->loaded
, inbuf
, copy
);
47 if (ppd
->prebuffered
+ inbuf_len
> fn
->bufsize
) {
48 fn
->bufsize
= PARA_MAX(2 * fn
->bufsize
,
49 ppd
->prebuffered
+ inbuf_len
);
50 fn
->buf
= para_realloc(fn
->buf
, fn
->bufsize
);
52 memcpy(fn
->buf
+ ppd
->prebuffered
, inbuf
, inbuf_len
);
53 if (ppd
->prebuffered
== 0) {
55 PARA_INFO_LOG("prebuffer period %dms\n",
57 ms2tv(conf
->duration_arg
, &tv
);
58 tv_add(&tv
, now
, &ppd
->barrier
);
60 ppd
->prebuffered
+= inbuf_len
;
61 PARA_DEBUG_LOG("%d bytes prebuffered\n", ppd
->prebuffered
);
62 if (*fn
->fc
->input_error
>= 0) {
64 if (tv_diff(now
, &ppd
->barrier
, &diff
) < 0)
66 if (ppd
->prebuffered
< conf
->size_arg
)
70 fn
->loaded
= ppd
->prebuffered
;
71 ppd
->prebuffered
= -1;
76 static void prebuffer_close(struct filter_node
*fn
)
78 free(fn
->private_data
);
82 static int prebuffer_parse_config(int argc
, char **argv
, void **config
)
84 struct prebuffer_filter_args_info
*prebuffer_conf
85 = para_calloc(sizeof(*prebuffer_conf
));
86 int ret
= -E_PREBUFFER_SYNTAX
;
88 if (prebuffer_cmdline_parser(argc
, argv
, prebuffer_conf
))
90 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
91 if (prebuffer_conf
->duration_arg
< 0)
93 if (prebuffer_conf
->size_arg
< 0)
95 PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n",
96 prebuffer_conf
->duration_arg
, prebuffer_conf
->size_arg
);
97 *config
= prebuffer_conf
;
100 free(prebuffer_conf
);
104 static void prebuffer_open(struct filter_node
*fn
)
106 struct private_prebuffer_data
*ppd
= para_calloc(sizeof(*ppd
));
108 ppd
->conf
= fn
->conf
;
109 fn
->private_data
= ppd
;
110 fn
->bufsize
= 8192; /* gets increased on demand */
111 fn
->buf
= para_malloc(fn
->bufsize
);
115 * The init function of the prebuffer filter.
117 * \param f Pointer to the struct to initialize.
119 void prebuffer_filter_init(struct filter
*f
)
121 struct prebuffer_filter_args_info dummy
;
123 prebuffer_cmdline_parser_init(&dummy
);
124 f
->open
= prebuffer_open
;
125 f
->close
= prebuffer_close
;
126 f
->convert
= prebuffer_convert
;
127 f
->parse_config
= prebuffer_parse_config
;
128 f
->help
= (struct ggo_help
) {
129 .short_help
= prebuffer_filter_args_info_help
,
130 .detailed_help
= prebuffer_filter_args_info_detailed_help