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