/*****************************************************************************
* udp.c
*****************************************************************************
- * Copyright (C) 2001-2005 the VideoLAN team
+ * Copyright (C) 2001-2007 the VideoLAN team
* $Id$
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
#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
"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") );
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();
*****************************************************************************/
static const char *ppsz_sout_options[] = {
+ "auto-mcast",
"caching",
"group",
"raw",
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
{
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";
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 );
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 );
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 */
/* 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 );
}
{
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 );
}
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++;
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;
+}