]> git.tuebingen.mpg.de Git - paraslash.git/blob - grab_client.c
configure: Add config options for libosl.
[paraslash.git] / grab_client.c
1 /*
2  * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /**
8  * \file grab_client.c Functions for grabbing the stream at any position
9  * in a filter chain.
10  *
11  * \sa filter_chain filter_chain_info filter.
12  */
13
14 #include <sys/types.h>
15 #include <dirent.h>
16
17 #include "para.h"
18 #include "list.h"
19 #include "sched.h"
20 #include "ggo.h"
21 #include "filter.h"
22 #include "grab_client.h"
23 #include "audiod.h"
24 #include "error.h"
25 #include "string.h"
26 #include "fd.h"
27
28 /** Grab clients that are not yet attached to a filter node. */
29 struct list_head inactive_grab_client_list;
30
31 static int max_num_filters(void)
32 {
33         int i, ret = 0;
34
35         for (i = 0; audio_formats[i]; i++) {
36                 PARA_INFO_LOG("%s filter chain length: %d\n", audio_formats[i],
37                         num_filters(i));
38                 ret = PARA_MAX(ret, num_filters(i));
39         }
40         PARA_INFO_LOG("maximal filter chain length: %d\n", ret);
41         return ret;
42 }
43
44 static struct filter_node *find_filter_node(int format, int filternum)
45 {
46         int i;
47
48         FOR_EACH_SLOT(i) {
49                 struct slot_info *s = &slot[i];
50                 if (s->format < 0 || !s->fc)
51                         continue;
52                 if (format >= 0 && s->format != format)
53                         continue;
54                 if (num_filters(i) <= filternum)
55                         continue;
56                 /* success */
57                 return  s->fc->filter_nodes + filternum;
58         }
59         return NULL;
60 }
61
62 static int gc_write(char *buf, size_t len, struct filter_callback *fcb)
63 {
64         struct grab_client *gc = container_of(fcb, struct grab_client, fcb);
65         size_t written = 0;
66
67         while (written < len) {
68                 int ret = write_ok(gc->fd);
69                 if (ret < 0)
70                         goto err;
71                 if (ret == 0) { /* fd not ready */
72                         if (gc->mode == GM_PEDANTIC)
73                                 goto err;
74                         if (gc->mode == GM_SLOPPY)
75                                 return 1;
76                 }
77                 ret = write(gc->fd, buf + written, len - written);
78                 if (ret < 0) {
79                         if (errno != EAGAIN && errno != EINTR)
80                                 goto err;
81                         if (gc->mode == GM_PEDANTIC)
82                                 goto err;
83                         if (gc->mode == GM_SLOPPY)
84                                 return 1;
85                 } else
86                         written += ret;
87         }
88         return 1;
89 err:
90         gc->error = -E_GC_WRITE;
91         return -E_GC_WRITE;
92 }
93
94 static void add_inactive_gc(struct grab_client *gc)
95 {
96         PARA_INFO_LOG("adding grab client %p (fd %d) to inactive list\n",
97                 gc, gc->fd);
98         para_list_add(&gc->node, &inactive_grab_client_list);
99 }
100
101 static void gc_close(struct filter_callback *fcb)
102 {
103         struct grab_client *gc = container_of(fcb, struct grab_client, fcb);
104
105         if ((gc->flags & GF_ONE_SHOT) || gc->error < 0) {
106                 PARA_INFO_LOG("closing fd %d (grab client %p)\n", gc->fd, gc);
107                 close(gc->fd);
108                 free(gc);
109                 return;
110         }
111         add_inactive_gc(gc);
112 }
113
114 /**
115  * Move a grab client from the inactive list to a filter node.
116  *
117  * \param gc The grab client to activate.
118  * \param fn The filter node \a gc gets attached to.
119  *
120  * \sa filter_node::callbacks, inactive_grab_client_list.
121  */
122 void activate_grab_client(struct grab_client *gc, struct filter_node *fn)
123 {
124         PARA_INFO_LOG("activating %p (fd %d, filter node: %p)\n", gc, gc->fd, fn);
125         list_del(&gc->node);
126         para_list_add(&gc->fcb.node, &fn->callbacks);
127 }
128
129 /**
130  * Activate inactive grab clients if possible.
131  *
132  * \param audio_format_num The number of the audio format of the new audio file.
133  * \param fc The filter chain containing the activated filters.
134  *
135  * This is called from audiod.c when the current audio file changes. It loops
136  * over all inactive grab clients and checks each grab client's configuration
137  * to determine if the client in question wishes to grab the new stream.  If
138  * yes, this grab client is moved from the inactive grab client list to an
139  * appropriate filter_node.
140  *
141  * \sa filter_chain_info::filters, inactive_grab_client_list,
142  * activate_grab_client.
143  */
144 void activate_inactive_grab_clients(int audio_format_num,
145                 struct filter_chain *fc)
146 {
147         struct grab_client *gc, *tmp;
148         struct filter_node *fn;
149
150         list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
151                 if (gc->audio_format_num >= 0 && gc->audio_format_num !=
152                                 audio_format_num)
153                         continue;
154                 if (gc->filter_num >= num_filters(audio_format_num))
155                         continue;
156                 fn = fc->filter_nodes + gc->filter_num;
157                 activate_grab_client(gc, fn);
158         }
159 }
160
161 static int check_gc_args(int argc, char **argv, struct grab_client *gc)
162 {
163         int i, ret;
164
165         gc->audio_format_num = -1; /* default: grab any audio format */
166         for (i = 1; i < argc; i++) {
167                 const char *arg = argv[i];
168                 if (arg[0] != '-')
169                         break;
170                 if (!strcmp(arg, "--")) {
171                         i++;
172                         break;
173                 }
174                 if (!strncmp(arg, "-n=", 3)) {
175                         ret = para_atoi32(arg + 3, &gc->filter_num);
176                         if (ret < 0)
177                                 return ret;
178                         if (gc->filter_num < 0)
179                                 return -E_BAD_GC_FILTER_NUM;
180                         if (gc->filter_num >= max_num_filters())
181                                 return -E_BAD_GC_FILTER_NUM;
182                         continue;
183                 }
184                 if (!strncmp(arg, "-m", 2)) {
185                         if (*(arg + 3))
186                                 return -E_GC_SYNTAX;
187                         switch(*(arg + 2)) {
188                         case 's':
189                                 gc->mode = GM_SLOPPY;
190                                 continue;
191                         case 'a':
192                                 gc->mode = GM_AGGRESSIVE;
193                                 continue;
194                         case 'p':
195                                 gc->mode = GM_PEDANTIC;
196                                 continue;
197                         default:
198                                 return -E_GC_SYNTAX;
199                         }
200                 }
201                 if (!strcmp(arg, "-i")) {
202                         gc->flags |= GF_INPUT_GRAB;
203                         continue;
204                 }
205                 if (!strcmp(arg, "-o")) {
206                         gc->flags |= GF_ONE_SHOT;
207                         continue;
208                 }
209                 if (!strncmp(arg, "-f=", 3)) {
210                         ret = get_audio_format_num(arg + 3);
211                         if (ret < 0)
212                                 return ret;
213                         gc->audio_format_num = ret;
214                         continue;
215                 }
216                 return -E_GC_SYNTAX;
217         }
218         if (i != argc)
219                 return -E_GC_SYNTAX;
220         return 1;
221 }
222
223 /**
224  * Check the command line options and allocate a grab_client structure.
225  *
226  * \param fd The file descriptor of the client.
227  * \param argc Argument count.
228  * \param argv Argument vector.
229  *
230  * If the command line options given by \a argc and \a argv are valid.
231  * allocate a struct grab_client and initialize it with this valid
232  * configuration. Moreover, add the new grab client to the inactive list.
233  *
234  * \return Standard.
235  *
236  * \sa grab_client, inactive_grab_client_list, activate_grab_client,
237  * filter_node::callbacks.
238  */
239 int grab_client_new(int fd, int argc, char **argv)
240 {
241         int ret;
242         struct grab_client *gc = para_calloc(sizeof(struct grab_client));
243         struct filter_node *fn;
244
245         ret = check_gc_args(argc, argv, gc);
246         if (ret < 0)
247                 goto err_out;
248         if (gc->flags & GF_INPUT_GRAB)
249                 gc->fcb.input_cb = gc_write;
250         else
251                 gc->fcb.output_cb = gc_write;
252         gc->fd = fd;
253         gc->fcb.close = gc_close;
254         fn = find_filter_node(gc->audio_format_num, gc->filter_num);
255         if (fn)
256                 para_list_add(&gc->fcb.node, &fn->callbacks);
257         else
258                 add_inactive_gc(gc);
259         return 1;
260 err_out:
261         free(gc);
262         return ret;
263 }
264
265 /**
266  * Initialize the grabbing subsystem.
267  *
268  * This has to be called once during startup before any other function from
269  * grab_client.c may be used. It initializes \a inactive_grab_client_list.
270  */
271 void init_grabbing(void)
272 {
273         PARA_INFO_LOG("grab init\n");
274         INIT_LIST_HEAD(&inactive_grab_client_list);
275 }