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>
25 #include "ortp_recv.cmdline.h"
31 #define CHUNK_SIZE 128 * 1024
33 extern int msg_to_buf(mblk_t *, char *, int);
36 * data specific to the ortp receiver
38 * \sa receiver receiver_node
40 struct private_ortp_recv_data {
45 * whether a header was received
47 * A flag indicating whether the ortp receiver already received a packet which
48 * contains the audio file header.
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
55 /** the ortp session this instance of the receiver belongs to */
57 /** the time the first data or header packet was received */
59 /** ETA of the next chunk */
60 struct timeval next_chunk;
61 /** number of consecutive bad chunks */
63 /** the current timestamp which is passed to the receiving function of libortp */
65 /** \a timestamp increases by this amount */
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)
73 struct private_ortp_recv_data *pord = rn->private_data;
74 struct timeval now, tmp;
76 gettimeofday(&now, NULL);
77 if (tv_diff(&now, &pord->next_chunk, &tmp) >= 0) {
81 if (tv_diff(timeout, &tmp, NULL) > 0)
83 return -1; /* we did not modify the fd sets */
86 static void compute_next_chunk(struct timeval *now, unsigned chunk_time,
87 struct private_ortp_recv_data *pord)
89 struct timeval chunk_tv = {0, chunk_time};
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);
100 static int ortp_recv_post_select(struct receiver_node *rn, int select_ret,
101 __a_unused fd_set *rfds, __a_unused fd_set *wfds)
103 struct private_ortp_recv_data *pord = rn->private_data;
105 int ret, packet_type, stream_type;
106 char tmpbuf[CHUNK_SIZE + 3];
110 gettimeofday(&now, NULL);
111 // PARA_DEBUG_LOG("rn: %p, pord: %p, session: %p\n", rn, pord, pord->session);
112 if (pord->start.tv_sec) {
114 if (tv_diff(&now, &pord->next_chunk, &diff) < 0)
117 mp = rtp_session_recvm_with_ts(pord->session, pord->timestamp);
119 struct timeval min_delay = {0, 100};
120 // PARA_INFO_LOG("nope, chunk_ts = %d, loaded: %d, bad: %d\n",
121 // pord->timestamp, rn->loaded, pord->c_bad);
123 if ((pord->c_bad > 5000 && pord->start.tv_sec) || pord->c_bad > 10000)
124 return -E_TOO_MANY_BAD_CHUNKS;
125 tv_add(&now, &min_delay, &pord->next_chunk);
128 /* okay, we have a chunk of data */
129 if (!pord->start.tv_sec)
131 ret = msg_to_buf(mp, tmpbuf, CHUNK_SIZE);
132 // PARA_DEBUG_LOG("have it ts = %d, chunk_ts = %d, loaded: %d, "
133 // "bad: %d, len: %d\n", pord->timestamp, pord->chunk_ts,
134 // rn->loaded, pord->c_bad, ret);
135 if (ret < ORTP_AUDIO_HEADER_LEN) {
142 packet_type = READ_PACKET_TYPE(tmpbuf);
143 stream_type = READ_STREAM_TYPE(tmpbuf);
144 chunk_time = READ_CHUNK_TIME(tmpbuf);
145 pord->chunk_ts = READ_CHUNK_TS(tmpbuf);
146 // PARA_INFO_LOG("packet type: %d, stream type: %d, chunk time: %u,"
147 // " chunk_ts: %u, loaded: %u, bad: %d\n", packet_type,
148 // stream_type, chunk_time, pord->chunk_ts, rn->loaded, pord->c_bad);
149 switch (packet_type) {
150 unsigned header_len, payload_len;
155 PARA_INFO_LOG("bof (%d)\n", ret);
156 pord->have_header = 1;
159 if (!pord->have_header && stream_type)
160 /* can't use the data, wait for header */
162 if (ret + rn->loaded >= CHUNK_SIZE + ORTP_AUDIO_HEADER_LEN) {
166 if (ret > ORTP_AUDIO_HEADER_LEN) {
167 memcpy(rn->buf + rn->loaded, tmpbuf + ORTP_AUDIO_HEADER_LEN,
168 ret - ORTP_AUDIO_HEADER_LEN);
169 rn->loaded += ret - ORTP_AUDIO_HEADER_LEN;
173 header_len = READ_HEADER_LEN(tmpbuf);
174 PARA_DEBUG_LOG("header packet (%d bytes), header len: %d\n",
176 if (!pord->have_header) {
177 pord->have_header = 1;
178 memcpy(rn->buf, tmpbuf + ORTP_AUDIO_HEADER_LEN,
179 ret - ORTP_AUDIO_HEADER_LEN);
180 rn->loaded = ret - ORTP_AUDIO_HEADER_LEN;
183 if (header_len + ORTP_AUDIO_HEADER_LEN > ret) {
184 ret = -E_INVALID_HEADER;
187 payload_len = ret - ORTP_AUDIO_HEADER_LEN - header_len;
188 // PARA_INFO_LOG("len: %d header_len: %d, payload_len: %d, loaded: %d\n", ret,
189 // header_len, payload_len, rn->loaded);
190 if (rn->loaded + payload_len > CHUNK_SIZE) {
195 memcpy(rn->buf + rn->loaded, tmpbuf
196 + (ret - payload_len), payload_len);
197 rn->loaded += payload_len;
204 pord->next_chunk = now;
206 compute_next_chunk(&now, chunk_time, pord);
213 static void ortp_shutdown(void)
215 // ortp_global_stats_display();
219 static void ortp_recv_close(struct receiver_node *rn)
221 struct private_ortp_recv_data *pord = rn->private_data;
223 rtp_session_destroy(pord->session);
224 free(rn->private_data);
228 static void *ortp_recv_parse_config(int argc, char **argv)
232 struct ortp_recv_args_info *tmp = para_calloc(sizeof(struct ortp_recv_args_info));
234 ret = ortp_recv_cmdline_parser(argc, argv, tmp)? -E_ORTP_SYNTAX : 1;
241 static int ortp_recv_open(struct receiver_node *rn)
243 struct private_ortp_recv_data *pord;
244 struct ortp_recv_args_info *conf = rn->conf;
246 rn->buf = para_calloc(CHUNK_SIZE);
248 rn->private_data = para_calloc(sizeof(struct private_ortp_recv_data));
249 pord = rn->private_data;
250 pord->session = rtp_session_new(RTP_SESSION_RECVONLY);
251 PARA_NOTICE_LOG("receiving from %s:%d\n", conf->host_arg, conf->port_arg);
252 rtp_session_set_local_addr(pord->session, conf->host_arg, conf->port_arg);
253 rtp_session_set_payload_type(pord->session, PAYLOAD_AUDIO_CONTINUOUS);
254 if (conf->jitter_compensation_arg) {
255 rtp_session_enable_adaptive_jitter_compensation(pord->session, TRUE);
256 rtp_session_set_jitter_compensation(pord->session,
257 conf->jitter_compensation_arg);
263 * the init function of the ortp receiver
265 * \param r pointer to the receiver struct to initialize
267 * Initialize all function pointers of \a r and call libortp's ortp_init().
269 void ortp_recv_init(struct receiver *r)
271 r->shutdown = ortp_shutdown;
272 r->open = ortp_recv_open;
273 r->close = ortp_recv_close;
274 r->pre_select = ortp_recv_pre_select;
275 r->post_select = ortp_recv_post_select;
276 r->parse_config = ortp_recv_parse_config;