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