1 /*****************************************************************************
2 * stream_output.c : stream output module
3 *****************************************************************************
4 * Copyright (C) 2002-2004 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Eric Petit <titer@videolan.org>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 #include <stdlib.h> /* free() */
33 #include <stdio.h> /* sprintf() */
34 #include <string.h> /* strerror() */
37 #include <vlc/input.h>
42 /*****************************************************************************
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 * );
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 );
66 /*****************************************************************************
67 * sout_NewInstance: creates a new stream output instance
68 *****************************************************************************/
69 sout_instance_t *__sout_NewInstance( vlc_object_t *p_parent, char * psz_dest )
71 sout_instance_t *p_sout;
74 if( var_Get( p_parent, "sout-keep", &keep ) < 0 )
76 msg_Warn( p_parent, "cannot get sout-keep value" );
77 keep.b_bool = VLC_FALSE;
81 if( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT,
82 FIND_ANYWHERE ) ) != NULL )
84 if( !strcmp( p_sout->psz_sout, psz_dest ) )
86 msg_Dbg( p_parent, "sout keep: reusing sout" );
87 msg_Dbg( p_parent, "sout keep: you probably want to use "
88 "gather stream_out" );
89 vlc_object_detach( p_sout );
90 vlc_object_attach( p_sout, p_parent );
91 vlc_object_release( p_sout );
96 msg_Dbg( p_parent, "sout keep: destroying unusable sout" );
97 vlc_object_release( p_sout );
98 sout_DeleteInstance( p_sout );
102 else if( !keep.b_bool )
104 while( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT,
105 FIND_PARENT ) ) != NULL )
107 msg_Dbg( p_parent, "sout keep: destroying old sout" );
108 vlc_object_release( p_sout );
109 sout_DeleteInstance( p_sout );
113 /* *** Allocate descriptor *** */
114 p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
117 msg_Err( p_parent, "out of memory" );
121 /* *** init descriptor *** */
122 p_sout->psz_sout = strdup( psz_dest );
123 p_sout->p_meta = NULL;
124 p_sout->i_out_pace_nocontrol = 0;
125 p_sout->p_sys = NULL;
127 vlc_mutex_init( p_sout, &p_sout->lock );
128 if( psz_dest && psz_dest[0] == '#' )
130 p_sout->psz_chain = strdup( &psz_dest[1] );
134 p_sout->psz_chain = sout_stream_url_to_chain( p_sout, psz_dest );
135 msg_Dbg( p_sout, "using sout chain=`%s'", p_sout->psz_chain );
137 p_sout->p_stream = NULL;
139 /* attach it for inherit */
140 vlc_object_attach( p_sout, p_parent );
142 p_sout->p_stream = sout_StreamNew( p_sout, p_sout->psz_chain );
144 if( p_sout->p_stream == NULL )
146 msg_Err( p_sout, "stream chain failed for `%s'", p_sout->psz_chain );
148 FREENULL( p_sout->psz_sout );
149 FREENULL( p_sout->psz_chain );
151 vlc_object_detach( p_sout );
152 vlc_object_destroy( p_sout );
159 /*****************************************************************************
160 * sout_DeleteInstance: delete a previously allocated instance
161 *****************************************************************************/
162 void sout_DeleteInstance( sout_instance_t * p_sout )
165 vlc_object_detach( p_sout );
167 /* remove the stream out chain */
168 sout_StreamDelete( p_sout->p_stream );
170 /* *** free all string *** */
171 FREENULL( p_sout->psz_sout );
172 FREENULL( p_sout->psz_chain );
177 vlc_meta_Delete( p_sout->p_meta );
180 vlc_mutex_destroy( &p_sout->lock );
182 /* *** free structure *** */
183 vlc_object_destroy( p_sout );
186 /*****************************************************************************
188 *****************************************************************************/
189 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
192 sout_packetizer_input_t *p_input;
194 msg_Dbg( p_sout, "adding a new input" );
196 /* *** create a packetizer input *** */
197 p_input = malloc( sizeof( sout_packetizer_input_t ) );
198 p_input->p_sout = p_sout;
199 p_input->p_fmt = p_fmt;
201 if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
203 vlc_object_release( p_sout );
207 /* *** add it to the stream chain */
208 vlc_mutex_lock( &p_sout->lock );
209 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
210 vlc_mutex_unlock( &p_sout->lock );
212 if( p_input->id == NULL )
221 /*****************************************************************************
223 *****************************************************************************/
224 int sout_InputDelete( sout_packetizer_input_t *p_input )
226 sout_instance_t *p_sout = p_input->p_sout;
228 msg_Dbg( p_sout, "removing an input" );
230 if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
232 vlc_mutex_lock( &p_sout->lock );
233 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
234 vlc_mutex_unlock( &p_sout->lock );
239 return( VLC_SUCCESS);
242 /*****************************************************************************
244 *****************************************************************************/
245 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
248 sout_instance_t *p_sout = p_input->p_sout;
251 if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
253 block_Release( p_buffer );
256 if( p_buffer->i_dts <= 0 )
258 msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
259 block_Release( p_buffer );
263 vlc_mutex_lock( &p_sout->lock );
264 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
265 p_input->id, p_buffer );
266 vlc_mutex_unlock( &p_sout->lock );
271 /*****************************************************************************
272 * sout_AccessOutNew: allocate a new access out
273 *****************************************************************************/
274 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
275 char *psz_access, char *psz_name )
277 sout_access_out_t *p_access;
280 if( !( p_access = vlc_object_create( p_sout,
281 sizeof( sout_access_out_t ) ) ) )
283 msg_Err( p_sout, "out of memory" );
287 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
293 p_access->psz_name = strdup( psz_name ? psz_name : "" );
294 p_access->p_sout = p_sout;
295 p_access->p_sys = NULL;
296 p_access->pf_seek = NULL;
297 p_access->pf_read = NULL;
298 p_access->pf_write = NULL;
299 p_access->p_module = NULL;
301 p_access->i_writes = 0;
302 p_access->i_sent_bytes = 0;
304 vlc_object_attach( p_access, p_sout );
307 module_Need( p_access, "sout access", p_access->psz_access, VLC_TRUE );
309 if( !p_access->p_module )
311 free( p_access->psz_access );
312 free( p_access->psz_name );
313 vlc_object_detach( p_access );
314 vlc_object_destroy( p_access );
320 /*****************************************************************************
321 * sout_AccessDelete: delete an access out
322 *****************************************************************************/
323 void sout_AccessOutDelete( sout_access_out_t *p_access )
325 vlc_object_detach( p_access );
326 if( p_access->p_module )
328 module_Unneed( p_access, p_access->p_module );
330 free( p_access->psz_access );
332 config_ChainDestroy( p_access->p_cfg );
334 free( p_access->psz_name );
336 vlc_object_destroy( p_access );
339 /*****************************************************************************
341 *****************************************************************************/
342 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
344 return p_access->pf_seek( p_access, i_pos );
347 /*****************************************************************************
349 *****************************************************************************/
350 int sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
352 return( p_access->pf_read ?
353 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
356 /*****************************************************************************
358 *****************************************************************************/
359 int sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
362 p_access->i_writes++;
363 p_access->i_sent_bytes += p_buffer->i_buffer;
364 if( p_access->p_libvlc->b_stats && p_access->i_writes % 30 == 0 )
366 /* Access_out -> sout_instance -> input_thread_t */
367 input_thread_t *p_input =
368 (input_thread_t *)vlc_object_find( p_access, VLC_OBJECT_INPUT,
372 stats_UpdateInteger( p_input, p_input->counters.p_sout_sent_packets,
374 stats_UpdateInteger( p_input, p_input->counters.p_sout_sent_bytes,
375 p_access->i_sent_bytes, &i_total );
376 stats_UpdateFloat( p_input, p_input->counters.p_sout_send_bitrate,
377 (float)i_total, NULL );
378 p_access->i_sent_bytes = 0;
379 vlc_object_release( p_input );
382 return p_access->pf_write( p_access, p_buffer );
385 /*****************************************************************************
386 * sout_MuxNew: create a new mux
387 *****************************************************************************/
388 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
389 sout_access_out_t *p_access )
394 p_mux = vlc_object_create( p_sout, sizeof( sout_mux_t ) );
397 msg_Err( p_sout, "out of memory" );
401 p_mux->p_sout = p_sout;
402 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
403 if( psz_next ) free( psz_next );
405 p_mux->p_access = p_access;
406 p_mux->pf_control = NULL;
407 p_mux->pf_addstream = NULL;
408 p_mux->pf_delstream = NULL;
409 p_mux->pf_mux = NULL;
410 p_mux->i_nb_inputs = 0;
411 p_mux->pp_inputs = NULL;
414 p_mux->p_module = NULL;
416 p_mux->b_add_stream_any_time = VLC_FALSE;
417 p_mux->b_waiting_stream = VLC_TRUE;
418 p_mux->i_add_stream_start = -1;
420 vlc_object_attach( p_mux, p_sout );
423 module_Need( p_mux, "sout mux", p_mux->psz_mux, VLC_TRUE );
425 if( p_mux->p_module == NULL )
427 FREENULL( p_mux->psz_mux );
429 vlc_object_detach( p_mux );
430 vlc_object_destroy( p_mux );
434 /* *** probe mux capacity *** */
435 if( p_mux->pf_control )
437 int b_answer = VLC_FALSE;
439 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
442 b_answer = VLC_FALSE;
447 msg_Dbg( p_sout, "muxer support adding stream at any time" );
448 p_mux->b_add_stream_any_time = VLC_TRUE;
449 p_mux->b_waiting_stream = VLC_FALSE;
451 /* If we control the output pace then it's better to wait before
452 * starting muxing (generates better streams/files). */
453 if( !p_sout->i_out_pace_nocontrol )
457 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
460 b_answer = VLC_FALSE;
465 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
467 p_mux->b_waiting_stream = VLC_TRUE;
475 /*****************************************************************************
477 *****************************************************************************/
478 void sout_MuxDelete( sout_mux_t *p_mux )
480 vlc_object_detach( p_mux );
481 if( p_mux->p_module )
483 module_Unneed( p_mux, p_mux->p_module );
485 free( p_mux->psz_mux );
487 config_ChainDestroy( p_mux->p_cfg );
489 vlc_object_destroy( p_mux );
492 /*****************************************************************************
494 *****************************************************************************/
495 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
497 sout_input_t *p_input;
499 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
501 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
506 msg_Dbg( p_mux, "adding a new input" );
508 /* create a new sout input */
509 p_input = malloc( sizeof( sout_input_t ) );
510 p_input->p_sout = p_mux->p_sout;
511 p_input->p_fmt = p_fmt;
512 p_input->p_fifo = block_FifoNew( p_mux->p_sout );
513 p_input->p_sys = NULL;
515 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
516 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
518 msg_Err( p_mux, "cannot add this stream" );
519 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
520 block_FifoRelease( p_input->p_fifo );
528 /*****************************************************************************
529 * sout_MuxDeleteStream:
530 *****************************************************************************/
531 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
535 if( p_mux->b_waiting_stream && p_input->p_fifo->i_depth > 0 )
537 /* We stop waiting, and call the muxer for taking care of the data
538 * before we remove this es */
539 p_mux->b_waiting_stream = VLC_FALSE;
540 p_mux->pf_mux( p_mux );
543 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
546 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
548 msg_Err( p_mux, "cannot delete this stream from mux" );
551 /* remove the entry */
552 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
554 if( p_mux->i_nb_inputs == 0 )
556 msg_Warn( p_mux, "no more input streams for this mux" );
559 block_FifoRelease( p_input->p_fifo );
564 /*****************************************************************************
565 * sout_MuxSendBuffer:
566 *****************************************************************************/
567 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
570 block_FifoPut( p_input->p_fifo, p_buffer );
572 if( p_mux->p_sout->i_out_pace_nocontrol )
574 mtime_t current_date = mdate();
575 if ( current_date > p_buffer->i_dts )
576 msg_Warn( p_mux, "late buffer for mux input ("I64Fd")",
577 current_date - p_buffer->i_dts );
580 if( p_mux->b_waiting_stream )
582 if( p_mux->i_add_stream_start < 0 )
584 p_mux->i_add_stream_start = p_buffer->i_dts;
587 if( p_mux->i_add_stream_start >= 0 &&
588 p_mux->i_add_stream_start + I64C(1500000) < p_buffer->i_dts )
590 /* Wait until we have more than 1.5 seconds worth of data
591 * before start muxing */
592 p_mux->b_waiting_stream = VLC_FALSE;
599 p_mux->pf_mux( p_mux );
602 /*****************************************************************************
604 *****************************************************************************/
605 static int mrl_Parse( mrl_t *p_mrl, char *psz_mrl )
607 char * psz_dup = strdup( psz_mrl );
608 char * psz_parser = psz_dup;
609 char * psz_access = "";
611 char * psz_name = "";
613 /* *** first parse psz_dest */
614 while( *psz_parser && *psz_parser != ':' )
616 if( *psz_parser == '{' )
618 while( *psz_parser && *psz_parser != '}' )
632 #if defined( WIN32 ) || defined( UNDER_CE )
633 if( psz_parser - psz_dup == 1 )
635 /* msg_Warn( p_sout, "drive letter %c: found in source string",
643 psz_access = psz_way = "";
648 *psz_parser++ = '\0';
650 /* let's skip '//' */
651 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
656 psz_name = psz_parser ;
658 /* Come back to parse the access and mux plug-ins */
659 psz_parser = psz_dup;
666 else if( *psz_parser == '/' )
674 psz_access = psz_parser;
676 while( *psz_parser && *psz_parser != '/' )
678 if( *psz_parser == '{' )
680 while( *psz_parser && *psz_parser != '}' )
695 if( *psz_parser == '/' )
697 *psz_parser++ = '\0';
708 psz_way = psz_parser;
712 p_mrl->psz_access = strdup( psz_access );
713 p_mrl->psz_way = strdup( psz_way );
714 p_mrl->psz_name = strdup( psz_name );
717 return( VLC_SUCCESS );
721 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
722 static void mrl_Clean( mrl_t *p_mrl )
724 FREENULL( p_mrl->psz_access );
725 FREENULL( p_mrl->psz_way );
726 FREENULL( p_mrl->psz_name );
730 /****************************************************************************
731 ****************************************************************************
735 ****************************************************************************
736 ****************************************************************************/
738 /* create a complete chain */
740 module{option=*:option=*}[:module{option=*:...}]
744 * parse module{options=str, option="str "}:
745 * return a pointer on the rest
746 * XXX: psz_chain is modified
748 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
749 #define SKIPTRAILINGSPACE( p, e ) \
750 { while( e > p && ( *(e-1) == ' ' || *(e-1) == '\t' ) ) e--; }
752 /* go accross " " and { } */
753 static char *_get_chain_end( char *str )
761 if( !*p || *p == ',' || *p == '}' ) return p;
763 if( *p != '{' && *p != '"' && *p != '\'' )
769 if( *p == '{' ) c = '}';
777 if( *p == c ) return ++p;
778 else if( *p == '{' && c == '}' ) p = _get_chain_end( p );
785 * XXX name and p_cfg are used (-> do NOT free them)
787 sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_chain )
789 sout_stream_t *p_stream;
793 msg_Err( p_sout, "invalid chain" );
797 p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
801 msg_Err( p_sout, "out of memory" );
805 p_stream->p_sout = p_sout;
806 p_stream->p_sys = NULL;
809 config_ChainCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
811 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
813 vlc_object_attach( p_stream, p_sout );
816 module_Need( p_stream, "sout stream", p_stream->psz_name, VLC_TRUE );
818 if( !p_stream->p_module )
820 sout_StreamDelete( p_stream );
827 void sout_StreamDelete( sout_stream_t *p_stream )
829 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
831 vlc_object_detach( p_stream );
832 if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
834 FREENULL( p_stream->psz_name );
835 FREENULL( p_stream->psz_next );
837 config_ChainDestroy( p_stream->p_cfg );
839 msg_Dbg( p_stream, "destroying chain done" );
840 vlc_object_destroy( p_stream );
843 static char *_sout_stream_url_to_chain( vlc_object_t *p_this, char *psz_url )
848 mrl_Parse( &mrl, psz_url );
849 p = psz_chain = malloc( 500 + strlen( mrl.psz_way ) +
850 strlen( mrl.psz_access ) +
851 strlen( mrl.psz_name ) );
854 if( config_GetInt( p_this, "sout-display" ) )
856 p += sprintf( p, "duplicate{dst=display,dst=std{mux=\"%s\","
857 "access=\"%s\",dst=\"%s\"}}",
858 mrl.psz_way, mrl.psz_access, mrl.psz_name );
862 p += sprintf( p, "std{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
863 mrl.psz_way, mrl.psz_access, mrl.psz_name );