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 static int Open (vlc_object_t *);
79 static void Close (vlc_object_t *);
85 set_shortname (N_("RTP"))
86 set_description (N_("Real-Time Protocol (RTP) input"))
87 set_category (CAT_INPUT)
88 set_subcategory (SUBCAT_INPUT_DEMUX)
89 set_capability ("access_demux", 0)
90 set_callbacks (Open, Close)
92 add_integer ("rtp-caching", 1000, NULL, RTP_CACHING_TEXT,
93 RTP_CACHING_LONGTEXT, true)
94 change_integer_range (0, 65535)
96 add_integer ("rtcp-port", 0, NULL, RTCP_PORT_TEXT,
97 RTCP_PORT_LONGTEXT, false)
98 change_integer_range (0, 65535)
101 add_string ("srtp-key", "", NULL,
102 SRTP_KEY_TEXT, SRTP_KEY_LONGTEXT, false)
103 add_string ("srtp-salt", "", NULL,
104 SRTP_SALT_TEXT, SRTP_SALT_LONGTEXT, false)
106 add_integer ("rtp-max-src", 1, NULL, RTP_MAX_SRC_TEXT,
107 RTP_MAX_SRC_LONGTEXT, true)
108 change_integer_range (1, 255)
109 add_integer ("rtp-timeout", 5, NULL, RTP_TIMEOUT_TEXT,
110 RTP_TIMEOUT_LONGTEXT, true)
111 add_integer ("rtp-max-dropout", 3000, NULL, RTP_MAX_DROPOUT_TEXT,
112 RTP_MAX_DROPOUT_LONGTEXT, true)
113 change_integer_range (0, 32767)
114 add_integer ("rtp-max-misorder", 100, NULL, RTP_MAX_MISORDER_TEXT,
115 RTP_MAX_MISORDER_LONGTEXT, true)
116 change_integer_range (0, 32767)
118 add_shortcut ("dccp")
119 /*add_shortcut ("sctp")*/
120 add_shortcut ("rtptcp") /* "tcp" is already taken :( */
122 add_shortcut ("udplite")
126 * TODO: so much stuff
127 * - send RTCP-RR and RTCP-BYE
128 * - dynamic payload types (need SDP parser)
129 * - multiple medias (need SDP parser, and RTCP-SR parser for lip-sync)
130 * - support for stream_filter in case of stream_Demux (MPEG-TS)
134 # define IPPROTO_DCCP 33 /* IANA */
137 #ifndef IPPROTO_UDPLITE
138 # define IPPROTO_UDPLITE 136 /* from IANA */
145 static int Control (demux_t *, int i_query, va_list args);
146 static int extract_port (char **phost);
149 * Probes and initializes.
151 static int Open (vlc_object_t *obj)
153 demux_t *demux = (demux_t *)obj;
154 int tp; /* transport protocol */
156 if (!strcmp (demux->psz_access, "dccp"))
159 if (!strcmp (demux->psz_access, "rtptcp"))
162 if (!strcmp (demux->psz_access, "rtp"))
165 if (!strcmp (demux->psz_access, "udplite"))
166 tp = IPPROTO_UDPLITE;
170 char *tmp = strdup (demux->psz_path);
175 char *dhost = strchr (tmp, '@');
187 /* Parses the port numbers */
188 int sport = 0, dport = 0;
190 sport = extract_port (&shost);
192 dport = extract_port (&dhost);
194 dport = 5004; /* avt-profile-1 port */
196 int rtcp_dport = var_CreateGetInteger (obj, "rtcp-port");
199 int fd = -1, rtcp_fd = -1;
204 case IPPROTO_UDPLITE:
205 fd = net_OpenDgram (obj, dhost, dport,
206 shost, sport, AF_UNSPEC, tp);
209 if (rtcp_dport > 0) /* XXX: source port is unknown */
210 rtcp_fd = net_OpenDgram (obj, dhost, rtcp_dport, shost, 0,
215 #ifndef SOCK_DCCP /* provisional API (FIXME) */
221 var_Create (obj, "dccp-service", VLC_VAR_STRING);
222 var_SetString (obj, "dccp-service", "RTPV"); /* FIXME: RTPA? */
223 fd = net_Connect (obj, dhost, dport, SOCK_DCCP, tp);
225 msg_Err (obj, "DCCP support not included");
230 fd = net_Connect (obj, dhost, dport, SOCK_STREAM, tp);
237 net_SetCSCov (fd, -1, 12);
239 /* Initializes demux */
240 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
249 vlc_mutex_init (&p_sys->lock);
254 p_sys->rtcp_fd = rtcp_fd;
255 p_sys->caching = var_CreateGetInteger (obj, "rtp-caching");
256 p_sys->max_src = var_CreateGetInteger (obj, "rtp-max-src");
257 p_sys->timeout = var_CreateGetInteger (obj, "rtp-timeout")
259 p_sys->max_dropout = var_CreateGetInteger (obj, "rtp-max-dropout");
260 p_sys->max_misorder = var_CreateGetInteger (obj, "rtp-max-misorder");
261 p_sys->framed_rtp = (tp == IPPROTO_TCP);
263 demux->pf_demux = NULL;
264 demux->pf_control = Control;
265 demux->p_sys = p_sys;
267 p_sys->session = rtp_session_create (demux);
268 if (p_sys->session == NULL)
272 char *key = var_CreateGetNonEmptyString (demux, "srtp-key");
275 p_sys->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
276 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
277 if (p_sys->srtp == NULL)
283 char *salt = var_CreateGetNonEmptyString (demux, "srtp-salt");
284 errno = srtp_setkeystring (p_sys->srtp, key, salt ? salt : "");
289 msg_Err (obj, "bad SRTP key/salt combination (%m)");
295 if (vlc_clone (&p_sys->thread, rtp_thread, demux,
296 VLC_THREAD_PRIORITY_INPUT))
298 p_sys->thread_ready = true;
310 static void Close (vlc_object_t *obj)
312 demux_t *demux = (demux_t *)obj;
313 demux_sys_t *p_sys = demux->p_sys;
315 if (p_sys->thread_ready)
317 vlc_cancel (p_sys->thread);
318 vlc_join (p_sys->thread, NULL);
320 vlc_mutex_destroy (&p_sys->lock);
324 srtp_destroy (p_sys->srtp);
327 rtp_session_destroy (demux, p_sys->session);
328 if (p_sys->rtcp_fd != -1)
329 net_Close (p_sys->rtcp_fd);
330 net_Close (p_sys->fd);
336 * Extracts port number from "[host]:port" or "host:port" strings,
337 * and remove brackets from the host name.
338 * @param phost pointer to the string upon entry,
339 * pointer to the hostname upon return.
340 * @return port number, 0 if missing.
342 static int extract_port (char **phost)
344 char *host = *phost, *port;
348 host = ++*phost; /* skip '[' */
349 port = strchr (host, ']');
351 *port++ = '\0'; /* skip ']' */
354 port = strchr (host, ':');
358 *port++ = '\0'; /* skip ':' */
366 static int Control (demux_t *demux, int i_query, va_list args)
368 demux_sys_t *p_sys = demux->p_sys;
372 case DEMUX_GET_POSITION:
374 float *v = va_arg (args, float *);
379 case DEMUX_GET_LENGTH:
382 int64_t *v = va_arg (args, int64_t *);
387 case DEMUX_GET_PTS_DELAY:
389 int64_t *v = va_arg (args, int64_t *);
390 *v = (int64_t)p_sys->caching * 1000;
394 case DEMUX_CAN_PAUSE:
396 case DEMUX_CAN_CONTROL_PACE:
398 bool *v = (bool*)va_arg( args, bool * );
409 * Generic packet handlers
412 static void *codec_init (demux_t *demux, es_format_t *fmt)
414 return es_out_Add (demux->out, fmt);
417 static void codec_destroy (demux_t *demux, void *data)
420 es_out_Del (demux->out, (es_out_id_t *)data);
423 /* Send a packet to decoder */
424 static void codec_decode (demux_t *demux, void *data, block_t *block)
428 block->i_dts = 0; /* RTP does not specify this */
429 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts );
430 es_out_Send (demux->out, (es_out_id_t *)data, block);
433 block_Release (block);
437 static void *stream_init (demux_t *demux, const char *name)
439 return stream_DemuxNew (demux, name, demux->out);
442 static void stream_destroy (demux_t *demux, void *data)
445 stream_Delete ((stream_t *)data);
449 /* Send a packet to a chained demuxer */
450 static void stream_decode (demux_t *demux, void *data, block_t *block)
453 stream_DemuxSend ((stream_t *)data, block);
455 block_Release (block);
460 * Static payload types handler
464 * PCMU: G.711 µ-law (RFC3551)
466 static void *pcmu_init (demux_t *demux)
470 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MULAW);
471 fmt.audio.i_rate = 8000;
472 fmt.audio.i_channels = 1;
473 return codec_init (demux, &fmt);
479 static void *gsm_init (demux_t *demux)
483 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_GSM);
484 fmt.audio.i_rate = 8000;
485 fmt.audio.i_channels = 1;
486 return codec_init (demux, &fmt);
490 * PCMA: G.711 A-law (RFC3551)
492 static void *pcma_init (demux_t *demux)
496 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_ALAW);
497 fmt.audio.i_rate = 8000;
498 fmt.audio.i_channels = 1;
499 return codec_init (demux, &fmt);
503 * L16: 16-bits (network byte order) PCM
505 static void *l16s_init (demux_t *demux)
509 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16B);
510 fmt.audio.i_rate = 44100;
511 fmt.audio.i_channels = 2;
512 return codec_init (demux, &fmt);
515 static void *l16m_init (demux_t *demux)
519 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16B);
520 fmt.audio.i_rate = 44100;
521 fmt.audio.i_channels = 1;
522 return codec_init (demux, &fmt);
528 static void *qcelp_init (demux_t *demux)
532 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_QCELP);
533 fmt.audio.i_rate = 8000;
534 fmt.audio.i_channels = 1;
535 return codec_init (demux, &fmt);
539 * MPA: MPEG Audio (RFC2250, §3.4)
541 static void *mpa_init (demux_t *demux)
545 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MPGA);
546 fmt.audio.i_channels = 2;
547 fmt.b_packetized = false;
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_CODEC_MPGV);
574 fmt.b_packetized = false;
575 return codec_init (demux, &fmt);
578 static void mpv_decode (demux_t *demux, void *data, block_t *block)
580 if (block->i_buffer < 4)
582 block_Release (block);
586 block->i_buffer -= 4; /* 32-bits RTP/MPV header */
587 block->p_buffer += 4;
589 if (block->p_buffer[-3] & 0x4)
591 /* MPEG2 Video extension header */
592 /* TODO: shouldn't we skip this too ? */
595 codec_decode (demux, data, block);
600 * MP2: MPEG TS (RFC2250, §2)
602 static void *ts_init (demux_t *demux)
604 return stream_init (demux, *demux->psz_demux ? demux->psz_demux : "ts");
608 /* Not using SDP, we need to guess the payload format used */
609 /* see http://www.iana.org/assignments/rtp-parameters */
610 int rtp_autodetect (demux_t *demux, rtp_session_t *session,
611 const block_t *block)
613 uint8_t ptype = rtp_ptype (block);
616 .destroy = codec_destroy,
617 .decode = codec_decode,
622 /* Remember to keep this in sync with modules/services_discovery/sap.c */
626 msg_Dbg (demux, "detected G.711 mu-law");
632 msg_Dbg (demux, "detected GSM");
638 msg_Dbg (demux, "detected G.711 A-law");
644 msg_Dbg (demux, "detected stereo PCM");
646 pt.frequency = 44100;
650 msg_Dbg (demux, "detected mono PCM");
652 pt.frequency = 44100;
656 msg_Dbg (demux, "detected QCELP");
657 pt.init = qcelp_init;
662 msg_Dbg (demux, "detected MPEG Audio");
664 pt.decode = mpa_decode;
665 pt.frequency = 90000;
669 msg_Dbg (demux, "detected MPEG Video");
671 pt.decode = mpv_decode;
672 pt.frequency = 90000;
676 msg_Dbg (demux, "detected MPEG2 TS");
678 pt.destroy = stream_destroy;
679 pt.decode = stream_decode;
680 pt.frequency = 90000;
686 rtp_add_type (demux, session, &pt);
691 * Dynamic payload type handlers
692 * Hmm, none implemented yet.