ao_pulse: support native mute control
[mplayer.git] / libao2 / ao_sdl.c
blob6ff8b83cb38b05eed338424c58d8b64d7fd825a1
1 /*
2 * SDLlib audio output driver for MPlayer
4 * Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net)
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * along with MPlayer; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "config.h"
28 #include "mp_msg.h"
30 #include "audio_out.h"
31 #include "audio_out_internal.h"
32 #include "libaf/af_format.h"
33 #ifdef CONFIG_SDL_SDL_H
34 #include <SDL/SDL.h>
35 #else
36 #include <SDL.h>
37 #endif
38 #include "osdep/timer.h"
40 #include "libavutil/fifo.h"
42 static const ao_info_t info =
44 "SDLlib audio output",
45 "sdl",
46 "Felix Buenemann <atmosfear@users.sourceforge.net>",
50 LIBAO_EXTERN(sdl)
52 // turn this on if you want to use the slower SDL_MixAudio
53 #undef USE_SDL_INTERNAL_MIXER
55 // Samplesize used by the SDLlib AudioSpec struct
56 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__AMIGAOS4__)
57 #define SAMPLESIZE 2048
58 #else
59 #define SAMPLESIZE 1024
60 #endif
62 #define CHUNK_SIZE 4096
63 #define NUM_CHUNKS 8
64 #define BUFFSIZE (NUM_CHUNKS * CHUNK_SIZE)
66 static AVFifoBuffer *buffer;
68 #ifdef USE_SDL_INTERNAL_MIXER
69 static unsigned char volume=SDL_MIX_MAXVOLUME;
70 #endif
72 static int write_buffer(unsigned char* data,int len){
73 int free = av_fifo_space(buffer);
74 if (len > free) len = free;
75 return av_fifo_generic_write(buffer, data, len, NULL);
78 #ifdef USE_SDL_INTERNAL_MIXER
79 static void mix_audio(void *dst, void *src, int len) {
80 SDL_MixAudio(dst, src, len, volume);
82 #endif
84 static int read_buffer(unsigned char* data,int len){
85 int buffered = av_fifo_size(buffer);
86 if (len > buffered) len = buffered;
87 #ifdef USE_SDL_INTERNAL_MIXER
88 av_fifo_generic_read(buffer, data, len, mix_audio);
89 #else
90 av_fifo_generic_read(buffer, data, len, NULL);
91 #endif
92 return len;
95 // end ring buffer stuff
98 // to set/get/query special features/parameters
99 static int control(int cmd,void *arg){
100 #ifdef USE_SDL_INTERNAL_MIXER
101 switch (cmd) {
102 case AOCONTROL_GET_VOLUME:
104 ao_control_vol_t* vol = (ao_control_vol_t*)arg;
105 vol->left = vol->right = volume * 100 / SDL_MIX_MAXVOLUME;
106 return CONTROL_OK;
108 case AOCONTROL_SET_VOLUME:
110 int diff;
111 ao_control_vol_t* vol = (ao_control_vol_t*)arg;
112 diff = (vol->left+vol->right) / 2;
113 volume = diff * SDL_MIX_MAXVOLUME / 100;
114 return CONTROL_OK;
117 #endif
118 return CONTROL_UNKNOWN;
121 // SDL Callback function
122 static void outputaudio(void *unused, Uint8 *stream, int len)
124 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME);
125 //if(!full_buffers) printf("SDL: Buffer underrun!\n");
127 read_buffer(stream, len);
128 //printf("SDL: Full Buffers: %i\n", full_buffers);
131 // open & setup audio device
132 // return: 1=success 0=fail
133 static int init(int rate,int channels,int format,int flags){
135 /* SDL Audio Specifications */
136 SDL_AudioSpec aspec, obtained;
138 global_ao->no_persistent_volume = true;
140 /* Allocate ring-buffer memory */
141 buffer = av_fifo_alloc(BUFFSIZE);
143 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO SDL] Samplerate: %iHz Channels: %s Format %s\n", rate, (channels > 1) ? "Stereo" : "Mono", af_fmt2str_short(format));
145 if(ao_subdevice) {
146 setenv("SDL_AUDIODRIVER", ao_subdevice, 1);
147 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO SDL] using %s audio driver.\n", ao_subdevice);
150 ao_data.channels=channels;
151 ao_data.samplerate=rate;
152 ao_data.format=format;
154 ao_data.bps=channels*rate;
155 if(format != AF_FORMAT_U8 && format != AF_FORMAT_S8)
156 ao_data.bps*=2;
158 /* The desired audio format (see SDL_AudioSpec) */
159 switch(format) {
160 case AF_FORMAT_U8:
161 aspec.format = AUDIO_U8;
162 break;
163 case AF_FORMAT_S16_LE:
164 aspec.format = AUDIO_S16LSB;
165 break;
166 case AF_FORMAT_S16_BE:
167 aspec.format = AUDIO_S16MSB;
168 break;
169 case AF_FORMAT_S8:
170 aspec.format = AUDIO_S8;
171 break;
172 case AF_FORMAT_U16_LE:
173 aspec.format = AUDIO_U16LSB;
174 break;
175 case AF_FORMAT_U16_BE:
176 aspec.format = AUDIO_U16MSB;
177 break;
178 default:
179 aspec.format = AUDIO_S16LSB;
180 ao_data.format = AF_FORMAT_S16_LE;
181 mp_tmsg(MSGT_AO,MSGL_WARN,"[AO SDL] Unsupported audio format: 0x%x.\n", format);
184 /* The desired audio frequency in samples-per-second. */
185 aspec.freq = rate;
187 /* Number of channels (mono/stereo) */
188 aspec.channels = channels;
190 /* The desired size of the audio buffer in samples. This number should be a power of two, and may be adjusted by the audio driver to a value more suitable for the hardware. Good values seem to range between 512 and 8192 inclusive, depending on the application and CPU speed. Smaller values yield faster response time, but can lead to underflow if the application is doing heavy processing and cannot fill the audio buffer in time. A stereo sample consists of both right and left channels in LR ordering. Note that the number of samples is directly related to time by the following formula: ms = (samples*1000)/freq */
191 aspec.samples = SAMPLESIZE;
193 /* This should be set to a function that will be called when the audio device is ready for more data. It is passed a pointer to the audio buffer, and the length in bytes of the audio buffer. This function usually runs in a separate thread, and so you should protect data structures that it accesses by calling SDL_LockAudio and SDL_UnlockAudio in your code. The callback prototype is:
194 void callback(void *userdata, Uint8 *stream, int len); userdata is the pointer stored in userdata field of the SDL_AudioSpec. stream is a pointer to the audio buffer you want to fill with information and len is the length of the audio buffer in bytes. */
195 aspec.callback = outputaudio;
197 /* This pointer is passed as the first parameter to the callback function. */
198 aspec.userdata = NULL;
200 /* initialize the SDL Audio system */
201 if (SDL_Init (SDL_INIT_AUDIO/*|SDL_INIT_NOPARACHUTE*/)) {
202 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO SDL] SDL Audio initialization failed: %s\n", SDL_GetError());
203 return 0;
206 /* Open the audio device and start playing sound! */
207 if(SDL_OpenAudio(&aspec, &obtained) < 0) {
208 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO SDL] Unable to open audio: %s\n", SDL_GetError());
209 return 0;
212 /* did we got what we wanted ? */
213 ao_data.channels=obtained.channels;
214 ao_data.samplerate=obtained.freq;
216 switch(obtained.format) {
217 case AUDIO_U8 :
218 ao_data.format = AF_FORMAT_U8;
219 break;
220 case AUDIO_S16LSB :
221 ao_data.format = AF_FORMAT_S16_LE;
222 break;
223 case AUDIO_S16MSB :
224 ao_data.format = AF_FORMAT_S16_BE;
225 break;
226 case AUDIO_S8 :
227 ao_data.format = AF_FORMAT_S8;
228 break;
229 case AUDIO_U16LSB :
230 ao_data.format = AF_FORMAT_U16_LE;
231 break;
232 case AUDIO_U16MSB :
233 ao_data.format = AF_FORMAT_U16_BE;
234 break;
235 default:
236 mp_tmsg(MSGT_AO,MSGL_WARN,"[AO SDL] Unsupported audio format: 0x%x.\n", obtained.format);
237 return 0;
240 mp_msg(MSGT_AO,MSGL_V,"SDL: buf size = %d\n",obtained.size);
241 ao_data.buffersize=obtained.size;
242 ao_data.outburst = CHUNK_SIZE;
244 /* unsilence audio, if callback is ready */
245 SDL_PauseAudio(0);
247 return 1;
250 // close audio device
251 static void uninit(int immed){
252 mp_msg(MSGT_AO,MSGL_V,"SDL: Audio Subsystem shutting down!\n");
253 if (!immed)
254 usec_sleep(get_delay() * 1000 * 1000);
255 SDL_CloseAudio();
256 SDL_QuitSubSystem(SDL_INIT_AUDIO);
257 av_fifo_free(buffer);
260 // stop playing and empty buffers (for seeking/pause)
261 static void reset(void){
263 //printf("SDL: reset called!\n");
265 SDL_PauseAudio(1);
266 /* Reset ring-buffer state */
267 av_fifo_reset(buffer);
268 SDL_PauseAudio(0);
271 // stop playing, keep buffers (for pause)
272 static void audio_pause(void)
275 //printf("SDL: audio_pause called!\n");
276 SDL_PauseAudio(1);
280 // resume playing, after audio_pause()
281 static void audio_resume(void)
283 //printf("SDL: audio_resume called!\n");
284 SDL_PauseAudio(0);
288 // return: how many bytes can be played without blocking
289 static int get_space(void){
290 return av_fifo_space(buffer);
293 // plays 'len' bytes of 'data'
294 // it should round it down to outburst*n
295 // return: number of bytes played
296 static int play(void* data,int len,int flags){
298 if (!(flags & AOPLAY_FINAL_CHUNK))
299 len = (len/ao_data.outburst)*ao_data.outburst;
300 #if 0
301 int ret;
303 /* Audio locking prohibits call of outputaudio */
304 SDL_LockAudio();
305 // copy audio stream into ring-buffer
306 ret = write_buffer(data, len);
307 SDL_UnlockAudio();
309 return ret;
310 #else
311 return write_buffer(data, len);
312 #endif
315 // return: delay in seconds between first and last sample in buffer
316 static float get_delay(void){
317 int buffered = av_fifo_size(buffer); // could be less
318 return (float)(buffered + ao_data.buffersize)/(float)ao_data.bps;