- reduce memory consumption when decoding RTP stream + bring back G722 and G729

This commit is contained in:
Dmytro Bogovych 2024-04-08 09:58:11 +03:00
parent cf9be1c3f2
commit 4ca4e84547
3 changed files with 93 additions and 93 deletions

View File

@ -10,171 +10,171 @@ using namespace Audio;
DataWindow::DataWindow()
{
mFilled = 0;
mData = NULL;
mCapacity = 0;
mFilled = 0;
mData = NULL;
mCapacity = 0;
}
DataWindow::~DataWindow()
{
if (mData)
free(mData);
if (mData)
free(mData);
}
void DataWindow::setCapacity(int capacity)
{
Lock l(mMutex);
int tail = capacity - mCapacity;
mData = (char*)realloc(mData, capacity);
if (tail > 0)
memset(mData + mCapacity, 0, tail);
mCapacity = capacity;
Lock l(mMutex);
int tail = capacity - mCapacity;
mData = (char*)realloc(mData, capacity);
if (tail > 0)
memset(mData + mCapacity, 0, tail);
mCapacity = capacity;
}
void DataWindow::addZero(int length)
{
Lock l(mMutex);
Lock l(mMutex);
if (length > mCapacity)
length = mCapacity;
if (length > mCapacity)
length = mCapacity;
int avail = mCapacity - mFilled;
int avail = mCapacity - mFilled;
if (avail < length)
{
memmove(mData, mData + length - avail, mFilled - (length - avail));
mFilled -= length - avail;
}
memset(mData + mFilled, 0, length);
mFilled += length;
if (avail < length)
{
memmove(mData, mData + length - avail, mFilled - (length - avail));
mFilled -= length - avail;
}
memset(mData + mFilled, 0, length);
mFilled += length;
}
void DataWindow::add(const void* data, int length)
{
Lock l(mMutex);
if (length > mCapacity)
{
// Use latest bytes from data buffer in this case.
data = (char*)data + length - mCapacity;
length = mCapacity;
}
Lock l(mMutex);
// Check how much free space we have
int avail = mCapacity - mFilled;
if (length > mCapacity)
{
// Use latest bytes from data buffer in this case.
data = (char*)data + length - mCapacity;
length = mCapacity;
}
if (avail < length)
{
// Find the portion of data to move & save
int delta = length - avail;
// Check how much free space we have
int avail = mCapacity - mFilled;
// Move the data
if (mFilled - delta > 0)
memmove(mData, mData + delta, mFilled - delta);
mFilled -= delta;
}
if (avail < length)
{
// Find the portion of data to move & save
int delta = length - avail;
memcpy(mData + mFilled, data, length);
mFilled += length;
// Move the data
if (mFilled - delta > 0)
memmove(mData, mData + delta, mFilled - delta);
mFilled -= delta;
}
memcpy(mData + mFilled, data, length);
mFilled += length;
}
void DataWindow::add(short sample)
{
add(&sample, sizeof sample);
add(&sample, sizeof sample);
}
void DataWindow::erase(int length)
{
Lock l(mMutex);
if (length > mFilled)
length = mFilled;
if (length != mFilled)
memmove(mData, mData + length, mFilled - length);
mFilled -= length;
Lock l(mMutex);
if (length > mFilled)
length = mFilled;
if (length != mFilled)
memmove(mData, mData + length, mFilled - length);
mFilled -= length;
}
const char* DataWindow::data() const
{
return mData;
return mData;
}
char* DataWindow::mutableData()
{
return mData;
return mData;
}
void DataWindow::clear()
{
Lock l(mMutex);
mFilled = 0;
Lock l(mMutex);
mFilled = 0;
}
short DataWindow::shortAt(int index) const
{
Lock l(mMutex);
assert(index < mFilled / 2);
return ((short*)mData)[index];
Lock l(mMutex);
assert(index < mFilled / 2);
return ((short*)mData)[index];
}
void DataWindow::setShortAt(short value, int index)
{
Lock l(mMutex);
assert(index < mFilled / 2);
((short*)mData)[index] = value;
Lock l(mMutex);
assert(index < mFilled / 2);
((short*)mData)[index] = value;
}
int DataWindow::read(void* buffer, int length)
{
Lock l(mMutex);
if (length > mFilled)
length = mFilled;
if (length)
{
if (buffer)
memcpy(buffer, mData, length);
if (length < mFilled)
memmove(mData, mData+length, mFilled - length);
mFilled -= length;
}
return length;
Lock l(mMutex);
if (length > mFilled)
length = mFilled;
if (length)
{
if (buffer)
memcpy(buffer, mData, length);
if (length < mFilled)
memmove(mData, mData+length, mFilled - length);
mFilled -= length;
}
return length;
}
int DataWindow::filled() const
{
Lock l(mMutex);
return mFilled;
Lock l(mMutex);
return mFilled;
}
void DataWindow::setFilled(int filled)
{
Lock l(mMutex);
mFilled = filled;
Lock l(mMutex);
mFilled = filled;
}
int DataWindow::capacity() const
{
Lock l(mMutex);
return mCapacity;
Lock l(mMutex);
return mCapacity;
}
void DataWindow::zero(int length)
{
Lock l(mMutex);
assert(length <= mCapacity);
mFilled = length;
memset(mData, 0, mFilled);
Lock l(mMutex);
assert(length <= mCapacity);
mFilled = length;
memset(mData, 0, mFilled);
}
void DataWindow::makeStereoFromMono(DataWindow& dst, DataWindow& src)
{
Lock lockDst(dst.mMutex), lockSrc(src.mMutex);
Lock lockDst(dst.mMutex), lockSrc(src.mMutex);
dst.setCapacity(src.filled()*2);
short* input = (short*)src.mutableData();
short* output = (short*)dst.mutableData();
dst.setCapacity(src.filled()*2);
short* input = (short*)src.mutableData();
short* output = (short*)dst.mutableData();
for (int i=0; i<src.filled()/2; i++)
output[i*2] = output[i*2+1] = input[i];
dst.mFilled = src.filled() * 2;
for (int i=0; i<src.filled()/2; i++)
output[i*2] = output[i*2+1] = input[i];
dst.mFilled = src.filled() * 2;
}

