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 *****************************************************************************/
36 #include <stdlib.h> /* free() */
37 #include <stdio.h> /* sprintf() */
41 #include <vlc_playlist.h>
43 #include "stream_output.h"
47 #include "input/input_internal.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, char * psz_dest )
79 sout_instance_t *p_sout;
81 /* *** Allocate descriptor *** */
82 p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
85 msg_Err( p_parent, "out of memory" );
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, &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 input_thread_t *p_input;
160 int i_bytes; /* That's pretty stupid to define it as an integer, it will overflow
163 if( !p_sout->p_libvlc->b_stats )
167 * TODO add a private (ie not VLC_EXPORTed) input_UpdateStatistic for that */
168 p_input = vlc_object_find( p_sout, VLC_OBJECT_INPUT, FIND_PARENT );
169 if( !p_input || p_input->i_state == INIT_S || p_input->i_state == ERROR_S )
174 #define I(c) stats_UpdateInteger( p_input, p_input->p->counters.c, i_delta, NULL )
175 case SOUT_STATISTIC_DECODED_VIDEO:
178 case SOUT_STATISTIC_DECODED_AUDIO:
181 case SOUT_STATISTIC_DECODED_SUBTITLE:
185 case SOUT_STATISTIC_ENCODED_VIDEO:
186 case SOUT_STATISTIC_ENCODED_AUDIO:
187 case SOUT_STATISTIC_ENCODED_SUBTITLE:
188 msg_Warn( p_sout, "Not yet supported statistic type %d", i_type );
192 case SOUT_STATISTIC_SENT_PACKET:
193 I(p_sout_sent_packets);
196 case SOUT_STATISTIC_SENT_BYTE:
197 if( !stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_bytes, i_delta, &i_bytes ) )
198 stats_UpdateFloat( p_input, p_input->p->counters.p_sout_send_bitrate, i_bytes, NULL );
202 msg_Err( p_sout, "Invalid statistic type %d (internal error)", i_type );
205 vlc_object_release( p_input );
207 /*****************************************************************************
209 *****************************************************************************/
210 sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
213 sout_packetizer_input_t *p_input;
215 msg_Dbg( p_sout, "adding a new input" );
217 /* *** create a packetizer input *** */
218 p_input = malloc( sizeof( sout_packetizer_input_t ) );
219 p_input->p_sout = p_sout;
220 p_input->p_fmt = p_fmt;
222 if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
224 vlc_object_release( p_sout );
228 /* *** add it to the stream chain */
229 vlc_mutex_lock( &p_sout->lock );
230 p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
231 vlc_mutex_unlock( &p_sout->lock );
233 if( p_input->id == NULL )
242 /*****************************************************************************
244 *****************************************************************************/
245 int sout_InputDelete( sout_packetizer_input_t *p_input )
247 sout_instance_t *p_sout = p_input->p_sout;
249 msg_Dbg( p_sout, "removing an input" );
251 if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
253 vlc_mutex_lock( &p_sout->lock );
254 p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
255 vlc_mutex_unlock( &p_sout->lock );
260 return( VLC_SUCCESS);
263 /*****************************************************************************
265 *****************************************************************************/
266 int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
269 sout_instance_t *p_sout = p_input->p_sout;
272 if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
274 block_Release( p_buffer );
277 if( p_buffer->i_dts <= 0 )
279 msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
280 block_Release( p_buffer );
284 vlc_mutex_lock( &p_sout->lock );
285 i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
286 p_input->id, p_buffer );
287 vlc_mutex_unlock( &p_sout->lock );
292 /*****************************************************************************
293 * sout_AccessOutNew: allocate a new access out
294 *****************************************************************************/
295 sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
296 const char *psz_access, const char *psz_name )
298 sout_access_out_t *p_access;
301 if( !( p_access = vlc_object_create( p_sout,
302 sizeof( sout_access_out_t ) ) ) )
304 msg_Err( p_sout, "out of memory" );
308 psz_next = config_ChainCreate( &p_access->psz_access, &p_access->p_cfg,
311 p_access->psz_path = strdup( psz_name ? psz_name : "" );
312 p_access->p_sout = p_sout;
313 p_access->p_sys = NULL;
314 p_access->pf_seek = NULL;
315 p_access->pf_read = NULL;
316 p_access->pf_write = NULL;
317 p_access->pf_control = NULL;
318 p_access->p_module = NULL;
320 p_access->i_writes = 0;
321 p_access->i_sent_bytes = 0;
323 vlc_object_attach( p_access, p_sout );
326 module_Need( p_access, "sout access", p_access->psz_access, VLC_TRUE );
328 if( !p_access->p_module )
330 free( p_access->psz_access );
331 free( p_access->psz_path );
332 vlc_object_detach( p_access );
333 vlc_object_release( p_access );
339 /*****************************************************************************
340 * sout_AccessDelete: delete an access out
341 *****************************************************************************/
342 void sout_AccessOutDelete( sout_access_out_t *p_access )
344 vlc_object_detach( p_access );
345 if( p_access->p_module )
347 module_Unneed( p_access, p_access->p_module );
349 free( p_access->psz_access );
351 config_ChainDestroy( p_access->p_cfg );
353 free( p_access->psz_path );
355 vlc_object_release( p_access );
358 /*****************************************************************************
360 *****************************************************************************/
361 int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
363 return p_access->pf_seek( p_access, i_pos );
366 /*****************************************************************************
368 *****************************************************************************/
369 ssize_t sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
371 return( p_access->pf_read ?
372 p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
375 /*****************************************************************************
377 *****************************************************************************/
378 ssize_t sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
380 const unsigned i_packets_gather = 30;
381 p_access->i_writes++;
382 p_access->i_sent_bytes += p_buffer->i_buffer;
383 if( (p_access->i_writes % i_packets_gather) == 0 )
385 sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_PACKET, i_packets_gather );
386 sout_UpdateStatistic( p_access->p_sout, SOUT_STATISTIC_SENT_BYTE, p_access->i_sent_bytes );
387 p_access->i_sent_bytes = 0;
389 return p_access->pf_write( p_access, p_buffer );
393 * sout_AccessOutControl
395 int sout_AccessOutControl (sout_access_out_t *access, int query, va_list args)
397 return (access->pf_control) ? access->pf_control (access, query, args)
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 )
410 p_mux = vlc_object_create( p_sout, sizeof( sout_mux_t ) );
413 msg_Err( p_sout, "out of memory" );
417 p_mux->p_sout = p_sout;
418 psz_next = config_ChainCreate( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
421 p_mux->p_access = p_access;
422 p_mux->pf_control = NULL;
423 p_mux->pf_addstream = NULL;
424 p_mux->pf_delstream = NULL;
425 p_mux->pf_mux = NULL;
426 p_mux->i_nb_inputs = 0;
427 p_mux->pp_inputs = NULL;
430 p_mux->p_module = NULL;
432 p_mux->b_add_stream_any_time = VLC_FALSE;
433 p_mux->b_waiting_stream = VLC_TRUE;
434 p_mux->i_add_stream_start = -1;
436 vlc_object_attach( p_mux, p_sout );
439 module_Need( p_mux, "sout mux", p_mux->psz_mux, VLC_TRUE );
441 if( p_mux->p_module == NULL )
443 FREENULL( p_mux->psz_mux );
445 vlc_object_detach( p_mux );
446 vlc_object_release( p_mux );
450 /* *** probe mux capacity *** */
451 if( p_mux->pf_control )
453 int b_answer = VLC_FALSE;
455 if( sout_MuxControl( p_mux, MUX_CAN_ADD_STREAM_WHILE_MUXING,
458 b_answer = VLC_FALSE;
463 msg_Dbg( p_sout, "muxer support adding stream at any time" );
464 p_mux->b_add_stream_any_time = VLC_TRUE;
465 p_mux->b_waiting_stream = VLC_FALSE;
467 /* If we control the output pace then it's better to wait before
468 * starting muxing (generates better streams/files). */
469 if( !p_sout->i_out_pace_nocontrol )
473 else if( sout_MuxControl( p_mux, MUX_GET_ADD_STREAM_WAIT,
476 b_answer = VLC_FALSE;
481 msg_Dbg( p_sout, "muxer prefers to wait for all ES before "
483 p_mux->b_waiting_stream = VLC_TRUE;
491 /*****************************************************************************
493 *****************************************************************************/
494 void sout_MuxDelete( sout_mux_t *p_mux )
496 vlc_object_detach( p_mux );
497 if( p_mux->p_module )
499 module_Unneed( p_mux, p_mux->p_module );
501 free( p_mux->psz_mux );
503 config_ChainDestroy( p_mux->p_cfg );
505 vlc_object_release( p_mux );
508 /*****************************************************************************
510 *****************************************************************************/
511 sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
513 sout_input_t *p_input;
515 if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
517 msg_Err( p_mux, "cannot add a new stream (unsupported while muxing "
518 "to this format). You can try increasing sout-mux-caching value" );
522 msg_Dbg( p_mux, "adding a new input" );
524 /* create a new sout input */
525 p_input = malloc( sizeof( sout_input_t ) );
528 msg_Err( p_mux, "out of memory" );
531 p_input->p_sout = p_mux->p_sout;
532 p_input->p_fmt = p_fmt;
533 p_input->p_fifo = block_FifoNew( p_mux->p_sout );
534 p_input->p_sys = NULL;
536 TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
537 if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
539 msg_Err( p_mux, "cannot add this stream" );
540 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
541 block_FifoRelease( p_input->p_fifo );
549 /*****************************************************************************
550 * sout_MuxDeleteStream:
551 *****************************************************************************/
552 void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
556 if( p_mux->b_waiting_stream
557 && block_FifoCount( p_input->p_fifo ) > 0 )
559 /* We stop waiting, and call the muxer for taking care of the data
560 * before we remove this es */
561 p_mux->b_waiting_stream = VLC_FALSE;
562 p_mux->pf_mux( p_mux );
565 TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
568 if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
570 msg_Err( p_mux, "cannot delete this stream from mux" );
573 /* remove the entry */
574 TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
576 if( p_mux->i_nb_inputs == 0 )
578 msg_Warn( p_mux, "no more input streams for this mux" );
581 block_FifoRelease( p_input->p_fifo );
586 /*****************************************************************************
587 * sout_MuxSendBuffer:
588 *****************************************************************************/
589 void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
592 block_FifoPut( p_input->p_fifo, p_buffer );
594 if( p_mux->p_sout->i_out_pace_nocontrol )
596 mtime_t current_date = mdate();
597 if ( current_date > p_buffer->i_dts )
598 msg_Warn( p_mux, "late buffer for mux input ("I64Fd")",
599 current_date - p_buffer->i_dts );
602 if( p_mux->b_waiting_stream )
604 const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * I64C(1000);
606 if( p_mux->i_add_stream_start < 0 )
607 p_mux->i_add_stream_start = p_buffer->i_dts;
609 /* Wait until we have enought data before muxing */
610 if( p_mux->i_add_stream_start < 0 ||
611 p_buffer->i_dts < p_mux->i_add_stream_start + i_caching )
613 p_mux->b_waiting_stream = VLC_FALSE;
615 p_mux->pf_mux( p_mux );
618 /*****************************************************************************
620 *****************************************************************************/
621 static int mrl_Parse( mrl_t *p_mrl, const char *psz_mrl )
623 char * psz_dup = strdup( psz_mrl );
624 char * psz_parser = psz_dup;
625 const char * psz_access;
626 const char * psz_way;
629 /* *** first parse psz_dest */
630 while( *psz_parser && *psz_parser != ':' )
632 if( *psz_parser == '{' )
634 while( *psz_parser && *psz_parser != '}' )
648 #if defined( WIN32 ) || defined( UNDER_CE )
649 if( psz_parser - psz_dup == 1 )
651 /* msg_Warn( p_sout, "drive letter %c: found in source string",
659 psz_access = psz_way = "";
664 *psz_parser++ = '\0';
666 /* let's skip '//' */
667 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
672 psz_name = psz_parser ;
674 /* Come back to parse the access and mux plug-ins */
675 psz_parser = psz_dup;
682 else if( *psz_parser == '/' )
690 psz_access = psz_parser;
692 while( *psz_parser && *psz_parser != '/' )
694 if( *psz_parser == '{' )
696 while( *psz_parser && *psz_parser != '}' )
711 if( *psz_parser == '/' )
713 *psz_parser++ = '\0';
724 psz_way = psz_parser;
728 p_mrl->psz_access = strdup( psz_access );
729 p_mrl->psz_way = strdup( psz_way );
730 p_mrl->psz_name = strdup( psz_name );
733 return( VLC_SUCCESS );
737 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
738 static void mrl_Clean( mrl_t *p_mrl )
740 FREENULL( p_mrl->psz_access );
741 FREENULL( p_mrl->psz_way );
742 FREENULL( p_mrl->psz_name );
746 /****************************************************************************
747 ****************************************************************************
751 ****************************************************************************
752 ****************************************************************************/
754 /* create a complete chain */
756 module{option=*:option=*}[:module{option=*:...}]
760 * parse module{options=str, option="str "}:
761 * return a pointer on the rest
762 * XXX: psz_chain is modified
764 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
765 #define SKIPTRAILINGSPACE( p, e ) \
766 { while( e > p && ( *(e-1) == ' ' || *(e-1) == '\t' ) ) e--; }
768 /* go accross " " and { } */
769 static char *_get_chain_end( char *str )
777 if( !*p || *p == ',' || *p == '}' ) return p;
779 if( *p != '{' && *p != '"' && *p != '\'' )
785 if( *p == '{' ) c = '}';
793 if( *p == c ) return ++p;
794 else if( *p == '{' && c == '}' ) p = _get_chain_end( p );
801 * XXX name and p_cfg are used (-> do NOT free them)
803 sout_stream_t *sout_StreamNew( sout_instance_t *p_sout, char *psz_chain )
805 sout_stream_t *p_stream;
809 msg_Err( p_sout, "invalid chain" );
813 p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
817 msg_Err( p_sout, "out of memory" );
821 p_stream->p_sout = p_sout;
822 p_stream->p_sys = NULL;
825 config_ChainCreate( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
827 msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
829 vlc_object_attach( p_stream, p_sout );
832 module_Need( p_stream, "sout stream", p_stream->psz_name, VLC_TRUE );
834 if( !p_stream->p_module )
836 sout_StreamDelete( p_stream );
843 void sout_StreamDelete( sout_stream_t *p_stream )
845 msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
847 vlc_object_detach( p_stream );
848 if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
850 FREENULL( p_stream->psz_name );
851 FREENULL( p_stream->psz_next );
853 config_ChainDestroy( p_stream->p_cfg );
855 msg_Dbg( p_stream, "destroying chain done" );
856 vlc_object_release( p_stream );
859 static char *_sout_stream_url_to_chain( vlc_object_t *p_this,
860 const char *psz_url )
864 const char *fmt = "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}";
865 static const char rtpfmt[] = "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s\"}";
867 mrl_Parse( &mrl, psz_url );
869 /* Check if the URLs goes #rtp - otherwise we'll use #standard */
870 if (strcmp (mrl.psz_access, "rtp") == 0)
872 /* For historical reasons, rtp:// means RTP over UDP */
873 strcpy (mrl.psz_access, "udp");
878 static const char list[] = "dccp\0sctp\0tcp\0udplite\0";
879 for (const char *a = list; *a; a += strlen (a) + 1)
880 if (strcmp (a, mrl.psz_access) == 0)
887 /* Convert the URL to a basic sout chain */
888 if (asprintf (&psz_chain, fmt,
889 mrl.psz_way, mrl.psz_access, mrl.psz_name) == -1)
892 /* Duplicate and wrap if sout-display is on */
893 if (psz_chain && (config_GetInt( p_this, "sout-display" ) > 0))
896 if (asprintf (&tmp, "duplicate{dst=display,dst=%s}", tmp) == -1)