2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file grab_client.c Functions for grabbing the audio stream. */
10 #include <sys/types.h>
13 #include "audiod_cmd.lsg.h"
18 #include "buffer_tree.h"
19 #include "grab_client.h"
26 * How to handle blocking writes for the grab client fds.
29 /** Ignore the data and do not write. */
31 /** Write anyway (default). */
33 /** Close fd if write would block. */
37 /** Flags specified as arguments to the grab command. */
39 /** Stop grabbing if audio file changes. */
43 /** Describes one active grab client. */
45 /* The value of the -p option. */
47 /* The value of the -n option. */
49 /** The file descriptor to send the grabbed stream to. */
51 /** See \ref grab_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. */
59 /** Belongs to either the active or the inactive list. */
60 struct list_head node;
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);
68 static int gc_write(struct grab_client *gc, char *buf, size_t len)
70 int ret = write_ok(gc->fd);
74 if (ret == 0) { /* fd not ready */
75 if (gc->mode == GM_PEDANTIC)
77 if (gc->mode == GM_SLOPPY)
80 ret = xwrite(gc->fd, buf, len);
86 if (gc->mode == GM_PEDANTIC)
88 if (gc->mode == GM_SLOPPY)
96 static void gc_pre_select(struct sched *s, void *context)
98 struct grab_client *gc = context;
99 int ret = btr_node_status(gc->btrn, 0, BTR_NT_LEAF);
105 para_fd_set(gc->fd, &s->wfds, &s->max_fileno);
109 * We need this forward declaration as post_select() needs
110 * activate_grab_client and vice versa.
112 static int gc_post_select(struct sched *s, void *context);
115 * Move a grab client to the active list and start it.
117 * \param gc The grab client to activate.
119 static void gc_activate(struct grab_client *gc, struct sched *s)
121 struct btr_node *root = audiod_get_btr_root(), *parent;
122 char *name = gc->name? gc->name : "grab";
126 parent = btr_search_node(gc->parent, root);
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));
134 gc->task = task_register(&(struct task_info) {
136 .pre_select = gc_pre_select,
137 .post_select = gc_post_select,
143 * Activate inactive grab clients if possible.
145 * \param s Needed to schedule the grab client task.
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.
152 * This function also garbage collects all grab clients whose tasks have been
155 void activate_grab_clients(struct sched *s)
157 struct grab_client *gc, *tmp;
159 list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
169 static int gc_close(struct grab_client *gc, int err)
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)) {
176 * We must not free the gc structure here as it contains ->task
177 * which is still used because this function is called from
189 static int gc_post_select(__a_unused struct sched *s, void *context)
191 struct grab_client *gc = context;
192 struct btr_node *btrn = gc->btrn;
197 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
202 sz = btr_next_buffer(btrn, &buf);
204 ret = gc_write(gc, buf, sz);
208 btr_consume(btrn, ret);
215 static int gc_check_args(struct lls_parse_result *lpr, struct grab_client *gc)
217 const struct lls_opt_result *r;
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;
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;
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);
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);
251 * Create and activate a grab client.
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.
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.
265 int grab_client_new(int fd, struct lls_parse_result *lpr, struct sched *s)
268 struct grab_client *gc = para_calloc(sizeof(struct grab_client));
270 ret = gc_check_args(lpr, gc);
275 ret = -ERRNO_TO_PARA_ERROR(errno);
279 para_list_add(&gc->node, &inactive_grab_client_list);