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)
114 add_string ("srtp-salt", "",
115 SRTP_SALT_TEXT, SRTP_SALT_LONGTEXT, false)
118 add_integer ("rtp-max-src", 1, RTP_MAX_SRC_TEXT,
119 RTP_MAX_SRC_LONGTEXT, true)
120 change_integer_range (1, 255)
121 add_integer ("rtp-timeout", 5, RTP_TIMEOUT_TEXT,
122 RTP_TIMEOUT_LONGTEXT, true)
123 add_integer ("rtp-max-dropout", 3000, RTP_MAX_DROPOUT_TEXT,
124 RTP_MAX_DROPOUT_LONGTEXT, true)
125 change_integer_range (0, 32767)
126 add_integer ("rtp-max-misorder", 100, RTP_MAX_MISORDER_TEXT,
127 RTP_MAX_MISORDER_LONGTEXT, true)
128 change_integer_range (0, 32767)
129 add_string ("rtp-dynamic-pt", NULL, RTP_DYNAMIC_PT_TEXT,
130 RTP_DYNAMIC_PT_LONGTEXT, true)
131 change_string_list (dynamic_pt_list, dynamic_pt_list_text, NULL)
133 /*add_shortcut ("sctp")*/
134 add_shortcut ("dccp", "rtptcp", /* "tcp" is already taken :( */
139 * TODO: so much stuff
140 * - send RTCP-RR and RTCP-BYE
141 * - dynamic payload types (need SDP parser)
142 * - multiple medias (need SDP parser, and RTCP-SR parser for lip-sync)
143 * - support for stream_filter in case of stream_Demux (MPEG-TS)
147 # define IPPROTO_DCCP 33 /* IANA */
150 #ifndef IPPROTO_UDPLITE
151 # define IPPROTO_UDPLITE 136 /* from IANA */
158 static int Control (demux_t *, int i_query, va_list args);
159 static int extract_port (char **phost);
162 * Probes and initializes.
164 static int Open (vlc_object_t *obj)
166 demux_t *demux = (demux_t *)obj;
167 int tp; /* transport protocol */
169 if (!strcmp (demux->psz_access, "dccp"))
172 if (!strcmp (demux->psz_access, "rtptcp"))
175 if (!strcmp (demux->psz_access, "rtp"))
178 if (!strcmp (demux->psz_access, "udplite"))
179 tp = IPPROTO_UDPLITE;
183 char *tmp = strdup (demux->psz_location);
188 char *dhost = strchr (tmp, '@');
200 /* Parses the port numbers */
201 int sport = 0, dport = 0;
203 sport = extract_port (&shost);
205 dport = extract_port (&dhost);
207 dport = 5004; /* avt-profile-1 port */
209 int rtcp_dport = var_CreateGetInteger (obj, "rtcp-port");
212 int fd = -1, rtcp_fd = -1;
217 case IPPROTO_UDPLITE:
218 fd = net_OpenDgram (obj, dhost, dport,
219 shost, sport, AF_UNSPEC, tp);
222 if (rtcp_dport > 0) /* XXX: source port is unknown */
223 rtcp_fd = net_OpenDgram (obj, dhost, rtcp_dport, shost, 0,
228 #ifndef SOCK_DCCP /* provisional API (FIXME) */
234 var_Create (obj, "dccp-service", VLC_VAR_STRING);
235 var_SetString (obj, "dccp-service", "RTPV"); /* FIXME: RTPA? */
236 fd = net_Connect (obj, dhost, dport, SOCK_DCCP, tp);
238 msg_Err (obj, "DCCP support not included");
243 fd = net_Connect (obj, dhost, dport, SOCK_STREAM, tp);
250 net_SetCSCov (fd, -1, 12);
252 /* Initializes demux */
253 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
266 p_sys->rtcp_fd = rtcp_fd;
267 p_sys->caching = var_CreateGetInteger (obj, "rtp-caching");
268 p_sys->max_src = var_CreateGetInteger (obj, "rtp-max-src");
269 p_sys->timeout = var_CreateGetInteger (obj, "rtp-timeout")
271 p_sys->max_dropout = var_CreateGetInteger (obj, "rtp-max-dropout");
272 p_sys->max_misorder = var_CreateGetInteger (obj, "rtp-max-misorder");
273 p_sys->thread_ready = false;
274 p_sys->autodetect = true;
276 demux->pf_demux = NULL;
277 demux->pf_control = Control;
278 demux->p_sys = p_sys;
280 p_sys->session = rtp_session_create (demux);
281 if (p_sys->session == NULL)
285 char *key = var_CreateGetNonEmptyString (demux, "srtp-key");
288 p_sys->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
289 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
290 if (p_sys->srtp == NULL)
296 char *salt = var_CreateGetNonEmptyString (demux, "srtp-salt");
297 errno = srtp_setkeystring (p_sys->srtp, key, salt ? salt : "");
302 msg_Err (obj, "bad SRTP key/salt combination (%m)");
308 if (vlc_clone (&p_sys->thread,
309 (tp != IPPROTO_TCP) ? rtp_dgram_thread : rtp_stream_thread,
310 demux, VLC_THREAD_PRIORITY_INPUT))
312 p_sys->thread_ready = true;
324 static void Close (vlc_object_t *obj)
326 demux_t *demux = (demux_t *)obj;
327 demux_sys_t *p_sys = demux->p_sys;
329 if (p_sys->thread_ready)
331 vlc_cancel (p_sys->thread);
332 vlc_join (p_sys->thread, NULL);
337 srtp_destroy (p_sys->srtp);
340 rtp_session_destroy (demux, p_sys->session);
341 if (p_sys->rtcp_fd != -1)
342 net_Close (p_sys->rtcp_fd);
343 net_Close (p_sys->fd);
349 * Extracts port number from "[host]:port" or "host:port" strings,
350 * and remove brackets from the host name.
351 * @param phost pointer to the string upon entry,
352 * pointer to the hostname upon return.
353 * @return port number, 0 if missing.
355 static int extract_port (char **phost)
357 char *host = *phost, *port;
361 host = ++*phost; /* skip '[' */
362 port = strchr (host, ']');
364 *port++ = '\0'; /* skip ']' */
367 port = strchr (host, ':');
371 *port++ = '\0'; /* skip ':' */
379 static int Control (demux_t *demux, int i_query, va_list args)
381 demux_sys_t *p_sys = demux->p_sys;
385 case DEMUX_GET_POSITION:
387 float *v = va_arg (args, float *);
392 case DEMUX_GET_LENGTH:
395 int64_t *v = va_arg (args, int64_t *);
400 case DEMUX_GET_PTS_DELAY:
402 int64_t *v = va_arg (args, int64_t *);
403 *v = (int64_t)p_sys->caching * 1000;
407 case DEMUX_CAN_PAUSE:
409 case DEMUX_CAN_CONTROL_PACE:
411 bool *v = (bool*)va_arg( args, bool * );
422 * Generic packet handlers
425 void *codec_init (demux_t *demux, es_format_t *fmt)
427 return es_out_Add (demux->out, fmt);
430 void codec_destroy (demux_t *demux, void *data)
433 es_out_Del (demux->out, (es_out_id_t *)data);
436 /* Send a packet to decoder */
437 void codec_decode (demux_t *demux, void *data, block_t *block)
441 block->i_dts = VLC_TS_INVALID; /* RTP does not specify this */
442 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts );
443 es_out_Send (demux->out, (es_out_id_t *)data, block);
446 block_Release (block);
449 static void *stream_init (demux_t *demux, const char *name)
451 return stream_DemuxNew (demux, name, demux->out);
454 static void stream_destroy (demux_t *demux, void *data)
457 stream_Delete ((stream_t *)data);
461 /* Send a packet to a chained demuxer */
462 static void stream_decode (demux_t *demux, void *data, block_t *block)
465 stream_DemuxSend ((stream_t *)data, block);
467 block_Release (block);
471 static void *demux_init (demux_t *demux)
473 return stream_init (demux, demux->psz_demux);
477 * Static payload types handler
481 * PCMU: G.711 µ-law (RFC3551)
483 static void *pcmu_init (demux_t *demux)
487 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MULAW);
488 fmt.audio.i_rate = 8000;
489 fmt.audio.i_channels = 1;
490 return codec_init (demux, &fmt);
496 static void *gsm_init (demux_t *demux)
500 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_GSM);
501 fmt.audio.i_rate = 8000;
502 fmt.audio.i_channels = 1;
503 return codec_init (demux, &fmt);
507 * PCMA: G.711 A-law (RFC3551)
509 static void *pcma_init (demux_t *demux)
513 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_ALAW);
514 fmt.audio.i_rate = 8000;
515 fmt.audio.i_channels = 1;
516 return codec_init (demux, &fmt);
520 * L16: 16-bits (network byte order) PCM
522 static void *l16s_init (demux_t *demux)
526 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16B);
527 fmt.audio.i_rate = 44100;
528 fmt.audio.i_channels = 2;
529 return codec_init (demux, &fmt);
532 static void *l16m_init (demux_t *demux)
536 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16B);
537 fmt.audio.i_rate = 44100;
538 fmt.audio.i_channels = 1;
539 return codec_init (demux, &fmt);
545 static void *qcelp_init (demux_t *demux)
549 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_QCELP);
550 fmt.audio.i_rate = 8000;
551 fmt.audio.i_channels = 1;
552 return codec_init (demux, &fmt);
556 * MPA: MPEG Audio (RFC2250, §3.4)
558 static void *mpa_init (demux_t *demux)
562 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MPGA);
563 fmt.audio.i_channels = 2;
564 fmt.b_packetized = false;
565 return codec_init (demux, &fmt);
568 static void mpa_decode (demux_t *demux, void *data, block_t *block)
570 if (block->i_buffer < 4)
572 block_Release (block);
576 block->i_buffer -= 4; /* 32-bits RTP/MPA header */
577 block->p_buffer += 4;
579 codec_decode (demux, data, block);
584 * MPV: MPEG Video (RFC2250, §3.5)
586 static void *mpv_init (demux_t *demux)
590 es_format_Init (&fmt, VIDEO_ES, VLC_CODEC_MPGV);
591 fmt.b_packetized = false;
592 return codec_init (demux, &fmt);
595 static void mpv_decode (demux_t *demux, void *data, block_t *block)
597 if (block->i_buffer < 4)
599 block_Release (block);
603 block->i_buffer -= 4; /* 32-bits RTP/MPV header */
604 block->p_buffer += 4;
606 if (block->p_buffer[-3] & 0x4)
608 /* MPEG2 Video extension header */
609 /* TODO: shouldn't we skip this too ? */
612 codec_decode (demux, data, block);
617 * MP2: MPEG TS (RFC2250, §2)
619 static void *ts_init (demux_t *demux)
621 return stream_init (demux, *demux->psz_demux ? demux->psz_demux : "ts");
625 /* Not using SDP, we need to guess the payload format used */
626 /* see http://www.iana.org/assignments/rtp-parameters */
627 int rtp_autodetect (demux_t *demux, rtp_session_t *session,
628 const block_t *block)
630 uint8_t ptype = rtp_ptype (block);
633 .destroy = codec_destroy,
634 .decode = codec_decode,
639 /* Remember to keep this in sync with modules/services_discovery/sap.c */
643 msg_Dbg (demux, "detected G.711 mu-law");
649 msg_Dbg (demux, "detected GSM");
655 msg_Dbg (demux, "detected G.711 A-law");
661 msg_Dbg (demux, "detected stereo PCM");
663 pt.frequency = 44100;
667 msg_Dbg (demux, "detected mono PCM");
669 pt.frequency = 44100;
673 msg_Dbg (demux, "detected QCELP");
674 pt.init = qcelp_init;
679 msg_Dbg (demux, "detected MPEG Audio");
681 pt.decode = mpa_decode;
682 pt.frequency = 90000;
686 msg_Dbg (demux, "detected MPEG Video");
688 pt.decode = mpv_decode;
689 pt.frequency = 90000;
693 msg_Dbg (demux, "detected MPEG2 TS");
695 pt.destroy = stream_destroy;
696 pt.decode = stream_decode;
697 pt.frequency = 90000;
702 * If the rtp payload type is unknown then check demux if it is specified
704 if ((strcmp(demux->psz_demux, "h264") == 0) || (strcmp(demux->psz_demux, "ts") == 0))
706 msg_Dbg (demux, "rtp autodetect specified demux=%s", demux->psz_demux);
707 pt.init = demux_init;
708 pt.destroy = stream_destroy;
709 pt.decode = stream_decode;
710 pt.frequency = 90000;
713 else if (ptype >= 96)
715 char *dynamic = var_InheritString(demux, "rtp-dynamic-pt");
718 else if (!strcmp(dynamic, "theora"))
720 msg_Dbg (demux, "assuming Theora Encoded Video");
721 pt.init = theora_init;
722 pt.destroy = xiph_destroy;
723 pt.decode = xiph_decode;
724 pt.frequency = 90000;
728 msg_Err (demux, "invalid dynamic payload format `%s' "
729 "specified", dynamic);
740 rtp_add_type (demux, session, &pt);
745 * Dynamic payload type handlers
746 * Hmm, none implemented yet apart from Xiph ones.