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 $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <stdio.h> /* "intf_msg.h" */
30 #include <stdlib.h> /* calloc(), malloc(), free() */
35 #include "mtime.h" /* mtime_t, mdate(), msleep() */
37 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
39 #include "audio_output.h"
40 #include "aout_common.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 aout_fifo_t * aout_CreateFifo( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
49 /* Take the fifos lock */
50 vlc_mutex_lock( &p_aout->fifos_lock );
52 /* Looking for a free fifo structure */
53 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
55 if ( p_aout->fifo[i_fifo].i_type == AOUT_EMPTY_FIFO)
57 /* Not very clever, but at least we know which fifo it is */
58 p_aout->fifo[i_fifo].i_fifo = i_fifo;
63 if ( i_fifo == AOUT_MAX_FIFOS )
65 intf_ErrMsg( "aout error: no fifo available" );
66 vlc_mutex_unlock( &p_aout->fifos_lock );
70 /* Initialize the new fifo structure */
71 switch ( p_aout->fifo[i_fifo].i_type = p_fifo->i_type )
73 case AOUT_INTF_MONO_FIFO:
74 case AOUT_INTF_STEREO_FIFO:
75 p_aout->fifo[i_fifo].b_die = 0;
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;
81 p_aout->fifo[i_fifo].buffer = p_fifo->buffer;
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;
89 case AOUT_ADEC_MONO_FIFO:
90 case AOUT_ADEC_STEREO_FIFO:
91 p_aout->fifo[i_fifo].b_die = 0;
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;
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 )
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 );
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 )
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 );
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;
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;
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 );
143 /* Release the fifos lock */
144 vlc_mutex_unlock( &p_aout->fifos_lock );
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 );
149 /* Return the pointer to the fifo structure */
153 /*****************************************************************************
155 *****************************************************************************/
156 void aout_DestroyFifo( aout_fifo_t * p_fifo )
158 intf_WarnMsg( 2, "aout info: fifo #%i destroyed", p_fifo->i_fifo );
163 /*****************************************************************************
165 *****************************************************************************/
166 void aout_FreeFifo( aout_fifo_t * p_fifo )
168 switch ( p_fifo->i_type )
170 case AOUT_EMPTY_FIFO:
174 case AOUT_INTF_MONO_FIFO:
175 case AOUT_INTF_STEREO_FIFO:
177 free( p_fifo->buffer );
178 p_fifo->i_type = AOUT_EMPTY_FIFO;
182 case AOUT_ADEC_MONO_FIFO:
183 case AOUT_ADEC_STEREO_FIFO:
185 free( p_fifo->buffer );
186 free( p_fifo->date );
187 p_fifo->i_type = AOUT_EMPTY_FIFO;