]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
Added aout pause support.
[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 /*****************************************************************************
43  * aout_DecNew : create a decoder
44  *****************************************************************************/
45 static aout_input_t * DecNew( vlc_object_t * p_this, aout_instance_t * p_aout,
46                               audio_sample_format_t *p_format,
47                               audio_replay_gain_t *p_replay_gain )
48 {
49     aout_input_t * p_input;
50
51     /* Sanitize audio format */
52     if( p_format->i_channels > 32 )
53     {
54         msg_Err( p_aout, "too many audio channels (%u)",
55                  p_format->i_channels );
56         return NULL;
57     }
58     if( p_format->i_channels <= 0 )
59     {
60         msg_Err( p_aout, "no audio channels" );
61         return NULL;
62     }
63
64     if( p_format->i_rate > 192000 )
65     {
66         msg_Err( p_aout, "excessive audio sample frequency (%u)",
67                  p_format->i_rate );
68         return NULL;
69     }
70     if( p_format->i_rate < 4000 )
71     {
72         msg_Err( p_aout, "too low audio sample frequency (%u)",
73                  p_format->i_rate );
74         return NULL;
75     }
76
77     /* We can only be called by the decoder, so no need to lock
78      * p_input->lock. */
79     aout_lock_mixer( p_aout );
80
81     if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
82     {
83         msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
84         goto error;
85     }
86
87     p_input = malloc(sizeof(aout_input_t));
88     if( p_input == NULL )
89         goto error;
90     memset( p_input, 0, sizeof(aout_input_t) );
91
92     vlc_mutex_init( &p_input->lock );
93
94     p_input->b_changed = false;
95     p_input->b_error = true;
96     p_input->b_paused = false;
97     p_input->i_pause_date = 0;
98
99     aout_FormatPrepare( p_format );
100
101     memcpy( &p_input->input, p_format,
102             sizeof(audio_sample_format_t) );
103     if( p_replay_gain )
104         p_input->replay_gain = *p_replay_gain;
105
106     aout_lock_input_fifos( p_aout );
107     p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
108     p_aout->i_nb_inputs++;
109
110     if ( p_aout->mixer.b_error )
111     {
112         int i;
113
114         var_Destroy( p_aout, "audio-device" );
115         var_Destroy( p_aout, "audio-channels" );
116
117         /* Recreate the output using the new format. */
118         if ( aout_OutputNew( p_aout, p_format ) < 0 )
119         {
120             for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
121             {
122                 aout_lock_input( p_aout, p_aout->pp_inputs[i] );
123                 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
124                 aout_unlock_input( p_aout, p_aout->pp_inputs[i] );
125             }
126             aout_unlock_input_fifos( p_aout );
127             aout_unlock_mixer( p_aout );
128             return p_input;
129         }
130
131         /* Create other input streams. */
132         for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
133         {
134             aout_lock_input( p_aout, p_aout->pp_inputs[i] );
135             aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
136             aout_InputNew( p_aout, p_aout->pp_inputs[i] );
137             aout_unlock_input( p_aout, p_aout->pp_inputs[i] );
138         }
139     }
140     else
141     {
142         aout_MixerDelete( p_aout );
143     }
144
145     if ( aout_MixerNew( p_aout ) == -1 )
146     {
147         aout_OutputDelete( p_aout );
148         aout_unlock_input_fifos( p_aout );
149         goto error;
150     }
151
152     aout_InputNew( p_aout, p_input );
153     aout_unlock_input_fifos( p_aout );
154
155     aout_unlock_mixer( p_aout );
156
157     return p_input;
158
159 error:
160     aout_unlock_mixer( p_aout );
161     return NULL;
162 }
163
164 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
165                               aout_instance_t ** pp_aout,
166                               audio_sample_format_t * p_format,
167                               audio_replay_gain_t *p_replay_gain )
168 {
169     aout_instance_t *p_aout = *pp_aout;
170     if ( p_aout == NULL )
171     {
172         msg_Dbg( p_this, "no aout present, spawning one" );
173         p_aout = aout_New( p_this );
174
175         /* Everything failed, I'm a loser, I just wanna die */
176         if( p_aout == NULL )
177             return NULL;
178
179         vlc_object_attach( p_aout, p_this );
180         *pp_aout = p_aout;
181     }
182
183     return DecNew( p_this, p_aout, p_format, p_replay_gain );
184 }
185
186 /*****************************************************************************
187  * aout_DecDelete : delete a decoder
188  *****************************************************************************/
189 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
190 {
191     int i_input;
192
193     /* This function can only be called by the decoder itself, so no need
194      * to lock p_input->lock. */
195     aout_lock_mixer( p_aout );
196
197     for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
198     {
199         if ( p_aout->pp_inputs[i_input] == p_input )
200         {
201             break;
202         }
203     }
204
205     if ( i_input == p_aout->i_nb_inputs )
206     {
207         msg_Err( p_aout, "cannot find an input to delete" );
208         aout_unlock_mixer( p_aout );
209         return -1;
210     }
211
212     /* Remove the input from the list. */
213     memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
214              (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
215     p_aout->i_nb_inputs--;
216
217     aout_InputDelete( p_aout, p_input );
218
219     vlc_mutex_destroy( &p_input->lock );
220     free( p_input );
221
222     if ( !p_aout->i_nb_inputs )
223     {
224         aout_OutputDelete( p_aout );
225         aout_MixerDelete( p_aout );
226         if ( var_Type( p_aout, "audio-device" ) != 0 )
227         {
228             var_Destroy( p_aout, "audio-device" );
229         }
230         if ( var_Type( p_aout, "audio-channels" ) != 0 )
231         {
232             var_Destroy( p_aout, "audio-channels" );
233         }
234     }
235
236     aout_unlock_mixer( p_aout );
237
238     return 0;
239 }
240
241
242 /*
243  * Buffer management
244  */
245
246 /*****************************************************************************
247  * aout_DecNewBuffer : ask for a new empty buffer
248  *****************************************************************************/
249 aout_buffer_t * aout_DecNewBuffer( aout_input_t * p_input,
250                                    size_t i_nb_samples )
251 {
252     aout_buffer_t * p_buffer;
253     mtime_t duration;
254
255     aout_lock_input( NULL, p_input );
256
257     if ( p_input->b_error )
258     {
259         aout_unlock_input( NULL, p_input );
260         return NULL;
261     }
262
263     duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
264
265     /* This necessarily allocates in the heap. */
266     aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
267     if( p_buffer != NULL )
268         p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
269                                   / p_input->input.i_frame_length;
270
271     /* Suppose the decoder doesn't have more than one buffered buffer */
272     p_input->b_changed = false;
273
274     aout_unlock_input( NULL, p_input );
275
276     if( p_buffer == NULL )
277         return NULL;
278
279     p_buffer->i_nb_samples = i_nb_samples;
280     p_buffer->start_date = p_buffer->end_date = 0;
281     return p_buffer;
282 }
283
284 /*****************************************************************************
285  * aout_DecDeleteBuffer : destroy an undecoded buffer
286  *****************************************************************************/
287 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
288                            aout_buffer_t * p_buffer )
289 {
290     (void)p_aout; (void)p_input;
291     aout_BufferFree( p_buffer );
292 }
293
294 /*****************************************************************************
295  * aout_DecPlay : filter & mix the decoded buffer
296  *****************************************************************************/
297 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
298                   aout_buffer_t * p_buffer, int i_input_rate )
299 {
300     assert( i_input_rate >= INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE &&
301             i_input_rate <= INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE );
302
303     assert( p_buffer->start_date > 0 );
304
305     p_buffer->end_date = p_buffer->start_date
306                             + (mtime_t)p_buffer->i_nb_samples * 1000000
307                                 / p_input->input.i_rate;
308
309     aout_lock_input( p_aout, p_input );
310
311     if( p_input->b_error )
312     {
313         aout_unlock_input( p_aout, p_input );
314         aout_BufferFree( p_buffer );
315         return -1;
316     }
317
318     if( p_input->b_changed )
319     {
320         /* Maybe the allocation size has changed. Re-allocate a buffer. */
321         aout_buffer_t * p_new_buffer;
322         mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
323                             / p_input->input.i_rate;
324
325         aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
326         vlc_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
327                     p_buffer->i_nb_bytes );
328         p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
329         p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
330         p_new_buffer->start_date = p_buffer->start_date;
331         p_new_buffer->end_date = p_buffer->end_date;
332         aout_BufferFree( p_buffer );
333         p_buffer = p_new_buffer;
334         p_input->b_changed = false;
335     }
336
337     int i_ret = aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate );
338
339     aout_unlock_input( p_aout, p_input );
340
341     if( i_ret == -1 )
342         return -1;
343
344     /* Run the mixer if it is able to run. */
345     aout_lock_mixer( p_aout );
346
347     aout_MixerRun( p_aout );
348
349     aout_unlock_mixer( p_aout );
350
351     return 0;
352 }
353
354 int aout_DecGetResetLost( aout_instance_t *p_aout, aout_input_t *p_input )
355 {
356     aout_lock_input( p_aout, p_input );
357     int i_value = p_input->i_buffer_lost;
358     p_input->i_buffer_lost = 0;
359     aout_unlock_input( p_aout, p_input );
360
361     return i_value;
362 }
363
364 void aout_DecChangePause( aout_instance_t *p_aout, aout_input_t *p_input, bool b_paused, mtime_t i_date )
365 {
366     mtime_t i_duration = 0;
367     aout_lock_input( p_aout, p_input );
368     assert( !p_input->b_paused || !b_paused );
369     if( p_input->b_paused )
370     {
371         i_duration = i_date - p_input->i_pause_date;
372     }
373     p_input->b_paused = b_paused;
374     p_input->i_pause_date = i_date;
375     aout_unlock_input( p_aout, p_input );
376
377     if( i_duration != 0 )
378     {
379         aout_lock_mixer( p_aout );
380         for( aout_buffer_t *p = p_input->fifo.p_first; p != NULL; p = p->p_next )
381         {
382             p->start_date += i_duration;
383             p->end_date += i_duration;
384         }
385         aout_unlock_mixer( p_aout );
386     }
387 }
388