1 /*****************************************************************************
2 * libvlc_audio.c: New libvlc audio control API
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
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 *****************************************************************************/
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>
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
)
51 input_thread_t
*p_input
= libvlc_get_input_thread( mp
);
55 aout_instance_t
* p_aout
= input_GetAout( p_input
);
56 vlc_object_release( p_input
);
58 libvlc_printerr( "No active audio output" );
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
,
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" ) )
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
);
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
);
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
;
122 free( p_actual
->psz_name
);
123 free( p_actual
->psz_description
);
125 p_actual
= p_before
->p_next
;
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
) )
138 var_SetString( mp
, "aout", psz_name
);
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
)
151 if( asprintf( &psz_config_name
, "%s-audio-device", psz_audio_output
) == -1 )
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
)
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
);
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
,
181 char *psz_config_name
;
182 if( !psz_audio_output
)
184 if( asprintf( &psz_config_name
, "%s-audio-device", psz_audio_output
) == -1 )
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
)
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
] );
210 return strdup( p_module_config
->ppsz_list
[i_device
] );
214 free( psz_config_name
);
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
,
225 char *psz_config_name
;
226 if( !psz_audio_output
)
228 if( asprintf( &psz_config_name
, "%s-audio-device", psz_audio_output
) == -1)
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
)
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
);
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
)
268 if( asprintf( &psz_config_name
, "%s-audio-device", psz_audio_output
) == -1 )
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
);
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
,
298 aout_instance_t
*p_aout
= GetAOut( mp
);
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" );
346 i_volume
= (i_volume
* AOUT_VOLUME_MAX
+ 100) / 200;
347 aout_VolumeSet( mp
, i_volume
);
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
);
359 if( !p_input_thread
)
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
;
388 if( !p_input_thread
)
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" );
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
)
407 var_FreeList( &val_list
, NULL
);
408 vlc_object_release( p_input_thread
);
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
;
422 if( !p_input_thread
)
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" );
433 newval
= val_list
.p_list
->p_values
[i_track
];
434 i_ret
= var_Set( p_input_thread
, "audio-es", newval
);
437 libvlc_printerr( "Audio track out of range" ); /* Race... */
444 var_FreeList( &val_list
, NULL
);
445 vlc_object_release( p_input_thread
);
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
);
458 int val
= var_GetInteger( p_aout
, "audio-channels" );
459 vlc_object_release( p_aout
);
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
);
474 if( var_SetInteger( p_aout
, "audio-channels", channel
) < 0 )
476 libvlc_printerr( "Audio channel out of range" );
479 vlc_object_release( p_aout
);
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
);
490 if( p_input_thread
!= NULL
)
492 val
= var_GetTime( p_input_thread
, "audio-delay" );
493 vlc_object_release( p_input_thread
);
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
);
505 if( p_input_thread
!= NULL
)
507 var_SetTime( p_input_thread
, "audio-delay", i_delay
);
508 vlc_object_release( p_input_thread
);