]> git.sesse.net Git - vlc/blobdiff - modules/access_output/udp.c
Undo broken Win32 ifdefs (breaks IPv6 case on decent OSes)
[vlc] / modules / access_output / udp.c
index 0879830c5ef1c5dc13c0904b5f5f0fd67a05e9e0..3ba7aa517e2431d391b987bcf591cc418c850a30 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * udp.c
  *****************************************************************************
- * Copyright (C) 2001-2005 the VideoLAN team
+ * Copyright (C) 2001-2007 the VideoLAN team
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
@@ -43,9 +43,6 @@
 #ifdef WIN32
 #   include <winsock2.h>
 #   include <ws2tcpip.h>
-#   ifndef IN_MULTICAST
-#       define IN_MULTICAST(a) IN_CLASSD(a)
-#   endif
 #else
 #   include <sys/socket.h>
 #endif
@@ -103,6 +100,9 @@ static void Close( vlc_object_t * );
                        "directly, without trying to fill the MTU (ie, " \
                        "without trying to make the biggest possible packets " \
                        "in order to improve streaming)." )
+#define AUTO_MCAST_TEXT N_("Automatic multicast streaming")
+#define AUTO_MCAST_LONGTEXT N_("Allocates an outbound multicast address " \
+                               "automatically.")
 
 vlc_module_begin();
     set_description( _("UDP stream output") );
@@ -115,12 +115,13 @@ vlc_module_begin();
     add_suppressed_integer( SOUT_CFG_PREFIX "late" );
     add_bool( SOUT_CFG_PREFIX "raw",  0, NULL, RAW_TEXT, RAW_LONGTEXT,
                                  VLC_TRUE );
+    add_bool( SOUT_CFG_PREFIX "auto-mcast", 0, NULL, AUTO_MCAST_TEXT,
+              AUTO_MCAST_LONGTEXT, VLC_TRUE );
 
     set_capability( "sout access", 100 );
     add_shortcut( "udp" );
     add_shortcut( "rtp" ); // Will work only with ts muxer
     add_shortcut( "udplite" );
-    add_shortcut( "rtplite" );
     set_callbacks( Open, Close );
 vlc_module_end();
 
