1 /*****************************************************************************
2 * dec.c : audio output API towards decoders
3 *****************************************************************************
4 * Copyright (C) 2002-2007 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* calloc(), malloc(), free() */
37 #include <vlc_input.h>
39 #include "aout_internal.h"
41 /** FIXME: Ugly but needed to access the counters */
42 #include "input/input_internal.h"
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 )
50 aout_input_t * p_input;
51 input_thread_t * p_input_thread;
54 /* We can only be called by the decoder, so no need to lock
56 vlc_mutex_lock( &p_aout->mixer_lock );
58 if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
60 msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
64 p_input = malloc(sizeof(aout_input_t));
65 if ( p_input == NULL )
67 msg_Err( p_aout, "out of memory" );
71 vlc_mutex_init( p_aout, &p_input->lock );
73 p_input->b_changed = 0;
75 aout_FormatPrepare( p_format );
76 memcpy( &p_input->input, p_format,
77 sizeof(audio_sample_format_t) );
79 p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
80 p_aout->i_nb_inputs++;
82 if ( p_aout->mixer.b_error )
86 var_Destroy( p_aout, "audio-device" );
87 var_Destroy( p_aout, "audio-channels" );
89 /* Recreate the output using the new format. */
90 if ( aout_OutputNew( p_aout, p_format ) < 0 )
92 for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
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 );
98 vlc_mutex_unlock( &p_aout->mixer_lock );
102 /* Create other input streams. */
103 for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
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 );
113 aout_MixerDelete( p_aout );
116 if ( aout_MixerNew( p_aout ) == -1 )
118 aout_OutputDelete( p_aout );
119 vlc_mutex_unlock( &p_aout->mixer_lock );
123 aout_InputNew( p_aout, p_input );
125 vlc_mutex_unlock( &p_aout->mixer_lock );
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;
131 p_input_thread = (input_thread_t *)vlc_object_find( p_this,
132 VLC_OBJECT_INPUT, FIND_PARENT );
135 p_input->i_pts_delay = p_input_thread->i_pts_delay;
136 p_input->i_pts_delay += p_input->i_desync;
137 p_input->p_input_thread = p_input_thread;
138 vlc_object_release( p_input_thread );
142 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
143 p_input->i_pts_delay += p_input->i_desync;
144 p_input->p_input_thread = NULL;
150 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
151 aout_instance_t ** pp_aout,
152 audio_sample_format_t * p_format )
154 if ( *pp_aout == NULL )
156 /* Create an audio output if there is none. */
157 *pp_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
159 if( *pp_aout == NULL )
161 msg_Dbg( p_this, "no aout present, spawning one" );
163 *pp_aout = aout_New( p_this );
164 /* Everything failed, I'm a loser, I just wanna die */
165 if( *pp_aout == NULL )
169 vlc_object_attach( *pp_aout, p_this->p_libvlc );
173 vlc_object_release( *pp_aout );
177 return DecNew( p_this, *pp_aout, p_format );
180 /*****************************************************************************
181 * aout_DecDelete : delete a decoder
182 *****************************************************************************/
183 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
187 /* This function can only be called by the decoder itself, so no need
188 * to lock p_input->lock. */
189 vlc_mutex_lock( &p_aout->mixer_lock );
191 for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
193 if ( p_aout->pp_inputs[i_input] == p_input )
199 if ( i_input == p_aout->i_nb_inputs )
201 msg_Err( p_aout, "cannot find an input to delete" );
205 /* Remove the input from the list. */
206 memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
207 (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
208 p_aout->i_nb_inputs--;
210 aout_InputDelete( p_aout, p_input );
212 vlc_mutex_destroy( &p_input->lock );
215 if ( !p_aout->i_nb_inputs )
217 aout_OutputDelete( p_aout );
218 aout_MixerDelete( p_aout );
219 if ( var_Type( p_aout, "audio-device" ) != 0 )
221 var_Destroy( p_aout, "audio-device" );
223 if ( var_Type( p_aout, "audio-channels" ) != 0 )
225 var_Destroy( p_aout, "audio-channels" );
229 vlc_mutex_unlock( &p_aout->mixer_lock );
239 /*****************************************************************************
240 * aout_DecNewBuffer : ask for a new empty buffer
241 *****************************************************************************/
242 aout_buffer_t * aout_DecNewBuffer( aout_instance_t * p_aout,
243 aout_input_t * p_input,
244 size_t i_nb_samples )
246 aout_buffer_t * p_buffer;
249 vlc_mutex_lock( &p_input->lock );
251 if ( p_input->b_error )
253 vlc_mutex_unlock( &p_input->lock );
257 duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
259 /* This necessarily allocates in the heap. */
260 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
261 p_buffer->i_nb_samples = i_nb_samples;
262 p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
263 / p_input->input.i_frame_length;
265 /* Suppose the decoder doesn't have more than one buffered buffer */
266 p_input->b_changed = 0;
268 vlc_mutex_unlock( &p_input->lock );
270 if ( p_buffer == NULL )
272 msg_Err( p_aout, "NULL buffer !" );
276 p_buffer->start_date = p_buffer->end_date = 0;
282 /*****************************************************************************
283 * aout_DecDeleteBuffer : destroy an undecoded buffer
284 *****************************************************************************/
285 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
286 aout_buffer_t * p_buffer )
288 (void)p_aout; (void)p_input;
289 aout_BufferFree( p_buffer );
292 /*****************************************************************************
293 * aout_DecPlay : filter & mix the decoded buffer
294 *****************************************************************************/
295 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
296 aout_buffer_t * p_buffer )
298 if ( p_buffer->start_date == 0 )
300 msg_Warn( p_aout, "non-dated buffer received" );
301 aout_BufferFree( p_buffer );
305 /* Apply the desynchronisation requested by the user */
306 p_buffer->start_date += p_input->i_desync;
307 p_buffer->end_date += p_input->i_desync;
309 if ( p_buffer->start_date > mdate() + p_input->i_pts_delay +
310 AOUT_MAX_ADVANCE_TIME )
312 msg_Warn( p_aout, "received buffer in the future ("I64Fd")",
313 p_buffer->start_date - mdate());
314 if( p_input->p_input_thread )
316 vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
317 stats_UpdateInteger( p_aout,
318 p_input->p_input_thread->p->counters.p_lost_abuffers,
320 vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
322 aout_BufferFree( p_buffer );
326 p_buffer->end_date = p_buffer->start_date
327 + (mtime_t)(p_buffer->i_nb_samples * 1000000)
328 / p_input->input.i_rate;
330 vlc_mutex_lock( &p_input->lock );
332 if ( p_input->b_error )
334 vlc_mutex_unlock( &p_input->lock );
335 aout_BufferFree( p_buffer );
339 if ( p_input->b_changed )
341 /* Maybe the allocation size has changed. Re-allocate a buffer. */
342 aout_buffer_t * p_new_buffer;
343 mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
344 / p_input->input.i_rate;
346 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
347 p_aout->p_libvlc->pf_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
348 p_buffer->i_nb_bytes );
349 p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
350 p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
351 p_new_buffer->start_date = p_buffer->start_date;
352 p_new_buffer->end_date = p_buffer->end_date;
353 aout_BufferFree( p_buffer );
354 p_buffer = p_new_buffer;
355 p_input->b_changed = 0;
358 /* If the buffer is too early, wait a while. */
359 mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
361 if ( aout_InputPlay( p_aout, p_input, p_buffer ) == -1 )
363 vlc_mutex_unlock( &p_input->lock );
367 vlc_mutex_unlock( &p_input->lock );
369 /* Run the mixer if it is able to run. */
370 vlc_mutex_lock( &p_aout->mixer_lock );
371 aout_MixerRun( p_aout );
372 if( p_input->p_input_thread )
374 vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
375 stats_UpdateInteger( p_aout,
376 p_input->p_input_thread->p->counters.p_played_abuffers,
378 vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
380 vlc_mutex_unlock( &p_aout->mixer_lock );