]> git.sesse.net Git - vlc/blobdiff - modules/access/rtp/rtp.c
Document size restrictions of SRTP parameters
[vlc] / modules / access / rtp / rtp.c
index 15075ad61767ec40c47f9dba6e84984f277697c2..169216649f3665be9ea400f8f5726f6b40e524ad 100644 (file)
@@ -8,7 +8,7 @@
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2.0
+ * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
@@ -16,7 +16,7 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU Lesser General Public
+ * You should have received a copy of the GNU General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  ****************************************************************************/
 #endif
 #include <stdarg.h>
 #include <assert.h>
+#include <errno.h>
 
 #include <vlc_common.h>
 #include <vlc_demux.h>
-#include <vlc_aout.h>
 #include <vlc_network.h>
 #include <vlc_plugin.h>
-
-#include <vlc_codecs.h>
+#include <vlc_dialog.h>
 
 #include "rtp.h"
-#include <srtp.h>
-
-#define RTP_CACHING_TEXT N_("RTP de-jitter buffer length (msec)")
-#define RTP_CACHING_LONGTEXT N_( \
-    "How long to wait for late RTP packets (and delay the performance)." )
+#ifdef HAVE_SRTP
+# include <srtp.h>
+# include <gcrypt.h>
+# include <vlc_gcrypt.h>
+#endif
 
 #define RTCP_PORT_TEXT N_("RTCP (local) port")
 #define RTCP_PORT_LONGTEXT N_( \
 #define SRTP_KEY_TEXT N_("SRTP key (hexadecimal)")
 #define SRTP_KEY_LONGTEXT N_( \
     "RTP packets will be authenticated and deciphered "\
-    "with this Secure RTP master shared secret key.")
+    "with this Secure RTP master shared secret key. "\
+    "This must be a 32-character-long hexadecimal string.")
 
 #define SRTP_SALT_TEXT N_("SRTP salt (hexadecimal)")
 #define SRTP_SALT_LONGTEXT N_( \
-    "Secure RTP requires a (non-secret) master salt value.")
+    "Secure RTP requires a (non-secret) master salt value. " \
+    "This must be a 28-character-long hexadecimal string.")
 
 #define RTP_MAX_SRC_TEXT N_("Maximum RTP sources")
 #define RTP_MAX_SRC_LONGTEXT N_( \
     "RTP packets will be discarded if they are too far behind (i.e. in the " \
     "past) by this many packets from the last received packet." )
 
+#define RTP_DYNAMIC_PT_TEXT N_("RTP payload format assumed for dynamic " \
+                               "payloads")
+#define RTP_DYNAMIC_PT_LONGTEXT N_( \
+    "This payload format will be assumed for dynamic payload types " \
+    "(between 96 and 127) if it can't be determined otherwise with " \
+    "out-of-band mappings (SDP)" )
+
+static const char *const dynamic_pt_list[] = { "theora" };
+static const char *const dynamic_pt_list_text[] = { "Theora Encoded Video" };
+
 static int  Open (vlc_object_t *);
 static void Close (vlc_object_t *);
 
@@ -88,35 +99,36 @@ vlc_module_begin ()
     set_capability ("access_demux", 0)
     set_callbacks (Open, Close)
 
