]> git.sesse.net Git - vlc/blob - modules/access/rtp/rtp.c
RTP: support build without libvlc_srtp
[vlc] / modules / access / rtp / rtp.c
1 /**
2  * @file rtp.c
3  * @brief Real-Time Protocol (RTP) demux module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright (C) 2001-2005 the VideoLAN team
7  * Copyright © 2007-2009 Rémi Denis-Courmont
8  *
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.
13  *
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.
18  *
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  ****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27 #include <stdarg.h>
28 #include <assert.h>
29 #include <errno.h>
30
31 #include <vlc_common.h>
32 #include <vlc_demux.h>
33 #include <vlc_aout.h>
34 #include <vlc_network.h>
35 #include <vlc_plugin.h>
36
37 #include <vlc_codecs.h>
38
39 #include "rtp.h"
40 #ifdef HAVE_SRTP
41 # include <srtp.h>
42 #endif
43
44 #define RTP_CACHING_TEXT N_("RTP de-jitter buffer length (msec)")
45 #define RTP_CACHING_LONGTEXT N_( \
46     "How long to wait for late RTP packets (and delay the performance)." )
47
48 #define RTCP_PORT_TEXT N_("RTCP (local) port")
49 #define RTCP_PORT_LONGTEXT N_( \
50     "RTCP packets will be received on this transport protocol port. " \
51     "If zero, multiplexed RTP/RTCP is used.")
52
53 #define SRTP_KEY_TEXT N_("SRTP key (hexadecimal)")
54 #define SRTP_KEY_LONGTEXT N_( \
55     "RTP packets will be authenticated and deciphered "\
56     "with this Secure RTP master shared secret key.")
57
58 #define SRTP_SALT_TEXT N_("SRTP salt (hexadecimal)")
59 #define SRTP_SALT_LONGTEXT N_( \
60     "Secure RTP requires a (non-secret) master salt value.")
61
62 #define RTP_MAX_SRC_TEXT N_("Maximum RTP sources")
63 #define RTP_MAX_SRC_LONGTEXT N_( \
64     "How many distinct active RTP sources are allowed at a time." )
65
66 #define RTP_TIMEOUT_TEXT N_("RTP source timeout (sec)")
67 #define RTP_TIMEOUT_LONGTEXT N_( \
68     "How long to wait for any packet before a source is expired.")
69
70 #define RTP_MAX_DROPOUT_TEXT N_("Maximum RTP sequence number dropout")
71 #define RTP_MAX_DROPOUT_LONGTEXT N_( \
72     "RTP packets will be discarded if they are too much ahead (i.e. in the " \
73     "future) by this many packets from the last received packet." )
74
75 #define RTP_MAX_MISORDER_TEXT N_("Maximum RTP sequence number misordering")
76 #define RTP_MAX_MISORDER_LONGTEXT N_( \
77     "RTP packets will be discarded if they are too far behind (i.e. in the " \
78     "past) by this many packets from the last received packet." )
79
80 static int  Open (vlc_object_t *);
81 static void Close (vlc_object_t *);
82
83 /*
84  * Module descriptor
85  */
86 vlc_module_begin ()
87     set_shortname (N_("RTP"))
88     set_description (N_("Real-Time Protocol (RTP) input"))
89     set_category (CAT_INPUT)
90     set_subcategory (SUBCAT_INPUT_DEMUX)
91     set_capability ("access_demux", 0)
92     set_callbacks (Open, Close)
93
94     add_integer ("rtp-caching", 1000, NULL, RTP_CACHING_TEXT,
95                  RTP_CACHING_LONGTEXT, true)
96         change_integer_range (0, 65535)
97         change_safe ()
98     add_integer ("rtcp-port", 0, NULL, RTCP_PORT_TEXT,
99                  RTCP_PORT_LONGTEXT, false)
100         change_integer_range (0, 65535)
101         change_safe ()
102 #ifdef HAVE_SRTP
103     add_string ("srtp-key", "", NULL,
104                 SRTP_KEY_TEXT, SRTP_KEY_LONGTEXT, false)
105     add_string ("srtp-salt", "", NULL,
106                 SRTP_SALT_TEXT, SRTP_SALT_LONGTEXT, false)
107 #endif
108     add_integer ("rtp-max-src", 1, NULL, RTP_MAX_SRC_TEXT,
109                  RTP_MAX_SRC_LONGTEXT, true)
110         change_integer_range (1, 255)
111     add_integer ("rtp-timeout", 5, NULL, RTP_TIMEOUT_TEXT,
112                  RTP_TIMEOUT_LONGTEXT, true)
113     add_integer ("rtp-max-dropout", 3000, NULL, RTP_MAX_DROPOUT_TEXT,
114                  RTP_MAX_DROPOUT_LONGTEXT, true)
115         change_integer_range (0, 32767)
116     add_integer ("rtp-max-misorder", 100, NULL, RTP_MAX_MISORDER_TEXT,
117                  RTP_MAX_MISORDER_LONGTEXT, true)
118         change_integer_range (0, 32767)
119
120     add_shortcut ("dccp")
121     /*add_shortcut ("sctp")*/
122     add_shortcut ("rtptcp") /* "tcp" is already taken :( */
123     add_shortcut ("rtp")
124     add_shortcut ("udplite")
125 vlc_module_end ()
126
127 /*
128  * TODO: so much stuff
129  * - send RTCP-RR and RTCP-BYE
130  * - dynamic payload types (need SDP parser)
131  * - multiple medias (need SDP parser, and RTCP-SR parser for lip-sync)
132  * - support for stream_filter in case of stream_Demux (MPEG-TS)
133  */
134
135 #ifndef IPPROTO_DCCP
136 # define IPPROTO_DCCP 33 /* IANA */
137 #endif
138
139 #ifndef IPPROTO_UDPLITE
140 # define IPPROTO_UDPLITE 136 /* from IANA */
141 #endif
142
143
144 /*
145  * Local prototypes
146  */
147 static int Control (demux_t *, int i_query, va_list args);
148 static int extract_port (char **phost);
149
150 /**
151  * Probes and initializes.
152  */
153 static int Open (vlc_object_t *obj)
154 {
155     demux_t *demux = (demux_t *)obj;
156     int tp; /* transport protocol */
157
158     if (!strcmp (demux->psz_access, "dccp"))
159         tp = IPPROTO_DCCP;
160     else
161     if (!strcmp (demux->psz_access, "rtptcp"))
162         tp = IPPROTO_TCP;
163     else
164     if (!strcmp (demux->psz_access, "rtp"))
165         tp = IPPROTO_UDP;
166     else
167     if (!strcmp (demux->psz_access, "udplite"))
168         tp = IPPROTO_UDPLITE;
169     else
170         return VLC_EGENERIC;
171
172     char *tmp = strdup (demux->psz_path);
173     char *shost = tmp;
174     if (shost == NULL)
175         return VLC_ENOMEM;
176
177     char *dhost = strchr (shost, '@');
178     if (dhost)
179         *dhost++ = '\0';
180
181     /* Parses the port numbers */
182     int sport = 0, dport = 0;
183     sport = extract_port (&shost);
184     if (dhost != NULL)
185         dport = extract_port (&dhost);
186     if (dport == 0)
187         dport = 5004; /* avt-profile-1 port */
188
189     int rtcp_dport = var_CreateGetInteger (obj, "rtcp-port");
190
191     /* Try to connect */
192     int fd = -1, rtcp_fd = -1;
193
194     switch (tp)
195     {
196         case IPPROTO_UDP:
197         case IPPROTO_UDPLITE:
198             fd = net_OpenDgram (obj, dhost, dport,
199                                 shost, sport, AF_UNSPEC, tp);
200             if (fd == -1)
201                 break;
202             if (rtcp_dport > 0) /* XXX: source port is unknown */
203                 rtcp_fd = net_OpenDgram (obj, dhost, rtcp_dport, shost, 0,
204                                          AF_UNSPEC, tp);
205             break;
206
207          case IPPROTO_DCCP:
208 #ifndef SOCK_DCCP /* provisional API (FIXME) */
209 # ifdef __linux__
210 #  define SOCK_DCCP 6
211 # endif
212 #endif
213 #ifdef SOCK_DCCP
214             var_Create (obj, "dccp-service", VLC_VAR_STRING);
215             var_SetString (obj, "dccp-service", "RTPV"); /* FIXME: RTPA? */
216             fd = net_Connect (obj, shost, sport, SOCK_DCCP, tp);
217 #else
218             msg_Err (obj, "DCCP support not included");
219 #endif
220             break;
221
222         case IPPROTO_TCP:
223             fd = net_Connect (obj, shost, sport, SOCK_STREAM, tp);
224             break;
225     }
226
227     free (tmp);
228     if (fd == -1)
229         return VLC_EGENERIC;
230     net_SetCSCov (fd, -1, 12);
231
232     /* Initializes demux */
233     demux_sys_t *p_sys = malloc (sizeof (*p_sys));
234     if (p_sys == NULL)
235     {
236         net_Close (fd);
237         if (rtcp_fd != -1)
238             net_Close (rtcp_fd);
239         return VLC_EGENERIC;
240     }
241
242     vlc_mutex_init (&p_sys->lock);
243 #ifdef HAVE_SRTP
244     p_sys->srtp         = NULL;
245 #endif
246     p_sys->fd           = fd;
247     p_sys->rtcp_fd      = rtcp_fd;
248     p_sys->caching      = var_CreateGetInteger (obj, "rtp-caching");
249     p_sys->max_src      = var_CreateGetInteger (obj, "rtp-max-src");
250     p_sys->timeout      = var_CreateGetInteger (obj, "rtp-timeout");
251     p_sys->max_dropout  = var_CreateGetInteger (obj, "rtp-max-dropout");
252     p_sys->max_misorder = var_CreateGetInteger (obj, "rtp-max-misorder");
253     p_sys->framed_rtp   = (tp == IPPROTO_TCP);
254
255     demux->pf_demux   = NULL;
256     demux->pf_control = Control;
257     demux->p_sys      = p_sys;
258
259     p_sys->session = rtp_session_create (demux);
260     if (p_sys->session == NULL)
261         goto error;
262
263 #ifdef HAVE_SRTP
264     char *key = var_CreateGetNonEmptyString (demux, "srtp-key");
265     if (key)
266     {
267         p_sys->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
268                                    SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
269         if (p_sys->srtp == NULL)
270         {
271             free (key);
272             goto error;
273         }
274
275         char *salt = var_CreateGetNonEmptyString (demux, "srtp-salt");
276         errno = srtp_setkeystring (p_sys->srtp, key, salt ? salt : "");
277         free (salt);
278         free (key);
279         if (errno)
280         {
281             msg_Err (obj, "bad SRTP key/salt combination (%m)");
282             goto error;
283         }
284     }
285 #endif
286
287     if (vlc_clone (&p_sys->thread, rtp_thread, demux,
288                    VLC_THREAD_PRIORITY_INPUT))
289         goto error;
290     p_sys->thread_ready = true;
291     return VLC_SUCCESS;
292
293 error:
294     Close (obj);
295     return VLC_EGENERIC;
296 }
297
298
299 /**
300  * Releases resources
301  */
302 static void Close (vlc_object_t *obj)
303 {
304     demux_t *demux = (demux_t *)obj;
305     demux_sys_t *p_sys = demux->p_sys;
306
307     if (p_sys->thread_ready)
308     {
309         vlc_cancel (p_sys->thread);
310         vlc_join (p_sys->thread, NULL);
311     }
312     vlc_mutex_destroy (&p_sys->lock);
313
314 #ifdef HAVE_SRTP
315     if (p_sys->srtp)
316         srtp_destroy (p_sys->srtp);
317 #endif
318     if (p_sys->session)
319         rtp_session_destroy (demux, p_sys->session);
320     if (p_sys->rtcp_fd != -1)
321         net_Close (p_sys->rtcp_fd);
322     net_Close (p_sys->fd);
323     free (p_sys);
324 }
325
326
327 /**
328  * Extracts port number from "[host]:port" or "host:port" strings,
329  * and remove brackets from the host name.
330  * @param phost pointer to the string upon entry,
331  * pointer to the hostname upon return.
332  * @return port number, 0 if missing.
333  */
334 static int extract_port (char **phost)
335 {
336     char *host = *phost, *port;
337
338     if (host[0] == '[')
339     {
340         host = ++*phost; /* skip '[' */
341         port = strchr (host, ']');
342         if (port)
343             *port++ = '\0'; /* skip ']' */
344     }
345     else
346         port = strchr (host, ':');
347
348     if (port == NULL)
349         return 0;
350     *port++ = '\0'; /* skip ':' */
351     return atoi (port);
352 }
353
354
355 /**
356  * Control callback
357  */
358 static int Control (demux_t *demux, int i_query, va_list args)
359 {
360     demux_sys_t *p_sys = demux->p_sys;
361
362     switch (i_query)
363     {
364         case DEMUX_GET_POSITION:
365         {
366             float *v = va_arg (args, float *);
367             *v = 0.;
368             return VLC_SUCCESS;
369         }
370
371         case DEMUX_GET_LENGTH:
372         case DEMUX_GET_TIME:
373         {
374             int64_t *v = va_arg (args, int64_t *);
375             *v = 0;
376             return VLC_SUCCESS;
377         }
378
379         case DEMUX_GET_PTS_DELAY:
380         {
381             int64_t *v = va_arg (args, int64_t *);
382             *v = (int64_t)p_sys->caching * 1000;
383             return VLC_SUCCESS;
384         }
385
386         case DEMUX_CAN_PAUSE:
387         case DEMUX_CAN_SEEK:
388         case DEMUX_CAN_CONTROL_PACE:
389         {
390             bool *v = (bool*)va_arg( args, bool * );
391             *v = false;
392             return VLC_SUCCESS;
393         }
394     }
395
396     return VLC_EGENERIC;
397 }
398
399
400 /*
401  * Generic packet handlers
402  */
403
404 static void *codec_init (demux_t *demux, es_format_t *fmt)
405 {
406     return es_out_Add (demux->out, fmt);
407 }
408
409 static void codec_destroy (demux_t *demux, void *data)
410 {
411     if (data)
412         es_out_Del (demux->out, (es_out_id_t *)data);
413 }
414
415 /* Send a packet to decoder */
416 static void codec_decode (demux_t *demux, void *data, block_t *block)
417 {
418     if (data)
419     {
420         block->i_dts = 0; /* RTP does not specify this */
421         es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts );
422         es_out_Send (demux->out, (es_out_id_t *)data, block);
423     }
424     else
425         block_Release (block);
426 }
427
428
429 static void *stream_init (demux_t *demux, const char *name)
430 {
431     return stream_DemuxNew (demux, name, demux->out);
432 }
433
434 static void stream_destroy (demux_t *demux, void *data)
435 {
436     if (data)
437         stream_Delete ((stream_t *)data);
438     (void)demux;
439 }
440
441 /* Send a packet to a chained demuxer */
442 static void stream_decode (demux_t *demux, void *data, block_t *block)
443 {
444     if (data)
445         stream_DemuxSend ((stream_t *)data, block);
446     else
447         block_Release (block);
448     (void)demux;
449 }
450
451 /*
452  * Static payload types handler
453  */
454
455 /* PT=0
456  * PCMU: G.711 µ-law (RFC3551)
457  */
458 static void *pcmu_init (demux_t *demux)
459 {
460     es_format_t fmt;
461
462     es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MULAW);
463     fmt.audio.i_rate = 8000;
464     fmt.audio.i_channels = 1;
465     return codec_init (demux, &fmt);
466 }
467
468 /* PT=3
469  * GSM
470  */
471 static void *gsm_init (demux_t *demux)
472 {
473     es_format_t fmt;
474
475     es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_GSM);
476     fmt.audio.i_rate = 8000;
477     fmt.audio.i_channels = 1;
478     return codec_init (demux, &fmt);
479 }
480
481 /* PT=8
482  * PCMA: G.711 A-law (RFC3551)
483  */
484 static void *pcma_init (demux_t *demux)
485 {
486     es_format_t fmt;
487
488     es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_ALAW);
489     fmt.audio.i_rate = 8000;
490     fmt.audio.i_channels = 1;
491     return codec_init (demux, &fmt);
492 }
493
494 /* PT=10,11
495  * L16: 16-bits (network byte order) PCM
496  */
497 static void *l16s_init (demux_t *demux)
498 {
499     es_format_t fmt;
500
501     es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16B);
502     fmt.audio.i_rate = 44100;
503     fmt.audio.i_channels = 2;
504     return codec_init (demux, &fmt);
505 }
506
507 static void *l16m_init (demux_t *demux)
508 {
509     es_format_t fmt;
510
511     es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_S16B);
512     fmt.audio.i_rate = 44100;
513     fmt.audio.i_channels = 1;
514     return codec_init (demux, &fmt);
515 }
516
517 /* PT=12
518  * QCELP
519  */
520 static void *qcelp_init (demux_t *demux)
521 {
522     es_format_t fmt;
523
524     es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_QCELP);
525     fmt.audio.i_rate = 8000;
526     fmt.audio.i_channels = 1;
527     return codec_init (demux, &fmt);
528 }
529
530 /* PT=14
531  * MPA: MPEG Audio (RFC2250, §3.4)
532  */
533 static void *mpa_init (demux_t *demux)
534 {
535     es_format_t fmt;
536
537     es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MPGA);
538     fmt.audio.i_channels = 2;
539     fmt.b_packetized = false;
540     return codec_init (demux, &fmt);
541 }
542
543 static void mpa_decode (demux_t *demux, void *data, block_t *block)
544 {
545     if (block->i_buffer < 4)
546     {
547         block_Release (block);
548         return;
549     }
550
551     block->i_buffer -= 4; /* 32-bits RTP/MPA header */
552     block->p_buffer += 4;
553
554     codec_decode (demux, data, block);
555 }
556
557
558 /* PT=32
559  * MPV: MPEG Video (RFC2250, §3.5)
560  */
561 static void *mpv_init (demux_t *demux)
562 {
563     es_format_t fmt;
564
565     es_format_Init (&fmt, VIDEO_ES, VLC_CODEC_MPGV);
566     fmt.b_packetized = false;
567     return codec_init (demux, &fmt);
568 }
569
570 static void mpv_decode (demux_t *demux, void *data, block_t *block)
571 {
572     if (block->i_buffer < 4)
573     {
574         block_Release (block);
575         return;
576     }
577
578     block->i_buffer -= 4; /* 32-bits RTP/MPV header */
579     block->p_buffer += 4;
580 #if 0
581     if (block->p_buffer[-3] & 0x4)
582     {
583         /* MPEG2 Video extension header */
584         /* TODO: shouldn't we skip this too ? */
585     }
586 #endif
587     codec_decode (demux, data, block);
588 }
589
590
591 /* PT=33
592  * MP2: MPEG TS (RFC2250, §2)
593  */
594 static void *ts_init (demux_t *demux)
595 {
596     return stream_init (demux, *demux->psz_demux ? demux->psz_demux : "ts");
597 }
598
599
600 /* Not using SDP, we need to guess the payload format used */
601 /* see http://www.iana.org/assignments/rtp-parameters */
602 int rtp_autodetect (demux_t *demux, rtp_session_t *session,
603                     const block_t *block)
604 {
605     uint8_t ptype = rtp_ptype (block);
606     rtp_pt_t pt = {
607         .init = NULL,
608         .destroy = codec_destroy,
609         .decode = codec_decode,
610         .frequency = 0,
611         .number = ptype,
612     };
613
614     /* Remember to keep this in sync with modules/services_discovery/sap.c */
615     switch (ptype)
616     {
617       case 0:
618         msg_Dbg (demux, "detected G.711 mu-law");
619         pt.init = pcmu_init;
620         pt.frequency = 8000;
621         break;
622
623       case 3:
624         msg_Dbg (demux, "detected GSM");
625         pt.init = gsm_init;
626         pt.frequency = 8000;
627         break;
628
629       case 8:
630         msg_Dbg (demux, "detected G.711 A-law");
631         pt.init = pcma_init;
632         pt.frequency = 8000;
633         break;
634
635       case 10:
636         msg_Dbg (demux, "detected stereo PCM");
637         pt.init = l16s_init;
638         pt.frequency = 44100;
639         break;
640
641       case 11:
642         msg_Dbg (demux, "detected mono PCM");
643         pt.init = l16m_init;
644         pt.frequency = 44100;
645         break;
646
647       case 12:
648         msg_Dbg (demux, "detected QCELP");
649         pt.init = qcelp_init;
650         pt.frequency = 8000;
651         break;
652
653       case 14:
654         msg_Dbg (demux, "detected MPEG Audio");
655         pt.init = mpa_init;
656         pt.decode = mpa_decode;
657         pt.frequency = 90000;
658         break;
659
660       case 32:
661         msg_Dbg (demux, "detected MPEG Video");
662         pt.init = mpv_init;
663         pt.decode = mpv_decode;
664         pt.frequency = 90000;
665         break;
666
667       case 33:
668         msg_Dbg (demux, "detected MPEG2 TS");
669         pt.init = ts_init;
670         pt.destroy = stream_destroy;
671         pt.decode = stream_decode;
672         pt.frequency = 90000;
673         break;
674
675       default:
676         return -1;
677     }
678     rtp_add_type (demux, session, &pt);
679     return 0;
680 }
681
682 /*
683  * Dynamic payload type handlers
684  * Hmm, none implemented yet.
685  */