]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
Last part of the Speex/RTP patch - fixes #1291
[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 #include <vlc/vlc.h>
28
29 #ifdef HAVE_ALLOCA_H
30 #   include <alloca.h>
31 #endif
32
33 #include <vlc_aout.h>
34 #include <vlc_input.h>
35
36 #include "aout_internal.h"
37
38 /** FIXME: Ugly but needed to access the counters */
39 #include "input/input_internal.h"
40
41 /*****************************************************************************
42  * aout_DecNew : create a decoder
43  *****************************************************************************/
44 static aout_input_t * DecNew( vlc_object_t * p_this, aout_instance_t * p_aout,
45                               audio_sample_format_t *p_format,
46                               audio_replay_gain_t *p_replay_gain )
47 {
48     aout_input_t * p_input;
49     input_thread_t * p_input_thread;
50     vlc_value_t val;
51
52     /* Sanitize audio format */
53     if( p_format->i_channels > 32 )
54     {
55         msg_Err( p_aout, "too many audio channels (%u)",
56                  p_format->i_channels );
57         goto error;
58     }
59
60     if( p_format->i_rate > 192000 )
61     {
62         msg_Err( p_aout, "excessive audio sample frequency (%u)",
63                  p_format->i_rate );
64         goto error;
65     }
66
67     /* We can only be called by the decoder, so no need to lock
68      * p_input->lock. */
69     vlc_mutex_lock( &p_aout->mixer_lock );
70
71     if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
72     {
73         msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
74         goto error;
75     }
76
77     p_input = malloc(sizeof(aout_input_t));
78     if ( p_input == NULL )
79     {
80         msg_Err( p_aout, "out of memory" );
81         goto error;
82     }
83     memset( p_input, 0, sizeof(aout_input_t) );
84
85     vlc_mutex_init( p_aout, &p_input->lock );
86
87     p_input->b_changed = 0;
88     p_input->b_error = 1;
89
90     if ( !p_format->i_bytes_per_frame )
91         aout_FormatPrepare( p_format );
92     else 
93         p_format->i_frame_length = 1;
94
95     memcpy( &p_input->input, p_format,
96             sizeof(audio_sample_format_t) );
97     if( p_replay_gain )
98         p_input->replay_gain = *p_replay_gain;
99
100     p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
101     p_aout->i_nb_inputs++;
102
103     if ( p_aout->mixer.b_error )
104     {
105         int i;
106
107         var_Destroy( p_aout, "audio-device" );
108         var_Destroy( p_aout, "audio-channels" );
109
110         /* Recreate the output using the new format. */
111         if ( aout_OutputNew( p_aout, p_format ) < 0 )
112         {
113             for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
114             {
115                 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
116                 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
117                 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
118             }
119             vlc_mutex_unlock( &p_aout->mixer_lock );
120             return p_input;
121         }
122
123         /* Create other input streams. */
124         for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
125         {
126             vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
127             aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
128             aout_InputNew( p_aout, p_aout->pp_inputs[i] );
129             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
130         }
131     }
132     else
133     {
134         aout_MixerDelete( p_aout );
135     }
136
137     if ( aout_MixerNew( p_aout ) == -1 )
138     {
139         aout_OutputDelete( p_aout );
140         goto error;
141     }
142
143     aout_InputNew( p_aout, p_input );
144
145     vlc_mutex_unlock( &p_aout->mixer_lock );
146     var_Create( p_this, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
147     var_Get( p_this, "audio-desync", &val );
148     p_input->i_desync = val.i_int * 1000;
149
150     p_input_thread = (input_thread_t *)vlc_object_find( p_this,
151                                            VLC_OBJECT_INPUT, FIND_PARENT );
152     if( p_input_thread )
153     {
154         p_input->i_pts_delay = p_input_thread->i_pts_delay;
155         p_input->i_pts_delay += p_input->i_desync;
156         p_input->p_input_thread = p_input_thread;
157         vlc_object_release( p_input_thread );
158     }
159     else
160     {
161         p_input->i_pts_delay = DEFAULT_PTS_DELAY;
162         p_input->i_pts_delay += p_input->i_desync;
163         p_input->p_input_thread = NULL;
164     }
165
166     return p_input;
167
168 error:
169     vlc_mutex_unlock( &p_aout->mixer_lock );
170     return NULL;
171 }
172
173 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
174                               aout_instance_t ** pp_aout,
175                               audio_sample_format_t * p_format,
176                               audio_replay_gain_t *p_replay_gain )
177 {
178     if ( *pp_aout == NULL )
179     {
180         /* Create an audio output if there is none. */
181         *pp_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
182
183         if( *pp_aout == NULL )
184         {
185             msg_Dbg( p_this, "no aout present, spawning one" );
186
187             *pp_aout = aout_New( p_this );
188             /* Everything failed, I'm a loser, I just wanna die */
189             if( *pp_aout == NULL )
190             {
191                 return NULL;
192             }
193             vlc_object_attach( *pp_aout, p_this->p_libvlc );
194         }
195         else
196         {
197             vlc_object_release( *pp_aout );
198         }
199     }
200
201     return DecNew( p_this, *pp_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         return -1;
227     }
228
229     /* Remove the input from the list. */
230     memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
231              (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
232     p_aout->i_nb_inputs--;
233
234     aout_InputDelete( p_aout, p_input );
235
236     vlc_mutex_destroy( &p_input->lock );
237     free( p_input );
238
239     if ( !p_aout->i_nb_inputs )
240     {
241         aout_OutputDelete( p_aout );
242         aout_MixerDelete( p_aout );
243         if ( var_Type( p_aout, "audio-device" ) != 0 )
244         {
245             var_Destroy( p_aout, "audio-device" );
246         }
247         if ( var_Type( p_aout, "audio-channels" ) != 0 )
248         {
249             var_Destroy( p_aout, "audio-channels" );
250         }
251     }
252
253     vlc_mutex_unlock( &p_aout->mixer_lock );
254
255     return 0;
256 }
257
258
259 /*
260  * Buffer management
261  */
262
263 /*****************************************************************************
264  * aout_DecNewBuffer : ask for a new empty buffer
265  *****************************************************************************/
266 aout_buffer_t * aout_DecNewBuffer( aout_instance_t * p_aout,
267                                    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     if( i_input_rate > INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE ||
326         i_input_rate < INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE )
327     {
328         aout_BufferFree( p_buffer );
329         return 0;
330     }
331
332     /* Apply the desynchronisation requested by the user */
333     p_buffer->start_date += p_input->i_desync;
334     p_buffer->end_date += p_input->i_desync;
335
336     if ( p_buffer->start_date > mdate() + p_input->i_pts_delay +
337          AOUT_MAX_ADVANCE_TIME )
338     {
339         msg_Warn( p_aout, "received buffer in the future ("I64Fd")",
340                   p_buffer->start_date - mdate());
341         if( p_input->p_input_thread )
342         {
343             vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
344             stats_UpdateInteger( p_aout,
345                            p_input->p_input_thread->p->counters.p_lost_abuffers,
346                            1, NULL );
347             vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
348         }
349         aout_BufferFree( p_buffer );
350         return -1;
351     }
352
353     p_buffer->end_date = p_buffer->start_date
354                             + (mtime_t)p_buffer->i_nb_samples * 1000000
355                                 / p_input->input.i_rate;
356
357     vlc_mutex_lock( &p_input->lock );
358
359     if ( p_input->b_error )
360     {
361         vlc_mutex_unlock( &p_input->lock );
362         aout_BufferFree( p_buffer );
363         return -1;
364     }
365
366     if ( p_input->b_changed )
367     {
368         /* Maybe the allocation size has changed. Re-allocate a buffer. */
369         aout_buffer_t * p_new_buffer;
370         mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
371                             / p_input->input.i_rate;
372
373         aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
374         p_aout->p_libvlc->pf_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
375                                   p_buffer->i_nb_bytes );
376         p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
377         p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
378         p_new_buffer->start_date = p_buffer->start_date;
379         p_new_buffer->end_date = p_buffer->end_date;
380         aout_BufferFree( p_buffer );
381         p_buffer = p_new_buffer;
382         p_input->b_changed = 0;
383     }
384
385     /* If the buffer is too early, wait a while. */
386     mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
387
388     if ( aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate ) == -1 )
389     {
390         vlc_mutex_unlock( &p_input->lock );
391         return -1;
392     }
393
394     vlc_mutex_unlock( &p_input->lock );
395
396     /* Run the mixer if it is able to run. */
397     vlc_mutex_lock( &p_aout->mixer_lock );
398     aout_MixerRun( p_aout );
399     if( p_input->p_input_thread )
400     {
401         vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
402         stats_UpdateInteger( p_aout,
403                              p_input->p_input_thread->p->counters.p_played_abuffers,
404                              1, NULL );
405         vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
406     }
407     vlc_mutex_unlock( &p_aout->mixer_lock );
408
409     return 0;
410 }