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