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