2 * ao_sdl.c - libao2 SDLlib Audio Output Driver for MPlayer
4 * This driver is under the same license as MPlayer.
5 * (http://www.mplayerhq.hu)
7 * Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net)
9 * Thanks to Arpi for nice ringbuffer-code!
21 #include "audio_out.h"
22 #include "audio_out_internal.h"
23 #include "libaf/af_format.h"
25 #include "osdep/timer.h"
27 #include "libvo/fastmemcpy.h"
29 static ao_info_t info
=
31 "SDLlib audio output",
33 "Felix Buenemann <atmosfear@users.sourceforge.net>",
39 // turn this on if you want to use the slower SDL_MixAudio
40 #undef USE_SDL_INTERNAL_MIXER
42 // Samplesize used by the SDLlib AudioSpec struct
43 #if defined(WIN32) || defined(__AMIGAOS4__)
44 #define SAMPLESIZE 2048
46 #define SAMPLESIZE 1024
49 #define CHUNK_SIZE 4096
51 // This type of ring buffer may never fill up completely, at least
52 // one byte must always be unused.
53 // For performance reasons (alignment etc.) one whole chunk always stays
54 // empty, not only one byte.
55 #define BUFFSIZE ((NUM_CHUNKS + 1) * CHUNK_SIZE)
57 static unsigned char *buffer
;
59 // may only be modified by SDL's playback thread or while it is stopped
60 static volatile int read_pos
;
61 // may only be modified by mplayer's thread
62 static volatile int write_pos
;
63 #ifdef USE_SDL_INTERNAL_MIXER
64 static unsigned char volume
=SDL_MIX_MAXVOLUME
;
67 // may only be called by mplayer's thread
68 // return value may change between immediately following two calls,
69 // and the real number of free bytes might be larger!
70 static int buf_free(void) {
71 int free
= read_pos
- write_pos
- CHUNK_SIZE
;
72 if (free
< 0) free
+= BUFFSIZE
;
76 // may only be called by SDL's playback thread
77 // return value may change between immediately following two calls,
78 // and the real number of buffered bytes might be larger!
79 static int buf_used(void) {
80 int used
= write_pos
- read_pos
;
81 if (used
< 0) used
+= BUFFSIZE
;
85 static int write_buffer(unsigned char* data
,int len
){
86 int first_len
= BUFFSIZE
- write_pos
;
87 int free
= buf_free();
88 if (len
> free
) len
= free
;
89 if (first_len
> len
) first_len
= len
;
91 fast_memcpy (&buffer
[write_pos
], data
, first_len
);
92 if (len
> first_len
) { // we have to wrap around
93 // remaining part from beginning of buffer
94 fast_memcpy (buffer
, &data
[first_len
], len
- first_len
);
96 write_pos
= (write_pos
+ len
) % BUFFSIZE
;
100 static int read_buffer(unsigned char* data
,int len
){
101 int first_len
= BUFFSIZE
- read_pos
;
102 int buffered
= buf_used();
103 if (len
> buffered
) len
= buffered
;
104 if (first_len
> len
) first_len
= len
;
105 // till end of buffer
106 #ifdef USE_SDL_INTERNAL_MIXER
107 SDL_MixAudio (data
, &buffer
[read_pos
], first_len
, volume
);
109 fast_memcpy (data
, &buffer
[read_pos
], first_len
);
111 if (len
> first_len
) { // we have to wrap around
112 // remaining part from beginning of buffer
113 #ifdef USE_SDL_INTERNAL_MIXER
114 SDL_MixAudio (&data
[first_len
], buffer
, len
- first_len
, volume
);
116 fast_memcpy (&data
[first_len
], buffer
, len
- first_len
);
119 read_pos
= (read_pos
+ len
) % BUFFSIZE
;
123 // end ring buffer stuff
126 // to set/get/query special features/parameters
127 static int control(int cmd
,void *arg
){
128 #ifdef USE_SDL_INTERNAL_MIXER
130 case AOCONTROL_GET_VOLUME
:
132 ao_control_vol_t
* vol
= (ao_control_vol_t
*)arg
;
133 vol
->left
= vol
->right
= volume
* 100 / SDL_MIX_MAXVOLUME
;
136 case AOCONTROL_SET_VOLUME
:
139 ao_control_vol_t
* vol
= (ao_control_vol_t
*)arg
;
140 diff
= (vol
->left
+vol
->right
) / 2;
141 volume
= diff
* SDL_MIX_MAXVOLUME
/ 100;
146 return CONTROL_UNKNOWN
;
149 // SDL Callback function
150 void outputaudio(void *unused
, Uint8
*stream
, int len
) {
151 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME);
152 //if(!full_buffers) printf("SDL: Buffer underrun!\n");
154 read_buffer(stream
, len
);
155 //printf("SDL: Full Buffers: %i\n", full_buffers);
158 // open & setup audio device
159 // return: 1=success 0=fail
160 static int init(int rate
,int channels
,int format
,int flags
){
162 /* SDL Audio Specifications */
163 SDL_AudioSpec aspec
, obtained
;
165 /* Allocate ring-buffer memory */
166 buffer
= (unsigned char *) malloc(BUFFSIZE
);
168 mp_msg(MSGT_AO
,MSGL_INFO
,MSGTR_AO_SDL_INFO
, rate
, (channels
> 1) ? "Stereo" : "Mono", af_fmt2str_short(format
));
171 setenv("SDL_AUDIODRIVER", ao_subdevice
, 1);
172 mp_msg(MSGT_AO
,MSGL_INFO
,MSGTR_AO_SDL_DriverInfo
, ao_subdevice
);
175 ao_data
.channels
=channels
;
176 ao_data
.samplerate
=rate
;
177 ao_data
.format
=format
;
179 ao_data
.bps
=channels
*rate
;
180 if(format
!= AF_FORMAT_U8
&& format
!= AF_FORMAT_S8
)
183 /* The desired audio format (see SDL_AudioSpec) */
186 aspec
.format
= AUDIO_U8
;
188 case AF_FORMAT_S16_LE
:
189 aspec
.format
= AUDIO_S16LSB
;
191 case AF_FORMAT_S16_BE
:
192 aspec
.format
= AUDIO_S16MSB
;
195 aspec
.format
= AUDIO_S8
;
197 case AF_FORMAT_U16_LE
:
198 aspec
.format
= AUDIO_U16LSB
;
200 case AF_FORMAT_U16_BE
:
201 aspec
.format
= AUDIO_U16MSB
;
204 aspec
.format
= AUDIO_S16LSB
;
205 ao_data
.format
= AF_FORMAT_S16_LE
;
206 mp_msg(MSGT_AO
,MSGL_WARN
,MSGTR_AO_SDL_UnsupportedAudioFmt
, format
);
209 /* The desired audio frequency in samples-per-second. */
212 /* Number of channels (mono/stereo) */
213 aspec
.channels
= channels
;
215 /* 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 */
216 aspec
.samples
= SAMPLESIZE
;
218 /* 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:
219 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. */
220 aspec
.callback
= outputaudio
;
222 /* This pointer is passed as the first parameter to the callback function. */
223 aspec
.userdata
= NULL
;
225 /* initialize the SDL Audio system */
226 if (SDL_Init (SDL_INIT_AUDIO
/*|SDL_INIT_NOPARACHUTE*/)) {
227 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_SDL_CantInit
, SDL_GetError());
231 /* Open the audio device and start playing sound! */
232 if(SDL_OpenAudio(&aspec
, &obtained
) < 0) {
233 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_SDL_CantOpenAudio
, SDL_GetError());
237 /* did we got what we wanted ? */
238 ao_data
.channels
=obtained
.channels
;
239 ao_data
.samplerate
=obtained
.freq
;
241 switch(obtained
.format
) {
243 ao_data
.format
= AF_FORMAT_U8
;
246 ao_data
.format
= AF_FORMAT_S16_LE
;
249 ao_data
.format
= AF_FORMAT_S16_BE
;
252 ao_data
.format
= AF_FORMAT_S8
;
255 ao_data
.format
= AF_FORMAT_U16_LE
;
258 ao_data
.format
= AF_FORMAT_U16_BE
;
261 mp_msg(MSGT_AO
,MSGL_WARN
,MSGTR_AO_SDL_UnsupportedAudioFmt
, obtained
.format
);
265 mp_msg(MSGT_AO
,MSGL_V
,"SDL: buf size = %d\n",obtained
.size
);
266 ao_data
.buffersize
=obtained
.size
;
267 ao_data
.outburst
= CHUNK_SIZE
;
270 /* unsilence audio, if callback is ready */
276 // close audio device
277 static void uninit(int immed
){
278 mp_msg(MSGT_AO
,MSGL_V
,"SDL: Audio Subsystem shutting down!\n");
280 usec_sleep(get_delay() * 1000 * 1000);
282 SDL_QuitSubSystem(SDL_INIT_AUDIO
);
285 // stop playing and empty buffers (for seeking/pause)
286 static void reset(void){
288 //printf("SDL: reset called!\n");
291 /* Reset ring-buffer state */
297 // stop playing, keep buffers (for pause)
298 static void audio_pause(void)
301 //printf("SDL: audio_pause called!\n");
306 // resume playing, after audio_pause()
307 static void audio_resume(void)
309 //printf("SDL: audio_resume called!\n");
314 // return: how many bytes can be played without blocking
315 static int get_space(void){
319 // plays 'len' bytes of 'data'
320 // it should round it down to outburst*n
321 // return: number of bytes played
322 static int play(void* data
,int len
,int flags
){
324 if (!(flags
& AOPLAY_FINAL_CHUNK
))
325 len
= (len
/ao_data
.outburst
)*ao_data
.outburst
;
329 /* Audio locking prohibits call of outputaudio */
331 // copy audio stream into ring-buffer
332 ret
= write_buffer(data
, len
);
337 return write_buffer(data
, len
);
341 // return: delay in seconds between first and last sample in buffer
342 static float get_delay(void){
343 int buffered
= BUFFSIZE
- CHUNK_SIZE
- buf_free(); // could be less
344 return (float)(buffered
+ ao_data
.buffersize
)/(float)ao_data
.bps
;