1 /*****************************************************************************
2 * audio_output.c : audio output thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: audio_output.c,v 1.86 2002/06/08 14:08:46 sam 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 <stdlib.h> /* calloc(), malloc(), free() */
34 # include <unistd.h> /* getpid() */
37 #ifdef WIN32 /* getpid() for win32 is located in process.h */
41 #include "audio_output.h"
44 #include "aout_spdif.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int aout_SpawnThread ( aout_thread_t * p_aout );
51 /*****************************************************************************
52 * aout_CreateThread: initialize audio thread
53 *****************************************************************************/
54 aout_thread_t *aout_CreateThread( vlc_object_t *p_parent,
55 int i_channels, int i_rate )
57 aout_thread_t * p_aout; /* thread descriptor */
61 /* Allocate descriptor */
62 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
68 p_aout->i_latency = 0;
69 p_aout->i_rate = config_GetInt( p_aout, "rate" );
70 p_aout->i_channels = config_GetInt( p_aout, "mono" ) ? 1 : 2;
72 i_format = config_GetInt( p_aout, "audio-format" );
73 if( ( !i_format ) || ( i_format > 8 ) )
75 p_aout->i_format = AOUT_FMT_S16_NE;
79 p_aout->i_format = 1 << ( i_format + 2 );
82 if( p_aout->i_rate == 0 )
84 msg_Err( p_aout, "null sample rate" );
85 vlc_object_destroy( p_aout );
89 /* Choose the best module */
90 psz_name = config_GetPsz( p_aout, "aout" );
91 p_aout->p_module = module_Need( p_aout, MODULE_CAPABILITY_AOUT,
92 psz_name, (void *)p_aout );
93 if( psz_name ) free( psz_name );
94 if( p_aout->p_module == NULL )
96 msg_Err( p_aout, "no suitable aout module" );
97 vlc_object_destroy( p_aout );
101 #define aout_functions p_aout->p_module->p_functions->aout.functions.aout
102 p_aout->pf_open = aout_functions.pf_open;
103 p_aout->pf_setformat = aout_functions.pf_setformat;
104 p_aout->pf_getbufinfo = aout_functions.pf_getbufinfo;
105 p_aout->pf_play = aout_functions.pf_play;
106 p_aout->pf_close = aout_functions.pf_close;
107 #undef aout_functions
110 * Initialize audio device
112 if ( p_aout->pf_setformat( p_aout ) )
114 p_aout->pf_close( p_aout );
115 module_Unneed( p_aout->p_module );
116 vlc_object_destroy( p_aout );
120 /* Initialize the volume level */
121 p_aout->i_volume = config_GetInt( p_aout, "volume" );
122 p_aout->i_savedvolume = 0;
124 /* FIXME: maybe it would be cleaner to change SpawnThread prototype
125 * see vout to handle status correctly ?? however, it is not critical since
126 * this thread is only called in main and all calls are blocking */
127 if( aout_SpawnThread( p_aout ) )
129 p_aout->pf_close( p_aout );
130 module_Unneed( p_aout->p_module );
131 vlc_object_destroy( p_aout );
135 vlc_object_attach( p_aout, p_parent->p_vlc );
140 /*****************************************************************************
142 *****************************************************************************/
143 static int aout_SpawnThread( aout_thread_t * p_aout )
145 int i_index, i_bytes;
146 void (* pf_aout_thread)( aout_thread_t * ) = NULL;
149 /* Initialize the fifos lock */
150 vlc_mutex_init( p_aout, &p_aout->fifos_lock );
152 /* Initialize audio fifos : set all fifos as empty and initialize locks */
153 for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
155 p_aout->fifo[i_index].i_format = AOUT_FIFO_NONE;
156 vlc_mutex_init( p_aout, &p_aout->fifo[i_index].data_lock );
157 vlc_cond_init( p_aout, &p_aout->fifo[i_index].data_wait );
160 /* Compute the size (in audio units) of the audio output buffer. Although
161 * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
162 * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
163 p_aout->i_units = (s64)p_aout->i_rate * AOUT_BUFFER_DURATION / 1000000;
165 /* Make pf_aout_thread point to the right thread function, and compute the
166 * byte size of the audio output buffer */
167 switch ( p_aout->i_format )
170 pf_aout_thread = aout_PCMThread;
171 psz_format = "unsigned 8 bits";
172 i_bytes = p_aout->i_units * p_aout->i_channels;
176 pf_aout_thread = aout_PCMThread;
177 psz_format = "signed 8 bits";
178 i_bytes = p_aout->i_units * p_aout->i_channels;
181 case AOUT_FMT_U16_LE:
182 case AOUT_FMT_U16_BE:
183 pf_aout_thread = aout_PCMThread;
184 psz_format = "unsigned 16 bits";
185 i_bytes = 2 * p_aout->i_units * p_aout->i_channels;
188 case AOUT_FMT_S16_LE:
189 case AOUT_FMT_S16_BE:
190 pf_aout_thread = aout_PCMThread;
191 psz_format = "signed 16 bits";
192 i_bytes = 2 * p_aout->i_units * p_aout->i_channels;
196 pf_aout_thread = aout_SpdifThread;
197 psz_format = "ac3 pass-through";
198 i_bytes = SPDIF_FRAME_SIZE;
202 msg_Err( p_aout, "unknown audio output format %i",
207 /* Allocate the memory needed by the audio output buffers, and set to zero
208 * the s32 buffer's memory */
209 p_aout->buffer = malloc( i_bytes );
210 if ( p_aout->buffer == NULL )
212 msg_Err( p_aout, "out of memory" );
216 p_aout->s32_buffer = (s32 *)calloc( p_aout->i_units,
217 sizeof(s32) * p_aout->i_channels );
218 if ( p_aout->s32_buffer == NULL )
220 msg_Err( p_aout, "out of memory" );
221 free( p_aout->buffer );
225 /* Rough estimate of the playing date */
226 p_aout->date = mdate() + p_aout->p_vlc->i_desync;
228 /* Launch the thread */
229 if ( vlc_thread_create( p_aout, "audio output", pf_aout_thread, 0 ) )
231 msg_Err( p_aout, "cannot spawn audio output thread" );
232 free( p_aout->buffer );
233 free( p_aout->s32_buffer );
237 msg_Dbg( p_aout, "%s thread spawned, %i channels, rate %i",
238 psz_format, p_aout->i_channels, p_aout->i_rate );
242 /*****************************************************************************
244 *****************************************************************************/
245 void aout_DestroyThread( aout_thread_t * p_aout )
249 /* Ask thread to kill itself and wait until it's done */
252 vlc_thread_join( p_aout );
254 /* Free the allocated memory */
255 free( p_aout->buffer );
256 free( p_aout->s32_buffer );
258 /* Destroy the condition and mutex locks */
259 for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
261 vlc_mutex_destroy( &p_aout->fifo[i_index].data_lock );
262 vlc_cond_destroy( &p_aout->fifo[i_index].data_wait );
264 vlc_mutex_destroy( &p_aout->fifos_lock );
266 /* Free the plugin */
267 p_aout->pf_close( p_aout );
269 /* Release the aout module */
270 module_Unneed( p_aout->p_module );
273 vlc_object_destroy( p_aout );