Set swap interval to 1 in glx when possible.
[vlc/solaris.git] / src / control / audio.c
blobd39de60ad75c1b339e7ad2d87473efe16fa1f91b
1 /*****************************************************************************
2 * libvlc_audio.c: New libvlc audio control API
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
5 * $Id$
7 * Authors: Filippo Carone <filippo@carone.org>
8 * Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <assert.h>
31 #include <vlc/libvlc.h>
32 #include <vlc/libvlc_media.h>
33 #include <vlc/libvlc_media_player.h>
35 #include <vlc_common.h>
36 #include <vlc_input.h>
37 #include <vlc_aout.h>
38 #include <vlc_modules.h>
40 #include "libvlc_internal.h"
41 #include "media_player_internal.h"
44 * Remember to release the returned aout_instance_t since it is locked at
45 * the end of this function.
47 static aout_instance_t *GetAOut( libvlc_media_player_t *mp )
49 assert( mp != NULL );
51 input_thread_t *p_input = libvlc_get_input_thread( mp );
52 if( p_input == NULL )
53 return NULL;
55 aout_instance_t * p_aout = input_GetAout( p_input );
56 vlc_object_release( p_input );
57 if( p_aout == NULL )
58 libvlc_printerr( "No active audio output" );
59 return p_aout;
62 /*****************************************
63 * Get the list of available audio outputs
64 *****************************************/
65 libvlc_audio_output_t *
66 libvlc_audio_output_list_get( libvlc_instance_t *p_instance )
68 VLC_UNUSED( p_instance );
69 libvlc_audio_output_t *p_list = NULL,
70 *p_actual = NULL,
71 *p_previous = NULL;
72 module_t **module_list = module_list_get( NULL );
74 for (size_t i = 0; module_list[i]; i++)
76 module_t *p_module = module_list[i];
78 if( module_provides( p_module, "audio output" ) )
80 if( p_actual == NULL)
82 p_actual = ( libvlc_audio_output_t * )
83 malloc( sizeof( libvlc_audio_output_t ) );
84 if( p_actual == NULL )
86 libvlc_printerr( "Not enough memory" );
87 libvlc_audio_output_list_release( p_list );
88 module_list_free( module_list );
89 return NULL;
91 if( p_list == NULL )
93 p_list = p_actual;
94 p_previous = p_actual;
97 p_actual->psz_name = strdup( module_get_object( p_module ) );
98 p_actual->psz_description = strdup( module_get_name( p_module, true ) );
99 p_actual->p_next = NULL;
100 if( p_previous != p_actual ) /* not first item */
101 p_previous->p_next = p_actual;
102 p_previous = p_actual;
103 p_actual = p_actual->p_next;
107 module_list_free( module_list );
109 return p_list;
112 /********************************************
113 * Free the list of available audio outputs
114 ***********************************************/
115 void libvlc_audio_output_list_release( libvlc_audio_output_t *p_list )
117 libvlc_audio_output_t *p_actual, *p_before;
118 p_actual = p_list;
120 while ( p_actual )
122 free( p_actual->psz_name );
123 free( p_actual->psz_description );
124 p_before = p_actual;
125 p_actual = p_before->p_next;
126 free( p_before );
131 /***********************
132 * Set the audio output.
133 ***********************/
134 int libvlc_audio_output_set( libvlc_media_player_t *mp, const char *psz_name )
136 if( !module_exists( psz_name ) )
137 return -1;
138 var_SetString( mp, "aout", psz_name );
139 return 0;
142 /****************************
143 * Get count of devices.
144 *****************************/
145 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
146 const char *psz_audio_output )
148 char *psz_config_name;
149 if( !psz_audio_output )
150 return 0;
151 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
152 return 0;
154 module_config_t *p_module_config = config_FindConfig(
155 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
157 if( p_module_config && p_module_config->pf_update_list )
159 vlc_value_t val;
160 val.psz_string = strdup( p_module_config->value.psz );
162 p_module_config->pf_update_list(
163 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
164 free( val.psz_string );
165 free( psz_config_name );
167 return p_module_config->i_list;
170 free( psz_config_name );
171 return 0;
174 /********************************
175 * Get long name of device
176 *********************************/
177 char * libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
178 const char *psz_audio_output,
179 int i_device )
181 char *psz_config_name;
182 if( !psz_audio_output )
183 return NULL;
184 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
185 return NULL;
187 module_config_t *p_module_config = config_FindConfig(
188 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
190 if( p_module_config )
192 // refresh if there arent devices
193 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
195 vlc_value_t val;
196 val.psz_string = strdup( p_module_config->value.psz );
198 p_module_config->pf_update_list(
199 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
200 free( val.psz_string );
203 if( i_device >= 0 && i_device < p_module_config->i_list )
205 free( psz_config_name );
207 if( p_module_config->ppsz_list_text[i_device] )
208 return strdup( p_module_config->ppsz_list_text[i_device] );
209 else
210 return strdup( p_module_config->ppsz_list[i_device] );
214 free( psz_config_name );
215 return NULL;
218 /********************************
219 * Get id name of device
220 *********************************/
221 char * libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
222 const char *psz_audio_output,
223 int i_device )
225 char *psz_config_name;
226 if( !psz_audio_output )
227 return NULL;
228 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1)
229 return NULL;
231 module_config_t *p_module_config = config_FindConfig(
232 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name );
234 if( p_module_config )
236 // refresh if there arent devices
237 if( p_module_config->i_list < 2 && p_module_config->pf_update_list )
239 vlc_value_t val;
240 val.psz_string = strdup( p_module_config->value.psz );
242 p_module_config->pf_update_list(
243 VLC_OBJECT( p_instance->p_libvlc_int ), psz_config_name, val, val, NULL );
244 free( val.psz_string );
247 if( i_device >= 0 && i_device < p_module_config->i_list )
249 free( psz_config_name );
250 return strdup( p_module_config->ppsz_list[i_device] );
254 free( psz_config_name );
255 return NULL;
258 /*****************************
259 * Set device for using
260 *****************************/
261 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
262 const char *psz_audio_output,
263 const char *psz_device_id )
265 char *psz_config_name;
266 if( !psz_audio_output || !psz_device_id )
267 return;
268 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
269 return;
270 if( !var_Type( mp, psz_audio_output ) )
271 /* Don't recreate the same variable over and over and over... */
272 var_Create( mp, psz_audio_output, VLC_VAR_STRING );
273 var_SetString( mp, psz_config_name, psz_device_id );
274 free( psz_config_name );
277 /*****************************************************************************
278 * libvlc_audio_output_get_device_type : Get the current audio device type
279 *****************************************************************************/
280 int libvlc_audio_output_get_device_type( libvlc_media_player_t *mp )
282 aout_instance_t *p_aout = GetAOut( mp );
283 if( p_aout )
285 int i_device_type = var_GetInteger( p_aout, "audio-device" );
286 vlc_object_release( p_aout );
287 return i_device_type;
289 return libvlc_AudioOutputDevice_Error;
292 /*****************************************************************************
293 * libvlc_audio_output_set_device_type : Set the audio device type
294 *****************************************************************************/
295 void libvlc_audio_output_set_device_type( libvlc_media_player_t *mp,
296 int device_type )
298 aout_instance_t *p_aout = GetAOut( mp );
299 if( !p_aout )
300 return;
301 if( var_SetInteger( p_aout, "audio-device", device_type ) < 0 )
302 libvlc_printerr( "Error setting audio device" );
303 vlc_object_release( p_aout );
306 /*****************************************************************************
307 * libvlc_audio_get_mute : Get the volume state, true if muted
308 *****************************************************************************/
309 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
311 aout_ToggleMute( mp, NULL );
314 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
316 return aout_IsMuted( VLC_OBJECT(mp) );
319 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
321 aout_SetMute( VLC_OBJECT(mp), NULL, !!mute );
324 /*****************************************************************************
325 * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
326 *****************************************************************************/
327 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
329 audio_volume_t i_volume = aout_VolumeGet( mp );
331 return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
335 /*****************************************************************************
336 * libvlc_audio_set_volume : Set the current volume
337 *****************************************************************************/
338 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int i_volume )
340 if( i_volume < 0 || i_volume > 200 )
342 libvlc_printerr( "Volume out of range" );
343 return -1;
346 i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;
347 aout_VolumeSet( mp, i_volume );
348 return 0;
351 /*****************************************************************************
352 * libvlc_audio_get_track_count : Get the number of available audio tracks
353 *****************************************************************************/
354 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
356 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
357 int i_track_count;
359 if( !p_input_thread )
360 return -1;
362 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
364 vlc_object_release( p_input_thread );
365 return i_track_count;
368 /*****************************************************************************
369 * libvlc_audio_get_track_description : Get the description of available audio tracks
370 *****************************************************************************/
371 libvlc_track_description_t *
372 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
374 return libvlc_get_track_description( p_mi, "audio-es" );
377 /*****************************************************************************
378 * libvlc_audio_get_track : Get the current audio track
379 *****************************************************************************/
380 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
382 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
383 vlc_value_t val_list;
384 vlc_value_t val;
385 int i_track = -1;
386 int i;
388 if( !p_input_thread )
389 return -1;
391 if( var_Get( p_input_thread, "audio-es", &val ) < 0 )
393 vlc_object_release( p_input_thread );
394 libvlc_printerr( "Audio track information not found" );
395 return -1;
398 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
399 for( i = 0; i < val_list.p_list->i_count; i++ )
401 if( val_list.p_list->p_values[i].i_int == val.i_int )
403 i_track = i;
404 break;
407 var_FreeList( &val_list, NULL );
408 vlc_object_release( p_input_thread );
409 return i_track;
412 /*****************************************************************************
413 * libvlc_audio_set_track : Set the current audio track
414 *****************************************************************************/
415 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
417 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
418 vlc_value_t val_list;
419 vlc_value_t newval;
420 int i_ret;
422 if( !p_input_thread )
423 return -1;
425 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
426 if( (i_track < 0) || (i_track > val_list.p_list->i_count) )
428 libvlc_printerr( "Audio track out of range" );
429 i_ret = -1;
430 goto end;
433 newval = val_list.p_list->p_values[i_track];
434 i_ret = var_Set( p_input_thread, "audio-es", newval );
435 if( i_ret < 0 )
437 libvlc_printerr( "Audio track out of range" ); /* Race... */
438 i_ret = -1;
439 goto end;
441 i_ret = 0;
443 end:
444 var_FreeList( &val_list, NULL );
445 vlc_object_release( p_input_thread );
446 return i_ret;
449 /*****************************************************************************
450 * libvlc_audio_get_channel : Get the current audio channel
451 *****************************************************************************/
452 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
454 aout_instance_t *p_aout = GetAOut( mp );
455 if( !p_aout )
456 return 0;
458 int val = var_GetInteger( p_aout, "audio-channels" );
459 vlc_object_release( p_aout );
460 return val;
463 /*****************************************************************************
464 * libvlc_audio_set_channel : Set the current audio channel
465 *****************************************************************************/
466 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
468 aout_instance_t *p_aout = GetAOut( mp );
469 int ret = 0;
471 if( !p_aout )
472 return -1;
474 if( var_SetInteger( p_aout, "audio-channels", channel ) < 0 )
476 libvlc_printerr( "Audio channel out of range" );
477 ret = -1;
479 vlc_object_release( p_aout );
480 return ret;
483 /*****************************************************************************
484 * libvlc_audio_get_delay : Get the current audio delay
485 *****************************************************************************/
486 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
488 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
489 int64_t val = 0;
490 if( p_input_thread != NULL )
492 val = var_GetTime( p_input_thread, "audio-delay" );
493 vlc_object_release( p_input_thread );
495 return val;
498 /*****************************************************************************
499 * libvlc_audio_set_delay : Set the current audio delay
500 *****************************************************************************/
501 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
503 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
504 int ret = 0;
505 if( p_input_thread != NULL )
507 var_SetTime( p_input_thread, "audio-delay", i_delay );
508 vlc_object_release( p_input_thread );
510 else
512 ret = -1;
514 return ret;