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 );
134 p_sout->p_stream = sout_stream_new( p_sout, p_sout->psz_chain );
136 if( p_sout->p_stream == NULL )
138 msg_Err( p_sout, "stream chained failed for `%s'", p_sout->psz_chain );
140 FREE( p_sout->psz_sout );
141 FREE( p_sout->psz_chain );
143 vlc_object_destroy( p_sout );
147 vlc_object_attach( p_sout, p_parent );
151 /*****************************************************************************
152 * sout_DeleteInstance: delete a previously allocated instance
153 *****************************************************************************/
154 void sout_DeleteInstance( sout_instance_t * p_sout )
157 vlc_object_detach( p_sout );
159 /* remove the stream out chain */
160 sout_stream_delete( p_sout->p_stream );
162 /* *** free all string *** */
163 FREE( p_sout->psz_sout );
164 FREE( p_sout->psz_chain );
169 vlc_meta_Delete( p_sout->p_meta );
172 vlc_mutex_destroy( &p_sout->lock );
174 /* *** free structure *** */
175 vlc_object_destroy( p_sout );
178 /*****************************************************************************
180 *****************************************************************************/
181 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
184 sout_packetizer_input_t *p_input;
186 msg_Dbg( p_sout, "adding a new input" );
188 /* *** create a packetizer input *** */
189 p_input = malloc( sizeof( sout_packetizer_input_t ) );
190 p_input->p_sout = p_sout;
191 p_input->p_fmt = p_fmt;
193 if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
195 vlc_object_release( p_sout );
199 /* *** add it to the stream chain */
200 vlc_mutex_lock( &p_sout->lock );
201 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
202 vlc_mutex_unlock( &p_sout->lock );
204 if( p_input->id == NULL )
213 /*****************************************************************************
215 *****************************************************************************/
216 int sout_InputDelete( sout_packetizer_input_t *p_input )
218 sout_instance_t *p_sout = p_input->p_sout;
220 msg_Dbg( p_sout, "removing an input" );
222 if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
224 vlc_mutex_lock( &p_sout->lock );
225 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
226 vlc_mutex_unlock( &p_sout->lock );
231 return( VLC_SUCCESS);
234 /*****************************************************************************
236 *****************************************************************************/
237 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
240 sout_instance_t *p_sout = p_input->p_sout;
243 if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
245 block_Release( p_buffer );
248 if( p_buffer->i_dts <= 0 )
250 msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
251 block_Release( p_buffer );
255 vlc_mutex_lock( &p_sout->lock );
256 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
257 p_input->id, p_buffer );
258 vlc_mutex_unlock( &p_sout->lock );
263 /*****************************************************************************
264 * sout_AccessOutNew: allocate a new access out
265 *****************************************************************************/
266 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
267 char *psz_access, char *psz_name )
269 sout_access_out_t *p_access;
272 if( !( p_access = vlc_object_create( p_sout,
273 sizeof( sout_access_out_t ) ) ) )
275 msg_Err( p_sout, "out of memory" );
279 psz_next = sout_cfg_parser( &p_access->psz_access, &p_access->p_cfg,
285 p_access->psz_name = strdup( psz_name ? psz_name : "" );
286 p_access->p_sout = p_sout;
287 p_access->p_sys = NULL;
288 p_access->pf_seek = NULL;
289 p_access->pf_read = NULL;
290 p_access->pf_write = NULL;
293 module_Need( p_access, "sout access", p_access->psz_access, VLC_TRUE );
295 if( !p_access->p_module )
297 free( p_access->psz_access );
298 free( p_access->psz_name );
299 vlc_object_destroy( p_access );
305 /*****************************************************************************
306 * sout_AccessDelete: delete an access out
307 *****************************************************************************/
308 void sout_AccessOutDelete( sout_access_out_t *p_access )
310 if( p_access->p_module )
312 module_Unneed( p_access, p_access->p_module );
314 free( p_access->psz_access );
316 sout_cfg_free( p_access->p_cfg );
318 free( p_access->psz_name );
320 vlc_object_destroy( p_access );
323 /*****************************************************************************
325 *****************************************************************************/
326 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
328 return p_access->pf_seek( p_access, i_pos );
331 /*****************************************************************************
333 *****************************************************************************/
334 int sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
336 return( p_access->pf_read ?
337 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
340 /*****************************************************************************
342 *****************************************************************************/
343 int sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
345 return p_access->pf_write( p_access, p_buffer );
348 /*****************************************************************************
349 * sout_MuxNew: create a new mux
350 *****************************************************************************/
351 sout_mux_t * sout_MuxNew ( sout_instance_t *p_sout,
353 sout_access_out_t *p_access )
358 p_mux = vlc_object_create( p_sout,
359 sizeof( sout_mux_t ) );
362 msg_Err( p_sout, "out of memory" );
366 p_mux->p_sout = p_sout;
367 psz_next = sout_cfg_parser( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
372 p_mux->p_access = p_access;
373 p_mux->pf_capacity = NULL;
374 p_mux->pf_addstream = NULL;
375 p_mux->pf_delstream = NULL;
376 p_mux->pf_mux = NULL;
377 p_mux->i_nb_inputs = 0;
378 p_mux->pp_inputs = NULL;
383 module_Need( p_mux, "sout mux", p_mux->psz_mux, VLC_TRUE );
385 if( p_mux->p_module == NULL )
387 FREE( p_mux->psz_mux );
389 vlc_object_destroy( p_mux );
393 /* *** probe mux capacity *** */
394 if( p_mux->pf_capacity )
397 if( p_mux->pf_capacity( p_mux,
398 SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME, NULL,
399 (void*)&b_answer ) != SOUT_MUX_CAP_ERR_OK )
401 b_answer = VLC_FALSE;
405 msg_Dbg( p_sout, "muxer support adding stream at any time" );
406 p_mux->b_add_stream_any_time = VLC_TRUE;
407 p_mux->b_waiting_stream = VLC_FALSE;
409 if( p_mux->pf_capacity( p_mux,
410 SOUT_MUX_CAP_GET_ADD_STREAM_WAIT, NULL,
411 (void*)&b_answer ) != SOUT_MUX_CAP_ERR_OK )
413 b_answer = VLC_FALSE;
417 msg_Dbg( p_sout, "muxer prefers waiting for all ES before "
419 p_mux->b_waiting_stream = VLC_TRUE;
424 p_mux->b_add_stream_any_time = VLC_FALSE;
425 p_mux->b_waiting_stream = VLC_TRUE;
430 p_mux->b_add_stream_any_time = VLC_FALSE;
431 p_mux->b_waiting_stream = VLC_TRUE;
433 p_mux->i_add_stream_start = -1;
438 /*****************************************************************************
440 *****************************************************************************/
441 void sout_MuxDelete( sout_mux_t *p_mux )
443 if( p_mux->p_module )
445 module_Unneed( p_mux, p_mux->p_module );
447 free( p_mux->psz_mux );
449 sout_cfg_free( p_mux->p_cfg );
451 vlc_object_destroy( p_mux );
454 /*****************************************************************************
456 *****************************************************************************/
457 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
459 sout_input_t *p_input;
461 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
463 msg_Err( p_mux, "cannot add a new stream (unsuported while muxing "
464 "for this format)" );
467 if( p_mux->i_add_stream_start < 0 )
469 /* we wait for one second */
470 p_mux->i_add_stream_start = mdate();
473 msg_Dbg( p_mux, "adding a new input" );
475 /* create a new sout input */
476 p_input = malloc( sizeof( sout_input_t ) );
477 p_input->p_sout = p_mux->p_sout;
478 p_input->p_fmt = p_fmt;
479 p_input->p_fifo = block_FifoNew( p_mux->p_sout );
480 p_input->p_sys = NULL;
482 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
483 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
485 msg_Err( p_mux, "cannot add this stream" );
486 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
487 block_FifoRelease( p_input->p_fifo );
495 /*****************************************************************************
496 * sout_MuxDeleteStream:
497 *****************************************************************************/
498 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
502 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
505 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
507 msg_Err( p_mux, "cannot del this stream from mux" );
510 /* remove the entry */
511 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
513 if( p_mux->i_nb_inputs == 0 )
515 msg_Warn( p_mux, "no more input stream for this mux" );
518 block_FifoRelease( p_input->p_fifo );
523 /*****************************************************************************
524 * sout_MuxSendBuffer:
525 *****************************************************************************/
526 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
529 block_FifoPut( p_input->p_fifo, p_buffer );
531 if( p_mux->b_waiting_stream )
533 if( p_mux->i_add_stream_start > 0 &&
534 p_mux->i_add_stream_start + (mtime_t)1500000 < mdate() )
536 /* more than 1.5 second, start muxing */
537 p_mux->b_waiting_stream = VLC_FALSE;
544 p_mux->pf_mux( p_mux );
547 /*****************************************************************************
549 *****************************************************************************/
550 static int mrl_Parse( mrl_t *p_mrl, char *psz_mrl )
552 char * psz_dup = strdup( psz_mrl );
553 char * psz_parser = psz_dup;
554 char * psz_access = "";
556 char * psz_name = "";
558 /* *** first parse psz_dest */
559 while( *psz_parser && *psz_parser != ':' )
561 if( *psz_parser == '{' )
563 while( *psz_parser && *psz_parser != '}' )
577 #if defined( WIN32 ) || defined( UNDER_CE )
578 if( psz_parser - psz_dup == 1 )
580 /* msg_Warn( p_sout, "drive letter %c: found in source string",
588 psz_access = psz_way = "";
593 *psz_parser++ = '\0';
595 /* let's skip '//' */
596 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
601 psz_name = psz_parser ;
603 /* Come back to parse the access and mux plug-ins */
604 psz_parser = psz_dup;
611 else if( *psz_parser == '/' )
619 psz_access = psz_parser;
621 while( *psz_parser && *psz_parser != '/' )
623 if( *psz_parser == '{' )
625 while( *psz_parser && *psz_parser != '}' )
640 if( *psz_parser == '/' )
642 *psz_parser++ = '\0';
653 psz_way = psz_parser;
657 p_mrl->psz_access = strdup( psz_access );
658 p_mrl->psz_way = strdup( psz_way );
659 p_mrl->psz_name = strdup( psz_name );
662 return( VLC_SUCCESS );
666 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
667 static void mrl_Clean( mrl_t *p_mrl )
669 FREE( p_mrl->psz_access );
670 FREE( p_mrl->psz_way );
671 FREE( p_mrl->psz_name );
675 /****************************************************************************
676 ****************************************************************************
680 ****************************************************************************
681 ****************************************************************************/
683 /* create a complete chain */
685 module{option=*:option=*}[:module{option=*:...}]
688 static char *_strndup( char *str, int i_len )
692 p = malloc( i_len + 1 );
693 strncpy( p, str, i_len );
700 * parse module{options=str, option="str "}:
701 * return a pointer on the rest
702 * XXX: psz_chain is modified
704 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
705 /* go accross " " and { } */
706 static char *_get_chain_end( char *str )
714 if( *p == '{' || *p == '"' || *p == '\'')
740 else if( *p == '{' && c == '}' )
742 p = _get_chain_end( p );
750 else if( *p == '\0' || *p == ',' || *p == '}' || *p == ' ' || *p == '\t' )
761 char * sout_cfg_parser( char **ppsz_name, sout_cfg_t **pp_cfg, char *psz_chain )
763 sout_cfg_t *p_cfg = NULL;
776 while( *p && *p != '{' && *p != ':' && *p != ' ' && *p != '\t' )
786 *ppsz_name = _strndup( psz_chain, p - psz_chain );
804 while( *p && *p != '=' && *p != ',' && *p != '}' && *p != ' ' && *p != '\t' )
809 /* fprintf( stderr, "name=%s - rest=%s\n", psz_name, p ); */
812 fprintf( stderr, "invalid options (empty)" );
816 cfg.psz_name = _strndup( psz_name, p - psz_name );
826 end = _get_chain_end( p );
829 cfg.psz_value = NULL;
833 if( *p == '\'' || *p =='"' || *p == '{' )
840 cfg.psz_value = NULL;
844 cfg.psz_value = _strndup( p, end - p );
853 cfg.psz_value = NULL;
859 p_cfg->p_next = malloc( sizeof( sout_cfg_t ) );
860 memcpy( p_cfg->p_next, &cfg, sizeof( sout_cfg_t ) );
862 p_cfg = p_cfg->p_next;
866 p_cfg = malloc( sizeof( sout_cfg_t ) );
867 memcpy( p_cfg, &cfg, sizeof( sout_cfg_t ) );
888 return( strdup( p + 1 ) );
894 static void sout_cfg_free( sout_cfg_t *p_cfg )
896 while( p_cfg != NULL )
900 p_next = p_cfg->p_next;
902 FREE( p_cfg->psz_name );
903 FREE( p_cfg->psz_value );
912 * XXX name and p_cfg are used (-> do NOT free them)
914 sout_stream_t *sout_stream_new( sout_instance_t *p_sout,
917 sout_stream_t *p_stream;
921 msg_Err( p_sout, "invalid chain" );
925 p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
929 msg_Err( p_sout, "out of memory" );
933 p_stream->p_sout = p_sout;
934 p_stream->p_sys = NULL;
937 sout_cfg_parser( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
939 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
942 module_Need( p_stream, "sout stream", p_stream->psz_name, VLC_TRUE );
944 if( !p_stream->p_module )
946 sout_stream_delete( p_stream );
953 void sout_stream_delete( sout_stream_t *p_stream )
955 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
956 if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
958 FREE( p_stream->psz_name );
959 FREE( p_stream->psz_next );
961 sout_cfg_free( p_stream->p_cfg );
963 msg_Dbg( p_stream, "destroying chain done" );
964 vlc_object_destroy( p_stream );
967 static char *_sout_stream_url_to_chain( vlc_object_t *p_this, char *psz_url )
972 mrl_Parse( &mrl, psz_url );
973 p = psz_chain = malloc( 500 + strlen( mrl.psz_way ) +
974 strlen( mrl.psz_access ) +
975 strlen( mrl.psz_name ) );
978 if( config_GetInt( p_this, "sout-display" ) )
980 p += sprintf( p, "duplicate{dst=display,dst=std{mux=\"%s\",access=\"%s\",url=\"%s\"}}",
981 mrl.psz_way, mrl.psz_access, mrl.psz_name );
985 p += sprintf( p, "std{mux=\"%s\",access=\"%s\",url=\"%s\"}",
986 mrl.psz_way, mrl.psz_access, mrl.psz_name );