sout: remove NULL fourcc stuff and decref
[vlc.git] / src / stream_output / stream_output.c
blob87f3c7b79434722428c9e65a1741f6484a605d23
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 es_format_t *p_fmt )
159 sout_packetizer_input_t *p_input;
161 /* *** create a packetizer input *** */
162 p_input = malloc( sizeof( sout_packetizer_input_t ) );
163 if( !p_input ) return NULL;
164 p_input->p_sout = p_sout;
165 p_input->p_fmt = p_fmt;
167 msg_Dbg( p_sout, "adding a new sout input (sout_input: %p)",
168 (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 free( p_input );
178 return NULL;
181 return( p_input );
184 /*****************************************************************************
186 *****************************************************************************/
187 int sout_InputDelete( sout_packetizer_input_t *p_input )
189 sout_instance_t *p_sout = p_input->p_sout;
191 msg_Dbg( p_sout, "removing a sout input (sout_input: %p)",
192 (void *)p_input );
194 if( p_input->id )
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 );
201 free( p_input );
203 return( VLC_SUCCESS);
206 bool sout_InputIsEmpty( sout_packetizer_input_t *p_input )
208 sout_instance_t *p_sout = p_input->p_sout;
209 bool b;
211 vlc_mutex_lock( &p_sout->lock );
212 if( sout_StreamControl( p_sout->p_stream, SOUT_STREAM_EMPTY, &b ) != VLC_SUCCESS )
213 b = true;
214 vlc_mutex_unlock( &p_sout->lock );
215 return b;
218 void sout_InputFlush( sout_packetizer_input_t *p_input )
220 sout_instance_t *p_sout = p_input->p_sout;
222 vlc_mutex_lock( &p_sout->lock );
223 sout_StreamFlush( p_sout->p_stream, p_input->id );
224 vlc_mutex_unlock( &p_sout->lock );
227 /*****************************************************************************
229 *****************************************************************************/
230 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
231 block_t *p_buffer )
233 sout_instance_t *p_sout = p_input->p_sout;
234 int i_ret;
236 if( p_input->id == NULL )
238 block_Release( p_buffer );
239 return VLC_SUCCESS;
242 vlc_mutex_lock( &p_sout->lock );
243 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
244 p_input->id, p_buffer );
245 vlc_mutex_unlock( &p_sout->lock );
247 return i_ret;
250 #undef sout_AccessOutNew
251 /*****************************************************************************
252 * sout_AccessOutNew: allocate a new access out
253 *****************************************************************************/
254 sout_access_out_t *sout_AccessOutNew( vlc_object_t *p_sout,
255 const char *psz_access, const char *psz_name )
257 sout_access_out_t *p_access;
258 char *psz_next;
260 p_access = vlc_custom_create( p_sout, sizeof( *p_access ), "access out" );
261 if( !p_access )
262 return NULL;
264 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
265 psz_access );
266 free( psz_next );
267 p_access->psz_path = strdup( psz_name ? psz_name : "" );
268 p_access->p_sys = NULL;
269 p_access->pf_seek = NULL;
270 p_access->pf_read = NULL;
271 p_access->pf_write = NULL;
272 p_access->pf_control = NULL;
273 p_access->p_module = NULL;
275 p_access->p_module =
276 module_need( p_access, "sout access", p_access->psz_access, true );
278 if( !p_access->p_module )
280 free( p_access->psz_access );
281 free( p_access->psz_path );
282 vlc_object_release( p_access );
283 return( NULL );
286 return p_access;
288 /*****************************************************************************
289 * sout_AccessDelete: delete an access out
290 *****************************************************************************/
291 void sout_AccessOutDelete( sout_access_out_t *p_access )
293 if( p_access->p_module )
295 module_unneed( p_access, p_access->p_module );
297 free( p_access->psz_access );
299 config_ChainDestroy( p_access->p_cfg );
301 free( p_access->psz_path );
303 vlc_object_release( p_access );
306 /*****************************************************************************
307 * sout_AccessSeek:
308 *****************************************************************************/
309 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
311 return p_access->pf_seek( p_access, i_pos );
314 /*****************************************************************************
315 * sout_AccessRead:
316 *****************************************************************************/
317 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
319 return( p_access->pf_read ?
320 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
323 /*****************************************************************************
324 * sout_AccessWrite:
325 *****************************************************************************/
326 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
328 return p_access->pf_write( p_access, p_buffer );
332 * sout_AccessOutControl
334 int sout_AccessOutControl (sout_access_out_t *access, int query, ...)
336 va_list ap;
337 int ret;
339 va_start (ap, query);
340 if (access->pf_control)
341 ret = access->pf_control (access, query, ap);
342 else
343 ret = VLC_EGENERIC;
344 va_end (ap);
345 return ret;
348 /*****************************************************************************
349 * sout_MuxNew: create a new mux
350 *****************************************************************************/
351 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, const char *psz_mux,
352 sout_access_out_t *p_access )
354 sout_mux_t *p_mux;
355 char *psz_next;
357 p_mux = vlc_custom_create( p_sout, sizeof( *p_mux ), "mux" );
358 if( p_mux == NULL )
359 return NULL;
361 p_mux->p_sout = p_sout;
362 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
363 free( psz_next );
365 p_mux->p_access = p_access;
366 p_mux->pf_control = NULL;
367 p_mux->pf_addstream = NULL;
368 p_mux->pf_delstream = NULL;
369 p_mux->pf_mux = NULL;
370 p_mux->i_nb_inputs = 0;
371 p_mux->pp_inputs = NULL;
373 p_mux->p_sys = NULL;
374 p_mux->p_module = NULL;
376 p_mux->b_add_stream_any_time = false;
377 p_mux->b_waiting_stream = true;
378 p_mux->i_add_stream_start = -1;
380 p_mux->p_module =
381 module_need( p_mux, "sout mux", p_mux->psz_mux, true );
383 if( p_mux->p_module == NULL )
385 FREENULL( p_mux->psz_mux );
387 vlc_object_release( p_mux );
388 return NULL;
391 /* *** probe mux capacity *** */
392 if( p_mux->pf_control )
394 int b_answer = false;
396 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
397 &b_answer ) )
399 b_answer = false;
402 if( b_answer )
404 msg_Dbg( p_sout, "muxer support adding stream at any time" );
405 p_mux->b_add_stream_any_time = true;
406 p_mux->b_waiting_stream = false;
408 /* If we control the output pace then it's better to wait before
409 * starting muxing (generates better streams/files). */
410 if( !p_sout->i_out_pace_nocontrol )
412 b_answer = true;
414 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
415 &b_answer ) )
417 b_answer = false;
420 if( b_answer )
422 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
423 "starting to mux" );
424 p_mux->b_waiting_stream = true;
429 return p_mux;
432 /*****************************************************************************
433 * sout_MuxDelete:
434 *****************************************************************************/
435 void sout_MuxDelete( sout_mux_t *p_mux )
437 if( p_mux->p_module )
439 module_unneed( p_mux, p_mux->p_module );
441 free( p_mux->psz_mux );
443 config_ChainDestroy( p_mux->p_cfg );
445 vlc_object_release( p_mux );
448 /*****************************************************************************
449 * sout_MuxAddStream:
450 *****************************************************************************/
451 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, const es_format_t *p_fmt )
453 sout_input_t *p_input;
455 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
457 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
458 "to this format). You can try increasing sout-mux-caching value" );
459 return NULL;
462 msg_Dbg( p_mux, "adding a new input" );
464 /* create a new sout input */
465 p_input = malloc( sizeof( sout_input_t ) );
466 if( !p_input )
467 return NULL;
469 // FIXME: remove either fmt or p_fmt...
470 es_format_Copy( &p_input->fmt, p_fmt );
471 p_input->p_fmt = &p_input->fmt;
473 p_input->p_fifo = block_FifoNew();
474 p_input->p_sys = NULL;
476 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
477 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
479 msg_Err( p_mux, "cannot add this stream" );
480 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
481 block_FifoRelease( p_input->p_fifo );
482 es_format_Clean( &p_input->fmt );
483 free( p_input );
484 return NULL;
487 return p_input;
490 /*****************************************************************************
491 * sout_MuxDeleteStream:
492 *****************************************************************************/
493 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
495 int i_index;
497 if( p_mux->b_waiting_stream
498 && block_FifoCount( p_input->p_fifo ) > 0 )
500 /* We stop waiting, and call the muxer for taking care of the data
501 * before we remove this es */
502 p_mux->b_waiting_stream = false;
503 p_mux->pf_mux( p_mux );
506 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
507 if( i_index >= 0 )
509 p_mux->pf_delstream( p_mux, p_input );
511 /* remove the entry */
512 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
514 if( p_mux->i_nb_inputs == 0 )
516 msg_Warn( p_mux, "no more input streams for this mux" );
519 block_FifoRelease( p_input->p_fifo );
520 es_format_Clean( &p_input->fmt );
521 free( p_input );
525 /*****************************************************************************
526 * sout_MuxSendBuffer:
527 *****************************************************************************/
528 int sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
529 block_t *p_buffer )
531 mtime_t i_dts = p_buffer->i_dts;
532 block_FifoPut( p_input->p_fifo, p_buffer );
534 if( p_mux->p_sout->i_out_pace_nocontrol )
536 mtime_t current_date = mdate();
537 if ( current_date > i_dts )
538 msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
539 current_date - i_dts );
542 if( p_mux->b_waiting_stream )
544 const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * INT64_C(1000);
546 if( p_mux->i_add_stream_start < 0 )
547 p_mux->i_add_stream_start = i_dts;
549 /* Wait until we have enough data before muxing */
550 if( p_mux->i_add_stream_start < 0 ||
551 i_dts < p_mux->i_add_stream_start + i_caching )
552 return VLC_SUCCESS;
553 p_mux->b_waiting_stream = false;
555 return p_mux->pf_mux( p_mux );
558 void sout_MuxFlush( sout_mux_t *p_mux, sout_input_t *p_input )
560 VLC_UNUSED(p_mux);
561 block_FifoEmpty( p_input->p_fifo );
564 /*****************************************************************************
565 * sout_MuxGetStream: find stream to be muxed
566 *****************************************************************************/
567 int sout_MuxGetStream( sout_mux_t *p_mux, unsigned i_blocks, mtime_t *pi_dts )
569 mtime_t i_dts = 0;
570 int i_stream = -1;
572 assert( i_blocks > 0 );
574 for( int i = 0; i < p_mux->i_nb_inputs; i++ )
576 sout_input_t *p_input = p_mux->pp_inputs[i];
577 block_t *p_data;
579 if( block_FifoCount( p_input->p_fifo ) < i_blocks )
581 if( (!p_mux->b_add_stream_any_time) &&
582 (p_input->p_fmt->i_cat != SPU_ES ) )
584 return -1;
586 /* FIXME: SPU muxing */
587 continue;
590 p_data = block_FifoShow( p_input->p_fifo );
591 if( i_stream < 0 || p_data->i_dts < i_dts )
593 i_stream = i;
594 i_dts = p_data->i_dts;
598 if( pi_dts ) *pi_dts = i_dts;
600 return i_stream;
604 /*****************************************************************************
606 *****************************************************************************/
607 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
609 char * psz_dup = strdup( psz_mrl );
610 char * psz_parser = psz_dup;
611 const char * psz_access;
612 const char * psz_way;
613 char * psz_name;
615 /* *** first parse psz_dest */
616 while( *psz_parser && *psz_parser != ':' )
618 if( *psz_parser == '{' )
620 while( *psz_parser && *psz_parser != '}' )
622 psz_parser++;
624 if( *psz_parser )
626 psz_parser++;
629 else
631 psz_parser++;
634 #if defined( _WIN32 ) || defined( __OS2__ )
635 if( psz_parser - psz_dup == 1 )
637 /* msg_Warn( p_sout, "drive letter %c: found in source string",
638 *psz_dup ) ; */
639 *psz_parser = '\0';
641 #endif
643 if( !*psz_parser )
645 psz_access = psz_way = "";
646 psz_name = psz_dup;
648 else
650 *psz_parser++ = '\0';
652 /* let's skip '//' */
653 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
655 psz_parser += 2 ;
658 psz_name = psz_parser ;
660 /* Come back to parse the access and mux plug-ins */
661 psz_parser = psz_dup;
663 if( !*psz_parser )
665 /* No access */
666 psz_access = "";
668 else if( *psz_parser == '/' )
670 /* No access */
671 psz_access = "";
672 psz_parser++;
674 else
676 psz_access = psz_parser;
678 while( *psz_parser && *psz_parser != '/' )
680 if( *psz_parser == '{' )
682 while( *psz_parser && *psz_parser != '}' )
684 psz_parser++;
686 if( *psz_parser )
688 psz_parser++;
691 else
693 psz_parser++;
697 if( *psz_parser == '/' )
699 *psz_parser++ = '\0';
703 if( !*psz_parser )
705 /* No mux */
706 psz_way = "";
708 else
710 psz_way = psz_parser;
714 p_mrl->psz_access = strdup( psz_access );
715 p_mrl->psz_way = strdup( psz_way );
716 p_mrl->psz_name = strdup( psz_name );
718 free( psz_dup );
719 return( VLC_SUCCESS );
723 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
724 static void mrl_Clean( mrl_t *p_mrl )
726 FREENULL( p_mrl->psz_access );
727 FREENULL( p_mrl->psz_way );
728 FREENULL( p_mrl->psz_name );
732 /****************************************************************************
733 ****************************************************************************
737 ****************************************************************************
738 ****************************************************************************/
740 /* Destroy a "stream_out" module */
741 static void sout_StreamDelete( sout_stream_t *p_stream )
743 sout_instance_t *p_sout = (sout_instance_t *)(p_stream->obj.parent);
745 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
747 p_sout->i_out_pace_nocontrol -= p_stream->pace_nocontrol;
749 if( p_stream->p_module != NULL )
750 module_unneed( p_stream, p_stream->p_module );
752 FREENULL( p_stream->psz_name );
754 config_ChainDestroy( p_stream->p_cfg );
756 msg_Dbg( p_stream, "destroying chain done" );
757 vlc_object_release( p_stream );
760 /* Destroy a "stream_out" modules chain
762 * p_first is the first module to be destroyed in the chain
763 * p_last is the last module to be destroyed
764 * if NULL, all modules are destroyed
765 * if not NULL, modules following it must be destroyed separately
767 void sout_StreamChainDelete(sout_stream_t *p_first, sout_stream_t *p_last)
769 while(p_first != NULL)
771 sout_stream_t *p_next = p_first->p_next;
773 sout_StreamDelete(p_first);
774 if(p_first == p_last)
775 break;
776 p_first = p_next;
780 /* Create a "stream_out" module, which may forward its ES to p_next module */
782 * XXX name and p_cfg are used (-> do NOT free them)
784 static sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_name,
785 config_chain_t *p_cfg, sout_stream_t *p_next)
787 sout_stream_t *p_stream;
789 assert(psz_name);
791 p_stream = vlc_custom_create( p_sout, sizeof( *p_stream ), "stream out" );
792 if( !p_stream )
793 return NULL;
795 p_stream->p_sout = p_sout;
796 p_stream->psz_name = psz_name;
797 p_stream->p_cfg = p_cfg;
798 p_stream->p_next = p_next;
799 p_stream->pf_flush = NULL;
800 p_stream->pf_control = NULL;
801 p_stream->pace_nocontrol = false;
802 p_stream->p_sys = NULL;
804 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
806 p_stream->p_module =
807 module_need( p_stream, "sout stream", p_stream->psz_name, true );
809 if( !p_stream->p_module )
811 /* those must be freed by the caller if creation failed */
812 p_stream->psz_name = NULL;
813 p_stream->p_cfg = NULL;
815 sout_StreamDelete( p_stream );
816 return NULL;
819 p_sout->i_out_pace_nocontrol += p_stream->pace_nocontrol;
820 return p_stream;
823 /* Creates a complete "stream_out" modules chain
825 * chain format: module1{option=*:option=*}[:module2{option=*:...}]
827 * The modules are created starting from the last one and linked together
828 * A pointer to the last module created is stored if pp_last isn't NULL, to
829 * make sure sout_StreamChainDelete doesn't delete modules created in another
830 * place.
832 * Returns a pointer to the first module.
834 sout_stream_t *sout_StreamChainNew(sout_instance_t *p_sout, const char *psz_chain,
835 sout_stream_t *p_next, sout_stream_t **pp_last)
837 if(!psz_chain || !*psz_chain)
839 if(pp_last) *pp_last = NULL;
840 return p_next;
843 char *psz_parser = strdup(psz_chain);
844 if(!psz_parser)
845 return NULL;
847 vlc_array_t cfg, name;
848 vlc_array_init(&cfg);
849 vlc_array_init(&name);
851 /* parse chain */
852 while(psz_parser)
854 config_chain_t *p_cfg;
855 char *psz_name;
856 char *psz_rest_chain = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
857 free( psz_parser );
858 psz_parser = psz_rest_chain;
860 vlc_array_append(&cfg, p_cfg);
861 vlc_array_append(&name, psz_name);
864 size_t i = vlc_array_count(&name);
865 vlc_array_t module;
866 vlc_array_init(&module);
867 while(i--)
869 p_next = sout_StreamNew( p_sout, vlc_array_item_at_index(&name, i),
870 vlc_array_item_at_index(&cfg, i), p_next);
872 if(!p_next)
873 goto error;
875 if(i == vlc_array_count(&name) - 1 && pp_last)
876 *pp_last = p_next; /* last module created in the chain */
878 vlc_array_append(&module, p_next);
881 vlc_array_clear(&name);
882 vlc_array_clear(&cfg);
883 vlc_array_clear(&module);
885 return p_next;
887 error:
889 i++; /* last module couldn't be created */
891 /* destroy all modules created, starting with the last one */
892 int modules = vlc_array_count(&module);
893 while(modules--)
894 sout_StreamDelete(vlc_array_item_at_index(&module, modules));
895 vlc_array_clear(&module);
897 /* then destroy all names and config which weren't destroyed by
898 * sout_StreamDelete */
899 while(i--)
901 free(vlc_array_item_at_index(&name, i));
902 config_ChainDestroy(vlc_array_item_at_index(&cfg, i));
904 vlc_array_clear(&name);
905 vlc_array_clear(&cfg);
907 return NULL;
910 static char *sout_stream_url_to_chain( bool b_sout_display,
911 const char *psz_url )
913 mrl_t mrl;
914 char *psz_chain;
916 mrl_Parse( &mrl, psz_url );
918 /* Check if the URLs goes to #rtp - otherwise we'll use #standard */
919 static const char rtplist[] = "dccp\0sctp\0tcp\0udplite\0";
920 for (const char *a = rtplist; *a; a += strlen (a) + 1)
921 if (strcmp (a, mrl.psz_access) == 0)
922 goto rtp;
924 if (strcmp (mrl.psz_access, "rtp") == 0)
926 char *port;
927 /* For historical reasons, rtp:// means RTP over UDP */
928 strcpy (mrl.psz_access, "udp");
929 rtp:
930 if (mrl.psz_name[0] == '[')
932 port = strstr (mrl.psz_name, "]:");
933 if (port != NULL)
934 port++;
936 else
937 port = strchr (mrl.psz_name, ':');
938 if (port != NULL)
939 *port++ = '\0'; /* erase ':' */
941 if (asprintf (&psz_chain,
942 "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s%s%s\"}",
943 mrl.psz_way, mrl.psz_access, mrl.psz_name,
944 port ? "\",port=\"" : "", port ? port : "") == -1)
945 psz_chain = NULL;
947 else
949 /* Convert the URL to a basic standard sout chain */
950 if (asprintf (&psz_chain,
951 "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
952 mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
953 psz_chain = NULL;
956 /* Duplicate and wrap if sout-display is on */
957 if (psz_chain && b_sout_display)
959 char *tmp;
960 if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", psz_chain) == -1)
961 tmp = NULL;
962 free (psz_chain);
963 psz_chain = tmp;
966 mrl_Clean( &mrl );
967 return psz_chain;
970 #undef sout_EncoderCreate
971 encoder_t *sout_EncoderCreate( vlc_object_t *p_this )
973 return vlc_custom_create( p_this, sizeof( encoder_t ), "encoder" );