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>
47 #include "input/input_interface.h"
50 /*****************************************************************************
52 *****************************************************************************/
53 #define sout_stream_url_to_chain( p, s ) \
54 _sout_stream_url_to_chain( VLC_OBJECT(p), s )
55 static char *_sout_stream_url_to_chain( vlc_object_t *, const char * );
69 /* mrl_Parse: parse psz_mrl and fill p_mrl */
70 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl );
71 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
72 static void mrl_Clean( mrl_t *p_mrl );
74 /*****************************************************************************
75 * sout_NewInstance: creates a new stream output instance
76 *****************************************************************************/
77 sout_instance_t *__sout_NewInstance( vlc_object_t *p_parent, const char *psz_dest )
79 static const char typename[] = "stream output";
80 sout_instance_t *p_sout;
82 /* *** Allocate descriptor *** */
83 p_sout = vlc_custom_create( p_parent, sizeof( *p_sout ),
84 VLC_OBJECT_GENERIC, typename );
88 /* *** init descriptor *** */
89 p_sout->psz_sout = strdup( psz_dest );
90 p_sout->p_meta = NULL;
91 p_sout->i_out_pace_nocontrol = 0;
94 vlc_mutex_init( &p_sout->lock );
95 if( psz_dest && psz_dest[0] == '#' )
97 p_sout->psz_chain = strdup( &psz_dest[1] );
101 p_sout->psz_chain = sout_stream_url_to_chain( p_sout, psz_dest );
102 msg_Dbg( p_sout, "using sout chain=`%s'", p_sout->psz_chain );
104 p_sout->p_stream = NULL;
106 /* attach it for inherit */
107 vlc_object_attach( p_sout, p_parent );
110 var_Create( p_sout, "sout-mux-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
113 p_sout->p_stream = sout_StreamNew( p_sout, p_sout->psz_chain );
114 if( p_sout->p_stream == NULL )
116 msg_Err( p_sout, "stream chain failed for `%s'", p_sout->psz_chain );
118 FREENULL( p_sout->psz_sout );
119 FREENULL( p_sout->psz_chain );
121 vlc_object_detach( p_sout );
122 vlc_object_release( p_sout );
129 /*****************************************************************************
130 * sout_DeleteInstance: delete a previously allocated instance
131 *****************************************************************************/
132 void sout_DeleteInstance( sout_instance_t * p_sout )
134 /* remove the stream out chain */
135 sout_StreamDelete( p_sout->p_stream );
137 /* *** free all string *** */
138 FREENULL( p_sout->psz_sout );
139 FREENULL( p_sout->psz_chain );
144 vlc_meta_Delete( p_sout->p_meta );
147 vlc_mutex_destroy( &p_sout->lock );
149 /* *** free structure *** */
150 vlc_object_release( p_sout );
153 /*****************************************************************************
155 *****************************************************************************/
156 void sout_UpdateStatistic( sout_instance_t *p_sout, sout_statistic_t i_type, int i_delta )
158 if( !libvlc_stats( p_sout ) )
162 input_thread_t *p_input = vlc_object_find( p_sout, VLC_OBJECT_INPUT, FIND_PARENT );
169 case SOUT_STATISTIC_DECODED_VIDEO:
170 i_input_type = SOUT_STATISTIC_DECODED_VIDEO;
172 case SOUT_STATISTIC_DECODED_AUDIO:
173 i_input_type = SOUT_STATISTIC_DECODED_AUDIO;
175 case SOUT_STATISTIC_DECODED_SUBTITLE:
176 i_input_type = SOUT_STATISTIC_DECODED_SUBTITLE;
179 case SOUT_STATISTIC_SENT_PACKET:
180 i_input_type = SOUT_STATISTIC_SENT_PACKET;
183 case SOUT_STATISTIC_SENT_BYTE:
184 i_input_type = SOUT_STATISTIC_SENT_BYTE;
188 msg_Err( p_sout, "Not yet supported statistic type %d", i_type );
189 vlc_object_release( p_input );
193 input_UpdateStatistic( p_input, i_input_type, i_delta );
195 vlc_object_release( p_input );
197 /*****************************************************************************
199 *****************************************************************************/
200 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
203 sout_packetizer_input_t *p_input;
205 /* *** create a packetizer input *** */
206 p_input = malloc( sizeof( sout_packetizer_input_t ) );
207 if( !p_input ) return NULL;
208 p_input->p_sout = p_sout;
209 p_input->p_fmt = p_fmt;
211 msg_Dbg( p_sout, "adding a new sout input (sout_input:%p)", p_input );
213 if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
215 vlc_object_release( p_sout );
219 /* *** add it to the stream chain */
220 vlc_mutex_lock( &p_sout->lock );
221 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
222 vlc_mutex_unlock( &p_sout->lock );
224 if( p_input->id == NULL )
233 /*****************************************************************************
235 *****************************************************************************/
236 int sout_InputDelete( sout_packetizer_input_t *p_input )
238 sout_instance_t *p_sout = p_input->p_sout;
240 msg_Dbg( p_sout, "removing a sout input (sout_input:%p)", p_input );
242 if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
244 vlc_mutex_lock( &p_sout->lock );
245 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
246 vlc_mutex_unlock( &p_sout->lock );
251 return( VLC_SUCCESS);
254 /*****************************************************************************
256 *****************************************************************************/
257 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
260 sout_instance_t *p_sout = p_input->p_sout;
263 if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
265 block_Release( p_buffer );
268 if( p_buffer->i_dts <= 0 )
270 msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
271 block_Release( p_buffer );
275 vlc_mutex_lock( &p_sout->lock );
276 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
277 p_input->id, p_buffer );
278 vlc_mutex_unlock( &p_sout->lock );
283 #undef sout_AccessOutNew
284 /*****************************************************************************
285 * sout_AccessOutNew: allocate a new access out
286 *****************************************************************************/
287 sout_access_out_t *sout_AccessOutNew( vlc_object_t *p_sout,
288 const char *psz_access, const char *psz_name )
290 static const char typename[] = "access out";
291 sout_access_out_t *p_access;
294 p_access = vlc_custom_create( p_sout, sizeof( *p_access ),
295 VLC_OBJECT_GENERIC, typename );
299 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
302 p_access->psz_path = strdup( psz_name ? psz_name : "" );
303 p_access->p_sys = NULL;
304 p_access->pf_seek = NULL;
305 p_access->pf_read = NULL;
306 p_access->pf_write = NULL;
307 p_access->pf_control = NULL;
308 p_access->p_module = NULL;
310 p_access->i_writes = 0;
311 p_access->i_sent_bytes = 0;
313 vlc_object_attach( p_access, p_sout );
316 module_need( p_access, "sout access", p_access->psz_access, true );
318 if( !p_access->p_module )
320 free( p_access->psz_access );
321 free( p_access->psz_path );
322 vlc_object_detach( p_access );
323 vlc_object_release( p_access );
329 /*****************************************************************************
330 * sout_AccessDelete: delete an access out
331 *****************************************************************************/
332 void sout_AccessOutDelete( sout_access_out_t *p_access )
334 vlc_object_detach( p_access );
335 if( p_access->p_module )
337 module_unneed( p_access, p_access->p_module );
339 free( p_access->psz_access );
341 config_ChainDestroy( p_access->p_cfg );
343 free( p_access->psz_path );
345 vlc_object_release( p_access );
348 /*****************************************************************************
350 *****************************************************************************/
351 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
353 return p_access->pf_seek( p_access, i_pos );
356 /*****************************************************************************
358 *****************************************************************************/
359 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
361 return( p_access->pf_read ?
362 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
365 /*****************************************************************************
367 *****************************************************************************/
368 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
371 const unsigned i_packets_gather = 30;
372 p_access->i_writes++;
373 p_access->i_sent_bytes += p_buffer->i_buffer;
374 if( (p_access->i_writes % i_packets_gather) == 0 )
376 sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_PACKET, i_packets_gather );
377 sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_BYTE, p_access->i_sent_bytes );
378 p_access->i_sent_bytes = 0;
381 return p_access->pf_write( p_access, p_buffer );
385 * sout_AccessOutControl
387 int sout_AccessOutControl (sout_access_out_t *access, int query, ...)
392 va_start (ap, query);
393 if (access->pf_control)
394 ret = access->pf_control (access, query, ap);
401 /*****************************************************************************
402 * sout_MuxNew: create a new mux
403 *****************************************************************************/
404 sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
405 sout_access_out_t *p_access )
407 static const char typename[] = "mux";
411 p_mux = vlc_custom_create( p_sout, sizeof( *p_mux ), VLC_OBJECT_GENERIC,
416 p_mux->p_sout = p_sout;
417 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
420 p_mux->p_access = p_access;
421 p_mux->pf_control = NULL;
422 p_mux->pf_addstream = NULL;
423 p_mux->pf_delstream = NULL;
424 p_mux->pf_mux = NULL;
425 p_mux->i_nb_inputs = 0;
426 p_mux->pp_inputs = NULL;
429 p_mux->p_module = NULL;
431 p_mux->b_add_stream_any_time = false;
432 p_mux->b_waiting_stream = true;
433 p_mux->i_add_stream_start = -1;
435 vlc_object_attach( p_mux, p_sout );
438 module_need( p_mux, "sout mux", p_mux->psz_mux, true );
440 if( p_mux->p_module == NULL )
442 FREENULL( p_mux->psz_mux );
444 vlc_object_detach( p_mux );
445 vlc_object_release( p_mux );
449 /* *** probe mux capacity *** */
450 if( p_mux->pf_control )
452 int b_answer = false;
454 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
462 msg_Dbg( p_sout, "muxer support adding stream at any time" );
463 p_mux->b_add_stream_any_time = true;
464 p_mux->b_waiting_stream = false;
466 /* If we control the output pace then it's better to wait before
467 * starting muxing (generates better streams/files). */
468 if( !p_sout->i_out_pace_nocontrol )
472 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
480 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
482 p_mux->b_waiting_stream = true;
490 /*****************************************************************************
492 *****************************************************************************/
493 void sout_MuxDelete( sout_mux_t *p_mux )
495 vlc_object_detach( p_mux );
496 if( p_mux->p_module )
498 module_unneed( p_mux, p_mux->p_module );
500 free( p_mux->psz_mux );
502 config_ChainDestroy( p_mux->p_cfg );
504 vlc_object_release( p_mux );
507 /*****************************************************************************
509 *****************************************************************************/
510 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
512 sout_input_t *p_input;
514 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
516 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
517 "to this format). You can try increasing sout-mux-caching value" );
521 msg_Dbg( p_mux, "adding a new input" );
523 /* create a new sout input */
524 p_input = malloc( sizeof( sout_input_t ) );
527 p_input->p_sout = p_mux->p_sout;
528 p_input->p_fmt = p_fmt;
529 p_input->p_fifo = block_FifoNew();
530 p_input->p_sys = NULL;
532 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
533 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
535 msg_Err( p_mux, "cannot add this stream" );
536 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
537 block_FifoRelease( p_input->p_fifo );
545 /*****************************************************************************
546 * sout_MuxDeleteStream:
547 *****************************************************************************/
548 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
552 if( p_mux->b_waiting_stream
553 && block_FifoCount( p_input->p_fifo ) > 0 )
555 /* We stop waiting, and call the muxer for taking care of the data
556 * before we remove this es */
557 p_mux->b_waiting_stream = false;
558 p_mux->pf_mux( p_mux );
561 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
564 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
566 msg_Err( p_mux, "cannot delete this stream from mux" );
569 /* remove the entry */
570 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
572 if( p_mux->i_nb_inputs == 0 )
574 msg_Warn( p_mux, "no more input streams for this mux" );
577 block_FifoRelease( p_input->p_fifo );
582 /*****************************************************************************
583 * sout_MuxSendBuffer:
584 *****************************************************************************/
585 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
588 block_FifoPut( p_input->p_fifo, p_buffer );
590 if( p_mux->p_sout->i_out_pace_nocontrol )
592 mtime_t current_date = mdate();
593 if ( current_date > p_buffer->i_dts )
594 msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
595 current_date - p_buffer->i_dts );
598 if( p_mux->b_waiting_stream )
600 const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * INT64_C(1000);
602 if( p_mux->i_add_stream_start < 0 )
603 p_mux->i_add_stream_start = p_buffer->i_dts;
605 /* Wait until we have enought data before muxing */
606 if( p_mux->i_add_stream_start < 0 ||
607 p_buffer->i_dts < p_mux->i_add_stream_start + i_caching )
609 p_mux->b_waiting_stream = false;
611 p_mux->pf_mux( p_mux );
614 /*****************************************************************************
616 *****************************************************************************/
617 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
619 char * psz_dup = strdup( psz_mrl );
620 char * psz_parser = psz_dup;
621 const char * psz_access;
622 const char * psz_way;
625 /* *** first parse psz_dest */
626 while( *psz_parser && *psz_parser != ':' )
628 if( *psz_parser == '{' )
630 while( *psz_parser && *psz_parser != '}' )
644 #if defined( WIN32 ) || defined( UNDER_CE )
645 if( psz_parser - psz_dup == 1 )
647 /* msg_Warn( p_sout, "drive letter %c: found in source string",
655 psz_access = psz_way = "";
660 *psz_parser++ = '\0';
662 /* let's skip '//' */
663 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
668 psz_name = psz_parser ;
670 /* Come back to parse the access and mux plug-ins */
671 psz_parser = psz_dup;
678 else if( *psz_parser == '/' )
686 psz_access = psz_parser;
688 while( *psz_parser && *psz_parser != '/' )
690 if( *psz_parser == '{' )
692 while( *psz_parser && *psz_parser != '}' )
707 if( *psz_parser == '/' )
709 *psz_parser++ = '\0';
720 psz_way = psz_parser;
724 p_mrl->psz_access = strdup( psz_access );
725 p_mrl->psz_way = strdup( psz_way );
726 p_mrl->psz_name = strdup( psz_name );
729 return( VLC_SUCCESS );
733 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
734 static void mrl_Clean( mrl_t *p_mrl )
736 FREENULL( p_mrl->psz_access );
737 FREENULL( p_mrl->psz_way );
738 FREENULL( p_mrl->psz_name );
742 /****************************************************************************
743 ****************************************************************************
747 ****************************************************************************
748 ****************************************************************************/
750 /* create a complete chain */
752 module{option=*:option=*}[:module{option=*:...}]
756 * parse module{options=str, option="str "}:
757 * return a pointer on the rest
758 * XXX: psz_chain is modified
762 * XXX name and p_cfg are used (-> do NOT free them)
764 sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_chain )
766 static const char typename[] = "stream out";
767 sout_stream_t *p_stream;
771 msg_Err( p_sout, "invalid chain" );
775 p_stream = vlc_custom_create( p_sout, sizeof( *p_stream ),
776 VLC_OBJECT_GENERIC, typename );
780 p_stream->p_sout = p_sout;
781 p_stream->p_sys = NULL;
784 config_ChainCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
786 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
788 vlc_object_attach( p_stream, p_sout );
791 module_need( p_stream, "sout stream", p_stream->psz_name, true );
793 if( !p_stream->p_module )
795 sout_StreamDelete( p_stream );
802 void sout_StreamDelete( sout_stream_t *p_stream )
804 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
806 vlc_object_detach( p_stream );
807 if( p_stream->p_module ) module_unneed( p_stream, p_stream->p_module );
809 FREENULL( p_stream->psz_name );
810 FREENULL( p_stream->psz_next );
812 config_ChainDestroy( p_stream->p_cfg );
814 msg_Dbg( p_stream, "destroying chain done" );
815 vlc_object_release( p_stream );
818 static char *_sout_stream_url_to_chain( vlc_object_t *p_this,
819 const char *psz_url )
824 mrl_Parse( &mrl, psz_url );
826 /* Check if the URLs goes to #rtp - otherwise we'll use #standard */
827 static const char rtplist[] = "dccp\0sctp\0tcp\0udplite\0";
828 for (const char *a = rtplist; *a; a += strlen (a) + 1)
829 if (strcmp (a, mrl.psz_access) == 0)
832 if (strcmp (mrl.psz_access, "rtp") == 0)
835 /* For historical reasons, rtp:// means RTP over UDP */
836 strcpy (mrl.psz_access, "udp");
838 if (mrl.psz_name[0] == '[')
840 port = strstr (mrl.psz_name, "]:");
845 port = strchr (mrl.psz_name, ':');
847 *port++ = '\0'; /* erase ':' */
849 if (asprintf (&psz_chain,
850 "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s%s%s\"}",
851 mrl.psz_way, mrl.psz_access, mrl.psz_name,
852 port ? "\",port=\"" : "", port ? port : "") == -1)
857 /* Convert the URL to a basic standard sout chain */
858 if (asprintf (&psz_chain,
859 "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
860 mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
864 /* Duplicate and wrap if sout-display is on */
865 if (psz_chain && (config_GetInt( p_this, "sout-display" ) > 0))
868 if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", tmp) == -1)