]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
Use audio desync as initial audio-delay value.
[vlc] / src / audio_output / dec.c
1 /*****************************************************************************
2  * dec.c : audio output API towards decoders
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32
33 #ifdef HAVE_ALLOCA_H
34 #   include <alloca.h>
35 #endif
36
37 #include <vlc_aout.h>
38 #include <vlc_input.h>
39
40 #include "aout_internal.h"
41
42 /** FIXME: Ugly but needed to access the counters */
43 #include "input/input_internal.h"
44
45 /*****************************************************************************
46  * aout_DecNew : create a decoder
47  *****************************************************************************/
48 static aout_input_t * DecNew( vlc_object_t * p_this, aout_instance_t * p_aout,
49                               audio_sample_format_t *p_format,
50                               audio_replay_gain_t *p_replay_gain )
51 {
52     aout_input_t * p_input;
53     input_thread_t * p_input_thread;
54
55     /* Sanitize audio format */
56     if( p_format->i_channels > 32 )
57     {
58         msg_Err( p_aout, "too many audio channels (%u)",
59                  p_format->i_channels );
60         return NULL;
61     }
62     if( p_format->i_channels <= 0 )
63     {
64         msg_Err( p_aout, "no audio channels" );
65         return NULL;
66     }
67
68     if( p_format->i_rate > 192000 )
69     {
70         msg_Err( p_aout, "excessive audio sample frequency (%u)",
71                  p_format->i_rate );
72         return NULL;
73     }
74     if( p_format->i_rate < 4000 )
75     {
76         msg_Err( p_aout, "too low audio sample frequency (%u)",
77                  p_format->i_rate );
78         return NULL;
79     }
80
81     /* We can only be called by the decoder, so no need to lock
82      * p_input->lock. */
83     aout_lock_mixer( p_aout );
84
85     if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
86     {
87         msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
88         goto error;
89     }
90
91     p_input = malloc(sizeof(aout_input_t));
92     if ( p_input == NULL )
93         goto error;
94     memset( p_input, 0, sizeof(aout_input_t) );
95
96     vlc_mutex_init( &p_input->lock );
97
98     p_input->b_changed = 0;
99     p_input->b_error = 1;
100
101     aout_FormatPrepare( p_format );
102
103     memcpy( &p_input->input, p_format,
104             sizeof(audio_sample_format_t) );
105     if( p_replay_gain )
106         p_input->replay_gain = *p_replay_gain;
107
108     aout_lock_input_fifos( p_aout );
109     p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
110     p_aout->i_nb_inputs++;
111
112     if ( p_aout->mixer.b_error )
113     {
114         int i;
115
116         var_Destroy( p_aout, "audio-device" );
117         var_Destroy( p_aout, "audio-channels" );
118
119         /* Recreate the output using the new format. */
120         if ( aout_OutputNew( p_aout, p_format ) < 0 )
121         {
122             for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
123             {
124                 aout_lock_input( p_aout, p_aout->pp_inputs[i] );
125                 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
126                 aout_unlock_input( p_aout, p_aout->pp_inputs[i] );
127             }
128             aout_unlock_input_fifos( p_aout );
129             aout_unlock_mixer( p_aout );
130             return p_input;
131         }
132
133         /* Create other input streams. */
134         for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
135         {
136             aout_lock_input( p_aout, p_aout->pp_inputs[i] );
137             aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
138             aout_InputNew( p_aout, p_aout->pp_inputs[i] );
139             aout_unlock_input( p_aout, p_aout->pp_inputs[i] );
140         }
141     }
142     else
143     {
144         aout_MixerDelete( p_aout );
145     }
146
147     if ( aout_MixerNew( p_aout ) == -1 )
148     {
149         aout_OutputDelete( p_aout );
150         aout_unlock_input_fifos( p_aout );
151         goto error;
152     }
153
154     aout_InputNew( p_aout, p_input );
155     aout_unlock_input_fifos( p_aout );
156
157     aout_unlock_mixer( p_aout );
158
159     p_input_thread = (input_thread_t *)vlc_object_find( p_this,
160                                            VLC_OBJECT_INPUT, FIND_PARENT );
161     if( p_input_thread )
162     {
163         p_input->i_pts_delay = p_input_thread->i_pts_delay;
164         p_input->p_input_thread = p_input_thread;
165         vlc_object_release( p_input_thread );
166     }
167     else
168     {
169         p_input->i_pts_delay = DEFAULT_PTS_DELAY;
170         p_input->p_input_thread = NULL;
171     }
172
173     return p_input;
174
175 error:
176     aout_unlock_mixer( p_aout );
177     return NULL;
178 }
179
180 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
181                               aout_instance_t ** pp_aout,
182                               audio_sample_format_t * p_format,
183                               audio_replay_gain_t *p_replay_gain )
184 {
185     aout_instance_t *p_aout = *pp_aout;
186     if ( p_aout == NULL )
187     {
188         msg_Dbg( p_this, "no aout present, spawning one" );
189         p_aout = aout_New( p_this );
190
191         /* Everything failed, I'm a loser, I just wanna die */
192         if( p_aout == NULL )
193             return NULL;
194
195         vlc_object_attach( p_aout, p_this );
196         *pp_aout = p_aout;
197     }
198
199     return DecNew( p_this, p_aout, p_format, p_replay_gain );
200 }
201
202 /*****************************************************************************
203  * aout_DecDelete : delete a decoder
204  *****************************************************************************/
205 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
206 {
207     int i_input;
208
209     /* This function can only be called by the decoder itself, so no need
210      * to lock p_input->lock. */
211     aout_lock_mixer( p_aout );
212
213     for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
214     {
215         if ( p_aout->pp_inputs[i_input] == p_input )
216         {
217             break;
218         }
219     }
220
221     if ( i_input == p_aout->i_nb_inputs )
222     {
223         msg_Err( p_aout, "cannot find an input to delete" );
224         aout_unlock_mixer( p_aout );
225         return -1;
226     }
227
228     /* Remove the input from the list. */
229     memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
230              (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
231     p_aout->i_nb_inputs--;
232
233     aout_InputDelete( p_aout, p_input );
234
235     vlc_mutex_destroy( &p_input->lock );
236     free( p_input );
237
238     if ( !p_aout->i_nb_inputs )
239     {
240         aout_OutputDelete( p_aout );
241         aout_MixerDelete( p_aout );
242         if ( var_Type( p_aout, "audio-device" ) != 0 )
243         {
244             var_Destroy( p_aout, "audio-device" );
245         }
246         if ( var_Type( p_aout, "audio-channels" ) != 0 )
247         {
248             var_Destroy( p_aout, "audio-channels" );
249         }
250     }
251
252     aout_unlock_mixer( p_aout );
253
254     return 0;
255 }
256
257
258 /*
259  * Buffer management
260  */
261
262 /*****************************************************************************
263  * aout_DecNewBuffer : ask for a new empty buffer
264  *****************************************************************************/
265 aout_buffer_t * aout_DecNewBuffer( aout_input_t * p_input,
266                                    size_t i_nb_samples )
267 {
268     aout_buffer_t * p_buffer;
269     mtime_t duration;
270
271     aout_lock_input( NULL, p_input );
272
273     if ( p_input->b_error )
274     {
275         aout_unlock_input( NULL, p_input );
276         return NULL;
277     }
278
279     duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
280
281     /* This necessarily allocates in the heap. */
282     aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
283     if( p_buffer != NULL )
284         p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
285                                   / p_input->input.i_frame_length;
286
287     /* Suppose the decoder doesn't have more than one buffered buffer */
288     p_input->b_changed = 0;
289
290     aout_unlock_input( NULL, p_input );
291
292     if( p_buffer == NULL )
293         return NULL;
294
295     p_buffer->i_nb_samples = i_nb_samples;
296     p_buffer->start_date = p_buffer->end_date = 0;
297     return p_buffer;
298 }
299
300 /*****************************************************************************
301  * aout_DecDeleteBuffer : destroy an undecoded buffer
302  *****************************************************************************/
303 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
304                            aout_buffer_t * p_buffer )
305 {
306     (void)p_aout; (void)p_input;
307     aout_BufferFree( p_buffer );
308 }
309
310 /*****************************************************************************
311  * aout_DecPlay : filter & mix the decoded buffer
312  *****************************************************************************/
313 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
314                   aout_buffer_t * p_buffer, int i_input_rate )
315 {
316     if ( p_buffer->start_date == 0 )
317     {
318         msg_Warn( p_aout, "non-dated buffer received" );
319         aout_BufferFree( p_buffer );
320         return -1;
321     }
322
323 #ifndef FIXME
324     /* This hack for #transcode{acodec=...}:display to work -- Courmisch */
325     if( i_input_rate == 0 )
326         i_input_rate = INPUT_RATE_DEFAULT;
327 #endif
328     if( i_input_rate > INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE ||
329         i_input_rate < INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE )
330     {
331         aout_BufferFree( p_buffer );
332         return 0;
333     }
334
335     if ( p_buffer->start_date > mdate() + p_input->i_pts_delay +
336          AOUT_MAX_ADVANCE_TIME )
337     {
338         msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
339                   p_buffer->start_date - mdate());
340         if( p_input->p_input_thread )
341         {
342             vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
343             stats_UpdateInteger( p_aout,
344                            p_input->p_input_thread->p->counters.p_lost_abuffers,
345                            1, NULL );
346             vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
347         }
348         aout_BufferFree( p_buffer );
349         return -1;
350     }
351
352     p_buffer->end_date = p_buffer->start_date
353                             + (mtime_t)p_buffer->i_nb_samples * 1000000
354                                 / p_input->input.i_rate;
355
356     aout_lock_input( p_aout, p_input );
357
358     if ( p_input->b_error )
359     {
360         aout_unlock_input( p_aout, p_input );
361         aout_BufferFree( p_buffer );
362         return -1;
363     }
364
365     if ( p_input->b_changed )
366     {
367         /* Maybe the allocation size has changed. Re-allocate a buffer. */
368         aout_buffer_t * p_new_buffer;
369         mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
370                             / p_input->input.i_rate;
371
372         aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
373         vlc_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
374                     p_buffer->i_nb_bytes );
375         p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
376         p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
377         p_new_buffer->start_date = p_buffer->start_date;
378         p_new_buffer->end_date = p_buffer->end_date;
379         aout_BufferFree( p_buffer );
380         p_buffer = p_new_buffer;
381         p_input->b_changed = 0;
382     }
383
384     /* If the buffer is too early, wait a while. */
385     mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
386
387     int ret = aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate );
388
389     aout_unlock_input( p_aout, p_input );
390
391     if ( ret == -1 ) return -1;
392
393     /* Run the mixer if it is able to run. */
394     aout_lock_mixer( p_aout );
395     aout_MixerRun( p_aout );
396     if( p_input->p_input_thread )
397     {
398         vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
399         stats_UpdateInteger( p_aout,
400                              p_input->p_input_thread->p->counters.p_played_abuffers,
401                              1, NULL );
402         vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
403     }
404     aout_unlock_mixer( p_aout );
405
406     return 0;
407 }