1 /*****************************************************************************
2 * stream_output.c : stream output module
3 *****************************************************************************
4 * Copyright (C) 2002-2007 VLC authors and VideoLAN
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 it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
36 #include <vlc_common.h>
38 #include <stdlib.h> /* free() */
39 #include <stdio.h> /* sprintf() */
44 #include "stream_output.h"
47 #include <vlc_block.h>
48 #include <vlc_codec.h>
49 #include <vlc_modules.h>
51 #include "input/input_interface.h"
53 #define VLC_CODEC_NULL VLC_FOURCC( 'n', 'u', 'l', 'l' )
56 /*****************************************************************************
58 *****************************************************************************/
59 static char *sout_stream_url_to_chain( bool, const char * );
73 /* mrl_Parse: parse psz_mrl and fill p_mrl */
74 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl );
75 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
76 static void mrl_Clean( mrl_t *p_mrl );
78 #undef sout_NewInstance
80 /*****************************************************************************
81 * sout_NewInstance: creates a new stream output instance
82 *****************************************************************************/
83 sout_instance_t *sout_NewInstance( vlc_object_t *p_parent, const char *psz_dest )
85 sout_instance_t *p_sout;
88 if( psz_dest && psz_dest[0] == '#' )
90 psz_chain = strdup( &psz_dest[1] );
94 psz_chain = sout_stream_url_to_chain(
95 var_InheritBool(p_parent, "sout-display"), psz_dest );
100 /* *** Allocate descriptor *** */
101 p_sout = vlc_custom_create( p_parent, sizeof( *p_sout ), "stream output" );
108 msg_Dbg( p_sout, "using sout chain=`%s'", psz_chain );
110 /* *** init descriptor *** */
111 p_sout->psz_sout = strdup( psz_dest );
112 p_sout->i_out_pace_nocontrol = 0;
114 vlc_mutex_init( &p_sout->lock );
115 p_sout->p_stream = NULL;
117 var_Create( p_sout, "sout-mux-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
119 p_sout->p_stream = sout_StreamChainNew( p_sout, psz_chain, NULL, NULL );
120 if( p_sout->p_stream )
126 msg_Err( p_sout, "stream chain failed for `%s'", psz_chain );
129 FREENULL( p_sout->psz_sout );
131 vlc_mutex_destroy( &p_sout->lock );
132 vlc_object_release( p_sout );
136 /*****************************************************************************
137 * sout_DeleteInstance: delete a previously allocated instance
138 *****************************************************************************/
139 void sout_DeleteInstance( sout_instance_t * p_sout )
141 /* remove the stream out chain */
142 sout_StreamChainDelete( p_sout->p_stream, NULL );
144 /* *** free all string *** */
145 FREENULL( p_sout->psz_sout );
147 vlc_mutex_destroy( &p_sout->lock );
149 /* *** free structure *** */
150 vlc_object_release( p_sout );
153 /*****************************************************************************
155 *****************************************************************************/
156 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
159 sout_packetizer_input_t *p_input;
161 /* *** create a packetizer input *** */
162 p_input = malloc( sizeof( sout_packetizer_input_t ) );
163 if( !p_input ) return NULL;
164 p_input->p_sout = p_sout;
165 p_input->p_fmt = p_fmt;
167 msg_Dbg( p_sout, "adding a new sout input (sout_input:%p)", p_input );
169 if( p_fmt->i_codec == VLC_CODEC_NULL )
171 vlc_object_release( p_sout );
175 /* *** add it to the stream chain */
176 vlc_mutex_lock( &p_sout->lock );
177 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
178 vlc_mutex_unlock( &p_sout->lock );
180 if( p_input->id == NULL )
189 /*****************************************************************************
191 *****************************************************************************/
192 int sout_InputDelete( sout_packetizer_input_t *p_input )
194 sout_instance_t *p_sout = p_input->p_sout;
196 msg_Dbg( p_sout, "removing a sout input (sout_input:%p)", p_input );
198 if( p_input->p_fmt->i_codec != VLC_CODEC_NULL )
200 vlc_mutex_lock( &p_sout->lock );
201 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
202 vlc_mutex_unlock( &p_sout->lock );
207 return( VLC_SUCCESS);
210 /*****************************************************************************
212 *****************************************************************************/
213 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
216 sout_instance_t *p_sout = p_input->p_sout;
219 if( p_input->p_fmt->i_codec == VLC_CODEC_NULL )
221 block_Release( p_buffer );
225 if( p_buffer->i_dts <= VLC_TS_INVALID )
227 msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
228 block_Release( p_buffer );
232 vlc_mutex_lock( &p_sout->lock );
233 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
234 p_input->id, p_buffer );
235 vlc_mutex_unlock( &p_sout->lock );
240 #undef sout_AccessOutNew
241 /*****************************************************************************
242 * sout_AccessOutNew: allocate a new access out
243 *****************************************************************************/
244 sout_access_out_t *sout_AccessOutNew( vlc_object_t *p_sout,
245 const char *psz_access, const char *psz_name )
247 sout_access_out_t *p_access;
250 p_access = vlc_custom_create( p_sout, sizeof( *p_access ), "access out" );
254 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
257 p_access->psz_path = strdup( psz_name ? psz_name : "" );
258 p_access->p_sys = NULL;
259 p_access->pf_seek = NULL;
260 p_access->pf_read = NULL;
261 p_access->pf_write = NULL;
262 p_access->pf_control = NULL;
263 p_access->p_module = NULL;
266 module_need( p_access, "sout access", p_access->psz_access, true );
268 if( !p_access->p_module )
270 free( p_access->psz_access );
271 free( p_access->psz_path );
272 vlc_object_release( p_access );
278 /*****************************************************************************
279 * sout_AccessDelete: delete an access out
280 *****************************************************************************/
281 void sout_AccessOutDelete( sout_access_out_t *p_access )
283 if( p_access->p_module )
285 module_unneed( p_access, p_access->p_module );
287 free( p_access->psz_access );
289 config_ChainDestroy( p_access->p_cfg );
291 free( p_access->psz_path );
293 vlc_object_release( p_access );
296 /*****************************************************************************
298 *****************************************************************************/
299 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
301 return p_access->pf_seek( p_access, i_pos );
304 /*****************************************************************************
306 *****************************************************************************/
307 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
309 return( p_access->pf_read ?
310 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
313 /*****************************************************************************
315 *****************************************************************************/
316 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
318 return p_access->pf_write( p_access, p_buffer );
322 * sout_AccessOutControl
324 int sout_AccessOutControl (sout_access_out_t *access, int query, ...)
329 va_start (ap, query);
330 if (access->pf_control)
331 ret = access->pf_control (access, query, ap);
338 /*****************************************************************************
339 * sout_MuxNew: create a new mux
340 *****************************************************************************/
341 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, const char *psz_mux,
342 sout_access_out_t *p_access )
347 p_mux = vlc_custom_create( p_sout, sizeof( *p_mux ), "mux" );
351 p_mux->p_sout = p_sout;
352 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
355 p_mux->p_access = p_access;
356 p_mux->pf_control = NULL;
357 p_mux->pf_addstream = NULL;
358 p_mux->pf_delstream = NULL;
359 p_mux->pf_mux = NULL;
360 p_mux->i_nb_inputs = 0;
361 p_mux->pp_inputs = NULL;
364 p_mux->p_module = NULL;
366 p_mux->b_add_stream_any_time = false;
367 p_mux->b_waiting_stream = true;
368 p_mux->i_add_stream_start = -1;
371 module_need( p_mux, "sout mux", p_mux->psz_mux, true );
373 if( p_mux->p_module == NULL )
375 FREENULL( p_mux->psz_mux );
377 vlc_object_release( p_mux );
381 /* *** probe mux capacity *** */
382 if( p_mux->pf_control )
384 int b_answer = false;
386 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
394 msg_Dbg( p_sout, "muxer support adding stream at any time" );
395 p_mux->b_add_stream_any_time = true;
396 p_mux->b_waiting_stream = false;
398 /* If we control the output pace then it's better to wait before
399 * starting muxing (generates better streams/files). */
400 if( !p_sout->i_out_pace_nocontrol )
404 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
412 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
414 p_mux->b_waiting_stream = true;
422 /*****************************************************************************
424 *****************************************************************************/
425 void sout_MuxDelete( sout_mux_t *p_mux )
427 if( p_mux->p_module )
429 module_unneed( p_mux, p_mux->p_module );
431 free( p_mux->psz_mux );
433 config_ChainDestroy( p_mux->p_cfg );
435 vlc_object_release( p_mux );
438 /*****************************************************************************
440 *****************************************************************************/
441 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
443 sout_input_t *p_input;
445 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
447 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
448 "to this format). You can try increasing sout-mux-caching value" );
452 msg_Dbg( p_mux, "adding a new input" );
454 /* create a new sout input */
455 p_input = malloc( sizeof( sout_input_t ) );
458 p_input->p_fmt = p_fmt;
459 p_input->p_fifo = block_FifoNew();
460 p_input->p_sys = NULL;
462 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
463 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
465 msg_Err( p_mux, "cannot add this stream" );
466 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
467 block_FifoRelease( p_input->p_fifo );
475 /*****************************************************************************
476 * sout_MuxDeleteStream:
477 *****************************************************************************/
478 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
482 if( p_mux->b_waiting_stream
483 && block_FifoCount( p_input->p_fifo ) > 0 )
485 /* We stop waiting, and call the muxer for taking care of the data
486 * before we remove this es */
487 p_mux->b_waiting_stream = false;
488 p_mux->pf_mux( p_mux );
491 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
494 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
496 msg_Err( p_mux, "cannot delete this stream from mux" );
499 /* remove the entry */
500 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
502 if( p_mux->i_nb_inputs == 0 )
504 msg_Warn( p_mux, "no more input streams for this mux" );
507 block_FifoRelease( p_input->p_fifo );
512 /*****************************************************************************
513 * sout_MuxSendBuffer:
514 *****************************************************************************/
515 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
518 block_FifoPut( p_input->p_fifo, p_buffer );
520 if( p_mux->p_sout->i_out_pace_nocontrol )
522 mtime_t current_date = mdate();
523 if ( current_date > p_buffer->i_dts )
524 msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
525 current_date - p_buffer->i_dts );
528 if( p_mux->b_waiting_stream )
530 const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * INT64_C(1000);
532 if( p_mux->i_add_stream_start < 0 )
533 p_mux->i_add_stream_start = p_buffer->i_dts;
535 /* Wait until we have enought data before muxing */
536 if( p_mux->i_add_stream_start < 0 ||
537 p_buffer->i_dts < p_mux->i_add_stream_start + i_caching )
539 p_mux->b_waiting_stream = false;
541 p_mux->pf_mux( p_mux );
545 /*****************************************************************************
546 * sout_MuxGetStream: find stream to be muxed
547 *****************************************************************************/
548 int sout_MuxGetStream( sout_mux_t *p_mux, int i_blocks, mtime_t *pi_dts )
553 for( int i = 0; i < p_mux->i_nb_inputs; i++ )
555 sout_input_t *p_input = p_mux->pp_inputs[i];
558 if( block_FifoCount( p_input->p_fifo ) < i_blocks )
560 if( p_input->p_fmt->i_cat != SPU_ES )
564 /* FIXME: SPU muxing */
568 p_data = block_FifoShow( p_input->p_fifo );
569 if( i_stream < 0 || p_data->i_dts < i_dts )
572 i_dts = p_data->i_dts;
576 if( pi_dts ) *pi_dts = i_dts;
582 /*****************************************************************************
584 *****************************************************************************/
585 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
587 char * psz_dup = strdup( psz_mrl );
588 char * psz_parser = psz_dup;
589 const char * psz_access;
590 const char * psz_way;
593 /* *** first parse psz_dest */
594 while( *psz_parser && *psz_parser != ':' )
596 if( *psz_parser == '{' )
598 while( *psz_parser && *psz_parser != '}' )
612 #if defined( _WIN32 ) || defined( __OS2__ )
613 if( psz_parser - psz_dup == 1 )
615 /* msg_Warn( p_sout, "drive letter %c: found in source string",
623 psz_access = psz_way = "";
628 *psz_parser++ = '\0';
630 /* let's skip '//' */
631 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
636 psz_name = psz_parser ;
638 /* Come back to parse the access and mux plug-ins */
639 psz_parser = psz_dup;
646 else if( *psz_parser == '/' )
654 psz_access = psz_parser;
656 while( *psz_parser && *psz_parser != '/' )
658 if( *psz_parser == '{' )
660 while( *psz_parser && *psz_parser != '}' )
675 if( *psz_parser == '/' )
677 *psz_parser++ = '\0';
688 psz_way = psz_parser;
692 p_mrl->psz_access = strdup( psz_access );
693 p_mrl->psz_way = strdup( psz_way );
694 p_mrl->psz_name = strdup( psz_name );
697 return( VLC_SUCCESS );
701 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
702 static void mrl_Clean( mrl_t *p_mrl )
704 FREENULL( p_mrl->psz_access );
705 FREENULL( p_mrl->psz_way );
706 FREENULL( p_mrl->psz_name );
710 /****************************************************************************
711 ****************************************************************************
715 ****************************************************************************
716 ****************************************************************************/
718 /* Destroy a "stream_out" module */
719 static void sout_StreamDelete( sout_stream_t *p_stream )
721 sout_instance_t *p_sout = (sout_instance_t *)(p_stream->p_parent);
723 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
725 p_sout->i_out_pace_nocontrol -= p_stream->pace_nocontrol;
727 if( p_stream->p_module != NULL )
728 module_unneed( p_stream, p_stream->p_module );
730 FREENULL( p_stream->psz_name );
732 config_ChainDestroy( p_stream->p_cfg );
734 msg_Dbg( p_stream, "destroying chain done" );
735 vlc_object_release( p_stream );
738 /* Destroy a "stream_out" modules chain
740 * p_first is the first module to be destroyed in the chain
741 * p_last is the last module to be destroyed
742 * if NULL, all modules are destroyed
743 * if not NULL, modules following it must be destroyed separately
745 void sout_StreamChainDelete(sout_stream_t *p_first, sout_stream_t *p_last)
747 while(p_first != NULL)
749 sout_stream_t *p_next = p_first->p_next;
751 sout_StreamDelete(p_first);
752 if(p_first == p_last)
758 /* Create a "stream_out" module, which may forward its ES to p_next module */
760 * XXX name and p_cfg are used (-> do NOT free them)
762 static sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_name,
763 config_chain_t *p_cfg, sout_stream_t *p_next)
765 sout_stream_t *p_stream;
769 p_stream = vlc_custom_create( p_sout, sizeof( *p_stream ), "stream out" );
773 p_stream->p_sout = p_sout;
774 p_stream->psz_name = psz_name;
775 p_stream->p_cfg = p_cfg;
776 p_stream->p_next = p_next;
777 p_stream->pace_nocontrol = false;
778 p_stream->p_sys = NULL;
780 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
783 module_need( p_stream, "sout stream", p_stream->psz_name, true );
785 if( !p_stream->p_module )
787 /* those must be freed by the caller if creation failed */
788 p_stream->psz_name = NULL;
789 p_stream->p_cfg = NULL;
791 sout_StreamDelete( p_stream );
795 p_sout->i_out_pace_nocontrol += p_stream->pace_nocontrol;
799 /* Creates a complete "stream_out" modules chain
801 * chain format: module1{option=*:option=*}[:module2{option=*:...}]
803 * The modules are created starting from the last one and linked together
804 * A pointer to the last module created is stored if pp_last isn't NULL, to
805 * make sure sout_StreamChainDelete doesn't delete modules created in another
808 * Returns a pointer to the first module.
810 sout_stream_t *sout_StreamChainNew(sout_instance_t *p_sout, char *psz_chain,
811 sout_stream_t *p_next, sout_stream_t **pp_last)
813 if(!psz_chain || !*psz_chain)
815 if(pp_last) *pp_last = NULL;
819 char *psz_parser = strdup(psz_chain);
823 vlc_array_t cfg, name;
824 vlc_array_init(&cfg);
825 vlc_array_init(&name);
830 config_chain_t *p_cfg;
832 psz_chain = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
834 psz_parser = psz_chain;
836 vlc_array_append(&cfg, p_cfg);
837 vlc_array_append(&name, psz_name);
840 int i = vlc_array_count(&name);
842 vlc_array_init(&module);
845 p_next = sout_StreamNew( p_sout, vlc_array_item_at_index(&name, i),
846 vlc_array_item_at_index(&cfg, i), p_next);
851 if(i == vlc_array_count(&name) - 1 && pp_last)
852 *pp_last = p_next; /* last module created in the chain */
854 vlc_array_append(&module, p_next);
857 vlc_array_clear(&name);
858 vlc_array_clear(&cfg);
859 vlc_array_clear(&module);
865 i++; /* last module couldn't be created */
867 /* destroy all modules created, starting with the last one */
868 int modules = vlc_array_count(&module);
870 sout_StreamDelete(vlc_array_item_at_index(&module, modules));
871 vlc_array_clear(&module);
873 /* then destroy all names and config which weren't destroyed by
874 * sout_StreamDelete */
877 free(vlc_array_item_at_index(&name, i));
878 config_ChainDestroy(vlc_array_item_at_index(&cfg, i));
880 vlc_array_clear(&name);
881 vlc_array_clear(&cfg);
886 static char *sout_stream_url_to_chain( bool b_sout_display,
887 const char *psz_url )
892 mrl_Parse( &mrl, psz_url );
894 /* Check if the URLs goes to #rtp - otherwise we'll use #standard */
895 static const char rtplist[] = "dccp\0sctp\0tcp\0udplite\0";
896 for (const char *a = rtplist; *a; a += strlen (a) + 1)
897 if (strcmp (a, mrl.psz_access) == 0)
900 if (strcmp (mrl.psz_access, "rtp") == 0)
903 /* For historical reasons, rtp:// means RTP over UDP */
904 strcpy (mrl.psz_access, "udp");
906 if (mrl.psz_name[0] == '[')
908 port = strstr (mrl.psz_name, "]:");
913 port = strchr (mrl.psz_name, ':');
915 *port++ = '\0'; /* erase ':' */
917 if (asprintf (&psz_chain,
918 "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s%s%s\"}",
919 mrl.psz_way, mrl.psz_access, mrl.psz_name,
920 port ? "\",port=\"" : "", port ? port : "") == -1)
925 /* Convert the URL to a basic standard sout chain */
926 if (asprintf (&psz_chain,
927 "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
928 mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
932 /* Duplicate and wrap if sout-display is on */
933 if (psz_chain && b_sout_display)
936 if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", psz_chain) == -1)
946 #undef sout_EncoderCreate
947 encoder_t *sout_EncoderCreate( vlc_object_t *p_this )
949 return vlc_custom_create( p_this, sizeof( encoder_t ), "encoder" );