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