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.16 2002/06/01 12:32:01 sam 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 <stdlib.h> /* calloc(), malloc(), free() */
33 #include "audio_output.h"
35 /*****************************************************************************
37 *****************************************************************************/
38 aout_fifo_t * aout_CreateFifo( vlc_object_t *p_this, int i_format,
39 int i_channels, int i_rate, int i_frame_size,
42 aout_thread_t *p_aout;
43 aout_fifo_t *p_fifo = NULL;
46 /* Spawn an audio output if there is none */
47 p_aout = vlc_object_find( p_this->p_vlc, VLC_OBJECT_AOUT, FIND_CHILD );
51 if( p_aout->fifo[0].i_format != i_format )
53 msg_Dbg( p_this, "changing aout type" );
54 vlc_object_unlink_all( p_aout );
55 vlc_object_release( p_aout );
56 aout_DestroyThread( p_aout );
63 msg_Dbg( p_this, "no aout present, spawning one" );
65 p_aout = aout_CreateThread( p_this, i_channels, i_rate );
66 /* Everything failed */
73 /* temporary hack to switch output type (mainly for spdif)
74 * FIXME: to be adapted when several output are available */
75 /* Take the fifos lock */
76 vlc_mutex_lock( &p_aout->fifos_lock );
78 /* Look for a free fifo structure */
79 for( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
81 if( p_aout->fifo[i_index].i_format == AOUT_FIFO_NONE )
83 p_fifo = &p_aout->fifo[i_index];
84 p_fifo->i_fifo = i_index;
91 msg_Err( p_aout, "no fifo available" );
92 vlc_mutex_unlock( &p_aout->fifos_lock );
96 /* Initialize the new fifo structure */
97 switch ( p_fifo->i_format = i_format )
100 case AOUT_FIFO_SPDIF:
103 p_fifo->i_channels = i_channels;
104 p_fifo->i_rate = i_rate;
105 p_fifo->i_frame_size = i_frame_size;
107 p_fifo->i_unit_limit = (AOUT_FIFO_SIZE + 1)
108 * (i_frame_size / i_channels);
110 /* Allocate the memory needed to store the audio frames and their
111 * dates. As the fifo is a rotative fifo, we must be able to find
112 * out whether the fifo is full or empty, that's why we must in
113 * fact allocate memory for (AOUT_FIFO_SIZE+1) audio frames. */
114 p_fifo->date = malloc( ( sizeof(s16) * i_frame_size
116 * ( AOUT_FIFO_SIZE + 1 ) );
117 if ( p_fifo->date == NULL )
119 msg_Err( p_aout, "out of memory" );
120 p_fifo->i_format = AOUT_FIFO_NONE;
121 vlc_mutex_unlock( &p_aout->fifos_lock );
125 p_fifo->buffer = (u8 *)p_fifo->date + sizeof(mtime_t)
126 * ( AOUT_FIFO_SIZE + 1 );
128 /* Set the fifo's buffer as empty (the first frame that is to be
129 * played is also the first frame that is not to be played) */
130 p_fifo->i_start_frame = 0;
131 /* p_fifo->i_next_frame = 0; */
132 p_fifo->i_end_frame = 0;
134 /* Waiting for the audio decoder to compute enough frames to work
135 * out the fifo's current rate (as soon as the decoder has decoded
136 * enough frames, the members of the fifo structure that are not
137 * initialized now will be calculated) */
138 p_fifo->b_start_frame = 0;
139 p_fifo->b_next_frame = 0;
143 msg_Err( p_aout, "unknown fifo type 0x%x", p_fifo->i_format );
144 p_fifo->i_format = AOUT_FIFO_NONE;
145 vlc_mutex_unlock( &p_aout->fifos_lock );
149 /* Release the fifos lock */
150 vlc_mutex_unlock( &p_aout->fifos_lock );
152 msg_Dbg( p_aout, "fifo #%i allocated, %i channels, rate %li, "
153 "frame size %i", p_fifo->i_fifo, p_fifo->i_channels,
154 p_fifo->i_rate, p_fifo->i_frame_size );
156 /* Return the pointer to the fifo structure */
160 /*****************************************************************************
162 *****************************************************************************/
163 void aout_DestroyFifo( aout_fifo_t * p_fifo )
165 //X intf_Warn( p_fifo, 2, "fifo #%i destroyed", p_fifo->i_fifo );
167 vlc_mutex_lock( &p_fifo->data_lock );
169 vlc_mutex_unlock( &p_fifo->data_lock );
172 /*****************************************************************************
174 *****************************************************************************/
175 void aout_FreeFifo( aout_fifo_t * p_fifo )
177 switch ( p_fifo->i_format )
184 case AOUT_FIFO_SPDIF:
186 free( p_fifo->date );
187 p_fifo->i_format = AOUT_FIFO_NONE;