]> git.sesse.net Git - vlc/blob - modules/stream_out/delay.c
skins2(Win32): wParam to be stripped of 4 bits (see msdn doc)
[vlc] / modules / stream_out / delay.c
1 /*****************************************************************************
2  * delay.c: delay a stream
3  *****************************************************************************
4  * Copyright (C) 2011 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
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.
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 General Public License for more details.
18  *
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  *****************************************************************************/
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_t   *p_out;
82     sout_stream_id_t *id;
83     int i_id;
84     mtime_t i_delay;
85 };
86
87 /*****************************************************************************
88  * Open:
89  *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
91 {
92     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
93     sout_stream_sys_t *p_sys;
94
95     if( !p_stream->p_next )
96     {
97         msg_Err( p_stream, "cannot create chain" );
98         return VLC_EGENERIC;
99     }
100
101     p_sys = calloc( 1, sizeof( sout_stream_sys_t ) );
102     if( !p_sys )
103         return VLC_ENOMEM;
104
105
106     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
107                    p_stream->p_cfg );
108
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" );
111
112     p_stream->pf_add    = Add;
113     p_stream->pf_del    = Del;
114     p_stream->pf_send   = Send;
115
116     p_stream->p_sys     = p_sys;
117
118     return VLC_SUCCESS;
119 }
120
121 /*****************************************************************************
122  * Close:
123  *****************************************************************************/
124 static void Close( vlc_object_t * p_this )
125 {
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;
128
129     free( p_sys );
130 }
131
132 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
133 {
134     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
135
136     if ( p_fmt->i_id == p_sys->i_id )
137     {
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 );
141         return p_sys->id;
142     }
143
144
145     return p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
146 }
147
148 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
149 {
150     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
151
152     if ( id == p_sys->id )
153         p_sys->id = NULL;
154
155     return p_sys->p_out->pf_del( p_sys->p_out, id );
156 }
157
158 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
159                  block_t *p_buffer )
160 {
161     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
162
163     if ( id == p_sys->id )
164     {
165         block_t *p_block = p_buffer;
166         while ( p_block != NULL )
167         {
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;
173         }
174     }
175
176     return p_sys->p_out->pf_send( p_sys->p_out, id, p_buffer );
177 }