Skip to content

Commit

Permalink
Merge pull request #1 from mickmack1213/push-dependency-versions
Browse files Browse the repository at this point in the history
Push dependency versions, improve error handling
  • Loading branch information
andre-ryll authored Aug 4, 2024
2 parents 82bb8b8 + e302e5e commit 353839e
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 50 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Build

on: push

jobs:
build:

runs-on: ubuntu-24.04

steps:
- uses: actions/checkout@v4
with:
submodules: 'true'
fetch-depth: 0
- name: Set up Dependencies
run: sudo apt update && sudo apt --yes install build-essential ffmpeg libglfw3-dev libprotobuf-dev cmake libavcodec-dev libavdevice-dev libavfilter-dev libavformat-dev libavutil-dev libpostproc-dev libswresample-dev libswscale-dev protobuf-compiler
- name: Build
run: mkdir build && cd build && cmake .. && make -j
46 changes: 29 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(BLEND2D_STATIC TRUE)
include("blend2d/CMakeLists.txt")

find_package(FFmpeg REQUIRED COMPONENTS AVCODEC AVFORMAT AVUTIL SWSCALE SWRESAMPLE)
find_package(Protobuf REQUIRED)
find_package(ZLIB REQUIRED)

protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS
set(PROTO_RAW_FILES
proto/ssl_gc_referee_message.proto
proto/ssl_gc_game_event.proto
proto/ssl_gc_common.proto
Expand All @@ -34,13 +30,25 @@ protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS
proto/ssl_vision_detection_tracked.proto
)

find_package(FFmpeg REQUIRED COMPONENTS AVCODEC AVFORMAT AVUTIL SWSCALE SWRESAMPLE)
find_package(Protobuf CONFIG)
if(NOT Protobuf_FOUND)
set(PROTO_ALTERNATIVE_VERSION TRUE)
find_package(Protobuf REQUIRED)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_RAW_FILES})
else()
set(PROTO_ALTERNATIVE_VERSION FALSE)
set(PROTO_SRCS ${PROTO_RAW_FILES})
endif()
find_package(ZLIB REQUIRED)

include_directories(
imgui
imgui/backends
ImGuiFileDialog
src
src/util

${Protobuf_INCLUDE_DIRS}
${CMAKE_CURRENT_BINARY_DIR}
)
Expand All @@ -49,57 +57,61 @@ add_compile_options(
-Wno-deprecated-declarations
)

add_executable(${PROJECT_NAME}
add_executable(${PROJECT_NAME}
src/main.cpp
src/Application.cpp
src/TigersClav.cpp
src/LogViewer.cpp

src/data/SSLGameLog.cpp
src/data/MediaSource.cpp
src/data/MediaEncoder.cpp

src/gui/ImageComposer.cpp
src/gui/AScoreBoard.cpp
src/gui/FancyScoreBoard.cpp
src/gui/ProgrammerScoreBoard.cpp
src/gui/ScoreBoardFactory.cpp
src/gui/FieldVisualizer.cpp

src/model/Project.cpp
src/model/Camera.cpp
src/model/GameLog.cpp
src/model/Director.cpp
src/model/VideoProducer.cpp

src/util/gzstream.cpp
src/util/easylogging++.cc
src/util/CustomFont.cpp
src/util/gl3w.c
src/util/ShaderProgram.cpp

ImGuiFileDialog/ImGuiFileDialog.cpp

imgui/imgui.cpp
imgui/imgui_draw.cpp
imgui/imgui_demo.cpp
imgui/imgui_tables.cpp
imgui/imgui_widgets.cpp

imgui/backends/imgui_impl_opengl3.cpp
imgui/backends/imgui_impl_glfw.cpp

${PROTO_SRCS}
)

if(NOT PROTO_ALTERNATIVE_VERSION)
protobuf_generate(TARGET ${PROJECT_NAME} IMPORT_DIRS proto)
endif()

add_definitions(
-DCUSTOM_IMGUIFILEDIALOG_CONFIG="../src/util/CustomImGuiFileDialogConfig.h"
-DELPP_STL_LOGGING
-DELPP_THREAD_SAFE)

if(WIN32)
target_link_libraries(${PROJECT_NAME} glfw3 opengl32 Blend2D::Blend2D ${ZLIB_LIBRARIES} ${Protobuf_LIBRARIES} ${FFMPEG_LIBRARIES} -static)
target_link_libraries(${PROJECT_NAME} glfw3 opengl32 Blend2D::Blend2D ${ZLIB_LIBRARIES} protobuf::libprotobuf ${FFMPEG_LIBRARIES} -static)
target_link_options(${PROJECT_NAME} PRIVATE -mwindows)
else()
target_link_libraries(${PROJECT_NAME} glfw GL Blend2D::Blend2D ${ZLIB_LIBRARIES} ${Protobuf_LIBRARIES} ${FFMPEG_LIBRARIES})
target_link_libraries(${PROJECT_NAME} glfw GL Blend2D::Blend2D ${ZLIB_LIBRARIES} protobuf::libprotobuf ${FFMPEG_LIBRARIES})
endif()
8 changes: 6 additions & 2 deletions src/LogViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void LogViewer::handle(const el::LogDispatchData* data) noexcept
entry.text = data->logMessage()->logger()->logBuilder()->build(data->logMessage(),
data->dispatchAction() == el::base::DispatchAction::NormalLog);

std::unique_lock<std::mutex> lock(entriesMutex_);
entries_.emplace_back(std::move(entry));
}

