Merge branch 't/interactive'
[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 static INITIALIZED_LIST_HEAD(active_grab_client_list);
65 /* Grab clients that are not currently attached any btr node. */
66 static 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, struct sched *s)
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 gc->task.pre_select = gc_pre_select;
134 gc->task.post_select = gc_post_select;
135 snprintf(gc->task.status, sizeof(gc->task.status) - 1, "%s", name);
136 gc->task.status[sizeof(gc->task.status) - 1] = '\0';
137 gc->task.error = 0;
138 register_task(s, &gc->task);
139 }
140
141 /**
142 * Activate inactive grab clients if possible.
143 *
144 * This is called from audiod.c when the current audio file changes. It loops
145 * over all inactive grab clients and checks each grab client's configuration
146 * to determine if the client in question wishes to grab the new stream. If
147 * yes, this grab client is moved from the inactive to the active grab client list.
148 *
149 * This function also garbage collects all grab clients whose tasks have been
150 * unscheduled.
151 */
152 void activate_grab_clients(struct sched *s)
153 {
154 struct grab_client *gc, *tmp;
155
156 list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
157 if (gc->fd < 0) {
158 list_del(&gc->node);
159 free(gc);
160 continue;
161 }
162 gc_activate(gc, s);
163 }
164 }
165
166 static int gc_close(struct grab_client *gc, int err)
167 {
168 btr_remove_node(gc->btrn);
169 btr_free_node(gc->btrn);
170 gc->btrn = NULL;
171 PARA_INFO_LOG("closing gc: %s\n", para_strerror(-err));
172 list_move(&gc->node, &inactive_grab_client_list);
173 if (err == -E_GC_WRITE || (gc->flags & GF_ONE_SHOT)) {
174 /*
175 * We must not free the gc structure here as it contains ->task
176 * which is still used because this function is called from
177 * post_select().
178 */
179 close(gc->fd);
180 gc->fd = -1;
181 free(gc->parent);
182 free(gc->name);
183 return 1;
184 }
185 return 0;
186 }
187
188 static void gc_post_select(__a_unused struct sched *s, struct task *t)
189 {
190 struct grab_client *gc = container_of(t, struct grab_client, task);
191 struct btr_node *btrn = gc->btrn;
192 int ret;
193 size_t sz;
194 char *buf;
195
196 t->error = 0;
197 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
198 if (ret == 0)
199 return;
200 if (ret < 0)
201 goto err;
202 sz = btr_next_buffer(btrn, &buf);
203 assert(sz != 0);
204 ret = gc_write(gc, buf, sz);
205 if (ret < 0)
206 goto err;
207 if (ret > 0)
208 btr_consume(btrn, ret);
209 return;
210 err:
211 gc_close(gc, ret);
212 t->error = ret;
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 * \param s The scheduler to register the grab client task to.
270 *
271 * If the command line options given by \a argc and \a argv are valid.
272 * allocate a struct grab_client and initialize it with this valid
273 * configuration.
274 *
275 * If the new grab client can be added to an existing buffer tree, activate it.
276 * Otherwise, add it to the inactive list for later activation.
277 *
278 * \return Standard.
279 */
280 int grab_client_new(int fd, int argc, char **argv, struct sched *s)
281 {
282 int ret;
283 struct grab_client *gc = para_calloc(sizeof(struct grab_client));
284
285 ret = gc_check_args(argc, argv, gc);
286 if (ret < 0)
287 goto err_out;
288 gc->fd = fd;
289 para_list_add(&gc->node, &inactive_grab_client_list);
290 gc_activate(gc, s);
291 return 1;
292 err_out:
293 free(gc);
294 return ret;
295 }