1 /*****************************************************************************
2 * audio_output.c : audio output thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: audio_output.c,v 1.66 2001/11/28 15:08:06 massiot 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 *****************************************************************************/
30 #include <unistd.h> /* getpid() */
33 #ifdef WIN32 /* getpid() for win32 is located in process.h */
37 #include <stdio.h> /* "intf_msg.h" */
38 #include <stdlib.h> /* calloc(), malloc(), free() */
43 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
45 #include "mtime.h" /* mtime_t, mdate(), msleep() */
48 #include "audio_output.h"
49 #include "aout_common.h"
53 /*****************************************************************************
55 *****************************************************************************/
56 static int aout_SpawnThread( aout_thread_t * p_aout );
58 /*****************************************************************************
59 * aout_InitBank: initialize the audio output bank.
60 *****************************************************************************/
61 void aout_InitBank ( void )
63 p_aout_bank->i_count = 0;
65 vlc_mutex_init( &p_aout_bank->lock );
68 /*****************************************************************************
69 * aout_EndBank: empty the audio output bank.
70 *****************************************************************************
71 * This function ends all unused audio outputs and empties the bank in
73 *****************************************************************************/
74 void aout_EndBank ( void )
76 /* Ask all remaining audio outputs to die */
77 while( p_aout_bank->i_count )
80 p_aout_bank->pp_aout[ --p_aout_bank->i_count ], NULL );
83 vlc_mutex_destroy( &p_aout_bank->lock );
86 /*****************************************************************************
87 * aout_CreateThread: initialize audio thread
88 *****************************************************************************/
89 aout_thread_t *aout_CreateThread( int *pi_status )
91 aout_thread_t * p_aout; /* thread descriptor */
93 int i_status; /* thread status */
96 /* Allocate descriptor */
97 p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
103 /* Choose the best module */
104 p_aout->p_module = module_Need( MODULE_CAPABILITY_AOUT, NULL );
106 if( p_aout->p_module == NULL )
108 intf_ErrMsg( "aout error: no suitable aout module" );
113 #define aout_functions p_aout->p_module->p_functions->aout.functions.aout
114 p_aout->pf_open = aout_functions.pf_open;
115 p_aout->pf_setformat = aout_functions.pf_setformat;
116 p_aout->pf_getbufinfo = aout_functions.pf_getbufinfo;
117 p_aout->pf_play = aout_functions.pf_play;
118 p_aout->pf_close = aout_functions.pf_close;
119 #undef aout_functions
122 * Initialize audio device
124 if ( p_aout->pf_open( p_aout ) )
126 module_Unneed( p_aout->p_module );
131 if( p_aout->l_rate == 0 )
133 intf_ErrMsg( "aout error: null sample rate" );
134 p_aout->pf_close( p_aout );
135 module_Unneed( p_aout->p_module );
140 /* special setting for ac3 pass-through mode */
141 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) && p_main->b_ac3 )
143 intf_WarnMsg( 4, "aout info: setting ac3 spdif" );
144 p_aout->i_format = AOUT_FMT_AC3;
145 p_aout->l_rate = 48000;
148 /* FIXME: only works for i_channels == 1 or 2 ?? */
149 p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0;
151 if ( p_aout->pf_setformat( p_aout ) )
153 p_aout->pf_close( p_aout );
154 module_Unneed( p_aout->p_module );
159 /* Initialize the volume level */
160 p_aout->i_volume = main_GetIntVariable( AOUT_VOLUME_VAR, VOLUME_DEFAULT );
161 p_aout->i_savedvolume = 0;
163 /* FIXME: maybe it would be cleaner to change SpawnThread prototype
164 * see vout to handle status correctly ?? however, it is not critical since
165 * this thread is only called in main and all calls are blocking */
166 if( aout_SpawnThread( p_aout ) )
168 p_aout->pf_close( p_aout );
169 module_Unneed( p_aout->p_module );
177 /*****************************************************************************
179 *****************************************************************************/
180 static int aout_SpawnThread( aout_thread_t * p_aout )
184 void (* pf_aout_thread)( aout_thread_t * ) = NULL;
186 /* We want the audio output thread to live */
188 p_aout->b_active = 1;
190 /* Initialize the fifos lock */
191 vlc_mutex_init( &p_aout->fifos_lock );
192 /* Initialize audio fifos : set all fifos as empty and initialize locks */
193 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
195 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
196 vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
197 vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
200 /* Compute the size (in audio units) of the audio output buffer. Although
201 * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
202 * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
203 p_aout->l_units = (long)( ((s64)p_aout->l_rate * AOUT_BUFFER_DURATION) / 1000000 );
204 p_aout->l_msleep = (long)( ((s64)p_aout->l_units * 1000000) / (s64)p_aout->l_rate );
206 /* Make pf_aout_thread point to the right thread function, and compute the
207 * byte size of the audio output buffer */
208 switch ( p_aout->i_channels )
210 /* Audio output is mono */
212 switch ( p_aout->i_format )
215 intf_WarnMsg( 2, "aout info: unsigned 8 bits mono thread" );
216 l_bytes = 1 * sizeof(u8) * p_aout->l_units;
217 pf_aout_thread = aout_U8MonoThread;
221 intf_WarnMsg( 2, "aout info: signed 8 bits mono thread" );
222 l_bytes = 1 * sizeof(s8) * p_aout->l_units;
223 pf_aout_thread = aout_S8MonoThread;
226 case AOUT_FMT_U16_LE:
227 case AOUT_FMT_U16_BE:
228 intf_WarnMsg( 2, "aout info: unsigned 16 bits mono thread" );
229 l_bytes = 1 * sizeof(u16) * p_aout->l_units;
230 pf_aout_thread = aout_U16MonoThread;
233 case AOUT_FMT_S16_LE:
234 case AOUT_FMT_S16_BE:
235 intf_WarnMsg( 2, "aout info: signed 16 bits mono thread" );
236 l_bytes = 1 * sizeof(s16) * p_aout->l_units;
237 pf_aout_thread = aout_S16MonoThread;
241 intf_ErrMsg( "aout error: unknown audio output format (%i)",
247 /* Audio output is stereo */
249 switch ( p_aout->i_format )
252 intf_WarnMsg( 2, "aout info: unsigned 8 bits stereo thread" );
253 l_bytes = 2 * sizeof(u8) * p_aout->l_units;
254 pf_aout_thread = aout_U8StereoThread;
258 intf_WarnMsg( 2, "aout info: signed 8 bits stereo thread" );
259 l_bytes = 2 * sizeof(s8) * p_aout->l_units;
260 pf_aout_thread = aout_S8StereoThread;
263 case AOUT_FMT_U16_LE:
264 case AOUT_FMT_U16_BE:
265 intf_WarnMsg( 2, "aout info: unsigned 16 bits stereo thread" );
266 l_bytes = 2 * sizeof(u16) * p_aout->l_units;
267 pf_aout_thread = aout_U16StereoThread;
270 case AOUT_FMT_S16_LE:
271 case AOUT_FMT_S16_BE:
272 intf_WarnMsg( 2, "aout info: signed 16 bits stereo thread" );
273 l_bytes = 2 * sizeof(s16) * p_aout->l_units;
274 pf_aout_thread = aout_S16StereoThread;
278 intf_WarnMsg( 2, "aout info: ac3 pass-through thread" );
279 l_bytes = SPDIF_FRAME_SIZE;
280 pf_aout_thread = aout_SpdifThread;
284 intf_ErrMsg( "aout error: unknown audio output format %i",
291 intf_ErrMsg( "aout error: unknown number of audio channels (%i)",
292 p_aout->i_channels );
296 /* Allocate the memory needed by the audio output buffers, and set to zero
297 * the s32 buffer's memory */
298 p_aout->buffer = malloc( l_bytes );
299 if ( p_aout->buffer == NULL )
301 intf_ErrMsg( "aout error: cannot create output buffer" );
305 p_aout->s32_buffer = (s32 *)calloc( p_aout->l_units,
306 sizeof(s32) << ( p_aout->b_stereo ) );
307 if ( p_aout->s32_buffer == NULL )
309 intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
310 free( p_aout->buffer );
314 /* Rough estimate of the playing date */
315 p_aout->date = mdate();
317 /* Launch the thread */
318 if ( vlc_thread_create( &p_aout->thread_id, "audio output",
319 (vlc_thread_func_t)pf_aout_thread, p_aout ) )
321 intf_ErrMsg( "aout error: cannot spawn audio output thread" );
322 free( p_aout->buffer );
323 free( p_aout->s32_buffer );
327 intf_WarnMsg( 2, "aout info: audio output thread %i spawned", getpid() );
331 /*****************************************************************************
333 *****************************************************************************/
334 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
338 /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
340 /* Ask thread to kill itself and wait until it's done */
342 vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
344 /* Free the allocated memory */
345 free( p_aout->buffer );
346 free( p_aout->s32_buffer );
348 /* Destroy the condition and mutex locks */
349 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
351 vlc_mutex_destroy( &p_aout->fifo[i_fifo].data_lock );
352 vlc_cond_destroy( &p_aout->fifo[i_fifo].data_wait );
354 vlc_mutex_destroy( &p_aout->fifos_lock );
356 /* Free the plugin */
357 p_aout->pf_close( p_aout );
359 /* Release the aout module */
360 module_Unneed( p_aout->p_module );