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