Merge branch 't/color_cleanups'
[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 <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);
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 sched_min_delay(s);
106 para_fd_set(gc->fd, &s->wfds, &s->max_fileno);
107 }
108
109 /*
110 * We need this forward declaration as post_select() needs
111 * activate_grab_client and vice versa.
112 */
113 static void gc_post_select(struct sched *s, struct task *t);
114
115 /**
116 * Move a grab client to the active list and start it.
117 *
118 * \param gc The grab client to activate.
119 */
120 static void gc_activate(struct grab_client *gc)
121 {
122 struct btr_node *root = audiod_get_btr_root(), *parent;
123 char *name = gc->name? gc->name : "grab";
124
125 if (!root)
126 return;
127 parent = btr_search_node(gc->parent, root);
128 if (!parent)
129 return;
130 PARA_INFO_LOG("activating fd %d\n", gc->fd);
131 list_move(&gc->node, &active_grab_client_list);
132 gc->btrn = btr_new_node(&(struct btr_node_description)
133 EMBRACE(.name = name, .parent = parent));
134 if (!gc->task.pre_select) {
135 gc->task.pre_select = gc_pre_select;
136 gc->task.post_select = gc_post_select;
137 snprintf(gc->task.status, sizeof(gc->task.status) - 1, "%s", name);
138 gc->task.status[sizeof(gc->task.status) - 1] = '\0';
139 register_task(&gc->task);
140 }
141 }
142
143 /**
144 * Activate inactive grab clients if possible.
145 *
146 * This is called from audiod.c when the current audio file changes. It loops
147 * over all inactive grab clients and checks each grab client's configuration
148 * to determine if the client in question wishes to grab the new stream. If
149 * yes, this grab client is moved from the inactive to the active grab client list.
150 *
151 * This function also garbage collects all grab clients whose tasks have been
152 * unscheduled.
153 */
154 void activate_grab_clients(void)
155 {
156 struct grab_client *gc, *tmp;
157
158 list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
159 if (gc->task.error == -E_TASK_UNREGISTERED) {
160 list_del(&gc->node);
161 free(gc);
162 continue;
163 }
164 gc_activate(gc);
165 }
166 }
167
168 static int gc_close(struct grab_client *gc, int err)
169 {
170 btr_remove_node(gc->btrn);
171 btr_free_node(gc->btrn);
172 gc->btrn = NULL;
173 PARA_INFO_LOG("closing gc: %s\n", para_strerror(-err));
174 list_move(&gc->node, &inactive_grab_client_list);
175 if (err == -E_GC_WRITE || (gc->flags & GF_ONE_SHOT)) {
176 /*
177 * We must not free the gc structure here as it contains ->task
178 * which is still used because this function is called from
179 * post_select().
180 */
181 close(gc->fd);
182 free(gc->parent);
183 free(gc->name);
184 return 1;
185 }
186 gc_activate(gc);
187 return 0;
188 }
189
190 static void gc_post_select(__a_unused struct sched *s, struct task *t)
191 {
192 struct grab_client *gc = container_of(t, struct grab_client, task);
193 struct btr_node *btrn = gc->btrn;
194 int ret;
195 size_t sz;
196 char *buf;
197
198 t->error = 0;
199 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
200 if (ret == 0)
201 return;
202 if (ret < 0)
203 goto err;
204 sz = btr_next_buffer(btrn, &buf);
205 assert(sz != 0);
206 ret = gc_write(gc, buf, sz);
207 if (ret < 0)
208 goto err;
209 if (ret > 0)
210 btr_consume(btrn, ret);
211 return;
212 err:
213 t->error = gc_close(gc, ret)? ret : 0;
214 }
215
216 static int gc_check_args(int argc, char **argv, struct grab_client *gc)
217 {
218 int i;
219
220 for (i = 1; i < argc; i++) {
221 const char *arg = argv[i];
222 if (arg[0] != '-')
223 break;
224 if (!strcmp(arg, "--")) {
225 i++;
226 break;
227 }
228 if (!strncmp(arg, "-m", 2)) {
229 if (*(arg + 3))
230 return -E_GC_SYNTAX;
231 switch(*(arg + 2)) {
232 case 's':
233 gc->mode = GM_SLOPPY;
234 continue;
235 case 'a':
236 gc->mode = GM_AGGRESSIVE;
237 continue;
238 case 'p':
239 gc->mode = GM_PEDANTIC;
240 continue;
241 default:
242 return -E_GC_SYNTAX;
243 }
244 }
245 if (!strcmp(arg, "-o")) {
246 gc->flags |= GF_ONE_SHOT;
247 continue;
248 }
249 if (!strncmp(arg, "-p=", 3)) {
250 gc->parent = para_strdup(arg + 3);
251 continue;
252 }
253 if (!strncmp(arg, "-n=", 3)) {
254 gc->name = para_strdup(arg + 3);
255 continue;
256 }
257 return -E_GC_SYNTAX;
258 }
259 if (i != argc)
260 return -E_GC_SYNTAX;
261 return 1;
262 }
263
264 /**
265 * Check the command line options and allocate a grab_client structure.
266 *
267 * \param fd The file descriptor of the client.
268 * \param argc Argument count.
269 * \param argv Argument vector.
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)
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);
291 return 1;
292 err_out:
293 free(gc);
294 return ret;
295 }