Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

dec.c

00001 /*****************************************************************************
00002  * dec.c : audio output API towards decoders
00003  *****************************************************************************
00004  * Copyright (C) 2002-2004 the VideoLAN team
00005  * $Id: dec.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Christophe Massiot <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>                            /* calloc(), malloc(), free() */
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>                 /* for input_thread_t and i_pts_delay */
00039 
00040 /*
00041  * Creation/Deletion
00042  */
00043 
00044 /*****************************************************************************
00045  * aout_DecNew : create a decoder
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     /* We can only be called by the decoder, so no need to lock
00055      * p_input->lock. */
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         /* Recreate the output using the new format. */
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         /* Create other input streams. */
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         /* Create an audio output if there is none. */
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             /* Everything failed, I'm a loser, I just wanna die */
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  * aout_DecDelete : delete a decoder
00180  *****************************************************************************/
00181 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
00182 {
00183     int i_input;
00184 
00185     /* This function can only be called by the decoder itself, so no need
00186      * to lock p_input->lock. */
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     /* Remove the input from the list. */
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  * Buffer management
00235  */
00236 
00237 /*****************************************************************************
00238  * aout_DecNewBuffer : ask for a new empty buffer
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     /* This necessarily allocates in the heap. */
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     /* Suppose the decoder doesn't have more than one buffered buffer */
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  * aout_DecDeleteBuffer : destroy an undecoded buffer
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  * aout_DecPlay : filter & mix the decoded buffer
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     /* Apply the desynchronisation requested by the user */
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         /* Maybe the allocation size has changed. Re-allocate a buffer. */
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     /* If the buffer is too early, wait a while. */
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     /* Run the mixer if it is able to run. */
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 }

Generated on Tue Dec 20 10:14:59 2005 for vlc-0.8.4a by  doxygen 1.4.2