2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
8 * \file grab_client.c Functions for grabbing the stream at any position
11 * \sa filter_chain filter_chain_info filter.
15 #include <sys/types.h>
23 #include "buffer_tree.h"
25 #include "grab_client.h"
31 /** Grab clients that are not yet attached to a filter node. */
32 static struct list_head inactive_grab_client_list;
34 static int max_num_filters(void)
38 for (i = 0; audio_formats[i]; i++) {
39 PARA_INFO_LOG("%s filter chain length: %d\n", audio_formats[i],
41 ret = PARA_MAX(ret, num_filters(i));
43 PARA_INFO_LOG("maximal filter chain length: %d\n", ret);
47 static struct filter_node *find_filter_node(int format, int filternum)
52 struct slot_info *s = &slot[i];
53 if (s->format < 0 || !s->fc)
55 if (format >= 0 && s->format != format)
57 if (num_filters(i) <= filternum)
60 return s->fc->filter_nodes + filternum;
65 static int gc_write(char *buf, size_t len, struct filter_callback *fcb)
67 struct grab_client *gc = container_of(fcb, struct grab_client, fcb);
70 while (written < len) {
71 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 = write(gc->fd, buf + written, len - written);
82 if (errno != EAGAIN && errno != EINTR)
84 if (gc->mode == GM_PEDANTIC)
86 if (gc->mode == GM_SLOPPY)
93 gc->error = -E_GC_WRITE;
97 static void add_inactive_gc(struct grab_client *gc)
99 PARA_INFO_LOG("adding grab client %p (fd %d) to inactive list\n",
101 para_list_add(&gc->node, &inactive_grab_client_list);
104 static void gc_close(struct filter_callback *fcb)
106 struct grab_client *gc = container_of(fcb, struct grab_client, fcb);
108 if ((gc->flags & GF_ONE_SHOT) || gc->error < 0) {
109 PARA_INFO_LOG("closing fd %d (grab client %p)\n", gc->fd, gc);
118 * Move a grab client from the inactive list to a filter node.
120 * \param gc The grab client to activate.
121 * \param fn The filter node \a gc gets attached to.
123 * \sa filter_node::callbacks, inactive_grab_client_list.
125 void activate_grab_client(struct grab_client *gc, struct filter_node *fn)
127 PARA_INFO_LOG("activating %p (fd %d, filter node: %p)\n", gc, gc->fd, fn);
129 para_list_add(&gc->fcb.node, &fn->callbacks);
133 * Activate inactive grab clients if possible.
135 * \param audio_format_num The number of the audio format of the new audio file.
136 * \param fc The filter chain containing the activated filters.
138 * This is called from audiod.c when the current audio file changes. It loops
139 * over all inactive grab clients and checks each grab client's configuration
140 * to determine if the client in question wishes to grab the new stream. If
141 * yes, this grab client is moved from the inactive grab client list to an
142 * appropriate filter_node.
144 * \sa filter_chain_info::filters, inactive_grab_client_list,
145 * activate_grab_client.
147 void activate_inactive_grab_clients(int audio_format_num,
148 struct filter_chain *fc)
150 struct grab_client *gc, *tmp;
151 struct filter_node *fn;
153 list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
154 if (gc->audio_format_num >= 0 && gc->audio_format_num !=
157 if (gc->filter_num >= num_filters(audio_format_num))
159 fn = fc->filter_nodes + gc->filter_num;
160 activate_grab_client(gc, fn);
164 static int check_gc_args(int argc, char **argv, struct grab_client *gc)
168 gc->audio_format_num = -1; /* default: grab any audio format */
169 for (i = 1; i < argc; i++) {
170 const char *arg = argv[i];
173 if (!strcmp(arg, "--")) {
177 if (!strncmp(arg, "-n=", 3)) {
178 ret = para_atoi32(arg + 3, &gc->filter_num);
181 if (gc->filter_num < 0)
182 return -E_BAD_GC_FILTER_NUM;
183 if (gc->filter_num >= max_num_filters())
184 return -E_BAD_GC_FILTER_NUM;
187 if (!strncmp(arg, "-m", 2)) {
192 gc->mode = GM_SLOPPY;
195 gc->mode = GM_AGGRESSIVE;
198 gc->mode = GM_PEDANTIC;
204 if (!strcmp(arg, "-i")) {
205 gc->flags |= GF_INPUT_GRAB;
208 if (!strcmp(arg, "-o")) {
209 gc->flags |= GF_ONE_SHOT;
212 if (!strncmp(arg, "-f=", 3)) {
213 ret = get_audio_format_num(arg + 3);
216 gc->audio_format_num = ret;
227 * Check the command line options and allocate a grab_client structure.
229 * \param fd The file descriptor of the client.
230 * \param argc Argument count.
231 * \param argv Argument vector.
233 * If the command line options given by \a argc and \a argv are valid.
234 * allocate a struct grab_client and initialize it with this valid
235 * configuration. Moreover, add the new grab client to the inactive list.
239 * \sa grab_client, inactive_grab_client_list, activate_grab_client,
240 * filter_node::callbacks.
242 int grab_client_new(int fd, int argc, char **argv)
245 struct grab_client *gc = para_calloc(sizeof(struct grab_client));
246 struct filter_node *fn;
248 ret = check_gc_args(argc, argv, gc);
251 if (gc->flags & GF_INPUT_GRAB)
252 gc->fcb.input_cb = gc_write;
254 gc->fcb.output_cb = gc_write;
256 gc->fcb.close = gc_close;
257 fn = find_filter_node(gc->audio_format_num, gc->filter_num);
259 para_list_add(&gc->fcb.node, &fn->callbacks);
269 * Initialize the grabbing subsystem.
271 * This has to be called once during startup before any other function from
272 * grab_client.c may be used. It initializes \a inactive_grab_client_list.
274 void init_grabbing(void)
276 PARA_INFO_LOG("grab init\n");
277 INIT_LIST_HEAD(&inactive_grab_client_list);