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