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