1 /*****************************************************************************
2 * audio_output.c : audio output instance
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: audio_output.c,v 1.98 2002/08/19 23:12:57 massiot Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* calloc(), malloc(), free() */
36 #include "audio_output.h"
37 #include "aout_internal.h"
39 /*****************************************************************************
40 * aout_NewInstance: initialize aout structure
41 *****************************************************************************/
42 aout_instance_t * __aout_NewInstance( vlc_object_t * p_parent )
44 aout_instance_t * p_aout;
46 /* Allocate descriptor. */
47 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
53 /* Initialize members. */
54 vlc_mutex_init( p_parent, &p_aout->input_lock );
55 vlc_cond_init( p_parent, &p_aout->input_signal );
56 p_aout->i_inputs_active = 0;
57 p_aout->b_change_requested = 0;
58 p_aout->i_nb_inputs = 0;
60 vlc_mutex_init( p_parent, &p_aout->mixer_lock );
62 vlc_object_attach( p_aout, p_parent->p_vlc );
67 /*****************************************************************************
68 * aout_DeleteInstance: destroy aout structure
69 *****************************************************************************/
70 void aout_DeleteInstance( aout_instance_t * p_aout )
72 vlc_mutex_destroy( &p_aout->input_lock );
73 vlc_cond_destroy( &p_aout->input_signal );
74 vlc_mutex_destroy( &p_aout->mixer_lock );
77 vlc_object_destroy( p_aout );
80 /*****************************************************************************
81 * aout_BufferNew : ask for a new empty buffer
82 *****************************************************************************/
83 aout_buffer_t * aout_BufferNew( aout_instance_t * p_aout,
84 aout_input_t * p_input,
87 aout_buffer_t * p_buffer;
88 mtime_t duration = (1000000 * (mtime_t)i_nb_samples)
89 / p_input->input.i_rate;
91 /* This necessarily allocates in the heap. */
92 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
93 p_buffer->i_nb_samples = i_nb_samples;
94 p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
95 / p_input->input.i_frame_length;
97 if ( p_buffer == NULL )
99 msg_Err( p_aout, "NULL buffer !" );
103 p_buffer->start_date = p_buffer->end_date = 0;
109 /*****************************************************************************
110 * aout_BufferDelete : destroy an undecoded buffer
111 *****************************************************************************/
112 void aout_BufferDelete( aout_instance_t * p_aout, aout_input_t * p_input,
113 aout_buffer_t * p_buffer )
115 aout_BufferFree( p_buffer );
118 /*****************************************************************************
119 * aout_BufferPlay : filter & mix the decoded buffer
120 *****************************************************************************/
121 void aout_BufferPlay( aout_instance_t * p_aout, aout_input_t * p_input,
122 aout_buffer_t * p_buffer )
124 if ( p_buffer->start_date == 0 )
126 msg_Warn( p_aout, "non-dated buffer received" );
127 aout_BufferFree( p_buffer );
131 p_buffer->end_date = p_buffer->start_date
132 + (mtime_t)(p_buffer->i_nb_samples * 1000000)
133 / p_input->input.i_rate;
136 /* If the buffer is too early, wait a while. */
137 mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
139 aout_InputPlay( p_aout, p_input, p_buffer );
141 /* Run the mixer if it is able to run. */
142 aout_MixerRun( p_aout );
145 /*****************************************************************************
146 * aout_FormatPrepare : compute the number of bytes per frame & frame length
147 *****************************************************************************/
148 void aout_FormatPrepare( audio_sample_format_t * p_format )
152 switch ( p_format->i_format )
159 case AOUT_FMT_U16_LE:
160 case AOUT_FMT_U16_BE:
161 case AOUT_FMT_S16_LE:
162 case AOUT_FMT_S16_BE:
166 case AOUT_FMT_FLOAT32:
167 case AOUT_FMT_FIXED32:
174 /* For these formats the caller has to indicate the parameters
179 i_result = 0; /* will segfault much sooner... */
182 p_format->i_bytes_per_frame = i_result * p_format->i_channels;
183 p_format->i_frame_length = 1;