exceptions
This commit is contained in:
@@ -232,40 +232,46 @@ void Alsa9Buf::GetSoundCardDebugInfo()
|
||||
LOG->Info( "ALSA device overridden to \"%s\"", PREFSMAN->m_iSoundDevice.c_str() );
|
||||
}
|
||||
|
||||
Alsa9Buf::Alsa9Buf( hw hardware, int channels_ )
|
||||
Alsa9Buf::Alsa9Buf()
|
||||
{
|
||||
GetSoundCardDebugInfo();
|
||||
|
||||
InitializeErrorHandler();
|
||||
|
||||
channels = channels_;
|
||||
samplerate = 44100;
|
||||
samplebits = 16;
|
||||
last_cursor_pos = 0;
|
||||
samplerate_set_explicitly = false;
|
||||
preferred_writeahead = 8192;
|
||||
preferred_chunksize = 1024;
|
||||
pcm = NULL;
|
||||
}
|
||||
|
||||
CString Alsa9Buf::Init( hw hardware, int channels_ )
|
||||
{
|
||||
channels = channels_;
|
||||
|
||||
GetSoundCardDebugInfo();
|
||||
|
||||
InitializeErrorHandler();
|
||||
|
||||
/* Open the device. */
|
||||
int err;
|
||||
err = dsnd_pcm_open( &pcm, DeviceName(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK );
|
||||
if (err < 0)
|
||||
RageException::ThrowNonfatal("dsnd_pcm_open(%s): %s", DeviceName().c_str(), dsnd_strerror(err));
|
||||
if( err < 0 )
|
||||
return ssprintf( "dsnd_pcm_open(%s): %s", DeviceName().c_str(), dsnd_strerror(err) );
|
||||
|
||||
if( !SetHWParams() )
|
||||
{
|
||||
CHECKPOINT;
|
||||
dsnd_pcm_close(pcm);
|
||||
CHECKPOINT;
|
||||
RageException::ThrowNonfatal( "SetHWParams failed" );
|
||||
return "SetHWParams failed";
|
||||
}
|
||||
|
||||
SetSWParams();
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
Alsa9Buf::~Alsa9Buf()
|
||||
{
|
||||
dsnd_pcm_close(pcm);
|
||||
if( pcm != NULL )
|
||||
dsnd_pcm_close( pcm );
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -34,9 +34,10 @@ public:
|
||||
enum hw { HW_HARDWARE, HW_SOFTWARE, HW_DONT_CARE };
|
||||
|
||||
/* Call SetSampleRate before you use the sample. */
|
||||
Alsa9Buf( hw hardware, int channels );
|
||||
Alsa9Buf();
|
||||
CString Init( hw hardware, int channels );
|
||||
~Alsa9Buf();
|
||||
|
||||
|
||||
int GetNumFramesToFill();
|
||||
bool WaitUntilFramesCanBeFilled( int timeout_ms );
|
||||
void Write( const int16_t *buffer, int frames );
|
||||
|
||||
@@ -268,7 +268,7 @@ int RageSound_ALSA9::GetSampleRate( int rate ) const
|
||||
return str->pcm->FindSampleRate( rate );
|
||||
}
|
||||
|
||||
static void CheckMixingBlacklist()
|
||||
static CString CheckMixingBlacklist()
|
||||
{
|
||||
CString sID = Alsa9Buf::GetHardwareID();
|
||||
const CString blacklist[] = {
|
||||
@@ -278,41 +278,46 @@ static void CheckMixingBlacklist()
|
||||
}, *blacklist_end = blacklist+ARRAYSIZE(blacklist);
|
||||
|
||||
if( find( &blacklist[0], blacklist_end, sID ) != blacklist_end )
|
||||
RageException::ThrowNonfatal( "ALSA driver \"%s\" not using hardware mixing", sID.c_str() );
|
||||
return ssprintf( "ALSA driver \"%s\" not using hardware mixing", sID.c_str() );
|
||||
return "";
|
||||
}
|
||||
|
||||
RageSound_ALSA9::RageSound_ALSA9():
|
||||
m_Mutex("ALSAMutex"),
|
||||
m_InactiveSoundMutex("InactiveSoundMutex")
|
||||
{
|
||||
CString err = LoadALSA();
|
||||
if( err != "" )
|
||||
RageException::ThrowNonfatal("Driver unusable: %s", err.c_str());
|
||||
try {
|
||||
CheckMixingBlacklist();
|
||||
|
||||
shutdown = false;
|
||||
}
|
||||
|
||||
CString RageSound_ALSA9::Init()
|
||||
{
|
||||
CString sError = LoadALSA();
|
||||
if( sError != "" )
|
||||
return ssprintf( "Driver unusable: %s", sError.c_str() );
|
||||
|
||||
sError = CheckMixingBlacklist();
|
||||
if( sError != "" )
|
||||
return sError;
|
||||
|
||||
/* Create a bunch of streams and put them into the stream pool. */
|
||||
for( int i = 0; i < 32; ++i )
|
||||
{
|
||||
Alsa9Buf *newbuf;
|
||||
try {
|
||||
newbuf = new Alsa9Buf( Alsa9Buf::HW_HARDWARE, channels );
|
||||
} catch(const RageException &e) {
|
||||
Alsa9Buf *newbuf = new Alsa9Buf;
|
||||
sError = newbuf->Init( Alsa9Buf::HW_HARDWARE, channels );
|
||||
if( sError != "" )
|
||||
{
|
||||
delete newbuf;
|
||||
|
||||
/* If we didn't get at least 8, fail. */
|
||||
if(i >= 8) break; /* OK */
|
||||
if( i >= 8 )
|
||||
break; /* OK */
|
||||
|
||||
/* Clean up; the dtor won't be called. */
|
||||
for(int n = 0; n < i; ++n)
|
||||
delete stream_pool[n];
|
||||
|
||||
if( !i )
|
||||
RageException::ThrowNonfatal( "%s", e.what() );
|
||||
if( i == 0 )
|
||||
return sError;
|
||||
|
||||
/* We created at least one hardware buffer. */
|
||||
LOG->Trace("Could only create %i buffers; need at least 8 (failed with %s). Hardware ALSA driver can't be used.", i, e.what());
|
||||
RageException::ThrowNonfatal("Not enough substreams for hardware mixing, using software mixing");
|
||||
LOG->Trace( "Could only create %i buffers; need at least 8 (failed with %s). Hardware ALSA driver can't be used.", i, sError.c_str() );
|
||||
return "Not enough substreams for hardware mixing, using software mixing";
|
||||
}
|
||||
|
||||
stream *s = new stream;
|
||||
@@ -320,23 +325,24 @@ try {
|
||||
stream_pool.push_back(s);
|
||||
}
|
||||
|
||||
LOG->Info("ALSA: Got %i hardware buffers", stream_pool.size());
|
||||
LOG->Info( "ALSA: Got %i hardware buffers", stream_pool.size() );
|
||||
|
||||
MixingThread.SetName( "RageSound_ALSA9" );
|
||||
MixingThread.Create( MixerThread_start, this );
|
||||
} catch(...) {
|
||||
UnloadALSA();
|
||||
throw;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
RageSound_ALSA9::~RageSound_ALSA9()
|
||||
{
|
||||
/* Signal the mixing thread to quit. */
|
||||
shutdown = true;
|
||||
LOG->Trace("Shutting down mixer thread ...");
|
||||
MixingThread.Wait();
|
||||
LOG->Trace("Mixer thread shut down.");
|
||||
if( MixingThread.IsCreated() )
|
||||
{
|
||||
/* Signal the mixing thread to quit. */
|
||||
shutdown = true;
|
||||
LOG->Trace("Shutting down mixer thread ...");
|
||||
MixingThread.Wait();
|
||||
LOG->Trace("Mixer thread shut down.");
|
||||
}
|
||||
|
||||
for(unsigned i = 0; i < stream_pool.size(); ++i)
|
||||
delete stream_pool[i];
|
||||
|
||||
@@ -66,6 +66,7 @@ public:
|
||||
void Update(float delta);
|
||||
|
||||
RageSound_ALSA9();
|
||||
CString Init();
|
||||
~RageSound_ALSA9();
|
||||
};
|
||||
|
||||
|
||||
@@ -80,11 +80,15 @@ void RageSound_ALSA9_Software::SetupDecodingThread()
|
||||
|
||||
RageSound_ALSA9_Software::RageSound_ALSA9_Software()
|
||||
{
|
||||
CString err = LoadALSA();
|
||||
if( err != "" )
|
||||
RageException::ThrowNonfatal("Driver unusable: %s", err.c_str());
|
||||
try {
|
||||
pcm = NULL;
|
||||
shutdown = false;
|
||||
}
|
||||
|
||||
CString RageSound_ALSA9_Software::Init()
|
||||
{
|
||||
CString sError = LoadALSA();
|
||||
if( sError != "" )
|
||||
return ssprintf( "Driver unusable: %s", sError.c_str() );
|
||||
|
||||
max_writeahead = safe_writeahead;
|
||||
CString sys;
|
||||
@@ -97,7 +101,10 @@ try {
|
||||
if( PREFSMAN->m_iSoundWriteAhead )
|
||||
max_writeahead = PREFSMAN->m_iSoundWriteAhead;
|
||||
|
||||
pcm = new Alsa9Buf( Alsa9Buf::HW_DONT_CARE, channels );
|
||||
pcm = new Alsa9Buf();
|
||||
sError = pcm->Init( Alsa9Buf::HW_DONT_CARE, channels );
|
||||
if( sError != "" )
|
||||
return sError;
|
||||
|
||||
samplerate = pcm->FindSampleRate( samplerate );
|
||||
pcm->SetSampleRate( samplerate );
|
||||
@@ -111,20 +118,20 @@ try {
|
||||
|
||||
MixingThread.SetName( "RageSound_ALSA9_Software" );
|
||||
MixingThread.Create( MixerThread_start, this );
|
||||
} catch(...) {
|
||||
UnloadALSA();
|
||||
throw;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
RageSound_ALSA9_Software::~RageSound_ALSA9_Software()
|
||||
{
|
||||
/* Signal the mixing thread to quit. */
|
||||
shutdown = true;
|
||||
LOG->Trace("Shutting down mixer thread ...");
|
||||
MixingThread.Wait();
|
||||
LOG->Trace("Mixer thread shut down.");
|
||||
if( MixingThread.IsCreated() )
|
||||
{
|
||||
/* Signal the mixing thread to quit. */
|
||||
shutdown = true;
|
||||
LOG->Trace("Shutting down mixer thread ...");
|
||||
MixingThread.Wait();
|
||||
LOG->Trace("Mixer thread shut down.");
|
||||
}
|
||||
|
||||
delete pcm;
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ public:
|
||||
|
||||
|
||||
RageSound_ALSA9_Software();
|
||||
CString Init();
|
||||
~RageSound_ALSA9_Software();
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user