Skip to content

Commit

Permalink
added better printing (#10)
Browse files Browse the repository at this point in the history
* added better printing

* ci
  • Loading branch information
Mast3rwaf1z authored Nov 26, 2024
1 parent 1cb1d94 commit b4f29a3
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 178 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-24.04
steps:
- name: install dependencies
run: sudo apt-get update && sudo apt-get -y install cmake libopencv-dev
run: sudo apt-get update && sudo apt-get -y install cmake libopencv-dev libspdlog-dev
- uses: actions/cache@v4
with:
path: build
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(argparse)

#spdlog
find_package(spdlog REQUIRED)

add_executable(sender src/Sender.cpp ${PROJECT_IMPLEMENTATIONS})

target_link_libraries(sender
${OpenCV_LIBS}
argparse
spdlog::spdlog
)

1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
opencv
ffmpeg
cmake
spdlog
];
};
};
Expand Down
13 changes: 7 additions & 6 deletions include/FFmpeg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@

namespace Hyperwall {

class FFmpeg{
class FFmpeg{
FILE* buffer;
public:
public:
const std::string url;

FFmpeg(const std::string, const std::string);
const void write(const cv::Mat&) const;
};
const cv::Mat read(int, int) const;
};

class FFmpegBuilder {
class FFmpegBuilder {
std::vector<std::string> options;
std::string _url;

public:
public:
FFmpegBuilder& add(const std::string);
FFmpegBuilder& add(const std::string, const std::string);
FFmpegBuilder& url(const std::string);
const FFmpeg build(std::string);
};
};
} // Hyperwall
35 changes: 18 additions & 17 deletions include/Hyperwall.hpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
#pragma once

#include <unordered_map>

#include "FFmpeg.hpp"
#include "Sources/VideoSource.hpp"
#include <unordered_map>

namespace Hyperwall {

class HyperFrame {
const int x;
const int y;
const int X;
const int Y;
const int RES_X;
const int RES_Y;
const FFmpeg ffmpeg;
const int x;
const int y;
const int X;
const int Y;
const int RES_X;
const int RES_Y;
const FFmpeg ffmpeg;
public:
HyperFrame(const HyperFrame&);
HyperFrame(const int, const int, std::unordered_map<std::string, std::string>, const FFmpeg&);
void run(const cv::Mat&);
HyperFrame(const HyperFrame&);
HyperFrame(const int, const int, std::unordered_map<std::string, std::string>, const FFmpeg&);
void run(const cv::Mat&);
};

class Hyperwall {
std::unique_ptr<VideoSourceT> source;
const int X;
const int Y;
std::unordered_map<int, std::unordered_map<int, HyperFrame>> frames;
std::unique_ptr<VideoSourceT> source;
std::unordered_map<int, std::unordered_map<int, HyperFrame>> frames;
const int X;
const int Y;
public:
Hyperwall(VideoSourceT&, std::unordered_map<std::string, std::string>);
void run();
Hyperwall(VideoSourceT&, std::unordered_map<std::string, std::string>);
void run();
};

} // Hyperwall
9 changes: 4 additions & 5 deletions include/Sources/FileSource.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#pragma once

#include "Sources/VideoSource.hpp"
#include <opencv2/videoio.hpp>

namespace Hyperwall {

class FileSource : public VideoSourceT {
cv::VideoCapture capture;
cv::VideoCapture capture;

public:
FileSource(std::string);
virtual cv::Mat read() override;
virtual std::unique_ptr<VideoSourceT> clone() const override;
FileSource(std::string);
virtual cv::Mat read() override;
virtual std::unique_ptr<VideoSourceT> clone() const override;
};

} // Hyperwall
6 changes: 3 additions & 3 deletions include/Sources/VideoSource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Hyperwall {

class VideoSourceT {
public:
virtual cv::Mat read() = 0;
virtual ~VideoSourceT() = default;
virtual std::unique_ptr<VideoSourceT> clone() const = 0;
virtual cv::Mat read() = 0;
virtual ~VideoSourceT() = default;
virtual std::unique_ptr<VideoSourceT> clone() const = 0;
};

} // Hyperwall
9 changes: 5 additions & 4 deletions include/Sources/WebcamSource.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#pragma once

