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, FIND_ANYWHERE ) ) )
84 if( !strcmp( p_sout->psz_sout, psz_dest ) )
86 msg_Warn( p_parent, "sout keep : reusing sout" );
87 msg_Warn( p_parent, "sout keep : you probably want to use gather stream_out" );
88 vlc_object_detach( p_sout );
89 vlc_object_attach( p_sout, p_parent );
90 vlc_object_release( p_sout );
95 msg_Warn( p_parent, "sout keep : destroying unusable sout" );
96 sout_DeleteInstance( p_sout );
100 else if( !keep.b_bool )
102 while( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT, FIND_PARENT ) ) )
104 msg_Warn( p_parent, "sout keep : destroying old sout" );
105 sout_DeleteInstance( p_sout );
109 /* *** Allocate descriptor *** */
110 p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
113 msg_Err( p_parent, "out of memory" );
117 /* *** init descriptor *** */
118 p_sout->psz_sout = strdup( psz_dest );
119 p_sout->p_meta = NULL;
120 p_sout->i_out_pace_nocontrol = 0;
121 p_sout->p_sys = NULL;
123 vlc_mutex_init( p_sout, &p_sout->lock );
124 if( psz_dest && psz_dest[0] == '#' )
126 p_sout->psz_chain = strdup( &psz_dest[1] );
130 p_sout->psz_chain = sout_stream_url_to_chain( p_sout, psz_dest );
131 msg_Dbg( p_sout, "using sout chain=`%s'", p_sout->psz_chain );
133 p_sout->p_stream = NULL;
135 /* attach it for inherit */
136 vlc_object_attach( p_sout, p_parent );
138 p_sout->p_stream = sout_stream_new( p_sout, p_sout->psz_chain );
140 if( p_sout->p_stream == NULL )
142 msg_Err( p_sout, "stream chained failed for `%s'", p_sout->psz_chain );
144 FREE( p_sout->psz_sout );
145 FREE( p_sout->psz_chain );
147 vlc_object_destroy( p_sout );
153 /*****************************************************************************
154 * sout_DeleteInstance: delete a previously allocated instance
155 *****************************************************************************/
156 void sout_DeleteInstance( sout_instance_t * p_sout )
159 vlc_object_detach( p_sout );
161 /* remove the stream out chain */
162 sout_stream_delete( p_sout->p_stream );
164 /* *** free all string *** */
165 FREE( p_sout->psz_sout );
166 FREE( p_sout->psz_chain );
171 vlc_meta_Delete( p_sout->p_meta );
174 vlc_mutex_destroy( &p_sout->lock );
176 /* *** free structure *** */
177 vlc_object_destroy( p_sout );
180 /*****************************************************************************
182 *****************************************************************************/
183 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
186 sout_packetizer_input_t *p_input;
188 msg_Dbg( p_sout, "adding a new input" );
190 /* *** create a packetizer input *** */
191 p_input = malloc( sizeof( sout_packetizer_input_t ) );
192 p_input->p_sout = p_sout;
193 p_input->p_fmt = p_fmt;
195 if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
197 vlc_object_release( p_sout );
201 /* *** add it to the stream chain */
202 vlc_mutex_lock( &p_sout->lock );
203 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
204 vlc_mutex_unlock( &p_sout->lock );
206 if( p_input->id == NULL )
215 /*****************************************************************************
217 *****************************************************************************/
218 int sout_InputDelete( sout_packetizer_input_t *p_input )
220 sout_instance_t *p_sout = p_input->p_sout;
222 msg_Dbg( p_sout, "removing an input" );
224 if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
226 vlc_mutex_lock( &p_sout->lock );
227 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
228 vlc_mutex_unlock( &p_sout->lock );
233 return( VLC_SUCCESS);
236 /*****************************************************************************
238 *****************************************************************************/
239 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
242 sout_instance_t *p_sout = p_input->p_sout;
245 if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
247 block_Release( p_buffer );
250 if( p_buffer->i_dts <= 0 )
252 msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
253 block_Release( p_buffer );
257 vlc_mutex_lock( &p_sout->lock );
258 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
259 p_input->id, p_buffer );
260 vlc_mutex_unlock( &p_sout->lock );
265 /*****************************************************************************
266 * sout_AccessOutNew: allocate a new access out
267 *****************************************************************************/
268 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
269 char *psz_access, char *psz_name )
271 sout_access_out_t *p_access;
274 if( !( p_access = vlc_object_create( p_sout,
275 sizeof( sout_access_out_t ) ) ) )
277 msg_Err( p_sout, "out of memory" );
281 psz_next = sout_cfg_parser( &p_access->psz_access, &p_access->p_cfg,
287 p_access->psz_name = strdup( psz_name ? psz_name : "" );
288 p_access->p_sout = p_sout;
289 p_access->p_sys = NULL;
290 p_access->pf_seek = NULL;
291 p_access->pf_read = NULL;
292 p_access->pf_write = NULL;
293 p_access->p_module = NULL;
294 vlc_object_attach( p_access, p_sout );
297 module_Need( p_access, "sout access", p_access->psz_access, VLC_TRUE );
299 if( !p_access->p_module )
301 free( p_access->psz_access );
302 free( p_access->psz_name );
303 vlc_object_destroy( p_access );
309 /*****************************************************************************
310 * sout_AccessDelete: delete an access out
311 *****************************************************************************/
312 void sout_AccessOutDelete( sout_access_out_t *p_access )
314 vlc_object_detach( p_access );
315 if( p_access->p_module )
317 module_Unneed( p_access, p_access->p_module );
319 free( p_access->psz_access );
321 sout_cfg_free( p_access->p_cfg );
323 free( p_access->psz_name );
325 vlc_object_destroy( p_access );
328 /*****************************************************************************
330 *****************************************************************************/
331 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
333 return p_access->pf_seek( p_access, i_pos );
336 /*****************************************************************************
338 *****************************************************************************/
339 int sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
341 return( p_access->pf_read ?
342 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
345 /*****************************************************************************
347 *****************************************************************************/
348 int sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
350 return p_access->pf_write( p_access, p_buffer );
353 /*****************************************************************************
354 * sout_MuxNew: create a new mux
355 *****************************************************************************/
356 sout_mux_t * sout_MuxNew ( sout_instance_t *p_sout,
358 sout_access_out_t *p_access )
363 p_mux = vlc_object_create( p_sout,
364 sizeof( sout_mux_t ) );
367 msg_Err( p_sout, "out of memory" );
371 p_mux->p_sout = p_sout;
372 psz_next = sout_cfg_parser( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
377 p_mux->p_access = p_access;
378 p_mux->pf_capacity = NULL;
379 p_mux->pf_addstream = NULL;
380 p_mux->pf_delstream = NULL;
381 p_mux->pf_mux = NULL;
382 p_mux->i_nb_inputs = 0;
383 p_mux->pp_inputs = NULL;
386 p_mux->p_module = NULL;
388 vlc_object_attach( p_mux, p_sout );
391 module_Need( p_mux, "sout mux", p_mux->psz_mux, VLC_TRUE );
393 if( p_mux->p_module == NULL )
395 FREE( p_mux->psz_mux );
397 vlc_object_destroy( p_mux );
401 /* *** probe mux capacity *** */
402 if( p_mux->pf_capacity )
405 if( p_mux->pf_capacity( p_mux,
406 SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME, NULL,
407 (void*)&b_answer ) != SOUT_MUX_CAP_ERR_OK )
409 b_answer = VLC_FALSE;
413 msg_Dbg( p_sout, "muxer support adding stream at any time" );
414 p_mux->b_add_stream_any_time = VLC_TRUE;
415 p_mux->b_waiting_stream = VLC_FALSE;
417 if( p_mux->pf_capacity( p_mux,
418 SOUT_MUX_CAP_GET_ADD_STREAM_WAIT, NULL,
419 (void*)&b_answer ) != SOUT_MUX_CAP_ERR_OK )
421 b_answer = VLC_FALSE;
425 msg_Dbg( p_sout, "muxer prefers waiting for all ES before "
427 p_mux->b_waiting_stream = VLC_TRUE;
432 p_mux->b_add_stream_any_time = VLC_FALSE;
433 p_mux->b_waiting_stream = VLC_TRUE;
438 p_mux->b_add_stream_any_time = VLC_FALSE;
439 p_mux->b_waiting_stream = VLC_TRUE;
441 p_mux->i_add_stream_start = -1;
446 /*****************************************************************************
448 *****************************************************************************/
449 void sout_MuxDelete( sout_mux_t *p_mux )
451 vlc_object_detach( p_mux );
452 if( p_mux->p_module )
454 module_Unneed( p_mux, p_mux->p_module );
456 free( p_mux->psz_mux );
458 sout_cfg_free( p_mux->p_cfg );
460 vlc_object_destroy( p_mux );
463 /*****************************************************************************
465 *****************************************************************************/
466 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
468 sout_input_t *p_input;
470 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
472 msg_Err( p_mux, "cannot add a new stream (unsuported while muxing "
473 "for this format)" );
476 if( p_mux->i_add_stream_start < 0 )
478 /* we wait for one second */
479 p_mux->i_add_stream_start = mdate();
482 msg_Dbg( p_mux, "adding a new input" );
484 /* create a new sout input */
485 p_input = malloc( sizeof( sout_input_t ) );
486 p_input->p_sout = p_mux->p_sout;
487 p_input->p_fmt = p_fmt;
488 p_input->p_fifo = block_FifoNew( p_mux->p_sout );
489 p_input->p_sys = NULL;
491 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
492 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
494 msg_Err( p_mux, "cannot add this stream" );
495 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
496 block_FifoRelease( p_input->p_fifo );
504 /*****************************************************************************
505 * sout_MuxDeleteStream:
506 *****************************************************************************/
507 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
511 if( p_mux->b_waiting_stream && p_input->p_fifo->i_depth > 0 )
513 /* We stop waiting, and call the muxer for taking care of the data
514 * before we remove this es */
515 p_mux->b_waiting_stream = VLC_FALSE;
516 p_mux->pf_mux( p_mux );
519 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
522 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
524 msg_Err( p_mux, "cannot del this stream from mux" );
527 /* remove the entry */
528 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
530 if( p_mux->i_nb_inputs == 0 )
532 msg_Warn( p_mux, "no more input stream for this mux" );
535 block_FifoRelease( p_input->p_fifo );
540 /*****************************************************************************
541 * sout_MuxSendBuffer:
542 *****************************************************************************/
543 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
546 block_FifoPut( p_input->p_fifo, p_buffer );
548 if( p_mux->b_waiting_stream )
550 if( p_mux->i_add_stream_start > 0 &&
551 p_mux->i_add_stream_start + (mtime_t)1500000 < mdate() )
553 /* more than 1.5 second, start muxing */
554 p_mux->b_waiting_stream = VLC_FALSE;
561 p_mux->pf_mux( p_mux );
564 /*****************************************************************************
566 *****************************************************************************/
567 static int mrl_Parse( mrl_t *p_mrl, char *psz_mrl )
569 char * psz_dup = strdup( psz_mrl );
570 char * psz_parser = psz_dup;
571 char * psz_access = "";
573 char * psz_name = "";
575 /* *** first parse psz_dest */
576 while( *psz_parser && *psz_parser != ':' )
578 if( *psz_parser == '{' )
580 while( *psz_parser && *psz_parser != '}' )
594 #if defined( WIN32 ) || defined( UNDER_CE )
595 if( psz_parser - psz_dup == 1 )
597 /* msg_Warn( p_sout, "drive letter %c: found in source string",
605 psz_access = psz_way = "";
610 *psz_parser++ = '\0';
612 /* let's skip '//' */
613 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
618 psz_name = psz_parser ;
620 /* Come back to parse the access and mux plug-ins */
621 psz_parser = psz_dup;
628 else if( *psz_parser == '/' )
636 psz_access = psz_parser;
638 while( *psz_parser && *psz_parser != '/' )
640 if( *psz_parser == '{' )
642 while( *psz_parser && *psz_parser != '}' )
657 if( *psz_parser == '/' )
659 *psz_parser++ = '\0';
670 psz_way = psz_parser;
674 p_mrl->psz_access = strdup( psz_access );
675 p_mrl->psz_way = strdup( psz_way );
676 p_mrl->psz_name = strdup( psz_name );
679 return( VLC_SUCCESS );
683 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
684 static void mrl_Clean( mrl_t *p_mrl )
686 FREE( p_mrl->psz_access );
687 FREE( p_mrl->psz_way );
688 FREE( p_mrl->psz_name );
692 /****************************************************************************
693 ****************************************************************************
697 ****************************************************************************
698 ****************************************************************************/
700 /* create a complete chain */
702 module{option=*:option=*}[:module{option=*:...}]
705 static char *_strndup( char *str, int i_len )
709 p = malloc( i_len + 1 );
710 strncpy( p, str, i_len );
717 * parse module{options=str, option="str "}:
718 * return a pointer on the rest
719 * XXX: psz_chain is modified
721 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
722 /* go accross " " and { } */
723 static char *_get_chain_end( char *str )
731 if( *p == '{' || *p == '"' || *p == '\'')
757 else if( *p == '{' && c == '}' )
759 p = _get_chain_end( p );
767 else if( *p == '\0' || *p == ',' || *p == '}' || *p == ' ' || *p == '\t' )
778 char * sout_cfg_parser( char **ppsz_name, sout_cfg_t **pp_cfg, char *psz_chain )
780 sout_cfg_t *p_cfg = NULL;
793 while( *p && *p != '{' && *p != ':' && *p != ' ' && *p != '\t' )
803 *ppsz_name = _strndup( psz_chain, p - psz_chain );
821 while( *p && *p != '=' && *p != ',' && *p != '}' && *p != ' ' && *p != '\t' )
826 /* fprintf( stderr, "name=%s - rest=%s\n", psz_name, p ); */
829 fprintf( stderr, "invalid options (empty)" );
833 cfg.psz_name = _strndup( psz_name, p - psz_name );
843 end = _get_chain_end( p );
846 cfg.psz_value = NULL;
850 if( *p == '\'' || *p =='"' || *p == '{' )
857 cfg.psz_value = NULL;
861 cfg.psz_value = _strndup( p, end - p );
870 cfg.psz_value = NULL;
876 p_cfg->p_next = malloc( sizeof( sout_cfg_t ) );
877 memcpy( p_cfg->p_next, &cfg, sizeof( sout_cfg_t ) );
879 p_cfg = p_cfg->p_next;
883 p_cfg = malloc( sizeof( sout_cfg_t ) );
884 memcpy( p_cfg, &cfg, sizeof( sout_cfg_t ) );
905 return( strdup( p + 1 ) );
911 static void sout_cfg_free( sout_cfg_t *p_cfg )
913 while( p_cfg != NULL )
917 p_next = p_cfg->p_next;
919 FREE( p_cfg->psz_name );
920 FREE( p_cfg->psz_value );
927 void __sout_ParseCfg( vlc_object_t *p_this, char *psz_prefix, const char **ppsz_options, sout_cfg_t *cfg )
933 /* First, var_Create all variables */
934 for( i = 0; ppsz_options[i] != NULL; i++ )
936 asprintf( &psz_name, "%s%s", psz_prefix, ppsz_options[i] );
938 i_type = config_GetType( p_this, psz_name );
940 var_Create( p_this, psz_name, i_type | VLC_VAR_DOINHERIT );
944 /* Now parse options and set value */
945 if( psz_prefix == NULL ) psz_prefix = "";
951 if( cfg->psz_name == NULL || *cfg->psz_name == '\0' )
956 for( i = 0; ppsz_options[i] != NULL; i++ )
958 if( !strcmp( ppsz_options[i], cfg->psz_name ) )
963 if( ppsz_options[i] == NULL )
970 asprintf( &psz_name, "%s%s", psz_prefix, cfg->psz_name );
972 /* get the type of the variable */
973 i_type = config_GetType( p_this, psz_name );
976 /* TODO check for no, no- */
981 msg_Warn( p_this, "unknown option %s (value=%s)", cfg->psz_name, cfg->psz_value );
984 if( i_type != VLC_VAR_BOOL && cfg->psz_value == NULL )
986 msg_Warn( p_this, "missing value for option %s", cfg->psz_name );
992 case VLC_VAR_INTEGER:
993 val.i_int = atoi( cfg->psz_value ? cfg->psz_value : "0" );
996 val.f_float = atof( cfg->psz_value ? cfg->psz_value : "0" );
999 val.psz_string = cfg->psz_value;
1004 msg_Warn( p_this, "unhandled config var type" );
1005 memset( &val, 0, sizeof( vlc_value_t ) );
1008 var_Set( p_this, psz_name, val );
1009 msg_Dbg( p_this, "set sout option: %s to %s", psz_name, cfg->psz_value );
1019 * XXX name and p_cfg are used (-> do NOT free them)
1021 sout_stream_t *sout_stream_new( sout_instance_t *p_sout,
1024 sout_stream_t *p_stream;
1028 msg_Err( p_sout, "invalid chain" );
1032 p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
1036 msg_Err( p_sout, "out of memory" );
1040 p_stream->p_sout = p_sout;
1041 p_stream->p_sys = NULL;
1043 p_stream->psz_next =
1044 sout_cfg_parser( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
1046 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
1048 vlc_object_attach( p_stream, p_sout );
1050 p_stream->p_module =
1051 module_Need( p_stream, "sout stream", p_stream->psz_name, VLC_TRUE );
1053 if( !p_stream->p_module )
1055 sout_stream_delete( p_stream );
1062 void sout_stream_delete( sout_stream_t *p_stream )
1064 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
1066 vlc_object_detach( p_stream );
1067 if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
1069 FREE( p_stream->psz_name );
1070 FREE( p_stream->psz_next );
1072 sout_cfg_free( p_stream->p_cfg );
1074 msg_Dbg( p_stream, "destroying chain done" );
1075 vlc_object_destroy( p_stream );
1078 static char *_sout_stream_url_to_chain( vlc_object_t *p_this, char *psz_url )
1081 char *psz_chain, *p;
1083 mrl_Parse( &mrl, psz_url );
1084 p = psz_chain = malloc( 500 + strlen( mrl.psz_way ) +
1085 strlen( mrl.psz_access ) +
1086 strlen( mrl.psz_name ) );
1089 if( config_GetInt( p_this, "sout-display" ) )
1091 p += sprintf( p, "duplicate{dst=display,dst=std{mux=\"%s\",access=\"%s\",url=\"%s\"}}",
1092 mrl.psz_way, mrl.psz_access, mrl.psz_name );
1096 p += sprintf( p, "std{mux=\"%s\",access=\"%s\",url=\"%s\"}",
1097 mrl.psz_way, mrl.psz_access, mrl.psz_name );
1101 return( psz_chain );