ao: Improve help text.
[paraslash.git] / grab_client.c
1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file grab_client.c Functions for grabbing the audio stream. */
4
5 #include <regex.h>
6 #include <sys/types.h>
7 #include <lopsub.h>
8
9 #include "audiod_cmd.lsg.h"
10
11 #include "para.h"
12 #include "list.h"
13 #include "sched.h"
14 #include "buffer_tree.h"
15 #include "grab_client.h"
16 #include "audiod.h"
17 #include "error.h"
18 #include "string.h"
19 #include "fd.h"
20
21 /**
22  * How to handle blocking writes for the grab client fds.
23  */
24 enum grab_mode {
25         /** Ignore the data and do not write. */
26         GM_SLOPPY,
27         /** Write anyway (default). */
28         GM_AGGRESSIVE,
29         /** Close fd if write would block. */
30         GM_PEDANTIC,
31 };
32
33 /** Flags specified as arguments to the grab command. */
34 enum grab_flags {
35         /** Stop grabbing if audio file changes. */
36         GF_ONE_SHOT = 1,
37 };
38
39 /** Describes one active grab client. */
40 struct grab_client {
41         /* The value of the -p option. */
42         char *parent;
43         /* The value of the -n option. */
44         char *name;
45         /** The file descriptor to send the grabbed stream to. */
46         int fd;
47         /** See \ref grab_mode. */
48         enum grab_mode mode;
49         /** Flags given at the command line. */
50         enum grab_flags flags;
51         /** The point of the grab client's node in the buffer tree. */
52         struct btr_node *btrn;
53         /* The task of this grab client. */
54         struct task *task;
55         /** Belongs to either the active or the inactive list. */
56         struct list_head node;
57 };
58
59 /* Grab clients that are attached to a btr node. */
60 static INITIALIZED_LIST_HEAD(active_grab_client_list);
61 /* Grab clients that are not currently attached any btr node. */
62 static INITIALIZED_LIST_HEAD(inactive_grab_client_list);
63
64 static int gc_write(struct grab_client *gc, char *buf, size_t len)
65 {
66         int ret = write_ok(gc->fd);
67
68         if (ret < 0)
69                 goto err;
70         if (ret == 0) { /* fd not ready */
71                 if (gc->mode == GM_PEDANTIC)
72                         goto err;
73                 if (gc->mode == GM_SLOPPY)
74                         return len;
75         }
76         ret = xwrite(gc->fd, buf, len);
77         if (ret < 0)
78                 goto err;
79         if (ret > 0)
80                 return ret;
81         if (ret == 0) {
82                 if (gc->mode == GM_PEDANTIC)
83                         goto err;
84                 if (gc->mode == GM_SLOPPY)
85                         return len;
86         }
87         return 0;
88 err:
89         return -E_GC_WRITE;
90 }
91
92 static void gc_pre_select(struct sched *s, void *context)
93 {
94         struct grab_client *gc = context;
95         int ret = btr_node_status(gc->btrn, 0, BTR_NT_LEAF);
96
97         if (ret == 0)
98                 return;
99         if (ret < 0)
100                 sched_min_delay(s);
101         para_fd_set(gc->fd, &s->wfds, &s->max_fileno);
102 }
103
104 /*
105  * We need this forward declaration as post_select() needs
106  * activate_grab_client and vice versa.
107  */
108 static int gc_post_select(struct sched *s, void *context);
109
110 /**
111  * Move a grab client to the active list and start it.
112  *
113  * \param gc The grab client to activate.
114  */
115 static void gc_activate(struct grab_client *gc, struct sched *s)
116 {
117         struct btr_node *root = audiod_get_btr_root(), *parent;
118         char *name = gc->name? gc->name : "grab";
119
120         if (!root)
121                 return;
122         parent = btr_search_node(gc->parent, root);
123         if (!parent)
124                 return;
125         PARA_INFO_LOG("activating fd %d\n", gc->fd);
126         list_move(&gc->node, &active_grab_client_list);
127         gc->btrn = btr_new_node(&(struct btr_node_description)
128                 EMBRACE(.name = name, .parent = parent));
129
130         gc->task = task_register(&(struct task_info) {
131                 .name = name,
132                 .pre_select = gc_pre_select,
133                 .post_select = gc_post_select,
134                 .context = gc,
135         }, s);
136 }
137
138 /**
139  * Activate inactive grab clients if possible.
140  *
141  * \param s Needed to schedule the grab client task.
142  *
143  * This is called from audiod.c when the current audio file changes. It loops
144  * over all inactive grab clients and checks each grab client's configuration
145  * to determine if the client in question wishes to grab the new stream.  If
146  * yes, this grab client is moved from the inactive to the active grab client list.
147  *
148  * This function also garbage collects all grab clients whose tasks have been
149  * unscheduled.
150  */
151 void activate_grab_clients(struct sched *s)
152 {
153         struct grab_client *gc, *tmp;
154
155         list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
156                 if (gc->fd < 0) {
157                         list_del(&gc->node);
158                         free(gc);
159                         continue;
160                 }
161                 gc_activate(gc, s);
162         }
163 }
164
165 static int gc_close(struct grab_client *gc, int err)
166 {
167         btr_remove_node(&gc->btrn);
168         PARA_INFO_LOG("closing gc: %s\n", para_strerror(-err));
169         list_move(&gc->node, &inactive_grab_client_list);
170         if (err == -E_GC_WRITE || (gc->flags & GF_ONE_SHOT)) {
171                 /*
172                  * We must not free the gc structure here as it contains ->task
173                  * which is still used because this function is called from
174                  * post_select().
175                  */
176                 close(gc->fd);
177                 gc->fd = -1;
178                 free(gc->parent);
179                 free(gc->name);
180                 return 1;
181         }
182         return 0;
183 }
184
185 static int gc_post_select(__a_unused struct sched *s, void *context)
186 {
187         struct grab_client *gc = context;
188         struct btr_node *btrn = gc->btrn;
189         int ret;
190         size_t sz;
191         char *buf;
192
193         ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
194         if (ret == 0)
195                 return 0;
196         if (ret < 0)
197                 goto err;
198         sz = btr_next_buffer(btrn, &buf);
199         assert(sz != 0);
200         ret = gc_write(gc, buf, sz);
201         if (ret < 0)
202                 goto err;
203         if (ret > 0)
204                 btr_consume(btrn, ret);
205         return 0;
206 err:
207         gc_close(gc, ret);
208         return ret;
209 }
210
211 static int gc_check_args(struct lls_parse_result *lpr, struct grab_client *gc)
212 {
213         const struct lls_opt_result *r;
214
215         r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_MODE, lpr);
216         if (lls_opt_given(r) > 0) {
217                 const char *arg = lls_string_val(0, r);
218                 if (strcmp(arg, "s") == 0)
219                         gc->mode = GM_SLOPPY;
220                 else if (strcmp(arg, "a") == 0)
221                         gc->mode = GM_AGGRESSIVE;
222                 else if (strcmp(arg, "p") == 0)
223                         gc->mode = GM_PEDANTIC;
224                 else
225                         return -E_GC_SYNTAX;
226         }
227
228         r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_ONE_SHOT, lpr);
229         if (lls_opt_given(r) > 0)
230                 gc->flags |= GF_ONE_SHOT;
231
232         r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_PARENT, lpr);
233         if (lls_opt_given(r) > 0) {
234                 const char *arg = lls_string_val(0, r);
235                 gc->parent = para_strdup(arg);
236         }
237
238         r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_NAME, lpr);
239         if (lls_opt_given(r) > 0) {
240                 const char *arg = lls_string_val(0, r);
241                 gc->name = para_strdup(arg);
242         }
243         return 1;
244 }
245
246 /**
247  * Create and activate a grab client.
248  *
249  * \param fd The file descriptor of the client.
250  * \param lpr The parsed command line of the grab command.
251  * \param s The scheduler to register the grab client task to.
252  *
253  * This function semantically parses the arguments given as options to the grab
254  * command. On success it allocates a struct grab_client, associates it with
255  * the given file descriptor and activates it. If the new grab client can not
256  * be attached to an existing buffer tree node it is put into the inactive list
257  * for later activation.
258  *
259  * \return Standard.
260  */
261 int grab_client_new(int fd, struct lls_parse_result *lpr, struct sched *s)
262 {
263         int ret;
264         struct grab_client *gc = para_calloc(sizeof(struct grab_client));
265
266         ret = gc_check_args(lpr, gc);
267         if (ret < 0)
268                 goto err_out;
269         ret = dup(fd);
270         if (ret < 0) {
271                 ret = -ERRNO_TO_PARA_ERROR(errno);
272                 goto err_out;
273         }
274         gc->fd = ret;
275         para_list_add(&gc->node, &inactive_grab_client_list);
276         gc_activate(gc, s);
277         return 1;
278 err_out:
279         free(gc);
280         return ret;
281 }