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 ****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_demux.h>
33 #include <vlc_network.h>
34 #include <vlc_plugin.h>
36 #include <vlc_codecs.h>
41 #define RTP_CACHING_TEXT N_("RTP de-jitter buffer length (msec)")
42 #define RTP_CACHING_LONGTEXT N_( \
43 "How long to wait for late RTP packets (and delay the performance)." )
45 #define RTCP_PORT_TEXT N_("RTCP (local) port")
46 #define RTCP_PORT_LONGTEXT N_( \
47 "RTCP packets will be received on this transport protocol port. " \
48 "If zero, multiplexed RTP/RTCP is used.")
50 #define SRTP_KEY_TEXT N_("SRTP key (hexadecimal)")
51 #define SRTP_KEY_LONGTEXT N_( \
52 "RTP packets will be authenticated and deciphered "\
53 "with this Secure RTP master shared secret key.")
55 #define SRTP_SALT_TEXT N_("SRTP salt (hexadecimal)")
56 #define SRTP_SALT_LONGTEXT N_( \
57 "Secure RTP requires a (non-secret) master salt value.")
59 #define RTP_MAX_SRC_TEXT N_("Maximum RTP sources")
60 #define RTP_MAX_SRC_LONGTEXT N_( \
61 "How many distinct active RTP sources are allowed at a time." )
63 #define RTP_TIMEOUT_TEXT N_("RTP source timeout (sec)")
64 #define RTP_TIMEOUT_LONGTEXT N_( \
65 "How long to wait for any packet before a source is expired.")
67 #define RTP_MAX_DROPOUT_TEXT N_("Maximum RTP sequence number dropout")
68 #define RTP_MAX_DROPOUT_LONGTEXT N_( \
69 "RTP packets will be discarded if they are too much ahead (i.e. in the " \
70 "future) by this many packets from the last received packet." )
72 #define RTP_MAX_MISORDER_TEXT N_("Maximum RTP sequence number misordering")
73 #define RTP_MAX_MISORDER_LONGTEXT N_( \
74 "RTP packets will be discarded if they are too far behind (i.e. in the " \
75 "past) by this many packets from the last received packet." )
77 static int Open (vlc_object_t *);
78 static void Close (vlc_object_t *);
84 set_shortname (N_("RTP"))
85 set_description (N_("Real-Time Protocol (RTP) input"))
86 set_category (CAT_INPUT)
87 set_subcategory (SUBCAT_INPUT_DEMUX)
88 set_capability ("access_demux", 0)
89 set_callbacks (Open, Close)
91 add_integer ("rtp-caching", 1000, NULL, RTP_CACHING_TEXT,
92 RTP_CACHING_LONGTEXT, true)
93 change_integer_range (0, 65535)
94 add_integer ("rtcp-port", 0, NULL, RTCP_PORT_TEXT,
95 RTCP_PORT_LONGTEXT, false)
96 change_integer_range (0, 65535)
98 add_string ("srtp-key", "", NULL,
99 SRTP_KEY_TEXT, SRTP_KEY_LONGTEXT, false)
100 add_string ("srtp-salt", "", NULL,
101 SRTP_SALT_TEXT, SRTP_SALT_LONGTEXT, false)
102 add_integer ("rtp-max-src", 1, NULL, RTP_MAX_SRC_TEXT,
103 RTP_MAX_SRC_LONGTEXT, true)
104 change_integer_range (1, 255)
105 add_integer ("rtp-timeout", 5, NULL, RTP_TIMEOUT_TEXT,
106 RTP_TIMEOUT_LONGTEXT, true)
107 add_integer ("rtp-max-dropout", 3000, NULL, RTP_MAX_DROPOUT_TEXT,
108 RTP_MAX_DROPOUT_LONGTEXT, true)
109 change_integer_range (0, 32767)
110 add_integer ("rtp-max-misorder", 100, NULL, RTP_MAX_MISORDER_TEXT,
111 RTP_MAX_MISORDER_LONGTEXT, true)
112 change_integer_range (0, 32767)
114 add_shortcut ("dccp")
115 /*add_shortcut ("sctp")*/
116 add_shortcut ("rtptcp") /* "tcp" is already taken :( */
118 add_shortcut ("udplite")
122 * TODO: so much stuff
123 * - send RTCP-RR and RTCP-BYE
124 * - dynamic payload types (need SDP parser)
125 * - multiple medias (need SDP parser, and RTCP-SR parser for lip-sync)
126 * - support for stream_filter in case of stream_Demux (MPEG-TS)
130 # define IPPROTO_DCCP 33 /* IANA */
133 #ifndef IPPROTO_UDPLITE
134 # define IPPROTO_UDPLITE 136 /* from IANA */
141 static int Demux (demux_t *);
142 static int Control (demux_t *, int i_query, va_list args);
143 static int extract_port (char **phost);
146 * Probes and initializes.
148 static int Open (vlc_object_t *obj)
150 demux_t *demux = (demux_t *)obj;
151 int tp; /* transport protocol */
153 if (!strcmp (demux->psz_access, "dccp"))
156 if (!strcmp (demux->psz_access, "rtptcp"))
159 if (!strcmp (demux->psz_access, "rtp"))
162 if (!strcmp (demux->psz_access, "udplite"))
163 tp = IPPROTO_UDPLITE;
167 char *tmp = strdup (demux->psz_path);
172 char *dhost = strchr (shost, '@');
176 /* Parses the port numbers */
177 int sport = 0, dport = 0;
178 sport = extract_port (&shost);
180 dport = extract_port (&dhost);
182 dport = 5004; /* avt-profile-1 port */
184 int rtcp_dport = var_CreateGetInteger (obj, "rtcp-port");
187 int fd = -1, rtcp_fd = -1;
192 case IPPROTO_UDPLITE:
193 fd = net_OpenDgram (obj, dhost, dport,
194 shost, sport, AF_UNSPEC, tp);
197 if (rtcp_dport > 0) /* XXX: source port is unknown */
198 rtcp_fd = net_OpenDgram (obj, dhost, rtcp_dport, shost, 0,
203 #ifndef SOCK_DCCP /* provisional API (FIXME) */
209 var_Create (obj, "dccp-service", VLC_VAR_STRING);
210 var_SetString (obj, "dccp-service", "RTPV"); /* FIXME: RTPA? */
211 fd = net_Connect (obj, shost, sport, SOCK_DCCP, tp);
213 msg_Err (obj, "DCCP support not included");
218 fd = net_Connect (obj, shost, sport, SOCK_STREAM, tp);
225 net_SetCSCov (fd, -1, 12);
227 /* Initializes demux */
228 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
237 vlc_mutex_init (&p_sys->lock);
238 vlc_cond_init (&p_sys->wait);
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);
250 demux->pf_demux = Demux;
251 demux->pf_control = Control;
252 demux->p_sys = p_sys;
254 p_sys->session = rtp_session_create (demux);
255 if (p_sys->session == NULL)
258 char *key = var_CreateGetNonEmptyString (demux, "srtp-key");
261 p_sys->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
262 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
263 if (p_sys->srtp == NULL)
269 char *salt = var_CreateGetNonEmptyString (demux, "srtp-salt");
270 errno = srtp_setkeystring (p_sys->srtp, key, salt ? salt : "");
275 msg_Err (obj, "bad SRTP key/salt combination (%m)");
280 if (vlc_clone (&p_sys->thread, rtp_thread, demux,
281 VLC_THREAD_PRIORITY_INPUT))
283 p_sys->thread_ready = true;
295 static void Close (vlc_object_t *obj)
297 demux_t *demux = (demux_t *)obj;
298 demux_sys_t *p_sys = demux->p_sys;
300 if (p_sys->thread_ready)
302 vlc_cancel (p_sys->thread);
303 vlc_join (p_sys->thread, NULL);
305 vlc_cond_destroy (&p_sys->wait);
306 vlc_mutex_destroy (&p_sys->lock);
309 srtp_destroy (p_sys->srtp);
311 rtp_session_destroy (demux, p_sys->session);
312 if (p_sys->rtcp_fd != -1)
313 net_Close (p_sys->rtcp_fd);
314 net_Close (p_sys->fd);
320 * Extracts port number from "[host]:port" or "host:port" strings,
321 * and remove brackets from the host name.
322 * @param phost pointer to the string upon entry,
323 * pointer to the hostname upon return.
324 * @return port number, 0 if missing.
326 static int extract_port (char **phost)
328 char *host = *phost, *port;
332 host = ++*phost; /* skip '[' */
333 port = strchr (host, ']');
335 *port++ = '\0'; /* skip ']' */
338 port = strchr (host, ':');
342 *port++ = '\0'; /* skip ':' */
350 static int Control (demux_t *demux, int i_query, va_list args)
352 demux_sys_t *p_sys = demux->p_sys;
356 case DEMUX_GET_POSITION:
358 float *v = va_arg (args, float *);
363 case DEMUX_GET_LENGTH:
366 int64_t *v = va_arg (args, int64_t *);
371 case DEMUX_GET_PTS_DELAY:
373 int64_t *v = va_arg (args, int64_t *);
374 *v = p_sys->caching * 1000;
378 case DEMUX_CAN_PAUSE:
380 case DEMUX_CAN_CONTROL_PACE:
382 bool *v = (bool*)va_arg( args, bool * );
393 * Generic packet handlers
396 static void *codec_init (demux_t *demux, es_format_t *fmt)
398 return es_out_Add (demux->out, fmt);
401 static void codec_destroy (demux_t *demux, void *data)
404 es_out_Del (demux->out, (es_out_id_t *)data);
407 /* Send a packet to decoder */
408 static void codec_decode (demux_t *demux, void *data, block_t *block)
412 block->i_dts = 0; /* RTP does not specify this */
413 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts );
414 es_out_Send (demux->out, (es_out_id_t *)data, block);
417 block_Release (block);
421 static void *stream_init (demux_t *demux, const char *name)
423 return stream_DemuxNew (demux, name, demux->out);
426 static void stream_destroy (demux_t *demux, void *data)
429 stream_Delete ((stream_t *)data);
433 /* Send a packet to a chained demuxer */
434 static void stream_decode (demux_t *demux, void *data, block_t *block)
437 stream_DemuxSend ((stream_t *)data, block);
439 block_Release (block);
444 * Static payload types handler
448 * PCMU: G.711 µ-law (RFC3551)
450 static void *pcmu_init (demux_t *demux)
454 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('u', 'l', 'a', 'w'));
455 fmt.audio.i_rate = 8000;
456 fmt.audio.i_channels = 1;
457 return codec_init (demux, &fmt);
463 static void *gsm_init (demux_t *demux)
467 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('g', 's', 'm', ' '));
468 fmt.audio.i_rate = 8000;
469 fmt.audio.i_channels = 1;
470 return codec_init (demux, &fmt);
474 * PCMA: G.711 A-law (RFC3551)
476 static void *pcma_init (demux_t *demux)
480 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('a', 'l', 'a', 'w'));
481 fmt.audio.i_rate = 8000;
482 fmt.audio.i_channels = 1;
483 return codec_init (demux, &fmt);
487 * L16: 16-bits (network byte order) PCM
489 static void *l16s_init (demux_t *demux)
493 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('s', '1', '6', 'b'));
494 fmt.audio.i_rate = 44100;
495 fmt.audio.i_channels = 2;
496 return codec_init (demux, &fmt);
499 static void *l16m_init (demux_t *demux)
503 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('s', '1', '6', 'b'));
504 fmt.audio.i_rate = 44100;
505 fmt.audio.i_channels = 1;
506 return codec_init (demux, &fmt);
512 static void *qcelp_init (demux_t *demux)
516 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('Q', 'c', 'l', 'p'));
517 fmt.audio.i_rate = 8000;
518 fmt.audio.i_channels = 1;
519 return codec_init (demux, &fmt);
523 * MPA: MPEG Audio (RFC2250, §3.4)
525 static void *mpa_init (demux_t *demux)
529 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('m', 'p', 'g', 'a'));
530 fmt.audio.i_channels = 2;
531 fmt.b_packetized = false;
532 return codec_init (demux, &fmt);
535 static void mpa_decode (demux_t *demux, void *data, block_t *block)
537 if (block->i_buffer < 4)
539 block_Release (block);
543 block->i_buffer -= 4; /* 32-bits RTP/MPA header */
544 block->p_buffer += 4;
546 codec_decode (demux, data, block);
551 * MPV: MPEG Video (RFC2250, §3.5)
553 static void *mpv_init (demux_t *demux)
557 es_format_Init (&fmt, VIDEO_ES, VLC_FOURCC ('m', 'p', 'g', 'v'));
558 fmt.b_packetized = false;
559 return codec_init (demux, &fmt);
562 static void mpv_decode (demux_t *demux, void *data, block_t *block)
564 if (block->i_buffer < 4)
566 block_Release (block);
570 block->i_buffer -= 4; /* 32-bits RTP/MPV header */
571 block->p_buffer += 4;
573 if (block->p_buffer[-3] & 0x4)
575 /* MPEG2 Video extension header */
576 /* TODO: shouldn't we skip this too ? */
579 codec_decode (demux, data, block);
584 * MP2: MPEG TS (RFC2250, §2)
586 static void *ts_init (demux_t *demux)
588 return stream_init (demux, *demux->psz_demux ? demux->psz_demux : "ts");
592 /* Not using SDP, we need to guess the payload format used */
593 /* see http://www.iana.org/assignments/rtp-parameters */
594 int rtp_autodetect (demux_t *demux, rtp_session_t *session,
595 const block_t *block)
597 uint8_t ptype = rtp_ptype (block);
600 .destroy = codec_destroy,
601 .decode = codec_decode,
606 /* Remember to keep this in sync with modules/services_discovery/sap.c */
610 msg_Dbg (demux, "detected G.711 mu-law");
616 msg_Dbg (demux, "detected GSM");
622 msg_Dbg (demux, "detected G.711 A-law");
628 msg_Dbg (demux, "detected stereo PCM");
630 pt.frequency = 44100;
634 msg_Dbg (demux, "detected mono PCM");
636 pt.frequency = 44100;
640 msg_Dbg (demux, "detected QCELP");
641 pt.init = qcelp_init;
646 msg_Dbg (demux, "detected MPEG Audio");
648 pt.decode = mpa_decode;
649 pt.frequency = 90000;
653 msg_Dbg (demux, "detected MPEG Video");
655 pt.decode = mpv_decode;
656 pt.frequency = 90000;
660 msg_Dbg (demux, "detected MPEG2 TS");
662 pt.destroy = stream_destroy;
663 pt.decode = stream_decode;
664 pt.frequency = 90000;
670 rtp_add_type (demux, session, &pt);
675 * Dynamic payload type handlers
676 * Hmm, none implemented yet.
680 * Processing callback
682 static int Demux (demux_t *demux)
684 return rtp_process (demux) ? 0 : 1;