2 * Copyright (C) 2006-2010 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>
18 #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 INITIALIZED_LIST_HEAD(active_grab_client_list);
66 /* Grab clients that are not currently attached any btr node. */
67 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 = write_nonblock(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, struct task *t)
99 struct grab_client *gc = container_of(t, struct grab_client, task);
100 int ret = btr_node_status(gc->btrn, 0, BTR_NT_LEAF);
105 s->timeout.tv_sec = 0;
106 s->timeout.tv_usec = 0;
109 para_fd_set(gc->fd, &s->wfds, &s->max_fileno);
113 * We need this forward declaration as post_select() needs
114 * activate_grab_client and vice versa.
116 static void gc_post_select(struct sched *s, struct task *t);
119 * Move a grab client to the active list and start it.
121 * \param gc The grab client to activate.
123 static void gc_activate(struct grab_client *gc)
125 struct btr_node *root = audiod_get_btr_root(), *parent;
126 char *name = gc->name? gc->name : "grab";
130 parent = btr_search_node(gc->parent, root);
133 PARA_INFO_LOG("activating fd %d\n", gc->fd);
134 list_move(&gc->node, &active_grab_client_list);
135 gc->btrn = btr_new_node(&(struct btr_node_description)
136 EMBRACE(.name = name, .parent = parent));
137 if (!gc->task.pre_select) {
138 gc->task.pre_select = gc_pre_select;
139 gc->task.post_select = gc_post_select;
140 snprintf(gc->task.status, sizeof(gc->task.status) - 1, "%s", name);
141 gc->task.status[sizeof(gc->task.status) - 1] = '\0';
142 register_task(&gc->task);
147 * Activate inactive grab clients if possible.
149 * This is called from audiod.c when the current audio file changes. It loops
150 * over all inactive grab clients and checks each grab client's configuration
151 * to determine if the client in question wishes to grab the new stream. If
152 * yes, this grab client is moved from the inactive to the active grab client list.
154 * This function also garbage collects all grab clients whose tasks have been
157 void activate_grab_clients(void)
159 struct grab_client *gc, *tmp;
161 list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
162 if (gc->task.error == -E_TASK_UNREGISTERED) {
171 static int gc_close(struct grab_client *gc, int err)
173 btr_remove_node(gc->btrn);
174 btr_free_node(gc->btrn);
176 PARA_INFO_LOG("closing gc: %s\n", para_strerror(-err));
177 list_move(&gc->node, &inactive_grab_client_list);
178 if (err == -E_GC_WRITE || (gc->flags & GF_ONE_SHOT)) {
180 * We must not free the gc structure here as it contains ->task
181 * which is still used because this function is called from
193 static void gc_post_select(__a_unused struct sched *s, struct task *t)
195 struct grab_client *gc = container_of(t, struct grab_client, task);
196 struct btr_node *btrn = gc->btrn;
202 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
207 sz = btr_next_buffer(btrn, &buf);
209 ret = gc_write(gc, buf, sz);
213 btr_consume(btrn, ret);
216 t->error = gc_close(gc, ret)? ret : 0;
219 static int gc_check_args(int argc, char **argv, struct grab_client *gc)
223 for (i = 1; i < argc; i++) {
224 const char *arg = argv[i];
227 if (!strcmp(arg, "--")) {
231 if (!strncmp(arg, "-m", 2)) {
236 gc->mode = GM_SLOPPY;
239 gc->mode = GM_AGGRESSIVE;
242 gc->mode = GM_PEDANTIC;
248 if (!strcmp(arg, "-o")) {
249 gc->flags |= GF_ONE_SHOT;
252 if (!strncmp(arg, "-p=", 3)) {
253 gc->parent = para_strdup(arg + 3);
256 if (!strncmp(arg, "-n=", 3)) {
257 gc->name = para_strdup(arg + 3);
268 * Check the command line options and allocate a grab_client structure.
270 * \param fd The file descriptor of the client.
271 * \param argc Argument count.
272 * \param argv Argument vector.
274 * If the command line options given by \a argc and \a argv are valid.
275 * allocate a struct grab_client and initialize it with this valid
278 * If the new grab client can be added to an existing buffer tree, activate it.
279 * Otherwise, add it to the inactive list for later activation.
283 int grab_client_new(int fd, int argc, char **argv)
286 struct grab_client *gc = para_calloc(sizeof(struct grab_client));
288 ret = gc_check_args(argc, argv, gc);
292 para_list_add(&gc->node, &inactive_grab_client_list);