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 specify the output access method used for the streaming " \
44 #define MUX_TEXT N_("Output muxer")
45 #define MUX_LONGTEXT N_( \
46 "Allows you to specify the output muxer method used for the streaming " \
48 #define URL_TEXT N_("Output URL")
49 #define URL_LONGTEXT N_( \
50 "Allows you to specify the output URL used for the streaming output." )
52 #define NAME_TEXT N_("Session name")
53 #define NAME_LONGTEXT N_( \
54 "Name of the session that will be announced with SAP or SLP" )
56 #define SAP_TEXT N_("SAP announcing")
57 #define SAP_LONGTEXT N_("Announce this session with SAP")
59 #define SAPv6_TEXT N_("SAP IPv6 announcing")
60 #define SAPv6_LONGTEXT N_("Use IPv6 to announce this session with SAP")
62 #define SLP_TEXT N_("SLP announcing")
63 #define SLP_LONGTEXT N_("Announce this session with SLP")
65 static int Open ( vlc_object_t * );
66 static void Close ( vlc_object_t * );
68 #define SOUT_CFG_PREFIX "sout-standard-"
71 set_description( _("Standard stream output") );
72 set_capability( "sout stream", 50 );
73 add_shortcut( "standard" );
74 add_shortcut( "std" );
76 add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
77 ACCESS_LONGTEXT, VLC_FALSE );
78 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
79 MUX_LONGTEXT, VLC_FALSE );
80 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
81 URL_LONGTEXT, VLC_FALSE );
83 add_bool( SOUT_CFG_PREFIX "sap", 0, NULL, SAP_TEXT, SAP_LONGTEXT, VLC_TRUE );
84 add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT,NAME_LONGTEXT
86 add_bool( SOUT_CFG_PREFIX "sap-ipv6", 0, NULL, SAPv6_TEXT, SAPv6_LONGTEXT,
89 add_bool( SOUT_CFG_PREFIX "slp", 0, NULL, SLP_TEXT, SLP_LONGTEXT, VLC_TRUE );
91 set_callbacks( Open, Close );
95 /*****************************************************************************
97 *****************************************************************************/
98 static const char *ppsz_sout_options[] = {
99 "access", "mux", "url",
100 "sap", "name", "sap-ipv6",
104 #define DEFAULT_IPV6_SCOPE '8'
105 #define DEFAULT_PORT 1234
107 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
108 static int Del ( sout_stream_t *, sout_stream_id_t * );
109 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
111 struct sout_stream_sys_t
114 slp_session_t *p_slp;
115 session_descriptor_t *p_session;
118 /*****************************************************************************
120 *****************************************************************************/
121 static int Open( vlc_object_t *p_this )
123 sout_stream_t *p_stream = (sout_stream_t*)p_this;
124 sout_instance_t *p_sout = p_stream->p_sout;
125 slp_session_t *p_slp = NULL;
133 sout_access_out_t *p_access;
136 char *psz_mux_byext = NULL;
138 sout_ParseCfg( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
140 var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
141 psz_access = *val.psz_string ? val.psz_string : NULL;
143 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
144 psz_mux = *val.psz_string ? val.psz_string : NULL;
146 var_Get( p_stream, SOUT_CFG_PREFIX "url", &val );
147 psz_url = *val.psz_string ? val.psz_string : NULL;
149 p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
150 p_stream->p_sys->p_session = NULL;
152 msg_Dbg( p_this, "creating `%s/%s://%s'",
153 psz_access, psz_mux, psz_url );
155 /* ext -> muxer name */
156 if( psz_url && strrchr( psz_url, '.' ) )
159 static struct { char *ext; char *mux; } exttomux[] =
178 char *psz_ext = strrchr( psz_url, '.' ) + 1;
181 msg_Dbg( p_this, "extention is %s", psz_ext );
182 for( i = 0; exttomux[i].ext != NULL; i++ )
184 if( !strcasecmp( psz_ext, exttomux[i].ext ) )
186 psz_mux_byext = exttomux[i].mux;
190 msg_Dbg( p_this, "extention -> mux=%s", psz_mux_byext );
193 /* We fix access/mux to valid couple */
195 if( psz_access == NULL && psz_mux == NULL )
200 "no access _and_ no muxer, extention gives file/%s",
203 psz_mux = psz_mux_byext;
207 msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
212 if( psz_access && psz_mux == NULL )
214 /* access given, no mux */
215 if( !strncmp( psz_access, "mmsh", 4 ) )
219 else if( !strncmp( psz_access, "udp", 3 ) )
225 psz_mux = psz_mux_byext;
228 else if( psz_mux && psz_access == NULL )
230 /* mux given, no access */
231 if( !strncmp( psz_mux, "asfh", 4 ) )
242 /* fix or warm of incompatible couple */
243 if( psz_mux && psz_access )
245 if( !strncmp( psz_access, "mmsh", 4 ) && strncmp( psz_mux, "asfh", 4 ) )
247 char *p = strchr( psz_mux,'{' );
249 msg_Warn( p_stream, "fixing to mmsh/asfh" );
252 /* -> a little memleak but ... */
253 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
254 sprintf( psz_mux, "asfh%s", p );
261 else if( ( !strncmp( psz_access, "rtp", 3 ) ||
262 !strncmp( psz_access, "udp", 3 ) ) &&
263 strncmp( psz_mux, "ts", 2 ) )
265 msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
267 else if( strncmp( psz_access, "file", 4 ) &&
268 ( !strncmp( psz_mux, "mov", 3 ) ||
269 !strncmp( psz_mux, "mp4", 3 ) ) )
271 msg_Err( p_stream, "mov and mp4 work only with file output" );
275 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
277 /* *** find and open appropriate access module *** */
278 p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
279 if( p_access == NULL )
281 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
282 psz_access, psz_mux, psz_url );
283 return( VLC_EGENERIC );
285 msg_Dbg( p_stream, "access opened" );
287 /* *** find and open appropriate mux module *** */
288 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
291 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
292 psz_access, psz_mux, psz_url );
294 sout_AccessOutDelete( p_access );
295 return( VLC_EGENERIC );
297 msg_Dbg( p_stream, "mux opened" );
299 /* *** Create the SAP Session structure *** */
300 var_Get( p_stream, SOUT_CFG_PREFIX "sap", &val );
302 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
304 session_descriptor_t *p_session = sout_AnnounceSessionCreate();
305 announce_method_t *p_method =
306 sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
309 var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
310 if( *val.psz_string )
312 p_session->psz_name = val.psz_string;
316 free( val.psz_string );
317 p_session->psz_name = strdup( psz_url );
319 var_Get( p_stream, SOUT_CFG_PREFIX "sap-ipv6", &val );
320 p_method->i_ip_version = val.b_bool ? 6 : 4;
322 /* Now, parse the URL to extract host and port */
323 vlc_UrlParse( &url, psz_url , 0);
327 if( url.i_port == 0 )
329 url.i_port = DEFAULT_PORT;
332 p_session->psz_uri = url.psz_host;
333 p_session->i_port = url.i_port;
334 p_session->psz_sdp = NULL;
336 p_session->i_ttl = config_GetInt( p_sout, "ttl" );
337 p_session->i_payload = 33;
339 msg_Info( p_this, "SAP Enabled");
341 sout_AnnounceRegister( p_sout, p_session, p_method );
343 /* FIXME: Free p_method */
345 p_stream->p_sys->p_session = p_session;
347 vlc_UrlClean( &url );
350 /* *** Register with slp *** */
352 var_Get( p_stream, SOUT_CFG_PREFIX "slp", &val );
354 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
358 msg_Info( p_this, "SLP Enabled");
359 var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
360 if( *val.psz_string )
362 i_ret = sout_SLPReg( p_sout, psz_url, val.psz_string );
366 i_ret = sout_SLPReg( p_sout, psz_url, psz_url );
371 msg_Warn( p_sout, "SLP Registering failed");
375 p_slp = malloc(sizeof(slp_session_t));
376 p_slp->psz_url= strdup(psz_url);
377 p_slp->psz_name = strdup( *val.psz_string ? val.psz_string :
380 free( val.psz_string );
384 p_stream->pf_add = Add;
385 p_stream->pf_del = Del;
386 p_stream->pf_send = Send;
388 p_stream->p_sys->p_mux = p_mux;
389 p_stream->p_sys->p_slp = p_slp;
394 /*****************************************************************************
396 *****************************************************************************/
397 static void Close( vlc_object_t * p_this )
399 sout_stream_t *p_stream = (sout_stream_t*)p_this;
400 sout_stream_sys_t *p_sys = p_stream->p_sys;
401 sout_access_out_t *p_access = p_sys->p_mux->p_access;
403 if( p_sys->p_session != NULL )
405 sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
411 sout_SLPDereg( (sout_instance_t *)p_this,
412 p_sys->p_slp->psz_url,
413 p_sys->p_slp->psz_name);
419 sout_MuxDelete( p_sys->p_mux );
420 sout_AccessOutDelete( p_access );
425 struct sout_stream_id_t
427 sout_input_t *p_input;
431 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
433 sout_stream_sys_t *p_sys = p_stream->p_sys;
434 sout_stream_id_t *id;
436 id = malloc( sizeof( sout_stream_id_t ) );
437 if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
447 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
449 sout_stream_sys_t *p_sys = p_stream->p_sys;
451 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
458 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
461 sout_stream_sys_t *p_sys = p_stream->p_sys;
463 sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );