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