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