Skip to content

Commit

Permalink
Stop output on OOM in MP3 (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuckMash committed Apr 26, 2024
1 parent 51a584c commit b837e7d
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/AudioGeneratorMP3.cpp
@@ -1,7 +1,7 @@
/*
AudioGeneratorMP3
Wrap libmad MP3 library to play audio
Copyright (C) 2017 Earle F. Philhower, III
This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -62,7 +62,7 @@ AudioGeneratorMP3::~AudioGeneratorMP3()
free(synth);
free(frame);
free(stream);
}
}
}


Expand Down Expand Up @@ -182,15 +182,15 @@ bool AudioGeneratorMP3::GetOneSample(int16_t sample[2])
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];
samplePtr++;
} else {
samplePtr = 0;

switch ( mad_synth_frame_onens(synth, frame, nsCount++) ) {
case MAD_FLOW_STOP:
case MAD_FLOW_BREAK: audioLogger->printf_P(PSTR("msf1ns failed\n"));
Expand Down Expand Up @@ -302,6 +302,7 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
synth = reinterpret_cast<struct mad_synth *>(preallocateSynthSpace);
}
else {
output->stop();
audioLogger->printf_P("OOM error in MP3: Want %d/%d/%d/%d bytes, have %d/%d/%d/%d bytes preallocated.\n",
preAllocBuffSize(), preAllocStreamSize(), preAllocFrameSize(), preAllocSynthSize(),
preallocateSize, preallocateStreamSize, preallocateFrameSize, preallocateSynthSize);
Expand All @@ -319,6 +320,7 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
p += preAllocSynthSize();
int neededBytes = p - reinterpret_cast<uint8_t *>(preallocateSpace);
if (neededBytes > preallocateSize) {
output->stop();
audioLogger->printf_P("OOM error in MP3: Want %d bytes, have %d bytes preallocated.\n", neededBytes, preallocateSize);
return false;
}
Expand All @@ -336,17 +338,20 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
stream = NULL;
frame = NULL;
synth = NULL;

output->stop();
audioLogger->printf_P("OOM error in MP3\n");
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;
return true;
}
Expand Down Expand Up @@ -413,4 +418,3 @@ extern "C" {
}
#endif
}

0 comments on commit b837e7d

Please sign in to comment.