- 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() DataWindow::DataWindow()
{ {
mFilled = 0; mFilled = 0;
mData = NULL; mData = NULL;
mCapacity = 0; mCapacity = 0;
} }
DataWindow::~DataWindow() DataWindow::~DataWindow()
{ {
if (mData) if (mData)
free(mData); free(mData);
} }
void DataWindow::setCapacity(int capacity) void DataWindow::setCapacity(int capacity)
{ {
Lock l(mMutex); Lock l(mMutex);
int tail = capacity - mCapacity; int tail = capacity - mCapacity;
mData = (char*)realloc(mData, capacity); mData = (char*)realloc(mData, capacity);
if (tail > 0) if (tail > 0)
memset(mData + mCapacity, 0, tail); memset(mData + mCapacity, 0, tail);
mCapacity = capacity; mCapacity = capacity;
} }
void DataWindow::addZero(int length) void DataWindow::addZero(int length)
{ {
Lock l(mMutex); Lock l(mMutex);
if (length > mCapacity) if (length > mCapacity)
length = mCapacity; length = mCapacity;
int avail = mCapacity - mFilled; int avail = mCapacity - mFilled;
if (avail < length) if (avail < length)
{ {
memmove(mData, mData + length - avail, mFilled - (length - avail)); memmove(mData, mData + length - avail, mFilled - (length - avail));
mFilled -= length - avail; mFilled -= length - avail;
} }
memset(mData + mFilled, 0, length); memset(mData + mFilled, 0, length);
mFilled += length; mFilled += length;
} }
void DataWindow::add(const void* data, int length) void DataWindow::add(const void* data, int length)
{ {
Lock l(mMutex); Lock l(mMutex);
if (length > mCapacity)
{
// Use latest bytes from data buffer in this case.
data = (char*)data + length - mCapacity;
length = mCapacity;
}
// Check how much free space we have if (length > mCapacity)
int avail = mCapacity - mFilled; {
// Use latest bytes from data buffer in this case.
data = (char*)data + length - mCapacity;
length = mCapacity;
}
if (avail < length) // Check how much free space we have
{ int avail = mCapacity - mFilled;
// Find the portion of data to move & save
int delta = length - avail;
// Move the data if (avail < length)
if (mFilled - delta > 0) {
memmove(mData, mData + delta, mFilled - delta); // Find the portion of data to move & save
mFilled -= delta; int delta = length - avail;
}
memcpy(mData + mFilled, data, length); // Move the data
mFilled += length; if (mFilled - delta > 0)
memmove(mData, mData + delta, mFilled - delta);
mFilled -= delta;
}
memcpy(mData + mFilled, data, length);
mFilled += length;
} }
void DataWindow::add(short sample) void DataWindow::add(short sample)
{ {
add(&sample, sizeof sample); add(&sample, sizeof sample);
} }
void DataWindow::erase(int length) void DataWindow::erase(int length)
{ {
Lock l(mMutex); Lock l(mMutex);
if (length > mFilled) if (length > mFilled)
length = mFilled; length = mFilled;
if (length != mFilled) if (length != mFilled)
memmove(mData, mData + length, mFilled - length); memmove(mData, mData + length, mFilled - length);
mFilled -= length; mFilled -= length;
} }
const char* DataWindow::data() const const char* DataWindow::data() const
{ {
return mData; return mData;
} }
char* DataWindow::mutableData() char* DataWindow::mutableData()
{ {
return mData; return mData;
} }
void DataWindow::clear() void DataWindow::clear()
{ {
Lock l(mMutex); Lock l(mMutex);
mFilled = 0; mFilled = 0;
} }
short DataWindow::shortAt(int index) const short DataWindow::shortAt(int index) const
{ {
Lock l(mMutex); Lock l(mMutex);
assert(index < mFilled / 2); assert(index < mFilled / 2);
return ((short*)mData)[index]; return ((short*)mData)[index];
} }
void DataWindow::setShortAt(short value, int index) void DataWindow::setShortAt(short value, int index)
{ {
Lock l(mMutex); Lock l(mMutex);
assert(index < mFilled / 2); assert(index < mFilled / 2);
((short*)mData)[index] = value; ((short*)mData)[index] = value;
} }
int DataWindow::read(void* buffer, int length) int DataWindow::read(void* buffer, int length)
{ {
Lock l(mMutex); Lock l(mMutex);
if (length > mFilled) if (length > mFilled)
length = mFilled; length = mFilled;
if (length) if (length)
{ {
if (buffer) if (buffer)
memcpy(buffer, mData, length); memcpy(buffer, mData, length);
if (length < mFilled) if (length < mFilled)
memmove(mData, mData+length, mFilled - length); memmove(mData, mData+length, mFilled - length);
mFilled -= length; mFilled -= length;
} }
return length; return length;
} }
int DataWindow::filled() const int DataWindow::filled() const
{ {
Lock l(mMutex); Lock l(mMutex);
return mFilled; return mFilled;
} }
void DataWindow::setFilled(int filled) void DataWindow::setFilled(int filled)
{ {
Lock l(mMutex); Lock l(mMutex);
mFilled = filled; mFilled = filled;
} }
int DataWindow::capacity() const int DataWindow::capacity() const
{ {
Lock l(mMutex); Lock l(mMutex);
return mCapacity; return mCapacity;
} }
void DataWindow::zero(int length) void DataWindow::zero(int length)
{ {
Lock l(mMutex); Lock l(mMutex);
assert(length <= mCapacity); assert(length <= mCapacity);
mFilled = length; mFilled = length;
memset(mData, 0, mFilled); memset(mData, 0, mFilled);
} }
void DataWindow::makeStereoFromMono(DataWindow& dst, DataWindow& src) 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); dst.setCapacity(src.filled()*2);
short* input = (short*)src.mutableData(); short* input = (short*)src.mutableData();
short* output = (short*)dst.mutableData(); short* output = (short*)dst.mutableData();
for (int i=0; i<src.filled()/2; i++) for (int i=0; i<src.filled()/2; i++)
output[i*2] = output[i*2+1] = input[i]; output[i*2] = output[i*2+1] = input[i];
dst.mFilled = src.filled() * 2; 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 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 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 G722Codec::G722Factory());
// mFactoryList.push_back(new G729Codec::G729Factory()); mFactoryList.push_back(new G729Codec::G729Factory());
#ifndef TARGET_ANDROID #ifndef TARGET_ANDROID
mFactoryList.push_back(new GsmHrCodec::GsmHrFactory(mSettings.mGsmHrPayloadType)); mFactoryList.push_back(new GsmHrCodec::GsmHrFactory(mSettings.mGsmHrPayloadType));
#endif #endif

View File

@ -72,7 +72,7 @@ public:
static EvsSpec parse(const std::string& spec); 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 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 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 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); static OpusSpec parse(const std::string& spec);
}; };