1 /*****************************************************************************
2 * delay.c: delay a stream
3 *****************************************************************************
4 * Copyright © 2009-2011 VLC authors and VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 #include <vlc_block.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 #define ID_TEXT N_("Elementary Stream ID")
40 #define ID_LONGTEXT N_( \
41 "Specify an identifier integer for this elementary stream" )
43 #define DELAY_TEXT N_("Delay of the ES (ms)")
44 #define DELAY_LONGTEXT N_( \
45 "Specify a delay (in ms) for this elementary stream. " \
46 "Positive means delay and negative means advance." )
48 static int Open ( vlc_object_t
* );
49 static void Close ( vlc_object_t
* );
51 #define SOUT_CFG_PREFIX "sout-delay-"
54 set_shortname( N_("Delay"))
55 set_description( N_("Delay a stream"))
56 set_capability( "sout stream", 50 )
57 add_shortcut( "delay" )
58 set_category( CAT_SOUT
)
59 set_subcategory( SUBCAT_SOUT_STREAM
)
60 set_callbacks( Open
, Close
)
61 add_integer( SOUT_CFG_PREFIX
"id", 0, ID_TEXT
, ID_LONGTEXT
,
63 add_integer( SOUT_CFG_PREFIX
"delay", 0, DELAY_TEXT
, DELAY_LONGTEXT
,
68 /*****************************************************************************
70 *****************************************************************************/
71 static const char *ppsz_sout_options
[] = {
75 static sout_stream_id_sys_t
*Add( sout_stream_t
*, const es_format_t
* );
76 static void Del ( sout_stream_t
*, sout_stream_id_sys_t
* );
77 static int Send ( sout_stream_t
*, sout_stream_id_sys_t
*, block_t
* );
79 struct sout_stream_sys_t
81 sout_stream_id_sys_t
*id
;
86 /*****************************************************************************
88 *****************************************************************************/
89 static int Open( vlc_object_t
*p_this
)
91 sout_stream_t
*p_stream
= (sout_stream_t
*)p_this
;
92 sout_stream_sys_t
*p_sys
;
94 if( !p_stream
->p_next
)
96 msg_Err( p_stream
, "cannot create chain" );
100 p_sys
= calloc( 1, sizeof( sout_stream_sys_t
) );
105 config_ChainParse( p_stream
, SOUT_CFG_PREFIX
, ppsz_sout_options
,
108 p_sys
->i_id
= var_GetInteger( p_stream
, SOUT_CFG_PREFIX
"id" );
109 p_sys
->i_delay
= 1000 * var_GetInteger( p_stream
, SOUT_CFG_PREFIX
"delay" );
111 p_stream
->pf_add
= Add
;
112 p_stream
->pf_del
= Del
;
113 p_stream
->pf_send
= Send
;
115 p_stream
->p_sys
= p_sys
;
120 /*****************************************************************************
122 *****************************************************************************/
123 static void Close( vlc_object_t
* p_this
)
125 sout_stream_t
*p_stream
= (sout_stream_t
*)p_this
;
126 sout_stream_sys_t
*p_sys
= (sout_stream_sys_t
*)p_stream
->p_sys
;
131 static sout_stream_id_sys_t
* Add( sout_stream_t
*p_stream
, const es_format_t
*p_fmt
)
133 sout_stream_sys_t
*p_sys
= (sout_stream_sys_t
*)p_stream
->p_sys
;
135 if ( p_fmt
->i_id
== p_sys
->i_id
)
137 msg_Dbg( p_stream
, "delaying ID %d by %"PRId64
,
138 p_sys
->i_id
, p_sys
->i_delay
);
139 p_sys
->id
= p_stream
->p_next
->pf_add( p_stream
->p_next
, p_fmt
);
143 return p_stream
->p_next
->pf_add( p_stream
->p_next
, p_fmt
);
146 static void Del( sout_stream_t
*p_stream
, sout_stream_id_sys_t
*id
)
148 sout_stream_sys_t
*p_sys
= (sout_stream_sys_t
*)p_stream
->p_sys
;
150 if ( id
== p_sys
->id
)
153 p_stream
->p_next
->pf_del( p_stream
->p_next
, id
);
156 static int Send( sout_stream_t
*p_stream
, sout_stream_id_sys_t
*id
,
159 sout_stream_sys_t
*p_sys
= (sout_stream_sys_t
*)p_stream
->p_sys
;
161 if ( id
== p_sys
->id
)
163 block_t
*p_block
= p_buffer
;
164 while ( p_block
!= NULL
)
166 if ( p_block
->i_pts
!= VLC_TS_INVALID
)
167 p_block
->i_pts
+= p_sys
->i_delay
;
168 if ( p_block
->i_dts
!= VLC_TS_INVALID
)
169 p_block
->i_dts
+= p_sys
->i_delay
;
170 p_block
= p_block
->p_next
;
174 return p_stream
->p_next
->pf_send( p_stream
->p_next
, id
, p_buffer
);