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