Skip to content

Commit

Permalink
Recorder: improve direct export robustness.
Browse files Browse the repository at this point in the history
Detect if requested output format is supported and sanitize the output path.
  • Loading branch information
kosua20 committed Dec 12, 2020
1 parent 9515054 commit 6ef0b65
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
19 changes: 18 additions & 1 deletion src/helpers/Recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,29 @@ void Recorder::setSize(const glm::ivec2 & size){
_size[1] += _size[1]%2;
}

void Recorder::setParameters(const std::string & path, Format format, int framerate, int bitrate, bool skipBackground){
bool Recorder::setParameters(const std::string & path, Format format, int framerate, int bitrate, bool skipBackground){
// Check if the format is supported.
if(int(format) >= _formats.size()){
std::cerr << "[EXPORT]: The requested output format is not supported by this executable. If this is a video format, make sure MIDIVisualizer has been compiled with ffmpeg enabled by checking the output of ./MIDIVisualizer --version" << std::endl;
return false;
}

_exportPath = path;
_outFormat = format;
_exportFramerate = framerate;
_bitRate = bitrate;
_exportNoBackground = skipBackground;

if(_outFormat != Format::PNG){
// Check that the export path is valid.
const std::string & ext = _formats.at(int(_outFormat)).ext;
const std::string fullExt = "." + ext;
// Append extension if needed.
if(_exportPath.size() < 5 || (_exportPath.substr(_exportPath.size()-4) != fullExt)){
_exportPath.append(fullExt);
}
}
return true;
}

bool Recorder::videoExportSupported(){
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/Recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class Recorder {

void setSize(const glm::ivec2 & size);

void setParameters(const std::string & path, Format format, int framerate, int bitrate, bool skipBackground);
bool setParameters(const std::string & path, Format format, int framerate, int bitrate, bool skipBackground);

static bool videoExportSupported();

private:
Expand Down
6 changes: 5 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ int main( int argc, char** argv) {
format = Recorder::Format::MPEG4;
}
}
renderer.startDirectRecording(exportPath, format, framerate, bitrate, pngAlpha, glm::vec2(isw, ish));
const bool success = renderer.startDirectRecording(exportPath, format, framerate, bitrate, pngAlpha, glm::vec2(isw, ish));
if(!success){
// Quit.
performAction(SystemAction::QUIT, window, frame);
}
}

if(fullscreen){
Expand Down
9 changes: 7 additions & 2 deletions src/rendering/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1222,11 +1222,16 @@ void Renderer::setGUIScale(float scale){
ImGui::GetStyle().FrameRounding = 3 * _guiScale;
}

void Renderer::startDirectRecording(const std::string & path, Recorder::Format format, int framerate, int bitrate, bool skipBackground, const glm::vec2 & size){
_recorder.setParameters(path, format, framerate, bitrate, skipBackground);
bool Renderer::startDirectRecording(const std::string & path, Recorder::Format format, int framerate, int bitrate, bool skipBackground, const glm::vec2 & size){
const bool success = _recorder.setParameters(path, format, framerate, bitrate, skipBackground);
if(!success){
std::cerr << "[EXPORT]: Unable to start direct export." << std::endl;
return false;
}
_recorder.setSize(size);
startRecording();
_exitAfterRecording = true;
return true;
}

void Renderer::startRecording(){
Expand Down
4 changes: 2 additions & 2 deletions src/rendering/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class Renderer {
/// Handle keyboard inputs
void keyPressed(int key, int action);

/// Diretly start recording.
void startDirectRecording(const std::string & path, Recorder::Format format, int framerate, int bitrate, bool skipBackground, const glm::vec2 & size);
/// Directly start recording.
bool startDirectRecording(const std::string & path, Recorder::Format format, int framerate, int bitrate, bool skipBackground, const glm::vec2 & size);

void setGUIScale(float scale);

Expand Down

0 comments on commit 6ef0b65

Please sign in to comment.