es: replace i_original_channels with i_chan_mode
[vlc.git] / modules / access / rtp / rtp.c
blob9c6c0666b442dba7d0f96e4184b04d92b1811941
1 /**
2 * @file rtp.c
3 * @brief Real-Time Protocol (RTP) demux module for VLC media player
4 */
5 /*****************************************************************************
6 * Copyright (C) 2001-2005 VLC authors and VideoLAN
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 Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
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 Lesser 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 ****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27 #include <stdarg.h>
28 #include <assert.h>
30 #include <vlc_common.h>
31 #include <vlc_demux.h>
32 #include <vlc_network.h>
33 #include <vlc_plugin.h>
34 #include <vlc_dialog.h>
35 #include <vlc_aout.h> /* aout_FormatPrepare() */
37 #include "rtp.h"
38 #ifdef HAVE_SRTP
39 # include <srtp.h>
40 # include <gcrypt.h>
41 # include <vlc_gcrypt.h>
42 #endif
44 #define RTCP_PORT_TEXT N_("RTCP (local) port")
45 #define RTCP_PORT_LONGTEXT N_( \
46 "RTCP packets will be received on this transport protocol port. " \
47 "If zero, multiplexed RTP/RTCP is used.")
49 #define SRTP_KEY_TEXT N_("SRTP key (hexadecimal)")
50 #define SRTP_KEY_LONGTEXT N_( \
51 "RTP packets will be authenticated and deciphered "\
52 "with this Secure RTP master shared secret key. "\
53 "This must be a 32-character-long hexadecimal string.")
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. " \
58 "This must be a 28-character-long hexadecimal string.")
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 " \
79 "payloads")
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 *);
92 * Module descriptor
94 vlc_module_begin ()
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 ("rtcp-port", 0, RTCP_PORT_TEXT,
103 RTCP_PORT_LONGTEXT, false)
104 change_integer_range (0, 65535)
105 change_safe ()
106 #ifdef HAVE_SRTP
107 add_string ("srtp-key", "",
108 SRTP_KEY_TEXT, SRTP_KEY_LONGTEXT, false)
109 change_safe ()
110 add_string ("srtp-salt", "",
111 SRTP_SALT_TEXT, SRTP_SALT_LONGTEXT, false)
112 change_safe ()
113 #endif
114 add_integer ("rtp-max-src", 1, RTP_MAX_SRC_TEXT,
115 RTP_MAX_SRC_LONGTEXT, true)
116 change_integer_range (1, 255)
117 add_integer ("rtp-timeout", 5, RTP_TIMEOUT_TEXT,
118 RTP_TIMEOUT_LONGTEXT, true)
119 add_integer ("rtp-max-dropout", 3000, RTP_MAX_DROPOUT_TEXT,
120 RTP_MAX_DROPOUT_LONGTEXT, true)
121 change_integer_range (0, 32767)
122 add_integer ("rtp-max-misorder", 100, RTP_MAX_MISORDER_TEXT,
123 RTP_MAX_MISORDER_LONGTEXT, true)
124 change_integer_range (0, 32767)
125 add_string ("rtp-dynamic-pt", NULL, RTP_DYNAMIC_PT_TEXT,
126 RTP_DYNAMIC_PT_LONGTEXT, true)
127 change_string_list (dynamic_pt_list, dynamic_pt_list_text)
129 /*add_shortcut ("sctp")*/
130 add_shortcut ("dccp", "rtptcp", /* "tcp" is already taken :( */
131 "rtp", "udplite")
132 vlc_module_end ()
135 * TODO: so much stuff
136 * - send RTCP-RR and RTCP-BYE
137 * - dynamic payload types (need SDP parser)
138 * - multiple medias (need SDP parser, and RTCP-SR parser for lip-sync)
139 * - support for stream_filter in case of chained demux (MPEG-TS)
142 #ifndef IPPROTO_DCCP
143 # define IPPROTO_DCCP 33 /* IANA */
144 #endif
146 #ifndef IPPROTO_UDPLITE
147 # define IPPROTO_UDPLITE 136 /* from IANA */
148 #endif
152 * Local prototypes
154 static int Control (demux_t *, int i_query, va_list args);
155 static int extract_port (char **phost);
158 * Probes and initializes.
160 static int Open (vlc_object_t *obj)
162 demux_t *demux = (demux_t *)obj;
163 int tp; /* transport protocol */
165 if (!strcmp (demux->psz_access, "dccp"))
166 tp = IPPROTO_DCCP;
167 else
168 if (!strcmp (demux->psz_access, "rtptcp"))
169 tp = IPPROTO_TCP;
170 else
171 if (!strcmp (demux->psz_access, "rtp"))
172 tp = IPPROTO_UDP;
173 else
174 if (!strcmp (demux->psz_access, "udplite"))
175 tp = IPPROTO_UDPLITE;
176 else
177 return VLC_EGENERIC;
179 char *tmp = strdup (demux->psz_location);
180 if (tmp == NULL)
181 return VLC_ENOMEM;
183 char *shost;
184 char *dhost = strchr (tmp, '@');
185 if (dhost != NULL)
187 *(dhost++) = '\0';
188 shost = tmp;
190 else
192 dhost = tmp;
193 shost = NULL;
196 /* Parses the port numbers */
197 int sport = 0, dport = 0;
198 if (shost != NULL)
199 sport = extract_port (&shost);
200 if (dhost != NULL)
201 dport = extract_port (&dhost);
202 if (dport == 0)
203 dport = 5004; /* avt-profile-1 port */
205 int rtcp_dport = var_CreateGetInteger (obj, "rtcp-port");
207 /* Try to connect */
208 int fd = -1, rtcp_fd = -1;
210 switch (tp)
212 case IPPROTO_UDP:
213 case IPPROTO_UDPLITE:
214 fd = net_OpenDgram (obj, dhost, dport, shost, sport, tp);
215 if (fd == -1)
216 break;
217 if (rtcp_dport > 0) /* XXX: source port is unknown */
218 rtcp_fd = net_OpenDgram (obj, dhost, rtcp_dport, shost, 0, tp);
219 break;
221 case IPPROTO_DCCP:
222 #ifndef SOCK_DCCP /* provisional API (FIXME) */
223 # ifdef __linux__
224 # define SOCK_DCCP 6
225 # endif
226 #endif
227 #ifdef SOCK_DCCP
228 var_Create (obj, "dccp-service", VLC_VAR_STRING);
229 var_SetString (obj, "dccp-service", "RTPV"); /* FIXME: RTPA? */
230 fd = net_Connect (obj, dhost, dport, SOCK_DCCP, tp);
231 #else
232 msg_Err (obj, "DCCP support not included");
233 #endif
234 break;
236 case IPPROTO_TCP:
237 fd = net_Connect (obj, dhost, dport, SOCK_STREAM, tp);
238 break;
241 free (tmp);
242 if (fd == -1)
243 return VLC_EGENERIC;
244 net_SetCSCov (fd, -1, 12);
246 /* Initializes demux */
247 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
248 if (p_sys == NULL)
250 net_Close (fd);
251 if (rtcp_fd != -1)
252 net_Close (rtcp_fd);
253 return VLC_EGENERIC;
256 p_sys->chained_demux = NULL;
257 #ifdef HAVE_SRTP
258 p_sys->srtp = NULL;
259 #endif
260 p_sys->fd = fd;
261 p_sys->rtcp_fd = rtcp_fd;
262 p_sys->max_src = var_CreateGetInteger (obj, "rtp-max-src");
263 p_sys->timeout = var_CreateGetInteger (obj, "rtp-timeout")
264 * CLOCK_FREQ;
265 p_sys->max_dropout = var_CreateGetInteger (obj, "rtp-max-dropout");
266 p_sys->max_misorder = var_CreateGetInteger (obj, "rtp-max-misorder");
267 p_sys->thread_ready = false;
268 p_sys->autodetect = true;
270 demux->pf_demux = NULL;
271 demux->pf_control = Control;
272 demux->p_sys = p_sys;
274 p_sys->session = rtp_session_create (demux);
275 if (p_sys->session == NULL)
276 goto error;
278 #ifdef HAVE_SRTP
279 char *key = var_CreateGetNonEmptyString (demux, "srtp-key");
280 if (key)
282 vlc_gcrypt_init ();
283 p_sys->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
284 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
285 if (p_sys->srtp == NULL)
287 free (key);
288 goto error;
291 char *salt = var_CreateGetNonEmptyString (demux, "srtp-salt");
292 int val = srtp_setkeystring (p_sys->srtp, key, salt ? salt : "");
293 free (salt);
294 free (key);
295 if (val)
297 msg_Err (obj, "bad SRTP key/salt combination (%s)",
298 vlc_strerror_c(val));
299 goto error;
302 #endif
304 if (vlc_clone (&p_sys->thread,
305 (tp != IPPROTO_TCP) ? rtp_dgram_thread : rtp_stream_thread,
306 demux, VLC_THREAD_PRIORITY_INPUT))
307 goto error;
308 p_sys->thread_ready = true;
309 return VLC_SUCCESS;
311 error:
312 Close (obj);
313 return VLC_EGENERIC;
318 * Releases resources
320 static void Close (vlc_object_t *obj)
322 demux_t *demux = (demux_t *)obj;
323 demux_sys_t *p_sys = demux->p_sys;
325 if (p_sys->thread_ready)
327 vlc_cancel (p_sys->thread);
328 vlc_join (p_sys->thread, NULL);
331 #ifdef HAVE_SRTP
332 if (p_sys->srtp)
333 srtp_destroy (p_sys->srtp);
334 #endif
335 if (p_sys->session)
336 rtp_session_destroy (demux, p_sys->session);
337 if (p_sys->rtcp_fd != -1)
338 net_Close (p_sys->rtcp_fd);
339 net_Close (p_sys->fd);
340 free (p_sys);
345 * Extracts port number from "[host]:port" or "host:port" strings,
346 * and remove brackets from the host name.
347 * @param phost pointer to the string upon entry,
348 * pointer to the hostname upon return.
349 * @return port number, 0 if missing.
351 static int extract_port (char **phost)
353 char *host = *phost, *port;
355 if (host[0] == '[')
357 host = ++*phost; /* skip '[' */
358 port = strchr (host, ']');
359 if (port)
360 *port++ = '\0'; /* skip ']' */
362 else
363 port = strchr (host, ':');
365 if (port == NULL)
366 return 0;
367 *port++ = '\0'; /* skip ':' */
368 return atoi (port);
373 * Control callback
375 static int Control (demux_t *demux, int query, va_list args)
377 demux_sys_t *sys = demux->p_sys;
379 switch (query)
381 case DEMUX_GET_PTS_DELAY:
383 int64_t *v = va_arg (args, int64_t *);
384 *v = INT64_C(1000) * var_InheritInteger (demux, "network-caching");
385 return VLC_SUCCESS;
388 case DEMUX_CAN_PAUSE:
389 case DEMUX_CAN_SEEK:
390 case DEMUX_CAN_CONTROL_PACE:
392 bool *v = va_arg( args, bool * );
393 *v = false;
394 return VLC_SUCCESS;
398 if (sys->chained_demux != NULL)
399 return vlc_demux_chained_ControlVa (sys->chained_demux, query, args);
401 switch (query)
403 case DEMUX_GET_POSITION:
405 float *v = va_arg (args, float *);
406 *v = 0.;
407 return VLC_SUCCESS;
410 case DEMUX_GET_LENGTH:
411 case DEMUX_GET_TIME:
413 int64_t *v = va_arg (args, int64_t *);
414 *v = 0;
415 return VLC_SUCCESS;
419 return VLC_EGENERIC;
424 * Generic packet handlers
427 void *codec_init (demux_t *demux, es_format_t *fmt)
429 if (fmt->i_cat == AUDIO_ES)
430 aout_FormatPrepare (&fmt->audio);
431 return es_out_Add (demux->out, fmt);
434 void codec_destroy (demux_t *demux, void *data)
436 if (data)
437 es_out_Del (demux->out, (es_out_id_t *)data);
440 /* Send a packet to decoder */
441 void codec_decode (demux_t *demux, void *data, block_t *block)
443 if (data)
445 block->i_dts = VLC_TS_INVALID; /* RTP does not specify this */
446 es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts );
447 es_out_Send (demux->out, (es_out_id_t *)data, block);
449 else
450 block_Release (block);
453 static void *stream_init (demux_t *demux, const char *name)
455 demux_sys_t *p_sys = demux->p_sys;
457 if (p_sys->chained_demux != NULL)
458 return NULL;
459 p_sys->chained_demux = vlc_demux_chained_New(VLC_OBJECT(demux), name,
460 demux->out);
461 return p_sys->chained_demux;
464 static void stream_destroy (demux_t *demux, void *data)
466 demux_sys_t *p_sys = demux->p_sys;
468 if (data)
470 vlc_demux_chained_Delete(data);
471 p_sys->chained_demux = NULL;
475 /* Send a packet to a chained demuxer */
476 static void stream_decode (demux_t *demux, void *data, block_t *block)
478 if (data)
479 vlc_demux_chained_Send(data, block);
480 else
481 block_Release (block);
482 (void)demux;
485 static void *demux_init (demux_t *demux)
487 return stream_init (demux, demux->psz_demux);
491 * Static payload types handler
494 /* PT=0
495 * PCMU: G.711 µ-law (RFC3551)
497 static void *pcmu_init (demux_t *demux)
499 es_format_t fmt;
501 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MULAW);
502 fmt.audio.i_rate = 8000;
503 fmt.audio.i_physical_channels = AOUT_CHAN_CENTER;
504 return codec_init (demux, &fmt);
507 /* PT=3
508 * GSM
510 static void *gsm_init (demux_t *demux)
512 es_format_t fmt;
514 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_GSM);
515 fmt.audio.i_rate = 8000;
516 fmt.audio.i_physical_channels = AOUT_CHAN_CENTER;
517 return codec_init (demux, &fmt);
520 /* PT=8
521 * PCMA: G.711 A-law (RFC3551)
523 static void *pcma_init (demux_t *demux)
525 es_format_t fmt;
527 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_ALAW);
528 fmt.audio.i_rate = 8000;
529 fmt.audio.i_physical_channels = AOUT_CHAN_CENTER;
530 return codec_init (demux, &fmt);
533 /* PT=10,11
534 * L16: 16-bits (network byte order) PCM
536 static void *l16s_init (demux_t *demux)
538 es_format_t fmt;
540 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16B);
541 fmt.audio.i_rate = 44100;
542 fmt.audio.i_physical_channels = AOUT_CHANS_STEREO;
543 return codec_init (demux, &fmt);
546 static void *l16m_init (demux_t *demux)
548 es_format_t fmt;
550 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16B);
551 fmt.audio.i_rate = 44100;
552 fmt.audio.i_physical_channels = AOUT_CHAN_CENTER;
553 return codec_init (demux, &fmt);
556 /* PT=12
557 * QCELP
559 static void *qcelp_init (demux_t *demux)
561 es_format_t fmt;
563 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_QCELP);
564 fmt.audio.i_rate = 8000;
565 fmt.audio.i_physical_channels = AOUT_CHAN_CENTER;
566 return codec_init (demux, &fmt);
569 /* PT=14
570 * MPA: MPEG Audio (RFC2250, §3.4)
572 static void *mpa_init (demux_t *demux)
574 es_format_t fmt;
576 es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MPGA);
577 fmt.audio.i_physical_channels = AOUT_CHANS_STEREO;
578 fmt.b_packetized = false;
579 return codec_init (demux, &fmt);
582 static void mpa_decode (demux_t *demux, void *data, block_t *block)
584 if (block->i_buffer < 4)
586 block_Release (block);
587 return;
590 block->i_buffer -= 4; /* 32-bits RTP/MPA header */
591 block->p_buffer += 4;
593 codec_decode (demux, data, block);
597 /* PT=32
598 * MPV: MPEG Video (RFC2250, §3.5)
600 static void *mpv_init (demux_t *demux)
602 es_format_t fmt;
604 es_format_Init (&fmt, VIDEO_ES, VLC_CODEC_MPGV);
605 fmt.b_packetized = false;
606 return codec_init (demux, &fmt);
609 static void mpv_decode (demux_t *demux, void *data, block_t *block)
611 if (block->i_buffer < 4)
613 block_Release (block);
614 return;
617 block->i_buffer -= 4; /* 32-bits RTP/MPV header */
618 block->p_buffer += 4;
619 #if 0
620 if (block->p_buffer[-3] & 0x4)
622 /* MPEG2 Video extension header */
623 /* TODO: shouldn't we skip this too ? */
625 #endif
626 codec_decode (demux, data, block);
630 /* PT=33
631 * MP2: MPEG TS (RFC2250, §2)
633 static void *ts_init (demux_t *demux)
635 char const* name = demux->psz_demux;
637 if (*name == '\0' || !strcasecmp(name, "any"))
638 name = NULL;
640 return stream_init (demux, name ? name : "ts");
644 /* Not using SDP, we need to guess the payload format used */
645 /* see http://www.iana.org/assignments/rtp-parameters */
646 void rtp_autodetect (demux_t *demux, rtp_session_t *session,
647 const block_t *block)
649 uint8_t ptype = rtp_ptype (block);
650 rtp_pt_t pt = {
651 .init = NULL,
652 .destroy = codec_destroy,
653 .decode = codec_decode,
654 .frequency = 0,
655 .number = ptype,
658 /* Remember to keep this in sync with modules/services_discovery/sap.c */
659 switch (ptype)
661 case 0:
662 msg_Dbg (demux, "detected G.711 mu-law");
663 pt.init = pcmu_init;
664 pt.frequency = 8000;
665 break;
667 case 3:
668 msg_Dbg (demux, "detected GSM");
669 pt.init = gsm_init;
670 pt.frequency = 8000;
671 break;
673 case 8:
674 msg_Dbg (demux, "detected G.711 A-law");
675 pt.init = pcma_init;
676 pt.frequency = 8000;
677 break;
679 case 10:
680 msg_Dbg (demux, "detected stereo PCM");
681 pt.init = l16s_init;
682 pt.frequency = 44100;
683 break;
685 case 11:
686 msg_Dbg (demux, "detected mono PCM");
687 pt.init = l16m_init;
688 pt.frequency = 44100;
689 break;
691 case 12:
692 msg_Dbg (demux, "detected QCELP");
693 pt.init = qcelp_init;
694 pt.frequency = 8000;
695 break;
697 case 14:
698 msg_Dbg (demux, "detected MPEG Audio");
699 pt.init = mpa_init;
700 pt.decode = mpa_decode;
701 pt.frequency = 90000;
702 break;
704 case 32:
705 msg_Dbg (demux, "detected MPEG Video");
706 pt.init = mpv_init;
707 pt.decode = mpv_decode;
708 pt.frequency = 90000;
709 break;
711 case 33:
712 msg_Dbg (demux, "detected MPEG2 TS");
713 pt.init = ts_init;
714 pt.destroy = stream_destroy;
715 pt.decode = stream_decode;
716 pt.frequency = 90000;
717 break;
719 default:
721 * If the rtp payload type is unknown then check demux if it is specified
723 if (!strcmp(demux->psz_demux, "h264")
724 || !strcmp(demux->psz_demux, "ts"))
726 msg_Dbg (demux, "dynamic payload format %s specified by demux",
727 demux->psz_demux);
728 pt.init = demux_init;
729 pt.destroy = stream_destroy;
730 pt.decode = stream_decode;
731 pt.frequency = 90000;
732 break;
734 if (ptype >= 96)
736 char *dynamic = var_InheritString(demux, "rtp-dynamic-pt");
737 if (dynamic == NULL)
739 else if (!strcmp(dynamic, "theora"))
741 msg_Dbg (demux, "assuming Theora Encoded Video");
742 pt.init = theora_init;
743 pt.destroy = xiph_destroy;
744 pt.decode = xiph_decode;
745 pt.frequency = 90000;
747 free (dynamic);
748 break;
750 else
751 msg_Err (demux, "unknown dynamic payload format `%s' "
752 "specified", dynamic);
753 free (dynamic);
756 msg_Err (demux, "unspecified payload format (type %"PRIu8")", ptype);
757 msg_Info (demux, "A valid SDP is needed to parse this RTP stream.");
758 vlc_dialog_display_error (demux, N_("SDP required"),
759 N_("A description in SDP format is required to receive the RTP "
760 "stream. Note that rtp:// URIs cannot work with dynamic "
761 "RTP payload format (%"PRIu8")."), ptype);
762 return;
764 rtp_add_type (demux, session, &pt);
768 * Dynamic payload type handlers
769 * Hmm, none implemented yet apart from Xiph ones.