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() */
38 #include "stream_output.h"
42 #include "input/input_internal.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 #define sout_stream_url_to_chain( p, s ) \
49 _sout_stream_url_to_chain( VLC_OBJECT(p), s )
50 static char *_sout_stream_url_to_chain( vlc_object_t *, char * );
64 /* mrl_Parse: parse psz_mrl and fill p_mrl */
65 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl );
66 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
67 static void mrl_Clean( mrl_t *p_mrl );
69 /*****************************************************************************
70 * sout_NewInstance: creates a new stream output instance
71 *****************************************************************************/
72 sout_instance_t *__sout_NewInstance( vlc_object_t *p_parent, char * psz_dest )
74 sout_instance_t *p_sout;
77 if( var_Get( p_parent, "sout-keep", &keep ) < 0 )
79 msg_Warn( p_parent, "cannot get sout-keep value" );
80 keep.b_bool = VLC_FALSE;
84 if( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT,
85 FIND_ANYWHERE ) ) != NULL )
87 if( !strcmp( p_sout->psz_sout, psz_dest ) )
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 );
99 msg_Dbg( p_parent, "sout keep: destroying unusable sout" );
100 vlc_object_release( p_sout );
101 sout_DeleteInstance( p_sout );
105 else if( !keep.b_bool )
107 while( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT,
108 FIND_PARENT ) ) != NULL )
110 msg_Dbg( p_parent, "sout keep: destroying old sout" );
111 vlc_object_release( p_sout );
112 sout_DeleteInstance( p_sout );
116 /* *** Allocate descriptor *** */
117 p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
120 msg_Err( p_parent, "out of memory" );
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;
130 vlc_mutex_init( p_sout, &p_sout->lock );
131 if( psz_dest && psz_dest[0] == '#' )
133 p_sout->psz_chain = strdup( &psz_dest[1] );
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 );
140 p_sout->p_stream = NULL;
142 /* attach it for inherit */
143 vlc_object_attach( p_sout, p_parent );
145 p_sout->p_stream = sout_StreamNew( p_sout, p_sout->psz_chain );
147 if( p_sout->p_stream == NULL )
149 msg_Err( p_sout, "stream chain failed for `%s'", p_sout->psz_chain );
151 FREENULL( p_sout->psz_sout );
152 FREENULL( p_sout->psz_chain );
154 vlc_object_detach( p_sout );
155 vlc_object_destroy( p_sout );
162 /*****************************************************************************
163 * sout_DeleteInstance: delete a previously allocated instance
164 *****************************************************************************/
165 void sout_DeleteInstance( sout_instance_t * p_sout )
168 vlc_object_detach( p_sout );
170 /* remove the stream out chain */
171 sout_StreamDelete( p_sout->p_stream );
173 /* *** free all string *** */
174 FREENULL( p_sout->psz_sout );
175 FREENULL( p_sout->psz_chain );
180 vlc_meta_Delete( p_sout->p_meta );
183 vlc_mutex_destroy( &p_sout->lock );
185 /* *** free structure *** */
186 vlc_object_destroy( p_sout );
189 /*****************************************************************************
191 *****************************************************************************/
192 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
195 sout_packetizer_input_t *p_input;
197 msg_Dbg( p_sout, "adding a new input" );
199 /* *** create a packetizer input *** */
200 p_input = malloc( sizeof( sout_packetizer_input_t ) );
201 p_input->p_sout = p_sout;
202 p_input->p_fmt = p_fmt;
204 if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
206 vlc_object_release( p_sout );
210 /* *** add it to the stream chain */
211 vlc_mutex_lock( &p_sout->lock );
212 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
213 vlc_mutex_unlock( &p_sout->lock );
215 if( p_input->id == NULL )
224 /*****************************************************************************
226 *****************************************************************************/
227 int sout_InputDelete( sout_packetizer_input_t *p_input )
229 sout_instance_t *p_sout = p_input->p_sout;
231 msg_Dbg( p_sout, "removing an input" );
233 if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
235 vlc_mutex_lock( &p_sout->lock );
236 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
237 vlc_mutex_unlock( &p_sout->lock );
242 return( VLC_SUCCESS);
245 /*****************************************************************************
247 *****************************************************************************/
248 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
251 sout_instance_t *p_sout = p_input->p_sout;
254 if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
256 block_Release( p_buffer );
259 if( p_buffer->i_dts <= 0 )
261 msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
262 block_Release( p_buffer );
266 vlc_mutex_lock( &p_sout->lock );
267 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
268 p_input->id, p_buffer );
269 vlc_mutex_unlock( &p_sout->lock );
274 /*****************************************************************************
275 * sout_AccessOutNew: allocate a new access out
276 *****************************************************************************/
277 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
278 char *psz_access, char *psz_name )
280 sout_access_out_t *p_access;
283 if( !( p_access = vlc_object_create( p_sout,
284 sizeof( sout_access_out_t ) ) ) )
286 msg_Err( p_sout, "out of memory" );
290 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
296 p_access->psz_name = strdup( psz_name ? psz_name : "" );
297 p_access->p_sout = p_sout;
298 p_access->p_sys = NULL;
299 p_access->pf_seek = NULL;
300 p_access->pf_read = NULL;
301 p_access->pf_write = NULL;
302 p_access->p_module = NULL;
304 p_access->i_writes = 0;
305 p_access->i_sent_bytes = 0;
307 vlc_object_attach( p_access, p_sout );
310 module_Need( p_access, "sout access", p_access->psz_access, VLC_TRUE );
312 if( !p_access->p_module )
314 free( p_access->psz_access );
315 free( p_access->psz_name );
316 vlc_object_detach( p_access );
317 vlc_object_destroy( p_access );
323 /*****************************************************************************
324 * sout_AccessDelete: delete an access out
325 *****************************************************************************/
326 void sout_AccessOutDelete( sout_access_out_t *p_access )
328 vlc_object_detach( p_access );
329 if( p_access->p_module )
331 module_Unneed( p_access, p_access->p_module );
333 free( p_access->psz_access );
335 config_ChainDestroy( p_access->p_cfg );
337 free( p_access->psz_name );
339 vlc_object_destroy( p_access );
342 /*****************************************************************************
344 *****************************************************************************/
345 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
347 return p_access->pf_seek( p_access, i_pos );
350 /*****************************************************************************
352 *****************************************************************************/
353 int sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
355 return( p_access->pf_read ?
356 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
359 /*****************************************************************************
361 *****************************************************************************/
362 int sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
365 p_access->i_writes++;
366 p_access->i_sent_bytes += p_buffer->i_buffer;
367 if( p_access->p_libvlc->b_stats && p_access->i_writes % 30 == 0 )
369 /* Access_out -> sout_instance -> input_thread_t */
370 input_thread_t *p_input =
371 (input_thread_t *)vlc_object_find( p_access, VLC_OBJECT_INPUT,
375 stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_packets,
377 stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_bytes,
378 p_access->i_sent_bytes, &i_total );
379 stats_UpdateFloat( p_input, p_input->p->counters.p_sout_send_bitrate,
380 (float)i_total, NULL );
381 p_access->i_sent_bytes = 0;
382 vlc_object_release( p_input );
385 return p_access->pf_write( p_access, p_buffer );
388 /*****************************************************************************
389 * sout_MuxNew: create a new mux
390 *****************************************************************************/
391 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
392 sout_access_out_t *p_access )
397 p_mux = vlc_object_create( p_sout, sizeof( sout_mux_t ) );
400 msg_Err( p_sout, "out of memory" );
404 p_mux->p_sout = p_sout;
405 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
406 if( psz_next ) free( psz_next );
408 p_mux->p_access = p_access;
409 p_mux->pf_control = NULL;
410 p_mux->pf_addstream = NULL;
411 p_mux->pf_delstream = NULL;
412 p_mux->pf_mux = NULL;
413 p_mux->i_nb_inputs = 0;
414 p_mux->pp_inputs = NULL;
417 p_mux->p_module = NULL;
419 p_mux->b_add_stream_any_time = VLC_FALSE;
420 p_mux->b_waiting_stream = VLC_TRUE;
421 p_mux->i_add_stream_start = -1;
423 vlc_object_attach( p_mux, p_sout );
426 module_Need( p_mux, "sout mux", p_mux->psz_mux, VLC_TRUE );
428 if( p_mux->p_module == NULL )
430 FREENULL( p_mux->psz_mux );
432 vlc_object_detach( p_mux );
433 vlc_object_destroy( p_mux );
437 /* *** probe mux capacity *** */
438 if( p_mux->pf_control )
440 int b_answer = VLC_FALSE;
442 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
445 b_answer = VLC_FALSE;
450 msg_Dbg( p_sout, "muxer support adding stream at any time" );
451 p_mux->b_add_stream_any_time = VLC_TRUE;
452 p_mux->b_waiting_stream = VLC_FALSE;
454 /* If we control the output pace then it's better to wait before
455 * starting muxing (generates better streams/files). */
456 if( !p_sout->i_out_pace_nocontrol )
460 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
463 b_answer = VLC_FALSE;
468 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
470 p_mux->b_waiting_stream = VLC_TRUE;
478 /*****************************************************************************
480 *****************************************************************************/
481 void sout_MuxDelete( sout_mux_t *p_mux )
483 vlc_object_detach( p_mux );
484 if( p_mux->p_module )
486 module_Unneed( p_mux, p_mux->p_module );
488 free( p_mux->psz_mux );
490 config_ChainDestroy( p_mux->p_cfg );
492 vlc_object_destroy( p_mux );
495 /*****************************************************************************
497 *****************************************************************************/
498 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
500 sout_input_t *p_input;
502 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
504 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
509 msg_Dbg( p_mux, "adding a new input" );
511 /* create a new sout input */
512 p_input = malloc( sizeof( sout_input_t ) );
513 p_input->p_sout = p_mux->p_sout;
514 p_input->p_fmt = p_fmt;
515 p_input->p_fifo = block_FifoNew( p_mux->p_sout );
516 p_input->p_sys = NULL;
518 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
519 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
521 msg_Err( p_mux, "cannot add this stream" );
522 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
523 block_FifoRelease( p_input->p_fifo );
531 /*****************************************************************************
532 * sout_MuxDeleteStream:
533 *****************************************************************************/
534 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
538 if( p_mux->b_waiting_stream && p_input->p_fifo->i_depth > 0 )
540 /* We stop waiting, and call the muxer for taking care of the data
541 * before we remove this es */
542 p_mux->b_waiting_stream = VLC_FALSE;
543 p_mux->pf_mux( p_mux );
546 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
549 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
551 msg_Err( p_mux, "cannot delete this stream from mux" );
554 /* remove the entry */
555 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
557 if( p_mux->i_nb_inputs == 0 )
559 msg_Warn( p_mux, "no more input streams for this mux" );
562 block_FifoRelease( p_input->p_fifo );
567 /*****************************************************************************
568 * sout_MuxSendBuffer:
569 *****************************************************************************/
570 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
573 block_FifoPut( p_input->p_fifo, p_buffer );
575 if( p_mux->p_sout->i_out_pace_nocontrol )
577 mtime_t current_date = mdate();
578 if ( current_date > p_buffer->i_dts )
579 msg_Warn( p_mux, "late buffer for mux input ("I64Fd")",
580 current_date - p_buffer->i_dts );
583 if( p_mux->b_waiting_stream )
585 if( p_mux->i_add_stream_start < 0 )
587 p_mux->i_add_stream_start = p_buffer->i_dts;
590 if( p_mux->i_add_stream_start >= 0 &&
591 p_mux->i_add_stream_start + I64C(1500000) < p_buffer->i_dts )
593 /* Wait until we have more than 1.5 seconds worth of data
594 * before start muxing */
595 p_mux->b_waiting_stream = VLC_FALSE;
602 p_mux->pf_mux( p_mux );
605 /*****************************************************************************
607 *****************************************************************************/
608 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
610 char * psz_dup = strdup( psz_mrl );
611 char * psz_parser = psz_dup;
612 const char * psz_access;
613 const char * psz_way;
616 /* *** first parse psz_dest */
617 while( *psz_parser && *psz_parser != ':' )
619 if( *psz_parser == '{' )
621 while( *psz_parser && *psz_parser != '}' )
635 #if defined( WIN32 ) || defined( UNDER_CE )
636 if( psz_parser - psz_dup == 1 )
638 /* msg_Warn( p_sout, "drive letter %c: found in source string",
646 psz_access = psz_way = "";
651 *psz_parser++ = '\0';
653 /* let's skip '//' */
654 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
659 psz_name = psz_parser ;
661 /* Come back to parse the access and mux plug-ins */
662 psz_parser = psz_dup;
669 else if( *psz_parser == '/' )
677 psz_access = psz_parser;
679 while( *psz_parser && *psz_parser != '/' )
681 if( *psz_parser == '{' )
683 while( *psz_parser && *psz_parser != '}' )
698 if( *psz_parser == '/' )
700 *psz_parser++ = '\0';
711 psz_way = psz_parser;
715 p_mrl->psz_access = strdup( psz_access );
716 p_mrl->psz_way = strdup( psz_way );
717 p_mrl->psz_name = strdup( psz_name );
720 return( VLC_SUCCESS );
724 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
725 static void mrl_Clean( mrl_t *p_mrl )
727 FREENULL( p_mrl->psz_access );
728 FREENULL( p_mrl->psz_way );
729 FREENULL( p_mrl->psz_name );
733 /****************************************************************************
734 ****************************************************************************
738 ****************************************************************************
739 ****************************************************************************/
741 /* create a complete chain */
743 module{option=*:option=*}[:module{option=*:...}]
747 * parse module{options=str, option="str "}:
748 * return a pointer on the rest
749 * XXX: psz_chain is modified
751 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
752 #define SKIPTRAILINGSPACE( p, e ) \
753 { while( e > p && ( *(e-1) == ' ' || *(e-1) == '\t' ) ) e--; }
755 /* go accross " " and { } */
756 static char *_get_chain_end( char *str )
764 if( !*p || *p == ',' || *p == '}' ) return p;
766 if( *p != '{' && *p != '"' && *p != '\'' )
772 if( *p == '{' ) c = '}';
780 if( *p == c ) return ++p;
781 else if( *p == '{' && c == '}' ) p = _get_chain_end( p );
788 * XXX name and p_cfg are used (-> do NOT free them)
790 sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_chain )
792 sout_stream_t *p_stream;
796 msg_Err( p_sout, "invalid chain" );
800 p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
804 msg_Err( p_sout, "out of memory" );
808 p_stream->p_sout = p_sout;
809 p_stream->p_sys = NULL;
812 config_ChainCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
814 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
816 vlc_object_attach( p_stream, p_sout );
819 module_Need( p_stream, "sout stream", p_stream->psz_name, VLC_TRUE );
821 if( !p_stream->p_module )
823 sout_StreamDelete( p_stream );
830 void sout_StreamDelete( sout_stream_t *p_stream )
832 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
834 vlc_object_detach( p_stream );
835 if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
837 FREENULL( p_stream->psz_name );
838 FREENULL( p_stream->psz_next );
840 config_ChainDestroy( p_stream->p_cfg );
842 msg_Dbg( p_stream, "destroying chain done" );
843 vlc_object_destroy( p_stream );
846 static char *_sout_stream_url_to_chain( vlc_object_t *p_this, char *psz_url )
851 mrl_Parse( &mrl, psz_url );
852 p = psz_chain = malloc( 500 + strlen( mrl.psz_way ) +
853 strlen( mrl.psz_access ) +
854 strlen( mrl.psz_name ) );
857 if( config_GetInt( p_this, "sout-display" ) )
859 p += sprintf( p, "duplicate{dst=display,dst=std{mux=\"%s\","
860 "access=\"%s\",dst=\"%s\"}}",
861 mrl.psz_way, mrl.psz_access, mrl.psz_name );
865 p += sprintf( p, "std{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
866 mrl.psz_way, mrl.psz_access, mrl.psz_name );