2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
18 /** \file ortp_recv.c paraslash's ortp receiver */
20 #include <ortp/ortp.h>
27 #include "ortp_recv.cmdline.h"
33 #define CHUNK_SIZE 128 * 1024
35 extern int msg_to_buf(mblk_t *, char *, int);
38 * data specific to the ortp receiver
40 * \sa receiver receiver_node
42 struct private_ortp_recv_data {
46 * whether a header was received
48 * A flag indicating whether the ortp receiver already received a packet which
49 * contains the audio file header.
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
56 /** the ortp session this instance of the receiver belongs to */
58 /** the time the first data or header packet was received */
60 /** ETA of the next chunk */
61 struct timeval next_chunk;
62 /** number of consecutive bad chunks */
64 /** the current timestamp which is passed to the receiving function of libortp */
66 /** \a timestamp increases by this amount */
70 static void ortp_recv_pre_select(struct sched *s, struct task *t)
72 struct receiver_node *rn = t->private_data;
73 struct private_ortp_recv_data *pord = rn->private_data;
76 if (tv_diff(&s->now, &pord->next_chunk, &tmp) >= 0) {
80 if (tv_diff(&s->timeout, &tmp, NULL) > 0)
85 static void compute_next_chunk(unsigned chunk_time,
86 struct private_ortp_recv_data *pord)
88 struct timeval chunk_tv = {0, chunk_time};
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);
99 static void ortp_recv_post_select(struct sched *s, struct task *t)
101 struct receiver_node *rn = t->private_data;
102 struct private_ortp_recv_data *pord = rn->private_data;
104 int packet_type, stream_type;
105 char tmpbuf[CHUNK_SIZE + 3];
108 // PARA_INFO_LOG("rn: %p, pord: %p, session: %p\n", rn, pord, pord->session);
110 if (pord->start.tv_sec)
111 if (tv_diff(&s->now, &pord->next_chunk, NULL) < 0)
113 mp = rtp_session_recvm_with_ts(pord->session, pord->timestamp);
115 struct timeval min_delay = {0, 100};
116 // PARA_INFO_LOG("nope, chunk_ts = %d, loaded: %d, bad: %d\n",
117 // pord->timestamp, rn->loaded, pord->c_bad);
119 t->ret = -E_TOO_MANY_BAD_CHUNKS;
120 if ((pord->c_bad > 5000 && pord->start.tv_sec) || pord->c_bad > 10000)
123 tv_add(&s->now, &min_delay, &pord->next_chunk);
126 /* okay, we have a chunk of data */
127 if (!pord->start.tv_sec)
128 pord->start = s->now;
129 t->ret = msg_to_buf(mp, tmpbuf, CHUNK_SIZE);
130 if (t->ret < ORTP_AUDIO_HEADER_LEN) {
133 t->ret = -E_MSG_TO_BUF;
135 t->ret = -E_ORTP_RECV_EOF;
138 packet_type = READ_PACKET_TYPE(tmpbuf);
139 stream_type = READ_STREAM_TYPE(tmpbuf);
140 chunk_time = READ_CHUNK_TIME(tmpbuf);
141 pord->chunk_ts = READ_CHUNK_TS(tmpbuf);
142 // PARA_INFO_LOG("packet type: %d, stream type: %d, chunk time: %u,"
143 // " chunk_ts: %u, loaded: %u, bad: %d\n", packet_type,
144 // stream_type, chunk_time, pord->chunk_ts, rn->loaded, pord->c_bad);
145 switch (packet_type) {
146 unsigned header_len, payload_len;
149 t->ret = -E_ORTP_RECV_EOF;
152 PARA_INFO_LOG("bof (%d)\n", t->ret);
153 pord->have_header = 1;
156 if (!pord->have_header && stream_type)
157 /* can't use the data, wait for header */
159 if (t->ret + rn->loaded >= CHUNK_SIZE + ORTP_AUDIO_HEADER_LEN) {
163 if (t->ret > ORTP_AUDIO_HEADER_LEN) {
164 memcpy(rn->buf + rn->loaded, tmpbuf + ORTP_AUDIO_HEADER_LEN,
165 t->ret - ORTP_AUDIO_HEADER_LEN);
166 rn->loaded += t->ret - ORTP_AUDIO_HEADER_LEN;
170 header_len = READ_HEADER_LEN(tmpbuf);
171 PARA_DEBUG_LOG("header packet (%d bytes), header len: %d\n",
173 if (!pord->have_header) {
174 pord->have_header = 1;
175 memcpy(rn->buf, tmpbuf + ORTP_AUDIO_HEADER_LEN,
176 t->ret - ORTP_AUDIO_HEADER_LEN);
177 rn->loaded = t->ret - ORTP_AUDIO_HEADER_LEN;
180 if (header_len + ORTP_AUDIO_HEADER_LEN > t->ret) {
181 t->ret = -E_INVALID_HEADER;
184 payload_len = t->ret - ORTP_AUDIO_HEADER_LEN - header_len;
185 // PARA_INFO_LOG("len: %d header_len: %d, payload_len: %d, loaded: %d\n", ret,
186 // header_len, payload_len, rn->loaded);
187 if (rn->loaded + payload_len > CHUNK_SIZE) {
192 memcpy(rn->buf + rn->loaded, tmpbuf
193 + (t->ret - payload_len), payload_len);
194 rn->loaded += payload_len;
202 pord->next_chunk = s->now;
204 compute_next_chunk(chunk_time, pord);
211 static void ortp_shutdown(void)
213 // ortp_global_stats_display();
217 static void ortp_recv_close(struct receiver_node *rn)
219 struct private_ortp_recv_data *pord = rn->private_data;
221 rtp_session_destroy(pord->session);
222 free(rn->private_data);
226 static void *ortp_recv_parse_config(int argc, char **argv)
230 struct ortp_recv_args_info *tmp = para_calloc(sizeof(struct ortp_recv_args_info));
232 ret = ortp_recv_cmdline_parser(argc, argv, tmp)? -E_ORTP_SYNTAX : 1;
239 static int ortp_recv_open(struct receiver_node *rn)
241 struct private_ortp_recv_data *pord;
242 struct ortp_recv_args_info *conf = rn->conf;
244 rn->buf = para_calloc(CHUNK_SIZE);
246 rn->private_data = para_calloc(sizeof(struct private_ortp_recv_data));
247 pord = rn->private_data;
248 pord->session = rtp_session_new(RTP_SESSION_RECVONLY);
249 PARA_NOTICE_LOG("receiving from %s:%d\n", conf->host_arg, conf->port_arg);
250 rtp_session_set_local_addr(pord->session, conf->host_arg, conf->port_arg);
251 rtp_session_set_payload_type(pord->session, PAYLOAD_AUDIO_CONTINUOUS);
252 if (conf->jitter_compensation_arg) {
253 rtp_session_enable_adaptive_jitter_compensation(pord->session, TRUE);
254 rtp_session_set_jitter_compensation(pord->session,
255 conf->jitter_compensation_arg);
261 * the init function of the ortp receiver
263 * \param r pointer to the receiver struct to initialize
265 * Initialize all function pointers of \a r and call libortp's ortp_init().
267 void ortp_recv_init(struct receiver *r)
269 r->shutdown = ortp_shutdown;
270 r->open = ortp_recv_open;
271 r->close = ortp_recv_close;
272 r->pre_select = ortp_recv_pre_select;
273 r->post_select = ortp_recv_post_select;
274 r->parse_config = ortp_recv_parse_config;