]> git.sesse.net Git - vlc/blob - src/audio_output/aout_internal.h
Galaktos does no longer exist.
[vlc] / src / audio_output / aout_internal.h
1 /*****************************************************************************
2  * aout_internal.h : internal defines for audio output
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #if defined(__PLUGIN__) || defined(__BUILTIN__) || !defined(__LIBVLC__)
25 # error This header file can only be included from LibVLC.
26 #endif
27
28 #ifndef __LIBVLC_AOUT_INTERNAL_H
29 # define __LIBVLC_AOUT_INTERNAL_H 1
30
31 #include <assert.h>
32
33 #if defined( __APPLE__ ) || defined( SYS_BSD )
34 #undef HAVE_ALLOCA
35 #endif
36
37 #ifdef HAVE_ALLOCA
38 #   define ALLOCA_TEST( p_alloc, p_new_buffer )                             \
39         if ( (p_alloc)->i_alloc_type == AOUT_ALLOC_STACK )                  \
40         {                                                                   \
41             (p_new_buffer) = alloca( i_alloc_size + sizeof(aout_buffer_t) );\
42             i_alloc_type = AOUT_ALLOC_STACK;                                \
43         }                                                                   \
44         else
45 #else
46 #   define ALLOCA_TEST( p_alloc, p_new_buffer )
47 #endif
48
49 #define aout_BufferAlloc( p_alloc, i_nb_usec, p_previous_buffer,            \
50                           p_new_buffer )                                    \
51     if ( (p_alloc)->i_alloc_type == AOUT_ALLOC_NONE )                       \
52     {                                                                       \
53         (p_new_buffer) = p_previous_buffer;                                 \
54     }                                                                       \
55     else                                                                    \
56     {                                                                       \
57         int i_alloc_size, i_alloc_type;                                     \
58         i_alloc_size = (int)( (uint64_t)(p_alloc)->i_bytes_per_sec          \
59                                             * (i_nb_usec) / 1000000 + 1 );  \
60         ALLOCA_TEST( p_alloc, p_new_buffer )                                \
61         {                                                                   \
62             (p_new_buffer) = malloc( i_alloc_size + sizeof(aout_buffer_t) );\
63             i_alloc_type = AOUT_ALLOC_HEAP;                                 \
64         }                                                                   \
65         if ( p_new_buffer != NULL )                                         \
66         {                                                                   \
67             (p_new_buffer)->i_alloc_type = i_alloc_type;                    \
68             (p_new_buffer)->i_size = i_alloc_size;                          \
69             (p_new_buffer)->p_buffer = (uint8_t *)(p_new_buffer)            \
70                                          + sizeof(aout_buffer_t);           \
71             (p_new_buffer)->b_discontinuity = false;                        \
72             if ( (p_previous_buffer) != NULL )                              \
73             {                                                               \
74                 (p_new_buffer)->start_date =                                \
75                            ((aout_buffer_t *)p_previous_buffer)->start_date;\
76                 (p_new_buffer)->end_date =                                  \
77                            ((aout_buffer_t *)p_previous_buffer)->end_date;  \
78             }                                                               \
79         }                                                                   \
80         /* we'll keep that for a while --Meuuh */                           \
81         /* else printf("%s:%d\n", __FILE__, __LINE__); */                   \
82     }
83
84 struct aout_filter_owner_sys_t
85 {
86     aout_instance_t *p_aout;
87     aout_input_t    *p_input;
88 };
89
90 /** an input stream for the audio output */
91 struct aout_input_t
92 {
93     /* When this lock is taken, the pipeline cannot be changed by a
94      * third-party. */
95     vlc_mutex_t             lock;
96
97     audio_sample_format_t   input;
98     aout_alloc_t            input_alloc;
99
100     /* pre-filters */
101     aout_filter_t *         pp_filters[AOUT_MAX_FILTERS];
102     int                     i_nb_filters;
103
104     aout_filter_t *         p_playback_rate_filter;
105
106     /* resamplers */
107     aout_filter_t *         pp_resamplers[AOUT_MAX_FILTERS];
108     int                     i_nb_resamplers;
109     int                     i_resampling_type;
110     mtime_t                 i_resamp_start_date;
111     int                     i_resamp_start_drift;
112
113     /* Mixer information */
114     audio_replay_gain_t     replay_gain;
115
116     /* If b_restart == 1, the input pipeline will be re-created. */
117     bool              b_restart;
118
119     /* If b_error == 1, there is no input pipeline. */
120     bool              b_error;
121
122     /* Did we just change the output format? (expect buffer inconsistencies) */
123     bool              b_changed;
124
125     /* last rate from input */
126     int               i_last_input_rate;
127
128     /* */
129     int               i_buffer_lost;
130
131     /* */
132     bool              b_paused;
133     mtime_t           i_pause_date;
134
135     /* */
136     bool                b_recycle_vout;
137     aout_request_vout_t request_vout;
138
139     /* */
140     aout_mixer_input_t mixer;
141  };
142
143 /****************************************************************************
144  * Prototypes
145  *****************************************************************************/
146
147 /* From input.c : */
148 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input, const aout_request_vout_t * );
149 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input );
150 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
151                     aout_buffer_t * p_buffer, int i_input_rate );
152
153 /* From filters.c : */
154 int aout_FiltersCreatePipeline ( aout_instance_t * p_aout, aout_filter_t ** pp_filters, int * pi_nb_filters, const audio_sample_format_t * p_input_format, const audio_sample_format_t * p_output_format );
155 void aout_FiltersDestroyPipeline ( aout_instance_t * p_aout, aout_filter_t ** pp_filters, int i_nb_filters );
156 void  aout_FiltersPlay ( aout_instance_t * p_aout, aout_filter_t ** pp_filters, int i_nb_filters, aout_buffer_t ** pp_input_buffer );
157 void aout_FiltersHintBuffers( aout_instance_t * p_aout, aout_filter_t ** pp_filters, int i_nb_filters, aout_alloc_t * p_first_alloc );
158
159 /* From mixer.c : */
160 int aout_MixerNew( aout_instance_t * p_aout );
161 void aout_MixerDelete( aout_instance_t * p_aout );
162 void aout_MixerRun( aout_instance_t * p_aout );
163 int aout_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier );
164 int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier );
165
166 /* From output.c : */
167 int aout_OutputNew( aout_instance_t * p_aout,
168                     audio_sample_format_t * p_format );
169 void aout_OutputPlay( aout_instance_t * p_aout, aout_buffer_t * p_buffer );
170 void aout_OutputDelete( aout_instance_t * p_aout );
171
172
173 /* From common.c : */
174 #define aout_New(a) __aout_New(VLC_OBJECT(a))
175 /* Release with vlc_object_release() */
176 aout_instance_t * __aout_New ( vlc_object_t * );
177
178 void aout_FifoInit( aout_instance_t *, aout_fifo_t *, uint32_t );
179 mtime_t aout_FifoNextStart( aout_instance_t *, aout_fifo_t * );
180 void aout_FifoPush( aout_instance_t *, aout_fifo_t *, aout_buffer_t * );
181 void aout_FifoSet( aout_instance_t *, aout_fifo_t *, mtime_t );
182 void aout_FifoMoveDates( aout_instance_t *, aout_fifo_t *, mtime_t );
183 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo );
184 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text, const audio_sample_format_t * p_format1, const audio_sample_format_t * p_format2 );
185
186
187 /* From intf.c :*/
188 int aout_VolumeSoftGet( aout_instance_t *, audio_volume_t * );
189 int aout_VolumeSoftSet( aout_instance_t *, audio_volume_t );
190 int aout_VolumeSoftInfos( aout_instance_t *, audio_volume_t * );
191 int aout_VolumeNoneGet( aout_instance_t *, audio_volume_t * );
192 int aout_VolumeNoneSet( aout_instance_t *, audio_volume_t );
193 int aout_VolumeNoneInfos( aout_instance_t *, audio_volume_t * );
194
195 /* From dec.c */
196 #define aout_DecNew(a, b, c, d, e) __aout_DecNew(VLC_OBJECT(a), b, c, d, e)
197 aout_input_t * __aout_DecNew( vlc_object_t *, aout_instance_t **,
198                               audio_sample_format_t *, const audio_replay_gain_t *,
199                               const aout_request_vout_t * );
200 int aout_DecDelete ( aout_instance_t *, aout_input_t * );
201 aout_buffer_t * aout_DecNewBuffer( aout_input_t *, size_t );
202 void aout_DecDeleteBuffer( aout_instance_t *, aout_input_t *, aout_buffer_t * );
203 int aout_DecPlay( aout_instance_t *, aout_input_t *, aout_buffer_t *, int i_input_rate );
204 int aout_DecGetResetLost( aout_instance_t *, aout_input_t * );
205 void aout_DecChangePause( aout_instance_t *, aout_input_t *, bool b_paused, mtime_t i_date );
206 void aout_DecFlush( aout_instance_t *, aout_input_t * );
207
208 /* Helpers */
209
210 static inline void aout_lock_mixer( aout_instance_t *p_aout )
211 {
212     vlc_mutex_lock( &p_aout->mixer_lock );
213 }
214
215 static inline void aout_unlock_mixer( aout_instance_t *p_aout )
216 {
217     vlc_mutex_unlock( &p_aout->mixer_lock );
218 }
219
220 static inline void aout_lock_input_fifos( aout_instance_t *p_aout )
221 {
222     vlc_mutex_lock( &p_aout->input_fifos_lock );
223 }
224
225 static inline void aout_unlock_input_fifos( aout_instance_t *p_aout )
226 {
227     vlc_mutex_unlock( &p_aout->input_fifos_lock );
228 }
229
230 static inline void aout_lock_output_fifo( aout_instance_t *p_aout )
231 {
232     vlc_mutex_lock( &p_aout->output_fifo_lock );
233 }
234
235 static inline void aout_unlock_output_fifo( aout_instance_t *p_aout )
236 {
237     vlc_mutex_unlock( &p_aout->output_fifo_lock );
238 }
239
240 static inline void aout_lock_input( aout_instance_t *p_aout, aout_input_t * p_input )
241 {
242     (void)p_aout;
243     vlc_mutex_lock( &p_input->lock );
244 }
245
246 static inline void aout_unlock_input( aout_instance_t *p_aout, aout_input_t * p_input )
247 {
248     (void)p_aout;
249     vlc_mutex_unlock( &p_input->lock );
250 }
251
252
253 /**
254  * This function will safely mark aout input to be restarted as soon as
255  * possible to take configuration changes into account */
256 static inline void AoutInputsMarkToRestart( aout_instance_t *p_aout )
257 {
258     int i;
259     aout_lock_mixer( p_aout );
260     for( i = 0; i < p_aout->i_nb_inputs; i++ )
261         p_aout->pp_inputs[i]->b_restart = true;
262     aout_unlock_mixer( p_aout );
263 }
264
265 /* This function will add or remove a a module from a string list (comma
266  * separated). It will return true if there is a modification
267  * In case p_aout is NULL, we will use configuration instead of variable */
268 static inline bool AoutChangeFilterString( vlc_object_t *p_obj, aout_instance_t * p_aout,
269                                            const char* psz_variable,
270                                            const char *psz_name, bool b_add )
271 {
272     vlc_value_t val;
273     char *psz_parser;
274
275     if( *psz_name == '\0' )
276         return false;
277
278     if( p_aout )
279         var_Get( p_aout, psz_variable, &val );
280     else
281         val.psz_string = config_GetPsz( p_obj, "audio-filter" );
282
283     if( !val.psz_string )
284         val.psz_string = strdup("");
285
286     psz_parser = strstr( val.psz_string, psz_name );
287
288     if( ( b_add && psz_parser ) || ( !b_add && !psz_parser ) )
289     {
290         /* Nothing to do */
291         free( val.psz_string );
292         return false;
293     }
294
295     if( b_add )
296     {
297         char *psz_old = val.psz_string;
298         if( *psz_old )
299         {
300             if( asprintf( &val.psz_string, "%s:%s", psz_old, psz_name ) == -1 )
301                 val.psz_string = NULL;
302         }
303         else
304             val.psz_string = strdup( psz_name );
305         free( psz_old );
306     }
307     else
308     {
309         const int i_name = strlen( psz_name );
310         const char *psz_next;
311
312         psz_next = &psz_parser[i_name];
313         if( *psz_next == ':' )
314             psz_next++;
315
316         memmove( psz_parser, psz_next, strlen(psz_next)+1 );
317     }
318
319     if( p_aout )
320         var_Set( p_aout, psz_variable, val );
321     else
322         config_PutPsz( p_obj, psz_variable, val.psz_string );
323     free( val.psz_string );
324     return true;
325 }
326
327 #endif /* !__LIBVLC_AOUT_INTERNAL_H */