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. */
10 #include "prebuffer_filter.cmdline.h"
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. */
24 /** End of prebuffering period. */
25 struct timeval barrier
;
28 static ssize_t
prebuffer_convert(char *inbuf
, size_t inbuf_len
,
29 struct filter_node
*fn
)
31 struct private_prebuffer_data
*ppd
= fn
->private_data
;
32 struct prebuffer_filter_args_info
*conf
= ppd
->conf
;
35 if (*fn
->fc
->input_error
< 0 && ppd
->prebuffered
>= 0)
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
);
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
);
50 memcpy(fn
->buf
+ ppd
->prebuffered
, inbuf
, inbuf_len
);
51 if (ppd
->prebuffered
== 0) {
53 PARA_INFO_LOG("prebuffer period %dms\n",
55 ms2tv(conf
->duration_arg
, &tv
);
56 tv_add(&tv
, now
, &ppd
->barrier
);
58 ppd
->prebuffered
+= inbuf_len
;
59 PARA_DEBUG_LOG("%zu bytes prebuffered\n", ppd
->prebuffered
);
60 if (*fn
->fc
->input_error
>= 0) {
62 if (tv_diff(now
, &ppd
->barrier
, &diff
) < 0)
64 if (ppd
->prebuffered
< conf
->size_arg
)
68 fn
->loaded
= ppd
->prebuffered
;
69 ppd
->prebuffered
= -1;
74 static void prebuffer_close(struct filter_node
*fn
)
76 free(fn
->private_data
);
80 static int prebuffer_parse_config(int argc
, char **argv
, void **config
)
82 struct prebuffer_filter_args_info
*prebuffer_conf
83 = para_calloc(sizeof(*prebuffer_conf
));
84 int ret
= -E_PREBUFFER_SYNTAX
;
86 if (prebuffer_cmdline_parser(argc
, argv
, prebuffer_conf
))
88 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
89 if (prebuffer_conf
->duration_arg
< 0)
91 if (prebuffer_conf
->size_arg
< 0)
93 PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n",
94 prebuffer_conf
->duration_arg
, prebuffer_conf
->size_arg
);
95 *config
= prebuffer_conf
;
102 static void prebuffer_open(struct filter_node
*fn
)
104 struct private_prebuffer_data
*ppd
= para_calloc(sizeof(*ppd
));
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
);
113 * The init function of the prebuffer filter.
115 * \param f Pointer to the struct to initialize.
117 void prebuffer_filter_init(struct filter
*f
)
119 struct prebuffer_filter_args_info dummy
;
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