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