]> git.sesse.net Git - vlc/blob - modules/demux/rtpsession.c
RTP: fix sequence tracking
[vlc] / modules / demux / rtpsession.c
1 /**
2  * @file session.c
3  * @brief RTP session handling
4  */
5 /*****************************************************************************
6  * Copyright © 2008 Rémi Denis-Courmont
7  *
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.
12  *
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.
17  *
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  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <errno.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc_demux.h>
33
34 #include "rtp.h"
35
36 typedef struct rtp_source_t rtp_source_t;
37
38 /** State for a RTP session: */
39 struct rtp_session_t
40 {
41     rtp_source_t **srcv;
42     unsigned       srcc;
43     uint8_t        ptc;
44     rtp_pt_t      *ptv;
45 };
46
47 static rtp_source_t *
48 rtp_source_create (demux_t *, const rtp_session_t *, uint32_t, uint16_t);
49 static void
50 rtp_source_destroy (demux_t *, const rtp_session_t *, rtp_source_t *);
51
52 static void rtp_decode (demux_t *, const rtp_session_t *, rtp_source_t *);
53
54 /**
55  * Creates a new RTP session.
56  */
57 rtp_session_t *
58 rtp_session_create (demux_t *demux)
59 {
60     rtp_session_t *session = malloc (sizeof (*session));
61     if (session == NULL)
62         return NULL;
63
64     session->srcv = NULL;
65     session->srcc = 0;
66     session->ptc = 0;
67     session->ptv = NULL;
68
69     (void)demux;
70     return session;
71 }
72
73
74 /**
75  * Destroys an RTP session.
76  */
77 void rtp_session_destroy (demux_t *demux, rtp_session_t *session)
78 {
79     for (unsigned i = 0; i < session->srcc; i++)
80         rtp_source_destroy (demux, session, session->srcv[i]);
81
82     free (session->srcv);
83     free (session->ptv);
84     free (session);
85     (void)demux;
86 }
87
88 static void *no_init (demux_t *demux)
89 {
90     (void)demux;
91     return NULL;
92 }
93
94 static void no_destroy (demux_t *demux, void *opaque)
95 {
96     (void)demux; (void)opaque;
97 }
98
99 static void no_decode (demux_t *demux, void *opaque, block_t *block)
100 {
101     (void)demux; (void)opaque;
102     block_Release (block);
103 }
104
105 /**
106  * Adds a payload type to an RTP session.
107  */
108 int rtp_add_type (demux_t *demux, rtp_session_t *ses, const rtp_pt_t *pt)
109 {
110     if (ses->srcc > 0)
111     {
112         msg_Err (demux, "cannot change RTP payload formats during session");
113         return EINVAL;
114     }
115
116     rtp_pt_t *ppt = realloc (ses->ptv, (ses->ptc + 1) * sizeof (rtp_pt_t));
117     if (ppt == NULL)
118         return ENOMEM;
119
120     ses->ptv = ppt;
121     ppt += ses->ptc++;
122
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);
130
131     assert (ppt->frequency > 0); /* SIGFPE! */
132     (void)demux;
133     return 0;
134 }
135
136 /** State for an RTP source */
137 struct rtp_source_t
138 {
139     mtime_t  expiry;  /* inactivation date */
140     uint32_t ssrc;
141     uint16_t bad_seq; /* tentatively next expected sequence for resync */
142     uint16_t max_seq; /* next expected sequence */
143
144     block_t *blocks; /* re-ordered blocks queue */
145     void    *opaque[0]; /* Per-source private payload data */
146 };
147
148 /**
149  * Initializes a new RTP source within an RTP session.
150  */
151 static rtp_source_t *
152 rtp_source_create (demux_t *demux, const rtp_session_t *session,
153                    uint32_t ssrc, uint16_t init_seq)
154 {
155     rtp_source_t *source;
156
157     source = malloc (sizeof (*source) + (sizeof (void *) * session->ptc));
158     if (source == NULL)
159         return NULL;
160
161     source->ssrc = ssrc;
162     source->max_seq = source->bad_seq = init_seq;
163     source->blocks = NULL;
164
165     /* Initializes all payload */
166     for (unsigned i = 0; i < session->ptc; i++)
167         source->opaque[i] = session->ptv[i].init (demux);
168
169     msg_Dbg (demux, "added RTP source (%08x)", ssrc);
170     return source;
171 }
172
173
174 /**
175  * Destroys an RTP source and its associated streams.
176  */
177 static void
178 rtp_source_destroy (demux_t *demux, const rtp_session_t *session,
179                     rtp_source_t *source)
180 {
181     msg_Dbg (demux, "removing RTP source (%08x)", source->ssrc);
182
183     for (unsigned i = 0; i < session->ptc; i++)
184         session->ptv[i].destroy (demux, source->opaque[i]);
185     block_ChainRelease (source->blocks);
186     free (source);
187 }
188
189
190 static inline uint16_t rtp_seq (const block_t *block)
191 {
192     assert (block->i_buffer >= 4);
193     return GetWBE (block->p_buffer + 2);
194 }
195
196 /**
197  * Receives an RTP packet and queues it.
198  * @param demux VLC demux object
199  * @param session RTP session receiving the packet
200  * @param block RTP packet including the RTP header
201  */
202 void
203 rtp_receive (demux_t *demux, rtp_session_t *session, block_t *block)
204 {
205     demux_sys_t *p_sys = demux->p_sys;
206
207     /* RTP header sanity checks (see RFC 3550) */
208     if (block->i_buffer < 12)
209         goto drop;
210     if ((block->p_buffer[0] >> 6 ) != 2) /* RTP version number */
211         goto drop;
212
213     /* Remove padding if present */
214     if (block->p_buffer[0] & 0x20)
215     {
216         uint8_t padding = block->p_buffer[block->i_buffer - 1];
217         if ((padding == 0) || (block->i_buffer < (12u + padding)))
218             goto drop; /* illegal value */
219
220         block->i_buffer -= padding;
221     }
222
223     mtime_t        now = mdate ();
224     rtp_source_t  *src  = NULL;
225     const uint16_t seq  = GetWBE (block->p_buffer + 2);
226     const uint32_t ssrc = GetDWBE (block->p_buffer + 8);
227
228     /* In most case, we know this source already */
229     for (unsigned i = 0, max = session->srcc; i < max; i++)
230     {
231         rtp_source_t *tmp = session->srcv[i];
232         if (tmp->ssrc == ssrc)
233         {
234             src = tmp;
235             break;
236         }
237
238         /* RTP source garbage collection */
239         if (tmp->expiry < now)
240         {
241             rtp_source_destroy (demux, session, tmp);
242             if (--session->srcc > 0)
243                 session->srcv[i] = session->srcv[session->srcc - 1];
244         }
245     }
246
247     if (src == NULL)
248     {
249         /* New source */
250         if (session->srcc >= p_sys->max_src)
251         {
252             msg_Warn (demux, "too many RTP sessions");
253             goto drop;
254         }
255
256         rtp_source_t **tab;
257         tab = realloc (session->srcv, (session->srcc + 1) * sizeof (*tab));
258         if (tab == NULL)
259             goto drop;
260         session->srcv = tab;
261
262         src = rtp_source_create (demux, session, ssrc, seq);
263         if (src == NULL)
264             goto drop;
265
266         tab[session->srcc++] = src;
267     }
268
269     /* Check sequence number */
270     /* NOTE: the sequence number is per-source,
271      * but is independent from the payload type. */
272     uint16_t delta_seq = seq - (src->max_seq + 1);
273     if ((delta_seq < 0x8000) ? (delta_seq > p_sys->max_dropout)
274                              : ((65535 - delta_seq) > p_sys->max_misorder))
275     {
276         msg_Dbg (demux, "sequence discontinuity (got: %u, expected: %u)",
277                  seq, (src->max_seq + 1) & 0xffff);
278         if (seq == ((src->bad_seq + 1) & 0xffff))
279         {
280             src->max_seq = src->bad_seq = seq;
281             msg_Warn (demux, "sequence resynchronized");
282             block_ChainRelease (src->blocks);
283             src->blocks = NULL;
284         }
285         else
286         {
287             src->bad_seq = seq;
288             goto drop;
289         }
290     }
291     else
292     if (delta_seq < 0x8000)
293         src->max_seq = seq;
294
295     /* Queues the block in sequence order,
296      * hence there is a single queue for all payload types. */
297     block_t **pp = &src->blocks;
298     for (block_t *prev = *pp; prev != NULL; prev = *pp)
299     {
300         if ((int16_t)(seq - rtp_seq (*pp)) >= 0)
301             break;
302         pp = &prev->p_next;
303     }
304     block->p_next = *pp;
305     *pp = block;
306
307     rtp_decode (demux, session, src);
308     return;
309
310 drop:
311     block_Release (block);
312 }
313
314
315 static void
316 rtp_decode (demux_t *demux, const rtp_session_t *session, rtp_source_t *src)
317 {
318     block_t *block = src->blocks;
319
320     /* Buffer underflow? */
321     if (!block || !block->p_next || !block->p_next->p_next)
322         return;
323     /* TODO: use time rather than packet counts for buffer measurement */
324     src->blocks = block->p_next;
325     block->p_next = NULL;
326
327     /* Match the payload type */
328     const rtp_pt_t *pt = NULL;
329     void *pt_data = NULL;
330     const uint8_t   ptype = block->p_buffer[1] & 0x7F;
331
332     for (unsigned i = 0; i < session->ptc; i++)
333     {
334         if (session->ptv[i].number == ptype)
335         {
336             pt = &session->ptv[i];
337             pt_data = src->opaque[i];
338             break;
339         }
340     }
341
342     if (pt == NULL)
343     {
344         msg_Dbg (demux, "ignoring unknown payload (%"PRIu8")", ptype);
345         goto drop;
346     }
347
348     /* Computes the PTS from the RTP timestamp and payload RTP frequency.
349      * DTS is unknown. Also, while the clock frequency depends on the payload
350      * format, a single source MUST only use payloads of a chosen frequency.
351      * Otherwise it would be impossible to compute consistent timestamps. */
352     /* FIXME: handle timestamp wrap properly */
353     /* TODO: sync multiple sources sanely... */
354     const uint32_t timestamp = GetDWBE (block->p_buffer + 4);
355     block->i_pts = UINT64_C(1) * CLOCK_FREQ * timestamp / pt->frequency;
356
357     /* CSRC count */
358     size_t skip = 12u + (block->p_buffer[0] & 0x0F) * 4;
359
360     /* Extension header (ignored for now) */
361     if (block->p_buffer[0] & 0x10)
362     {
363         skip += 4;
364         if (block->i_buffer < skip)
365             goto drop;
366
367         skip += 4 * GetWBE (block->p_buffer + skip - 2);
368     }
369
370     if (block->i_buffer < skip)
371         goto drop;
372
373     block->p_buffer += skip;
374     block->i_buffer -= skip;
375
376     pt->decode (demux, pt_data, block);
377     return;
378
379 drop:
380     block_Release (block);
381 }