1 /*****************************************************************************
2 * audio_output.c : audio output thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: audio_output.c,v 1.69 2001/12/30 07:09:56 sam Exp $
7 * Authors: Michel Kaempf <maxx@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 <stdio.h> /* "intf_msg.h" */
28 #include <stdlib.h> /* calloc(), malloc(), free() */
31 #include <videolan/vlc.h>
34 # include <unistd.h> /* getpid() */
37 #ifdef WIN32 /* getpid() for win32 is located in process.h */
41 #include "audio_output.h"
42 #include "aout_common.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 static int aout_SpawnThread( aout_thread_t * p_aout );
49 /*****************************************************************************
50 * aout_InitBank: initialize the audio output bank.
51 *****************************************************************************/
52 void aout_InitBank ( void )
54 p_aout_bank->i_count = 0;
56 vlc_mutex_init( &p_aout_bank->lock );
59 /*****************************************************************************
60 * aout_EndBank: empty the audio output bank.
61 *****************************************************************************
62 * This function ends all unused audio outputs and empties the bank in
64 *****************************************************************************/
65 void aout_EndBank ( void )
67 /* Ask all remaining audio outputs to die */
68 while( p_aout_bank->i_count )
71 p_aout_bank->pp_aout[ --p_aout_bank->i_count ], NULL );
74 vlc_mutex_destroy( &p_aout_bank->lock );
77 /*****************************************************************************
78 * aout_CreateThread: initialize audio thread
79 *****************************************************************************/
80 aout_thread_t *aout_CreateThread( int *pi_status )
82 aout_thread_t * p_aout; /* thread descriptor */
84 int i_status; /* thread status */
87 /* Allocate descriptor */
88 p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
94 /* Choose the best module */
95 p_aout->p_module = module_Need( MODULE_CAPABILITY_AOUT,
96 main_GetPszVariable( AOUT_METHOD_VAR, NULL ),
99 if( p_aout->p_module == NULL )
101 intf_ErrMsg( "aout error: no suitable aout module" );
106 #define aout_functions p_aout->p_module->p_functions->aout.functions.aout
107 p_aout->pf_open = aout_functions.pf_open;
108 p_aout->pf_setformat = aout_functions.pf_setformat;
109 p_aout->pf_getbufinfo = aout_functions.pf_getbufinfo;
110 p_aout->pf_play = aout_functions.pf_play;
111 p_aout->pf_close = aout_functions.pf_close;
112 #undef aout_functions
115 * Initialize audio device
117 if ( p_aout->pf_open( p_aout ) )
119 module_Unneed( p_aout->p_module );
124 if( p_aout->l_rate == 0 )
126 intf_ErrMsg( "aout error: null sample rate" );
127 p_aout->pf_close( p_aout );
128 module_Unneed( p_aout->p_module );
133 /* special setting for ac3 pass-through mode */
134 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) && p_main->b_ac3 )
136 intf_WarnMsg( 4, "aout info: setting ac3 spdif" );
137 p_aout->i_format = AOUT_FMT_AC3;
138 p_aout->l_rate = 48000;
141 /* FIXME: only works for i_channels == 1 or 2 ?? */
142 p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0;
144 if ( p_aout->pf_setformat( p_aout ) )
146 p_aout->pf_close( p_aout );
147 module_Unneed( p_aout->p_module );
152 /* Initialize the volume level */
153 p_aout->i_volume = main_GetIntVariable( AOUT_VOLUME_VAR, VOLUME_DEFAULT );
154 p_aout->i_savedvolume = 0;
156 /* FIXME: maybe it would be cleaner to change SpawnThread prototype
157 * see vout to handle status correctly ?? however, it is not critical since
158 * this thread is only called in main and all calls are blocking */
159 if( aout_SpawnThread( p_aout ) )
161 p_aout->pf_close( p_aout );
162 module_Unneed( p_aout->p_module );
170 /*****************************************************************************
172 *****************************************************************************/
173 static int aout_SpawnThread( aout_thread_t * p_aout )
177 void (* pf_aout_thread)( aout_thread_t * ) = NULL;
179 /* We want the audio output thread to live */
181 p_aout->b_active = 1;
183 /* Initialize the fifos lock */
184 vlc_mutex_init( &p_aout->fifos_lock );
185 /* Initialize audio fifos : set all fifos as empty and initialize locks */
186 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
188 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
189 vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
190 vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
193 /* Compute the size (in audio units) of the audio output buffer. Although
194 * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
195 * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
196 p_aout->l_units = (long)( ((s64)p_aout->l_rate * AOUT_BUFFER_DURATION) / 1000000 );
197 p_aout->l_msleep = (long)( ((s64)p_aout->l_units * 1000000) / (s64)p_aout->l_rate );
199 /* Make pf_aout_thread point to the right thread function, and compute the
200 * byte size of the audio output buffer */
201 switch ( p_aout->i_channels )
203 /* Audio output is mono */
205 switch ( p_aout->i_format )
208 intf_WarnMsg( 2, "aout info: unsigned 8 bits mono thread" );
209 l_bytes = 1 * sizeof(u8) * p_aout->l_units;
210 pf_aout_thread = aout_U8MonoThread;
214 intf_WarnMsg( 2, "aout info: signed 8 bits mono thread" );
215 l_bytes = 1 * sizeof(s8) * p_aout->l_units;
216 pf_aout_thread = aout_S8MonoThread;
219 case AOUT_FMT_U16_LE:
220 case AOUT_FMT_U16_BE:
221 intf_WarnMsg( 2, "aout info: unsigned 16 bits mono thread" );
222 l_bytes = 1 * sizeof(u16) * p_aout->l_units;
223 pf_aout_thread = aout_U16MonoThread;
226 case AOUT_FMT_S16_LE:
227 case AOUT_FMT_S16_BE:
228 intf_WarnMsg( 2, "aout info: signed 16 bits mono thread" );
229 l_bytes = 1 * sizeof(s16) * p_aout->l_units;
230 pf_aout_thread = aout_S16MonoThread;
234 intf_ErrMsg( "aout error: unknown audio output format (%i)",
240 /* Audio output is stereo */
242 switch ( p_aout->i_format )
245 intf_WarnMsg( 2, "aout info: unsigned 8 bits stereo thread" );
246 l_bytes = 2 * sizeof(u8) * p_aout->l_units;
247 pf_aout_thread = aout_U8StereoThread;
251 intf_WarnMsg( 2, "aout info: signed 8 bits stereo thread" );
252 l_bytes = 2 * sizeof(s8) * p_aout->l_units;
253 pf_aout_thread = aout_S8StereoThread;
256 case AOUT_FMT_U16_LE:
257 case AOUT_FMT_U16_BE:
258 intf_WarnMsg( 2, "aout info: unsigned 16 bits stereo thread" );
259 l_bytes = 2 * sizeof(u16) * p_aout->l_units;
260 pf_aout_thread = aout_U16StereoThread;
263 case AOUT_FMT_S16_LE:
264 case AOUT_FMT_S16_BE:
265 intf_WarnMsg( 2, "aout info: signed 16 bits stereo thread" );
266 l_bytes = 2 * sizeof(s16) * p_aout->l_units;
267 pf_aout_thread = aout_S16StereoThread;
271 intf_WarnMsg( 2, "aout info: ac3 pass-through thread" );
272 l_bytes = SPDIF_FRAME_SIZE;
273 pf_aout_thread = aout_SpdifThread;
277 intf_ErrMsg( "aout error: unknown audio output format %i",
284 intf_ErrMsg( "aout error: unknown number of audio channels (%i)",
285 p_aout->i_channels );
289 /* Allocate the memory needed by the audio output buffers, and set to zero
290 * the s32 buffer's memory */
291 p_aout->buffer = malloc( l_bytes );
292 if ( p_aout->buffer == NULL )
294 intf_ErrMsg( "aout error: cannot create output buffer" );
298 p_aout->s32_buffer = (s32 *)calloc( p_aout->l_units,
299 sizeof(s32) << ( p_aout->b_stereo ) );
300 if ( p_aout->s32_buffer == NULL )
302 intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
303 free( p_aout->buffer );
307 /* Rough estimate of the playing date */
308 p_aout->date = mdate() + p_main->i_desync;
310 /* Launch the thread */
311 if ( vlc_thread_create( &p_aout->thread_id, "audio output",
312 (vlc_thread_func_t)pf_aout_thread, p_aout ) )
314 intf_ErrMsg( "aout error: cannot spawn audio output thread" );
315 free( p_aout->buffer );
316 free( p_aout->s32_buffer );
320 intf_WarnMsg( 2, "aout info: audio output thread %i spawned", getpid() );
324 /*****************************************************************************
326 *****************************************************************************/
327 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
331 /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
333 /* Ask thread to kill itself and wait until it's done */
335 vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
337 /* Free the allocated memory */
338 free( p_aout->buffer );
339 free( p_aout->s32_buffer );
341 /* Destroy the condition and mutex locks */
342 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
344 vlc_mutex_destroy( &p_aout->fifo[i_fifo].data_lock );
345 vlc_cond_destroy( &p_aout->fifo[i_fifo].data_wait );
347 vlc_mutex_destroy( &p_aout->fifos_lock );
349 /* Free the plugin */
350 p_aout->pf_close( p_aout );
352 /* Release the aout module */
353 module_Unneed( p_aout->p_module );