]> git.sesse.net Git - vlc/blob - src/audio_output/aout_ext-dec.c
29b19bb849f39c15dfe0b00516bae6d0502db702
[vlc] / src / audio_output / aout_ext-dec.c
1 /*****************************************************************************
2  * aout_ext-dec.c : exported fifo management functions
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: aout_ext-dec.c,v 1.5 2001/06/12 18:16:49 stef 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 "defs.h"
28
29 #include <stdio.h>                                           /* "intf_msg.h" */
30 #include <stdlib.h>                            /* calloc(), malloc(), free() */
31
32 #include "config.h"
33 #include "common.h"
34 #include "threads.h"
35 #include "mtime.h"                             /* mtime_t, mdate(), msleep() */
36
37 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
38
39 #include "audio_output.h"
40 #include "aout_common.h"
41
42 #include "main.h"
43
44 /*****************************************************************************
45  * aout_CreateFifo
46  *****************************************************************************/
47 aout_fifo_t * aout_CreateFifo( int i_type, int i_channels, long l_rate,
48                                long l_units, long l_frame_size,
49                                void *p_buffer )
50 {
51     aout_thread_t *p_aout;
52     int i_fifo;
53
54     /* Spawn an audio output if there is none */
55     vlc_mutex_lock( &p_aout_bank->lock );
56
57     if( p_aout_bank->i_count == 0 )
58     {
59         intf_WarnMsg( 1, "aout: no aout present, spawning one" );
60
61         p_aout = aout_CreateThread( NULL );
62
63         /* Everything failed */
64         if( p_aout == NULL )
65         {
66             vlc_mutex_unlock( &p_aout_bank->lock );
67             return NULL;
68         }
69
70         p_aout_bank->pp_aout[ p_aout_bank->i_count ] = p_aout;
71         p_aout_bank->i_count++;
72     }
73     /* temporary hack to switch output type (mainly for spdif)
74      * FIXME: to be adapted when several output are available */
75     else if( p_aout_bank->pp_aout[0]->fifo[0].i_type != i_type )
76     {
77         intf_WarnMsg( 1, "aout: changing aout type" );
78
79         aout_DestroyThread( p_aout_bank->pp_aout[0], NULL );
80
81         p_aout = aout_CreateThread( NULL );
82
83         /* Everything failed */
84         if( p_aout == NULL )
85         {
86             vlc_mutex_unlock( &p_aout_bank->lock );
87             return NULL;
88         }
89
90         p_aout_bank->pp_aout[0] = p_aout;
91
92     }
93     else
94     {
95         /* Take the first audio output FIXME: take the best one */
96         p_aout = p_aout_bank->pp_aout[ 0 ];
97     }
98
99     /* Take the fifos lock */
100     vlc_mutex_lock( &p_aout->fifos_lock );
101
102     /* Looking for a free fifo structure */
103     for( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
104     {
105         if( p_aout->fifo[i_fifo].i_type == AOUT_EMPTY_FIFO )
106         {
107             /* Not very clever, but at least we know which fifo it is */
108             p_aout->fifo[i_fifo].i_fifo = i_fifo;
109             break;
110         }
111     }
112
113     if( i_fifo == AOUT_MAX_FIFOS )
114     {
115         intf_ErrMsg( "aout error: no fifo available" );
116         vlc_mutex_unlock( &p_aout->fifos_lock );
117         vlc_mutex_unlock( &p_aout_bank->lock );
118         return( NULL );
119     }
120
121     /* Initialize the new fifo structure */
122     switch ( p_aout->fifo[i_fifo].i_type = i_type )
123     {
124         case AOUT_INTF_MONO_FIFO:
125         case AOUT_INTF_STEREO_FIFO:
126             p_aout->fifo[i_fifo].b_die = 0;
127
128             p_aout->fifo[i_fifo].i_channels = i_channels;
129             p_aout->fifo[i_fifo].b_stereo = ( i_channels == 2 );
130             p_aout->fifo[i_fifo].l_rate = l_rate;
131
132             p_aout->fifo[i_fifo].buffer = p_buffer;
133
134             p_aout->fifo[i_fifo].l_unit = 0;
135             InitializeIncrement( &p_aout->fifo[i_fifo].unit_increment,
136                                  l_rate, p_aout->l_rate );
137             p_aout->fifo[i_fifo].l_units = l_units;
138             break;
139
140         case AOUT_ADEC_MONO_FIFO:
141         case AOUT_ADEC_STEREO_FIFO:
142         case AOUT_ADEC_SPDIF_FIFO:
143             p_aout->fifo[i_fifo].b_die = 0;
144
145             p_aout->fifo[i_fifo].i_channels = i_channels;
146             p_aout->fifo[i_fifo].b_stereo = ( i_channels == 2 );
147             p_aout->fifo[i_fifo].l_rate = l_rate;
148
149             p_aout->fifo[i_fifo].l_frame_size = l_frame_size;
150             /* Allocate the memory needed to store the audio frames. As the
151              * fifo is a rotative fifo, we must be able to find out whether
152              * the fifo is full or empty, that's why we must in fact allocate
153              * memory for (AOUT_FIFO_SIZE+1) audio frames. */
154             p_aout->fifo[i_fifo].buffer = malloc( sizeof(s16) *
155                                    ( AOUT_FIFO_SIZE + 1 ) * l_frame_size );
156             if ( p_aout->fifo[i_fifo].buffer == NULL )
157             {
158                 intf_ErrMsg( "aout error: cannot create frame buffer" );
159                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
160                 vlc_mutex_unlock( &p_aout->fifos_lock );
161                 vlc_mutex_unlock( &p_aout_bank->lock );
162                 return( NULL );
163             }
164
165             /* Allocate the memory needed to store the dates of the frames */
166             p_aout->fifo[i_fifo].date =
167                            malloc( sizeof(mtime_t) * ( AOUT_FIFO_SIZE +  1) );
168
169             if ( p_aout->fifo[i_fifo].date == NULL )
170             {
171                 intf_ErrMsg( "aout error: cannot create date buffer");
172                 free( p_aout->fifo[i_fifo].buffer );
173                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
174                 vlc_mutex_unlock( &p_aout->fifos_lock );
175                 vlc_mutex_unlock( &p_aout_bank->lock );
176                 return( NULL );
177             }
178
179             /* Set the fifo's buffer as empty (the first frame that is to be
180              * played is also the first frame that is not to be played) */
181             p_aout->fifo[i_fifo].l_start_frame = 0;
182             /* p_aout->fifo[i_fifo].l_next_frame = 0; */
183             p_aout->fifo[i_fifo].l_end_frame = 0;
184
185             /* Waiting for the audio decoder to compute enough frames to work
186              * out the fifo's current rate (as soon as the decoder has decoded
187              * enough frames, the members of the fifo structure that are not
188              * initialized now will be calculated) */
189             p_aout->fifo[i_fifo].b_start_frame = 0;
190             p_aout->fifo[i_fifo].b_next_frame = 0;
191             break;
192
193         default:
194             intf_ErrMsg( "aout error: unknown fifo type 0x%x",
195                          p_aout->fifo[i_fifo].i_type );
196             p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
197             vlc_mutex_unlock( &p_aout->fifos_lock );
198             vlc_mutex_unlock( &p_aout_bank->lock );
199             return( NULL );
200     }
201
202     /* Release the fifos lock */
203     vlc_mutex_unlock( &p_aout->fifos_lock );
204     vlc_mutex_unlock( &p_aout_bank->lock );
205
206     intf_WarnMsg( 2, "aout info: fifo #%i allocated, %i channels, rate %li",
207                   p_aout->fifo[i_fifo].i_fifo, p_aout->fifo[i_fifo].i_channels,
208                   p_aout->fifo[i_fifo].l_rate );
209
210     /* Return the pointer to the fifo structure */
211     return( &p_aout->fifo[i_fifo] );
212 }
213
214 /*****************************************************************************
215  * aout_DestroyFifo
216  *****************************************************************************/
217 void aout_DestroyFifo( aout_fifo_t * p_fifo )
218 {
219     intf_WarnMsg( 2, "aout info: fifo #%i destroyed", p_fifo->i_fifo );
220
221     p_fifo->b_die = 1;
222 }
223
224 /*****************************************************************************
225  * aout_FreeFifo
226  *****************************************************************************/
227 void aout_FreeFifo( aout_fifo_t * p_fifo )
228 {
229     switch ( p_fifo->i_type )
230     {
231         case AOUT_EMPTY_FIFO:
232
233             break;
234
235         case AOUT_INTF_MONO_FIFO:
236         case AOUT_INTF_STEREO_FIFO:
237
238             free( p_fifo->buffer );
239             p_fifo->i_type = AOUT_EMPTY_FIFO;
240
241             break;
242
243         case AOUT_ADEC_MONO_FIFO:
244         case AOUT_ADEC_STEREO_FIFO:
245         case AOUT_ADEC_SPDIF_FIFO:
246
247             free( p_fifo->buffer );
248             free( p_fifo->date );
249             p_fifo->i_type = AOUT_EMPTY_FIFO;
250
251             break;
252
253         default:
254
255             break;
256     }
257 }
258