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