]> git.sesse.net Git - vlc/blob - src/control/audio.c
libvlc: Rename input to media_instance. And add the possibility to create a medi_inst...
[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  * Remember to release the returned aout_instance_t since it is locked at
33  * the end of this function.
34  */
35 static aout_instance_t *GetAOut( libvlc_instance_t *p_instance,
36                                  libvlc_exception_t *p_exception )
37 {
38     aout_instance_t * p_aout = NULL;
39
40     p_aout = vlc_object_find( p_instance->p_libvlc_int, VLC_OBJECT_AOUT, FIND_CHILD );
41     if( !p_aout )
42     {
43         libvlc_exception_raise( p_exception, "No active audio output" );
44         return NULL;
45     }
46
47     return p_aout;
48 }
49
50
51 /*****************************************************************************
52  * libvlc_audio_get_mute : Get the volume state, true if muted
53  *****************************************************************************/
54 void libvlc_audio_toggle_mute( libvlc_instance_t *p_instance,
55                                libvlc_exception_t *p_e )
56 {
57     aout_VolumeMute( p_instance->p_libvlc_int, NULL );
58 }
59
60 vlc_bool_t libvlc_audio_get_mute( libvlc_instance_t *p_instance,
61                                   libvlc_exception_t *p_e )
62 {
63     /*
64      * If the volume level is 0, then the channel is muted
65      */
66     audio_volume_t i_volume;
67
68     i_volume = libvlc_audio_get_volume(p_instance, p_e);
69     if ( i_volume == 0 )
70         return VLC_TRUE;
71     return VLC_FALSE;
72 }
73
74 void libvlc_audio_set_mute( libvlc_instance_t *p_instance, vlc_bool_t mute,
75                             libvlc_exception_t *p_e )
76 {
77     if ( mute ^ libvlc_audio_get_mute( p_instance, p_e ) )
78     {
79         aout_VolumeMute( p_instance->p_libvlc_int, NULL );
80     }
81 }
82
83 /*****************************************************************************
84  * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
85  *****************************************************************************/
86 int libvlc_audio_get_volume( libvlc_instance_t *p_instance,
87                              libvlc_exception_t *p_e )
88 {
89     audio_volume_t i_volume;
90
91     aout_VolumeGet( p_instance->p_libvlc_int, &i_volume );
92
93     return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
94 }
95
96
97 /*****************************************************************************
98  * libvlc_audio_set_volume : Set the current volume
99  *****************************************************************************/
100 void libvlc_audio_set_volume( libvlc_instance_t *p_instance, int i_volume,
101                               libvlc_exception_t *p_e )
102 {
103     if( i_volume >= 0 && i_volume <= 200 )
104     {
105         i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;
106
107         aout_VolumeSet( p_instance->p_libvlc_int, i_volume );
108     }
109     else
110     {
111         libvlc_exception_raise( p_e, "Volume out of range" );
112     }
113 }
114
115 /*****************************************************************************
116  * libvlc_audio_get_track : Get the current audio track
117  *****************************************************************************/
118 int libvlc_audio_get_track( libvlc_media_instance_t *p_mi,
119                             libvlc_exception_t *p_e )
120 {
121     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
122     vlc_value_t val_list;
123     vlc_value_t val;
124     int i_track = -1;
125     int i_ret = -1;
126     int i;
127
128     if( !p_input_thread )
129         return -1;
130
131     i_ret = var_Get( p_input_thread, "audio-es", &val );
132     if( i_ret < 0 )
133     {
134         libvlc_exception_raise( p_e, "Getting Audio track information failed" );
135         vlc_object_release( p_input_thread );
136         return i_ret;
137     }
138
139     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
140     for( i = 0; i < val_list.p_list->i_count; i++ )
141     {
142         vlc_value_t track_val = val_list.p_list->p_values[i];
143         if( track_val.i_int == val.i_int )
144         {
145             i_track = i;
146             break;
147        }
148     }
149     vlc_object_release( p_input_thread );
150     return i_track;
151 }
152
153 /*****************************************************************************
154  * libvlc_audio_set_track : Set the current audio track
155  *****************************************************************************/
156 void libvlc_audio_set_track( libvlc_media_instance_t *p_mi, int i_track,
157                              libvlc_exception_t *p_e )
158 {
159     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
160     vlc_value_t val_list;
161     int i_ret = -1;
162     int i;
163
164     if( !p_input_thread )
165         return;
166
167     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
168     for( i = 0; i < val_list.p_list->i_count; i++ )
169     {
170         vlc_value_t val = val_list.p_list->p_values[i];
171         if( i_track == i )
172         {
173             i_ret = var_Set( p_input_thread, "audio-es", val );
174             if( i_ret < 0 )
175             {
176                 libvlc_exception_raise( p_e, "Setting audio track failed" );
177             }
178             vlc_object_release( p_input_thread );
179             return;
180         }
181     }
182     libvlc_exception_raise( p_e, "Audio track out of range" );
183     vlc_object_release( p_input_thread );
184 }
185
186 /*****************************************************************************
187  * libvlc_audio_get_channel : Get the current audio channel
188  *****************************************************************************/
189 int libvlc_audio_get_channel( libvlc_instance_t *p_instance,
190                                 libvlc_exception_t *p_e )
191 {
192     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
193     if( p_aout )
194     {
195         vlc_value_t val;
196
197         var_Get( p_aout, "audio-channels", &val );
198         vlc_object_release( p_aout );
199         return val.i_int;
200     }
201     return -1;
202 }
203
204 /*****************************************************************************
205  * libvlc_audio_set_channel : Set the current audio channel
206  *****************************************************************************/
207 void libvlc_audio_set_channel( libvlc_instance_t *p_instance, int i_channel,
208                                libvlc_exception_t *p_e )
209 {
210     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
211     if( p_aout )
212     {
213         vlc_value_t val;
214         int i_ret = -1;
215
216         val.i_int = i_channel;
217         switch( i_channel )
218         {
219             case AOUT_VAR_CHAN_RSTEREO:
220             case AOUT_VAR_CHAN_STEREO:
221             case AOUT_VAR_CHAN_LEFT:
222             case AOUT_VAR_CHAN_RIGHT:
223             case AOUT_VAR_CHAN_DOLBYS:
224                 i_ret = var_Set( p_aout, "audio-channels", val );
225                 if( i_ret < 0 )
226                 {
227                     libvlc_exception_raise( p_e, "Failed setting audio channel" );
228                     vlc_object_release( p_aout );
229                     return;
230                 }
231                 vlc_object_release( p_aout );
232                 return; /* Found */
233             default:
234                 libvlc_exception_raise( p_e, "Audio channel out of range" );
235                 break;
236         }
237         vlc_object_release( p_aout );
238     }
239 }