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 assert( psz_dest != NULL );
90 if( psz_dest[0] == '#' )
92 psz_chain = strdup( &psz_dest[1] );
96 psz_chain = sout_stream_url_to_chain(
97 var_InheritBool(p_parent, "sout-display"), psz_dest );
102 /* *** Allocate descriptor *** */
103 p_sout = vlc_custom_create( p_parent, sizeof( *p_sout ), "stream output" );
110 msg_Dbg( p_sout, "using sout chain=`%s'", psz_chain );
112 /* *** init descriptor *** */
113 p_sout->psz_sout = strdup( psz_dest );
114 p_sout->i_out_pace_nocontrol = 0;
116 vlc_mutex_init( &p_sout->lock );
117 p_sout->p_stream = NULL;
119 var_Create( p_sout, "sout-mux-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
121 p_sout->p_stream = sout_StreamChainNew( p_sout, psz_chain, NULL, NULL );
122 if( p_sout->p_stream )
128 msg_Err( p_sout, "stream chain failed for `%s'", psz_chain );
131 FREENULL( p_sout->psz_sout );
133 vlc_mutex_destroy( &p_sout->lock );
134 vlc_object_release( p_sout );
138 /*****************************************************************************
139 * sout_DeleteInstance: delete a previously allocated instance
140 *****************************************************************************/
141 void sout_DeleteInstance( sout_instance_t * p_sout )
143 /* remove the stream out chain */
144 sout_StreamChainDelete( p_sout->p_stream, NULL );
146 /* *** free all string *** */
147 FREENULL( p_sout->psz_sout );
149 vlc_mutex_destroy( &p_sout->lock );
151 /* *** free structure *** */
152 vlc_object_release( p_sout );
155 /*****************************************************************************
157 *****************************************************************************/
158 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
161 sout_packetizer_input_t *p_input;
163 /* *** create a packetizer input *** */
164 p_input = malloc( sizeof( sout_packetizer_input_t ) );
165 if( !p_input ) return NULL;
166 p_input->p_sout = p_sout;
167 p_input->p_fmt = p_fmt;
169 msg_Dbg( p_sout, "adding a new sout input (sout_input:%p)", p_input );
171 if( p_fmt->i_codec == VLC_CODEC_NULL )
173 vlc_object_release( p_sout );
177 /* *** add it to the stream chain */
178 vlc_mutex_lock( &p_sout->lock );
179 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
180 vlc_mutex_unlock( &p_sout->lock );
182 if( p_input->id == NULL )
191 /*****************************************************************************
193 *****************************************************************************/
194 int sout_InputDelete( sout_packetizer_input_t *p_input )
196 sout_instance_t *p_sout = p_input->p_sout;
198 msg_Dbg( p_sout, "removing a sout input (sout_input:%p)", p_input );
200 if( p_input->p_fmt->i_codec != VLC_CODEC_NULL )
202 vlc_mutex_lock( &p_sout->lock );
203 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
204 vlc_mutex_unlock( &p_sout->lock );
209 return( VLC_SUCCESS);
212 /*****************************************************************************
214 *****************************************************************************/
215 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
218 sout_instance_t *p_sout = p_input->p_sout;
221 if( p_input->p_fmt->i_codec == VLC_CODEC_NULL )
223 block_Release( p_buffer );
227 vlc_mutex_lock( &p_sout->lock );
228 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
229 p_input->id, p_buffer );
230 vlc_mutex_unlock( &p_sout->lock );
235 #undef sout_AccessOutNew
236 /*****************************************************************************
237 * sout_AccessOutNew: allocate a new access out
238 *****************************************************************************/
239 sout_access_out_t *sout_AccessOutNew( vlc_object_t *p_sout,
240 const char *psz_access, const char *psz_name )
242 sout_access_out_t *p_access;
245 p_access = vlc_custom_create( p_sout, sizeof( *p_access ), "access out" );
249 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
252 p_access->psz_path = strdup( psz_name ? psz_name : "" );
253 p_access->p_sys = NULL;
254 p_access->pf_seek = NULL;
255 p_access->pf_read = NULL;
256 p_access->pf_write = NULL;
257 p_access->pf_control = NULL;
258 p_access->p_module = NULL;
261 module_need( p_access, "sout access", p_access->psz_access, true );
263 if( !p_access->p_module )
265 free( p_access->psz_access );
266 free( p_access->psz_path );
267 vlc_object_release( p_access );
273 /*****************************************************************************
274 * sout_AccessDelete: delete an access out
275 *****************************************************************************/
276 void sout_AccessOutDelete( sout_access_out_t *p_access )
278 if( p_access->p_module )
280 module_unneed( p_access, p_access->p_module );
282 free( p_access->psz_access );
284 config_ChainDestroy( p_access->p_cfg );
286 free( p_access->psz_path );
288 vlc_object_release( p_access );
291 /*****************************************************************************
293 *****************************************************************************/
294 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
296 return p_access->pf_seek( p_access, i_pos );
299 /*****************************************************************************
301 *****************************************************************************/
302 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
304 return( p_access->pf_read ?
305 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
308 /*****************************************************************************
310 *****************************************************************************/
311 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
313 return p_access->pf_write( p_access, p_buffer );
317 * sout_AccessOutControl
319 int sout_AccessOutControl (sout_access_out_t *access, int query, ...)
324 va_start (ap, query);
325 if (access->pf_control)
326 ret = access->pf_control (access, query, ap);
333 /*****************************************************************************
334 * sout_MuxNew: create a new mux
335 *****************************************************************************/
336 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, const char *psz_mux,
337 sout_access_out_t *p_access )
342 p_mux = vlc_custom_create( p_sout, sizeof( *p_mux ), "mux" );
346 p_mux->p_sout = p_sout;
347 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
350 p_mux->p_access = p_access;
351 p_mux->pf_control = NULL;
352 p_mux->pf_addstream = NULL;
353 p_mux->pf_delstream = NULL;
354 p_mux->pf_mux = NULL;
355 p_mux->i_nb_inputs = 0;
356 p_mux->pp_inputs = NULL;
359 p_mux->p_module = NULL;
361 p_mux->b_add_stream_any_time = false;
362 p_mux->b_waiting_stream = true;
363 p_mux->i_add_stream_start = -1;
366 module_need( p_mux, "sout mux", p_mux->psz_mux, true );
368 if( p_mux->p_module == NULL )
370 FREENULL( p_mux->psz_mux );
372 vlc_object_release( p_mux );
376 /* *** probe mux capacity *** */
377 if( p_mux->pf_control )
379 int b_answer = false;
381 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
389 msg_Dbg( p_sout, "muxer support adding stream at any time" );
390 p_mux->b_add_stream_any_time = true;
391 p_mux->b_waiting_stream = false;
393 /* If we control the output pace then it's better to wait before
394 * starting muxing (generates better streams/files). */
395 if( !p_sout->i_out_pace_nocontrol )
399 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
407 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
409 p_mux->b_waiting_stream = true;
417 /*****************************************************************************
419 *****************************************************************************/
420 void sout_MuxDelete( sout_mux_t *p_mux )
422 if( p_mux->p_module )
424 module_unneed( p_mux, p_mux->p_module );
426 free( p_mux->psz_mux );
428 config_ChainDestroy( p_mux->p_cfg );
430 vlc_object_release( p_mux );
433 /*****************************************************************************
435 *****************************************************************************/
436 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, const es_format_t *p_fmt )
438 sout_input_t *p_input;
440 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
442 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
443 "to this format). You can try increasing sout-mux-caching value" );
447 msg_Dbg( p_mux, "adding a new input" );
449 /* create a new sout input */
450 p_input = malloc( sizeof( sout_input_t ) );
454 // FIXME: remove either fmt or p_fmt...
455 es_format_Copy( &p_input->fmt, p_fmt );
456 p_input->p_fmt = &p_input->fmt;
458 p_input->p_fifo = block_FifoNew();
459 p_input->p_sys = NULL;
461 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
462 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
464 msg_Err( p_mux, "cannot add this stream" );
465 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
466 block_FifoRelease( p_input->p_fifo );
474 /*****************************************************************************
475 * sout_MuxDeleteStream:
476 *****************************************************************************/
477 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
481 if( p_mux->b_waiting_stream
482 && block_FifoCount( p_input->p_fifo ) > 0 )
484 /* We stop waiting, and call the muxer for taking care of the data
485 * before we remove this es */
486 p_mux->b_waiting_stream = false;
487 p_mux->pf_mux( p_mux );
490 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
493 p_mux->pf_delstream( p_mux, p_input );
495 /* remove the entry */
496 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
498 if( p_mux->i_nb_inputs == 0 )
500 msg_Warn( p_mux, "no more input streams for this mux" );
503 block_FifoRelease( p_input->p_fifo );
504 es_format_Clean( &p_input->fmt );
509 /*****************************************************************************
510 * sout_MuxSendBuffer:
511 *****************************************************************************/
512 int sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
515 mtime_t i_dts = p_buffer->i_dts;
516 block_FifoPut( p_input->p_fifo, p_buffer );
518 if( p_mux->p_sout->i_out_pace_nocontrol )
520 mtime_t current_date = mdate();
521 if ( current_date > i_dts )
522 msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
523 current_date - i_dts );
526 if( p_mux->b_waiting_stream )
528 const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * INT64_C(1000);
530 if( p_mux->i_add_stream_start < 0 )
531 p_mux->i_add_stream_start = i_dts;
533 /* Wait until we have enought data before muxing */
534 if( p_mux->i_add_stream_start < 0 ||
535 i_dts < p_mux->i_add_stream_start + i_caching )
537 p_mux->b_waiting_stream = false;
539 return p_mux->pf_mux( p_mux );
543 /*****************************************************************************
544 * sout_MuxGetStream: find stream to be muxed
545 *****************************************************************************/
546 int sout_MuxGetStream( sout_mux_t *p_mux, unsigned i_blocks, mtime_t *pi_dts )
551 for( int i = 0; i < p_mux->i_nb_inputs; i++ )
553 sout_input_t *p_input = p_mux->pp_inputs[i];
556 if( (!p_mux->b_add_stream_any_time) && block_FifoCount( p_input->p_fifo ) < i_blocks )
558 if( p_input->p_fmt->i_cat != SPU_ES )
562 /* FIXME: SPU muxing */
566 p_data = block_FifoShow( p_input->p_fifo );
567 if( i_stream < 0 || p_data->i_dts < i_dts )
570 i_dts = p_data->i_dts;
574 if( pi_dts ) *pi_dts = i_dts;
580 /*****************************************************************************
582 *****************************************************************************/
583 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
585 char * psz_dup = strdup( psz_mrl );
586 char * psz_parser = psz_dup;
587 const char * psz_access;
588 const char * psz_way;
591 /* *** first parse psz_dest */
592 while( *psz_parser && *psz_parser != ':' )
594 if( *psz_parser == '{' )
596 while( *psz_parser && *psz_parser != '}' )
610 #if defined( _WIN32 ) || defined( __OS2__ )
611 if( psz_parser - psz_dup == 1 )
613 /* msg_Warn( p_sout, "drive letter %c: found in source string",
621 psz_access = psz_way = "";
626 *psz_parser++ = '\0';
628 /* let's skip '//' */
629 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
634 psz_name = psz_parser ;
636 /* Come back to parse the access and mux plug-ins */
637 psz_parser = psz_dup;
644 else if( *psz_parser == '/' )
652 psz_access = psz_parser;
654 while( *psz_parser && *psz_parser != '/' )
656 if( *psz_parser == '{' )
658 while( *psz_parser && *psz_parser != '}' )
673 if( *psz_parser == '/' )
675 *psz_parser++ = '\0';
686 psz_way = psz_parser;
690 p_mrl->psz_access = strdup( psz_access );
691 p_mrl->psz_way = strdup( psz_way );
692 p_mrl->psz_name = strdup( psz_name );
695 return( VLC_SUCCESS );
699 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
700 static void mrl_Clean( mrl_t *p_mrl )
702 FREENULL( p_mrl->psz_access );
703 FREENULL( p_mrl->psz_way );
704 FREENULL( p_mrl->psz_name );
708 /****************************************************************************
709 ****************************************************************************
713 ****************************************************************************
714 ****************************************************************************/
716 /* Destroy a "stream_out" module */
717 static void sout_StreamDelete( sout_stream_t *p_stream )
719 sout_instance_t *p_sout = (sout_instance_t *)(p_stream->p_parent);
721 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
723 p_sout->i_out_pace_nocontrol -= p_stream->pace_nocontrol;
725 if( p_stream->p_module != NULL )
726 module_unneed( p_stream, p_stream->p_module );
728 FREENULL( p_stream->psz_name );
730 config_ChainDestroy( p_stream->p_cfg );
732 msg_Dbg( p_stream, "destroying chain done" );
733 vlc_object_release( p_stream );
736 /* Destroy a "stream_out" modules chain
738 * p_first is the first module to be destroyed in the chain
739 * p_last is the last module to be destroyed
740 * if NULL, all modules are destroyed
741 * if not NULL, modules following it must be destroyed separately
743 void sout_StreamChainDelete(sout_stream_t *p_first, sout_stream_t *p_last)
745 while(p_first != NULL)
747 sout_stream_t *p_next = p_first->p_next;
749 sout_StreamDelete(p_first);
750 if(p_first == p_last)
756 /* Create a "stream_out" module, which may forward its ES to p_next module */
758 * XXX name and p_cfg are used (-> do NOT free them)
760 static sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_name,
761 config_chain_t *p_cfg, sout_stream_t *p_next)
763 sout_stream_t *p_stream;
767 p_stream = vlc_custom_create( p_sout, sizeof( *p_stream ), "stream out" );
771 p_stream->p_sout = p_sout;
772 p_stream->psz_name = psz_name;
773 p_stream->p_cfg = p_cfg;
774 p_stream->p_next = p_next;
775 p_stream->pace_nocontrol = false;
776 p_stream->p_sys = NULL;
778 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
781 module_need( p_stream, "sout stream", p_stream->psz_name, true );
783 if( !p_stream->p_module )
785 /* those must be freed by the caller if creation failed */
786 p_stream->psz_name = NULL;
787 p_stream->p_cfg = NULL;
789 sout_StreamDelete( p_stream );
793 p_sout->i_out_pace_nocontrol += p_stream->pace_nocontrol;
797 /* Creates a complete "stream_out" modules chain
799 * chain format: module1{option=*:option=*}[:module2{option=*:...}]
801 * The modules are created starting from the last one and linked together
802 * A pointer to the last module created is stored if pp_last isn't NULL, to
803 * make sure sout_StreamChainDelete doesn't delete modules created in another
806 * Returns a pointer to the first module.
808 sout_stream_t *sout_StreamChainNew(sout_instance_t *p_sout, char *psz_chain,
809 sout_stream_t *p_next, sout_stream_t **pp_last)
811 if(!psz_chain || !*psz_chain)
813 if(pp_last) *pp_last = NULL;
817 char *psz_parser = strdup(psz_chain);
821 vlc_array_t cfg, name;
822 vlc_array_init(&cfg);
823 vlc_array_init(&name);
828 config_chain_t *p_cfg;
830 psz_chain = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
832 psz_parser = psz_chain;
834 vlc_array_append(&cfg, p_cfg);
835 vlc_array_append(&name, psz_name);
838 int i = vlc_array_count(&name);
840 vlc_array_init(&module);
843 p_next = sout_StreamNew( p_sout, vlc_array_item_at_index(&name, i),
844 vlc_array_item_at_index(&cfg, i), p_next);
849 if(i == vlc_array_count(&name) - 1 && pp_last)
850 *pp_last = p_next; /* last module created in the chain */
852 vlc_array_append(&module, p_next);
855 vlc_array_clear(&name);
856 vlc_array_clear(&cfg);
857 vlc_array_clear(&module);
863 i++; /* last module couldn't be created */
865 /* destroy all modules created, starting with the last one */
866 int modules = vlc_array_count(&module);
868 sout_StreamDelete(vlc_array_item_at_index(&module, modules));
869 vlc_array_clear(&module);
871 /* then destroy all names and config which weren't destroyed by
872 * sout_StreamDelete */
875 free(vlc_array_item_at_index(&name, i));
876 config_ChainDestroy(vlc_array_item_at_index(&cfg, i));
878 vlc_array_clear(&name);
879 vlc_array_clear(&cfg);
884 static char *sout_stream_url_to_chain( bool b_sout_display,
885 const char *psz_url )
890 mrl_Parse( &mrl, psz_url );
892 /* Check if the URLs goes to #rtp - otherwise we'll use #standard */
893 static const char rtplist[] = "dccp\0sctp\0tcp\0udplite\0";
894 for (const char *a = rtplist; *a; a += strlen (a) + 1)
895 if (strcmp (a, mrl.psz_access) == 0)
898 if (strcmp (mrl.psz_access, "rtp") == 0)
901 /* For historical reasons, rtp:// means RTP over UDP */
902 strcpy (mrl.psz_access, "udp");
904 if (mrl.psz_name[0] == '[')
906 port = strstr (mrl.psz_name, "]:");
911 port = strchr (mrl.psz_name, ':');
913 *port++ = '\0'; /* erase ':' */
915 if (asprintf (&psz_chain,
916 "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s%s%s\"}",
917 mrl.psz_way, mrl.psz_access, mrl.psz_name,
918 port ? "\",port=\"" : "", port ? port : "") == -1)
923 /* Convert the URL to a basic standard sout chain */
924 if (asprintf (&psz_chain,
925 "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
926 mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
930 /* Duplicate and wrap if sout-display is on */
931 if (psz_chain && b_sout_display)
934 if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", psz_chain) == -1)
944 #undef sout_EncoderCreate
945 encoder_t *sout_EncoderCreate( vlc_object_t *p_this )
947 return vlc_custom_create( p_this, sizeof( encoder_t ), "encoder" );