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