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