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