]> git.tuebingen.mpg.de Git - paraslash.git/blob - grab_client.c
2dfb295b8ce87fcb5f165bcfd4b3c25f3bdb5b50
[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 /** grab clients that are not yet attached to a filter node */
37 struct list_head inactive_grab_client_list;
38
39 static int max_num_filters(void)
40 {
41         int i, ret = 0;
42         for (i = 0; audio_formats[i]; i++) {
43                 PARA_INFO_LOG("%s filter chain length: %d\n", audio_formats[i],
44                         num_filters(i));
45                 ret = MAX(ret, num_filters(i));
46         }
47         PARA_INFO_LOG("maximal filter chain length: %d\n", ret);
48         return ret;
49 }
50
51 static int gc_write(char *buf, size_t len, struct filter_callback *fcb)
52 {
53         struct grab_client *gc = fcb->data;
54         struct timeval tv = {0, 100};
55         int ret;
56
57 //      PARA_INFO_LOG("writing %d bytes to fd %d\n", len, gc->fd);
58         fd_set wfds;
59         do {
60                 FD_ZERO(&wfds);
61                 FD_SET(gc->fd, &wfds);
62                 ret = select(gc->fd + 1, NULL, &wfds, NULL, &tv);
63         } while (ret == EAGAIN || ret == EINTR);
64         if (ret != 1) {
65                 if (gc->mode == GRAB_PEDANTIC)
66                         return -E_PEDANTIC_GRAB;
67                 if (gc->mode == GRAB_SLOPPY)
68                         return 1;
69         }
70 rewrite:
71         ret = write(gc->fd, buf, len);
72         if (ret < 0) {
73                 ret = -E_GC_WRITE;
74                 gc->error = E_GC_WRITE;
75         } else {
76                 if (ret != len) {
77                         if (gc->mode == GRAB_PEDANTIC)
78                                 return -E_PEDANTIC_GRAB;
79                         if (gc->mode == GRAB_AGGRESSIVE) {
80                                 len -= ret;
81                                 memmove(buf, buf + ret, len);
82                                 goto rewrite;
83                         }
84                 }
85         }
86         return ret;
87 }
88
89 static int check_gc_args(struct grab_client *gc)
90 {
91         int i;
92         struct grab_client_args_info *conf = gc->conf;
93         char **mv = grab_client_cmdline_parser_mode_values;
94
95         PARA_INFO_LOG("filter_num: %d\n", gc->conf->filter_num_arg);
96         for (i = 0; mv[i]; i++)
97                 if (!strcmp(conf->mode_arg, mv[i]))
98                         break;
99         gc->mode = i;
100         if (conf->audio_format_given) {
101                 gc->audio_format_num = get_audio_format_num(conf->audio_format_arg);
102                 if (gc->audio_format_num < 0)
103                         return gc->audio_format_num;
104         }
105         if (conf->slot_arg > MAX_STREAM_SLOTS)
106                 return -E_BAD_GC_SLOT;
107         if (conf->filter_num_arg <= 0)
108                 return -E_BAD_GC_FILTER_NUM;
109         if (conf->audio_format_given) {
110                 if (num_filters(gc->audio_format_num) < conf->filter_num_arg)
111                         return -E_BAD_GC_FILTER_NUM;
112         } else
113                 if (conf->filter_num_arg > max_num_filters())
114                         return -E_BAD_GC_FILTER_NUM;
115
116         return 1;
117 }
118
119 static void add_inactive_gc(struct grab_client *gc)
120 {
121         PARA_INFO_LOG("adding grab client %p (fd %d) to inactive list\n",
122                 gc, gc->fd);
123         list_add(&gc->node, &inactive_grab_client_list);
124 }
125
126 static void gc_free(struct grab_client *gc)
127 {
128         int i;
129
130         for (i = 0; i < gc->argc; i++)
131                 free(gc->argv[i]);
132         free(gc->argv);
133         free(gc->conf);
134         free(gc);
135
136 }
137
138 static void gc_close(struct filter_callback *fcb)
139 {
140         struct grab_client *gc = fcb->data;
141
142         if (gc->conf->one_shot_given || gc->error) {
143                 PARA_INFO_LOG("closing fd %d (grab client %p)\n", gc->fd, gc);
144                 del_close_on_fork_list(gc->fd);
145                 close(gc->fd);
146                 gc_free(gc);
147                 /* close on fork ?*/
148                 return;
149         }
150         add_inactive_gc(gc);
151 }
152
153 /**
154  * move a grab client from the inactive list to a filter node
155  *
156  * \param gc the grab client to activate
157  * \param fn the filter node \a gc gets attached to
158  *
159  * \sa filter_node::callbacks, inactive_grab_client_list
160  */
161 void activate_grab_client(struct grab_client *gc, struct filter_node *fn)
162 {
163         PARA_INFO_LOG("activating %p (fd %d, filter node: %p)\n", gc, gc->fd, fn);
164         list_del(&gc->node);
165         list_add(&gc->fcb.node, &fn->callbacks);
166 }
167
168 /**
169  * activate inactive grab clients if possible
170  *
171  * \param slot audiod's slot for the new audio file
172  * \param audio_format_num the number of the audio format of the new audio file
173  * \param filter_list the list of activated filters for that new audio file
174  *
175  * This is called from audiod.c when the current audio file changes. It loops
176  * over all inactive grab clients and checks each grab client's configuration
177  * to determine if the client in question wishes to grab the new stream.  If
178  * yes, this grab client is moved from the inactive grab client list to an
179  * appropriate filter_node.
180  *
181  * \sa filter_chain_info::filters, inactive_grab_client_list,
182  * activate_grab_client
183  */
184 void activate_inactive_grab_clients(int slot, int audio_format_num,
185                 struct list_head *filter_list)
186 {
187         struct grab_client *gc, *tmp;
188         int i;
189         struct filter_node *fn;
190
191         list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
192 //              PARA_INFO_LOG("checking inactive grab client %p\n", gc);
193                 if (gc->conf->slot_arg >= 0 && gc->conf->slot_arg != slot)
194                         continue;
195                 if (gc->audio_format_num >= 0 && gc->audio_format_num !=
196                                 audio_format_num)
197                         continue;
198                 if (gc->conf->filter_num_arg >= 0 &&
199                                 num_filters(gc->audio_format_num)
200                                 < gc->conf->filter_num_arg)
201                         continue;
202                 i = 1;
203                 list_for_each_entry(fn, filter_list, node) {
204                         if (gc->conf->filter_num_arg <= 0
205                                 || i == gc->conf->filter_num_arg)
206                                 break;
207                         i++;
208                 }
209                 activate_grab_client(gc, fn);
210         }
211 }
212
213 /**
214  * check the command line options and allocate a grab_client structure
215  *
216  * \param fd the file descriptor of the client
217  * \param argc the number of command line options
218  * \param argv pointers to the command line options
219  * \param err non-zero if an error occured
220  *
221  * If the command line options  given by \a argc and \a argv are valid.
222  * allocate a struct grab_client and initialize it with this valid
223  * configuration. Moreover, add the new grab client to the inactive list.
224  *
225  * \return On success, this function returns a pointer to the newly created
226  * struct. On errors, it returns NULL and sets \a err appropriately.
227  *
228  * \sa grab_client, inactive_grab_client_list, activate_grab_client,
229  * filter_node::callbacks
230  */
231 /*
232  * argc, argv get freed when com_grab() returns, so we have to make a
233  * copy.
234  */
235 __malloc struct grab_client *grab_client_new(int fd, int argc, char **argv, int *err)
236 {
237         int i, ret;
238         struct grab_client *gc = para_calloc(sizeof(struct grab_client));
239
240         gc->conf = para_calloc(sizeof(struct grab_client_args_info));
241         gc->argc = argc;
242         gc->argv = para_calloc((argc + 1) * sizeof(char *));
243
244         for (i = 0; i < argc; i++) {
245                 gc->argv[i] = para_strdup(argv[i]);
246                 PARA_INFO_LOG("argc: %d, argv[%d]: %s\n", argc, i, gc->argv[i]);
247         }
248         PARA_INFO_LOG("argv[%d]: %s\n", argc, gc->argv[argc]);
249         ret = grab_client_cmdline_parser(gc->argc, gc->argv , gc->conf);
250         *err = -E_GC_SYNTAX;
251         if (ret)
252                 goto err_out;
253         *err = -E_GC_HELP_GIVEN;
254         if (gc->conf->help_given)
255                 goto err_out;
256         *err = -E_GC_VERSION_GIVEN;
257         if (gc->conf->version_given)
258                 goto err_out;
259         *err = check_gc_args(gc);
260         if (*err < 0)
261                 goto err_out;
262         if (gc->conf->input_grab_given) {
263                 gc->fcb.input_cb = gc_write;
264                 gc->fcb.output_cb = NULL;
265         } else {
266                 gc->fcb.output_cb = gc_write;
267                 gc->fcb.input_cb = NULL;
268         }
269         gc->fd = fd;
270         gc->fcb.close = gc_close;
271         gc->fcb.data = gc;
272         add_inactive_gc(gc);
273         return gc;
274 err_out:
275         for (i = 0; i < argc; i++)
276                 free(gc->argv[i]);
277         free(gc->argv);
278         free(gc->conf);
279         free(gc);
280         return NULL;
281 }
282
283 /** initialize the grabbing subsystem.
284  *
285  * This has to be called once during startup before any other function from
286  * grab_client.c may be used. It initializes \a inactive_grab_client_list.
287  */
288 void init_grabbing()
289 {
290         PARA_INFO_LOG("%s", "grab init\n");
291         INIT_LIST_HEAD(&inactive_grab_client_list);
292 }
293