]> git.sesse.net Git - vlc/blob - src/audio_output/aout_fifo.c
712a8461fc663f98244812743f10b811635f1a7d
[vlc] / src / audio_output / aout_fifo.c
1 /*****************************************************************************
2  * aout_fifo.c : exported fifo management functions
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: aout_fifo.c,v 1.2 2001/03/21 13:42:34 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 "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 /*****************************************************************************
43  * aout_CreateFifo
44  *****************************************************************************/
45 aout_fifo_t * aout_CreateFifo( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
46 {
47     int i_fifo;
48
49     /* Take the fifos lock */
50     vlc_mutex_lock( &p_aout->fifos_lock );
51
52     /* Looking for a free fifo structure */
53     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
54     {
55         if ( p_aout->fifo[i_fifo].i_type == AOUT_EMPTY_FIFO)
56         {
57             /* Not very clever, but at least we know which fifo it is */
58             p_aout->fifo[i_fifo].i_fifo = i_fifo;
59             break;
60         }
61     }
62
63     if ( i_fifo == AOUT_MAX_FIFOS )
64     {
65         intf_ErrMsg( "aout error: no fifo available" );
66         vlc_mutex_unlock( &p_aout->fifos_lock );
67         return( NULL );
68     }
69
70     /* Initialize the new fifo structure */
71     switch ( p_aout->fifo[i_fifo].i_type = p_fifo->i_type )
72     {
73         case AOUT_INTF_MONO_FIFO:
74         case AOUT_INTF_STEREO_FIFO:
75             p_aout->fifo[i_fifo].b_die = 0;
76
77             p_aout->fifo[i_fifo].i_channels = p_fifo->i_channels;
78             p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
79             p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
80
81             p_aout->fifo[i_fifo].buffer = p_fifo->buffer;
82
83             p_aout->fifo[i_fifo].l_unit = 0;
84             InitializeIncrement( &p_aout->fifo[i_fifo].unit_increment,
85                                  p_fifo->l_rate, p_aout->l_rate );
86             p_aout->fifo[i_fifo].l_units = p_fifo->l_units;
87             break;
88
89         case AOUT_ADEC_MONO_FIFO:
90         case AOUT_ADEC_STEREO_FIFO:
91             p_aout->fifo[i_fifo].b_die = 0;
92
93             p_aout->fifo[i_fifo].i_channels = p_fifo->i_channels;
94             p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
95             p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
96
97             p_aout->fifo[i_fifo].l_frame_size = p_fifo->l_frame_size;
98             /* Allocate the memory needed to store the audio frames. As the
99              * fifo is a rotative fifo, we must be able to find out whether the
100              * fifo is full or empty, that's why we must in fact allocate memory
101              * for (AOUT_FIFO_SIZE+1) audio frames. */
102             p_aout->fifo[i_fifo].buffer = malloc( sizeof(s16)*(AOUT_FIFO_SIZE+1)*p_fifo->l_frame_size );
103             if ( p_aout->fifo[i_fifo].buffer == NULL )
104             {
105                 intf_ErrMsg( "aout error: cannot create frame buffer" );
106                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
107                 vlc_mutex_unlock( &p_aout->fifos_lock );
108                 return( NULL );
109             }
110
111             /* Allocate the memory needed to store the dates of the frames */
112             p_aout->fifo[i_fifo].date = (mtime_t *)malloc( sizeof(mtime_t)*(AOUT_FIFO_SIZE+1) );
113             if ( p_aout->fifo[i_fifo].date == NULL )
114             {
115                 intf_ErrMsg( "aout error: cannot create date buffer");
116                 free( p_aout->fifo[i_fifo].buffer );
117                 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
118                 vlc_mutex_unlock( &p_aout->fifos_lock );
119                 return( NULL );
120             }
121
122             /* Set the fifo's buffer as empty (the first frame that is to be
123              * played is also the first frame that is not to be played) */
124             p_aout->fifo[i_fifo].l_start_frame = 0;
125             /* p_aout->fifo[i_fifo].l_next_frame = 0; */
126             p_aout->fifo[i_fifo].l_end_frame = 0;
127
128             /* Waiting for the audio decoder to compute enough frames to work
129              * out the fifo's current rate (as soon as the decoder has decoded
130              * enough frames, the members of the fifo structure that are not
131              * initialized now will be calculated) */
132             p_aout->fifo[i_fifo].b_start_frame = 0;
133             p_aout->fifo[i_fifo].b_next_frame = 0;
134             break;
135
136         default:
137             intf_ErrMsg( "aout error: unknown fifo type 0x%x", p_aout->fifo[i_fifo].i_type );
138             p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
139             vlc_mutex_unlock( &p_aout->fifos_lock );
140             return( NULL );
141     }
142
143     /* Release the fifos lock */
144     vlc_mutex_unlock( &p_aout->fifos_lock );
145
146     intf_WarnMsg( 2, "aout info: fifo #%i allocated, %i channels, rate %li",
147                   p_aout->fifo[i_fifo].i_fifo, p_aout->fifo[i_fifo].i_channels, p_aout->fifo[i_fifo].l_rate );
148
149     /* Return the pointer to the fifo structure */
150     return( &FIFO );
151 }
152
153 /*****************************************************************************
154  * aout_DestroyFifo
155  *****************************************************************************/
156 void aout_DestroyFifo( aout_fifo_t * p_fifo )
157 {
158     intf_WarnMsg( 2, "aout info: fifo #%i destroyed", p_fifo->i_fifo );
159
160     p_fifo->b_die = 1;
161 }
162
163 /*****************************************************************************
164  * aout_FreeFifo
165  *****************************************************************************/
166 void aout_FreeFifo( aout_fifo_t * p_fifo )
167 {
168     switch ( p_fifo->i_type )
169     {
170         case AOUT_EMPTY_FIFO:
171
172             break;
173
174         case AOUT_INTF_MONO_FIFO:
175         case AOUT_INTF_STEREO_FIFO:
176
177             free( p_fifo->buffer );
178             p_fifo->i_type = AOUT_EMPTY_FIFO;
179
180             break;
181
182         case AOUT_ADEC_MONO_FIFO:
183         case AOUT_ADEC_STEREO_FIFO:
184
185             free( p_fifo->buffer );
186             free( p_fifo->date );
187             p_fifo->i_type = AOUT_EMPTY_FIFO;
188
189             break;
190
191         default:
192
193             break;
194     }
195 }
196