std: cosmetic
[vlc/asuraparaju-public.git] / src / stream_output / stream_output.c
blob103d69089916e0c2ca6b52449beb47719c12d9b3
1 /*****************************************************************************
2 * stream_output.c : stream output module
3 *****************************************************************************
4 * Copyright (C) 2002-2007 the VideoLAN team
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
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., 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>
50 #include "input/input_interface.h"
52 #define VLC_CODEC_NULL VLC_FOURCC( 'n', 'u', 'l', 'l' )
54 #undef DEBUG_BUFFER
55 /*****************************************************************************
56 * Local prototypes
57 *****************************************************************************/
58 static char *sout_stream_url_to_chain( bool, const char * );
61 * Generic MRL parser
65 typedef struct
67 char *psz_access;
68 char *psz_way;
69 char *psz_name;
70 } mrl_t;
72 /* mrl_Parse: parse psz_mrl and fill p_mrl */
73 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl );
74 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
75 static void mrl_Clean( mrl_t *p_mrl );
77 /*****************************************************************************
78 * sout_NewInstance: creates a new stream output instance
79 *****************************************************************************/
80 sout_instance_t *__sout_NewInstance( vlc_object_t *p_parent, const char *psz_dest )
82 static const char typename[] = "stream output";
83 sout_instance_t *p_sout;
85 char *psz_chain;
86 if( psz_dest && psz_dest[0] == '#' )
88 psz_chain = strdup( &psz_dest[1] );
90 else
92 psz_chain = sout_stream_url_to_chain(
93 var_InheritBool(p_parent, "sout-display"), psz_dest );
95 if(!psz_chain)
96 return NULL;
98 /* *** Allocate descriptor *** */
99 p_sout = vlc_custom_create( p_parent, sizeof( *p_sout ),
100 VLC_OBJECT_GENERIC, typename );
101 if( p_sout == NULL )
102 return NULL;
104 msg_Dbg( p_sout, "using sout chain=`%s'", psz_chain );
106 /* *** init descriptor *** */
107 p_sout->psz_sout = strdup( psz_dest );
108 p_sout->p_meta = NULL;
109 p_sout->i_out_pace_nocontrol = 0;
110 p_sout->p_sys = NULL;
112 vlc_mutex_init( &p_sout->lock );
113 p_sout->p_stream = NULL;
115 /* attach it for inherit */
116 vlc_object_attach( p_sout, p_parent );
118 var_Create( p_sout, "sout-mux-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
120 p_sout->p_stream = sout_StreamChainNew( p_sout, psz_chain, NULL, NULL );
121 if( p_sout->p_stream )
123 free( psz_chain );
124 return p_sout;
127 msg_Err( p_sout, "stream chain failed for `%s'", psz_chain );
128 free( psz_chain );
130 FREENULL( p_sout->psz_sout );
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 /* delete meta */
148 if( p_sout->p_meta )
150 vlc_meta_Delete( p_sout->p_meta );
153 vlc_mutex_destroy( &p_sout->lock );
155 /* *** free structure *** */
156 vlc_object_release( p_sout );
159 /*****************************************************************************
161 *****************************************************************************/
162 void sout_UpdateStatistic( sout_instance_t *p_sout, sout_statistic_t i_type, int i_delta )
164 if( !libvlc_stats( p_sout ) )
165 return;
167 /* */
168 input_thread_t *p_input = vlc_object_find( p_sout, VLC_OBJECT_INPUT, FIND_PARENT );
169 if( !p_input )
170 return;
172 int i_input_type;
173 switch( i_type )
175 case SOUT_STATISTIC_DECODED_VIDEO:
176 i_input_type = SOUT_STATISTIC_DECODED_VIDEO;
177 break;
178 case SOUT_STATISTIC_DECODED_AUDIO:
179 i_input_type = SOUT_STATISTIC_DECODED_AUDIO;
180 break;
181 case SOUT_STATISTIC_DECODED_SUBTITLE:
182 i_input_type = SOUT_STATISTIC_DECODED_SUBTITLE;
183 break;
185 case SOUT_STATISTIC_SENT_PACKET:
186 i_input_type = SOUT_STATISTIC_SENT_PACKET;
187 break;
189 case SOUT_STATISTIC_SENT_BYTE:
190 i_input_type = SOUT_STATISTIC_SENT_BYTE;
191 break;
193 default:
194 msg_Err( p_sout, "Not yet supported statistic type %d", i_type );
195 vlc_object_release( p_input );
196 return;
199 input_UpdateStatistic( p_input, i_input_type, i_delta );
201 vlc_object_release( p_input );
203 /*****************************************************************************
204 * Packetizer/Input
205 *****************************************************************************/
206 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
207 es_format_t *p_fmt )
209 sout_packetizer_input_t *p_input;
211 /* *** create a packetizer input *** */
212 p_input = malloc( sizeof( sout_packetizer_input_t ) );
213 if( !p_input ) return NULL;
214 p_input->p_sout = p_sout;
215 p_input->p_fmt = p_fmt;
217 msg_Dbg( p_sout, "adding a new sout input (sout_input:%p)", p_input );
219 if( p_fmt->i_codec == VLC_CODEC_NULL )
221 vlc_object_release( p_sout );
222 return p_input;
225 /* *** add it to the stream chain */
226 vlc_mutex_lock( &p_sout->lock );
227 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
228 vlc_mutex_unlock( &p_sout->lock );
230 if( p_input->id == NULL )
232 free( p_input );
233 return NULL;
236 return( p_input );
239 /*****************************************************************************
241 *****************************************************************************/
242 int sout_InputDelete( sout_packetizer_input_t *p_input )
244 sout_instance_t *p_sout = p_input->p_sout;
246 msg_Dbg( p_sout, "removing a sout input (sout_input:%p)", p_input );
248 if( p_input->p_fmt->i_codec != VLC_CODEC_NULL )
250 vlc_mutex_lock( &p_sout->lock );
251 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
252 vlc_mutex_unlock( &p_sout->lock );
255 free( p_input );
257 return( VLC_SUCCESS);
260 /*****************************************************************************
262 *****************************************************************************/
263 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
264 block_t *p_buffer )
266 sout_instance_t *p_sout = p_input->p_sout;
267 int i_ret;
269 if( p_input->p_fmt->i_codec == VLC_CODEC_NULL )
271 block_Release( p_buffer );
272 return VLC_SUCCESS;
275 if( p_buffer->i_dts <= VLC_TS_INVALID )
277 msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
278 block_Release( p_buffer );
279 return VLC_SUCCESS;
282 vlc_mutex_lock( &p_sout->lock );
283 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
284 p_input->id, p_buffer );
285 vlc_mutex_unlock( &p_sout->lock );
287 return i_ret;
290 #undef sout_AccessOutNew
291 /*****************************************************************************
292 * sout_AccessOutNew: allocate a new access out
293 *****************************************************************************/
294 sout_access_out_t *sout_AccessOutNew( vlc_object_t *p_sout,
295 const char *psz_access, const char *psz_name )
297 static const char typename[] = "access out";
298 sout_access_out_t *p_access;
299 char *psz_next;
301 p_access = vlc_custom_create( p_sout, sizeof( *p_access ),
302 VLC_OBJECT_GENERIC, typename );
303 if( !p_access )
304 return NULL;
306 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
307 psz_access );
308 free( psz_next );
309 p_access->psz_path = strdup( psz_name ? psz_name : "" );
310 p_access->p_sys = NULL;
311 p_access->pf_seek = NULL;
312 p_access->pf_read = NULL;
313 p_access->pf_write = NULL;
314 p_access->pf_control = NULL;
315 p_access->p_module = NULL;
317 p_access->i_writes = 0;
318 p_access->i_sent_bytes = 0;
320 vlc_object_attach( p_access, p_sout );
322 p_access->p_module =
323 module_need( p_access, "sout access", p_access->psz_access, true );
325 if( !p_access->p_module )
327 free( p_access->psz_access );
328 free( p_access->psz_path );
329 vlc_object_release( p_access );
330 return( NULL );
333 return p_access;
335 /*****************************************************************************
336 * sout_AccessDelete: delete an access out
337 *****************************************************************************/
338 void sout_AccessOutDelete( sout_access_out_t *p_access )
340 if( p_access->p_module )
342 module_unneed( p_access, p_access->p_module );
344 free( p_access->psz_access );
346 config_ChainDestroy( p_access->p_cfg );
348 free( p_access->psz_path );
350 vlc_object_release( p_access );
353 /*****************************************************************************
354 * sout_AccessSeek:
355 *****************************************************************************/
356 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
358 return p_access->pf_seek( p_access, i_pos );
361 /*****************************************************************************
362 * sout_AccessRead:
363 *****************************************************************************/
364 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
366 return( p_access->pf_read ?
367 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
370 /*****************************************************************************
371 * sout_AccessWrite:
372 *****************************************************************************/
373 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
375 #if 0
376 const unsigned i_packets_gather = 30;
377 p_access->i_writes++;
378 p_access->i_sent_bytes += p_buffer->i_buffer;
379 if( (p_access->i_writes % i_packets_gather) == 0 )
381 sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_PACKET, i_packets_gather );
382 sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_BYTE, p_access->i_sent_bytes );
383 p_access->i_sent_bytes = 0;
385 #endif
386 return p_access->pf_write( p_access, p_buffer );
390 * sout_AccessOutControl
392 int sout_AccessOutControl (sout_access_out_t *access, int query, ...)
394 va_list ap;
395 int ret;
397 va_start (ap, query);
398 if (access->pf_control)
399 ret = access->pf_control (access, query, ap);
400 else
401 ret = VLC_EGENERIC;
402 va_end (ap);
403 return ret;
406 /*****************************************************************************
407 * sout_MuxNew: create a new mux
408 *****************************************************************************/
409 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, const char *psz_mux,
410 sout_access_out_t *p_access )
412 static const char typename[] = "mux";
413 sout_mux_t *p_mux;
414 char *psz_next;
416 p_mux = vlc_custom_create( p_sout, sizeof( *p_mux ), VLC_OBJECT_GENERIC,
417 typename);
418 if( p_mux == NULL )
419 return NULL;
421 p_mux->p_sout = p_sout;
422 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
423 free( psz_next );
425 p_mux->p_access = p_access;
426 p_mux->pf_control = NULL;
427 p_mux->pf_addstream = NULL;
428 p_mux->pf_delstream = NULL;
429 p_mux->pf_mux = NULL;
430 p_mux->i_nb_inputs = 0;
431 p_mux->pp_inputs = NULL;
433 p_mux->p_sys = NULL;
434 p_mux->p_module = NULL;
436 p_mux->b_add_stream_any_time = false;
437 p_mux->b_waiting_stream = true;
438 p_mux->i_add_stream_start = -1;
440 vlc_object_attach( p_mux, p_sout );
442 p_mux->p_module =
443 module_need( p_mux, "sout mux", p_mux->psz_mux, true );
445 if( p_mux->p_module == NULL )
447 FREENULL( p_mux->psz_mux );
449 vlc_object_release( p_mux );
450 return NULL;
453 /* *** probe mux capacity *** */
454 if( p_mux->pf_control )
456 int b_answer = false;
458 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
459 &b_answer ) )
461 b_answer = false;
464 if( b_answer )
466 msg_Dbg( p_sout, "muxer support adding stream at any time" );
467 p_mux->b_add_stream_any_time = true;
468 p_mux->b_waiting_stream = false;
470 /* If we control the output pace then it's better to wait before
471 * starting muxing (generates better streams/files). */
472 if( !p_sout->i_out_pace_nocontrol )
474 b_answer = true;
476 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
477 &b_answer ) )
479 b_answer = false;
482 if( b_answer )
484 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
485 "starting to mux" );
486 p_mux->b_waiting_stream = true;
491 return p_mux;
494 /*****************************************************************************
495 * sout_MuxDelete:
496 *****************************************************************************/
497 void sout_MuxDelete( sout_mux_t *p_mux )
499 if( p_mux->p_module )
501 module_unneed( p_mux, p_mux->p_module );
503 free( p_mux->psz_mux );
505 config_ChainDestroy( p_mux->p_cfg );
507 vlc_object_release( p_mux );
510 /*****************************************************************************
511 * sout_MuxAddStream:
512 *****************************************************************************/
513 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
515 sout_input_t *p_input;
517 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
519 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
520 "to this format). You can try increasing sout-mux-caching value" );
521 return NULL;
524 msg_Dbg( p_mux, "adding a new input" );
526 /* create a new sout input */
527 p_input = malloc( sizeof( sout_input_t ) );
528 if( !p_input )
529 return NULL;
530 p_input->p_sout = p_mux->p_sout;
531 p_input->p_fmt = p_fmt;
532 p_input->p_fifo = block_FifoNew();
533 p_input->p_sys = NULL;
535 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
536 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
538 msg_Err( p_mux, "cannot add this stream" );
539 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
540 block_FifoRelease( p_input->p_fifo );
541 free( p_input );
542 return NULL;
545 return p_input;
548 /*****************************************************************************
549 * sout_MuxDeleteStream:
550 *****************************************************************************/
551 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
553 int i_index;
555 if( p_mux->b_waiting_stream
556 && block_FifoCount( p_input->p_fifo ) > 0 )
558 /* We stop waiting, and call the muxer for taking care of the data
559 * before we remove this es */
560 p_mux->b_waiting_stream = false;
561 p_mux->pf_mux( p_mux );
564 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
565 if( i_index >= 0 )
567 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
569 msg_Err( p_mux, "cannot delete this stream from mux" );
572 /* remove the entry */
573 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
575 if( p_mux->i_nb_inputs == 0 )
577 msg_Warn( p_mux, "no more input streams for this mux" );
580 block_FifoRelease( p_input->p_fifo );
581 free( p_input );
585 /*****************************************************************************
586 * sout_MuxSendBuffer:
587 *****************************************************************************/
588 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
589 block_t *p_buffer )
591 block_FifoPut( p_input->p_fifo, p_buffer );
593 if( p_mux->p_sout->i_out_pace_nocontrol )
595 mtime_t current_date = mdate();
596 if ( current_date > p_buffer->i_dts )
597 msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
598 current_date - p_buffer->i_dts );
601 if( p_mux->b_waiting_stream )
603 const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * INT64_C(1000);
605 if( p_mux->i_add_stream_start < 0 )
606 p_mux->i_add_stream_start = p_buffer->i_dts;
608 /* Wait until we have enought data before muxing */
609 if( p_mux->i_add_stream_start < 0 ||
610 p_buffer->i_dts < p_mux->i_add_stream_start + i_caching )
611 return;
612 p_mux->b_waiting_stream = false;
614 p_mux->pf_mux( p_mux );
618 /*****************************************************************************
619 * sout_MuxGetStream: find stream to be muxed
620 *****************************************************************************/
621 int sout_MuxGetStream( sout_mux_t *p_mux, int i_blocks, mtime_t *pi_dts )
623 mtime_t i_dts = 0;
624 int i_stream = -1;
626 for( int i = 0; i < p_mux->i_nb_inputs; i++ )
628 sout_input_t *p_input = p_mux->pp_inputs[i];
629 block_t *p_data;
631 if( block_FifoCount( p_input->p_fifo ) < i_blocks )
633 if( p_input->p_fmt->i_cat != SPU_ES )
635 return -1;
637 /* FIXME: SPU muxing */
638 continue;
641 p_data = block_FifoShow( p_input->p_fifo );
642 if( i_stream < 0 || p_data->i_dts < i_dts )
644 i_stream = i;
645 i_dts = p_data->i_dts;
649 if( pi_dts ) *pi_dts = i_dts;
651 return i_stream;
655 /*****************************************************************************
657 *****************************************************************************/
658 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
660 char * psz_dup = strdup( psz_mrl );
661 char * psz_parser = psz_dup;
662 const char * psz_access;
663 const char * psz_way;
664 char * psz_name;
666 /* *** first parse psz_dest */
667 while( *psz_parser && *psz_parser != ':' )
669 if( *psz_parser == '{' )
671 while( *psz_parser && *psz_parser != '}' )
673 psz_parser++;
675 if( *psz_parser )
677 psz_parser++;
680 else
682 psz_parser++;
685 #if defined( WIN32 ) || defined( UNDER_CE )
686 if( psz_parser - psz_dup == 1 )
688 /* msg_Warn( p_sout, "drive letter %c: found in source string",
689 *psz_dup ) ; */
690 psz_parser = "";
692 #endif
694 if( !*psz_parser )
696 psz_access = psz_way = "";
697 psz_name = psz_dup;
699 else
701 *psz_parser++ = '\0';
703 /* let's skip '//' */
704 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
706 psz_parser += 2 ;
709 psz_name = psz_parser ;
711 /* Come back to parse the access and mux plug-ins */
712 psz_parser = psz_dup;
714 if( !*psz_parser )
716 /* No access */
717 psz_access = "";
719 else if( *psz_parser == '/' )
721 /* No access */
722 psz_access = "";
723 psz_parser++;
725 else
727 psz_access = psz_parser;
729 while( *psz_parser && *psz_parser != '/' )
731 if( *psz_parser == '{' )
733 while( *psz_parser && *psz_parser != '}' )
735 psz_parser++;
737 if( *psz_parser )
739 psz_parser++;
742 else
744 psz_parser++;
748 if( *psz_parser == '/' )
750 *psz_parser++ = '\0';
754 if( !*psz_parser )
756 /* No mux */
757 psz_way = "";
759 else
761 psz_way = psz_parser;
765 p_mrl->psz_access = strdup( psz_access );
766 p_mrl->psz_way = strdup( psz_way );
767 p_mrl->psz_name = strdup( psz_name );
769 free( psz_dup );
770 return( VLC_SUCCESS );
774 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
775 static void mrl_Clean( mrl_t *p_mrl )
777 FREENULL( p_mrl->psz_access );
778 FREENULL( p_mrl->psz_way );
779 FREENULL( p_mrl->psz_name );
783 /****************************************************************************
784 ****************************************************************************
788 ****************************************************************************
789 ****************************************************************************/
791 /* Destroy a "stream_out" module */
792 static void sout_StreamDelete( sout_stream_t *p_stream )
794 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
796 if( p_stream->p_module ) module_unneed( p_stream, p_stream->p_module );
798 FREENULL( p_stream->psz_name );
800 config_ChainDestroy( p_stream->p_cfg );
802 msg_Dbg( p_stream, "destroying chain done" );
803 vlc_object_release( p_stream );
806 /* Destroy a "stream_out" modules chain
808 * p_first is the first module to be destroyed in the chain
809 * p_last is the last module to be destroyed
810 * if NULL, all modules are destroyed
811 * if not NULL, modules following it must be destroyed separately
813 void sout_StreamChainDelete(sout_stream_t *p_first, sout_stream_t *p_last)
815 while(p_first != NULL)
817 sout_stream_t *p_next = p_first->p_next;
819 sout_StreamDelete(p_first);
820 if(p_first == p_last)
821 break;
822 p_first = p_next;
826 /* Create a "stream_out" module, which may forward its ES to p_next module */
828 * XXX name and p_cfg are used (-> do NOT free them)
830 static sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_name,
831 config_chain_t *p_cfg, sout_stream_t *p_next)
833 static const char typename[] = "stream out";
834 sout_stream_t *p_stream;
836 assert(psz_name);
838 p_stream = vlc_custom_create( p_sout, sizeof( *p_stream ),
839 VLC_OBJECT_GENERIC, typename );
840 if( !p_stream )
841 return NULL;
843 p_stream->p_sout = p_sout;
844 p_stream->p_sys = NULL;
845 p_stream->psz_name = psz_name;
846 p_stream->p_cfg = p_cfg;
847 p_stream->p_next = p_next;
849 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
851 vlc_object_attach( p_stream, p_sout );
853 p_stream->p_module =
854 module_need( p_stream, "sout stream", p_stream->psz_name, true );
856 if( !p_stream->p_module )
858 /* those must be freed by the caller if creation failed */
859 p_stream->psz_name = NULL;
860 p_stream->p_cfg = NULL;
862 sout_StreamDelete( p_stream );
863 return NULL;
866 return p_stream;
869 /* Creates a complete "stream_out" modules chain
871 * chain format: module1{option=*:option=*}[:module2{option=*:...}]
873 * The modules are created starting from the last one and linked together
874 * A pointer to the last module created is stored if pp_last isn't NULL, to
875 * make sure sout_StreamChainDelete doesn't delete modules created in another
876 * place.
878 * Returns a pointer to the first module.
880 sout_stream_t *sout_StreamChainNew(sout_instance_t *p_sout, char *psz_chain,
881 sout_stream_t *p_next, sout_stream_t **pp_last)
883 if(!psz_chain || !*psz_chain)
885 if(pp_last) *pp_last = NULL;
886 return p_next;
889 char *psz_parser = strdup(psz_chain);
890 if(!psz_parser)
891 return NULL;
893 vlc_array_t cfg, name;
894 vlc_array_init(&cfg);
895 vlc_array_init(&name);
897 /* parse chain */
898 while(psz_parser)
900 config_chain_t *p_cfg;
901 char *psz_name;
902 psz_chain = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
903 free( psz_parser );
904 psz_parser = psz_chain;
906 vlc_array_append(&cfg, p_cfg);
907 vlc_array_append(&name, psz_name);
910 int i = vlc_array_count(&name);
911 vlc_array_t module;
912 vlc_array_init(&module);
913 while(i--)
915 p_next = sout_StreamNew( p_sout, vlc_array_item_at_index(&name, i),
916 vlc_array_item_at_index(&cfg, i), p_next);
918 if(!p_next)
919 goto error;
921 if(i == vlc_array_count(&name) - 1 && pp_last)
922 *pp_last = p_next; /* last module created in the chain */
924 vlc_array_append(&module, p_next);
927 vlc_array_clear(&name);
928 vlc_array_clear(&cfg);
929 vlc_array_clear(&module);
931 return p_next;
933 error:
935 i++; /* last module couldn't be created */
937 /* destroy all modules created, starting with the last one */
938 int modules = vlc_array_count(&module);
939 while(modules--)
940 sout_StreamDelete(vlc_array_item_at_index(&module, modules));
941 vlc_array_clear(&module);
943 /* then destroy all names and config which weren't destroyed by
944 * sout_StreamDelete */
945 while(i--)
947 free(vlc_array_item_at_index(&name, i));
948 config_ChainDestroy(vlc_array_item_at_index(&cfg, i));
950 vlc_array_clear(&name);
951 vlc_array_clear(&cfg);
953 return NULL;
956 static char *sout_stream_url_to_chain( bool b_sout_display,
957 const char *psz_url )
959 mrl_t mrl;
960 char *psz_chain;
962 mrl_Parse( &mrl, psz_url );
964 /* Check if the URLs goes to #rtp - otherwise we'll use #standard */
965 static const char rtplist[] = "dccp\0sctp\0tcp\0udplite\0";
966 for (const char *a = rtplist; *a; a += strlen (a) + 1)
967 if (strcmp (a, mrl.psz_access) == 0)
968 goto rtp;
970 if (strcmp (mrl.psz_access, "rtp") == 0)
972 char *port;
973 /* For historical reasons, rtp:// means RTP over UDP */
974 strcpy (mrl.psz_access, "udp");
975 rtp:
976 if (mrl.psz_name[0] == '[')
978 port = strstr (mrl.psz_name, "]:");
979 if (port != NULL)
980 port++;
982 else
983 port = strchr (mrl.psz_name, ':');
984 if (port != NULL)
985 *port++ = '\0'; /* erase ':' */
987 if (asprintf (&psz_chain,
988 "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s%s%s\"}",
989 mrl.psz_way, mrl.psz_access, mrl.psz_name,
990 port ? "\",port=\"" : "", port ? port : "") == -1)
991 psz_chain = NULL;
993 else
995 /* Convert the URL to a basic standard sout chain */
996 if (asprintf (&psz_chain,
997 "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
998 mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
999 psz_chain = NULL;
1002 /* Duplicate and wrap if sout-display is on */
1003 if (psz_chain && b_sout_display)
1005 char *tmp;
1006 if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", psz_chain) == -1)
1007 tmp = NULL;
1008 free (psz_chain);
1009 psz_chain = tmp;
1012 mrl_Clean( &mrl );
1013 return psz_chain;
1016 #undef sout_EncoderCreate
1017 encoder_t *sout_EncoderCreate( vlc_object_t *p_this )
1019 static const char type[] = "encoder";
1020 return vlc_custom_create( p_this, sizeof( encoder_t ), VLC_OBJECT_GENERIC,
1021 type );