dav1d: picture_Clone the output picture
[vlc.git] / src / stream_output / stream_output.c
blobc09993fe58f00a31585af0ed24985397f35e8e91
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;
113 p_sout->b_wants_substreams = false;
115 vlc_mutex_init( &p_sout->lock );
116 p_sout->p_stream = NULL;
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 sout_StreamControl( p_sout->p_stream,
125 SOUT_STREAM_WANTS_SUBSTREAMS,
126 &p_sout->b_wants_substreams );
127 return p_sout;
130 msg_Err( p_sout, "stream chain failed for `%s'", psz_chain );
131 free( psz_chain );
133 FREENULL( p_sout->psz_sout );
135 vlc_mutex_destroy( &p_sout->lock );
136 vlc_object_release( p_sout );
137 return NULL;
140 /*****************************************************************************
141 * sout_DeleteInstance: delete a previously allocated instance
142 *****************************************************************************/
143 void sout_DeleteInstance( sout_instance_t * p_sout )
145 /* remove the stream out chain */
146 sout_StreamChainDelete( p_sout->p_stream, NULL );
148 /* *** free all string *** */
149 FREENULL( p_sout->psz_sout );
151 vlc_mutex_destroy( &p_sout->lock );
153 /* *** free structure *** */
154 vlc_object_release( p_sout );
157 /*****************************************************************************
158 * Packetizer/Input
159 *****************************************************************************/
160 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
161 const es_format_t *p_fmt )
163 sout_packetizer_input_t *p_input;
165 /* *** create a packetizer input *** */
166 if( !p_fmt->i_codec || !(p_input = malloc(sizeof(sout_packetizer_input_t))) )
167 return NULL;
169 p_input->p_sout = p_sout;
170 p_input->b_flushed = false;
172 msg_Dbg( p_sout, "adding a new sout input for `%4.4s` (sout_input: %p)",
173 (char*) &p_fmt->i_codec, (void *)p_input );
175 /* *** add it to the stream chain */
176 vlc_mutex_lock( &p_sout->lock );
177 p_input->id = sout_StreamIdAdd( p_sout->p_stream, p_fmt );
178 vlc_mutex_unlock( &p_sout->lock );
180 if( p_input->id == NULL )
182 msg_Warn( p_sout, "new sout input failed (sout_input: %p)",
183 (void *)p_input );
184 free( p_input );
185 p_input = NULL;
188 return( p_input );
191 /*****************************************************************************
193 *****************************************************************************/
194 int sout_InputDelete( sout_packetizer_input_t *p_input )
196 sout_instance_t *p_sout = p_input->p_sout;
198 msg_Dbg( p_sout, "removing a sout input (sout_input: %p)",
199 (void *)p_input );
201 vlc_mutex_lock( &p_sout->lock );
202 sout_StreamIdDel( p_sout->p_stream, p_input->id );
203 vlc_mutex_unlock( &p_sout->lock );
205 free( p_input );
207 return( VLC_SUCCESS);
210 bool sout_InputIsEmpty( sout_packetizer_input_t *p_input )
212 sout_instance_t *p_sout = p_input->p_sout;
213 bool b;
215 vlc_mutex_lock( &p_sout->lock );
216 if( sout_StreamControl( p_sout->p_stream, SOUT_STREAM_EMPTY, &b ) != VLC_SUCCESS )
217 b = true;
218 vlc_mutex_unlock( &p_sout->lock );
219 return b;
222 static int sout_InputControlVa( sout_packetizer_input_t *p_input, int i_query, va_list args )
224 sout_instance_t *p_sout = p_input->p_sout;
225 if( i_query == SOUT_INPUT_SET_SPU_HIGHLIGHT )
227 vlc_mutex_lock( &p_sout->lock );
228 int i_ret = sout_StreamControl( p_sout->p_stream,
229 SOUT_STREAM_ID_SPU_HIGHLIGHT,
230 p_input->id, va_arg(args, void *) );
231 vlc_mutex_unlock( &p_sout->lock );
232 return i_ret;
234 return VLC_EGENERIC;
237 int sout_InputControl( sout_packetizer_input_t *p_input, int i_query, ... )
239 va_list args;
240 int i_result;
242 va_start( args, i_query );
243 i_result = sout_InputControlVa( p_input, i_query, args );
244 va_end( args );
245 return i_result;
248 void sout_InputFlush( sout_packetizer_input_t *p_input )
250 sout_instance_t *p_sout = p_input->p_sout;
252 vlc_mutex_lock( &p_sout->lock );
253 sout_StreamFlush( p_sout->p_stream, p_input->id );
254 vlc_mutex_unlock( &p_sout->lock );
255 p_input->b_flushed = true;
258 /*****************************************************************************
260 *****************************************************************************/
261 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
262 block_t *p_buffer )
264 sout_instance_t *p_sout = p_input->p_sout;
265 int i_ret;
267 if( p_input->b_flushed )
269 p_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
270 p_input->b_flushed = false;
272 vlc_mutex_lock( &p_sout->lock );
273 i_ret = sout_StreamIdSend( p_sout->p_stream, p_input->id, p_buffer );
274 vlc_mutex_unlock( &p_sout->lock );
276 return i_ret;
279 #undef sout_AccessOutNew
280 /*****************************************************************************
281 * sout_AccessOutNew: allocate a new access out
282 *****************************************************************************/
283 sout_access_out_t *sout_AccessOutNew( vlc_object_t *p_sout,
284 const char *psz_access, const char *psz_name )
286 sout_access_out_t *p_access;
287 char *psz_next;
289 p_access = vlc_custom_create( p_sout, sizeof( *p_access ), "access out" );
290 if( !p_access )
291 return NULL;
293 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
294 psz_access );
295 free( psz_next );
296 p_access->psz_path = strdup( psz_name ? psz_name : "" );
297 if( unlikely(p_access->psz_path == NULL) )
298 goto error;
299 p_access->p_sys = NULL;
300 p_access->pf_seek = NULL;
301 p_access->pf_read = NULL;
302 p_access->pf_write = NULL;
303 p_access->pf_control = NULL;
304 p_access->p_module = NULL;
306 p_access->p_module =
307 module_need( p_access, "sout access", p_access->psz_access, true );
309 if( !p_access->p_module )
311 free( p_access->psz_path );
312 error:
313 free( p_access->psz_access );
314 vlc_object_release( p_access );
315 return( NULL );
318 return p_access;
320 /*****************************************************************************
321 * sout_AccessDelete: delete an access out
322 *****************************************************************************/
323 void sout_AccessOutDelete( sout_access_out_t *p_access )
325 if( p_access->p_module )
327 module_unneed( p_access, p_access->p_module );
329 free( p_access->psz_access );
331 config_ChainDestroy( p_access->p_cfg );
333 free( p_access->psz_path );
335 vlc_object_release( p_access );
338 /*****************************************************************************
339 * sout_AccessSeek:
340 *****************************************************************************/
341 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
343 if (p_access->pf_seek == NULL)
344 return VLC_EGENERIC;
345 return p_access->pf_seek( p_access, i_pos );
348 /*****************************************************************************
349 * sout_AccessRead:
350 *****************************************************************************/
351 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
353 return( p_access->pf_read ?
354 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
357 /*****************************************************************************
358 * sout_AccessWrite:
359 *****************************************************************************/
360 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
362 return p_access->pf_write( p_access, p_buffer );
366 * sout_AccessOutControl
368 int sout_AccessOutControl (sout_access_out_t *access, int query, ...)
370 va_list ap;
371 int ret;
373 va_start (ap, query);
374 if (access->pf_control)
375 ret = access->pf_control (access, query, ap);
376 else
377 ret = VLC_EGENERIC;
378 va_end (ap);
379 return ret;
382 /*****************************************************************************
383 * sout_MuxNew: create a new mux
384 *****************************************************************************/
385 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, const char *psz_mux,
386 sout_access_out_t *p_access )
388 sout_mux_t *p_mux;
389 char *psz_next;
391 p_mux = vlc_custom_create( p_sout, sizeof( *p_mux ), "mux" );
392 if( p_mux == NULL )
393 return NULL;
395 p_mux->p_sout = p_sout;
396 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
397 free( psz_next );
399 p_mux->p_access = p_access;
400 p_mux->pf_control = NULL;
401 p_mux->pf_addstream = NULL;
402 p_mux->pf_delstream = NULL;
403 p_mux->pf_mux = NULL;
404 p_mux->i_nb_inputs = 0;
405 p_mux->pp_inputs = NULL;
407 p_mux->p_sys = NULL;
408 p_mux->p_module = NULL;
410 p_mux->b_add_stream_any_time = false;
411 p_mux->b_waiting_stream = true;
412 p_mux->i_add_stream_start = VLC_TICK_INVALID;
414 p_mux->p_module =
415 module_need( p_mux, "sout mux", p_mux->psz_mux, true );
417 if( p_mux->p_module == NULL )
419 FREENULL( p_mux->psz_mux );
421 vlc_object_release( p_mux );
422 return NULL;
425 /* *** probe mux capacity *** */
426 if( p_mux->pf_control )
428 int b_answer = false;
430 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
431 &b_answer ) )
433 b_answer = false;
436 if( b_answer )
438 msg_Dbg( p_sout, "muxer support adding stream at any time" );
439 p_mux->b_add_stream_any_time = true;
440 p_mux->b_waiting_stream = false;
442 /* If we control the output pace then it's better to wait before
443 * starting muxing (generates better streams/files). */
444 if( !p_sout->i_out_pace_nocontrol )
446 b_answer = true;
448 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
449 &b_answer ) )
451 b_answer = false;
454 if( b_answer )
456 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
457 "starting to mux" );
458 p_mux->b_waiting_stream = true;
463 return p_mux;
466 /*****************************************************************************
467 * sout_MuxDelete:
468 *****************************************************************************/
469 void sout_MuxDelete( sout_mux_t *p_mux )
471 if( p_mux->p_module )
473 module_unneed( p_mux, p_mux->p_module );
475 free( p_mux->psz_mux );
477 config_ChainDestroy( p_mux->p_cfg );
479 vlc_object_release( p_mux );
482 /*****************************************************************************
483 * sout_MuxAddStream:
484 *****************************************************************************/
485 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, const es_format_t *p_fmt )
487 sout_input_t *p_input;
489 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
491 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
492 "to this format). You can try increasing sout-mux-caching value" );
493 return NULL;
496 msg_Dbg( p_mux, "adding a new input" );
498 /* create a new sout input */
499 p_input = malloc( sizeof( sout_input_t ) );
500 if( !p_input )
501 return NULL;
503 // FIXME: remove either fmt or p_fmt...
504 es_format_Copy( &p_input->fmt, p_fmt );
505 p_input->p_fmt = &p_input->fmt;
507 p_input->p_fifo = block_FifoNew();
508 p_input->p_sys = NULL;
510 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
511 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
513 msg_Err( p_mux, "cannot add this stream" );
514 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
515 block_FifoRelease( p_input->p_fifo );
516 es_format_Clean( &p_input->fmt );
517 free( p_input );
518 return NULL;
521 return p_input;
524 /*****************************************************************************
525 * sout_MuxDeleteStream:
526 *****************************************************************************/
527 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
529 int i_index;
531 if( p_mux->b_waiting_stream
532 && block_FifoCount( p_input->p_fifo ) > 0 )
534 /* We stop waiting, and call the muxer for taking care of the data
535 * before we remove this es */
536 p_mux->b_waiting_stream = false;
537 p_mux->pf_mux( p_mux );
540 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
541 if( i_index >= 0 )
543 /* remove the entry */
544 TAB_ERASE( p_mux->i_nb_inputs, p_mux->pp_inputs, i_index );
546 p_mux->pf_delstream( p_mux, p_input );
548 if( p_mux->i_nb_inputs == 0 )
550 msg_Warn( p_mux, "no more input streams for this mux" );
553 block_FifoRelease( p_input->p_fifo );
554 es_format_Clean( &p_input->fmt );
555 free( p_input );
559 /*****************************************************************************
560 * sout_MuxSendBuffer:
561 *****************************************************************************/
562 int sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
563 block_t *p_buffer )
565 vlc_tick_t i_dts = p_buffer->i_dts;
566 block_FifoPut( p_input->p_fifo, p_buffer );
568 if( p_mux->p_sout->i_out_pace_nocontrol )
570 vlc_tick_t current_date = vlc_tick_now();
571 if ( current_date > i_dts )
572 msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
573 current_date - i_dts );
576 if( i_dts == VLC_TICK_INVALID )
577 i_dts = p_buffer->i_pts;
579 if( p_mux->b_waiting_stream && i_dts != VLC_TICK_INVALID )
581 const vlc_tick_t i_caching = VLC_TICK_FROM_MS(var_GetInteger( p_mux->p_sout, "sout-mux-caching" ));
583 if( p_mux->i_add_stream_start == VLC_TICK_INVALID )
584 p_mux->i_add_stream_start = i_dts;
586 /* Wait until we have enough data before muxing */
587 if( llabs( i_dts - p_mux->i_add_stream_start ) < i_caching )
588 return VLC_SUCCESS;
589 p_mux->b_waiting_stream = false;
591 return p_mux->pf_mux( p_mux );
594 void sout_MuxFlush( sout_mux_t *p_mux, sout_input_t *p_input )
596 VLC_UNUSED(p_mux);
597 block_FifoEmpty( p_input->p_fifo );
600 /*****************************************************************************
601 * sout_MuxGetStream: find stream to be muxed
602 *****************************************************************************/
603 int sout_MuxGetStream( sout_mux_t *p_mux, unsigned i_blocks, vlc_tick_t *pi_dts )
605 vlc_tick_t i_dts = 0;
606 int i_stream = -1;
608 assert( i_blocks > 0 );
610 for( int i = 0; i < p_mux->i_nb_inputs; i++ )
612 sout_input_t *p_input = p_mux->pp_inputs[i];
613 block_t *p_data;
615 if( block_FifoCount( p_input->p_fifo ) < i_blocks )
617 if( (!p_mux->b_add_stream_any_time) &&
618 (p_input->p_fmt->i_cat != SPU_ES ) )
620 return -1;
622 /* FIXME: SPU muxing */
623 continue;
626 p_data = block_FifoShow( p_input->p_fifo );
627 if( i_stream < 0 || p_data->i_dts < i_dts )
629 i_stream = i;
630 i_dts = p_data->i_dts;
634 if( pi_dts ) *pi_dts = i_dts;
636 return i_stream;
640 /*****************************************************************************
642 *****************************************************************************/
643 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
645 char * psz_dup = strdup( psz_mrl );
646 char * psz_parser = psz_dup;
647 const char * psz_access;
648 const char * psz_way;
649 char * psz_name;
651 /* *** first parse psz_dest */
652 while( *psz_parser && *psz_parser != ':' )
654 if( *psz_parser == '{' )
656 while( *psz_parser && *psz_parser != '}' )
658 psz_parser++;
660 if( *psz_parser )
662 psz_parser++;
665 else
667 psz_parser++;
670 #if defined( _WIN32 ) || defined( __OS2__ )
671 if( psz_parser - psz_dup == 1 )
673 /* msg_Warn( p_sout, "drive letter %c: found in source string",
674 *psz_dup ) ; */
675 *psz_parser = '\0';
677 #endif
679 if( !*psz_parser )
681 psz_access = psz_way = "";
682 psz_name = psz_dup;
684 else
686 *psz_parser++ = '\0';
688 /* let's skip '//' */
689 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
691 psz_parser += 2 ;
694 psz_name = psz_parser ;
696 /* Come back to parse the access and mux plug-ins */
697 psz_parser = psz_dup;
699 if( !*psz_parser )
701 /* No access */
702 psz_access = "";
704 else if( *psz_parser == '/' )
706 /* No access */
707 psz_access = "";
708 psz_parser++;
710 else
712 psz_access = psz_parser;
714 while( *psz_parser && *psz_parser != '/' )
716 if( *psz_parser == '{' )
718 while( *psz_parser && *psz_parser != '}' )
720 psz_parser++;
722 if( *psz_parser )
724 psz_parser++;
727 else
729 psz_parser++;
733 if( *psz_parser == '/' )
735 *psz_parser++ = '\0';
739 if( !*psz_parser )
741 /* No mux */
742 psz_way = "";
744 else
746 psz_way = psz_parser;
750 p_mrl->psz_access = strdup( psz_access );
751 p_mrl->psz_way = strdup( psz_way );
752 p_mrl->psz_name = strdup( psz_name );
754 free( psz_dup );
755 return( VLC_SUCCESS );
759 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
760 static void mrl_Clean( mrl_t *p_mrl )
762 FREENULL( p_mrl->psz_access );
763 FREENULL( p_mrl->psz_way );
764 FREENULL( p_mrl->psz_name );
768 /****************************************************************************
769 ****************************************************************************
773 ****************************************************************************
774 ****************************************************************************/
776 /* Destroy a "stream_out" module */
777 static void sout_StreamDelete( sout_stream_t *p_stream )
779 sout_instance_t *p_sout = (sout_instance_t *)(p_stream->obj.parent);
781 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
783 p_sout->i_out_pace_nocontrol -= p_stream->pace_nocontrol;
785 if( p_stream->p_module != NULL )
786 module_unneed( p_stream, p_stream->p_module );
788 FREENULL( p_stream->psz_name );
790 config_ChainDestroy( p_stream->p_cfg );
792 msg_Dbg( p_stream, "destroying chain done" );
793 vlc_object_release( p_stream );
796 /* Destroy a "stream_out" modules chain
798 * p_first is the first module to be destroyed in the chain
799 * p_last is the last module to be destroyed
800 * if NULL, all modules are destroyed
801 * if not NULL, modules following it must be destroyed separately
803 void sout_StreamChainDelete(sout_stream_t *p_first, sout_stream_t *p_last)
805 while(p_first != NULL)
807 sout_stream_t *p_next = p_first->p_next;
809 sout_StreamDelete(p_first);
810 if(p_first == p_last)
811 break;
812 p_first = p_next;
816 /* Create a "stream_out" module, which may forward its ES to p_next module */
818 * XXX name and p_cfg are used (-> do NOT free them)
820 static sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_name,
821 config_chain_t *p_cfg, sout_stream_t *p_next)
823 sout_stream_t *p_stream;
825 assert(psz_name);
827 p_stream = vlc_custom_create( p_sout, sizeof( *p_stream ), "stream out" );
828 if( !p_stream )
829 return NULL;
831 p_stream->p_sout = p_sout;
832 p_stream->psz_name = psz_name;
833 p_stream->p_cfg = p_cfg;
834 p_stream->p_next = p_next;
835 p_stream->pf_flush = NULL;
836 p_stream->pf_control = NULL;
837 p_stream->pace_nocontrol = false;
838 p_stream->p_sys = NULL;
840 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
842 p_stream->p_module =
843 module_need( p_stream, "sout stream", p_stream->psz_name, true );
845 if( !p_stream->p_module )
847 /* those must be freed by the caller if creation failed */
848 p_stream->psz_name = NULL;
849 p_stream->p_cfg = NULL;
851 sout_StreamDelete( p_stream );
852 return NULL;
855 p_sout->i_out_pace_nocontrol += p_stream->pace_nocontrol;
856 return p_stream;
859 /* Creates a complete "stream_out" modules chain
861 * chain format: module1{option=*:option=*}[:module2{option=*:...}]
863 * The modules are created starting from the last one and linked together
864 * A pointer to the last module created is stored if pp_last isn't NULL, to
865 * make sure sout_StreamChainDelete doesn't delete modules created in another
866 * place.
868 * Returns a pointer to the first module.
870 sout_stream_t *sout_StreamChainNew(sout_instance_t *p_sout, const char *psz_chain,
871 sout_stream_t *p_next, sout_stream_t **pp_last)
873 if(!psz_chain || !*psz_chain)
875 if(pp_last) *pp_last = NULL;
876 return p_next;
879 char *psz_parser = strdup(psz_chain);
880 if(!psz_parser)
881 return NULL;
883 vlc_array_t cfg, name;
884 vlc_array_init(&cfg);
885 vlc_array_init(&name);
887 /* parse chain */
888 while(psz_parser)
890 config_chain_t *p_cfg;
891 char *psz_name;
892 char *psz_rest_chain = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
893 free( psz_parser );
894 psz_parser = psz_rest_chain;
896 vlc_array_append_or_abort(&cfg, p_cfg);
897 vlc_array_append_or_abort(&name, psz_name);
900 size_t i = vlc_array_count(&name);
901 vlc_array_t module;
902 vlc_array_init(&module);
903 while(i--)
905 p_next = sout_StreamNew( p_sout, vlc_array_item_at_index(&name, i),
906 vlc_array_item_at_index(&cfg, i), p_next);
908 if(!p_next)
909 goto error;
911 if(i == vlc_array_count(&name) - 1 && pp_last)
912 *pp_last = p_next; /* last module created in the chain */
914 vlc_array_append_or_abort(&module, p_next);
917 vlc_array_clear(&name);
918 vlc_array_clear(&cfg);
919 vlc_array_clear(&module);
921 return p_next;
923 error:
925 i++; /* last module couldn't be created */
927 /* destroy all modules created, starting with the last one */
928 int modules = vlc_array_count(&module);
929 while(modules--)
930 sout_StreamDelete(vlc_array_item_at_index(&module, modules));
931 vlc_array_clear(&module);
933 /* then destroy all names and config which weren't destroyed by
934 * sout_StreamDelete */
935 while(i--)
937 free(vlc_array_item_at_index(&name, i));
938 config_ChainDestroy(vlc_array_item_at_index(&cfg, i));
940 vlc_array_clear(&name);
941 vlc_array_clear(&cfg);
943 return NULL;
946 static char *sout_stream_url_to_chain( bool b_sout_display,
947 const char *psz_url )
949 mrl_t mrl;
950 char *psz_chain;
952 mrl_Parse( &mrl, psz_url );
954 /* Check if the URLs goes to #rtp - otherwise we'll use #standard */
955 static const char rtplist[] = "dccp\0sctp\0tcp\0udplite\0";
956 for (const char *a = rtplist; *a; a += strlen (a) + 1)
957 if (strcmp (a, mrl.psz_access) == 0)
958 goto rtp;
960 if (strcmp (mrl.psz_access, "rtp") == 0)
962 char *port;
963 /* For historical reasons, rtp:// means RTP over UDP */
964 strcpy (mrl.psz_access, "udp");
965 rtp:
966 if (mrl.psz_name[0] == '[')
968 port = strstr (mrl.psz_name, "]:");
969 if (port != NULL)
970 port++;
972 else
973 port = strchr (mrl.psz_name, ':');
974 if (port != NULL)
975 *port++ = '\0'; /* erase ':' */
977 if (asprintf (&psz_chain,
978 "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s%s%s\"}",
979 mrl.psz_way, mrl.psz_access, mrl.psz_name,
980 port ? "\",port=\"" : "", port ? port : "") == -1)
981 psz_chain = NULL;
983 else
985 /* Convert the URL to a basic standard sout chain */
986 if (asprintf (&psz_chain,
987 "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
988 mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
989 psz_chain = NULL;
992 /* Duplicate and wrap if sout-display is on */
993 if (psz_chain && b_sout_display)
995 char *tmp;
996 if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", psz_chain) == -1)
997 tmp = NULL;
998 free (psz_chain);
999 psz_chain = tmp;
1002 mrl_Clean( &mrl );
1003 return psz_chain;
1006 #undef sout_EncoderCreate
1007 encoder_t *sout_EncoderCreate( vlc_object_t *p_this )
1009 return vlc_custom_create( p_this, sizeof( encoder_t ), "encoder" );