]> git.sesse.net Git - vlc/blob - modules/stream_out/display.c
* all: cosmetics.
[vlc] / modules / stream_out / display.c
1 /*****************************************************************************
2  * display.c: display stream output module
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@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 #include <stdlib.h>
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32 #include <vlc/sout.h>
33
34 /*****************************************************************************
35  * Module descriptor
36  *****************************************************************************/
37 #define AUDIO_TEXT N_("Enable audio")
38 #define AUDIO_LONGTEXT N_( "Enable/disable audio rendering." )
39 #define VIDEO_TEXT N_("Enable video")
40 #define VIDEO_LONGTEXT N_( "Enable/disable video rendering." )
41 #define DELAY_TEXT N_("Delay")
42 #define DELAY_LONGTEXT N_( "Introduces a delay in the display of the stream." )
43
44 static int  Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
46
47 #define SOUT_CFG_PREFIX "sout-display-"
48
49 vlc_module_begin();
50     set_description( _("Display stream output") );
51     set_capability( "sout stream", 50 );
52     add_shortcut( "display" );
53     add_bool( SOUT_CFG_PREFIX "audio", 1, NULL, AUDIO_TEXT,
54               AUDIO_LONGTEXT, VLC_TRUE );
55     add_bool( SOUT_CFG_PREFIX "video", 1, NULL, VIDEO_TEXT,
56               VIDEO_LONGTEXT, VLC_TRUE );
57     add_integer( SOUT_CFG_PREFIX "delay", 100, NULL, DELAY_TEXT,
58                  DELAY_LONGTEXT, VLC_TRUE );
59     set_callbacks( Open, Close );
60 vlc_module_end();
61
62
63 /*****************************************************************************
64  * Exported prototypes
65  *****************************************************************************/
66 static const char *ppsz_sout_options[] = {
67     "audio", "video", "delay", NULL
68 };
69
70 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
71 static int               Del ( sout_stream_t *, sout_stream_id_t * );
72 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
73
74 struct sout_stream_sys_t
75 {
76     input_thread_t *p_input;
77
78     vlc_bool_t     b_audio;
79     vlc_bool_t     b_video;
80
81     mtime_t        i_delay;
82 };
83
84 /*****************************************************************************
85  * Open:
86  *****************************************************************************/
87 static int Open( vlc_object_t *p_this )
88 {
89     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
90     sout_stream_sys_t *p_sys;
91     vlc_value_t val;
92
93     sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
94                    p_stream->p_cfg );
95
96     p_sys          = malloc( sizeof( sout_stream_sys_t ) );
97     p_sys->p_input = vlc_object_find( p_stream, VLC_OBJECT_INPUT,
98                                       FIND_ANYWHERE );
99     if( !p_sys->p_input )
100     {
101         msg_Err( p_stream, "cannot find p_input" );
102         free( p_sys );
103         return VLC_EGENERIC;
104     }
105
106     var_Get( p_stream, SOUT_CFG_PREFIX "audio", &val );
107     p_sys->b_audio = val.b_bool;
108
109     var_Get( p_stream, SOUT_CFG_PREFIX "video", &val );
110     p_sys->b_video = val.b_bool;
111
112     var_Get( p_stream, SOUT_CFG_PREFIX "delay", &val );
113     p_sys->i_delay = (int64_t)val.i_int * 1000;
114
115     p_stream->pf_add    = Add;
116     p_stream->pf_del    = Del;
117     p_stream->pf_send   = Send;
118
119     p_stream->p_sys     = p_sys;
120
121     /* update p_sout->i_out_pace_nocontrol */
122     p_stream->p_sout->i_out_pace_nocontrol++;
123
124     return VLC_SUCCESS;
125 }
126
127 /*****************************************************************************
128  * Close:
129  *****************************************************************************/
130 static void Close( vlc_object_t * p_this )
131 {
132     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
133     sout_stream_sys_t *p_sys = p_stream->p_sys;
134
135     /* update p_sout->i_out_pace_nocontrol */
136     p_stream->p_sout->i_out_pace_nocontrol--;
137
138     vlc_object_release( p_sys->p_input );
139
140     free( p_sys );
141 }
142
143 struct sout_stream_id_t
144 {
145     es_descriptor_t *p_es;
146 };
147
148 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
149 {
150     sout_stream_sys_t *p_sys = p_stream->p_sys;
151     sout_stream_id_t *id;
152
153     if( ( p_fmt->i_cat == AUDIO_ES && !p_sys->b_audio )||
154         ( p_fmt->i_cat == VIDEO_ES && !p_sys->b_video ) )
155     {
156         return NULL;
157     }
158
159     id = malloc( sizeof( sout_stream_id_t ) );
160
161     id->p_es = malloc( sizeof( es_descriptor_t ) );
162     memset( id->p_es, 0, sizeof( es_descriptor_t ) );
163     id->p_es->i_cat         = p_fmt->i_cat;
164     id->p_es->i_fourcc      = p_fmt->i_codec;
165     id->p_es->b_force_decoder = VLC_TRUE;
166     es_format_Copy( &id->p_es->fmt, p_fmt );
167
168     id->p_es->p_dec = input_RunDecoder( p_sys->p_input, id->p_es );
169     if( id->p_es->p_dec == NULL )
170     {
171         msg_Err( p_stream, "cannot create decoder for fcc=`%4.4s'",
172                  (char*)&p_fmt->i_codec );
173         free( id->p_es );
174         free( id );
175         return NULL;
176     }
177
178     return id;
179 }
180
181 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
182 {
183     sout_stream_sys_t *p_sys = p_stream->p_sys;
184
185     input_EndDecoder( p_sys->p_input, id->p_es );
186
187     free( id->p_es );
188     free( id );
189
190     return VLC_SUCCESS;
191 }
192
193 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
194                  block_t *p_buffer )
195 {
196     sout_stream_sys_t *p_sys = p_stream->p_sys;
197
198     while( p_buffer )
199     {
200         block_t *p_next = p_buffer->p_next;
201
202         p_buffer->p_next = NULL;
203
204         if( id->p_es->p_dec && p_buffer->i_buffer > 0 )
205         {
206             if( p_buffer->i_dts <= 0 )
207                 p_buffer->i_dts= 0;
208             else
209                 p_buffer->i_dts += p_sys->i_delay;
210
211             if( p_buffer->i_pts <= 0 )
212                 p_buffer->i_pts= 0;
213             else
214                 p_buffer->i_pts += p_sys->i_delay;
215
216             input_DecodeBlock( id->p_es->p_dec, p_buffer );
217         }
218
219         p_buffer = p_next;
220     }
221
222     return VLC_SUCCESS;
223 }