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