2001-06-03 20:54:46 +00:00
/*
* ao_sdl . c - libao2 SDLlib Audio Output Driver for MPlayer
*
* This driver is under the same license as MPlayer .
2004-01-05 21:49:23 +00:00
* ( http : //www.mplayerhq.hu)
2001-06-03 20:54:46 +00:00
*
* Copyleft 2001 by Felix B <EFBFBD> nemann ( atmosfear @ users . sf . net )
*
* Thanks to Arpi for nice ringbuffer - code !
*
*/
2001-06-03 10:48:36 +00:00
# include <stdio.h>
2001-06-05 18:40:44 +00:00
# include <stdlib.h>
2004-04-01 19:33:58 +00:00
# include <string.h>
2001-06-03 10:48:36 +00:00
2004-12-07 02:24:15 +00:00
# include "config.h"
# include "mp_msg.h"
# include "help_mp.h"
2002-11-01 17:46:45 +00:00
2001-06-03 10:48:36 +00:00
# include "audio_out.h"
# include "audio_out_internal.h"
2004-12-27 17:30:15 +00:00
# include "libaf/af_format.h"
2002-05-25 11:08:53 +00:00
# include <SDL.h>
2004-04-01 19:33:58 +00:00
# include "osdep/timer.h"
2001-06-03 10:48:36 +00:00
2004-12-07 02:24:15 +00:00
# include "libvo/fastmemcpy.h"
2001-06-03 20:54:46 +00:00
2001-06-03 10:48:36 +00:00
static ao_info_t info =
{
" SDLlib audio output " ,
" sdl " ,
" Felix Buenemann <atmosfear@users.sourceforge.net> " ,
" "
} ;
LIBAO_EXTERN ( sdl )
2004-06-26 09:21:07 +00:00
// turn this on if you want to use the slower SDL_MixAudio
2004-07-28 12:17:50 +00:00
# undef USE_SDL_INTERNAL_MIXER
2004-06-26 09:21:07 +00:00
2001-06-03 20:54:46 +00:00
// Samplesize used by the SDLlib AudioSpec struct
2004-04-03 14:39:16 +00:00
# ifdef WIN32
2004-03-13 16:25:08 +00:00
# define SAMPLESIZE 2048
2004-04-03 14:39:16 +00:00
# else
# define SAMPLESIZE 1024
# endif
2001-06-03 20:54:46 +00:00
2004-10-31 21:02:47 +00:00
# define CHUNK_SIZE 4096
# define NUM_CHUNKS 8
// This type of ring buffer may never fill up completely, at least
// one byte must always be unused.
// For performance reasons (alignment etc.) one whole chunk always stays
// empty, not only one byte.
# define BUFFSIZE ((NUM_CHUNKS + 1) * CHUNK_SIZE)
static unsigned char * buffer ;
// may only be modified by SDL's playback thread or while it is stopped
static volatile int read_pos ;
// may only be modified by mplayer's thread
static volatile int write_pos ;
2004-06-26 09:21:07 +00:00
# ifdef USE_SDL_INTERNAL_MIXER
2004-04-01 19:33:58 +00:00
static unsigned char volume = SDL_MIX_MAXVOLUME ;
2004-06-26 09:21:07 +00:00
# endif
2001-06-03 20:54:46 +00:00
2004-10-31 21:02:47 +00:00
// may only be called by mplayer's thread
// return value may change between immediately following two calls,
// and the real number of free bytes might be larger!
static int buf_free ( ) {
int free = read_pos - write_pos - CHUNK_SIZE ;
if ( free < 0 ) free + = BUFFSIZE ;
return free ;
}
// may only be called by SDL's playback thread
// return value may change between immediately following two calls,
// and the real number of buffered bytes might be larger!
static int buf_used ( ) {
int used = write_pos - read_pos ;
if ( used < 0 ) used + = BUFFSIZE ;
return used ;
}
2001-06-05 18:40:44 +00:00
2001-06-03 20:54:46 +00:00
static int write_buffer ( unsigned char * data , int len ) {
2004-10-31 21:02:47 +00:00
int first_len = BUFFSIZE - write_pos ;
int free = buf_free ( ) ;
if ( len > free ) len = free ;
if ( first_len > len ) first_len = len ;
// till end of buffer
memcpy ( & buffer [ write_pos ] , data , first_len ) ;
if ( len > first_len ) { // we have to wrap around
// remaining part from beginning of buffer
memcpy ( buffer , & data [ first_len ] , len - first_len ) ;
2001-06-03 20:54:46 +00:00
}
2004-10-31 21:02:47 +00:00
write_pos = ( write_pos + len ) % BUFFSIZE ;
return len ;
2001-06-03 20:54:46 +00:00
}
static int read_buffer ( unsigned char * data , int len ) {
2004-10-31 21:02:47 +00:00
int first_len = BUFFSIZE - read_pos ;
int buffered = buf_used ( ) ;
if ( len > buffered ) len = buffered ;
if ( first_len > len ) first_len = len ;
// till end of buffer
2004-06-26 09:21:07 +00:00
# ifdef USE_SDL_INTERNAL_MIXER
2004-10-31 21:02:47 +00:00
SDL_MixAudio ( data , & buffer [ read_pos ] , first_len , volume ) ;
2004-06-26 09:21:07 +00:00
# else
2004-10-31 21:02:47 +00:00
memcpy ( data , & buffer [ read_pos ] , first_len ) ;
# endif
if ( len > first_len ) { // we have to wrap around
// remaining part from beginning of buffer
# ifdef USE_SDL_INTERNAL_MIXER
SDL_MixAudio ( & data [ first_len ] , buffer , len - first_len , volume ) ;
# else
memcpy ( & data [ first_len ] , buffer , len - first_len ) ;
2004-06-26 09:21:07 +00:00
# endif
2001-06-03 20:54:46 +00:00
}
2004-10-31 21:02:47 +00:00
read_pos = ( read_pos + len ) % BUFFSIZE ;
return len ;
2001-06-03 20:54:46 +00:00
}
// end ring buffer stuff
2001-06-03 10:48:36 +00:00
2004-03-15 05:07:46 +00:00
# if defined(__MINGW32__) || defined(HPUX) || defined(sgi) || (defined(sun) && defined(__svr4__))
2004-03-13 16:25:08 +00:00
/* setenv is missing on win32, solaris, IRIX and HPUX */
2001-06-05 18:40:44 +00:00
static void setenv ( const char * name , const char * val , int _xx )
{
int len = strlen ( name ) + strlen ( val ) + 2 ;
char * env = malloc ( len ) ;
if ( env ! = NULL ) {
strcpy ( env , name ) ;
strcat ( env , " = " ) ;
strcat ( env , val ) ;
putenv ( env ) ;
}
}
# endif
2001-06-03 10:48:36 +00:00
// to set/get/query special features/parameters
2003-03-21 16:42:50 +00:00
static int control ( int cmd , void * arg ) {
2004-06-26 09:21:07 +00:00
# ifdef USE_SDL_INTERNAL_MIXER
2002-05-25 11:08:53 +00:00
switch ( cmd ) {
case AOCONTROL_GET_VOLUME :
{
ao_control_vol_t * vol = ( ao_control_vol_t * ) arg ;
2004-04-01 19:33:58 +00:00
vol - > left = vol - > right = volume * 100 / SDL_MIX_MAXVOLUME ;
2002-05-25 11:08:53 +00:00
return CONTROL_OK ;
}
case AOCONTROL_SET_VOLUME :
{
2004-04-01 19:33:58 +00:00
int diff ;
2002-05-25 11:08:53 +00:00
ao_control_vol_t * vol = ( ao_control_vol_t * ) arg ;
diff = ( vol - > left + vol - > right ) / 2 ;
2004-04-01 19:33:58 +00:00
volume = diff * SDL_MIX_MAXVOLUME / 100 ;
2002-05-25 11:08:53 +00:00
return CONTROL_OK ;
}
}
2004-06-26 09:21:07 +00:00
# endif
return CONTROL_UNKNOWN ;
2001-06-03 10:48:36 +00:00
}
2001-06-03 20:54:46 +00:00
// SDL Callback function
void outputaudio ( void * unused , Uint8 * stream , int len ) {
//SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME);
2001-06-09 07:36:32 +00:00
//if(!full_buffers) printf("SDL: Buffer underrun!\n");
2001-06-03 20:54:46 +00:00
read_buffer ( stream , len ) ;
2001-06-09 07:36:32 +00:00
//printf("SDL: Full Buffers: %i\n", full_buffers);
2001-06-03 10:48:36 +00:00
}
// open & setup audio device
// return: 1=success 0=fail
static int init ( int rate , int channels , int format , int flags ) {
2001-06-03 20:54:46 +00:00
/* SDL Audio Specifications */
2002-10-25 10:27:20 +00:00
SDL_AudioSpec aspec , obtained ;
2001-06-03 20:54:46 +00:00
/* Allocate ring-buffer memory */
2004-10-31 21:02:47 +00:00
buffer = ( unsigned char * ) malloc ( BUFFSIZE ) ;
2001-06-03 20:54:46 +00:00
2004-12-28 19:11:14 +00:00
mp_msg ( MSGT_AO , MSGL_INFO , MSGTR_AO_SDL_INFO , rate , ( channels > 1 ) ? " Stereo " : " Mono " , af_fmt2str_short ( format ) ) ;
2001-06-04 00:27:55 +00:00
2001-06-21 22:48:34 +00:00
if ( ao_subdevice ) {
setenv ( " SDL_AUDIODRIVER " , ao_subdevice , 1 ) ;
2004-09-18 20:31:28 +00:00
mp_msg ( MSGT_AO , MSGL_INFO , MSGTR_AO_SDL_DriverInfo , ao_subdevice ) ;
2001-06-21 22:48:34 +00:00
}
2001-11-24 05:21:22 +00:00
2002-10-07 20:01:28 +00:00
ao_data . channels = channels ;
ao_data . samplerate = rate ;
ao_data . format = format ;
2001-11-26 11:30:35 +00:00
ao_data . bps = channels * rate ;
2004-12-27 17:30:15 +00:00
if ( format ! = AF_FORMAT_U8 & & format ! = AF_FORMAT_S8 )
2001-11-24 05:21:22 +00:00
ao_data . bps * = 2 ;
2001-06-03 10:48:36 +00:00
2001-06-09 07:36:32 +00:00
/* The desired audio format (see SDL_AudioSpec) */
switch ( format ) {
2004-12-27 17:30:15 +00:00
case AF_FORMAT_U8 :
2001-06-09 07:36:32 +00:00
aspec . format = AUDIO_U8 ;
break ;
2004-12-27 17:30:15 +00:00
case AF_FORMAT_S16_LE :
2001-06-09 07:36:32 +00:00
aspec . format = AUDIO_S16LSB ;
break ;
2004-12-27 17:30:15 +00:00
case AF_FORMAT_S16_BE :
2001-06-09 07:36:32 +00:00
aspec . format = AUDIO_S16MSB ;
break ;
2004-12-27 17:30:15 +00:00
case AF_FORMAT_S8 :
2001-06-09 07:36:32 +00:00
aspec . format = AUDIO_S8 ;
break ;
2004-12-27 17:30:15 +00:00
case AF_FORMAT_U16_LE :
2001-06-09 07:36:32 +00:00
aspec . format = AUDIO_U16LSB ;
break ;
2004-12-27 17:30:15 +00:00
case AF_FORMAT_U16_BE :
2001-06-09 07:36:32 +00:00
aspec . format = AUDIO_U16MSB ;
break ;
default :
2004-05-07 16:12:30 +00:00
aspec . format = AUDIO_S16LSB ;
2004-12-27 17:30:15 +00:00
ao_data . format = AF_FORMAT_S16_LE ;
2004-09-18 20:31:28 +00:00
mp_msg ( MSGT_AO , MSGL_WARN , MSGTR_AO_SDL_UnsupportedAudioFmt , format ) ;
2001-06-09 07:36:32 +00:00
}
2001-06-03 10:48:36 +00:00
/* The desired audio frequency in samples-per-second. */
aspec . freq = rate ;
/* Number of channels (mono/stereo) */
aspec . channels = channels ;
/* 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 */
2001-06-03 20:54:46 +00:00
aspec . samples = SAMPLESIZE ;
2001-06-03 10:48:36 +00:00
/* 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:
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 . */
2001-06-03 20:54:46 +00:00
aspec . callback = outputaudio ;
2001-06-03 10:48:36 +00:00
/* This pointer is passed as the first parameter to the callback function. */
aspec . userdata = NULL ;
2001-06-03 20:54:46 +00:00
/* initialize the SDL Audio system */
if ( SDL_Init ( SDL_INIT_AUDIO /*|SDL_INIT_NOPARACHUTE*/ ) ) {
2004-09-18 20:31:28 +00:00
mp_msg ( MSGT_AO , MSGL_ERR , MSGTR_AO_SDL_CantInit , SDL_GetError ( ) ) ;
2001-06-03 20:54:46 +00:00
return 0 ;
}
2001-06-03 10:48:36 +00:00
/* Open the audio device and start playing sound! */
2002-10-25 10:27:20 +00:00
if ( SDL_OpenAudio ( & aspec , & obtained ) < 0 ) {
2004-09-18 20:31:28 +00:00
mp_msg ( MSGT_AO , MSGL_ERR , MSGTR_AO_SDL_CantOpenAudio , SDL_GetError ( ) ) ;
2001-06-03 10:48:36 +00:00
return ( 0 ) ;
}
2002-10-25 10:27:20 +00:00
/* did we got what we wanted ? */
ao_data . channels = obtained . channels ;
ao_data . samplerate = obtained . freq ;
switch ( obtained . format ) {
case AUDIO_U8 :
2004-12-27 17:30:15 +00:00
ao_data . format = AF_FORMAT_U8 ;
2002-10-25 10:27:20 +00:00
break ;
case AUDIO_S16LSB :
2004-12-27 17:30:15 +00:00
ao_data . format = AF_FORMAT_S16_LE ;
2002-10-25 10:27:20 +00:00
break ;
case AUDIO_S16MSB :
2004-12-27 17:30:15 +00:00
ao_data . format = AF_FORMAT_S16_BE ;
2002-10-25 10:27:20 +00:00
break ;
case AUDIO_S8 :
2004-12-27 17:30:15 +00:00
ao_data . format = AF_FORMAT_S8 ;
2002-10-25 10:27:20 +00:00
break ;
case AUDIO_U16LSB :
2004-12-27 17:30:15 +00:00
ao_data . format = AF_FORMAT_U16_LE ;
2002-10-25 10:27:20 +00:00
break ;
case AUDIO_U16MSB :
2004-12-27 17:30:15 +00:00
ao_data . format = AF_FORMAT_U16_BE ;
2002-10-25 10:27:20 +00:00
break ;
default :
2004-09-18 20:31:28 +00:00
mp_msg ( MSGT_AO , MSGL_WARN , MSGTR_AO_SDL_UnsupportedAudioFmt , obtained . format ) ;
2002-10-25 10:27:20 +00:00
return 0 ;
}
2002-11-01 17:46:45 +00:00
mp_msg ( MSGT_AO , MSGL_V , " SDL: buf size = %d \n " , obtained . size ) ;
2002-10-25 10:27:20 +00:00
ao_data . buffersize = obtained . size ;
2004-11-06 22:33:17 +00:00
ao_data . outburst = CHUNK_SIZE ;
2001-06-03 21:26:23 +00:00
2004-10-31 21:02:47 +00:00
reset ( ) ;
2001-06-03 10:48:36 +00:00
/* unsilence audio, if callback is ready */
SDL_PauseAudio ( 0 ) ;
return 1 ;
}
// close audio device
2004-04-06 17:55:36 +00:00
static void uninit ( int immed ) {
2002-11-01 17:46:45 +00:00
mp_msg ( MSGT_AO , MSGL_V , " SDL: Audio Subsystem shutting down! \n " ) ;
2004-11-06 22:33:17 +00:00
if ( ! immed )
2005-02-27 23:06:32 +00:00
usec_sleep ( get_delay ( ) * 1000 * 1000 ) ;
2001-06-03 10:48:36 +00:00
SDL_CloseAudio ( ) ;
2001-06-03 20:54:46 +00:00
SDL_QuitSubSystem ( SDL_INIT_AUDIO ) ;
2001-06-03 10:48:36 +00:00
}
// stop playing and empty buffers (for seeking/pause)
static void reset ( ) {
2001-06-09 07:36:32 +00:00
2001-06-10 23:43:37 +00:00
//printf("SDL: reset called!\n");
2001-06-09 07:36:32 +00:00
2004-10-31 21:02:47 +00:00
SDL_PauseAudio ( 1 ) ;
2001-06-03 20:54:46 +00:00
/* Reset ring-buffer state */
2004-10-31 21:02:47 +00:00
read_pos = 0 ;
write_pos = 0 ;
SDL_PauseAudio ( 0 ) ;
2001-06-03 10:48:36 +00:00
}
2001-06-05 18:40:44 +00:00
// stop playing, keep buffers (for pause)
static void audio_pause ( )
{
2001-06-09 08:56:15 +00:00
2001-06-10 23:43:37 +00:00
//printf("SDL: audio_pause called!\n");
2002-10-24 18:13:24 +00:00
SDL_PauseAudio ( 1 ) ;
2001-06-09 08:56:15 +00:00
2001-06-05 18:40:44 +00:00
}
// resume playing, after audio_pause()
static void audio_resume ( )
{
2001-06-10 23:43:37 +00:00
//printf("SDL: audio_resume called!\n");
2002-10-24 18:13:24 +00:00
SDL_PauseAudio ( 0 ) ;
2001-06-05 18:40:44 +00:00
}
2001-06-03 10:48:36 +00:00
// return: how many bytes can be played without blocking
static int get_space ( ) {
2004-10-31 21:02:47 +00:00
return buf_free ( ) ;
2001-06-03 10:48:36 +00:00
}
// plays 'len' bytes of 'data'
// it should round it down to outburst*n
// return: number of bytes played
static int play ( void * data , int len , int flags ) {
2004-04-07 17:53:42 +00:00
len = ( len / ao_data . outburst ) * ao_data . outburst ;
2001-06-04 01:18:52 +00:00
#if 0
2001-06-03 20:54:46 +00:00
int ret ;
2001-06-03 10:48:36 +00:00
2001-06-03 20:54:46 +00:00
/* Audio locking prohibits call of outputaudio */
SDL_LockAudio ( ) ;
// copy audio stream into ring-buffer
ret = write_buffer ( data , len ) ;
2001-06-03 10:48:36 +00:00
SDL_UnlockAudio ( ) ;
2001-06-03 20:54:46 +00:00
return ret ;
# else
return write_buffer ( data , len ) ;
# endif
2001-06-03 10:48:36 +00:00
}
2001-11-24 05:21:22 +00:00
// return: delay in seconds between first and last sample in buffer
static float get_delay ( ) {
2004-10-31 21:02:47 +00:00
int buffered = BUFFSIZE - CHUNK_SIZE - buf_free ( ) ; // could be less
return ( float ) ( buffered + ao_data . buffersize ) / ( float ) ao_data . bps ;
2001-06-03 10:48:36 +00:00
}