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