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