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