#include "Sources/VideoSource.hpp"

namespace Hyperwall {

class WebcamSource : public VideoSourceT {
cv::VideoCapture capture;
cv::VideoCapture capture;
public:
WebcamSource(const int);
virtual cv::Mat read() override;
virtual std::unique_ptr<VideoSourceT> clone() const override;
WebcamSource(const int);
virtual cv::Mat read() override;
virtual std::unique_ptr<VideoSourceT> clone() const override;
};

} // Hyperwall
22 changes: 22 additions & 0 deletions include/Utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#include <vector>

namespace Util {

template<typename T = int>
constexpr std::vector<T> range(const int n) {
std::vector<T> values;
for(auto i = 0; i < n; i++)
values.push_back(i);
return values;
}

template<typename T = int>
constexpr std::vector<T> range(const int start, const int end) {
std::vector<T> values;
for(auto i = start; i < end; i++)
values.push_back(i);
return values;
}

}
112 changes: 68 additions & 44 deletions src/Sender.cpp
Original file line number Diff line number Diff line change
@@ -1,63 +1,87 @@
#include <cstdlib>
#include <opencv2/core/mat.hpp>
#include <opencv2/opencv.hpp>
#include <filesystem>
#include <spdlog/common.h>
#include <string>
#include <unordered_map>

#include <argparse/argparse.hpp>
#include <opencv2/opencv.hpp>
#include <spdlog/spdlog.h>

#include "Hyperwall.hpp"
#include "Sources/FileSource.hpp"
#include "Sources/WebcamSource.hpp"

std::string split_res(std::string res, std::string direction) {
if (direction == "x") {
return res.substr(0, res.find("x"));
}
return res.substr(res.find("x")+1, res.size());
if (direction == "x") {
return res.substr(0, res.find("x"));
}
return res.substr(res.find("x")+1, res.size());
}

