Rename error handler() to event_handler()
[paraslash.git] / ortp_send.c
1 /*
2  * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file ortp_send.c para_server's ortp sender */
20
21 #include <ortp/ortp.h>
22
23 #include "server.cmdline.h"
24 #include "server.h"
25 #include "afs.h"
26 #include "send.h"
27 #include "list.h"
28 #include "ortp.h"
29 #include "string.h"
30
31 /** \cond convert in_addr to ascii */
32 #define TARGET_ADDR(oc) inet_ntoa((oc)->addr)
33 /** \endcond */
34
35 extern struct gengetopt_args_info conf;
36
37 /** describes one entry in the list of targets for the ortp sender */
38 struct ortp_target {
39 /** address info */
40         struct in_addr addr;
41 /** whether the ortp sender is activated */
42         int status;
43 /** the ortp timestamp increases by this amount */
44         int chunk_ts;
45 /** the currently used timestamp for this target */
46         int last_ts;
47 /** the position of this target in the list of targets */
48         struct list_head node;
49 /** the UDP port */
50         int port;
51 /** non-zero if at least one chunk has been sent to this target */
52         int streaming;
53 /** the session pointer from libortp */
54         RtpSession *session;
55 };
56
57 static struct list_head targets;
58 static struct sender *self;
59
60 static void ortp_delete_target(struct ortp_target *ot, const char *msg)
61 {
62         PARA_NOTICE_LOG("deleting %s:%d (%s) from list\n", TARGET_ADDR(ot),
63                 ot->port, msg);
64         if (ot->session) {
65                 rtp_session_destroy(ot->session);
66                 ot->session = NULL;
67         }
68         list_del(&ot->node);
69         free(ot);
70 }
71
72 static void ortp_send_buf(char *buf, int len, long unsigned chunks_sent)
73 {
74         struct ortp_target *ot, *tmp;
75         int ret;
76
77         list_for_each_entry_safe(ot, tmp, &targets, node) {
78                 int ts;
79                 if (!ot->session)
80                         continue;
81                 WRITE_CHUNK_TS(buf, ot->chunk_ts);
82                 ts = ot->chunk_ts * chunks_sent;
83                 ret = rtp_session_send_with_ts(ot->session, buf, len, ts);
84                 ot->last_ts = ts;
85                 if (ret < 0)
86                         ortp_delete_target(ot, "send error");
87                 if (ret != len +12)
88                         PARA_NOTICE_LOG("short write %d\n", ret);
89         }
90 }
91
92 static void ortp_init_session(struct ortp_target *ot)
93 {
94         RtpSession *s;
95         int ret;
96
97         PARA_NOTICE_LOG("sending to udp %s:%d\n", TARGET_ADDR(ot), ot->port);
98         ot->session = rtp_session_new(RTP_SESSION_SENDONLY);
99         if (!ot->session)
100                 return;
101         s = ot->session;
102         if (conf.ortp_jitter_compensation_arg) {
103                 rtp_session_enable_adaptive_jitter_compensation(ot->session, TRUE);
104                 rtp_session_set_jitter_compensation(ot->session,
105                         conf.ortp_jitter_compensation_arg);
106         }
107         /* always successful */
108         rtp_session_set_send_payload_type(s, PAYLOAD_AUDIO_CONTINUOUS);
109         ret = rtp_session_set_remote_addr(s, TARGET_ADDR(ot), ot->port);
110         if (ret < 0) {
111                 rtp_session_destroy(ot->session);
112                 ot->session = NULL;
113         }
114 }
115
116 /* called by afs */
117 static void ortp_shutdown_targets(void)
118 {
119         char buf[ORTP_AUDIO_HEADER_LEN];
120         struct ortp_target *ot, *tmp;
121
122         WRITE_PACKET_TYPE(buf, ORTP_EOF);
123         list_for_each_entry_safe(ot, tmp, &targets, node) {
124                 if (!ot->session || !ot->streaming)
125                         continue;
126                 PARA_INFO_LOG("sending eof to ortp target %s:%d, ts = %d\n",
127                         TARGET_ADDR(ot), ot->port, ot->last_ts);
128                 rtp_session_send_with_ts(ot->session, buf,
129                         ORTP_AUDIO_HEADER_LEN, ot->last_ts);
130                 ot->streaming = 0;
131                 ot->chunk_ts = 0;
132                 rtp_session_reset(ot->session);
133         }
134 }
135
136 static int need_extra_header(long unsigned current_chunk)
137 {
138         static struct timeval last_header;
139         struct timeval now, diff;
140
141         if (!current_chunk)
142                 return 0;
143         gettimeofday(&now, NULL);
144         tv_diff(&now, &last_header, &diff);
145         if (tv2ms(&diff) < conf.ortp_header_interval_arg)
146                 return 0;
147         last_header = now;
148         return 1;
149 }
150
151 static void ortp_send(long unsigned current_chunk, long unsigned chunks_sent,
152                 const char *buf, size_t len)
153 {
154         struct ortp_target *ot, *tmp;
155         size_t sendbuf_len;
156         int header_len = 0;
157         int packet_type = ORTP_DATA;
158         char *sendbuf, *header_buf = NULL;
159         struct timeval *chunk_tv;
160
161         if (self->status != SENDER_ON)
162                 return;
163         chunk_tv = afs_chunk_time();
164         if (!chunk_tv)
165                 return;
166         list_for_each_entry_safe(ot, tmp, &targets, node) {
167                 if (!ot->session) {
168                         ortp_init_session(ot);
169                         if (!ot->session)
170                                 continue;
171                 }
172                 if (!ot->chunk_ts)
173                         ot->chunk_ts = rtp_session_time_to_ts(ot->session,
174                                 tv2ms(chunk_tv));
175 //              PARA_DEBUG_LOG("len: %d, ts: %lu, ts: %d\n",
176 //                      len, ot->chunk_ts * chunks_sent, ot->chunk_ts);
177                 ot->streaming = 1;
178         }
179         if (list_empty(&targets))
180                 return;
181         header_buf = afs_get_header(&header_len);
182         if (!need_extra_header(current_chunk))
183                 header_len = 0;
184         sendbuf_len = ORTP_AUDIO_HEADER_LEN + header_len + len;
185         sendbuf = para_malloc(sendbuf_len);
186         if (!current_chunk)
187                 packet_type = ORTP_BOF;
188         else if (header_len)
189                 packet_type = ORTP_HEADER;
190         WRITE_PACKET_TYPE(sendbuf, packet_type);
191         WRITE_CHUNK_TIME(sendbuf, chunk_tv->tv_usec);
192         WRITE_STREAM_TYPE(sendbuf,  header_buf? 1 : 0);
193         WRITE_HEADER_LEN(sendbuf, header_len);
194         if (header_len)
195                 memcpy(sendbuf + ORTP_AUDIO_HEADER_LEN, header_buf,
196                         header_len);
197         memcpy(sendbuf + ORTP_AUDIO_HEADER_LEN + header_len, buf, len);
198         ortp_send_buf(sendbuf, sendbuf_len, chunks_sent);
199         free(sendbuf);
200 }
201
202 static int ortp_com_on(__a_unused struct sender_command_data *scd)
203 {
204
205         self->status = SENDER_ON;
206         return 1;
207 }
208
209 static int ortp_com_off(__a_unused struct sender_command_data *scd)
210 {
211         ortp_shutdown_targets();
212         self->status = SENDER_OFF;
213         return 1;
214 }
215
216 static int ortp_com_delete(struct sender_command_data *scd)
217 {
218         char *a = para_strdup(inet_ntoa(scd->addr));
219         struct ortp_target *ot, *tmp;
220         list_for_each_entry_safe(ot, tmp, &targets, node) {
221                 if (scd->port != ot->port)
222                         continue;
223                 if (strcmp(TARGET_ADDR(ot), a))
224                         continue;
225                 ortp_delete_target(ot, "com_delete");
226         }
227         return 1;
228 }
229
230 static void ortp_add_target(int port, struct in_addr *addr)
231 {
232         struct ortp_target *ot = para_calloc(sizeof(struct ortp_target));
233         ot->port = port;
234         ot->addr = *addr;
235         PARA_INFO_LOG("adding to target list (%s:%d)\n",
236                 TARGET_ADDR(ot), ot->port);
237         list_add(&ot->node, &targets);
238 }
239
240 static int ortp_com_add(struct sender_command_data *scd)
241 {
242         int port = (scd->port > 0)? scd->port : conf.ortp_default_port_arg;
243         ortp_add_target(port, &scd->addr);
244         return 1;
245 }
246
247 static char *ortp_info(void)
248 {
249         struct ortp_target *ot;
250         char *ret, *tgts = NULL;
251
252         list_for_each_entry(ot, &targets, node) {
253                 char *tmp = make_message("%s%s:%d ", tgts? tgts : "",
254                         TARGET_ADDR(ot), ot->port);
255                 free(tgts);
256                 tgts = tmp;
257         }
258         ret = make_message(
259                 "ortp status: %s\n"
260                 "ortp default port: udp %d\n"
261                 "ortp targets: %s\n",
262                 (self->status == SENDER_ON)? "on" : "off",
263                 conf.ortp_default_port_arg,
264                 tgts? tgts : "(none)"
265         );
266         free(tgts);
267         return ret;
268 }
269
270 static void ortp_init_target_list(void)
271 {
272         int i;
273
274         INIT_LIST_HEAD(&targets);
275         for (i = 0; i < conf.ortp_target_given; i++) {
276                 char *arg = para_strdup(conf.ortp_target_arg[i]);
277                 char *p = strchr(arg, ':');
278                 int port;
279                 struct in_addr addr;
280
281                 if (!p)
282                         goto err;
283                 *p = '\0';
284                 if (!inet_aton(arg, &addr))
285                         goto err;
286                 port = atoi(++p);
287                 if (port < 0 || port > 65535)
288                         port = conf.ortp_default_port_arg;
289                 ortp_add_target(port, &addr);
290                 goto success;
291 err:
292                 PARA_CRIT_LOG("syntax error for ortp_target option "
293                         "#%d, ignoring\n", i);
294 success:
295                 free(arg);
296                 continue;
297         }
298 }
299
300 static void ortp_pre_select(__a_unused  int *max_fileno, __a_unused fd_set *rfds,
301         __a_unused fd_set *wfds)
302 {
303         return;
304 }
305
306 static char *ortp_help(void)
307 {
308         return make_message(
309                 "usage: {on|off}\n"
310                 "usage: {add|delete} IP port\n"
311                 "example: add 224.0.1.38 1500 (LAN-streaming)\n"
312         );
313 }
314
315 /**
316  * the init function of para_server's ortp sender
317  *
318  * \param s pointer to the http sender struct
319  *
320  * It initializes all function pointers of \a s and the list of ortp targets.
321  */
322 void ortp_send_init(struct sender *s)
323 {
324         ortp_init();
325         INIT_LIST_HEAD(&targets);
326         s->info = ortp_info;
327         s->help = ortp_help;
328         s->send = ortp_send;
329         s->pre_select = ortp_pre_select;
330         s->post_select = NULL;
331         s->shutdown_clients = ortp_shutdown_targets;
332         s->client_cmds[SENDER_ON] = ortp_com_on;
333         s->client_cmds[SENDER_OFF] = ortp_com_off;
334         s->client_cmds[SENDER_DENY] = NULL;
335         s->client_cmds[SENDER_ALLOW] = NULL;
336         s->client_cmds[SENDER_ADD] = ortp_com_add;
337         s->client_cmds[SENDER_DELETE] = ortp_com_delete;
338         self = s;
339         s->status = SENDER_OFF;
340         ortp_init_target_list();
341         if (!conf.ortp_no_autostart_given)
342                 s->status = SENDER_ON;
343         PARA_DEBUG_LOG("%s", "ortp sender init complete\n");
344 }