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