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