2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
20 * \file grab_client.c functions for grabbing the stream at any position
23 * \sa filter_chain filter_chain_info filter
27 #include "grab_client.cmdline.h"
31 #include "grab_client.h"
37 /** grab clients that are not yet attached to a filter node */
38 struct list_head inactive_grab_client_list
;
40 static int max_num_filters(void)
43 for (i
= 0; audio_formats
[i
]; i
++) {
44 PARA_INFO_LOG("%s filter chain length: %d\n", audio_formats
[i
],
46 ret
= PARA_MAX(ret
, num_filters(i
));
48 PARA_INFO_LOG("maximal filter chain length: %d\n", ret
);
52 static int gc_write(char *buf
, size_t len
, struct filter_callback
*fcb
)
54 struct grab_client
*gc
= fcb
->data
;
55 struct timeval tv
= {0, 100};
58 // PARA_INFO_LOG("writing %d bytes to fd %d\n", len, gc->fd);
61 FD_SET(gc
->fd
, &wfds
);
62 ret
= para_select(gc
->fd
+ 1, NULL
, &wfds
, &tv
);
64 if (gc
->mode
== GRAB_PEDANTIC
)
65 return -E_PEDANTIC_GRAB
;
66 if (gc
->mode
== GRAB_SLOPPY
)
70 ret
= write(gc
->fd
, buf
, len
);
73 gc
->error
= E_GC_WRITE
;
76 if (gc
->mode
== GRAB_PEDANTIC
)
77 return -E_PEDANTIC_GRAB
;
78 if (gc
->mode
== GRAB_AGGRESSIVE
) {
80 memmove(buf
, buf
+ ret
, len
);
88 /* TODO: gengetopt can handle the grab client modes */
89 static int check_gc_args(struct grab_client
*gc
)
92 struct grab_client_args_info
*c
= gc
->conf
;
93 char **mv
= grab_client_cmdline_parser_mode_values
;
95 PARA_INFO_LOG("filter_num: %d\n", c
->filter_num_arg
);
96 for (i
= 0; mv
[i
]; i
++)
97 if (!strcmp(c
->mode_arg
, mv
[i
]))
102 gc
->audio_format_num
= -1;
103 if (c
->audio_format_given
) {
104 gc
->audio_format_num
= get_audio_format_num(c
->audio_format_arg
);
105 if (gc
->audio_format_num
< 0)
106 return gc
->audio_format_num
;
108 if (c
->slot_arg
> MAX_STREAM_SLOTS
)
109 return -E_BAD_GC_SLOT
;
110 if (c
->filter_num_arg
<= 0)
111 return -E_BAD_GC_FILTER_NUM
;
112 if (c
->audio_format_given
) {
113 if (num_filters(gc
->audio_format_num
) < c
->filter_num_arg
)
114 return -E_BAD_GC_FILTER_NUM
;
116 if (c
->filter_num_arg
> max_num_filters())
117 return -E_BAD_GC_FILTER_NUM
;
122 static void add_inactive_gc(struct grab_client
*gc
)
124 PARA_INFO_LOG("adding grab client %p (fd %d) to inactive list\n",
126 list_add(&gc
->node
, &inactive_grab_client_list
);
129 static void gc_free(struct grab_client
*gc
)
133 for (i
= 0; i
< gc
->argc
; i
++)
141 static void gc_close(struct filter_callback
*fcb
)
143 struct grab_client
*gc
= fcb
->data
;
145 if (gc
->conf
->one_shot_given
|| gc
->error
) {
146 PARA_INFO_LOG("closing fd %d (grab client %p)\n", gc
->fd
, gc
);
156 * move a grab client from the inactive list to a filter node
158 * \param gc the grab client to activate
159 * \param fn the filter node \a gc gets attached to
161 * \sa filter_node::callbacks, inactive_grab_client_list
163 void activate_grab_client(struct grab_client
*gc
, struct filter_node
*fn
)
165 PARA_INFO_LOG("activating %p (fd %d, filter node: %p)\n", gc
, gc
->fd
, fn
);
167 list_add(&gc
->fcb
.node
, &fn
->callbacks
);
171 * activate inactive grab clients if possible
173 * \param slot_num audiod's slot for the new audio file
174 * \param audio_format_num the number of the audio format of the new audio file
175 * \param filter_list the list of activated filters for that new audio file
177 * This is called from audiod.c when the current audio file changes. It loops
178 * over all inactive grab clients and checks each grab client's configuration
179 * to determine if the client in question wishes to grab the new stream. If
180 * yes, this grab client is moved from the inactive grab client list to an
181 * appropriate filter_node.
183 * \sa filter_chain_info::filters, inactive_grab_client_list,
184 * activate_grab_client
186 void activate_inactive_grab_clients(int slot_num
, int audio_format_num
,
187 struct list_head
*filter_list
)
189 struct grab_client
*gc
, *tmp
;
191 struct filter_node
*fn
;
193 list_for_each_entry_safe(gc
, tmp
, &inactive_grab_client_list
, node
) {
194 // PARA_INFO_LOG("checking inactive grab client %p\n", gc);
195 if (gc
->conf
->slot_arg
>= 0 && gc
->conf
->slot_arg
!= slot_num
)
197 if (gc
->audio_format_num
>= 0 && gc
->audio_format_num
!=
200 if (gc
->conf
->filter_num_arg
>= 0 &&
201 num_filters(gc
->audio_format_num
)
202 < gc
->conf
->filter_num_arg
)
205 list_for_each_entry(fn
, filter_list
, node
) {
206 if (gc
->conf
->filter_num_arg
<= 0
207 || i
== gc
->conf
->filter_num_arg
)
211 activate_grab_client(gc
, fn
);
216 * check the command line options and allocate a grab_client structure
218 * \param fd the file descriptor of the client
219 * \param line the command line
220 * \param err non-zero if an error occured
222 * If the command line options given by \a argc and \a argv are valid.
223 * allocate a struct grab_client and initialize it with this valid
224 * configuration. Moreover, add the new grab client to the inactive list.
226 * \return On success, this function returns a pointer to the newly created
227 * struct. On errors, it returns NULL and sets \a err appropriately.
229 * \sa grab_client, inactive_grab_client_list, activate_grab_client,
230 * filter_node::callbacks
233 * argc, argv get freed when com_grab() returns, so we have to make a
236 struct grab_client
*grab_client_new(int fd
, char *line
, int *err
)
239 struct grab_client
*gc
= para_calloc(sizeof(struct grab_client
));
241 gc
->conf
= para_calloc(sizeof(struct grab_client_args_info
));
243 ret
= grab_client_cmdline_parser_string(line
, gc
->conf
, "grab");
247 *err
= -E_GC_HELP_GIVEN
;
248 if (gc
->conf
->help_given
)
250 *err
= -E_GC_VERSION_GIVEN
;
251 if (gc
->conf
->version_given
)
253 *err
= check_gc_args(gc
);
256 if (gc
->conf
->input_grab_given
) {
257 gc
->fcb
.input_cb
= gc_write
;
258 gc
->fcb
.output_cb
= NULL
;
260 gc
->fcb
.output_cb
= gc_write
;
261 gc
->fcb
.input_cb
= NULL
;
264 gc
->fcb
.close
= gc_close
;
274 /** initialize the grabbing subsystem.
276 * This has to be called once during startup before any other function from
277 * grab_client.c may be used. It initializes \a inactive_grab_client_list.
281 PARA_INFO_LOG("%s", "grab init\n");
282 INIT_LIST_HEAD(&inactive_grab_client_list
);