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"
45 #include <vlc_block.h>
46 #include <vlc_codec.h>
48 #include "input/input_interface.h"
50 #define VLC_CODEC_NULL VLC_FOURCC( 'n', 'u', 'l', 'l' )
53 /*****************************************************************************
55 *****************************************************************************/
56 #define sout_stream_url_to_chain( p, s ) \
57 _sout_stream_url_to_chain( VLC_OBJECT(p), s )
58 static char *_sout_stream_url_to_chain( vlc_object_t *, const char * );
72 /* mrl_Parse: parse psz_mrl and fill p_mrl */
73 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl );
74 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
75 static void mrl_Clean( mrl_t *p_mrl );
77 /*****************************************************************************
78 * sout_NewInstance: creates a new stream output instance
79 *****************************************************************************/
80 sout_instance_t *__sout_NewInstance( vlc_object_t *p_parent, const char *psz_dest )
82 static const char typename[] = "stream output";
83 sout_instance_t *p_sout;
85 /* *** Allocate descriptor *** */
86 p_sout = vlc_custom_create( p_parent, sizeof( *p_sout ),
87 VLC_OBJECT_GENERIC, typename );
91 /* *** init descriptor *** */
92 p_sout->psz_sout = strdup( psz_dest );
93 p_sout->p_meta = NULL;
94 p_sout->i_out_pace_nocontrol = 0;
97 vlc_mutex_init( &p_sout->lock );
98 if( psz_dest && psz_dest[0] == '#' )
100 p_sout->psz_chain = strdup( &psz_dest[1] );
104 p_sout->psz_chain = sout_stream_url_to_chain( p_sout, psz_dest );
105 msg_Dbg( p_sout, "using sout chain=`%s'", p_sout->psz_chain );
107 p_sout->p_stream = NULL;
109 /* attach it for inherit */
110 vlc_object_attach( p_sout, p_parent );
113 var_Create( p_sout, "sout-mux-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
116 p_sout->p_stream = sout_StreamNew( p_sout, p_sout->psz_chain );
117 if( p_sout->p_stream == NULL )
119 msg_Err( p_sout, "stream chain failed for `%s'", p_sout->psz_chain );
121 FREENULL( p_sout->psz_sout );
122 FREENULL( p_sout->psz_chain );
124 vlc_object_detach( p_sout );
125 vlc_object_release( p_sout );
132 /*****************************************************************************
133 * sout_DeleteInstance: delete a previously allocated instance
134 *****************************************************************************/
135 void sout_DeleteInstance( sout_instance_t * p_sout )
137 /* remove the stream out chain */
138 sout_StreamDelete( p_sout->p_stream );
140 /* *** free all string *** */
141 FREENULL( p_sout->psz_sout );
142 FREENULL( p_sout->psz_chain );
147 vlc_meta_Delete( p_sout->p_meta );
150 vlc_mutex_destroy( &p_sout->lock );
152 /* *** free structure *** */
153 vlc_object_release( p_sout );
156 /*****************************************************************************
158 *****************************************************************************/
159 void sout_UpdateStatistic( sout_instance_t *p_sout, sout_statistic_t i_type, int i_delta )
161 if( !libvlc_stats( p_sout ) )
165 input_thread_t *p_input = vlc_object_find( p_sout, VLC_OBJECT_INPUT, FIND_PARENT );
172 case SOUT_STATISTIC_DECODED_VIDEO:
173 i_input_type = SOUT_STATISTIC_DECODED_VIDEO;
175 case SOUT_STATISTIC_DECODED_AUDIO:
176 i_input_type = SOUT_STATISTIC_DECODED_AUDIO;
178 case SOUT_STATISTIC_DECODED_SUBTITLE:
179 i_input_type = SOUT_STATISTIC_DECODED_SUBTITLE;
182 case SOUT_STATISTIC_SENT_PACKET:
183 i_input_type = SOUT_STATISTIC_SENT_PACKET;
186 case SOUT_STATISTIC_SENT_BYTE:
187 i_input_type = SOUT_STATISTIC_SENT_BYTE;
191 msg_Err( p_sout, "Not yet supported statistic type %d", i_type );
192 vlc_object_release( p_input );
196 input_UpdateStatistic( p_input, i_input_type, i_delta );
198 vlc_object_release( p_input );
200 /*****************************************************************************
202 *****************************************************************************/
203 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
206 sout_packetizer_input_t *p_input;
208 /* *** create a packetizer input *** */
209 p_input = malloc( sizeof( sout_packetizer_input_t ) );
210 if( !p_input ) return NULL;
211 p_input->p_sout = p_sout;
212 p_input->p_fmt = p_fmt;
214 msg_Dbg( p_sout, "adding a new sout input (sout_input:%p)", p_input );
216 if( p_fmt->i_codec == VLC_CODEC_NULL )
218 vlc_object_release( p_sout );
222 /* *** add it to the stream chain */
223 vlc_mutex_lock( &p_sout->lock );
224 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
225 vlc_mutex_unlock( &p_sout->lock );
227 if( p_input->id == NULL )
236 /*****************************************************************************
238 *****************************************************************************/
239 int sout_InputDelete( sout_packetizer_input_t *p_input )
241 sout_instance_t *p_sout = p_input->p_sout;
243 msg_Dbg( p_sout, "removing a sout input (sout_input:%p)", p_input );
245 if( p_input->p_fmt->i_codec != VLC_CODEC_NULL )
247 vlc_mutex_lock( &p_sout->lock );
248 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
249 vlc_mutex_unlock( &p_sout->lock );
254 return( VLC_SUCCESS);
257 /*****************************************************************************
259 *****************************************************************************/
260 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
263 sout_instance_t *p_sout = p_input->p_sout;
266 if( p_input->p_fmt->i_codec == VLC_CODEC_NULL )
268 block_Release( p_buffer );
271 if( p_buffer->i_dts <= 0 )
273 msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
274 block_Release( p_buffer );
278 vlc_mutex_lock( &p_sout->lock );
279 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
280 p_input->id, p_buffer );
281 vlc_mutex_unlock( &p_sout->lock );
286 #undef sout_AccessOutNew
287 /*****************************************************************************
288 * sout_AccessOutNew: allocate a new access out
289 *****************************************************************************/
290 sout_access_out_t *sout_AccessOutNew( vlc_object_t *p_sout,
291 const char *psz_access, const char *psz_name )
293 static const char typename[] = "access out";
294 sout_access_out_t *p_access;
297 p_access = vlc_custom_create( p_sout, sizeof( *p_access ),
298 VLC_OBJECT_GENERIC, typename );
302 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
305 p_access->psz_path = strdup( psz_name ? psz_name : "" );
306 p_access->p_sys = NULL;
307 p_access->pf_seek = NULL;
308 p_access->pf_read = NULL;
309 p_access->pf_write = NULL;
310 p_access->pf_control = NULL;
311 p_access->p_module = NULL;
313 p_access->i_writes = 0;
314 p_access->i_sent_bytes = 0;
316 vlc_object_set_name( p_access, p_access->psz_access );
317 vlc_object_attach( p_access, p_sout );
320 module_need( p_access, "sout access", p_access->psz_access, true );
322 if( !p_access->p_module )
324 free( p_access->psz_access );
325 free( p_access->psz_path );
326 vlc_object_detach( p_access );
327 vlc_object_release( p_access );
333 /*****************************************************************************
334 * sout_AccessDelete: delete an access out
335 *****************************************************************************/
336 void sout_AccessOutDelete( sout_access_out_t *p_access )
338 vlc_object_detach( p_access );
339 if( p_access->p_module )
341 module_unneed( p_access, p_access->p_module );
343 free( p_access->psz_access );
345 config_ChainDestroy( p_access->p_cfg );
347 free( p_access->psz_path );
349 vlc_object_release( p_access );
352 /*****************************************************************************
354 *****************************************************************************/
355 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
357 return p_access->pf_seek( p_access, i_pos );
360 /*****************************************************************************
362 *****************************************************************************/
363 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
365 return( p_access->pf_read ?
366 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
369 /*****************************************************************************
371 *****************************************************************************/
372 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
375 const unsigned i_packets_gather = 30;
376 p_access->i_writes++;
377 p_access->i_sent_bytes += p_buffer->i_buffer;
378 if( (p_access->i_writes % i_packets_gather) == 0 )
380 sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_PACKET, i_packets_gather );
381 sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_BYTE, p_access->i_sent_bytes );
382 p_access->i_sent_bytes = 0;
385 return p_access->pf_write( p_access, p_buffer );
389 * sout_AccessOutControl
391 int sout_AccessOutControl (sout_access_out_t *access, int query, ...)
396 va_start (ap, query);
397 if (access->pf_control)
398 ret = access->pf_control (access, query, ap);
405 /*****************************************************************************
406 * sout_MuxNew: create a new mux
407 *****************************************************************************/
408 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
409 sout_access_out_t *p_access )
411 static const char typename[] = "mux";
415 p_mux = vlc_custom_create( p_sout, sizeof( *p_mux ), VLC_OBJECT_GENERIC,
420 p_mux->p_sout = p_sout;
421 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
424 p_mux->p_access = p_access;
425 p_mux->pf_control = NULL;
426 p_mux->pf_addstream = NULL;
427 p_mux->pf_delstream = NULL;
428 p_mux->pf_mux = NULL;
429 p_mux->i_nb_inputs = 0;
430 p_mux->pp_inputs = NULL;
433 p_mux->p_module = NULL;
435 p_mux->b_add_stream_any_time = false;
436 p_mux->b_waiting_stream = true;
437 p_mux->i_add_stream_start = -1;
439 vlc_object_set_name( p_mux, p_mux->psz_mux );
440 vlc_object_attach( p_mux, p_sout );
443 module_need( p_mux, "sout mux", p_mux->psz_mux, true );
445 if( p_mux->p_module == NULL )
447 FREENULL( p_mux->psz_mux );
449 vlc_object_detach( p_mux );
450 vlc_object_release( p_mux );
454 /* *** probe mux capacity *** */
455 if( p_mux->pf_control )
457 int b_answer = false;
459 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
467 msg_Dbg( p_sout, "muxer support adding stream at any time" );
468 p_mux->b_add_stream_any_time = true;
469 p_mux->b_waiting_stream = false;
471 /* If we control the output pace then it's better to wait before
472 * starting muxing (generates better streams/files). */
473 if( !p_sout->i_out_pace_nocontrol )
477 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
485 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
487 p_mux->b_waiting_stream = true;
495 /*****************************************************************************
497 *****************************************************************************/
498 void sout_MuxDelete( sout_mux_t *p_mux )
500 vlc_object_detach( p_mux );
501 if( p_mux->p_module )
503 module_unneed( p_mux, p_mux->p_module );
505 free( p_mux->psz_mux );
507 config_ChainDestroy( p_mux->p_cfg );
509 vlc_object_release( p_mux );
512 /*****************************************************************************
514 *****************************************************************************/
515 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
517 sout_input_t *p_input;
519 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
521 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
522 "to this format). You can try increasing sout-mux-caching value" );
526 msg_Dbg( p_mux, "adding a new input" );
528 /* create a new sout input */
529 p_input = malloc( sizeof( sout_input_t ) );
532 p_input->p_sout = p_mux->p_sout;
533 p_input->p_fmt = p_fmt;
534 p_input->p_fifo = block_FifoNew();
535 p_input->p_sys = NULL;
537 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
538 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
540 msg_Err( p_mux, "cannot add this stream" );
541 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
542 block_FifoRelease( p_input->p_fifo );
550 /*****************************************************************************
551 * sout_MuxDeleteStream:
552 *****************************************************************************/
553 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
557 if( p_mux->b_waiting_stream
558 && block_FifoCount( p_input->p_fifo ) > 0 )
560 /* We stop waiting, and call the muxer for taking care of the data
561 * before we remove this es */
562 p_mux->b_waiting_stream = false;
563 p_mux->pf_mux( p_mux );
566 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
569 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
571 msg_Err( p_mux, "cannot delete this stream from mux" );
574 /* remove the entry */
575 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
577 if( p_mux->i_nb_inputs == 0 )
579 msg_Warn( p_mux, "no more input streams for this mux" );
582 block_FifoRelease( p_input->p_fifo );
587 /*****************************************************************************
588 * sout_MuxSendBuffer:
589 *****************************************************************************/
590 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
593 block_FifoPut( p_input->p_fifo, p_buffer );
595 if( p_mux->p_sout->i_out_pace_nocontrol )
597 mtime_t current_date = mdate();
598 if ( current_date > p_buffer->i_dts )
599 msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
600 current_date - p_buffer->i_dts );
603 if( p_mux->b_waiting_stream )
605 const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * INT64_C(1000);
607 if( p_mux->i_add_stream_start < 0 )
608 p_mux->i_add_stream_start = p_buffer->i_dts;
610 /* Wait until we have enought data before muxing */
611 if( p_mux->i_add_stream_start < 0 ||
612 p_buffer->i_dts < p_mux->i_add_stream_start + i_caching )
614 p_mux->b_waiting_stream = false;
616 p_mux->pf_mux( p_mux );
619 /*****************************************************************************
621 *****************************************************************************/
622 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
624 char * psz_dup = strdup( psz_mrl );
625 char * psz_parser = psz_dup;
626 const char * psz_access;
627 const char * psz_way;
630 /* *** first parse psz_dest */
631 while( *psz_parser && *psz_parser != ':' )
633 if( *psz_parser == '{' )
635 while( *psz_parser && *psz_parser != '}' )
649 #if defined( WIN32 ) || defined( UNDER_CE )
650 if( psz_parser - psz_dup == 1 )
652 /* msg_Warn( p_sout, "drive letter %c: found in source string",
660 psz_access = psz_way = "";
665 *psz_parser++ = '\0';
667 /* let's skip '//' */
668 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
673 psz_name = psz_parser ;
675 /* Come back to parse the access and mux plug-ins */
676 psz_parser = psz_dup;
683 else if( *psz_parser == '/' )
691 psz_access = psz_parser;
693 while( *psz_parser && *psz_parser != '/' )
695 if( *psz_parser == '{' )
697 while( *psz_parser && *psz_parser != '}' )
712 if( *psz_parser == '/' )
714 *psz_parser++ = '\0';
725 psz_way = psz_parser;
729 p_mrl->psz_access = strdup( psz_access );
730 p_mrl->psz_way = strdup( psz_way );
731 p_mrl->psz_name = strdup( psz_name );
734 return( VLC_SUCCESS );
738 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
739 static void mrl_Clean( mrl_t *p_mrl )
741 FREENULL( p_mrl->psz_access );
742 FREENULL( p_mrl->psz_way );
743 FREENULL( p_mrl->psz_name );
747 /****************************************************************************
748 ****************************************************************************
752 ****************************************************************************
753 ****************************************************************************/
755 /* create a complete chain */
757 module{option=*:option=*}[:module{option=*:...}]
761 * parse module{options=str, option="str "}:
762 * return a pointer on the rest
763 * XXX: psz_chain is modified
767 * XXX name and p_cfg are used (-> do NOT free them)
769 sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_chain )
771 static const char typename[] = "stream out";
772 sout_stream_t *p_stream;
776 msg_Err( p_sout, "invalid chain" );
780 p_stream = vlc_custom_create( p_sout, sizeof( *p_stream ),
781 VLC_OBJECT_GENERIC, typename );
785 p_stream->p_sout = p_sout;
786 p_stream->p_sys = NULL;
789 config_ChainCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
791 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
793 vlc_object_set_name( p_stream, 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)
884 #undef sout_EncoderCreate
885 encoder_t *sout_EncoderCreate( vlc_object_t *p_this )
887 static const char type[] = "encoder";
888 return vlc_custom_create( p_this, sizeof( encoder_t ), VLC_OBJECT_GENERIC,