Merge branch 't/audiod_com_version'
[paraslash.git] / prebuffer_filter.c
1 /*
2  * Copyright (C) 2009-2014 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
11 #include "para.h"
12 #include "prebuffer_filter.cmdline.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "buffer_tree.h"
17 #include "filter.h"
18 #include "string.h"
19 #include "error.h"
20
21 /** Data specific to the prebuffer filter. */
22 struct private_prebuffer_data {
23         /** The configuration data for this instance of the filter. */
24         struct prebuffer_filter_args_info *conf;
25         /** Number of bytes prebuffered or -1 if no longer prebuffering. */
26         int prebuffered;
27         /** End of prebuffering period. */
28         struct timeval barrier;
29 };
30
31 static void prebuffer_pre_select(struct sched *s, struct task *t)
32 {
33         struct filter_node *fn = container_of(t, struct filter_node, task);
34         struct btr_node *btrn = fn->btrn;
35         size_t iqs = btr_get_input_queue_size(btrn);
36         struct private_prebuffer_data *ppd = fn->private_data;
37         struct prebuffer_filter_args_info *conf = ppd->conf;
38         struct timeval diff;
39
40         if (iqs == 0)
41                 return;
42         if (ppd->barrier.tv_sec == 0) {
43                 struct timeval tv;
44                 PARA_INFO_LOG("prebuffer period %dms\n",
45                         conf->duration_arg);
46                 ms2tv(conf->duration_arg, &tv);
47                 tv_add(&tv, now, &ppd->barrier);
48         }
49         if (tv_diff(&ppd->barrier, now, &diff) < 0)
50                 return sched_min_delay(s);
51         sched_request_timeout(&diff, s);
52 }
53
54 static void prebuffer_close(struct filter_node *fn)
55 {
56         free(fn->private_data);
57 }
58
59 static int prebuffer_post_select(__a_unused struct sched *s, struct task *t)
60 {
61         struct filter_node *fn = container_of(t, struct filter_node, task);
62         struct btr_node *btrn = fn->btrn;
63         size_t iqs = btr_get_input_queue_size(btrn);
64         struct private_prebuffer_data *ppd = fn->private_data;
65         struct prebuffer_filter_args_info *conf = ppd->conf;
66
67         if (ppd->barrier.tv_sec == 0)
68                 return 0;
69         if (tv_diff(now, &ppd->barrier, NULL) < 0)
70                 return 0;
71         if (iqs < conf->size_arg)
72                 return 0;
73         btr_splice_out_node(&fn->btrn);
74         return -E_PREBUFFER_SUCCESS;
75 }
76
77 static int prebuffer_parse_config(int argc, char **argv, void **config)
78 {
79         struct prebuffer_filter_args_info *conf = para_calloc(sizeof(*conf));
80         int ret = -E_PREBUFFER_SYNTAX;
81
82         prebuffer_filter_cmdline_parser(argc, argv, conf);
83         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
84         if (conf->duration_arg < 0)
85                 goto err;
86         if (conf->size_arg < 0)
87                 goto err;
88         PARA_NOTICE_LOG("prebuffering %ims, %i bytes\n", conf->duration_arg,
89                 conf->size_arg);
90         *config = conf;
91         return 1;
92 err:
93         free(conf);
94         return ret;
95 }
96
97 static void prebuffer_open(struct filter_node *fn)
98 {
99         struct private_prebuffer_data *ppd = para_calloc(sizeof(*ppd));
100
101         ppd->conf = fn->conf;
102         fn->private_data = ppd;
103 }
104
105 static void prebuffer_free_config(void *conf)
106 {
107         prebuffer_filter_cmdline_parser_free(conf);
108 }
109
110 /**
111  * The init function of the prebuffer filter.
112  *
113  * \param f Pointer to the struct to initialize.
114  */
115 void prebuffer_filter_init(struct filter *f)
116 {
117         struct prebuffer_filter_args_info dummy;
118
119         prebuffer_filter_cmdline_parser_init(&dummy);
120         f->open = prebuffer_open;
121         f->close = prebuffer_close;
122         f->parse_config = prebuffer_parse_config;
123         f->free_config = prebuffer_free_config;
124         f->pre_select = prebuffer_pre_select;
125         f->post_select = prebuffer_post_select;
126         f->help = (struct ggo_help)DEFINE_GGO_HELP(prebuffer_filter);
127 }