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