int main(int argc, char* argv[]) {
argparse::ArgumentParser parser;
parser.add_argument("--dimensions")
.default_value("2x2");
parser.add_argument("--resolution")
.default_value("1920x1080");
parser.add_argument("--framerate")
.default_value("60");
parser.add_argument("--file")
.default_value("file.mp4");
parser.add_argument("mode")
.default_value("file");
auto loglevel = (int)spdlog::level::info;

try {
parser.parse_args(argc, argv);
} catch(std::exception& e) {
std::cerr << "fuck, exiting..." << std::endl;
exit(EXIT_FAILURE);
}
argparse::ArgumentParser parser;
parser.add_argument("--dimensions")
.default_value("2x2");
parser.add_argument("--resolution")
.default_value("1920x1080");
parser.add_argument("--framerate")
.default_value("60");
parser.add_argument("--file")
.default_value("file.mp4");
parser.add_argument("mode")
.default_value("file");
parser.add_argument("-v")
.action([&](const auto &){loglevel--;})
.append()
.default_value(false)
.implicit_value(true)
.nargs(0);
parser.add_argument("-q")
.action([&](const auto &){loglevel++;})
.append()
.default_value(false)
.implicit_value(true)
.nargs(0);

std::unordered_map<std::string, std::string> settings({
{"RES_X", split_res(parser.get<std::string>("--resolution"), "x")},
{"RES_Y", split_res(parser.get<std::string>("--resolution"), "y")},
{"X", split_res(parser.get<std::string>("--dimensions"), "x")},
{"Y", split_res(parser.get<std::string>("--dimensions"), "y")},
{"FRAMERATE", parser.get<std::string>("--framerate")}
});
Hyperwall::Hyperwall hyperwall = [&settings, &parser](std::string mode) {
if(mode == "webcam") {
Hyperwall::WebcamSource source(0);
Hyperwall::Hyperwall hyperwall(source, settings);
return hyperwall;
try {
parser.parse_args(argc, argv);
} catch(std::exception& e) {
spdlog::error("Failed to parse arguments, exiting!");
exit(EXIT_FAILURE);
}
else {
Hyperwall::FileSource source(parser.get<std::string>("--file"));
Hyperwall::Hyperwall hyperwall(source, settings);
return hyperwall;
spdlog::set_level(static_cast<spdlog::level::level_enum>(loglevel));
spdlog::info("Log level: {}", loglevel);
spdlog::debug("Generating settings");
std::unordered_map<std::string, std::string> settings({
{"RES_X", split_res(parser.get<std::string>("--resolution"), "x")},
{"RES_Y", split_res(parser.get<std::string>("--resolution"), "y")},
{"X", split_res(parser.get<std::string>("--dimensions"), "x")},
{"Y", split_res(parser.get<std::string>("--dimensions"), "y")},
{"FRAMERATE", parser.get<std::string>("--framerate")}
});
Hyperwall::Hyperwall hyperwall = [&settings, &parser](std::string mode) {
spdlog::info("Chosen mode: {}", mode);
if(mode == "webcam") {
Hyperwall::WebcamSource source(0);
Hyperwall::Hyperwall hyperwall(source, settings);
return hyperwall;
}
else {
auto filename = parser.get<std::string>("--file");
if(!std::filesystem::exists(std::filesystem::path(filename))){
spdlog::error("File: {} does not exist!", filename);
exit(EXIT_FAILURE);
}
Hyperwall::FileSource source(filename);
Hyperwall::Hyperwall hyperwall(source, settings);
return hyperwall;
}
}(parser.get<std::string>("mode"));
{
hyperwall.run();
}
}(parser.get<std::string>("mode"));
{
hyperwall.run();
}
exit(EXIT_SUCCESS);
exit(EXIT_SUCCESS);
}
45 changes: 24 additions & 21 deletions src/impl/FFmpeg.cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
#include "FFmpeg.hpp"
#include <cstdio>
#include <format>
#include <opencv2/opencv.hpp>
#include <spdlog/spdlog.h>
#include <sstream>
#include <string>

#include <opencv2/opencv.hpp>

#include "FFmpeg.hpp"

Hyperwall::FFmpegBuilder& Hyperwall::FFmpegBuilder::add(const std::string option) {
options.push_back(option);
return *this;
options.push_back(option);
spdlog::debug("Adding option: {}", option);
return *this;
}

Hyperwall::FFmpegBuilder& Hyperwall::FFmpegBuilder::add(const std::string key, const std::string value) {
options.push_back(std::format("{} {}", key, value));
return *this;
options.push_back(std::format("{} {}", key, value));
spdlog::debug("Adding option: {} identified by: {}", value, key);
return *this;
}

Hyperwall::FFmpegBuilder& Hyperwall::FFmpegBuilder::url(const std::string url) {
this->_url = url;
add(url);
return *this;
this->_url = url;
add(url);
return *this;
}

const Hyperwall::FFmpeg Hyperwall::FFmpegBuilder::build(std::string logname) {
std::stringstream ss;
ss << "ffmpeg ";
for(auto option : options) {
ss << option << " ";
}
ss << std::format(">> ./logs/stdout-{0} 2> ./logs/stderr-{0}", logname);
return {ss.str(), _url};
std::stringstream ss;
ss << "ffmpeg ";
for(auto option : options)
ss << option << " ";
ss << std::format(">> ./logs/stdout-{0} 2> ./logs/stderr-{0}", logname);
return {ss.str(), _url};
}

Hyperwall::FFmpeg::FFmpeg(const std::string command, const std::string url) : url(url) {
//std::cout << command << std::endl;
buffer = popen(command.c_str(), "w");
buffer = popen(command.c_str(), "w");
}

const void Hyperwall::FFmpeg::write(const cv::Mat& mat) const {
//std::cout << url << std::endl;
fwrite(mat.data, 1, mat.cols * mat.rows * 3, buffer);
fflush(buffer);
//std::cout << url << std::endl;
fwrite(mat.data, 1, mat.cols * mat.rows * 3, buffer);
fflush(buffer);
}

Loading

0 comments on commit b4f29a3

Please sign in to comment.