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 */
140 uint32_t jitter; /* interarrival delay jitter estimate */
141 mtime_t last_rx; /* last received packet local timestamp */
142 uint32_t last_ts; /* last received packet RTP timestamp */
144 uint16_t bad_seq; /* tentatively next expected sequence for resync */
145 uint16_t max_seq; /* next expected sequence */
147 uint16_t last_seq; /* sequence of the next dequeued packet */
148 block_t *blocks; /* re-ordered blocks queue */
149 void *opaque[0]; /* Per-source private payload data */
153 * Initializes a new RTP source within an RTP session.
155 static rtp_source_t *
156 rtp_source_create (demux_t *demux, const rtp_session_t *session,
157 uint32_t ssrc, uint16_t init_seq)
159 rtp_source_t *source;
161 source = malloc (sizeof (*source) + (sizeof (void *) * session->ptc));
167 source->max_seq = source->bad_seq = init_seq;
168 source->last_seq = init_seq - 1;
169 source->blocks = NULL;
171 /* Initializes all payload */
172 for (unsigned i = 0; i < session->ptc; i++)
173 source->opaque[i] = session->ptv[i].init (demux);
175 msg_Dbg (demux, "added RTP source (%08x)", ssrc);
181 * Destroys an RTP source and its associated streams.
184 rtp_source_destroy (demux_t *demux, const rtp_session_t *session,
185 rtp_source_t *source)
187 msg_Dbg (demux, "removing RTP source (%08x)", source->ssrc);
189 for (unsigned i = 0; i < session->ptc; i++)
190 session->ptv[i].destroy (demux, source->opaque[i]);
191 block_ChainRelease (source->blocks);
195 static inline uint16_t rtp_seq (const block_t *block)
197 assert (block->i_buffer >= 4);
198 return GetWBE (block->p_buffer + 2);
201 static inline uint32_t rtp_timestamp (const block_t *block)
203 assert (block->i_buffer >= 12);
204 return GetDWBE (block->p_buffer + 4);
207 static const struct rtp_pt_t *
208 rtp_find_ptype (const rtp_session_t *session, rtp_source_t *source,
209 const block_t *block, void **pt_data)
211 uint8_t ptype = rtp_ptype (block);
213 for (unsigned i = 0; i < session->ptc; i++)
215 if (session->ptv[i].number == ptype)
218 *pt_data = source->opaque[i];
219 return &session->ptv[i];
226 * Receives an RTP packet and queues it. Not a cancellation point.
228 * @param demux VLC demux object
229 * @param session RTP session receiving the packet
230 * @param block RTP packet including the RTP header
233 rtp_queue (demux_t *demux, rtp_session_t *session, block_t *block)
235 demux_sys_t *p_sys = demux->p_sys;
237 /* RTP header sanity checks (see RFC 3550) */
238 if (block->i_buffer < 12)
240 if ((block->p_buffer[0] >> 6 ) != 2) /* RTP version number */
243 /* Remove padding if present */
244 if (block->p_buffer[0] & 0x20)
246 uint8_t padding = block->p_buffer[block->i_buffer - 1];
247 if ((padding == 0) || (block->i_buffer < (12u + padding)))
248 goto drop; /* illegal value */
250 block->i_buffer -= padding;
253 mtime_t now = mdate ();
254 rtp_source_t *src = NULL;
255 const uint16_t seq = rtp_seq (block);
256 const uint32_t ssrc = GetDWBE (block->p_buffer + 8);
258 /* In most case, we know this source already */
259 for (unsigned i = 0, max = session->srcc; i < max; i++)
261 rtp_source_t *tmp = session->srcv[i];
262 if (tmp->ssrc == ssrc)
268 /* RTP source garbage collection */
269 if ((tmp->last_rx + (p_sys->timeout * CLOCK_FREQ)) < now)
271 rtp_source_destroy (demux, session, tmp);
272 if (--session->srcc > 0)
273 session->srcv[i] = session->srcv[session->srcc - 1];
280 if (session->srcc >= p_sys->max_src)
282 msg_Warn (demux, "too many RTP sessions");
287 tab = realloc (session->srcv, (session->srcc + 1) * sizeof (*tab));
292 src = rtp_source_create (demux, session, ssrc, seq);
296 tab[session->srcc++] = src;
297 /* Cannot compute jitter yet */
301 const rtp_pt_t *pt = rtp_find_ptype (session, src, block, NULL);
305 /* Recompute jitter estimate.
306 * That is computed from the RTP timestamps and the system clock.
307 * It is independent of RTP sequence. */
308 uint32_t freq = pt->frequency;
309 int64_t ts = rtp_timestamp (block);
310 int64_t d = ((now - src->last_rx) * freq) / CLOCK_FREQ;
311 d -= ts - src->last_ts;
313 src->jitter += ((d - src->jitter) + 8) >> 4;
317 src->last_ts = rtp_timestamp (block);
319 /* Check sequence number */
320 /* NOTE: the sequence number is per-source,
321 * but is independent from the payload type. */
322 int delta_seq = seq - src->max_seq;
323 if ((delta_seq > 0) ? (delta_seq > p_sys->max_dropout)
324 : (-delta_seq > p_sys->max_misorder))
326 msg_Dbg (demux, "sequence discontinuity"
327 " (got: %"PRIu16", expected: %"PRIu16")", seq, src->max_seq);
328 if (seq == src->bad_seq)
330 src->max_seq = src->bad_seq = seq + 1;
331 src->last_seq = seq - 0x7fffe; /* hack for rtp_decode() */
332 msg_Warn (demux, "sequence resynchronized");
333 block_ChainRelease (src->blocks);
338 src->bad_seq = seq + 1;
344 src->max_seq = seq + 1;
346 /* Queues the block in sequence order,
347 * hence there is a single queue for all payload types. */
348 block_t **pp = &src->blocks;
349 for (block_t *prev = *pp; prev != NULL; prev = *pp)
351 int delta_seq = seq - rtp_seq (prev);
356 msg_Dbg (demux, "duplicate packet (sequence: %"PRIu16")", seq);
357 goto drop; /* duplicate */
364 /*rtp_decode (demux, session, src);*/
368 block_Release (block);
373 rtp_decode (demux_t *demux, const rtp_session_t *session, rtp_source_t *src)
375 block_t *block = src->blocks;
378 src->blocks = block->p_next;
379 block->p_next = NULL;
381 /* Discontinuity detection */
382 uint16_t delta_seq = rtp_seq (block) - (src->last_seq + 1);
385 if (delta_seq >= 0x8000)
386 { /* Unrecoverable if later packets have already been dequeued */
387 msg_Warn (demux, "ignoring late packet (sequence: %"PRIu16")",
391 block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
393 src->last_seq = rtp_seq (block);
395 /* Match the payload type */
397 const rtp_pt_t *pt = rtp_find_ptype (session, src, block, &pt_data);
400 msg_Dbg (demux, "unknown payload (%"PRIu8")",
405 /* Computes the PTS from the RTP timestamp and payload RTP frequency.
406 * DTS is unknown. Also, while the clock frequency depends on the payload
407 * format, a single source MUST only use payloads of a chosen frequency.
408 * Otherwise it would be impossible to compute consistent timestamps. */
409 /* FIXME: handle timestamp wrap properly */
410 /* TODO: inter-medias/sessions sync (using RTCP-SR) */
411 const uint32_t timestamp = rtp_timestamp (block);
412 block->i_pts = UINT64_C(1) * CLOCK_FREQ * timestamp / pt->frequency;
415 size_t skip = 12u + (block->p_buffer[0] & 0x0F) * 4;
417 /* Extension header (ignored for now) */
418 if (block->p_buffer[0] & 0x10)
421 if (block->i_buffer < skip)
424 skip += 4 * GetWBE (block->p_buffer + skip - 2);
427 if (block->i_buffer < skip)
430 block->p_buffer += skip;
431 block->i_buffer -= skip;
433 pt->decode (demux, pt_data, block);
437 block_Release (block);
442 * Dequeues an RTP packet and pass it to decoder. Not cancellation-safe(?).
444 * @param demux VLC demux object
445 * @param session RTP session receiving the packet
446 * @param deadlinep pointer to deadline to call rtp_dequeue() again
447 * @return true if the buffer is not empty, false otherwise.
448 * In the later case, *deadlinep is undefined.
450 bool rtp_dequeue (demux_t *demux, const rtp_session_t *session,
451 mtime_t *restrict deadlinep)
453 mtime_t now = mdate ();
454 bool pending = false;
456 for (unsigned i = 0, max = session->srcc; i < max; i++)
458 rtp_source_t *src = session->srcv[i];
461 /* Because of IP packet delay variation (IPDV), we need to guesstimate
462 * how long to wait for a missing packet in the RTP sequence
463 * (see RFC3393 for background on IPDV).
465 * This situation occurs if a packet got lost, or if the network has
466 * re-ordered packets. Unfortunately, the MSL is 2 minutes, orders of
467 * magnitude too long for multimedia. We need a tradeoff.
468 * If we underestimated IPDV, we may have to discard valid but late
469 * packets. If we overestimate it, we will either cause too much
470 * delay, or worse, underflow our downstream buffers, as we wait for
471 * definitely a lost packets.
473 * The rest of the "de-jitter buffer" work is done by the interval
474 * LibVLC E/S-out clock synchronization. Here, we need to bother about
475 * re-ordering packets, as decoders can't cope with mis-ordered data.
477 while (((block = src->blocks)) != NULL)
479 if ((int16_t)(rtp_seq (block) - (src->last_seq + 1)) <= 0)
480 { /* Next (or earlier) block ready, no need to wait */
481 rtp_decode (demux, session, src);
485 /* Wait for 3 times the inter-arrival delay variance (about 99.7%
486 * match for random gaussian jitter). Additionnaly, we implicitly
487 * wait for misordering times the packetization time.
489 mtime_t deadline = src->last_rx;
490 const rtp_pt_t *pt = rtp_find_ptype (session, src, block, NULL);
492 deadline += UINT64_C(3) * CLOCK_FREQ * src->jitter
497 rtp_decode (demux, session, src);
500 if (*deadlinep > deadline)
501 *deadlinep = deadline;
502 pending = true; /* packet pending in buffer */