afs.c: Fix documentation of stdin_command().
[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 <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 filter_list the list of activated filters for that new audio file
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 list_head *filter_list)
180 {
181         struct grab_client *gc, *tmp;
182         int i;
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                 if (gc->conf->filter_num_arg >= 0 &&
193                                 num_filters(gc->audio_format_num)
194                                 < gc->conf->filter_num_arg)
195                         continue;
196                 i = 1;
197                 list_for_each_entry(fn, filter_list, node) {
198                         if (gc->conf->filter_num_arg <= 0
199                                 || i == gc->conf->filter_num_arg)
200                                 break;
201                         i++;
202                 }
203                 activate_grab_client(gc, fn);
204         }
205 }
206
207 /**
208  * check the command line options and allocate a grab_client structure
209  *
210  * \param fd the file descriptor of the client
211  * \param line the command line
212  * \param err non-zero if an error occured
213  *
214  * If the command line options given by \a argc and \a argv are valid.
215  * allocate a struct grab_client and initialize it with this valid
216  * configuration. Moreover, add the new grab client to the inactive list.
217  *
218  * \return On success, this function returns a pointer to the newly created
219  * struct. On errors, it returns NULL and sets \a err appropriately.
220  *
221  * \sa grab_client, inactive_grab_client_list, activate_grab_client,
222  * filter_node::callbacks
223  */
224 /*
225  * argc, argv get freed when com_grab() returns, so we have to make a
226  * copy.
227  */
228 struct grab_client *grab_client_new(int fd, char *line, int *err)
229 {
230         int ret;
231         struct grab_client *gc = para_calloc(sizeof(struct grab_client));
232
233         gc->conf = para_calloc(sizeof(struct grab_client_args_info));
234
235         ret = grab_client_cmdline_parser_string(line, gc->conf, "grab");
236         *err = -E_GC_SYNTAX;
237         if (ret)
238                 goto err_out;
239         *err = -E_GC_HELP_GIVEN;
240         if (gc->conf->help_given)
241                 goto err_out;
242         *err = -E_GC_VERSION_GIVEN;
243         if (gc->conf->version_given)
244                 goto err_out;
245         *err = check_gc_args(gc);
246         if (*err < 0)
247                 goto err_out;
248         if (gc->conf->input_grab_given) {
249                 gc->fcb.input_cb = gc_write;
250                 gc->fcb.output_cb = NULL;
251         } else {
252                 gc->fcb.output_cb = gc_write;
253                 gc->fcb.input_cb = NULL;
254         }
255         gc->fd = fd;
256         gc->fcb.close = gc_close;
257         gc->fcb.data = gc;
258         add_inactive_gc(gc);
259         return gc;
260 err_out:
261         free(gc->conf);
262         free(gc);
263         return NULL;
264 }
265
266 /** initialize the grabbing subsystem.
267  *
268  * This has to be called once during startup before any other function from
269  * grab_client.c may be used. It initializes \a inactive_grab_client_list.
270  */
271 void init_grabbing(void)
272 {
273         PARA_INFO_LOG("%s", "grab init\n");
274         INIT_LIST_HEAD(&inactive_grab_client_list);
275 }
276