3 * @brief Real-Time Protocol (RTP) demux module for VLC media player
5 /*****************************************************************************
6 * Copyright (C) 2001-2005 the VideoLAN team
7 * Copyright © 2007-2008 Rémi Denis-Courmont
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.
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.
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 ****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_demux.h>
33 #include <vlc_network.h>
34 #include <vlc_plugin.h>
36 #include <vlc_codecs.h>
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)." )
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." )
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.")
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." )
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." )
62 static int Open (vlc_object_t *);
63 static void Close (vlc_object_t *);
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);
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);
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)
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);
110 * Probes and initializes.
112 static int Open (vlc_object_t *obj)
114 demux_t *demux = (demux_t *)obj;
116 if (strcmp (demux->psz_access, "rtp"))
119 char *tmp = strdup (demux->psz_path);
124 char *dhost = strchr (shost, '@');
128 /* Parses the port numbers */
129 int sport = 0, dport = 0;
130 sport = extract_port (&shost);
132 dport = extract_port (&dhost);
134 dport = 5004; /* avt-profile-1 port */
137 int fd = net_OpenDgram (obj, dhost, dport, shost, sport,
138 AF_UNSPEC, IPPROTO_UDP);
143 /* Initializes demux */
144 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
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;
155 demux->pf_demux = Demux;
156 demux->pf_control = Control;
157 demux->p_sys = p_sys;
159 p_sys->session = rtp_session_create (demux);
160 if (p_sys->session == NULL)
176 static void Close (vlc_object_t *obj)
178 demux_t *demux = (demux_t *)obj;
179 demux_sys_t *p_sys = demux->p_sys;
181 rtp_session_destroy (demux, p_sys->session);
182 net_Close (p_sys->fd);
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.
194 static int extract_port (char **phost)
196 char *host = *phost, *port;
200 host = *++phost; /* skip '[' */
201 port = strchr (host, ']');
203 *port++ = '\0'; /* skip ']' */
206 port = strchr (host, ':');
210 *port++ = '\0'; /* skip ':' */
218 static int Control (demux_t *demux, int i_query, va_list args)
220 /*demux_sys_t *p_sys = demux->p_sys;*/
225 case DEMUX_GET_POSITION:
227 float *v = va_arg (args, float *);
232 case DEMUX_GET_LENGTH:
235 int64_t *v = va_arg (args, int64_t *);
240 case DEMUX_GET_PTS_DELAY:
242 int64_t *v = va_arg (args, int64_t *);
243 *v = var_GetInteger (demux, "rtp-caching");
253 * Gets a datagram from the network
255 static block_t *rtp_dgram_recv (demux_t *demux, int fd)
257 block_t *block = block_Alloc (0xffff);
259 ssize_t len = net_Read (VLC_OBJECT (demux), fd, NULL,
260 block->p_buffer, block->i_buffer, false);
263 block_Release (block);
266 return block_Realloc (block, 0, len);
271 * Generic packet handlers
273 static void *stream_init (demux_t *demux, const char *name)
275 return stream_DemuxNew (demux, name, demux->out);
278 static void stream_destroy (demux_t *demux, void *data)
281 stream_DemuxDelete ((stream_t *)data);
285 /* Send a packet to a chained demuxer */
286 static void stream_decode (demux_t *demux, void *data, block_t *block)
289 stream_DemuxSend ((stream_t *)data, block);
294 * MP2: MPEG TS (RFC2250, §2)
296 static void *ts_init (demux_t *demux)
298 return stream_init (demux, "ts");
303 * Processing callback
305 static int Demux (demux_t *demux)
307 demux_sys_t *p_sys = demux->p_sys;
310 block = rtp_dgram_recv (demux, p_sys->fd);
313 /* Not using SDP, we need to guess the payload format used */
314 if (p_sys->autodetect && block->i_buffer >= 2)
316 rtp_pt_t pt = { .init = NULL, };
318 switch (pt.number = (block->p_buffer[1] & 0x7f))
321 msg_Dbg (demux, "detected MPEG Audio over RTP");
322 pt.frequency = 44100;
326 msg_Dbg (demux, "detected MPEG Video over RTP");
327 pt.frequency = 90000;
331 msg_Dbg (demux, "detected MPEG2 TS over RTP");
333 pt.destroy = stream_destroy;
334 pt.decode = stream_decode;
335 pt.frequency = 90000;
338 rtp_add_type (demux, p_sys->session, &pt);
339 p_sys->autodetect = false;
342 rtp_receive (demux, p_sys->session, block);
349 /* Send a packet to decoder */
351 static void pt_decode (demux_t *obj, block_t *block, rtp_pt_t *self)
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);
363 * Static payload types handler
367 * MPA: MPEG Audio (RFC2250, §3.4)
369 static int pt_mpa (demux_t *obj, block_t *block, rtp_pt_t *self)
371 if (block->i_buffer < 4)
374 block->i_buffer -= 4; // 32 bits RTP/MPA header
375 block->p_buffer += 4;
377 return pt_demux (obj, block, self, "mpga");
382 * MPV: MPEG Video (RFC2250, §3.5)
384 static int pt_mpv (demux_t *obj, block_t *block, rtp_pt_t *self)
386 if (block->i_buffer < 4)
389 block->i_buffer -= 4; // 32 bits RTP/MPV header
390 block->p_buffer += 4;
392 if (block->p_buffer[-3] & 0x4)
394 /* MPEG2 Video extension header */
395 /* TODO: shouldn't we skip this too ? */
398 return pt_demux (obj, block, self, "mpgv");
405 * Dynamic payload type handlers
406 * Hmm, none implemented yet.