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