]> git.sesse.net Git - vlc/blob - src/audio_output/aout_internal.h
libvlc: Unify volume/mute change functions and add a mutex layer
[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 aout_buffer_t *aout_BufferAlloc(aout_alloc_t *allocation, mtime_t microseconds,
32         aout_buffer_t *old_buffer);
33
34 typedef struct
35 {
36     struct vout_thread_t  *(*pf_request_vout)( void *, struct vout_thread_t *,
37                                                video_format_t *, bool );
38     void *p_private;
39 } aout_request_vout_t;
40
41 struct filter_owner_sys_t
42 {
43     aout_instance_t *p_aout;
44     aout_input_t    *p_input;
45 };
46
47 block_t *aout_FilterBufferNew( filter_t *, int );
48
49 /** an input stream for the audio output */
50 struct aout_input_t
51 {
52     /* When this lock is taken, the pipeline cannot be changed by a
53      * third-party. */
54     vlc_mutex_t             lock;
55
56     audio_sample_format_t   input;
57     aout_alloc_t            input_alloc;
58
59     /* pre-filters */
60     filter_t *              pp_filters[AOUT_MAX_FILTERS];
61     int                     i_nb_filters;
62
63     filter_t *              p_playback_rate_filter;
64
65     /* resamplers */
66     filter_t *              pp_resamplers[AOUT_MAX_FILTERS];
67     int                     i_nb_resamplers;
68     int                     i_resampling_type;
69     mtime_t                 i_resamp_start_date;
70     int                     i_resamp_start_drift;
71
72     /* Mixer information */
73     audio_replay_gain_t     replay_gain;
74
75     /* If b_restart == 1, the input pipeline will be re-created. */
76     bool              b_restart;
77
78     /* If b_error == 1, there is no input pipeline. */
79     bool              b_error;
80
81     /* Did we just change the output format? (expect buffer inconsistencies) */
82     bool              b_changed;
83
84     /* last rate from input */
85     int               i_last_input_rate;
86
87     /* */
88     int               i_buffer_lost;
89
90     /* */
91     bool              b_paused;
92     mtime_t           i_pause_date;
93
94     /* */
95     bool                b_recycle_vout;
96     aout_request_vout_t request_vout;
97
98     /* */
99     aout_mixer_input_t mixer;
100  };
101
102 /****************************************************************************
103  * Prototypes
104  *****************************************************************************/
105
106 /* From input.c : */
107 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input, const aout_request_vout_t * );
108 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input );
109 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
110                     aout_buffer_t * p_buffer, int i_input_rate );
111 void aout_InputCheckAndRestart( aout_instance_t * p_aout, aout_input_t * p_input );
112
113 /* From filters.c : */
114 int aout_FiltersCreatePipeline ( aout_instance_t * p_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 );
115 void aout_FiltersDestroyPipeline ( aout_instance_t * p_aout, filter_t ** pp_filters, int i_nb_filters );
116 void  aout_FiltersPlay ( filter_t ** pp_filters, unsigned i_nb_filters, aout_buffer_t ** pp_input_buffer );
117 void aout_FiltersHintBuffers( aout_instance_t * p_aout, filter_t ** pp_filters, int i_nb_filters, aout_alloc_t * p_first_alloc );
118
119 /* From mixer.c : */
120 int aout_MixerNew( aout_instance_t * p_aout );
121 void aout_MixerDelete( aout_instance_t * p_aout );
122 void aout_MixerRun( aout_instance_t * p_aout );
123 int aout_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier );
124 int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier );
125
126 /* From output.c : */
127 int aout_OutputNew( aout_instance_t * p_aout,
128                     audio_sample_format_t * p_format );
129 void aout_OutputPlay( aout_instance_t * p_aout, aout_buffer_t * p_buffer );
130 void aout_OutputDelete( aout_instance_t * p_aout );
131
132
133 /* From common.c : */
134 #define aout_New(a) __aout_New(VLC_OBJECT(a))
135 /* Release with vlc_object_release() */
136 aout_instance_t * __aout_New ( vlc_object_t * );
137
138 void aout_FifoInit( aout_instance_t *, aout_fifo_t *, uint32_t );
139 mtime_t aout_FifoNextStart( aout_instance_t *, aout_fifo_t * );
140 void aout_FifoPush( aout_instance_t *, aout_fifo_t *, aout_buffer_t * );
141 void aout_FifoSet( aout_instance_t *, aout_fifo_t *, mtime_t );
142 void aout_FifoMoveDates( aout_instance_t *, aout_fifo_t *, mtime_t );
143 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo );
144 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 );
145
146
147 /* From intf.c :*/
148 int aout_VolumeSoftGet( aout_instance_t *, audio_volume_t * );
149 int aout_VolumeSoftSet( aout_instance_t *, audio_volume_t );
150 int aout_VolumeSoftInfos( aout_instance_t *, audio_volume_t * );
151 int aout_VolumeNoneGet( aout_instance_t *, audio_volume_t * );
152 int aout_VolumeNoneSet( aout_instance_t *, audio_volume_t );
153 int aout_VolumeNoneInfos( aout_instance_t *, audio_volume_t * );
154 int doVolumeChanges( unsigned action, vlc_object_t * p_object, int i_nb_steps,
155                     audio_volume_t i_volume, audio_volume_t * i_return_volume,
156                     bool b_mute );
157
158 /* From dec.c */
159 #define aout_DecNew(a, b, c, d, e) __aout_DecNew(VLC_OBJECT(a), b, c, d, e)
160 aout_input_t * __aout_DecNew( vlc_object_t *, aout_instance_t **,
161                               audio_sample_format_t *, const audio_replay_gain_t *,
162                               const aout_request_vout_t * );
163 int aout_DecDelete ( aout_instance_t *, aout_input_t * );
164 aout_buffer_t * aout_DecNewBuffer( aout_input_t *, size_t );
165 void aout_DecDeleteBuffer( aout_instance_t *, aout_input_t *, aout_buffer_t * );
166 int aout_DecPlay( aout_instance_t *, aout_input_t *, aout_buffer_t *, int i_input_rate );
167 int aout_DecGetResetLost( aout_instance_t *, aout_input_t * );
168 void aout_DecChangePause( aout_instance_t *, aout_input_t *, bool b_paused, mtime_t i_date );
169 void aout_DecFlush( aout_instance_t *, aout_input_t * );
170
171 /* Audio output locking */
172
173 #if !defined (NDEBUG) \
174  && defined __linux__ && (defined (__i386__) || defined (__x86_64__))
175 # define AOUT_DEBUG 1
176 #endif
177
178 #ifdef AOUT_DEBUG
179 enum
180 {
181     MIXER_LOCK=1,
182     INPUT_LOCK=2,
183     INPUT_FIFO_LOCK=4,
184     OUTPUT_FIFO_LOCK=8,
185     VOLUME_VARS_LOCK=16
186 };
187
188 void aout_lock (unsigned);
189 void aout_unlock (unsigned);
190
191 #else
192 # define aout_lock( i )   (void)0
193 # define aout_unlock( i ) (void)0
194 #endif
195
196 static inline void aout_lock_mixer( aout_instance_t *p_aout )
197 {
198     aout_lock( MIXER_LOCK );
199     vlc_mutex_lock( &p_aout->mixer_lock );
200 }
201
202 static inline void aout_unlock_mixer( aout_instance_t *p_aout )
203 {
204     aout_unlock( MIXER_LOCK );
205     vlc_mutex_unlock( &p_aout->mixer_lock );
206 }
207
208 static inline void aout_lock_input_fifos( aout_instance_t *p_aout )
209 {
210     aout_lock( INPUT_FIFO_LOCK );
211     vlc_mutex_lock( &p_aout->input_fifos_lock );
212 }
213
214 static inline void aout_unlock_input_fifos( aout_instance_t *p_aout )
215 {
216     aout_unlock( INPUT_FIFO_LOCK );
217     vlc_mutex_unlock( &p_aout->input_fifos_lock );
218 }
219
220 static inline void aout_lock_output_fifo( aout_instance_t *p_aout )
221 {
222     aout_lock( OUTPUT_FIFO_LOCK );
223     vlc_mutex_lock( &p_aout->output_fifo_lock );
224 }
225
226 static inline void aout_unlock_output_fifo( aout_instance_t *p_aout )
227 {
228     aout_unlock( OUTPUT_FIFO_LOCK );
229     vlc_mutex_unlock( &p_aout->output_fifo_lock );
230 }
231
232 static inline void aout_lock_input( aout_instance_t *p_aout, aout_input_t * p_input )
233 {
234     (void)p_aout;
235     aout_lock( INPUT_LOCK );
236     vlc_mutex_lock( &p_input->lock );
237 }
238
239 static inline void aout_unlock_input( aout_instance_t *p_aout, aout_input_t * p_input )
240 {
241     (void)p_aout;
242     aout_unlock( INPUT_LOCK );
243     vlc_mutex_unlock( &p_input->lock );
244 }
245
246 static inline void aout_lock_volume( aout_instance_t *p_aout )
247 {
248     aout_lock( VOLUME_VARS_LOCK );
249     vlc_mutex_lock( &p_aout->volume_vars_lock );
250 }
251
252 static inline void aout_unlock_volume( aout_instance_t *p_aout )
253 {
254     aout_unlock( VOLUME_VARS_LOCK );
255     vlc_mutex_unlock( &p_aout->volume_vars_lock );
256 }
257
258 /* Helpers */
259
260 /**
261  * This function will safely mark aout input to be restarted as soon as
262  * possible to take configuration changes into account */
263 static inline void AoutInputsMarkToRestart( aout_instance_t *p_aout )
264 {
265     int i;
266     aout_lock_mixer( p_aout );
267     for( i = 0; i < p_aout->i_nb_inputs; i++ )
268         p_aout->pp_inputs[i]->b_restart = true;
269     aout_unlock_mixer( p_aout );
270 }
271
272 /* This function will add or remove a a module from a string list (comma
273  * separated). It will return true if there is a modification
274  * In case p_aout is NULL, we will use configuration instead of variable */
275 static inline bool AoutChangeFilterString( vlc_object_t *p_obj, aout_instance_t * p_aout,
276                                            const char* psz_variable,
277                                            const char *psz_name, bool b_add )
278 {
279     char *psz_val;
280     char *psz_parser;
281
282     if( *psz_name == '\0' )
283         return false;
284
285     if( p_aout )
286         psz_val = var_GetString( p_aout, psz_variable );
287     else
288     {
289         psz_val = var_CreateGetString( p_obj->p_libvlc, "audio-filter" );
290         var_Destroy( p_obj->p_libvlc, "audio-filter" );
291     }
292
293     if( !psz_val )
294         psz_val = strdup( "" );
295
296     psz_parser = strstr( psz_val, psz_name );
297
298     if( ( b_add && psz_parser ) || ( !b_add && !psz_parser ) )
299     {
300         /* Nothing to do */
301         free( psz_val );
302         return false;
303     }
304
305     if( b_add )
306     {
307         char *psz_old = psz_val;
308         if( *psz_old )
309         {
310             if( asprintf( &psz_val, "%s:%s", psz_old, psz_name ) == -1 )
311                 psz_val = NULL;
312         }
313         else
314             psz_val = strdup( psz_name );
315         free( psz_old );
316     }
317     else
318     {
319         const int i_name = strlen( psz_name );
320         const char *psz_next;
321
322         psz_next = &psz_parser[i_name];
323         if( *psz_next == ':' )
324             psz_next++;
325
326         memmove( psz_parser, psz_next, strlen(psz_next)+1 );
327     }
328
329     if( p_aout )
330         var_SetString( p_aout, psz_variable, psz_val );
331     else
332         config_PutPsz( p_obj, psz_variable, psz_val );
333     free( psz_val );
334     return true;
335 }
336
337 #endif /* !__LIBVLC_AOUT_INTERNAL_H */