Kill unused struct filter_callbacks.
[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 /** \file grab_client.c Functions for grabbing the audio stream. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <stdbool.h>
13
14 #include "para.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "ggo.h"
18 #include "buffer_tree.h"
19 #include "filter.h"
20 #include "grab_client.h"
21 #include "audiod.h"
22 #include "error.h"
23 #include "string.h"
24 #include "fd.h"
25
26 /**
27  * How to handle blocking writes for the grab client fds.
28  */
29 enum grab_mode {
30         /** Ignore the data and do not write. */
31         GM_SLOPPY,
32         /** Write anyway (default). */
33         GM_AGGRESSIVE,
34         /** Close fd if write would block. */
35         GM_PEDANTIC,
36 };
37
38 /** Flags specified as arguments to the grab command. */
39 enum grab_flags {
40         /** Stop grabbing if audio file changes. */
41         GF_ONE_SHOT = 1,
42 };
43
44 /** Describes one active grab client. */
45 struct grab_client {
46         /* The value of the -p option. */
47         char *parent;
48         /* The value of the -n option. */
49         char *name;
50         /** The file descriptor to send the grabbed stream to. */
51         int fd;
52         /** See \ref grab_mode. */
53         enum grab_mode mode;
54         /** Flags given at the command line. */
55         enum grab_flags flags;
56         /** The point of the grab client's node in the buffer tree. */
57         struct btr_node *btrn;
58         /* The task of this grab client. */
59         struct task task;
60         /** Belongs to either the active or the inactive list. */
61         struct list_head node;
62 };
63
64 /* Grab clients that are attached to a btr node. */
65 INITIALIZED_LIST_HEAD(active_grab_client_list);
66 /* Grab clients that are not currently attached any btr node. */
67 INITIALIZED_LIST_HEAD(inactive_grab_client_list);
68
69 static int gc_write(struct grab_client *gc, char *buf, size_t len)
70 {
71         int ret = write_ok(gc->fd);
72
73         if (ret < 0)
74                 goto err;
75         if (ret == 0) { /* fd not ready */
76                 if (gc->mode == GM_PEDANTIC)
77                         goto err;
78                 if (gc->mode == GM_SLOPPY)
79                         return len;
80         }
81         ret = write_nonblock(gc->fd, buf, len, 0);
82         if (ret < 0)
83                 goto err;
84         if (ret > 0)
85                 return ret;
86         if (ret == 0) {
87                 if (gc->mode == GM_PEDANTIC)
88                         goto err;
89                 if (gc->mode == GM_SLOPPY)
90                         return len;
91         }
92         return 0;
93 err:
94         return -E_GC_WRITE;
95 }
96
97 static void gc_pre_select(struct sched *s, struct task *t)
98 {
99         struct grab_client *gc = container_of(t, struct grab_client, task);
100         int ret = btr_node_status(gc->btrn, 0, BTR_NT_LEAF);
101
102         if (ret == 0)
103                 return;
104         if (ret < 0) {
105                 s->timeout.tv_sec = 0;
106                 s->timeout.tv_usec = 0;
107                 return;
108         }
109         para_fd_set(gc->fd, &s->wfds, &s->max_fileno);
110 }
111
112 /*
113  * We need this forward declaration as post_select() needs
114  * activate_grab_client and vice versa.
115  */
116 static void gc_post_select(struct sched *s, struct task *t);
117
118 /**
119  * Move a grab client to the active list and start it.
120  *
121  * \param gc The grab client to activate.
122  */
123 static void gc_activate(struct grab_client *gc)
124 {
125         struct btr_node *root = audiod_get_btr_root(), *parent;
126         char *name = gc->name? gc->name : "grab";
127
128         if (!root)
129                 return;
130         parent = btr_search_node(gc->parent, root);
131         if (!parent)
132                 return;
133         PARA_INFO_LOG("activating fd %d\n", gc->fd);
134         list_move(&gc->node, &active_grab_client_list);
135         gc->btrn = btr_new_node(name, parent, NULL, NULL);
136         if (!gc->task.pre_select) {
137                 gc->task.pre_select = gc_pre_select;
138                 gc->task.post_select = gc_post_select;
139                 snprintf(gc->task.status, sizeof(gc->task.status) - 1, "%s", name);
140                 gc->task.status[sizeof(gc->task.status) - 1] = '\0';
141                 register_task(&gc->task);
142         }
143 }
144
145 /**
146  * Activate inactive grab clients if possible.
147  *
148  * This is called from audiod.c when the current audio file changes. It loops
149  * over all inactive grab clients and checks each grab client's configuration
150  * to determine if the client in question wishes to grab the new stream.  If
151  * yes, this grab client is moved from the inactive to the active grab client list.
152  *
153  * This function also garbage collects all grab clients whose tasks have been
154  * unscheduled.
155  */
156 void activate_grab_clients(void)
157 {
158         struct grab_client *gc, *tmp;
159
160         list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
161                 if (gc->task.error == -E_TASK_UNREGISTERED) {
162                         list_del(&gc->node);
163                         free(gc);
164                         continue;
165                 }
166                 gc_activate(gc);
167         }
168 }
169
170 static int gc_close(struct grab_client *gc, int err)
171 {
172         btr_remove_node(gc->btrn);
173         btr_free_node(gc->btrn);
174         gc->btrn = NULL;
175         PARA_INFO_LOG("closing gc: %s\n", para_strerror(-err));
176         list_move(&gc->node, &inactive_grab_client_list);
177         if (err == -E_GC_WRITE || (gc->flags & GF_ONE_SHOT)) {
178                 /*
179                  * We must not free the gc structure here as it contains ->task
180                  * which is still used because this function is called from
181                  * post_select().
182                  */
183                 close(gc->fd);
184                 free(gc->parent);
185                 free(gc->name);
186                 return 1;
187         }
188         gc_activate(gc);
189         return 0;
190 }
191
192 static void gc_post_select(__a_unused struct sched *s, struct task *t)
193 {
194         struct grab_client *gc = container_of(t, struct grab_client, task);
195         struct btr_node *btrn = gc->btrn;
196         int ret;
197         size_t sz;
198         char *buf;
199
200         t->error = 0;
201         ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
202         if (ret == 0)
203                 return;
204         if (ret < 0)
205                 goto err;
206         sz = btr_next_buffer(btrn, &buf);
207         assert(sz != 0);
208         ret = gc_write(gc, buf, sz);
209         if (ret < 0)
210                 goto err;
211         if (ret > 0)
212                 btr_consume(btrn, ret);
213         return;
214 err:
215         t->error = gc_close(gc, ret)? ret : 0;
216 }
217
218 static int gc_check_args(int argc, char **argv, struct grab_client *gc)
219 {
220         int i;
221
222         for (i = 1; i < argc; i++) {
223                 const char *arg = argv[i];
224                 if (arg[0] != '-')
225                         break;
226                 if (!strcmp(arg, "--")) {
227                         i++;
228                         break;
229                 }
230                 if (!strncmp(arg, "-m", 2)) {
231                         if (*(arg + 3))
232                                 return -E_GC_SYNTAX;
233                         switch(*(arg + 2)) {
234                         case 's':
235                                 gc->mode = GM_SLOPPY;
236                                 continue;
237                         case 'a':
238                                 gc->mode = GM_AGGRESSIVE;
239                                 continue;
240                         case 'p':
241                                 gc->mode = GM_PEDANTIC;
242                                 continue;
243                         default:
244                                 return -E_GC_SYNTAX;
245                         }
246                 }
247                 if (!strcmp(arg, "-o")) {
248                         gc->flags |= GF_ONE_SHOT;
249                         continue;
250                 }
251                 if (!strncmp(arg, "-p=", 3)) {
252                         gc->parent = para_strdup(arg + 3);
253                         continue;
254                 }
255                 if (!strncmp(arg, "-n=", 3)) {
256                         gc->name = para_strdup(arg + 3);
257                         continue;
258                 }
259                 return -E_GC_SYNTAX;
260         }
261         if (i != argc)
262                 return -E_GC_SYNTAX;
263         return 1;
264 }
265
266 /**
267  * Check the command line options and allocate a grab_client structure.
268  *
269  * \param fd The file descriptor of the client.
270  * \param argc Argument count.
271  * \param argv Argument vector.
272  *
273  * If the command line options given by \a argc and \a argv are valid.
274  * allocate a struct grab_client and initialize it with this valid
275  * configuration.
276  *
277  * If the new grab client can be added to an existing buffer tree, activate it.
278  * Otherwise, add it to the inactive list for later activation.
279  *
280  * \return Standard.
281  */
282 int grab_client_new(int fd, int argc, char **argv)
283 {
284         int ret;
285         struct grab_client *gc = para_calloc(sizeof(struct grab_client));
286
287         ret = gc_check_args(argc, argv, gc);
288         if (ret < 0)
289                 goto err_out;
290         gc->fd = fd;
291         para_list_add(&gc->node, &inactive_grab_client_list);
292         gc_activate(gc);
293         return 1;
294 err_out:
295         free(gc);
296         return ret;
297 }