Merge branch 'refs/heads/t/si_update'
[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 #include <lopsub.h>
12
13 #include "audiod_cmd.lsg.h"
14
15 #include "para.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "buffer_tree.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 = xwrite(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, void *context)
97 {
98 struct grab_client *gc = context;
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 int gc_post_select(struct sched *s, void *context);
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
134 gc->task = task_register(&(struct task_info) {
135 .name = name,
136 .pre_select = gc_pre_select,
137 .post_select = gc_post_select,
138 .context = gc,
139 }, s);
140 }
141
142 /**
143 * Activate inactive grab clients if possible.
144 *
145 * \param s Needed to schedule the grab client task.
146 *
147 * This is called from audiod.c when the current audio file changes. It loops
148 * over all inactive grab clients and checks each grab client's configuration
149 * to determine if the client in question wishes to grab the new stream. If
150 * yes, this grab client is moved from the inactive to the active grab client list.
151 *
152 * This function also garbage collects all grab clients whose tasks have been
153 * unscheduled.
154 */
155 void activate_grab_clients(struct sched *s)
156 {
157 struct grab_client *gc, *tmp;
158
159 list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
160 if (gc->fd < 0) {
161 list_del(&gc->node);
162 free(gc);
163 continue;
164 }
165 gc_activate(gc, s);
166 }
167 }
168
169 static int gc_close(struct grab_client *gc, int err)
170 {
171 btr_remove_node(&gc->btrn);
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 int gc_post_select(__a_unused struct sched *s, void *context)
190 {
191 struct grab_client *gc = context;
192 struct btr_node *btrn = gc->btrn;
193 int ret;
194 size_t sz;
195 char *buf;
196
197 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
198 if (ret == 0)
199 return 0;
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 0;
210 err:
211 gc_close(gc, ret);
212 return ret;
213 }
214
215 static int gc_check_args(struct lls_parse_result *lpr, struct grab_client *gc)
216 {
217 const struct lls_opt_result *r;
218
219 r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_MODE, lpr);
220 if (lls_opt_given(r) > 0) {
221 const char *arg = lls_string_val(0, r);
222 if (strcmp(arg, "s") == 0)
223 gc->mode = GM_SLOPPY;
224 else if (strcmp(arg, "a") == 0)
225 gc->mode = GM_AGGRESSIVE;
226 else if (strcmp(arg, "p") == 0)
227 gc->mode = GM_PEDANTIC;
228 else
229 return -E_GC_SYNTAX;
230 }
231
232 r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_ONE_SHOT, lpr);
233 if (lls_opt_given(r) > 0)
234 gc->flags |= GF_ONE_SHOT;
235
236 r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_PARENT, lpr);
237 if (lls_opt_given(r) > 0) {
238 const char *arg = lls_string_val(0, r);
239 gc->parent = para_strdup(arg);
240 }
241
242 r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_NAME, lpr);
243 if (lls_opt_given(r) > 0) {
244 const char *arg = lls_string_val(0, r);
245 gc->name = para_strdup(arg);
246 }
247 return 1;
248 }
249
250 /**
251 * Create and activate a grab client.
252 *
253 * \param fd The file descriptor of the client.
254 * \param lpr The parsed command line of the grab command.
255 * \param s The scheduler to register the grab client task to.
256 *
257 * This function semantically parses the arguments given as options to the grab
258 * command. On success it allocates a struct grab_client, associates it with
259 * the given file descriptor and activates it. If the new grab client can not
260 * be attached to an existing buffer tree node it is put into the inactive list
261 * for later activation.
262 *
263 * \return Standard.
264 */
265 int grab_client_new(int fd, struct lls_parse_result *lpr, struct sched *s)
266 {
267 int ret;
268 struct grab_client *gc = para_calloc(sizeof(struct grab_client));
269
270 ret = gc_check_args(lpr, gc);
271 if (ret < 0)
272 goto err_out;
273 ret = dup(fd);
274 if (ret < 0) {
275 ret = -ERRNO_TO_PARA_ERROR(errno);
276 goto err_out;
277 }
278 gc->fd = ret;
279 para_list_add(&gc->node, &inactive_grab_client_list);
280 gc_activate(gc, s);
281 return 1;
282 err_out:
283 free(gc);
284 return ret;
285 }