]> git.sesse.net Git - vlc/blob - src/stream_output/stream_output.c
Use the new block_Fifo functions
[vlc] / src / stream_output / stream_output.c
1 /*****************************************************************************
2  * stream_output.c : stream output module
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Eric Petit <titer@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #include <vlc/vlc.h>
31
32 #include <stdlib.h>                                                /* free() */
33 #include <stdio.h>                                              /* sprintf() */
34 #include <string.h>                                            /* strerror() */
35
36 #include <vlc_sout.h>
37 #include <vlc_playlist.h>
38
39 #include "stream_output.h"
40
41 #include <vlc_meta.h>
42
43 #include "input/input_internal.h"
44
45 #undef DEBUG_BUFFER
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 #define sout_stream_url_to_chain( p, s ) \
50     _sout_stream_url_to_chain( VLC_OBJECT(p), s )
51 static char *_sout_stream_url_to_chain( vlc_object_t *, char * );
52
53 /*
54  * Generic MRL parser
55  *
56  */
57
58 typedef struct
59 {
60     char *psz_access;
61     char *psz_way;
62     char *psz_name;
63 } mrl_t;
64
65 /* mrl_Parse: parse psz_mrl and fill p_mrl */
66 static int  mrl_Parse( mrl_t *p_mrl, const char *psz_mrl );
67 /* mrl_Clean: clean p_mrl  after a call to mrl_Parse */
68 static void mrl_Clean( mrl_t *p_mrl );
69
70 /*****************************************************************************
71  * sout_NewInstance: creates a new stream output instance
72  *****************************************************************************/
73 sout_instance_t *__sout_NewInstance( vlc_object_t *p_parent, char * psz_dest )
74 {
75     sout_instance_t *p_sout;
76
77     /* *** Allocate descriptor *** */
78     p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
79     if( p_sout == NULL )
80     {
81         msg_Err( p_parent, "out of memory" );
82         return NULL;
83     }
84
85     /* *** init descriptor *** */
86     p_sout->psz_sout    = strdup( psz_dest );
87     p_sout->p_meta      = NULL;
88     p_sout->i_out_pace_nocontrol = 0;
89     p_sout->p_sys       = NULL;
90
91     vlc_mutex_init( p_sout, &p_sout->lock );
92     if( psz_dest && psz_dest[0] == '#' )
93     {
94         p_sout->psz_chain = strdup( &psz_dest[1] );
95     }
96     else
97     {
98         p_sout->psz_chain = sout_stream_url_to_chain( p_sout, psz_dest );
99         msg_Dbg( p_sout, "using sout chain=`%s'", p_sout->psz_chain );
100     }
101     p_sout->p_stream = NULL;
102
103     /* attach it for inherit */
104     vlc_object_attach( p_sout, p_parent );
105
106     p_sout->p_stream = sout_StreamNew( p_sout, p_sout->psz_chain );
107
108     if( p_sout->p_stream == NULL )
109     {
110         msg_Err( p_sout, "stream chain failed for `%s'", p_sout->psz_chain );
111
112         FREENULL( p_sout->psz_sout );
113         FREENULL( p_sout->psz_chain );
114
115         vlc_object_detach( p_sout );
116         vlc_object_destroy( p_sout );
117         return NULL;
118     }
119
120     return p_sout;
121 }
122
123 /*****************************************************************************
124  * sout_DeleteInstance: delete a previously allocated instance
125  *****************************************************************************/
126 void sout_DeleteInstance( sout_instance_t * p_sout )
127 {
128     /* remove the stream out chain */
129     sout_StreamDelete( p_sout->p_stream );
130
131     /* *** free all string *** */
132     FREENULL( p_sout->psz_sout );
133     FREENULL( p_sout->psz_chain );
134
135     /* delete meta */
136     if( p_sout->p_meta )
137     {
138         vlc_meta_Delete( p_sout->p_meta );
139     }
140
141     vlc_mutex_destroy( &p_sout->lock );
142
143     /* *** free structure *** */
144     vlc_object_destroy( p_sout );
145 }
146
147 /*****************************************************************************
148  * Packetizer/Input
149  *****************************************************************************/
150 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
151                                         es_format_t *p_fmt )
152 {
153     sout_packetizer_input_t *p_input;
154
155     msg_Dbg( p_sout, "adding a new input" );
156
157     /* *** create a packetizer input *** */
158     p_input         = malloc( sizeof( sout_packetizer_input_t ) );
159     p_input->p_sout = p_sout;
160     p_input->p_fmt  = p_fmt;
161
162     if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
163     {
164         vlc_object_release( p_sout );
165         return p_input;
166     }
167
168     /* *** add it to the stream chain */
169     vlc_mutex_lock( &p_sout->lock );
170     p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
171     vlc_mutex_unlock( &p_sout->lock );
172
173     if( p_input->id == NULL )
174     {
175         free( p_input );
176         return NULL;
177     }
178
179     return( p_input );
180 }
181
182 /*****************************************************************************
183  *
184  *****************************************************************************/
185 int sout_InputDelete( sout_packetizer_input_t *p_input )
186 {
187     sout_instance_t     *p_sout = p_input->p_sout;
188
189     msg_Dbg( p_sout, "removing an input" );
190
191     if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
192     {
193         vlc_mutex_lock( &p_sout->lock );
194         p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
195         vlc_mutex_unlock( &p_sout->lock );
196     }
197
198     free( p_input );
199
200     return( VLC_SUCCESS);
201 }
202
203 /*****************************************************************************
204  *
205  *****************************************************************************/
206 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
207                           block_t *p_buffer )
208 {
209     sout_instance_t     *p_sout = p_input->p_sout;
210     int                 i_ret;
211
212     if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
213     {
214         block_Release( p_buffer );
215         return VLC_SUCCESS;
216     }
217     if( p_buffer->i_dts <= 0 )
218     {
219         msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
220         block_Release( p_buffer );
221         return VLC_SUCCESS;
222     }
223
224     vlc_mutex_lock( &p_sout->lock );
225     i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
226                                        p_input->id, p_buffer );
227     vlc_mutex_unlock( &p_sout->lock );
228
229     return i_ret;
230 }
231
232 /*****************************************************************************
233  * sout_AccessOutNew: allocate a new access out
234  *****************************************************************************/
235 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
236                                       const char *psz_access, const char *psz_name )
237 {
238     sout_access_out_t *p_access;
239     char              *psz_next;
240
241     if( !( p_access = vlc_object_create( p_sout,
242                                          sizeof( sout_access_out_t ) ) ) )
243     {
244         msg_Err( p_sout, "out of memory" );
245         return NULL;
246     }
247
248     psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
249                                    psz_access );
250     if( psz_next )
251     {
252         free( psz_next );
253     }
254     p_access->psz_path   = strdup( psz_name ? psz_name : "" );
255     p_access->p_sout     = p_sout;
256     p_access->p_sys = NULL;
257     p_access->pf_seek    = NULL;
258     p_access->pf_read    = NULL;
259     p_access->pf_write   = NULL;
260     p_access->pf_control = NULL;
261     p_access->p_module   = NULL;
262
263     p_access->i_writes = 0;
264     p_access->i_sent_bytes = 0;
265
266     vlc_object_attach( p_access, p_sout );
267
268     p_access->p_module   =
269         module_Need( p_access, "sout access", p_access->psz_access, VLC_TRUE );
270
271     if( !p_access->p_module )
272     {
273         free( p_access->psz_access );
274         free( p_access->psz_path );
275         vlc_object_detach( p_access );
276         vlc_object_destroy( p_access );
277         return( NULL );
278     }
279
280     return p_access;
281 }
282 /*****************************************************************************
283  * sout_AccessDelete: delete an access out
284  *****************************************************************************/
285 void sout_AccessOutDelete( sout_access_out_t *p_access )
286 {
287     vlc_object_detach( p_access );
288     if( p_access->p_module )
289     {
290         module_Unneed( p_access, p_access->p_module );
291     }
292     free( p_access->psz_access );
293
294     config_ChainDestroy( p_access->p_cfg );
295
296     free( p_access->psz_path );
297
298     vlc_object_destroy( p_access );
299 }
300
301 /*****************************************************************************
302  * sout_AccessSeek:
303  *****************************************************************************/
304 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
305 {
306     return p_access->pf_seek( p_access, i_pos );
307 }
308
309 /*****************************************************************************
310  * sout_AccessRead:
311  *****************************************************************************/
312 int sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
313 {
314     return( p_access->pf_read ?
315             p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
316 }
317
318 /*****************************************************************************
319  * sout_AccessWrite:
320  *****************************************************************************/
321 int sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
322 {
323     int i_total = 0;
324     p_access->i_writes++;
325     p_access->i_sent_bytes += p_buffer->i_buffer;
326     if( p_access->p_libvlc->b_stats && p_access->i_writes % 30 == 0 )
327     {
328         /* Access_out -> sout_instance -> input_thread_t */
329         input_thread_t *p_input =
330             (input_thread_t *)vlc_object_find( p_access, VLC_OBJECT_INPUT,
331                                                FIND_PARENT );
332         if( p_input )
333         {
334             stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_packets,
335                      30, NULL );
336             stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_bytes,
337                                  p_access->i_sent_bytes, &i_total );
338             stats_UpdateFloat( p_input, p_input->p->counters.p_sout_send_bitrate,
339                     (float)i_total, NULL );
340             p_access->i_sent_bytes = 0;
341             vlc_object_release( p_input );
342         }
343     }
344     return p_access->pf_write( p_access, p_buffer );
345 }
346
347 /**
348  * sout_AccessOutControl
349  */
350 int sout_AccessOutControl (sout_access_out_t *access, int query, va_list args)
351 {
352     return (access->pf_control) ? access->pf_control (access, query, args)
353                                 : VLC_EGENERIC;
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, sizeof( sout_mux_t ) );
366     if( p_mux == NULL )
367     {
368         msg_Err( p_sout, "out of memory" );
369         return NULL;
370     }
371
372     p_mux->p_sout = p_sout;
373     psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
374     if( psz_next ) free( psz_next );
375
376     p_mux->p_access     = p_access;
377     p_mux->pf_control   = NULL;
378     p_mux->pf_addstream = NULL;
379     p_mux->pf_delstream = NULL;
380     p_mux->pf_mux       = NULL;
381     p_mux->i_nb_inputs  = 0;
382     p_mux->pp_inputs    = NULL;
383
384     p_mux->p_sys        = NULL;
385     p_mux->p_module     = NULL;
386
387     p_mux->b_add_stream_any_time = VLC_FALSE;
388     p_mux->b_waiting_stream = VLC_TRUE;
389     p_mux->i_add_stream_start = -1;
390
391     vlc_object_attach( p_mux, p_sout );
392
393     p_mux->p_module =
394         module_Need( p_mux, "sout mux", p_mux->psz_mux, VLC_TRUE );
395
396     if( p_mux->p_module == NULL )
397     {
398         FREENULL( p_mux->psz_mux );
399
400         vlc_object_detach( p_mux );
401         vlc_object_destroy( p_mux );
402         return NULL;
403     }
404
405     /* *** probe mux capacity *** */
406     if( p_mux->pf_control )
407     {
408         int b_answer = VLC_FALSE;
409
410         if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
411                              &b_answer ) )
412         {
413             b_answer = VLC_FALSE;
414         }
415
416         if( b_answer )
417         {
418             msg_Dbg( p_sout, "muxer support adding stream at any time" );
419             p_mux->b_add_stream_any_time = VLC_TRUE;
420             p_mux->b_waiting_stream = VLC_FALSE;
421
422             /* If we control the output pace then it's better to wait before
423              * starting muxing (generates better streams/files). */
424             if( !p_sout->i_out_pace_nocontrol )
425             {
426                 b_answer = VLC_TRUE;
427             }
428             else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
429                                       &b_answer ) )
430             {
431                 b_answer = VLC_FALSE;
432             }
433
434             if( b_answer )
435             {
436                 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
437                          "starting to mux" );
438                 p_mux->b_waiting_stream = VLC_TRUE;
439             }
440         }
441     }
442
443     return p_mux;
444 }
445
446 /*****************************************************************************
447  * sout_MuxDelete:
448  *****************************************************************************/
449 void sout_MuxDelete( sout_mux_t *p_mux )
450 {
451     vlc_object_detach( p_mux );
452     if( p_mux->p_module )
453     {
454         module_Unneed( p_mux, p_mux->p_module );
455     }
456     free( p_mux->psz_mux );
457
458     config_ChainDestroy( p_mux->p_cfg );
459
460     vlc_object_destroy( p_mux );
461 }
462
463 /*****************************************************************************
464  * sout_MuxAddStream:
465  *****************************************************************************/
466 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
467 {
468     sout_input_t *p_input;
469
470     if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
471     {
472         msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
473                         "to this format)" );
474         return NULL;
475     }
476
477     msg_Dbg( p_mux, "adding a new input" );
478
479     /* create a new sout input */
480     p_input = malloc( sizeof( sout_input_t ) );
481     p_input->p_sout = p_mux->p_sout;
482     p_input->p_fmt  = p_fmt;
483     p_input->p_fifo = block_FifoNew( p_mux->p_sout );
484     p_input->p_sys  = NULL;
485
486     TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
487     if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
488     {
489             msg_Err( p_mux, "cannot add this stream" );
490             TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
491             block_FifoRelease( p_input->p_fifo );
492             free( p_input );
493             return NULL;
494     }
495
496     return p_input;
497 }
498
499 /*****************************************************************************
500  * sout_MuxDeleteStream:
501  *****************************************************************************/
502 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
503 {
504     int i_index;
505
506     if( p_mux->b_waiting_stream
507      && block_FifoCount( p_input->p_fifo ) > 0 )
508     {
509         /* We stop waiting, and call the muxer for taking care of the data
510          * before we remove this es */
511         p_mux->b_waiting_stream = VLC_FALSE;
512         p_mux->pf_mux( p_mux );
513     }
514
515     TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
516     if( i_index >= 0 )
517     {
518         if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
519         {
520             msg_Err( p_mux, "cannot delete this stream from mux" );
521         }
522
523         /* remove the entry */
524         TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
525
526         if( p_mux->i_nb_inputs == 0 )
527         {
528             msg_Warn( p_mux, "no more input streams for this mux" );
529         }
530
531         block_FifoRelease( p_input->p_fifo );
532         free( p_input );
533     }
534 }
535
536 /*****************************************************************************
537  * sout_MuxSendBuffer:
538  *****************************************************************************/
539 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
540                          block_t *p_buffer )
541 {
542     block_FifoPut( p_input->p_fifo, p_buffer );
543
544     if( p_mux->p_sout->i_out_pace_nocontrol )
545     {
546         mtime_t current_date = mdate();
547         if ( current_date > p_buffer->i_dts )
548             msg_Warn( p_mux, "late buffer for mux input ("I64Fd")",
549                       current_date - p_buffer->i_dts );
550     }
551
552     if( p_mux->b_waiting_stream )
553     {
554         if( p_mux->i_add_stream_start < 0 )
555         {
556             p_mux->i_add_stream_start = p_buffer->i_dts;
557         }
558
559         if( p_mux->i_add_stream_start >= 0 &&
560             p_mux->i_add_stream_start + I64C(1500000) < p_buffer->i_dts )
561         {
562             /* Wait until we have more than 1.5 seconds worth of data
563              * before start muxing */
564             p_mux->b_waiting_stream = VLC_FALSE;
565         }
566         else
567         {
568             return;
569         }
570     }
571     p_mux->pf_mux( p_mux );
572 }
573
574 /*****************************************************************************
575  *
576  *****************************************************************************/
577 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
578 {
579     char * psz_dup = strdup( psz_mrl );
580     char * psz_parser = psz_dup;
581     const char * psz_access;
582     const char * psz_way;
583     char * psz_name;
584
585     /* *** first parse psz_dest */
586     while( *psz_parser && *psz_parser != ':' )
587     {
588         if( *psz_parser == '{' )
589         {
590             while( *psz_parser && *psz_parser != '}' )
591             {
592                 psz_parser++;
593             }
594             if( *psz_parser )
595             {
596                 psz_parser++;
597             }
598         }
599         else
600         {
601             psz_parser++;
602         }
603     }
604 #if defined( WIN32 ) || defined( UNDER_CE )
605     if( psz_parser - psz_dup == 1 )
606     {
607         /* msg_Warn( p_sout, "drive letter %c: found in source string",
608                           *psz_dup ) ; */
609         psz_parser = "";
610     }
611 #endif
612
613     if( !*psz_parser )
614     {
615         psz_access = psz_way = "";
616         psz_name = psz_dup;
617     }
618     else
619     {
620         *psz_parser++ = '\0';
621
622         /* let's skip '//' */
623         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
624         {
625             psz_parser += 2 ;
626         }
627
628         psz_name = psz_parser ;
629
630         /* Come back to parse the access and mux plug-ins */
631         psz_parser = psz_dup;
632
633         if( !*psz_parser )
634         {
635             /* No access */
636             psz_access = "";
637         }
638         else if( *psz_parser == '/' )
639         {
640             /* No access */
641             psz_access = "";
642             psz_parser++;
643         }
644         else
645         {
646             psz_access = psz_parser;
647
648             while( *psz_parser && *psz_parser != '/' )
649             {
650                 if( *psz_parser == '{' )
651                 {
652                     while( *psz_parser && *psz_parser != '}' )
653                     {
654                         psz_parser++;
655                     }
656                     if( *psz_parser )
657                     {
658                         psz_parser++;
659                     }
660                 }
661                 else
662                 {
663                     psz_parser++;
664                 }
665             }
666
667             if( *psz_parser == '/' )
668             {
669                 *psz_parser++ = '\0';
670             }
671         }
672
673         if( !*psz_parser )
674         {
675             /* No mux */
676             psz_way = "";
677         }
678         else
679         {
680             psz_way = psz_parser;
681         }
682     }
683
684     p_mrl->psz_access = strdup( psz_access );
685     p_mrl->psz_way    = strdup( psz_way );
686     p_mrl->psz_name   = strdup( psz_name );
687
688     free( psz_dup );
689     return( VLC_SUCCESS );
690 }
691
692
693 /* mrl_Clean: clean p_mrl  after a call to mrl_Parse */
694 static void mrl_Clean( mrl_t *p_mrl )
695 {
696     FREENULL( p_mrl->psz_access );
697     FREENULL( p_mrl->psz_way );
698     FREENULL( p_mrl->psz_name );
699 }
700
701
702 /****************************************************************************
703  ****************************************************************************
704  **
705  **
706  **
707  ****************************************************************************
708  ****************************************************************************/
709
710 /* create a complete chain */
711 /* chain format:
712     module{option=*:option=*}[:module{option=*:...}]
713  */
714
715 /*
716  * parse module{options=str, option="str "}:
717  *  return a pointer on the rest
718  *  XXX: psz_chain is modified
719  */
720 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
721 #define SKIPTRAILINGSPACE( p, e ) \
722     { while( e > p && ( *(e-1) == ' ' || *(e-1) == '\t' ) ) e--; }
723
724 /* go accross " " and { } */
725 static char *_get_chain_end( char *str )
726 {
727     char c, *p = str;
728
729     SKIPSPACE( p );
730
731     for( ;; )
732     {
733         if( !*p || *p == ',' || *p == '}' ) return p;
734
735         if( *p != '{' && *p != '"' && *p != '\'' )
736         {
737             p++;
738             continue;
739         }
740
741         if( *p == '{' ) c = '}';
742         else c = *p;
743         p++;
744
745         for( ;; )
746         {
747             if( !*p ) return p;
748
749             if( *p == c ) return ++p;
750             else if( *p == '{' && c == '}' ) p = _get_chain_end( p );
751             else p++;
752         }
753     }
754 }
755
756 /*
757  * XXX name and p_cfg are used (-> do NOT free them)
758  */
759 sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_chain )
760 {
761     sout_stream_t *p_stream;
762
763     if( !psz_chain )
764     {
765         msg_Err( p_sout, "invalid chain" );
766         return NULL;
767     }
768
769     p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
770
771     if( !p_stream )
772     {
773         msg_Err( p_sout, "out of memory" );
774         return NULL;
775     }
776
777     p_stream->p_sout   = p_sout;
778     p_stream->p_sys    = NULL;
779
780     p_stream->psz_next =
781         config_ChainCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
782
783     msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
784
785     vlc_object_attach( p_stream, p_sout );
786
787     p_stream->p_module =
788         module_Need( p_stream, "sout stream", p_stream->psz_name, VLC_TRUE );
789
790     if( !p_stream->p_module )
791     {
792         sout_StreamDelete( p_stream );
793         return NULL;
794     }
795
796     return p_stream;
797 }
798
799 void sout_StreamDelete( sout_stream_t *p_stream )
800 {
801     msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
802
803     vlc_object_detach( p_stream );
804     if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
805
806     FREENULL( p_stream->psz_name );
807     FREENULL( p_stream->psz_next );
808
809     config_ChainDestroy( p_stream->p_cfg );
810
811     msg_Dbg( p_stream, "destroying chain done" );
812     vlc_object_destroy( p_stream );
813 }
814
815 static char *_sout_stream_url_to_chain( vlc_object_t *p_this, char *psz_url )
816 {
817     mrl_t       mrl;
818     char        *psz_chain, *p;
819
820     mrl_Parse( &mrl, psz_url );
821     p = psz_chain = malloc( 500 + strlen( mrl.psz_way ) +
822                                   strlen( mrl.psz_access ) +
823                                   strlen( mrl.psz_name ) );
824
825
826     if( config_GetInt( p_this, "sout-display" ) )
827     {
828         p += sprintf( p, "duplicate{dst=display,dst=std{mux=\"%s\","
829                       "access=\"%s\",dst=\"%s\"}}",
830                       mrl.psz_way, mrl.psz_access, mrl.psz_name );
831     }
832     else
833     {
834         p += sprintf( p, "std{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
835                       mrl.psz_way, mrl.psz_access, mrl.psz_name );
836     }
837
838     mrl_Clean( &mrl );
839     return( psz_chain );
840 }