]> git.sesse.net Git - vlc/blob - src/stream_output/stream_output.c
Check malloc return value
[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/vlc.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     sout_instance_t *p_sout;
80
81     /* *** Allocate descriptor *** */
82     p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
83     if( p_sout == NULL )
84     {
85         msg_Err( p_parent, "out of memory" );
86         return NULL;
87     }
88
89     /* *** init descriptor *** */
90     p_sout->psz_sout    = strdup( psz_dest );
91     p_sout->p_meta      = NULL;
92     p_sout->i_out_pace_nocontrol = 0;
93     p_sout->p_sys       = NULL;
94
95     vlc_mutex_init( &p_sout->lock );
96     if( psz_dest && psz_dest[0] == '#' )
97     {
98         p_sout->psz_chain = strdup( &psz_dest[1] );
99     }
100     else
101     {
102         p_sout->psz_chain = sout_stream_url_to_chain( p_sout, psz_dest );
103         msg_Dbg( p_sout, "using sout chain=`%s'", p_sout->psz_chain );
104     }
105     p_sout->p_stream = NULL;
106
107     /* attach it for inherit */
108     vlc_object_attach( p_sout, p_parent );
109
110     /* */
111     var_Create( p_sout, "sout-mux-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
112
113     /* */
114     p_sout->p_stream = sout_StreamNew( p_sout, p_sout->psz_chain );
115     if( p_sout->p_stream == NULL )
116     {
117         msg_Err( p_sout, "stream chain failed for `%s'", p_sout->psz_chain );
118
119         FREENULL( p_sout->psz_sout );
120         FREENULL( p_sout->psz_chain );
121
122         vlc_object_detach( p_sout );
123         vlc_object_release( p_sout );
124         return NULL;
125     }
126
127     return p_sout;
128 }
129
130 /*****************************************************************************
131  * sout_DeleteInstance: delete a previously allocated instance
132  *****************************************************************************/
133 void sout_DeleteInstance( sout_instance_t * p_sout )
134 {
135     /* remove the stream out chain */
136     sout_StreamDelete( p_sout->p_stream );
137
138     /* *** free all string *** */
139     FREENULL( p_sout->psz_sout );
140     FREENULL( p_sout->psz_chain );
141
142     /* delete meta */
143     if( p_sout->p_meta )
144     {
145         vlc_meta_Delete( p_sout->p_meta );
146     }
147
148     vlc_mutex_destroy( &p_sout->lock );
149
150     /* *** free structure *** */
151     vlc_object_release( p_sout );
152 }
153
154 /*****************************************************************************
155  * 
156  *****************************************************************************/
157 void sout_UpdateStatistic( sout_instance_t *p_sout, sout_statistic_t i_type, int i_delta )
158 {
159     input_thread_t *p_input;
160     int i_bytes; /* That's pretty stupid to define it as an integer, it will overflow
161                     really fast ... */
162
163     if( !libvlc_stats (p_sout) )
164         return;
165
166     /* FIXME that's ugly
167      * TODO add a private (ie not VLC_EXPORTed) input_UpdateStatistic for that */
168     p_input = vlc_object_find( p_sout, VLC_OBJECT_INPUT, FIND_PARENT );
169     if( !p_input || p_input->i_state == INIT_S || p_input->i_state == ERROR_S )
170         return;
171
172     switch( i_type )
173     {
174 #define I(c) stats_UpdateInteger( p_input, p_input->p->counters.c, i_delta, NULL )
175     case SOUT_STATISTIC_DECODED_VIDEO:
176         I(p_decoded_video);
177         break;
178     case SOUT_STATISTIC_DECODED_AUDIO:
179         I(p_decoded_audio);
180         break;
181     case SOUT_STATISTIC_DECODED_SUBTITLE:
182         I(p_decoded_sub);
183         break;
184 #if 0
185     case SOUT_STATISTIC_ENCODED_VIDEO:
186     case SOUT_STATISTIC_ENCODED_AUDIO:
187     case SOUT_STATISTIC_ENCODED_SUBTITLE:
188         msg_Warn( p_sout, "Not yet supported statistic type %d", i_type );
189         break;
190 #endif
191
192     case SOUT_STATISTIC_SENT_PACKET:
193         I(p_sout_sent_packets);
194         break;
195 #undef I
196     case SOUT_STATISTIC_SENT_BYTE:
197         if( !stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_bytes, i_delta, &i_bytes ) )
198             stats_UpdateFloat( p_input, p_input->p->counters.p_sout_send_bitrate, i_bytes, NULL );
199         break;
200
201     default:
202         msg_Err( p_sout, "Invalid statistic type %d (internal error)", i_type );
203         break;
204     }
205     vlc_object_release( p_input );
206 }
207 /*****************************************************************************
208  * Packetizer/Input
209  *****************************************************************************/
210 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
211                                         es_format_t *p_fmt )
212 {
213     sout_packetizer_input_t *p_input;
214
215     /* *** create a packetizer input *** */
216     p_input         = malloc( sizeof( sout_packetizer_input_t ) );
217     if( !p_input )  return NULL;
218     p_input->p_sout = p_sout;
219     p_input->p_fmt  = p_fmt;
220
221     msg_Dbg( p_sout, "adding a new sout input (sout_input:%p)", p_input );
222
223     if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
224     {
225         vlc_object_release( p_sout );
226         return p_input;
227     }
228
229     /* *** add it to the stream chain */
230     vlc_mutex_lock( &p_sout->lock );
231     p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
232     vlc_mutex_unlock( &p_sout->lock );
233
234     if( p_input->id == NULL )
235     {
236         free( p_input );
237         return NULL;
238     }
239
240     return( p_input );
241 }
242
243 /*****************************************************************************
244  *
245  *****************************************************************************/
246 int sout_InputDelete( sout_packetizer_input_t *p_input )
247 {
248     sout_instance_t     *p_sout = p_input->p_sout;
249
250     msg_Dbg( p_sout, "removing a sout input (sout_input:%p)", p_input );
251
252     if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
253     {
254         vlc_mutex_lock( &p_sout->lock );
255         p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
256         vlc_mutex_unlock( &p_sout->lock );
257     }
258
259     free( p_input );
260
261     return( VLC_SUCCESS);
262 }
263
264 /*****************************************************************************
265  *
266  *****************************************************************************/
267 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
268                           block_t *p_buffer )
269 {
270     sout_instance_t     *p_sout = p_input->p_sout;
271     int                 i_ret;
272
273     if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
274     {
275         block_Release( p_buffer );
276         return VLC_SUCCESS;
277     }
278     if( p_buffer->i_dts <= 0 )
279     {
280         msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
281         block_Release( p_buffer );
282         return VLC_SUCCESS;
283     }
284
285     vlc_mutex_lock( &p_sout->lock );
286     i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
287                                        p_input->id, p_buffer );
288     vlc_mutex_unlock( &p_sout->lock );
289
290     return i_ret;
291 }
292
293 /*****************************************************************************
294  * sout_AccessOutNew: allocate a new access out
295  *****************************************************************************/
296 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
297                                       const char *psz_access, const char *psz_name )
298 {
299     sout_access_out_t *p_access;
300     char              *psz_next;
301
302     if( !( p_access = vlc_object_create( p_sout,
303                                          sizeof( sout_access_out_t ) ) ) )
304     {
305         msg_Err( p_sout, "out of memory" );
306         return NULL;
307     }
308
309     psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
310                                    psz_access );
311     free( psz_next );
312     p_access->psz_path   = strdup( psz_name ? psz_name : "" );
313     p_access->p_sout     = p_sout;
314     p_access->p_sys = NULL;
315     p_access->pf_seek    = NULL;
316     p_access->pf_read    = NULL;
317     p_access->pf_write   = NULL;
318     p_access->pf_control = NULL;
319     p_access->p_module   = NULL;
320
321     p_access->i_writes = 0;
322     p_access->i_sent_bytes = 0;
323
324     vlc_object_attach( p_access, p_sout );
325
326     p_access->p_module   =
327         module_Need( p_access, "sout access", p_access->psz_access, true );
328
329     if( !p_access->p_module )
330     {
331         free( p_access->psz_access );
332         free( p_access->psz_path );
333         vlc_object_detach( p_access );
334         vlc_object_release( p_access );
335         return( NULL );
336     }
337
338     return p_access;
339 }
340 /*****************************************************************************
341  * sout_AccessDelete: delete an access out
342  *****************************************************************************/
343 void sout_AccessOutDelete( sout_access_out_t *p_access )
344 {
345     vlc_object_detach( p_access );
346     if( p_access->p_module )
347     {
348         module_Unneed( p_access, p_access->p_module );
349     }
350     free( p_access->psz_access );
351
352     config_ChainDestroy( p_access->p_cfg );
353
354     free( p_access->psz_path );
355
356     vlc_object_release( p_access );
357 }
358
359 /*****************************************************************************
360  * sout_AccessSeek:
361  *****************************************************************************/
362 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
363 {
364     return p_access->pf_seek( p_access, i_pos );
365 }
366
367 /*****************************************************************************
368  * sout_AccessRead:
369  *****************************************************************************/
370 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
371 {
372     return( p_access->pf_read ?
373             p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
374 }
375
376 /*****************************************************************************
377  * sout_AccessWrite:
378  *****************************************************************************/
379 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
380 {
381     const unsigned i_packets_gather = 30;
382     p_access->i_writes++;
383     p_access->i_sent_bytes += p_buffer->i_buffer;
384     if( (p_access->i_writes % i_packets_gather) == 0 )
385     {
386         sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_PACKET, i_packets_gather );
387         sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_BYTE, p_access->i_sent_bytes );
388         p_access->i_sent_bytes = 0;
389     }
390     return p_access->pf_write( p_access, p_buffer );
391 }
392
393 /**
394  * sout_AccessOutControl
395  */
396 int sout_AccessOutControl (sout_access_out_t *access, int query, va_list args)
397 {
398     return (access->pf_control) ? access->pf_control (access, query, args)
399                                 : VLC_EGENERIC;
400 }
401
402 /*****************************************************************************
403  * sout_MuxNew: create a new mux
404  *****************************************************************************/
405 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
406                           sout_access_out_t *p_access )
407 {
408     sout_mux_t *p_mux;
409     char       *psz_next;
410
411     p_mux = vlc_object_create( p_sout, sizeof( sout_mux_t ) );
412     if( p_mux == NULL )
413     {
414         msg_Err( p_sout, "out of memory" );
415         return NULL;
416     }
417
418     p_mux->p_sout = p_sout;
419     psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
420     free( psz_next );
421
422     p_mux->p_access     = p_access;
423     p_mux->pf_control   = NULL;
424     p_mux->pf_addstream = NULL;
425     p_mux->pf_delstream = NULL;
426     p_mux->pf_mux       = NULL;
427     p_mux->i_nb_inputs  = 0;
428     p_mux->pp_inputs    = NULL;
429
430     p_mux->p_sys        = NULL;
431     p_mux->p_module     = NULL;
432
433     p_mux->b_add_stream_any_time = false;
434     p_mux->b_waiting_stream = true;
435     p_mux->i_add_stream_start = -1;
436
437     vlc_object_attach( p_mux, p_sout );
438
439     p_mux->p_module =
440         module_Need( p_mux, "sout mux", p_mux->psz_mux, true );
441
442     if( p_mux->p_module == NULL )
443     {
444         FREENULL( p_mux->psz_mux );
445
446         vlc_object_detach( p_mux );
447         vlc_object_release( p_mux );
448         return NULL;
449     }
450
451     /* *** probe mux capacity *** */
452     if( p_mux->pf_control )
453     {
454         int b_answer = false;
455
456         if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
457                              &b_answer ) )
458         {
459             b_answer = false;
460         }
461
462         if( b_answer )
463         {
464             msg_Dbg( p_sout, "muxer support adding stream at any time" );
465             p_mux->b_add_stream_any_time = true;
466             p_mux->b_waiting_stream = false;
467
468             /* If we control the output pace then it's better to wait before
469              * starting muxing (generates better streams/files). */
470             if( !p_sout->i_out_pace_nocontrol )
471             {
472                 b_answer = true;
473             }
474             else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
475                                       &b_answer ) )
476             {
477                 b_answer = false;
478             }
479
480             if( b_answer )
481             {
482                 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
483                          "starting to mux" );
484                 p_mux->b_waiting_stream = true;
485             }
486         }
487     }
488
489     return p_mux;
490 }
491
492 /*****************************************************************************
493  * sout_MuxDelete:
494  *****************************************************************************/
495 void sout_MuxDelete( sout_mux_t *p_mux )
496 {
497     vlc_object_detach( p_mux );
498     if( p_mux->p_module )
499     {
500         module_Unneed( p_mux, p_mux->p_module );
501     }
502     free( p_mux->psz_mux );
503
504     config_ChainDestroy( p_mux->p_cfg );
505
506     vlc_object_release( p_mux );
507 }
508
509 /*****************************************************************************
510  * sout_MuxAddStream:
511  *****************************************************************************/
512 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
513 {
514     sout_input_t *p_input;
515
516     if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
517     {
518         msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
519                         "to this format). You can try increasing sout-mux-caching value" );
520         return NULL;
521     }
522
523     msg_Dbg( p_mux, "adding a new input" );
524
525     /* create a new sout input */
526     p_input = malloc( sizeof( sout_input_t ) );
527     if( !p_input )
528     {
529         msg_Err( p_mux, "out of memory" );
530         return NULL;
531     }
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     sout_stream_t *p_stream;
772
773     if( !psz_chain )
774     {
775         msg_Err( p_sout, "invalid chain" );
776         return NULL;
777     }
778
779     p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
780
781     if( !p_stream )
782     {
783         msg_Err( p_sout, "out of memory" );
784         return NULL;
785     }
786
787     p_stream->p_sout   = p_sout;
788     p_stream->p_sys    = NULL;
789
790     p_stream->psz_next =
791         config_ChainCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
792
793     msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
794
795     vlc_object_attach( p_stream, p_sout );
796
797     p_stream->p_module =
798         module_Need( p_stream, "sout stream", p_stream->psz_name, true );
799
800     if( !p_stream->p_module )
801     {
802         sout_StreamDelete( p_stream );
803         return NULL;
804     }
805
806     return p_stream;
807 }
808
809 void sout_StreamDelete( sout_stream_t *p_stream )
810 {
811     msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
812
813     vlc_object_detach( p_stream );
814     if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
815
816     FREENULL( p_stream->psz_name );
817     FREENULL( p_stream->psz_next );
818
819     config_ChainDestroy( p_stream->p_cfg );
820
821     msg_Dbg( p_stream, "destroying chain done" );
822     vlc_object_release( p_stream );
823 }
824
825 static char *_sout_stream_url_to_chain( vlc_object_t *p_this,
826                                         const char *psz_url )
827 {
828     mrl_t       mrl;
829     char        *psz_chain;
830     const char  *fmt = "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}";
831     static const char rtpfmt[] = "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s\"}";
832
833     mrl_Parse( &mrl, psz_url );
834
835     /* Check if the URLs goes #rtp - otherwise we'll use #standard */
836     if (strcmp (mrl.psz_access, "rtp") == 0)
837     {
838         /* For historical reasons, rtp:// means RTP over UDP */
839         strcpy (mrl.psz_access, "udp");
840         fmt = rtpfmt;
841     }
842     else
843     {
844         static const char list[] = "dccp\0sctp\0tcp\0udplite\0";
845         for (const char *a = list; *a; a += strlen (a) + 1)
846              if (strcmp (a, mrl.psz_access) == 0)
847              {
848                  fmt = rtpfmt;
849                  break;
850              }
851     }
852
853     /* Convert the URL to a basic sout chain */
854     if (asprintf (&psz_chain, fmt,
855                   mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
856         psz_chain = NULL;
857
858     /* Duplicate and wrap if sout-display is on */
859     if (psz_chain && (config_GetInt( p_this, "sout-display" ) > 0))
860     {
861         char *tmp;
862         if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", tmp) == -1)
863             tmp = NULL;
864         free (psz_chain);
865         psz_chain = tmp;
866     }
867
868     mrl_Clean( &mrl );
869     return psz_chain;
870 }