]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
audio_output: Add a missing unlock in aout_DecDelete().
[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     vlc_mutex_lock( &p_aout->mixer_lock );
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     vlc_mutex_lock( &p_aout->input_fifos_lock );
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                 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
125                 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
126                 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
127             }
128             vlc_mutex_unlock( &p_aout->mixer_lock );
129             return p_input;
130         }
131
132         /* Create other input streams. */
133         for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
134         {
135             vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
136             aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
137             aout_InputNew( p_aout, p_aout->pp_inputs[i] );
138             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
139         }
140     }
141     else
142     {
143         aout_MixerDelete( p_aout );
144     }
145
146     if ( aout_MixerNew( p_aout ) == -1 )
147     {
148         aout_OutputDelete( p_aout );
149         vlc_mutex_unlock( &p_aout->input_fifos_lock );
150         goto error;
151     }
152
153     aout_InputNew( p_aout, p_input );
154     vlc_mutex_unlock( &p_aout->input_fifos_lock );
155
156     vlc_mutex_unlock( &p_aout->mixer_lock );
157     p_input->i_desync = var_CreateGetInteger( p_this, "audio-desync" ) * 1000;
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->i_pts_delay += p_input->i_desync;
165         p_input->p_input_thread = p_input_thread;
166         vlc_object_release( p_input_thread );
167     }
168     else
169     {
170         p_input->i_pts_delay = DEFAULT_PTS_DELAY;
171         p_input->i_pts_delay += p_input->i_desync;
172         p_input->p_input_thread = NULL;
173     }
174
175     return p_input;
176
177 error:
178     vlc_mutex_unlock( &p_aout->mixer_lock );
179     return NULL;
180 }
181
182 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
183                               aout_instance_t ** pp_aout,
184                               audio_sample_format_t * p_format,
185                               audio_replay_gain_t *p_replay_gain )
186 {
187     aout_instance_t *p_aout = *pp_aout;
188     if ( p_aout == NULL )
189     {
190         msg_Dbg( p_this, "no aout present, spawning one" );
191         p_aout = aout_New( p_this );
192
193         /* Everything failed, I'm a loser, I just wanna die */
194         if( p_aout == NULL )
195             return NULL;
196
197         vlc_object_attach( p_aout, p_this );
198         *pp_aout = p_aout;
199     }
200
201     return DecNew( p_this, p_aout, p_format, p_replay_gain );
202 }
203
204 /*****************************************************************************
205  * aout_DecDelete : delete a decoder
206  *****************************************************************************/
207 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
208 {
209     int i_input;
210
211     /* This function can only be called by the decoder itself, so no need
212      * to lock p_input->lock. */
213     vlc_mutex_lock( &p_aout->mixer_lock );
214
215     for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
216     {
217         if ( p_aout->pp_inputs[i_input] == p_input )
218         {
219             break;
220         }
221     }
222
223     if ( i_input == p_aout->i_nb_inputs )
224     {
225         msg_Err( p_aout, "cannot find an input to delete" );
226         vlc_mutex_unlock( &p_aout->mixer_lock );
227         return -1;
228     }
229
230     /* Remove the input from the list. */
231     memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
232              (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
233     p_aout->i_nb_inputs--;
234
235     aout_InputDelete( p_aout, p_input );
236
237     vlc_mutex_destroy( &p_input->lock );
238     free( p_input );
239
240     if ( !p_aout->i_nb_inputs )
241     {
242         aout_OutputDelete( p_aout );
243         aout_MixerDelete( p_aout );
244         if ( var_Type( p_aout, "audio-device" ) != 0 )
245         {
246             var_Destroy( p_aout, "audio-device" );
247         }
248         if ( var_Type( p_aout, "audio-channels" ) != 0 )
249         {
250             var_Destroy( p_aout, "audio-channels" );
251         }
252     }
253
254     vlc_mutex_unlock( &p_aout->mixer_lock );
255
256     return 0;
257 }
258
259
260 /*
261  * Buffer management
262  */
263
264 /*****************************************************************************
265  * aout_DecNewBuffer : ask for a new empty buffer
266  *****************************************************************************/
267 aout_buffer_t * aout_DecNewBuffer( aout_input_t * p_input,
268                                    size_t i_nb_samples )
269 {
270     aout_buffer_t * p_buffer;
271     mtime_t duration;
272
273     vlc_mutex_lock( &p_input->lock );
274
275     if ( p_input->b_error )
276     {
277         vlc_mutex_unlock( &p_input->lock );
278         return NULL;
279     }
280
281     duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
282
283     /* This necessarily allocates in the heap. */
284     aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
285     if( p_buffer != NULL )
286         p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
287                                   / p_input->input.i_frame_length;
288
289     /* Suppose the decoder doesn't have more than one buffered buffer */
290     p_input->b_changed = 0;
291
292     vlc_mutex_unlock( &p_input->lock );
293
294     if( p_buffer == NULL )
295         return NULL;
296
297     p_buffer->i_nb_samples = i_nb_samples;
298     p_buffer->start_date = p_buffer->end_date = 0;
299     return p_buffer;
300 }
301
302 /*****************************************************************************
303  * aout_DecDeleteBuffer : destroy an undecoded buffer
304  *****************************************************************************/
305 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
306                            aout_buffer_t * p_buffer )
307 {
308     (void)p_aout; (void)p_input;
309     aout_BufferFree( p_buffer );
310 }
311
312 /*****************************************************************************
313  * aout_DecPlay : filter & mix the decoded buffer
314  *****************************************************************************/
315 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
316                   aout_buffer_t * p_buffer, int i_input_rate )
317 {
318     if ( p_buffer->start_date == 0 )
319     {
320         msg_Warn( p_aout, "non-dated buffer received" );
321         aout_BufferFree( p_buffer );
322         return -1;
323     }
324
325 #ifndef FIXME
326     /* This hack for #transcode{acodec=...}:display to work -- Courmisch */
327     if( i_input_rate == 0 )
328         i_input_rate = INPUT_RATE_DEFAULT;
329 #endif
330     if( i_input_rate > INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE ||
331         i_input_rate < INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE )
332     {
333         aout_BufferFree( p_buffer );
334         return 0;
335     }
336
337     /* Apply the desynchronisation requested by the user */
338     p_buffer->start_date += p_input->i_desync;
339     p_buffer->end_date += p_input->i_desync;
340
341     if ( p_buffer->start_date > mdate() + p_input->i_pts_delay +
342          AOUT_MAX_ADVANCE_TIME )
343     {
344         msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
345                   p_buffer->start_date - mdate());
346         if( p_input->p_input_thread )
347         {
348             vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
349             stats_UpdateInteger( p_aout,
350                            p_input->p_input_thread->p->counters.p_lost_abuffers,
351                            1, NULL );
352             vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
353         }
354         aout_BufferFree( p_buffer );
355         return -1;
356     }
357
358     p_buffer->end_date = p_buffer->start_date
359                             + (mtime_t)p_buffer->i_nb_samples * 1000000
360                                 / p_input->input.i_rate;
361
362     vlc_mutex_lock( &p_input->lock );
363
364     if ( p_input->b_error )
365     {
366         vlc_mutex_unlock( &p_input->lock );
367         aout_BufferFree( p_buffer );
368         return -1;
369     }
370
371     if ( p_input->b_changed )
372     {
373         /* Maybe the allocation size has changed. Re-allocate a buffer. */
374         aout_buffer_t * p_new_buffer;
375         mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
376                             / p_input->input.i_rate;
377
378         aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
379         vlc_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
380                     p_buffer->i_nb_bytes );
381         p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
382         p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
383         p_new_buffer->start_date = p_buffer->start_date;
384         p_new_buffer->end_date = p_buffer->end_date;
385         aout_BufferFree( p_buffer );
386         p_buffer = p_new_buffer;
387         p_input->b_changed = 0;
388     }
389
390     /* If the buffer is too early, wait a while. */
391     mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
392
393     if ( aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate ) == -1 )
394     {
395         vlc_mutex_unlock( &p_input->lock );
396         return -1;
397     }
398
399     vlc_mutex_unlock( &p_input->lock );
400
401     /* Run the mixer if it is able to run. */
402     vlc_mutex_lock( &p_aout->mixer_lock );
403     aout_MixerRun( p_aout );
404     if( p_input->p_input_thread )
405     {
406         vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
407         stats_UpdateInteger( p_aout,
408                              p_input->p_input_thread->p->counters.p_played_abuffers,
409                              1, NULL );
410         vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
411     }
412     vlc_mutex_unlock( &p_aout->mixer_lock );
413
414     return 0;
415 }