1 /*****************************************************************************
2 * standard.c: standard stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
37 /*****************************************************************************
39 *****************************************************************************/
40 #define ACCESS_TEXT N_("Output access method")
41 #define ACCESS_LONGTEXT N_( \
42 "Allows you to pecify the output access method used for the streaming " \
44 #define MUX_TEXT N_("Output muxer")
45 #define MUX_LONGTEXT N_( \
46 "Allows you to pecify the output muxer method used for the streaming " \
48 #define URL_TEXT N_("Output URL")
49 #define URL_LONGTEXT N_( \
50 "Allows you to pecify the output URL used for the streaming output." )
52 static int Open ( vlc_object_t * );
53 static void Close ( vlc_object_t * );
55 #define SOUT_CFG_PREFIX "sout-standard-"
58 set_description( _("Standard stream output") );
59 set_capability( "sout stream", 50 );
60 add_shortcut( "standard" );
61 add_shortcut( "std" );
63 add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
64 ACCESS_LONGTEXT, VLC_FALSE );
65 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
66 MUX_LONGTEXT, VLC_FALSE );
67 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
68 URL_LONGTEXT, VLC_FALSE );
70 add_bool( SOUT_CFG_PREFIX "sap", 0, NULL, "sap", "", VLC_TRUE );
71 add_string( SOUT_CFG_PREFIX "sap-name", "", NULL, "sap name", "", VLC_TRUE );
72 add_bool( SOUT_CFG_PREFIX "sap-ipv6", 0, NULL, "sap ipv6", "", VLC_TRUE );
74 add_bool( SOUT_CFG_PREFIX "slp", 0, NULL, "slp", "", VLC_TRUE );
75 add_string( SOUT_CFG_PREFIX "slp-name", "", NULL, "slp name", "", VLC_TRUE );
77 set_callbacks( Open, Close );
81 /*****************************************************************************
83 *****************************************************************************/
84 static const char *ppsz_sout_options[] = {
85 "access", "mux", "url",
86 "sap", "sap-name", "sap-ipv6",
91 #define DEFAULT_IPV6_SCOPE '8'
92 #define DEFAULT_PORT 1234
94 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
95 static int Del ( sout_stream_t *, sout_stream_id_t * );
96 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
98 struct sout_stream_sys_t
101 slp_session_t *p_slp;
102 session_descriptor_t *p_session;
105 /*****************************************************************************
107 *****************************************************************************/
108 static int Open( vlc_object_t *p_this )
110 sout_stream_t *p_stream = (sout_stream_t*)p_this;
111 sout_instance_t *p_sout = p_stream->p_sout;
112 slp_session_t *p_slp = NULL;
120 sout_access_out_t *p_access;
123 char *psz_mux_byext = NULL;
125 sout_ParseCfg( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
127 var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
128 psz_access = *val.psz_string ? val.psz_string : NULL;
130 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
131 psz_mux = *val.psz_string ? val.psz_string : NULL;
133 var_Get( p_stream, SOUT_CFG_PREFIX "url", &val );
134 psz_url = *val.psz_string ? val.psz_string : NULL;
136 p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
137 p_stream->p_sys->p_session = NULL;
139 msg_Dbg( p_this, "creating `%s/%s://%s'",
140 psz_access, psz_mux, psz_url );
142 /* ext -> muxer name */
143 if( psz_url && strrchr( psz_url, '.' ) )
146 static struct { char *ext; char *mux; } exttomux[] =
165 char *psz_ext = strrchr( psz_url, '.' ) + 1;
168 msg_Dbg( p_this, "extention is %s", psz_ext );
169 for( i = 0; exttomux[i].ext != NULL; i++ )
171 if( !strcasecmp( psz_ext, exttomux[i].ext ) )
173 psz_mux_byext = exttomux[i].mux;
177 msg_Dbg( p_this, "extention -> mux=%s", psz_mux_byext );
180 /* We fix access/mux to valid couple */
182 if( psz_access == NULL && psz_mux == NULL )
187 "no access _and_ no muxer, extention gives file/%s",
190 psz_mux = psz_mux_byext;
194 msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
199 if( psz_access && psz_mux == NULL )
201 /* access given, no mux */
202 if( !strncmp( psz_access, "mmsh", 4 ) )
206 else if( !strncmp( psz_access, "udp", 3 ) )
212 psz_mux = psz_mux_byext;
215 else if( psz_mux && psz_access == NULL )
217 /* mux given, no access */
218 if( !strncmp( psz_mux, "asfh", 4 ) )
229 /* fix or warm of incompatible couple */
230 if( psz_mux && psz_access )
232 if( !strncmp( psz_access, "mmsh", 4 ) && strncmp( psz_mux, "asfh", 4 ) )
234 char *p = strchr( psz_mux,'{' );
236 msg_Warn( p_stream, "fixing to mmsh/asfh" );
239 /* -> a little memleak but ... */
240 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
241 sprintf( psz_mux, "asfh%s", p );
248 else if( ( !strncmp( psz_access, "rtp", 3 ) ||
249 !strncmp( psz_access, "udp", 3 ) ) &&
250 strncmp( psz_mux, "ts", 2 ) )
252 msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
254 else if( strncmp( psz_access, "file", 4 ) &&
255 ( !strncmp( psz_mux, "mov", 3 ) ||
256 !strncmp( psz_mux, "mp4", 3 ) ) )
258 msg_Err( p_stream, "mov and mp4 work only with file output" );
262 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
264 /* *** find and open appropriate access module *** */
265 p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
266 if( p_access == NULL )
268 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
269 psz_access, psz_mux, psz_url );
270 return( VLC_EGENERIC );
272 msg_Dbg( p_stream, "access opened" );
274 /* *** find and open appropriate mux module *** */
275 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
278 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
279 psz_access, psz_mux, psz_url );
281 sout_AccessOutDelete( p_access );
282 return( VLC_EGENERIC );
284 msg_Dbg( p_stream, "mux opened" );
286 /* *** Create the SAP Session structure *** */
287 var_Get( p_stream, SOUT_CFG_PREFIX "sap", &val );
289 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
291 session_descriptor_t *p_session = sout_AnnounceSessionCreate();
292 announce_method_t *p_method =
293 sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
296 var_Get( p_stream, SOUT_CFG_PREFIX "sap-name", &val );
297 if( *val.psz_string )
299 p_session->psz_name = val.psz_string;
303 free( val.psz_string );
304 p_session->psz_name = strdup( psz_url );
306 var_Get( p_stream, SOUT_CFG_PREFIX "sap-ipv6", &val );
307 p_method->i_ip_version = val.b_bool ? 6 : 4;
309 /* Now, parse the URL to extract host and port */
310 vlc_UrlParse( &url, psz_url , 0);
314 if( url.i_port == 0 )
316 url.i_port = DEFAULT_PORT;
319 p_session->psz_uri = url.psz_host;
320 p_session->i_port = url.i_port;
321 p_session->psz_sdp = NULL;
323 p_session->i_ttl = config_GetInt( p_sout, "ttl" );
324 p_session->i_payload = 33;
326 msg_Info( p_this, "SAP Enabled");
328 sout_AnnounceRegister( p_sout, p_session, p_method );
330 /* FIXME: Free p_method */
332 p_stream->p_sys->p_session = p_session;
334 vlc_UrlClean( &url );
337 /* *** Register with slp *** */
339 var_Get( p_stream, SOUT_CFG_PREFIX "slp", &val );
341 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
345 msg_Info( p_this, "SLP Enabled");
346 var_Get( p_stream, SOUT_CFG_PREFIX "sap-name", &val );
347 if( *val.psz_string )
349 i_ret = sout_SLPReg( p_sout, psz_url, val.psz_string );
353 i_ret = sout_SLPReg( p_sout, psz_url, psz_url );
355 free( val.psz_string );
359 msg_Warn( p_sout, "SLP Registering failed");
363 p_slp = malloc(sizeof(slp_session_t));
364 p_slp->psz_url= strdup(psz_url);
365 p_slp->psz_name = strdup(
366 p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url);
371 p_stream->pf_add = Add;
372 p_stream->pf_del = Del;
373 p_stream->pf_send = Send;
375 p_stream->p_sys->p_mux = p_mux;
376 p_stream->p_sys->p_slp = p_slp;
381 /*****************************************************************************
383 *****************************************************************************/
384 static void Close( vlc_object_t * p_this )
386 sout_stream_t *p_stream = (sout_stream_t*)p_this;
387 sout_stream_sys_t *p_sys = p_stream->p_sys;
388 sout_access_out_t *p_access = p_sys->p_mux->p_access;
390 if( p_sys->p_session != NULL )
392 sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
398 sout_SLPDereg( (sout_instance_t *)p_this,
399 p_sys->p_slp->psz_url,
400 p_sys->p_slp->psz_name);
406 sout_MuxDelete( p_sys->p_mux );
407 sout_AccessOutDelete( p_access );
412 struct sout_stream_id_t
414 sout_input_t *p_input;
418 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
420 sout_stream_sys_t *p_sys = p_stream->p_sys;
421 sout_stream_id_t *id;
423 id = malloc( sizeof( sout_stream_id_t ) );
424 if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
434 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
436 sout_stream_sys_t *p_sys = p_stream->p_sys;
438 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
445 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
448 sout_stream_sys_t *p_sys = p_stream->p_sys;
450 sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );