remove COPYING from the files to be converted by grutatxt
[paraslash.git] / ortp_recv.c
1 /*
2  * Copyright (C) 2005-2007 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 = t->private_data;
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         t->ret = 1;
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 = t->private_data;
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         t->ret = -E_ORTP_RECV_EOF;
127         if (rn->output_eof && *rn->output_eof) {
128                 rn->eof = 1;
129                 return;
130         }
131         t->ret = 1;
132         if (pord->start.tv_sec)
133                 if (tv_diff(now, &pord->next_chunk, NULL) < 0)
134                         return;
135         mp = rtp_session_recvm_with_ts(pord->session, pord->timestamp);
136         if (!mp) {
137                 struct timeval min_delay = {0, 100};
138 //              PARA_INFO_LOG("nope, chunk_ts = %d, loaded: %d, bad: %d\n",
139 //                      pord->timestamp, rn->loaded, pord->c_bad);
140                 pord->c_bad++;
141                 t->ret = -E_TOO_MANY_BAD_CHUNKS;
142                 if ((pord->c_bad > 5000 && pord->start.tv_sec) || pord->c_bad > 10000)
143                         return;
144                 t->ret = 1;
145                 tv_add(now, &min_delay, &pord->next_chunk);
146                 return;
147         }
148         /* okay, we have a chunk of data */
149         if (!pord->start.tv_sec)
150                 pord->start = *now;
151         t->ret = msg_to_buf(mp, tmpbuf, CHUNK_SIZE);
152         if (t->ret < ORTP_AUDIO_HEADER_LEN) {
153                 rn->eof = 1;
154                 if (t->ret < 0)
155                         t->ret = -E_MSG_TO_BUF;
156                 else
157                         t->ret = -E_ORTP_RECV_EOF;
158                 goto err_out;
159         }
160         packet_size = t->ret;
161         packet_type = READ_PACKET_TYPE(tmpbuf);
162         stream_type = READ_STREAM_TYPE(tmpbuf);
163         chunk_time = READ_CHUNK_TIME(tmpbuf);
164         pord->chunk_ts = READ_CHUNK_TS(tmpbuf);
165 //      PARA_INFO_LOG("packet type: %d, stream type: %d, chunk time: %u,"
166 //              " chunk_ts: %u, loaded: %u, bad: %d\n", packet_type,
167 //              stream_type, chunk_time, pord->chunk_ts, rn->loaded, pord->c_bad);
168         switch (packet_type) {
169         unsigned header_len, payload_len;
170         case ORTP_EOF:
171                 rn->eof = 1;
172                 t->ret = -E_ORTP_RECV_EOF;
173                 goto err_out;
174         case ORTP_BOF:
175                 PARA_INFO_LOG("bof (%zu)\n", packet_size);
176                 pord->have_header = 1;
177                 /* fall through */
178         case ORTP_DATA:
179                 if (!pord->have_header && stream_type)
180                 /* can't use the data, wait for header */
181                         goto success;
182                 t->ret = -E_OVERRUN;
183                 if (packet_size + rn->loaded >= CHUNK_SIZE + ORTP_AUDIO_HEADER_LEN)
184                         goto err_out;
185                 if (packet_size > ORTP_AUDIO_HEADER_LEN) {
186                         memcpy(rn->buf + rn->loaded, tmpbuf + ORTP_AUDIO_HEADER_LEN,
187                                 packet_size - ORTP_AUDIO_HEADER_LEN);
188                         rn->loaded += packet_size - ORTP_AUDIO_HEADER_LEN;
189                 }
190                 goto success;
191         case ORTP_HEADER:
192                 header_len = READ_HEADER_LEN(tmpbuf);
193                 PARA_DEBUG_LOG("header packet (%zu bytes), header len: %d\n",
194                         packet_size, header_len);
195                 if (!pord->have_header) {
196                         pord->have_header = 1;
197                         memcpy(rn->buf, tmpbuf + ORTP_AUDIO_HEADER_LEN,
198                                 packet_size - ORTP_AUDIO_HEADER_LEN);
199                         rn->loaded = packet_size - ORTP_AUDIO_HEADER_LEN;
200                         goto success;
201                 }
202                 t->ret = -E_INVALID_HEADER;
203                 if (header_len + ORTP_AUDIO_HEADER_LEN > packet_size)
204                         goto err_out;
205                 payload_len = packet_size - ORTP_AUDIO_HEADER_LEN - header_len;
206                 t->ret = -E_OVERRUN;
207                 if (rn->loaded + payload_len > CHUNK_SIZE)
208                         goto err_out;
209                 if (payload_len)
210                         memcpy(rn->buf + rn->loaded, tmpbuf
211                                 + (packet_size - payload_len), payload_len);
212                 rn->loaded += payload_len;
213                 goto success;
214         }
215 success:
216         t->ret = 1;
217         freemsg(mp);
218         if (pord->c_bad) {
219                 pord->c_bad = 0;
220                 pord->next_chunk = *now;
221         }
222         compute_next_chunk(chunk_time, pord);
223         return;
224 err_out:
225         rn->eof = 1;
226         freemsg(mp);
227 }
228
229 static void ortp_shutdown(void)
230 {
231 //      ortp_global_stats_display();
232         ortp_exit();
233 }
234
235 static void ortp_recv_close(struct receiver_node *rn)
236 {
237         struct private_ortp_recv_data *pord = rn->private_data;
238
239         rtp_session_destroy(pord->session);
240         free(rn->private_data);
241         free(rn->buf);
242 }
243
244 static void *ortp_recv_parse_config(int argc, char **argv)
245 {
246         int ret;
247
248         struct ortp_recv_args_info *tmp =
249                 para_calloc(sizeof(struct ortp_recv_args_info));
250
251         ret = ortp_recv_cmdline_parser(argc, argv, tmp)? -E_ORTP_SYNTAX : 1;
252         if (ret > 0)
253                 return tmp;
254         free(tmp);
255         return NULL;
256 }
257
258 static int ortp_recv_open(struct receiver_node *rn)
259 {
260         struct private_ortp_recv_data *pord;
261         struct ortp_recv_args_info *c = rn->conf;
262
263         rn->buf = para_calloc(CHUNK_SIZE);
264
265         rn->private_data = para_calloc(sizeof(struct private_ortp_recv_data));
266         pord = rn->private_data;
267         pord->session = rtp_session_new(RTP_SESSION_RECVONLY);
268         PARA_NOTICE_LOG("receiving from %s:%d\n", c->host_arg, c->port_arg);
269         rtp_session_set_local_addr(pord->session, c->host_arg, c->port_arg);
270         rtp_session_set_remote_addr(pord->session, c->host_arg, c->port_arg);
271         rtp_session_set_payload_type(pord->session, PAYLOAD_AUDIO_CONTINUOUS);
272         if (c->jitter_compensation_arg) {
273                 rtp_session_enable_adaptive_jitter_compensation(pord->session, TRUE);
274                 rtp_session_set_jitter_compensation(pord->session,
275                         c->jitter_compensation_arg);
276         }
277         return 1;
278 }
279
280 /**
281  * the init function of the ortp receiver
282  *
283  * \param r pointer to the receiver struct to initialize
284  *
285  * Initialize all function pointers of \a r and call libortp's ortp_init().
286  */
287 void ortp_recv_init(struct receiver *r)
288 {
289         r->shutdown = ortp_shutdown;
290         r->open = ortp_recv_open;
291         r->close = ortp_recv_close;
292         r->pre_select = ortp_recv_pre_select;
293         r->post_select = ortp_recv_post_select;
294         r->parse_config = ortp_recv_parse_config;
295
296         ortp_init();
297 }