Commit ef215ef4 authored by Rafaël Carré's avatar Rafaël Carré

stream_out_std: simplify / factorize

split out some functions from Open()
move static functions
sout_stream_id_t -> define struct as empty and use the pointer to store sout_input_t

functional change:
do not force asfh mux if user specified another one but only warn him (like for upd/ts)
parent 52957d05
/***************************************************************************** /*****************************************************************************
* standard.c: standard stream output module * standard.c: standard stream output module
***************************************************************************** *****************************************************************************
* Copyright (C) 2003-2007 the VideoLAN team * Copyright (C) 2003-2011 the VideoLAN team
* $Id$ * $Id$
* *
* Authors: Laurent Aimar <fenrir@via.ecp.fr> * Authors: Laurent Aimar <fenrir@via.ecp.fr>
...@@ -100,31 +100,19 @@ vlc_module_begin () ...@@ -100,31 +100,19 @@ vlc_module_begin ()
set_category( CAT_SOUT ) set_category( CAT_SOUT )
set_subcategory( SUBCAT_SOUT_STREAM ) set_subcategory( SUBCAT_SOUT_STREAM )
add_string( SOUT_CFG_PREFIX "access", "", ACCESS_TEXT, add_string( SOUT_CFG_PREFIX "access", "", ACCESS_TEXT, ACCESS_LONGTEXT, false )
ACCESS_LONGTEXT, false ) add_string( SOUT_CFG_PREFIX "mux", "", MUX_TEXT, MUX_LONGTEXT, false )
add_string( SOUT_CFG_PREFIX "mux", "", MUX_TEXT, add_string( SOUT_CFG_PREFIX "dst", "", DEST_TEXT, DEST_LONGTEXT, false )
MUX_LONGTEXT, false ) add_string( SOUT_CFG_PREFIX "bind", "", BIND_TEXT, BIND_LONGTEXT, false )
add_string( SOUT_CFG_PREFIX "dst", "", DEST_TEXT, add_string( SOUT_CFG_PREFIX "path", "", PATH_TEXT, PATH_LONGTEXT, false )
DEST_LONGTEXT, false ) add_bool( SOUT_CFG_PREFIX "sap", false, SAP_TEXT, SAP_LONGTEXT, true )
add_string( SOUT_CFG_PREFIX "bind", "", BIND_TEXT, add_string( SOUT_CFG_PREFIX "name", "", NAME_TEXT, NAME_LONGTEXT, true )
BIND_LONGTEXT, false ) add_string( SOUT_CFG_PREFIX "group", "", GROUP_TEXT, GROUP_LONGTEXT, true )
add_string( SOUT_CFG_PREFIX "path", "", PATH_TEXT, add_string( SOUT_CFG_PREFIX "description", "", DESC_TEXT, DESC_LONGTEXT, true )
PATH_LONGTEXT, false ) add_string( SOUT_CFG_PREFIX "url", "", URL_TEXT, URL_LONGTEXT, true )
add_string( SOUT_CFG_PREFIX "email", "", EMAIL_TEXT, EMAIL_LONGTEXT, true )
add_bool( SOUT_CFG_PREFIX "sap", false, SAP_TEXT, SAP_LONGTEXT, add_string( SOUT_CFG_PREFIX "phone", "", PHONE_TEXT, PHONE_LONGTEXT, true )
true )
add_string( SOUT_CFG_PREFIX "name", "", NAME_TEXT, NAME_LONGTEXT,
true )
add_string( SOUT_CFG_PREFIX "group", "", GROUP_TEXT, GROUP_LONGTEXT,
true )
add_string( SOUT_CFG_PREFIX "description", "", DESC_TEXT, DESC_LONGTEXT,
true )
add_string( SOUT_CFG_PREFIX "url", "", URL_TEXT, URL_LONGTEXT,
true )
add_string( SOUT_CFG_PREFIX "email", "", EMAIL_TEXT, EMAIL_LONGTEXT,
true )
add_string( SOUT_CFG_PREFIX "phone", "", PHONE_TEXT, PHONE_LONGTEXT,
true )
set_callbacks( Open, Close ) set_callbacks( Open, Close )
vlc_module_end () vlc_module_end ()
...@@ -140,100 +128,90 @@ static const char *const ppsz_sout_options[] = { ...@@ -140,100 +128,90 @@ static const char *const ppsz_sout_options[] = {
#define DEFAULT_PORT 1234 #define DEFAULT_PORT 1234
static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
static int Del ( sout_stream_t *, sout_stream_id_t * );
static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
struct sout_stream_sys_t struct sout_stream_sys_t
{ {
sout_mux_t *p_mux; sout_mux_t *p_mux;
session_descriptor_t *p_session; session_descriptor_t *p_session;
}; };
/***************************************************************************** struct sout_stream_id_t
* Open:
*****************************************************************************/
static int Open( vlc_object_t *p_this )
{ {
sout_stream_t *p_stream = (sout_stream_t*)p_this; };
sout_instance_t *p_sout = p_stream->p_sout;
sout_stream_sys_t *p_sys;
char *psz_mux;
char *psz_access;
char *psz_url=NULL;
char *psz_bind;
char *psz_path;
vlc_value_t val; static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
{
return (sout_stream_id_t*)sout_MuxAddStream( p_stream->p_sys->p_mux, p_fmt );
}
sout_access_out_t *p_access; static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
sout_mux_t *p_mux; {
sout_MuxDeleteStream( p_stream->p_sys->p_mux, (sout_input_t*)id );
return VLC_SUCCESS;
}
const char *psz_mux_byext = NULL; static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
block_t *p_buffer )
{
sout_MuxSendBuffer( p_stream->p_sys->p_mux, (sout_input_t*)id, p_buffer );
return VLC_SUCCESS;
}
static void create_SDP(sout_stream_t *p_stream, sout_access_out_t *p_access)
{
sout_stream_sys_t *p_sys = p_stream->p_sys;
config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, static const struct addrinfo hints = {
p_stream->p_cfg ); .ai_family = AF_UNSPEC,
.ai_socktype = SOCK_DGRAM,
.ai_protocol = 0,
.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV
};
char *shost = var_GetNonEmptyString (p_access, "src-addr");
char *dhost = var_GetNonEmptyString (p_access, "dst-addr");
int sport = var_GetInteger (p_access, "src-port");
int dport = var_GetInteger (p_access, "dst-port");
struct sockaddr_storage src, dst;
socklen_t srclen = 0, dstlen = 0;
struct addrinfo *res;
psz_access = var_GetString( p_stream, SOUT_CFG_PREFIX "access" ); if (!vlc_getaddrinfo ( VLC_OBJECT(p_stream), dhost, dport, &hints, &res))
if( EMPTY_STR(psz_access) )
{
if( !strcmp( p_stream->psz_name, "http" ) )
{ {
psz_access = strdup("http"); memcpy (&dst, res->ai_addr, dstlen = res->ai_addrlen);
} freeaddrinfo (res);
else if (!strcmp (p_stream->psz_name, "udp"))
{
psz_access = strdup("udp");
} }
else if (!strcmp (p_stream->psz_name, "file"))
if (!vlc_getaddrinfo ( VLC_OBJECT(p_stream), shost, sport, &hints, &res))
{ {
psz_access = strdup("file"); memcpy (&src, res->ai_addr, srclen = res->ai_addrlen);
} freeaddrinfo (res);
} }
psz_mux = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" ); char *head = vlc_sdp_Start (VLC_OBJECT (p_stream), SOUT_CFG_PREFIX,
psz_bind = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "bind" ); (struct sockaddr *)&src, srclen,
psz_path = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "path" ); (struct sockaddr *)&dst, dstlen);
free (shost);
if( psz_bind && psz_path ) if (head != NULL)
{
if( asprintf( &psz_url, "%s/%s", psz_bind, psz_path ) == -1 )
psz_url = NULL;
}
else if( psz_bind )
{ {
psz_url = psz_bind; char *psz_sdp = NULL;
psz_bind = NULL; if (asprintf (&psz_sdp, "%s"
} "m=video %d udp mpeg\r\n", head, dport) == -1)
free( psz_path ); psz_sdp = NULL;
free (head);
var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val ); /* Register the SDP with the SAP thread */
if( *val.psz_string ) if (psz_sdp)
{ {
free( psz_url); msg_Dbg (p_stream, "Generated SDP:\n%s", psz_sdp);
psz_url = val.psz_string; p_sys->p_session =
sout_AnnounceRegisterSDP (p_stream->p_sout, psz_sdp, dhost);
free( psz_sdp );
} }
else
free( val.psz_string );
p_sys = p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
if( !p_sys )
{
free( psz_access );
free( psz_mux );
free( psz_bind );
free( psz_url );
return VLC_ENOMEM;
} }
p_stream->p_sys->p_session = NULL; free (dhost);
}
msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
/* ext -> muxer name */ static const char *getMuxFromExt( const char *psz_url )
if( psz_url && strrchr( psz_url, '.' ) ) {
{
/* by extension */
static struct { const char ext[6]; const char mux[32]; } exttomux[] = static struct { const char ext[6]; const char mux[32]; } exttomux[] =
{ {
{ "avi", "avi" }, { "avi", "avi" },
...@@ -258,228 +236,192 @@ static int Open( vlc_object_t *p_this ) ...@@ -258,228 +236,192 @@ static int Open( vlc_object_t *p_this )
{ "flv", "ffmpeg{mux=flv}" }, { "flv", "ffmpeg{mux=flv}" },
{ "mkv", "ffmpeg{mux=matroska}"}, { "mkv", "ffmpeg{mux=matroska}"},
{ "webm", "ffmpeg{mux=webm}"}, { "webm", "ffmpeg{mux=webm}"},
{ "", "" }
}; };
const char *psz_ext = strrchr( psz_url, '.' ) + 1;
msg_Dbg( p_this, "extension is %s", psz_ext ); if( !psz_url )
for( int i = 0; exttomux[i].ext[0]; i++ ) return NULL;
{ const char *psz_ext = strrchr( psz_url, '.' );
if( !psz_ext )
return NULL;
psz_ext++;
for( size_t i = 0; i < sizeof exttomux / sizeof *exttomux; i++ )
if( !strcasecmp( psz_ext, exttomux[i].ext ) ) if( !strcasecmp( psz_ext, exttomux[i].ext ) )
{ return exttomux[i].mux;
psz_mux_byext = exttomux[i].mux;
break;
}
}
msg_Dbg( p_this, "extension -> mux=%s", psz_mux_byext );
}
/* We fix access/mux to valid couple */ return NULL;
}
if( !psz_access && !psz_mux ) static int fixAccessMux( sout_stream_t *p_stream, char **ppsz_mux,
char **ppsz_access, const char *psz_url )
{
char *psz_mux = *ppsz_mux;
char *psz_access = *ppsz_access;
if( !psz_mux )
{
const char *psz_mux_byext = getMuxFromExt( psz_url );
if( !psz_access )
{ {
if( psz_mux_byext ) if( !psz_mux_byext )
{ {
msg_Err( p_stream, "no access _and_ no muxer" );
return 1;
}
msg_Warn( p_stream, msg_Warn( p_stream,
"no access _and_ no muxer, extension gives file/%s", "no access _and_ no muxer, extension gives file/%s",
psz_mux_byext ); psz_mux_byext );
psz_access = strdup("file"); *ppsz_access = strdup("file");
psz_mux = strdup(psz_mux_byext); *ppsz_mux = strdup(psz_mux_byext);
} }
else else
{ {
msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
free( psz_bind );
free( psz_url );
free( p_sys );
return VLC_EGENERIC;
}
}
if( psz_access && !psz_mux )
{
/* access given, no mux */
if( !strncmp( psz_access, "mmsh", 4 ) ) if( !strncmp( psz_access, "mmsh", 4 ) )
{ *ppsz_mux = strdup("asfh");
psz_mux = strdup("asfh");
}
else if (!strcmp (psz_access, "udp")) else if (!strcmp (psz_access, "udp"))
{ *ppsz_mux = strdup("ts");
psz_mux = strdup("ts");
}
else if( psz_mux_byext ) else if( psz_mux_byext )
{ *ppsz_mux = strdup(psz_mux_byext);
psz_mux = strdup(psz_mux_byext);
}
else else
{ {
msg_Err( p_stream, "no mux specified or found by extension" ); msg_Err( p_stream, "no mux specified or found by extension" );
free( psz_access ); return 1;
free( psz_bind );
free( psz_url );
free( p_sys );
return VLC_EGENERIC;
} }
} }
else if( psz_mux && !psz_access )
{
/* mux given, no access */
if( !strncmp( psz_mux, "asfh", 4 ) )
{
psz_access = strdup("mmsh");
} }
else else if( !psz_access )
{ {
/* default file */ if( !strncmp( psz_mux, "asfh", 4 ) )
psz_access = strdup("file"); *ppsz_access = strdup("mmsh");
} else /* default file */
*ppsz_access = strdup("file");
} }
return 0;
}
/* fix or warn of incompatible couple */ static void checkAccessMux( sout_stream_t *p_stream, char *psz_access,
if( !strncmp( psz_access, "mmsh", 4 ) && char *psz_mux )
strncmp( psz_mux, "asfh", 4 ) ) {
{ if( !strncmp( psz_access, "mmsh", 4 ) && strncmp( psz_mux, "asfh", 4 ) )
char *p = strchr( psz_mux,'{' ); msg_Err( p_stream, "mmsh output is only valid with asfh mux" );
else if( strncmp( psz_access, "file", 4 ) &&
msg_Warn( p_stream, "fixing to mmsh/asfh" ); ( !strncmp( psz_mux, "mov", 3 ) || !strncmp( psz_mux, "mp4", 3 ) ) )
if( p ) msg_Err( p_stream, "mov and mp4 mux are only valid with file output" );
{
if( asprintf( &p, "asfh%s", p ) == -1 )
p = NULL;
free( psz_mux );
psz_mux = p;
}
else
{
free( psz_mux );
psz_mux = strdup("asfh");
}
}
else if( !strncmp( psz_access, "udp", 3 ) ) else if( !strncmp( psz_access, "udp", 3 ) )
{ {
if( !strncmp( psz_mux, "ffmpeg", 6 ) ) if( !strncmp( psz_mux, "ffmpeg", 6 ) )
{ /* why would you use ffmpeg's ts muxer ? YOU DON'T LOVE VLC ??? */ { /* why would you use ffmpeg's ts muxer ? YOU DON'T LOVE VLC ??? */
char *psz_ffmpeg_mux = var_CreateGetString( p_this, "ffmpeg-mux" ); char *psz_ffmpeg_mux = var_CreateGetString( p_stream, "ffmpeg-mux" );
if( !psz_ffmpeg_mux || strncmp( psz_ffmpeg_mux, "mpegts", 6 ) ) if( !psz_ffmpeg_mux || strncmp( psz_ffmpeg_mux, "mpegts", 6 ) )
msg_Err( p_stream, "UDP is only valid with TS" ); msg_Err( p_stream, "UDP output is only valid with TS mux" );
free( psz_ffmpeg_mux ); free( psz_ffmpeg_mux );
} }
else if( strncmp( psz_mux, "ts", 2 ) ) else if( strncmp( psz_mux, "ts", 2 ) )
msg_Err( p_stream, "UDP output is only valid with TS mux" );
}
}
/*****************************************************************************
* Open:
*****************************************************************************/
static int Open( vlc_object_t *p_this )
{
sout_stream_t *p_stream = (sout_stream_t*)p_this;
sout_instance_t *p_sout = p_stream->p_sout;
sout_stream_sys_t *p_sys;
char *psz_mux, *psz_access, *psz_url;
sout_access_out_t *p_access;
int ret = VLC_EGENERIC;
config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
p_stream->p_cfg );
psz_mux = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
psz_access = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "access" );
if( !psz_access )
{ {
msg_Err( p_stream, "UDP is only valid with TS" ); if( !strcmp( p_stream->psz_name, "http" ) )
psz_access = strdup("http");
else if (!strcmp (p_stream->psz_name, "udp"))
psz_access = strdup("udp");
else if (!strcmp (p_stream->psz_name, "file"))
psz_access = strdup("file");
} }
psz_url = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "dst" );
if (!psz_url)
{
char *psz_bind = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "bind" );
if( psz_bind )
{
char *psz_path = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "path" );
if( psz_path )
{
if( asprintf( &psz_url, "%s/%s", psz_bind, psz_path ) == -1 )
psz_url = NULL;
free(psz_bind);
free( psz_path );
} }
else if( strncmp( psz_access, "file", 4 ) && else
( !strncmp( psz_mux, "mov", 3 ) || psz_url = psz_bind;
!strncmp( psz_mux, "mp4", 3 ) ) ) }
}
p_sys = p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
if( !p_sys )
{ {
msg_Err( p_stream, "mov and mp4 work only with file output" ); ret = VLC_ENOMEM;
goto end;
} }
p_sys->p_session = NULL;
msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url ); if( fixAccessMux( p_stream, &psz_mux, &psz_access, psz_url ) )
goto end;
checkAccessMux( p_stream, psz_access, psz_mux );
/* *** find and open appropriate access module *** */
p_access = sout_AccessOutNew( p_sout, psz_access, psz_url ); p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
if( p_access == NULL ) if( p_access == NULL )
{ {
msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'", msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
psz_access, psz_mux, psz_url ); psz_access, psz_mux, psz_url );
free( psz_access ); goto end;
free( psz_mux );
free( psz_bind );
free( psz_url );
free( p_sys );
return VLC_EGENERIC;
} }
msg_Dbg( p_stream, "access opened" );
/* *** find and open appropriate mux module *** */ p_sys->p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
p_mux = sout_MuxNew( p_sout, psz_mux, p_access ); if( !p_sys->p_mux )
if( p_mux == NULL )
{ {
msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'", msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
psz_access, psz_mux, psz_url ); psz_access, psz_mux, psz_url );
sout_AccessOutDelete( p_access ); sout_AccessOutDelete( p_access );
free( psz_access ); goto end;
free( psz_mux );
free( psz_bind );
free( psz_url );
free( p_sys );
return VLC_EGENERIC;
} }
msg_Dbg( p_stream, "mux opened" );
/* *** Create the SAP Session structure *** */
if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) ) if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
{ create_SDP( p_stream, p_access );
/* Create the SDP */
static const struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_DGRAM,
.ai_protocol = 0,
.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV
};
char *shost = var_GetNonEmptyString (p_access, "src-addr");
char *dhost = var_GetNonEmptyString (p_access, "dst-addr");
int sport = var_GetInteger (p_access, "src-port");
int dport = var_GetInteger (p_access, "dst-port");
struct sockaddr_storage src, dst;
socklen_t srclen = 0, dstlen = 0;
struct addrinfo *res;
if ( vlc_getaddrinfo ( VLC_OBJECT(p_stream), dhost, dport, &hints, &res) == 0)
{
memcpy (&dst, res->ai_addr, dstlen = res->ai_addrlen);
freeaddrinfo (res);
}
if (vlc_getaddrinfo ( VLC_OBJECT(p_stream), shost, sport, &hints, &res) == 0) if( !sout_AccessOutCanControlPace( p_access ) )
{ p_sout->i_out_pace_nocontrol++;
memcpy (&src, res->ai_addr, srclen = res->ai_addrlen);
freeaddrinfo (res);
}
char *head = vlc_sdp_Start (VLC_OBJECT (p_stream), SOUT_CFG_PREFIX,
(struct sockaddr *)&src, srclen,
(struct sockaddr *)&dst, dstlen);
free (shost);
char *psz_sdp = NULL;
if (head != NULL)
{
if (asprintf (&psz_sdp, "%s"
"m=video %d udp mpeg\r\n", head, dport) == -1)
psz_sdp = NULL;
free (head);
}
/* Register the SDP with the SAP thread */
if (psz_sdp != NULL)
{
msg_Dbg (p_stream, "Generated SDP:\n%s", psz_sdp);
p_sys->p_session =
sout_AnnounceRegisterSDP (p_sout, psz_sdp, dhost);
free( psz_sdp );
}
free (dhost);
}
p_stream->pf_add = Add; p_stream->pf_add = Add;
p_stream->pf_del = Del; p_stream->pf_del = Del;
p_stream->pf_send = Send; p_stream->pf_send = Send;
p_sys->p_mux = p_mux; ret = VLC_SUCCESS;
msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
end:
if( ret != VLC_SUCCESS )
free( p_sys );
free( psz_access ); free( psz_access );
free( psz_mux ); free( psz_mux );
free( psz_bind );
free( psz_url ); free( psz_url );
if( !sout_AccessOutCanControlPace( p_access ) ) return ret;
p_sout->i_out_pace_nocontrol++;
return VLC_SUCCESS;
} }
/***************************************************************************** /*****************************************************************************
...@@ -501,48 +443,3 @@ static void Close( vlc_object_t * p_this ) ...@@ -501,48 +443,3 @@ static void Close( vlc_object_t * p_this )
free( p_sys ); free( p_sys );
} }
struct sout_stream_id_t
{
sout_input_t *p_input;
};
static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
{
sout_stream_sys_t *p_sys = p_stream->p_sys;
sout_stream_id_t *id;
id = malloc( sizeof( sout_stream_id_t ) );
if( !id )
return NULL;
if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
{
free( id );
return NULL;
}
return id;
}
static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
{
sout_stream_sys_t *p_sys = p_stream->p_sys;
sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
free( id );
return VLC_SUCCESS;
}
static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
block_t *p_buffer )
{
sout_stream_sys_t *p_sys = p_stream->p_sys;
sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );
return VLC_SUCCESS;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment