fix potential memory leak

increase decoder thread priority
init the hardware stream in the driver, not in the converter
This commit is contained in:
Glenn Maynard
2004-03-28 21:18:21 +00:00
parent 75488133d3
commit bee8234100
4 changed files with 104 additions and 82 deletions
+13 -70
View File
@@ -2,50 +2,26 @@
#include "RageSoundDriver_CA.h"
#include "CAHelpers.h"
#include "CAAudioHardwareDevice.h"
#include "CAAudioHardwareStream.h"
#include "CAException.h"
const UInt32 kFramesPerPacket = 1;
const UInt32 kChannelsPerFrame = 2;
const UInt32 kBitsPerChannel = 16;
const UInt32 kBytesPerPacket = kChannelsPerFrame * kBitsPerChannel / 8;
const UInt32 kBytesPerFrame = kBytesPerPacket;
const UInt32 kFormatFlags = kAudioFormatFlagsNativeEndian |
kAudioFormatFlagIsSignedInteger;
AudioConverter::AudioConverter(CAAudioHardwareDevice *dev, RageSound_CA *driver)
AudioConverter::AudioConverter( RageSound_CA *driver, const Desc &procFormat )
: mBuffer(NULL), mBufferSize(0)
{
AudioStreamID sID = dev->GetStreamByIndex(kAudioDeviceSectionOutput, 0);
CAAudioHardwareStream stream(sID);
vector<Desc> procFormats;
vector<Desc> physicalFormats;
UInt32 numFormats = stream.GetNumberAvailableIOProcFormats();
for (UInt32 i=0; i<numFormats; ++i)
{
Desc desc;
stream.GetAvailableIOProcFormatByIndex(i, desc);
procFormats.push_back(desc);
}
const Desc& procFormat = FindClosestFormat(procFormats);
stream.SetCurrentIOProcFormat(procFormat);
numFormats = stream.GetNumberAvailablePhysicalFormats();
for (UInt32 i=0; i<numFormats; ++i)
{
Desc desc;
stream.GetAvailablePhysicalFormatByIndex(i, desc);
physicalFormats.push_back(desc);
}
const Desc& physicalFormat = FindClosestFormat(physicalFormats);
stream.SetCurrentPhysicalFormat(physicalFormat);
const Desc SMFormat(44100.0, kAudioFormatLinearPCM, kBytesPerPacket,
kFramesPerPacket, kBytesPerFrame, kChannelsPerFrame,
kBitsPerChannel, kFormatFlags);
SMFormat.Print();
procFormat.Print();
if (this->Initialize(SMFormat, procFormat))
RageException::ThrowNonfatal("Couldn't create the converter.");
if( this->Initialize(SMFormat, procFormat) )
RageException::ThrowNonfatal( "Couldn't create the converter." );
mDriver = driver;
}
@@ -57,9 +33,9 @@ OSStatus AudioConverter::FormatConverterInputProc(UInt32& ioNumberDataPackets,
AudioBuffer& buf = ioData.mBuffers[0];
// This really shouldn't happen more than once, but better be sure.
if (mBufferSize != buf.mDataByteSize)
if( mBufferSize != buf.mDataByteSize )
{
delete mBuffer; // deleting NULL does not crash, unlike free(NULL)
delete mBuffer;
mBufferSize = buf.mDataByteSize;
mBuffer = new UInt8[mBufferSize];
}
@@ -69,36 +45,3 @@ OSStatus AudioConverter::FormatConverterInputProc(UInt32& ioNumberDataPackets,
return noErr;
}
Desc AudioConverter::FindClosestFormat(const vector<Desc>& formats)
{
vector<Desc> v;
vector<Desc>::const_iterator i;
for (i = formats.begin(); i != formats.end(); ++i)
{
const Desc& format = *i;
if (!format.IsPCM() || format.mSampleRate != 44100.0)
continue;
if (format.SampleWordSize() == 2 &&
(format.mFormatFlags & kAudioFormatFlagIsSignedInteger) ==
kAudioFormatFlagIsSignedInteger)
{ // exact match
return format;
}
v.push_back(format);
}
for (i = v.begin(); i != v.end(); ++i)
{
const Desc& format = *i;
if (format.SampleWordSize() == 2)
{
return format; // close
}
}
if (v.empty())
RageException::ThrowNonfatal("Couldn't find a close format.");
return v[0]; // something is better than nothing.
}