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