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_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
141 var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
142 psz_access = *val.psz_string ? val.psz_string : NULL;
143 if( val.psz_string && !*val.psz_string ) free( val.psz_string );
145 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
146 psz_mux = *val.psz_string ? val.psz_string : NULL;
147 if( val.psz_string && !*val.psz_string ) free( val.psz_string );
149 var_Get( p_stream, SOUT_CFG_PREFIX "url", &val );
150 psz_url = *val.psz_string ? val.psz_string : NULL;
151 if( val.psz_string && !*val.psz_string ) free( val.psz_string );
153 p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
154 p_stream->p_sys->p_session = NULL;
156 msg_Dbg( p_this, "creating `%s/%s://%s'", psz_access, psz_mux, psz_url );
158 /* ext -> muxer name */
159 if( psz_url && strrchr( psz_url, '.' ) )
162 static struct { char *ext; char *mux; } exttomux[] =
181 char *psz_ext = strrchr( psz_url, '.' ) + 1;
184 msg_Dbg( p_this, "extention is %s", psz_ext );
185 for( i = 0; exttomux[i].ext != NULL; i++ )
187 if( !strcasecmp( psz_ext, exttomux[i].ext ) )
189 psz_mux_byext = exttomux[i].mux;
193 msg_Dbg( p_this, "extention -> mux=%s", psz_mux_byext );
196 /* We fix access/mux to valid couple */
198 if( !psz_access && !psz_mux )
203 "no access _and_ no muxer, extention gives file/%s",
205 psz_access = strdup("file");
206 psz_mux = strdup(psz_mux_byext);
210 msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
215 if( psz_access && !psz_mux )
217 /* access given, no mux */
218 if( !strncmp( psz_access, "mmsh", 4 ) )
220 psz_mux = strdup("asfh");
222 else if( !strncmp( psz_access, "udp", 3 ) )
224 psz_mux = strdup("ts");
226 else if( psz_mux_byext )
228 psz_mux = strdup(psz_mux_byext);
232 msg_Err( p_stream, "no mux specified or found by extention" );
236 else if( psz_mux && !psz_access )
238 /* mux given, no access */
239 if( !strncmp( psz_mux, "asfh", 4 ) )
241 psz_access = strdup("mmsh");
246 psz_access = strdup("file");
250 /* fix or warm of incompatible couple */
251 if( psz_mux && psz_access )
253 if( !strncmp( psz_access, "mmsh", 4 ) &&
254 strncmp( psz_mux, "asfh", 4 ) )
256 char *p = strchr( psz_mux,'{' );
258 msg_Warn( p_stream, "fixing to mmsh/asfh" );
261 /* -> a little memleak but ... */
262 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
263 sprintf( psz_mux, "asfh%s", p );
267 psz_mux = strdup("asfh");
270 else if( ( !strncmp( psz_access, "rtp", 3 ) ||
271 !strncmp( psz_access, "udp", 3 ) ) &&
272 strncmp( psz_mux, "ts", 2 ) )
274 msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
276 else if( strncmp( psz_access, "file", 4 ) &&
277 ( !strncmp( psz_mux, "mov", 3 ) ||
278 !strncmp( psz_mux, "mp4", 3 ) ) )
280 msg_Err( p_stream, "mov and mp4 work only with file output" );
284 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
286 /* *** find and open appropriate access module *** */
287 p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
288 if( p_access == NULL )
290 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
291 psz_access, psz_mux, psz_url );
292 if( psz_access ) free( psz_access );
293 if( psz_mux ) free( psz_mux );
296 msg_Dbg( p_stream, "access opened" );
298 /* *** find and open appropriate mux module *** */
299 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
302 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
303 psz_access, psz_mux, psz_url );
305 sout_AccessOutDelete( p_access );
306 if( psz_access ) free( psz_access );
307 if( psz_mux ) free( psz_mux );
310 msg_Dbg( p_stream, "mux opened" );
312 /* *** Create the SAP Session structure *** */
313 var_Get( p_stream, SOUT_CFG_PREFIX "sap", &val );
315 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
317 session_descriptor_t *p_session = sout_AnnounceSessionCreate();
318 announce_method_t *p_method =
319 sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
322 var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
323 if( *val.psz_string )
325 p_session->psz_name = val.psz_string;
329 free( val.psz_string );
330 p_session->psz_name = strdup( psz_url );
332 var_Get( p_stream, SOUT_CFG_PREFIX "sap-ipv6", &val );
333 p_method->i_ip_version = val.b_bool ? 6 : 4;
335 /* Now, parse the URL to extract host and port */
336 vlc_UrlParse( &url, psz_url , 0);
340 if( url.i_port == 0 ) url.i_port = DEFAULT_PORT;
342 p_session->psz_uri = url.psz_host;
343 p_session->i_port = url.i_port;
344 p_session->psz_sdp = NULL;
346 p_session->i_ttl = config_GetInt( p_sout, "ttl" );
347 p_session->i_payload = 33;
349 msg_Info( p_this, "SAP Enabled");
351 sout_AnnounceRegister( p_sout, p_session, p_method );
353 /* FIXME: Free p_method */
355 p_stream->p_sys->p_session = p_session;
357 vlc_UrlClean( &url );
360 /* *** Register with slp *** */
362 var_Get( p_stream, SOUT_CFG_PREFIX "slp", &val );
364 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
368 msg_Info( p_this, "SLP Enabled");
369 var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
370 if( *val.psz_string )
372 i_ret = sout_SLPReg( p_sout, psz_url, val.psz_string );
376 i_ret = sout_SLPReg( p_sout, psz_url, psz_url );
381 msg_Warn( p_sout, "SLP Registering failed");
385 p_slp = malloc(sizeof(slp_session_t));
386 p_slp->psz_url = strdup( psz_url );
388 strdup( *val.psz_string ? val.psz_string : psz_url );
390 free( val.psz_string );
394 p_stream->pf_add = Add;
395 p_stream->pf_del = Del;
396 p_stream->pf_send = Send;
398 p_stream->p_sys->p_mux = p_mux;
399 p_stream->p_sys->p_slp = p_slp;
401 if( psz_access ) free( psz_access );
402 if( psz_mux ) free( psz_mux );
403 if( psz_url ) free( psz_url );
408 /*****************************************************************************
410 *****************************************************************************/
411 static void Close( vlc_object_t * p_this )
413 sout_stream_t *p_stream = (sout_stream_t*)p_this;
414 sout_stream_sys_t *p_sys = p_stream->p_sys;
415 sout_access_out_t *p_access = p_sys->p_mux->p_access;
417 if( p_sys->p_session != NULL )
419 sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
425 sout_SLPDereg( (sout_instance_t *)p_this,
426 p_sys->p_slp->psz_url,
427 p_sys->p_slp->psz_name);
433 sout_MuxDelete( p_sys->p_mux );
434 sout_AccessOutDelete( p_access );
439 struct sout_stream_id_t
441 sout_input_t *p_input;
445 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
447 sout_stream_sys_t *p_sys = p_stream->p_sys;
448 sout_stream_id_t *id;
450 id = malloc( sizeof( sout_stream_id_t ) );
451 if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
461 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
463 sout_stream_sys_t *p_sys = p_stream->p_sys;
465 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
472 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
475 sout_stream_sys_t *p_sys = p_stream->p_sys;
477 sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );