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.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 ****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_demux.h>
34 #include <vlc_network.h>
35 #include <vlc_plugin.h>
37 #include <vlc_codecs.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)
100 add_string ("srtp-key", "", NULL,
101 SRTP_KEY_TEXT, SRTP_KEY_LONGTEXT, false)
102 add_string ("srtp-salt", "", NULL,
103 SRTP_SALT_TEXT, SRTP_SALT_LONGTEXT, false)
104 add_integer ("rtp-max-src", 1, NULL, RTP_MAX_SRC_TEXT,
105 RTP_MAX_SRC_LONGTEXT, true)
106 change_integer_range (1, 255)
107 add_integer ("rtp-timeout", 5, NULL, RTP_TIMEOUT_TEXT,
108 RTP_TIMEOUT_LONGTEXT, true)
109 add_integer ("rtp-max-dropout", 3000, NULL, RTP_MAX_DROPOUT_TEXT,
110 RTP_MAX_DROPOUT_LONGTEXT, true)
111 change_integer_range (0, 32767)
112 add_integer ("rtp-max-misorder", 100, NULL, RTP_MAX_MISORDER_TEXT,
113 RTP_MAX_MISORDER_LONGTEXT, true)
114 change_integer_range (0, 32767)
116 add_shortcut ("dccp")
117 /*add_shortcut ("sctp")*/
118 add_shortcut ("rtptcp") /* "tcp" is already taken :( */
120 add_shortcut ("udplite")
124 * TODO: so much stuff
125 * - send RTCP-RR and RTCP-BYE
126 * - dynamic payload types (need SDP parser)
127 * - multiple medias (need SDP parser, and RTCP-SR parser for lip-sync)
128 * - support for stream_filter in case of stream_Demux (MPEG-TS)
132 # define IPPROTO_DCCP 33 /* IANA */
135 #ifndef IPPROTO_UDPLITE
136 # define IPPROTO_UDPLITE 136 /* from IANA */
143 static int Control (demux_t *, int i_query, va_list args);
144 static int extract_port (char **phost);
147 * Probes and initializes.
149 static int Open (vlc_object_t *obj)
151 demux_t *demux = (demux_t *)obj;
152 int tp; /* transport protocol */
154 if (!strcmp (demux->psz_access, "dccp"))
157 if (!strcmp (demux->psz_access, "rtptcp"))
160 if (!strcmp (demux->psz_access, "rtp"))
163 if (!strcmp (demux->psz_access, "udplite"))
164 tp = IPPROTO_UDPLITE;
168 char *tmp = strdup (demux->psz_path);
173 char *dhost = strchr (shost, '@');
177 /* Parses the port numbers */
178 int sport = 0, dport = 0;
179 sport = extract_port (&shost);
181 dport = extract_port (&dhost);
183 dport = 5004; /* avt-profile-1 port */
185 int rtcp_dport = var_CreateGetInteger (obj, "rtcp-port");
188 int fd = -1, rtcp_fd = -1;
193 case IPPROTO_UDPLITE:
194 fd = net_OpenDgram (obj, dhost, dport,
195 shost, sport, AF_UNSPEC, tp);
198 if (rtcp_dport > 0) /* XXX: source port is unknown */
199 rtcp_fd = net_OpenDgram (obj, dhost, rtcp_dport, shost, 0,
204 #ifndef SOCK_DCCP /* provisional API (FIXME) */
210 var_Create (obj, "dccp-service", VLC_VAR_STRING);
211 var_SetString (obj, "dccp-service", "RTPV"); /* FIXME: RTPA? */
212 fd = net_Connect (obj, shost, sport, SOCK_DCCP, tp);
214 msg_Err (obj, "DCCP support not included");
219 fd = net_Connect (obj, shost, sport, SOCK_STREAM, tp);
226 net_SetCSCov (fd, -1, 12);
228 /* Initializes demux */
229 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
238 vlc_mutex_init (&p_sys->lock);
241 p_sys->rtcp_fd = rtcp_fd;
242 p_sys->caching = var_CreateGetInteger (obj, "rtp-caching");
243 p_sys->max_src = var_CreateGetInteger (obj, "rtp-max-src");
244 p_sys->timeout = var_CreateGetInteger (obj, "rtp-timeout");
245 p_sys->max_dropout = var_CreateGetInteger (obj, "rtp-max-dropout");
246 p_sys->max_misorder = var_CreateGetInteger (obj, "rtp-max-misorder");
247 p_sys->framed_rtp = (tp == IPPROTO_TCP);
249 demux->pf_demux = NULL;
250 demux->pf_control = Control;
251 demux->p_sys = p_sys;
253 p_sys->session = rtp_session_create (demux);
254 if (p_sys->session == NULL)
257 char *key = var_CreateGetNonEmptyString (demux, "srtp-key");
260 p_sys->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
261 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
262 if (p_sys->srtp == NULL)
268 char *salt = var_CreateGetNonEmptyString (demux, "srtp-salt");
269 errno = srtp_setkeystring (p_sys->srtp, key, salt ? salt : "");
274 msg_Err (obj, "bad SRTP key/salt combination (%m)");
279 if (vlc_clone (&p_sys->thread, rtp_thread, demux,
280 VLC_THREAD_PRIORITY_INPUT))
282 p_sys->thread_ready = true;
294 static void Close (vlc_object_t *obj)
296 demux_t *demux = (demux_t *)obj;
297 demux_sys_t *p_sys = demux->p_sys;
299 if (p_sys->thread_ready)
301 vlc_cancel (p_sys->thread);
302 vlc_join (p_sys->thread, NULL);
304 vlc_mutex_destroy (&p_sys->lock);
307 srtp_destroy (p_sys->srtp);
309 rtp_session_destroy (demux, p_sys->session);
310 if (p_sys->rtcp_fd != -1)
311 net_Close (p_sys->rtcp_fd);
312 net_Close (p_sys->fd);
318 * Extracts port number from "[host]:port" or "host:port" strings,
319 * and remove brackets from the host name.
320 * @param phost pointer to the string upon entry,
321 * pointer to the hostname upon return.
322 * @return port number, 0 if missing.
324 static int extract_port (char **phost)
326 char *host = *phost, *port;
330 host = ++*phost; /* skip '[' */
331 port = strchr (host, ']');
333 *port++ = '\0'; /* skip ']' */
336 port = strchr (host, ':');
340 *port++ = '\0'; /* skip ':' */
348 static int Control (demux_t *demux, int i_query, va_list args)
350 demux_sys_t *p_sys = demux->p_sys;
354 case DEMUX_GET_POSITION:
356 float *v = va_arg (args, float *);
361 case DEMUX_GET_LENGTH:
364 int64_t *v = va_arg (args, int64_t *);
369 case DEMUX_GET_PTS_DELAY:
371 int64_t *v = va_arg (args, int64_t *);
372 *v = (int64_t)p_sys->caching * 1000;
376 case DEMUX_CAN_PAUSE:
378 case DEMUX_CAN_CONTROL_PACE:
380 bool *v = (bool*)va_arg( args, bool * );
391 * Generic packet handlers
394 static void *codec_init (demux_t *demux, es_format_t *fmt)
396 return es_out_Add (demux->out, fmt);
399 static void codec_destroy (demux_t *demux, void *data)
402 es_out_Del (demux->out, (es_out_id_t *)data);
405 /* Send a packet to decoder */
406 static void codec_decode (demux_t *demux, void *data, block_t *block)
410 block->i_dts = 0; /* RTP does not specify this */
411 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts );
412 es_out_Send (demux->out, (es_out_id_t *)data, block);
415 block_Release (block);
419 static void *stream_init (demux_t *demux, const char *name)
421 return stream_DemuxNew (demux, name, demux->out);
424 static void stream_destroy (demux_t *demux, void *data)
427 stream_Delete ((stream_t *)data);
431 /* Send a packet to a chained demuxer */
432 static void stream_decode (demux_t *demux, void *data, block_t *block)
435 stream_DemuxSend ((stream_t *)data, block);
437 block_Release (block);
442 * Static payload types handler
446 * PCMU: G.711 µ-law (RFC3551)
448 static void *pcmu_init (demux_t *demux)
452 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MULAW);
453 fmt.audio.i_rate = 8000;
454 fmt.audio.i_channels = 1;
455 return codec_init (demux, &fmt);
461 static void *gsm_init (demux_t *demux)
465 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_GSM);
466 fmt.audio.i_rate = 8000;
467 fmt.audio.i_channels = 1;
468 return codec_init (demux, &fmt);
472 * PCMA: G.711 A-law (RFC3551)
474 static void *pcma_init (demux_t *demux)
478 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_ALAW);
479 fmt.audio.i_rate = 8000;
480 fmt.audio.i_channels = 1;
481 return codec_init (demux, &fmt);
485 * L16: 16-bits (network byte order) PCM
487 static void *l16s_init (demux_t *demux)
491 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16B);
492 fmt.audio.i_rate = 44100;
493 fmt.audio.i_channels = 2;
494 return codec_init (demux, &fmt);
497 static void *l16m_init (demux_t *demux)
501 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16B);
502 fmt.audio.i_rate = 44100;
503 fmt.audio.i_channels = 1;
504 return codec_init (demux, &fmt);
510 static void *qcelp_init (demux_t *demux)
514 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_QCELP);
515 fmt.audio.i_rate = 8000;
516 fmt.audio.i_channels = 1;
517 return codec_init (demux, &fmt);
521 * MPA: MPEG Audio (RFC2250, §3.4)
523 static void *mpa_init (demux_t *demux)
527 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MPGA);
528 fmt.audio.i_channels = 2;
529 fmt.b_packetized = false;
530 return codec_init (demux, &fmt);
533 static void mpa_decode (demux_t *demux, void *data, block_t *block)
535 if (block->i_buffer < 4)
537 block_Release (block);
541 block->i_buffer -= 4; /* 32-bits RTP/MPA header */
542 block->p_buffer += 4;
544 codec_decode (demux, data, block);
549 * MPV: MPEG Video (RFC2250, §3.5)
551 static void *mpv_init (demux_t *demux)
555 es_format_Init (&fmt, VIDEO_ES, VLC_CODEC_MPGV);
556 fmt.b_packetized = false;
557 return codec_init (demux, &fmt);
560 static void mpv_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/MPV header */
569 block->p_buffer += 4;
571 if (block->p_buffer[-3] & 0x4)
573 /* MPEG2 Video extension header */
574 /* TODO: shouldn't we skip this too ? */
577 codec_decode (demux, data, block);
582 * MP2: MPEG TS (RFC2250, §2)
584 static void *ts_init (demux_t *demux)
586 return stream_init (demux, *demux->psz_demux ? demux->psz_demux : "ts");
590 /* Not using SDP, we need to guess the payload format used */
591 /* see http://www.iana.org/assignments/rtp-parameters */
592 int rtp_autodetect (demux_t *demux, rtp_session_t *session,
593 const block_t *block)
595 uint8_t ptype = rtp_ptype (block);
598 .destroy = codec_destroy,
599 .decode = codec_decode,
604 /* Remember to keep this in sync with modules/services_discovery/sap.c */
608 msg_Dbg (demux, "detected G.711 mu-law");
614 msg_Dbg (demux, "detected GSM");
620 msg_Dbg (demux, "detected G.711 A-law");
626 msg_Dbg (demux, "detected stereo PCM");
628 pt.frequency = 44100;
632 msg_Dbg (demux, "detected mono PCM");
634 pt.frequency = 44100;
638 msg_Dbg (demux, "detected QCELP");
639 pt.init = qcelp_init;
644 msg_Dbg (demux, "detected MPEG Audio");
646 pt.decode = mpa_decode;
647 pt.frequency = 90000;
651 msg_Dbg (demux, "detected MPEG Video");
653 pt.decode = mpv_decode;
654 pt.frequency = 90000;
658 msg_Dbg (demux, "detected MPEG2 TS");
660 pt.destroy = stream_destroy;
661 pt.decode = stream_decode;
662 pt.frequency = 90000;
668 rtp_add_type (demux, session, &pt);
673 * Dynamic payload type handlers
674 * Hmm, none implemented yet.