3 * @brief RTP session handling
5 /*****************************************************************************
6 * Copyright © 2008 Rémi Denis-Courmont
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2.0
11 * of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ****************************************************************************/
32 #include <vlc_demux.h>
36 typedef struct rtp_source_t rtp_source_t;
38 /** State for a RTP session: */
48 rtp_source_create (demux_t *, const rtp_session_t *, uint32_t, uint16_t);
50 rtp_source_destroy (demux_t *, const rtp_session_t *, rtp_source_t *);
52 static void rtp_decode (demux_t *, const rtp_session_t *, rtp_source_t *);
55 * Creates a new RTP session.
58 rtp_session_create (demux_t *demux)
60 rtp_session_t *session = malloc (sizeof (*session));
75 * Destroys an RTP session.
77 void rtp_session_destroy (demux_t *demux, rtp_session_t *session)
79 for (unsigned i = 0; i < session->srcc; i++)
80 rtp_source_destroy (demux, session, session->srcv[i]);
88 static void *no_init (demux_t *demux)
94 static void no_destroy (demux_t *demux, void *opaque)
96 (void)demux; (void)opaque;
99 static void no_decode (demux_t *demux, void *opaque, block_t *block)
101 (void)demux; (void)opaque;
102 block_Release (block);
106 * Adds a payload type to an RTP session.
108 int rtp_add_type (demux_t *demux, rtp_session_t *ses, const rtp_pt_t *pt)
112 msg_Err (demux, "cannot change RTP payload formats during session");
116 rtp_pt_t *ppt = realloc (ses->ptv, (ses->ptc + 1) * sizeof (rtp_pt_t));
123 ppt->init = pt->init ? pt->init : no_init;
124 ppt->destroy = pt->destroy ? pt->destroy : no_destroy;
125 ppt->decode = pt->decode ? pt->decode : no_decode;
126 ppt->frequency = pt->frequency;
127 ppt->number = pt->number;
128 msg_Dbg (demux, "added payload type %"PRIu8" (f = %"PRIu32" Hz)",
129 ppt->number, ppt->frequency);
131 assert (ppt->frequency > 0); /* SIGFPE! */
136 /** State for an RTP source */
139 mtime_t expiry; /* inactivation date */
141 uint16_t bad_seq; /* tentatively next expected sequence for resync */
142 uint16_t max_seq; /* next expected sequence */
144 uint16_t last_seq; /* sequence of the last dequeued packet */
145 block_t *blocks; /* re-ordered blocks queue */
146 void *opaque[0]; /* Per-source private payload data */
150 * Initializes a new RTP source within an RTP session.
152 static rtp_source_t *
153 rtp_source_create (demux_t *demux, const rtp_session_t *session,
154 uint32_t ssrc, uint16_t init_seq)
156 rtp_source_t *source;
158 source = malloc (sizeof (*source) + (sizeof (void *) * session->ptc));
163 source->max_seq = source->bad_seq = init_seq;
164 source->last_seq = init_seq - 1;
165 source->blocks = NULL;
167 /* Initializes all payload */
168 for (unsigned i = 0; i < session->ptc; i++)
169 source->opaque[i] = session->ptv[i].init (demux);
171 msg_Dbg (demux, "added RTP source (%08x)", ssrc);
177 * Destroys an RTP source and its associated streams.
180 rtp_source_destroy (demux_t *demux, const rtp_session_t *session,
181 rtp_source_t *source)
183 msg_Dbg (demux, "removing RTP source (%08x)", source->ssrc);
185 for (unsigned i = 0; i < session->ptc; i++)
186 session->ptv[i].destroy (demux, source->opaque[i]);
187 block_ChainRelease (source->blocks);
192 static inline uint16_t rtp_seq (const block_t *block)
194 assert (block->i_buffer >= 4);
195 return GetWBE (block->p_buffer + 2);
199 * Receives an RTP packet and queues it.
200 * @param demux VLC demux object
201 * @param session RTP session receiving the packet
202 * @param block RTP packet including the RTP header
205 rtp_receive (demux_t *demux, rtp_session_t *session, block_t *block)
207 demux_sys_t *p_sys = demux->p_sys;
209 /* RTP header sanity checks (see RFC 3550) */
210 if (block->i_buffer < 12)
212 if ((block->p_buffer[0] >> 6 ) != 2) /* RTP version number */
215 /* Remove padding if present */
216 if (block->p_buffer[0] & 0x20)
218 uint8_t padding = block->p_buffer[block->i_buffer - 1];
219 if ((padding == 0) || (block->i_buffer < (12u + padding)))
220 goto drop; /* illegal value */
222 block->i_buffer -= padding;
225 mtime_t now = mdate ();
226 rtp_source_t *src = NULL;
227 const uint16_t seq = GetWBE (block->p_buffer + 2);
228 const uint32_t ssrc = GetDWBE (block->p_buffer + 8);
230 /* In most case, we know this source already */
231 for (unsigned i = 0, max = session->srcc; i < max; i++)
233 rtp_source_t *tmp = session->srcv[i];
234 if (tmp->ssrc == ssrc)
240 /* RTP source garbage collection */
241 if (tmp->expiry < now)
243 rtp_source_destroy (demux, session, tmp);
244 if (--session->srcc > 0)
245 session->srcv[i] = session->srcv[session->srcc - 1];
252 if (session->srcc >= p_sys->max_src)
254 msg_Warn (demux, "too many RTP sessions");
259 tab = realloc (session->srcv, (session->srcc + 1) * sizeof (*tab));
264 src = rtp_source_create (demux, session, ssrc, seq);
268 tab[session->srcc++] = src;
271 /* Be optimistic for the first packet. Certain codec, such as Vorbis
272 * do not like loosing the first packet(s), so we cannot just wait
273 * for proper sequence synchronization. And we don't want to assume that
274 * the sender starts at seq=0 either. */
275 if (src->blocks == NULL)
276 src->max_seq = seq - p_sys->max_dropout;
278 /* Check sequence number */
279 /* NOTE: the sequence number is per-source,
280 * but is independent from the payload type. */
281 uint16_t delta_seq = seq - (src->max_seq + 1);
282 if ((delta_seq < 0x8000) ? (delta_seq > p_sys->max_dropout)
283 : ((65535 - delta_seq) > p_sys->max_misorder))
285 msg_Dbg (demux, "sequence discontinuity (got: %u, expected: %u)",
286 seq, (src->max_seq + 1) & 0xffff);
287 if (seq == ((src->bad_seq + 1) & 0xffff))
289 src->max_seq = src->bad_seq = seq;
290 msg_Warn (demux, "sequence resynchronized");
291 block_ChainRelease (src->blocks);
301 if (delta_seq < 0x8000)
304 /* Queues the block in sequence order,
305 * hence there is a single queue for all payload types. */
306 block_t **pp = &src->blocks;
307 for (block_t *prev = *pp; prev != NULL; prev = *pp)
309 if ((int16_t)(seq - rtp_seq (*pp)) < 0)
316 rtp_decode (demux, session, src);
320 block_Release (block);
325 rtp_decode (demux_t *demux, const rtp_session_t *session, rtp_source_t *src)
327 block_t *block = src->blocks;
329 /* Buffer underflow? */
330 if (!block || !block->p_next || !block->p_next->p_next)
332 /* TODO: use time rather than packet counts for buffer measurement */
333 src->blocks = block->p_next;
334 block->p_next = NULL;
336 /* Discontinuity detection */
337 if (((src->last_seq + 1) & 0xffff) != rtp_seq (block))
338 block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
339 src->last_seq = rtp_seq (block);
341 /* Match the payload type */
342 const rtp_pt_t *pt = NULL;
343 void *pt_data = NULL;
344 const uint8_t ptype = block->p_buffer[1] & 0x7F;
346 for (unsigned i = 0; i < session->ptc; i++)
348 if (session->ptv[i].number == ptype)
350 pt = &session->ptv[i];
351 pt_data = src->opaque[i];
358 msg_Dbg (demux, "ignoring unknown payload (%"PRIu8")", ptype);
362 /* Computes the PTS from the RTP timestamp and payload RTP frequency.
363 * DTS is unknown. Also, while the clock frequency depends on the payload
364 * format, a single source MUST only use payloads of a chosen frequency.
365 * Otherwise it would be impossible to compute consistent timestamps. */
366 /* FIXME: handle timestamp wrap properly */
367 /* TODO: sync multiple sources sanely... */
368 const uint32_t timestamp = GetDWBE (block->p_buffer + 4);
369 block->i_pts = UINT64_C(1) * CLOCK_FREQ * timestamp / pt->frequency;
372 size_t skip = 12u + (block->p_buffer[0] & 0x0F) * 4;
374 /* Extension header (ignored for now) */
375 if (block->p_buffer[0] & 0x10)
378 if (block->i_buffer < skip)
381 skip += 4 * GetWBE (block->p_buffer + skip - 2);
384 if (block->i_buffer < skip)
387 block->p_buffer += skip;
388 block->i_buffer -= skip;
390 pt->decode (demux, pt_data, block);
394 block_Release (block);