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