1 /*****************************************************************************
2 * stream_output.c : stream output module
3 *****************************************************************************
4 * Copyright (C) 2002-2007 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 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <stdlib.h> /* free() */
37 #include <stdio.h> /* sprintf() */
42 #include "stream_output.h"
46 #include "input/input_internal.h"
49 /*****************************************************************************
51 *****************************************************************************/
52 #define sout_stream_url_to_chain( p, s ) \
53 _sout_stream_url_to_chain( VLC_OBJECT(p), s )
54 static char *_sout_stream_url_to_chain( vlc_object_t *, const char * );
68 /* mrl_Parse: parse psz_mrl and fill p_mrl */
69 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl );
70 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
71 static void mrl_Clean( mrl_t *p_mrl );
73 /*****************************************************************************
74 * sout_NewInstance: creates a new stream output instance
75 *****************************************************************************/
76 sout_instance_t *__sout_NewInstance( vlc_object_t *p_parent, const char *psz_dest )
78 static const char typename[] = "stream output";
79 sout_instance_t *p_sout;
81 /* *** Allocate descriptor *** */
82 p_sout = vlc_custom_create( p_parent, sizeof( *p_sout ),
83 VLC_OBJECT_GENERIC, typename );
87 /* *** init descriptor *** */
88 p_sout->psz_sout = strdup( psz_dest );
89 p_sout->p_meta = NULL;
90 p_sout->i_out_pace_nocontrol = 0;
93 vlc_mutex_init( &p_sout->lock );
94 if( psz_dest && psz_dest[0] == '#' )
96 p_sout->psz_chain = strdup( &psz_dest[1] );
100 p_sout->psz_chain = sout_stream_url_to_chain( p_sout, psz_dest );
101 msg_Dbg( p_sout, "using sout chain=`%s'", p_sout->psz_chain );
103 p_sout->p_stream = NULL;
105 /* attach it for inherit */
106 vlc_object_attach( p_sout, p_parent );
109 var_Create( p_sout, "sout-mux-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
112 p_sout->p_stream = sout_StreamNew( p_sout, p_sout->psz_chain );
113 if( p_sout->p_stream == NULL )
115 msg_Err( p_sout, "stream chain failed for `%s'", p_sout->psz_chain );
117 FREENULL( p_sout->psz_sout );
118 FREENULL( p_sout->psz_chain );
120 vlc_object_detach( p_sout );
121 vlc_object_release( p_sout );
128 /*****************************************************************************
129 * sout_DeleteInstance: delete a previously allocated instance
130 *****************************************************************************/
131 void sout_DeleteInstance( sout_instance_t * p_sout )
133 /* remove the stream out chain */
134 sout_StreamDelete( p_sout->p_stream );
136 /* *** free all string *** */
137 FREENULL( p_sout->psz_sout );
138 FREENULL( p_sout->psz_chain );
143 vlc_meta_Delete( p_sout->p_meta );
146 vlc_mutex_destroy( &p_sout->lock );
148 /* *** free structure *** */
149 vlc_object_release( p_sout );
152 /*****************************************************************************
154 *****************************************************************************/
155 void sout_UpdateStatistic( sout_instance_t *p_sout, sout_statistic_t i_type, int i_delta )
157 input_thread_t *p_input;
158 int i_bytes; /* That's pretty stupid to define it as an integer, it will overflow
161 if( !libvlc_stats (p_sout) )
165 * TODO add a private (ie not VLC_EXPORTed) input_UpdateStatistic for that */
166 p_input = vlc_object_find( p_sout, VLC_OBJECT_INPUT, FIND_PARENT );
167 if( !p_input || p_input->i_state == INIT_S || p_input->i_state == ERROR_S )
172 #define I(c) stats_UpdateInteger( p_input, p_input->p->counters.c, i_delta, NULL )
173 case SOUT_STATISTIC_DECODED_VIDEO:
176 case SOUT_STATISTIC_DECODED_AUDIO:
179 case SOUT_STATISTIC_DECODED_SUBTITLE:
183 case SOUT_STATISTIC_ENCODED_VIDEO:
184 case SOUT_STATISTIC_ENCODED_AUDIO:
185 case SOUT_STATISTIC_ENCODED_SUBTITLE:
186 msg_Warn( p_sout, "Not yet supported statistic type %d", i_type );
190 case SOUT_STATISTIC_SENT_PACKET:
191 I(p_sout_sent_packets);
194 case SOUT_STATISTIC_SENT_BYTE:
195 if( !stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_bytes, i_delta, &i_bytes ) )
196 stats_UpdateFloat( p_input, p_input->p->counters.p_sout_send_bitrate, i_bytes, NULL );
200 msg_Err( p_sout, "Invalid statistic type %d (internal error)", i_type );
203 vlc_object_release( p_input );
205 /*****************************************************************************
207 *****************************************************************************/
208 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
211 sout_packetizer_input_t *p_input;
213 /* *** create a packetizer input *** */
214 p_input = malloc( sizeof( sout_packetizer_input_t ) );
215 if( !p_input ) return NULL;
216 p_input->p_sout = p_sout;
217 p_input->p_fmt = p_fmt;
219 msg_Dbg( p_sout, "adding a new sout input (sout_input:%p)", p_input );
221 if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
223 vlc_object_release( p_sout );
227 /* *** add it to the stream chain */
228 vlc_mutex_lock( &p_sout->lock );
229 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
230 vlc_mutex_unlock( &p_sout->lock );
232 if( p_input->id == NULL )
241 /*****************************************************************************
243 *****************************************************************************/
244 int sout_InputDelete( sout_packetizer_input_t *p_input )
246 sout_instance_t *p_sout = p_input->p_sout;
248 msg_Dbg( p_sout, "removing a sout input (sout_input:%p)", p_input );
250 if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
252 vlc_mutex_lock( &p_sout->lock );
253 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
254 vlc_mutex_unlock( &p_sout->lock );
259 return( VLC_SUCCESS);
262 /*****************************************************************************
264 *****************************************************************************/
265 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
268 sout_instance_t *p_sout = p_input->p_sout;
271 if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
273 block_Release( p_buffer );
276 if( p_buffer->i_dts <= 0 )
278 msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
279 block_Release( p_buffer );
283 vlc_mutex_lock( &p_sout->lock );
284 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
285 p_input->id, p_buffer );
286 vlc_mutex_unlock( &p_sout->lock );
291 /*****************************************************************************
292 * sout_AccessOutNew: allocate a new access out
293 *****************************************************************************/
294 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
295 const char *psz_access, const char *psz_name )
297 static const char typename[] = "access out";
298 sout_access_out_t *p_access;
301 p_access = vlc_custom_create( p_sout, sizeof( *p_access ),
302 VLC_OBJECT_GENERIC, typename );
306 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
309 p_access->psz_path = strdup( psz_name ? psz_name : "" );
310 p_access->p_sout = p_sout;
311 p_access->p_sys = NULL;
312 p_access->pf_seek = NULL;
313 p_access->pf_read = NULL;
314 p_access->pf_write = NULL;
315 p_access->pf_control = NULL;
316 p_access->p_module = NULL;
318 p_access->i_writes = 0;
319 p_access->i_sent_bytes = 0;
321 vlc_object_attach( p_access, p_sout );
324 module_Need( p_access, "sout access", p_access->psz_access, true );
326 if( !p_access->p_module )
328 free( p_access->psz_access );
329 free( p_access->psz_path );
330 vlc_object_detach( p_access );
331 vlc_object_release( p_access );
337 /*****************************************************************************
338 * sout_AccessDelete: delete an access out
339 *****************************************************************************/
340 void sout_AccessOutDelete( sout_access_out_t *p_access )
342 vlc_object_detach( p_access );
343 if( p_access->p_module )
345 module_Unneed( p_access, p_access->p_module );
347 free( p_access->psz_access );
349 config_ChainDestroy( p_access->p_cfg );
351 free( p_access->psz_path );
353 vlc_object_release( p_access );
356 /*****************************************************************************
358 *****************************************************************************/
359 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
361 return p_access->pf_seek( p_access, i_pos );
364 /*****************************************************************************
366 *****************************************************************************/
367 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
369 return( p_access->pf_read ?
370 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
373 /*****************************************************************************
375 *****************************************************************************/
376 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
378 const unsigned i_packets_gather = 30;
379 p_access->i_writes++;
380 p_access->i_sent_bytes += p_buffer->i_buffer;
381 if( (p_access->i_writes % i_packets_gather) == 0 )
383 sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_PACKET, i_packets_gather );
384 sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_BYTE, p_access->i_sent_bytes );
385 p_access->i_sent_bytes = 0;
387 return p_access->pf_write( p_access, p_buffer );
391 * sout_AccessOutControl
393 int sout_AccessOutControl (sout_access_out_t *access, int query, ...)
398 va_start (ap, query);
399 if (access->pf_control)
400 ret = access->pf_control (access, query, ap);
407 /*****************************************************************************
408 * sout_MuxNew: create a new mux
409 *****************************************************************************/
410 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
411 sout_access_out_t *p_access )
413 static const char typename[] = "mux";
417 p_mux = vlc_custom_create( p_sout, sizeof( *p_mux ), VLC_OBJECT_GENERIC,
422 p_mux->p_sout = p_sout;
423 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
426 p_mux->p_access = p_access;
427 p_mux->pf_control = NULL;
428 p_mux->pf_addstream = NULL;
429 p_mux->pf_delstream = NULL;
430 p_mux->pf_mux = NULL;
431 p_mux->i_nb_inputs = 0;
432 p_mux->pp_inputs = NULL;
435 p_mux->p_module = NULL;
437 p_mux->b_add_stream_any_time = false;
438 p_mux->b_waiting_stream = true;
439 p_mux->i_add_stream_start = -1;
441 vlc_object_attach( p_mux, p_sout );
444 module_Need( p_mux, "sout mux", p_mux->psz_mux, true );
446 if( p_mux->p_module == NULL )
448 FREENULL( p_mux->psz_mux );
450 vlc_object_detach( p_mux );
451 vlc_object_release( p_mux );
455 /* *** probe mux capacity *** */
456 if( p_mux->pf_control )
458 int b_answer = false;
460 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
468 msg_Dbg( p_sout, "muxer support adding stream at any time" );
469 p_mux->b_add_stream_any_time = true;
470 p_mux->b_waiting_stream = false;
472 /* If we control the output pace then it's better to wait before
473 * starting muxing (generates better streams/files). */
474 if( !p_sout->i_out_pace_nocontrol )
478 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
486 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
488 p_mux->b_waiting_stream = true;
496 /*****************************************************************************
498 *****************************************************************************/
499 void sout_MuxDelete( sout_mux_t *p_mux )
501 vlc_object_detach( p_mux );
502 if( p_mux->p_module )
504 module_Unneed( p_mux, p_mux->p_module );
506 free( p_mux->psz_mux );
508 config_ChainDestroy( p_mux->p_cfg );
510 vlc_object_release( p_mux );
513 /*****************************************************************************
515 *****************************************************************************/
516 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
518 sout_input_t *p_input;
520 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
522 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
523 "to this format). You can try increasing sout-mux-caching value" );
527 msg_Dbg( p_mux, "adding a new input" );
529 /* create a new sout input */
530 p_input = malloc( sizeof( sout_input_t ) );
533 p_input->p_sout = p_mux->p_sout;
534 p_input->p_fmt = p_fmt;
535 p_input->p_fifo = block_FifoNew();
536 p_input->p_sys = NULL;
538 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
539 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
541 msg_Err( p_mux, "cannot add this stream" );
542 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
543 block_FifoRelease( p_input->p_fifo );
551 /*****************************************************************************
552 * sout_MuxDeleteStream:
553 *****************************************************************************/
554 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
558 if( p_mux->b_waiting_stream
559 && block_FifoCount( p_input->p_fifo ) > 0 )
561 /* We stop waiting, and call the muxer for taking care of the data
562 * before we remove this es */
563 p_mux->b_waiting_stream = false;
564 p_mux->pf_mux( p_mux );
567 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
570 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
572 msg_Err( p_mux, "cannot delete this stream from mux" );
575 /* remove the entry */
576 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
578 if( p_mux->i_nb_inputs == 0 )
580 msg_Warn( p_mux, "no more input streams for this mux" );
583 block_FifoRelease( p_input->p_fifo );
588 /*****************************************************************************
589 * sout_MuxSendBuffer:
590 *****************************************************************************/
591 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
594 block_FifoPut( p_input->p_fifo, p_buffer );
596 if( p_mux->p_sout->i_out_pace_nocontrol )
598 mtime_t current_date = mdate();
599 if ( current_date > p_buffer->i_dts )
600 msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
601 current_date - p_buffer->i_dts );
604 if( p_mux->b_waiting_stream )
606 const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * INT64_C(1000);
608 if( p_mux->i_add_stream_start < 0 )
609 p_mux->i_add_stream_start = p_buffer->i_dts;
611 /* Wait until we have enought data before muxing */
612 if( p_mux->i_add_stream_start < 0 ||
613 p_buffer->i_dts < p_mux->i_add_stream_start + i_caching )
615 p_mux->b_waiting_stream = false;
617 p_mux->pf_mux( p_mux );
620 /*****************************************************************************
622 *****************************************************************************/
623 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
625 char * psz_dup = strdup( psz_mrl );
626 char * psz_parser = psz_dup;
627 const char * psz_access;
628 const char * psz_way;
631 /* *** first parse psz_dest */
632 while( *psz_parser && *psz_parser != ':' )
634 if( *psz_parser == '{' )
636 while( *psz_parser && *psz_parser != '}' )
650 #if defined( WIN32 ) || defined( UNDER_CE )
651 if( psz_parser - psz_dup == 1 )
653 /* msg_Warn( p_sout, "drive letter %c: found in source string",
661 psz_access = psz_way = "";
666 *psz_parser++ = '\0';
668 /* let's skip '//' */
669 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
674 psz_name = psz_parser ;
676 /* Come back to parse the access and mux plug-ins */
677 psz_parser = psz_dup;
684 else if( *psz_parser == '/' )
692 psz_access = psz_parser;
694 while( *psz_parser && *psz_parser != '/' )
696 if( *psz_parser == '{' )
698 while( *psz_parser && *psz_parser != '}' )
713 if( *psz_parser == '/' )
715 *psz_parser++ = '\0';
726 psz_way = psz_parser;
730 p_mrl->psz_access = strdup( psz_access );
731 p_mrl->psz_way = strdup( psz_way );
732 p_mrl->psz_name = strdup( psz_name );
735 return( VLC_SUCCESS );
739 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
740 static void mrl_Clean( mrl_t *p_mrl )
742 FREENULL( p_mrl->psz_access );
743 FREENULL( p_mrl->psz_way );
744 FREENULL( p_mrl->psz_name );
748 /****************************************************************************
749 ****************************************************************************
753 ****************************************************************************
754 ****************************************************************************/
756 /* create a complete chain */
758 module{option=*:option=*}[:module{option=*:...}]
762 * parse module{options=str, option="str "}:
763 * return a pointer on the rest
764 * XXX: psz_chain is modified
768 * XXX name and p_cfg are used (-> do NOT free them)
770 sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_chain )
772 static const char typename[] = "stream out";
773 sout_stream_t *p_stream;
777 msg_Err( p_sout, "invalid chain" );
781 p_stream = vlc_custom_create( p_sout, sizeof( *p_stream ),
782 VLC_OBJECT_GENERIC, typename );
786 p_stream->p_sout = p_sout;
787 p_stream->p_sys = NULL;
790 config_ChainCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
792 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
794 vlc_object_attach( p_stream, p_sout );
797 module_Need( p_stream, "sout stream", p_stream->psz_name, true );
799 if( !p_stream->p_module )
801 sout_StreamDelete( p_stream );
808 void sout_StreamDelete( sout_stream_t *p_stream )
810 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
812 vlc_object_detach( p_stream );
813 if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
815 FREENULL( p_stream->psz_name );
816 FREENULL( p_stream->psz_next );
818 config_ChainDestroy( p_stream->p_cfg );
820 msg_Dbg( p_stream, "destroying chain done" );
821 vlc_object_release( p_stream );
824 static char *_sout_stream_url_to_chain( vlc_object_t *p_this,
825 const char *psz_url )
830 mrl_Parse( &mrl, psz_url );
832 /* Check if the URLs goes to #rtp - otherwise we'll use #standard */
833 static const char rtplist[] = "dccp\0sctp\0tcp\0udplite\0";
834 for (const char *a = rtplist; *a; a += strlen (a) + 1)
835 if (strcmp (a, mrl.psz_access) == 0)
838 if (strcmp (mrl.psz_access, "rtp") == 0)
841 /* For historical reasons, rtp:// means RTP over UDP */
842 strcpy (mrl.psz_access, "udp");
844 if (mrl.psz_name[0] == '[')
846 port = strstr (mrl.psz_name, "]:");
851 port = strchr (mrl.psz_name, ':');
853 *port++ = '\0'; /* erase ':' */
855 if (asprintf (&psz_chain,
856 "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s%s%s\"}",
857 mrl.psz_way, mrl.psz_access, mrl.psz_name,
858 port ? "\",port=\"" : "", port ? port : "") == -1)
863 /* Convert the URL to a basic standard sout chain */
864 if (asprintf (&psz_chain,
865 "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
866 mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
870 /* Duplicate and wrap if sout-display is on */
871 if (psz_chain && (config_GetInt( p_this, "sout-display" ) > 0))
874 if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", tmp) == -1)