]> git.sesse.net Git - vlc/blob - src/stream_output/stream_output.c
Remove whitespace
[vlc] / src / stream_output / stream_output.c
1 /*****************************************************************************
2  * stream_output.c : stream output module
3  *****************************************************************************
4  * Copyright (C) 2002-2004 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 #include <stdlib.h>                                                /* free() */
30 #include <stdio.h>                                              /* sprintf() */
31 #include <string.h>                                            /* strerror() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/sout.h>
35 #include <vlc/input.h>
36
37 #include "vlc_meta.h"
38
39 #undef DEBUG_BUFFER
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static void sout_CfgDestroy( sout_cfg_t * );
44
45 #define sout_stream_url_to_chain( p, s ) \
46     _sout_stream_url_to_chain( VLC_OBJECT(p), s )
47 static char *_sout_stream_url_to_chain( vlc_object_t *, char * );
48
49 /*
50  * Generic MRL parser
51  *
52  */
53
54 typedef struct
55 {
56     char *psz_access;
57     char *psz_way;
58     char *psz_name;
59 } mrl_t;
60
61 /* mrl_Parse: parse psz_mrl and fill p_mrl */
62 static int  mrl_Parse( mrl_t *p_mrl, char *psz_mrl );
63 /* mrl_Clean: clean p_mrl  after a call to mrl_Parse */
64 static void mrl_Clean( mrl_t *p_mrl );
65
66 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
67
68 /*****************************************************************************
69  * sout_NewInstance: creates a new stream output instance
70  *****************************************************************************/
71 sout_instance_t *__sout_NewInstance( vlc_object_t *p_parent, char * psz_dest )
72 {
73     sout_instance_t *p_sout;
74     vlc_value_t keep;
75     counter_t *p_counter;
76
77     if( var_Get( p_parent, "sout-keep", &keep ) < 0 )
78     {
79         msg_Warn( p_parent, "cannot get sout-keep value" );
80         keep.b_bool = VLC_FALSE;
81     }
82     if( keep.b_bool )
83     {
84         if( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT,
85                                         FIND_ANYWHERE ) ) != NULL )
86         {
87             if( !strcmp( p_sout->psz_sout, psz_dest ) )
88             {
89                 msg_Dbg( p_parent, "sout keep: reusing sout" );
90                 msg_Dbg( p_parent, "sout keep: you probably want to use "
91                           "gather stream_out" );
92                 vlc_object_detach( p_sout );
93                 vlc_object_attach( p_sout, p_parent );
94                 vlc_object_release( p_sout );
95                 return p_sout;
96             }
97             else
98             {
99                 msg_Dbg( p_parent, "sout keep: destroying unusable sout" );
100                 vlc_object_release( p_sout );
101                 sout_DeleteInstance( p_sout );
102             }
103         }
104     }
105     else if( !keep.b_bool )
106     {
107         while( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT,
108                                            FIND_PARENT ) ) != NULL )
109         {
110             msg_Dbg( p_parent, "sout keep: destroying old sout" );
111             vlc_object_release( p_sout );
112             sout_DeleteInstance( p_sout );
113         }
114     }
115
116     /* *** Allocate descriptor *** */
117     p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
118     if( p_sout == NULL )
119     {
120         msg_Err( p_parent, "out of memory" );
121         return NULL;
122     }
123
124     /* *** init descriptor *** */
125     p_sout->psz_sout    = strdup( psz_dest );
126     p_sout->p_meta      = NULL;
127     p_sout->i_out_pace_nocontrol = 0;
128     p_sout->p_sys       = NULL;
129
130     vlc_mutex_init( p_sout, &p_sout->lock );
131     if( psz_dest && psz_dest[0] == '#' )
132     {
133         p_sout->psz_chain = strdup( &psz_dest[1] );
134     }
135     else
136     {
137         p_sout->psz_chain = sout_stream_url_to_chain( p_sout, psz_dest );
138         msg_Dbg( p_sout, "using sout chain=`%s'", p_sout->psz_chain );
139     }
140     p_sout->p_stream = NULL;
141
142     /* attach it for inherit */
143     vlc_object_attach( p_sout, p_parent );
144
145     /* Create statistics */
146     stats_Create( p_parent, "sout_sent_packets", STATS_SOUT_SENT_PACKETS,
147                   VLC_VAR_INTEGER, STATS_COUNTER );
148     stats_Create( p_parent, "sout_sent_bytes", STATS_SOUT_SENT_BYTES,
149                   VLC_VAR_INTEGER, STATS_COUNTER );
150     stats_Create( p_parent, "sout_send_bitrate", STATS_SOUT_SEND_BITRATE,
151                   VLC_VAR_FLOAT, STATS_DERIVATIVE );
152     p_counter = stats_CounterGet( p_parent, p_parent->i_object_id,
153                                   STATS_SOUT_SEND_BITRATE );
154     if( p_counter) p_counter->update_interval = 1000000;
155
156     p_sout->p_stream = sout_StreamNew( p_sout, p_sout->psz_chain );
157
158     if( p_sout->p_stream == NULL )
159     {
160         msg_Err( p_sout, "stream chain failed for `%s'", p_sout->psz_chain );
161
162         FREE( p_sout->psz_sout );
163         FREE( p_sout->psz_chain );
164
165         vlc_object_detach( p_sout );
166         vlc_object_destroy( p_sout );
167         return NULL;
168     }
169
170     return p_sout;
171 }
172
173 /*****************************************************************************
174  * sout_DeleteInstance: delete a previously allocated instance
175  *****************************************************************************/
176 void sout_DeleteInstance( sout_instance_t * p_sout )
177 {
178     /* Unlink object */
179     vlc_object_detach( p_sout );
180
181     /* remove the stream out chain */
182     sout_StreamDelete( p_sout->p_stream );
183
184     /* *** free all string *** */
185     FREE( p_sout->psz_sout );
186     FREE( p_sout->psz_chain );
187
188     /* delete meta */
189     if( p_sout->p_meta )
190     {
191         vlc_meta_Delete( p_sout->p_meta );
192     }
193
194     vlc_mutex_destroy( &p_sout->lock );
195
196     /* *** free structure *** */
197     vlc_object_destroy( p_sout );
198 }
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     msg_Dbg( p_sout, "adding a new input" );
209
210     /* *** create a packetizer input *** */
211     p_input         = malloc( sizeof( sout_packetizer_input_t ) );
212     p_input->p_sout = p_sout;
213     p_input->p_fmt  = p_fmt;
214
215     if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
216     {
217         vlc_object_release( p_sout );
218         return p_input;
219     }
220
221     /* *** add it to the stream chain */
222     vlc_mutex_lock( &p_sout->lock );
223     p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
224     vlc_mutex_unlock( &p_sout->lock );
225
226     if( p_input->id == NULL )
227     {
228         free( p_input );
229         return NULL;
230     }
231
232     return( p_input );
233 }
234
235 /*****************************************************************************
236  *
237  *****************************************************************************/
238 int sout_InputDelete( sout_packetizer_input_t *p_input )
239 {
240     sout_instance_t     *p_sout = p_input->p_sout;
241
242     msg_Dbg( p_sout, "removing an input" );
243
244     if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
245     {
246         vlc_mutex_lock( &p_sout->lock );
247         p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
248         vlc_mutex_unlock( &p_sout->lock );
249     }
250
251     free( p_input );
252
253     return( VLC_SUCCESS);
254 }
255
256 /*****************************************************************************
257  *
258  *****************************************************************************/
259 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
260                           block_t *p_buffer )
261 {
262     sout_instance_t     *p_sout = p_input->p_sout;
263     int                 i_ret;
264
265     if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
266     {
267         block_Release( p_buffer );
268         return VLC_SUCCESS;
269     }
270     if( p_buffer->i_dts <= 0 )
271     {
272         msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
273         block_Release( p_buffer );
274         return VLC_SUCCESS;
275     }
276
277     vlc_mutex_lock( &p_sout->lock );
278     i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
279                                        p_input->id, p_buffer );
280     vlc_mutex_unlock( &p_sout->lock );
281
282     return i_ret;
283 }
284
285 /*****************************************************************************
286  * sout_AccessOutNew: allocate a new access out
287  *****************************************************************************/
288 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
289                                       char *psz_access, char *psz_name )
290 {
291     sout_access_out_t *p_access;
292     char              *psz_next;
293
294     if( !( p_access = vlc_object_create( p_sout,
295                                          sizeof( sout_access_out_t ) ) ) )
296     {
297         msg_Err( p_sout, "out of memory" );
298         return NULL;
299     }
300
301     psz_next = sout_CfgCreate( &p_access->psz_access, &p_access->p_cfg,
302                                 psz_access );
303     if( psz_next )
304     {
305         free( psz_next );
306     }
307     p_access->psz_name   = strdup( psz_name ? psz_name : "" );
308     p_access->p_sout     = p_sout;
309     p_access->p_sys = NULL;
310     p_access->pf_seek    = NULL;
311     p_access->pf_read    = NULL;
312     p_access->pf_write   = NULL;
313     p_access->p_module   = NULL;
314
315     p_access->i_writes = 0;
316     p_access->i_sent_bytes = 0;
317
318     vlc_object_attach( p_access, p_sout );
319
320     p_access->p_module   =
321         module_Need( p_access, "sout access", p_access->psz_access, VLC_TRUE );
322
323     if( !p_access->p_module )
324     {
325         free( p_access->psz_access );
326         free( p_access->psz_name );
327         vlc_object_detach( p_access );
328         vlc_object_destroy( p_access );
329         return( NULL );
330     }
331
332     return p_access;
333 }
334 /*****************************************************************************
335  * sout_AccessDelete: delete an access out
336  *****************************************************************************/
337 void sout_AccessOutDelete( sout_access_out_t *p_access )
338 {
339     vlc_object_detach( p_access );
340     if( p_access->p_module )
341     {
342         module_Unneed( p_access, p_access->p_module );
343     }
344     free( p_access->psz_access );
345
346     sout_CfgDestroy( p_access->p_cfg );
347
348     free( p_access->psz_name );
349
350     vlc_object_destroy( p_access );
351 }
352
353 /*****************************************************************************
354  * sout_AccessSeek:
355  *****************************************************************************/
356 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
357 {
358     return p_access->pf_seek( p_access, i_pos );
359 }
360
361 /*****************************************************************************
362  * sout_AccessRead:
363  *****************************************************************************/
364 int sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
365 {
366     return( p_access->pf_read ?
367             p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
368 }
369
370 /*****************************************************************************
371  * sout_AccessWrite:
372  *****************************************************************************/
373 int sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
374 {
375     int i_total;
376     p_access->i_writes++;
377     p_access->i_sent_bytes += p_buffer->i_buffer;
378     if( p_access->p_libvlc->b_stats && p_access->i_writes % 30 == 0 )
379     {
380         /* Access_out -> sout_instance -> input_thread_t */
381         input_thread_t *p_input =
382             (input_thread_t *)vlc_object_find( p_access, VLC_OBJECT_INPUT,
383                                                FIND_PARENT );
384         if( p_input )
385         {
386             stats_UpdateInteger( p_input, STATS_SOUT_SENT_PACKETS, 30, NULL );
387             stats_UpdateInteger( p_input, STATS_SOUT_SENT_BYTES,
388                                  p_access->i_sent_bytes, &i_total );
389             stats_UpdateFloat( p_input, STATS_SOUT_SEND_BITRATE, (float)i_total,
390                                NULL );
391             p_access->i_sent_bytes = 0;
392             vlc_object_release( p_input );
393         }
394     }
395     return p_access->pf_write( p_access, p_buffer );
396 }
397
398 /*****************************************************************************
399  * sout_MuxNew: create a new mux
400  *****************************************************************************/
401 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
402                           sout_access_out_t *p_access )
403 {
404     sout_mux_t *p_mux;
405     char       *psz_next;
406
407     p_mux = vlc_object_create( p_sout, sizeof( sout_mux_t ) );
408     if( p_mux == NULL )
409     {
410         msg_Err( p_sout, "out of memory" );
411         return NULL;
412     }
413
414     p_mux->p_sout = p_sout;
415     psz_next = sout_CfgCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
416     if( psz_next ) free( psz_next );
417
418     p_mux->p_access     = p_access;
419     p_mux->pf_control   = NULL;
420     p_mux->pf_addstream = NULL;
421     p_mux->pf_delstream = NULL;
422     p_mux->pf_mux       = NULL;
423     p_mux->i_nb_inputs  = 0;
424     p_mux->pp_inputs    = NULL;
425
426     p_mux->p_sys        = NULL;
427     p_mux->p_module     = NULL;
428
429     p_mux->b_add_stream_any_time = VLC_FALSE;
430     p_mux->b_waiting_stream = VLC_TRUE;
431     p_mux->i_add_stream_start = -1;
432
433     vlc_object_attach( p_mux, p_sout );
434
435     p_mux->p_module =
436         module_Need( p_mux, "sout mux", p_mux->psz_mux, VLC_TRUE );
437
438     if( p_mux->p_module == NULL )
439     {
440         FREE( p_mux->psz_mux );
441
442         vlc_object_detach( p_mux );
443         vlc_object_destroy( p_mux );
444         return NULL;
445     }
446
447     /* *** probe mux capacity *** */
448     if( p_mux->pf_control )
449     {
450         int b_answer = VLC_FALSE;
451
452         if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
453                              &b_answer ) )
454         {
455             b_answer = VLC_FALSE;
456         }
457
458         if( b_answer )
459         {
460             msg_Dbg( p_sout, "muxer support adding stream at any time" );
461             p_mux->b_add_stream_any_time = VLC_TRUE;
462             p_mux->b_waiting_stream = VLC_FALSE;
463
464             /* If we control the output pace then it's better to wait before
465              * starting muxing (generates better streams/files). */
466             if( !p_sout->i_out_pace_nocontrol )
467             {
468                 b_answer = VLC_TRUE;
469             }
470             else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
471                                       &b_answer ) )
472             {
473                 b_answer = VLC_FALSE;
474             }
475
476             if( b_answer )
477             {
478                 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
479                          "starting to mux" );
480                 p_mux->b_waiting_stream = VLC_TRUE;
481             }
482         }
483     }
484
485     return p_mux;
486 }
487
488 /*****************************************************************************
489  * sout_MuxDelete:
490  *****************************************************************************/
491 void sout_MuxDelete( sout_mux_t *p_mux )
492 {
493     vlc_object_detach( p_mux );
494     if( p_mux->p_module )
495     {
496         module_Unneed( p_mux, p_mux->p_module );
497     }
498     free( p_mux->psz_mux );
499
500     sout_CfgDestroy( p_mux->p_cfg );
501
502     vlc_object_destroy( p_mux );
503 }
504
505 /*****************************************************************************
506  * sout_MuxAddStream:
507  *****************************************************************************/
508 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
509 {
510     sout_input_t *p_input;
511
512     if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
513     {
514         msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
515                         "to this format)" );
516         return NULL;
517     }
518
519     msg_Dbg( p_mux, "adding a new input" );
520
521     /* create a new sout input */
522     p_input = malloc( sizeof( sout_input_t ) );
523     p_input->p_sout = p_mux->p_sout;
524     p_input->p_fmt  = p_fmt;
525     p_input->p_fifo = block_FifoNew( p_mux->p_sout );
526     p_input->p_sys  = NULL;
527
528     TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
529     if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
530     {
531             msg_Err( p_mux, "cannot add this stream" );
532             TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
533             block_FifoRelease( p_input->p_fifo );
534             free( p_input );
535             return NULL;
536     }
537
538     return p_input;
539 }
540
541 /*****************************************************************************
542  * sout_MuxDeleteStream:
543  *****************************************************************************/
544 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
545 {
546     int i_index;
547
548     if( p_mux->b_waiting_stream && p_input->p_fifo->i_depth > 0 )
549     {
550         /* We stop waiting, and call the muxer for taking care of the data
551          * before we remove this es */
552         p_mux->b_waiting_stream = VLC_FALSE;
553         p_mux->pf_mux( p_mux );
554     }
555
556     TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
557     if( i_index >= 0 )
558     {
559         if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
560         {
561             msg_Err( p_mux, "cannot delete this stream from mux" );
562         }
563
564         /* remove the entry */
565         TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
566
567         if( p_mux->i_nb_inputs == 0 )
568         {
569             msg_Warn( p_mux, "no more input streams for this mux" );
570         }
571
572         block_FifoRelease( p_input->p_fifo );
573         free( p_input );
574     }
575 }
576
577 /*****************************************************************************
578  * sout_MuxSendBuffer:
579  *****************************************************************************/
580 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
581                          block_t *p_buffer )
582 {
583     block_FifoPut( p_input->p_fifo, p_buffer );
584
585     if( p_mux->p_sout->i_out_pace_nocontrol )
586     {
587         mtime_t current_date = mdate();
588         if ( current_date > p_buffer->i_dts )
589             msg_Warn( p_mux, "late buffer for mux input ("I64Fd")",
590                       current_date - p_buffer->i_dts );
591     }
592
593     if( p_mux->b_waiting_stream )
594     {
595         if( p_mux->i_add_stream_start < 0 )
596         {
597             p_mux->i_add_stream_start = p_buffer->i_dts;
598         }
599
600         if( p_mux->i_add_stream_start >= 0 &&
601             p_mux->i_add_stream_start + I64C(1500000) < p_buffer->i_dts )
602         {
603             /* Wait until we have more than 1.5 seconds worth of data
604              * before start muxing */
605             p_mux->b_waiting_stream = VLC_FALSE;
606         }
607         else
608         {
609             return;
610         }
611     }
612     p_mux->pf_mux( p_mux );
613 }
614
615 /*****************************************************************************
616  *
617  *****************************************************************************/
618 static int mrl_Parse( mrl_t *p_mrl, char *psz_mrl )
619 {
620     char * psz_dup = strdup( psz_mrl );
621     char * psz_parser = psz_dup;
622     char * psz_access = "";
623     char * psz_way = "";
624     char * psz_name = "";
625
626     /* *** first parse psz_dest */
627     while( *psz_parser && *psz_parser != ':' )
628     {
629         if( *psz_parser == '{' )
630         {
631             while( *psz_parser && *psz_parser != '}' )
632             {
633                 psz_parser++;
634             }
635             if( *psz_parser )
636             {
637                 psz_parser++;
638             }
639         }
640         else
641         {
642             psz_parser++;
643         }
644     }
645 #if defined( WIN32 ) || defined( UNDER_CE )
646     if( psz_parser - psz_dup == 1 )
647     {
648         /* msg_Warn( p_sout, "drive letter %c: found in source string",
649                           *psz_dup ) ; */
650         psz_parser = "";
651     }
652 #endif
653
654     if( !*psz_parser )
655     {
656         psz_access = psz_way = "";
657         psz_name = psz_dup;
658     }
659     else
660     {
661         *psz_parser++ = '\0';
662
663         /* let's skip '//' */
664         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
665         {
666             psz_parser += 2 ;
667         }
668
669         psz_name = psz_parser ;
670
671         /* Come back to parse the access and mux plug-ins */
672         psz_parser = psz_dup;
673
674         if( !*psz_parser )
675         {
676             /* No access */
677             psz_access = "";
678         }
679         else if( *psz_parser == '/' )
680         {
681             /* No access */
682             psz_access = "";
683             psz_parser++;
684         }
685         else
686         {
687             psz_access = psz_parser;
688
689             while( *psz_parser && *psz_parser != '/' )
690             {
691                 if( *psz_parser == '{' )
692                 {
693                     while( *psz_parser && *psz_parser != '}' )
694                     {
695                         psz_parser++;
696                     }
697                     if( *psz_parser )
698                     {
699                         psz_parser++;
700                     }
701                 }
702                 else
703                 {
704                     psz_parser++;
705                 }
706             }
707
708             if( *psz_parser == '/' )
709             {
710                 *psz_parser++ = '\0';
711             }
712         }
713
714         if( !*psz_parser )
715         {
716             /* No mux */
717             psz_way = "";
718         }
719         else
720         {
721             psz_way = psz_parser;
722         }
723     }
724
725     p_mrl->psz_access = strdup( psz_access );
726     p_mrl->psz_way    = strdup( psz_way );
727     p_mrl->psz_name   = strdup( psz_name );
728
729     free( psz_dup );
730     return( VLC_SUCCESS );
731 }
732
733
734 /* mrl_Clean: clean p_mrl  after a call to mrl_Parse */
735 static void mrl_Clean( mrl_t *p_mrl )
736 {
737     FREE( p_mrl->psz_access );
738     FREE( p_mrl->psz_way );
739     FREE( p_mrl->psz_name );
740 }
741
742
743 /****************************************************************************
744  ****************************************************************************
745  **
746  **
747  **
748  ****************************************************************************
749  ****************************************************************************/
750
751 /* create a complete chain */
752 /* chain format:
753     module{option=*:option=*}[:module{option=*:...}]
754  */
755
756 /*
757  * parse module{options=str, option="str "}:
758  *  return a pointer on the rest
759  *  XXX: psz_chain is modified
760  */
761 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
762 #define SKIPTRAILINGSPACE( p, e ) \
763     { while( e > p && ( *(e-1) == ' ' || *(e-1) == '\t' ) ) e--; }
764
765 /* go accross " " and { } */
766 static char *_get_chain_end( char *str )
767 {
768     char c, *p = str;
769
770     SKIPSPACE( p );
771
772     for( ;; )
773     {
774         if( !*p || *p == ',' || *p == '}' ) return p;
775
776         if( *p != '{' && *p != '"' && *p != '\'' )
777         {
778             p++;
779             continue;
780         }
781
782         if( *p == '{' ) c = '}';
783         else c = *p;
784         p++;
785
786         for( ;; )
787         {
788             if( !*p ) return p;
789
790             if( *p == c ) return ++p;
791             else if( *p == '{' && c == '}' ) p = _get_chain_end( p );
792             else p++;
793         }
794     }
795 }
796
797 char *sout_CfgCreate( char **ppsz_name, sout_cfg_t **pp_cfg, char *psz_chain )
798 {
799     sout_cfg_t *p_cfg = NULL;
800     char       *p = psz_chain;
801
802     *ppsz_name = NULL;
803     *pp_cfg    = NULL;
804
805     if( !p ) return NULL;
806
807     SKIPSPACE( p );
808
809     while( *p && *p != '{' && *p != ':' && *p != ' ' && *p != '\t' ) p++;
810
811     if( p == psz_chain ) return NULL;
812
813     *ppsz_name = strndup( psz_chain, p - psz_chain );
814
815     SKIPSPACE( p );
816
817     if( *p == '{' )
818     {
819         char *psz_name;
820
821         p++;
822
823         for( ;; )
824         {
825             sout_cfg_t cfg;
826
827             SKIPSPACE( p );
828
829             psz_name = p;
830
831             while( *p && *p != '=' && *p != ',' && *p != '{' && *p != '}' &&
832                    *p != ' ' && *p != '\t' ) p++;
833
834             /* fprintf( stderr, "name=%s - rest=%s\n", psz_name, p ); */
835             if( p == psz_name )
836             {
837                 fprintf( stderr, "invalid options (empty)" );
838                 break;
839             }
840
841             cfg.psz_name = strndup( psz_name, p - psz_name );
842
843             SKIPSPACE( p );
844
845             if( *p == '=' || *p == '{' )
846             {
847                 char *end;
848                 vlc_bool_t b_keep_brackets = (*p == '{');
849
850                 if( *p == '=' ) p++;
851
852                 end = _get_chain_end( p );
853                 if( end <= p )
854                 {
855                     cfg.psz_value = NULL;
856                 }
857                 else
858                 {
859                     /* Skip heading and trailing spaces.
860                      * This ain't necessary but will avoid simple
861                      * user mistakes. */
862                     SKIPSPACE( p );
863                 }
864
865                 if( end <= p )
866                 {
867                     cfg.psz_value = NULL;
868                 }
869                 else
870                 {
871                     if( *p == '\'' || *p == '"' ||
872                         ( !b_keep_brackets && *p == '{' ) )
873                     {
874                         p++;
875
876                         if( *(end-1) != '\'' && *(end-1) == '"' )
877                             SKIPTRAILINGSPACE( p, end );
878
879                         if( end - 1 <= p ) cfg.psz_value = NULL;
880                         else cfg.psz_value = strndup( p, end -1 - p );
881                     }
882                     else
883                     {
884                         SKIPTRAILINGSPACE( p, end );
885                         if( end <= p ) cfg.psz_value = NULL;
886                         else cfg.psz_value = strndup( p, end - p );
887                     }
888                 }
889
890                 p = end;
891                 SKIPSPACE( p );
892             }
893             else
894             {
895                 cfg.psz_value = NULL;
896             }
897
898             cfg.p_next = NULL;
899             if( p_cfg )
900             {
901                 p_cfg->p_next = malloc( sizeof( sout_cfg_t ) );
902                 memcpy( p_cfg->p_next, &cfg, sizeof( sout_cfg_t ) );
903
904                 p_cfg = p_cfg->p_next;
905             }
906             else
907             {
908                 p_cfg = malloc( sizeof( sout_cfg_t ) );
909                 memcpy( p_cfg, &cfg, sizeof( sout_cfg_t ) );
910
911                 *pp_cfg = p_cfg;
912             }
913
914             if( *p == ',' ) p++;
915
916             if( *p == '}' )
917             {
918                 p++;
919                 break;
920             }
921         }
922     }
923
924     if( *p == ':' ) return( strdup( p + 1 ) );
925
926     return NULL;
927 }
928
929 static void sout_CfgDestroy( sout_cfg_t *p_cfg )
930 {
931     while( p_cfg != NULL )
932     {
933         sout_cfg_t *p_next;
934
935         p_next = p_cfg->p_next;
936
937         FREE( p_cfg->psz_name );
938         FREE( p_cfg->psz_value );
939         free( p_cfg );
940
941         p_cfg = p_next;
942     }
943 }
944
945 void __sout_CfgParse( vlc_object_t *p_this, char *psz_prefix,
946                       const char **ppsz_options, sout_cfg_t *cfg )
947 {
948     char *psz_name;
949     int  i_type;
950     int  i;
951
952     /* First, var_Create all variables */
953     for( i = 0; ppsz_options[i] != NULL; i++ )
954     {
955         asprintf( &psz_name, "%s%s", psz_prefix,
956                   *ppsz_options[i] == '*' ? &ppsz_options[i][1] : ppsz_options[i] );
957
958         i_type = config_GetType( p_this, psz_name );
959
960         var_Create( p_this, psz_name, i_type | VLC_VAR_DOINHERIT );
961         free( psz_name );
962     }
963
964     /* Now parse options and set value */
965     if( psz_prefix == NULL ) psz_prefix = "";
966
967     while( cfg )
968     {
969         vlc_value_t val;
970         vlc_bool_t b_yes = VLC_TRUE;
971         vlc_bool_t b_once = VLC_FALSE;
972         module_config_t *p_conf;
973
974         if( cfg->psz_name == NULL || *cfg->psz_name == '\0' )
975         {
976             cfg = cfg->p_next;
977             continue;
978         }
979         for( i = 0; ppsz_options[i] != NULL; i++ )
980         {
981             if( !strcmp( ppsz_options[i], cfg->psz_name ) )
982             {
983                 break;
984             }
985             if( ( !strncmp( cfg->psz_name, "no-", 3 ) &&
986                   !strcmp( ppsz_options[i], cfg->psz_name + 3 ) ) ||
987                 ( !strncmp( cfg->psz_name, "no", 2 ) &&
988                   !strcmp( ppsz_options[i], cfg->psz_name + 2 ) ) )
989             {
990                 b_yes = VLC_FALSE;
991                 break;
992             }
993
994             if( *ppsz_options[i] == '*' &&
995                 !strcmp( &ppsz_options[i][1], cfg->psz_name ) )
996             {
997                 b_once = VLC_TRUE;
998                 break;
999             }
1000
1001         }
1002         if( ppsz_options[i] == NULL )
1003         {
1004             msg_Warn( p_this, "option %s is unknown", cfg->psz_name );
1005             cfg = cfg->p_next;
1006             continue;
1007         }
1008
1009         /* create name */
1010         asprintf( &psz_name, "%s%s", psz_prefix, b_once ? &ppsz_options[i][1] : ppsz_options[i] );
1011
1012         /* Check if the option is deprecated */
1013         p_conf = config_FindConfig( p_this, psz_name );
1014
1015         /* This is basically cut and paste from src/misc/configuration.c
1016          * with slight changes */
1017         if( p_conf && p_conf->psz_current )
1018         {
1019             if( !strcmp( p_conf->psz_current, "SUPPRESSED" ) )
1020             {
1021                 msg_Err( p_this, "Option %s is no longer used.",
1022                          p_conf->psz_name );
1023                 goto next;
1024             }
1025             else if( p_conf->b_strict )
1026             {
1027                 msg_Err( p_this, "Option %s is deprecated. Use %s instead.",
1028                          p_conf->psz_name, p_conf->psz_current );
1029                 /* TODO: this should return an error and end option parsing
1030                  * ... but doing this would change the VLC API and all the
1031                  * modules so i'll do it later */
1032                 goto next;
1033             }
1034             else
1035             {
1036                 msg_Warn( p_this, "Option %s is deprecated. You should use "
1037                         "%s instead.", p_conf->psz_name, p_conf->psz_current );
1038                 free( psz_name );
1039                 psz_name = strdup( p_conf->psz_current );
1040             }
1041         }
1042         /* </Check if the option is deprecated> */
1043
1044         /* get the type of the variable */
1045         i_type = config_GetType( p_this, psz_name );
1046         if( !i_type )
1047         {
1048             msg_Warn( p_this, "unknown option %s (value=%s)",
1049                       cfg->psz_name, cfg->psz_value );
1050             goto next;
1051         }
1052         if( i_type != VLC_VAR_BOOL && cfg->psz_value == NULL )
1053         {
1054             msg_Warn( p_this, "missing value for option %s", cfg->psz_name );
1055             goto next;
1056         }
1057         if( i_type != VLC_VAR_STRING && b_once )
1058         {
1059             msg_Warn( p_this, "*option_name need to be a string option" );
1060             goto next;
1061         }
1062
1063         switch( i_type )
1064         {
1065             case VLC_VAR_BOOL:
1066                 val.b_bool = b_yes;
1067                 break;
1068             case VLC_VAR_INTEGER:
1069                 val.i_int = strtol( cfg->psz_value ? cfg->psz_value : "0",
1070                                     NULL, 0 );
1071                 break;
1072             case VLC_VAR_FLOAT:
1073                 val.f_float = atof( cfg->psz_value ? cfg->psz_value : "0" );
1074                 break;
1075             case VLC_VAR_STRING:
1076             case VLC_VAR_MODULE:
1077                 val.psz_string = cfg->psz_value;
1078                 break;
1079             default:
1080                 msg_Warn( p_this, "unhandled config var type" );
1081                 memset( &val, 0, sizeof( vlc_value_t ) );
1082                 break;
1083         }
1084         if( b_once )
1085         {
1086             vlc_value_t val2;
1087
1088             var_Get( p_this, psz_name, &val2 );
1089             if( *val2.psz_string )
1090             {
1091                 free( val2.psz_string );
1092                 msg_Dbg( p_this, "ignoring option %s (not first occurrence)", psz_name );
1093                 goto next;
1094             }
1095             free( val2.psz_string );
1096         }
1097         var_Set( p_this, psz_name, val );
1098         msg_Dbg( p_this, "set sout option: %s to %s", psz_name, cfg->psz_value );
1099
1100     next:
1101         free( psz_name );
1102         cfg = cfg->p_next;
1103     }
1104 }
1105
1106
1107 /*
1108  * XXX name and p_cfg are used (-> do NOT free them)
1109  */
1110 sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_chain )
1111 {
1112     sout_stream_t *p_stream;
1113
1114     if( !psz_chain )
1115     {
1116         msg_Err( p_sout, "invalid chain" );
1117         return NULL;
1118     }
1119
1120     p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
1121
1122     if( !p_stream )
1123     {
1124         msg_Err( p_sout, "out of memory" );
1125         return NULL;
1126     }
1127
1128     p_stream->p_sout   = p_sout;
1129     p_stream->p_sys    = NULL;
1130
1131     p_stream->psz_next =
1132         sout_CfgCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
1133
1134     msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
1135
1136     vlc_object_attach( p_stream, p_sout );
1137
1138     p_stream->p_module =
1139         module_Need( p_stream, "sout stream", p_stream->psz_name, VLC_TRUE );
1140
1141     if( !p_stream->p_module )
1142     {
1143         sout_StreamDelete( p_stream );
1144         return NULL;
1145     }
1146
1147     return p_stream;
1148 }
1149
1150 void sout_StreamDelete( sout_stream_t *p_stream )
1151 {
1152     msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
1153
1154     vlc_object_detach( p_stream );
1155     if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
1156
1157     FREE( p_stream->psz_name );
1158     FREE( p_stream->psz_next );
1159
1160     sout_CfgDestroy( p_stream->p_cfg );
1161
1162     msg_Dbg( p_stream, "destroying chain done" );
1163     vlc_object_destroy( p_stream );
1164 }
1165
1166 static char *_sout_stream_url_to_chain( vlc_object_t *p_this, char *psz_url )
1167 {
1168     mrl_t       mrl;
1169     char        *psz_chain, *p;
1170
1171     mrl_Parse( &mrl, psz_url );
1172     p = psz_chain = malloc( 500 + strlen( mrl.psz_way ) +
1173                                   strlen( mrl.psz_access ) +
1174                                   strlen( mrl.psz_name ) );
1175
1176
1177     if( config_GetInt( p_this, "sout-display" ) )
1178     {
1179         p += sprintf( p, "duplicate{dst=display,dst=std{mux=\"%s\","
1180                       "access=\"%s\",dst=\"%s\"}}",
1181                       mrl.psz_way, mrl.psz_access, mrl.psz_name );
1182     }
1183     else
1184     {
1185         p += sprintf( p, "std{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
1186                       mrl.psz_way, mrl.psz_access, mrl.psz_name );
1187     }
1188
1189     mrl_Clean( &mrl );
1190     return( psz_chain );
1191 }