]> git.sesse.net Git - vlc/blob - modules/stream_out/delay.c
contrib: shout: skip if not building encoders
[vlc] / modules / stream_out / delay.c
1 /*****************************************************************************
2  * delay.c: delay a stream
3  *****************************************************************************
4  * Copyright © 2009-2011 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_sout.h>
34 #include <vlc_block.h>
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 #define ID_TEXT N_("Elementary Stream ID")
40 #define ID_LONGTEXT N_( \
41     "Specify an identifier integer for this elementary stream" )
42
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." )
47
48 static int  Open    ( vlc_object_t * );
49 static void Close   ( vlc_object_t * );
50
51 #define SOUT_CFG_PREFIX "sout-delay-"
52
53 vlc_module_begin()
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,
62                  false )
63     add_integer( SOUT_CFG_PREFIX "delay", 0, DELAY_TEXT, DELAY_LONGTEXT,
64                  false )
65 vlc_module_end()
66
67
68 /*****************************************************************************
69  * Local prototypes
70  *****************************************************************************/
71 static const char *ppsz_sout_options[] = {
72     "id", "delay", NULL
73 };
74
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 * );
78
79 struct sout_stream_sys_t
80 {
81     sout_stream_id_t *id;
82     int i_id;
83     mtime_t i_delay;
84 };
85
86 /*****************************************************************************
87  * Open:
88  *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
90 {
91     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
92     sout_stream_sys_t *p_sys;
93
94     if( !p_stream->p_next )
95     {
96         msg_Err( p_stream, "cannot create chain" );
97         return VLC_EGENERIC;
98     }
99
100     p_sys = calloc( 1, sizeof( sout_stream_sys_t ) );
101     if( !p_sys )
102         return VLC_ENOMEM;
103
104
105     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
106                    p_stream->p_cfg );
107
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" );
110
111     p_stream->pf_add    = Add;
112     p_stream->pf_del    = Del;
113     p_stream->pf_send   = Send;
114
115     p_stream->p_sys     = p_sys;
116
117     return VLC_SUCCESS;
118 }
119
120 /*****************************************************************************
121  * Close:
122  *****************************************************************************/
123 static void Close( vlc_object_t * p_this )
124 {
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;
127
128     free( p_sys );
129 }
130
131 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
132 {
133     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
134
135     if ( p_fmt->i_id == p_sys->i_id )
136     {
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 );
140         return p_sys->id;
141     }
142
143     return p_stream->p_next->pf_add( p_stream->p_next, p_fmt );
144 }
145
146 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
147 {
148     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
149
150     if ( id == p_sys->id )
151         p_sys->id = NULL;
152
153     return p_stream->p_next->pf_del( p_stream->p_next, id );
154 }
155
156 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
157                  block_t *p_buffer )
158 {
159     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
160
161     if ( id == p_sys->id )
162     {
163         block_t *p_block = p_buffer;
164         while ( p_block != NULL )
165         {
166             if ( p_block->i_pts && p_block->i_pts != VLC_TS_INVALID )
167                 p_block->i_pts += p_sys->i_delay;
168             if ( p_block->i_dts && p_block->i_dts != VLC_TS_INVALID )
169                 p_block->i_dts += p_sys->i_delay;
170             p_block = p_block->p_next;
171         }
172     }
173
174     return p_stream->p_next->pf_send( p_stream->p_next, id, p_buffer );
175 }