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