]> git.sesse.net Git - vlc/blob - src/audio_output/audio_output.c
Some heavy changes today:
[vlc] / src / audio_output / audio_output.c
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 $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdio.h>                                           /* "intf_msg.h" */
28 #include <stdlib.h>                            /* calloc(), malloc(), free() */
29 #include <string.h>
30
31 #include <videolan/vlc.h>
32
33 #ifdef HAVE_UNISTD_H
34 #   include <unistd.h>                                           /* getpid() */
35 #endif
36
37 #ifdef WIN32                   /* getpid() for win32 is located in process.h */
38 #   include <process.h>
39 #endif
40
41 #include "audio_output.h"
42 #include "aout_common.h"
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static int aout_SpawnThread( aout_thread_t * p_aout );
48
49 /*****************************************************************************
50  * aout_InitBank: initialize the audio output bank.
51  *****************************************************************************/
52 void aout_InitBank ( void )
53 {
54     p_aout_bank->i_count = 0;
55
56     vlc_mutex_init( &p_aout_bank->lock );
57 }
58
59 /*****************************************************************************
60  * aout_EndBank: empty the audio output bank.
61  *****************************************************************************
62  * This function ends all unused audio outputs and empties the bank in
63  * case of success.
64  *****************************************************************************/
65 void aout_EndBank ( void )
66 {
67     /* Ask all remaining audio outputs to die */
68     while( p_aout_bank->i_count )
69     {
70         aout_DestroyThread(
71                 p_aout_bank->pp_aout[ --p_aout_bank->i_count ], NULL );
72     }
73
74     vlc_mutex_destroy( &p_aout_bank->lock );
75 }
76
77 /*****************************************************************************
78  * aout_CreateThread: initialize audio thread
79  *****************************************************************************/
80 aout_thread_t *aout_CreateThread( int *pi_status )
81 {
82     aout_thread_t * p_aout;                             /* thread descriptor */
83 #if 0
84     int             i_status;                               /* thread status */
85 #endif
86
87     /* Allocate descriptor */
88     p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
89     if( p_aout == NULL )
90     {
91         return( NULL );
92     }
93
94     /* Choose the best module */
95     p_aout->p_module = module_Need( MODULE_CAPABILITY_AOUT,
96                            main_GetPszVariable( AOUT_METHOD_VAR, NULL ), 
97                            NULL );
98
99     if( p_aout->p_module == NULL )
100     {
101         intf_ErrMsg( "aout error: no suitable aout module" );
102         free( p_aout );
103         return( NULL );
104     }
105
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
113
114     /*
115      * Initialize audio device
116      */
117     if ( p_aout->pf_open( p_aout ) )
118     {
119         module_Unneed( p_aout->p_module );
120         free( p_aout );
121         return( NULL );
122     }
123
124     if( p_aout->l_rate == 0 )
125     {
126         intf_ErrMsg( "aout error: null sample rate" );
127         p_aout->pf_close( p_aout );
128         module_Unneed( p_aout->p_module );
129         free( p_aout );
130         return( NULL );
131     }
132
133     /* special setting for ac3 pass-through mode */
134     if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) && p_main->b_ac3 )
135     {
136         intf_WarnMsg( 4, "aout info: setting ac3 spdif" );
137         p_aout->i_format = AOUT_FMT_AC3;
138         p_aout->l_rate = 48000;
139     }
140
141     /* FIXME: only works for i_channels == 1 or 2 ?? */
142     p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0;
143
144     if ( p_aout->pf_setformat( p_aout ) )
145     {
146         p_aout->pf_close( p_aout );
147         module_Unneed( p_aout->p_module );
148         free( p_aout );
149         return( NULL );
150     }
151
152     /* Initialize the volume level */
153     p_aout->i_volume = main_GetIntVariable( AOUT_VOLUME_VAR, VOLUME_DEFAULT );
154     p_aout->i_savedvolume = 0;
155     
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 ) )
160     {
161         p_aout->pf_close( p_aout );
162         module_Unneed( p_aout->p_module );
163         free( p_aout );
164         return( NULL );
165     }
166
167     return( p_aout );
168 }
169
170 /*****************************************************************************
171  * aout_SpawnThread
172  *****************************************************************************/
173 static int aout_SpawnThread( aout_thread_t * p_aout )
174 {
175     int     i_fifo;
176     long    l_bytes;
177     void (* pf_aout_thread)( aout_thread_t * ) = NULL;
178
179     /* We want the audio output thread to live */
180     p_aout->b_die = 0;
181     p_aout->b_active = 1;
182
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++ )
187     {
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 );
191     }
192
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 );
198
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 )
202     {
203     /* Audio output is mono */
204     case 1:
205         switch ( p_aout->i_format )
206         {
207         case AOUT_FMT_U8:
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;
211             break;
212
213         case AOUT_FMT_S8:
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;
217             break;
218
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;
224             break;
225
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;
231             break;
232
233         default:
234             intf_ErrMsg( "aout error: unknown audio output format (%i)",
235                          p_aout->i_format );
236             return( -1 );
237         }
238         break;
239
240     /* Audio output is stereo */
241     case 2:
242         switch ( p_aout->i_format )
243         {
244         case AOUT_FMT_U8:
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;
248             break;
249
250         case AOUT_FMT_S8:
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;
254             break;
255
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;
261             break;
262
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;
268             break;
269
270         case AOUT_FMT_AC3:
271             intf_WarnMsg( 2, "aout info: ac3 pass-through thread" );
272             l_bytes = SPDIF_FRAME_SIZE;
273             pf_aout_thread = aout_SpdifThread;
274             break;
275
276         default:
277             intf_ErrMsg( "aout error: unknown audio output format %i",
278                          p_aout->i_format );
279             return( -1 );
280         }
281         break;
282
283     default:
284         intf_ErrMsg( "aout error: unknown number of audio channels (%i)",
285                      p_aout->i_channels );
286         return( -1 );
287     }
288
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 )
293     {
294         intf_ErrMsg( "aout error: cannot create output buffer" );
295         return( -1 );
296     }
297
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 )
301     {
302         intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
303         free( p_aout->buffer );
304         return( -1 );
305     }
306
307     /* Rough estimate of the playing date */
308     p_aout->date = mdate() + p_main->i_desync;
309
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 ) )
313     {
314         intf_ErrMsg( "aout error: cannot spawn audio output thread" );
315         free( p_aout->buffer );
316         free( p_aout->s32_buffer );
317         return( -1 );
318     }
319
320     intf_WarnMsg( 2, "aout info: audio output thread %i spawned", getpid() );
321     return( 0 );
322 }
323
324 /*****************************************************************************
325  * aout_DestroyThread
326  *****************************************************************************/
327 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
328 {
329     int i_fifo;
330     
331     /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
332
333     /* Ask thread to kill itself and wait until it's done */
334     p_aout->b_die = 1;
335     vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
336
337     /* Free the allocated memory */
338     free( p_aout->buffer );
339     free( p_aout->s32_buffer );
340
341     /* Destroy the condition and mutex locks */
342     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
343     {
344         vlc_mutex_destroy( &p_aout->fifo[i_fifo].data_lock );
345         vlc_cond_destroy( &p_aout->fifo[i_fifo].data_wait );
346     }
347     vlc_mutex_destroy( &p_aout->fifos_lock );
348     
349     /* Free the plugin */
350     p_aout->pf_close( p_aout );
351
352     /* Release the aout module */
353     module_Unneed( p_aout->p_module );
354
355     /* Free structure */
356     free( p_aout );
357 }
358