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