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