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