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 #define DEFAULT_IPV6_SCOPE '8'
38 #define DEFAULT_PORT 1234
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close ( vlc_object_t * );
46 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
47 static int Del ( sout_stream_t *, sout_stream_id_t * );
48 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
50 /*****************************************************************************
52 *****************************************************************************/
54 set_description( _("Standard stream output") );
55 set_capability( "sout stream", 50 );
56 add_shortcut( "standard" );
57 add_shortcut( "std" );
58 set_callbacks( Open, Close );
61 struct sout_stream_sys_t
65 session_descriptor_t *p_session;
68 /*****************************************************************************
70 *****************************************************************************/
71 static int Open( vlc_object_t *p_this )
73 sout_stream_t *p_stream = (sout_stream_t*)p_this;
74 sout_instance_t *p_sout = p_stream->p_sout;
75 slp_session_t *p_slp = NULL;
76 session_descriptor_t *p_session = NULL;
78 char *psz_mux = sout_cfg_find_value( p_stream->p_cfg, "mux" );
79 char *psz_access = sout_cfg_find_value( p_stream->p_cfg, "access" );
80 char *psz_url = sout_cfg_find_value( p_stream->p_cfg, "url" );
84 sout_cfg_t *p_sap_cfg = sout_cfg_find( p_stream->p_cfg, "sap" );
86 sout_cfg_t *p_slp_cfg = sout_cfg_find( p_stream->p_cfg, "slp" );
89 sout_access_out_t *p_access;
92 char *psz_mux_byext = NULL;
94 p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) );
95 p_stream->p_sys->p_session = NULL;
97 msg_Dbg( p_this, "creating `%s/%s://%s'",
98 psz_access, psz_mux, psz_url );
100 /* ext -> muxer name */
101 if( psz_url && strrchr( psz_url, '.' ) )
104 static struct { char *ext; char *mux; } exttomux[] =
123 char *psz_ext = strrchr( psz_url, '.' ) + 1;
126 msg_Dbg( p_this, "extention is %s", psz_ext );
127 for( i = 0; exttomux[i].ext != NULL; i++ )
129 if( !strcasecmp( psz_ext, exttomux[i].ext ) )
131 psz_mux_byext = exttomux[i].mux;
135 msg_Dbg( p_this, "extention -> mux=%s", psz_mux_byext );
138 /* We fix access/mux to valid couple */
140 if( ( psz_access == NULL || *psz_access == '\0' )&&
141 ( psz_mux == NULL || *psz_mux == '\0' ) )
146 "no access _and_ no muxer, extention gives file/%s",
149 psz_mux = psz_mux_byext;
153 msg_Err( p_stream, "no access _and_ no muxer (fatal error)" );
158 if( psz_access && *psz_access &&
159 ( psz_mux == NULL || *psz_mux == '\0' ) )
161 /* access given, no mux */
162 if( !strncmp( psz_access, "mmsh", 4 ) )
166 else if( !strncmp( psz_access, "udp", 3 ) )
172 psz_mux = psz_mux_byext;
175 else if( psz_mux && *psz_mux &&
176 ( psz_access == NULL || *psz_access == '\0' ) )
178 /* mux given, no access */
179 if( !strncmp( psz_mux, "asfh", 4 ) )
190 /* fix or warm of incompatible couple */
191 if( psz_mux && *psz_mux && psz_access && *psz_access )
193 if( !strncmp( psz_access, "mmsh", 4 ) && strncmp( psz_mux, "asfh", 4 ) )
195 char *p = strchr( psz_mux,'{' );
197 msg_Warn( p_stream, "fixing to mmsh/asfh" );
200 /* -> a little memleak but ... */
201 psz_mux = malloc( strlen( "asfh" ) + strlen( p ) + 1);
202 sprintf( psz_mux, "asfh%s", p );
209 else if( ( !strncmp( psz_access, "rtp", 3 ) ||
210 !strncmp( psz_access, "udp", 3 ) ) &&
211 strncmp( psz_mux, "ts", 2 ) )
213 msg_Err( p_stream, "for now udp and rtp are only valid with TS" );
215 else if( strncmp( psz_access, "file", 4 ) &&
216 ( !strncmp( psz_mux, "mov", 3 ) ||
217 !strncmp( psz_mux, "mp4", 3 ) ) )
219 msg_Err( p_stream, "mov and mp4 work only with file output" );
223 msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url );
225 /* *** find and open appropriate access module *** */
226 p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
227 if( p_access == NULL )
229 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
230 psz_access, psz_mux, psz_url );
231 return( VLC_EGENERIC );
233 msg_Dbg( p_stream, "access opened" );
235 /* *** find and open appropriate mux module *** */
236 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
239 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
240 psz_access, psz_mux, psz_url );
242 sout_AccessOutDelete( p_access );
243 return( VLC_EGENERIC );
245 msg_Dbg( p_stream, "mux opened" );
247 /* *** Create the SAP Session structure *** */
248 if( psz_access && p_sap_cfg && ( strstr( psz_access, "udp" ) ||
249 strstr( psz_access , "rtp" ) ) )
251 session_descriptor_t *p_session= sout_AnnounceSessionCreate();
252 announce_method_t *p_method = sout_AnnounceMethodCreate(
255 /* Parse user input */
256 if( p_sap_cfg->psz_value )
258 char *psz_sap = p_sap_cfg->psz_value;
260 if( ! strncmp(psz_sap, "sap{", 4 ) )
263 char *psz_curr,*psz_null;
264 sout_cfg_parser( &psz_null, &p_cfg, psz_sap );
265 psz_curr = sout_cfg_find_value( p_cfg,"name");
266 if( psz_curr != NULL)
268 p_session->psz_name = strdup( psz_curr );
272 p_session->psz_name = strdup( psz_url );
276 psz_curr = sout_cfg_find_value( p_cfg,"ip_version");
277 if( psz_curr != NULL)
279 p_method->i_ip_version = atoi( psz_curr ) != 0 ?
286 p_session->psz_name = strdup( p_sap_cfg->psz_value );
291 p_session->psz_name = strdup( psz_url );
294 /* Now, parse the URL to extract host and port */
295 p_url = (vlc_url_t *)malloc( sizeof(vlc_url_t ) );
301 vlc_UrlParse( p_url, psz_url , 0);
303 if (!p_url->psz_host)
308 if(p_url->i_port == 0)
310 p_url->i_port = DEFAULT_PORT;
313 p_session->psz_uri = p_url->psz_host;
314 p_session->i_port = p_url->i_port;
315 p_session->psz_sdp = NULL;
317 p_session->i_ttl = config_GetInt( p_sout,"ttl" );
318 p_session->i_payload = 33;
320 msg_Info( p_this, "SAP Enabled");
322 sout_AnnounceRegister( p_sout, p_session, p_method );
324 /* FIXME: Free p_method */
326 p_stream->p_sys->p_session = p_session;
330 vlc_UrlClean( p_url );
336 /* *** Register with slp *** */
338 if( p_slp_cfg && ( strstr( psz_access, "udp" ) ||
339 strstr( psz_access , "rtp" ) ) )
341 msg_Info( p_this, "SLP Enabled");
342 if( sout_SLPReg( p_sout, psz_url,
343 p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url) )
345 msg_Warn( p_sout, "SLP Registering failed");
349 p_slp = (slp_session_t*)malloc(sizeof(slp_session_t));
352 msg_Warn(p_sout,"out of memory");
353 // if( p_sap ) free( p_sap );
356 p_slp->psz_url= strdup(psz_url);
357 p_slp->psz_name = strdup(
358 p_slp_cfg->psz_value ? p_slp_cfg->psz_value : psz_url);
363 p_stream->pf_add = Add;
364 p_stream->pf_del = Del;
365 p_stream->pf_send = Send;
367 p_stream->p_sys->p_mux = p_mux;
368 p_stream->p_sys->p_slp = p_slp;
373 /*****************************************************************************
375 *****************************************************************************/
376 static void Close( vlc_object_t * p_this )
378 sout_stream_t *p_stream = (sout_stream_t*)p_this;
379 sout_stream_sys_t *p_sys = p_stream->p_sys;
380 sout_access_out_t *p_access = p_sys->p_mux->p_access;
382 if( p_sys->p_session != NULL )
384 sout_AnnounceUnRegister( p_stream->p_sout, p_sys->p_session );
390 sout_SLPDereg( (sout_instance_t *)p_this,
391 p_sys->p_slp->psz_url,
392 p_sys->p_slp->psz_name);
398 sout_MuxDelete( p_sys->p_mux );
399 sout_AccessOutDelete( p_access );
404 struct sout_stream_id_t
406 sout_input_t *p_input;
410 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
412 sout_stream_sys_t *p_sys = p_stream->p_sys;
413 sout_stream_id_t *id;
415 id = malloc( sizeof( sout_stream_id_t ) );
416 if( ( id->p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
426 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
428 sout_stream_sys_t *p_sys = p_stream->p_sys;
430 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
437 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
440 sout_stream_sys_t *p_sys = p_stream->p_sys;
441 sout_instance_t *p_sout = p_stream->p_sout;
443 sout_MuxSendBuffer( p_sys->p_mux, id->p_input, p_buffer );