1 /*****************************************************************************
2 * stream_output.c : stream output module
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: stream_output.c,v 1.18 2003/03/11 20:53:28 sam 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 #define p_mux p_sout->pp_mux[i]
247 sout_AccessOutDelete( p_mux->p_access );
251 FREE( p_sout->pp_mux );
254 for( i = 0; i < p_sout->p_sys->i_nb_mux; i++ )
256 FREE( p_sout->p_sys->pp_mux[i] );
258 FREE( p_sout->p_sys->pp_mux );
262 vlc_object_destroy( p_sout );
267 /*****************************************************************************
268 * InitInstance: opens appropriate modules
269 *****************************************************************************/
270 static int InstanceNewOutput (sout_instance_t *p_sout, char *psz_dest )
275 /* Parse dest string. Syntax : [[<access>][/<mux>]:][<dest>] */
276 /* This code is identical to input.c:InitThread. FIXME : factorize it ? */
278 char * psz_dup = strdup( psz_dest );
279 char * psz_parser = psz_dup;
280 char * psz_access = "";
282 char * psz_name = "";
283 /* *** first parse psz_dest */
284 while( *psz_parser && *psz_parser != ':' )
288 #if defined( WIN32 ) || defined( UNDER_CE )
289 if( psz_parser - psz_dup == 1 )
291 msg_Warn( p_sout, "drive letter %c: found in source string",
299 psz_access = psz_mux = "";
304 *psz_parser++ = '\0';
306 /* let's skip '//' */
307 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
312 psz_name = psz_parser ;
314 /* Come back to parse the access and mux plug-ins */
315 psz_parser = psz_dup;
322 else if( *psz_parser == '/' )
330 psz_access = psz_parser;
332 while( *psz_parser && *psz_parser != '/' )
337 if( *psz_parser == '/' )
339 *psz_parser++ = '\0';
350 psz_mux = psz_parser;
354 msg_Dbg( p_sout, "access `%s', mux `%s', name `%s'",
355 psz_access, psz_mux, psz_name );
358 mrl_Parse( &mrl, psz_dest );
359 msg_Dbg( p_sout, "access `%s', mux `%s', name `%s'",
360 mrl.psz_access, mrl.psz_way, mrl.psz_name );
362 vlc_mutex_lock( &p_sout->lock );
363 /* *** create mux *** */
365 if( InstanceMuxNew( p_sout, mrl.psz_way, mrl.psz_access, mrl.psz_name ) )
367 msg_Err( p_sout, "cannot create sout chain for %s/%s://%s",
368 mrl.psz_access, mrl.psz_way, mrl.psz_name );
371 vlc_mutex_unlock( &p_sout->lock );
372 return( VLC_EGENERIC );
376 /* *** finish all setup *** */
377 if( p_sout->psz_sout )
380 realloc( p_sout->psz_sout,
381 strlen( p_sout->psz_sout ) +2+1+ strlen( psz_dest ) );
382 strcat( p_sout->psz_sout, "#" );
383 strcat( p_sout->psz_sout, psz_dest );
387 p_sout->psz_sout = strdup( psz_dest );
389 psz_dup = strdup( psz_dest );
390 TAB_APPEND( p_sout->i_nb_dest, p_sout->ppsz_dest, psz_dup );
391 vlc_mutex_unlock( &p_sout->lock );
393 msg_Dbg( p_sout, "complete sout `%s'", p_sout->psz_sout );
398 static int InstanceMuxNew ( sout_instance_t *p_sout,
399 char *psz_mux, char *psz_access, char *psz_name )
401 sout_access_out_t *p_access;
404 /* *** find and open appropriate access module *** */
406 sout_AccessOutNew( p_sout, psz_access, psz_name );
407 if( p_access == NULL )
409 msg_Err( p_sout, "no suitable sout access module for `%s/%s://%s'",
410 psz_access, psz_mux, psz_name );
411 return( VLC_EGENERIC );
414 /* *** find and open appropriate mux module *** */
415 p_mux = MuxNew( p_sout, psz_mux, p_access );
418 msg_Err( p_sout, "no suitable sout mux module for `%s/%s://%s'",
419 psz_access, psz_mux, psz_name );
421 sout_AccessOutDelete( p_access );
422 return( VLC_EGENERIC );
425 p_sout->i_preheader = __MAX( p_sout->i_preheader,
426 p_mux->i_preheader );
428 TAB_APPEND( p_sout->i_nb_mux, p_sout->pp_mux, p_mux );
433 /*****************************************************************************
434 * sout_AccessOutNew: allocate a new access out
435 *****************************************************************************/
436 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
437 char *psz_access, char *psz_name )
439 sout_access_out_t *p_access;
441 if( !( p_access = vlc_object_create( p_sout,
442 sizeof( sout_access_out_t ) ) ) )
444 msg_Err( p_sout, "out of memory" );
447 p_access->psz_access = strdup( psz_access ? psz_access : "" );
448 p_access->psz_name = strdup( psz_name ? psz_name : "" );
449 p_access->p_sout = p_sout;
450 p_access->p_sys = NULL;
451 p_access->pf_seek = NULL;
452 p_access->pf_write = NULL;
454 p_access->p_module = module_Need( p_access,
456 p_access->psz_access );;
458 if( !p_access->p_module )
460 free( p_access->psz_access );
461 free( p_access->psz_name );
462 vlc_object_destroy( p_access );
468 /*****************************************************************************
469 * sout_AccessDelete: delete an access out
470 *****************************************************************************/
471 void sout_AccessOutDelete( sout_access_out_t *p_access )
473 if( p_access->p_module )
475 module_Unneed( p_access, p_access->p_module );
477 free( p_access->psz_access );
478 free( p_access->psz_name );
480 vlc_object_destroy( p_access );
483 /*****************************************************************************
485 *****************************************************************************/
486 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
488 return( p_access->pf_seek( p_access, i_pos ) );
491 /*****************************************************************************
493 *****************************************************************************/
494 int sout_AccessOutWrite( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
496 return( p_access->pf_write( p_access, p_buffer ) );
501 static sout_input_t *SoutInputCreate( sout_instance_t *p_sout,
502 sout_packet_format_t *p_format )
504 sout_input_t *p_input;
506 p_input = malloc( sizeof( sout_input_t ) );
508 p_input->p_sout = p_sout;
509 memcpy( &p_input->input_format,
511 sizeof( sout_packet_format_t ) );
512 p_input->p_fifo = sout_FifoCreate( p_sout );
513 p_input->p_sys = NULL;
518 static void SoutInputDestroy( sout_instance_t *p_sout,
519 sout_input_t *p_input )
521 sout_FifoDestroy( p_sout, p_input->p_fifo );
525 /*****************************************************************************
526 * Mux*: create/destroy/manipulate muxer.
527 * XXX: for now they are private, but I will near export them
528 * to allow muxer creating private muxer (ogg in avi, flexmux in ts/ps)
529 *****************************************************************************/
531 /*****************************************************************************
532 * MuxNew: allocate a new mux
533 *****************************************************************************/
534 static sout_mux_t * MuxNew ( sout_instance_t *p_sout,
536 sout_access_out_t *p_access )
540 p_mux = vlc_object_create( p_sout,
541 sizeof( sout_mux_t ) );
544 msg_Err( p_sout, "out of memory" );
548 p_mux->p_sout = p_sout;
549 p_mux->psz_mux = strdup( psz_mux);
550 p_mux->p_access = p_access;
551 p_mux->i_preheader = 0;
552 p_mux->pf_capacity = NULL;
553 p_mux->pf_addstream = NULL;
554 p_mux->pf_delstream = NULL;
555 p_mux->pf_mux = NULL;
556 p_mux->i_nb_inputs = 0;
557 p_mux->pp_inputs = NULL;
561 p_mux->p_module = module_Need( p_mux,
564 if( p_mux->p_module == NULL )
566 FREE( p_mux->psz_mux );
568 vlc_object_destroy( p_mux );
572 /* *** probe mux capacity *** */
573 if( p_mux->pf_capacity )
576 if( p_mux->pf_capacity( p_mux,
577 SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME,
578 NULL, (void*)&b_answer ) != SOUT_MUX_CAP_ERR_OK )
580 b_answer = VLC_FALSE;
584 msg_Dbg( p_sout, "muxer support adding stream at any time" );
585 p_mux->b_add_stream_any_time = VLC_TRUE;
586 p_mux->b_waiting_stream = VLC_FALSE;
590 p_mux->b_add_stream_any_time = VLC_FALSE;
591 p_mux->b_waiting_stream = VLC_TRUE;
596 p_mux->b_add_stream_any_time = VLC_FALSE;
597 p_mux->b_waiting_stream = VLC_TRUE;
599 p_mux->i_add_stream_start = -1;
604 static void MuxDelete ( sout_mux_t *p_mux )
606 if( p_mux->p_module )
608 module_Unneed( p_mux, p_mux->p_module );
610 free( p_mux->psz_mux );
612 vlc_object_destroy( p_mux );
615 static sout_input_t *MuxAddStream ( sout_mux_t *p_mux,
616 sout_packet_format_t *p_format )
618 sout_input_t *p_input;
620 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream)
622 msg_Err( p_mux, "cannot add a new stream (unsuported while muxing for this format)" );
625 if( p_mux->i_add_stream_start < 0 )
627 /* we wait for one second */
628 p_mux->i_add_stream_start = mdate();
631 msg_Dbg( p_mux, "adding a new input" );
632 /* create a new sout input */
633 p_input = SoutInputCreate( p_mux->p_sout, p_format );
635 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
636 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
638 msg_Err( p_mux, "cannot add this stream" );
639 MuxDeleteStream( p_mux, p_input );
646 static void MuxDeleteStream ( sout_mux_t *p_mux,
647 sout_input_t *p_input )
651 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
654 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
656 msg_Err( p_mux, "cannot del this stream from mux" );
659 /* remove the entry */
660 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
662 if( p_mux->i_nb_inputs == 0 )
664 msg_Warn( p_mux, "no more input stream for this mux" );
667 SoutInputDestroy( p_mux->p_sout, p_input );
671 static void MuxSendBuffer ( sout_mux_t *p_mux,
672 sout_input_t *p_input,
673 sout_buffer_t *p_buffer )
675 sout_FifoPut( p_input->p_fifo, p_buffer );
677 if( p_mux->b_waiting_stream )
679 if( p_mux->i_add_stream_start > 0 &&
680 p_mux->i_add_stream_start + (mtime_t)1000000 < mdate() )
682 /* more than 1 second, start muxing */
683 p_mux->b_waiting_stream = VLC_FALSE;
690 p_mux->pf_mux( p_mux );
693 /*****************************************************************************
695 *****************************************************************************/
696 sout_packetizer_input_t *__sout_InputNew( vlc_object_t *p_this,
697 sout_packet_format_t *p_format )
699 sout_instance_t *p_sout = NULL;
700 sout_packetizer_input_t *p_input;
703 vlc_bool_t b_accepted = VLC_FALSE;
705 /* search an stream output */
706 for( i_try = 0; i_try < 12; i_try++ )
708 p_sout = vlc_object_find( p_this, VLC_OBJECT_SOUT, FIND_ANYWHERE );
712 msg_Dbg( p_this, "waiting for sout" );
722 msg_Err( p_this, "cannot find any stream ouput" );
725 msg_Dbg( p_sout, "adding a new input" );
727 /* *** create a packetizer input *** */
728 p_input = malloc( sizeof( sout_packetizer_input_t ) );
729 p_input->p_sout = p_sout;
730 p_input->i_nb_inputs = 0;
731 p_input->pp_inputs = NULL;
732 p_input->i_nb_mux = 0;
733 p_input->pp_mux = NULL;
734 memcpy( &p_input->input_format,
736 sizeof( sout_packet_format_t ) );
738 if( p_format->i_fourcc == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
740 vlc_object_release( p_sout );
744 vlc_mutex_lock( &p_sout->lock );
745 /* *** add this input to all muxers *** */
746 for( i_mux = 0; i_mux < p_sout->i_nb_mux; i_mux++ )
748 sout_input_t *p_mux_input;
749 #define p_mux p_sout->pp_mux[i_mux]
751 p_mux_input = MuxAddStream( p_mux, p_format );
754 TAB_APPEND( p_input->i_nb_inputs, p_input->pp_inputs, p_mux_input );
755 TAB_APPEND( p_input->i_nb_mux, p_input->pp_mux, p_mux );
757 b_accepted = VLC_TRUE;
764 /* all muxer refuse this stream, so delete it */
767 vlc_mutex_unlock( &p_sout->lock );
768 vlc_object_release( p_sout );
772 TAB_APPEND( p_sout->i_nb_inputs, p_sout->pp_inputs, p_input );
773 vlc_mutex_unlock( &p_sout->lock );
775 vlc_object_release( p_sout );
781 int sout_InputDelete( sout_packetizer_input_t *p_input )
783 sout_instance_t *p_sout = p_input->p_sout;
786 msg_Dbg( p_sout, "removing an input" );
788 vlc_mutex_lock( &p_sout->lock );
790 /* *** remove this input to all muxers *** */
791 for( i_input = 0; i_input < p_input->i_nb_inputs; i_input++ )
793 MuxDeleteStream( p_input->pp_mux[i_input], p_input->pp_inputs[i_input] );
796 TAB_REMOVE( p_sout->i_nb_inputs, p_sout->pp_inputs, p_input );
798 free( p_input->pp_inputs );
799 free( p_input->pp_mux );
803 vlc_mutex_unlock( &p_sout->lock );
808 int sout_InputSendBuffer( sout_packetizer_input_t *p_input, sout_buffer_t *p_buffer )
810 // sout_instance_sys_t *p_sys = p_input->p_sout->p_sys;
811 /* msg_Dbg( p_input->p_sout,
812 "send buffer, size:%d", p_buffer->i_size ); */
814 if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) &&
815 p_input->i_nb_inputs > 0 )
819 vlc_mutex_lock( &p_input->p_sout->lock );
820 for( i = 0; i < p_input->i_nb_inputs - 1; i++ )
822 sout_buffer_t *p_dup;
824 p_dup = sout_BufferDuplicate( p_input->p_sout, p_buffer );
826 MuxSendBuffer( p_input->pp_mux[i],
827 p_input->pp_inputs[i],
830 MuxSendBuffer( p_input->pp_mux[p_input->i_nb_inputs-1],
831 p_input->pp_inputs[p_input->i_nb_inputs-1],
834 vlc_mutex_unlock( &p_input->p_sout->lock );
838 sout_BufferDelete( p_input->p_sout, p_buffer );
843 sout_fifo_t *sout_FifoCreate( sout_instance_t *p_sout )
847 if( !( p_fifo = malloc( sizeof( sout_fifo_t ) ) ) )
852 vlc_mutex_init( p_sout, &p_fifo->lock );
853 vlc_cond_init ( p_sout, &p_fifo->wait );
855 p_fifo->p_first = NULL;
856 p_fifo->pp_last = &p_fifo->p_first;
861 void sout_FifoFree( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
863 sout_buffer_t *p_buffer;
865 vlc_mutex_lock( &p_fifo->lock );
866 p_buffer = p_fifo->p_first;
869 sout_buffer_t *p_next;
870 p_next = p_buffer->p_next;
871 sout_BufferDelete( p_sout, p_buffer );
874 vlc_mutex_unlock( &p_fifo->lock );
878 void sout_FifoDestroy( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
880 sout_FifoFree( p_sout, p_fifo );
881 vlc_mutex_destroy( &p_fifo->lock );
882 vlc_cond_destroy ( &p_fifo->wait );
887 void sout_FifoPut( sout_fifo_t *p_fifo, sout_buffer_t *p_buffer )
889 vlc_mutex_lock( &p_fifo->lock );
893 *p_fifo->pp_last = p_buffer;
894 p_fifo->pp_last = &p_buffer->p_next;
897 p_buffer = p_buffer->p_next;
901 /* warm there is data in this fifo */
902 vlc_cond_signal( &p_fifo->wait );
903 vlc_mutex_unlock( &p_fifo->lock );
906 sout_buffer_t *sout_FifoGet( sout_fifo_t *p_fifo )
908 sout_buffer_t *p_buffer;
910 vlc_mutex_lock( &p_fifo->lock );
912 if( p_fifo->p_first == NULL )
914 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
917 p_buffer = p_fifo->p_first;
919 p_fifo->p_first = p_buffer->p_next;
922 if( p_fifo->p_first == NULL )
924 p_fifo->pp_last = &p_fifo->p_first;
927 vlc_mutex_unlock( &p_fifo->lock );
929 p_buffer->p_next = NULL;
933 sout_buffer_t *sout_FifoShow( sout_fifo_t *p_fifo )
935 sout_buffer_t *p_buffer;
937 vlc_mutex_lock( &p_fifo->lock );
939 if( p_fifo->p_first == NULL )
941 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
944 p_buffer = p_fifo->p_first;
946 vlc_mutex_unlock( &p_fifo->lock );
951 sout_buffer_t *sout_BufferNew( sout_instance_t *p_sout, size_t i_size )
953 sout_buffer_t *p_buffer;
957 msg_Dbg( p_sout, "allocating an new buffer, size:%d", (uint32_t)i_size );
960 p_buffer = malloc( sizeof( sout_buffer_t ) );
961 i_preheader = p_sout->i_preheader;
965 p_buffer->p_allocated_buffer = malloc( i_size + i_preheader );
966 p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_preheader;
970 p_buffer->p_allocated_buffer = NULL;
971 p_buffer->p_buffer = NULL;
973 p_buffer->i_allocated_size = i_size + i_preheader;
974 p_buffer->i_buffer_size = i_size;
976 p_buffer->i_size = i_size;
977 p_buffer->i_length = 0;
980 p_buffer->i_bitrate = 0;
981 p_buffer->i_flags = 0x0000;
982 p_buffer->p_next = NULL;
986 int sout_BufferRealloc( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
992 "realloc buffer old size:%d new size:%d",
993 (uint32_t)p_buffer->i_allocated_size,
997 i_preheader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
999 if( !( p_buffer->p_allocated_buffer = realloc( p_buffer->p_allocated_buffer, i_size + i_preheader ) ) )
1001 msg_Err( p_sout, "realloc failed" );
1002 p_buffer->i_allocated_size = 0;
1003 p_buffer->i_buffer_size = 0;
1004 p_buffer->i_size = 0;
1005 p_buffer->p_buffer = NULL;
1008 p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_preheader;
1010 p_buffer->i_allocated_size = i_size + i_preheader;
1011 p_buffer->i_buffer_size = i_size;
1016 int sout_BufferReallocFromPreHeader( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
1020 i_preheader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
1022 if( i_preheader < i_size )
1027 p_buffer->p_buffer -= i_size;
1028 p_buffer->i_size += i_size;
1029 p_buffer->i_buffer_size += i_size;
1034 int sout_BufferDelete( sout_instance_t *p_sout, sout_buffer_t *p_buffer )
1037 msg_Dbg( p_sout, "freeing buffer, size:%d", p_buffer->i_size );
1039 if( p_buffer->p_allocated_buffer )
1041 free( p_buffer->p_allocated_buffer );
1047 sout_buffer_t *sout_BufferDuplicate( sout_instance_t *p_sout,
1048 sout_buffer_t *p_buffer )
1050 sout_buffer_t *p_dup;
1052 p_dup = sout_BufferNew( p_sout, p_buffer->i_size );
1054 p_dup->i_bitrate= p_buffer->i_bitrate;
1055 p_dup->i_dts = p_buffer->i_dts;
1056 p_dup->i_pts = p_buffer->i_pts;
1057 p_dup->i_length = p_buffer->i_length;
1058 p_dup->i_flags = p_buffer->i_flags;
1059 p_sout->p_vlc->pf_memcpy( p_dup->p_buffer, p_buffer->p_buffer, p_buffer->i_size );
1064 void sout_BufferChain( sout_buffer_t **pp_chain,
1065 sout_buffer_t *p_buffer )
1067 if( *pp_chain == NULL )
1069 *pp_chain = p_buffer;
1071 else if( p_buffer != NULL )
1073 sout_buffer_t *p = *pp_chain;
1080 p->p_next = p_buffer;
1085 static int mrl_ParseOptions( mrl_option_t **pp_opt, char *psz_options )
1087 mrl_option_t **pp_last = pp_opt;
1089 char *psz_parser = strdup( psz_options );
1093 if( *psz_parser == '=' )
1096 return( VLC_EGENERIC );
1098 if( *psz_parser == '{' )
1109 while( *psz_parser && ( *psz_parser == ' ' || *psz_parser == '\t' || *psz_parser == ';' ) )
1114 if( ( psz_end = strchr( psz_parser, '=' ) ) != NULL )
1118 while( psz_end > psz_parser && ( *psz_end == ' ' || *psz_end == '\t' ) )
1123 if( psz_end - psz_parser <= 0 )
1125 return( VLC_EGENERIC );
1129 opt.psz_name = strdup( psz_parser );
1131 psz_parser = psz_end + 1;
1132 if( ( psz_end = strchr( psz_parser, ';' ) ) == NULL &&
1133 ( psz_end = strchr( psz_parser, '}' ) ) == NULL )
1135 psz_end = psz_parser + strlen( psz_parser ) + 1;
1138 opt.psz_value = strdup( psz_parser );
1140 fprintf( stderr, "option: name=`%s' value=`%s'\n",
1143 psz_parser = psz_end + 1;
1145 *pp_last = malloc( sizeof( mrl_option_t ) );
1156 static int mrl_Parse( mrl_t *p_mrl, char *psz_mrl )
1158 char * psz_dup = strdupa( psz_mrl );
1159 char * psz_parser = psz_dup;
1160 char * psz_access = "";
1161 char * psz_way = "";
1162 char * psz_name = "";
1164 /* *** first parse psz_dest */
1165 while( *psz_parser && *psz_parser != ':' )
1167 if( *psz_parser == '{' )
1169 while( *psz_parser && *psz_parser != '}' )
1183 #if defined( WIN32 ) || defined( UNDER_CE )
1184 if( psz_parser - psz_dup == 1 )
1186 msg_Warn( p_sout, "drive letter %c: found in source string",
1194 psz_access = psz_way = "";
1199 *psz_parser++ = '\0';
1201 /* let's skip '//' */
1202 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
1207 psz_name = psz_parser ;
1209 /* Come back to parse the access and mux plug-ins */
1210 psz_parser = psz_dup;
1217 else if( *psz_parser == '/' )
1225 psz_access = psz_parser;
1227 while( *psz_parser && *psz_parser != '/' )
1229 if( *psz_parser == '{' )
1231 while( *psz_parser && *psz_parser != '}' )
1246 if( *psz_parser == '/' )
1248 *psz_parser++ = '\0';
1259 psz_way = psz_parser;
1264 if( ( psz_parser = strchr( psz_access, '{' ) ) != NULL )
1266 mrl_ParseOptions( &p_mrl->p_access_options, psz_parser );
1270 if( ( psz_parser = strchr( psz_way, '{' ) ) != NULL )
1272 mrl_ParseOptions( &p_mrl->p_way_options, psz_parser );
1277 p_mrl->p_access_options = NULL;
1278 p_mrl->p_way_options = NULL;
1280 p_mrl->psz_access = strdup( psz_access );
1281 p_mrl->psz_way = strdup( psz_way );
1282 p_mrl->psz_name = strdup( psz_name );
1284 return( VLC_SUCCESS );
1288 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
1289 static void mrl_Clean( mrl_t *p_mrl )
1291 FREE( p_mrl->psz_access );
1292 FREE( p_mrl->psz_way );
1293 FREE( p_mrl->psz_name );