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_description( _("Standard stream output") );
79 set_capability( "sout stream", 50 );
80 add_shortcut( "standard" );
81 add_shortcut( "std" );
82 set_category( CAT_SOUT );
83 set_subcategory( SUBCAT_SOUT_STREAM );
85 add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
86 ACCESS_LONGTEXT, VLC_FALSE );
87 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
88 MUX_LONGTEXT, VLC_FALSE );
89 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
90 URL_LONGTEXT, VLC_FALSE );
92 add_bool( SOUT_CFG_PREFIX "sap", 0, NULL, SAP_TEXT, SAP_LONGTEXT, VLC_TRUE );
93 add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT, NAME_LONGTEXT,
95 add_string( SOUT_CFG_PREFIX "group", "", NULL, GROUP_TEXT, GROUP_LONGTEXT,
97 add_bool( SOUT_CFG_PREFIX "sap-ipv6", 0, NULL, SAPv6_TEXT, SAPv6_LONGTEXT,
100 add_bool( SOUT_CFG_PREFIX "slp", 0, NULL, SLP_TEXT, SLP_LONGTEXT, VLC_TRUE );
102 set_callbacks( Open, Close );
106 /*****************************************************************************
107 * Exported prototypes
108 *****************************************************************************/
109 static const char *ppsz_sout_options[] = {
110 "access", "mux", "url",
111 "sap", "name", "sap-ipv6", "group",
115 #define DEFAULT_IPV6_SCOPE '8'
116 #define DEFAULT_PORT 1234
118 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
119 static int Del ( sout_stream_t *, sout_stream_id_t * );
120 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
122 struct sout_stream_sys_t
125 slp_session_t *p_slp;
126 session_descriptor_t *p_session;
129 /*****************************************************************************
131 *****************************************************************************/
132 static int Open( vlc_object_t *p_this )
134 sout_stream_t *p_stream = (sout_stream_t*)p_this;
135 sout_instance_t *p_sout = p_stream->p_sout;
136 slp_session_t *p_slp = NULL;
144 sout_access_out_t *p_access;
147 char *psz_mux_byext = NULL;
149 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
152 var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
153 psz_access = *val.psz_string ? val.psz_string : NULL;
154 if( val.psz_string && !*val.psz_string ) free( val.psz_string );
156 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
157 psz_mux = *val.psz_string ? val.psz_string : NULL;
158 if( val.psz_string && !*val.psz_string ) free( val.psz_string );
160 var_Get( p_stream, SOUT_CFG_PREFIX "url", &val );
161 psz_url = *val.psz_string ? val.psz_string : NULL;
162 if( val.psz_string && !*val.psz_string ) free( val.psz_string );
164 p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
165 p_stream->p_sys->p_session = NULL;
167 msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
169 /* ext -> muxer name */
170 if( psz_url && strrchr( psz_url, '.' ) )
173 static struct { char *ext; char *mux; } exttomux[] =
192 char *psz_ext = strrchr( psz_url, '.' ) + 1;
195 msg_Dbg( p_this, "extention is %s", psz_ext );
196 for( i = 0; exttomux[i].ext != NULL; i++ )
198 if( !strcasecmp( psz_ext, exttomux[i].ext ) )
200 psz_mux_byext = exttomux[i].mux;
204 msg_Dbg( p_this, "extention -> mux=%s", psz_mux_byext );
207 /* We fix access/mux to valid couple */
209 if( !psz_access && !psz_mux )
214 "no access _and_ no muxer, extention gives file/%s",
216 psz_access = strdup("file");
217 psz_mux = strdup(psz_mux_byext);
221 msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
226 if( psz_access && !psz_mux )
228 /* access given, no mux */
229 if( !strncmp( psz_access, "mmsh", 4 ) )
231 psz_mux = strdup("asfh");
233 else if( !strncmp( psz_access, "udp", 3 ) )
235 psz_mux = strdup("ts");
237 else if( psz_mux_byext )
239 psz_mux = strdup(psz_mux_byext);
243 msg_Err( p_stream, "no mux specified or found by extention" );
247 else if( psz_mux && !psz_access )
249 /* mux given, no access */
250 if( !strncmp( psz_mux, "asfh", 4 ) )
252 psz_access = strdup("mmsh");
257 psz_access = strdup("file");
261 /* fix or warm of incompatible couple */
262 if( psz_mux && psz_access )
264 if( !strncmp( psz_access, "mmsh", 4 ) &&
265 strncmp( psz_mux, "asfh", 4 ) )
267 char *p = strchr( psz_mux,'{' );
269 msg_Warn( p_stream, "fixing to mmsh/asfh" );
272 /* -> a little memleak but ... */
273 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
274 sprintf( psz_mux, "asfh%s", p );
278 psz_mux = strdup("asfh");
281 else if( ( !strncmp( psz_access, "rtp", 3 ) ||
282 !strncmp( psz_access, "udp", 3 ) ) &&
283 strncmp( psz_mux, "ts", 2 ) )
285 msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
287 else if( strncmp( psz_access, "file", 4 ) &&
288 ( !strncmp( psz_mux, "mov", 3 ) ||
289 !strncmp( psz_mux, "mp4", 3 ) ) )
291 msg_Err( p_stream, "mov and mp4 work only with file output" );
295 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
297 /* *** find and open appropriate access module *** */
298 p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
299 if( p_access == NULL )
301 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
302 psz_access, psz_mux, psz_url );
303 if( psz_access ) free( psz_access );
304 if( psz_mux ) free( psz_mux );
307 msg_Dbg( p_stream, "access opened" );
309 /* *** find and open appropriate mux module *** */
310 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
313 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
314 psz_access, psz_mux, psz_url );
316 sout_AccessOutDelete( p_access );
317 if( psz_access ) free( psz_access );
318 if( psz_mux ) free( psz_mux );
321 msg_Dbg( p_stream, "mux opened" );
323 /* *** Create the SAP Session structure *** */
324 var_Get( p_stream, SOUT_CFG_PREFIX "sap", &val );
326 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
328 session_descriptor_t *p_session = sout_AnnounceSessionCreate();
329 announce_method_t *p_method =
330 sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
333 var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
334 if( *val.psz_string )
336 p_session->psz_name = strdup( val.psz_string );
340 p_session->psz_name = strdup( psz_url );
342 free( val.psz_string );
344 var_Get( p_stream, SOUT_CFG_PREFIX "group", &val );
345 if( *val.psz_string )
347 p_session->psz_group = strdup( val.psz_string );
349 free( val.psz_string );
351 var_Get( p_stream, SOUT_CFG_PREFIX "sap-ipv6", &val );
352 p_method->i_ip_version = val.b_bool ? 6 : 4;
354 /* Now, parse the URL to extract host and port */
355 vlc_UrlParse( &url, psz_url , 0);
359 if( url.i_port == 0 ) url.i_port = DEFAULT_PORT;
361 p_session->psz_uri = strdup( url.psz_host );
362 p_session->i_port = url.i_port;
363 p_session->psz_sdp = NULL;
365 p_session->i_ttl = config_GetInt( p_sout, "ttl" );
366 p_session->i_payload = 33;
368 msg_Info( p_this, "SAP Enabled");
370 sout_AnnounceRegister( p_sout, p_session, p_method );
372 /* FIXME: Free p_method */
374 p_stream->p_sys->p_session = p_session;
376 vlc_UrlClean( &url );
378 if( p_method->psz_address) free( p_method->psz_address );
382 /* *** Register with slp *** */
384 var_Get( p_stream, SOUT_CFG_PREFIX "slp", &val );
386 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
390 msg_Info( p_this, "SLP Enabled");
391 var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
392 if( *val.psz_string )
394 i_ret = sout_SLPReg( p_sout, psz_url, val.psz_string );
398 i_ret = sout_SLPReg( p_sout, psz_url, psz_url );
403 msg_Warn( p_sout, "SLP Registering failed");
407 p_slp = malloc(sizeof(slp_session_t));
408 p_slp->psz_url = strdup( psz_url );
410 strdup( *val.psz_string ? val.psz_string : psz_url );
412 free( val.psz_string );
416 p_stream->pf_add = Add;
417 p_stream->pf_del = Del;
418 p_stream->pf_send = Send;
420 p_stream->p_sys->p_mux = p_mux;
421 p_stream->p_sys->p_slp = p_slp;
423 if( psz_access ) free( psz_access );
424 if( psz_mux ) free( psz_mux );
425 if( psz_url ) free( psz_url );
431 /*****************************************************************************
433 *****************************************************************************/
434 static void Close( vlc_object_t * p_this )
436 sout_stream_t *p_stream = (sout_stream_t*)p_this;
437 sout_stream_sys_t *p_sys = p_stream->p_sys;
438 sout_access_out_t *p_access = p_sys->p_mux->p_access;
440 if( p_sys->p_session != NULL )
442 sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
443 sout_AnnounceSessionDestroy( p_sys->p_session );
449 sout_SLPDereg( (sout_instance_t *)p_this,
450 p_sys->p_slp->psz_url,
451 p_sys->p_slp->psz_name);
457 sout_MuxDelete( p_sys->p_mux );
458 sout_AccessOutDelete( p_access );
463 struct sout_stream_id_t
465 sout_input_t *p_input;
469 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
471 sout_stream_sys_t *p_sys = p_stream->p_sys;
472 sout_stream_id_t *id;
474 id = malloc( sizeof( sout_stream_id_t ) );
475 if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
485 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
487 sout_stream_sys_t *p_sys = p_stream->p_sys;
489 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
496 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
499 sout_stream_sys_t *p_sys = p_stream->p_sys;
501 sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );