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