3 * @brief Real-Time Protocol (RTP) demux module for VLC media player
5 /*****************************************************************************
6 * Copyright (C) 2001-2005 the VideoLAN team
7 * Copyright © 2007-2009 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
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 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 ****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_demux.h>
34 #include <vlc_network.h>
35 #include <vlc_plugin.h>
42 #define RTP_CACHING_TEXT N_("RTP de-jitter buffer length (msec)")
43 #define RTP_CACHING_LONGTEXT N_( \
44 "How long to wait for late RTP packets (and delay the performance)." )
46 #define RTCP_PORT_TEXT N_("RTCP (local) port")
47 #define RTCP_PORT_LONGTEXT N_( \
48 "RTCP packets will be received on this transport protocol port. " \
49 "If zero, multiplexed RTP/RTCP is used.")
51 #define SRTP_KEY_TEXT N_("SRTP key (hexadecimal)")
52 #define SRTP_KEY_LONGTEXT N_( \
53 "RTP packets will be authenticated and deciphered "\
54 "with this Secure RTP master shared secret key.")
56 #define SRTP_SALT_TEXT N_("SRTP salt (hexadecimal)")
57 #define SRTP_SALT_LONGTEXT N_( \
58 "Secure RTP requires a (non-secret) master salt value.")
60 #define RTP_MAX_SRC_TEXT N_("Maximum RTP sources")
61 #define RTP_MAX_SRC_LONGTEXT N_( \
62 "How many distinct active RTP sources are allowed at a time." )
64 #define RTP_TIMEOUT_TEXT N_("RTP source timeout (sec)")
65 #define RTP_TIMEOUT_LONGTEXT N_( \
66 "How long to wait for any packet before a source is expired.")
68 #define RTP_MAX_DROPOUT_TEXT N_("Maximum RTP sequence number dropout")
69 #define RTP_MAX_DROPOUT_LONGTEXT N_( \
70 "RTP packets will be discarded if they are too much ahead (i.e. in the " \
71 "future) by this many packets from the last received packet." )
73 #define RTP_MAX_MISORDER_TEXT N_("Maximum RTP sequence number misordering")
74 #define RTP_MAX_MISORDER_LONGTEXT N_( \
75 "RTP packets will be discarded if they are too far behind (i.e. in the " \
76 "past) by this many packets from the last received packet." )
78 #define RTP_DYNAMIC_PT_TEXT N_("RTP payload format assumed for dynamic " \
80 #define RTP_DYNAMIC_PT_LONGTEXT N_( \
81 "This payload format will be assumed for dynamic payload types " \
82 "(between 96 and 127) if it can't be determined otherwise with " \
83 "out-of-band mappings (SDP)" )
85 static const char *const dynamic_pt_list[] = { "theora" };
86 static const char *const dynamic_pt_list_text[] = { "Theora Encoded Video" };
88 static int Open (vlc_object_t *);
89 static void Close (vlc_object_t *);
95 set_shortname (N_("RTP"))
96 set_description (N_("Real-Time Protocol (RTP) input"))
97 set_category (CAT_INPUT)
98 set_subcategory (SUBCAT_INPUT_DEMUX)
99 set_capability ("access_demux", 0)
100 set_callbacks (Open, Close)
102 add_integer ("rtp-caching", 1000, RTP_CACHING_TEXT,
103 RTP_CACHING_LONGTEXT, true)
104 change_integer_range (0, 65535)
106 add_integer ("rtcp-port", 0, RTCP_PORT_TEXT,
107 RTCP_PORT_LONGTEXT, false)
108 change_integer_range (0, 65535)
111 add_string ("srtp-key", "",
112 SRTP_KEY_TEXT, SRTP_KEY_LONGTEXT, false)
113 add_string ("srtp-salt", "",
114 SRTP_SALT_TEXT, SRTP_SALT_LONGTEXT, false)
116 add_integer ("rtp-max-src", 1, RTP_MAX_SRC_TEXT,
117 RTP_MAX_SRC_LONGTEXT, true)
118 change_integer_range (1, 255)
119 add_integer ("rtp-timeout", 5, RTP_TIMEOUT_TEXT,
120 RTP_TIMEOUT_LONGTEXT, true)
121 add_integer ("rtp-max-dropout", 3000, RTP_MAX_DROPOUT_TEXT,
122 RTP_MAX_DROPOUT_LONGTEXT, true)
123 change_integer_range (0, 32767)
124 add_integer ("rtp-max-misorder", 100, RTP_MAX_MISORDER_TEXT,
125 RTP_MAX_MISORDER_LONGTEXT, true)
126 change_integer_range (0, 32767)
127 add_string ("rtp-dynamic-pt", NULL, RTP_DYNAMIC_PT_TEXT,
128 RTP_DYNAMIC_PT_LONGTEXT, true)
129 change_string_list (dynamic_pt_list, dynamic_pt_list_text, NULL)
131 /*add_shortcut ("sctp")*/
132 add_shortcut ("dccp", "rtptcp", /* "tcp" is already taken :( */
137 * TODO: so much stuff
138 * - send RTCP-RR and RTCP-BYE
139 * - dynamic payload types (need SDP parser)
140 * - multiple medias (need SDP parser, and RTCP-SR parser for lip-sync)
141 * - support for stream_filter in case of stream_Demux (MPEG-TS)
145 # define IPPROTO_DCCP 33 /* IANA */
148 #ifndef IPPROTO_UDPLITE
149 # define IPPROTO_UDPLITE 136 /* from IANA */
156 static int Control (demux_t *, int i_query, va_list args);
157 static int extract_port (char **phost);
160 * Probes and initializes.
162 static int Open (vlc_object_t *obj)
164 demux_t *demux = (demux_t *)obj;
165 int tp; /* transport protocol */
167 if (!strcmp (demux->psz_access, "dccp"))
170 if (!strcmp (demux->psz_access, "rtptcp"))
173 if (!strcmp (demux->psz_access, "rtp"))
176 if (!strcmp (demux->psz_access, "udplite"))
177 tp = IPPROTO_UDPLITE;
181 char *tmp = strdup (demux->psz_location);
186 char *dhost = strchr (tmp, '@');
198 /* Parses the port numbers */
199 int sport = 0, dport = 0;
201 sport = extract_port (&shost);
203 dport = extract_port (&dhost);
205 dport = 5004; /* avt-profile-1 port */
207 int rtcp_dport = var_CreateGetInteger (obj, "rtcp-port");
210 int fd = -1, rtcp_fd = -1;
215 case IPPROTO_UDPLITE:
216 fd = net_OpenDgram (obj, dhost, dport,
217 shost, sport, AF_UNSPEC, tp);
220 if (rtcp_dport > 0) /* XXX: source port is unknown */
221 rtcp_fd = net_OpenDgram (obj, dhost, rtcp_dport, shost, 0,
226 #ifndef SOCK_DCCP /* provisional API (FIXME) */
232 var_Create (obj, "dccp-service", VLC_VAR_STRING);
233 var_SetString (obj, "dccp-service", "RTPV"); /* FIXME: RTPA? */
234 fd = net_Connect (obj, dhost, dport, SOCK_DCCP, tp);
236 msg_Err (obj, "DCCP support not included");
241 fd = net_Connect (obj, dhost, dport, SOCK_STREAM, tp);
248 net_SetCSCov (fd, -1, 12);
250 /* Initializes demux */
251 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
264 p_sys->rtcp_fd = rtcp_fd;
265 p_sys->caching = var_CreateGetInteger (obj, "rtp-caching");
266 p_sys->max_src = var_CreateGetInteger (obj, "rtp-max-src");
267 p_sys->timeout = var_CreateGetInteger (obj, "rtp-timeout")
269 p_sys->max_dropout = var_CreateGetInteger (obj, "rtp-max-dropout");
270 p_sys->max_misorder = var_CreateGetInteger (obj, "rtp-max-misorder");
271 p_sys->thread_ready = false;
272 p_sys->autodetect = true;
274 demux->pf_demux = NULL;
275 demux->pf_control = Control;
276 demux->p_sys = p_sys;
278 p_sys->session = rtp_session_create (demux);
279 if (p_sys->session == NULL)
283 char *key = var_CreateGetNonEmptyString (demux, "srtp-key");
286 p_sys->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
287 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
288 if (p_sys->srtp == NULL)
294 char *salt = var_CreateGetNonEmptyString (demux, "srtp-salt");
295 errno = srtp_setkeystring (p_sys->srtp, key, salt ? salt : "");
300 msg_Err (obj, "bad SRTP key/salt combination (%m)");
306 if (vlc_clone (&p_sys->thread,
307 (tp != IPPROTO_TCP) ? rtp_dgram_thread : rtp_stream_thread,
308 demux, VLC_THREAD_PRIORITY_INPUT))
310 p_sys->thread_ready = true;
322 static void Close (vlc_object_t *obj)
324 demux_t *demux = (demux_t *)obj;
325 demux_sys_t *p_sys = demux->p_sys;
327 if (p_sys->thread_ready)
329 vlc_cancel (p_sys->thread);
330 vlc_join (p_sys->thread, NULL);
335 srtp_destroy (p_sys->srtp);
338 rtp_session_destroy (demux, p_sys->session);
339 if (p_sys->rtcp_fd != -1)
340 net_Close (p_sys->rtcp_fd);
341 net_Close (p_sys->fd);
347 * Extracts port number from "[host]:port" or "host:port" strings,
348 * and remove brackets from the host name.
349 * @param phost pointer to the string upon entry,
350 * pointer to the hostname upon return.
351 * @return port number, 0 if missing.
353 static int extract_port (char **phost)
355 char *host = *phost, *port;
359 host = ++*phost; /* skip '[' */
360 port = strchr (host, ']');
362 *port++ = '\0'; /* skip ']' */
365 port = strchr (host, ':');
369 *port++ = '\0'; /* skip ':' */
377 static int Control (demux_t *demux, int i_query, va_list args)
379 demux_sys_t *p_sys = demux->p_sys;
383 case DEMUX_GET_POSITION:
385 float *v = va_arg (args, float *);
390 case DEMUX_GET_LENGTH:
393 int64_t *v = va_arg (args, int64_t *);
398 case DEMUX_GET_PTS_DELAY:
400 int64_t *v = va_arg (args, int64_t *);
401 *v = (int64_t)p_sys->caching * 1000;
405 case DEMUX_CAN_PAUSE:
407 case DEMUX_CAN_CONTROL_PACE:
409 bool *v = (bool*)va_arg( args, bool * );
420 * Generic packet handlers
423 void *codec_init (demux_t *demux, es_format_t *fmt)
425 return es_out_Add (demux->out, fmt);
428 void codec_destroy (demux_t *demux, void *data)
431 es_out_Del (demux->out, (es_out_id_t *)data);
434 /* Send a packet to decoder */
435 void codec_decode (demux_t *demux, void *data, block_t *block)
439 block->i_dts = VLC_TS_INVALID; /* RTP does not specify this */
440 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts );
441 es_out_Send (demux->out, (es_out_id_t *)data, block);
444 block_Release (block);
447 static void *stream_init (demux_t *demux, const char *name)
449 return stream_DemuxNew (demux, name, demux->out);
452 static void stream_destroy (demux_t *demux, void *data)
455 stream_Delete ((stream_t *)data);
459 /* Send a packet to a chained demuxer */
460 static void stream_decode (demux_t *demux, void *data, block_t *block)
463 stream_DemuxSend ((stream_t *)data, block);
465 block_Release (block);
469 static void *demux_init (demux_t *demux)
471 return stream_init (demux, demux->psz_demux);
475 * Static payload types handler
479 * PCMU: G.711 µ-law (RFC3551)
481 static void *pcmu_init (demux_t *demux)
485 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MULAW);
486 fmt.audio.i_rate = 8000;
487 fmt.audio.i_channels = 1;
488 return codec_init (demux, &fmt);
494 static void *gsm_init (demux_t *demux)
498 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_GSM);
499 fmt.audio.i_rate = 8000;
500 fmt.audio.i_channels = 1;
501 return codec_init (demux, &fmt);
505 * PCMA: G.711 A-law (RFC3551)
507 static void *pcma_init (demux_t *demux)
511 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_ALAW);
512 fmt.audio.i_rate = 8000;
513 fmt.audio.i_channels = 1;
514 return codec_init (demux, &fmt);
518 * L16: 16-bits (network byte order) PCM
520 static void *l16s_init (demux_t *demux)
524 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16B);
525 fmt.audio.i_rate = 44100;
526 fmt.audio.i_channels = 2;
527 return codec_init (demux, &fmt);
530 static void *l16m_init (demux_t *demux)
534 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16B);
535 fmt.audio.i_rate = 44100;
536 fmt.audio.i_channels = 1;
537 return codec_init (demux, &fmt);
543 static void *qcelp_init (demux_t *demux)
547 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_QCELP);
548 fmt.audio.i_rate = 8000;
549 fmt.audio.i_channels = 1;
550 return codec_init (demux, &fmt);
554 * MPA: MPEG Audio (RFC2250, §3.4)
556 static void *mpa_init (demux_t *demux)
560 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MPGA);
561 fmt.audio.i_channels = 2;
562 fmt.b_packetized = false;
563 return codec_init (demux, &fmt);
566 static void mpa_decode (demux_t *demux, void *data, block_t *block)
568 if (block->i_buffer < 4)
570 block_Release (block);
574 block->i_buffer -= 4; /* 32-bits RTP/MPA header */
575 block->p_buffer += 4;
577 codec_decode (demux, data, block);
582 * MPV: MPEG Video (RFC2250, §3.5)
584 static void *mpv_init (demux_t *demux)
588 es_format_Init (&fmt, VIDEO_ES, VLC_CODEC_MPGV);
589 fmt.b_packetized = false;
590 return codec_init (demux, &fmt);
593 static void mpv_decode (demux_t *demux, void *data, block_t *block)
595 if (block->i_buffer < 4)
597 block_Release (block);
601 block->i_buffer -= 4; /* 32-bits RTP/MPV header */
602 block->p_buffer += 4;
604 if (block->p_buffer[-3] & 0x4)
606 /* MPEG2 Video extension header */
607 /* TODO: shouldn't we skip this too ? */
610 codec_decode (demux, data, block);
615 * MP2: MPEG TS (RFC2250, §2)
617 static void *ts_init (demux_t *demux)
619 return stream_init (demux, *demux->psz_demux ? demux->psz_demux : "ts");
623 /* Not using SDP, we need to guess the payload format used */
624 /* see http://www.iana.org/assignments/rtp-parameters */
625 int rtp_autodetect (demux_t *demux, rtp_session_t *session,
626 const block_t *block)
628 uint8_t ptype = rtp_ptype (block);
631 .destroy = codec_destroy,
632 .decode = codec_decode,
637 /* Remember to keep this in sync with modules/services_discovery/sap.c */
641 msg_Dbg (demux, "detected G.711 mu-law");
647 msg_Dbg (demux, "detected GSM");
653 msg_Dbg (demux, "detected G.711 A-law");
659 msg_Dbg (demux, "detected stereo PCM");
661 pt.frequency = 44100;
665 msg_Dbg (demux, "detected mono PCM");
667 pt.frequency = 44100;
671 msg_Dbg (demux, "detected QCELP");
672 pt.init = qcelp_init;
677 msg_Dbg (demux, "detected MPEG Audio");
679 pt.decode = mpa_decode;
680 pt.frequency = 90000;
684 msg_Dbg (demux, "detected MPEG Video");
686 pt.decode = mpv_decode;
687 pt.frequency = 90000;
691 msg_Dbg (demux, "detected MPEG2 TS");
693 pt.destroy = stream_destroy;
694 pt.decode = stream_decode;
695 pt.frequency = 90000;
700 * If the rtp payload type is unknown then check demux if it is specified
702 if ((strcmp(demux->psz_demux, "h264") == 0) || (strcmp(demux->psz_demux, "ts") == 0))
704 msg_Dbg (demux, "rtp autodetect specified demux=%s", demux->psz_demux);
705 pt.init = demux_init;
706 pt.destroy = stream_destroy;
707 pt.decode = stream_decode;
708 pt.frequency = 90000;
711 else if (ptype >= 96)
713 char *dynamic = var_InheritString(demux, "rtp-dynamic-pt");
716 else if (!strcmp(dynamic, "theora"))
718 msg_Dbg (demux, "assuming Theora Encoded Video");
719 pt.init = theora_init;
720 pt.destroy = xiph_destroy;
721 pt.decode = xiph_decode;
722 pt.frequency = 90000;
726 msg_Err (demux, "invalid dynamic payload format `%s' "
727 "specified", dynamic);
738 rtp_add_type (demux, session, &pt);
743 * Dynamic payload type handlers
744 * Hmm, none implemented yet apart from Xiph ones.