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