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,
49 audio_replay_gain_t *p_replay_gain )
51 aout_input_t * p_input;
52 input_thread_t * p_input_thread;
55 /* Sanitize audio format */
56 if( p_format->i_channels > 32 )
58 msg_Err( p_aout, "too many audio channels (%u)",
59 p_format->i_channels );
63 if( p_format->i_rate > 192000 )
65 msg_Err( p_aout, "excessive audio sample frequency (%u)",
70 /* We can only be called by the decoder, so no need to lock
72 vlc_mutex_lock( &p_aout->mixer_lock );
74 if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
76 msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
80 p_input = malloc(sizeof(aout_input_t));
81 if ( p_input == NULL )
83 msg_Err( p_aout, "out of memory" );
86 memset( p_input, 0, sizeof(aout_input_t) );
88 vlc_mutex_init( p_aout, &p_input->lock );
90 p_input->b_changed = 0;
92 aout_FormatPrepare( p_format );
93 memcpy( &p_input->input, p_format,
94 sizeof(audio_sample_format_t) );
96 p_input->replay_gain = *p_replay_gain;
98 p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
99 p_aout->i_nb_inputs++;
101 if ( p_aout->mixer.b_error )
105 var_Destroy( p_aout, "audio-device" );
106 var_Destroy( p_aout, "audio-channels" );
108 /* Recreate the output using the new format. */
109 if ( aout_OutputNew( p_aout, p_format ) < 0 )
111 for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
113 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
114 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
115 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
117 vlc_mutex_unlock( &p_aout->mixer_lock );
121 /* Create other input streams. */
122 for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
124 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
125 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
126 aout_InputNew( p_aout, p_aout->pp_inputs[i] );
127 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
132 aout_MixerDelete( p_aout );
135 if ( aout_MixerNew( p_aout ) == -1 )
137 aout_OutputDelete( p_aout );
141 aout_InputNew( p_aout, p_input );
143 vlc_mutex_unlock( &p_aout->mixer_lock );
144 var_Create( p_this, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
145 var_Get( p_this, "audio-desync", &val );
146 p_input->i_desync = val.i_int * 1000;
148 p_input_thread = (input_thread_t *)vlc_object_find( p_this,
149 VLC_OBJECT_INPUT, FIND_PARENT );
152 p_input->i_pts_delay = p_input_thread->i_pts_delay;
153 p_input->i_pts_delay += p_input->i_desync;
154 p_input->p_input_thread = p_input_thread;
155 vlc_object_release( p_input_thread );
159 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
160 p_input->i_pts_delay += p_input->i_desync;
161 p_input->p_input_thread = NULL;
167 vlc_mutex_unlock( &p_aout->mixer_lock );
171 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
172 aout_instance_t ** pp_aout,
173 audio_sample_format_t * p_format,
174 audio_replay_gain_t *p_replay_gain )
176 if ( *pp_aout == NULL )
178 /* Create an audio output if there is none. */
179 *pp_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
181 if( *pp_aout == NULL )
183 msg_Dbg( p_this, "no aout present, spawning one" );
185 *pp_aout = aout_New( p_this );
186 /* Everything failed, I'm a loser, I just wanna die */
187 if( *pp_aout == NULL )
191 vlc_object_attach( *pp_aout, p_this->p_libvlc );
195 vlc_object_release( *pp_aout );
199 return DecNew( p_this, *pp_aout, p_format, p_replay_gain );
202 /*****************************************************************************
203 * aout_DecDelete : delete a decoder
204 *****************************************************************************/
205 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
209 /* This function can only be called by the decoder itself, so no need
210 * to lock p_input->lock. */
211 vlc_mutex_lock( &p_aout->mixer_lock );
213 for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
215 if ( p_aout->pp_inputs[i_input] == p_input )
221 if ( i_input == p_aout->i_nb_inputs )
223 msg_Err( p_aout, "cannot find an input to delete" );
227 /* Remove the input from the list. */
228 memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
229 (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
230 p_aout->i_nb_inputs--;
232 aout_InputDelete( p_aout, p_input );
234 vlc_mutex_destroy( &p_input->lock );
237 if ( !p_aout->i_nb_inputs )
239 aout_OutputDelete( p_aout );
240 aout_MixerDelete( p_aout );
241 if ( var_Type( p_aout, "audio-device" ) != 0 )
243 var_Destroy( p_aout, "audio-device" );
245 if ( var_Type( p_aout, "audio-channels" ) != 0 )
247 var_Destroy( p_aout, "audio-channels" );
251 vlc_mutex_unlock( &p_aout->mixer_lock );
261 /*****************************************************************************
262 * aout_DecNewBuffer : ask for a new empty buffer
263 *****************************************************************************/
264 aout_buffer_t * aout_DecNewBuffer( aout_instance_t * p_aout,
265 aout_input_t * p_input,
266 size_t i_nb_samples )
268 aout_buffer_t * p_buffer;
271 vlc_mutex_lock( &p_input->lock );
273 if ( p_input->b_error )
275 vlc_mutex_unlock( &p_input->lock );
279 duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
281 /* This necessarily allocates in the heap. */
282 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
283 p_buffer->i_nb_samples = i_nb_samples;
284 p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
285 / p_input->input.i_frame_length;
287 /* Suppose the decoder doesn't have more than one buffered buffer */
288 p_input->b_changed = 0;
290 vlc_mutex_unlock( &p_input->lock );
292 if ( p_buffer == NULL )
294 msg_Err( p_aout, "NULL buffer !" );
298 p_buffer->start_date = p_buffer->end_date = 0;
304 /*****************************************************************************
305 * aout_DecDeleteBuffer : destroy an undecoded buffer
306 *****************************************************************************/
307 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
308 aout_buffer_t * p_buffer )
310 (void)p_aout; (void)p_input;
311 aout_BufferFree( p_buffer );
314 /*****************************************************************************
315 * aout_DecPlay : filter & mix the decoded buffer
316 *****************************************************************************/
317 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
318 aout_buffer_t * p_buffer, int i_input_rate )
320 if ( p_buffer->start_date == 0 )
322 msg_Warn( p_aout, "non-dated buffer received" );
323 aout_BufferFree( p_buffer );
327 if( i_input_rate > INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE ||
328 i_input_rate < INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE )
330 aout_BufferFree( p_buffer );
334 /* Apply the desynchronisation requested by the user */
335 p_buffer->start_date += p_input->i_desync;
336 p_buffer->end_date += p_input->i_desync;
338 if ( p_buffer->start_date > mdate() + p_input->i_pts_delay +
339 AOUT_MAX_ADVANCE_TIME )
341 msg_Warn( p_aout, "received buffer in the future ("I64Fd")",
342 p_buffer->start_date - mdate());
343 if( p_input->p_input_thread )
345 vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
346 stats_UpdateInteger( p_aout,
347 p_input->p_input_thread->p->counters.p_lost_abuffers,
349 vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
351 aout_BufferFree( p_buffer );
355 p_buffer->end_date = p_buffer->start_date
356 + (mtime_t)p_buffer->i_nb_samples * 1000000
357 / p_input->input.i_rate;
359 vlc_mutex_lock( &p_input->lock );
361 if ( p_input->b_error )
363 vlc_mutex_unlock( &p_input->lock );
364 aout_BufferFree( p_buffer );
368 if ( p_input->b_changed )
370 /* Maybe the allocation size has changed. Re-allocate a buffer. */
371 aout_buffer_t * p_new_buffer;
372 mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
373 / p_input->input.i_rate;
375 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
376 p_aout->p_libvlc->pf_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
377 p_buffer->i_nb_bytes );
378 p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
379 p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
380 p_new_buffer->start_date = p_buffer->start_date;
381 p_new_buffer->end_date = p_buffer->end_date;
382 aout_BufferFree( p_buffer );
383 p_buffer = p_new_buffer;
384 p_input->b_changed = 0;
387 /* If the buffer is too early, wait a while. */
388 mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
390 if ( aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate ) == -1 )
392 vlc_mutex_unlock( &p_input->lock );
396 vlc_mutex_unlock( &p_input->lock );
398 /* Run the mixer if it is able to run. */
399 vlc_mutex_lock( &p_aout->mixer_lock );
400 aout_MixerRun( p_aout );
401 if( p_input->p_input_thread )
403 vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
404 stats_UpdateInteger( p_aout,
405 p_input->p_input_thread->p->counters.p_played_abuffers,
407 vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
409 vlc_mutex_unlock( &p_aout->mixer_lock );