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