decoder: cc: detect/auto raise reorder depth
[vlc.git] / src / stream_output / stream_output.c
bloba30e66852c80997a1d48e784296558f83d70adf3
1 /*****************************************************************************
2 * stream_output.c : stream output module
3 *****************************************************************************
4 * Copyright (C) 2002-2007 VLC authors and VideoLAN
5 * $Id$
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 it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <assert.h>
36 #include <vlc_common.h>
38 #include <stdlib.h> /* free() */
39 #include <stdio.h> /* sprintf() */
40 #include <string.h>
42 #include <vlc_sout.h>
44 #include "stream_output.h"
46 #include <vlc_meta.h>
47 #include <vlc_block.h>
48 #include <vlc_codec.h>
49 #include <vlc_modules.h>
51 #include "input/input_interface.h"
53 #undef DEBUG_BUFFER
54 /*****************************************************************************
55 * Local prototypes
56 *****************************************************************************/
57 static char *sout_stream_url_to_chain( bool, const char * );
60 * Generic MRL parser
64 typedef struct
66 char *psz_access;
67 char *psz_way;
68 char *psz_name;
69 } mrl_t;
71 /* mrl_Parse: parse psz_mrl and fill p_mrl */
72 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl );
73 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
74 static void mrl_Clean( mrl_t *p_mrl );
76 #undef sout_NewInstance
78 /*****************************************************************************
79 * sout_NewInstance: creates a new stream output instance
80 *****************************************************************************/
81 sout_instance_t *sout_NewInstance( vlc_object_t *p_parent, const char *psz_dest )
83 sout_instance_t *p_sout;
84 char *psz_chain;
86 assert( psz_dest != NULL );
88 if( psz_dest[0] == '#' )
90 psz_chain = strdup( &psz_dest[1] );
92 else
94 psz_chain = sout_stream_url_to_chain(
95 var_InheritBool(p_parent, "sout-display"), psz_dest );
97 if(!psz_chain)
98 return NULL;
100 /* *** Allocate descriptor *** */
101 p_sout = vlc_custom_create( p_parent, sizeof( *p_sout ), "stream output" );
102 if( p_sout == NULL )
104 free( psz_chain );
105 return NULL;
108 msg_Dbg( p_sout, "using sout chain=`%s'", psz_chain );
110 /* *** init descriptor *** */
111 p_sout->psz_sout = strdup( psz_dest );
112 p_sout->i_out_pace_nocontrol = 0;
114 vlc_mutex_init( &p_sout->lock );
115 p_sout->p_stream = NULL;
117 var_Create( p_sout, "sout-mux-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
119 p_sout->p_stream = sout_StreamChainNew( p_sout, psz_chain, NULL, NULL );
120 if( p_sout->p_stream )
122 free( psz_chain );
123 return p_sout;
126 msg_Err( p_sout, "stream chain failed for `%s'", psz_chain );
127 free( psz_chain );
129 FREENULL( p_sout->psz_sout );
131 vlc_mutex_destroy( &p_sout->lock );
132 vlc_object_release( p_sout );
133 return NULL;
136 /*****************************************************************************
137 * sout_DeleteInstance: delete a previously allocated instance
138 *****************************************************************************/
139 void sout_DeleteInstance( sout_instance_t * p_sout )
141 /* remove the stream out chain */
142 sout_StreamChainDelete( p_sout->p_stream, NULL );
144 /* *** free all string *** */
145 FREENULL( p_sout->psz_sout );
147 vlc_mutex_destroy( &p_sout->lock );
149 /* *** free structure *** */
150 vlc_object_release( p_sout );
153 /*****************************************************************************
154 * Packetizer/Input
155 *****************************************************************************/
156 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
157 const es_format_t *p_fmt )
159 sout_packetizer_input_t *p_input;
161 /* *** create a packetizer input *** */
162 if( !p_fmt->i_codec || !(p_input = malloc(sizeof(sout_packetizer_input_t))) )
163 return NULL;
165 p_input->p_sout = p_sout;
167 msg_Dbg( p_sout, "adding a new sout input for `%4.4s` (sout_input: %p)",
168 (char*) &p_fmt->i_codec, (void *)p_input );
170 /* *** add it to the stream chain */
171 vlc_mutex_lock( &p_sout->lock );
172 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
173 vlc_mutex_unlock( &p_sout->lock );
175 if( p_input->id == NULL )
177 msg_Warn( p_sout, "new sout input failed (sout_input: %p)",
178 (void *)p_input );
179 free( p_input );
180 p_input = NULL;
183 return( p_input );
186 /*****************************************************************************
188 *****************************************************************************/
189 int sout_InputDelete( sout_packetizer_input_t *p_input )
191 sout_instance_t *p_sout = p_input->p_sout;
193 msg_Dbg( p_sout, "removing a sout input (sout_input: %p)",
194 (void *)p_input );
196 vlc_mutex_lock( &p_sout->lock );
197 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
198 vlc_mutex_unlock( &p_sout->lock );
200 free( p_input );
202 return( VLC_SUCCESS);
205 bool sout_InputIsEmpty( sout_packetizer_input_t *p_input )
207 sout_instance_t *p_sout = p_input->p_sout;
208 bool b;
210 vlc_mutex_lock( &p_sout->lock );
211 if( sout_StreamControl( p_sout->p_stream, SOUT_STREAM_EMPTY, &b ) != VLC_SUCCESS )
212 b = true;
213 vlc_mutex_unlock( &p_sout->lock );
214 return b;
217 void sout_InputFlush( sout_packetizer_input_t *p_input )
219 sout_instance_t *p_sout = p_input->p_sout;
221 vlc_mutex_lock( &p_sout->lock );
222 sout_StreamFlush( p_sout->p_stream, p_input->id );
223 vlc_mutex_unlock( &p_sout->lock );
226 /*****************************************************************************
228 *****************************************************************************/
229 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
230 block_t *p_buffer )
232 sout_instance_t *p_sout = p_input->p_sout;
233 int i_ret;
235 vlc_mutex_lock( &p_sout->lock );
236 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
237 p_input->id, p_buffer );
238 vlc_mutex_unlock( &p_sout->lock );
240 return i_ret;
243 #undef sout_AccessOutNew
244 /*****************************************************************************
245 * sout_AccessOutNew: allocate a new access out
246 *****************************************************************************/
247 sout_access_out_t *sout_AccessOutNew( vlc_object_t *p_sout,
248 const char *psz_access, const char *psz_name )
250 sout_access_out_t *p_access;
251 char *psz_next;
253 p_access = vlc_custom_create( p_sout, sizeof( *p_access ), "access out" );
254 if( !p_access )
255 return NULL;
257 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
258 psz_access );
259 free( psz_next );
260 p_access->psz_path = strdup( psz_name ? psz_name : "" );
261 p_access->p_sys = NULL;
262 p_access->pf_seek = NULL;
263 p_access->pf_read = NULL;
264 p_access->pf_write = NULL;
265 p_access->pf_control = NULL;
266 p_access->p_module = NULL;
268 p_access->p_module =
269 module_need( p_access, "sout access", p_access->psz_access, true );
271 if( !p_access->p_module )
273 free( p_access->psz_access );
274 free( p_access->psz_path );
275 vlc_object_release( p_access );
276 return( NULL );
279 return p_access;
281 /*****************************************************************************
282 * sout_AccessDelete: delete an access out
283 *****************************************************************************/
284 void sout_AccessOutDelete( sout_access_out_t *p_access )
286 if( p_access->p_module )
288 module_unneed( p_access, p_access->p_module );
290 free( p_access->psz_access );
292 config_ChainDestroy( p_access->p_cfg );
294 free( p_access->psz_path );
296 vlc_object_release( p_access );
299 /*****************************************************************************
300 * sout_AccessSeek:
301 *****************************************************************************/
302 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
304 if (p_access->pf_seek == NULL)
305 return VLC_EGENERIC;
306 return p_access->pf_seek( p_access, i_pos );
309 /*****************************************************************************
310 * sout_AccessRead:
311 *****************************************************************************/
312 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
314 return( p_access->pf_read ?
315 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
318 /*****************************************************************************
319 * sout_AccessWrite:
320 *****************************************************************************/
321 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
323 return p_access->pf_write( p_access, p_buffer );
327 * sout_AccessOutControl
329 int sout_AccessOutControl (sout_access_out_t *access, int query, ...)
331 va_list ap;
332 int ret;
334 va_start (ap, query);
335 if (access->pf_control)
336 ret = access->pf_control (access, query, ap);
337 else
338 ret = VLC_EGENERIC;
339 va_end (ap);
340 return ret;
343 /*****************************************************************************
344 * sout_MuxNew: create a new mux
345 *****************************************************************************/
346 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, const char *psz_mux,
347 sout_access_out_t *p_access )
349 sout_mux_t *p_mux;
350 char *psz_next;
352 p_mux = vlc_custom_create( p_sout, sizeof( *p_mux ), "mux" );
353 if( p_mux == NULL )
354 return NULL;
356 p_mux->p_sout = p_sout;
357 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
358 free( psz_next );
360 p_mux->p_access = p_access;
361 p_mux->pf_control = NULL;
362 p_mux->pf_addstream = NULL;
363 p_mux->pf_delstream = NULL;
364 p_mux->pf_mux = NULL;
365 p_mux->i_nb_inputs = 0;
366 p_mux->pp_inputs = NULL;
368 p_mux->p_sys = NULL;
369 p_mux->p_module = NULL;
371 p_mux->b_add_stream_any_time = false;
372 p_mux->b_waiting_stream = true;
373 p_mux->i_add_stream_start = -1;
375 p_mux->p_module =
376 module_need( p_mux, "sout mux", p_mux->psz_mux, true );
378 if( p_mux->p_module == NULL )
380 FREENULL( p_mux->psz_mux );
382 vlc_object_release( p_mux );
383 return NULL;
386 /* *** probe mux capacity *** */
387 if( p_mux->pf_control )
389 int b_answer = false;
391 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
392 &b_answer ) )
394 b_answer = false;
397 if( b_answer )
399 msg_Dbg( p_sout, "muxer support adding stream at any time" );
400 p_mux->b_add_stream_any_time = true;
401 p_mux->b_waiting_stream = false;
403 /* If we control the output pace then it's better to wait before
404 * starting muxing (generates better streams/files). */
405 if( !p_sout->i_out_pace_nocontrol )
407 b_answer = true;
409 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
410 &b_answer ) )
412 b_answer = false;
415 if( b_answer )
417 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
418 "starting to mux" );
419 p_mux->b_waiting_stream = true;
424 return p_mux;
427 /*****************************************************************************
428 * sout_MuxDelete:
429 *****************************************************************************/
430 void sout_MuxDelete( sout_mux_t *p_mux )
432 if( p_mux->p_module )
434 module_unneed( p_mux, p_mux->p_module );
436 free( p_mux->psz_mux );
438 config_ChainDestroy( p_mux->p_cfg );
440 vlc_object_release( p_mux );
443 /*****************************************************************************
444 * sout_MuxAddStream:
445 *****************************************************************************/
446 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, const es_format_t *p_fmt )
448 sout_input_t *p_input;
450 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
452 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
453 "to this format). You can try increasing sout-mux-caching value" );
454 return NULL;
457 msg_Dbg( p_mux, "adding a new input" );
459 /* create a new sout input */
460 p_input = malloc( sizeof( sout_input_t ) );
461 if( !p_input )
462 return NULL;
464 // FIXME: remove either fmt or p_fmt...
465 es_format_Copy( &p_input->fmt, p_fmt );
466 p_input->p_fmt = &p_input->fmt;
468 p_input->p_fifo = block_FifoNew();
469 p_input->p_sys = NULL;
471 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
472 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
474 msg_Err( p_mux, "cannot add this stream" );
475 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
476 block_FifoRelease( p_input->p_fifo );
477 es_format_Clean( &p_input->fmt );
478 free( p_input );
479 return NULL;
482 return p_input;
485 /*****************************************************************************
486 * sout_MuxDeleteStream:
487 *****************************************************************************/
488 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
490 int i_index;
492 if( p_mux->b_waiting_stream
493 && block_FifoCount( p_input->p_fifo ) > 0 )
495 /* We stop waiting, and call the muxer for taking care of the data
496 * before we remove this es */
497 p_mux->b_waiting_stream = false;
498 p_mux->pf_mux( p_mux );
501 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
502 if( i_index >= 0 )
504 p_mux->pf_delstream( p_mux, p_input );
506 /* remove the entry */
507 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
509 if( p_mux->i_nb_inputs == 0 )
511 msg_Warn( p_mux, "no more input streams for this mux" );
514 block_FifoRelease( p_input->p_fifo );
515 es_format_Clean( &p_input->fmt );
516 free( p_input );
520 /*****************************************************************************
521 * sout_MuxSendBuffer:
522 *****************************************************************************/
523 int sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
524 block_t *p_buffer )
526 mtime_t i_dts = p_buffer->i_dts;
527 block_FifoPut( p_input->p_fifo, p_buffer );
529 if( p_mux->p_sout->i_out_pace_nocontrol )
531 mtime_t current_date = mdate();
532 if ( current_date > i_dts )
533 msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
534 current_date - i_dts );
537 if( p_mux->b_waiting_stream )
539 const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * INT64_C(1000);
541 if( p_mux->i_add_stream_start < 0 )
542 p_mux->i_add_stream_start = i_dts;
544 /* Wait until we have enough data before muxing */
545 if( p_mux->i_add_stream_start < 0 ||
546 i_dts < p_mux->i_add_stream_start + i_caching )
547 return VLC_SUCCESS;
548 p_mux->b_waiting_stream = false;
550 return p_mux->pf_mux( p_mux );
553 void sout_MuxFlush( sout_mux_t *p_mux, sout_input_t *p_input )
555 VLC_UNUSED(p_mux);
556 block_FifoEmpty( p_input->p_fifo );
559 /*****************************************************************************
560 * sout_MuxGetStream: find stream to be muxed
561 *****************************************************************************/
562 int sout_MuxGetStream( sout_mux_t *p_mux, unsigned i_blocks, mtime_t *pi_dts )
564 mtime_t i_dts = 0;
565 int i_stream = -1;
567 assert( i_blocks > 0 );
569 for( int i = 0; i < p_mux->i_nb_inputs; i++ )
571 sout_input_t *p_input = p_mux->pp_inputs[i];
572 block_t *p_data;
574 if( block_FifoCount( p_input->p_fifo ) < i_blocks )
576 if( (!p_mux->b_add_stream_any_time) &&
577 (p_input->p_fmt->i_cat != SPU_ES ) )
579 return -1;
581 /* FIXME: SPU muxing */
582 continue;
585 p_data = block_FifoShow( p_input->p_fifo );
586 if( i_stream < 0 || p_data->i_dts < i_dts )
588 i_stream = i;
589 i_dts = p_data->i_dts;
593 if( pi_dts ) *pi_dts = i_dts;
595 return i_stream;
599 /*****************************************************************************
601 *****************************************************************************/
602 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
604 char * psz_dup = strdup( psz_mrl );
605 char * psz_parser = psz_dup;
606 const char * psz_access;
607 const char * psz_way;
608 char * psz_name;
610 /* *** first parse psz_dest */
611 while( *psz_parser && *psz_parser != ':' )
613 if( *psz_parser == '{' )
615 while( *psz_parser && *psz_parser != '}' )
617 psz_parser++;
619 if( *psz_parser )
621 psz_parser++;
624 else
626 psz_parser++;
629 #if defined( _WIN32 ) || defined( __OS2__ )
630 if( psz_parser - psz_dup == 1 )
632 /* msg_Warn( p_sout, "drive letter %c: found in source string",
633 *psz_dup ) ; */
634 *psz_parser = '\0';
636 #endif
638 if( !*psz_parser )
640 psz_access = psz_way = "";
641 psz_name = psz_dup;
643 else
645 *psz_parser++ = '\0';
647 /* let's skip '//' */
648 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
650 psz_parser += 2 ;
653 psz_name = psz_parser ;
655 /* Come back to parse the access and mux plug-ins */
656 psz_parser = psz_dup;
658 if( !*psz_parser )
660 /* No access */
661 psz_access = "";
663 else if( *psz_parser == '/' )
665 /* No access */
666 psz_access = "";
667 psz_parser++;
669 else
671 psz_access = psz_parser;
673 while( *psz_parser && *psz_parser != '/' )
675 if( *psz_parser == '{' )
677 while( *psz_parser && *psz_parser != '}' )
679 psz_parser++;
681 if( *psz_parser )
683 psz_parser++;
686 else
688 psz_parser++;
692 if( *psz_parser == '/' )
694 *psz_parser++ = '\0';
698 if( !*psz_parser )
700 /* No mux */
701 psz_way = "";
703 else
705 psz_way = psz_parser;
709 p_mrl->psz_access = strdup( psz_access );
710 p_mrl->psz_way = strdup( psz_way );
711 p_mrl->psz_name = strdup( psz_name );
713 free( psz_dup );
714 return( VLC_SUCCESS );
718 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
719 static void mrl_Clean( mrl_t *p_mrl )
721 FREENULL( p_mrl->psz_access );
722 FREENULL( p_mrl->psz_way );
723 FREENULL( p_mrl->psz_name );
727 /****************************************************************************
728 ****************************************************************************
732 ****************************************************************************
733 ****************************************************************************/
735 /* Destroy a "stream_out" module */
736 static void sout_StreamDelete( sout_stream_t *p_stream )
738 sout_instance_t *p_sout = (sout_instance_t *)(p_stream->obj.parent);
740 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
742 p_sout->i_out_pace_nocontrol -= p_stream->pace_nocontrol;
744 if( p_stream->p_module != NULL )
745 module_unneed( p_stream, p_stream->p_module );
747 FREENULL( p_stream->psz_name );
749 config_ChainDestroy( p_stream->p_cfg );
751 msg_Dbg( p_stream, "destroying chain done" );
752 vlc_object_release( p_stream );
755 /* Destroy a "stream_out" modules chain
757 * p_first is the first module to be destroyed in the chain
758 * p_last is the last module to be destroyed
759 * if NULL, all modules are destroyed
760 * if not NULL, modules following it must be destroyed separately
762 void sout_StreamChainDelete(sout_stream_t *p_first, sout_stream_t *p_last)
764 while(p_first != NULL)
766 sout_stream_t *p_next = p_first->p_next;
768 sout_StreamDelete(p_first);
769 if(p_first == p_last)
770 break;
771 p_first = p_next;
775 /* Create a "stream_out" module, which may forward its ES to p_next module */
777 * XXX name and p_cfg are used (-> do NOT free them)
779 static sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_name,
780 config_chain_t *p_cfg, sout_stream_t *p_next)
782 sout_stream_t *p_stream;
784 assert(psz_name);
786 p_stream = vlc_custom_create( p_sout, sizeof( *p_stream ), "stream out" );
787 if( !p_stream )
788 return NULL;
790 p_stream->p_sout = p_sout;
791 p_stream->psz_name = psz_name;
792 p_stream->p_cfg = p_cfg;
793 p_stream->p_next = p_next;
794 p_stream->pf_flush = NULL;
795 p_stream->pf_control = NULL;
796 p_stream->pace_nocontrol = false;
797 p_stream->p_sys = NULL;
799 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
801 p_stream->p_module =
802 module_need( p_stream, "sout stream", p_stream->psz_name, true );
804 if( !p_stream->p_module )
806 /* those must be freed by the caller if creation failed */
807 p_stream->psz_name = NULL;
808 p_stream->p_cfg = NULL;
810 sout_StreamDelete( p_stream );
811 return NULL;
814 p_sout->i_out_pace_nocontrol += p_stream->pace_nocontrol;
815 return p_stream;
818 /* Creates a complete "stream_out" modules chain
820 * chain format: module1{option=*:option=*}[:module2{option=*:...}]
822 * The modules are created starting from the last one and linked together
823 * A pointer to the last module created is stored if pp_last isn't NULL, to
824 * make sure sout_StreamChainDelete doesn't delete modules created in another
825 * place.
827 * Returns a pointer to the first module.
829 sout_stream_t *sout_StreamChainNew(sout_instance_t *p_sout, const char *psz_chain,
830 sout_stream_t *p_next, sout_stream_t **pp_last)
832 if(!psz_chain || !*psz_chain)
834 if(pp_last) *pp_last = NULL;
835 return p_next;
838 char *psz_parser = strdup(psz_chain);
839 if(!psz_parser)
840 return NULL;
842 vlc_array_t cfg, name;
843 vlc_array_init(&cfg);
844 vlc_array_init(&name);
846 /* parse chain */
847 while(psz_parser)
849 config_chain_t *p_cfg;
850 char *psz_name;
851 char *psz_rest_chain = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
852 free( psz_parser );
853 psz_parser = psz_rest_chain;
855 vlc_array_append(&cfg, p_cfg);
856 vlc_array_append(&name, psz_name);
859 size_t i = vlc_array_count(&name);
860 vlc_array_t module;
861 vlc_array_init(&module);
862 while(i--)
864 p_next = sout_StreamNew( p_sout, vlc_array_item_at_index(&name, i),
865 vlc_array_item_at_index(&cfg, i), p_next);
867 if(!p_next)
868 goto error;
870 if(i == vlc_array_count(&name) - 1 && pp_last)
871 *pp_last = p_next; /* last module created in the chain */
873 vlc_array_append(&module, p_next);
876 vlc_array_clear(&name);
877 vlc_array_clear(&cfg);
878 vlc_array_clear(&module);
880 return p_next;
882 error:
884 i++; /* last module couldn't be created */
886 /* destroy all modules created, starting with the last one */
887 int modules = vlc_array_count(&module);
888 while(modules--)
889 sout_StreamDelete(vlc_array_item_at_index(&module, modules));
890 vlc_array_clear(&module);
892 /* then destroy all names and config which weren't destroyed by
893 * sout_StreamDelete */
894 while(i--)
896 free(vlc_array_item_at_index(&name, i));
897 config_ChainDestroy(vlc_array_item_at_index(&cfg, i));
899 vlc_array_clear(&name);
900 vlc_array_clear(&cfg);
902 return NULL;
905 static char *sout_stream_url_to_chain( bool b_sout_display,
906 const char *psz_url )
908 mrl_t mrl;
909 char *psz_chain;
911 mrl_Parse( &mrl, psz_url );
913 /* Check if the URLs goes to #rtp - otherwise we'll use #standard */
914 static const char rtplist[] = "dccp\0sctp\0tcp\0udplite\0";
915 for (const char *a = rtplist; *a; a += strlen (a) + 1)
916 if (strcmp (a, mrl.psz_access) == 0)
917 goto rtp;
919 if (strcmp (mrl.psz_access, "rtp") == 0)
921 char *port;
922 /* For historical reasons, rtp:// means RTP over UDP */
923 strcpy (mrl.psz_access, "udp");
924 rtp:
925 if (mrl.psz_name[0] == '[')
927 port = strstr (mrl.psz_name, "]:");
928 if (port != NULL)
929 port++;
931 else
932 port = strchr (mrl.psz_name, ':');
933 if (port != NULL)
934 *port++ = '\0'; /* erase ':' */
936 if (asprintf (&psz_chain,
937 "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s%s%s\"}",
938 mrl.psz_way, mrl.psz_access, mrl.psz_name,
939 port ? "\",port=\"" : "", port ? port : "") == -1)
940 psz_chain = NULL;
942 else
944 /* Convert the URL to a basic standard sout chain */
945 if (asprintf (&psz_chain,
946 "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
947 mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
948 psz_chain = NULL;
951 /* Duplicate and wrap if sout-display is on */
952 if (psz_chain && b_sout_display)
954 char *tmp;
955 if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", psz_chain) == -1)
956 tmp = NULL;
957 free (psz_chain);
958 psz_chain = tmp;
961 mrl_Clean( &mrl );
962 return psz_chain;
965 #undef sout_EncoderCreate
966 encoder_t *sout_EncoderCreate( vlc_object_t *p_this )
968 return vlc_custom_create( p_this, sizeof( encoder_t ), "encoder" );