-    add_integer ("rtp-caching", 1000, NULL, RTP_CACHING_TEXT,
-                 RTP_CACHING_LONGTEXT, true)
-        change_integer_range (0, 65535)
-        change_safe ()
-    add_integer ("rtcp-port", 0, NULL, RTCP_PORT_TEXT,
+    add_integer ("rtcp-port", 0, RTCP_PORT_TEXT,
                  RTCP_PORT_LONGTEXT, false)
         change_integer_range (0, 65535)
         change_safe ()
-    add_string ("srtp-key", "", NULL,
+#ifdef HAVE_SRTP
+    add_string ("srtp-key", "",
                 SRTP_KEY_TEXT, SRTP_KEY_LONGTEXT, false)
-    add_string ("srtp-salt", "", NULL,
+        change_safe ()
+    add_string ("srtp-salt", "",
                 SRTP_SALT_TEXT, SRTP_SALT_LONGTEXT, false)
-    add_integer ("rtp-max-src", 1, NULL, RTP_MAX_SRC_TEXT,
+        change_safe ()
+#endif
+    add_integer ("rtp-max-src", 1, RTP_MAX_SRC_TEXT,
                  RTP_MAX_SRC_LONGTEXT, true)
         change_integer_range (1, 255)
-    add_integer ("rtp-timeout", 5, NULL, RTP_TIMEOUT_TEXT,
+    add_integer ("rtp-timeout", 5, RTP_TIMEOUT_TEXT,
                  RTP_TIMEOUT_LONGTEXT, true)
-    add_integer ("rtp-max-dropout", 3000, NULL, RTP_MAX_DROPOUT_TEXT,
+    add_integer ("rtp-max-dropout", 3000, RTP_MAX_DROPOUT_TEXT,
                  RTP_MAX_DROPOUT_LONGTEXT, true)
         change_integer_range (0, 32767)
-    add_integer ("rtp-max-misorder", 100, NULL, RTP_MAX_MISORDER_TEXT,
+    add_integer ("rtp-max-misorder", 100, RTP_MAX_MISORDER_TEXT,
                  RTP_MAX_MISORDER_LONGTEXT, true)
         change_integer_range (0, 32767)
+    add_string ("rtp-dynamic-pt", NULL, RTP_DYNAMIC_PT_TEXT,
+                RTP_DYNAMIC_PT_LONGTEXT, true)
+        change_string_list (dynamic_pt_list, dynamic_pt_list_text, NULL)
 
-    add_shortcut ("dccp")
     /*add_shortcut ("sctp")*/
-    add_shortcut ("rtptcp") /* "tcp" is already taken :( */
-    add_shortcut ("rtp")
-    add_shortcut ("udplite")
+    add_shortcut ("dccp", "rtptcp", /* "tcp" is already taken :( */
+                  "rtp", "udplite")
 vlc_module_end ()
 
 /*
@@ -139,7 +151,6 @@ vlc_module_end ()
 /*
  * Local prototypes
  */
-static int Demux (demux_t *);
 static int Control (demux_t *, int i_query, va_list args);
 static int extract_port (char **phost);
 
@@ -165,18 +176,27 @@ static int Open (vlc_object_t *obj)
     else
         return VLC_EGENERIC;
 
-    char *tmp = strdup (demux->psz_path);
-    char *shost = tmp;
-    if (shost == NULL)
+    char *tmp = strdup (demux->psz_location);
+    if (tmp == NULL)
         return VLC_ENOMEM;
 
-    char *dhost = strchr (shost, '@');
-    if (dhost)
-        *dhost++ = '\0';
+    char *shost;
+    char *dhost = strchr (tmp, '@');
+    if (dhost != NULL)
+    {
+        *(dhost++) = '\0';
+        shost = tmp;
+    }
+    else
+    {
+        dhost = tmp;
+        shost = NULL;
+    }
 
     /* Parses the port numbers */
     int sport = 0, dport = 0;
-    sport = extract_port (&shost);
+    if (shost != NULL)
+        sport = extract_port (&shost);
     if (dhost != NULL)
         dport = extract_port (&dhost);
     if (dport == 0)
@@ -191,13 +211,11 @@ static int Open (vlc_object_t *obj)
     {
         case IPPROTO_UDP:
         case IPPROTO_UDPLITE:
-            fd = net_OpenDgram (obj, dhost, dport,
-                                shost, sport, AF_UNSPEC, tp);
+            fd = net_OpenDgram (obj, dhost, dport, shost, sport, tp);
             if (fd == -1)
                 break;
             if (rtcp_dport > 0) /* XXX: source port is unknown */
-                rtcp_fd = net_OpenDgram (obj, dhost, rtcp_dport, shost, 0,
-                                         AF_UNSPEC, tp);
+                rtcp_fd = net_OpenDgram (obj, dhost, rtcp_dport, shost, 0, tp);
             break;
 
          case IPPROTO_DCCP:
@@ -209,14 +227,14 @@ static int Open (vlc_object_t *obj)
 #ifdef SOCK_DCCP
             var_Create (obj, "dccp-service", VLC_VAR_STRING);
             var_SetString (obj, "dccp-service", "RTPV"); /* FIXME: RTPA? */
-            fd = net_Connect (obj, shost, sport, SOCK_DCCP, tp);
+            fd = net_Connect (obj, dhost, dport, SOCK_DCCP, tp);
 #else
             msg_Err (obj, "DCCP support not included");
 #endif
             break;
 
         case IPPROTO_TCP:
-            fd = net_Connect (obj, shost, sport, SOCK_STREAM, tp);
+            fd = net_Connect (obj, dhost, dport, SOCK_STREAM, tp);
             break;
     }
 
@@ -235,20 +253,20 @@ static int Open (vlc_object_t *obj)
         return VLC_EGENERIC;
     }
 
-    vlc_mutex_init (&p_sys->lock);
-    vlc_cond_init (&p_sys->wait);
+#ifdef HAVE_SRTP
     p_sys->srtp         = NULL;
+#endif
     p_sys->fd           = fd;
     p_sys->rtcp_fd      = rtcp_fd;
-    p_sys->caching      = var_CreateGetInteger (obj, "rtp-caching");
     p_sys->max_src      = var_CreateGetInteger (obj, "rtp-max-src");
-    p_sys->timeout      = var_CreateGetInteger (obj, "rtp-timeout");
+    p_sys->timeout      = var_CreateGetInteger (obj, "rtp-timeout")
+                        * CLOCK_FREQ;
     p_sys->max_dropout  = var_CreateGetInteger (obj, "rtp-max-dropout");
     p_sys->max_misorder = var_CreateGetInteger (obj, "rtp-max-misorder");
-    p_sys->framed_rtp   = (tp == IPPROTO_TCP);
-    p_sys->dead         = false;
+    p_sys->thread_ready = false;
+    p_sys->autodetect   = true;
 
-    demux->pf_demux   = Demux;
+    demux->pf_demux   = NULL;
     demux->pf_control = Control;
     demux->p_sys      = p_sys;
 
@@ -256,9 +274,11 @@ static int Open (vlc_object_t *obj)
     if (p_sys->session == NULL)
         goto error;
 
+#ifdef HAVE_SRTP
     char *key = var_CreateGetNonEmptyString (demux, "srtp-key");
     if (key)
     {
+        vlc_gcrypt_init ();
         p_sys->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
                                    SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
         if (p_sys->srtp == NULL)
@@ -277,9 +297,11 @@ static int Open (vlc_object_t *obj)
             goto error;
         }
     }
+#endif
 
-    if (vlc_clone (&p_sys->thread, rtp_thread, demux,
-                   VLC_THREAD_PRIORITY_INPUT))
+    if (vlc_clone (&p_sys->thread,
+                   (tp != IPPROTO_TCP) ? rtp_dgram_thread : rtp_stream_thread,
+                   demux, VLC_THREAD_PRIORITY_INPUT))
         goto error;
     p_sys->thread_ready = true;
     return VLC_SUCCESS;
@@ -303,11 +325,11 @@ static void Close (vlc_object_t *obj)
         vlc_cancel (p_sys->thread);
         vlc_join (p_sys->thread, NULL);
     }
-    vlc_cond_destroy (&p_sys->wait);
-    vlc_mutex_destroy (&p_sys->lock);
 
+#ifdef HAVE_SRTP
     if (p_sys->srtp)
         srtp_destroy (p_sys->srtp);
+#endif
     if (p_sys->session)
         rtp_session_destroy (demux, p_sys->session);
     if (p_sys->rtcp_fd != -1)
@@ -350,8 +372,6 @@ static int extract_port (char **phost)
  */
 static int Control (demux_t *demux, int i_query, va_list args)
 {
-    demux_sys_t *p_sys = demux->p_sys;
-
     switch (i_query)
     {
         case DEMUX_GET_POSITION:
@@ -372,7 +392,7 @@ static int Control (demux_t *demux, int i_query, va_list args)
         case DEMUX_GET_PTS_DELAY:
         {
             int64_t *v = va_arg (args, int64_t *);
-            *v = p_sys->caching * 1000;
+            *v = INT64_C(1000) * var_InheritInteger (demux, "network-caching");
             return VLC_SUCCESS;
         }
 
@@ -394,23 +414,23 @@ static int Control (demux_t *demux, int i_query, va_list args)
  * Generic packet handlers
  */
 
-static void *codec_init (demux_t *demux, es_format_t *fmt)
+void *codec_init (demux_t *demux, es_format_t *fmt)
 {
     return es_out_Add (demux->out, fmt);
 }
 
-static void codec_destroy (demux_t *demux, void *data)
+void codec_destroy (demux_t *demux, void *data)
 {
     if (data)
         es_out_Del (demux->out, (es_out_id_t *)data);
 }
 
 /* Send a packet to decoder */
-static void codec_decode (demux_t *demux, void *data, block_t *block)
+void codec_decode (demux_t *demux, void *data, block_t *block)
 {
     if (data)
     {
-        block->i_dts = 0; /* RTP does not specify this */
+        block->i_dts = VLC_TS_INVALID; /* RTP does not specify this */
         es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts );
         es_out_Send (demux->out, (es_out_id_t *)data, block);
     }
@@ -418,7 +438,6 @@ static void codec_decode (demux_t *demux, void *data, block_t *block)
         block_Release (block);
 }
 
-
 static void *stream_init (demux_t *demux, const char *name)
 {
     return stream_DemuxNew (demux, name, demux->out);
@@ -441,6 +460,11 @@ static void stream_decode (demux_t *demux, void *data, block_t *block)
     (void)demux;
 }
 
+static void *demux_init (demux_t *demux)
+{
+    return stream_init (demux, demux->psz_demux);
+}
+
 /*
  * Static payload types handler
  */
@@ -592,8 +616,8 @@ static void *ts_init (demux_t *demux)
 
 /* Not using SDP, we need to guess the payload format used */
 /* see http://www.iana.org/assignments/rtp-parameters */
-int rtp_autodetect (demux_t *demux, rtp_session_t *session,
-                    const block_t *block)
+void rtp_autodetect (demux_t *demux, rtp_session_t *session,
+                     const block_t *block)
 {
     uint8_t ptype = rtp_ptype (block);
     rtp_pt_t pt = {
@@ -666,21 +690,51 @@ int rtp_autodetect (demux_t *demux, rtp_session_t *session,
         break;
 
       default:
-        return -1;
+        /*
+         * If the rtp payload type is unknown then check demux if it is specified
+         */
+        if (!strcmp(demux->psz_demux, "h264")
+         || !strcmp(demux->psz_demux, "ts"))
+        {
+            msg_Dbg (demux, "dynamic payload format %s specified by demux",
+                     demux->psz_demux);
+            pt.init = demux_init;
+            pt.destroy = stream_destroy;
+            pt.decode = stream_decode;
+            pt.frequency = 90000;
+            break;
+        }
+        if (ptype >= 96)
+        {
+            char *dynamic = var_InheritString(demux, "rtp-dynamic-pt");
+            if (dynamic == NULL)
+                ;
+            else if (!strcmp(dynamic, "theora"))
+            {
+                msg_Dbg (demux, "assuming Theora Encoded Video");
+                pt.init = theora_init;
+                pt.destroy = xiph_destroy;
+                pt.decode = xiph_decode;
+                pt.frequency = 90000;
+            }
+            else
+                msg_Err (demux, "unknown dynamic payload format `%s' "
+                                "specified", dynamic);
+            free (dynamic);
+        }
+
+        msg_Err (demux, "unspecified payload format (type %"PRIu8")", ptype);
+        msg_Info (demux, "A valid SDP is needed to parse this RTP stream.");
+        dialog_Fatal (demux, N_("SDP required"),
+             N_("A description in SDP format is required to receive the RTP "
+                "stream. Note that rtp:// URIs cannot work with dynamic "
+                "RTP payload format (%"PRIu8")."), ptype);
+        return;
     }
     rtp_add_type (demux, session, &pt);
-    return 0;
 }
 
 /*
  * Dynamic payload type handlers
- * Hmm, none implemented yet.
+ * Hmm, none implemented yet apart from Xiph ones.
  */
-
-/**
- * Processing callback
- */
-static int Demux (demux_t *demux)
-{
-    return rtp_process (demux) ? 0 : 1;
-}