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 set_callbacks( Open, Close );
59 /*****************************************************************************
61 *****************************************************************************/
62 static const char *ppsz_sout_options[] = {
63 "access", "mux", "url", NULL
66 #define DEFAULT_IPV6_SCOPE '8'
67 #define DEFAULT_PORT 1234
69 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
70 static int Del ( sout_stream_t *, sout_stream_id_t * );
71 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
73 struct sout_stream_sys_t
77 session_descriptor_t *p_session;
80 /*****************************************************************************
82 *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
85 sout_stream_t *p_stream = (sout_stream_t*)p_this;
86 sout_instance_t *p_sout = p_stream->p_sout;
87 slp_session_t *p_slp = NULL;
93 sout_cfg_t *p_sap_cfg = sout_cfg_find( p_stream->p_cfg, "sap" );
95 sout_cfg_t *p_slp_cfg = sout_cfg_find( p_stream->p_cfg, "slp" );
100 sout_access_out_t *p_access;
103 char *psz_mux_byext = NULL;
105 sout_ParseCfg( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
107 var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
108 psz_access = *val.psz_string ? val.psz_string : NULL;
110 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
111 psz_mux = *val.psz_string ? val.psz_string : NULL;
113 var_Get( p_stream, SOUT_CFG_PREFIX "url", &val );
114 psz_url = *val.psz_string ? val.psz_string : NULL;
116 p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
117 p_stream->p_sys->p_session = NULL;
119 msg_Dbg( p_this, "creating `%s/%s://%s'",
120 psz_access, psz_mux, psz_url );
122 /* ext -> muxer name */
123 if( psz_url && strrchr( psz_url, '.' ) )
126 static struct { char *ext; char *mux; } exttomux[] =
145 char *psz_ext = strrchr( psz_url, '.' ) + 1;
148 msg_Dbg( p_this, "extention is %s", psz_ext );
149 for( i = 0; exttomux[i].ext != NULL; i++ )
151 if( !strcasecmp( psz_ext, exttomux[i].ext ) )
153 psz_mux_byext = exttomux[i].mux;
157 msg_Dbg( p_this, "extention -> mux=%s", psz_mux_byext );
160 /* We fix access/mux to valid couple */
162 if( psz_access == NULL && psz_mux == NULL )
167 "no access _and_ no muxer, extention gives file/%s",
170 psz_mux = psz_mux_byext;
174 msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
179 if( psz_access && psz_mux == NULL )
181 /* access given, no mux */
182 if( !strncmp( psz_access, "mmsh", 4 ) )
186 else if( !strncmp( psz_access, "udp", 3 ) )
192 psz_mux = psz_mux_byext;
195 else if( psz_mux && psz_access == NULL )
197 /* mux given, no access */
198 if( !strncmp( psz_mux, "asfh", 4 ) )
209 /* fix or warm of incompatible couple */
210 if( psz_mux && psz_access )
212 if( !strncmp( psz_access, "mmsh", 4 ) && strncmp( psz_mux, "asfh", 4 ) )
214 char *p = strchr( psz_mux,'{' );
216 msg_Warn( p_stream, "fixing to mmsh/asfh" );
219 /* -> a little memleak but ... */
220 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
221 sprintf( psz_mux, "asfh%s", p );
228 else if( ( !strncmp( psz_access, "rtp", 3 ) ||
229 !strncmp( psz_access, "udp", 3 ) ) &&
230 strncmp( psz_mux, "ts", 2 ) )
232 msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
234 else if( strncmp( psz_access, "file", 4 ) &&
235 ( !strncmp( psz_mux, "mov", 3 ) ||
236 !strncmp( psz_mux, "mp4", 3 ) ) )
238 msg_Err( p_stream, "mov and mp4 work only with file output" );
242 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
244 /* *** find and open appropriate access module *** */
245 p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
246 if( p_access == NULL )
248 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
249 psz_access, psz_mux, psz_url );
250 return( VLC_EGENERIC );
252 msg_Dbg( p_stream, "access opened" );
254 /* *** find and open appropriate mux module *** */
255 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
258 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
259 psz_access, psz_mux, psz_url );
261 sout_AccessOutDelete( p_access );
262 return( VLC_EGENERIC );
264 msg_Dbg( p_stream, "mux opened" );
266 /* *** Create the SAP Session structure *** */
267 if( psz_access && p_sap_cfg &&
268 ( strstr( psz_access, "udp" ) || strstr( psz_access , "rtp" ) ) )
270 session_descriptor_t *p_session = sout_AnnounceSessionCreate();
271 announce_method_t *p_method =
272 sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
275 /* Parse user input */
276 if( p_sap_cfg->psz_value )
278 char *psz_sap = p_sap_cfg->psz_value;
280 if( ! strncmp(psz_sap, "sap{", 4 ) )
283 char *psz_curr,*psz_null;
284 sout_cfg_parser( &psz_null, &p_cfg, psz_sap );
285 psz_curr = sout_cfg_find_value( p_cfg,"name");
286 if( psz_curr != NULL)
288 p_session->psz_name = strdup( psz_curr );
292 p_session->psz_name = strdup( psz_url );
296 psz_curr = sout_cfg_find_value( p_cfg,"ip_version");
297 if( psz_curr != NULL)
299 p_method->i_ip_version =
300 atoi( psz_curr ) != 0 ? atoi(psz_curr) : 4;
305 p_session->psz_name = strdup( p_sap_cfg->psz_value );
310 p_session->psz_name = strdup( psz_url );
313 /* Now, parse the URL to extract host and port */
314 vlc_UrlParse( &url, psz_url , 0);
318 if( url.i_port == 0 )
320 url.i_port = DEFAULT_PORT;
323 p_session->psz_uri = url.psz_host;
324 p_session->i_port = url.i_port;
325 p_session->psz_sdp = NULL;
327 p_session->i_ttl = config_GetInt( p_sout, "ttl" );
328 p_session->i_payload = 33;
330 msg_Info( p_this, "SAP Enabled");
332 sout_AnnounceRegister( p_sout, p_session, p_method );
334 /* FIXME: Free p_method */
336 p_stream->p_sys->p_session = p_session;
338 vlc_UrlClean( &url );
341 /* *** Register with slp *** */
343 if( p_slp_cfg && ( strstr( psz_access, "udp" ) ||
344 strstr( psz_access , "rtp" ) ) )
346 msg_Info( p_this, "SLP Enabled");
347 if( sout_SLPReg( p_sout, psz_url,
348 p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url) )
350 msg_Warn( p_sout, "SLP Registering failed");
354 p_slp = malloc(sizeof(slp_session_t));
355 p_slp->psz_url= strdup(psz_url);
356 p_slp->psz_name = strdup(
357 p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url);
362 p_stream->pf_add = Add;
363 p_stream->pf_del = Del;
364 p_stream->pf_send = Send;
366 p_stream->p_sys->p_mux = p_mux;
367 p_stream->p_sys->p_slp = p_slp;
372 /*****************************************************************************
374 *****************************************************************************/
375 static void Close( vlc_object_t * p_this )
377 sout_stream_t *p_stream = (sout_stream_t*)p_this;
378 sout_stream_sys_t *p_sys = p_stream->p_sys;
379 sout_access_out_t *p_access = p_sys->p_mux->p_access;
381 if( p_sys->p_session != NULL )
383 sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
389 sout_SLPDereg( (sout_instance_t *)p_this,
390 p_sys->p_slp->psz_url,
391 p_sys->p_slp->psz_name);
397 sout_MuxDelete( p_sys->p_mux );
398 sout_AccessOutDelete( p_access );
403 struct sout_stream_id_t
405 sout_input_t *p_input;
409 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
411 sout_stream_sys_t *p_sys = p_stream->p_sys;
412 sout_stream_id_t *id;
414 id = malloc( sizeof( sout_stream_id_t ) );
415 if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
425 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
427 sout_stream_sys_t *p_sys = p_stream->p_sys;
429 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
436 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
439 sout_stream_sys_t *p_sys = p_stream->p_sys;
441 sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );