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