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.
15 #include <sys/types.h>
23 #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)
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 struct filter_node
*find_filter_node(int format
, int filternum
)
50 struct slot_info
*s
= &slot
[i
];
51 if (s
->format
< 0 || !s
->fc
)
53 if (format
>= 0 && s
->format
!= format
)
55 if (num_filters(i
) <= filternum
)
58 return s
->fc
->filter_nodes
+ filternum
;
63 static int gc_write(char *buf
, size_t len
, struct filter_callback
*fcb
)
65 struct grab_client
*gc
= container_of(fcb
, struct grab_client
, fcb
);
68 while (written
< len
) {
69 int ret
= write_ok(gc
->fd
);
72 if (ret
== 0) { /* fd not ready */
73 if (gc
->mode
== GM_PEDANTIC
)
75 if (gc
->mode
== GM_SLOPPY
)
78 ret
= write(gc
->fd
, buf
+ written
, len
- written
);
80 if (errno
!= EAGAIN
&& errno
!= EINTR
)
82 if (gc
->mode
== GM_PEDANTIC
)
84 if (gc
->mode
== GM_SLOPPY
)
91 gc
->error
= -E_GC_WRITE
;
95 static void add_inactive_gc(struct grab_client
*gc
)
97 PARA_INFO_LOG("adding grab client %p (fd %d) to inactive list\n",
99 para_list_add(&gc
->node
, &inactive_grab_client_list
);
102 static void gc_close(struct filter_callback
*fcb
)
104 struct grab_client
*gc
= container_of(fcb
, struct grab_client
, fcb
);
106 if ((gc
->flags
& GF_ONE_SHOT
) || gc
->error
< 0) {
107 PARA_INFO_LOG("closing fd %d (grab client %p)\n", gc
->fd
, gc
);
116 * Move a grab client from the inactive list to a filter node.
118 * \param gc The grab client to activate.
119 * \param fn The filter node \a gc gets attached to.
121 * \sa filter_node::callbacks, inactive_grab_client_list.
123 void activate_grab_client(struct grab_client
*gc
, struct filter_node
*fn
)
125 PARA_INFO_LOG("activating %p (fd %d, filter node: %p)\n", gc
, gc
->fd
, fn
);
127 para_list_add(&gc
->fcb
.node
, &fn
->callbacks
);
131 * Activate inactive grab clients if possible.
133 * \param audio_format_num The number of the audio format of the new audio file.
134 * \param fc The filter chain containing the activated filters.
136 * This is called from audiod.c when the current audio file changes. It loops
137 * over all inactive grab clients and checks each grab client's configuration
138 * to determine if the client in question wishes to grab the new stream. If
139 * yes, this grab client is moved from the inactive grab client list to an
140 * appropriate filter_node.
142 * \sa filter_chain_info::filters, inactive_grab_client_list,
143 * activate_grab_client.
145 void activate_inactive_grab_clients(int audio_format_num
,
146 struct filter_chain
*fc
)
148 struct grab_client
*gc
, *tmp
;
149 struct filter_node
*fn
;
151 list_for_each_entry_safe(gc
, tmp
, &inactive_grab_client_list
, node
) {
152 if (gc
->audio_format_num
>= 0 && gc
->audio_format_num
!=
155 if (gc
->filter_num
>= num_filters(audio_format_num
))
157 fn
= fc
->filter_nodes
+ gc
->filter_num
;
158 activate_grab_client(gc
, fn
);
162 static int check_gc_args(int argc
, char **argv
, struct grab_client
*gc
)
166 gc
->audio_format_num
= -1; /* default: grab any audio format */
167 for (i
= 1; i
< argc
; i
++) {
168 const char *arg
= argv
[i
];
171 if (!strcmp(arg
, "--")) {
175 if (!strncmp(arg
, "-n=", 3)) {
176 ret
= para_atoi32(arg
+ 3, &gc
->filter_num
);
179 if (gc
->filter_num
< 0)
180 return -E_BAD_GC_FILTER_NUM
;
181 if (gc
->filter_num
>= max_num_filters())
182 return -E_BAD_GC_FILTER_NUM
;
185 if (!strncmp(arg
, "-m", 2)) {
190 gc
->mode
= GM_SLOPPY
;
193 gc
->mode
= GM_AGGRESSIVE
;
196 gc
->mode
= GM_PEDANTIC
;
202 if (!strcmp(arg
, "-i")) {
203 gc
->flags
|= GF_INPUT_GRAB
;
206 if (!strcmp(arg
, "-o")) {
207 gc
->flags
|= GF_ONE_SHOT
;
210 if (!strncmp(arg
, "-f=", 3)) {
211 ret
= get_audio_format_num(arg
+ 3);
214 gc
->audio_format_num
= ret
;
225 * Check the command line options and allocate a grab_client structure.
227 * \param fd The file descriptor of the client.
228 * \param argc Argument count.
229 * \param argv Argument vector.
231 * If the command line options given by \a argc and \a argv are valid.
232 * allocate a struct grab_client and initialize it with this valid
233 * configuration. Moreover, add the new grab client to the inactive list.
237 * \sa grab_client, inactive_grab_client_list, activate_grab_client,
238 * filter_node::callbacks.
240 int grab_client_new(int fd
, int argc
, char **argv
)
243 struct grab_client
*gc
= para_calloc(sizeof(struct grab_client
));
244 struct filter_node
*fn
;
246 ret
= check_gc_args(argc
, argv
, gc
);
249 if (gc
->flags
& GF_INPUT_GRAB
)
250 gc
->fcb
.input_cb
= gc_write
;
252 gc
->fcb
.output_cb
= gc_write
;
254 gc
->fcb
.close
= gc_close
;
255 fn
= find_filter_node(gc
->audio_format_num
, gc
->filter_num
);
257 para_list_add(&gc
->fcb
.node
, &fn
->callbacks
);
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("grab init\n");
275 INIT_LIST_HEAD(&inactive_grab_client_list
);