oss: Bail out on unsupported audio formats.
[paraslash.git] / send.h
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file send.h Sender-related defines and structures. */
4
5 #define SENDER_SUBCOMMANDS \
6         SENDER_SUBCOMMAND(add) /**< Add a target (udp only). */ \
7         SENDER_SUBCOMMAND(delete) /**< Delete a target (udp only). */ \
8         SENDER_SUBCOMMAND(allow) /**< Allow connections from given IP address(es). */ \
9         SENDER_SUBCOMMAND(deny) /**< Deny connections from given IP address(es). */ \
10         SENDER_SUBCOMMAND(on) /**< Activate the sender. */ \
11         SENDER_SUBCOMMAND(off) /**< Deactivate the sender. */ \
12
13 #define SENDER_SUBCOMMAND(_name) SENDER_ ## _name,
14 enum sender_subcommand {
15         SENDER_SUBCOMMANDS
16         NUM_SENDER_CMDS /**< Used as array size in struct \ref sender. */
17 };
18 #undef SENDER_SUBCOMMAND
19 #define SENDER_SUBCOMMAND(_name) #_name,
20
21 /**
22  * Describes one supported sender of para_server.
23  *
24  * \sa \ref http_send.c \ref udp_send.c, \ref dccp_send.c.
25  */
26 struct sender {
27         /** The name of the sender. */
28         const char *name;
29         /**
30          * Parse the command line options and initialize this sender (e.g.,
31          * initialize target or access control lists, listen on a network
32          * socket, etc.).
33          */
34         void (*init)(void);
35         /**
36          * Return the help text of this sender.
37          *
38          * The result must be dynamically allocated and is freed by the caller.
39          */
40         char* (*help)(void);
41         /**
42          * Return current status info about this sender.
43          *
44          * The result must be dynamically allocated and is freed by the caller.
45          */
46         char* (*status)(void);
47         /**
48          * The send-hook.
49          *
50          * It gets called whenever para_server is playing and the current
51          * audio format handler indicates that another chunk of data should
52          * be sent now. The two parameters \a current_chunk and \a chunks_sent
53          * only differ if the stream was repositioned by the \a ff or \a jmp
54          * command. Of course, \a buf is a pointer to the chunk of data which
55          * should be sent, and \a len is the length of this buffer.
56          */
57         void (*send)(long unsigned current_chunk, long unsigned chunks_sent,
58                 const char *buf, size_t len, const char *header_buf,
59                 size_t header_len);
60         /**
61          * Add file descriptors to fd_sets.
62          *
63          * The pre_select function of each supported sender is called just before
64          * para_server enters its main select loop. Each sender may add its own
65          * file descriptors to the \a rfds or the \a wfds set.
66          *
67          * If a file descriptor was added, \a max_fileno must be increased by
68          * this function, if necessary.
69          *
70          * \sa select(2).
71          */
72         void (*pre_select)(int *max_fileno, fd_set *rfds, fd_set *wfds);
73         /**
74          * Handle the file descriptors which are ready for I/O.
75          *
76          * If the pre_select hook added one ore more file descriptors to the
77          * read or write set, this is the hook to check the result and do any
78          * I/O on those descriptors which are ready for reading/writing.
79          */
80         void (*post_select)(fd_set *rfds, fd_set *wfds);
81         /**
82          * Terminate all connected clients.
83          *
84          * This is called e.g. if the stop command was executed. It should make
85          * the clients aware of the end-of-file condition.
86          */
87         void (*shutdown_clients)(void);
88         /** Dellocate all resources. Only called on exit. */
89         void (*shutdown)(void);
90         /**
91          * Array of function pointers for the sender subcommands.
92          *
93          * Each sender may implement any subset of the sender commands by
94          * filling in the appropriate function pointer in the array. A \p NULL
95          * pointer means this command is not implemented by this sender.
96          */
97         int (*client_cmds[NUM_SENDER_CMDS])(struct sender_command_data*);
98         /**
99          * Resolve target-specific URL string
100          *
101          * This method must be defined if the sender supports the add/delete
102          * subcommands. It interprets a string specifying a target URL in a
103          * sender-specific fashion (e.g. embedded FEC string). It can also
104          * fill in sender-specific defaults if necessary.
105          */
106         int (*resolve_target)(const char *, struct sender_command_data *);
107 };
108
109 /** NULL-terminated list, defined in \ref vss.c. */
110 extern const struct sender * const senders[];
111 /** Iterate over all senders. */
112 #define FOR_EACH_SENDER(_i) for ((_i) = 0; senders[(_i)]; (_i)++)
113
114 /** Describes one client, connected to a paraslash sender. */
115 struct sender_client {
116         /** The file descriptor of the client. */
117         int fd;
118         /** The socket "name" of the client. */
119         char *name;
120         /** The position of this client in the client list. */
121         struct list_head node;
122         /** Non-zero if audio file header has been sent. */
123         int header_sent;
124         /** The list of pending chunks for this client. */
125         struct chunk_queue *cq;
126         /** Data specific to the particular sender. */
127         void *private_data;
128 };
129
130 /**
131  * Each paraslash sender may register arbitrary many clients to the virtual
132  * streaming system, possibly with varying fec parameters. In order to do so,
133  * it must allocate a \a fec_client_parms structure and pass it to \ref
134  * vss_add_fec_client.
135  *
136  * Clients are automatically removed from that list by the vss if an error
137  * occurs, or if the sender requests deletion of a client by calling \ref
138  * vss_del_fec_client().
139  */
140
141 /** FEC parameters requested by FEC clients. */
142 struct fec_client_parms {
143         /** Number of data slices plus redundant slices. */
144         uint8_t slices_per_group;
145         /** Number of slices minus number of redundant slices. */
146         uint8_t data_slices_per_group;
147         /** Whether the header must be sent periodically. */
148         bool need_periodic_header;
149         /**
150          * Transport-layer initialisation for FEC support.
151          *
152          * This optional function serves (a) to make the transport layer
153          * ready to send FEC slices and (b) to determine the Maximum
154          * Packet Size (MPS) supported by the connection. The MPS value
155          * determines the largest payload size. This value is used to
156          * send FEC slices that are not larger than the path MTU, to avoid
157          * fragmentation and to maximize packet utilization. The user can
158          * alternatively specify a slice size of up to this value.
159          */
160         int (*init_fec)(struct sender_client *sc);
161         /** Push out FEC-encoded packets */
162         void (*send_fec)(struct sender_client *sc, char *buf, size_t len);
163 };
164
165 /** Describes the current status of one paraslash sender. */
166 struct sender_status {
167         /** Number of sockets to listen on, size of the two arrays below. */
168         unsigned num_listen_fds;
169         /** Derived from --http-listen-address and --dccp-listen-address. */
170         char **listen_addresses;
171         /** Default TCP/DCCP port number for addresses w/o port. */
172         int default_port;
173         /** The socket fd(s) this sender is listening on. */
174         int *listen_fds;
175         /** The current number of simultaneous connections. */
176         int num_clients;
177         /** The maximal number of simultaneous connections. */
178         int max_clients;
179         /** Whether the access control list is a whitelist. */
180         int default_deny;
181         /** The whitelist/blacklist. */
182         struct list_head acl;
183         /** The list of connected clients. */
184         struct list_head client_list;
185 };
186
187 /** Iterate over all listening addresses of the http/dccp sender. */
188 #define FOR_EACH_LISTEN_FD(_n, _ss) for (_n = 0; _n < (_ss)->num_listen_fds; _n++)
189
190 void shutdown_client(struct sender_client *sc, struct sender_status *ss);
191 void shutdown_clients(struct sender_status *ss);
192 void init_sender_status(struct sender_status *ss,
193                 const struct lls_opt_result *acl_opt_result,
194                 const struct lls_opt_result *listen_address_opt_result,
195                 int default_port, int max_clients, int default_deny);
196 char *generic_sender_status(struct sender_status *ss, const char *name);
197 void generic_com_allow(struct sender_command_data *scd,
198                 struct sender_status *ss);
199 void generic_com_deny(struct sender_command_data *scd,
200                 struct sender_status *ss);
201 void generic_com_on(struct sender_status *ss, unsigned protocol);
202 void generic_acl_deplete(struct list_head *acl);
203 void generic_com_off(struct sender_status *ss);
204 char *generic_sender_help(void);
205 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds);
206 int send_queued_chunks(int fd, struct chunk_queue *cq);
207 int parse_fec_url(const char *arg, struct sender_command_data *scd);