View File

@ -240,8 +240,8 @@ void CodecList::init(const Settings& settings)
mFactoryList.push_back(new G711Codec::UlawFactory());
mFactoryList.push_back(new GsmCodec::GsmFactory(mSettings.mGsmFrPayloadLength == 32 ? GsmCodec::Type::Bytes_32 : GsmCodec::Type::Bytes_33, mSettings.mGsmFrPayloadType));
// mFactoryList.push_back(new G722Codec::G722Factory());
// mFactoryList.push_back(new G729Codec::G729Factory());
mFactoryList.push_back(new G722Codec::G722Factory());
mFactoryList.push_back(new G729Codec::G729Factory());
#ifndef TARGET_ANDROID
mFactoryList.push_back(new GsmHrCodec::GsmHrFactory(mSettings.mGsmHrPayloadType));
#endif

View File

@ -72,7 +72,7 @@ public:
static EvsSpec parse(const std::string& spec);
bool operator == (const EvsSpec& rhs) const { return std::tie(mPayloadType, mBandwidth, mEncodingType) == std::tie(rhs.mPayloadType, rhs.mBandwidth, rhs.mEncodingType);}
bool operator != (const EvsSpec& rhs) const { return ! (operator ==) (rhs);};
bool operator != (const EvsSpec& rhs) const { return ! (operator ==) (rhs);}
};
@ -90,7 +90,7 @@ public:
bool isValid() const;
bool operator == (const OpusSpec& rhs) const { return std::tie(mPayloadType, mRate, mChannels) == std::tie(rhs.mPayloadType, rhs.mRate, rhs.mChannels);}
bool operator != (const OpusSpec& rhs) const { return ! (operator ==) (rhs);};
bool operator != (const OpusSpec& rhs) const { return ! (operator ==) (rhs);}
static OpusSpec parse(const std::string& spec);
};