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"
51 /*****************************************************************************
53 *****************************************************************************/
54 #define sout_stream_url_to_chain( p, s ) \
55 _sout_stream_url_to_chain( VLC_OBJECT(p), s )
56 static char *_sout_stream_url_to_chain( vlc_object_t *, const char * );
70 /* mrl_Parse: parse psz_mrl and fill p_mrl */
71 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl );
72 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
73 static void mrl_Clean( mrl_t *p_mrl );
75 /*****************************************************************************
76 * sout_NewInstance: creates a new stream output instance
77 *****************************************************************************/
78 sout_instance_t *__sout_NewInstance( vlc_object_t *p_parent, const char *psz_dest )
80 static const char typename[] = "stream output";
81 sout_instance_t *p_sout;
83 /* *** Allocate descriptor *** */
84 p_sout = vlc_custom_create( p_parent, sizeof( *p_sout ),
85 VLC_OBJECT_GENERIC, typename );
89 /* *** init descriptor *** */
90 p_sout->psz_sout = strdup( psz_dest );
91 p_sout->p_meta = NULL;
92 p_sout->i_out_pace_nocontrol = 0;
95 vlc_mutex_init( &p_sout->lock );
96 if( psz_dest && psz_dest[0] == '#' )
98 p_sout->psz_chain = strdup( &psz_dest[1] );
102 p_sout->psz_chain = sout_stream_url_to_chain( p_sout, psz_dest );
103 msg_Dbg( p_sout, "using sout chain=`%s'", p_sout->psz_chain );
105 p_sout->p_stream = NULL;
107 /* attach it for inherit */
108 vlc_object_attach( p_sout, p_parent );
111 var_Create( p_sout, "sout-mux-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
114 p_sout->p_stream = sout_StreamNew( p_sout, p_sout->psz_chain );
115 if( p_sout->p_stream == NULL )
117 msg_Err( p_sout, "stream chain failed for `%s'", p_sout->psz_chain );
119 FREENULL( p_sout->psz_sout );
120 FREENULL( p_sout->psz_chain );
122 vlc_object_detach( p_sout );
123 vlc_object_release( p_sout );
130 /*****************************************************************************
131 * sout_DeleteInstance: delete a previously allocated instance
132 *****************************************************************************/
133 void sout_DeleteInstance( sout_instance_t * p_sout )
135 /* remove the stream out chain */
136 sout_StreamDelete( p_sout->p_stream );
138 /* *** free all string *** */
139 FREENULL( p_sout->psz_sout );
140 FREENULL( p_sout->psz_chain );
145 vlc_meta_Delete( p_sout->p_meta );
148 vlc_mutex_destroy( &p_sout->lock );
150 /* *** free structure *** */
151 vlc_object_release( p_sout );
154 /*****************************************************************************
156 *****************************************************************************/
157 void sout_UpdateStatistic( sout_instance_t *p_sout, sout_statistic_t i_type, int i_delta )
159 if( !libvlc_stats( p_sout ) )
163 input_thread_t *p_input = vlc_object_find( p_sout, VLC_OBJECT_INPUT, FIND_PARENT );
170 case SOUT_STATISTIC_DECODED_VIDEO:
171 i_input_type = SOUT_STATISTIC_DECODED_VIDEO;
173 case SOUT_STATISTIC_DECODED_AUDIO:
174 i_input_type = SOUT_STATISTIC_DECODED_AUDIO;
176 case SOUT_STATISTIC_DECODED_SUBTITLE:
177 i_input_type = SOUT_STATISTIC_DECODED_SUBTITLE;
180 case SOUT_STATISTIC_SENT_PACKET:
181 i_input_type = SOUT_STATISTIC_SENT_PACKET;
184 case SOUT_STATISTIC_SENT_BYTE:
185 i_input_type = SOUT_STATISTIC_SENT_BYTE;
189 msg_Err( p_sout, "Not yet supported statistic type %d", i_type );
190 vlc_object_release( p_input );
194 input_UpdateStatistic( p_input, i_input_type, i_delta );
196 vlc_object_release( p_input );
198 /*****************************************************************************
200 *****************************************************************************/
201 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
204 sout_packetizer_input_t *p_input;
206 /* *** create a packetizer input *** */
207 p_input = malloc( sizeof( sout_packetizer_input_t ) );
208 if( !p_input ) return NULL;
209 p_input->p_sout = p_sout;
210 p_input->p_fmt = p_fmt;
212 msg_Dbg( p_sout, "adding a new sout input (sout_input:%p)", p_input );
214 if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
216 vlc_object_release( p_sout );
220 /* *** add it to the stream chain */
221 vlc_mutex_lock( &p_sout->lock );
222 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
223 vlc_mutex_unlock( &p_sout->lock );
225 if( p_input->id == NULL )
234 /*****************************************************************************
236 *****************************************************************************/
237 int sout_InputDelete( sout_packetizer_input_t *p_input )
239 sout_instance_t *p_sout = p_input->p_sout;
241 msg_Dbg( p_sout, "removing a sout input (sout_input:%p)", p_input );
243 if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
245 vlc_mutex_lock( &p_sout->lock );
246 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
247 vlc_mutex_unlock( &p_sout->lock );
252 return( VLC_SUCCESS);
255 /*****************************************************************************
257 *****************************************************************************/
258 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
261 sout_instance_t *p_sout = p_input->p_sout;
264 if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
266 block_Release( p_buffer );
269 if( p_buffer->i_dts <= 0 )
271 msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
272 block_Release( p_buffer );
276 vlc_mutex_lock( &p_sout->lock );
277 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
278 p_input->id, p_buffer );
279 vlc_mutex_unlock( &p_sout->lock );
284 #undef sout_AccessOutNew
285 /*****************************************************************************
286 * sout_AccessOutNew: allocate a new access out
287 *****************************************************************************/
288 sout_access_out_t *sout_AccessOutNew( vlc_object_t *p_sout,
289 const char *psz_access, const char *psz_name )
291 static const char typename[] = "access out";
292 sout_access_out_t *p_access;
295 p_access = vlc_custom_create( p_sout, sizeof( *p_access ),
296 VLC_OBJECT_GENERIC, typename );
300 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
303 if( !p_access->psz_access )
304 p_access->psz_access = strdup( "" );
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_attach( p_access, p_sout );
319 module_need( p_access, "sout access", p_access->psz_access, true );
321 if( !p_access->p_module )
323 free( p_access->psz_access );
324 free( p_access->psz_path );
325 vlc_object_detach( p_access );
326 vlc_object_release( p_access );
332 /*****************************************************************************
333 * sout_AccessDelete: delete an access out
334 *****************************************************************************/
335 void sout_AccessOutDelete( sout_access_out_t *p_access )
337 vlc_object_detach( p_access );
338 if( p_access->p_module )
340 module_unneed( p_access, p_access->p_module );
342 free( p_access->psz_access );
344 config_ChainDestroy( p_access->p_cfg );
346 free( p_access->psz_path );
348 vlc_object_release( p_access );
351 /*****************************************************************************
353 *****************************************************************************/
354 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
356 return p_access->pf_seek( p_access, i_pos );
359 /*****************************************************************************
361 *****************************************************************************/
362 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
364 return( p_access->pf_read ?
365 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
368 /*****************************************************************************
370 *****************************************************************************/
371 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
374 const unsigned i_packets_gather = 30;
375 p_access->i_writes++;
376 p_access->i_sent_bytes += p_buffer->i_buffer;
377 if( (p_access->i_writes % i_packets_gather) == 0 )
379 sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_PACKET, i_packets_gather );
380 sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_BYTE, p_access->i_sent_bytes );
381 p_access->i_sent_bytes = 0;
384 return p_access->pf_write( p_access, p_buffer );
388 * sout_AccessOutControl
390 int sout_AccessOutControl (sout_access_out_t *access, int query, ...)
395 va_start (ap, query);
396 if (access->pf_control)
397 ret = access->pf_control (access, query, ap);
404 /*****************************************************************************
405 * sout_MuxNew: create a new mux
406 *****************************************************************************/
407 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
408 sout_access_out_t *p_access )
410 static const char typename[] = "mux";
414 p_mux = vlc_custom_create( p_sout, sizeof( *p_mux ), VLC_OBJECT_GENERIC,
419 p_mux->p_sout = p_sout;
420 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
423 p_mux->p_access = p_access;
424 p_mux->pf_control = NULL;
425 p_mux->pf_addstream = NULL;
426 p_mux->pf_delstream = NULL;
427 p_mux->pf_mux = NULL;
428 p_mux->i_nb_inputs = 0;
429 p_mux->pp_inputs = NULL;
432 p_mux->p_module = NULL;
434 p_mux->b_add_stream_any_time = false;
435 p_mux->b_waiting_stream = true;
436 p_mux->i_add_stream_start = -1;
438 vlc_object_attach( p_mux, p_sout );
441 module_need( p_mux, "sout mux", p_mux->psz_mux, true );
443 if( p_mux->p_module == NULL )
445 FREENULL( p_mux->psz_mux );
447 vlc_object_detach( p_mux );
448 vlc_object_release( p_mux );
452 /* *** probe mux capacity *** */
453 if( p_mux->pf_control )
455 int b_answer = false;
457 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
465 msg_Dbg( p_sout, "muxer support adding stream at any time" );
466 p_mux->b_add_stream_any_time = true;
467 p_mux->b_waiting_stream = false;
469 /* If we control the output pace then it's better to wait before
470 * starting muxing (generates better streams/files). */
471 if( !p_sout->i_out_pace_nocontrol )
475 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
483 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
485 p_mux->b_waiting_stream = true;
493 /*****************************************************************************
495 *****************************************************************************/
496 void sout_MuxDelete( sout_mux_t *p_mux )
498 vlc_object_detach( p_mux );
499 if( p_mux->p_module )
501 module_unneed( p_mux, p_mux->p_module );
503 free( p_mux->psz_mux );
505 config_ChainDestroy( p_mux->p_cfg );
507 vlc_object_release( p_mux );
510 /*****************************************************************************
512 *****************************************************************************/
513 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
515 sout_input_t *p_input;
517 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
519 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
520 "to this format). You can try increasing sout-mux-caching value" );
524 msg_Dbg( p_mux, "adding a new input" );
526 /* create a new sout input */
527 p_input = malloc( sizeof( sout_input_t ) );
530 p_input->p_sout = p_mux->p_sout;
531 p_input->p_fmt = p_fmt;
532 p_input->p_fifo = block_FifoNew();
533 p_input->p_sys = NULL;
535 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
536 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
538 msg_Err( p_mux, "cannot add this stream" );
539 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
540 block_FifoRelease( p_input->p_fifo );
548 /*****************************************************************************
549 * sout_MuxDeleteStream:
550 *****************************************************************************/
551 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
555 if( p_mux->b_waiting_stream
556 && block_FifoCount( p_input->p_fifo ) > 0 )
558 /* We stop waiting, and call the muxer for taking care of the data
559 * before we remove this es */
560 p_mux->b_waiting_stream = false;
561 p_mux->pf_mux( p_mux );
564 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
567 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
569 msg_Err( p_mux, "cannot delete this stream from mux" );
572 /* remove the entry */
573 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
575 if( p_mux->i_nb_inputs == 0 )
577 msg_Warn( p_mux, "no more input streams for this mux" );
580 block_FifoRelease( p_input->p_fifo );
585 /*****************************************************************************
586 * sout_MuxSendBuffer:
587 *****************************************************************************/
588 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
591 block_FifoPut( p_input->p_fifo, p_buffer );
593 if( p_mux->p_sout->i_out_pace_nocontrol )
595 mtime_t current_date = mdate();
596 if ( current_date > p_buffer->i_dts )
597 msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
598 current_date - p_buffer->i_dts );
601 if( p_mux->b_waiting_stream )
603 const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * INT64_C(1000);
605 if( p_mux->i_add_stream_start < 0 )
606 p_mux->i_add_stream_start = p_buffer->i_dts;
608 /* Wait until we have enought data before muxing */
609 if( p_mux->i_add_stream_start < 0 ||
610 p_buffer->i_dts < p_mux->i_add_stream_start + i_caching )
612 p_mux->b_waiting_stream = false;
614 p_mux->pf_mux( p_mux );
617 /*****************************************************************************
619 *****************************************************************************/
620 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
622 char * psz_dup = strdup( psz_mrl );
623 char * psz_parser = psz_dup;
624 const char * psz_access;
625 const char * psz_way;
628 /* *** first parse psz_dest */
629 while( *psz_parser && *psz_parser != ':' )
631 if( *psz_parser == '{' )
633 while( *psz_parser && *psz_parser != '}' )
647 #if defined( WIN32 ) || defined( UNDER_CE )
648 if( psz_parser - psz_dup == 1 )
650 /* msg_Warn( p_sout, "drive letter %c: found in source string",
658 psz_access = psz_way = "";
663 *psz_parser++ = '\0';
665 /* let's skip '//' */
666 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
671 psz_name = psz_parser ;
673 /* Come back to parse the access and mux plug-ins */
674 psz_parser = psz_dup;
681 else if( *psz_parser == '/' )
689 psz_access = psz_parser;
691 while( *psz_parser && *psz_parser != '/' )
693 if( *psz_parser == '{' )
695 while( *psz_parser && *psz_parser != '}' )
710 if( *psz_parser == '/' )
712 *psz_parser++ = '\0';
723 psz_way = psz_parser;
727 p_mrl->psz_access = strdup( psz_access );
728 p_mrl->psz_way = strdup( psz_way );
729 p_mrl->psz_name = strdup( psz_name );
732 return( VLC_SUCCESS );
736 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
737 static void mrl_Clean( mrl_t *p_mrl )
739 FREENULL( p_mrl->psz_access );
740 FREENULL( p_mrl->psz_way );
741 FREENULL( p_mrl->psz_name );
745 /****************************************************************************
746 ****************************************************************************
750 ****************************************************************************
751 ****************************************************************************/
753 /* create a complete chain */
755 module{option=*:option=*}[:module{option=*:...}]
759 * parse module{options=str, option="str "}:
760 * return a pointer on the rest
761 * XXX: psz_chain is modified
765 * XXX name and p_cfg are used (-> do NOT free them)
767 sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_chain )
769 static const char typename[] = "stream out";
770 sout_stream_t *p_stream;
774 msg_Err( p_sout, "invalid chain" );
778 p_stream = vlc_custom_create( p_sout, sizeof( *p_stream ),
779 VLC_OBJECT_GENERIC, typename );
783 p_stream->p_sout = p_sout;
784 p_stream->p_sys = NULL;
787 config_ChainCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
789 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
791 vlc_object_attach( p_stream, p_sout );
794 module_need( p_stream, "sout stream", p_stream->psz_name, true );
796 if( !p_stream->p_module )
798 sout_StreamDelete( p_stream );
805 void sout_StreamDelete( sout_stream_t *p_stream )
807 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
809 vlc_object_detach( p_stream );
810 if( p_stream->p_module ) module_unneed( p_stream, p_stream->p_module );
812 FREENULL( p_stream->psz_name );
813 FREENULL( p_stream->psz_next );
815 config_ChainDestroy( p_stream->p_cfg );
817 msg_Dbg( p_stream, "destroying chain done" );
818 vlc_object_release( p_stream );
821 static char *_sout_stream_url_to_chain( vlc_object_t *p_this,
822 const char *psz_url )
827 mrl_Parse( &mrl, psz_url );
829 /* Check if the URLs goes to #rtp - otherwise we'll use #standard */
830 static const char rtplist[] = "dccp\0sctp\0tcp\0udplite\0";
831 for (const char *a = rtplist; *a; a += strlen (a) + 1)
832 if (strcmp (a, mrl.psz_access) == 0)
835 if (strcmp (mrl.psz_access, "rtp") == 0)
838 /* For historical reasons, rtp:// means RTP over UDP */
839 strcpy (mrl.psz_access, "udp");
841 if (mrl.psz_name[0] == '[')
843 port = strstr (mrl.psz_name, "]:");
848 port = strchr (mrl.psz_name, ':');
850 *port++ = '\0'; /* erase ':' */
852 if (asprintf (&psz_chain,
853 "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s%s%s\"}",
854 mrl.psz_way, mrl.psz_access, mrl.psz_name,
855 port ? "\",port=\"" : "", port ? port : "") == -1)
860 /* Convert the URL to a basic standard sout chain */
861 if (asprintf (&psz_chain,
862 "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
863 mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
867 /* Duplicate and wrap if sout-display is on */
868 if (psz_chain && (config_GetInt( p_this, "sout-display" ) > 0))
871 if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", tmp) == -1)
881 #undef sout_EncoderCreate
882 encoder_t *sout_EncoderCreate( vlc_object_t *p_this )
884 static const char type[] = "encoder";
885 return vlc_custom_create( p_this, sizeof( encoder_t ), VLC_OBJECT_GENERIC,