1 /*****************************************************************************
2 * stream_output.c : stream output module
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Eric Petit <titer@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* free() */
30 #include <stdio.h> /* sprintf() */
31 #include <string.h> /* strerror() */
39 /*****************************************************************************
41 *****************************************************************************/
42 static void sout_cfg_free( sout_cfg_t * );
44 #define sout_stream_url_to_chain( p, s ) _sout_stream_url_to_chain( VLC_OBJECT(p), s )
45 static char *_sout_stream_url_to_chain( vlc_object_t *, char * );
59 /* mrl_Parse: parse psz_mrl and fill p_mrl */
60 static int mrl_Parse( mrl_t *p_mrl, char *psz_mrl );
61 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
62 static void mrl_Clean( mrl_t *p_mrl );
64 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
66 /*****************************************************************************
67 * sout_NewInstance: creates a new stream output instance
68 *****************************************************************************/
69 sout_instance_t *__sout_NewInstance( vlc_object_t *p_parent, char * psz_dest )
71 sout_instance_t *p_sout;
74 if( var_Get( p_parent, "sout-keep", &keep ) < 0 )
76 msg_Warn( p_parent, "cannot get sout-keep value" );
77 keep.b_bool = VLC_FALSE;
79 else if( keep.b_bool )
81 msg_Warn( p_parent, "sout-keep true" );
82 if( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT,
85 if( !strcmp( p_sout->psz_sout, psz_dest ) )
87 msg_Warn( p_parent, "sout keep : reusing sout" );
88 msg_Warn( p_parent, "sout keep : you probably want to use "
89 "gather stream_out" );
90 vlc_object_detach( p_sout );
91 vlc_object_attach( p_sout, p_parent );
92 vlc_object_release( p_sout );
97 msg_Warn( p_parent, "sout keep : destroying unusable sout" );
98 sout_DeleteInstance( p_sout );
102 else if( !keep.b_bool )
104 while( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT,
107 msg_Warn( p_parent, "sout keep : destroying old sout" );
108 sout_DeleteInstance( p_sout );
112 /* *** Allocate descriptor *** */
113 p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
116 msg_Err( p_parent, "out of memory" );
120 /* *** init descriptor *** */
121 p_sout->psz_sout = strdup( psz_dest );
122 p_sout->p_meta = NULL;
123 p_sout->i_out_pace_nocontrol = 0;
124 p_sout->p_sys = NULL;
126 vlc_mutex_init( p_sout, &p_sout->lock );
127 if( psz_dest && psz_dest[0] == '#' )
129 p_sout->psz_chain = strdup( &psz_dest[1] );
133 p_sout->psz_chain = sout_stream_url_to_chain( p_sout, psz_dest );
134 msg_Dbg( p_sout, "using sout chain=`%s'", p_sout->psz_chain );
136 p_sout->p_stream = NULL;
138 /* attach it for inherit */
139 vlc_object_attach( p_sout, p_parent );
141 p_sout->p_stream = sout_stream_new( p_sout, p_sout->psz_chain );
143 if( p_sout->p_stream == NULL )
145 msg_Err( p_sout, "stream chained failed for `%s'", p_sout->psz_chain );
147 FREE( p_sout->psz_sout );
148 FREE( p_sout->psz_chain );
150 vlc_object_destroy( p_sout );
156 /*****************************************************************************
157 * sout_DeleteInstance: delete a previously allocated instance
158 *****************************************************************************/
159 void sout_DeleteInstance( sout_instance_t * p_sout )
162 vlc_object_detach( p_sout );
164 /* remove the stream out chain */
165 sout_stream_delete( p_sout->p_stream );
167 /* *** free all string *** */
168 FREE( p_sout->psz_sout );
169 FREE( p_sout->psz_chain );
174 vlc_meta_Delete( p_sout->p_meta );
177 vlc_mutex_destroy( &p_sout->lock );
179 /* *** free structure *** */
180 vlc_object_destroy( p_sout );
183 /*****************************************************************************
185 *****************************************************************************/
186 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
189 sout_packetizer_input_t *p_input;
191 msg_Dbg( p_sout, "adding a new input" );
193 /* *** create a packetizer input *** */
194 p_input = malloc( sizeof( sout_packetizer_input_t ) );
195 p_input->p_sout = p_sout;
196 p_input->p_fmt = p_fmt;
198 if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
200 vlc_object_release( p_sout );
204 /* *** add it to the stream chain */
205 vlc_mutex_lock( &p_sout->lock );
206 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
207 vlc_mutex_unlock( &p_sout->lock );
209 if( p_input->id == NULL )
218 /*****************************************************************************
220 *****************************************************************************/
221 int sout_InputDelete( sout_packetizer_input_t *p_input )
223 sout_instance_t *p_sout = p_input->p_sout;
225 msg_Dbg( p_sout, "removing an input" );
227 if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
229 vlc_mutex_lock( &p_sout->lock );
230 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
231 vlc_mutex_unlock( &p_sout->lock );
236 return( VLC_SUCCESS);
239 /*****************************************************************************
241 *****************************************************************************/
242 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
245 sout_instance_t *p_sout = p_input->p_sout;
248 if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
250 block_Release( p_buffer );
253 if( p_buffer->i_dts <= 0 )
255 msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
256 block_Release( p_buffer );
260 vlc_mutex_lock( &p_sout->lock );
261 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
262 p_input->id, p_buffer );
263 vlc_mutex_unlock( &p_sout->lock );
268 /*****************************************************************************
269 * sout_AccessOutNew: allocate a new access out
270 *****************************************************************************/
271 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
272 char *psz_access, char *psz_name )
274 sout_access_out_t *p_access;
277 if( !( p_access = vlc_object_create( p_sout,
278 sizeof( sout_access_out_t ) ) ) )
280 msg_Err( p_sout, "out of memory" );
284 psz_next = sout_cfg_parser( &p_access->psz_access, &p_access->p_cfg,
290 p_access->psz_name = strdup( psz_name ? psz_name : "" );
291 p_access->p_sout = p_sout;
292 p_access->p_sys = NULL;
293 p_access->pf_seek = NULL;
294 p_access->pf_read = NULL;
295 p_access->pf_write = NULL;
296 p_access->p_module = NULL;
297 vlc_object_attach( p_access, p_sout );
300 module_Need( p_access, "sout access", p_access->psz_access, VLC_TRUE );
302 if( !p_access->p_module )
304 free( p_access->psz_access );
305 free( p_access->psz_name );
306 vlc_object_destroy( p_access );
312 /*****************************************************************************
313 * sout_AccessDelete: delete an access out
314 *****************************************************************************/
315 void sout_AccessOutDelete( sout_access_out_t *p_access )
317 vlc_object_detach( p_access );
318 if( p_access->p_module )
320 module_Unneed( p_access, p_access->p_module );
322 free( p_access->psz_access );
324 sout_cfg_free( p_access->p_cfg );
326 free( p_access->psz_name );
328 vlc_object_destroy( p_access );
331 /*****************************************************************************
333 *****************************************************************************/
334 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
336 return p_access->pf_seek( p_access, i_pos );
339 /*****************************************************************************
341 *****************************************************************************/
342 int sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
344 return( p_access->pf_read ?
345 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
348 /*****************************************************************************
350 *****************************************************************************/
351 int sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
353 return p_access->pf_write( p_access, p_buffer );
356 /*****************************************************************************
357 * sout_MuxNew: create a new mux
358 *****************************************************************************/
359 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
360 sout_access_out_t *p_access )
365 p_mux = vlc_object_create( p_sout,
366 sizeof( sout_mux_t ) );
369 msg_Err( p_sout, "out of memory" );
373 p_mux->p_sout = p_sout;
374 psz_next = sout_cfg_parser( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
379 p_mux->p_access = p_access;
380 p_mux->pf_capacity = NULL;
381 p_mux->pf_addstream = NULL;
382 p_mux->pf_delstream = NULL;
383 p_mux->pf_mux = NULL;
384 p_mux->i_nb_inputs = 0;
385 p_mux->pp_inputs = NULL;
388 p_mux->p_module = NULL;
390 vlc_object_attach( p_mux, p_sout );
393 module_Need( p_mux, "sout mux", p_mux->psz_mux, VLC_TRUE );
395 if( p_mux->p_module == NULL )
397 FREE( p_mux->psz_mux );
399 vlc_object_destroy( p_mux );
403 /* *** probe mux capacity *** */
404 if( p_mux->pf_capacity )
407 if( p_mux->pf_capacity( p_mux,
408 SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME, NULL,
409 (void*)&b_answer ) != SOUT_MUX_CAP_ERR_OK )
411 b_answer = VLC_FALSE;
415 msg_Dbg( p_sout, "muxer support adding stream at any time" );
416 p_mux->b_add_stream_any_time = VLC_TRUE;
417 p_mux->b_waiting_stream = VLC_FALSE;
419 if( p_mux->pf_capacity( p_mux,
420 SOUT_MUX_CAP_GET_ADD_STREAM_WAIT, NULL,
421 (void*)&b_answer ) != SOUT_MUX_CAP_ERR_OK )
423 b_answer = VLC_FALSE;
427 msg_Dbg( p_sout, "muxer prefers waiting for all ES before "
429 p_mux->b_waiting_stream = VLC_TRUE;
434 p_mux->b_add_stream_any_time = VLC_FALSE;
435 p_mux->b_waiting_stream = VLC_TRUE;
440 p_mux->b_add_stream_any_time = VLC_FALSE;
441 p_mux->b_waiting_stream = VLC_TRUE;
443 p_mux->i_add_stream_start = -1;
448 /*****************************************************************************
450 *****************************************************************************/
451 void sout_MuxDelete( sout_mux_t *p_mux )
453 vlc_object_detach( p_mux );
454 if( p_mux->p_module )
456 module_Unneed( p_mux, p_mux->p_module );
458 free( p_mux->psz_mux );
460 sout_cfg_free( p_mux->p_cfg );
462 vlc_object_destroy( p_mux );
465 /*****************************************************************************
467 *****************************************************************************/
468 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
470 sout_input_t *p_input;
472 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
474 msg_Err( p_mux, "cannot add a new stream (unsuported while muxing "
475 "for this format)" );
478 if( p_mux->i_add_stream_start < 0 )
480 /* we wait for one second */
481 p_mux->i_add_stream_start = mdate();
484 msg_Dbg( p_mux, "adding a new input" );
486 /* create a new sout input */
487 p_input = malloc( sizeof( sout_input_t ) );
488 p_input->p_sout = p_mux->p_sout;
489 p_input->p_fmt = p_fmt;
490 p_input->p_fifo = block_FifoNew( p_mux->p_sout );
491 p_input->p_sys = NULL;
493 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
494 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
496 msg_Err( p_mux, "cannot add this stream" );
497 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
498 block_FifoRelease( p_input->p_fifo );
506 /*****************************************************************************
507 * sout_MuxDeleteStream:
508 *****************************************************************************/
509 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
513 if( p_mux->b_waiting_stream && p_input->p_fifo->i_depth > 0 )
515 /* We stop waiting, and call the muxer for taking care of the data
516 * before we remove this es */
517 p_mux->b_waiting_stream = VLC_FALSE;
518 p_mux->pf_mux( p_mux );
521 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
524 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
526 msg_Err( p_mux, "cannot del this stream from mux" );
529 /* remove the entry */
530 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
532 if( p_mux->i_nb_inputs == 0 )
534 msg_Warn( p_mux, "no more input stream for this mux" );
537 block_FifoRelease( p_input->p_fifo );
542 /*****************************************************************************
543 * sout_MuxSendBuffer:
544 *****************************************************************************/
545 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
548 block_FifoPut( p_input->p_fifo, p_buffer );
550 if( p_mux->b_waiting_stream )
552 if( p_mux->i_add_stream_start > 0 &&
553 p_mux->i_add_stream_start + (mtime_t)1500000 < mdate() )
555 /* more than 1.5 second, start muxing */
556 p_mux->b_waiting_stream = VLC_FALSE;
563 p_mux->pf_mux( p_mux );
566 /*****************************************************************************
568 *****************************************************************************/
569 static int mrl_Parse( mrl_t *p_mrl, char *psz_mrl )
571 char * psz_dup = strdup( psz_mrl );
572 char * psz_parser = psz_dup;
573 char * psz_access = "";
575 char * psz_name = "";
577 /* *** first parse psz_dest */
578 while( *psz_parser && *psz_parser != ':' )
580 if( *psz_parser == '{' )
582 while( *psz_parser && *psz_parser != '}' )
596 #if defined( WIN32 ) || defined( UNDER_CE )
597 if( psz_parser - psz_dup == 1 )
599 /* msg_Warn( p_sout, "drive letter %c: found in source string",
607 psz_access = psz_way = "";
612 *psz_parser++ = '\0';
614 /* let's skip '//' */
615 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
620 psz_name = psz_parser ;
622 /* Come back to parse the access and mux plug-ins */
623 psz_parser = psz_dup;
630 else if( *psz_parser == '/' )
638 psz_access = psz_parser;
640 while( *psz_parser && *psz_parser != '/' )
642 if( *psz_parser == '{' )
644 while( *psz_parser && *psz_parser != '}' )
659 if( *psz_parser == '/' )
661 *psz_parser++ = '\0';
672 psz_way = psz_parser;
676 p_mrl->psz_access = strdup( psz_access );
677 p_mrl->psz_way = strdup( psz_way );
678 p_mrl->psz_name = strdup( psz_name );
681 return( VLC_SUCCESS );
685 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
686 static void mrl_Clean( mrl_t *p_mrl )
688 FREE( p_mrl->psz_access );
689 FREE( p_mrl->psz_way );
690 FREE( p_mrl->psz_name );
694 /****************************************************************************
695 ****************************************************************************
699 ****************************************************************************
700 ****************************************************************************/
702 /* create a complete chain */
704 module{option=*:option=*}[:module{option=*:...}]
708 * parse module{options=str, option="str "}:
709 * return a pointer on the rest
710 * XXX: psz_chain is modified
712 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
713 /* go accross " " and { } */
714 static char *_get_chain_end( char *str )
722 if( *p == '{' || *p == '"' || *p == '\'')
748 else if( *p == '{' && c == '}' )
750 p = _get_chain_end( p );
758 else if( *p == '\0' || *p == ',' || *p == '}' ||
759 *p == ' ' || *p == '\t' )
770 char *sout_cfg_parser( char **ppsz_name, sout_cfg_t **pp_cfg, char *psz_chain )
772 sout_cfg_t *p_cfg = NULL;
778 if( !p ) return NULL;
782 while( *p && *p != '{' && *p != ':' && *p != ' ' && *p != '\t' ) p++;
784 if( p == psz_chain ) return NULL;
786 *ppsz_name = strndup( psz_chain, p - psz_chain );
804 while( *p && *p != '=' && *p != ',' && *p != '{' && *p != '}' &&
805 *p != ' ' && *p != '\t' ) p++;
807 /* fprintf( stderr, "name=%s - rest=%s\n", psz_name, p ); */
810 fprintf( stderr, "invalid options (empty)" );
814 cfg.psz_name = strndup( psz_name, p - psz_name );
818 if( *p == '=' || *p == '{' )
821 vlc_bool_t b_keep_brackets = (*p == '{');
825 end = _get_chain_end( p );
828 cfg.psz_value = NULL;
832 if( *p == '\'' || *p =='"' ||
833 ( !b_keep_brackets && *p == '{' ) )
838 if( end <= p ) cfg.psz_value = NULL;
839 else cfg.psz_value = strndup( p, end - p );
847 cfg.psz_value = NULL;
853 p_cfg->p_next = malloc( sizeof( sout_cfg_t ) );
854 memcpy( p_cfg->p_next, &cfg, sizeof( sout_cfg_t ) );
856 p_cfg = p_cfg->p_next;
860 p_cfg = malloc( sizeof( sout_cfg_t ) );
861 memcpy( p_cfg, &cfg, sizeof( sout_cfg_t ) );
876 if( *p == ':' ) return( strdup( p + 1 ) );
881 static void sout_cfg_free( sout_cfg_t *p_cfg )
883 while( p_cfg != NULL )
887 p_next = p_cfg->p_next;
889 FREE( p_cfg->psz_name );
890 FREE( p_cfg->psz_value );
897 void __sout_ParseCfg( vlc_object_t *p_this, char *psz_prefix,
898 const char **ppsz_options, sout_cfg_t *cfg )
904 /* First, var_Create all variables */
905 for( i = 0; ppsz_options[i] != NULL; i++ )
907 asprintf( &psz_name, "%s%s", psz_prefix, ppsz_options[i] );
909 i_type = config_GetType( p_this, psz_name );
911 var_Create( p_this, psz_name, i_type | VLC_VAR_DOINHERIT );
915 /* Now parse options and set value */
916 if( psz_prefix == NULL ) psz_prefix = "";
921 vlc_bool_t b_yes = VLC_TRUE;
923 if( cfg->psz_name == NULL || *cfg->psz_name == '\0' )
928 for( i = 0; ppsz_options[i] != NULL; i++ )
930 if( !strcmp( ppsz_options[i], cfg->psz_name ) )
934 if( ( !strncmp( cfg->psz_name, "no-", 3 ) &&
935 !strcmp( ppsz_options[i], cfg->psz_name + 3 ) ) ||
936 ( !strncmp( cfg->psz_name, "no", 2 ) &&
937 !strcmp( ppsz_options[i], cfg->psz_name + 2 ) ) )
943 if( ppsz_options[i] == NULL )
945 msg_Warn( p_this, "option %s is unknown", cfg->psz_name );
951 asprintf( &psz_name, "%s%s", psz_prefix, ppsz_options[i] );
953 /* get the type of the variable */
954 i_type = config_GetType( p_this, psz_name );
957 msg_Warn( p_this, "unknown option %s (value=%s)",
958 cfg->psz_name, cfg->psz_value );
961 if( i_type != VLC_VAR_BOOL && cfg->psz_value == NULL )
963 msg_Warn( p_this, "missing value for option %s", cfg->psz_name );
972 case VLC_VAR_INTEGER:
973 val.i_int = strtol( cfg->psz_value ? cfg->psz_value : "0",
977 val.f_float = atof( cfg->psz_value ? cfg->psz_value : "0" );
980 val.psz_string = cfg->psz_value;
983 msg_Warn( p_this, "unhandled config var type" );
984 memset( &val, 0, sizeof( vlc_value_t ) );
987 var_Set( p_this, psz_name, val );
988 msg_Dbg( p_this, "set sout option: %s to %s", psz_name, cfg->psz_value );
998 * XXX name and p_cfg are used (-> do NOT free them)
1000 sout_stream_t *sout_stream_new( sout_instance_t *p_sout, char *psz_chain )
1002 sout_stream_t *p_stream;
1006 msg_Err( p_sout, "invalid chain" );
1010 p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
1014 msg_Err( p_sout, "out of memory" );
1018 p_stream->p_sout = p_sout;
1019 p_stream->p_sys = NULL;
1021 p_stream->psz_next =
1022 sout_cfg_parser( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
1024 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
1026 vlc_object_attach( p_stream, p_sout );
1028 p_stream->p_module =
1029 module_Need( p_stream, "sout stream", p_stream->psz_name, VLC_TRUE );
1031 if( !p_stream->p_module )
1033 sout_stream_delete( p_stream );
1040 void sout_stream_delete( sout_stream_t *p_stream )
1042 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
1044 vlc_object_detach( p_stream );
1045 if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
1047 FREE( p_stream->psz_name );
1048 FREE( p_stream->psz_next );
1050 sout_cfg_free( p_stream->p_cfg );
1052 msg_Dbg( p_stream, "destroying chain done" );
1053 vlc_object_destroy( p_stream );
1056 static char *_sout_stream_url_to_chain( vlc_object_t *p_this, char *psz_url )
1059 char *psz_chain, *p;
1061 mrl_Parse( &mrl, psz_url );
1062 p = psz_chain = malloc( 500 + strlen( mrl.psz_way ) +
1063 strlen( mrl.psz_access ) +
1064 strlen( mrl.psz_name ) );
1067 if( config_GetInt( p_this, "sout-display" ) )
1069 p += sprintf( p, "duplicate{dst=display,dst=std{mux=\"%s\","
1070 "access=\"%s\",url=\"%s\"}}",
1071 mrl.psz_way, mrl.psz_access, mrl.psz_name );
1075 p += sprintf( p, "std{mux=\"%s\",access=\"%s\",url=\"%s\"}",
1076 mrl.psz_way, mrl.psz_access, mrl.psz_name );
1080 return( psz_chain );