2 * Copyright (C) 2006-2009 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"
23 #include "grab_client.h"
30 /** Grab clients that are not yet attached to a filter node. */
31 struct list_head inactive_grab_client_list
;
33 static int max_num_filters(void)
36 for (i
= 0; audio_formats
[i
]; i
++) {
37 PARA_INFO_LOG("%s filter chain length: %d\n", audio_formats
[i
],
39 ret
= PARA_MAX(ret
, num_filters(i
));
41 PARA_INFO_LOG("maximal filter chain length: %d\n", ret
);
45 static int gc_write(char *buf
, size_t len
, struct filter_callback
*fcb
)
47 struct grab_client
*gc
= fcb
->data
;
48 struct timeval tv
= {0, 100};
51 // PARA_INFO_LOG("writing %d bytes to fd %d\n", len, gc->fd);
54 FD_SET(gc
->fd
, &wfds
);
55 ret
= para_select(gc
->fd
+ 1, NULL
, &wfds
, &tv
);
57 if (gc
->mode
== GRAB_PEDANTIC
)
58 return -E_PEDANTIC_GRAB
;
59 if (gc
->mode
== GRAB_SLOPPY
)
63 ret
= write(gc
->fd
, buf
, len
);
66 gc
->error
= E_GC_WRITE
;
69 if (gc
->mode
== GRAB_PEDANTIC
)
70 return -E_PEDANTIC_GRAB
;
71 if (gc
->mode
== GRAB_AGGRESSIVE
) {
73 memmove(buf
, buf
+ ret
, len
);
81 /* TODO: gengetopt can handle the grab client modes */
82 static int check_gc_args(struct grab_client
*gc
)
85 struct grab_client_args_info
*c
= gc
->conf
;
86 const char **mv
= grab_client_cmdline_parser_mode_values
;
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
]))
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
;
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
;
109 if (c
->filter_num_arg
>= max_num_filters())
110 return -E_BAD_GC_FILTER_NUM
;
115 static void add_inactive_gc(struct grab_client
*gc
)
117 PARA_INFO_LOG("adding grab client %p (fd %d) to inactive list\n",
119 para_list_add(&gc
->node
, &inactive_grab_client_list
);
122 static void gc_free(struct grab_client
*gc
)
126 for (i
= 0; i
< gc
->argc
; i
++)
134 static void gc_close(struct filter_callback
*fcb
)
136 struct grab_client
*gc
= fcb
->data
;
138 if (gc
->conf
->one_shot_given
|| gc
->error
) {
139 PARA_INFO_LOG("closing fd %d (grab client %p)\n", gc
->fd
, gc
);
149 * Move a grab client from the inactive list to a filter node.
151 * \param gc The grab client to activate.
152 * \param fn The filter node \a gc gets attached to.
154 * \sa filter_node::callbacks, inactive_grab_client_list.
156 void activate_grab_client(struct grab_client
*gc
, struct filter_node
*fn
)
158 PARA_INFO_LOG("activating %p (fd %d, filter node: %p)\n", gc
, gc
->fd
, fn
);
160 para_list_add(&gc
->fcb
.node
, &fn
->callbacks
);
164 * Activate inactive grab clients if possible.
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.
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.
176 * \sa filter_chain_info::filters, inactive_grab_client_list,
177 * activate_grab_client.
179 void activate_inactive_grab_clients(int slot_num
, int audio_format_num
,
180 struct filter_chain
*fc
)
182 struct grab_client
*gc
, *tmp
;
184 struct filter_node
*fn
;
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
)
190 if (gc
->audio_format_num
>= 0 && gc
->audio_format_num
!=
193 filter_num
= gc
->conf
->filter_num_arg
;
194 if (filter_num
>= num_filters(gc
->audio_format_num
))
196 fn
= fc
->filter_nodes
+ filter_num
;
197 activate_grab_client(gc
, fn
);
202 * Check the command line options and allocate a grab_client structure.
204 * \param fd The file descriptor of the client.
205 * \param line The command line.
206 * \param err Non-zero if an error occurred.
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.
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.
215 * \sa grab_client, inactive_grab_client_list, activate_grab_client,
216 * filter_node::callbacks.
219 * argc, argv get freed when com_grab() returns, so we have to make a
222 struct grab_client
*grab_client_new(int fd
, char *line
, int *err
)
225 struct grab_client
*gc
= para_calloc(sizeof(struct grab_client
));
227 gc
->conf
= para_calloc(sizeof(struct grab_client_args_info
));
229 ret
= grab_client_cmdline_parser_string(line
, gc
->conf
, "grab");
233 *err
= -E_GC_HELP_GIVEN
;
234 if (gc
->conf
->help_given
)
236 *err
= -E_GC_VERSION_GIVEN
;
237 if (gc
->conf
->version_given
)
239 *err
= check_gc_args(gc
);
242 if (gc
->conf
->input_grab_given
) {
243 gc
->fcb
.input_cb
= gc_write
;
244 gc
->fcb
.output_cb
= NULL
;
246 gc
->fcb
.output_cb
= gc_write
;
247 gc
->fcb
.input_cb
= NULL
;
250 gc
->fcb
.close
= gc_close
;
261 * Initialize the grabbing subsystem.
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.
266 void init_grabbing(void)
268 PARA_INFO_LOG("grab init\n");
269 INIT_LIST_HEAD(&inactive_grab_client_list
);