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