Skip to content

Commit

Permalink
fix: fix clang-tidy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Totto16 committed Nov 14, 2024
1 parent 8718ce0 commit 50daebe
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/graphics/video_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct VideoRendererBackend {
get_encoding_paramaters(u32 fps, shapes::UPoint size, const std::filesystem::path& destination_path);

public:
OOPETRIS_GRAPHICS_EXPORTED explicit VideoRendererBackend(const std::filesystem::path& destination_path);
OOPETRIS_GRAPHICS_EXPORTED explicit VideoRendererBackend(std::filesystem::path destination_path);

OOPETRIS_GRAPHICS_EXPORTED ~VideoRendererBackend();

Expand Down
45 changes: 26 additions & 19 deletions src/graphics/video_renderer_embedded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,34 @@ struct Decoder {

// general information and usage from: https://ffmpeg.org//doxygen/trunk/index.html
// and https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/README
VideoRendererBackend::VideoRendererBackend(const std::filesystem::path& destination_path)
: m_destination_path{ destination_path },
VideoRendererBackend::VideoRendererBackend(std::filesystem::path destination_path)
: m_destination_path{ std::move(destination_path) },
m_decoder{ nullptr } { }

VideoRendererBackend::~VideoRendererBackend() = default;

namespace {

constexpr const int READ_END = 0;
constexpr const int WRITE_END = 1;
constexpr const int read_end = 0;
constexpr const int write_end = 1;

constexpr const size_t BUF_LEN = 1024;
constexpr const size_t buf_len = 1024;

std::string av_error_to_string(int errnum) {
auto* buf = new char[BUF_LEN];
auto* buff_res = av_make_error_string(buf, BUF_LEN, errnum);
auto* buf = new char[buf_len]; //NOLINT(cppcoreguidelines-owning-memory)
auto* buff_res = av_make_error_string(buf, buf_len, errnum);
if (buff_res == nullptr) {
return "Unknown error";
}

std::string result{ buff_res };

delete[] buf;
delete[] buf; //NOLINT(cppcoreguidelines-owning-memory)

return result;
}

std::optional<std::string> start_encoding(
std::optional<std::string> start_encoding( //NOLINT(readability-function-cognitive-complexity)
u32 fps,
shapes::UPoint size,
const std::filesystem::path& destination_path,
Expand Down Expand Up @@ -150,7 +150,8 @@ namespace {
);
}

AVStream* input_video_stream = input_format_ctx->streams[video_stream_index];
AVStream* input_video_stream =
input_format_ctx->streams[video_stream_index]; //NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)

AVCodecContext* input_codec_context = avcodec_alloc_context3(input_decoder);
if (input_codec_context == nullptr) {
Expand Down Expand Up @@ -203,7 +204,7 @@ namespace {
return fmt::format("Could not alloc output file {}: {}", destination_path.string(), av_output_ret);
}

std::string encoder_metadata_name = fmt::format(
const std::string encoder_metadata_name = fmt::format(
"{} v{} ({}) {}", constants::program_name.string(), constants::version.string(), utils::git_commit(),
LIBAVFORMAT_IDENT
);
Expand Down Expand Up @@ -263,7 +264,7 @@ namespace {

out_stream->time_base = output_codec_context->time_base;

std::string stream_encoder_metadata_name = fmt::format(
const std::string stream_encoder_metadata_name = fmt::format(
"{} v{} ({}) {} {}", constants::program_name.string(), constants::version.string(), utils::git_commit(),
LIBAVCODEC_IDENT, output_encoder->name
);
Expand Down Expand Up @@ -354,7 +355,9 @@ namespace {
auto read_frame_ret = av_read_frame(input_format_ctx, pkt);
if (read_frame_ret == AVERROR_EOF) {
break;
} else if (read_frame_ret < 0) {
}

if (read_frame_ret < 0) {
return fmt::format("Receiving a frame from the input failed: {}", av_error_to_string(read_frame_ret));
}

Expand All @@ -376,7 +379,9 @@ namespace {
read_ret = avcodec_receive_frame(input_codec_context, decode_frame);
if (read_ret == AVERROR(EAGAIN) || read_ret == AVERROR_EOF) {
break;
} else if (read_ret < 0) {
}

if (read_ret < 0) {
return fmt::format("Receiving a frame from the decoder failed: {}", av_error_to_string(read_ret));
}

Expand Down Expand Up @@ -404,7 +409,9 @@ namespace {
write_ret = avcodec_receive_packet(output_codec_context, pkt);
if (write_ret == AVERROR(EAGAIN) || write_ret == AVERROR_EOF) {
break;
} else if (write_ret < 0) {
}

if (write_ret < 0) {
return fmt::format(
"Receiving a packet from the encoder failed: {}", av_error_to_string(write_ret)
);
Expand Down Expand Up @@ -474,13 +481,13 @@ std::optional<std::string> VideoRendererBackend::setup(u32 fps, shapes::UPoint s
if (pipe(pipefd.data()) < 0) {
return fmt::format("Could not create a pipe: {}", strerror(errno));
}
int close_fd = pipefd[READ_END];
int input_fd = pipefd[WRITE_END];
std::string input_url = fmt::format("pipe:{}", close_fd);
const int close_fd = pipefd[read_end];
const int input_fd = pipefd[write_end];
const std::string input_url = fmt::format("pipe:{}", close_fd);
#endif

std::future<std::optional<std::string>> encoding_thread =
std::async(std::launch::async, [close_fd, input_url, fps, size, this] -> std::optional<std::string> {
std::async(std::launch::async, [close_fd, input_url, fps, size, this]() -> std::optional<std::string> {
utils::set_thread_name("ffmpeg encoder");
auto result = start_encoding(fps, size, this->m_destination_path, input_url, this->m_decoder);

Expand Down
23 changes: 12 additions & 11 deletions src/graphics/video_renderer_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ struct Decoder {
};

// inspired by: https://github.com/tsoding/musializer/blob/762a729ff69ba1f984b0f2604e0eac08af46327c/src/ffmpeg_linux.c
VideoRendererBackend::VideoRendererBackend(const std::filesystem::path& destination_path)
: m_destination_path{ destination_path },
VideoRendererBackend::VideoRendererBackend(std::filesystem::path destination_path)
: m_destination_path{ std::move(destination_path) },
m_decoder{ nullptr } { }

VideoRendererBackend::~VideoRendererBackend() = default;

namespace {

constexpr const int READ_END = 0;
constexpr const int WRITE_END = 1;
constexpr const int read_end = 0;
constexpr const int write_end = 1;

} // namespace

Expand All @@ -45,17 +45,17 @@ std::optional<std::string> VideoRendererBackend::setup(u32 fps, shapes::UPoint s
return fmt::format("FFMPEG: Could not create a pipe: {}", strerror(errno));
}

pid_t child = fork();
const pid_t child = fork();
if (child < 0) {
return fmt::format("FFMPEG: could not fork a child: {}", strerror(errno));
}

if (child == 0) {
if (dup2(pipefd.at(READ_END), STDIN_FILENO) < 0) {
if (dup2(pipefd.at(read_end), STDIN_FILENO) < 0) {
std::cerr << "FFMPEG CHILD: could not reopen read end of pipe as stdin: " << strerror(errno) << "\n";
std::exit(1);
}
close(pipefd[WRITE_END]);
close(pipefd[write_end]);


auto paramaters = VideoRendererBackend::get_encoding_paramaters(fps, size, m_destination_path);
Expand All @@ -67,7 +67,7 @@ std::optional<std::string> VideoRendererBackend::setup(u32 fps, shapes::UPoint s

args.push_back(nullptr);
//TODO(Totto): support audio, that loops the music as in the main game
int ret =
const int ret =
execvp("ffmpeg",
const_cast<char* const*>(args.data())); // NOLINT(cppcoreguidelines-pro-type-const-cast)
if (ret < 0) {
Expand All @@ -78,11 +78,11 @@ std::optional<std::string> VideoRendererBackend::setup(u32 fps, shapes::UPoint s
std::exit(1);
}

if (close(pipefd[READ_END]) < 0) {
if (close(pipefd[read_end]) < 0) {
spdlog::error("FFMPEG: could not close read end of the pipe on the parent's end: {}", strerror(errno));
}

m_decoder = std::make_unique<Decoder>(pipefd[WRITE_END], child);
m_decoder = std::make_unique<Decoder>(pipefd[write_end], child);
return std::nullopt;
}

Expand All @@ -102,8 +102,9 @@ bool VideoRendererBackend::finish(bool cancel) {
spdlog::warn("FFMPEG: could not close write end of the pipe on the parent's end: {}", strerror(errno));
}

if (cancel)
if (cancel) {
kill(m_decoder->pid, SIGKILL);
}

while (true) {
int wstatus = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/video_renderer_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ struct Decoder {
};

// inspired by: https://github.com/tsoding/musializer/blob/762a729ff69ba1f984b0f2604e0eac08af46327c/src/ffmpeg_windows.c
VideoRendererBackend::VideoRendererBackend(const std::filesystem::path& destination_path)
: m_destination_path{ destination_path },
VideoRendererBackend::VideoRendererBackend(std::filesystem::path destination_path)
: m_destination_path{ std::move(destination_path) },
m_decoder{ nullptr } { }

VideoRendererBackend::~VideoRendererBackend() = default;
Expand Down
12 changes: 7 additions & 5 deletions src/scenes/recording_selector/recording_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ ui::Widget::EventHandleResult custom_ui::RecordingComponent::handle_event(
if (m_current_focus_id == m_main_layout.focus_id()) {
return {
true,
{ ui::EventHandleType::RequestAction, this, nullptr }
{ .handle_type = ui::EventHandleType::RequestAction, .widget = this, .data = nullptr }
};
}

if (m_current_focus_id == render_button->focus_id()) {
return {
true,
{ ui::EventHandleType::RequestAction, render_button, nullptr }
{ .handle_type = ui::EventHandleType::RequestAction, .widget = render_button, .data = nullptr }
};
}

Expand Down Expand Up @@ -162,13 +162,13 @@ ui::Widget::EventHandleResult custom_ui::RecordingComponent::handle_event(
if (not has_focus()) {
return {
true,
{ ui::EventHandleType::RequestFocus, this, nullptr }
{ .handle_type = ui::EventHandleType::RequestFocus, .widget = this, .data = nullptr }
};
}

return {
true,
{ ui::EventHandleType::RequestAction, render_button, this }
{ .handle_type = ui::EventHandleType::RequestAction, .widget = render_button, .data = this }
};
}

Expand All @@ -179,7 +179,9 @@ ui::Widget::EventHandleResult custom_ui::RecordingComponent::handle_event(

return {
true,
{ has_focus() ? ui::EventHandleType::RequestAction : ui::EventHandleType::RequestFocus, this, nullptr }
{ .handle_type = has_focus() ? ui::EventHandleType::RequestAction : ui::EventHandleType::RequestFocus,
.widget = this,
.data = nullptr }
};
}
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/scenes/settings_menu/color_setting_row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ ui::Widget::EventHandleResult detail::ColorSettingRectangle::handle_event(
if (has_focus() and navigation_event == input::NavigationEvent::OK) {
return {
true,
{ ui::EventHandleType::RequestAction, this, nullptr }
{ .handle_type = ui::EventHandleType::RequestAction, .widget = this, .data = nullptr }
};
}

Expand All @@ -74,7 +74,7 @@ ui::Widget::EventHandleResult detail::ColorSettingRectangle::handle_event(
if (hover_result.is(ui::ActionType::Clicked)) {
return {
true,
{ ui::EventHandleType::RequestAction, this, nullptr }
{ .handle_type = ui::EventHandleType::RequestAction, .widget = this, .data = nullptr }
};
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/components/color_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ detail::ColorCanvas::handle_event(const std::shared_ptr<input::InputManager>& in
SDL_CaptureMouse(SDL_TRUE);
handled = {
true,
{ ui::EventHandleType::RequestFocus, this, nullptr }
{ .handle_type = ui::EventHandleType::RequestFocus, .widget = this, .data = nullptr }
};
}
} else if (pointer_event == input::PointerEvent::PointerUp) {
Expand Down
4 changes: 2 additions & 2 deletions src/ui/components/textinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ ui::Widget::EventHandleResult ui::TextInput::handle_event( //NOLINT(readability-
if (hover_result.is(ActionType::Clicked)) {
return {
true,
{ EventHandleType::RequestFocus, this, nullptr }
{ .handle_type = EventHandleType::RequestFocus, .widget = this, .data = nullptr }
};
}

Expand All @@ -129,7 +129,7 @@ ui::Widget::EventHandleResult ui::TextInput::handle_event( //NOLINT(readability-
on_unfocus();
return {
true,
{ EventHandleType::RequestAction, this, nullptr }
{ .handle_type = EventHandleType::RequestAction, .widget = this, .data = nullptr }
};
}
//TODO(Totto): in some cases this is caught before that, and never triggered
Expand Down

0 comments on commit 50daebe

Please sign in to comment.