1 /*****************************************************************************
2 * aout_ext-dec.c : exported fifo management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: aout_ext-dec.c,v 1.15 2002/05/18 15:51:37 gbazin Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Cyril Deguet <asmax@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdio.h> /* "intf_msg.h" */
29 #include <stdlib.h> /* calloc(), malloc(), free() */
32 #include <videolan/vlc.h>
34 #include "audio_output.h"
36 /*****************************************************************************
38 *****************************************************************************/
39 aout_fifo_t * aout_CreateFifo( int i_format, int i_channels, int i_rate,
40 int i_frame_size, void *p_buffer )
42 aout_thread_t *p_aout;
43 aout_fifo_t *p_fifo = NULL;
46 /* Spawn an audio output if there is none */
47 vlc_mutex_lock( &p_aout_bank->lock );
49 if( p_aout_bank->i_count == 0 )
51 intf_WarnMsg( 1, "aout: no aout present, spawning one" );
53 p_aout = aout_CreateThread( NULL, i_channels, i_rate );
55 /* Everything failed */
58 vlc_mutex_unlock( &p_aout_bank->lock );
62 p_aout_bank->pp_aout[ p_aout_bank->i_count ] = p_aout;
63 p_aout_bank->i_count++;
65 /* temporary hack to switch output type (mainly for spdif)
66 * FIXME: to be adapted when several output are available */
67 else if( p_aout_bank->pp_aout[0]->fifo[0].i_format != i_format )
69 intf_WarnMsg( 1, "aout: changing aout type" );
71 aout_DestroyThread( p_aout_bank->pp_aout[0], NULL );
73 p_aout = aout_CreateThread( NULL, i_channels, i_rate );
75 /* Everything failed */
78 vlc_mutex_unlock( &p_aout_bank->lock );
82 p_aout_bank->pp_aout[0] = p_aout;
86 /* Take the first audio output FIXME: take the best one */
87 p_aout = p_aout_bank->pp_aout[ 0 ];
90 /* Take the fifos lock */
91 vlc_mutex_lock( &p_aout->fifos_lock );
93 /* Look for a free fifo structure */
94 for( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
96 if( p_aout->fifo[i_index].i_format == AOUT_FIFO_NONE )
98 p_fifo = &p_aout->fifo[i_index];
99 p_fifo->i_fifo = i_index;
106 intf_ErrMsg( "aout error: no fifo available" );
107 vlc_mutex_unlock( &p_aout->fifos_lock );
108 vlc_mutex_unlock( &p_aout_bank->lock );
112 /* Initialize the new fifo structure */
113 switch ( p_fifo->i_format = i_format )
116 case AOUT_FIFO_SPDIF:
119 p_fifo->i_channels = i_channels;
120 p_fifo->i_rate = i_rate;
121 p_fifo->i_frame_size = i_frame_size;
123 p_fifo->i_unit_limit = (AOUT_FIFO_SIZE + 1)
124 * (i_frame_size / i_channels);
126 /* Allocate the memory needed to store the audio frames and their
127 * dates. As the fifo is a rotative fifo, we must be able to find
128 * out whether the fifo is full or empty, that's why we must in
129 * fact allocate memory for (AOUT_FIFO_SIZE+1) audio frames. */
130 p_fifo->date = malloc( ( sizeof(s16) * i_frame_size
132 * ( AOUT_FIFO_SIZE + 1 ) );
133 if ( p_fifo->date == NULL )
135 intf_ErrMsg( "aout error: cannot create fifo data" );
136 p_fifo->i_format = AOUT_FIFO_NONE;
137 vlc_mutex_unlock( &p_aout->fifos_lock );
138 vlc_mutex_unlock( &p_aout_bank->lock );
142 p_fifo->buffer = (u8 *)p_fifo->date + sizeof(mtime_t)
143 * ( AOUT_FIFO_SIZE + 1 );
145 /* Set the fifo's buffer as empty (the first frame that is to be
146 * played is also the first frame that is not to be played) */
147 p_fifo->i_start_frame = 0;
148 /* p_fifo->i_next_frame = 0; */
149 p_fifo->i_end_frame = 0;
151 /* Waiting for the audio decoder to compute enough frames to work
152 * out the fifo's current rate (as soon as the decoder has decoded
153 * enough frames, the members of the fifo structure that are not
154 * initialized now will be calculated) */
155 p_fifo->b_start_frame = 0;
156 p_fifo->b_next_frame = 0;
160 intf_ErrMsg( "aout error: unknown fifo type 0x%x",
162 p_fifo->i_format = AOUT_FIFO_NONE;
163 vlc_mutex_unlock( &p_aout->fifos_lock );
164 vlc_mutex_unlock( &p_aout_bank->lock );
168 /* Release the fifos lock */
169 vlc_mutex_unlock( &p_aout->fifos_lock );
170 vlc_mutex_unlock( &p_aout_bank->lock );
172 intf_WarnMsg( 2, "aout info: fifo #%i allocated, %i channels, rate %li, "
173 "frame size %i", p_fifo->i_fifo, p_fifo->i_channels,
174 p_fifo->i_rate, p_fifo->i_frame_size );
176 /* Return the pointer to the fifo structure */
180 /*****************************************************************************
182 *****************************************************************************/
183 void aout_DestroyFifo( aout_fifo_t * p_fifo )
185 intf_WarnMsg( 2, "aout info: fifo #%i destroyed", p_fifo->i_fifo );
187 vlc_mutex_lock( &p_fifo->data_lock );
189 vlc_mutex_unlock( &p_fifo->data_lock );
192 /*****************************************************************************
194 *****************************************************************************/
195 void aout_FreeFifo( aout_fifo_t * p_fifo )
197 switch ( p_fifo->i_format )
204 case AOUT_FIFO_SPDIF:
206 free( p_fifo->date );
207 p_fifo->i_format = AOUT_FIFO_NONE;