Merge branch 'maint'
[paraslash.git] / grab_client.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
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
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "buffer_tree.h"
17 #include "filter.h"
18 #include "grab_client.h"
19 #include "audiod.h"
20 #include "error.h"
21 #include "string.h"
22 #include "fd.h"
23
24 /**
25 * How to handle blocking writes for the grab client fds.
26 */
27 enum grab_mode {
28 /** Ignore the data and do not write. */
29 GM_SLOPPY,
30 /** Write anyway (default). */
31 GM_AGGRESSIVE,
32 /** Close fd if write would block. */
33 GM_PEDANTIC,
34 };
35
36 /** Flags specified as arguments to the grab command. */
37 enum grab_flags {
38 /** Stop grabbing if audio file changes. */
39 GF_ONE_SHOT = 1,
40 };
41
42 /** Describes one active grab client. */
43 struct grab_client {
44 /* The value of the -p option. */
45 char *parent;
46 /* The value of the -n option. */
47 char *name;
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 static INITIALIZED_LIST_HEAD(active_grab_client_list);
64 /* Grab clients that are not currently attached any btr node. */
65 static 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 = xwrite(gc->fd, buf, len);
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, void *context)
96 {
97 struct grab_client *gc = context;
98 int ret = btr_node_status(gc->btrn, 0, BTR_NT_LEAF);
99
100 if (ret == 0)
101 return;
102 if (ret < 0)
103 sched_min_delay(s);
104 para_fd_set(gc->fd, &s->wfds, &s->max_fileno);
105 }
106
107 /*
108 * We need this forward declaration as post_select() needs
109 * activate_grab_client and vice versa.
110 */
111 static int gc_post_select(struct sched *s, void *context);
112
113 /**
114 * Move a grab client to the active list and start it.
115 *
116 * \param gc The grab client to activate.
117 */
118 static void gc_activate(struct grab_client *gc, struct sched *s)
119 {
120 struct btr_node *root = audiod_get_btr_root(), *parent;
121 char *name = gc->name? gc->name : "grab";
122
123 if (!root)
124 return;
125 parent = btr_search_node(gc->parent, root);
126 if (!parent)
127 return;
128 PARA_INFO_LOG("activating fd %d\n", gc->fd);
129 list_move(&gc->node, &active_grab_client_list);
130 gc->btrn = btr_new_node(&(struct btr_node_description)
131 EMBRACE(.name = name, .parent = parent));
132
133 gc->task = task_register(&(struct task_info) {
134 .name = name,
135 .pre_select = gc_pre_select,
136 .post_select = gc_post_select,
137 .context = gc,
138 }, s);
139 }
140
141 /**
142 * Activate inactive grab clients if possible.
143 *
144 * \param s Needed to schedule the grab client task.
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(struct sched *s)
155 {
156 struct grab_client *gc, *tmp;
157
158 list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
159 if (gc->fd < 0) {
160 list_del(&gc->node);
161 free(gc);
162 continue;
163 }
164 gc_activate(gc, s);
165 }
166 }
167
168 static int gc_close(struct grab_client *gc, int err)
169 {
170 btr_remove_node(&gc->btrn);
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 int gc_post_select(__a_unused struct sched *s, void *context)
189 {
190 struct grab_client *gc = context;
191 struct btr_node *btrn = gc->btrn;
192 int ret;
193 size_t sz;
194 char *buf;
195
196 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
197 if (ret == 0)
198 return 0;
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 0;
209 err:
210 gc_close(gc, ret);
211 return ret;
212 }
213
214 static int gc_check_args(int argc, char **argv, struct grab_client *gc)
215 {
216 int i;
217
218 for (i = 1; i < argc; i++) {
219 const char *arg = argv[i];
220 if (arg[0] != '-')
221 break;
222 if (!strcmp(arg, "--")) {
223 i++;
224 break;
225 }
226 if (!strncmp(arg, "-m", 2)) {
227 if (*(arg + 3))
228 return -E_GC_SYNTAX;
229 switch(*(arg + 2)) {
230 case 's':
231 gc->mode = GM_SLOPPY;
232 continue;
233 case 'a':
234 gc->mode = GM_AGGRESSIVE;
235 continue;
236 case 'p':
237 gc->mode = GM_PEDANTIC;
238 continue;
239 default:
240 return -E_GC_SYNTAX;
241 }
242 }
243 if (!strcmp(arg, "-o")) {
244 gc->flags |= GF_ONE_SHOT;
245 continue;
246 }
247 if (!strncmp(arg, "-p=", 3)) {
248 gc->parent = para_strdup(arg + 3);
249 continue;
250 }
251 if (!strncmp(arg, "-n=", 3)) {
252 gc->name = para_strdup(arg + 3);
253 continue;
254 }
255 return -E_GC_SYNTAX;
256 }
257 if (i != argc)
258 return -E_GC_SYNTAX;
259 return 1;
260 }
261
262 /**
263 * Check the command line options and allocate a grab_client structure.
264 *
265 * \param fd The file descriptor of the client.
266 * \param argc Argument count.
267 * \param argv Argument vector.
268 * \param s The scheduler to register the grab client task to.
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, struct sched *s)
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 ret = dup(fd);
288 if (ret < 0) {
289 ret = -ERRNO_TO_PARA_ERROR(errno);
290 goto err_out;
291 }
292 gc->fd = ret;
293 para_list_add(&gc->node, &inactive_grab_client_list);
294 gc_activate(gc, s);
295 return 1;
296 err_out:
297 free(gc);
298 return ret;
299 }