]> git.sesse.net Git - vlc/blob - src/audio_output/aout_ext-dec.c
* ALL: the first libvlc commit.
[vlc] / src / audio_output / aout_ext-dec.c
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 $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Cyril Deguet <asmax@via.ecp.fr>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                            /* calloc(), malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32
33 #include "audio_output.h"
34
35 /*****************************************************************************
36  * aout_CreateFifo
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,
40                                void *p_buffer )
41 {
42     aout_thread_t *p_aout;
43     aout_fifo_t   *p_fifo = NULL;
44     int i_index;
45
46     /* Spawn an audio output if there is none */
47     p_aout = vlc_object_find( p_this->p_vlc, VLC_OBJECT_AOUT, FIND_CHILD );
48
49     if( p_aout )
50     {
51         if( p_aout->fifo[0].i_format != i_format )
52         {
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 );
57             p_aout = NULL;
58         }
59     }
60
61     if( p_aout == NULL )
62     {
63         msg_Dbg( p_this, "no aout present, spawning one" );
64
65         p_aout = aout_CreateThread( p_this, i_channels, i_rate );
66         /* Everything failed */
67         if( p_aout == NULL )
68         {
69             return NULL;
70         }
71     }
72
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 );
77
78     /* Look for a free fifo structure */
79     for( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
80     {
81         if( p_aout->fifo[i_index].i_format == AOUT_FIFO_NONE )
82         {
83             p_fifo = &p_aout->fifo[i_index];
84             p_fifo->i_fifo = i_index;
85             break;
86         }
87     }
88
89     if( p_fifo == NULL )
90     {
91         msg_Err( p_aout, "no fifo available" );
92         vlc_mutex_unlock( &p_aout->fifos_lock );
93         return NULL;
94     }
95
96     /* Initialize the new fifo structure */
97     switch ( p_fifo->i_format = i_format )
98     {
99         case AOUT_FIFO_PCM:
100         case AOUT_FIFO_SPDIF:
101             p_fifo->b_die = 0;
102
103             p_fifo->i_channels = i_channels;
104             p_fifo->i_rate = i_rate;
105             p_fifo->i_frame_size = i_frame_size;
106
107             p_fifo->i_unit_limit = (AOUT_FIFO_SIZE + 1)
108                                      * (i_frame_size / i_channels);
109
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 
115                                       + sizeof(mtime_t) )
116                                    * ( AOUT_FIFO_SIZE + 1 ) );
117             if ( p_fifo->date == NULL )
118             {
119                 msg_Err( p_aout, "out of memory" );
120                 p_fifo->i_format = AOUT_FIFO_NONE;
121                 vlc_mutex_unlock( &p_aout->fifos_lock );
122                 return NULL;
123             }
124
125             p_fifo->buffer = (u8 *)p_fifo->date + sizeof(mtime_t)
126                                                      * ( AOUT_FIFO_SIZE + 1 );
127
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;
133
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;
140             break;
141
142         default:
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 );
146             return NULL;
147     }
148
149     /* Release the fifos lock */
150     vlc_mutex_unlock( &p_aout->fifos_lock );
151
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 );
155
156     /* Return the pointer to the fifo structure */
157     return p_fifo;
158 }
159
160 /*****************************************************************************
161  * aout_DestroyFifo
162  *****************************************************************************/
163 void aout_DestroyFifo( aout_fifo_t * p_fifo )
164 {
165 //X    intf_Warn( p_fifo, 2, "fifo #%i destroyed", p_fifo->i_fifo );
166
167     vlc_mutex_lock( &p_fifo->data_lock );
168     p_fifo->b_die = 1;
169     vlc_mutex_unlock( &p_fifo->data_lock );
170 }
171
172 /*****************************************************************************
173  * aout_FreeFifo
174  *****************************************************************************/
175 void aout_FreeFifo( aout_fifo_t * p_fifo )
176 {
177     switch ( p_fifo->i_format )
178     {
179         case AOUT_FIFO_NONE:
180
181             break;
182
183         case AOUT_FIFO_PCM:
184         case AOUT_FIFO_SPDIF:
185
186             free( p_fifo->date );
187             p_fifo->i_format = AOUT_FIFO_NONE;
188
189             break;
190
191         default:
192
193             break;
194     }
195 }
196