3 * @brief Real-Time Protocol (RTP) demux module for VLC media player
5 /*****************************************************************************
6 * Copyright (C) 2001-2005 the VideoLAN team
7 * Copyright © 2007-2008 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 SRTP_KEY_TEXT N_("SRTP key (hexadecimal)")
46 #define SRTP_KEY_LONGTEXT N_( \
47 "RTP packets will be authenticated and deciphered "\
48 "with this Secure RTP master shared secret key.")
50 #define SRTP_SALT_TEXT N_("SRTP salt (hexadecimal)")
51 #define SRTP_SALT_LONGTEXT N_( \
52 "Secure RTP requires a (non-secret) master salt value.")
54 #define RTP_MAX_SRC_TEXT N_("Maximum RTP sources")
55 #define RTP_MAX_SRC_LONGTEXT N_( \
56 "How many distinct active RTP sources are allowed at a time." )
58 #define RTP_TIMEOUT_TEXT N_("RTP source timeout (sec)")
59 #define RTP_TIMEOUT_LONGTEXT N_( \
60 "How long to wait for any packet before a source is expired.")
62 #define RTP_MAX_DROPOUT_TEXT N_("Maximum RTP sequence number dropout")
63 #define RTP_MAX_DROPOUT_LONGTEXT N_( \
64 "RTP packets will be discarded if they are too much ahead (i.e. in the " \
65 "future) by this many packets from the last received packet." )
67 #define RTP_MAX_MISORDER_TEXT N_("Maximum RTP sequence number misordering")
68 #define RTP_MAX_MISORDER_LONGTEXT N_( \
69 "RTP packets will be discarded if they are too far behind (i.e. in the " \
70 "past) by this many packets from the last received packet." )
72 static int Open (vlc_object_t *);
73 static void Close (vlc_object_t *);
79 set_shortname (_("RTP"))
80 set_description (_("(Experimental) Real-Time Protocol demuxer"))
81 set_category (CAT_INPUT)
82 set_subcategory (SUBCAT_INPUT_DEMUX)
83 set_capability ("access_demux", 0)
84 set_callbacks (Open, Close)
86 add_integer ("rtp-caching", 1000, NULL, RTP_CACHING_TEXT,
87 RTP_CACHING_LONGTEXT, true);
88 change_integer_range (0, 65535)
89 add_string ("srtp-key", "", NULL,
90 SRTP_KEY_TEXT, SRTP_KEY_LONGTEXT, false);
91 add_string ("srtp-salt", "", NULL,
92 SRTP_SALT_TEXT, SRTP_SALT_LONGTEXT, false);
93 add_integer ("rtp-max-src", 1, NULL, RTP_MAX_SRC_TEXT,
94 RTP_MAX_SRC_LONGTEXT, true);
95 change_integer_range (1, 255)
96 add_integer ("rtp-timeout", 5, NULL, RTP_TIMEOUT_TEXT,
97 RTP_TIMEOUT_LONGTEXT, true);
98 add_integer ("rtp-max-dropout", 3000, NULL, RTP_MAX_DROPOUT_TEXT,
99 RTP_MAX_DROPOUT_LONGTEXT, true);
100 change_integer_range (0, 32767)
101 add_integer ("rtp-max-misorder", 100, NULL, RTP_MAX_MISORDER_TEXT,
102 RTP_MAX_MISORDER_LONGTEXT, true);
103 change_integer_range (0, 32767)
105 add_shortcut ("dccp")
106 /*add_shortcut ("sctp")*/
107 add_shortcut ("rtptcp") /* "tcp" is already taken :( */
109 add_shortcut ("udplite")
113 * TODO: so much stuff
114 * - send RTCP-RR and RTCP-BYE
115 * - dynamic payload types (need SDP parser)
116 * - multiple medias (need SDP parser, and RTCP-SR parser for lip-sync)
117 * - support for access_filter in case of stream_Demux (MPEG-TS)
121 # define IPPROTO_DCCP 33 /* IANA */
124 #ifndef IPPROTO_UDPLITE
125 # define IPPROTO_UDPLITE 136 /* from IANA */
132 static int Demux (demux_t *);
133 static int Control (demux_t *, int i_query, va_list args);
134 static int extract_port (char **phost);
137 * Probes and initializes.
139 static int Open (vlc_object_t *obj)
141 demux_t *demux = (demux_t *)obj;
142 int tp; /* transport protocol */
144 if (!strcmp (demux->psz_access, "dccp"))
147 if (!strcmp (demux->psz_access, "rtptcp"))
150 if (!strcmp (demux->psz_access, "rtp"))
153 if (!strcmp (demux->psz_access, "udplite"))
154 tp = IPPROTO_UDPLITE;
158 char *tmp = strdup (demux->psz_path);
163 char *dhost = strchr (shost, '@');
167 /* Parses the port numbers */
168 int sport = 0, dport = 0;
169 sport = extract_port (&shost);
171 dport = extract_port (&dhost);
173 dport = 5004; /* avt-profile-1 port */
176 int fd = -1, rtcp_fd = -1;
181 case IPPROTO_UDPLITE:
182 if ((dport & 1) != 0 || (sport & 1) != 0)
183 msg_Err (obj, "Using odd port number is higly discouraged");
185 fd = net_OpenDgram (obj, dhost, dport,
186 shost, sport, AF_UNSPEC, tp);
189 rtcp_fd = net_OpenDgram (obj, dhost, dport + 1, shost,
190 sport ? (sport + 1) : 0, AF_UNSPEC, tp);
194 #ifndef SOCK_DCCP /* provisional API (FIXME) */
200 var_Create (obj, "dccp-service", VLC_VAR_STRING);
201 var_SetString (obj, "dccp-service", "RTPV"); /* FIXME: RTPA? */
202 fd = net_Connect (obj, shost, (sport + 1) & ~1, SOCK_DCCP, tp);
204 msg_Err (obj, "DCCP support not included");
209 fd = net_Connect (obj, shost, (sport + 1) & ~1, SOCK_STREAM, tp);
216 net_SetCSCov (fd, -1, 12);
218 /* Initializes demux */
219 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
228 vlc_mutex_init (&p_sys->lock);
229 vlc_cond_init (&p_sys->wait);
232 p_sys->rtcp_fd = rtcp_fd;
233 p_sys->caching = var_CreateGetInteger (obj, "rtp-caching");
234 p_sys->max_src = var_CreateGetInteger (obj, "rtp-max-src");
235 p_sys->timeout = var_CreateGetInteger (obj, "rtp-timeout");
236 p_sys->max_dropout = var_CreateGetInteger (obj, "rtp-max-dropout");
237 p_sys->max_misorder = var_CreateGetInteger (obj, "rtp-max-misorder");
238 p_sys->autodetect = true;
239 p_sys->framed_rtp = (tp == IPPROTO_TCP);
242 demux->pf_demux = Demux;
243 demux->pf_control = Control;
244 demux->p_sys = p_sys;
246 p_sys->session = rtp_session_create (demux);
247 if (p_sys->session == NULL)
250 char *key = var_CreateGetNonEmptyString (demux, "srtp-key");
253 p_sys->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
254 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
255 if (p_sys->srtp == NULL)
261 char *salt = var_CreateGetNonEmptyString (demux, "srtp-salt");
262 errno = srtp_setkeystring (p_sys->srtp, key, salt ? salt : "");
267 msg_Err (obj, "bad SRTP key/salt combination (%m)");
272 if (vlc_clone (&p_sys->thread, rtp_thread, demux,
273 VLC_THREAD_PRIORITY_INPUT))
275 p_sys->thread_ready = true;
287 static void Close (vlc_object_t *obj)
289 demux_t *demux = (demux_t *)obj;
290 demux_sys_t *p_sys = demux->p_sys;
292 if (p_sys->thread_ready)
294 vlc_cancel (p_sys->thread);
295 vlc_join (p_sys->thread, NULL);
297 vlc_cond_destroy (&p_sys->wait);
298 vlc_mutex_destroy (&p_sys->lock);
301 srtp_destroy (p_sys->srtp);
303 rtp_session_destroy (demux, p_sys->session);
304 if (p_sys->rtcp_fd != -1)
305 net_Close (p_sys->rtcp_fd);
306 net_Close (p_sys->fd);
312 * Extracts port number from "[host]:port" or "host:port" strings,
313 * and remove brackets from the host name.
314 * @param phost pointer to the string upon entry,
315 * pointer to the hostname upon return.
316 * @return port number, 0 if missing.
318 static int extract_port (char **phost)
320 char *host = *phost, *port;
324 host = ++*phost; /* skip '[' */
325 port = strchr (host, ']');
327 *port++ = '\0'; /* skip ']' */
330 port = strchr (host, ':');
334 *port++ = '\0'; /* skip ':' */
342 static int Control (demux_t *demux, int i_query, va_list args)
344 demux_sys_t *p_sys = demux->p_sys;
348 case DEMUX_GET_POSITION:
350 float *v = va_arg (args, float *);
355 case DEMUX_GET_LENGTH:
358 int64_t *v = va_arg (args, int64_t *);
363 case DEMUX_GET_PTS_DELAY:
365 int64_t *v = va_arg (args, int64_t *);
366 *v = p_sys->caching * 1000;
370 case DEMUX_CAN_PAUSE:
372 case DEMUX_CAN_CONTROL_PACE:
374 bool *v = (bool*)va_arg( args, bool * );
385 * Generic packet handlers
388 static void *codec_init (demux_t *demux, es_format_t *fmt)
390 return es_out_Add (demux->out, fmt);
393 static void codec_destroy (demux_t *demux, void *data)
396 es_out_Del (demux->out, (es_out_id_t *)data);
399 /* Send a packet to decoder */
400 static void codec_decode (demux_t *demux, void *data, block_t *block)
404 block->i_dts = 0; /* RTP does not specify this */
405 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts );
406 es_out_Send (demux->out, (es_out_id_t *)data, block);
409 block_Release (block);
413 static void *stream_init (demux_t *demux, const char *name)
415 return stream_DemuxNew (demux, name, demux->out);
418 static void stream_destroy (demux_t *demux, void *data)
421 stream_Delete ((stream_t *)data);
425 /* Send a packet to a chained demuxer */
426 static void stream_decode (demux_t *demux, void *data, block_t *block)
429 stream_DemuxSend ((stream_t *)data, block);
431 block_Release (block);
436 * Static payload types handler
440 * PCMU: G.711 µ-law (RFC3551)
442 static void *pcmu_init (demux_t *demux)
446 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('u', 'l', 'a', 'w'));
447 fmt.audio.i_rate = 8000;
448 fmt.audio.i_channels = 1;
449 return codec_init (demux, &fmt);
455 static void *gsm_init (demux_t *demux)
459 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('g', 's', 'm', ' '));
460 fmt.audio.i_rate = 8000;
461 fmt.audio.i_channels = 1;
462 return codec_init (demux, &fmt);
466 * PCMA: G.711 A-law (RFC3551)
468 static void *pcma_init (demux_t *demux)
472 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('a', 'l', 'a', 'w'));
473 fmt.audio.i_rate = 8000;
474 fmt.audio.i_channels = 1;
475 return codec_init (demux, &fmt);
479 * L16: 16-bits (network byte order) PCM
481 static void *l16s_init (demux_t *demux)
485 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('s', '1', '6', 'b'));
486 fmt.audio.i_rate = 44100;
487 fmt.audio.i_channels = 2;
488 return codec_init (demux, &fmt);
491 static void *l16m_init (demux_t *demux)
495 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('s', '1', '6', 'b'));
496 fmt.audio.i_rate = 44100;
497 fmt.audio.i_channels = 1;
498 return codec_init (demux, &fmt);
504 static void *qcelp_init (demux_t *demux)
508 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('Q', 'c', 'l', 'p'));
509 fmt.audio.i_rate = 8000;
510 fmt.audio.i_channels = 1;
511 return codec_init (demux, &fmt);
515 * MPA: MPEG Audio (RFC2250, §3.4)
517 static void *mpa_init (demux_t *demux)
521 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC ('m', 'p', 'g', 'a'));
522 fmt.audio.i_channels = 2;
523 return codec_init (demux, &fmt);
526 static void mpa_decode (demux_t *demux, void *data, block_t *block)
528 if (block->i_buffer < 4)
530 block_Release (block);
534 block->i_buffer -= 4; /* 32-bits RTP/MPA header */
535 block->p_buffer += 4;
537 codec_decode (demux, data, block);
542 * MPV: MPEG Video (RFC2250, §3.5)
544 static void *mpv_init (demux_t *demux)
548 es_format_Init (&fmt, VIDEO_ES, VLC_FOURCC ('m', 'p', 'g', 'v'));
549 return codec_init (demux, &fmt);
552 static void mpv_decode (demux_t *demux, void *data, block_t *block)
554 if (block->i_buffer < 4)
556 block_Release (block);
560 block->i_buffer -= 4; /* 32-bits RTP/MPV header */
561 block->p_buffer += 4;
563 if (block->p_buffer[-3] & 0x4)
565 /* MPEG2 Video extension header */
566 /* TODO: shouldn't we skip this too ? */
569 codec_decode (demux, data, block);
574 * MP2: MPEG TS (RFC2250, §2)
576 static void *ts_init (demux_t *demux)
578 return stream_init (demux, *demux->psz_demux ? demux->psz_demux : "ts");
582 /* Not using SDP, we need to guess the payload format used */
583 /* see http://www.iana.org/assignments/rtp-parameters */
584 int rtp_autodetect (demux_t *demux, rtp_session_t *session,
585 const block_t *block)
587 uint8_t ptype = rtp_ptype (block);
590 .destroy = codec_destroy,
591 .decode = codec_decode,
599 msg_Dbg (demux, "detected G.711 mu-law");
605 msg_Dbg (demux, "detected GSM");
611 msg_Dbg (demux, "detected G.711 A-law");
617 msg_Dbg (demux, "detected stereo PCM");
619 pt.frequency = 44100;
623 msg_Dbg (demux, "detected mono PCM");
625 pt.frequency = 44100;
629 msg_Dbg (demux, "detected QCELP");
630 pt.init = qcelp_init;
635 msg_Dbg (demux, "detected MPEG Audio");
637 pt.decode = mpa_decode;
638 pt.frequency = 90000;
642 msg_Dbg (demux, "detected MPEG Video");
644 pt.decode = mpv_decode;
645 pt.frequency = 90000;
649 msg_Dbg (demux, "detected MPEG2 TS");
651 pt.destroy = stream_destroy;
652 pt.decode = stream_decode;
653 pt.frequency = 90000;
659 rtp_add_type (demux, session, &pt);
664 * Dynamic payload type handlers
665 * Hmm, none implemented yet.
669 * Processing callback
671 static int Demux (demux_t *demux)
673 return rtp_process (demux) ? 0 : 1;