]> git.sesse.net Git - vlc/blob - src/stream_output/stream_output.c
Fix compiler warning on uninitialized variable.
[vlc] / src / stream_output / stream_output.c
1 /*****************************************************************************
2  * stream_output.c : stream output module
3  *****************************************************************************
4  * Copyright (C) 2002-2004 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 #include <stdlib.h>                                                /* free() */
30 #include <stdio.h>                                              /* sprintf() */
31 #include <string.h>                                            /* strerror() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/sout.h>
35 #include <vlc/input.h>
36
37 #include "vlc_meta.h"
38
39 #undef DEBUG_BUFFER
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static void sout_CfgDestroy( sout_cfg_t * );
44
45 #define sout_stream_url_to_chain( p, s ) \
46     _sout_stream_url_to_chain( VLC_OBJECT(p), s )
47 static char *_sout_stream_url_to_chain( vlc_object_t *, char * );
48
49 /*
50  * Generic MRL parser
51  *
52  */
53
54 typedef struct
55 {
56     char *psz_access;
57     char *psz_way;
58     char *psz_name;
59 } mrl_t;
60
61 /* mrl_Parse: parse psz_mrl and fill p_mrl */
62 static int  mrl_Parse( mrl_t *p_mrl, char *psz_mrl );
63 /* mrl_Clean: clean p_mrl  after a call to mrl_Parse */
64 static void mrl_Clean( mrl_t *p_mrl );
65
66 /*****************************************************************************
67  * sout_NewInstance: creates a new stream output instance
68  *****************************************************************************/
69 sout_instance_t *__sout_NewInstance( vlc_object_t *p_parent, char * psz_dest )
70 {
71     sout_instance_t *p_sout;
72     vlc_value_t keep;
73
74     if( var_Get( p_parent, "sout-keep", &keep ) < 0 )
75     {
76         msg_Warn( p_parent, "cannot get sout-keep value" );
77         keep.b_bool = VLC_FALSE;
78     }
79     if( keep.b_bool )
80     {
81         if( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT,
82                                         FIND_ANYWHERE ) ) != NULL )
83         {
84             if( !strcmp( p_sout->psz_sout, psz_dest ) )
85             {
86                 msg_Dbg( p_parent, "sout keep: reusing sout" );
87                 msg_Dbg( p_parent, "sout keep: you probably want to use "
88                           "gather stream_out" );
89                 vlc_object_detach( p_sout );
90                 vlc_object_attach( p_sout, p_parent );
91                 vlc_object_release( p_sout );
92                 return p_sout;
93             }
94             else
95             {
96                 msg_Dbg( p_parent, "sout keep: destroying unusable sout" );
97                 vlc_object_release( p_sout );
98                 sout_DeleteInstance( p_sout );
99             }
100         }
101     }
102     else if( !keep.b_bool )
103     {
104         while( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT,
105                                            FIND_PARENT ) ) != NULL )
106         {
107             msg_Dbg( p_parent, "sout keep: destroying old sout" );
108             vlc_object_release( p_sout );
109             sout_DeleteInstance( p_sout );
110         }
111     }
112
113     /* *** Allocate descriptor *** */
114     p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
115     if( p_sout == NULL )
116     {
117         msg_Err( p_parent, "out of memory" );
118         return NULL;
119     }
120
121     /* *** init descriptor *** */
122     p_sout->psz_sout    = strdup( psz_dest );
123     p_sout->p_meta      = NULL;
124     p_sout->i_out_pace_nocontrol = 0;
125     p_sout->p_sys       = NULL;
126
127     vlc_mutex_init( p_sout, &p_sout->lock );
128     if( psz_dest && psz_dest[0] == '#' )
129     {
130         p_sout->psz_chain = strdup( &psz_dest[1] );
131     }
132     else
133     {
134         p_sout->psz_chain = sout_stream_url_to_chain( p_sout, psz_dest );
135         msg_Dbg( p_sout, "using sout chain=`%s'", p_sout->psz_chain );
136     }
137     p_sout->p_stream = NULL;
138
139     /* attach it for inherit */
140     vlc_object_attach( p_sout, p_parent );
141
142     p_sout->p_stream = sout_StreamNew( p_sout, p_sout->psz_chain );
143
144     if( p_sout->p_stream == NULL )
145     {
146         msg_Err( p_sout, "stream chain failed for `%s'", p_sout->psz_chain );
147
148         FREENULL( p_sout->psz_sout );
149         FREENULL( p_sout->psz_chain );
150
151         vlc_object_detach( p_sout );
152         vlc_object_destroy( p_sout );
153         return NULL;
154     }
155
156     return p_sout;
157 }
158
159 /*****************************************************************************
160  * sout_DeleteInstance: delete a previously allocated instance
161  *****************************************************************************/
162 void sout_DeleteInstance( sout_instance_t * p_sout )
163 {
164     /* Unlink object */
165     vlc_object_detach( p_sout );
166
167     /* remove the stream out chain */
168     sout_StreamDelete( p_sout->p_stream );
169
170     /* *** free all string *** */
171     FREENULL( p_sout->psz_sout );
172     FREENULL( p_sout->psz_chain );
173
174     /* delete meta */
175     if( p_sout->p_meta )
176     {
177         vlc_meta_Delete( p_sout->p_meta );
178     }
179
180     vlc_mutex_destroy( &p_sout->lock );
181
182     /* *** free structure *** */
183     vlc_object_destroy( p_sout );
184 }
185
186 /*****************************************************************************
187  * Packetizer/Input
188  *****************************************************************************/
189 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
190                                         es_format_t *p_fmt )
191 {
192     sout_packetizer_input_t *p_input;
193
194     msg_Dbg( p_sout, "adding a new input" );
195
196     /* *** create a packetizer input *** */
197     p_input         = malloc( sizeof( sout_packetizer_input_t ) );
198     p_input->p_sout = p_sout;
199     p_input->p_fmt  = p_fmt;
200
201     if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
202     {
203         vlc_object_release( p_sout );
204         return p_input;
205     }
206
207     /* *** add it to the stream chain */
208     vlc_mutex_lock( &p_sout->lock );
209     p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
210     vlc_mutex_unlock( &p_sout->lock );
211
212     if( p_input->id == NULL )
213     {
214         free( p_input );
215         return NULL;
216     }
217
218     return( p_input );
219 }
220
221 /*****************************************************************************
222  *
223  *****************************************************************************/
224 int sout_InputDelete( sout_packetizer_input_t *p_input )
225 {
226     sout_instance_t     *p_sout = p_input->p_sout;
227
228     msg_Dbg( p_sout, "removing an input" );
229
230     if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
231     {
232         vlc_mutex_lock( &p_sout->lock );
233         p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
234         vlc_mutex_unlock( &p_sout->lock );
235     }
236
237     free( p_input );
238
239     return( VLC_SUCCESS);
240 }
241
242 /*****************************************************************************
243  *
244  *****************************************************************************/
245 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
246                           block_t *p_buffer )
247 {
248     sout_instance_t     *p_sout = p_input->p_sout;
249     int                 i_ret;
250
251     if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
252     {
253         block_Release( p_buffer );
254         return VLC_SUCCESS;
255     }
256     if( p_buffer->i_dts <= 0 )
257     {
258         msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
259         block_Release( p_buffer );
260         return VLC_SUCCESS;
261     }
262
263     vlc_mutex_lock( &p_sout->lock );
264     i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
265                                        p_input->id, p_buffer );
266     vlc_mutex_unlock( &p_sout->lock );
267
268     return i_ret;
269 }
270
271 /*****************************************************************************
272  * sout_AccessOutNew: allocate a new access out
273  *****************************************************************************/
274 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
275                                       char *psz_access, char *psz_name )
276 {
277     sout_access_out_t *p_access;
278     char              *psz_next;
279
280     if( !( p_access = vlc_object_create( p_sout,
281                                          sizeof( sout_access_out_t ) ) ) )
282     {
283         msg_Err( p_sout, "out of memory" );
284         return NULL;
285     }
286
287     psz_next = sout_CfgCreate( &p_access->psz_access, &p_access->p_cfg,
288                                 psz_access );
289     if( psz_next )
290     {
291         free( psz_next );
292     }
293     p_access->psz_name   = strdup( psz_name ? psz_name : "" );
294     p_access->p_sout     = p_sout;
295     p_access->p_sys = NULL;
296     p_access->pf_seek    = NULL;
297     p_access->pf_read    = NULL;
298     p_access->pf_write   = NULL;
299     p_access->p_module   = NULL;
300
301     p_access->i_writes = 0;
302     p_access->i_sent_bytes = 0;
303
304     vlc_object_attach( p_access, p_sout );
305
306     p_access->p_module   =
307         module_Need( p_access, "sout access", p_access->psz_access, VLC_TRUE );
308
309     if( !p_access->p_module )
310     {
311         free( p_access->psz_access );
312         free( p_access->psz_name );
313         vlc_object_detach( p_access );
314         vlc_object_destroy( p_access );
315         return( NULL );
316     }
317
318     return p_access;
319 }
320 /*****************************************************************************
321  * sout_AccessDelete: delete an access out
322  *****************************************************************************/
323 void sout_AccessOutDelete( sout_access_out_t *p_access )
324 {
325     vlc_object_detach( p_access );
326     if( p_access->p_module )
327     {
328         module_Unneed( p_access, p_access->p_module );
329     }
330     free( p_access->psz_access );
331
332     sout_CfgDestroy( p_access->p_cfg );
333
334     free( p_access->psz_name );
335
336     vlc_object_destroy( p_access );
337 }
338
339 /*****************************************************************************
340  * sout_AccessSeek:
341  *****************************************************************************/
342 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
343 {
344     return p_access->pf_seek( p_access, i_pos );
345 }
346
347 /*****************************************************************************
348  * sout_AccessRead:
349  *****************************************************************************/
350 int sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
351 {
352     return( p_access->pf_read ?
353             p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
354 }
355
356 /*****************************************************************************
357  * sout_AccessWrite:
358  *****************************************************************************/
359 int sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
360 {
361     int i_total = 0;
362     p_access->i_writes++;
363     p_access->i_sent_bytes += p_buffer->i_buffer;
364     if( p_access->p_libvlc->b_stats && p_access->i_writes % 30 == 0 )
365     {
366         /* Access_out -> sout_instance -> input_thread_t */
367         input_thread_t *p_input =
368             (input_thread_t *)vlc_object_find( p_access, VLC_OBJECT_INPUT,
369                                                FIND_PARENT );
370         if( p_input )
371         {
372             stats_UpdateInteger( p_input, p_input->counters.p_sout_sent_packets,
373                                  30, NULL );
374             stats_UpdateInteger( p_input, p_input->counters.p_sout_sent_bytes,
375                                  p_access->i_sent_bytes, &i_total );
376             stats_UpdateFloat( p_input, p_input->counters.p_sout_send_bitrate,
377                                  (float)i_total, NULL );
378             p_access->i_sent_bytes = 0;
379             vlc_object_release( p_input );
380         }
381     }
382     return p_access->pf_write( p_access, p_buffer );
383 }
384
385 /*****************************************************************************
386  * sout_MuxNew: create a new mux
387  *****************************************************************************/
388 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
389                           sout_access_out_t *p_access )
390 {
391     sout_mux_t *p_mux;
392     char       *psz_next;
393
394     p_mux = vlc_object_create( p_sout, sizeof( sout_mux_t ) );
395     if( p_mux == NULL )
396     {
397         msg_Err( p_sout, "out of memory" );
398         return NULL;
399     }
400
401     p_mux->p_sout = p_sout;
402     psz_next = sout_CfgCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
403     if( psz_next ) free( psz_next );
404
405     p_mux->p_access     = p_access;
406     p_mux->pf_control   = NULL;
407     p_mux->pf_addstream = NULL;
408     p_mux->pf_delstream = NULL;
409     p_mux->pf_mux       = NULL;
410     p_mux->i_nb_inputs  = 0;
411     p_mux->pp_inputs    = NULL;
412
413     p_mux->p_sys        = NULL;
414     p_mux->p_module     = NULL;
415
416     p_mux->b_add_stream_any_time = VLC_FALSE;
417     p_mux->b_waiting_stream = VLC_TRUE;
418     p_mux->i_add_stream_start = -1;
419
420     vlc_object_attach( p_mux, p_sout );
421
422     p_mux->p_module =
423         module_Need( p_mux, "sout mux", p_mux->psz_mux, VLC_TRUE );
424
425     if( p_mux->p_module == NULL )
426     {
427         FREENULL( p_mux->psz_mux );
428
429         vlc_object_detach( p_mux );
430         vlc_object_destroy( p_mux );
431         return NULL;
432     }
433
434     /* *** probe mux capacity *** */
435     if( p_mux->pf_control )
436     {
437         int b_answer = VLC_FALSE;
438
439         if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
440                              &b_answer ) )
441         {
442             b_answer = VLC_FALSE;
443         }
444
445         if( b_answer )
446         {
447             msg_Dbg( p_sout, "muxer support adding stream at any time" );
448             p_mux->b_add_stream_any_time = VLC_TRUE;
449             p_mux->b_waiting_stream = VLC_FALSE;
450
451             /* If we control the output pace then it's better to wait before
452              * starting muxing (generates better streams/files). */
453             if( !p_sout->i_out_pace_nocontrol )
454             {
455                 b_answer = VLC_TRUE;
456             }
457             else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
458                                       &b_answer ) )
459             {
460                 b_answer = VLC_FALSE;
461             }
462
463             if( b_answer )
464             {
465                 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
466                          "starting to mux" );
467                 p_mux->b_waiting_stream = VLC_TRUE;
468             }
469         }
470     }
471
472     return p_mux;
473 }
474
475 /*****************************************************************************
476  * sout_MuxDelete:
477  *****************************************************************************/
478 void sout_MuxDelete( sout_mux_t *p_mux )
479 {
480     vlc_object_detach( p_mux );
481     if( p_mux->p_module )
482     {
483         module_Unneed( p_mux, p_mux->p_module );
484     }
485     free( p_mux->psz_mux );
486
487     sout_CfgDestroy( p_mux->p_cfg );
488
489     vlc_object_destroy( p_mux );
490 }
491
492 /*****************************************************************************
493  * sout_MuxAddStream:
494  *****************************************************************************/
495 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
496 {
497     sout_input_t *p_input;
498
499     if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
500     {
501         msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
502                         "to this format)" );
503         return NULL;
504     }
505
506     msg_Dbg( p_mux, "adding a new input" );
507
508     /* create a new sout input */
509     p_input = malloc( sizeof( sout_input_t ) );
510     p_input->p_sout = p_mux->p_sout;
511     p_input->p_fmt  = p_fmt;
512     p_input->p_fifo = block_FifoNew( p_mux->p_sout );
513     p_input->p_sys  = NULL;
514
515     TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
516     if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
517     {
518             msg_Err( p_mux, "cannot add this stream" );
519             TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
520             block_FifoRelease( p_input->p_fifo );
521             free( p_input );
522             return NULL;
523     }
524
525     return p_input;
526 }
527
528 /*****************************************************************************
529  * sout_MuxDeleteStream:
530  *****************************************************************************/
531 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
532 {
533     int i_index;
534
535     if( p_mux->b_waiting_stream && p_input->p_fifo->i_depth > 0 )
536     {
537         /* We stop waiting, and call the muxer for taking care of the data
538          * before we remove this es */
539         p_mux->b_waiting_stream = VLC_FALSE;
540         p_mux->pf_mux( p_mux );
541     }
542
543     TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
544     if( i_index >= 0 )
545     {
546         if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
547         {
548             msg_Err( p_mux, "cannot delete this stream from mux" );
549         }
550
551         /* remove the entry */
552         TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
553
554         if( p_mux->i_nb_inputs == 0 )
555         {
556             msg_Warn( p_mux, "no more input streams for this mux" );
557         }
558
559         block_FifoRelease( p_input->p_fifo );
560         free( p_input );
561     }
562 }
563
564 /*****************************************************************************
565  * sout_MuxSendBuffer:
566  *****************************************************************************/
567 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
568                          block_t *p_buffer )
569 {
570     block_FifoPut( p_input->p_fifo, p_buffer );
571
572     if( p_mux->p_sout->i_out_pace_nocontrol )
573     {
574         mtime_t current_date = mdate();
575         if ( current_date > p_buffer->i_dts )
576             msg_Warn( p_mux, "late buffer for mux input ("I64Fd")",
577                       current_date - p_buffer->i_dts );
578     }
579
580     if( p_mux->b_waiting_stream )
581     {
582         if( p_mux->i_add_stream_start < 0 )
583         {
584             p_mux->i_add_stream_start = p_buffer->i_dts;
585         }
586
587         if( p_mux->i_add_stream_start >= 0 &&
588             p_mux->i_add_stream_start + I64C(1500000) < p_buffer->i_dts )
589         {
590             /* Wait until we have more than 1.5 seconds worth of data
591              * before start muxing */
592             p_mux->b_waiting_stream = VLC_FALSE;
593         }
594         else
595         {
596             return;
597         }
598     }
599     p_mux->pf_mux( p_mux );
600 }
601
602 /*****************************************************************************
603  *
604  *****************************************************************************/
605 static int mrl_Parse( mrl_t *p_mrl, char *psz_mrl )
606 {
607     char * psz_dup = strdup( psz_mrl );
608     char * psz_parser = psz_dup;
609     char * psz_access = "";
610     char * psz_way = "";
611     char * psz_name = "";
612
613     /* *** first parse psz_dest */
614     while( *psz_parser && *psz_parser != ':' )
615     {
616         if( *psz_parser == '{' )
617         {
618             while( *psz_parser && *psz_parser != '}' )
619             {
620                 psz_parser++;
621             }
622             if( *psz_parser )
623             {
624                 psz_parser++;
625             }
626         }
627         else
628         {
629             psz_parser++;
630         }
631     }
632 #if defined( WIN32 ) || defined( UNDER_CE )
633     if( psz_parser - psz_dup == 1 )
634     {
635         /* msg_Warn( p_sout, "drive letter %c: found in source string",
636                           *psz_dup ) ; */
637         psz_parser = "";
638     }
639 #endif
640
641     if( !*psz_parser )
642     {
643         psz_access = psz_way = "";
644         psz_name = psz_dup;
645     }
646     else
647     {
648         *psz_parser++ = '\0';
649
650         /* let's skip '//' */
651         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
652         {
653             psz_parser += 2 ;
654         }
655
656         psz_name = psz_parser ;
657
658         /* Come back to parse the access and mux plug-ins */
659         psz_parser = psz_dup;
660
661         if( !*psz_parser )
662         {
663             /* No access */
664             psz_access = "";
665         }
666         else if( *psz_parser == '/' )
667         {
668             /* No access */
669             psz_access = "";
670             psz_parser++;
671         }
672         else
673         {
674             psz_access = psz_parser;
675
676             while( *psz_parser && *psz_parser != '/' )
677             {
678                 if( *psz_parser == '{' )
679                 {
680                     while( *psz_parser && *psz_parser != '}' )
681                     {
682                         psz_parser++;
683                     }
684                     if( *psz_parser )
685                     {
686                         psz_parser++;
687                     }
688                 }
689                 else
690                 {
691                     psz_parser++;
692                 }
693             }
694
695             if( *psz_parser == '/' )
696             {
697                 *psz_parser++ = '\0';
698             }
699         }
700
701         if( !*psz_parser )
702         {
703             /* No mux */
704             psz_way = "";
705         }
706         else
707         {
708             psz_way = psz_parser;
709         }
710     }
711
712     p_mrl->psz_access = strdup( psz_access );
713     p_mrl->psz_way    = strdup( psz_way );
714     p_mrl->psz_name   = strdup( psz_name );
715
716     free( psz_dup );
717     return( VLC_SUCCESS );
718 }
719
720
721 /* mrl_Clean: clean p_mrl  after a call to mrl_Parse */
722 static void mrl_Clean( mrl_t *p_mrl )
723 {
724     FREENULL( p_mrl->psz_access );
725     FREENULL( p_mrl->psz_way );
726     FREENULL( p_mrl->psz_name );
727 }
728
729
730 /****************************************************************************
731  ****************************************************************************
732  **
733  **
734  **
735  ****************************************************************************
736  ****************************************************************************/
737
738 /* create a complete chain */
739 /* chain format:
740     module{option=*:option=*}[:module{option=*:...}]
741  */
742
743 /*
744  * parse module{options=str, option="str "}:
745  *  return a pointer on the rest
746  *  XXX: psz_chain is modified
747  */
748 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
749 #define SKIPTRAILINGSPACE( p, e ) \
750     { while( e > p && ( *(e-1) == ' ' || *(e-1) == '\t' ) ) e--; }
751
752 /* go accross " " and { } */
753 static char *_get_chain_end( char *str )
754 {
755     char c, *p = str;
756
757     SKIPSPACE( p );
758
759     for( ;; )
760     {
761         if( !*p || *p == ',' || *p == '}' ) return p;
762
763         if( *p != '{' && *p != '"' && *p != '\'' )
764         {
765             p++;
766             continue;
767         }
768
769         if( *p == '{' ) c = '}';
770         else c = *p;
771         p++;
772
773         for( ;; )
774         {
775             if( !*p ) return p;
776
777             if( *p == c ) return ++p;
778             else if( *p == '{' && c == '}' ) p = _get_chain_end( p );
779             else p++;
780         }
781     }
782 }
783
784 char *sout_CfgCreate( char **ppsz_name, sout_cfg_t **pp_cfg, char *psz_chain )
785 {
786     sout_cfg_t *p_cfg = NULL;
787     char       *p = psz_chain;
788
789     *ppsz_name = NULL;
790     *pp_cfg    = NULL;
791
792     if( !p ) return NULL;
793
794     SKIPSPACE( p );
795
796     while( *p && *p != '{' && *p != ':' && *p != ' ' && *p != '\t' ) p++;
797
798     if( p == psz_chain ) return NULL;
799
800     *ppsz_name = strndup( psz_chain, p - psz_chain );
801
802     SKIPSPACE( p );
803
804     if( *p == '{' )
805     {
806         char *psz_name;
807
808         p++;
809
810         for( ;; )
811         {
812             sout_cfg_t cfg;
813
814             SKIPSPACE( p );
815
816             psz_name = p;
817
818             while( *p && *p != '=' && *p != ',' && *p != '{' && *p != '}' &&
819                    *p != ' ' && *p != '\t' ) p++;
820
821             /* fprintf( stderr, "name=%s - rest=%s\n", psz_name, p ); */
822             if( p == psz_name )
823             {
824                 fprintf( stderr, "invalid options (empty)" );
825                 break;
826             }
827
828             cfg.psz_name = strndup( psz_name, p - psz_name );
829
830             SKIPSPACE( p );
831
832             if( *p == '=' || *p == '{' )
833             {
834                 char *end;
835                 vlc_bool_t b_keep_brackets = (*p == '{');
836
837                 if( *p == '=' ) p++;
838
839                 end = _get_chain_end( p );
840                 if( end <= p )
841                 {
842                     cfg.psz_value = NULL;
843                 }
844                 else
845                 {
846                     /* Skip heading and trailing spaces.
847                      * This ain't necessary but will avoid simple
848                      * user mistakes. */
849                     SKIPSPACE( p );
850                 }
851
852                 if( end <= p )
853                 {
854                     cfg.psz_value = NULL;
855                 }
856                 else
857                 {
858                     if( *p == '\'' || *p == '"' ||
859                         ( !b_keep_brackets && *p == '{' ) )
860                     {
861                         p++;
862
863                         if( *(end-1) != '\'' && *(end-1) == '"' )
864                             SKIPTRAILINGSPACE( p, end );
865
866                         if( end - 1 <= p ) cfg.psz_value = NULL;
867                         else cfg.psz_value = strndup( p, end -1 - p );
868                     }
869                     else
870                     {
871                         SKIPTRAILINGSPACE( p, end );
872                         if( end <= p ) cfg.psz_value = NULL;
873                         else cfg.psz_value = strndup( p, end - p );
874                     }
875                 }
876
877                 p = end;
878                 SKIPSPACE( p );
879             }
880             else
881             {
882                 cfg.psz_value = NULL;
883             }
884
885             cfg.p_next = NULL;
886             if( p_cfg )
887             {
888                 p_cfg->p_next = malloc( sizeof( sout_cfg_t ) );
889                 memcpy( p_cfg->p_next, &cfg, sizeof( sout_cfg_t ) );
890
891                 p_cfg = p_cfg->p_next;
892             }
893             else
894             {
895                 p_cfg = malloc( sizeof( sout_cfg_t ) );
896                 memcpy( p_cfg, &cfg, sizeof( sout_cfg_t ) );
897
898                 *pp_cfg = p_cfg;
899             }
900
901             if( *p == ',' ) p++;
902
903             if( *p == '}' )
904             {
905                 p++;
906                 break;
907             }
908         }
909     }
910
911     if( *p == ':' ) return( strdup( p + 1 ) );
912
913     return NULL;
914 }
915
916 static void sout_CfgDestroy( sout_cfg_t *p_cfg )
917 {
918     while( p_cfg != NULL )
919     {
920         sout_cfg_t *p_next;
921
922         p_next = p_cfg->p_next;
923
924         FREENULL( p_cfg->psz_name );
925         FREENULL( p_cfg->psz_value );
926         free( p_cfg );
927
928         p_cfg = p_next;
929     }
930 }
931
932 void __sout_CfgParse( vlc_object_t *p_this, char *psz_prefix,
933                       const char **ppsz_options, sout_cfg_t *cfg )
934 {
935     char *psz_name;
936     int  i_type;
937     int  i;
938
939     /* First, var_Create all variables */
940     for( i = 0; ppsz_options[i] != NULL; i++ )
941     {
942         asprintf( &psz_name, "%s%s", psz_prefix,
943                   *ppsz_options[i] == '*' ? &ppsz_options[i][1] : ppsz_options[i] );
944
945         i_type = config_GetType( p_this, psz_name );
946
947         var_Create( p_this, psz_name, i_type | VLC_VAR_DOINHERIT );
948         free( psz_name );
949     }
950
951     /* Now parse options and set value */
952     if( psz_prefix == NULL ) psz_prefix = "";
953
954     while( cfg )
955     {
956         vlc_value_t val;
957         vlc_bool_t b_yes = VLC_TRUE;
958         vlc_bool_t b_once = VLC_FALSE;
959         module_config_t *p_conf;
960
961         if( cfg->psz_name == NULL || *cfg->psz_name == '\0' )
962         {
963             cfg = cfg->p_next;
964             continue;
965         }
966         for( i = 0; ppsz_options[i] != NULL; i++ )
967         {
968             if( !strcmp( ppsz_options[i], cfg->psz_name ) )
969             {
970                 break;
971             }
972             if( ( !strncmp( cfg->psz_name, "no-", 3 ) &&
973                   !strcmp( ppsz_options[i], cfg->psz_name + 3 ) ) ||
974                 ( !strncmp( cfg->psz_name, "no", 2 ) &&
975                   !strcmp( ppsz_options[i], cfg->psz_name + 2 ) ) )
976             {
977                 b_yes = VLC_FALSE;
978                 break;
979             }
980
981             if( *ppsz_options[i] == '*' &&
982                 !strcmp( &ppsz_options[i][1], cfg->psz_name ) )
983             {
984                 b_once = VLC_TRUE;
985                 break;
986             }
987
988         }
989         if( ppsz_options[i] == NULL )
990         {
991             msg_Warn( p_this, "option %s is unknown", cfg->psz_name );
992             cfg = cfg->p_next;
993             continue;
994         }
995
996         /* create name */
997         asprintf( &psz_name, "%s%s", psz_prefix, b_once ? &ppsz_options[i][1] : ppsz_options[i] );
998
999         /* Check if the option is deprecated */
1000         p_conf = config_FindConfig( p_this, psz_name );
1001
1002         /* This is basically cut and paste from src/misc/configuration.c
1003          * with slight changes */
1004         if( p_conf && p_conf->psz_current )
1005         {
1006             if( !strcmp( p_conf->psz_current, "SUPPRESSED" ) )
1007             {
1008                 msg_Err( p_this, "Option %s is no longer used.",
1009                          p_conf->psz_name );
1010                 goto next;
1011             }
1012             else if( p_conf->b_strict )
1013             {
1014                 msg_Err( p_this, "Option %s is deprecated. Use %s instead.",
1015                          p_conf->psz_name, p_conf->psz_current );
1016                 /* TODO: this should return an error and end option parsing
1017                  * ... but doing this would change the VLC API and all the
1018                  * modules so i'll do it later */
1019                 goto next;
1020             }
1021             else
1022             {
1023                 msg_Warn( p_this, "Option %s is deprecated. You should use "
1024                         "%s instead.", p_conf->psz_name, p_conf->psz_current );
1025                 free( psz_name );
1026                 psz_name = strdup( p_conf->psz_current );
1027             }
1028         }
1029         /* </Check if the option is deprecated> */
1030
1031         /* get the type of the variable */
1032         i_type = config_GetType( p_this, psz_name );
1033         if( !i_type )
1034         {
1035             msg_Warn( p_this, "unknown option %s (value=%s)",
1036                       cfg->psz_name, cfg->psz_value );
1037             goto next;
1038         }
1039         if( i_type != VLC_VAR_BOOL && cfg->psz_value == NULL )
1040         {
1041             msg_Warn( p_this, "missing value for option %s", cfg->psz_name );
1042             goto next;
1043         }
1044         if( i_type != VLC_VAR_STRING && b_once )
1045         {
1046             msg_Warn( p_this, "*option_name need to be a string option" );
1047             goto next;
1048         }
1049
1050         switch( i_type )
1051         {
1052             case VLC_VAR_BOOL:
1053                 val.b_bool = b_yes;
1054                 break;
1055             case VLC_VAR_INTEGER:
1056                 val.i_int = strtol( cfg->psz_value ? cfg->psz_value : "0",
1057                                     NULL, 0 );
1058                 break;
1059             case VLC_VAR_FLOAT:
1060                 val.f_float = atof( cfg->psz_value ? cfg->psz_value : "0" );
1061                 break;
1062             case VLC_VAR_STRING:
1063             case VLC_VAR_MODULE:
1064                 val.psz_string = cfg->psz_value;
1065                 break;
1066             default:
1067                 msg_Warn( p_this, "unhandled config var type" );
1068                 memset( &val, 0, sizeof( vlc_value_t ) );
1069                 break;
1070         }
1071         if( b_once )
1072         {
1073             vlc_value_t val2;
1074
1075             var_Get( p_this, psz_name, &val2 );
1076             if( *val2.psz_string )
1077             {
1078                 free( val2.psz_string );
1079                 msg_Dbg( p_this, "ignoring option %s (not first occurrence)", psz_name );
1080                 goto next;
1081             }
1082             free( val2.psz_string );
1083         }
1084         var_Set( p_this, psz_name, val );
1085         msg_Dbg( p_this, "set sout option: %s to %s", psz_name, cfg->psz_value );
1086
1087     next:
1088         free( psz_name );
1089         cfg = cfg->p_next;
1090     }
1091 }
1092
1093
1094 /*
1095  * XXX name and p_cfg are used (-> do NOT free them)
1096  */
1097 sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_chain )
1098 {
1099     sout_stream_t *p_stream;
1100
1101     if( !psz_chain )
1102     {
1103         msg_Err( p_sout, "invalid chain" );
1104         return NULL;
1105     }
1106
1107     p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
1108
1109     if( !p_stream )
1110     {
1111         msg_Err( p_sout, "out of memory" );
1112         return NULL;
1113     }
1114
1115     p_stream->p_sout   = p_sout;
1116     p_stream->p_sys    = NULL;
1117
1118     p_stream->psz_next =
1119         sout_CfgCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
1120
1121     msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
1122
1123     vlc_object_attach( p_stream, p_sout );
1124
1125     p_stream->p_module =
1126         module_Need( p_stream, "sout stream", p_stream->psz_name, VLC_TRUE );
1127
1128     if( !p_stream->p_module )
1129     {
1130         sout_StreamDelete( p_stream );
1131         return NULL;
1132     }
1133
1134     return p_stream;
1135 }
1136
1137 void sout_StreamDelete( sout_stream_t *p_stream )
1138 {
1139     msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
1140
1141     vlc_object_detach( p_stream );
1142     if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
1143
1144     FREENULL( p_stream->psz_name );
1145     FREENULL( p_stream->psz_next );
1146
1147     sout_CfgDestroy( p_stream->p_cfg );
1148
1149     msg_Dbg( p_stream, "destroying chain done" );
1150     vlc_object_destroy( p_stream );
1151 }
1152
1153 static char *_sout_stream_url_to_chain( vlc_object_t *p_this, char *psz_url )
1154 {
1155     mrl_t       mrl;
1156     char        *psz_chain, *p;
1157
1158     mrl_Parse( &mrl, psz_url );
1159     p = psz_chain = malloc( 500 + strlen( mrl.psz_way ) +
1160                                   strlen( mrl.psz_access ) +
1161                                   strlen( mrl.psz_name ) );
1162
1163
1164     if( config_GetInt( p_this, "sout-display" ) )
1165     {
1166         p += sprintf( p, "duplicate{dst=display,dst=std{mux=\"%s\","
1167                       "access=\"%s\",dst=\"%s\"}}",
1168                       mrl.psz_way, mrl.psz_access, mrl.psz_name );
1169     }
1170     else
1171     {
1172         p += sprintf( p, "std{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
1173                       mrl.psz_way, mrl.psz_access, mrl.psz_name );
1174     }
1175
1176     mrl_Clean( &mrl );
1177     return( psz_chain );
1178 }