Enable static allocated buffers, no dynamic malloc

Because we are allocating objects totaling over 80% of the ESP8266 memory
space, we are very sensitive to memory fragmentation.  While we can control
our own allocs, the IP and SDK can allocate any size, at any time, which
can cause us to not be able to find the large contiguous memory areas we need
for the codes.

Work around this by preallocating, before anything else, buffer memory and
never freeing it.  Pass this memory into the codec and it will allocate from
this block and be guaranteed to always succeed.

This may fix the last of the hangs on the webradio/http streaming, which only
seemed to happen after a stop/start cycle.
This commit is contained in:
Earle F. Philhower, III committed 2018-01-21 13:02:05 -08:00
1 parent d04beb1105
commit 8ffebe6496
23 files changed
+306 -138

No files matched your search

+99 -48
View File
@@ -26,38 +26,57 @@ AudioGeneratorMP3::AudioGeneratorMP3()
running = false;
file = NULL;
output = NULL;
buffLen = 1500; // Max theoretical frame of 1441 + 8 guard bytes, with some fluff
buff = NULL;
nsCountMax = 1152/32;
madInitted = false;
preallocateSpace = NULL;
preallocateSize = 0;
}
AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size)
{
running = false;
file = NULL;
output = NULL;
buff = NULL;
nsCountMax = 1152/32;
madInitted = false;
preallocateSpace = space;
preallocateSize = size;
}
AudioGeneratorMP3::~AudioGeneratorMP3()
{
free(buff);
buff = NULL;
}
bool AudioGeneratorMP3::SetBufferSize(int sz)
{
if (running) return false;
buffLen = sz;
return true;
if (!preallocateSpace) {
free(buff);
free(synth);
free(frame);
free(stream);
}
}
bool AudioGeneratorMP3::stop()
{
free(buff);
buff = NULL;
if (madInitted) {
mad_synth_finish(&synth);
mad_frame_finish(&frame);
mad_stream_finish(&stream);
mad_synth_finish(synth);
mad_frame_finish(frame);
mad_stream_finish(stream);
madInitted = false;
}
if (!preallocateSpace) {
free(buff);
free(synth);
free(frame);
free(stream);
}
buff = NULL;
synth = NULL;
frame = NULL;
stream = NULL;
running = false;
output->stop();
return file->close();
@@ -74,13 +93,13 @@ enum mad_flow AudioGeneratorMP3::ErrorToFlow()
char errLine[128];
// Special case - eat "lost sync @ byte 0" as it always occurs and is not really correct....it never had sync!
if ((lastReadPos==0) && (stream.error==MAD_ERROR_LOSTSYNC)) return MAD_FLOW_CONTINUE;
if ((lastReadPos==0) && (stream->error==MAD_ERROR_LOSTSYNC)) return MAD_FLOW_CONTINUE;
strcpy_P(err, mad_stream_errorstr(&stream));
strcpy_P(err, mad_stream_errorstr(stream));
snprintf_P(errLine, sizeof(errLine), PSTR("Decoding error '%s' at byte offset %d"),
err, (stream.this_frame - buff) + lastReadPos);
err, (stream->this_frame - buff) + lastReadPos);
yield(); // Something bad happened anyway, ensure WiFi gets some time, too
cb.st(stream.error, errLine);
cb.st(stream->error, errLine);
return MAD_FLOW_CONTINUE;
}
@@ -88,10 +107,10 @@ enum mad_flow AudioGeneratorMP3::Input()
{
int unused = 0;
if (stream.next_frame) {
unused = buffLen - (stream.next_frame - buff);
memmove(buff, stream.next_frame, unused);
stream.next_frame = NULL;
if (stream->next_frame) {
unused = buffLen - (stream->next_frame - buff);
memmove(buff, stream->next_frame, unused);
stream->next_frame = NULL;
}
if (unused == buffLen) {
// Something wicked this way came, throw it all out and try again
@@ -106,7 +125,7 @@ enum mad_flow AudioGeneratorMP3::Input()
return MAD_FLOW_STOP;
}
mad_stream_buffer(&stream, buff, len + unused);
mad_stream_buffer(stream, buff, len + unused);
return MAD_FLOW_CONTINUE;
}
@@ -114,34 +133,34 @@ enum mad_flow AudioGeneratorMP3::Input()
bool AudioGeneratorMP3::DecodeNextFrame()
{
if (mad_frame_decode(&frame, &stream) == -1) {
if (mad_frame_decode(frame, stream) == -1) {
ErrorToFlow(); // Always returns CONTINUE
return false;
}
nsCountMax = MAD_NSBSAMPLES(&frame.header);
nsCountMax = MAD_NSBSAMPLES(&frame->header);
return true;
}
bool AudioGeneratorMP3::GetOneSample(int16_t sample[2])
{
if (synth.pcm.samplerate != lastRate) {
output->SetRate(synth.pcm.samplerate);
lastRate = synth.pcm.samplerate;
if (synth->pcm.samplerate != lastRate) {
output->SetRate(synth->pcm.samplerate);
lastRate = synth->pcm.samplerate;
}
if (synth.pcm.channels != lastChannels) {
output->SetChannels(synth.pcm.channels);
lastChannels = synth.pcm.channels;
if (synth->pcm.channels != lastChannels) {
output->SetChannels(synth->pcm.channels);
lastChannels = synth->pcm.channels;
}
// If we're here, we have one decoded frame and sent 0 or more samples out
if (samplePtr < synth.pcm.length) {
sample[AudioOutput::LEFTCHANNEL ] = synth.pcm.samples[0][samplePtr];
sample[AudioOutput::RIGHTCHANNEL] = synth.pcm.samples[1][samplePtr];
if (samplePtr < synth->pcm.length) {
sample[AudioOutput::LEFTCHANNEL ] = synth->pcm.samples[0][samplePtr];
sample[AudioOutput::RIGHTCHANNEL] = synth->pcm.samples[1][samplePtr];
samplePtr++;
} else {
samplePtr = 0;
switch ( mad_synth_frame_onens(&synth, &frame, nsCount++) ) {
switch ( mad_synth_frame_onens(synth, frame, nsCount++) ) {
case MAD_FLOW_STOP:
case MAD_FLOW_BREAK: Serial.printf_P(PSTR("msf1ns failed\n"));
return false; // Either way we're done
@@ -149,8 +168,8 @@ bool AudioGeneratorMP3::GetOneSample(int16_t sample[2])
break; // Do nothing
}
// for IGNORE and CONTINUE, just play what we have now
sample[AudioOutput::LEFTCHANNEL ] = synth.pcm.samples[0][samplePtr];
sample[AudioOutput::RIGHTCHANNEL] = synth.pcm.samples[1][samplePtr];
sample[AudioOutput::LEFTCHANNEL ] = synth->pcm.samples[0][samplePtr];
sample[AudioOutput::RIGHTCHANNEL] = synth->pcm.samples[1][samplePtr];
samplePtr++;
}
return true;
@@ -168,7 +187,7 @@ bool AudioGeneratorMP3::loop()
do
{
// Decode next frame if we're beyond the existing generated data
if ( (samplePtr >= synth.pcm.length) && (nsCount >= nsCountMax) ) {
if ( (samplePtr >= synth->pcm.length) && (nsCount >= nsCountMax) ) {
retry:
if (Input() == MAD_FLOW_STOP) {
return false;
@@ -216,17 +235,49 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
// Where we are in generating one frame's data, set to invalid so we will run loop on first getsample()
samplePtr = 9999;
nsCount = 9999;
synth.pcm.length = 0;
lastRate = 0;
lastChannels = 0;
lastReadPos = 0;
buff = reinterpret_cast<unsigned char *>(malloc(buffLen));
if (!buff) return false;
mad_stream_init(&stream);
mad_frame_init(&frame);
mad_synth_init(&synth);
mad_stream_options(&stream, 0); // TODO - add options support
// Allocate all large memory chunks
if (preallocateSpace) {
uint8_t *p = reinterpret_cast<uint8_t *>(preallocateSpace);
buff = reinterpret_cast<unsigned char *>(p);
p += (buffLen+7) & ~7;
stream = reinterpret_cast<struct mad_stream *>(p);
p += (sizeof(struct mad_stream)+7) & ~7;
frame = reinterpret_cast<struct mad_frame *>(p);
p += (sizeof(struct mad_frame)+7) & ~7;
synth = reinterpret_cast<struct mad_synth *>(p);
p += (sizeof(struct mad_synth)+7) & ~7;
int neededBytes = p - reinterpret_cast<uint8_t *>(preallocateSpace);
if (neededBytes > preallocateSize) {
Serial.printf_P("OOM error in MP3: Want %d bytes, have %d bytes preallocated.\n", neededBytes, preallocateSize);
return false;
}
} else {
buff = reinterpret_cast<unsigned char *>(malloc(buffLen));
stream = reinterpret_cast<struct mad_stream *>(malloc(sizeof(struct mad_stream)));
frame = reinterpret_cast<struct mad_frame *>(malloc(sizeof(struct mad_frame)));
synth = reinterpret_cast<struct mad_synth *>(malloc(sizeof(struct mad_synth)));
if (!buff || !stream || !frame || !synth) {
free(buff);
free(stream);
free(frame);
free(synth);
buff = NULL;
stream = NULL;
frame = NULL;
synth = NULL;
return false;
}
}
mad_stream_init(stream);
mad_frame_init(frame);
mad_synth_init(synth);
synth->pcm.length = 0;
mad_stream_options(stream, 0); // TODO - add options support
madInitted = true;
running = true;