@@ -129,6 +130,7 @@ vlc_module_end();
  *****************************************************************************/
 
 static const char *ppsz_sout_options[] = {
+    "auto-mcast",
     "caching",
     "group",
     "raw",
@@ -150,6 +152,7 @@ static int  Seek    ( sout_access_out_t *, off_t  );
 
 static void ThreadWrite( vlc_object_t * );
 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
+static const char *MakeRandMulticast (int family, char *buf, size_t buflen);
 
 typedef struct sout_access_thread_t
 {
@@ -194,8 +197,7 @@ static int Open( vlc_object_t *p_this )
     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
     sout_access_out_sys_t   *p_sys;
 
-    char                *psz_parser;
-    char                *psz_dst_addr;
+    char                *psz_dst_addr = NULL;
     int                 i_dst_port, proto = IPPROTO_UDP, cscov = 8;
     const char          *protoname = "UDP";
 
@@ -208,59 +210,61 @@ static int Open( vlc_object_t *p_this )
     config_ChainParse( p_access, "",
                        ppsz_core_options, p_access->p_cfg );
 
-    if( !( p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
+    if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
     {
         msg_Err( p_access, "not enough memory" );
         return VLC_EGENERIC;
     }
-    memset( p_sys, 0, sizeof(sout_access_out_sys_t) );
     p_access->p_sys = p_sys;
 
     if( p_access->psz_access != NULL )
     {
-        if (strncmp (p_access->psz_access, "rtp", 3) == 0)
+        if (strcmp (p_access->psz_access, "rtp") == 0)
+            p_sys->b_rtpts = VLC_TRUE;
+        if (strcmp (p_access->psz_access, "udplite") == 0)
         {
-            p_sys->b_rtpts = 1;
-            cscov += RTP_HEADER_LENGTH;
-        }
-        if ((strlen (p_access->psz_access) >= 3)
-         && (strcmp (p_access->psz_access + 3, "lite") == 0))
+            protoname = "UDP-Lite";
             proto = IPPROTO_UDPLITE;
+            p_sys->b_rtpts = VLC_TRUE;
+        }
     }
+    if (p_sys->b_rtpts)
+        cscov += RTP_HEADER_LENGTH;
 
-    psz_parser = strdup( p_access->psz_name );
+    i_dst_port = DEFAULT_PORT;
+    if (var_CreateGetBool (p_access, SOUT_CFG_PREFIX"auto-mcast"))
+    {
+        char buf[INET6_ADDRSTRLEN];
+        if (MakeRandMulticast (AF_INET, buf, sizeof (buf)) != NULL)
+            psz_dst_addr = strdup (buf);
+    }
+    else
+    {
+        char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
 
-    psz_dst_addr = psz_parser;
-    i_dst_port = 0;
+        if (psz_parser[0] == '[')
+            psz_parser = strchr (psz_parser, ']');
 
-    if ( *psz_parser == '[' )
-    {
-        while( *psz_parser && *psz_parser != ']' )
+        psz_parser = strchr (psz_parser, ':');
+        if (psz_parser != NULL)
         {
-            psz_parser++;
+            *psz_parser++ = '\0';
+            i_dst_port = atoi (psz_parser);
         }
     }
-    while( *psz_parser && *psz_parser != ':' )
-    {
-        psz_parser++;
-    }
-    if( *psz_parser == ':' )
-    {
-        *psz_parser = '\0';
-        psz_parser++;
-        i_dst_port = atoi( psz_parser );
-    }
-    if( i_dst_port <= 0 )
-    {
-        i_dst_port = DEFAULT_PORT;
-    }
+
+    if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
+     || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
+     || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
+     || var_Create (p_access, "src-addr", VLC_VAR_STRING))
+        return VLC_ENOMEM;
 
     p_sys->p_thread =
         vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
     if( !p_sys->p_thread )
     {
         msg_Err( p_access, "out of memory" );
-        return VLC_EGENERIC;
+        return VLC_ENOMEM;
     }
 
     vlc_object_attach( p_sys->p_thread, p_access );
@@ -273,10 +277,28 @@ static int Open( vlc_object_t *p_this )
     i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1, proto );
     if( i_handle == -1 )
     {
-         msg_Err( p_access, "failed to create UDP socket" );
+         msg_Err( p_access, "failed to create %s socket", protoname );
          return VLC_EGENERIC;
     }
+    else
+    {
+        char addr[NI_MAXNUMERICHOST];
+        int port;
+
+        if (net_GetSockAddress (i_handle, addr, &port) == 0)
+        {
+            msg_Dbg (p_access, "source: %s port %d", addr, port);
+            var_SetString (p_access, "src-addr", addr);
+            var_SetInteger (p_access, "src-port", port);
+        }
 
+        if (net_GetPeerAddress (i_handle, addr, &port) == 0)
+        {
+            msg_Dbg (p_access, "destination: %s port %d", addr, port);
+            var_SetString (p_access, "dst-addr", addr);
+            var_SetInteger (p_access, "dst-port", port);
+        }
+    }
     p_sys->p_thread->i_handle = i_handle;
     net_StopRecv( i_handle );
 
@@ -313,9 +335,6 @@ static int Open( vlc_object_t *p_this )
 
     p_access->pf_seek = Seek;
 
-    msg_Dbg( p_access, "udp access output opened(%s:%d)",
-             psz_dst_addr, i_dst_port );
-
     free( psz_dst_addr );
 
     /* update p_sout->i_out_pace_nocontrol */
@@ -357,7 +376,7 @@ static void Close( vlc_object_t * p_this )
     /* update p_sout->i_out_pace_nocontrol */
     p_access->p_sout->i_out_pace_nocontrol--;
 
-    msg_Dbg( p_access, "udp access output closed" );
+    msg_Dbg( p_access, "UDP access output closed" );
     free( p_sys );
 }
 
@@ -386,7 +405,7 @@ static int Write( sout_access_out_t *p_access, block_t *p_buffer )
         {
             if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < mdate() )
             {
-                msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
+                msg_Dbg( p_access, "late packet for UDP input (" I64Fd ")",
                          mdate() - p_sys->p_buffer->i_dts
                           - p_sys->p_thread->i_caching );
             }
@@ -399,7 +418,7 @@ static int Write( sout_access_out_t *p_access, block_t *p_buffer )
             int i_payload_size = p_sys->i_mtu;
             if( p_sys->b_rtpts )
                 i_payload_size -= RTP_HEADER_LENGTH;
-            
+
             int i_write = __MIN( p_buffer->i_buffer, i_payload_size );
 
             i_packets++;
@@ -617,3 +636,36 @@ static void ThreadWrite( vlc_object_t *p_this )
         i_date_last = i_date;
     }
 }
+
+
+static const char *MakeRandMulticast (int family, char *buf, size_t buflen)
+{
+    uint32_t rand = (getpid() & 0xffff)
+                  | (uint32_t)(((mdate () >> 10) & 0xffff) << 16);
+
+    switch (family)
+    {
+#ifdef AF_INET6
+        case AF_INET6:
+        {
+            struct in6_addr addr;
+            memcpy (&addr, "\xff\x38\x00\x00" "\x00\x00\x00\x00"
+                           "\x00\x00\x00\x00", 12);
+            rand |= 0x80000000;
+            memcpy (addr.s6_addr + 12, &(uint32_t){ htonl (rand) }, 4);
+            return inet_ntop (family, &addr, buf, buflen);
+        }
+#endif
+
+        case AF_INET:
+        {
+            struct in_addr addr;
+            addr.s_addr = htonl ((rand & 0xffffff) | 0xe8000000);
+            return inet_ntop (family, &addr, buf, buflen);
+        }
+    }
+#ifdef EAFNOSUPPORT
+    errno = EAFNOSUPPORT;
+#endif
+    return NULL;
+}