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