1 /*****************************************************************************
2 * stream_output.c : stream output module
3 *****************************************************************************
4 * Copyright (C) 2002-2007 VLC authors and VideoLAN
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 it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
36 #include <vlc_common.h>
38 #include <stdlib.h> /* free() */
39 #include <stdio.h> /* sprintf() */
44 #include "stream_output.h"
47 #include <vlc_block.h>
48 #include <vlc_codec.h>
49 #include <vlc_modules.h>
51 #include "input/input_interface.h"
53 #define VLC_CODEC_NULL VLC_FOURCC( 'n', 'u', 'l', 'l' )
56 /*****************************************************************************
58 *****************************************************************************/
59 static char *sout_stream_url_to_chain( bool, const char * );
73 /* mrl_Parse: parse psz_mrl and fill p_mrl */
74 static int mrl_Parse( mrl_t
*p_mrl
, const char *psz_mrl
);
75 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
76 static void mrl_Clean( mrl_t
*p_mrl
);
78 #undef sout_NewInstance
80 /*****************************************************************************
81 * sout_NewInstance: creates a new stream output instance
82 *****************************************************************************/
83 sout_instance_t
*sout_NewInstance( vlc_object_t
*p_parent
, const char *psz_dest
)
85 sout_instance_t
*p_sout
;
88 if( psz_dest
&& psz_dest
[0] == '#' )
90 psz_chain
= strdup( &psz_dest
[1] );
94 psz_chain
= sout_stream_url_to_chain(
95 var_InheritBool(p_parent
, "sout-display"), psz_dest
);
100 /* *** Allocate descriptor *** */
101 p_sout
= vlc_custom_create( p_parent
, sizeof( *p_sout
), "stream output" );
105 msg_Dbg( p_sout
, "using sout chain=`%s'", psz_chain
);
107 /* *** init descriptor *** */
108 p_sout
->psz_sout
= strdup( psz_dest
);
109 p_sout
->p_meta
= NULL
;
110 p_sout
->i_out_pace_nocontrol
= 0;
111 p_sout
->p_sys
= NULL
;
113 vlc_mutex_init( &p_sout
->lock
);
114 p_sout
->p_stream
= NULL
;
116 var_Create( p_sout
, "sout-mux-caching", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
118 p_sout
->p_stream
= sout_StreamChainNew( p_sout
, psz_chain
, NULL
, NULL
);
119 if( p_sout
->p_stream
)
125 msg_Err( p_sout
, "stream chain failed for `%s'", psz_chain
);
128 FREENULL( p_sout
->psz_sout
);
130 vlc_object_release( p_sout
);
134 /*****************************************************************************
135 * sout_DeleteInstance: delete a previously allocated instance
136 *****************************************************************************/
137 void sout_DeleteInstance( sout_instance_t
* p_sout
)
139 /* remove the stream out chain */
140 sout_StreamChainDelete( p_sout
->p_stream
, NULL
);
142 /* *** free all string *** */
143 FREENULL( p_sout
->psz_sout
);
148 vlc_meta_Delete( p_sout
->p_meta
);
151 vlc_mutex_destroy( &p_sout
->lock
);
153 /* *** free structure *** */
154 vlc_object_release( p_sout
);
157 /*****************************************************************************
159 *****************************************************************************/
160 sout_packetizer_input_t
*sout_InputNew( sout_instance_t
*p_sout
,
163 sout_packetizer_input_t
*p_input
;
165 /* *** create a packetizer input *** */
166 p_input
= malloc( sizeof( sout_packetizer_input_t
) );
167 if( !p_input
) return NULL
;
168 p_input
->p_sout
= p_sout
;
169 p_input
->p_fmt
= p_fmt
;
171 msg_Dbg( p_sout
, "adding a new sout input (sout_input:%p)", p_input
);
173 if( p_fmt
->i_codec
== VLC_CODEC_NULL
)
175 vlc_object_release( p_sout
);
179 /* *** add it to the stream chain */
180 vlc_mutex_lock( &p_sout
->lock
);
181 p_input
->id
= p_sout
->p_stream
->pf_add( p_sout
->p_stream
, p_fmt
);
182 vlc_mutex_unlock( &p_sout
->lock
);
184 if( p_input
->id
== NULL
)
193 /*****************************************************************************
195 *****************************************************************************/
196 int sout_InputDelete( sout_packetizer_input_t
*p_input
)
198 sout_instance_t
*p_sout
= p_input
->p_sout
;
200 msg_Dbg( p_sout
, "removing a sout input (sout_input:%p)", p_input
);
202 if( p_input
->p_fmt
->i_codec
!= VLC_CODEC_NULL
)
204 vlc_mutex_lock( &p_sout
->lock
);
205 p_sout
->p_stream
->pf_del( p_sout
->p_stream
, p_input
->id
);
206 vlc_mutex_unlock( &p_sout
->lock
);
211 return( VLC_SUCCESS
);
214 /*****************************************************************************
216 *****************************************************************************/
217 int sout_InputSendBuffer( sout_packetizer_input_t
*p_input
,
220 sout_instance_t
*p_sout
= p_input
->p_sout
;
223 if( p_input
->p_fmt
->i_codec
== VLC_CODEC_NULL
)
225 block_Release( p_buffer
);
229 if( p_buffer
->i_dts
<= VLC_TS_INVALID
)
231 msg_Warn( p_sout
, "trying to send non-dated packet to stream output!");
232 block_Release( p_buffer
);
236 vlc_mutex_lock( &p_sout
->lock
);
237 i_ret
= p_sout
->p_stream
->pf_send( p_sout
->p_stream
,
238 p_input
->id
, p_buffer
);
239 vlc_mutex_unlock( &p_sout
->lock
);
244 #undef sout_AccessOutNew
245 /*****************************************************************************
246 * sout_AccessOutNew: allocate a new access out
247 *****************************************************************************/
248 sout_access_out_t
*sout_AccessOutNew( vlc_object_t
*p_sout
,
249 const char *psz_access
, const char *psz_name
)
251 sout_access_out_t
*p_access
;
254 p_access
= vlc_custom_create( p_sout
, sizeof( *p_access
), "access out" );
258 psz_next
= config_ChainCreate( &p_access
->psz_access
, &p_access
->p_cfg
,
261 p_access
->psz_path
= strdup( psz_name
? psz_name
: "" );
262 p_access
->p_sys
= NULL
;
263 p_access
->pf_seek
= NULL
;
264 p_access
->pf_read
= NULL
;
265 p_access
->pf_write
= NULL
;
266 p_access
->pf_control
= NULL
;
267 p_access
->p_module
= NULL
;
269 p_access
->i_writes
= 0;
270 p_access
->i_sent_bytes
= 0;
273 module_need( p_access
, "sout access", p_access
->psz_access
, true );
275 if( !p_access
->p_module
)
277 free( p_access
->psz_access
);
278 free( p_access
->psz_path
);
279 vlc_object_release( p_access
);
285 /*****************************************************************************
286 * sout_AccessDelete: delete an access out
287 *****************************************************************************/
288 void sout_AccessOutDelete( sout_access_out_t
*p_access
)
290 if( p_access
->p_module
)
292 module_unneed( p_access
, p_access
->p_module
);
294 free( p_access
->psz_access
);
296 config_ChainDestroy( p_access
->p_cfg
);
298 free( p_access
->psz_path
);
300 vlc_object_release( p_access
);
303 /*****************************************************************************
305 *****************************************************************************/
306 int sout_AccessOutSeek( sout_access_out_t
*p_access
, off_t i_pos
)
308 return p_access
->pf_seek( p_access
, i_pos
);
311 /*****************************************************************************
313 *****************************************************************************/
314 ssize_t
sout_AccessOutRead( sout_access_out_t
*p_access
, block_t
*p_buffer
)
316 return( p_access
->pf_read
?
317 p_access
->pf_read( p_access
, p_buffer
) : VLC_EGENERIC
);
320 /*****************************************************************************
322 *****************************************************************************/
323 ssize_t
sout_AccessOutWrite( sout_access_out_t
*p_access
, block_t
*p_buffer
)
325 return p_access
->pf_write( p_access
, p_buffer
);
329 * sout_AccessOutControl
331 int sout_AccessOutControl (sout_access_out_t
*access
, int query
, ...)
336 va_start (ap
, query
);
337 if (access
->pf_control
)
338 ret
= access
->pf_control (access
, query
, ap
);
345 /*****************************************************************************
346 * sout_MuxNew: create a new mux
347 *****************************************************************************/
348 sout_mux_t
* sout_MuxNew( sout_instance_t
*p_sout
, const char *psz_mux
,
349 sout_access_out_t
*p_access
)
354 p_mux
= vlc_custom_create( p_sout
, sizeof( *p_mux
), "mux" );
358 p_mux
->p_sout
= p_sout
;
359 psz_next
= config_ChainCreate( &p_mux
->psz_mux
, &p_mux
->p_cfg
, psz_mux
);
362 p_mux
->p_access
= p_access
;
363 p_mux
->pf_control
= NULL
;
364 p_mux
->pf_addstream
= NULL
;
365 p_mux
->pf_delstream
= NULL
;
366 p_mux
->pf_mux
= NULL
;
367 p_mux
->i_nb_inputs
= 0;
368 p_mux
->pp_inputs
= NULL
;
371 p_mux
->p_module
= NULL
;
373 p_mux
->b_add_stream_any_time
= false;
374 p_mux
->b_waiting_stream
= true;
375 p_mux
->i_add_stream_start
= -1;
378 module_need( p_mux
, "sout mux", p_mux
->psz_mux
, true );
380 if( p_mux
->p_module
== NULL
)
382 FREENULL( p_mux
->psz_mux
);
384 vlc_object_release( p_mux
);
388 /* *** probe mux capacity *** */
389 if( p_mux
->pf_control
)
391 int b_answer
= false;
393 if( sout_MuxControl( p_mux
, MUX_CAN_ADD_STREAM_WHILE_MUXING
,
401 msg_Dbg( p_sout
, "muxer support adding stream at any time" );
402 p_mux
->b_add_stream_any_time
= true;
403 p_mux
->b_waiting_stream
= false;
405 /* If we control the output pace then it's better to wait before
406 * starting muxing (generates better streams/files). */
407 if( !p_sout
->i_out_pace_nocontrol
)
411 else if( sout_MuxControl( p_mux
, MUX_GET_ADD_STREAM_WAIT
,
419 msg_Dbg( p_sout
, "muxer prefers to wait for all ES before "
421 p_mux
->b_waiting_stream
= true;
429 /*****************************************************************************
431 *****************************************************************************/
432 void sout_MuxDelete( sout_mux_t
*p_mux
)
434 if( p_mux
->p_module
)
436 module_unneed( p_mux
, p_mux
->p_module
);
438 free( p_mux
->psz_mux
);
440 config_ChainDestroy( p_mux
->p_cfg
);
442 vlc_object_release( p_mux
);
445 /*****************************************************************************
447 *****************************************************************************/
448 sout_input_t
*sout_MuxAddStream( sout_mux_t
*p_mux
, es_format_t
*p_fmt
)
450 sout_input_t
*p_input
;
452 if( !p_mux
->b_add_stream_any_time
&& !p_mux
->b_waiting_stream
)
454 msg_Err( p_mux
, "cannot add a new stream (unsupported while muxing "
455 "to this format). You can try increasing sout-mux-caching value" );
459 msg_Dbg( p_mux
, "adding a new input" );
461 /* create a new sout input */
462 p_input
= malloc( sizeof( sout_input_t
) );
465 p_input
->p_sout
= p_mux
->p_sout
;
466 p_input
->p_fmt
= p_fmt
;
467 p_input
->p_fifo
= block_FifoNew();
468 p_input
->p_sys
= NULL
;
470 TAB_APPEND( p_mux
->i_nb_inputs
, p_mux
->pp_inputs
, p_input
);
471 if( p_mux
->pf_addstream( p_mux
, p_input
) < 0 )
473 msg_Err( p_mux
, "cannot add this stream" );
474 TAB_REMOVE( p_mux
->i_nb_inputs
, p_mux
->pp_inputs
, p_input
);
475 block_FifoRelease( p_input
->p_fifo
);
483 /*****************************************************************************
484 * sout_MuxDeleteStream:
485 *****************************************************************************/
486 void sout_MuxDeleteStream( sout_mux_t
*p_mux
, sout_input_t
*p_input
)
490 if( p_mux
->b_waiting_stream
491 && block_FifoCount( p_input
->p_fifo
) > 0 )
493 /* We stop waiting, and call the muxer for taking care of the data
494 * before we remove this es */
495 p_mux
->b_waiting_stream
= false;
496 p_mux
->pf_mux( p_mux
);
499 TAB_FIND( p_mux
->i_nb_inputs
, p_mux
->pp_inputs
, p_input
, i_index
);
502 if( p_mux
->pf_delstream( p_mux
, p_input
) < 0 )
504 msg_Err( p_mux
, "cannot delete this stream from mux" );
507 /* remove the entry */
508 TAB_REMOVE( p_mux
->i_nb_inputs
, p_mux
->pp_inputs
, p_input
);
510 if( p_mux
->i_nb_inputs
== 0 )
512 msg_Warn( p_mux
, "no more input streams for this mux" );
515 block_FifoRelease( p_input
->p_fifo
);
520 /*****************************************************************************
521 * sout_MuxSendBuffer:
522 *****************************************************************************/
523 void sout_MuxSendBuffer( sout_mux_t
*p_mux
, sout_input_t
*p_input
,
526 block_FifoPut( p_input
->p_fifo
, p_buffer
);
528 if( p_mux
->p_sout
->i_out_pace_nocontrol
)
530 mtime_t current_date
= mdate();
531 if ( current_date
> p_buffer
->i_dts
)
532 msg_Warn( p_mux
, "late buffer for mux input (%"PRId64
")",
533 current_date
- p_buffer
->i_dts
);
536 if( p_mux
->b_waiting_stream
)
538 const int64_t i_caching
= var_GetInteger( p_mux
->p_sout
, "sout-mux-caching" ) * INT64_C(1000);
540 if( p_mux
->i_add_stream_start
< 0 )
541 p_mux
->i_add_stream_start
= p_buffer
->i_dts
;
543 /* Wait until we have enought data before muxing */
544 if( p_mux
->i_add_stream_start
< 0 ||
545 p_buffer
->i_dts
< p_mux
->i_add_stream_start
+ i_caching
)
547 p_mux
->b_waiting_stream
= false;
549 p_mux
->pf_mux( p_mux
);
553 /*****************************************************************************
554 * sout_MuxGetStream: find stream to be muxed
555 *****************************************************************************/
556 int sout_MuxGetStream( sout_mux_t
*p_mux
, int i_blocks
, mtime_t
*pi_dts
)
561 for( int i
= 0; i
< p_mux
->i_nb_inputs
; i
++ )
563 sout_input_t
*p_input
= p_mux
->pp_inputs
[i
];
566 if( block_FifoCount( p_input
->p_fifo
) < i_blocks
)
568 if( p_input
->p_fmt
->i_cat
!= SPU_ES
)
572 /* FIXME: SPU muxing */
576 p_data
= block_FifoShow( p_input
->p_fifo
);
577 if( i_stream
< 0 || p_data
->i_dts
< i_dts
)
580 i_dts
= p_data
->i_dts
;
584 if( pi_dts
) *pi_dts
= i_dts
;
590 /*****************************************************************************
592 *****************************************************************************/
593 static int mrl_Parse( mrl_t
*p_mrl
, const char *psz_mrl
)
595 char * psz_dup
= strdup( psz_mrl
);
596 char * psz_parser
= psz_dup
;
597 const char * psz_access
;
598 const char * psz_way
;
601 /* *** first parse psz_dest */
602 while( *psz_parser
&& *psz_parser
!= ':' )
604 if( *psz_parser
== '{' )
606 while( *psz_parser
&& *psz_parser
!= '}' )
620 #if defined( WIN32 ) || defined( UNDER_CE ) || defined( __OS2__ )
621 if( psz_parser
- psz_dup
== 1 )
623 /* msg_Warn( p_sout, "drive letter %c: found in source string",
631 psz_access
= psz_way
= "";
636 *psz_parser
++ = '\0';
638 /* let's skip '//' */
639 if( psz_parser
[0] == '/' && psz_parser
[1] == '/' )
644 psz_name
= psz_parser
;
646 /* Come back to parse the access and mux plug-ins */
647 psz_parser
= psz_dup
;
654 else if( *psz_parser
== '/' )
662 psz_access
= psz_parser
;
664 while( *psz_parser
&& *psz_parser
!= '/' )
666 if( *psz_parser
== '{' )
668 while( *psz_parser
&& *psz_parser
!= '}' )
683 if( *psz_parser
== '/' )
685 *psz_parser
++ = '\0';
696 psz_way
= psz_parser
;
700 p_mrl
->psz_access
= strdup( psz_access
);
701 p_mrl
->psz_way
= strdup( psz_way
);
702 p_mrl
->psz_name
= strdup( psz_name
);
705 return( VLC_SUCCESS
);
709 /* mrl_Clean: clean p_mrl after a call to mrl_Parse */
710 static void mrl_Clean( mrl_t
*p_mrl
)
712 FREENULL( p_mrl
->psz_access
);
713 FREENULL( p_mrl
->psz_way
);
714 FREENULL( p_mrl
->psz_name
);
718 /****************************************************************************
719 ****************************************************************************
723 ****************************************************************************
724 ****************************************************************************/
726 /* Destroy a "stream_out" module */
727 static void sout_StreamDelete( sout_stream_t
*p_stream
)
729 msg_Dbg( p_stream
, "destroying chain... (name=%s)", p_stream
->psz_name
);
731 if( p_stream
->p_module
) module_unneed( p_stream
, p_stream
->p_module
);
733 FREENULL( p_stream
->psz_name
);
735 config_ChainDestroy( p_stream
->p_cfg
);
737 msg_Dbg( p_stream
, "destroying chain done" );
738 vlc_object_release( p_stream
);
741 /* Destroy a "stream_out" modules chain
743 * p_first is the first module to be destroyed in the chain
744 * p_last is the last module to be destroyed
745 * if NULL, all modules are destroyed
746 * if not NULL, modules following it must be destroyed separately
748 void sout_StreamChainDelete(sout_stream_t
*p_first
, sout_stream_t
*p_last
)
750 while(p_first
!= NULL
)
752 sout_stream_t
*p_next
= p_first
->p_next
;
754 sout_StreamDelete(p_first
);
755 if(p_first
== p_last
)
761 /* Create a "stream_out" module, which may forward its ES to p_next module */
763 * XXX name and p_cfg are used (-> do NOT free them)
765 static sout_stream_t
*sout_StreamNew( sout_instance_t
*p_sout
, char *psz_name
,
766 config_chain_t
*p_cfg
, sout_stream_t
*p_next
)
768 sout_stream_t
*p_stream
;
772 p_stream
= vlc_custom_create( p_sout
, sizeof( *p_stream
), "stream out" );
776 p_stream
->p_sout
= p_sout
;
777 p_stream
->p_sys
= NULL
;
778 p_stream
->psz_name
= psz_name
;
779 p_stream
->p_cfg
= p_cfg
;
780 p_stream
->p_next
= p_next
;
782 msg_Dbg( p_sout
, "stream=`%s'", p_stream
->psz_name
);
785 module_need( p_stream
, "sout stream", p_stream
->psz_name
, true );
787 if( !p_stream
->p_module
)
789 /* those must be freed by the caller if creation failed */
790 p_stream
->psz_name
= NULL
;
791 p_stream
->p_cfg
= NULL
;
793 sout_StreamDelete( p_stream
);
800 /* Creates a complete "stream_out" modules chain
802 * chain format: module1{option=*:option=*}[:module2{option=*:...}]
804 * The modules are created starting from the last one and linked together
805 * A pointer to the last module created is stored if pp_last isn't NULL, to
806 * make sure sout_StreamChainDelete doesn't delete modules created in another
809 * Returns a pointer to the first module.
811 sout_stream_t
*sout_StreamChainNew(sout_instance_t
*p_sout
, char *psz_chain
,
812 sout_stream_t
*p_next
, sout_stream_t
**pp_last
)
814 if(!psz_chain
|| !*psz_chain
)
816 if(pp_last
) *pp_last
= NULL
;
820 char *psz_parser
= strdup(psz_chain
);
824 vlc_array_t cfg
, name
;
825 vlc_array_init(&cfg
);
826 vlc_array_init(&name
);
831 config_chain_t
*p_cfg
;
833 psz_chain
= config_ChainCreate( &psz_name
, &p_cfg
, psz_parser
);
835 psz_parser
= psz_chain
;
837 vlc_array_append(&cfg
, p_cfg
);
838 vlc_array_append(&name
, psz_name
);
841 int i
= vlc_array_count(&name
);
843 vlc_array_init(&module
);
846 p_next
= sout_StreamNew( p_sout
, vlc_array_item_at_index(&name
, i
),
847 vlc_array_item_at_index(&cfg
, i
), p_next
);
852 if(i
== vlc_array_count(&name
) - 1 && pp_last
)
853 *pp_last
= p_next
; /* last module created in the chain */
855 vlc_array_append(&module
, p_next
);
858 vlc_array_clear(&name
);
859 vlc_array_clear(&cfg
);
860 vlc_array_clear(&module
);
866 i
++; /* last module couldn't be created */
868 /* destroy all modules created, starting with the last one */
869 int modules
= vlc_array_count(&module
);
871 sout_StreamDelete(vlc_array_item_at_index(&module
, modules
));
872 vlc_array_clear(&module
);
874 /* then destroy all names and config which weren't destroyed by
875 * sout_StreamDelete */
878 free(vlc_array_item_at_index(&name
, i
));
879 config_ChainDestroy(vlc_array_item_at_index(&cfg
, i
));
881 vlc_array_clear(&name
);
882 vlc_array_clear(&cfg
);
887 static char *sout_stream_url_to_chain( bool b_sout_display
,
888 const char *psz_url
)
893 mrl_Parse( &mrl
, psz_url
);
895 /* Check if the URLs goes to #rtp - otherwise we'll use #standard */
896 static const char rtplist
[] = "dccp\0sctp\0tcp\0udplite\0";
897 for (const char *a
= rtplist
; *a
; a
+= strlen (a
) + 1)
898 if (strcmp (a
, mrl
.psz_access
) == 0)
901 if (strcmp (mrl
.psz_access
, "rtp") == 0)
904 /* For historical reasons, rtp:// means RTP over UDP */
905 strcpy (mrl
.psz_access
, "udp");
907 if (mrl
.psz_name
[0] == '[')
909 port
= strstr (mrl
.psz_name
, "]:");
914 port
= strchr (mrl
.psz_name
, ':');
916 *port
++ = '\0'; /* erase ':' */
918 if (asprintf (&psz_chain
,
919 "rtp{mux=\"%s\",proto=\"%s\",dst=\"%s%s%s\"}",
920 mrl
.psz_way
, mrl
.psz_access
, mrl
.psz_name
,
921 port
? "\",port=\"" : "", port
? port
: "") == -1)
926 /* Convert the URL to a basic standard sout chain */
927 if (asprintf (&psz_chain
,
928 "standard{mux=\"%s\",access=\"%s\",dst=\"%s\"}",
929 mrl
.psz_way
, mrl
.psz_access
, mrl
.psz_name
) == -1)
933 /* Duplicate and wrap if sout-display is on */
934 if (psz_chain
&& b_sout_display
)
937 if (asprintf (&tmp
, "duplicate{dst=display,dst=%s}", psz_chain
) == -1)
947 #undef sout_EncoderCreate
948 encoder_t
*sout_EncoderCreate( vlc_object_t
*p_this
)
950 return vlc_custom_create( p_this
, sizeof( encoder_t
), "encoder" );