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 static int Open ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
43 #define SOUT_CFG_PREFIX "sout-standard-"
46 set_description( _("Standard stream output") );
47 set_capability( "sout stream", 50 );
48 add_shortcut( "standard" );
49 add_shortcut( "std" );
51 add_string( SOUT_CFG_PREFIX "access", "", NULL, "access", "", VLC_TRUE );
52 add_string( SOUT_CFG_PREFIX "mux", "", NULL, "mux", "", VLC_TRUE );
53 add_string( SOUT_CFG_PREFIX "url", "", NULL, "url", "", VLC_TRUE );
55 add_bool( SOUT_CFG_PREFIX "sap", 0, NULL, "sap", "", VLC_TRUE );
56 add_string( SOUT_CFG_PREFIX "sap-name", "", NULL, "sap name", "", VLC_TRUE );
57 add_bool( SOUT_CFG_PREFIX "sap-ipv6", 0, NULL, "sap ipv6", "", VLC_TRUE );
59 add_bool( SOUT_CFG_PREFIX "slp", 0, NULL, "slp", "", VLC_TRUE );
60 add_string( SOUT_CFG_PREFIX "slp-name", "", NULL, "slp name", "", VLC_TRUE );
62 set_callbacks( Open, Close );
66 /*****************************************************************************
68 *****************************************************************************/
69 static const char *ppsz_sout_options[] = {
70 "access", "mux", "url",
71 "sap", "sap-name", "sap-ipv6",
76 #define DEFAULT_IPV6_SCOPE '8'
77 #define DEFAULT_PORT 1234
79 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
80 static int Del ( sout_stream_t *, sout_stream_id_t * );
81 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
83 struct sout_stream_sys_t
87 session_descriptor_t *p_session;
90 /*****************************************************************************
92 *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
95 sout_stream_t *p_stream = (sout_stream_t*)p_this;
96 sout_instance_t *p_sout = p_stream->p_sout;
97 slp_session_t *p_slp = NULL;
105 sout_access_out_t *p_access;
108 char *psz_mux_byext = NULL;
110 sout_ParseCfg( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
112 var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
113 psz_access = *val.psz_string ? val.psz_string : NULL;
115 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
116 psz_mux = *val.psz_string ? val.psz_string : NULL;
118 var_Get( p_stream, SOUT_CFG_PREFIX "url", &val );
119 psz_url = *val.psz_string ? val.psz_string : NULL;
121 p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
122 p_stream->p_sys->p_session = NULL;
124 msg_Dbg( p_this, "creating `%s/%s://%s'",
125 psz_access, psz_mux, psz_url );
127 /* ext -> muxer name */
128 if( psz_url && strrchr( psz_url, '.' ) )
131 static struct { char *ext; char *mux; } exttomux[] =
150 char *psz_ext = strrchr( psz_url, '.' ) + 1;
153 msg_Dbg( p_this, "extention is %s", psz_ext );
154 for( i = 0; exttomux[i].ext != NULL; i++ )
156 if( !strcasecmp( psz_ext, exttomux[i].ext ) )
158 psz_mux_byext = exttomux[i].mux;
162 msg_Dbg( p_this, "extention -> mux=%s", psz_mux_byext );
165 /* We fix access/mux to valid couple */
167 if( psz_access == NULL && psz_mux == NULL )
172 "no access _and_ no muxer, extention gives file/%s",
175 psz_mux = psz_mux_byext;
179 msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
184 if( psz_access && psz_mux == NULL )
186 /* access given, no mux */
187 if( !strncmp( psz_access, "mmsh", 4 ) )
191 else if( !strncmp( psz_access, "udp", 3 ) )
197 psz_mux = psz_mux_byext;
200 else if( psz_mux && psz_access == NULL )
202 /* mux given, no access */
203 if( !strncmp( psz_mux, "asfh", 4 ) )
214 /* fix or warm of incompatible couple */
215 if( psz_mux && psz_access )
217 if( !strncmp( psz_access, "mmsh", 4 ) && strncmp( psz_mux, "asfh", 4 ) )
219 char *p = strchr( psz_mux,'{' );
221 msg_Warn( p_stream, "fixing to mmsh/asfh" );
224 /* -> a little memleak but ... */
225 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
226 sprintf( psz_mux, "asfh%s", p );
233 else if( ( !strncmp( psz_access, "rtp", 3 ) ||
234 !strncmp( psz_access, "udp", 3 ) ) &&
235 strncmp( psz_mux, "ts", 2 ) )
237 msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
239 else if( strncmp( psz_access, "file", 4 ) &&
240 ( !strncmp( psz_mux, "mov", 3 ) ||
241 !strncmp( psz_mux, "mp4", 3 ) ) )
243 msg_Err( p_stream, "mov and mp4 work only with file output" );
247 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
249 /* *** find and open appropriate access module *** */
250 p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
251 if( p_access == NULL )
253 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
254 psz_access, psz_mux, psz_url );
255 return( VLC_EGENERIC );
257 msg_Dbg( p_stream, "access opened" );
259 /* *** find and open appropriate mux module *** */
260 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
263 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
264 psz_access, psz_mux, psz_url );
266 sout_AccessOutDelete( p_access );
267 return( VLC_EGENERIC );
269 msg_Dbg( p_stream, "mux opened" );
271 /* *** Create the SAP Session structure *** */
272 var_Get( p_stream, SOUT_CFG_PREFIX "sap", &val );
274 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
276 session_descriptor_t *p_session = sout_AnnounceSessionCreate();
277 announce_method_t *p_method =
278 sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
281 var_Get( p_stream, SOUT_CFG_PREFIX "sap-name", &val );
282 if( *val.psz_string )
284 p_session->psz_name = val.psz_string;
288 free( val.psz_string );
289 p_session->psz_name = strdup( psz_url );
291 var_Get( p_stream, SOUT_CFG_PREFIX "sap-ipv6", &val );
292 p_method->i_ip_version = val.b_bool ? 6 : 4;
294 /* Now, parse the URL to extract host and port */
295 vlc_UrlParse( &url, psz_url , 0);
299 if( url.i_port == 0 )
301 url.i_port = DEFAULT_PORT;
304 p_session->psz_uri = url.psz_host;
305 p_session->i_port = url.i_port;
306 p_session->psz_sdp = NULL;
308 p_session->i_ttl = config_GetInt( p_sout, "ttl" );
309 p_session->i_payload = 33;
311 msg_Info( p_this, "SAP Enabled");
313 sout_AnnounceRegister( p_sout, p_session, p_method );
315 /* FIXME: Free p_method */
317 p_stream->p_sys->p_session = p_session;
319 vlc_UrlClean( &url );
322 /* *** Register with slp *** */
324 var_Get( p_stream, SOUT_CFG_PREFIX "slp", &val );
326 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
330 msg_Info( p_this, "SLP Enabled");
331 var_Get( p_stream, SOUT_CFG_PREFIX "sap-name", &val );
332 if( *val.psz_string )
334 i_ret = sout_SLPReg( p_sout, psz_url, val.psz_string );
338 i_ret = sout_SLPReg( p_sout, psz_url, psz_url );
340 free( val.psz_string );
344 msg_Warn( p_sout, "SLP Registering failed");
348 p_slp = malloc(sizeof(slp_session_t));
349 p_slp->psz_url= strdup(psz_url);
350 p_slp->psz_name = strdup(
351 p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url);
356 p_stream->pf_add = Add;
357 p_stream->pf_del = Del;
358 p_stream->pf_send = Send;
360 p_stream->p_sys->p_mux = p_mux;
361 p_stream->p_sys->p_slp = p_slp;
366 /*****************************************************************************
368 *****************************************************************************/
369 static void Close( vlc_object_t * p_this )
371 sout_stream_t *p_stream = (sout_stream_t*)p_this;
372 sout_stream_sys_t *p_sys = p_stream->p_sys;
373 sout_access_out_t *p_access = p_sys->p_mux->p_access;
375 if( p_sys->p_session != NULL )
377 sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
383 sout_SLPDereg( (sout_instance_t *)p_this,
384 p_sys->p_slp->psz_url,
385 p_sys->p_slp->psz_name);
391 sout_MuxDelete( p_sys->p_mux );
392 sout_AccessOutDelete( p_access );
397 struct sout_stream_id_t
399 sout_input_t *p_input;
403 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
405 sout_stream_sys_t *p_sys = p_stream->p_sys;
406 sout_stream_id_t *id;
408 id = malloc( sizeof( sout_stream_id_t ) );
409 if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
419 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
421 sout_stream_sys_t *p_sys = p_stream->p_sys;
423 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
430 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
433 sout_stream_sys_t *p_sys = p_stream->p_sys;
435 sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );