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