]> git.sesse.net Git - vlc/blob - modules/demux/rtp.c
Minimalistic RTP demux
[vlc] / modules / demux / rtp.c
1 /**
2  * @file rtp.c
3  * @brief Real-Time Protocol (RTP) demux module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright (C) 2001-2005 the VideoLAN team
7  * Copyright © 2007-2008 Rémi Denis-Courmont
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2.0
12  * of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  ****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27 #include <stdarg.h>
28 #include <assert.h>
29
30 #include <vlc_common.h>
31 #include <vlc_demux.h>
32 #include <vlc_aout.h>
33 #include <vlc_network.h>
34 #include <vlc_plugin.h>
35
36 #include <vlc_codecs.h>
37
38 #include "rtp.h"
39
40 #define RTP_CACHING_TEXT N_("RTP de-jitter buffer length (msec)")
41 #define RTP_CACHING_LONGTEXT N_( \
42     "How long to wait for late RTP packets (and delay the performance)." )
43
44 #define RTP_MAX_SRC_TEXT N_("Maximum RTP sources")
45 #define RTP_MAX_SRC_LONGTEXT N_( \
46     "How many distinct active RTP sources are allowed at a time." )
47
48 #define RTP_TIMEOUT_TEXT N_("RTP source timeout (sec)")
49 #define RTP_TIMEOUT_LONGTEXT N_( \
50     "How long to wait for any packet before a source is expired.")
51
52 #define RTP_MAX_DROPOUT_TEXT N_("Maximum RTP sequence number dropout")
53 #define RTP_MAX_DROPOUT_LONGTEXT N_( \
54     "RTP packets will be discarded if they are too much ahead (i.e. in the " \
55     "future) by this many packets from the last received packet." )
56
57 #define RTP_MAX_MISORDER_TEXT N_("Maximum RTP sequence number misordering")
58 #define RTP_MAX_MISORDER_LONGTEXT N_( \
59     "RTP packets will be discarded if they are too far behind (i.e. in the " \
60     "past) by this many packets from the last received packet." )
61
62 static int  Open (vlc_object_t *);
63 static void Close (vlc_object_t *);
64
65 /*
66  * Module descriptor
67  */
68 vlc_module_begin ();
69     set_shortname (_("RTP"));
70     set_description (_("(Experimental) Real-Time Protocol demuxer"));
71     set_category (CAT_INPUT);
72     set_subcategory (SUBCAT_INPUT_DEMUX);
73     set_capability ("access_demux", 10);
74     set_callbacks (Open, Close);
75
76     add_integer ("rtp-caching", 1000, NULL, RTP_CACHING_TEXT,
77                  RTP_CACHING_LONGTEXT, true);
78         change_integer_range (0, 65535);
79     add_integer ("rtp-max-src", 1, NULL, RTP_MAX_SRC_TEXT,
80                  RTP_MAX_SRC_LONGTEXT, true);
81         change_integer_range (1, 255);
82     add_integer ("rtp-timeout", 5, NULL, RTP_TIMEOUT_TEXT,
83                  RTP_TIMEOUT_LONGTEXT, true);
84     add_integer ("rtp-max-dropout", 3000, NULL, RTP_MAX_DROPOUT_TEXT,
85                  RTP_MAX_DROPOUT_LONGTEXT, true);
86         change_integer_range (0, 32767);
87     add_integer ("rtp-max-misorder", 100, NULL, RTP_MAX_MISORDER_TEXT,
88                  RTP_MAX_MISORDER_LONGTEXT, true);
89         change_integer_range (0, 32767);
90
91     add_shortcut ("rtp");
92 vlc_module_end ();
93
94 /*
95  * TODO: so much stuff
96  * - send RTCP-RR and RTCP-BYE
97  * - dynamic payload types (need SDP parser)
98  * - multiple medias (need SDP parser, and RTCP-SR parser for lip-sync)
99  * - support for access_filter in case of stream_Demux (MPEG-TS)
100  */
101
102 /*
103  * Local prototypes
104  */
105 static int Demux (demux_t *);
106 static int Control (demux_t *, int i_query, va_list args);
107 static int extract_port (char **phost);
108
109 /**
110  * Probes and initializes.
111  */
112 static int Open (vlc_object_t *obj)
113 {
114     demux_t *demux = (demux_t *)obj;
115
116     if (strcmp (demux->psz_access, "rtp"))
117         return VLC_EGENERIC;
118
119     char *tmp = strdup (demux->psz_path);
120     char *shost = tmp;
121     if (shost == NULL)
122         return VLC_ENOMEM;
123
124     char *dhost = strchr (shost, '@');
125     if (dhost)
126         *dhost++ = '\0';
127
128     /* Parses the port numbers */
129     int sport = 0, dport = 0;
130     sport = extract_port (&shost);
131     if (dhost != NULL)
132         dport = extract_port (&dhost);
133     if (dport == 0)
134         dport = 5004; /* avt-profile-1 port */
135
136     /* Try to connect */
137     int fd = net_OpenDgram (obj, dhost, dport, shost, sport,
138                             AF_UNSPEC, IPPROTO_UDP);
139     free (tmp);
140     if (fd == -1)
141         return VLC_EGENERIC;
142
143     /* Initializes demux */
144     demux_sys_t *p_sys = malloc (sizeof (*p_sys));
145     if (p_sys == NULL)
146         goto error;
147
148     var_Create (obj, "rtp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
149     p_sys->max_src      = var_CreateGetInteger (obj, "rtp-max-src");
150     p_sys->timeout      = var_CreateGetInteger (obj, "rtp-timeout");
151     p_sys->max_dropout  = var_CreateGetInteger (obj, "rtp-max-dropout");
152     p_sys->max_misorder = var_CreateGetInteger (obj, "rtp-max-misorder");
153     p_sys->autodetect   = true;
154
155     demux->pf_demux   = Demux;
156     demux->pf_control = Control;
157     demux->p_sys      = p_sys;
158
159     p_sys->session = rtp_session_create (demux);
160     if (p_sys->session == NULL)
161         goto error;
162
163     p_sys->fd = fd;
164     return VLC_SUCCESS;
165
166 error:
167     net_Close (fd);
168     free (p_sys);
169     return VLC_SUCCESS;
170 }
171
172
173 /**
174  * Releases resources
175  */
176 static void Close (vlc_object_t *obj)
177 {
178     demux_t *demux = (demux_t *)obj;
179     demux_sys_t *p_sys = demux->p_sys;
180
181     rtp_session_destroy (demux, p_sys->session);
182     net_Close (p_sys->fd);
183     free (p_sys);
184 }
185
186
187 /**
188  * Extracts port number from "[host]:port" or "host:port" strings,
189  * and remove brackets from the host name.
190  * @param phost pointer to the string upon entry,
191  * pointer to the hostname upon return.
192  * @return port number, 0 if missing.
193  */
194 static int extract_port (char **phost)
195 {
196     char *host = *phost, *port;
197
198     if (host[0] == '[')
199     {
200         host = *++phost; /* skip '[' */
201         port = strchr (host, ']');
202         if (port)
203             *port++ = '\0'; /* skip ']' */
204     }
205     else
206         port = strchr (host, ':');
207
208     if (port == NULL)
209         return 0;
210     *port++ = '\0'; /* skip ':' */
211     return atoi (port);
212 }
213
214
215 /**
216  * Control callback
217  */
218 static int Control (demux_t *demux, int i_query, va_list args)
219 {
220     /*demux_sys_t *p_sys  = demux->p_sys;*/
221     (void)demux;
222
223     switch (i_query)
224     {
225         case DEMUX_GET_POSITION:
226         {
227             float *v = va_arg (args, float *);
228             *v = 0.;
229             return 0;
230         }
231
232         case DEMUX_GET_LENGTH:
233         case DEMUX_GET_TIME:
234         {
235             int64_t *v = va_arg (args, int64_t *);
236             *v = 0;
237             return 0;
238         }
239
240         case DEMUX_GET_PTS_DELAY:
241         {
242             int64_t *v = va_arg (args, int64_t *);
243             *v = var_GetInteger (demux, "rtp-caching");
244             return 0;
245         }
246     }
247
248     return VLC_EGENERIC;
249 }
250
251
252 /**
253  * Gets a datagram from the network
254  */
255 static block_t *rtp_dgram_recv (demux_t *demux, int fd)
256 {
257     block_t *block = block_Alloc (0xffff);
258
259     ssize_t len = net_Read (VLC_OBJECT (demux), fd, NULL,
260                             block->p_buffer, block->i_buffer, false);
261     if (len == -1)
262     {
263         block_Release (block);
264         return NULL;
265     }
266     return block_Realloc (block, 0, len);
267 }
268
269
270 /*
271  * Generic packet handlers
272  */
273 static void *stream_init (demux_t *demux, const char *name)
274 {
275     return stream_DemuxNew (demux, name, demux->out);
276 }
277
278 static void stream_destroy (demux_t *demux, void *data)
279 {
280     if (data)
281         stream_DemuxDelete ((stream_t *)data);
282     (void)demux;
283 }
284
285 /* Send a packet to a chained demuxer */
286 static void stream_decode (demux_t *demux, void *data, block_t *block)
287 {
288     if (data)
289         stream_DemuxSend ((stream_t *)data, block);
290     (void)demux;
291 }
292
293 /* PT=33
294  * MP2: MPEG TS (RFC2250, §2)
295  */
296 static void *ts_init (demux_t *demux)
297 {
298     return stream_init (demux, "ts");
299 }
300
301
302 /**
303  * Processing callback
304  */
305 static int Demux (demux_t *demux)
306 {
307     demux_sys_t *p_sys = demux->p_sys;
308     block_t     *block;
309
310     block = rtp_dgram_recv (demux, p_sys->fd);
311     if (block)
312     {
313         /* Not using SDP, we need to guess the payload format used */
314         if (p_sys->autodetect && block->i_buffer >= 2)
315         {
316             rtp_pt_t pt = { .init = NULL, };
317
318             switch (pt.number = (block->p_buffer[1] & 0x7f))
319             {
320               case 14:
321                 msg_Dbg (demux, "detected MPEG Audio over RTP");
322                 pt.frequency = 44100;
323                 break;
324
325               case 32:
326                 msg_Dbg (demux, "detected MPEG Video over RTP");
327                 pt.frequency = 90000;
328                 break;
329
330               case 33:
331                 msg_Dbg (demux, "detected MPEG2 TS over RTP");
332                 pt.init = ts_init;
333                 pt.destroy = stream_destroy;
334                 pt.decode = stream_decode;
335                 pt.frequency = 90000;
336                 break;
337             }
338             rtp_add_type (demux, p_sys->session, &pt);
339             p_sys->autodetect = false;
340         }
341
342         rtp_receive (demux, p_sys->session, block);
343     }
344
345     return 1;
346 }
347
348
349 /* Send a packet to decoder */
350 #if 0
351 static void pt_decode (demux_t *obj, block_t *block, rtp_pt_t *self)
352 {
353     p_block->i_pts = p_block->i_dts = date_... (...);
354     es_out_Control (obj->out, ES_OUT_SET_PCR, p_block->i_pts);
355     es_out_Send (obj->out, (es_out_id_t *)*p_id, block);
356     return 0;
357 }
358 #endif
359
360
361 #if 0
362 /*
363  * Static payload types handler
364  */
365
366 /* PT=14
367  * MPA: MPEG Audio (RFC2250, §3.4)
368  */
369 static int pt_mpa (demux_t *obj, block_t *block, rtp_pt_t *self)
370 {
371     if (block->i_buffer < 4)
372         return VLC_EGENERIC;
373
374     block->i_buffer -= 4; // 32 bits RTP/MPA header
375     block->p_buffer += 4;
376
377     return pt_demux (obj, block, self, "mpga");
378 }
379
380
381 /* PT=32
382  * MPV: MPEG Video (RFC2250, §3.5)
383  */
384 static int pt_mpv (demux_t *obj, block_t *block, rtp_pt_t *self)
385 {
386     if (block->i_buffer < 4)
387         return VLC_EGENERIC;
388
389     block->i_buffer -= 4; // 32 bits RTP/MPV header
390     block->p_buffer += 4;
391
392     if (block->p_buffer[-3] & 0x4)
393     {
394         /* MPEG2 Video extension header */
395         /* TODO: shouldn't we skip this too ? */
396     }
397
398     return pt_demux (obj, block, self, "mpgv");
399 }
400
401
402 #endif
403
404 /*
405  * Dynamic payload type handlers
406  * Hmm, none implemented yet.
407  */