]> git.sesse.net Git - vlc/blob - modules/stream_out/display.c
* rtp, display: update p_sout->i_out_pace_nocontrol.
[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 #include "codecs.h"
35
36 /*****************************************************************************
37  * Exported prototypes
38  *****************************************************************************/
39 static int      Open    ( vlc_object_t * );
40 static void     Close   ( vlc_object_t * );
41
42 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
43 static int               Del ( sout_stream_t *, sout_stream_id_t * );
44 static int               Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t* );
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 vlc_module_begin();
50     set_description( _("Display stream output") );
51     set_capability( "sout stream", 50 );
52     add_shortcut( "display" );
53     set_callbacks( Open, Close );
54 vlc_module_end();
55
56 struct sout_stream_sys_t
57 {
58     input_thread_t *p_input;
59
60     vlc_bool_t     b_audio;
61     vlc_bool_t     b_video;
62
63     mtime_t        i_delay;
64 };
65
66 /*****************************************************************************
67  * Open:
68  *****************************************************************************/
69 static int Open( vlc_object_t *p_this )
70 {
71     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
72     sout_stream_sys_t *p_sys;
73     char              *val;
74     p_sys           = malloc( sizeof( sout_stream_sys_t ) );
75     p_sys->p_input  = vlc_object_find( p_stream, VLC_OBJECT_INPUT, FIND_ANYWHERE );
76     if( !p_sys->p_input )
77     {
78         msg_Err( p_stream, "cannot find p_input" );
79         free( p_sys );
80         return VLC_EGENERIC;
81     }
82
83     p_sys->b_audio = VLC_TRUE;
84     p_sys->b_video = VLC_TRUE;
85     p_sys->i_delay = 100*1000;
86     if( sout_cfg_find( p_stream->p_cfg, "noaudio" ) )
87     {
88         p_sys->b_audio = VLC_FALSE;
89     }
90     if( sout_cfg_find( p_stream->p_cfg, "novideo" ) )
91     {
92         p_sys->b_video = VLC_FALSE;
93     }
94     if( ( val = sout_cfg_find_value( p_stream->p_cfg, "delay" ) ) )
95     {
96         p_sys->i_delay = (mtime_t)atoi( val ) * (mtime_t)1000;
97     }
98
99     p_stream->pf_add    = Add;
100     p_stream->pf_del    = Del;
101     p_stream->pf_send   = Send;
102
103     p_stream->p_sys     = p_sys;
104
105     /* update p_sout->i_out_pace_nocontrol */
106     p_stream->p_sout->i_out_pace_nocontrol++;
107
108     return VLC_SUCCESS;
109 }
110
111 /*****************************************************************************
112  * Close:
113  *****************************************************************************/
114 static void Close( vlc_object_t * p_this )
115 {
116     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
117     sout_stream_sys_t *p_sys = p_stream->p_sys;
118
119     /* update p_sout->i_out_pace_nocontrol */
120     p_stream->p_sout->i_out_pace_nocontrol--;
121
122     vlc_object_release( p_sys->p_input );
123
124     free( p_sys );
125 }
126
127 struct sout_stream_id_t
128 {
129     es_descriptor_t *p_es;
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 = p_stream->p_sys;
135     sout_stream_id_t *id;
136
137     if( ( p_fmt->i_cat == AUDIO_ES && !p_sys->b_audio )||
138         ( p_fmt->i_cat == VIDEO_ES && !p_sys->b_video ) )
139     {
140         return NULL;
141     }
142
143     id = malloc( sizeof( sout_stream_id_t ) );
144
145     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
146     id->p_es = input_AddES( p_sys->p_input,
147                             NULL,           /* no program */
148                             12,             /* es_id */
149                             p_fmt->i_cat,   /* es category */
150                             NULL,           /* description */
151                             0 );            /* no extra data */
152
153     if( !id->p_es )
154     {
155         vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
156
157         msg_Err( p_stream, "cannot create es" );
158         free( id );
159         return NULL;
160     }
161     id->p_es->i_stream_id   = 1;
162     id->p_es->i_fourcc      = p_fmt->i_codec;
163     id->p_es->b_force_decoder = VLC_TRUE;
164
165     es_format_Copy( &id->p_es->fmt, p_fmt );
166
167     if( input_SelectES( p_sys->p_input, id->p_es ) )
168     {
169         input_DelES( p_sys->p_input, id->p_es );
170         vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
171
172         msg_Err( p_stream, "cannot select es" );
173         free( id );
174         return NULL;
175     }
176     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
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_DelES( p_sys->p_input, id->p_es );
186
187     free( id );
188
189     return VLC_SUCCESS;
190 }
191
192 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
193                  sout_buffer_t *p_buffer )
194 {
195     sout_stream_sys_t *p_sys = p_stream->p_sys;
196
197     while( p_buffer )
198     {
199         sout_buffer_t *p_next;
200         block_t *p_block;
201
202         vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
203         if( id->p_es->p_dec && p_buffer->i_size > 0 &&
204             (p_block = block_New( p_stream, p_buffer->i_size )) )
205         {
206             p_block->i_dts = p_buffer->i_dts <= 0 ? 0 :
207                              p_buffer->i_dts + p_sys->i_delay;
208             p_block->i_pts = p_buffer->i_pts <= 0 ? 0 :
209                              p_buffer->i_pts + p_sys->i_delay;
210
211             p_stream->p_vlc->pf_memcpy( p_block->p_buffer,
212                                         p_buffer->p_buffer, p_buffer->i_size );
213
214             input_DecodeBlock( id->p_es->p_dec, p_block );
215         }
216         vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
217
218         /* *** go to next buffer *** */
219         p_next = p_buffer->p_next;
220         sout_BufferDelete( p_stream->p_sout, p_buffer );
221         p_buffer = p_next;
222     }
223
224     return VLC_SUCCESS;
225 }