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>
22 #include "grab_client.h"
28 /** Grab clients that are not yet attached to a filter node. */
29 struct list_head inactive_grab_client_list
;
31 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 struct filter_node
*find_filter_node(int format
, int filternum
)
49 struct slot_info
*s
= &slot
[i
];
50 if (s
->format
< 0 || !s
->fc
)
52 if (format
>= 0 && s
->format
!= format
)
54 if (num_filters(i
) <= filternum
)
57 return s
->fc
->filter_nodes
+ filternum
;
62 static int gc_write(char *buf
, size_t len
, struct filter_callback
*fcb
)
64 struct grab_client
*gc
= container_of(fcb
, struct grab_client
, fcb
);
67 while (written
< len
) {
68 int ret
= write_ok(gc
->fd
);
71 if (ret
== 0) { /* fd not ready */
72 if (gc
->mode
== GM_PEDANTIC
)
74 if (gc
->mode
== GM_SLOPPY
)
77 ret
= write(gc
->fd
, buf
+ written
, len
- written
);
79 if (errno
!= EAGAIN
&& errno
!= EINTR
)
81 if (gc
->mode
== GM_PEDANTIC
)
83 if (gc
->mode
== GM_SLOPPY
)
90 gc
->error
= -E_GC_WRITE
;
94 static void add_inactive_gc(struct grab_client
*gc
)
96 PARA_INFO_LOG("adding grab client %p (fd %d) to inactive list\n",
98 para_list_add(&gc
->node
, &inactive_grab_client_list
);
101 static void gc_close(struct filter_callback
*fcb
)
103 struct grab_client
*gc
= container_of(fcb
, struct grab_client
, fcb
);
105 if ((gc
->flags
& GF_ONE_SHOT
) || gc
->error
< 0) {
106 PARA_INFO_LOG("closing fd %d (grab client %p)\n", gc
->fd
, gc
);
115 * Move a grab client from the inactive list to a filter node.
117 * \param gc The grab client to activate.
118 * \param fn The filter node \a gc gets attached to.
120 * \sa filter_node::callbacks, inactive_grab_client_list.
122 void activate_grab_client(struct grab_client
*gc
, struct filter_node
*fn
)
124 PARA_INFO_LOG("activating %p (fd %d, filter node: %p)\n", gc
, gc
->fd
, fn
);
126 para_list_add(&gc
->fcb
.node
, &fn
->callbacks
);
130 * Activate inactive grab clients if possible.
132 * \param audio_format_num The number of the audio format of the new audio file.
133 * \param fc The filter chain containing the activated filters.
135 * This is called from audiod.c when the current audio file changes. It loops
136 * over all inactive grab clients and checks each grab client's configuration
137 * to determine if the client in question wishes to grab the new stream. If
138 * yes, this grab client is moved from the inactive grab client list to an
139 * appropriate filter_node.
141 * \sa filter_chain_info::filters, inactive_grab_client_list,
142 * activate_grab_client.
144 void activate_inactive_grab_clients(int audio_format_num
,
145 struct filter_chain
*fc
)
147 struct grab_client
*gc
, *tmp
;
148 struct filter_node
*fn
;
150 list_for_each_entry_safe(gc
, tmp
, &inactive_grab_client_list
, node
) {
151 if (gc
->audio_format_num
>= 0 && gc
->audio_format_num
!=
154 if (gc
->filter_num
>= num_filters(audio_format_num
))
156 fn
= fc
->filter_nodes
+ gc
->filter_num
;
157 activate_grab_client(gc
, fn
);
161 static int check_gc_args(int argc
, char **argv
, struct grab_client
*gc
)
165 gc
->audio_format_num
= -1; /* default: grab any audio format */
166 for (i
= 1; i
< argc
; i
++) {
167 const char *arg
= argv
[i
];
170 if (!strcmp(arg
, "--")) {
174 if (!strncmp(arg
, "-n=", 3)) {
175 ret
= para_atoi32(arg
+ 3, &gc
->filter_num
);
178 if (gc
->filter_num
< 0)
179 return -E_BAD_GC_FILTER_NUM
;
180 if (gc
->filter_num
>= max_num_filters())
181 return -E_BAD_GC_FILTER_NUM
;
184 if (!strncmp(arg
, "-m", 2)) {
189 gc
->mode
= GM_SLOPPY
;
192 gc
->mode
= GM_AGGRESSIVE
;
195 gc
->mode
= GM_PEDANTIC
;
201 if (!strcmp(arg
, "-i")) {
202 gc
->flags
|= GF_INPUT_GRAB
;
205 if (!strcmp(arg
, "-o")) {
206 gc
->flags
|= GF_ONE_SHOT
;
209 if (!strncmp(arg
, "-f=", 3)) {
210 ret
= get_audio_format_num(arg
+ 3);
213 gc
->audio_format_num
= ret
;
224 * Check the command line options and allocate a grab_client structure.
226 * \param fd The file descriptor of the client.
227 * \param argc Argument count.
228 * \param argv Argument vector.
230 * If the command line options given by \a argc and \a argv are valid.
231 * allocate a struct grab_client and initialize it with this valid
232 * configuration. Moreover, add the new grab client to the inactive list.
236 * \sa grab_client, inactive_grab_client_list, activate_grab_client,
237 * filter_node::callbacks.
239 int grab_client_new(int fd
, int argc
, char **argv
)
242 struct grab_client
*gc
= para_calloc(sizeof(struct grab_client
));
243 struct filter_node
*fn
;
245 ret
= check_gc_args(argc
, argv
, gc
);
248 if (gc
->flags
& GF_INPUT_GRAB
)
249 gc
->fcb
.input_cb
= gc_write
;
251 gc
->fcb
.output_cb
= gc_write
;
253 gc
->fcb
.close
= gc_close
;
254 fn
= find_filter_node(gc
->audio_format_num
, gc
->filter_num
);
256 para_list_add(&gc
->fcb
.node
, &fn
->callbacks
);
266 * Initialize the grabbing subsystem.
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.
271 void init_grabbing(void)
273 PARA_INFO_LOG("grab init\n");
274 INIT_LIST_HEAD(&inactive_grab_client_list
);