cosmetics
[paraslash.git] / ortp_recv.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 /** \file ortp_recv.c paraslash's ortp receiver */
19
20 #include <ortp/ortp.h>
21 #include "para.h"
22
23 #include "ortp.h"
24 #include "list.h"
25 #include "sched.h"
26 #include "recv.h"
27 #include "ortp_recv.cmdline.h"
28
29 #include "error.h"
30 #include "audiod.h"
31 #include "string.h"
32
33 #define CHUNK_SIZE 128 * 1024
34
35 /**
36 * data specific to the ortp receiver
37 *
38 * \sa receiver receiver_node
39 */
40 struct private_ortp_recv_data {
41 /**
42 *
43 *
44 * whether a header was received
45 *
46 * A flag indicating whether the ortp receiver already received a packet which
47 * contains the audio file header.
48 *
49 * This flag has no effect if the audio stream indicates that no extra headers
50 * will be sent (mp3). Otherwise, all data packets are dropped until the
51 * header is received.
52 */
53 int have_header;
54 /** the ortp session this instance of the receiver belongs to */
55 RtpSession *session;
56 /** the time the first data or header packet was received */
57 struct timeval start;
58 /** ETA of the next chunk */
59 struct timeval next_chunk;
60 /** number of consecutive bad chunks */
61 int c_bad;
62 /** the current timestamp which is passed to the receiving function of libortp */
63 uint32_t timestamp;
64 /** \a timestamp increases by this amount */
65 uint32_t chunk_ts;
66 };
67
68
69 static int msg_to_buf(mblk_t *mp, char *buffer, int len)
70 {
71 int rlen = len;
72 mblk_t *m, *mprev;
73 int mlen;
74
75 m = mp->b_cont;
76 mprev = mp;
77 while (m != NULL) {
78 mlen = (int) (m->b_wptr - m->b_rptr);
79 if (mlen <= rlen) {
80 mblk_t *consumed = m;
81 memcpy (buffer, m->b_rptr, mlen);
82 /* go to next mblk_t */
83 mprev->b_cont = m->b_cont;
84 m = m->b_cont;
85 consumed->b_cont = NULL;
86 freeb (consumed);
87 buffer += mlen;
88 rlen -= mlen;
89 } else { /*if mlen>rlen */
90 memcpy (buffer, m->b_rptr, rlen);
91 m->b_rptr += rlen;
92 return len;
93 }
94 }
95 return len - rlen;
96 }
97
98
99 static void ortp_recv_pre_select(struct sched *s, struct task *t)
100 {
101 struct receiver_node *rn = t->private_data;
102 struct private_ortp_recv_data *pord = rn->private_data;
103 struct timeval tmp;
104
105 if (tv_diff(now, &pord->next_chunk, &tmp) >= 0) {
106 tmp.tv_sec = 0;
107 tmp.tv_usec = 1000;
108 }
109 if (tv_diff(&s->timeout, &tmp, NULL) > 0)
110 s->timeout = tmp;
111 t->ret = 1;
112 }
113
114 static void compute_next_chunk(unsigned chunk_time,
115 struct private_ortp_recv_data *pord)
116 {
117 struct timeval chunk_tv = {0, chunk_time};
118 struct timeval tmp;
119
120 tv_add(&chunk_tv, &pord->next_chunk, &tmp);
121 pord->next_chunk = tmp;
122 pord->timestamp += pord->chunk_ts;
123 // PARA_DEBUG_LOG("next chunk (ts = %d) due at %lu:%lu\n",
124 // pord->timestamp, pord->next_chunk.tv_sec,
125 // pord->next_chunk.tv_usec);
126 }
127
128 static void ortp_recv_post_select(__a_unused struct sched *s, struct task *t)
129 {
130 struct receiver_node *rn = t->private_data;
131 struct private_ortp_recv_data *pord = rn->private_data;
132 mblk_t *mp;
133 int packet_type, stream_type;
134 char tmpbuf[CHUNK_SIZE + 3];
135 unsigned chunk_time;
136
137 // PARA_INFO_LOG("rn: %p, pord: %p, session: %p\n", rn, pord, pord->session);
138 t->ret = -E_ORTP_RECV_EOF;
139 if (rn->output_eof && *rn->output_eof) {
140 rn->eof = 1;
141 return;
142 }
143 t->ret = 1;
144 if (pord->start.tv_sec)
145 if (tv_diff(now, &pord->next_chunk, NULL) < 0)
146 return;
147 mp = rtp_session_recvm_with_ts(pord->session, pord->timestamp);
148 if (!mp) {
149 struct timeval min_delay = {0, 100};
150 // PARA_INFO_LOG("nope, chunk_ts = %d, loaded: %d, bad: %d\n",
151 // pord->timestamp, rn->loaded, pord->c_bad);
152 pord->c_bad++;
153 t->ret = -E_TOO_MANY_BAD_CHUNKS;
154 if ((pord->c_bad > 5000 && pord->start.tv_sec) || pord->c_bad > 10000)
155 return;
156 t->ret = 1;
157 tv_add(now, &min_delay, &pord->next_chunk);
158 return;
159 }
160 /* okay, we have a chunk of data */
161 if (!pord->start.tv_sec)
162 pord->start = *now;
163 t->ret = msg_to_buf(mp, tmpbuf, CHUNK_SIZE);
164 if (t->ret < ORTP_AUDIO_HEADER_LEN) {
165 rn->eof = 1;
166 if (t->ret < 0)
167 t->ret = -E_MSG_TO_BUF;
168 else
169 t->ret = -E_ORTP_RECV_EOF;
170 goto err_out;
171 }
172 packet_type = READ_PACKET_TYPE(tmpbuf);
173 stream_type = READ_STREAM_TYPE(tmpbuf);
174 chunk_time = READ_CHUNK_TIME(tmpbuf);
175 pord->chunk_ts = READ_CHUNK_TS(tmpbuf);
176 // PARA_INFO_LOG("packet type: %d, stream type: %d, chunk time: %u,"
177 // " chunk_ts: %u, loaded: %u, bad: %d\n", packet_type,
178 // stream_type, chunk_time, pord->chunk_ts, rn->loaded, pord->c_bad);
179 switch (packet_type) {
180 unsigned header_len, payload_len;
181 case ORTP_EOF:
182 rn->eof = 1;
183 t->ret = -E_ORTP_RECV_EOF;
184 goto err_out;
185 case ORTP_BOF:
186 PARA_INFO_LOG("bof (%d)\n", t->ret);
187 pord->have_header = 1;
188 /* fall through */
189 case ORTP_DATA:
190 if (!pord->have_header && stream_type)
191 /* can't use the data, wait for header */
192 goto success;
193 if (t->ret + rn->loaded >= CHUNK_SIZE + ORTP_AUDIO_HEADER_LEN) {
194 t->ret = -E_OVERRUN;
195 goto err_out;
196 }
197 if (t->ret > ORTP_AUDIO_HEADER_LEN) {
198 memcpy(rn->buf + rn->loaded, tmpbuf + ORTP_AUDIO_HEADER_LEN,
199 t->ret - ORTP_AUDIO_HEADER_LEN);
200 rn->loaded += t->ret - ORTP_AUDIO_HEADER_LEN;
201 }
202 goto success;
203 case ORTP_HEADER:
204 header_len = READ_HEADER_LEN(tmpbuf);
205 PARA_DEBUG_LOG("header packet (%d bytes), header len: %d\n",
206 t->ret, header_len);
207 if (!pord->have_header) {
208 pord->have_header = 1;
209 memcpy(rn->buf, tmpbuf + ORTP_AUDIO_HEADER_LEN,
210 t->ret - ORTP_AUDIO_HEADER_LEN);
211 rn->loaded = t->ret - ORTP_AUDIO_HEADER_LEN;
212 goto success;
213 }
214 if (header_len + ORTP_AUDIO_HEADER_LEN > t->ret) {
215 t->ret = -E_INVALID_HEADER;
216 goto err_out;
217 }
218 payload_len = t->ret - ORTP_AUDIO_HEADER_LEN - header_len;
219 // PARA_INFO_LOG("len: %d header_len: %d, payload_len: %d, loaded: %d\n", ret,
220 // header_len, payload_len, rn->loaded);
221 if (rn->loaded + payload_len > CHUNK_SIZE) {
222 t->ret = -E_OVERRUN;
223 goto err_out;
224 }
225 if (payload_len)
226 memcpy(rn->buf + rn->loaded, tmpbuf
227 + (t->ret - payload_len), payload_len);
228 rn->loaded += payload_len;
229 goto success;
230 }
231 success:
232 t->ret = 1;
233 freemsg(mp);
234 if (pord->c_bad) {
235 pord->c_bad = 0;
236 pord->next_chunk = *now;
237 }
238 compute_next_chunk(chunk_time, pord);
239 return;
240 err_out:
241 rn->eof = 1;
242 freemsg(mp);
243 }
244
245 static void ortp_shutdown(void)
246 {
247 // ortp_global_stats_display();
248 ortp_exit();
249 }
250
251 static void ortp_recv_close(struct receiver_node *rn)
252 {
253 struct private_ortp_recv_data *pord = rn->private_data;
254
255 rtp_session_destroy(pord->session);
256 free(rn->private_data);
257 free(rn->buf);
258 }
259
260 static void *ortp_recv_parse_config(int argc, char **argv)
261 {
262 int ret;
263
264 struct ortp_recv_args_info *tmp = para_calloc(sizeof(struct ortp_recv_args_info));
265
266 ret = ortp_recv_cmdline_parser(argc, argv, tmp)? -E_ORTP_SYNTAX : 1;
267 if (ret > 0)
268 return tmp;
269 free(tmp);
270 return NULL;
271 }
272
273 static int ortp_recv_open(struct receiver_node *rn)
274 {
275 struct private_ortp_recv_data *pord;
276 struct ortp_recv_args_info *c = rn->conf;
277
278 rn->buf = para_calloc(CHUNK_SIZE);
279
280 rn->private_data = para_calloc(sizeof(struct private_ortp_recv_data));
281 pord = rn->private_data;
282 pord->session = rtp_session_new(RTP_SESSION_RECVONLY);
283 PARA_NOTICE_LOG("receiving from %s:%d\n", c->host_arg, c->port_arg);
284 rtp_session_set_local_addr(pord->session, c->host_arg, c->port_arg);
285 rtp_session_set_remote_addr(pord->session, c->host_arg, c->port_arg);
286 rtp_session_set_payload_type(pord->session, PAYLOAD_AUDIO_CONTINUOUS);
287 if (c->jitter_compensation_arg) {
288 rtp_session_enable_adaptive_jitter_compensation(pord->session, TRUE);
289 rtp_session_set_jitter_compensation(pord->session,
290 c->jitter_compensation_arg);
291 }
292 return 1;
293 }
294
295 /**
296 * the init function of the ortp receiver
297 *
298 * \param r pointer to the receiver struct to initialize
299 *
300 * Initialize all function pointers of \a r and call libortp's ortp_init().
301 */
302 void ortp_recv_init(struct receiver *r)
303 {
304 r->shutdown = ortp_shutdown;
305 r->open = ortp_recv_open;
306 r->close = ortp_recv_close;
307 r->pre_select = ortp_recv_pre_select;
308 r->post_select = ortp_recv_post_select;
309 r->parse_config = ortp_recv_parse_config;
310
311 ortp_init();
312 }