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>
37 #include <vlc_plugin.h>
39 #include <vlc_codecs.h>
43 #define RTP_CACHING_TEXT N_("RTP de-jitter buffer length (msec)")
44 #define RTP_CACHING_LONGTEXT N_( \
45 "How long to wait for late RTP packets (and delay the performance)." )
47 #define RTP_MAX_SRC_TEXT N_("Maximum RTP sources")
48 #define RTP_MAX_SRC_LONGTEXT N_( \
49 "How many distinct active RTP sources are allowed at a time." )
51 #define RTP_TIMEOUT_TEXT N_("RTP source timeout (sec)")
52 #define RTP_TIMEOUT_LONGTEXT N_( \
53 "How long to wait for any packet before a source is expired.")
55 #define RTP_MAX_DROPOUT_TEXT N_("Maximum RTP sequence number dropout")
56 #define RTP_MAX_DROPOUT_LONGTEXT N_( \
57 "RTP packets will be discarded if they are too much ahead (i.e. in the " \
58 "future) by this many packets from the last received packet." )
60 #define RTP_MAX_MISORDER_TEXT N_("Maximum RTP sequence number misordering")
61 #define RTP_MAX_MISORDER_LONGTEXT N_( \
62 "RTP packets will be discarded if they are too far behind (i.e. in the " \
63 "past) by this many packets from the last received packet." )
65 static int Open (vlc_object_t *);
66 static void Close (vlc_object_t *);
72 set_shortname (_("RTP"));
73 set_description (_("(Experimental) Real-Time Protocol demuxer"));
74 set_category (CAT_INPUT);
75 set_subcategory (SUBCAT_INPUT_DEMUX);
76 set_capability ("access_demux", 10);
77 set_callbacks (Open, Close);
79 add_integer ("rtp-caching", 1000, NULL, RTP_CACHING_TEXT,
80 RTP_CACHING_LONGTEXT, true);
81 change_integer_range (0, 65535);
82 add_integer ("rtp-max-src", 1, NULL, RTP_MAX_SRC_TEXT,
83 RTP_MAX_SRC_LONGTEXT, true);
84 change_integer_range (1, 255);
85 add_integer ("rtp-timeout", 5, NULL, RTP_TIMEOUT_TEXT,
86 RTP_TIMEOUT_LONGTEXT, true);
87 add_integer ("rtp-max-dropout", 3000, NULL, RTP_MAX_DROPOUT_TEXT,
88 RTP_MAX_DROPOUT_LONGTEXT, true);
89 change_integer_range (0, 32767);
90 add_integer ("rtp-max-misorder", 100, NULL, RTP_MAX_MISORDER_TEXT,
91 RTP_MAX_MISORDER_LONGTEXT, true);
92 change_integer_range (0, 32767);
94 add_shortcut ("dccp");
95 /*add_shortcut ("sctp");*/
96 add_shortcut ("rtptcp"); /* "tcp" is already taken :( */
98 add_shortcut ("udplite");
102 * TODO: so much stuff
103 * - send RTCP-RR and RTCP-BYE
104 * - dynamic payload types (need SDP parser)
105 * - multiple medias (need SDP parser, and RTCP-SR parser for lip-sync)
106 * - support for access_filter in case of stream_Demux (MPEG-TS)
110 # define IPPROTO_DCCP 33 /* IANA */
113 #ifndef IPPROTO_UDPLITE
114 # define IPPROTO_UDPLITE 136 /* from IANA */
121 static int Demux (demux_t *);
122 static int Control (demux_t *, int i_query, va_list args);
123 static int extract_port (char **phost);
126 * Probes and initializes.
128 static int Open (vlc_object_t *obj)
130 demux_t *demux = (demux_t *)obj;
131 int tp; /* transport protocol */
133 if (!strcmp (demux->psz_access, "dccp"))
136 if (!strcmp (demux->psz_access, "rtptcp"))
139 if (!strcmp (demux->psz_access, "rtp"))
142 if (!strcmp (demux->psz_access, "udplite"))
143 tp = IPPROTO_UDPLITE;
147 char *tmp = strdup (demux->psz_path);
152 char *dhost = strchr (shost, '@');
156 /* Parses the port numbers */
157 int sport = 0, dport = 0;
158 sport = extract_port (&shost);
160 dport = extract_port (&dhost);
162 dport = 5004; /* avt-profile-1 port */
170 case IPPROTO_UDPLITE:
171 fd = net_OpenDgram (obj, dhost, dport, shost, sport, AF_UNSPEC,
176 #ifndef SOCK_DCCP /* provisional API (FIXME) */
182 var_Create (obj, "dccp-service", VLC_VAR_STRING);
183 var_SetString (obj, "dccp-service", "RTPV");
184 fd = net_Connect (obj, shost, sport, SOCK_DCCP, tp);
186 msg_Err (obj, "DCCP support not included");
191 fd = net_Connect (obj, shost, sport, SOCK_STREAM, tp);
198 net_SetCSCov (fd, -1, 12);
200 /* Initializes demux */
201 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
205 p_sys->caching = var_CreateGetInteger (obj, "rtp-caching");
206 p_sys->max_src = var_CreateGetInteger (obj, "rtp-max-src");
207 p_sys->timeout = var_CreateGetInteger (obj, "rtp-timeout");
208 p_sys->max_dropout = var_CreateGetInteger (obj, "rtp-max-dropout");
209 p_sys->max_misorder = var_CreateGetInteger (obj, "rtp-max-misorder");
210 p_sys->autodetect = true;
211 p_sys->framed_rtp = (tp == IPPROTO_TCP);
213 demux->pf_demux = Demux;
214 demux->pf_control = Control;
215 demux->p_sys = p_sys;
217 p_sys->session = rtp_session_create (demux);
218 if (p_sys->session == NULL)
234 static void Close (vlc_object_t *obj)
236 demux_t *demux = (demux_t *)obj;
237 demux_sys_t *p_sys = demux->p_sys;
239 rtp_session_destroy (demux, p_sys->session);
240 net_Close (p_sys->fd);
246 * Extracts port number from "[host]:port" or "host:port" strings,
247 * and remove brackets from the host name.
248 * @param phost pointer to the string upon entry,
249 * pointer to the hostname upon return.
250 * @return port number, 0 if missing.
252 static int extract_port (char **phost)
254 char *host = *phost, *port;
258 host = *++phost; /* skip '[' */
259 port = strchr (host, ']');
261 *port++ = '\0'; /* skip ']' */
264 port = strchr (host, ':');
268 *port++ = '\0'; /* skip ':' */
276 static int Control (demux_t *demux, int i_query, va_list args)
278 demux_sys_t *p_sys = demux->p_sys;
282 case DEMUX_GET_POSITION:
284 float *v = va_arg (args, float *);
289 case DEMUX_GET_LENGTH:
292 int64_t *v = va_arg (args, int64_t *);
297 case DEMUX_GET_PTS_DELAY:
299 int64_t *v = va_arg (args, int64_t *);
310 * Checks if a file descriptor is hung up.
312 static bool fd_dead (int fd)
314 struct pollfd ufd = { .fd = fd, };
316 return (poll (&ufd, 1, 0) == 1) && (ufd.revents & POLLHUP);
321 * Gets a datagram from the network, or NULL in case of fatal error.
323 static block_t *rtp_dgram_recv (demux_t *demux, int fd)
325 block_t *block = block_Alloc (0xffff);
330 len = net_Read (VLC_OBJECT (demux), fd, NULL,
331 block->p_buffer, block->i_buffer, false);
332 if (((len <= 0) && fd_dead (fd))
335 block_Release (block);
341 return block_Realloc (block, 0, len);
345 * Gets a framed RTP packet, or NULL in case of fatal error.
347 static block_t *rtp_stream_recv (demux_t *demux, int fd)
350 uint8_t hdr[2]; /* frame header */
352 /* Receives the RTP frame header */
355 ssize_t val = net_Read (VLC_OBJECT (demux), fd, NULL,
356 hdr + len, 2 - len, false);
363 block_t *block = block_Alloc (GetWBE (hdr));
365 /* Receives the RTP packet */
366 for (ssize_t i = 0; i < len;)
370 val = net_Read (VLC_OBJECT (demux), fd, NULL,
371 block->p_buffer + i, block->i_buffer - i, false);
374 block_Release (block);
385 * Generic packet handlers
388 static void *codec_init (demux_t *demux, es_format_t *fmt)
390 return es_out_Add (demux->out, fmt);
393 static void codec_destroy (demux_t *demux, void *data)
396 es_out_Del (demux->out, (es_out_id_t *)data);
399 /* Send a packet to decoder */
400 static void codec_decode (demux_t *demux, void *data, block_t *block)
404 block->i_dts = 0; /* RTP does not specify this */
405 es_out_Control (demux->out, ES_OUT_SET_PCR,
406 block->i_pts - demux->p_sys->caching * 1000);
407 es_out_Send (demux->out, (es_out_id_t *)data, block);
410 block_Release (block);
414 static void *stream_init (demux_t *demux, const char *name)
416 return stream_DemuxNew (demux, name, demux->out);
419 static void stream_destroy (demux_t *demux, void *data)
422 stream_DemuxDelete ((stream_t *)data);
426 /* Send a packet to a chained demuxer */
427 static void stream_decode (demux_t *demux, void *data, block_t *block)
430 stream_DemuxSend ((stream_t *)data, block);
432 block_Release (block);
437 * Static payload types handler
441 * PCMU: G.711 µ-law (RFC3551)
443 static void *pcmu_init (demux_t *demux)
447 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('u', 'l', 'a', 'w'));
448 fmt.audio.i_rate = 8000;
449 fmt.audio.i_channels = 1;
450 return codec_init (demux, &fmt);
454 * PCMA: G.711 A-law (RFC3551)
456 static void *pcma_init (demux_t *demux)
460 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('a', 'l', 'a', 'w'));
461 fmt.audio.i_rate = 8000;
462 fmt.audio.i_channels = 1;
463 return codec_init (demux, &fmt);
467 * L16: 16-bits (network byte order) PCM
469 static void *l16s_init (demux_t *demux)
473 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('s', '1', '6', 'b'));
474 fmt.audio.i_rate = 44100;
475 fmt.audio.i_channels = 2;
476 return codec_init (demux, &fmt);
479 static void *l16m_init (demux_t *demux)
483 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('s', '1', '6', 'b'));
484 fmt.audio.i_rate = 44100;
485 fmt.audio.i_channels = 1;
486 return codec_init (demux, &fmt);
490 * MPA: MPEG Audio (RFC2250, §3.4)
492 static void *mpa_init (demux_t *demux)
496 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('m', 'p', 'g', 'a'));
497 fmt.audio.i_channels = 2;
498 return codec_init (demux, &fmt);
501 static void mpa_decode (demux_t *demux, void *data, block_t *block)
503 if (block->i_buffer < 4)
505 block_Release (block);
509 block->i_buffer -= 4; /* 32-bits RTP/MPA header */
510 block->p_buffer += 4;
512 codec_decode (demux, data, block);
517 * MPV: MPEG Video (RFC2250, §3.5)
519 static void *mpv_init (demux_t *demux)
523 es_format_Init (&fmt, VIDEO_ES, VLC_FOURCC ('m', 'p', 'g', 'v'));
524 return codec_init (demux, &fmt);
527 static void mpv_decode (demux_t *demux, void *data, block_t *block)
529 if (block->i_buffer < 4)
531 block_Release (block);
535 block->i_buffer -= 4; /* 32-bits RTP/MPV header */
536 block->p_buffer += 4;
538 if (block->p_buffer[-3] & 0x4)
540 /* MPEG2 Video extension header */
541 /* TODO: shouldn't we skip this too ? */
544 codec_decode (demux, data, block);
549 * MP2: MPEG TS (RFC2250, §2)
551 static void *ts_init (demux_t *demux)
553 return stream_init (demux, "ts");
558 * Dynamic payload type handlers
559 * Hmm, none implemented yet.
563 * Processing callback
565 static int Demux (demux_t *demux)
567 demux_sys_t *p_sys = demux->p_sys;
570 block = p_sys->framed_rtp
571 ? rtp_dgram_recv (demux, p_sys->fd)
572 : rtp_stream_recv (demux, p_sys->fd);
576 if (block->i_buffer < 2)
579 const uint8_t ptype = block->p_buffer[1] & 0x7F;
580 if (ptype >= 72 && ptype <= 76)
581 goto drop; /* Muxed RTCP, ignore for now */
583 /* Not using SDP, we need to guess the payload format used */
584 /* see http://www.iana.org/assignments/rtp-parameters */
585 if (p_sys->autodetect)
589 .destroy = codec_destroy,
590 .decode = codec_decode,
597 msg_Dbg (demux, "detected G.711 mu-law");
603 msg_Dbg (demux, "detected G.711 A-law");
609 msg_Dbg (demux, "detected stereo PCM");
611 pt.frequency = 44100;
615 msg_Dbg (demux, "detected mono PCM");
617 pt.frequency = 44100;
621 msg_Dbg (demux, "detected MPEG Audio");
623 pt.decode = mpa_decode;
624 pt.frequency = 44100;
628 msg_Dbg (demux, "detected MPEG Video");
630 pt.decode = mpv_decode;
631 pt.frequency = 90000;
635 msg_Dbg (demux, "detected MPEG2 TS");
637 pt.destroy = stream_destroy;
638 pt.decode = stream_decode;
639 pt.frequency = 90000;
645 rtp_add_type (demux, p_sys->session, &pt);
646 p_sys->autodetect = false;
648 rtp_receive (demux, p_sys->session, block);
652 block_Release (block);