00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdlib.h>
00028 #include <string.h>
00029
00030 #include <vlc/vlc.h>
00031
00032 #ifdef HAVE_ALLOCA_H
00033 # include <alloca.h>
00034 #endif
00035
00036 #include "audio_output.h"
00037 #include "aout_internal.h"
00038 #include <vlc/input.h>
00039
00040
00041
00042
00043
00044
00045
00046
00047 static aout_input_t * DecNew( vlc_object_t * p_this, aout_instance_t * p_aout,
00048 audio_sample_format_t * p_format )
00049 {
00050 aout_input_t * p_input;
00051 input_thread_t * p_input_thread;
00052 vlc_value_t val;
00053
00054
00055
00056 vlc_mutex_lock( &p_aout->mixer_lock );
00057
00058 if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
00059 {
00060 msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
00061 return NULL;
00062 }
00063
00064 p_input = malloc(sizeof(aout_input_t));
00065 if ( p_input == NULL )
00066 {
00067 msg_Err( p_aout, "out of memory" );
00068 return NULL;
00069 }
00070
00071 vlc_mutex_init( p_aout, &p_input->lock );
00072
00073 p_input->b_changed = 0;
00074 p_input->b_error = 1;
00075 aout_FormatPrepare( p_format );
00076 memcpy( &p_input->input, p_format,
00077 sizeof(audio_sample_format_t) );
00078
00079 p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
00080 p_aout->i_nb_inputs++;
00081
00082 if ( p_aout->mixer.b_error )
00083 {
00084 int i;
00085
00086 var_Destroy( p_aout, "audio-device" );
00087 var_Destroy( p_aout, "audio-channels" );
00088
00089
00090 if ( aout_OutputNew( p_aout, p_format ) < 0 )
00091 {
00092 for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
00093 {
00094 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
00095 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
00096 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
00097 }
00098 vlc_mutex_unlock( &p_aout->mixer_lock );
00099 return p_input;
00100 }
00101
00102
00103 for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
00104 {
00105 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
00106 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
00107 aout_InputNew( p_aout, p_aout->pp_inputs[i] );
00108 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
00109 }
00110 }
00111 else
00112 {
00113 aout_MixerDelete( p_aout );
00114 }
00115
00116 if ( aout_MixerNew( p_aout ) == -1 )
00117 {
00118 aout_OutputDelete( p_aout );
00119 vlc_mutex_unlock( &p_aout->mixer_lock );
00120 return NULL;
00121 }
00122
00123 aout_InputNew( p_aout, p_input );
00124
00125 vlc_mutex_unlock( &p_aout->mixer_lock );
00126
00127 var_Create( p_this, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
00128 var_Get( p_this, "audio-desync", &val );
00129 p_input->i_desync = val.i_int * 1000;
00130
00131 p_input_thread = (input_thread_t *)vlc_object_find( p_this,
00132 VLC_OBJECT_INPUT, FIND_PARENT );
00133 if( p_input_thread )
00134 {
00135 p_input->i_pts_delay = p_input_thread->i_pts_delay;
00136 p_input->i_pts_delay += p_input->i_desync;
00137 vlc_object_release( p_input_thread );
00138 }
00139 else
00140 {
00141 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
00142 p_input->i_pts_delay += p_input->i_desync;
00143 }
00144
00145 return p_input;
00146 }
00147
00148 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
00149 aout_instance_t ** pp_aout,
00150 audio_sample_format_t * p_format )
00151 {
00152 if ( *pp_aout == NULL )
00153 {
00154
00155 *pp_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
00156
00157 if( *pp_aout == NULL )
00158 {
00159 msg_Dbg( p_this, "no aout present, spawning one" );
00160
00161 *pp_aout = aout_New( p_this );
00162
00163 if( *pp_aout == NULL )
00164 {
00165 return NULL;
00166 }
00167 vlc_object_attach( *pp_aout, p_this->p_vlc );
00168 }
00169 else
00170 {
00171 vlc_object_release( *pp_aout );
00172 }
00173 }
00174
00175 return DecNew( p_this, *pp_aout, p_format );
00176 }
00177
00178
00179
00180
00181 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
00182 {
00183 int i_input;
00184
00185
00186
00187 vlc_mutex_lock( &p_aout->mixer_lock );
00188
00189 for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
00190 {
00191 if ( p_aout->pp_inputs[i_input] == p_input )
00192 {
00193 break;
00194 }
00195 }
00196
00197 if ( i_input == p_aout->i_nb_inputs )
00198 {
00199 msg_Err( p_aout, "cannot find an input to delete" );
00200 return -1;
00201 }
00202
00203
00204 memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
00205 (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
00206 p_aout->i_nb_inputs--;
00207
00208 aout_InputDelete( p_aout, p_input );
00209
00210 vlc_mutex_destroy( &p_input->lock );
00211 free( p_input );
00212
00213 if ( !p_aout->i_nb_inputs )
00214 {
00215 aout_OutputDelete( p_aout );
00216 aout_MixerDelete( p_aout );
00217 if ( var_Type( p_aout, "audio-device" ) != 0 )
00218 {
00219 var_Destroy( p_aout, "audio-device" );
00220 }
00221 if ( var_Type( p_aout, "audio-channels" ) != 0 )
00222 {
00223 var_Destroy( p_aout, "audio-channels" );
00224 }
00225 }
00226
00227 vlc_mutex_unlock( &p_aout->mixer_lock );
00228
00229 return 0;
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 aout_buffer_t * aout_DecNewBuffer( aout_instance_t * p_aout,
00241 aout_input_t * p_input,
00242 size_t i_nb_samples )
00243 {
00244 aout_buffer_t * p_buffer;
00245 mtime_t duration;
00246
00247 vlc_mutex_lock( &p_input->lock );
00248
00249 if ( p_input->b_error )
00250 {
00251 vlc_mutex_unlock( &p_input->lock );
00252 return NULL;
00253 }
00254
00255 duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
00256
00257
00258 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
00259 p_buffer->i_nb_samples = i_nb_samples;
00260 p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
00261 / p_input->input.i_frame_length;
00262
00263
00264 p_input->b_changed = 0;
00265
00266 vlc_mutex_unlock( &p_input->lock );
00267
00268 if ( p_buffer == NULL )
00269 {
00270 msg_Err( p_aout, "NULL buffer !" );
00271 }
00272 else
00273 {
00274 p_buffer->start_date = p_buffer->end_date = 0;
00275 }
00276
00277 return p_buffer;
00278 }
00279
00280
00281
00282
00283 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
00284 aout_buffer_t * p_buffer )
00285 {
00286 aout_BufferFree( p_buffer );
00287 }
00288
00289
00290
00291
00292 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
00293 aout_buffer_t * p_buffer )
00294 {
00295 if ( p_buffer->start_date == 0 )
00296 {
00297 msg_Warn( p_aout, "non-dated buffer received" );
00298 aout_BufferFree( p_buffer );
00299 return -1;
00300 }
00301
00302
00303 p_buffer->start_date += p_input->i_desync;
00304 p_buffer->end_date += p_input->i_desync;
00305
00306 if ( p_buffer->start_date > mdate() + p_input->i_pts_delay +
00307 AOUT_MAX_ADVANCE_TIME )
00308 {
00309 msg_Warn( p_aout, "received buffer in the future ("I64Fd")",
00310 p_buffer->start_date - mdate());
00311 aout_BufferFree( p_buffer );
00312 return -1;
00313 }
00314
00315 p_buffer->end_date = p_buffer->start_date
00316 + (mtime_t)(p_buffer->i_nb_samples * 1000000)
00317 / p_input->input.i_rate;
00318
00319 vlc_mutex_lock( &p_input->lock );
00320
00321 if ( p_input->b_error )
00322 {
00323 vlc_mutex_unlock( &p_input->lock );
00324 aout_BufferFree( p_buffer );
00325 return -1;
00326 }
00327
00328 if ( p_input->b_changed )
00329 {
00330
00331 aout_buffer_t * p_new_buffer;
00332 mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
00333 / p_input->input.i_rate;
00334
00335 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
00336 p_aout->p_vlc->pf_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
00337 p_buffer->i_nb_bytes );
00338 p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
00339 p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
00340 p_new_buffer->start_date = p_buffer->start_date;
00341 p_new_buffer->end_date = p_buffer->end_date;
00342 aout_BufferFree( p_buffer );
00343 p_buffer = p_new_buffer;
00344 p_input->b_changed = 0;
00345 }
00346
00347
00348 mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
00349
00350 if ( aout_InputPlay( p_aout, p_input, p_buffer ) == -1 )
00351 {
00352 vlc_mutex_unlock( &p_input->lock );
00353 return -1;
00354 }
00355
00356 vlc_mutex_unlock( &p_input->lock );
00357
00358
00359 vlc_mutex_lock( &p_aout->mixer_lock );
00360 aout_MixerRun( p_aout );
00361 vlc_mutex_unlock( &p_aout->mixer_lock );
00362
00363 return 0;
00364 }