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