]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
* Added sanity checks for PTS in the future.
[vlc] / src / audio_output / dec.c
1 /*****************************************************************************
2  * dec.c : audio output API towards decoders
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: dec.c,v 1.8 2003/02/26 18:15:33 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@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>                            /* calloc(), malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31
32 #ifdef HAVE_ALLOCA_H
33 #   include <alloca.h>
34 #endif
35
36 #include "audio_output.h"
37 #include "aout_internal.h"
38
39 /*
40  * Creation/Deletion
41  */
42
43 /*****************************************************************************
44  * aout_DecNew : create a decoder
45  *****************************************************************************/
46 static aout_input_t * DecNew( aout_instance_t * p_aout,
47                               audio_sample_format_t * p_format )
48 {
49     aout_input_t * p_input;
50
51     /* We can only be called by the decoder, so no need to lock
52      * p_input->lock. */
53     vlc_mutex_lock( &p_aout->mixer_lock );
54
55     if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
56     {
57         msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
58         return NULL;
59     }
60
61     p_input = malloc(sizeof(aout_input_t));
62     if ( p_input == NULL )
63     {
64         msg_Err( p_aout, "out of memory" );
65         return NULL;
66     }
67
68     vlc_mutex_init( p_aout, &p_input->lock );
69
70     p_input->b_changed = 0;
71     p_input->b_error = 1;
72     aout_FormatPrepare( p_format );
73     memcpy( &p_input->input, p_format,
74             sizeof(audio_sample_format_t) );
75
76     p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
77     p_aout->i_nb_inputs++;
78
79     if ( p_aout->mixer.b_error )
80     {
81         int i;
82
83         if ( var_Type( p_aout, "audio-device" ) != 0 )
84         {
85             var_Destroy( p_aout, "audio-device" );
86         }
87         if ( var_Type( p_aout, "audio-channels" ) != 0 )
88         {
89             var_Destroy( p_aout, "audio-channels" );
90         }
91
92         /* Recreate the output using the new format. */
93         if ( aout_OutputNew( p_aout, p_format ) < 0 )
94         {
95             for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
96             {
97                 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
98                 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
99                 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
100             }
101             vlc_mutex_unlock( &p_aout->mixer_lock );
102             return p_input;
103         }
104
105         /* Create other input streams. */
106         for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
107         {
108             vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
109             aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
110             aout_InputNew( p_aout, p_aout->pp_inputs[i] );
111             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
112         }
113     }
114     else
115     {
116         aout_MixerDelete( p_aout );
117     }
118
119     if ( aout_MixerNew( p_aout ) == -1 )
120     {
121         aout_OutputDelete( p_aout );
122         vlc_mutex_unlock( &p_aout->mixer_lock );
123         return NULL;
124     }
125
126     aout_MixerNew( p_aout );
127
128     aout_InputNew( p_aout, p_input );
129
130     vlc_mutex_unlock( &p_aout->mixer_lock );
131
132     return p_input;
133 }
134
135 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
136                               aout_instance_t ** pp_aout,
137                               audio_sample_format_t * p_format )
138 {
139     if ( *pp_aout == NULL )
140     {
141         /* Create an audio output if there is none. */
142         *pp_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
143
144         if( *pp_aout == NULL )
145         {
146             msg_Dbg( p_this, "no aout present, spawning one" );
147
148             *pp_aout = aout_New( p_this );
149             /* Everything failed, I'm a loser, I just wanna die */
150             if( *pp_aout == NULL )
151             {
152                 return NULL;
153             }
154         }
155         else
156         {
157             vlc_object_release( *pp_aout );
158         }
159     }
160
161     return DecNew( *pp_aout, p_format );
162 }
163
164 /*****************************************************************************
165  * aout_DecDelete : delete a decoder
166  *****************************************************************************/
167 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
168 {
169     int i_input;
170
171     /* This function can only be called by the decoder itself, so no need
172      * to lock p_input->lock. */
173     vlc_mutex_lock( &p_aout->mixer_lock );
174
175     for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
176     {
177         if ( p_aout->pp_inputs[i_input] == p_input )
178         {
179             break;
180         }
181     }
182
183     if ( i_input == p_aout->i_nb_inputs )
184     {
185         msg_Err( p_aout, "cannot find an input to delete" );
186         return -1;
187     }
188
189     /* Remove the input from the list. */
190     memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
191              (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
192     p_aout->i_nb_inputs--;
193
194     aout_InputDelete( p_aout, p_input );
195
196     vlc_mutex_destroy( &p_input->lock );
197     free( p_input );
198
199     if ( !p_aout->i_nb_inputs )
200     {
201         aout_OutputDelete( p_aout );
202         aout_MixerDelete( p_aout );
203         if ( var_Type( p_aout, "audio-device" ) != 0 )
204         {
205             var_Destroy( p_aout, "audio-device" );
206         }
207         if ( var_Type( p_aout, "audio-channels" ) != 0 )
208         {
209             var_Destroy( p_aout, "audio-channels" );
210         }
211     }
212
213     vlc_mutex_unlock( &p_aout->mixer_lock );
214
215     return 0;
216 }
217
218
219 /*
220  * Buffer management
221  */
222
223 /*****************************************************************************
224  * aout_DecNewBuffer : ask for a new empty buffer
225  *****************************************************************************/
226 aout_buffer_t * aout_DecNewBuffer( aout_instance_t * p_aout,
227                                    aout_input_t * p_input,
228                                    size_t i_nb_samples )
229 {
230     aout_buffer_t * p_buffer;
231     mtime_t duration;
232
233     vlc_mutex_lock( &p_input->lock );
234
235     if ( p_input->b_error )
236     {
237         vlc_mutex_unlock( &p_input->lock );
238         return NULL;
239     }
240
241     duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
242
243     /* This necessarily allocates in the heap. */
244     aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
245     p_buffer->i_nb_samples = i_nb_samples;
246     p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
247                               / p_input->input.i_frame_length;
248
249     /* Suppose the decoder doesn't have more than one buffered buffer */
250     p_input->b_changed = 0;
251
252     vlc_mutex_unlock( &p_input->lock );
253
254     if ( p_buffer == NULL )
255     {
256         msg_Err( p_aout, "NULL buffer !" );
257     }
258     else
259     {
260         p_buffer->start_date = p_buffer->end_date = 0;
261     }
262
263     return p_buffer;
264 }
265
266 /*****************************************************************************
267  * aout_DecDeleteBuffer : destroy an undecoded buffer
268  *****************************************************************************/
269 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
270                            aout_buffer_t * p_buffer )
271 {
272     aout_BufferFree( p_buffer );
273 }
274
275 /*****************************************************************************
276  * aout_DecPlay : filter & mix the decoded buffer
277  *****************************************************************************/
278 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
279                   aout_buffer_t * p_buffer )
280 {
281     if ( p_buffer->start_date == 0 )
282     {
283         msg_Warn( p_aout, "non-dated buffer received" );
284         aout_BufferFree( p_buffer );
285         return -1;
286     }
287
288     if ( p_buffer->start_date > mdate() + AOUT_MAX_ADVANCE_TIME )
289     {
290         msg_Warn( p_aout, "received buffer in the future ("I64Fd")",
291                   p_buffer->start_date - mdate());
292         aout_BufferFree( p_buffer );
293         return -1;
294     }
295
296     p_buffer->end_date = p_buffer->start_date
297                             + (mtime_t)(p_buffer->i_nb_samples * 1000000)
298                                 / p_input->input.i_rate;
299
300     vlc_mutex_lock( &p_input->lock );
301
302     if ( p_input->b_error )
303     {
304         vlc_mutex_unlock( &p_input->lock );
305         aout_BufferFree( p_buffer );
306         return -1;
307     }
308
309     if ( p_input->b_changed )
310     {
311         /* Maybe the allocation size has changed. Re-allocate a buffer. */
312         aout_buffer_t * p_new_buffer;
313         mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
314                             / p_input->input.i_rate;
315
316         aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
317         p_aout->p_vlc->pf_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
318                                   p_buffer->i_nb_bytes );
319         p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
320         p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
321         p_new_buffer->start_date = p_buffer->start_date;
322         p_new_buffer->end_date = p_buffer->end_date;
323         aout_BufferFree( p_buffer );
324         p_buffer = p_new_buffer;
325         p_input->b_changed = 0;
326     }
327
328     /* If the buffer is too early, wait a while. */
329     mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
330
331     if ( aout_InputPlay( p_aout, p_input, p_buffer ) == -1 )
332     {
333         vlc_mutex_unlock( &p_input->lock );
334         return -1;
335     }
336
337     vlc_mutex_unlock( &p_input->lock );
338
339     /* Run the mixer if it is able to run. */
340     vlc_mutex_lock( &p_aout->mixer_lock );
341     aout_MixerRun( p_aout );
342     vlc_mutex_unlock( &p_aout->mixer_lock );
343
344     return 0;
345 }
346