]> git.sesse.net Git - vlc/blob - src/stream_output/stream_output.c
Special case support --sout URL syntax for RTP demux.
[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, &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( !p_sout->p_libvlc->b_stats )
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     msg_Dbg( p_sout, "adding a new input" );
216
217     /* *** create a packetizer input *** */
218     p_input         = malloc( sizeof( sout_packetizer_input_t ) );
219     p_input->p_sout = p_sout;
220     p_input->p_fmt  = p_fmt;
221
222     if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
223     {
224         vlc_object_release( p_sout );
225         return p_input;
226     }
227
228     /* *** add it to the stream chain */
229     vlc_mutex_lock( &p_sout->lock );
230     p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
231     vlc_mutex_unlock( &p_sout->lock );
232
233     if( p_input->id == NULL )
234     {
235         free( p_input );
236         return NULL;
237     }
238
239     return( p_input );
240 }
241
242 /*****************************************************************************
243  *
244  *****************************************************************************/
245 int sout_InputDelete( sout_packetizer_input_t *p_input )
246 {
247     sout_instance_t     *p_sout = p_input->p_sout;
248
249     msg_Dbg( p_sout, "removing an input" );
250
251     if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
252     {
253         vlc_mutex_lock( &p_sout->lock );
254         p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
255         vlc_mutex_unlock( &p_sout->lock );
256     }
257
258     free( p_input );
259
260     return( VLC_SUCCESS);
261 }
262
263 /*****************************************************************************
264  *
265  *****************************************************************************/
266 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
267                           block_t *p_buffer )
268 {
269     sout_instance_t     *p_sout = p_input->p_sout;
270     int                 i_ret;
271
272     if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
273     {
274         block_Release( p_buffer );
275         return VLC_SUCCESS;
276     }
277     if( p_buffer->i_dts <= 0 )
278     {
279         msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
280         block_Release( p_buffer );
281         return VLC_SUCCESS;
282     }
283
284     vlc_mutex_lock( &p_sout->lock );
285     i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
286                                        p_input->id, p_buffer );
287     vlc_mutex_unlock( &p_sout->lock );
288
289     return i_ret;
290 }
291
292 /*****************************************************************************
293  * sout_AccessOutNew: allocate a new access out
294  *****************************************************************************/
295 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
296                                       const char *psz_access, const char *psz_name )
297 {
298     sout_access_out_t *p_access;
299     char              *psz_next;
300
301     if( !( p_access = vlc_object_create( p_sout,
302                                          sizeof( sout_access_out_t ) ) ) )
303     {
304         msg_Err( p_sout, "out of memory" );
305         return NULL;
306     }
307
308     psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
309                                    psz_access );
310     if( psz_next )
311     {
312         free( psz_next );
313     }
314     p_access->psz_path   = strdup( psz_name ? psz_name : "" );
315     p_access->p_sout     = p_sout;
316     p_access->p_sys = NULL;
317     p_access->pf_seek    = NULL;
318     p_access->pf_read    = NULL;
319     p_access->pf_write   = NULL;
320     p_access->pf_control = NULL;
321     p_access->p_module   = NULL;
322
323     p_access->i_writes = 0;
324     p_access->i_sent_bytes = 0;
325
326     vlc_object_attach( p_access, p_sout );
327
328     p_access->p_module   =
329         module_Need( p_access, "sout access", p_access->psz_access, VLC_TRUE );
330
331     if( !p_access->p_module )
332     {
333         free( p_access->psz_access );
334         free( p_access->psz_path );
335         vlc_object_detach( p_access );
336         vlc_object_release( p_access );
337         return( NULL );
338     }
339
340     return p_access;
341 }
342 /*****************************************************************************
343  * sout_AccessDelete: delete an access out
344  *****************************************************************************/
345 void sout_AccessOutDelete( sout_access_out_t *p_access )
346 {
347     vlc_object_detach( p_access );
348     if( p_access->p_module )
349     {
350         module_Unneed( p_access, p_access->p_module );
351     }
352     free( p_access->psz_access );
353
354     config_ChainDestroy( p_access->p_cfg );
355
356     free( p_access->psz_path );
357
358     vlc_object_release( p_access );
359 }
360
361 /*****************************************************************************
362  * sout_AccessSeek:
363  *****************************************************************************/
364 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
365 {
366     return p_access->pf_seek( p_access, i_pos );
367 }
368
369 /*****************************************************************************
370  * sout_AccessRead:
371  *****************************************************************************/
372 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
373 {
374     return( p_access->pf_read ?
375             p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
376 }
377
378 /*****************************************************************************
379  * sout_AccessWrite:
380  *****************************************************************************/
381 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
382 {
383     const unsigned i_packets_gather = 30;
384     p_access->i_writes++;
385     p_access->i_sent_bytes += p_buffer->i_buffer;
386     if( (p_access->i_writes % i_packets_gather) == 0 )
387     {
388         sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_PACKET, i_packets_gather );
389         sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_BYTE, p_access->i_sent_bytes );
390         p_access->i_sent_bytes = 0;
391     }
392     return p_access->pf_write( p_access, p_buffer );
393 }
394
395 /**
396  * sout_AccessOutControl
397  */
398 int sout_AccessOutControl (sout_access_out_t *access, int query, va_list args)
399 {
400     return (access->pf_control) ? access->pf_control (access, query, args)
401                                 : VLC_EGENERIC;
402 }
403
404 /*****************************************************************************
405  * sout_MuxNew: create a new mux
406  *****************************************************************************/
407 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
408                           sout_access_out_t *p_access )
409 {
410     sout_mux_t *p_mux;
411     char       *psz_next;
412
413     p_mux = vlc_object_create( p_sout, sizeof( sout_mux_t ) );
414     if( p_mux == NULL )
415     {
416         msg_Err( p_sout, "out of memory" );
417         return NULL;
418     }
419
420     p_mux->p_sout = p_sout;
421     psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
422     if( psz_next ) free( psz_next );
423
424     p_mux->p_access     = p_access;
425     p_mux->pf_control   = NULL;
426     p_mux->pf_addstream = NULL;
427     p_mux->pf_delstream = NULL;
428     p_mux->pf_mux       = NULL;
429     p_mux->i_nb_inputs  = 0;
430     p_mux->pp_inputs    = NULL;
431
432     p_mux->p_sys        = NULL;
433     p_mux->p_module     = NULL;
434
435     p_mux->b_add_stream_any_time = VLC_FALSE;
436     p_mux->b_waiting_stream = VLC_TRUE;
437     p_mux->i_add_stream_start = -1;
438
439     vlc_object_attach( p_mux, p_sout );
440
441     p_mux->p_module =
442         module_Need( p_mux, "sout mux", p_mux->psz_mux, VLC_TRUE );
443
444     if( p_mux->p_module == NULL )
445     {
446         FREENULL( p_mux->psz_mux );
447
448         vlc_object_detach( p_mux );
449         vlc_object_release( p_mux );
450         return NULL;
451     }
452
453     /* *** probe mux capacity *** */
454     if( p_mux->pf_control )
455     {
456         int b_answer = VLC_FALSE;
457
458         if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
459                              &b_answer ) )
460         {
461             b_answer = VLC_FALSE;
462         }
463
464         if( b_answer )
465         {
466             msg_Dbg( p_sout, "muxer support adding stream at any time" );
467             p_mux->b_add_stream_any_time = VLC_TRUE;
468             p_mux->b_waiting_stream = VLC_FALSE;
469
470             /* If we control the output pace then it's better to wait before
471              * starting muxing (generates better streams/files). */
472             if( !p_sout->i_out_pace_nocontrol )
473             {
474                 b_answer = VLC_TRUE;
475             }
476             else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
477                                       &b_answer ) )
478             {
479                 b_answer = VLC_FALSE;
480             }
481
482             if( b_answer )
483             {
484                 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
485                          "starting to mux" );
486                 p_mux->b_waiting_stream = VLC_TRUE;
487             }
488         }
489     }
490
491     return p_mux;
492 }
493
494 /*****************************************************************************
495  * sout_MuxDelete:
496  *****************************************************************************/
497 void sout_MuxDelete( sout_mux_t *p_mux )
498 {
499     vlc_object_detach( p_mux );
500     if( p_mux->p_module )
501     {
502         module_Unneed( p_mux, p_mux->p_module );
503     }
504     free( p_mux->psz_mux );
505
506     config_ChainDestroy( p_mux->p_cfg );
507
508     vlc_object_release( p_mux );
509 }
510
511 /*****************************************************************************
512  * sout_MuxAddStream:
513  *****************************************************************************/
514 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
515 {
516     sout_input_t *p_input;
517
518     if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
519     {
520         msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
521                         "to this format). You can try increasing sout-mux-caching value" );
522         return NULL;
523     }
524
525     msg_Dbg( p_mux, "adding a new input" );
526
527     /* create a new sout input */
528     p_input = malloc( sizeof( sout_input_t ) );
529     if( !p_input )
530     {
531         msg_Err( p_mux, "out of memory" );
532         return NULL;
533     }
534     p_input->p_sout = p_mux->p_sout;
535     p_input->p_fmt  = p_fmt;
536     p_input->p_fifo = block_FifoNew( p_mux->p_sout );
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 = VLC_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 ("I64Fd")",
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" ) * I64C(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 = VLC_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 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
768 #define SKIPTRAILINGSPACE( p, e ) \
769     { while( e > p && ( *(e-1) == ' ' || *(e-1) == '\t' ) ) e--; }
770
771 /* go accross " " and { } */
772 static char *_get_chain_end( char *str )
773 {
774     char c, *p = str;
775
776     SKIPSPACE( p );
777
778     for( ;; )
779     {
780         if( !*p || *p == ',' || *p == '}' ) return p;
781
782         if( *p != '{' && *p != '"' && *p != '\'' )
783         {
784             p++;
785             continue;
786         }
787
788         if( *p == '{' ) c = '}';
789         else c = *p;
790         p++;
791
792         for( ;; )
793         {
794             if( !*p ) return p;
795
796             if( *p == c ) return ++p;
797             else if( *p == '{' && c == '}' ) p = _get_chain_end( p );
798             else p++;
799         }
800     }
801 }
802
803 /*
804  * XXX name and p_cfg are used (-> do NOT free them)
805  */
806 sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_chain )
807 {
808     sout_stream_t *p_stream;
809
810     if( !psz_chain )
811     {
812         msg_Err( p_sout, "invalid chain" );
813         return NULL;
814     }
815
816     p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
817
818     if( !p_stream )
819     {
820         msg_Err( p_sout, "out of memory" );
821         return NULL;
822     }
823
824     p_stream->p_sout   = p_sout;
825     p_stream->p_sys    = NULL;
826
827     p_stream->psz_next =
828         config_ChainCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
829
830     msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
831
832     vlc_object_attach( p_stream, p_sout );
833
834     p_stream->p_module =
835         module_Need( p_stream, "sout stream", p_stream->psz_name, VLC_TRUE );
836
837     if( !p_stream->p_module )
838     {
839         sout_StreamDelete( p_stream );
840         return NULL;
841     }
842
843     return p_stream;
844 }
845
846 void sout_StreamDelete( sout_stream_t *p_stream )
847 {
848     msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
849
850     vlc_object_detach( p_stream );
851     if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
852
853     FREENULL( p_stream->psz_name );
854     FREENULL( p_stream->psz_next );
855
856     config_ChainDestroy( p_stream->p_cfg );
857
858     msg_Dbg( p_stream, "destroying chain done" );
859     vlc_object_release( p_stream );
860 }
861
862 static char *_sout_stream_url_to_chain( vlc_object_t *p_this,
863                                         const char *psz_url )
864 {
865     mrl_t       mrl;
866     char        *psz_chain;
867     const char  *fmt = "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}";
868     static const char rtpfmt[] = "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s\"}";
869
870     mrl_Parse( &mrl, psz_url );
871
872     /* Check if the URLs goes #rtp - otherwise we'll use #standard */
873     if (strcmp (mrl.psz_access, "rtp") == 0)
874     {
875         /* For historical reasons, rtp:// means RTP over UDP */
876         strcpy (mrl.psz_access, "udp");
877         fmt = rtpfmt;
878     }
879     else
880     {
881         static const char list[] = "dccp\0sctp\0tcp\0udplite\0";
882         for (const char *a = list; *a; a += strlen (a) + 1)
883              if (strcmp (a, mrl.psz_access) == 0)
884              {
885                  fmt = rtpfmt;
886                  break;
887              }
888     }
889
890     /* Convert the URL to a basic sout chain */
891     if (asprintf (&psz_chain, fmt,
892                   mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
893         psz_chain = NULL;
894
895     /* Duplicate and wrap if sout-display is on */
896     if (psz_chain && (config_GetInt( p_this, "sout-display" ) > 0))
897     {
898         char *tmp;
899         if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", tmp) == -1)
900             tmp = NULL;
901         free (psz_chain);
902         psz_chain = tmp;
903     }
904
905     mrl_Clean( &mrl );
906     return psz_chain;
907 }