Commit 861ef85c authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont
Browse files

Big SAP/announce cleanup

plus several minor bugfixes in SAP announce

OTOH, SAP is currently broken for standard output (and untested for RTP)
parent 4c858d7e
......@@ -209,28 +209,14 @@ static inline int sout_StreamIdSend( sout_stream_t *s, sout_stream_id_t *id, blo
/****************************************************************************
* Announce handler mess
****************************************************************************/
struct sap_session_t;
struct session_descriptor_t
{
char *psz_name;
char *psz_uri;
int i_port;
int i_payload; /* SAP Payload type */
char *psz_group;
char *psz_sdp;
vlc_bool_t b_rtp;
};
VLC_EXPORT( int, sout_AnnounceRegister, (sout_instance_t *,session_descriptor_t*, announce_method_t* ) );
VLC_EXPORT(session_descriptor_t*,sout_AnnounceRegisterSDP, (sout_instance_t *,const char *, const char *, announce_method_t* ) );
VLC_EXPORT(session_descriptor_t*,sout_AnnounceRegisterSDP, (sout_instance_t *, const char *, const char *, const char *, announce_method_t* ) );
VLC_EXPORT( int, sout_AnnounceUnRegister, (sout_instance_t *,session_descriptor_t* ) );
VLC_EXPORT(session_descriptor_t*,sout_AnnounceSessionCreate, (void) );
VLC_EXPORT(session_descriptor_t*,sout_AnnounceSessionCreate, (vlc_object_t *obj, const char *cfgpref) );
VLC_EXPORT(void, sout_AnnounceSessionDestroy, (session_descriptor_t *) );
VLC_EXPORT(announce_method_t*, sout_SAPMethod, (void) );
VLC_EXPORT(void, sout_MethodRelease, (announce_method_t *) );
#ifdef __cplusplus
}
......
......@@ -399,7 +399,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++;
......
......@@ -1545,12 +1545,13 @@ static int SapSetup( sout_stream_t *p_stream )
if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
{
p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, SOUT_CFG_PREFIX,
p_sys->psz_sdp,
p_sys->psz_destination,
p_method );
}
free( p_method );
sout_MethodRelease( p_method );
return VLC_SUCCESS;
}
......
......@@ -58,6 +58,25 @@
"This allows you to specify a group for the session, that will be announced "\
"if you choose to use SAP." )
#define DESC_TEXT N_("Session descriptipn")
#define DESC_LONGTEXT N_( \
"This allows you to give a short description with details about the stream, " \
"that will be announced in the SDP (Session Descriptor)." )
#define URL_TEXT N_("Session URL")
#define URL_LONGTEXT N_( \
"This allows you to give an URL with more details about the stream " \
"(often the website of the streaming organization), that will " \
"be announced in the SDP (Session Descriptor)." )
#define EMAIL_TEXT N_("Session email")
#define EMAIL_LONGTEXT N_( \
"This allows you to give a contact mail address for the stream, that will " \
"be announced in the SDP (Session Descriptor)." )
#define PHONE_TEXT N_("Session phone number")
#define PHONE_LONGTEXT N_( \
"This allows you to give a contact telephone number for the stream, that will " \
"be announced in the SDP (Session Descriptor)." )
#define SAP_TEXT N_("SAP announcing")
#define SAP_LONGTEXT N_("Announce this session with SAP.")
......@@ -81,13 +100,20 @@ vlc_module_begin();
MUX_LONGTEXT, VLC_FALSE );
add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
DST_LONGTEXT, VLC_FALSE );
add_deprecated( SOUT_CFG_PREFIX "url", VLC_FALSE );
add_bool( SOUT_CFG_PREFIX "sap", 0, NULL, SAP_TEXT, SAP_LONGTEXT, VLC_TRUE );
add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT, NAME_LONGTEXT,
VLC_TRUE );
add_string( SOUT_CFG_PREFIX "group", "", NULL, GROUP_TEXT, GROUP_LONGTEXT,
VLC_TRUE );
add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT, DESC_LONGTEXT,
VLC_TRUE );
add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT, URL_LONGTEXT,
VLC_TRUE );
add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT, EMAIL_LONGTEXT,
VLC_TRUE );
add_string( SOUT_CFG_PREFIX "phone", "", NULL, PHONE_TEXT, PHONE_LONGTEXT,
VLC_TRUE );
add_suppressed_bool( SOUT_CFG_PREFIX "sap-ipv6" );
set_callbacks( Open, Close );
......@@ -99,7 +125,7 @@ vlc_module_end();
*****************************************************************************/
static const char *ppsz_sout_options[] = {
"access", "mux", "url", "dst",
"sap", "name", "group", NULL
"sap", "name", "group", "description", "url", "email", "phone", NULL
};
#define DEFAULT_PORT 1234
......@@ -310,29 +336,14 @@ static int Open( vlc_object_t *p_this )
}
msg_Dbg( p_stream, "mux opened" );
/* *** Create the SAP Session structure *** */
var_Get( p_stream, SOUT_CFG_PREFIX "sap", &val );
if( val.b_bool &&
( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
/* *** Create the SAP Session structure *** */
if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) &&
( strncmp( psz_access, "udp", 3 ) || strncmp( psz_access , "rtp", 3 ) ) )
{
session_descriptor_t *p_session = sout_AnnounceSessionCreate();
announce_method_t *p_method = sout_SAPMethod();
session_descriptor_t *p_session;
announce_method_t *p_method = sout_SAPMethod ();
vlc_url_t url;
var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
if( *val.psz_string )
p_session->psz_name = val.psz_string;
else
{
p_session->psz_name = strdup( psz_url );
free( val.psz_string );
}
var_Get( p_stream, SOUT_CFG_PREFIX "group", &val );
if( *val.psz_string )
p_session->psz_group = val.psz_string;
else
free( val.psz_string );
p_session = sout_AnnounceSessionCreate (VLC_OBJECT (p_stream), SOUT_CFG_PREFIX);
/* Now, parse the URL to extract host and port */
vlc_UrlParse( &url, psz_url , 0);
......@@ -341,20 +352,20 @@ static int Open( vlc_object_t *p_this )
{
if( url.i_port == 0 ) url.i_port = DEFAULT_PORT;
#if 0
p_session->psz_uri = strdup( url.psz_host );
p_session->i_port = url.i_port;
p_session->psz_sdp = NULL;
p_session->i_payload = 33;
p_session->b_rtp = strstr( psz_access, "rtp") ? 1 : 0;
#endif
msg_Info( p_this, "SAP Enabled");
sout_AnnounceRegister( p_sout, p_session, p_method );
p_stream->p_sys->p_session = p_session;
}
vlc_UrlClean( &url );
free( p_method );
sout_MethodRelease (p_method);
}
p_stream->pf_add = Add;
......
......@@ -27,9 +27,11 @@
#include <stdlib.h> /* free() */
#include <stdio.h> /* sprintf() */
#include <string.h> /* strerror() */
#include <assert.h>
#include <vlc/vlc.h>
#include <vlc_sout.h>
#include <vlc_network.h> /* FIXME: fix RegisterSDP() and remove this */
#include "stream_output.h"
struct announce_method_t
......@@ -82,11 +84,12 @@ int sout_AnnounceRegister( sout_instance_t *p_sout,
*
* \param p_sout a sout instance structure
* \param psz_sdp the SDP to register
* \param psz_uri session URI (needed for SAP address auto detection
* \param psz_uri session address (needed for SAP address auto detection)
* \param p_method an announce method descriptor
* \return the new session descriptor structure
*/
session_descriptor_t *sout_AnnounceRegisterSDP( sout_instance_t *p_sout,
session_descriptor_t *
sout_AnnounceRegisterSDP( sout_instance_t *p_sout, const char *cfgpref,
const char *psz_sdp, const char *psz_uri,
announce_method_t *p_method )
{
......@@ -107,9 +110,19 @@ session_descriptor_t *sout_AnnounceRegisterSDP( sout_instance_t *p_sout,
vlc_object_yield( p_announce );
}
p_session = sout_AnnounceSessionCreate();
p_session = sout_AnnounceSessionCreate(VLC_OBJECT (p_sout), cfgpref);
p_session->psz_sdp = strdup( psz_sdp );
p_session->psz_uri = strdup( psz_uri );
/* GRUIK. We should not convert back-and-forth from string to numbers */
struct addrinfo *res;
if (vlc_getaddrinfo (VLC_OBJECT (p_sout), psz_uri, 0, NULL, &res) == 0)
{
if (res->ai_addrlen <= sizeof (p_session->addr))
memcpy (&p_session->addr, res->ai_addr,
p_session->addrlen = res->ai_addrlen);
freeaddrinfo (res);
}
announce_Register( p_announce, p_session, p_method );
vlc_object_release( p_announce );
......@@ -148,15 +161,34 @@ int sout_AnnounceUnRegister( sout_instance_t *p_sout,
*
* \return a new session descriptor
*/
session_descriptor_t * sout_AnnounceSessionCreate(void)
session_descriptor_t * sout_AnnounceSessionCreate (vlc_object_t *obj,
const char *cfgpref)
{
session_descriptor_t *p_session;
size_t cfglen = strlen (cfgpref);
if (cfglen > 100)
return NULL;
char varname[cfglen + sizeof ("description")], *subvar = varname + cfglen;
strcpy (varname, cfgpref);
p_session = (session_descriptor_t *)malloc( sizeof(session_descriptor_t));
session_descriptor_t *p_session = calloc (1, sizeof (*p_session));
if (p_session == NULL)
return NULL;
memset (p_session, 0, sizeof (*p_session));
strcpy (subvar, "name");
p_session->psz_name = var_GetNonEmptyString (obj, varname);
strcpy (subvar, "group");
p_session->psz_group = var_GetNonEmptyString (obj, varname);
strcpy (subvar, "description");
p_session->description = var_GetNonEmptyString (obj, varname);
strcpy (subvar, "url");
p_session->url = var_GetNonEmptyString (obj, varname);
strcpy (subvar, "email");
p_session->email = var_GetNonEmptyString (obj, varname);
strcpy (subvar, "phone");
p_session->phone = var_GetNonEmptyString (obj, varname);
return p_session;
}
......@@ -170,10 +202,13 @@ void sout_AnnounceSessionDestroy( session_descriptor_t *p_session )
{
if( p_session )
{
FREENULL( p_session->psz_name );
FREENULL( p_session->psz_group );
FREENULL( p_session->psz_uri );
FREENULL( p_session->psz_sdp );
free (p_session->psz_name);
free (p_session->psz_group);
free (p_session->psz_sdp);
free (p_session->description);
free (p_session->url);
free (p_session->email);
free (p_session->phone);
free( p_session );
}
}
......@@ -186,6 +221,11 @@ announce_method_t * sout_SAPMethod (void)
return &sap_method;
}
void sout_MethodRelease (announce_method_t *m)
{
assert (m == &sap_method);
}
/************************************************************************
* Announce handler functions (private)
************************************************************************/
......@@ -241,6 +281,8 @@ int announce_Register( announce_handler_t *p_announce,
session_descriptor_t *p_session,
announce_method_t *p_method )
{
if (p_method == NULL)
return VLC_EGENERIC;
msg_Dbg( p_announce, "registering announce");
if( p_method == &sap_method )
......
......@@ -59,7 +59,8 @@
struct sap_address_t
{
char *psz_address;
char psz_machine[NI_MAXNUMERICHOST];
struct sockaddr_storage orig;
socklen_t origlen;
int i_rfd; /* Read socket */
int i_wfd; /* Write socket */
......@@ -74,7 +75,6 @@ struct sap_address_t
/* A SAP session descriptor, enqueued in the SAP handler queue */
struct sap_session_t {
char *psz_sdp;
uint8_t *psz_data;
unsigned i_length;
sap_address_t *p_address;
......@@ -160,7 +160,6 @@ void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
for( i = 0 ; i< p_sap->i_sessions ; i++)
{
sap_session_t *p_session = p_sap->pp_sessions[i];
FREENULL( p_session->psz_sdp );
FREENULL( p_session->psz_data );
REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i );
FREENULL( p_session );
......@@ -247,53 +246,27 @@ static void RunThread( vlc_object_t *p_this)
static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
session_descriptor_t *p_session )
{
int i_header_size, i;
char *psz_head, psz_addr[NI_MAXNUMERICHOST];
int i;
char psz_addr[NI_MAXNUMERICHOST];
vlc_bool_t b_ipv6 = VLC_FALSE, b_ssm = VLC_FALSE;
sap_session_t *p_sap_session;
mtime_t i_hash;
struct addrinfo hints, *res;
struct sockaddr_storage addr;
socklen_t addrlen;
vlc_mutex_lock( &p_sap->object_lock );
if( p_session->psz_uri == NULL )
{
vlc_mutex_unlock( &p_sap->object_lock );
msg_Err( p_sap, "*FIXME* unexpected NULL URI for SAP announce" );
msg_Err( p_sap, "This should not happen. VLC needs fixing." );
return VLC_EGENERIC;
}
/* Determine SAP multicast address automatically */
memset( &hints, 0, sizeof( hints ) );
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_NUMERICHOST;
i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0,
&hints, &res );
if( i )
addrlen = p_session->addrlen;
if ((addrlen == 0) || (addrlen > sizeof (addr)))
{
vlc_mutex_unlock( &p_sap->object_lock );
msg_Err( p_sap, "Invalid URI for SAP announce: %s: %s",
p_session->psz_uri, vlc_gai_strerror( i ) );
msg_Err( p_sap, "No/invalid address specified for SAP announce" );
return VLC_EGENERIC;
}
addrlen = res->ai_addrlen;
if ((unsigned)addrlen > sizeof (addr))
{
vlc_mutex_unlock( &p_sap->object_lock );
vlc_freeaddrinfo( res );
msg_Err( p_sap, "Unsupported address family of size %d > %u",
res->ai_addrlen, (unsigned) sizeof( addr ) );
return VLC_EGENERIC;
}
memcpy (&addr, res->ai_addr, addrlen);
vlc_freeaddrinfo (res);
/* Determine SAP multicast address automatically */
memcpy (&addr, &p_session->addr, addrlen);
switch( addr.ss_family )
switch( p_session->addr.ss_family )
{
#if defined (HAVE_INET_PTON) || defined (WIN32)
case AF_INET6:
......@@ -348,10 +321,11 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
ipv4 = 0xe0027ffe;
}
/* FIXME FIXME FIXME */
if( ipv4 == 0 )
{
msg_Err( p_sap, "Out-of-scope multicast address "
"not supported by SAP: %s", p_session->psz_uri );
"not supported by SAP" );
vlc_mutex_unlock( &p_sap->object_lock );
return VLC_EGENERIC;
}
......@@ -400,23 +374,16 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
malloc( sizeof(sap_address_t) );
if( !p_address )
{
msg_Err( p_sap, "out of memory" );
vlc_mutex_unlock( &p_sap->object_lock );
return VLC_ENOMEM;
}
p_address->psz_address = strdup( psz_addr );
p_address->i_wfd = net_ConnectUDP( VLC_OBJECT(p_sap), psz_addr, SAP_PORT, 255 );
if( p_address->i_wfd != -1 )
{
char *ptr;
net_StopRecv( p_address->i_wfd );
net_GetSockAddress( p_address->i_wfd, p_address->psz_machine,
NULL );
/* removes scope if present */
ptr = strchr( p_address->psz_machine, '%' );
if( ptr != NULL )
*ptr = '\0';
getsockname (p_address->i_wfd, (struct sockaddr *)&p_address->orig,
&p_address->origlen);
}
if( p_sap->b_control == VLC_TRUE )
......@@ -452,41 +419,28 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
p_sap_session->p_address = p_address;
}
if (p_session->origlen == 0)
memcpy (&p_session->orig, &p_sap_session->p_address->orig,
p_session->origlen = p_sap_session->p_address->origlen);
/* Build the SAP Headers */
i_header_size = ( b_ipv6 ? 16 : 4 ) + 20;
psz_head = (char *) malloc( i_header_size * sizeof( char ) );
if( psz_head == NULL )
{
msg_Err( p_sap, "out of memory" );
return VLC_ENOMEM;
}
/* SAPv1, not encrypted, not compressed */
psz_head[0] = b_ipv6 ? 0x30 : 0x20;
psz_head[1] = 0x00; /* No authentification length */
i_hash = mdate();
psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */
psz_head[3] = (i_hash & 0xFF); /* Msg id hash 2 */
#if defined (HAVE_INET_PTON) || defined (WIN32)
if( b_ipv6 )
size_t headsize = 20;
switch (p_session->orig.ss_family)
{
inet_pton( AF_INET6, /* can't fail */
p_sap_session->p_address->psz_machine,
psz_head + 4 );
}
else
#ifdef AF_INET6
case AF_INET6:
headsize += 16;
break;
#endif
{
inet_pton( AF_INET, /* can't fail */
p_sap_session->p_address->psz_machine,
psz_head + 4 );
case AF_INET:
headsize =+ 4;
break;
default:
msg_Err( p_sap, "Address family %d not supported by SAP",
addr.ss_family );
vlc_mutex_unlock( &p_sap->object_lock );
return VLC_EGENERIC;
}
memcpy( psz_head + (b_ipv6 ? 20 : 8), "application/sdp", 15 );
/* If needed, build the SDP */
if( p_session->psz_sdp == NULL )
{
......@@ -499,21 +453,57 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
}
}
p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
p_sap_session->i_last = 0;
p_sap_session->i_length = headsize + strlen (p_session->psz_sdp);
p_sap_session->psz_data = malloc (p_sap_session->i_length + 1);
if (p_sap_session->psz_data == NULL)
{
free (p_session->psz_sdp);
vlc_mutex_unlock( &p_sap->object_lock );
return VLC_ENOMEM;
}
psz_head[ i_header_size-1 ] = '\0';
p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp);
/* Build the SAP Headers */
uint8_t *psz_head = p_sap_session->psz_data;
p_sap_session->psz_data = (uint8_t *)malloc( sizeof(char)*
p_sap_session->i_length );
/* SAPv1, not encrypted, not compressed */
psz_head[0] = 0x20;
psz_head[1] = 0x00; /* No authentification length */
/* Build the final message */
memcpy( p_sap_session->psz_data, psz_head, i_header_size );
memcpy( p_sap_session->psz_data+i_header_size, p_sap_session->psz_sdp,
strlen( p_sap_session->psz_sdp) );
i_hash = mdate();
psz_head[2] = i_hash >> 8; /* Msg id hash */
psz_head[3] = i_hash; /* Msg id hash 2 */
headsize = 4;
switch (p_session->orig.ss_family)
{
#ifdef AF_INET6
case AF_INET6:
{
struct in6_addr *a6 =
&((struct sockaddr_in6 *)&p_session->orig)->sin6_addr;
memcpy (psz_head + headsize, a6, 16);
psz_head[0] |= 0x10; /* IPv6 flag */
headsize += 16;
break;
}
#endif
case AF_INET:
{
uint32_t ipv4 =
(((struct sockaddr_in *)&p_session->orig)->sin_addr.s_addr);
memcpy (psz_head + headsize, &ipv4, 4);
headsize += 4;
break;
}
free( psz_head );
}
memcpy (psz_head + headsize, "application/sdp", 16);
headsize += 16;
/* Build the final message */
strcpy( (char *)psz_head + headsize, p_session->psz_sdp);
/* Enqueue the announce */
INSERT_ELEM( p_sap->pp_sessions,
......@@ -547,7 +537,6 @@ static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
p_sap->i_sessions,
i );
free( p_mysession->psz_sdp );
free( p_mysession->psz_data );
free( p_mysession );
break;
......@@ -607,65 +596,43 @@ static char *SDPGenerate( sap_handler_t *p_sap,
const session_descriptor_t *p_session,
const sap_address_t *p_addr, vlc_bool_t b_ssm )
{
int64_t i_sdp_id = mdate();
int i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri,
*psz_sdp;
char ipv;
char *psz_group, *psz_name, *psz_sdp;
char *head = StartSDP (p_session->psz_name, p_session->description,
p_session->url, p_session->email, p_session->phone,
(const struct sockaddr *)&p_session->orig, p_session->origlen,
(const struct sockaddr *)&p_session->addr, p_session->addrlen);
if (head == NULL)
return NULL;
psz_group = p_session->psz_group;
psz_name = p_session->psz_name;
/* FIXME: really check that psz_uri is a real IP address
* FIXME: make a common function to obtain a canonical IP address */
ipv = ( strchr( p_session->psz_uri, ':' ) != NULL) ? '6' : '4';
if( *p_session->psz_uri == '[' )
{
char *ptr;
strlcpy( psz_uribuf, p_session->psz_uri + 1, sizeof( psz_uribuf ) );
ptr = strchr( psz_uribuf, '%' );
if( ptr != NULL)
*ptr = '\0';
ptr = strchr( psz_uribuf, ']' );
if( ptr != NULL)
*ptr = '\0';
psz_uri = psz_uribuf;
}
else
psz_uri = p_session->psz_uri;
char *sfilter = NULL;
if (b_ssm)
{
if (asprintf (&sfilter, "a=source-filter: incl IN IP%c * %s\r\n",
ipv, p_addr->psz_machine) == -1)
return NULL;
}
int res = asprintf (&psz_sdp,
"v=0\r\n"
"o=- "I64Fd" %d IN IP%c %s\r\n"
"s=%s\r\n"
"c=IN IP%c %s/255\r\n"
"t=0 0\r\n"
"a=tool:"PACKAGE_STRING"\r\n"
"a=recvonly\r\n"