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 *****************************************************************************/
40 /*****************************************************************************
42 *****************************************************************************/
43 #define ACCESS_TEXT N_("Output access method")
44 #define ACCESS_LONGTEXT N_( \
45 "Allows you to specify the output access method used for the streaming " \
47 #define MUX_TEXT N_("Output muxer")
48 #define MUX_LONGTEXT N_( \
49 "Allows you to specify the output muxer method used for the streaming " \
51 #define URL_TEXT N_("Output URL")
52 #define URL_LONGTEXT N_( \
53 "Allows you to specify the output URL used for the streaming output." )
55 #define NAME_TEXT N_("Session name")
56 #define NAME_LONGTEXT N_( \
57 "Name of the session that will be announced with SAP or SLP" )
59 #define GROUP_TEXT N_("Session groupname")
60 #define GROUP_LONGTEXT N_( \
61 "Name of the group that will be announced for the session" )
63 #define SAP_TEXT N_("SAP announcing")
64 #define SAP_LONGTEXT N_("Announce this session with SAP")
66 #define SAPv6_TEXT N_("SAP IPv6 announcing")
67 #define SAPv6_LONGTEXT N_("Use IPv6 to announce this session with SAP")
69 #define SLP_TEXT N_("SLP announcing")
70 #define SLP_LONGTEXT N_("Announce this session with SLP")
72 static int Open ( vlc_object_t * );
73 static void Close ( vlc_object_t * );
75 #define SOUT_CFG_PREFIX "sout-standard-"
78 set_shortname( _("Standard"));
79 set_description( _("Standard stream output") );
80 set_capability( "sout stream", 50 );
81 add_shortcut( "standard" );
82 add_shortcut( "std" );
83 set_category( CAT_SOUT );
84 set_subcategory( SUBCAT_SOUT_STREAM );
86 add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
87 ACCESS_LONGTEXT, VLC_FALSE );
88 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
89 MUX_LONGTEXT, VLC_FALSE );
90 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
91 URL_LONGTEXT, VLC_FALSE );
93 add_bool( SOUT_CFG_PREFIX "sap", 0, NULL, SAP_TEXT, SAP_LONGTEXT, VLC_TRUE );
94 add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT, NAME_LONGTEXT,
96 add_string( SOUT_CFG_PREFIX "group", "", NULL, GROUP_TEXT, GROUP_LONGTEXT,
98 add_bool( SOUT_CFG_PREFIX "sap-ipv6", 0, NULL, SAPv6_TEXT, SAPv6_LONGTEXT,
101 add_bool( SOUT_CFG_PREFIX "slp", 0, NULL, SLP_TEXT, SLP_LONGTEXT, VLC_TRUE );
103 set_callbacks( Open, Close );
107 /*****************************************************************************
108 * Exported prototypes
109 *****************************************************************************/
110 static const char *ppsz_sout_options[] = {
111 "access", "mux", "url",
112 "sap", "name", "sap-ipv6", "group",
116 #define DEFAULT_IPV6_SCOPE '8'
117 #define DEFAULT_PORT 1234
119 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
120 static int Del ( sout_stream_t *, sout_stream_id_t * );
121 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
123 struct sout_stream_sys_t
126 slp_session_t *p_slp;
127 session_descriptor_t *p_session;
130 /*****************************************************************************
132 *****************************************************************************/
133 static int Open( vlc_object_t *p_this )
135 sout_stream_t *p_stream = (sout_stream_t*)p_this;
136 sout_instance_t *p_sout = p_stream->p_sout;
137 slp_session_t *p_slp = NULL;
145 sout_access_out_t *p_access;
148 char *psz_mux_byext = NULL;
150 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
153 var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
154 psz_access = *val.psz_string ? val.psz_string : NULL;
155 if( val.psz_string && !*val.psz_string ) free( val.psz_string );
157 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
158 psz_mux = *val.psz_string ? val.psz_string : NULL;
159 if( val.psz_string && !*val.psz_string ) free( val.psz_string );
161 var_Get( p_stream, SOUT_CFG_PREFIX "url", &val );
162 psz_url = *val.psz_string ? val.psz_string : NULL;
163 if( val.psz_string && !*val.psz_string ) free( val.psz_string );
165 p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
166 p_stream->p_sys->p_session = NULL;
168 msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
170 /* ext -> muxer name */
171 if( psz_url && strrchr( psz_url, '.' ) )
174 static struct { char *ext; char *mux; } exttomux[] =
193 char *psz_ext = strrchr( psz_url, '.' ) + 1;
196 msg_Dbg( p_this, "extention is %s", psz_ext );
197 for( i = 0; exttomux[i].ext != NULL; i++ )
199 if( !strcasecmp( psz_ext, exttomux[i].ext ) )
201 psz_mux_byext = exttomux[i].mux;
205 msg_Dbg( p_this, "extention -> mux=%s", psz_mux_byext );
208 /* We fix access/mux to valid couple */
210 if( !psz_access && !psz_mux )
215 "no access _and_ no muxer, extention gives file/%s",
217 psz_access = strdup("file");
218 psz_mux = strdup(psz_mux_byext);
222 msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
227 if( psz_access && !psz_mux )
229 /* access given, no mux */
230 if( !strncmp( psz_access, "mmsh", 4 ) )
232 psz_mux = strdup("asfh");
234 else if( !strncmp( psz_access, "udp", 3 ) )
236 psz_mux = strdup("ts");
238 else if( psz_mux_byext )
240 psz_mux = strdup(psz_mux_byext);
244 msg_Err( p_stream, "no mux specified or found by extention" );
248 else if( psz_mux && !psz_access )
250 /* mux given, no access */
251 if( !strncmp( psz_mux, "asfh", 4 ) )
253 psz_access = strdup("mmsh");
258 psz_access = strdup("file");
262 /* fix or warm of incompatible couple */
263 if( psz_mux && psz_access )
265 if( !strncmp( psz_access, "mmsh", 4 ) &&
266 strncmp( psz_mux, "asfh", 4 ) )
268 char *p = strchr( psz_mux,'{' );
270 msg_Warn( p_stream, "fixing to mmsh/asfh" );
273 /* -> a little memleak but ... */
274 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
275 sprintf( psz_mux, "asfh%s", p );
279 psz_mux = strdup("asfh");
282 else if( ( !strncmp( psz_access, "rtp", 3 ) ||
283 !strncmp( psz_access, "udp", 3 ) ) &&
284 strncmp( psz_mux, "ts", 2 ) )
286 msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
288 else if( strncmp( psz_access, "file", 4 ) &&
289 ( !strncmp( psz_mux, "mov", 3 ) ||
290 !strncmp( psz_mux, "mp4", 3 ) ) )
292 msg_Err( p_stream, "mov and mp4 work only with file output" );
296 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
298 /* *** find and open appropriate access module *** */
299 p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
300 if( p_access == NULL )
302 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
303 psz_access, psz_mux, psz_url );
304 if( psz_access ) free( psz_access );
305 if( psz_mux ) free( psz_mux );
308 msg_Dbg( p_stream, "access opened" );
310 /* *** find and open appropriate mux module *** */
311 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
314 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
315 psz_access, psz_mux, psz_url );
317 sout_AccessOutDelete( p_access );
318 if( psz_access ) free( psz_access );
319 if( psz_mux ) free( psz_mux );
322 msg_Dbg( p_stream, "mux opened" );
324 /* *** Create the SAP Session structure *** */
325 var_Get( p_stream, SOUT_CFG_PREFIX "sap", &val );
327 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
329 session_descriptor_t *p_session = sout_AnnounceSessionCreate();
330 announce_method_t *p_method =
331 sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
334 var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
335 if( *val.psz_string )
337 p_session->psz_name = strdup( val.psz_string );
341 p_session->psz_name = strdup( psz_url );
343 free( val.psz_string );
345 var_Get( p_stream, SOUT_CFG_PREFIX "group", &val );
346 if( *val.psz_string )
348 p_session->psz_group = strdup( val.psz_string );
350 free( val.psz_string );
352 var_Get( p_stream, SOUT_CFG_PREFIX "sap-ipv6", &val );
353 p_method->i_ip_version = val.b_bool ? 6 : 4;
355 /* Now, parse the URL to extract host and port */
356 vlc_UrlParse( &url, psz_url , 0);
360 if( url.i_port == 0 ) url.i_port = DEFAULT_PORT;
362 p_session->psz_uri = strdup( url.psz_host );
363 p_session->i_port = url.i_port;
364 p_session->psz_sdp = NULL;
366 p_session->i_ttl = config_GetInt( p_sout, "ttl" );
367 p_session->i_payload = 33;
369 msg_Info( p_this, "SAP Enabled");
371 sout_AnnounceRegister( p_sout, p_session, p_method );
373 /* FIXME: Free p_method */
375 p_stream->p_sys->p_session = p_session;
377 vlc_UrlClean( &url );
379 if( p_method->psz_address) free( p_method->psz_address );
383 /* *** Register with slp *** */
385 var_Get( p_stream, SOUT_CFG_PREFIX "slp", &val );
387 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
391 msg_Info( p_this, "SLP Enabled");
392 var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
393 if( *val.psz_string )
395 i_ret = sout_SLPReg( p_sout, psz_url, val.psz_string );
399 i_ret = sout_SLPReg( p_sout, psz_url, psz_url );
404 msg_Warn( p_sout, "SLP Registering failed");
408 p_slp = malloc(sizeof(slp_session_t));
409 p_slp->psz_url = strdup( psz_url );
411 strdup( *val.psz_string ? val.psz_string : psz_url );
413 free( val.psz_string );
417 p_stream->pf_add = Add;
418 p_stream->pf_del = Del;
419 p_stream->pf_send = Send;
421 p_stream->p_sys->p_mux = p_mux;
422 p_stream->p_sys->p_slp = p_slp;
424 if( psz_access ) free( psz_access );
425 if( psz_mux ) free( psz_mux );
426 if( psz_url ) free( psz_url );
432 /*****************************************************************************
434 *****************************************************************************/
435 static void Close( vlc_object_t * p_this )
437 sout_stream_t *p_stream = (sout_stream_t*)p_this;
438 sout_stream_sys_t *p_sys = p_stream->p_sys;
439 sout_access_out_t *p_access = p_sys->p_mux->p_access;
441 if( p_sys->p_session != NULL )
443 sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
444 sout_AnnounceSessionDestroy( p_sys->p_session );
450 sout_SLPDereg( (sout_instance_t *)p_this,
451 p_sys->p_slp->psz_url,
452 p_sys->p_slp->psz_name);
458 sout_MuxDelete( p_sys->p_mux );
459 sout_AccessOutDelete( p_access );
464 struct sout_stream_id_t
466 sout_input_t *p_input;
470 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
472 sout_stream_sys_t *p_sys = p_stream->p_sys;
473 sout_stream_id_t *id;
475 id = malloc( sizeof( sout_stream_id_t ) );
476 if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
486 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
488 sout_stream_sys_t *p_sys = p_stream->p_sys;
490 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
497 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
500 sout_stream_sys_t *p_sys = p_stream->p_sys;
502 sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );