1 /*****************************************************************************
2 * stream_output.c : stream output module
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: stream_output.c,v 1.22 2003/03/17 23:42:12 fenrir Exp $
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() */
37 /*****************************************************************************
39 *****************************************************************************/
40 static int InstanceNewOutput ( sout_instance_t *, char * );
41 static int InstanceMuxNew ( sout_instance_t *,
42 char *, char *, char * );
44 static sout_mux_t * MuxNew ( sout_instance_t*,
45 char *, sout_access_out_t * );
46 static sout_input_t *MuxAddStream ( sout_mux_t *, sout_packet_format_t * );
47 static void MuxDeleteStream ( sout_mux_t *, sout_input_t * );
48 static void MuxDelete ( sout_mux_t * );
53 /* if muxer doesn't support adding stream at any time then we first wait
54 * for stream then we refuse all stream and start muxing */
55 vlc_bool_t b_add_stream_any_time;
56 vlc_bool_t b_waiting_stream;
58 /* we wait one second after first stream added */
59 mtime_t i_add_stream_start;
61 } sout_instance_sys_mux_t;
64 struct sout_instance_sys_t
73 /* <access>{options}/<way>{options}://<name> */
74 typedef struct mrl_option_s
76 struct mrl_option_s *p_next;
85 mrl_option_t *p_access_options;
88 mrl_option_t *p_way_options;
93 /* mrl_Parse: parse psz_mrl and fill p_mrl */
94 static int mrl_Parse( mrl_t *p_mrl, char *psz_mrl );
95 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
96 static void mrl_Clean( mrl_t *p_mrl );
99 #define TAB_APPEND( count, tab, p ) \
102 (tab) = realloc( (tab), sizeof( void ** ) * ( (count) + 1 ) ); \
106 (tab) = malloc( sizeof( void ** ) ); \
108 (void**)(tab)[(count)] = (void*)(p); \
111 #define TAB_FIND( count, tab, p, index ) \
115 for( _i_ = 0; _i_ < (count); _i_++ ) \
117 if((void**)(tab)[_i_]==(void*)(p)) \
125 #define TAB_REMOVE( count, tab, p ) \
128 TAB_FIND( count, tab, p, i_index ); \
133 memmove( ((void**)tab + i_index), \
134 ((void**)tab + i_index+1), \
135 ( (count) - i_index - 1 ) * sizeof( void* ) );\
146 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
148 /*****************************************************************************
149 * sout_NewInstance: creates a new stream output instance
150 *****************************************************************************/
151 sout_instance_t * __sout_NewInstance ( vlc_object_t *p_parent,
154 sout_instance_t *p_sout;
155 char *psz_dup, *psz_parser, *psz_pos;
157 /* Allocate descriptor */
158 p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
161 msg_Err( p_parent, "out of memory" );
165 p_sout->psz_sout = NULL;
167 p_sout->i_nb_dest = 0;
168 p_sout->ppsz_dest = NULL;
170 p_sout->i_preheader = 0;
171 p_sout->i_nb_mux = 0;
174 vlc_mutex_init( p_sout, &p_sout->lock );
175 p_sout->i_nb_inputs = 0;
176 p_sout->pp_inputs = NULL;
178 p_sout->p_sys = malloc( sizeof( sout_instance_sys_t ) );
180 /* now parse psz_sout */
181 psz_dup = strdup( psz_dest );
182 psz_parser = psz_dup;
184 while( ( psz_pos = strchr( psz_parser, '#' ) ) != NULL )
188 if( InstanceNewOutput( p_sout, psz_parser ) )
190 msg_Err( p_sout, "adding `%s' failed", psz_parser );
193 psz_parser = psz_pos;
198 if( InstanceNewOutput( p_sout, psz_parser ) )
200 msg_Err( p_sout, "adding `%s' failed", psz_parser );
206 if( p_sout->i_nb_dest <= 0 )
208 msg_Err( p_sout, "all sout failed" );
209 vlc_object_destroy( p_sout );
213 vlc_object_attach( p_sout, p_parent );
217 /*****************************************************************************
218 * sout_DeleteInstance: delete a previously allocated instance
219 *****************************************************************************/
220 void sout_DeleteInstance( sout_instance_t * p_sout )
224 vlc_object_detach( p_sout );
226 /* *** free all string *** */
227 FREE( p_sout->psz_sout );
229 for( i = 0; i < p_sout->i_nb_dest; i++ )
231 FREE( p_sout->ppsz_dest[i] );
233 FREE( p_sout->ppsz_dest );
235 /* *** there shouldn't be any input ** */
236 if( p_sout->i_nb_inputs > 0 )
238 msg_Err( p_sout, "i_nb_inputs=%d > 0 !!!!!!", p_sout->i_nb_inputs );
239 msg_Err( p_sout, "mmmh I have a bad feeling..." );
241 vlc_mutex_destroy( &p_sout->lock );
243 /* *** remove all muxer *** */
244 for( i = 0; i < p_sout->i_nb_mux; i++ )
246 sout_access_out_t *p_access;
247 #define p_mux p_sout->pp_mux[i]
249 p_access = p_mux->p_access;
252 sout_AccessOutDelete( p_access );
255 FREE( p_sout->pp_mux );
258 for( i = 0; i < p_sout->p_sys->i_nb_mux; i++ )
260 FREE( p_sout->p_sys->pp_mux[i] );
262 FREE( p_sout->p_sys->pp_mux );
266 vlc_object_destroy( p_sout );
271 /*****************************************************************************
272 * InitInstance: opens appropriate modules
273 *****************************************************************************/
274 static int InstanceNewOutput (sout_instance_t *p_sout, char *psz_dest )
279 /* Parse dest string. Syntax : [[<access>][/<mux>]:][<dest>] */
280 /* This code is identical to input.c:InitThread. FIXME : factorize it ? */
282 char * psz_dup = strdup( psz_dest );
283 char * psz_parser = psz_dup;
284 char * psz_access = "";
286 char * psz_name = "";
287 /* *** first parse psz_dest */
288 while( *psz_parser && *psz_parser != ':' )
292 #if defined( WIN32 ) || defined( UNDER_CE )
293 if( psz_parser - psz_dup == 1 )
295 msg_Warn( p_sout, "drive letter %c: found in source string",
303 psz_access = psz_mux = "";
308 *psz_parser++ = '\0';
310 /* let's skip '//' */
311 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
316 psz_name = psz_parser ;
318 /* Come back to parse the access and mux plug-ins */
319 psz_parser = psz_dup;
326 else if( *psz_parser == '/' )
334 psz_access = psz_parser;
336 while( *psz_parser && *psz_parser != '/' )
341 if( *psz_parser == '/' )
343 *psz_parser++ = '\0';
354 psz_mux = psz_parser;
358 msg_Dbg( p_sout, "access `%s', mux `%s', name `%s'",
359 psz_access, psz_mux, psz_name );
362 mrl_Parse( &mrl, psz_dest );
363 msg_Dbg( p_sout, "access `%s', mux `%s', name `%s'",
364 mrl.psz_access, mrl.psz_way, mrl.psz_name );
366 vlc_mutex_lock( &p_sout->lock );
367 /* *** create mux *** */
369 if( InstanceMuxNew( p_sout, mrl.psz_way, mrl.psz_access, mrl.psz_name ) )
371 msg_Err( p_sout, "cannot create sout chain for %s/%s://%s",
372 mrl.psz_access, mrl.psz_way, mrl.psz_name );
375 vlc_mutex_unlock( &p_sout->lock );
376 return( VLC_EGENERIC );
380 /* *** finish all setup *** */
381 if( p_sout->psz_sout )
384 realloc( p_sout->psz_sout,
385 strlen( p_sout->psz_sout ) +2+1+ strlen( psz_dest ) );
386 strcat( p_sout->psz_sout, "#" );
387 strcat( p_sout->psz_sout, psz_dest );
391 p_sout->psz_sout = strdup( psz_dest );
393 psz_dup = strdup( psz_dest );
394 TAB_APPEND( p_sout->i_nb_dest, p_sout->ppsz_dest, psz_dup );
395 vlc_mutex_unlock( &p_sout->lock );
397 msg_Dbg( p_sout, "complete sout `%s'", p_sout->psz_sout );
402 static int InstanceMuxNew ( sout_instance_t *p_sout,
403 char *psz_mux, char *psz_access, char *psz_name )
405 sout_access_out_t *p_access;
408 /* *** find and open appropriate access module *** */
410 sout_AccessOutNew( p_sout, psz_access, psz_name );
411 if( p_access == NULL )
413 msg_Err( p_sout, "no suitable sout access module for `%s/%s://%s'",
414 psz_access, psz_mux, psz_name );
415 return( VLC_EGENERIC );
418 /* *** find and open appropriate mux module *** */
419 p_mux = MuxNew( p_sout, psz_mux, p_access );
422 msg_Err( p_sout, "no suitable sout mux module for `%s/%s://%s'",
423 psz_access, psz_mux, psz_name );
425 sout_AccessOutDelete( p_access );
426 return( VLC_EGENERIC );
429 p_sout->i_preheader = __MAX( p_sout->i_preheader,
430 p_mux->i_preheader );
432 TAB_APPEND( p_sout->i_nb_mux, p_sout->pp_mux, p_mux );
437 /*****************************************************************************
438 * sout_AccessOutNew: allocate a new access out
439 *****************************************************************************/
440 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
441 char *psz_access, char *psz_name )
443 sout_access_out_t *p_access;
445 if( !( p_access = vlc_object_create( p_sout,
446 sizeof( sout_access_out_t ) ) ) )
448 msg_Err( p_sout, "out of memory" );
451 p_access->psz_access = strdup( psz_access ? psz_access : "" );
452 p_access->psz_name = strdup( psz_name ? psz_name : "" );
453 p_access->p_sout = p_sout;
454 p_access->p_sys = NULL;
455 p_access->pf_seek = NULL;
456 p_access->pf_write = NULL;
458 p_access->p_module = module_Need( p_access,
460 p_access->psz_access );;
462 if( !p_access->p_module )
464 free( p_access->psz_access );
465 free( p_access->psz_name );
466 vlc_object_destroy( p_access );
472 /*****************************************************************************
473 * sout_AccessDelete: delete an access out
474 *****************************************************************************/
475 void sout_AccessOutDelete( sout_access_out_t *p_access )
477 if( p_access->p_module )
479 module_Unneed( p_access, p_access->p_module );
481 free( p_access->psz_access );
482 free( p_access->psz_name );
484 vlc_object_destroy( p_access );
487 /*****************************************************************************
489 *****************************************************************************/
490 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
492 return( p_access->pf_seek( p_access, i_pos ) );
495 /*****************************************************************************
497 *****************************************************************************/
498 int sout_AccessOutWrite( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
500 return( p_access->pf_write( p_access, p_buffer ) );
505 static sout_input_t *SoutInputCreate( sout_instance_t *p_sout,
506 sout_packet_format_t *p_format )
508 sout_input_t *p_input;
510 p_input = malloc( sizeof( sout_input_t ) );
512 p_input->p_sout = p_sout;
513 memcpy( &p_input->input_format,
515 sizeof( sout_packet_format_t ) );
516 p_input->p_fifo = sout_FifoCreate( p_sout );
517 p_input->p_sys = NULL;
522 static void SoutInputDestroy( sout_instance_t *p_sout,
523 sout_input_t *p_input )
525 sout_FifoDestroy( p_sout, p_input->p_fifo );
529 /*****************************************************************************
530 * Mux*: create/destroy/manipulate muxer.
531 * XXX: for now they are private, but I will near export them
532 * to allow muxer creating private muxer (ogg in avi, flexmux in ts/ps)
533 *****************************************************************************/
535 /*****************************************************************************
536 * MuxNew: allocate a new mux
537 *****************************************************************************/
538 static sout_mux_t * MuxNew ( sout_instance_t *p_sout,
540 sout_access_out_t *p_access )
544 p_mux = vlc_object_create( p_sout,
545 sizeof( sout_mux_t ) );
548 msg_Err( p_sout, "out of memory" );
552 p_mux->p_sout = p_sout;
553 p_mux->psz_mux = strdup( psz_mux);
554 p_mux->p_access = p_access;
555 p_mux->i_preheader = 0;
556 p_mux->pf_capacity = NULL;
557 p_mux->pf_addstream = NULL;
558 p_mux->pf_delstream = NULL;
559 p_mux->pf_mux = NULL;
560 p_mux->i_nb_inputs = 0;
561 p_mux->pp_inputs = NULL;
565 p_mux->p_module = module_Need( p_mux,
568 if( p_mux->p_module == NULL )
570 FREE( p_mux->psz_mux );
572 vlc_object_destroy( p_mux );
576 /* *** probe mux capacity *** */
577 if( p_mux->pf_capacity )
580 if( p_mux->pf_capacity( p_mux,
581 SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME,
582 NULL, (void*)&b_answer ) != SOUT_MUX_CAP_ERR_OK )
584 b_answer = VLC_FALSE;
588 msg_Dbg( p_sout, "muxer support adding stream at any time" );
589 p_mux->b_add_stream_any_time = VLC_TRUE;
590 p_mux->b_waiting_stream = VLC_FALSE;
594 p_mux->b_add_stream_any_time = VLC_FALSE;
595 p_mux->b_waiting_stream = VLC_TRUE;
600 p_mux->b_add_stream_any_time = VLC_FALSE;
601 p_mux->b_waiting_stream = VLC_TRUE;
603 p_mux->i_add_stream_start = -1;
608 static void MuxDelete ( sout_mux_t *p_mux )
610 if( p_mux->p_module )
612 module_Unneed( p_mux, p_mux->p_module );
614 free( p_mux->psz_mux );
616 vlc_object_destroy( p_mux );
619 static sout_input_t *MuxAddStream ( sout_mux_t *p_mux,
620 sout_packet_format_t *p_format )
622 sout_input_t *p_input;
624 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream)
626 msg_Err( p_mux, "cannot add a new stream (unsuported while muxing for this format)" );
629 if( p_mux->i_add_stream_start < 0 )
631 /* we wait for one second */
632 p_mux->i_add_stream_start = mdate();
635 msg_Dbg( p_mux, "adding a new input" );
636 /* create a new sout input */
637 p_input = SoutInputCreate( p_mux->p_sout, p_format );
639 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
640 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
642 msg_Err( p_mux, "cannot add this stream" );
643 MuxDeleteStream( p_mux, p_input );
650 static void MuxDeleteStream ( sout_mux_t *p_mux,
651 sout_input_t *p_input )
655 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
658 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
660 msg_Err( p_mux, "cannot del this stream from mux" );
663 /* remove the entry */
664 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
666 if( p_mux->i_nb_inputs == 0 )
668 msg_Warn( p_mux, "no more input stream for this mux" );
671 SoutInputDestroy( p_mux->p_sout, p_input );
675 static void MuxSendBuffer ( sout_mux_t *p_mux,
676 sout_input_t *p_input,
677 sout_buffer_t *p_buffer )
679 sout_FifoPut( p_input->p_fifo, p_buffer );
681 if( p_mux->b_waiting_stream )
683 if( p_mux->i_add_stream_start > 0 &&
684 p_mux->i_add_stream_start + (mtime_t)1000000 < mdate() )
686 /* more than 1 second, start muxing */
687 p_mux->b_waiting_stream = VLC_FALSE;
694 p_mux->pf_mux( p_mux );
697 /*****************************************************************************
699 *****************************************************************************/
700 sout_packetizer_input_t *__sout_InputNew( vlc_object_t *p_this,
701 sout_packet_format_t *p_format )
703 sout_instance_t *p_sout = NULL;
704 sout_packetizer_input_t *p_input;
707 vlc_bool_t b_accepted = VLC_FALSE;
709 /* search an stream output */
710 for( i_try = 0; i_try < 12; i_try++ )
712 p_sout = vlc_object_find( p_this, VLC_OBJECT_SOUT, FIND_ANYWHERE );
716 msg_Dbg( p_this, "waiting for sout" );
726 msg_Err( p_this, "cannot find any stream ouput" );
729 msg_Dbg( p_sout, "adding a new input" );
731 /* *** create a packetizer input *** */
732 p_input = malloc( sizeof( sout_packetizer_input_t ) );
733 p_input->p_sout = p_sout;
734 p_input->i_nb_inputs = 0;
735 p_input->pp_inputs = NULL;
736 p_input->i_nb_mux = 0;
737 p_input->pp_mux = NULL;
738 memcpy( &p_input->input_format,
740 sizeof( sout_packet_format_t ) );
742 if( p_format->i_fourcc == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
744 vlc_object_release( p_sout );
748 vlc_mutex_lock( &p_sout->lock );
749 /* *** add this input to all muxers *** */
750 for( i_mux = 0; i_mux < p_sout->i_nb_mux; i_mux++ )
752 sout_input_t *p_mux_input;
753 #define p_mux p_sout->pp_mux[i_mux]
755 p_mux_input = MuxAddStream( p_mux, p_format );
758 TAB_APPEND( p_input->i_nb_inputs, p_input->pp_inputs, p_mux_input );
759 TAB_APPEND( p_input->i_nb_mux, p_input->pp_mux, p_mux );
761 b_accepted = VLC_TRUE;
768 /* all muxer refuse this stream, so delete it */
771 vlc_mutex_unlock( &p_sout->lock );
772 vlc_object_release( p_sout );
776 TAB_APPEND( p_sout->i_nb_inputs, p_sout->pp_inputs, p_input );
777 vlc_mutex_unlock( &p_sout->lock );
779 vlc_object_release( p_sout );
785 int sout_InputDelete( sout_packetizer_input_t *p_input )
787 sout_instance_t *p_sout = p_input->p_sout;
790 msg_Dbg( p_sout, "removing an input" );
792 vlc_mutex_lock( &p_sout->lock );
794 /* *** remove this input to all muxers *** */
795 for( i_input = 0; i_input < p_input->i_nb_inputs; i_input++ )
797 MuxDeleteStream( p_input->pp_mux[i_input], p_input->pp_inputs[i_input] );
800 TAB_REMOVE( p_sout->i_nb_inputs, p_sout->pp_inputs, p_input );
802 free( p_input->pp_inputs );
803 free( p_input->pp_mux );
807 vlc_mutex_unlock( &p_sout->lock );
812 int sout_InputSendBuffer( sout_packetizer_input_t *p_input, sout_buffer_t *p_buffer )
814 // sout_instance_sys_t *p_sys = p_input->p_sout->p_sys;
815 /* msg_Dbg( p_input->p_sout,
816 "send buffer, size:%d", p_buffer->i_size ); */
818 if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) &&
819 p_input->i_nb_inputs > 0 )
823 vlc_mutex_lock( &p_input->p_sout->lock );
824 for( i = 0; i < p_input->i_nb_inputs - 1; i++ )
826 sout_buffer_t *p_dup;
828 p_dup = sout_BufferDuplicate( p_input->p_sout, p_buffer );
830 MuxSendBuffer( p_input->pp_mux[i],
831 p_input->pp_inputs[i],
834 MuxSendBuffer( p_input->pp_mux[p_input->i_nb_inputs-1],
835 p_input->pp_inputs[p_input->i_nb_inputs-1],
838 vlc_mutex_unlock( &p_input->p_sout->lock );
842 sout_BufferDelete( p_input->p_sout, p_buffer );
847 sout_fifo_t *sout_FifoCreate( sout_instance_t *p_sout )
851 if( !( p_fifo = malloc( sizeof( sout_fifo_t ) ) ) )
856 vlc_mutex_init( p_sout, &p_fifo->lock );
857 vlc_cond_init ( p_sout, &p_fifo->wait );
859 p_fifo->p_first = NULL;
860 p_fifo->pp_last = &p_fifo->p_first;
865 void sout_FifoFree( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
867 sout_buffer_t *p_buffer;
869 vlc_mutex_lock( &p_fifo->lock );
870 p_buffer = p_fifo->p_first;
873 sout_buffer_t *p_next;
874 p_next = p_buffer->p_next;
875 sout_BufferDelete( p_sout, p_buffer );
878 vlc_mutex_unlock( &p_fifo->lock );
882 void sout_FifoDestroy( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
884 sout_FifoFree( p_sout, p_fifo );
885 vlc_mutex_destroy( &p_fifo->lock );
886 vlc_cond_destroy ( &p_fifo->wait );
891 void sout_FifoPut( sout_fifo_t *p_fifo, sout_buffer_t *p_buffer )
893 vlc_mutex_lock( &p_fifo->lock );
897 *p_fifo->pp_last = p_buffer;
898 p_fifo->pp_last = &p_buffer->p_next;
901 p_buffer = p_buffer->p_next;
905 /* warm there is data in this fifo */
906 vlc_cond_signal( &p_fifo->wait );
907 vlc_mutex_unlock( &p_fifo->lock );
910 sout_buffer_t *sout_FifoGet( sout_fifo_t *p_fifo )
912 sout_buffer_t *p_buffer;
914 vlc_mutex_lock( &p_fifo->lock );
916 if( p_fifo->p_first == NULL )
918 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
921 p_buffer = p_fifo->p_first;
923 p_fifo->p_first = p_buffer->p_next;
926 if( p_fifo->p_first == NULL )
928 p_fifo->pp_last = &p_fifo->p_first;
931 vlc_mutex_unlock( &p_fifo->lock );
933 p_buffer->p_next = NULL;
937 sout_buffer_t *sout_FifoShow( sout_fifo_t *p_fifo )
939 sout_buffer_t *p_buffer;
941 vlc_mutex_lock( &p_fifo->lock );
943 if( p_fifo->p_first == NULL )
945 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
948 p_buffer = p_fifo->p_first;
950 vlc_mutex_unlock( &p_fifo->lock );
955 sout_buffer_t *sout_BufferNew( sout_instance_t *p_sout, size_t i_size )
957 sout_buffer_t *p_buffer;
961 msg_Dbg( p_sout, "allocating an new buffer, size:%d", (uint32_t)i_size );
964 p_buffer = malloc( sizeof( sout_buffer_t ) );
965 i_preheader = p_sout->i_preheader;
969 p_buffer->p_allocated_buffer = malloc( i_size + i_preheader );
970 p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_preheader;
974 p_buffer->p_allocated_buffer = NULL;
975 p_buffer->p_buffer = NULL;
977 p_buffer->i_allocated_size = i_size + i_preheader;
978 p_buffer->i_buffer_size = i_size;
980 p_buffer->i_size = i_size;
981 p_buffer->i_length = 0;
984 p_buffer->i_bitrate = 0;
985 p_buffer->i_flags = 0x0000;
986 p_buffer->p_next = NULL;
990 int sout_BufferRealloc( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
996 "realloc buffer old size:%d new size:%d",
997 (uint32_t)p_buffer->i_allocated_size,
1001 i_preheader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
1003 if( !( p_buffer->p_allocated_buffer = realloc( p_buffer->p_allocated_buffer, i_size + i_preheader ) ) )
1005 msg_Err( p_sout, "realloc failed" );
1006 p_buffer->i_allocated_size = 0;
1007 p_buffer->i_buffer_size = 0;
1008 p_buffer->i_size = 0;
1009 p_buffer->p_buffer = NULL;
1012 p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_preheader;
1014 p_buffer->i_allocated_size = i_size + i_preheader;
1015 p_buffer->i_buffer_size = i_size;
1020 int sout_BufferReallocFromPreHeader( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
1024 i_preheader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
1026 if( i_preheader < i_size )
1031 p_buffer->p_buffer -= i_size;
1032 p_buffer->i_size += i_size;
1033 p_buffer->i_buffer_size += i_size;
1038 int sout_BufferDelete( sout_instance_t *p_sout, sout_buffer_t *p_buffer )
1041 msg_Dbg( p_sout, "freeing buffer, size:%d", p_buffer->i_size );
1043 if( p_buffer->p_allocated_buffer )
1045 free( p_buffer->p_allocated_buffer );
1051 sout_buffer_t *sout_BufferDuplicate( sout_instance_t *p_sout,
1052 sout_buffer_t *p_buffer )
1054 sout_buffer_t *p_dup;
1056 p_dup = sout_BufferNew( p_sout, p_buffer->i_size );
1058 p_dup->i_bitrate= p_buffer->i_bitrate;
1059 p_dup->i_dts = p_buffer->i_dts;
1060 p_dup->i_pts = p_buffer->i_pts;
1061 p_dup->i_length = p_buffer->i_length;
1062 p_dup->i_flags = p_buffer->i_flags;
1063 p_sout->p_vlc->pf_memcpy( p_dup->p_buffer, p_buffer->p_buffer, p_buffer->i_size );
1068 void sout_BufferChain( sout_buffer_t **pp_chain,
1069 sout_buffer_t *p_buffer )
1071 if( *pp_chain == NULL )
1073 *pp_chain = p_buffer;
1075 else if( p_buffer != NULL )
1077 sout_buffer_t *p = *pp_chain;
1084 p->p_next = p_buffer;
1089 static int mrl_ParseOptions( mrl_option_t **pp_opt, char *psz_options )
1091 mrl_option_t **pp_last = pp_opt;
1093 char *psz_parser = strdup( psz_options );
1097 if( *psz_parser == '=' )
1100 return( VLC_EGENERIC );
1102 if( *psz_parser == '{' )
1113 while( *psz_parser && ( *psz_parser == ' ' || *psz_parser == '\t' || *psz_parser == ';' ) )
1118 if( ( psz_end = strchr( psz_parser, '=' ) ) != NULL )
1122 while( psz_end > psz_parser && ( *psz_end == ' ' || *psz_end == '\t' ) )
1127 if( psz_end - psz_parser <= 0 )
1129 return( VLC_EGENERIC );
1133 opt.psz_name = strdup( psz_parser );
1135 psz_parser = psz_end + 1;
1136 if( ( psz_end = strchr( psz_parser, ';' ) ) == NULL &&
1137 ( psz_end = strchr( psz_parser, '}' ) ) == NULL )
1139 psz_end = psz_parser + strlen( psz_parser ) + 1;
1142 opt.psz_value = strdup( psz_parser );
1144 fprintf( stderr, "option: name=`%s' value=`%s'\n",
1147 psz_parser = psz_end + 1;
1149 *pp_last = malloc( sizeof( mrl_option_t ) );
1160 static int mrl_Parse( mrl_t *p_mrl, char *psz_mrl )
1162 char * psz_dup = strdup( psz_mrl );
1163 char * psz_parser = psz_dup;
1164 char * psz_access = "";
1165 char * psz_way = "";
1166 char * psz_name = "";
1168 /* *** first parse psz_dest */
1169 while( *psz_parser && *psz_parser != ':' )
1171 if( *psz_parser == '{' )
1173 while( *psz_parser && *psz_parser != '}' )
1187 #if defined( WIN32 ) || defined( UNDER_CE )
1188 if( psz_parser - psz_dup == 1 )
1190 /* msg_Warn( p_sout, "drive letter %c: found in source string",
1198 psz_access = psz_way = "";
1203 *psz_parser++ = '\0';
1205 /* let's skip '//' */
1206 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
1211 psz_name = psz_parser ;
1213 /* Come back to parse the access and mux plug-ins */
1214 psz_parser = psz_dup;
1221 else if( *psz_parser == '/' )
1229 psz_access = psz_parser;
1231 while( *psz_parser && *psz_parser != '/' )
1233 if( *psz_parser == '{' )
1235 while( *psz_parser && *psz_parser != '}' )
1250 if( *psz_parser == '/' )
1252 *psz_parser++ = '\0';
1263 psz_way = psz_parser;
1268 if( ( psz_parser = strchr( psz_access, '{' ) ) != NULL )
1270 mrl_ParseOptions( &p_mrl->p_access_options, psz_parser );
1274 if( ( psz_parser = strchr( psz_way, '{' ) ) != NULL )
1276 mrl_ParseOptions( &p_mrl->p_way_options, psz_parser );
1281 p_mrl->p_access_options = NULL;
1282 p_mrl->p_way_options = NULL;
1284 p_mrl->psz_access = strdup( psz_access );
1285 p_mrl->psz_way = strdup( psz_way );
1286 p_mrl->psz_name = strdup( psz_name );
1289 return( VLC_SUCCESS );
1293 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
1294 static void mrl_Clean( mrl_t *p_mrl )
1296 FREE( p_mrl->psz_access );
1297 FREE( p_mrl->psz_way );
1298 FREE( p_mrl->psz_name );