]> git.sesse.net Git - vlc/blob - src/stream_output/stream_output.c
LGPL
[vlc] / src / stream_output / stream_output.c
1 /*****************************************************************************
2  * stream_output.c : stream output module
3  *****************************************************************************
4  * Copyright (C) 2002-2007 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Eric Petit <titer@videolan.org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <assert.h>
35
36 #include <vlc_common.h>
37
38 #include <stdlib.h>                                                /* free() */
39 #include <stdio.h>                                              /* sprintf() */
40 #include <string.h>
41
42 #include <vlc_sout.h>
43
44 #include "stream_output.h"
45
46 #include <vlc_meta.h>
47 #include <vlc_block.h>
48 #include <vlc_codec.h>
49 #include <vlc_modules.h>
50
51 #include "input/input_interface.h"
52
53 #define VLC_CODEC_NULL VLC_FOURCC( 'n', 'u', 'l', 'l' )
54
55 #undef DEBUG_BUFFER
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 static char *sout_stream_url_to_chain( bool, const char * );
60
61 /*
62  * Generic MRL parser
63  *
64  */
65
66 typedef struct
67 {
68     char *psz_access;
69     char *psz_way;
70     char *psz_name;
71 } mrl_t;
72
73 /* mrl_Parse: parse psz_mrl and fill p_mrl */
74 static int  mrl_Parse( mrl_t *p_mrl, const char *psz_mrl );
75 /* mrl_Clean: clean p_mrl  after a call to mrl_Parse */
76 static void mrl_Clean( mrl_t *p_mrl );
77
78 #undef sout_NewInstance
79
80 /*****************************************************************************
81  * sout_NewInstance: creates a new stream output instance
82  *****************************************************************************/
83 sout_instance_t *sout_NewInstance( vlc_object_t *p_parent, const char *psz_dest )
84 {
85     sout_instance_t *p_sout;
86
87     char *psz_chain;
88     if( psz_dest && psz_dest[0] == '#' )
89     {
90         psz_chain = strdup( &psz_dest[1] );
91     }
92     else
93     {
94         psz_chain = sout_stream_url_to_chain(
95             var_InheritBool(p_parent, "sout-display"), psz_dest );
96     }
97     if(!psz_chain)
98         return NULL;
99
100     /* *** Allocate descriptor *** */
101     p_sout = vlc_custom_create( p_parent, sizeof( *p_sout ), "stream output" );
102     if( p_sout == NULL )
103         return NULL;
104
105     msg_Dbg( p_sout, "using sout chain=`%s'", psz_chain );
106
107     /* *** init descriptor *** */
108     p_sout->psz_sout    = strdup( psz_dest );
109     p_sout->p_meta      = NULL;
110     p_sout->i_out_pace_nocontrol = 0;
111     p_sout->p_sys       = NULL;
112
113     vlc_mutex_init( &p_sout->lock );
114     p_sout->p_stream = NULL;
115
116     var_Create( p_sout, "sout-mux-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
117
118     p_sout->p_stream = sout_StreamChainNew( p_sout, psz_chain, NULL, NULL );
119     if( p_sout->p_stream )
120     {
121         free( psz_chain );
122         return p_sout;
123     }
124
125     msg_Err( p_sout, "stream chain failed for `%s'", psz_chain );
126     free( psz_chain );
127
128     FREENULL( p_sout->psz_sout );
129
130     vlc_object_release( p_sout );
131     return NULL;
132 }
133
134 /*****************************************************************************
135  * sout_DeleteInstance: delete a previously allocated instance
136  *****************************************************************************/
137 void sout_DeleteInstance( sout_instance_t * p_sout )
138 {
139     /* remove the stream out chain */
140     sout_StreamChainDelete( p_sout->p_stream, NULL );
141
142     /* *** free all string *** */
143     FREENULL( p_sout->psz_sout );
144
145     /* delete meta */
146     if( p_sout->p_meta )
147     {
148         vlc_meta_Delete( p_sout->p_meta );
149     }
150
151     vlc_mutex_destroy( &p_sout->lock );
152
153     /* *** free structure *** */
154     vlc_object_release( p_sout );
155 }
156
157 /*****************************************************************************
158  * Packetizer/Input
159  *****************************************************************************/
160 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
161                                         es_format_t *p_fmt )
162 {
163     sout_packetizer_input_t *p_input;
164
165     /* *** create a packetizer input *** */
166     p_input         = malloc( sizeof( sout_packetizer_input_t ) );
167     if( !p_input )  return NULL;
168     p_input->p_sout = p_sout;
169     p_input->p_fmt  = p_fmt;
170
171     msg_Dbg( p_sout, "adding a new sout input (sout_input:%p)", p_input );
172
173     if( p_fmt->i_codec == VLC_CODEC_NULL )
174     {
175         vlc_object_release( p_sout );
176         return p_input;
177     }
178
179     /* *** add it to the stream chain */
180     vlc_mutex_lock( &p_sout->lock );
181     p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
182     vlc_mutex_unlock( &p_sout->lock );
183
184     if( p_input->id == NULL )
185     {
186         free( p_input );
187         return NULL;
188     }
189
190     return( p_input );
191 }
192
193 /*****************************************************************************
194  *
195  *****************************************************************************/
196 int sout_InputDelete( sout_packetizer_input_t *p_input )
197 {
198     sout_instance_t     *p_sout = p_input->p_sout;
199
200     msg_Dbg( p_sout, "removing a sout input (sout_input:%p)", p_input );
201
202     if( p_input->p_fmt->i_codec != VLC_CODEC_NULL )
203     {
204         vlc_mutex_lock( &p_sout->lock );
205         p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
206         vlc_mutex_unlock( &p_sout->lock );
207     }
208
209     free( p_input );
210
211     return( VLC_SUCCESS);
212 }
213
214 /*****************************************************************************
215  *
216  *****************************************************************************/
217 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
218                           block_t *p_buffer )
219 {
220     sout_instance_t     *p_sout = p_input->p_sout;
221     int                 i_ret;
222
223     if( p_input->p_fmt->i_codec == VLC_CODEC_NULL )
224     {
225         block_Release( p_buffer );
226         return VLC_SUCCESS;
227     }
228
229     if( p_buffer->i_dts <= VLC_TS_INVALID )
230     {
231         msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
232         block_Release( p_buffer );
233         return VLC_SUCCESS;
234     }
235
236     vlc_mutex_lock( &p_sout->lock );
237     i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
238                                        p_input->id, p_buffer );
239     vlc_mutex_unlock( &p_sout->lock );
240
241     return i_ret;
242 }
243
244 #undef sout_AccessOutNew
245 /*****************************************************************************
246  * sout_AccessOutNew: allocate a new access out
247  *****************************************************************************/
248 sout_access_out_t *sout_AccessOutNew( vlc_object_t *p_sout,
249                                       const char *psz_access, const char *psz_name )
250 {
251     sout_access_out_t *p_access;
252     char              *psz_next;
253
254     p_access = vlc_custom_create( p_sout, sizeof( *p_access ), "access out" );
255     if( !p_access )
256         return NULL;
257
258     psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
259                                    psz_access );
260     free( psz_next );
261     p_access->psz_path   = strdup( psz_name ? psz_name : "" );
262     p_access->p_sys      = NULL;
263     p_access->pf_seek    = NULL;
264     p_access->pf_read    = NULL;
265     p_access->pf_write   = NULL;
266     p_access->pf_control = NULL;
267     p_access->p_module   = NULL;
268
269     p_access->i_writes = 0;
270     p_access->i_sent_bytes = 0;
271
272     p_access->p_module   =
273         module_need( p_access, "sout access", p_access->psz_access, true );
274
275     if( !p_access->p_module )
276     {
277         free( p_access->psz_access );
278         free( p_access->psz_path );
279         vlc_object_release( p_access );
280         return( NULL );
281     }
282
283     return p_access;
284 }
285 /*****************************************************************************
286  * sout_AccessDelete: delete an access out
287  *****************************************************************************/
288 void sout_AccessOutDelete( sout_access_out_t *p_access )
289 {
290     if( p_access->p_module )
291     {
292         module_unneed( p_access, p_access->p_module );
293     }
294     free( p_access->psz_access );
295
296     config_ChainDestroy( p_access->p_cfg );
297
298     free( p_access->psz_path );
299
300     vlc_object_release( p_access );
301 }
302
303 /*****************************************************************************
304  * sout_AccessSeek:
305  *****************************************************************************/
306 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
307 {
308     return p_access->pf_seek( p_access, i_pos );
309 }
310
311 /*****************************************************************************
312  * sout_AccessRead:
313  *****************************************************************************/
314 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
315 {
316     return( p_access->pf_read ?
317             p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
318 }
319
320 /*****************************************************************************
321  * sout_AccessWrite:
322  *****************************************************************************/
323 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
324 {
325     return p_access->pf_write( p_access, p_buffer );
326 }
327
328 /**
329  * sout_AccessOutControl
330  */
331 int sout_AccessOutControl (sout_access_out_t *access, int query, ...)
332 {
333     va_list ap;
334     int ret;
335
336     va_start (ap, query);
337     if (access->pf_control)
338         ret = access->pf_control (access, query, ap);
339     else
340         ret = VLC_EGENERIC;
341     va_end (ap);
342     return ret;
343 }
344
345 /*****************************************************************************
346  * sout_MuxNew: create a new mux
347  *****************************************************************************/
348 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, const char *psz_mux,
349                           sout_access_out_t *p_access )
350 {
351     sout_mux_t *p_mux;
352     char       *psz_next;
353
354     p_mux = vlc_custom_create( p_sout, sizeof( *p_mux ), "mux" );
355     if( p_mux == NULL )
356         return NULL;
357
358     p_mux->p_sout = p_sout;
359     psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
360     free( psz_next );
361
362     p_mux->p_access     = p_access;
363     p_mux->pf_control   = NULL;
364     p_mux->pf_addstream = NULL;
365     p_mux->pf_delstream = NULL;
366     p_mux->pf_mux       = NULL;
367     p_mux->i_nb_inputs  = 0;
368     p_mux->pp_inputs    = NULL;
369
370     p_mux->p_sys        = NULL;
371     p_mux->p_module     = NULL;
372
373     p_mux->b_add_stream_any_time = false;
374     p_mux->b_waiting_stream = true;
375     p_mux->i_add_stream_start = -1;
376
377     p_mux->p_module =
378         module_need( p_mux, "sout mux", p_mux->psz_mux, true );
379
380     if( p_mux->p_module == NULL )
381     {
382         FREENULL( p_mux->psz_mux );
383
384         vlc_object_release( p_mux );
385         return NULL;
386     }
387
388     /* *** probe mux capacity *** */
389     if( p_mux->pf_control )
390     {
391         int b_answer = false;
392
393         if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
394                              &b_answer ) )
395         {
396             b_answer = false;
397         }
398
399         if( b_answer )
400         {
401             msg_Dbg( p_sout, "muxer support adding stream at any time" );
402             p_mux->b_add_stream_any_time = true;
403             p_mux->b_waiting_stream = false;
404
405             /* If we control the output pace then it's better to wait before
406              * starting muxing (generates better streams/files). */
407             if( !p_sout->i_out_pace_nocontrol )
408             {
409                 b_answer = true;
410             }
411             else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
412                                       &b_answer ) )
413             {
414                 b_answer = false;
415             }
416
417             if( b_answer )
418             {
419                 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
420                          "starting to mux" );
421                 p_mux->b_waiting_stream = true;
422             }
423         }
424     }
425
426     return p_mux;
427 }
428
429 /*****************************************************************************
430  * sout_MuxDelete:
431  *****************************************************************************/
432 void sout_MuxDelete( sout_mux_t *p_mux )
433 {
434     if( p_mux->p_module )
435     {
436         module_unneed( p_mux, p_mux->p_module );
437     }
438     free( p_mux->psz_mux );
439
440     config_ChainDestroy( p_mux->p_cfg );
441
442     vlc_object_release( p_mux );
443 }
444
445 /*****************************************************************************
446  * sout_MuxAddStream:
447  *****************************************************************************/
448 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
449 {
450     sout_input_t *p_input;
451
452     if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
453     {
454         msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
455                         "to this format). You can try increasing sout-mux-caching value" );
456         return NULL;
457     }
458
459     msg_Dbg( p_mux, "adding a new input" );
460
461     /* create a new sout input */
462     p_input = malloc( sizeof( sout_input_t ) );
463     if( !p_input )
464         return NULL;
465     p_input->p_sout = p_mux->p_sout;
466     p_input->p_fmt  = p_fmt;
467     p_input->p_fifo = block_FifoNew();
468     p_input->p_sys  = NULL;
469
470     TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
471     if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
472     {
473         msg_Err( p_mux, "cannot add this stream" );
474         TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
475         block_FifoRelease( p_input->p_fifo );
476         free( p_input );
477         return NULL;
478     }
479
480     return p_input;
481 }
482
483 /*****************************************************************************
484  * sout_MuxDeleteStream:
485  *****************************************************************************/
486 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
487 {
488     int i_index;
489
490     if( p_mux->b_waiting_stream
491      && block_FifoCount( p_input->p_fifo ) > 0 )
492     {
493         /* We stop waiting, and call the muxer for taking care of the data
494          * before we remove this es */
495         p_mux->b_waiting_stream = false;
496         p_mux->pf_mux( p_mux );
497     }
498
499     TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
500     if( i_index >= 0 )
501     {
502         if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
503         {
504             msg_Err( p_mux, "cannot delete this stream from mux" );
505         }
506
507         /* remove the entry */
508         TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
509
510         if( p_mux->i_nb_inputs == 0 )
511         {
512             msg_Warn( p_mux, "no more input streams for this mux" );
513         }
514
515         block_FifoRelease( p_input->p_fifo );
516         free( p_input );
517     }
518 }
519
520 /*****************************************************************************
521  * sout_MuxSendBuffer:
522  *****************************************************************************/
523 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
524                          block_t *p_buffer )
525 {
526     block_FifoPut( p_input->p_fifo, p_buffer );
527
528     if( p_mux->p_sout->i_out_pace_nocontrol )
529     {
530         mtime_t current_date = mdate();
531         if ( current_date > p_buffer->i_dts )
532             msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
533                       current_date - p_buffer->i_dts );
534     }
535
536     if( p_mux->b_waiting_stream )
537     {
538         const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * INT64_C(1000);
539
540         if( p_mux->i_add_stream_start < 0 )
541             p_mux->i_add_stream_start = p_buffer->i_dts;
542
543         /* Wait until we have enought data before muxing */
544         if( p_mux->i_add_stream_start < 0 ||
545             p_buffer->i_dts < p_mux->i_add_stream_start + i_caching )
546             return;
547         p_mux->b_waiting_stream = false;
548     }
549     p_mux->pf_mux( p_mux );
550 }
551
552
553 /*****************************************************************************
554  * sout_MuxGetStream: find stream to be muxed
555  *****************************************************************************/
556 int sout_MuxGetStream( sout_mux_t *p_mux, int i_blocks, mtime_t *pi_dts )
557 {
558     mtime_t i_dts = 0;
559     int     i_stream = -1;
560
561     for( int i = 0; i < p_mux->i_nb_inputs; i++ )
562     {
563         sout_input_t *p_input = p_mux->pp_inputs[i];
564         block_t *p_data;
565
566         if( block_FifoCount( p_input->p_fifo ) < i_blocks )
567         {
568             if( p_input->p_fmt->i_cat != SPU_ES )
569             {
570                 return -1;
571             }
572             /* FIXME: SPU muxing */
573             continue;
574         }
575
576         p_data = block_FifoShow( p_input->p_fifo );
577         if( i_stream < 0 || p_data->i_dts < i_dts )
578         {
579             i_stream = i;
580             i_dts    = p_data->i_dts;
581         }
582     }
583
584     if( pi_dts ) *pi_dts = i_dts;
585
586     return i_stream;
587 }
588
589
590 /*****************************************************************************
591  *
592  *****************************************************************************/
593 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
594 {
595     char * psz_dup = strdup( psz_mrl );
596     char * psz_parser = psz_dup;
597     const char * psz_access;
598     const char * psz_way;
599     char * psz_name;
600
601     /* *** first parse psz_dest */
602     while( *psz_parser && *psz_parser != ':' )
603     {
604         if( *psz_parser == '{' )
605         {
606             while( *psz_parser && *psz_parser != '}' )
607             {
608                 psz_parser++;
609             }
610             if( *psz_parser )
611             {
612                 psz_parser++;
613             }
614         }
615         else
616         {
617             psz_parser++;
618         }
619     }
620 #if defined( WIN32 ) || defined( UNDER_CE ) || defined( __OS2__ )
621     if( psz_parser - psz_dup == 1 )
622     {
623         /* msg_Warn( p_sout, "drive letter %c: found in source string",
624                           *psz_dup ) ; */
625         psz_parser = "";
626     }
627 #endif
628
629     if( !*psz_parser )
630     {
631         psz_access = psz_way = "";
632         psz_name = psz_dup;
633     }
634     else
635     {
636         *psz_parser++ = '\0';
637
638         /* let's skip '//' */
639         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
640         {
641             psz_parser += 2 ;
642         }
643
644         psz_name = psz_parser ;
645
646         /* Come back to parse the access and mux plug-ins */
647         psz_parser = psz_dup;
648
649         if( !*psz_parser )
650         {
651             /* No access */
652             psz_access = "";
653         }
654         else if( *psz_parser == '/' )
655         {
656             /* No access */
657             psz_access = "";
658             psz_parser++;
659         }
660         else
661         {
662             psz_access = psz_parser;
663
664             while( *psz_parser && *psz_parser != '/' )
665             {
666                 if( *psz_parser == '{' )
667                 {
668                     while( *psz_parser && *psz_parser != '}' )
669                     {
670                         psz_parser++;
671                     }
672                     if( *psz_parser )
673                     {
674                         psz_parser++;
675                     }
676                 }
677                 else
678                 {
679                     psz_parser++;
680                 }
681             }
682
683             if( *psz_parser == '/' )
684             {
685                 *psz_parser++ = '\0';
686             }
687         }
688
689         if( !*psz_parser )
690         {
691             /* No mux */
692             psz_way = "";
693         }
694         else
695         {
696             psz_way = psz_parser;
697         }
698     }
699
700     p_mrl->psz_access = strdup( psz_access );
701     p_mrl->psz_way    = strdup( psz_way );
702     p_mrl->psz_name   = strdup( psz_name );
703
704     free( psz_dup );
705     return( VLC_SUCCESS );
706 }
707
708
709 /* mrl_Clean: clean p_mrl  after a call to mrl_Parse */
710 static void mrl_Clean( mrl_t *p_mrl )
711 {
712     FREENULL( p_mrl->psz_access );
713     FREENULL( p_mrl->psz_way );
714     FREENULL( p_mrl->psz_name );
715 }
716
717
718 /****************************************************************************
719  ****************************************************************************
720  **
721  **
722  **
723  ****************************************************************************
724  ****************************************************************************/
725
726 /* Destroy a "stream_out" module */
727 static void sout_StreamDelete( sout_stream_t *p_stream )
728 {
729     msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
730
731     if( p_stream->p_module ) module_unneed( p_stream, p_stream->p_module );
732
733     FREENULL( p_stream->psz_name );
734
735     config_ChainDestroy( p_stream->p_cfg );
736
737     msg_Dbg( p_stream, "destroying chain done" );
738     vlc_object_release( p_stream );
739 }
740
741 /* Destroy a "stream_out" modules chain
742  *
743  * p_first is the first module to be destroyed in the chain
744  * p_last is the last module to be destroyed
745  *  if NULL, all modules are destroyed
746  *  if not NULL, modules following it must be destroyed separately
747  */
748 void sout_StreamChainDelete(sout_stream_t *p_first, sout_stream_t *p_last)
749 {
750     while(p_first != NULL)
751     {
752         sout_stream_t *p_next = p_first->p_next;
753
754         sout_StreamDelete(p_first);
755         if(p_first == p_last)
756            break;
757         p_first = p_next;
758     }
759 }
760
761 /* Create a "stream_out" module, which may forward its ES to p_next module */
762 /*
763  * XXX name and p_cfg are used (-> do NOT free them)
764  */
765 static sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_name,
766                                config_chain_t *p_cfg, sout_stream_t *p_next)
767 {
768     sout_stream_t *p_stream;
769
770     assert(psz_name);
771
772     p_stream = vlc_custom_create( p_sout, sizeof( *p_stream ), "stream out" );
773     if( !p_stream )
774         return NULL;
775
776     p_stream->p_sout   = p_sout;
777     p_stream->p_sys    = NULL;
778     p_stream->psz_name = psz_name;
779     p_stream->p_cfg    = p_cfg;
780     p_stream->p_next   = p_next;
781
782     msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
783
784     p_stream->p_module =
785         module_need( p_stream, "sout stream", p_stream->psz_name, true );
786
787     if( !p_stream->p_module )
788     {
789         /* those must be freed by the caller if creation failed */
790         p_stream->psz_name = NULL;
791         p_stream->p_cfg = NULL;
792
793         sout_StreamDelete( p_stream );
794         return NULL;
795     }
796
797     return p_stream;
798 }
799
800 /* Creates a complete "stream_out" modules chain
801  *
802  *  chain format: module1{option=*:option=*}[:module2{option=*:...}]
803  *
804  *  The modules are created starting from the last one and linked together
805  *  A pointer to the last module created is stored if pp_last isn't NULL, to
806  *  make sure sout_StreamChainDelete doesn't delete modules created in another
807  *  place.
808  *
809  *  Returns a pointer to the first module.
810  */
811 sout_stream_t *sout_StreamChainNew(sout_instance_t *p_sout, char *psz_chain,
812                                 sout_stream_t *p_next, sout_stream_t **pp_last)
813 {
814     if(!psz_chain || !*psz_chain)
815     {
816         if(pp_last) *pp_last = NULL;
817         return p_next;
818     }
819
820     char *psz_parser = strdup(psz_chain);
821     if(!psz_parser)
822         return NULL;
823
824     vlc_array_t cfg, name;
825     vlc_array_init(&cfg);
826     vlc_array_init(&name);
827
828     /* parse chain */
829     while(psz_parser)
830     {
831         config_chain_t *p_cfg;
832         char *psz_name;
833         psz_chain = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
834         free( psz_parser );
835         psz_parser = psz_chain;
836
837         vlc_array_append(&cfg, p_cfg);
838         vlc_array_append(&name, psz_name);
839     }
840
841     int i = vlc_array_count(&name);
842     vlc_array_t module;
843     vlc_array_init(&module);
844     while(i--)
845     {
846         p_next = sout_StreamNew( p_sout, vlc_array_item_at_index(&name, i),
847             vlc_array_item_at_index(&cfg, i), p_next);
848
849         if(!p_next)
850             goto error;
851
852         if(i == vlc_array_count(&name) - 1 && pp_last)
853             *pp_last = p_next;   /* last module created in the chain */
854
855         vlc_array_append(&module, p_next);
856     }
857
858     vlc_array_clear(&name);
859     vlc_array_clear(&cfg);
860     vlc_array_clear(&module);
861
862     return p_next;
863
864 error:
865
866     i++;    /* last module couldn't be created */
867
868     /* destroy all modules created, starting with the last one */
869     int modules = vlc_array_count(&module);
870     while(modules--)
871         sout_StreamDelete(vlc_array_item_at_index(&module, modules));
872     vlc_array_clear(&module);
873
874     /* then destroy all names and config which weren't destroyed by
875      * sout_StreamDelete */
876     while(i--)
877     {
878         free(vlc_array_item_at_index(&name, i));
879         config_ChainDestroy(vlc_array_item_at_index(&cfg, i));
880     }
881     vlc_array_clear(&name);
882     vlc_array_clear(&cfg);
883
884     return NULL;
885 }
886
887 static char *sout_stream_url_to_chain( bool b_sout_display,
888                                        const char *psz_url )
889 {
890     mrl_t       mrl;
891     char        *psz_chain;
892
893     mrl_Parse( &mrl, psz_url );
894
895     /* Check if the URLs goes to #rtp - otherwise we'll use #standard */
896     static const char rtplist[] = "dccp\0sctp\0tcp\0udplite\0";
897     for (const char *a = rtplist; *a; a += strlen (a) + 1)
898         if (strcmp (a, mrl.psz_access) == 0)
899             goto rtp;
900
901     if (strcmp (mrl.psz_access, "rtp") == 0)
902     {
903         char *port;
904         /* For historical reasons, rtp:// means RTP over UDP */
905         strcpy (mrl.psz_access, "udp");
906 rtp:
907         if (mrl.psz_name[0] == '[')
908         {
909             port = strstr (mrl.psz_name, "]:");
910             if (port != NULL)
911                 port++;
912         }
913         else
914             port = strchr (mrl.psz_name, ':');
915         if (port != NULL)
916             *port++ = '\0'; /* erase ':' */
917
918         if (asprintf (&psz_chain,
919                       "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s%s%s\"}",
920                       mrl.psz_way, mrl.psz_access, mrl.psz_name,
921                       port ? "\",port=\"" : "", port ? port : "") == -1)
922             psz_chain = NULL;
923     }
924     else
925     {
926         /* Convert the URL to a basic standard sout chain */
927         if (asprintf (&psz_chain,
928                       "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
929                       mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
930             psz_chain = NULL;
931     }
932
933     /* Duplicate and wrap if sout-display is on */
934     if (psz_chain && b_sout_display)
935     {
936         char *tmp;
937         if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", psz_chain) == -1)
938             tmp = NULL;
939         free (psz_chain);
940         psz_chain = tmp;
941     }
942
943     mrl_Clean( &mrl );
944     return psz_chain;
945 }
946
947 #undef sout_EncoderCreate
948 encoder_t *sout_EncoderCreate( vlc_object_t *p_this )
949 {
950     return vlc_custom_create( p_this, sizeof( encoder_t ), "encoder" );
951 }