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