para_play: simplify play_pcm(), add E_PLAY_OVERRUN, E_PREMATURE_END
[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 "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 int msg_to_buf(mblk_t *, char *, int);
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 uint32_t timestamp;
65 /** \a timestamp increases by this amount */
66 uint32_t chunk_ts;
67 };
68
69 static int ortp_recv_pre_select(struct receiver_node *rn,
70                 __a_unused fd_set *rfds, __a_unused fd_set *wfds,
71                 struct timeval *timeout)
72 {
73         struct private_ortp_recv_data *pord = rn->private_data;
74         struct timeval now, tmp;
75
76         gettimeofday(&now, NULL);
77         if (tv_diff(&now, &pord->next_chunk, &tmp) >= 0) {
78                 tmp.tv_sec = 0;
79                 tmp.tv_usec = 1000;
80         }
81         if (tv_diff(timeout, &tmp, NULL) > 0)
82                 *timeout = tmp;
83         return -1; /* we did not modify the fd sets */
84 }
85
86 static void compute_next_chunk(struct timeval *now, 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 /** \cond */
100 #define BUF_TO_VAL(buf) (((unsigned)(buf)[1] & 0xff) +  256 \
101         * ((unsigned)(buf)[0] & 0xff))
102 /** \endcond */
103
104
105 static int ortp_recv_post_select(struct receiver_node *rn, int select_ret,
106         __a_unused fd_set *rfds, __a_unused fd_set *wfds)
107 {
108         struct private_ortp_recv_data *pord = rn->private_data;
109         mblk_t *mp;
110         int ret, packet_type, stream_type;
111         char tmpbuf[CHUNK_SIZE + 3];
112         struct timeval now;
113         unsigned chunk_time;
114
115         gettimeofday(&now, NULL);
116 //      PARA_DEBUG_LOG("rn: %p, pord: %p, session: %p\n", rn, pord, pord->session);
117         if (pord->start.tv_sec) {
118                 struct timeval diff;
119                 if (tv_diff(&now, &pord->next_chunk, &diff) < 0)
120                                 return 1;
121         }
122         mp = rtp_session_recvm_with_ts(pord->session, pord->timestamp);
123         if (!mp) {
124                 struct timeval min_delay = {0, 100};
125 //              PARA_INFO_LOG("nope, chunk_ts = %d, loaded: %d, bad: %d\n",
126 //                      pord->timestamp, rn->loaded, pord->c_bad);
127                 pord->c_bad++;
128                 if ((pord->c_bad > 5000 && pord->start.tv_sec) || pord->c_bad > 10000)
129                         return -E_TOO_MANY_BAD_CHUNKS;
130                 tv_add(&now, &min_delay, &pord->next_chunk);
131                 return 1;
132         }
133         /* okay, we have a chunk of data */
134         if (!pord->start.tv_sec)
135                 pord->start = now;
136         ret = msg_to_buf(mp, tmpbuf, CHUNK_SIZE);
137 //      PARA_DEBUG_LOG("have it ts = %d, chunk_ts = %d, loaded: %d, "
138 //              "bad: %d, len: %d\n", pord->timestamp, pord->chunk_ts,
139 //              rn->loaded, pord->c_bad, ret);
140         if (ret < ORTP_AUDIO_HEADER_LEN) {
141                 if (ret < 0)
142                         ret = -E_MSG_TO_BUF;
143                 else
144                         ret = 0;
145                 goto err_out;
146         }
147         packet_type = READ_PACKET_TYPE(tmpbuf);
148         stream_type = READ_STREAM_TYPE(tmpbuf);
149         chunk_time = READ_CHUNK_TIME(tmpbuf);
150         pord->chunk_ts = READ_CHUNK_TS(tmpbuf);
151 //      PARA_INFO_LOG("packet type: %d, stream type: %d, chunk time: %u,"
152 //              " chunk_ts: %u, loaded: %u, bad: %d\n", packet_type,
153 //              stream_type, chunk_time, pord->chunk_ts, rn->loaded, pord->c_bad);
154         switch (packet_type) {
155         unsigned header_len, payload_len;
156         case ORTP_EOF:
157                 ret = 0;
158                 goto err_out;
159         case ORTP_BOF:
160                 PARA_INFO_LOG("bof (%d)\n", ret);
161                 pord->have_header = 1;
162                 /* fall through */
163         case ORTP_DATA:
164                 if (!pord->have_header && stream_type)
165                 /* can't use the data, wait for header */
166                         goto success;
167                 if (ret + rn->loaded >= CHUNK_SIZE + ORTP_AUDIO_HEADER_LEN) {
168                         ret = -E_OVERRUN;
169                         goto err_out;
170                 }
171                 if (ret > ORTP_AUDIO_HEADER_LEN) {
172                         memcpy(rn->buf + rn->loaded, tmpbuf + ORTP_AUDIO_HEADER_LEN,
173                                 ret - ORTP_AUDIO_HEADER_LEN);
174                         rn->loaded += ret - ORTP_AUDIO_HEADER_LEN;
175                 }
176                 goto success;
177         case ORTP_HEADER:
178                 header_len = READ_HEADER_LEN(tmpbuf);
179                 PARA_DEBUG_LOG("header packet (%d bytes), header len: %d\n",
180                         ret, header_len);
181                 if (!pord->have_header) {
182                         pord->have_header = 1;
183                         memcpy(rn->buf, tmpbuf + ORTP_AUDIO_HEADER_LEN,
184                                 ret - ORTP_AUDIO_HEADER_LEN);
185                         rn->loaded = ret - ORTP_AUDIO_HEADER_LEN;
186                         goto success;
187                 }
188                 if (header_len + ORTP_AUDIO_HEADER_LEN > ret) {
189                         ret = -E_INVALID_HEADER;
190                         goto err_out;
191                 }
192                 payload_len = ret - ORTP_AUDIO_HEADER_LEN - header_len;
193 //              PARA_INFO_LOG("len: %d header_len: %d, payload_len: %d, loaded: %d\n", ret,
194 //                      header_len, payload_len, rn->loaded);
195                 if (rn->loaded + payload_len > CHUNK_SIZE) {
196                         ret = -E_OVERRUN;
197                         goto err_out;
198                 }
199                 if (payload_len)
200                         memcpy(rn->buf + rn->loaded, tmpbuf
201                                 + (ret - payload_len), payload_len);
202                 rn->loaded += payload_len;
203                 goto success;
204         }
205 success:
206         freemsg(mp);
207         if (pord->c_bad) {
208                 pord->c_bad = 0;
209                 pord->next_chunk = now;
210         }
211         compute_next_chunk(&now, chunk_time, pord);
212         return 1;
213 err_out:
214         freemsg(mp);
215         return ret;
216 }
217
218 static void ortp_shutdown(void)
219 {
220 //      ortp_global_stats_display();
221         ortp_exit();
222 }
223
224 static void ortp_recv_close(struct receiver_node *rn)
225 {
226         struct private_ortp_recv_data *pord = rn->private_data;
227
228         rtp_session_destroy(pord->session);
229         free(rn->private_data);
230         free(rn->buf);
231 }
232
233 static void *ortp_recv_parse_config(int argc, char **argv)
234 {
235         int ret;
236
237         struct ortp_recv_args_info *tmp = para_calloc(sizeof(struct ortp_recv_args_info));
238
239         ret = ortp_recv_cmdline_parser(argc, argv, tmp)? -E_ORTP_SYNTAX : 1;
240         if (ret > 0)
241                 return tmp;
242         free(tmp);
243         return NULL;
244 }
245
246 static int ortp_recv_open(struct receiver_node *rn)
247 {
248         struct private_ortp_recv_data *pord;
249         struct ortp_recv_args_info *conf = rn->conf;
250
251         rn->buf = para_calloc(CHUNK_SIZE);
252
253         rn->private_data = para_calloc(sizeof(struct private_ortp_recv_data));
254         pord = rn->private_data;
255         pord->session = rtp_session_new(RTP_SESSION_RECVONLY);
256         PARA_NOTICE_LOG("receiving from %s:%d\n", conf->host_arg, conf->port_arg);
257         rtp_session_set_local_addr(pord->session, conf->host_arg, conf->port_arg);
258         rtp_session_set_payload_type(pord->session, PAYLOAD_AUDIO_CONTINUOUS);
259         if (conf->jitter_compensation_arg) {
260                 rtp_session_enable_adaptive_jitter_compensation(pord->session, TRUE);
261                 rtp_session_set_jitter_compensation(pord->session,
262                         conf->jitter_compensation_arg);
263         }
264         return 1;
265 }
266
267 /**
268  * the init function of the ortp receiver
269  *
270  * \param r pointer to the receiver struct to initialize
271  *
272  * Initialize all function pointers of \a r and call libortp's ortp_init().
273  */
274 void ortp_recv_init(struct receiver *r)
275 {
276         r->shutdown = ortp_shutdown;
277         r->open = ortp_recv_open;
278         r->close = ortp_recv_close;
279         r->pre_select = ortp_recv_pre_select;
280         r->post_select = ortp_recv_post_select;
281         r->parse_config = ortp_recv_parse_config;
282
283         ortp_init();
284 }