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>
44 #define RTP_CACHING_TEXT N_("RTP de-jitter buffer length (msec)")
45 #define RTP_CACHING_LONGTEXT N_( \
46 "How long to wait for late RTP packets (and delay the performance)." )
48 #define SRTP_KEY_TEXT N_("SRTP key (hexadecimal)")
49 #define SRTP_KEY_LONGTEXT N_( \
50 "RTP packets will be authenticated and deciphered "\
51 "with this Secure RTP master shared secret key.")
53 #define SRTP_SALT_TEXT N_("SRTP salt (hexadecimal)")
54 #define SRTP_SALT_LONGTEXT N_( \
55 "Secure RTP requires a (non-secret) master salt value.")
57 #define RTP_MAX_SRC_TEXT N_("Maximum RTP sources")
58 #define RTP_MAX_SRC_LONGTEXT N_( \
59 "How many distinct active RTP sources are allowed at a time." )
61 #define RTP_TIMEOUT_TEXT N_("RTP source timeout (sec)")
62 #define RTP_TIMEOUT_LONGTEXT N_( \
63 "How long to wait for any packet before a source is expired.")
65 #define RTP_MAX_DROPOUT_TEXT N_("Maximum RTP sequence number dropout")
66 #define RTP_MAX_DROPOUT_LONGTEXT N_( \
67 "RTP packets will be discarded if they are too much ahead (i.e. in the " \
68 "future) by this many packets from the last received packet." )
70 #define RTP_MAX_MISORDER_TEXT N_("Maximum RTP sequence number misordering")
71 #define RTP_MAX_MISORDER_LONGTEXT N_( \
72 "RTP packets will be discarded if they are too far behind (i.e. in the " \
73 "past) by this many packets from the last received packet." )
75 static int Open (vlc_object_t *);
76 static void Close (vlc_object_t *);
82 set_shortname (_("RTP"));
83 set_description (_("(Experimental) Real-Time Protocol demuxer"));
84 set_category (CAT_INPUT);
85 set_subcategory (SUBCAT_INPUT_DEMUX);
86 set_capability ("access_demux", 10);
87 set_callbacks (Open, Close);
89 add_integer ("rtp-caching", 1000, NULL, RTP_CACHING_TEXT,
90 RTP_CACHING_LONGTEXT, true);
91 change_integer_range (0, 65535);
92 add_string ("srtp-key", "", NULL,
93 SRTP_KEY_TEXT, SRTP_KEY_LONGTEXT, false);
94 add_string ("srtp-salt", "", NULL,
95 SRTP_SALT_TEXT, SRTP_SALT_LONGTEXT, false);
96 add_integer ("rtp-max-src", 1, NULL, RTP_MAX_SRC_TEXT,
97 RTP_MAX_SRC_LONGTEXT, true);
98 change_integer_range (1, 255);
99 add_integer ("rtp-timeout", 5, NULL, RTP_TIMEOUT_TEXT,
100 RTP_TIMEOUT_LONGTEXT, true);
101 add_integer ("rtp-max-dropout", 3000, NULL, RTP_MAX_DROPOUT_TEXT,
102 RTP_MAX_DROPOUT_LONGTEXT, true);
103 change_integer_range (0, 32767);
104 add_integer ("rtp-max-misorder", 100, NULL, RTP_MAX_MISORDER_TEXT,
105 RTP_MAX_MISORDER_LONGTEXT, true);
106 change_integer_range (0, 32767);
108 add_shortcut ("dccp");
109 /*add_shortcut ("sctp");*/
110 add_shortcut ("rtptcp"); /* "tcp" is already taken :( */
111 add_shortcut ("rtp");
112 add_shortcut ("udplite");
116 * TODO: so much stuff
117 * - send RTCP-RR and RTCP-BYE
118 * - dynamic payload types (need SDP parser)
119 * - multiple medias (need SDP parser, and RTCP-SR parser for lip-sync)
120 * - support for access_filter in case of stream_Demux (MPEG-TS)
124 # define IPPROTO_DCCP 33 /* IANA */
127 #ifndef IPPROTO_UDPLITE
128 # define IPPROTO_UDPLITE 136 /* from IANA */
135 static int Demux (demux_t *);
136 static int Control (demux_t *, int i_query, va_list args);
137 static int extract_port (char **phost);
140 * Probes and initializes.
142 static int Open (vlc_object_t *obj)
144 demux_t *demux = (demux_t *)obj;
145 int tp; /* transport protocol */
147 if (!strcmp (demux->psz_access, "dccp"))
150 if (!strcmp (demux->psz_access, "rtptcp"))
153 if (!strcmp (demux->psz_access, "rtp"))
156 if (!strcmp (demux->psz_access, "udplite"))
157 tp = IPPROTO_UDPLITE;
161 char *tmp = strdup (demux->psz_path);
166 char *dhost = strchr (shost, '@');
170 /* Parses the port numbers */
171 int sport = 0, dport = 0;
172 sport = extract_port (&shost);
174 dport = extract_port (&dhost);
176 dport = 5004; /* avt-profile-1 port */
179 int fd = -1, rtcp_fd = -1;
184 case IPPROTO_UDPLITE:
185 fd = net_OpenDgram (obj, dhost, (dport + 1) & ~1,
186 shost, (sport + 1) & ~1, AF_UNSPEC, tp);
189 rtcp_fd = net_OpenDgram (obj, dhost, dport | 1, shost,
190 sport ? (sport | 1) : 0, AF_UNSPEC, tp);
194 #ifndef SOCK_DCCP /* provisional API (FIXME) */
200 var_Create (obj, "dccp-service", VLC_VAR_STRING);
201 var_SetString (obj, "dccp-service", "RTPV"); /* FIXME: RTPA? */
202 fd = net_Connect (obj, shost, (sport + 1) & ~1, SOCK_DCCP, tp);
204 msg_Err (obj, "DCCP support not included");
209 fd = net_Connect (obj, shost, (sport + 1) & ~1, SOCK_STREAM, tp);
216 net_SetCSCov (fd, -1, 12);
218 /* Initializes demux */
219 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
230 p_sys->caching = var_CreateGetInteger (obj, "rtp-caching");
231 p_sys->max_src = var_CreateGetInteger (obj, "rtp-max-src");
232 p_sys->timeout = var_CreateGetInteger (obj, "rtp-timeout");
233 p_sys->max_dropout = var_CreateGetInteger (obj, "rtp-max-dropout");
234 p_sys->max_misorder = var_CreateGetInteger (obj, "rtp-max-misorder");
235 p_sys->autodetect = true;
236 p_sys->framed_rtp = (tp == IPPROTO_TCP);
238 demux->pf_demux = Demux;
239 demux->pf_control = Control;
240 demux->p_sys = p_sys;
242 p_sys->session = rtp_session_create (demux);
243 if (p_sys->session == NULL)
246 char *key = var_CreateGetNonEmptyString (demux, "srtp-key");
249 p_sys->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
250 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
251 if (p_sys->srtp == NULL)
257 char *salt = var_CreateGetNonEmptyString (demux, "srtp-salt");
258 errno = srtp_setkeystring (p_sys->srtp, key, salt ? salt : "");
263 msg_Err (obj, "bad SRTP key/salt combination (%m)");
279 static void Close (vlc_object_t *obj)
281 demux_t *demux = (demux_t *)obj;
282 demux_sys_t *p_sys = demux->p_sys;
285 srtp_destroy (p_sys->srtp);
287 rtp_session_destroy (demux, p_sys->session);
288 if (p_sys->rtcp_fd != -1)
289 net_Close (p_sys->rtcp_fd);
290 net_Close (p_sys->fd);
296 * Extracts port number from "[host]:port" or "host:port" strings,
297 * and remove brackets from the host name.
298 * @param phost pointer to the string upon entry,
299 * pointer to the hostname upon return.
300 * @return port number, 0 if missing.
302 static int extract_port (char **phost)
304 char *host = *phost, *port;
308 host = *++phost; /* skip '[' */
309 port = strchr (host, ']');
311 *port++ = '\0'; /* skip ']' */
314 port = strchr (host, ':');
318 *port++ = '\0'; /* skip ':' */
326 static int Control (demux_t *demux, int i_query, va_list args)
328 demux_sys_t *p_sys = demux->p_sys;
332 case DEMUX_GET_POSITION:
334 float *v = va_arg (args, float *);
339 case DEMUX_GET_LENGTH:
342 int64_t *v = va_arg (args, int64_t *);
347 case DEMUX_GET_PTS_DELAY:
349 int64_t *v = va_arg (args, int64_t *);
360 * Checks if a file descriptor is hung up.
362 static bool fd_dead (int fd)
364 struct pollfd ufd = { .fd = fd, };
366 return (poll (&ufd, 1, 0) == 1) && (ufd.revents & POLLHUP);
371 * Gets a datagram from the network, or NULL in case of fatal error.
373 static block_t *rtp_dgram_recv (demux_t *demux, int fd)
375 block_t *block = block_Alloc (0xffff);
380 len = net_Read (VLC_OBJECT (demux), fd, NULL,
381 block->p_buffer, block->i_buffer, false);
382 if (((len <= 0) && fd_dead (fd))
383 || !vlc_object_alive (demux))
385 block_Release (block);
391 return block_Realloc (block, 0, len);
395 * Gets a framed RTP packet, or NULL in case of fatal error.
397 static block_t *rtp_stream_recv (demux_t *demux, int fd)
400 uint8_t hdr[2]; /* frame header */
402 /* Receives the RTP frame header */
405 ssize_t val = net_Read (VLC_OBJECT (demux), fd, NULL,
406 hdr + len, 2 - len, false);
413 block_t *block = block_Alloc (GetWBE (hdr));
415 /* Receives the RTP packet */
416 for (ssize_t i = 0; i < len;)
420 val = net_Read (VLC_OBJECT (demux), fd, NULL,
421 block->p_buffer + i, block->i_buffer - i, false);
424 block_Release (block);
435 * Generic packet handlers
438 static void *codec_init (demux_t *demux, es_format_t *fmt)
440 return es_out_Add (demux->out, fmt);
443 static void codec_destroy (demux_t *demux, void *data)
446 es_out_Del (demux->out, (es_out_id_t *)data);
449 /* Send a packet to decoder */
450 static void codec_decode (demux_t *demux, void *data, block_t *block)
454 block->i_dts = 0; /* RTP does not specify this */
455 es_out_Control (demux->out, ES_OUT_SET_PCR,
456 block->i_pts - demux->p_sys->caching * 1000);
457 es_out_Send (demux->out, (es_out_id_t *)data, block);
460 block_Release (block);
464 static void *stream_init (demux_t *demux, const char *name)
466 return stream_DemuxNew (demux, name, demux->out);
469 static void stream_destroy (demux_t *demux, void *data)
472 stream_DemuxDelete ((stream_t *)data);
476 /* Send a packet to a chained demuxer */
477 static void stream_decode (demux_t *demux, void *data, block_t *block)
480 stream_DemuxSend ((stream_t *)data, block);
482 block_Release (block);
487 * Static payload types handler
491 * PCMU: G.711 µ-law (RFC3551)
493 static void *pcmu_init (demux_t *demux)
497 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('u', 'l', 'a', 'w'));
498 fmt.audio.i_rate = 8000;
499 fmt.audio.i_channels = 1;
500 return codec_init (demux, &fmt);
504 * PCMA: G.711 A-law (RFC3551)
506 static void *pcma_init (demux_t *demux)
510 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('a', 'l', 'a', 'w'));
511 fmt.audio.i_rate = 8000;
512 fmt.audio.i_channels = 1;
513 return codec_init (demux, &fmt);
517 * L16: 16-bits (network byte order) PCM
519 static void *l16s_init (demux_t *demux)
523 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('s', '1', '6', 'b'));
524 fmt.audio.i_rate = 44100;
525 fmt.audio.i_channels = 2;
526 return codec_init (demux, &fmt);
529 static void *l16m_init (demux_t *demux)
533 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('s', '1', '6', 'b'));
534 fmt.audio.i_rate = 44100;
535 fmt.audio.i_channels = 1;
536 return codec_init (demux, &fmt);
540 * MPA: MPEG Audio (RFC2250, §3.4)
542 static void *mpa_init (demux_t *demux)
546 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('m', 'p', 'g', 'a'));
547 fmt.audio.i_channels = 2;
548 return codec_init (demux, &fmt);
551 static void mpa_decode (demux_t *demux, void *data, block_t *block)
553 if (block->i_buffer < 4)
555 block_Release (block);
559 block->i_buffer -= 4; /* 32-bits RTP/MPA header */
560 block->p_buffer += 4;
562 codec_decode (demux, data, block);
567 * MPV: MPEG Video (RFC2250, §3.5)
569 static void *mpv_init (demux_t *demux)
573 es_format_Init (&fmt, VIDEO_ES, VLC_FOURCC ('m', 'p', 'g', 'v'));
574 return codec_init (demux, &fmt);
577 static void mpv_decode (demux_t *demux, void *data, block_t *block)
579 if (block->i_buffer < 4)
581 block_Release (block);
585 block->i_buffer -= 4; /* 32-bits RTP/MPV header */
586 block->p_buffer += 4;
588 if (block->p_buffer[-3] & 0x4)
590 /* MPEG2 Video extension header */
591 /* TODO: shouldn't we skip this too ? */
594 codec_decode (demux, data, block);
599 * MP2: MPEG TS (RFC2250, §2)
601 static void *ts_init (demux_t *demux)
603 return stream_init (demux, "ts");
608 * Dynamic payload type handlers
609 * Hmm, none implemented yet.
613 * Processing callback
615 static int Demux (demux_t *demux)
617 demux_sys_t *p_sys = demux->p_sys;
620 block = p_sys->framed_rtp
621 ? rtp_stream_recv (demux, p_sys->fd)
622 : rtp_dgram_recv (demux, p_sys->fd);
626 if (block->i_buffer < 2)
629 const uint8_t ptype = block->p_buffer[1] & 0x7F;
630 if (ptype >= 72 && ptype <= 76)
631 goto drop; /* Muxed RTCP, ignore for now */
635 size_t len = block->i_buffer;
636 if (srtp_recv (p_sys->srtp, block->p_buffer, &len))
638 msg_Dbg (demux, "SRTP authentication/decryption failed");
641 block->i_buffer = len;
644 /* Not using SDP, we need to guess the payload format used */
645 /* see http://www.iana.org/assignments/rtp-parameters */
646 if (p_sys->autodetect)
650 .destroy = codec_destroy,
651 .decode = codec_decode,
658 msg_Dbg (demux, "detected G.711 mu-law");
664 msg_Dbg (demux, "detected G.711 A-law");
670 msg_Dbg (demux, "detected stereo PCM");
672 pt.frequency = 44100;
676 msg_Dbg (demux, "detected mono PCM");
678 pt.frequency = 44100;
682 msg_Dbg (demux, "detected MPEG Audio");
684 pt.decode = mpa_decode;
685 pt.frequency = 90000;
689 msg_Dbg (demux, "detected MPEG Video");
691 pt.decode = mpv_decode;
692 pt.frequency = 90000;
696 msg_Dbg (demux, "detected MPEG2 TS");
698 pt.destroy = stream_destroy;
699 pt.decode = stream_decode;
700 pt.frequency = 90000;
706 rtp_add_type (demux, p_sys->session, &pt);
707 p_sys->autodetect = false;
709 rtp_receive (demux, p_sys->session, block);
713 block_Release (block);