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