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