Major grab_client cleanups.
[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 file descriptor to send the grabbed stream to. */
49 int fd;
50 /** See \ref grab_mode. */
51 enum grab_mode mode;
52 /** Flags given at the command line. */
53 enum grab_flags flags;
54 /** The point of the grab client's node in the buffer tree. */
55 struct btr_node *btrn;
56 /* The task of this grab client. */
57 struct task task;
58 /** Belongs to either the active or the inactive list. */
59 struct list_head node;
60 };
61
62 /* Grab clients that are attached to a btr node. */
63 INITIALIZED_LIST_HEAD(active_grab_client_list);
64 /* Grab clients that are not currently attached any btr node. */
65 INITIALIZED_LIST_HEAD(inactive_grab_client_list);
66
67 static int gc_write(struct grab_client *gc, char *buf, size_t len)
68 {
69 int ret = write_ok(gc->fd);
70
71 if (ret < 0)
72 goto err;
73 if (ret == 0) { /* fd not ready */
74 if (gc->mode == GM_PEDANTIC)
75 goto err;
76 if (gc->mode == GM_SLOPPY)
77 return len;
78 }
79 ret = write_nonblock(gc->fd, buf, len, 0);
80 if (ret < 0)
81 goto err;
82 if (ret > 0)
83 return ret;
84 if (ret == 0) {
85 if (gc->mode == GM_PEDANTIC)
86 goto err;
87 if (gc->mode == GM_SLOPPY)
88 return len;
89 }
90 return 0;
91 err:
92 return -E_GC_WRITE;
93 }
94
95 static void gc_pre_select(struct sched *s, struct task *t)
96 {
97 struct grab_client *gc = container_of(t, struct grab_client, task);
98 int ret = btr_node_status(gc->btrn, 0, BTR_NT_LEAF);
99
100 if (ret == 0)
101 return;
102 if (ret < 0) {
103 s->timeout.tv_sec = 0;
104 s->timeout.tv_usec = 0;
105 return;
106 }
107 para_fd_set(gc->fd, &s->wfds, &s->max_fileno);
108 }
109
110 /*
111 * We need this forward declaration as post_select() needs
112 * activate_grab_client and vice versa.
113 */
114 static void gc_post_select(struct sched *s, struct task *t);
115
116 /**
117 * Move a grab client to the active list and start it.
118 *
119 * \param gc The grab client to activate.
120 */
121 static void gc_activate(struct grab_client *gc)
122 {
123 struct btr_node *root = audiod_get_btr_root(), *parent;
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("grab", parent, NULL, NULL);
133 if (!gc->task.pre_select) {
134 gc->task.pre_select = gc_pre_select;
135 gc->task.post_select = gc_post_select;
136 sprintf(gc->task.status, "grab");
137 register_task(&gc->task);
138 }
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(void)
153 {
154 struct grab_client *gc, *tmp;
155
156 list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
157 if (gc->task.error == -E_TASK_UNREGISTERED) {
158 list_del(&gc->node);
159 free(gc);
160 continue;
161 }
162 gc_activate(gc);
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 free(gc->parent);
181 return 1;
182 }
183 gc_activate(gc);
184 return 0;
185 }
186
187 static void gc_post_select(__a_unused struct sched *s, struct task *t)
188 {
189 struct grab_client *gc = container_of(t, struct grab_client, task);
190 struct btr_node *btrn = gc->btrn;
191 int ret;
192 size_t sz;
193 char *buf;
194
195 t->error = 0;
196 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
197 if (ret == 0)
198 return;
199 if (ret < 0)
200 goto err;
201 sz = btr_next_buffer(btrn, &buf);
202 assert(sz != 0);
203 ret = gc_write(gc, buf, sz);
204 if (ret < 0)
205 goto err;
206 if (ret > 0)
207 btr_consume(btrn, ret);
208 return;
209 err:
210 t->error = gc_close(gc, ret)? ret : 0;
211 }
212
213 static int gc_check_args(int argc, char **argv, struct grab_client *gc)
214 {
215 int i;
216
217 for (i = 1; i < argc; i++) {
218 const char *arg = argv[i];
219 if (arg[0] != '-')
220 break;
221 if (!strcmp(arg, "--")) {
222 i++;
223 break;
224 }
225 if (!strncmp(arg, "-m", 2)) {
226 if (*(arg + 3))
227 return -E_GC_SYNTAX;
228 switch(*(arg + 2)) {
229 case 's':
230 gc->mode = GM_SLOPPY;
231 continue;
232 case 'a':
233 gc->mode = GM_AGGRESSIVE;
234 continue;
235 case 'p':
236 gc->mode = GM_PEDANTIC;
237 continue;
238 default:
239 return -E_GC_SYNTAX;
240 }
241 }
242 if (!strcmp(arg, "-o")) {
243 gc->flags |= GF_ONE_SHOT;
244 continue;
245 }
246 if (!strncmp(arg, "-p=", 3)) {
247 gc->parent = para_strdup(arg + 3);
248 continue;
249 }
250 return -E_GC_SYNTAX;
251 }
252 if (i != argc)
253 return -E_GC_SYNTAX;
254 return 1;
255 }
256
257 /**
258 * Check the command line options and allocate a grab_client structure.
259 *
260 * \param fd The file descriptor of the client.
261 * \param argc Argument count.
262 * \param argv Argument vector.
263 *
264 * If the command line options given by \a argc and \a argv are valid.
265 * allocate a struct grab_client and initialize it with this valid
266 * configuration.
267 *
268 * If the new grab client can be added to an existing buffer tree, activate it.
269 * Otherwise, add it to the inactive list for later activation.
270 *
271 * \return Standard.
272 */
273 int grab_client_new(int fd, int argc, char **argv)
274 {
275 int ret;
276 struct grab_client *gc = para_calloc(sizeof(struct grab_client));
277
278 ret = gc_check_args(argc, argv, gc);
279 if (ret < 0)
280 goto err_out;
281 gc->fd = fd;
282 para_list_add(&gc->node, &inactive_grab_client_list);
283 gc_activate(gc);
284 return 1;
285 err_out:
286 free(gc);
287 return ret;
288 }