]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
0480c6b69a519a981faa8fac6316369aed748080
[vlc] / src / audio_output / dec.c
1 /*****************************************************************************
2  * dec.c : audio output API towards decoders
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: dec.c,v 1.12 2003/10/27 21:54:10 gbazin Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                            /* calloc(), malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31
32 #ifdef HAVE_ALLOCA_H
33 #   include <alloca.h>
34 #endif
35
36 #include "audio_output.h"
37 #include "aout_internal.h"
38 #include <vlc/input.h>                 /* for input_thread_t and i_pts_delay */
39
40 /*
41  * Creation/Deletion
42  */
43
44 /*****************************************************************************
45  * aout_DecNew : create a decoder
46  *****************************************************************************/
47 static aout_input_t * DecNew( vlc_object_t * p_this, aout_instance_t * p_aout,
48                               audio_sample_format_t * p_format )
49 {
50     aout_input_t * p_input;
51     input_thread_t * p_input_thread;
52     vlc_value_t val;
53
54     /* We can only be called by the decoder, so no need to lock
55      * p_input->lock. */
56     vlc_mutex_lock( &p_aout->mixer_lock );
57
58     if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
59     {
60         msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
61         return NULL;
62     }
63
64     p_input = malloc(sizeof(aout_input_t));
65     if ( p_input == NULL )
66     {
67         msg_Err( p_aout, "out of memory" );
68         return NULL;
69     }
70
71     vlc_mutex_init( p_aout, &p_input->lock );
72
73     p_input->b_changed = 0;
74     p_input->b_error = 1;
75     aout_FormatPrepare( p_format );
76     memcpy( &p_input->input, p_format,
77             sizeof(audio_sample_format_t) );
78
79     p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
80     p_aout->i_nb_inputs++;
81
82     if ( p_aout->mixer.b_error )
83     {
84         int i;
85
86         var_Destroy( p_aout, "audio-device" );
87         var_Destroy( p_aout, "audio-channels" );
88
89         /* Recreate the output using the new format. */
90         if ( aout_OutputNew( p_aout, p_format ) < 0 )
91         {
92             for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
93             {
94                 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
95                 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
96                 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
97             }
98             vlc_mutex_unlock( &p_aout->mixer_lock );
99             return p_input;
100         }
101
102         /* Create other input streams. */
103         for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
104         {
105             vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
106             aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
107             aout_InputNew( p_aout, p_aout->pp_inputs[i] );
108             vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
109         }
110     }
111     else
112     {
113         aout_MixerDelete( p_aout );
114     }
115
116     if ( aout_MixerNew( p_aout ) == -1 )
117     {
118         aout_OutputDelete( p_aout );
119         vlc_mutex_unlock( &p_aout->mixer_lock );
120         return NULL;
121     }
122
123     aout_InputNew( p_aout, p_input );
124
125     vlc_mutex_unlock( &p_aout->mixer_lock );
126
127     var_Create( p_this, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
128     var_Get( p_this, "audio-desync", &val );
129     p_input->i_desync = val.i_int * 1000;
130
131     p_input_thread = (input_thread_t *)vlc_object_find( p_this,
132                                            VLC_OBJECT_INPUT, FIND_PARENT );
133     if( p_input_thread )
134     {
135         p_input->i_pts_delay = p_input_thread->i_pts_delay;
136         p_input->i_pts_delay += p_input->i_desync;
137         vlc_object_release( p_input_thread );
138     }
139     else
140     {
141         p_input->i_pts_delay = DEFAULT_PTS_DELAY;
142         p_input->i_pts_delay += p_input->i_desync;
143     }
144
145     return p_input;
146 }
147
148 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
149                               aout_instance_t ** pp_aout,
150                               audio_sample_format_t * p_format )
151 {
152     if ( *pp_aout == NULL )
153     {
154         /* Create an audio output if there is none. */
155         *pp_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
156
157         if( *pp_aout == NULL )
158         {
159             msg_Dbg( p_this, "no aout present, spawning one" );
160
161             *pp_aout = aout_New( p_this );
162             /* Everything failed, I'm a loser, I just wanna die */
163             if( *pp_aout == NULL )
164             {
165                 return NULL;
166             }
167         }
168         else
169         {
170             vlc_object_release( *pp_aout );
171         }
172     }
173
174     return DecNew( p_this, *pp_aout, p_format );
175 }
176
177 /*****************************************************************************
178  * aout_DecDelete : delete a decoder
179  *****************************************************************************/
180 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
181 {
182     int i_input;
183
184     /* This function can only be called by the decoder itself, so no need
185      * to lock p_input->lock. */
186     vlc_mutex_lock( &p_aout->mixer_lock );
187
188     for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
189     {
190         if ( p_aout->pp_inputs[i_input] == p_input )
191         {
192             break;
193         }
194     }
195
196     if ( i_input == p_aout->i_nb_inputs )
197     {
198         msg_Err( p_aout, "cannot find an input to delete" );
199         return -1;
200     }
201
202     /* Remove the input from the list. */
203     memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
204              (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
205     p_aout->i_nb_inputs--;
206
207     aout_InputDelete( p_aout, p_input );
208
209     vlc_mutex_destroy( &p_input->lock );
210     free( p_input );
211
212     if ( !p_aout->i_nb_inputs )
213     {
214         aout_OutputDelete( p_aout );
215         aout_MixerDelete( p_aout );
216         if ( var_Type( p_aout, "audio-device" ) != 0 )
217         {
218             var_Destroy( p_aout, "audio-device" );
219         }
220         if ( var_Type( p_aout, "audio-channels" ) != 0 )
221         {
222             var_Destroy( p_aout, "audio-channels" );
223         }
224     }
225
226     vlc_mutex_unlock( &p_aout->mixer_lock );
227
228     return 0;
229 }
230
231
232 /*
233  * Buffer management
234  */
235
236 /*****************************************************************************
237  * aout_DecNewBuffer : ask for a new empty buffer
238  *****************************************************************************/
239 aout_buffer_t * aout_DecNewBuffer( aout_instance_t * p_aout,
240                                    aout_input_t * p_input,
241                                    size_t i_nb_samples )
242 {
243     aout_buffer_t * p_buffer;
244     mtime_t duration;
245
246     vlc_mutex_lock( &p_input->lock );
247
248     if ( p_input->b_error )
249     {
250         vlc_mutex_unlock( &p_input->lock );
251         return NULL;
252     }
253
254     duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
255
256     /* This necessarily allocates in the heap. */
257     aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
258     p_buffer->i_nb_samples = i_nb_samples;
259     p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
260                               / p_input->input.i_frame_length;
261
262     /* Suppose the decoder doesn't have more than one buffered buffer */
263     p_input->b_changed = 0;
264
265     vlc_mutex_unlock( &p_input->lock );
266
267     if ( p_buffer == NULL )
268     {
269         msg_Err( p_aout, "NULL buffer !" );
270     }
271     else
272     {
273         p_buffer->start_date = p_buffer->end_date = 0;
274     }
275
276     return p_buffer;
277 }
278
279 /*****************************************************************************
280  * aout_DecDeleteBuffer : destroy an undecoded buffer
281  *****************************************************************************/
282 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
283                            aout_buffer_t * p_buffer )
284 {
285     aout_BufferFree( p_buffer );
286 }
287
288 /*****************************************************************************
289  * aout_DecPlay : filter & mix the decoded buffer
290  *****************************************************************************/
291 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
292                   aout_buffer_t * p_buffer )
293 {
294     if ( p_buffer->start_date == 0 )
295     {
296         msg_Warn( p_aout, "non-dated buffer received" );
297         aout_BufferFree( p_buffer );
298         return -1;
299     }
300
301     /* Apply the desynchronisation requested by the user */
302     p_buffer->start_date += p_input->i_desync;
303     p_buffer->end_date += p_input->i_desync;
304
305     if ( p_buffer->start_date > mdate() + p_input->i_pts_delay +
306          AOUT_MAX_ADVANCE_TIME )
307     {
308         msg_Warn( p_aout, "received buffer in the future ("I64Fd")",
309                   p_buffer->start_date - mdate());
310         aout_BufferFree( p_buffer );
311         return -1;
312     }
313
314     p_buffer->end_date = p_buffer->start_date
315                             + (mtime_t)(p_buffer->i_nb_samples * 1000000)
316                                 / p_input->input.i_rate;
317
318     vlc_mutex_lock( &p_input->lock );
319
320     if ( p_input->b_error )
321     {
322         vlc_mutex_unlock( &p_input->lock );
323         aout_BufferFree( p_buffer );
324         return -1;
325     }
326
327     if ( p_input->b_changed )
328     {
329         /* Maybe the allocation size has changed. Re-allocate a buffer. */
330         aout_buffer_t * p_new_buffer;
331         mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
332                             / p_input->input.i_rate;
333
334         aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
335         p_aout->p_vlc->pf_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
336                                   p_buffer->i_nb_bytes );
337         p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
338         p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
339         p_new_buffer->start_date = p_buffer->start_date;
340         p_new_buffer->end_date = p_buffer->end_date;
341         aout_BufferFree( p_buffer );
342         p_buffer = p_new_buffer;
343         p_input->b_changed = 0;
344     }
345
346     /* If the buffer is too early, wait a while. */
347     mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
348
349     if ( aout_InputPlay( p_aout, p_input, p_buffer ) == -1 )
350     {
351         vlc_mutex_unlock( &p_input->lock );
352         return -1;
353     }
354
355     vlc_mutex_unlock( &p_input->lock );
356
357     /* Run the mixer if it is able to run. */
358     vlc_mutex_lock( &p_aout->mixer_lock );
359     aout_MixerRun( p_aout );
360     vlc_mutex_unlock( &p_aout->mixer_lock );
361
362     return 0;
363 }