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