]> git.sesse.net Git - vlc/blob - src/stream_output/stream_output.c
e2c9d7e3d4c7e20a2aede98277828eded18df4d9
[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_set_name( p_access, p_access->psz_access );
317     vlc_object_attach( p_access, p_sout );
318
319     p_access->p_module   =
320         module_need( p_access, "sout access", p_access->psz_access, true );
321
322     if( !p_access->p_module )
323     {
324         free( p_access->psz_access );
325         free( p_access->psz_path );
326         vlc_object_detach( p_access );
327         vlc_object_release( p_access );
328         return( NULL );
329     }
330
331     return p_access;
332 }
333 /*****************************************************************************
334  * sout_AccessDelete: delete an access out
335  *****************************************************************************/
336 void sout_AccessOutDelete( sout_access_out_t *p_access )
337 {
338     vlc_object_detach( p_access );
339     if( p_access->p_module )
340     {
341         module_unneed( p_access, p_access->p_module );
342     }
343     free( p_access->psz_access );
344
345     config_ChainDestroy( p_access->p_cfg );
346
347     free( p_access->psz_path );
348
349     vlc_object_release( p_access );
350 }
351
352 /*****************************************************************************
353  * sout_AccessSeek:
354  *****************************************************************************/
355 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
356 {
357     return p_access->pf_seek( p_access, i_pos );
358 }
359
360 /*****************************************************************************
361  * sout_AccessRead:
362  *****************************************************************************/
363 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
364 {
365     return( p_access->pf_read ?
366             p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
367 }
368
369 /*****************************************************************************
370  * sout_AccessWrite:
371  *****************************************************************************/
372 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
373 {
374 #if 0
375     const unsigned i_packets_gather = 30;
376     p_access->i_writes++;
377     p_access->i_sent_bytes += p_buffer->i_buffer;
378     if( (p_access->i_writes % i_packets_gather) == 0 )
379     {
380         sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_PACKET, i_packets_gather );
381         sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_BYTE, p_access->i_sent_bytes );
382         p_access->i_sent_bytes = 0;
383     }
384 #endif
385     return p_access->pf_write( p_access, p_buffer );
386 }
387
388 /**
389  * sout_AccessOutControl
390  */
391 int sout_AccessOutControl (sout_access_out_t *access, int query, ...)
392 {
393     va_list ap;
394     int ret;
395
396     va_start (ap, query);
397     if (access->pf_control)
398         ret = access->pf_control (access, query, ap);
399     else
400         ret = VLC_EGENERIC;
401     va_end (ap);
402     return ret;
403 }
404
405 /*****************************************************************************
406  * sout_MuxNew: create a new mux
407  *****************************************************************************/
408 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
409                           sout_access_out_t *p_access )
410 {
411     static const char typename[] = "mux";
412     sout_mux_t *p_mux;
413     char       *psz_next;
414
415     p_mux = vlc_custom_create( p_sout, sizeof( *p_mux ), VLC_OBJECT_GENERIC,
416                                typename);
417     if( p_mux == NULL )
418         return NULL;
419
420     p_mux->p_sout = p_sout;
421     psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
422     free( psz_next );
423
424     p_mux->p_access     = p_access;
425     p_mux->pf_control   = NULL;
426     p_mux->pf_addstream = NULL;
427     p_mux->pf_delstream = NULL;
428     p_mux->pf_mux       = NULL;
429     p_mux->i_nb_inputs  = 0;
430     p_mux->pp_inputs    = NULL;
431
432     p_mux->p_sys        = NULL;
433     p_mux->p_module     = NULL;
434
435     p_mux->b_add_stream_any_time = false;
436     p_mux->b_waiting_stream = true;
437     p_mux->i_add_stream_start = -1;
438
439     vlc_object_set_name( p_mux, p_mux->psz_mux );
440     vlc_object_attach( p_mux, p_sout );
441
442     p_mux->p_module =
443         module_need( p_mux, "sout mux", p_mux->psz_mux, true );
444
445     if( p_mux->p_module == NULL )
446     {
447         FREENULL( p_mux->psz_mux );
448
449         vlc_object_detach( p_mux );
450         vlc_object_release( p_mux );
451         return NULL;
452     }
453
454     /* *** probe mux capacity *** */
455     if( p_mux->pf_control )
456     {
457         int b_answer = false;
458
459         if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
460                              &b_answer ) )
461         {
462             b_answer = false;
463         }
464
465         if( b_answer )
466         {
467             msg_Dbg( p_sout, "muxer support adding stream at any time" );
468             p_mux->b_add_stream_any_time = true;
469             p_mux->b_waiting_stream = false;
470
471             /* If we control the output pace then it's better to wait before
472              * starting muxing (generates better streams/files). */
473             if( !p_sout->i_out_pace_nocontrol )
474             {
475                 b_answer = true;
476             }
477             else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
478                                       &b_answer ) )
479             {
480                 b_answer = false;
481             }
482
483             if( b_answer )
484             {
485                 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
486                          "starting to mux" );
487                 p_mux->b_waiting_stream = true;
488             }
489         }
490     }
491
492     return p_mux;
493 }
494
495 /*****************************************************************************
496  * sout_MuxDelete:
497  *****************************************************************************/
498 void sout_MuxDelete( sout_mux_t *p_mux )
499 {
500     vlc_object_detach( p_mux );
501     if( p_mux->p_module )
502     {
503         module_unneed( p_mux, p_mux->p_module );
504     }
505     free( p_mux->psz_mux );
506
507     config_ChainDestroy( p_mux->p_cfg );
508
509     vlc_object_release( p_mux );
510 }
511
512 /*****************************************************************************
513  * sout_MuxAddStream:
514  *****************************************************************************/
515 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
516 {
517     sout_input_t *p_input;
518
519     if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
520     {
521         msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
522                         "to this format). You can try increasing sout-mux-caching value" );
523         return NULL;
524     }
525
526     msg_Dbg( p_mux, "adding a new input" );
527
528     /* create a new sout input */
529     p_input = malloc( sizeof( sout_input_t ) );
530     if( !p_input )
531         return NULL;
532     p_input->p_sout = p_mux->p_sout;
533     p_input->p_fmt  = p_fmt;
534     p_input->p_fifo = block_FifoNew();
535     p_input->p_sys  = NULL;
536
537     TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
538     if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
539     {
540         msg_Err( p_mux, "cannot add this stream" );
541         TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
542         block_FifoRelease( p_input->p_fifo );
543         free( p_input );
544         return NULL;
545     }
546
547     return p_input;
548 }
549
550 /*****************************************************************************
551  * sout_MuxDeleteStream:
552  *****************************************************************************/
553 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
554 {
555     int i_index;
556
557     if( p_mux->b_waiting_stream
558      && block_FifoCount( p_input->p_fifo ) > 0 )
559     {
560         /* We stop waiting, and call the muxer for taking care of the data
561          * before we remove this es */
562         p_mux->b_waiting_stream = false;
563         p_mux->pf_mux( p_mux );
564     }
565
566     TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
567     if( i_index >= 0 )
568     {
569         if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
570         {
571             msg_Err( p_mux, "cannot delete this stream from mux" );
572         }
573
574         /* remove the entry */
575         TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
576
577         if( p_mux->i_nb_inputs == 0 )
578         {
579             msg_Warn( p_mux, "no more input streams for this mux" );
580         }
581
582         block_FifoRelease( p_input->p_fifo );
583         free( p_input );
584     }
585 }
586
587 /*****************************************************************************
588  * sout_MuxSendBuffer:
589  *****************************************************************************/
590 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
591                          block_t *p_buffer )
592 {
593     block_FifoPut( p_input->p_fifo, p_buffer );
594
595     if( p_mux->p_sout->i_out_pace_nocontrol )
596     {
597         mtime_t current_date = mdate();
598         if ( current_date > p_buffer->i_dts )
599             msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
600                       current_date - p_buffer->i_dts );
601     }
602
603     if( p_mux->b_waiting_stream )
604     {
605         const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * INT64_C(1000);
606
607         if( p_mux->i_add_stream_start < 0 )
608             p_mux->i_add_stream_start = p_buffer->i_dts;
609
610         /* Wait until we have enought data before muxing */
611         if( p_mux->i_add_stream_start < 0 ||
612             p_buffer->i_dts < p_mux->i_add_stream_start + i_caching )
613             return;
614         p_mux->b_waiting_stream = false;
615     }
616     p_mux->pf_mux( p_mux );
617 }
618
619 /*****************************************************************************
620  *
621  *****************************************************************************/
622 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
623 {
624     char * psz_dup = strdup( psz_mrl );
625     char * psz_parser = psz_dup;
626     const char * psz_access;
627     const char * psz_way;
628     char * psz_name;
629
630     /* *** first parse psz_dest */
631     while( *psz_parser && *psz_parser != ':' )
632     {
633         if( *psz_parser == '{' )
634         {
635             while( *psz_parser && *psz_parser != '}' )
636             {
637                 psz_parser++;
638             }
639             if( *psz_parser )
640             {
641                 psz_parser++;
642             }
643         }
644         else
645         {
646             psz_parser++;
647         }
648     }
649 #if defined( WIN32 ) || defined( UNDER_CE )
650     if( psz_parser - psz_dup == 1 )
651     {
652         /* msg_Warn( p_sout, "drive letter %c: found in source string",
653                           *psz_dup ) ; */
654         psz_parser = "";
655     }
656 #endif
657
658     if( !*psz_parser )
659     {
660         psz_access = psz_way = "";
661         psz_name = psz_dup;
662     }
663     else
664     {
665         *psz_parser++ = '\0';
666
667         /* let's skip '//' */
668         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
669         {
670             psz_parser += 2 ;
671         }
672
673         psz_name = psz_parser ;
674
675         /* Come back to parse the access and mux plug-ins */
676         psz_parser = psz_dup;
677
678         if( !*psz_parser )
679         {
680             /* No access */
681             psz_access = "";
682         }
683         else if( *psz_parser == '/' )
684         {
685             /* No access */
686             psz_access = "";
687             psz_parser++;
688         }
689         else
690         {
691             psz_access = psz_parser;
692
693             while( *psz_parser && *psz_parser != '/' )
694             {
695                 if( *psz_parser == '{' )
696                 {
697                     while( *psz_parser && *psz_parser != '}' )
698                     {
699                         psz_parser++;
700                     }
701                     if( *psz_parser )
702                     {
703                         psz_parser++;
704                     }
705                 }
706                 else
707                 {
708                     psz_parser++;
709                 }
710             }
711
712             if( *psz_parser == '/' )
713             {
714                 *psz_parser++ = '\0';
715             }
716         }
717
718         if( !*psz_parser )
719         {
720             /* No mux */
721             psz_way = "";
722         }
723         else
724         {
725             psz_way = psz_parser;
726         }
727     }
728
729     p_mrl->psz_access = strdup( psz_access );
730     p_mrl->psz_way    = strdup( psz_way );
731     p_mrl->psz_name   = strdup( psz_name );
732
733     free( psz_dup );
734     return( VLC_SUCCESS );
735 }
736
737
738 /* mrl_Clean: clean p_mrl  after a call to mrl_Parse */
739 static void mrl_Clean( mrl_t *p_mrl )
740 {
741     FREENULL( p_mrl->psz_access );
742     FREENULL( p_mrl->psz_way );
743     FREENULL( p_mrl->psz_name );
744 }
745
746
747 /****************************************************************************
748  ****************************************************************************
749  **
750  **
751  **
752  ****************************************************************************
753  ****************************************************************************/
754
755 /* create a complete chain */
756 /* chain format:
757     module{option=*:option=*}[:module{option=*:...}]
758  */
759
760 /*
761  * parse module{options=str, option="str "}:
762  *  return a pointer on the rest
763  *  XXX: psz_chain is modified
764  */
765
766 /*
767  * XXX name and p_cfg are used (-> do NOT free them)
768  */
769 sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_chain )
770 {
771     static const char typename[] = "stream out";
772     sout_stream_t *p_stream;
773
774     if( !psz_chain )
775     {
776         msg_Err( p_sout, "invalid chain" );
777         return NULL;
778     }
779
780     p_stream = vlc_custom_create( p_sout, sizeof( *p_stream ),
781                                   VLC_OBJECT_GENERIC, typename );
782     if( !p_stream )
783         return NULL;
784
785     p_stream->p_sout   = p_sout;
786     p_stream->p_sys    = NULL;
787
788     p_stream->psz_next =
789         config_ChainCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
790
791     msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
792
793     vlc_object_set_name( p_stream, p_stream->psz_name );
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
830     mrl_Parse( &mrl, psz_url );
831
832     /* Check if the URLs goes to #rtp - otherwise we'll use #standard */
833     static const char rtplist[] = "dccp\0sctp\0tcp\0udplite\0";
834     for (const char *a = rtplist; *a; a += strlen (a) + 1)
835         if (strcmp (a, mrl.psz_access) == 0)
836             goto rtp;
837
838     if (strcmp (mrl.psz_access, "rtp") == 0)
839     {
840         char *port;
841         /* For historical reasons, rtp:// means RTP over UDP */
842         strcpy (mrl.psz_access, "udp");
843 rtp:
844         if (mrl.psz_name[0] == '[')
845         {
846             port = strstr (mrl.psz_name, "]:");
847             if (port != NULL)
848                 port++;
849         }
850         else
851             port = strchr (mrl.psz_name, ':');
852         if (port != NULL)
853             *port++ = '\0'; /* erase ':' */
854
855         if (asprintf (&psz_chain,
856                       "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s%s%s\"}",
857                       mrl.psz_way, mrl.psz_access, mrl.psz_name,
858                       port ? "\",port=\"" : "", port ? port : "") == -1)
859             psz_chain = NULL;
860     }
861     else
862     {
863         /* Convert the URL to a basic standard sout chain */
864         if (asprintf (&psz_chain,
865                       "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
866                       mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
867             psz_chain = NULL;
868     }
869
870     /* Duplicate and wrap if sout-display is on */
871     if (psz_chain && (config_GetInt( p_this, "sout-display" ) > 0))
872     {
873         char *tmp;
874         if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", tmp) == -1)
875             tmp = NULL;
876         free (psz_chain);
877         psz_chain = tmp;
878     }
879
880     mrl_Clean( &mrl );
881     return psz_chain;
882 }
883
884 #undef sout_EncoderCreate
885 encoder_t *sout_EncoderCreate( vlc_object_t *p_this )
886 {
887     static const char type[] = "encoder";
888     return vlc_custom_create( p_this, sizeof( encoder_t ), VLC_OBJECT_GENERIC,
889                               type );
890 }