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