1 /*****************************************************************************
2 * delay.c: delay a stream
3 *****************************************************************************
4 * Copyright (C) 2011 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, 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_t *Add ( sout_stream_t *, es_format_t * );
76 static int Del ( sout_stream_t *, sout_stream_id_t * );
77 static int Send ( sout_stream_t *, sout_stream_id_t *, block_t * );
79 struct sout_stream_sys_t
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
92 sout_stream_t *p_stream = (sout_stream_t*)p_this;
93 sout_stream_sys_t *p_sys;
95 if( !p_stream->p_next )
97 msg_Err( p_stream, "cannot create chain" );
101 p_sys = calloc( 1, sizeof( sout_stream_sys_t ) );
106 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
109 p_sys->i_id = var_GetInteger( p_stream, SOUT_CFG_PREFIX "id" );
110 p_sys->i_delay = 1000 * var_GetInteger( p_stream, SOUT_CFG_PREFIX "delay" );
112 p_stream->pf_add = Add;
113 p_stream->pf_del = Del;
114 p_stream->pf_send = Send;
116 p_stream->p_sys = p_sys;
121 /*****************************************************************************
123 *****************************************************************************/
124 static void Close( vlc_object_t * p_this )
126 sout_stream_t *p_stream = (sout_stream_t*)p_this;
127 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
132 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
134 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
136 if ( p_fmt->i_id == p_sys->i_id )
138 msg_Dbg( p_stream, "delaying ID %d by %"PRId64,
139 p_sys->i_id, p_sys->i_delay );
140 p_sys->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
145 return p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
148 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
150 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
152 if ( id == p_sys->id )
155 return p_sys->p_out->pf_del( p_sys->p_out, id );
158 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
161 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
163 if ( id == p_sys->id )
165 block_t *p_block = p_buffer;
166 while ( p_block != NULL )
168 if ( p_block->i_pts && p_block->i_pts != VLC_TS_INVALID )
169 p_block->i_pts += p_sys->i_delay;
170 if ( p_block->i_dts && p_block->i_dts != VLC_TS_INVALID )
171 p_block->i_dts += p_sys->i_delay;
172 p_block = p_block->p_next;
176 return p_sys->p_out->pf_send( p_sys->p_out, id, p_buffer );