Expand All @@ -26,9 +27,12 @@ void LogViewer::render()

ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Tighten spacing

for(const auto& entry : entries_)
{
ImGui::Text("%s", entry.text.c_str());
std::unique_lock<std::mutex> lock(entriesMutex_);
for(const auto& entry : entries_)
{
ImGui::Text("%s", entry.text.c_str());
}
}

if(autoScroll_)
Expand Down
2 changes: 2 additions & 0 deletions src/LogViewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "imgui.h"
#include <vector>
#include <string>
#include <mutex>

class LogViewer : public el::LogDispatchCallback
{
Expand All @@ -23,6 +24,7 @@ class LogViewer : public el::LogDispatchCallback
};

std::vector<LogEntry> entries_;
std::mutex entriesMutex_;

bool autoScroll_;
bool showDemoWindow_;
Expand Down
6 changes: 5 additions & 1 deletion src/TigersClav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,11 @@ void TigersClav::drawSyncPanel()
ImGui::SameLine(0.0f, 0.0f);
}
ImVec2 logBtnScreenPos = ImGui::GetCursorScreenPos();
ImGui::InvisibleButton("##log", ImVec2(pGameLog->getTotalDuration_ns() * scaleX, heightGameLog));
float logBtnWidth = pGameLog->getTotalDuration_ns() * scaleX;
if(logBtnWidth < 1)
logBtnWidth = 1;

ImGui::InvisibleButton("##log", ImVec2(logBtnWidth, heightGameLog));

ImVec2 logBtnSize = ImGui::GetItemRectSize();

Expand Down
2 changes: 2 additions & 0 deletions src/data/AVWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class AVPacketWrapper
AVPacket* operator->() { return pPacket; }
AVPacket& operator*() { return *pPacket; }

operator bool() { return pPacket != 0; }

private:
AVPacket* pPacket;
};
Expand Down
63 changes: 47 additions & 16 deletions src/data/MediaEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ bool MediaEncoder::initialize(std::shared_ptr<const MediaFrame> pFrame)

if(pAudio->format != AV_SAMPLE_FMT_FLTP)
{
pResampler_ = swr_alloc_set_opts(NULL, pAudio->channel_layout, AV_SAMPLE_FMT_FLTP, pAudio->sample_rate,
pAudio->channel_layout, (enum AVSampleFormat)pAudio->format, pAudio->sample_rate, 0, NULL);
swr_alloc_set_opts2(&pResampler_, &pAudio->ch_layout, AV_SAMPLE_FMT_FLTP, pAudio->sample_rate, &pAudio->ch_layout,
(enum AVSampleFormat)pAudio->format, pAudio->sample_rate, 0, NULL);
if(!pResampler_)
{
LOG(ERROR) << "Failed to create audio resampler.";
Expand Down Expand Up @@ -165,8 +165,7 @@ bool MediaEncoder::initialize(std::shared_ptr<const MediaFrame> pFrame)
return false;
}

pAudioCodecContext_->channels = (*pFrame->pSamples)->channels;
pAudioCodecContext_->channel_layout = (*pFrame->pSamples)->channel_layout;
pAudioCodecContext_->ch_layout = (*pFrame->pSamples)->ch_layout;
pAudioCodecContext_->sample_rate = (*pFrame->pSamples)->sample_rate;
pAudioCodecContext_->sample_fmt = AV_SAMPLE_FMT_FLTP;
pAudioCodecContext_->bit_rate = pFrame->audioBitRate;
Expand All @@ -184,7 +183,7 @@ bool MediaEncoder::initialize(std::shared_ptr<const MediaFrame> pFrame)

avcodec_parameters_from_context(pAudioStream_->codecpar, pAudioCodecContext_);

LOG(INFO) << "Encoder audio: " << pAudioCodec_->name << ", channels: " << pAudioCodecContext_->channels << ", sample rate: " << pAudioCodecContext_->sample_rate
LOG(INFO) << "Encoder audio: " << pAudioCodec_->name << ", channels: " << pAudioCodecContext_->ch_layout.nb_channels << ", sample rate: " << pAudioCodecContext_->sample_rate
<< ", bit rate: " << pAudioCodecContext_->bit_rate << ", frame_size: " << pAudioCodecContext_->frame_size;
}

Expand Down Expand Up @@ -273,13 +272,36 @@ int MediaEncoder::sendVideoFrame(const AVFrame* pVideo)
auto tStart = std::chrono::high_resolution_clock::now();

