]> git.sesse.net Git - vlc/blob - src/control/audio.c
libvlc: description and count of tracks, titles, chapters and subtitles
[vlc] / src / control / audio.c
1 /*****************************************************************************
2  * libvlc_audio.c: New libvlc audio control API
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Filippo Carone <filippo@carone.org>
8  *          Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27
28 #include <vlc_input.h>
29 #include <vlc_aout.h>
30
31
32 /*
33  * Remember to release the returned aout_instance_t since it is locked at
34  * the end of this function.
35  */
36 static aout_instance_t *GetAOut( libvlc_instance_t *p_instance,
37                                  libvlc_exception_t *p_exception )
38 {
39     aout_instance_t * p_aout = NULL;
40
41     p_aout = vlc_object_find( p_instance->p_libvlc_int, VLC_OBJECT_AOUT, FIND_CHILD );
42     if( !p_aout )
43     {
44         libvlc_exception_raise( p_exception, "No active audio output" );
45         return NULL;
46     }
47
48     return p_aout;
49 }
50
51
52 /*****************************************************************************
53  * libvlc_audio_get_mute : Get the volume state, true if muted
54  *****************************************************************************/
55 void libvlc_audio_toggle_mute( libvlc_instance_t *p_instance,
56                                libvlc_exception_t *p_e )
57 {
58     VLC_UNUSED(p_e);
59
60     aout_VolumeMute( p_instance->p_libvlc_int, NULL );
61 }
62
63 int libvlc_audio_get_mute( libvlc_instance_t *p_instance,
64                            libvlc_exception_t *p_e )
65 {
66     /*
67      * If the volume level is 0, then the channel is muted
68      */
69     audio_volume_t i_volume;
70
71     i_volume = libvlc_audio_get_volume(p_instance, p_e);
72     if ( i_volume == 0 )
73         return true;
74     return false;
75 }
76
77 void libvlc_audio_set_mute( libvlc_instance_t *p_instance, int mute,
78                             libvlc_exception_t *p_e )
79 {
80     if ( mute ^ libvlc_audio_get_mute( p_instance, p_e ) )
81     {
82         aout_VolumeMute( p_instance->p_libvlc_int, NULL );
83     }
84 }
85
86 /*****************************************************************************
87  * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
88  *****************************************************************************/
89 int libvlc_audio_get_volume( libvlc_instance_t *p_instance,
90                              libvlc_exception_t *p_e )
91 {
92     VLC_UNUSED(p_e);
93
94     audio_volume_t i_volume;
95
96     aout_VolumeGet( p_instance->p_libvlc_int, &i_volume );
97
98     return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
99 }
100
101
102 /*****************************************************************************
103  * libvlc_audio_set_volume : Set the current volume
104  *****************************************************************************/
105 void libvlc_audio_set_volume( libvlc_instance_t *p_instance, int i_volume,
106                               libvlc_exception_t *p_e )
107 {
108     if( i_volume >= 0 && i_volume <= 200 )
109     {
110         i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;
111
112         aout_VolumeSet( p_instance->p_libvlc_int, i_volume );
113     }
114     else
115     {
116         libvlc_exception_raise( p_e, "Volume out of range" );
117     }
118 }
119
120 /*****************************************************************************
121  * libvlc_audio_get_track_count : Get the number of available audio tracks
122  *****************************************************************************/
123 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi,
124                                   libvlc_exception_t *p_e )
125 {
126     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
127     vlc_value_t val_list;
128
129     if( !p_input_thread )
130         return -1;
131
132     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
133     vlc_object_release( p_input_thread );
134     return val_list.p_list->i_count;
135 }
136
137 /*****************************************************************************
138  * libvlc_audio_get_track_description : Get the description of available audio tracks
139  *****************************************************************************/
140 libvlc_track_description_t *
141         libvlc_audio_get_track_description( libvlc_media_player_t *p_mi,
142                                             libvlc_exception_t *p_e )
143 {
144     return libvlc_get_track_description( p_mi, "audio-es", p_e);
145 }
146
147 /*****************************************************************************
148  * libvlc_audio_get_track : Get the current audio track
149  *****************************************************************************/
150 int libvlc_audio_get_track( libvlc_media_player_t *p_mi,
151                             libvlc_exception_t *p_e )
152 {
153     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
154     vlc_value_t val_list;
155     vlc_value_t val;
156     int i_track = -1;
157     int i_ret = -1;
158     int i;
159
160     if( !p_input_thread )
161         return -1;
162
163     i_ret = var_Get( p_input_thread, "audio-es", &val );
164     if( i_ret < 0 )
165     {
166         libvlc_exception_raise( p_e, "Getting Audio track information failed" );
167         vlc_object_release( p_input_thread );
168         return i_ret;
169     }
170
171     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
172     for( i = 0; i < val_list.p_list->i_count; i++ )
173     {
174         vlc_value_t track_val = val_list.p_list->p_values[i];
175         if( track_val.i_int == val.i_int )
176         {
177             i_track = i;
178             break;
179        }
180     }
181     vlc_object_release( p_input_thread );
182     return i_track;
183 }
184
185 /*****************************************************************************
186  * libvlc_audio_set_track : Set the current audio track
187  *****************************************************************************/
188 void libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track,
189                              libvlc_exception_t *p_e )
190 {
191     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
192     vlc_value_t val_list;
193     vlc_value_t newval;
194     int i_ret = -1;
195
196     if( !p_input_thread )
197         return;
198
199     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
200     if( (i_track < 0) && (i_track > val_list.p_list->i_count) )
201     {
202         libvlc_exception_raise( p_e, "Audio track out of range" );
203         vlc_object_release( p_input_thread );
204         return;
205     }
206
207     newval = val_list.p_list->p_values[i_track];
208     i_ret = var_Set( p_input_thread, "audio-es", newval );
209     if( i_ret < 0 )
210     {
211         libvlc_exception_raise( p_e, "Setting audio track failed" );
212     }
213     vlc_object_release( p_input_thread );
214 }
215
216 /*****************************************************************************
217  * libvlc_audio_get_channel : Get the current audio channel
218  *****************************************************************************/
219 int libvlc_audio_get_channel( libvlc_instance_t *p_instance,
220                                 libvlc_exception_t *p_e )
221 {
222     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
223     if( p_aout )
224     {
225         vlc_value_t val;
226
227         var_Get( p_aout, "audio-channels", &val );
228         vlc_object_release( p_aout );
229         return val.i_int;
230     }
231     return -1;
232 }
233
234 /*****************************************************************************
235  * libvlc_audio_set_channel : Set the current audio channel
236  *****************************************************************************/
237 void libvlc_audio_set_channel( libvlc_instance_t *p_instance, int i_channel,
238                                libvlc_exception_t *p_e )
239 {
240     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
241     if( p_aout )
242     {
243         vlc_value_t val;
244         int i_ret = -1;
245
246         val.i_int = i_channel;
247         switch( i_channel )
248         {
249             case AOUT_VAR_CHAN_RSTEREO:
250             case AOUT_VAR_CHAN_STEREO:
251             case AOUT_VAR_CHAN_LEFT:
252             case AOUT_VAR_CHAN_RIGHT:
253             case AOUT_VAR_CHAN_DOLBYS:
254                 i_ret = var_Set( p_aout, "audio-channels", val );
255                 if( i_ret < 0 )
256                 {
257                     libvlc_exception_raise( p_e, "Failed setting audio channel" );
258                     vlc_object_release( p_aout );
259                     return;
260                 }
261                 vlc_object_release( p_aout );
262                 return; /* Found */
263             default:
264                 libvlc_exception_raise( p_e, "Audio channel out of range" );
265                 break;
266         }
267         vlc_object_release( p_aout );
268     }
269 }