1 /*****************************************************************************
2 * audio_output.c : audio output thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: audio_output.c,v 1.83 2002/03/12 18:37:46 stef Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Cyril Deguet <asmax@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdio.h> /* "intf_msg.h" */
29 #include <stdlib.h> /* calloc(), malloc(), free() */
32 #include <videolan/vlc.h>
35 # include <unistd.h> /* getpid() */
38 #ifdef WIN32 /* getpid() for win32 is located in process.h */
42 #include "audio_output.h"
45 #include "aout_spdif.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 static int aout_SpawnThread ( aout_thread_t * p_aout );
52 /*****************************************************************************
53 * aout_InitBank: initialize the audio output bank.
54 *****************************************************************************/
55 void aout_InitBank ( void )
57 p_aout_bank->i_count = 0;
59 vlc_mutex_init( &p_aout_bank->lock );
62 /*****************************************************************************
63 * aout_EndBank: empty the audio output bank.
64 *****************************************************************************
65 * This function ends all unused audio outputs and empties the bank in
67 *****************************************************************************/
68 void aout_EndBank ( void )
70 /* Ask all remaining audio outputs to die */
71 while( p_aout_bank->i_count )
74 p_aout_bank->pp_aout[ --p_aout_bank->i_count ], NULL );
77 vlc_mutex_destroy( &p_aout_bank->lock );
80 /*****************************************************************************
81 * aout_CreateThread: initialize audio thread
82 *****************************************************************************/
83 aout_thread_t *aout_CreateThread( int *pi_status, int i_channels, int i_rate )
85 aout_thread_t * p_aout; /* thread descriptor */
87 int i_status; /* thread status */
92 /* Allocate descriptor */
93 p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
99 p_aout->i_latency = 0;
100 p_aout->i_rate = config_GetIntVariable( "rate" );
101 p_aout->i_channels = config_GetIntVariable( "mono" ) ? 1 : 2;
103 i_format = config_GetIntVariable( "aout_format" );
104 if( ( !i_format ) || ( i_format > 8 ) )
106 p_aout->i_format = AOUT_FMT_S16_NE;
110 p_aout->i_format = 1 << ( i_format + 2 );
113 if( p_aout->i_rate == 0 )
115 intf_ErrMsg( "aout error: null sample rate" );
120 /* Choose the best module */
121 psz_name = config_GetPszVariable( "aout" );
122 p_aout->p_module = module_Need( MODULE_CAPABILITY_AOUT, psz_name,
124 if( psz_name ) free( psz_name );
125 if( p_aout->p_module == NULL )
127 intf_ErrMsg( "aout error: no suitable aout module" );
132 #define aout_functions p_aout->p_module->p_functions->aout.functions.aout
133 p_aout->pf_open = aout_functions.pf_open;
134 p_aout->pf_setformat = aout_functions.pf_setformat;
135 p_aout->pf_getbufinfo = aout_functions.pf_getbufinfo;
136 p_aout->pf_play = aout_functions.pf_play;
137 p_aout->pf_close = aout_functions.pf_close;
138 #undef aout_functions
141 * Initialize audio device
143 if ( p_aout->pf_setformat( p_aout ) )
145 p_aout->pf_close( p_aout );
146 module_Unneed( p_aout->p_module );
151 /* Initialize the volume level */
152 p_aout->i_volume = config_GetIntVariable( "volume" );
153 p_aout->i_savedvolume = 0;
155 /* FIXME: maybe it would be cleaner to change SpawnThread prototype
156 * see vout to handle status correctly ?? however, it is not critical since
157 * this thread is only called in main and all calls are blocking */
158 if( aout_SpawnThread( p_aout ) )
160 p_aout->pf_close( p_aout );
161 module_Unneed( p_aout->p_module );
169 /*****************************************************************************
171 *****************************************************************************/
172 static int aout_SpawnThread( aout_thread_t * p_aout )
174 int i_index, i_bytes;
175 void (* pf_aout_thread)( aout_thread_t * ) = NULL;
178 /* We want the audio output thread to live */
180 p_aout->b_active = 1;
182 /* Initialize the fifos lock */
183 vlc_mutex_init( &p_aout->fifos_lock );
185 /* Initialize audio fifos : set all fifos as empty and initialize locks */
186 for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
188 p_aout->fifo[i_index].i_format = AOUT_FIFO_NONE;
189 vlc_mutex_init( &p_aout->fifo[i_index].data_lock );
190 vlc_cond_init( &p_aout->fifo[i_index].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->i_units = (s64)p_aout->i_rate * AOUT_BUFFER_DURATION / 1000000;
198 /* Make pf_aout_thread point to the right thread function, and compute the
199 * byte size of the audio output buffer */
200 switch ( p_aout->i_format )
203 pf_aout_thread = aout_PCMThread;
204 psz_format = "unsigned 8 bits";
205 i_bytes = p_aout->i_units * p_aout->i_channels;
209 pf_aout_thread = aout_PCMThread;
210 psz_format = "signed 8 bits";
211 i_bytes = p_aout->i_units * p_aout->i_channels;
214 case AOUT_FMT_U16_LE:
215 case AOUT_FMT_U16_BE:
216 pf_aout_thread = aout_PCMThread;
217 psz_format = "unsigned 16 bits";
218 i_bytes = 2 * p_aout->i_units * p_aout->i_channels;
221 case AOUT_FMT_S16_LE:
222 case AOUT_FMT_S16_BE:
223 pf_aout_thread = aout_PCMThread;
224 psz_format = "signed 16 bits";
225 i_bytes = 2 * p_aout->i_units * p_aout->i_channels;
229 pf_aout_thread = aout_SpdifThread;
230 psz_format = "ac3 pass-through";
231 i_bytes = SPDIF_FRAME_SIZE;
235 intf_ErrMsg( "aout error: unknown audio output format %i",
240 /* Allocate the memory needed by the audio output buffers, and set to zero
241 * the s32 buffer's memory */
242 p_aout->buffer = malloc( i_bytes );
243 if ( p_aout->buffer == NULL )
245 intf_ErrMsg( "aout error: cannot create output buffer" );
249 p_aout->s32_buffer = (s32 *)calloc( p_aout->i_units,
250 sizeof(s32) * p_aout->i_channels );
251 if ( p_aout->s32_buffer == NULL )
253 intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
254 free( p_aout->buffer );
258 /* Rough estimate of the playing date */
259 p_aout->date = mdate() + p_main->i_desync;
261 /* Launch the thread */
262 if ( vlc_thread_create( &p_aout->thread_id, "audio output",
263 (vlc_thread_func_t)pf_aout_thread, p_aout ) )
265 intf_ErrMsg( "aout error: cannot spawn audio output thread" );
266 free( p_aout->buffer );
267 free( p_aout->s32_buffer );
271 intf_WarnMsg( 2, "aout info: %s thread spawned, %i channels, rate %i",
272 psz_format, p_aout->i_channels, p_aout->i_rate );
276 /*****************************************************************************
278 *****************************************************************************/
279 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
283 /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
285 /* Ask thread to kill itself and wait until it's done */
287 vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
289 /* Free the allocated memory */
290 free( p_aout->buffer );
291 free( p_aout->s32_buffer );
293 /* Destroy the condition and mutex locks */
294 for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
296 vlc_mutex_destroy( &p_aout->fifo[i_index].data_lock );
297 vlc_cond_destroy( &p_aout->fifo[i_index].data_wait );
299 vlc_mutex_destroy( &p_aout->fifos_lock );
301 /* Free the plugin */
302 p_aout->pf_close( p_aout );
304 /* Release the aout module */
305 module_Unneed( p_aout->p_module );