]> git.sesse.net Git - vlc/blob - modules/stream_out/display.c
* display: set correct ES type (otherwise it segfaults in input_dec.c) but
[vlc] / modules / stream_out / display.c
1 /*****************************************************************************
2  * display.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: display.c,v 1.6 2003/11/18 16:46:31 fenrir Exp $
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 *, sout_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") );
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     return VLC_SUCCESS;
106 }
107
108 /*****************************************************************************
109  * Close:
110  *****************************************************************************/
111
112 static void Close( vlc_object_t * p_this )
113 {
114     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
115     sout_stream_sys_t *p_sys = p_stream->p_sys;
116
117     vlc_object_release( p_sys->p_input );
118
119     free( p_sys );
120 }
121
122 struct sout_stream_id_t
123 {
124     es_descriptor_t *p_es;
125 };
126
127
128 static sout_stream_id_t * Add      ( sout_stream_t *p_stream, sout_format_t *p_fmt )
129 {
130     sout_stream_sys_t *p_sys = p_stream->p_sys;
131     sout_stream_id_t *id;
132
133     if( ( p_fmt->i_cat == AUDIO_ES && !p_sys->b_audio )||
134         ( p_fmt->i_cat == VIDEO_ES && !p_sys->b_video ) )
135     {
136         return NULL;
137     }
138
139     id = malloc( sizeof( sout_stream_id_t ) );
140
141     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
142     id->p_es = input_AddES( p_sys->p_input,
143                             NULL,           /* no program */
144                             12,             /* es_id */
145                             p_fmt->i_cat, /* UNKNOWN_ES,*/    /* es category */
146                             NULL,           /* description */
147                             0 );            /* no extra data */
148
149     if( !id->p_es )
150     {
151         vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
152
153         msg_Err( p_stream, "cannot create es" );
154         free( id );
155         return NULL;
156     }
157     id->p_es->i_stream_id   = 1;
158     id->p_es->i_fourcc      = p_fmt->i_fourcc;
159     id->p_es->b_force_decoder = VLC_TRUE;
160     switch( p_fmt->i_cat )
161     {
162         case AUDIO_ES:
163             id->p_es->p_bitmapinfoheader = NULL;
164             id->p_es->p_waveformatex =
165                 malloc( sizeof( WAVEFORMATEX ) + p_fmt->i_extra_data );
166 #define p_wf    ((WAVEFORMATEX*)id->p_es->p_waveformatex)
167             p_wf->wFormatTag     = WAVE_FORMAT_UNKNOWN;
168             p_wf->nChannels      = p_fmt->i_channels;
169             p_wf->nSamplesPerSec = p_fmt->i_sample_rate;
170             p_wf->nAvgBytesPerSec= p_fmt->i_bitrate / 8;
171             p_wf->nBlockAlign    = p_fmt->i_block_align;
172             p_wf->wBitsPerSample = 0;
173             p_wf->cbSize         = p_fmt->i_extra_data;
174             if( p_fmt->i_extra_data > 0 )
175             {
176                 memcpy( &p_wf[1],
177                         p_fmt->p_extra_data,
178                         p_fmt->i_extra_data );
179             }
180 #undef p_wf
181             break;
182         case VIDEO_ES:
183             id->p_es->p_waveformatex = NULL;
184             id->p_es->p_bitmapinfoheader = malloc( sizeof( BITMAPINFOHEADER ) + p_fmt->i_extra_data );
185 #define p_bih ((BITMAPINFOHEADER*)id->p_es->p_bitmapinfoheader)
186             p_bih->biSize   = sizeof( BITMAPINFOHEADER ) + p_fmt->i_extra_data;
187             p_bih->biWidth  = p_fmt->i_width;
188             p_bih->biHeight = p_fmt->i_height;
189             p_bih->biPlanes   = 0;
190             p_bih->biBitCount = 0;
191             p_bih->biCompression = 0;
192             p_bih->biSizeImage   = 0;
193             p_bih->biXPelsPerMeter = 0;
194             p_bih->biYPelsPerMeter = 0;
195             p_bih->biClrUsed       = 0;
196             p_bih->biClrImportant  = 0;
197             if( p_fmt->i_extra_data > 0 )
198             {
199                 memcpy( &p_bih[1],
200                         p_fmt->p_extra_data,
201                         p_fmt->i_extra_data );
202             }
203 #undef p_bih
204             break;
205         default:
206             msg_Err( p_stream, "unknown es type" );
207             free( id );
208             return NULL;
209     }
210
211     if( input_SelectES( p_sys->p_input, id->p_es ) )
212     {
213         input_DelES( p_sys->p_input, id->p_es );
214         vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
215
216         msg_Err( p_stream, "cannot select es" );
217         free( id );
218         return NULL;
219     }
220     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
221
222     return id;
223 }
224
225 static int     Del      ( sout_stream_t *p_stream, sout_stream_id_t *id )
226 {
227     sout_stream_sys_t *p_sys = p_stream->p_sys;
228
229     input_DelES( p_sys->p_input, id->p_es );
230
231     free( id );
232
233     return VLC_SUCCESS;
234 }
235
236 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
237                  sout_buffer_t *p_buffer )
238 {
239     sout_stream_sys_t *p_sys = p_stream->p_sys;
240
241     while( p_buffer )
242     {
243         sout_buffer_t *p_next;
244         pes_packet_t *p_pes;
245         data_packet_t   *p_data;
246
247         if( p_buffer->i_size > 0 )
248         {
249             if( !( p_pes = input_NewPES( p_sys->p_input->p_method_data ) ) )
250             {
251                 msg_Err( p_stream, "cannot allocate new PES" );
252                 return VLC_EGENERIC;
253             }
254             if( !( p_data = input_NewPacket( p_sys->p_input->p_method_data,
255                                              p_buffer->i_size ) ) )
256             {
257                 msg_Err( p_stream, "cannot allocate new data_packet" );
258                 return VLC_EGENERIC;
259             }
260             p_data->p_payload_end = p_data->p_payload_start + p_buffer->i_size;
261
262             p_pes->i_dts = p_buffer->i_dts < 0 ? 0 :
263                            p_buffer->i_dts + p_sys->i_delay;
264             p_pes->i_pts = p_buffer->i_pts < 0 ? 0 :
265                            p_buffer->i_pts + p_sys->i_delay;
266             p_pes->p_first = p_pes->p_last = p_data;
267             p_pes->i_nb_data = 1;
268             p_pes->i_pes_size = p_buffer->i_size;
269
270             p_stream->p_vlc->pf_memcpy( p_data->p_payload_start,
271                                         p_buffer->p_buffer,
272                                         p_buffer->i_size );
273
274             if( id->p_es->p_decoder_fifo )
275             {
276                 input_DecodePES( id->p_es->p_decoder_fifo, p_pes );
277             }
278             else
279             {
280                 input_DeletePES( p_sys->p_input->p_method_data, p_pes );
281             }
282         }
283
284         /* *** go to next buffer *** */
285         p_next = p_buffer->p_next;
286         sout_BufferDelete( p_stream->p_sout, p_buffer );
287         p_buffer = p_next;
288     }
289
290     return VLC_SUCCESS;
291 }
292