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