]> git.sesse.net Git - vlc/blob - src/audio_output/audio_output.c
* ./include/vlc_threads.h, ./src/misc/threads.c: improved the cond_wait
[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.86 2002/06/08 14:08:46 sam Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Cyril Deguet <asmax@via.ecp.fr>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                            /* calloc(), malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/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
43 #include "aout_pcm.h"
44 #include "aout_spdif.h"
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int  aout_SpawnThread ( aout_thread_t * p_aout );
50
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 )
56 {
57     aout_thread_t * p_aout;                             /* thread descriptor */
58     char *          psz_name;
59     int             i_format;
60
61     /* Allocate descriptor */
62     p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
63     if( p_aout == NULL )
64     {
65         return NULL;
66     }
67
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;
71
72     i_format = config_GetInt( p_aout, "audio-format" );
73     if( ( !i_format ) || ( i_format > 8 ) )
74     {
75         p_aout->i_format = AOUT_FMT_S16_NE;
76     }
77     else
78     {
79         p_aout->i_format = 1 << ( i_format + 2 );
80     }
81
82     if( p_aout->i_rate == 0 )
83     {
84         msg_Err( p_aout, "null sample rate" );
85         vlc_object_destroy( p_aout );
86         return NULL;
87     }
88
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 )
95     {
96         msg_Err( p_aout, "no suitable aout module" );
97         vlc_object_destroy( p_aout );
98         return NULL;
99     }
100
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
108
109     /*
110      * Initialize audio device
111      */
112     if ( p_aout->pf_setformat( p_aout ) )
113     {
114         p_aout->pf_close( p_aout );
115         module_Unneed( p_aout->p_module );
116         vlc_object_destroy( p_aout );
117         return NULL;
118     }
119
120     /* Initialize the volume level */
121     p_aout->i_volume = config_GetInt( p_aout, "volume" );
122     p_aout->i_savedvolume = 0;
123     
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 ) )
128     {
129         p_aout->pf_close( p_aout );
130         module_Unneed( p_aout->p_module );
131         vlc_object_destroy( p_aout );
132         return NULL;
133     }
134
135     vlc_object_attach( p_aout, p_parent->p_vlc );
136
137     return p_aout;
138 }
139
140 /*****************************************************************************
141  * aout_SpawnThread
142  *****************************************************************************/
143 static int aout_SpawnThread( aout_thread_t * p_aout )
144 {
145     int     i_index, i_bytes;
146     void (* pf_aout_thread)( aout_thread_t * ) = NULL;
147     char   *psz_format;
148
149     /* Initialize the fifos lock */
150     vlc_mutex_init( p_aout, &p_aout->fifos_lock );
151
152     /* Initialize audio fifos : set all fifos as empty and initialize locks */
153     for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
154     {
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 );
158     }
159
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;
164
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 )
168     {
169         case AOUT_FMT_U8:
170             pf_aout_thread = aout_PCMThread;
171             psz_format = "unsigned 8 bits";
172             i_bytes = p_aout->i_units * p_aout->i_channels;
173             break;
174
175         case AOUT_FMT_S8:
176             pf_aout_thread = aout_PCMThread;
177             psz_format = "signed 8 bits";
178             i_bytes = p_aout->i_units * p_aout->i_channels;
179             break;
180
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;
186             break;
187
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;
193             break;
194
195         case AOUT_FMT_AC3:
196             pf_aout_thread = aout_SpdifThread;
197             psz_format = "ac3 pass-through";
198             i_bytes = SPDIF_FRAME_SIZE;
199             break;
200
201         default:
202             msg_Err( p_aout, "unknown audio output format %i",
203                              p_aout->i_format );
204             return( -1 );
205     }
206
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 )
211     {
212         msg_Err( p_aout, "out of memory" );
213         return( -1 );
214     }
215
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 )
219     {
220         msg_Err( p_aout, "out of memory" );
221         free( p_aout->buffer );
222         return( -1 );
223     }
224
225     /* Rough estimate of the playing date */
226     p_aout->date = mdate() + p_aout->p_vlc->i_desync;
227
228     /* Launch the thread */
229     if ( vlc_thread_create( p_aout, "audio output", pf_aout_thread, 0 ) )
230     {
231         msg_Err( p_aout, "cannot spawn audio output thread" );
232         free( p_aout->buffer );
233         free( p_aout->s32_buffer );
234         return( -1 );
235     }
236
237     msg_Dbg( p_aout, "%s thread spawned, %i channels, rate %i",
238                      psz_format, p_aout->i_channels, p_aout->i_rate );
239     return( 0 );
240 }
241
242 /*****************************************************************************
243  * aout_DestroyThread
244  *****************************************************************************/
245 void aout_DestroyThread( aout_thread_t * p_aout )
246 {
247     int i_index;
248     
249     /* Ask thread to kill itself and wait until it's done */
250     p_aout->b_die = 1;
251
252     vlc_thread_join( p_aout );
253
254     /* Free the allocated memory */
255     free( p_aout->buffer );
256     free( p_aout->s32_buffer );
257
258     /* Destroy the condition and mutex locks */
259     for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
260     {
261         vlc_mutex_destroy( &p_aout->fifo[i_index].data_lock );
262         vlc_cond_destroy( &p_aout->fifo[i_index].data_wait );
263     }
264     vlc_mutex_destroy( &p_aout->fifos_lock );
265     
266     /* Free the plugin */
267     p_aout->pf_close( p_aout );
268
269     /* Release the aout module */
270     module_Unneed( p_aout->p_module );
271
272     /* Free structure */
273     vlc_object_destroy( p_aout );
274 }
275