vss: add state variable for fec client
[paraslash.git] / send.h
1 /*
2 * Copyright (C) 2005-2010 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file send.h Sender-related defines and structures. */
8
9 /** The sender subcommands. */
10 enum {SENDER_ADD, SENDER_DELETE, SENDER_ALLOW, SENDER_DENY, SENDER_ON, SENDER_OFF, NUM_SENDER_CMDS};
11
12 /**
13 * Each paraslash sender may register arbitrary many clients to the virtual
14 * streaming system, possibly with varying fec parameters. In order to do so,
15 * it must allocate a \a fec_client_parms structure and pass it to \ref
16 * add_fec_client.
17 *
18 * Clients are automatically removed from that list by the vss if an error
19 * occurs, or if the sender requests deletion of a client by calling \ref
20 * vss_del_fec_client().
21 */
22 struct fec_client;
23 struct sender_client;
24
25 /** FEC parameters requested by FEC clients. */
26 struct fec_client_parms {
27 /** Number of data slices plus redundant slices. */
28 uint8_t slices_per_group;
29 /** Number of slices minus number of redundant slices. */
30 uint8_t data_slices_per_group;
31 /** Maximal number of bytes per slice, initially zero. */
32 uint16_t max_slice_bytes;
33 /**
34 * Transport-layer initialisation for FEC support.
35 *
36 * This optional function serves (a) to make the transport layer
37 * ready to send FEC slices and (b) to determine the Maximum
38 * Packet Size (MPS) supported by the connection. The MPS value
39 * determines the largest payload size. This value is used to
40 * send FEC slices that are not larger than the path MTU, to avoid
41 * fragmentation and to maximize packet utilization. The user can
42 * alternatively specify a slice size of up to this value.
43 */
44 int (*init_fec)(struct sender_client *sc);
45 /** Push out FEC-encoded packets */
46 int (*send_fec)(struct sender_client *sc, char *buf, size_t len);
47 };
48
49 /**
50 * Describes one supported sender of para_server.
51 *
52 * \sa http_send.c udp_send.c, dccp_send.c.
53 */
54 struct sender {
55 /** The name of the sender. */
56 const char *name;
57 /**
58 * The init function of this sender.
59 *
60 * It must fill in all function pointers of \a s as well as the \a
61 * client_cmds array, see below. It should also do all necessary
62 * preparations to init this sending facility, for example it could
63 * open a tcp port.
64 */
65 void (*init)(struct sender *s);
66 /**
67 * Return the help text of this sender.
68 *
69 * The result must be dynamically allocated and is freed by the caller.
70 */
71 char* (*help)(void);
72 /**
73 * Return current status info about this sender.
74 *
75 * The result must be dynamically allocated and is freed by the caller.
76 */
77 char* (*info)(void);
78 /**
79 * The send-hook.
80 *
81 * It gets called whenever para_server is playing and the current
82 * audio format handler indicates that another chunk of data should
83 * be sent now. The two parameters \a current_chunk and \a chunks_sent
84 * only differ if the stream was repositioned by the \a ff or \a jmp
85 * command. Of course, \a buf is a pointer to the chunk of data which
86 * should be sent, and \a len is the length of this buffer.
87 */
88 void (*send)(long unsigned current_chunk, long unsigned chunks_sent,
89 const char *buf, size_t len, const char *header_buf,
90 size_t header_len);
91
92 /**
93 * Obtain the FEC parameters of a FEC client.
94 *
95 * This is called once by vss.c at the beginning of a stream. Senders
96 * are supposed to set \a fcp to a struct which is suitable for the FEC
97 * client identified by \a private_data.
98 */
99 int (*open)(void *client, struct fec_client_parms **fcp);
100 /**
101 * Send the next slice to a FEC client.
102 *
103 * Called by vss.c when the next slice should be sent to the FEC client
104 * identified by \a private_data, the pointer which was previously
105 * passed to vss_add_fec_target().
106 */
107 int (*send_fec)(char *buf, size_t num_bytes, void *private_data);
108 /**
109 * Add file descriptors to fd_sets.
110 *
111 * The pre_select function of each supported sender is called just before
112 * para_server enters its main select loop. Each sender may add its own
113 * file descriptors to the \a rfds or the \a wfds set.
114 *
115 * If a file descriptor was added, \a max_fileno must be increased by
116 * this function, if necessary.
117 *
118 * \sa select(2).
119 */
120 void (*pre_select)(int *max_fileno, fd_set *rfds, fd_set *wfds);
121 /**
122 * Handle the file descriptors which are ready for I/O.
123 *
124 * If the pre_select hook added one ore more file descriptors to the
125 * read or write set, this is the hook to check the result and do any
126 * I/O on those descriptors which are ready for reading/writing.
127 */
128 void (*post_select)(fd_set *rfds, fd_set *wfds);
129 /**
130 * Terminate all connected clients.
131 *
132 * This is called e.g. if the stop command was executed. It should make
133 * the clients aware of the end-of-file condition.
134 */
135 void (*shutdown_clients)(void);
136 /**
137 * Array of function pointers for the sender subcommands.
138 *
139 * Each sender may implement any subset of the sender commands by
140 * filling in the appropriate function pointer in the array. A \p NULL
141 * pointer means this command is not implemented by this sender.
142 */
143 int (*client_cmds[NUM_SENDER_CMDS])(struct sender_command_data*);
144 };
145
146 /** Describes one client, connected to a paraslash sender. */
147 struct sender_client {
148 /** The file descriptor of the client. */
149 int fd;
150 /** The socket "name" of the client. */
151 char *name;
152 /** The position of this client in the client list. */
153 struct list_head node;
154 /** Non-zero if audio file header has been sent. */
155 int header_sent;
156 /** The list of pending chunks for this client. */
157 struct chunk_queue *cq;
158 /** Data specific to the particular sender. */
159 void *private_data;
160 };
161
162 /** Describes the current status of one paraslash sender. */
163 struct sender_status {
164 /** The file descriptor of the socket this sender is listening on. */
165 int listen_fd;
166 /** The TCP/DCCP port used by this sender. */
167 int port;
168 /** The current number of simultaneous connections. */
169 int num_clients;
170 /** The maximal number of simultaneous connections. */
171 int max_clients;
172 /** Whether the access control list is a whitelist. */
173 int default_deny;
174 /** The whitelist/blacklist. */
175 struct list_head acl;
176 /** The list of connected clients. */
177 struct list_head client_list;
178 };
179
180 void shutdown_client(struct sender_client *sc, struct sender_status *ss);
181 void shutdown_clients(struct sender_status *ss);
182 void send_chunk(struct sender_client *sc, struct sender_status *ss,
183 size_t max_bytes_per_write, long unsigned current_chunk,
184 const char *buf, size_t len, const char *header_buf,
185 size_t header_len);
186 void init_sender_status(struct sender_status *ss, char **access_arg, int num_access_args,
187 int port, int max_clients, int default_deny);
188 char *get_sender_info(struct sender_status *ss, const char *name);
189
190 void generic_com_allow(struct sender_command_data *scd,
191 struct sender_status *ss);
192 void generic_com_deny(struct sender_command_data *scd,
193 struct sender_status *ss);
194 int generic_com_on(struct sender_status *ss, unsigned protocol);
195 void generic_com_off(struct sender_status *ss);
196 char *generic_sender_help(void);
197 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds);
198 int send_queued_chunks(int fd, struct chunk_queue *cq,
199 size_t max_bytes_per_write);
200 int parse_fec_url(const char *arg, struct sender_command_data *scd);