grab_client.c: Fix another documentation bug
[paraslash.git] / grab_client.c
1 /*
2 * Copyright (C) 2006-2012 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
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 = write_nonblock(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, 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 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 void gc_post_select(struct sched *s, struct task *t);
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 gc->task.pre_select = gc_pre_select;
133 gc->task.post_select = gc_post_select;
134 snprintf(gc->task.status, sizeof(gc->task.status) - 1, "%s", name);
135 gc->task.status[sizeof(gc->task.status) - 1] = '\0';
136 gc->task.error = 0;
137 register_task(s, &gc->task);
138 }
139
140 /**
141 * Activate inactive grab clients if possible.
142 *
143 * \param s Needed to schedule the grab client task.
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(struct sched *s)
154 {
155 struct grab_client *gc, *tmp;
156
157 list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
158 if (gc->fd < 0) {
159 list_del(&gc->node);
160 free(gc);
161 continue;
162 }
163 gc_activate(gc, s);
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 gc->fd = -1;
182 free(gc->parent);
183 free(gc->name);
184 return 1;
185 }
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 gc_close(gc, ret);
213 t->error = ret;
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 * \param s The scheduler to register the grab client task to.
271 *
272 * If the command line options given by \a argc and \a argv are valid.
273 * allocate a struct grab_client and initialize it with this valid
274 * configuration.
275 *
276 * If the new grab client can be added to an existing buffer tree, activate it.
277 * Otherwise, add it to the inactive list for later activation.
278 *
279 * \return Standard.
280 */
281 int grab_client_new(int fd, int argc, char **argv, struct sched *s)
282 {
283 int ret;
284 struct grab_client *gc = para_calloc(sizeof(struct grab_client));
285
286 ret = gc_check_args(argc, argv, gc);
287 if (ret < 0)
288 goto err_out;
289 gc->fd = fd;
290 para_list_add(&gc->node, &inactive_grab_client_list);
291 gc_activate(gc, s);
292 return 1;
293 err_out:
294 free(gc);
295 return ret;
296 }