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
30 #include "audio_out.h"
31 #include "audio_out_internal.h"
32 #include "libaf/af_format.h"
33 #ifdef CONFIG_SDL_SDL_H
38 #include "osdep/timer.h"
40 #include "libavutil/fifo.h"
42 static const ao_info_t info
=
44 "SDLlib audio output",
46 "Felix Buenemann <atmosfear@users.sourceforge.net>",
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
59 #define SAMPLESIZE 1024
62 #define CHUNK_SIZE 4096
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
;
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
);
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
);
90 av_fifo_generic_read(buffer
, data
, len
, NULL
);
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
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
;
108 case AOCONTROL_SET_VOLUME
:
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;
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
));
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
)
158 /* The desired audio format (see SDL_AudioSpec) */
161 aspec
.format
= AUDIO_U8
;
163 case AF_FORMAT_S16_LE
:
164 aspec
.format
= AUDIO_S16LSB
;
166 case AF_FORMAT_S16_BE
:
167 aspec
.format
= AUDIO_S16MSB
;
170 aspec
.format
= AUDIO_S8
;
172 case AF_FORMAT_U16_LE
:
173 aspec
.format
= AUDIO_U16LSB
;
175 case AF_FORMAT_U16_BE
:
176 aspec
.format
= AUDIO_U16MSB
;
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. */
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());
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());
212 /* did we got what we wanted ? */
213 ao_data
.channels
=obtained
.channels
;
214 ao_data
.samplerate
=obtained
.freq
;
216 switch(obtained
.format
) {
218 ao_data
.format
= AF_FORMAT_U8
;
221 ao_data
.format
= AF_FORMAT_S16_LE
;
224 ao_data
.format
= AF_FORMAT_S16_BE
;
227 ao_data
.format
= AF_FORMAT_S8
;
230 ao_data
.format
= AF_FORMAT_U16_LE
;
233 ao_data
.format
= AF_FORMAT_U16_BE
;
236 mp_tmsg(MSGT_AO
,MSGL_WARN
,"[AO SDL] Unsupported audio format: 0x%x.\n", obtained
.format
);
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 */
250 // close audio device
251 static void uninit(int immed
){
252 mp_msg(MSGT_AO
,MSGL_V
,"SDL: Audio Subsystem shutting down!\n");
254 usec_sleep(get_delay() * 1000 * 1000);
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");
266 /* Reset ring-buffer state */
267 av_fifo_reset(buffer
);
271 // stop playing, keep buffers (for pause)
272 static void audio_pause(void)
275 //printf("SDL: audio_pause called!\n");
280 // resume playing, after audio_pause()
281 static void audio_resume(void)
283 //printf("SDL: audio_resume called!\n");
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
;
303 /* Audio locking prohibits call of outputaudio */
305 // copy audio stream into ring-buffer
306 ret
= write_buffer(data
, len
);
311 return write_buffer(data
, len
);
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
;