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>
14 #include "audiod_cmd.lsg.h"
19 #include "buffer_tree.h"
20 #include "grab_client.h"
27 * How to handle blocking writes for the grab client fds.
30 /** Ignore the data and do not write. */
32 /** Write anyway (default). */
34 /** Close fd if write would block. */
38 /** Flags specified as arguments to the grab command. */
40 /** Stop grabbing if audio file changes. */
44 /** Describes one active grab client. */
46 /* The value of the -p option. */
48 /* The value of the -n option. */
50 /** The file descriptor to send the grabbed stream to. */
52 /** See \ref grab_mode. */
54 /** Flags given at the command line. */
55 enum grab_flags flags;
56 /** The point of the grab client's node in the buffer tree. */
57 struct btr_node *btrn;
58 /* The task of this grab client. */
60 /** Belongs to either the active or the inactive list. */
61 struct list_head node;
64 /* Grab clients that are attached to a btr node. */
65 static INITIALIZED_LIST_HEAD(active_grab_client_list);
66 /* Grab clients that are not currently attached any btr node. */
67 static INITIALIZED_LIST_HEAD(inactive_grab_client_list);
69 static int gc_write(struct grab_client *gc, char *buf, size_t len)
71 int ret = write_ok(gc->fd);
75 if (ret == 0) { /* fd not ready */
76 if (gc->mode == GM_PEDANTIC)
78 if (gc->mode == GM_SLOPPY)
81 ret = xwrite(gc->fd, buf, len);
87 if (gc->mode == GM_PEDANTIC)
89 if (gc->mode == GM_SLOPPY)
97 static void gc_pre_select(struct sched *s, void *context)
99 struct grab_client *gc = context;
100 int ret = btr_node_status(gc->btrn, 0, BTR_NT_LEAF);
106 para_fd_set(gc->fd, &s->wfds, &s->max_fileno);
110 * We need this forward declaration as post_select() needs
111 * activate_grab_client and vice versa.
113 static int gc_post_select(struct sched *s, void *context);
116 * Move a grab client to the active list and start it.
118 * \param gc The grab client to activate.
120 static void gc_activate(struct grab_client *gc, struct sched *s)
122 struct btr_node *root = audiod_get_btr_root(), *parent;
123 char *name = gc->name? gc->name : "grab";
127 parent = btr_search_node(gc->parent, root);
130 PARA_INFO_LOG("activating fd %d\n", gc->fd);
131 list_move(&gc->node, &active_grab_client_list);
132 gc->btrn = btr_new_node(&(struct btr_node_description)
133 EMBRACE(.name = name, .parent = parent));
135 gc->task = task_register(&(struct task_info) {
137 .pre_select = gc_pre_select,
138 .post_select = gc_post_select,
144 * Activate inactive grab clients if possible.
146 * \param s Needed to schedule the grab client task.
148 * This is called from audiod.c when the current audio file changes. It loops
149 * over all inactive grab clients and checks each grab client's configuration
150 * to determine if the client in question wishes to grab the new stream. If
151 * yes, this grab client is moved from the inactive to the active grab client list.
153 * This function also garbage collects all grab clients whose tasks have been
156 void activate_grab_clients(struct sched *s)
158 struct grab_client *gc, *tmp;
160 list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
170 static int gc_close(struct grab_client *gc, int err)
172 btr_remove_node(&gc->btrn);
173 PARA_INFO_LOG("closing gc: %s\n", para_strerror(-err));
174 list_move(&gc->node, &inactive_grab_client_list);
175 if (err == -E_GC_WRITE || (gc->flags & GF_ONE_SHOT)) {
177 * We must not free the gc structure here as it contains ->task
178 * which is still used because this function is called from
190 static int gc_post_select(__a_unused struct sched *s, void *context)
192 struct grab_client *gc = context;
193 struct btr_node *btrn = gc->btrn;
198 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
203 sz = btr_next_buffer(btrn, &buf);
205 ret = gc_write(gc, buf, sz);
209 btr_consume(btrn, ret);
216 static int gc_check_args(struct lls_parse_result *lpr, struct grab_client *gc)
218 const struct lls_opt_result *r;
220 r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_MODE, lpr);
221 if (lls_opt_given(r) > 0) {
222 const char *arg = lls_string_val(0, r);
223 if (strcmp(arg, "s") == 0)
224 gc->mode = GM_SLOPPY;
225 else if (strcmp(arg, "a") == 0)
226 gc->mode = GM_AGGRESSIVE;
227 else if (strcmp(arg, "p") == 0)
228 gc->mode = GM_PEDANTIC;
233 r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_ONE_SHOT, lpr);
234 if (lls_opt_given(r) > 0)
235 gc->flags |= GF_ONE_SHOT;
237 r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_PARENT, lpr);
238 if (lls_opt_given(r) > 0) {
239 const char *arg = lls_string_val(0, r);
240 gc->parent = para_strdup(arg);
243 r = lls_opt_result(LSG_AUDIOD_CMD_GRAB_OPT_NAME, lpr);
244 if (lls_opt_given(r) > 0) {
245 const char *arg = lls_string_val(0, r);
246 gc->name = para_strdup(arg);
252 * Create and activate a grab client.
254 * \param fd The file descriptor of the client.
255 * \param lpr The parsed command line of the grab command.
256 * \param s The scheduler to register the grab client task to.
258 * This function semantically parses the arguments given as options to the grab
259 * command. On success it allocates a struct grab_client, associates it with
260 * the given file descriptor and activates it. If the new grab client can not
261 * be attached to an existing buffer tree node it is put into the inactive list
262 * for later activation.
266 int grab_client_new(int fd, struct lls_parse_result *lpr, struct sched *s)
269 struct grab_client *gc = para_calloc(sizeof(struct grab_client));
271 ret = gc_check_args(lpr, gc);
276 ret = -ERRNO_TO_PARA_ERROR(errno);
280 para_list_add(&gc->node, &inactive_grab_client_list);