2 * Copyright (C) 2006-2014 Andre Noll <maan@systemlinux.org>
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>
16 #include "buffer_tree.h"
18 #include "grab_client.h"
25 * How to handle blocking writes for the grab client fds.
28 /** Ignore the data and do not write. */
30 /** Write anyway (default). */
32 /** Close fd if write would block. */
36 /** Flags specified as arguments to the grab command. */
38 /** Stop grabbing if audio file changes. */
42 /** Describes one active grab client. */
44 /* The value of the -p option. */
46 /* The value of the -n option. */
48 /** The file descriptor to send the grabbed stream to. */
50 /** See \ref grab_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. */
58 /** Belongs to either the active or the inactive list. */
59 struct list_head node;
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);
67 static int gc_write(struct grab_client *gc, char *buf, size_t len)
69 int ret = write_ok(gc->fd);
73 if (ret == 0) { /* fd not ready */
74 if (gc->mode == GM_PEDANTIC)
76 if (gc->mode == GM_SLOPPY)
79 ret = xwrite(gc->fd, buf, len);
85 if (gc->mode == GM_PEDANTIC)
87 if (gc->mode == GM_SLOPPY)
95 static void gc_pre_select(struct sched *s, struct task *t)
97 struct grab_client *gc = container_of(t, struct grab_client, task);
98 int ret = btr_node_status(gc->btrn, 0, BTR_NT_LEAF);
104 para_fd_set(gc->fd, &s->wfds, &s->max_fileno);
108 * We need this forward declaration as post_select() needs
109 * activate_grab_client and vice versa.
111 static int gc_post_select(struct sched *s, struct task *t);
114 * Move a grab client to the active list and start it.
116 * \param gc The grab client to activate.
118 static void gc_activate(struct grab_client *gc, struct sched *s)
120 struct btr_node *root = audiod_get_btr_root(), *parent;
121 char *name = gc->name? gc->name : "grab";
125 parent = btr_search_node(gc->parent, root);
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';
137 register_task(s, &gc->task);
141 * Activate inactive grab clients if possible.
143 * \param s Needed to schedule the grab client task.
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.
150 * This function also garbage collects all grab clients whose tasks have been
153 void activate_grab_clients(struct sched *s)
155 struct grab_client *gc, *tmp;
157 list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
167 static int gc_close(struct grab_client *gc, int err)
169 btr_remove_node(&gc->btrn);
170 PARA_INFO_LOG("closing gc: %s\n", para_strerror(-err));
171 list_move(&gc->node, &inactive_grab_client_list);
172 if (err == -E_GC_WRITE || (gc->flags & GF_ONE_SHOT)) {
174 * We must not free the gc structure here as it contains ->task
175 * which is still used because this function is called from
187 static int gc_post_select(__a_unused struct sched *s, struct task *t)
189 struct grab_client *gc = container_of(t, struct grab_client, task);
190 struct btr_node *btrn = gc->btrn;
195 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
200 sz = btr_next_buffer(btrn, &buf);
202 ret = gc_write(gc, buf, sz);
206 btr_consume(btrn, ret);
213 static int gc_check_args(int argc, char **argv, struct grab_client *gc)
217 for (i = 1; i < argc; i++) {
218 const char *arg = argv[i];
221 if (!strcmp(arg, "--")) {
225 if (!strncmp(arg, "-m", 2)) {
230 gc->mode = GM_SLOPPY;
233 gc->mode = GM_AGGRESSIVE;
236 gc->mode = GM_PEDANTIC;
242 if (!strcmp(arg, "-o")) {
243 gc->flags |= GF_ONE_SHOT;
246 if (!strncmp(arg, "-p=", 3)) {
247 gc->parent = para_strdup(arg + 3);
250 if (!strncmp(arg, "-n=", 3)) {
251 gc->name = para_strdup(arg + 3);
262 * Check the command line options and allocate a grab_client structure.
264 * \param fd The file descriptor of the client.
265 * \param argc Argument count.
266 * \param argv Argument vector.
267 * \param s The scheduler to register the grab client task to.
269 * If the command line options given by \a argc and \a argv are valid.
270 * allocate a struct grab_client and initialize it with this valid
273 * If the new grab client can be added to an existing buffer tree, activate it.
274 * Otherwise, add it to the inactive list for later activation.
278 int grab_client_new(int fd, int argc, char **argv, struct sched *s)
281 struct grab_client *gc = para_calloc(sizeof(struct grab_client));
283 ret = gc_check_args(argc, argv, gc);
287 para_list_add(&gc->node, &inactive_grab_client_list);