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