Add btr support to filter code.
[paraslash.git] / grab_client.c
1 /*
2  * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /**
8  * \file grab_client.c Functions for grabbing the stream at any position
9  * in a filter chain.
10  *
11  * \sa filter_chain filter_chain_info filter.
12  */
13
14 #include <regex.h>
15 #include <sys/types.h>
16 #include <dirent.h>
17 #include <stdbool.h>
18
19 #include "para.h"
20 #include "list.h"
21 #include "sched.h"
22 #include "ggo.h"
23 #include "buffer_tree.h"
24 #include "filter.h"
25 #include "grab_client.h"
26 #include "audiod.h"
27 #include "error.h"
28 #include "string.h"
29 #include "fd.h"
30
31 /** Grab clients that are not yet attached to a filter node. */
32 static struct list_head inactive_grab_client_list;
33
34 static int max_num_filters(void)
35 {
36         int i, ret = 0;
37
38         for (i = 0; audio_formats[i]; i++) {
39                 PARA_INFO_LOG("%s filter chain length: %d\n", audio_formats[i],
40                         num_filters(i));
41                 ret = PARA_MAX(ret, num_filters(i));
42         }
43         PARA_INFO_LOG("maximal filter chain length: %d\n", ret);
44         return ret;
45 }
46
47 static struct filter_node *find_filter_node(int format, int filternum)
48 {
49         int i;
50
51         FOR_EACH_SLOT(i) {
52                 struct slot_info *s = &slot[i];
53                 if (s->format < 0 || !s->fc)
54                         continue;
55                 if (format >= 0 && s->format != format)
56                         continue;
57                 if (num_filters(i) <= filternum)
58                         continue;
59                 /* success */
60                 return  s->fc->filter_nodes + filternum;
61         }
62         return NULL;
63 }
64
65 static int gc_write(char *buf, size_t len, struct filter_callback *fcb)
66 {
67         struct grab_client *gc = container_of(fcb, struct grab_client, fcb);
68         size_t written = 0;
69
70         while (written < len) {
71                 int ret = write_ok(gc->fd);
72                 if (ret < 0)
73                         goto err;
74                 if (ret == 0) { /* fd not ready */
75                         if (gc->mode == GM_PEDANTIC)
76                                 goto err;
77                         if (gc->mode == GM_SLOPPY)
78                                 return 1;
79                 }
80                 ret = write(gc->fd, buf + written, len - written);
81                 if (ret < 0) {
82                         if (errno != EAGAIN && errno != EINTR)
83                                 goto err;
84                         if (gc->mode == GM_PEDANTIC)
85                                 goto err;
86                         if (gc->mode == GM_SLOPPY)
87                                 return 1;
88                 } else
89                         written += ret;
90         }
91         return 1;
92 err:
93         gc->error = -E_GC_WRITE;
94         return -E_GC_WRITE;
95 }
96
97 static void add_inactive_gc(struct grab_client *gc)
98 {
99         PARA_INFO_LOG("adding grab client %p (fd %d) to inactive list\n",
100                 gc, gc->fd);
101         para_list_add(&gc->node, &inactive_grab_client_list);
102 }
103
104 static void gc_close(struct filter_callback *fcb)
105 {
106         struct grab_client *gc = container_of(fcb, struct grab_client, fcb);
107
108         if ((gc->flags & GF_ONE_SHOT) || gc->error < 0) {
109                 PARA_INFO_LOG("closing fd %d (grab client %p)\n", gc->fd, gc);
110                 close(gc->fd);
111                 free(gc);
112                 return;
113         }
114         add_inactive_gc(gc);
115 }
116
117 /**
118  * Move a grab client from the inactive list to a filter node.
119  *
120  * \param gc The grab client to activate.
121  * \param fn The filter node \a gc gets attached to.
122  *
123  * \sa filter_node::callbacks, inactive_grab_client_list.
124  */
125 void activate_grab_client(struct grab_client *gc, struct filter_node *fn)
126 {
127         PARA_INFO_LOG("activating %p (fd %d, filter node: %p)\n", gc, gc->fd, fn);
128         list_del(&gc->node);
129         para_list_add(&gc->fcb.node, &fn->callbacks);
130 }
131
132 /**
133  * Activate inactive grab clients if possible.
134  *
135  * \param audio_format_num The number of the audio format of the new audio file.
136  * \param fc The filter chain containing the activated filters.
137  *
138  * This is called from audiod.c when the current audio file changes. It loops
139  * over all inactive grab clients and checks each grab client's configuration
140  * to determine if the client in question wishes to grab the new stream.  If
141  * yes, this grab client is moved from the inactive grab client list to an
142  * appropriate filter_node.
143  *
144  * \sa filter_chain_info::filters, inactive_grab_client_list,
145  * activate_grab_client.
146  */
147 void activate_inactive_grab_clients(int audio_format_num,
148                 struct filter_chain *fc)
149 {
150         struct grab_client *gc, *tmp;
151         struct filter_node *fn;
152
153         list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
154                 if (gc->audio_format_num >= 0 && gc->audio_format_num !=
155                                 audio_format_num)
156                         continue;
157                 if (gc->filter_num >= num_filters(audio_format_num))
158                         continue;
159                 fn = fc->filter_nodes + gc->filter_num;
160                 activate_grab_client(gc, fn);
161         }
162 }
163
164 static int check_gc_args(int argc, char **argv, struct grab_client *gc)
165 {
166         int i, ret;
167
168         gc->audio_format_num = -1; /* default: grab any audio format */
169         for (i = 1; i < argc; i++) {
170                 const char *arg = argv[i];
171                 if (arg[0] != '-')
172                         break;
173                 if (!strcmp(arg, "--")) {
174                         i++;
175                         break;
176                 }
177                 if (!strncmp(arg, "-n=", 3)) {
178                         ret = para_atoi32(arg + 3, &gc->filter_num);
179                         if (ret < 0)
180                                 return ret;
181                         if (gc->filter_num < 0)
182                                 return -E_BAD_GC_FILTER_NUM;
183                         if (gc->filter_num >= max_num_filters())
184                                 return -E_BAD_GC_FILTER_NUM;
185                         continue;
186                 }
187                 if (!strncmp(arg, "-m", 2)) {
188                         if (*(arg + 3))
189                                 return -E_GC_SYNTAX;
190                         switch(*(arg + 2)) {
191                         case 's':
192                                 gc->mode = GM_SLOPPY;
193                                 continue;
194                         case 'a':
195                                 gc->mode = GM_AGGRESSIVE;
196                                 continue;
197                         case 'p':
198                                 gc->mode = GM_PEDANTIC;
199                                 continue;
200                         default:
201                                 return -E_GC_SYNTAX;
202                         }
203                 }
204                 if (!strcmp(arg, "-i")) {
205                         gc->flags |= GF_INPUT_GRAB;
206                         continue;
207                 }
208                 if (!strcmp(arg, "-o")) {
209                         gc->flags |= GF_ONE_SHOT;
210                         continue;
211                 }
212                 if (!strncmp(arg, "-f=", 3)) {
213                         ret = get_audio_format_num(arg + 3);
214                         if (ret < 0)
215                                 return ret;
216                         gc->audio_format_num = ret;
217                         continue;
218                 }
219                 return -E_GC_SYNTAX;
220         }
221         if (i != argc)
222                 return -E_GC_SYNTAX;
223         return 1;
224 }
225
226 /**
227  * Check the command line options and allocate a grab_client structure.
228  *
229  * \param fd The file descriptor of the client.
230  * \param argc Argument count.
231  * \param argv Argument vector.
232  *
233  * If the command line options given by \a argc and \a argv are valid.
234  * allocate a struct grab_client and initialize it with this valid
235  * configuration. Moreover, add the new grab client to the inactive list.
236  *
237  * \return Standard.
238  *
239  * \sa grab_client, inactive_grab_client_list, activate_grab_client,
240  * filter_node::callbacks.
241  */
242 int grab_client_new(int fd, int argc, char **argv)
243 {
244         int ret;
245         struct grab_client *gc = para_calloc(sizeof(struct grab_client));
246         struct filter_node *fn;
247
248         ret = check_gc_args(argc, argv, gc);
249         if (ret < 0)
250                 goto err_out;
251         if (gc->flags & GF_INPUT_GRAB)
252                 gc->fcb.input_cb = gc_write;
253         else
254                 gc->fcb.output_cb = gc_write;
255         gc->fd = fd;
256         gc->fcb.close = gc_close;
257         fn = find_filter_node(gc->audio_format_num, gc->filter_num);
258         if (fn)
259                 para_list_add(&gc->fcb.node, &fn->callbacks);
260         else
261                 add_inactive_gc(gc);
262         return 1;
263 err_out:
264         free(gc);
265         return ret;
266 }
267
268 /**
269  * Initialize the grabbing subsystem.
270  *
271  * This has to be called once during startup before any other function from
272  * grab_client.c may be used. It initializes \a inactive_grab_client_list.
273  */
274 void init_grabbing(void)
275 {
276         PARA_INFO_LOG("grab init\n");
277         INIT_LIST_HEAD(&inactive_grab_client_list);
278 }