]> git.sesse.net Git - vlc/blob - src/stream_output/stream_output.c
* stream_output: sout_buffer_t -> block_t.
[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     TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
503     if( i_index >= 0 )
504     {
505         if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
506         {
507             msg_Err( p_mux, "cannot del this stream from mux" );
508         }
509
510         /* remove the entry */
511         TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
512
513         if( p_mux->i_nb_inputs == 0 )
514         {
515             msg_Warn( p_mux, "no more input stream for this mux" );
516         }
517
518         block_FifoRelease( p_input->p_fifo );
519         free( p_input );
520     }
521 }
522
523 /*****************************************************************************
524  * sout_MuxSendBuffer:
525  *****************************************************************************/
526 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
527                          block_t *p_buffer )
528 {
529     block_FifoPut( p_input->p_fifo, p_buffer );
530
531     if( p_mux->b_waiting_stream )
532     {
533         if( p_mux->i_add_stream_start > 0 &&
534             p_mux->i_add_stream_start + (mtime_t)1500000 < mdate() )
535         {
536             /* more than 1.5 second, start muxing */
537             p_mux->b_waiting_stream = VLC_FALSE;
538         }
539         else
540         {
541             return;
542         }
543     }
544     p_mux->pf_mux( p_mux );
545 }
546
547 /*****************************************************************************
548  *
549  *****************************************************************************/
550 static int  mrl_Parse( mrl_t *p_mrl, char *psz_mrl )
551 {
552     char * psz_dup = strdup( psz_mrl );
553     char * psz_parser = psz_dup;
554     char * psz_access = "";
555     char * psz_way = "";
556     char * psz_name = "";
557
558     /* *** first parse psz_dest */
559     while( *psz_parser && *psz_parser != ':' )
560     {
561         if( *psz_parser == '{' )
562         {
563             while( *psz_parser && *psz_parser != '}' )
564             {
565                 psz_parser++;
566             }
567             if( *psz_parser )
568             {
569                 psz_parser++;
570             }
571         }
572         else
573         {
574             psz_parser++;
575         }
576     }
577 #if defined( WIN32 ) || defined( UNDER_CE )
578     if( psz_parser - psz_dup == 1 )
579     {
580         /* msg_Warn( p_sout, "drive letter %c: found in source string",
581                           *psz_dup ) ; */
582         psz_parser = "";
583     }
584 #endif
585
586     if( !*psz_parser )
587     {
588         psz_access = psz_way = "";
589         psz_name = psz_dup;
590     }
591     else
592     {
593         *psz_parser++ = '\0';
594
595         /* let's skip '//' */
596         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
597         {
598             psz_parser += 2 ;
599         }
600
601         psz_name = psz_parser ;
602
603         /* Come back to parse the access and mux plug-ins */
604         psz_parser = psz_dup;
605
606         if( !*psz_parser )
607         {
608             /* No access */
609             psz_access = "";
610         }
611         else if( *psz_parser == '/' )
612         {
613             /* No access */
614             psz_access = "";
615             psz_parser++;
616         }
617         else
618         {
619             psz_access = psz_parser;
620
621             while( *psz_parser && *psz_parser != '/' )
622             {
623                 if( *psz_parser == '{' )
624                 {
625                     while( *psz_parser && *psz_parser != '}' )
626                     {
627                         psz_parser++;
628                     }
629                     if( *psz_parser )
630                     {
631                         psz_parser++;
632                     }
633                 }
634                 else
635                 {
636                     psz_parser++;
637                 }
638             }
639
640             if( *psz_parser == '/' )
641             {
642                 *psz_parser++ = '\0';
643             }
644         }
645
646         if( !*psz_parser )
647         {
648             /* No mux */
649             psz_way = "";
650         }
651         else
652         {
653             psz_way = psz_parser;
654         }
655     }
656
657     p_mrl->psz_access = strdup( psz_access );
658     p_mrl->psz_way    = strdup( psz_way );
659     p_mrl->psz_name   = strdup( psz_name );
660
661     free( psz_dup );
662     return( VLC_SUCCESS );
663 }
664
665
666 /* mrl_Clean: clean p_mrl  after a call to mrl_Parse */
667 static void mrl_Clean( mrl_t *p_mrl )
668 {
669     FREE( p_mrl->psz_access );
670     FREE( p_mrl->psz_way );
671     FREE( p_mrl->psz_name );
672 }
673
674
675 /****************************************************************************
676  ****************************************************************************
677  **
678  **
679  **
680  ****************************************************************************
681  ****************************************************************************/
682
683 /* create a complete chain */
684 /* chain format:
685     module{option=*:option=*}[:module{option=*:...}]
686  */
687
688 static char *_strndup( char *str, int i_len )
689 {
690     char *p;
691
692     p = malloc( i_len + 1 );
693     strncpy( p, str, i_len );
694     p[i_len] = '\0';
695
696     return( p );
697 }
698
699 /*
700  * parse module{options=str, option="str "}:
701  *  return a pointer on the rest
702  *  XXX: psz_chain is modified
703  */
704 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
705 /* go accross " " and { } */
706 static char *_get_chain_end( char *str )
707 {
708     char *p = str;
709
710     SKIPSPACE( p );
711
712     for( ;; )
713     {
714         if( *p == '{' || *p == '"' || *p == '\'')
715         {
716             char c;
717
718             if( *p == '{' )
719             {
720                 c = '}';
721             }
722             else
723             {
724                 c = *p;
725             }
726             p++;
727
728             for( ;; )
729             {
730                 if( *p == '\0' )
731                 {
732                     return p;
733                 }
734
735                 if( *p == c )
736                 {
737                     p++;
738                     return p;
739                 }
740                 else if( *p == '{' && c == '}' )
741                 {
742                     p = _get_chain_end( p );
743                 }
744                 else
745                 {
746                     p++;
747                 }
748             }
749         }
750         else if( *p == '\0' || *p == ',' || *p == '}' || *p == ' ' || *p == '\t' )
751         {
752             return p;
753         }
754         else
755         {
756             p++;
757         }
758     }
759 }
760
761 char * sout_cfg_parser( char **ppsz_name, sout_cfg_t **pp_cfg, char *psz_chain )
762 {
763     sout_cfg_t *p_cfg = NULL;
764     char       *p = psz_chain;
765
766     *ppsz_name = NULL;
767     *pp_cfg    = NULL;
768
769     if( p == NULL )
770     {
771         return NULL;
772     }
773
774     SKIPSPACE( p );
775
776     while( *p && *p != '{' && *p != ':' && *p != ' ' && *p != '\t' )
777     {
778         p++;
779     }
780
781     if( p == psz_chain )
782     {
783         return NULL;
784     }
785
786     *ppsz_name = _strndup( psz_chain, p - psz_chain );
787
788     SKIPSPACE( p );
789
790     if( *p == '{' )
791     {
792         char *psz_name;
793
794         p++;
795
796         for( ;; )
797         {
798             sout_cfg_t cfg;
799
800             SKIPSPACE( p );
801
802             psz_name = p;
803
804             while( *p && *p != '=' && *p != ',' && *p != '}' && *p != ' ' && *p != '\t' )
805             {
806                 p++;
807             }
808
809             /* fprintf( stderr, "name=%s - rest=%s\n", psz_name, p ); */
810             if( p == psz_name )
811             {
812                 fprintf( stderr, "invalid options (empty)" );
813                 break;
814             }
815
816             cfg.psz_name = _strndup( psz_name, p - psz_name );
817
818             SKIPSPACE( p );
819
820             if( *p == '=' )
821             {
822                 char *end;
823
824                 p++;
825
826                 end = _get_chain_end( p );
827                 if( end <= p )
828                 {
829                     cfg.psz_value = NULL;
830                 }
831                 else
832                 {
833                     if( *p == '\'' || *p =='"' || *p == '{' )
834                     {
835                         p++;
836                         end--;
837                     }
838                     if( end <= p )
839                     {
840                         cfg.psz_value = NULL;
841                     }
842                     else
843                     {
844                         cfg.psz_value = _strndup( p, end - p );
845                     }
846                 }
847
848                 p = end;
849                 SKIPSPACE( p );
850             }
851             else
852             {
853                 cfg.psz_value = NULL;
854             }
855
856             cfg.p_next = NULL;
857             if( p_cfg )
858             {
859                 p_cfg->p_next = malloc( sizeof( sout_cfg_t ) );
860                 memcpy( p_cfg->p_next, &cfg, sizeof( sout_cfg_t ) );
861
862                 p_cfg = p_cfg->p_next;
863             }
864             else
865             {
866                 p_cfg = malloc( sizeof( sout_cfg_t ) );
867                 memcpy( p_cfg, &cfg, sizeof( sout_cfg_t ) );
868
869                 *pp_cfg = p_cfg;
870             }
871
872             if( *p == ',' )
873             {
874                 p++;
875             }
876
877             if( *p == '}' )
878             {
879                 p++;
880
881                 break;
882             }
883         }
884     }
885
886     if( *p == ':' )
887     {
888         return( strdup( p + 1 ) );
889     }
890
891     return( NULL );
892 }
893
894 static void sout_cfg_free( sout_cfg_t *p_cfg )
895 {
896     while( p_cfg != NULL )
897     {
898         sout_cfg_t *p_next;
899
900         p_next = p_cfg->p_next;
901
902         FREE( p_cfg->psz_name );
903         FREE( p_cfg->psz_value );
904         free( p_cfg );
905
906         p_cfg = p_next;
907     }
908 }
909
910
911 /*
912  * XXX name and p_cfg are used (-> do NOT free them)
913  */
914 sout_stream_t *sout_stream_new( sout_instance_t *p_sout,
915                                 char *psz_chain )
916 {
917     sout_stream_t *p_stream;
918
919     if( !psz_chain )
920     {
921         msg_Err( p_sout, "invalid chain" );
922         return NULL;
923     }
924
925     p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
926
927     if( !p_stream )
928     {
929         msg_Err( p_sout, "out of memory" );
930         return NULL;
931     }
932
933     p_stream->p_sout   = p_sout;
934     p_stream->p_sys    = NULL;
935
936     p_stream->psz_next =
937         sout_cfg_parser( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
938
939     msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
940
941     p_stream->p_module =
942         module_Need( p_stream, "sout stream", p_stream->psz_name, VLC_TRUE );
943
944     if( !p_stream->p_module )
945     {
946         sout_stream_delete( p_stream );
947         return NULL;
948     }
949
950     return p_stream;
951 }
952
953 void sout_stream_delete( sout_stream_t *p_stream )
954 {
955     msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
956     if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
957
958     FREE( p_stream->psz_name );
959     FREE( p_stream->psz_next );
960
961     sout_cfg_free( p_stream->p_cfg );
962
963     msg_Dbg( p_stream, "destroying chain done" );
964     vlc_object_destroy( p_stream );
965 }
966
967 static char *_sout_stream_url_to_chain( vlc_object_t *p_this, char *psz_url )
968 {
969     mrl_t       mrl;
970     char        *psz_chain, *p;
971
972     mrl_Parse( &mrl, psz_url );
973     p = psz_chain = malloc( 500 + strlen( mrl.psz_way ) +
974                                   strlen( mrl.psz_access ) +
975                                   strlen( mrl.psz_name ) );
976
977
978     if( config_GetInt( p_this, "sout-display" ) )
979     {
980         p += sprintf( p, "duplicate{dst=display,dst=std{mux=\"%s\",access=\"%s\",url=\"%s\"}}",
981                       mrl.psz_way, mrl.psz_access, mrl.psz_name );
982     }
983     else
984     {
985         p += sprintf( p, "std{mux=\"%s\",access=\"%s\",url=\"%s\"}",
986                       mrl.psz_way, mrl.psz_access, mrl.psz_name );
987     }
988
989     mrl_Clean( &mrl );
990     return( psz_chain );
991 }