(no log message)
[paraslash.git] / grab_client.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /**
20  * \file grab_client.c functions for grabbing the stream at any position
21  * in a filter chain
22  *
23  * \sa filter_chain filter_chain_info filter
24  */
25
26 #include "para.h"
27 #include "close_on_fork.h"
28 #include "grab_client.cmdline.h"
29 #include "list.h"
30 #include "filter.h"
31 #include "grab_client.h"
32 #include "audiod.h"
33 #include "error.h"
34 #include "string.h"
35
36
37 /** this maps the enum to the text used at the command line */
38 static const char *gc_modes[] = {
39         [GRAB_PEDANTIC] = "pedantic",
40         [GRAB_SLOPPY] = "sloppy",
41         [GRAB_AGGRESSIVE] = "aggressive",
42         NULL
43 };
44
45 /** grab clients that are not yet attached to a filter node */
46 struct list_head inactive_grab_client_list;
47
48 static int max_num_filters(void)
49 {
50         int i, ret = 0;
51         for (i = 0; audio_formats[i]; i++) {
52                 PARA_INFO_LOG("%s filter chain length: %d\n", audio_formats[i],
53                         num_filters(i));
54                 ret = MAX(ret, num_filters(i));
55         }
56         PARA_INFO_LOG("maximal filter chain length: %d\n", ret);
57         return ret;
58 }
59
60 static int gc_write(char *buf, size_t len, struct filter_callback *fcb)
61 {
62         struct grab_client *gc = fcb->data;
63         struct timeval tv = {0, 100};
64         int ret;
65
66 //      PARA_INFO_LOG("writing %d bytes to fd %d\n", len, gc->fd);
67         fd_set wfds;
68         do {
69                 FD_ZERO(&wfds);
70                 FD_SET(gc->fd, &wfds);
71                 ret = select(gc->fd + 1, NULL, &wfds, NULL, &tv);
72         } while (ret == EAGAIN || ret == EINTR);
73         if (ret != 1) {
74                 if (gc->mode == GRAB_PEDANTIC)
75                         return -E_PEDANTIC_GRAB;
76                 if (gc->mode == GRAB_SLOPPY)
77                         return 1;
78         }
79 rewrite:
80         ret = write(gc->fd, buf, len);
81         if (ret < 0) {
82                 ret = -E_GC_WRITE;
83                 gc->error = E_GC_WRITE;
84         } else {
85                 if (ret != len) {
86                         if (gc->mode == GRAB_PEDANTIC)
87                                 return -E_PEDANTIC_GRAB;
88                         if (gc->mode == GRAB_AGGRESSIVE) {
89                                 len -= ret;
90                                 memmove(buf, buf + ret, len);
91                                 goto rewrite;
92                         }
93                 }
94         }
95         return ret;
96 }
97
98 static int check_gc_args(struct grab_client *gc)
99 {
100         int i;
101         struct grab_client_args_info *conf = gc->conf;
102
103         PARA_INFO_LOG("filter_num: %d\n", gc->conf->filter_num_arg);
104         for (i = 0; gc_modes[i]; i++)
105                 if (!strcmp(conf->mode_arg, gc_modes[i]))
106                         break;
107         if (!gc_modes[i])
108                 return -E_INVALID_GRAB_MODE;
109         gc->mode = i;
110         if (conf->audio_format_given) {
111                 gc->audio_format_num = get_audio_format_num(conf->audio_format_arg);
112                 if (gc->audio_format_num < 0)
113                         return gc->audio_format_num;
114         }
115         if (conf->slot_arg > MAX_STREAM_SLOTS)
116                 return -E_BAD_GC_SLOT;
117         if (conf->filter_num_arg <= 0)
118                 return -E_BAD_GC_FILTER_NUM;
119         if (conf->audio_format_given) {
120                 if (num_filters(gc->audio_format_num) < conf->filter_num_arg)
121                         return -E_BAD_GC_FILTER_NUM;
122         } else
123                 if (conf->filter_num_arg > max_num_filters())
124                         return -E_BAD_GC_FILTER_NUM;
125
126         return 1;
127 }
128
129 static void add_inactive_gc(struct grab_client *gc)
130 {
131         PARA_INFO_LOG("adding grab client %p (fd %d) to inactive list\n",
132                 gc, gc->fd);
133         list_add(&gc->node, &inactive_grab_client_list);
134 }
135
136 static void gc_free(struct grab_client *gc)
137 {
138         int i;
139
140         for (i = 0; i < gc->argc; i++)
141                 free(gc->argv[i]);
142         free(gc->argv);
143         free(gc->conf);
144         free(gc);
145
146 }
147
148 static void gc_close(struct filter_callback *fcb)
149 {
150         struct grab_client *gc = fcb->data;
151
152         if (gc->conf->one_shot_given || gc->error) {
153                 PARA_INFO_LOG("closing fd %d (grab client %p)\n", gc->fd, gc);
154                 del_close_on_fork_list(gc->fd);
155                 close(gc->fd);
156                 gc_free(gc);
157                 /* close on fork ?*/
158                 return;
159         }
160         add_inactive_gc(gc);
161 }
162
163 /**
164  * move a grab client from the inactive list to a filter node
165  *
166  * \param gc the grab client to activate
167  * \param fn the filter node \a gc gets attached to
168  *
169  * \sa filter_node::callbacks, inactive_grab_client_list
170  */
171 void activate_grab_client(struct grab_client *gc, struct filter_node *fn)
172 {
173         PARA_INFO_LOG("activating %p (fd %d, filter node: %p)\n", gc, gc->fd, fn);
174         list_del(&gc->node);
175         list_add(&gc->fcb.node, &fn->callbacks);
176 }
177
178 /**
179  * activate inactive grab clients if possible
180  *
181  * \param slot audiod's slot for the new audio file
182  * \param audio_format_num the number of the audio format of the new audio file
183  * \param filter_list the list of activated filters for that new audio file
184  *
185  * This is called from audiod.c when the current audio file changes. It loops
186  * over all inactive grab clients and checks each grab client's configuration
187  * to determine if the client in question wishes to grab the new stream.  If
188  * yes, this grab client is moved from the inactive grab client list to an
189  * appropriate filter_node.
190  *
191  * \sa filter_chain_info::filters, inactive_grab_client_list,
192  * activate_grab_client
193  */
194 void activate_inactive_grab_clients(int slot, int audio_format_num,
195                 struct list_head *filter_list)
196 {
197         struct grab_client *gc, *tmp;
198         int i;
199         struct filter_node *fn;
200
201         list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
202 //              PARA_INFO_LOG("checking inactive grab client %p\n", gc);
203                 if (gc->conf->slot_arg >= 0 && gc->conf->slot_arg != slot)
204                         continue;
205                 if (gc->audio_format_num >= 0 && gc->audio_format_num !=
206                                 audio_format_num)
207                         continue;
208                 if (gc->conf->filter_num_arg >= 0 &&
209                                 num_filters(gc->audio_format_num)
210                                 < gc->conf->filter_num_arg)
211                         continue;
212                 i = 1;
213                 list_for_each_entry(fn, filter_list, node) {
214                         if (gc->conf->filter_num_arg <= 0
215                                 || i == gc->conf->filter_num_arg)
216                                 break;
217                         i++;
218                 }
219                 activate_grab_client(gc, fn);
220         }
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 the number of command line options
228  * \param argv pointers to the command line options
229  * \param err non-zero if an error occured
230  *
231  * If the command line options  given by \a argc and \a argv are valid.
232  * allocate a struct grab_client and initialize it with this valid
233  * configuration. Moreover, add the new grab client to the inactive list.
234  *
235  * \return On success, this function returns a pointer to the newly created
236  * struct. On errors, it returns NULL and sets \a err appropriately.
237  *
238  * \sa grab_client, inactive_grab_client_list, activate_grab_client,
239  * filter_node::callbacks
240  */
241 /*
242  * argc, argv get freed when com_grab() returns, so we have to make a
243  * copy.
244  */
245 __malloc struct grab_client *grab_client_new(int fd, int argc, char **argv, int *err)
246 {
247         int i, ret;
248         struct grab_client *gc = para_calloc(sizeof(struct grab_client));
249
250         gc->conf = para_calloc(sizeof(struct grab_client_args_info));
251         gc->argc = argc;
252         gc->argv = para_calloc((argc + 1) * sizeof(char *));
253
254         for (i = 0; i < argc; i++) {
255                 gc->argv[i] = para_strdup(argv[i]);
256                 PARA_INFO_LOG("argc: %d, argv[%d]: %s\n", argc, i, gc->argv[i]);
257         }
258         PARA_INFO_LOG("argv[%d]: %s\n", argc, gc->argv[argc]);
259         ret = grab_client_cmdline_parser(gc->argc, gc->argv , gc->conf);
260         *err = -E_GC_SYNTAX;
261         if (ret)
262                 goto err_out;
263         *err = -E_GC_HELP_GIVEN;
264         if (gc->conf->help_given)
265                 goto err_out;
266         *err = check_gc_args(gc);
267         if (*err < 0)
268                 goto err_out;
269         if (gc->conf->input_grab_given) {
270                 gc->fcb.input_cb = gc_write;
271                 gc->fcb.output_cb = NULL;
272         } else {
273                 gc->fcb.output_cb = gc_write;
274                 gc->fcb.input_cb = NULL;
275         }
276         gc->fd = fd;
277         gc->fcb.close = gc_close;
278         gc->fcb.data = gc;
279         add_inactive_gc(gc);
280         return gc;
281 err_out:
282         for (i = 0; i < argc; i++)
283                 free(gc->argv[i]);
284         free(gc->argv);
285         free(gc->conf);
286         free(gc);
287         return NULL;
288 }
289
290 /** initialize the grabbing subsystem.
291  *
292  * This has to be called once during startup before any other function from
293  * grab_client.c may be used. It initializes \a inactive_grab_client_list.
294  */
295 void init_grabbing()
296 {
297         PARA_INFO_LOG("%s", "grab init\n");
298         INIT_LIST_HEAD(&inactive_grab_client_list);
299 }
300