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