2 * Copyright (C) 2005-2007 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
36 * data specific to the ortp receiver
38 * \sa receiver receiver_node
40 struct private_ortp_recv_data
{
44 * whether a header was received
46 * A flag indicating whether the ortp receiver already received a packet which
47 * contains the audio file header.
49 * This flag has no effect if the audio stream indicates that no extra headers
50 * will be sent (mp3). Otherwise, all data packets are dropped until the
54 /** the ortp session this instance of the receiver belongs to */
56 /** the time the first data or header packet was received */
58 /** ETA of the next chunk */
59 struct timeval next_chunk
;
60 /** number of consecutive bad chunks */
62 /** the current timestamp which is passed to the receiving function of libortp */
64 /** \a timestamp increases by this amount */
69 static int msg_to_buf(mblk_t
*mp
, char *buffer
, int len
)
72 size_t mlen
, rlen
= len
;
77 mlen
= m
->b_wptr
- m
->b_rptr
;
80 memcpy(buffer
, m
->b_rptr
, mlen
);
81 /* go to next mblk_t */
82 mprev
->b_cont
= m
->b_cont
;
84 consumed
->b_cont
= NULL
;
89 memcpy (buffer
, m
->b_rptr
, rlen
);
98 static void ortp_recv_pre_select(struct sched
*s
, struct task
*t
)
100 struct receiver_node
*rn
= t
->private_data
;
101 struct private_ortp_recv_data
*pord
= rn
->private_data
;
104 if (tv_diff(now
, &pord
->next_chunk
, &tmp
) >= 0) {
108 if (tv_diff(&s
->timeout
, &tmp
, NULL
) > 0)
113 static void compute_next_chunk(unsigned chunk_time
,
114 struct private_ortp_recv_data
*pord
)
116 struct timeval chunk_tv
= {0, chunk_time
};
119 tv_add(&chunk_tv
, &pord
->next_chunk
, &tmp
);
120 pord
->next_chunk
= tmp
;
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);
127 static void ortp_recv_post_select(__a_unused
struct sched
*s
, struct task
*t
)
129 struct receiver_node
*rn
= t
->private_data
;
130 struct private_ortp_recv_data
*pord
= rn
->private_data
;
132 int packet_type
, stream_type
;
133 char tmpbuf
[CHUNK_SIZE
+ 3];
137 // PARA_INFO_LOG("rn: %p, pord: %p, session: %p\n", rn, pord, pord->session);
138 t
->ret
= -E_ORTP_RECV_EOF
;
139 if (rn
->output_eof
&& *rn
->output_eof
) {
144 if (pord
->start
.tv_sec
)
145 if (tv_diff(now
, &pord
->next_chunk
, NULL
) < 0)
147 mp
= rtp_session_recvm_with_ts(pord
->session
, pord
->timestamp
);
149 struct timeval min_delay
= {0, 100};
150 // PARA_INFO_LOG("nope, chunk_ts = %d, loaded: %d, bad: %d\n",
151 // pord->timestamp, rn->loaded, pord->c_bad);
153 t
->ret
= -E_TOO_MANY_BAD_CHUNKS
;
154 if ((pord
->c_bad
> 5000 && pord
->start
.tv_sec
) || pord
->c_bad
> 10000)
157 tv_add(now
, &min_delay
, &pord
->next_chunk
);
160 /* okay, we have a chunk of data */
161 if (!pord
->start
.tv_sec
)
163 t
->ret
= msg_to_buf(mp
, tmpbuf
, CHUNK_SIZE
);
164 if (t
->ret
< ORTP_AUDIO_HEADER_LEN
) {
167 t
->ret
= -E_MSG_TO_BUF
;
169 t
->ret
= -E_ORTP_RECV_EOF
;
172 packet_size
= t
->ret
;
173 packet_type
= READ_PACKET_TYPE(tmpbuf
);
174 stream_type
= READ_STREAM_TYPE(tmpbuf
);
175 chunk_time
= READ_CHUNK_TIME(tmpbuf
);
176 pord
->chunk_ts
= READ_CHUNK_TS(tmpbuf
);
177 // PARA_INFO_LOG("packet type: %d, stream type: %d, chunk time: %u,"
178 // " chunk_ts: %u, loaded: %u, bad: %d\n", packet_type,
179 // stream_type, chunk_time, pord->chunk_ts, rn->loaded, pord->c_bad);
180 switch (packet_type
) {
181 unsigned header_len
, payload_len
;
184 t
->ret
= -E_ORTP_RECV_EOF
;
187 PARA_INFO_LOG("bof (%zu)\n", packet_size
);
188 pord
->have_header
= 1;
191 if (!pord
->have_header
&& stream_type
)
192 /* can't use the data, wait for header */
195 if (packet_size
+ rn
->loaded
>= CHUNK_SIZE
+ ORTP_AUDIO_HEADER_LEN
)
197 if (packet_size
> ORTP_AUDIO_HEADER_LEN
) {
198 memcpy(rn
->buf
+ rn
->loaded
, tmpbuf
+ ORTP_AUDIO_HEADER_LEN
,
199 packet_size
- ORTP_AUDIO_HEADER_LEN
);
200 rn
->loaded
+= packet_size
- ORTP_AUDIO_HEADER_LEN
;
204 header_len
= READ_HEADER_LEN(tmpbuf
);
205 PARA_DEBUG_LOG("header packet (%zu bytes), header len: %d\n",
206 packet_size
, header_len
);
207 if (!pord
->have_header
) {
208 pord
->have_header
= 1;
209 memcpy(rn
->buf
, tmpbuf
+ ORTP_AUDIO_HEADER_LEN
,
210 packet_size
- ORTP_AUDIO_HEADER_LEN
);
211 rn
->loaded
= packet_size
- ORTP_AUDIO_HEADER_LEN
;
214 t
->ret
= -E_INVALID_HEADER
;
215 if (header_len
+ ORTP_AUDIO_HEADER_LEN
> packet_size
)
217 payload_len
= packet_size
- ORTP_AUDIO_HEADER_LEN
- header_len
;
219 if (rn
->loaded
+ payload_len
> CHUNK_SIZE
)
222 memcpy(rn
->buf
+ rn
->loaded
, tmpbuf
223 + (packet_size
- payload_len
), payload_len
);
224 rn
->loaded
+= payload_len
;
232 pord
->next_chunk
= *now
;
234 compute_next_chunk(chunk_time
, pord
);
241 static void ortp_shutdown(void)
243 // ortp_global_stats_display();
247 static void ortp_recv_close(struct receiver_node
*rn
)
249 struct private_ortp_recv_data
*pord
= rn
->private_data
;
251 rtp_session_destroy(pord
->session
);
252 free(rn
->private_data
);
256 static void *ortp_recv_parse_config(int argc
, char **argv
)
260 struct ortp_recv_args_info
*tmp
=
261 para_calloc(sizeof(struct ortp_recv_args_info
));
263 ret
= ortp_recv_cmdline_parser(argc
, argv
, tmp
)? -E_ORTP_SYNTAX
: 1;
270 static int ortp_recv_open(struct receiver_node
*rn
)
272 struct private_ortp_recv_data
*pord
;
273 struct ortp_recv_args_info
*c
= rn
->conf
;
275 rn
->buf
= para_calloc(CHUNK_SIZE
);
277 rn
->private_data
= para_calloc(sizeof(struct private_ortp_recv_data
));
278 pord
= rn
->private_data
;
279 pord
->session
= rtp_session_new(RTP_SESSION_RECVONLY
);
280 PARA_NOTICE_LOG("receiving from %s:%d\n", c
->host_arg
, c
->port_arg
);
281 rtp_session_set_local_addr(pord
->session
, c
->host_arg
, c
->port_arg
);
282 rtp_session_set_remote_addr(pord
->session
, c
->host_arg
, c
->port_arg
);
283 rtp_session_set_payload_type(pord
->session
, PAYLOAD_AUDIO_CONTINUOUS
);
284 if (c
->jitter_compensation_arg
) {
285 rtp_session_enable_adaptive_jitter_compensation(pord
->session
, TRUE
);
286 rtp_session_set_jitter_compensation(pord
->session
,
287 c
->jitter_compensation_arg
);
293 * the init function of the ortp receiver
295 * \param r pointer to the receiver struct to initialize
297 * Initialize all function pointers of \a r and call libortp's ortp_init().
299 void ortp_recv_init(struct receiver
*r
)
301 r
->shutdown
= ortp_shutdown
;
302 r
->open
= ortp_recv_open
;
303 r
->close
= ortp_recv_close
;
304 r
->pre_select
= ortp_recv_pre_select
;
305 r
->post_select
= ortp_recv_post_select
;
306 r
->parse_config
= ortp_recv_parse_config
;