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