2 * Copyright (C) 2006-2007 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.
14 #include <sys/types.h>
18 #include "grab_client.cmdline.h"
22 #include "grab_client.h"
29 /** Grab clients that are not yet attached to a filter node. */
30 struct list_head inactive_grab_client_list
;
32 static int max_num_filters(void)
35 for (i
= 0; audio_formats
[i
]; i
++) {
36 PARA_INFO_LOG("%s filter chain length: %d\n", audio_formats
[i
],
38 ret
= PARA_MAX(ret
, num_filters(i
));
40 PARA_INFO_LOG("maximal filter chain length: %d\n", ret
);
44 static int gc_write(char *buf
, size_t len
, struct filter_callback
*fcb
)
46 struct grab_client
*gc
= fcb
->data
;
47 struct timeval tv
= {0, 100};
50 // PARA_INFO_LOG("writing %d bytes to fd %d\n", len, gc->fd);
53 FD_SET(gc
->fd
, &wfds
);
54 ret
= para_select(gc
->fd
+ 1, NULL
, &wfds
, &tv
);
56 if (gc
->mode
== GRAB_PEDANTIC
)
57 return -E_PEDANTIC_GRAB
;
58 if (gc
->mode
== GRAB_SLOPPY
)
62 ret
= write(gc
->fd
, buf
, len
);
65 gc
->error
= E_GC_WRITE
;
68 if (gc
->mode
== GRAB_PEDANTIC
)
69 return -E_PEDANTIC_GRAB
;
70 if (gc
->mode
== GRAB_AGGRESSIVE
) {
72 memmove(buf
, buf
+ ret
, len
);
80 /* TODO: gengetopt can handle the grab client modes */
81 static int check_gc_args(struct grab_client
*gc
)
84 struct grab_client_args_info
*c
= gc
->conf
;
85 char **mv
= grab_client_cmdline_parser_mode_values
;
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
]))
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
;
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
;
108 if (c
->filter_num_arg
> max_num_filters())
109 return -E_BAD_GC_FILTER_NUM
;
114 static void add_inactive_gc(struct grab_client
*gc
)
116 PARA_INFO_LOG("adding grab client %p (fd %d) to inactive list\n",
118 para_list_add(&gc
->node
, &inactive_grab_client_list
);
121 static void gc_free(struct grab_client
*gc
)
125 for (i
= 0; i
< gc
->argc
; i
++)
133 static void gc_close(struct filter_callback
*fcb
)
135 struct grab_client
*gc
= fcb
->data
;
137 if (gc
->conf
->one_shot_given
|| gc
->error
) {
138 PARA_INFO_LOG("closing fd %d (grab client %p)\n", gc
->fd
, gc
);
148 * Move a grab client from the inactive list to a filter node.
150 * \param gc The grab client to activate.
151 * \param fn The filter node \a gc gets attached to.
153 * \sa filter_node::callbacks, inactive_grab_client_list.
155 void activate_grab_client(struct grab_client
*gc
, struct filter_node
*fn
)
157 PARA_INFO_LOG("activating %p (fd %d, filter node: %p)\n", gc
, gc
->fd
, fn
);
159 para_list_add(&gc
->fcb
.node
, &fn
->callbacks
);
163 * Activate inactive grab clients if possible.
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.
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.
175 * \sa filter_chain_info::filters, inactive_grab_client_list,
176 * activate_grab_client.
178 void activate_inactive_grab_clients(int slot_num
, int audio_format_num
,
179 struct list_head
*filter_list
)
181 struct grab_client
*gc
, *tmp
;
183 struct filter_node
*fn
;
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
)
189 if (gc
->audio_format_num
>= 0 && gc
->audio_format_num
!=
192 if (gc
->conf
->filter_num_arg
>= 0 &&
193 num_filters(gc
->audio_format_num
)
194 < gc
->conf
->filter_num_arg
)
197 list_for_each_entry(fn
, filter_list
, node
) {
198 if (gc
->conf
->filter_num_arg
<= 0
199 || i
== gc
->conf
->filter_num_arg
)
203 activate_grab_client(gc
, fn
);
208 * Check the command line options and allocate a grab_client structure.
210 * \param fd The file descriptor of the client.
211 * \param line The command line.
212 * \param err Non-zero if an error occurred.
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.
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.
221 * \sa grab_client, inactive_grab_client_list, activate_grab_client,
222 * filter_node::callbacks.
225 * argc, argv get freed when com_grab() returns, so we have to make a
228 struct grab_client
*grab_client_new(int fd
, char *line
, int *err
)
231 struct grab_client
*gc
= para_calloc(sizeof(struct grab_client
));
233 gc
->conf
= para_calloc(sizeof(struct grab_client_args_info
));
235 ret
= grab_client_cmdline_parser_string(line
, gc
->conf
, "grab");
239 *err
= -E_GC_HELP_GIVEN
;
240 if (gc
->conf
->help_given
)
242 *err
= -E_GC_VERSION_GIVEN
;
243 if (gc
->conf
->version_given
)
245 *err
= check_gc_args(gc
);
248 if (gc
->conf
->input_grab_given
) {
249 gc
->fcb
.input_cb
= gc_write
;
250 gc
->fcb
.output_cb
= NULL
;
252 gc
->fcb
.output_cb
= gc_write
;
253 gc
->fcb
.input_cb
= NULL
;
256 gc
->fcb
.close
= gc_close
;
267 * Initialize the grabbing subsystem.
269 * This has to be called once during startup before any other function from
270 * grab_client.c may be used. It initializes \a inactive_grab_client_list.
272 void init_grabbing(void)
274 PARA_INFO_LOG("%s", "grab init\n");
275 INIT_LIST_HEAD(&inactive_grab_client_list
);