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