AVFrame* pEnc = av_frame_alloc();
if(!pEnc)
{
LOG(ERROR) << "Failed to allocate frame.";
return -1;
}

pEnc->width = pVideo->width;
pEnc->height = pVideo->height;
pEnc->format = pVideo->format;

av_frame_get_buffer(pEnc, 0);
av_frame_copy(pEnc, pVideo);
av_frame_copy_props(pEnc, pVideo);
result = av_frame_get_buffer(pEnc, 0);
if(result < 0)
{
LOG(ERROR) << "av_frame_get_buffer (video) error: " << err2str(result);
return -1;
}

result = av_frame_copy(pEnc, pVideo);
if(result < 0)
{
LOG(ERROR) << "av_frame_copy (video) error: " << err2str(result);
return -1;
}

result = av_frame_copy_props(pEnc, pVideo);
if(result < 0)
{
LOG(ERROR) << "av_frame_copy_props (video) error: " << err2str(result);
return -1;
}

auto tCopy = std::chrono::high_resolution_clock::now();
videoTiming_.copy = std::chrono::duration_cast<std::chrono::microseconds>(tCopy - tStart).count() * 1e-6f;
Expand Down Expand Up @@ -314,6 +336,12 @@ int MediaEncoder::receiveVideoPackets()
AVPacketWrapper packet;
auto tStart = std::chrono::high_resolution_clock::now();

if(!packet)
{
LOG(ERROR) << "No memory for AVPacketWrapper.";
return -1;
}

result = avcodec_receive_packet(pVideoCodecContext_, packet);
if(result == AVERROR(EAGAIN) || result == AVERROR_EOF)
{
Expand Down Expand Up @@ -365,7 +393,7 @@ int MediaEncoder::sendAudioFrameFromBuffer(bool flush)
bufferedSamples += (*buf.pSamples)->nb_samples - buf.firstSample;
}

if(bufferedSamples < pAudioCodecContext_->frame_size && !flush)
if(bufferedSamples < pAudioCodecContext_->frame_size && (!flush || bufferedSamples == 0))
{
// not enough audio samples for a full frame
return 0;
Expand All @@ -382,8 +410,7 @@ int MediaEncoder::sendAudioFrameFromBuffer(bool flush)
pAudioBuf->nb_samples = encSamples;
pAudioBuf->format = pAudio->format;
pAudioBuf->sample_rate = pAudio->sample_rate;
pAudioBuf->channel_layout = pAudio->channel_layout;
pAudioBuf->channels = pAudio->channels;
pAudioBuf->ch_layout = pAudio->ch_layout;
pAudioBuf->pts = curAudioPts_;
curAudioPts_ += audioPtsInc * encSamples;

Expand All @@ -399,7 +426,7 @@ int MediaEncoder::sendAudioFrameFromBuffer(bool flush)
if(copySize > samplesLeft)
copySize = samplesLeft;

av_samples_copy(pAudioBuf->data, (*iter->pSamples)->data, dstOffset, iter->firstSample, copySize, pAudioBuf->channels, (enum AVSampleFormat)pAudioBuf->format);
av_samples_copy(pAudioBuf->data, (*iter->pSamples)->data, dstOffset, iter->firstSample, copySize, pAudioBuf->ch_layout.nb_channels, (enum AVSampleFormat)pAudioBuf->format);

samplesLeft -= copySize;
dstOffset += copySize;
Expand All @@ -420,11 +447,15 @@ int MediaEncoder::sendAudioFrameFromBuffer(bool flush)
pAudioEnc->nb_samples = pAudioBuf->nb_samples;
pAudioEnc->format = AV_SAMPLE_FMT_FLTP;
pAudioEnc->sample_rate = pAudioBuf->sample_rate;
pAudioEnc->channel_layout = pAudioBuf->channel_layout;
pAudioEnc->channels = pAudioBuf->channels;
pAudioEnc->ch_layout = pAudioBuf->ch_layout;
pAudioEnc->pts = pAudioBuf->pts;

LOG_IF(debug_, INFO) << "Encoding audio frame. PTS: " << pAudioEnc->pts << ", channel_layout: " << pAudioEnc->channel_layout;
if(debug_)
{
char layoutString[128];
av_channel_layout_describe(&pAudioEnc->ch_layout, layoutString, sizeof(layoutString));
LOG(INFO) << "Encoding audio frame. PTS: " << pAudioEnc->pts << ", channel_layout: " << layoutString;
}

av_frame_get_buffer(pAudioEnc, 0);

Expand All @@ -449,7 +480,7 @@ int MediaEncoder::sendAudioFrameFromBuffer(bool flush)
if(result < 0)
{
LOG(ERROR) << "avcodec_send_frame (audio) error: " << err2str(result);
return -1;
// Just keep going, this mostly works and gets around encoder hiccups
}

av_frame_free(&pAudioBuf);
Expand Down
Loading

0 comments on commit 353839e

Please sign in to comment.