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