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", 0)
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 :( */
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->rtcp_fd = rtcp_fd;
231 p_sys->caching = var_CreateGetInteger (obj, "rtp-caching");
232 p_sys->max_src = var_CreateGetInteger (obj, "rtp-max-src");
233 p_sys->timeout = var_CreateGetInteger (obj, "rtp-timeout");
234 p_sys->max_dropout = var_CreateGetInteger (obj, "rtp-max-dropout");
235 p_sys->max_misorder = var_CreateGetInteger (obj, "rtp-max-misorder");
236 p_sys->autodetect = true;
237 p_sys->framed_rtp = (tp == IPPROTO_TCP);
239 demux->pf_demux = Demux;
240 demux->pf_control = Control;
241 demux->p_sys = p_sys;
243 p_sys->session = rtp_session_create (demux);
244 if (p_sys->session == NULL)
247 char *key = var_CreateGetNonEmptyString (demux, "srtp-key");
250 p_sys->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
251 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
252 if (p_sys->srtp == NULL)
258 char *salt = var_CreateGetNonEmptyString (demux, "srtp-salt");
259 errno = srtp_setkeystring (p_sys->srtp, key, salt ? salt : "");
264 msg_Err (obj, "bad SRTP key/salt combination (%m)");
280 static void Close (vlc_object_t *obj)
282 demux_t *demux = (demux_t *)obj;
283 demux_sys_t *p_sys = demux->p_sys;
286 srtp_destroy (p_sys->srtp);
288 rtp_session_destroy (demux, p_sys->session);
289 if (p_sys->rtcp_fd != -1)
290 net_Close (p_sys->rtcp_fd);
291 net_Close (p_sys->fd);
297 * Extracts port number from "[host]:port" or "host:port" strings,
298 * and remove brackets from the host name.
299 * @param phost pointer to the string upon entry,
300 * pointer to the hostname upon return.
301 * @return port number, 0 if missing.
303 static int extract_port (char **phost)
305 char *host = *phost, *port;
309 host = ++*phost; /* skip '[' */
310 port = strchr (host, ']');
312 *port++ = '\0'; /* skip ']' */
315 port = strchr (host, ':');
319 *port++ = '\0'; /* skip ':' */
327 static int Control (demux_t *demux, int i_query, va_list args)
329 demux_sys_t *p_sys = demux->p_sys;
333 case DEMUX_GET_POSITION:
335 float *v = va_arg (args, float *);
340 case DEMUX_GET_LENGTH:
343 int64_t *v = va_arg (args, int64_t *);
348 case DEMUX_GET_PTS_DELAY:
350 int64_t *v = va_arg (args, int64_t *);
351 *v = p_sys->caching * 1000;
355 case DEMUX_CAN_PAUSE:
357 case DEMUX_CAN_CONTROL_PACE:
359 bool *v = (bool*)va_arg( args, bool * );
370 * Checks if a file descriptor is hung up.
372 static bool fd_dead (int fd)
374 struct pollfd ufd = { .fd = fd, };
376 return (poll (&ufd, 1, 0) == 1) && (ufd.revents & POLLHUP);
381 * Gets a datagram from the network, or NULL in case of fatal error.
383 static block_t *rtp_dgram_recv (demux_t *demux, int fd)
385 block_t *block = block_Alloc (0xffff);
390 len = net_Read (VLC_OBJECT (demux), fd, NULL,
391 block->p_buffer, block->i_buffer, false);
392 if (((len <= 0) && fd_dead (fd))
393 || !vlc_object_alive (demux))
395 block_Release (block);
401 return block_Realloc (block, 0, len);
405 * Gets a framed RTP packet, or NULL in case of fatal error.
407 static block_t *rtp_stream_recv (demux_t *demux, int fd)
410 uint8_t hdr[2]; /* frame header */
412 /* Receives the RTP frame header */
415 ssize_t val = net_Read (VLC_OBJECT (demux), fd, NULL,
416 hdr + len, 2 - len, false);
423 block_t *block = block_Alloc (GetWBE (hdr));
425 /* Receives the RTP packet */
426 for (ssize_t i = 0; i < len;)
430 val = net_Read (VLC_OBJECT (demux), fd, NULL,
431 block->p_buffer + i, block->i_buffer - i, false);
434 block_Release (block);
445 * Generic packet handlers
448 static void *codec_init (demux_t *demux, es_format_t *fmt)
450 return es_out_Add (demux->out, fmt);
453 static void codec_destroy (demux_t *demux, void *data)
456 es_out_Del (demux->out, (es_out_id_t *)data);
459 /* Send a packet to decoder */
460 static void codec_decode (demux_t *demux, void *data, block_t *block)
464 block->i_dts = 0; /* RTP does not specify this */
465 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts );
466 es_out_Send (demux->out, (es_out_id_t *)data, block);
469 block_Release (block);
473 static void *stream_init (demux_t *demux, const char *name)
475 return stream_DemuxNew (demux, name, demux->out);
478 static void stream_destroy (demux_t *demux, void *data)
481 stream_DemuxDelete ((stream_t *)data);
485 /* Send a packet to a chained demuxer */
486 static void stream_decode (demux_t *demux, void *data, block_t *block)
489 stream_DemuxSend ((stream_t *)data, block);
491 block_Release (block);
496 * Static payload types handler
500 * PCMU: G.711 µ-law (RFC3551)
502 static void *pcmu_init (demux_t *demux)
506 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('u', 'l', 'a', 'w'));
507 fmt.audio.i_rate = 8000;
508 fmt.audio.i_channels = 1;
509 return codec_init (demux, &fmt);
513 * PCMA: G.711 A-law (RFC3551)
515 static void *pcma_init (demux_t *demux)
519 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('a', 'l', 'a', 'w'));
520 fmt.audio.i_rate = 8000;
521 fmt.audio.i_channels = 1;
522 return codec_init (demux, &fmt);
526 * L16: 16-bits (network byte order) PCM
528 static void *l16s_init (demux_t *demux)
532 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('s', '1', '6', 'b'));
533 fmt.audio.i_rate = 44100;
534 fmt.audio.i_channels = 2;
535 return codec_init (demux, &fmt);
538 static void *l16m_init (demux_t *demux)
542 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('s', '1', '6', 'b'));
543 fmt.audio.i_rate = 44100;
544 fmt.audio.i_channels = 1;
545 return codec_init (demux, &fmt);
549 * MPA: MPEG Audio (RFC2250, §3.4)
551 static void *mpa_init (demux_t *demux)
555 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('m', 'p', 'g', 'a'));
556 fmt.audio.i_channels = 2;
557 return codec_init (demux, &fmt);
560 static void mpa_decode (demux_t *demux, void *data, block_t *block)
562 if (block->i_buffer < 4)
564 block_Release (block);
568 block->i_buffer -= 4; /* 32-bits RTP/MPA header */
569 block->p_buffer += 4;
571 codec_decode (demux, data, block);
576 * MPV: MPEG Video (RFC2250, §3.5)
578 static void *mpv_init (demux_t *demux)
582 es_format_Init (&fmt, VIDEO_ES, VLC_FOURCC ('m', 'p', 'g', 'v'));
583 return codec_init (demux, &fmt);
586 static void mpv_decode (demux_t *demux, void *data, block_t *block)
588 if (block->i_buffer < 4)
590 block_Release (block);
594 block->i_buffer -= 4; /* 32-bits RTP/MPV header */
595 block->p_buffer += 4;
597 if (block->p_buffer[-3] & 0x4)
599 /* MPEG2 Video extension header */
600 /* TODO: shouldn't we skip this too ? */
603 codec_decode (demux, data, block);
608 * MP2: MPEG TS (RFC2250, §2)
610 static void *ts_init (demux_t *demux)
612 return stream_init (demux, *demux->psz_demux ? demux->psz_demux : "ts");
617 * Dynamic payload type handlers
618 * Hmm, none implemented yet.
622 * Processing callback
624 static int Demux (demux_t *demux)
626 demux_sys_t *p_sys = demux->p_sys;
629 block = p_sys->framed_rtp
630 ? rtp_stream_recv (demux, p_sys->fd)
631 : rtp_dgram_recv (demux, p_sys->fd);
635 if (block->i_buffer < 2)
638 const uint8_t ptype = block->p_buffer[1] & 0x7F;
639 if (ptype >= 72 && ptype <= 76)
640 goto drop; /* Muxed RTCP, ignore for now */
644 size_t len = block->i_buffer;
645 if (srtp_recv (p_sys->srtp, block->p_buffer, &len))
647 msg_Dbg (demux, "SRTP authentication/decryption failed");
650 block->i_buffer = len;
653 /* Not using SDP, we need to guess the payload format used */
654 /* see http://www.iana.org/assignments/rtp-parameters */
655 if (p_sys->autodetect)
659 .destroy = codec_destroy,
660 .decode = codec_decode,
667 msg_Dbg (demux, "detected G.711 mu-law");
673 msg_Dbg (demux, "detected G.711 A-law");
679 msg_Dbg (demux, "detected stereo PCM");
681 pt.frequency = 44100;
685 msg_Dbg (demux, "detected mono PCM");
687 pt.frequency = 44100;
691 msg_Dbg (demux, "detected MPEG Audio");
693 pt.decode = mpa_decode;
694 pt.frequency = 90000;
698 msg_Dbg (demux, "detected MPEG Video");
700 pt.decode = mpv_decode;
701 pt.frequency = 90000;
705 msg_Dbg (demux, "detected MPEG2 TS");
707 pt.destroy = stream_destroy;
708 pt.decode = stream_decode;
709 pt.frequency = 90000;
715 rtp_add_type (demux, p_sys->session, &pt);
716 p_sys->autodetect = false;
718 rtp_receive (demux, p_sys->session, block);
722 block_Release (block);