2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
8 * \file grab_client.c functions for grabbing the stream at any position
11 * \sa filter_chain filter_chain_info filter
15 #include "grab_client.cmdline.h"
19 #include "grab_client.h"
26 /** grab clients that are not yet attached to a filter node */
27 struct list_head inactive_grab_client_list
;
29 static int max_num_filters(void)
32 for (i
= 0; audio_formats
[i
]; i
++) {
33 PARA_INFO_LOG("%s filter chain length: %d\n", audio_formats
[i
],
35 ret
= PARA_MAX(ret
, num_filters(i
));
37 PARA_INFO_LOG("maximal filter chain length: %d\n", ret
);
41 static int gc_write(char *buf
, size_t len
, struct filter_callback
*fcb
)
43 struct grab_client
*gc
= fcb
->data
;
44 struct timeval tv
= {0, 100};
47 // PARA_INFO_LOG("writing %d bytes to fd %d\n", len, gc->fd);
50 FD_SET(gc
->fd
, &wfds
);
51 ret
= para_select(gc
->fd
+ 1, NULL
, &wfds
, &tv
);
53 if (gc
->mode
== GRAB_PEDANTIC
)
54 return -E_PEDANTIC_GRAB
;
55 if (gc
->mode
== GRAB_SLOPPY
)
59 ret
= write(gc
->fd
, buf
, len
);
62 gc
->error
= E_GC_WRITE
;
65 if (gc
->mode
== GRAB_PEDANTIC
)
66 return -E_PEDANTIC_GRAB
;
67 if (gc
->mode
== GRAB_AGGRESSIVE
) {
69 memmove(buf
, buf
+ ret
, len
);
77 /* TODO: gengetopt can handle the grab client modes */
78 static int check_gc_args(struct grab_client
*gc
)
81 struct grab_client_args_info
*c
= gc
->conf
;
82 char **mv
= grab_client_cmdline_parser_mode_values
;
84 PARA_INFO_LOG("filter_num: %d\n", c
->filter_num_arg
);
85 for (i
= 0; mv
[i
]; i
++)
86 if (!strcmp(c
->mode_arg
, mv
[i
]))
91 gc
->audio_format_num
= -1;
92 if (c
->audio_format_given
) {
93 gc
->audio_format_num
= get_audio_format_num(c
->audio_format_arg
);
94 if (gc
->audio_format_num
< 0)
95 return gc
->audio_format_num
;
97 if (c
->slot_arg
> MAX_STREAM_SLOTS
)
98 return -E_BAD_GC_SLOT
;
99 if (c
->filter_num_arg
<= 0)
100 return -E_BAD_GC_FILTER_NUM
;
101 if (c
->audio_format_given
) {
102 if (num_filters(gc
->audio_format_num
) < c
->filter_num_arg
)
103 return -E_BAD_GC_FILTER_NUM
;
105 if (c
->filter_num_arg
> max_num_filters())
106 return -E_BAD_GC_FILTER_NUM
;
111 static void add_inactive_gc(struct grab_client
*gc
)
113 PARA_INFO_LOG("adding grab client %p (fd %d) to inactive list\n",
115 para_list_add(&gc
->node
, &inactive_grab_client_list
);
118 static void gc_free(struct grab_client
*gc
)
122 for (i
= 0; i
< gc
->argc
; i
++)
130 static void gc_close(struct filter_callback
*fcb
)
132 struct grab_client
*gc
= fcb
->data
;
134 if (gc
->conf
->one_shot_given
|| gc
->error
) {
135 PARA_INFO_LOG("closing fd %d (grab client %p)\n", gc
->fd
, gc
);
145 * move a grab client from the inactive list to a filter node
147 * \param gc the grab client to activate
148 * \param fn the filter node \a gc gets attached to
150 * \sa filter_node::callbacks, inactive_grab_client_list
152 void activate_grab_client(struct grab_client
*gc
, struct filter_node
*fn
)
154 PARA_INFO_LOG("activating %p (fd %d, filter node: %p)\n", gc
, gc
->fd
, fn
);
156 para_list_add(&gc
->fcb
.node
, &fn
->callbacks
);
160 * activate inactive grab clients if possible
162 * \param slot_num audiod's slot for the new audio file
163 * \param audio_format_num the number of the audio format of the new audio file
164 * \param filter_list the list of activated filters for that new audio file
166 * This is called from audiod.c when the current audio file changes. It loops
167 * over all inactive grab clients and checks each grab client's configuration
168 * to determine if the client in question wishes to grab the new stream. If
169 * yes, this grab client is moved from the inactive grab client list to an
170 * appropriate filter_node.
172 * \sa filter_chain_info::filters, inactive_grab_client_list,
173 * activate_grab_client
175 void activate_inactive_grab_clients(int slot_num
, int audio_format_num
,
176 struct list_head
*filter_list
)
178 struct grab_client
*gc
, *tmp
;
180 struct filter_node
*fn
;
182 list_for_each_entry_safe(gc
, tmp
, &inactive_grab_client_list
, node
) {
183 // PARA_INFO_LOG("checking inactive grab client %p\n", gc);
184 if (gc
->conf
->slot_arg
>= 0 && gc
->conf
->slot_arg
!= slot_num
)
186 if (gc
->audio_format_num
>= 0 && gc
->audio_format_num
!=
189 if (gc
->conf
->filter_num_arg
>= 0 &&
190 num_filters(gc
->audio_format_num
)
191 < gc
->conf
->filter_num_arg
)
194 list_for_each_entry(fn
, filter_list
, node
) {
195 if (gc
->conf
->filter_num_arg
<= 0
196 || i
== gc
->conf
->filter_num_arg
)
200 activate_grab_client(gc
, fn
);
205 * check the command line options and allocate a grab_client structure
207 * \param fd the file descriptor of the client
208 * \param line the command line
209 * \param err non-zero if an error occured
211 * If the command line options given by \a argc and \a argv are valid.
212 * allocate a struct grab_client and initialize it with this valid
213 * configuration. Moreover, add the new grab client to the inactive list.
215 * \return On success, this function returns a pointer to the newly created
216 * struct. On errors, it returns NULL and sets \a err appropriately.
218 * \sa grab_client, inactive_grab_client_list, activate_grab_client,
219 * filter_node::callbacks
222 * argc, argv get freed when com_grab() returns, so we have to make a
225 struct grab_client
*grab_client_new(int fd
, char *line
, int *err
)
228 struct grab_client
*gc
= para_calloc(sizeof(struct grab_client
));
230 gc
->conf
= para_calloc(sizeof(struct grab_client_args_info
));
232 ret
= grab_client_cmdline_parser_string(line
, gc
->conf
, "grab");
236 *err
= -E_GC_HELP_GIVEN
;
237 if (gc
->conf
->help_given
)
239 *err
= -E_GC_VERSION_GIVEN
;
240 if (gc
->conf
->version_given
)
242 *err
= check_gc_args(gc
);
245 if (gc
->conf
->input_grab_given
) {
246 gc
->fcb
.input_cb
= gc_write
;
247 gc
->fcb
.output_cb
= NULL
;
249 gc
->fcb
.output_cb
= gc_write
;
250 gc
->fcb
.input_cb
= NULL
;
253 gc
->fcb
.close
= gc_close
;
263 /** initialize the grabbing subsystem.
265 * This has to be called once during startup before any other function from
266 * grab_client.c may be used. It initializes \a inactive_grab_client_list.
268 void init_grabbing(void)
270 PARA_INFO_LOG("%s", "grab init\n");
271 INIT_LIST_HEAD(&inactive_grab_client_list
);