From: Daniel Karbach Date: Sun, 3 Nov 2024 12:32:02 +0000 (+0100) Subject: move config to json file X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=623ee1db8ac6c23e39e40b5355d8dfd71ac99ac0;p=ffmpeg-test.git move config to json file --- diff --git a/.gitignore b/.gitignore index ae1bbc3..1a45de1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .gdb_history compile_flags.txt +config.json main out.flv test.mkv diff --git a/src/app/Application.h b/src/app/Application.h index 7629e59..79f0e55 100644 --- a/src/app/Application.h +++ b/src/app/Application.h @@ -6,6 +6,7 @@ #include #include "ChannelInfo.h" +#include "Config.h" #include "DrawingGame.h" #include "Mixer.h" #include "Renderer.h" @@ -23,19 +24,18 @@ namespace app { class Application { public: - Application(int width, int height, int fps, const char *url) - : net() + explicit Application(const Config &config) + : config(config) + , net() , loop() , ws_ctx(loop) , pusher_conn(ws_ctx) , twitch_conn(ws_ctx) - , stream(url, width, height, fps) + , stream(config.url.c_str(), config.width, config.height, config.fps) , mixer(stream.GetAudioPlane(), stream.GetAudioChannels(), stream.GetAudioFrameSize()) - , renderer(stream.GetVideoPlane(), stream.GetVideoLineSize(), width, height) - , state(width, height) - , own_channel_id("1020523186") + , renderer(stream.GetVideoPlane(), stream.GetVideoLineSize(), config.width, config.height) + , state(config.width, config.height) , enable_realtime(false) - , enable_shoutouts(false) , drawing_game(renderer.GetContext(), 45, 50, { 1, 1, 1 }) { state.SetGame(&drawing_game); } @@ -50,10 +50,6 @@ public: enable_realtime = true; } - void EnableShoutouts() { - enable_shoutouts = true; - } - void Start() { std::cout << "starting services" << std::endl; pusher_conn.Subscribe("Channel").Listen([this](const Json::Value &json) -> void { @@ -62,7 +58,7 @@ public: pusher_conn.Subscribe("ChatBotLog").Listen([this](const Json::Value &json) -> void { HandlePusherChatBotLog(json); }); - twitch_conn.Join("#horstiebot").Listen([this](const twitch::IRCMessage &msg) -> void { + twitch_conn.Join(config.chat_channel).Listen([this](const twitch::IRCMessage &msg) -> void { HandleTwitch(msg); }); ws_ctx.HttpsRequest("GET", "alttp.localhorst.tv", "/api/channels?chatting=1").GetPromise().Then([this](ws::HttpsConnection *rsp) -> void { @@ -132,7 +128,7 @@ private: ChannelInfo &info = state.GetChannelInfo(channel_id); info.Update(channel); } - ShoutoutChannel(33); + //ShoutoutChannel(33); } void HandlePusherChannel(const Json::Value &json) { @@ -169,9 +165,9 @@ private: void ShoutoutChannel(int id) { ChannelInfo &channel = state.GetChannelInfo(id); Shoutout &shout = renderer.CreateShoutout(id, state); - if (!channel.twitch_id.empty() && channel.twitch_id != own_channel_id) { - if (enable_shoutouts) { - twitch_conn.Shoutout(own_channel_id, channel.twitch_id); + if (!channel.twitch_id.empty() && channel.twitch_id != config.own_channel_id) { + if (config.shoutouts) { + twitch_conn.Shoutout(config.own_channel_id, channel.twitch_id); } shout.FetchClip(twitch_conn); } @@ -213,6 +209,7 @@ private: } private: + const Config &config; ffmpeg::Network net; uv::Loop loop; ws::Context ws_ctx; @@ -222,9 +219,7 @@ private: Mixer mixer; Renderer renderer; State state; - std::string own_channel_id; bool enable_realtime; - bool enable_shoutouts; DrawingGame drawing_game; diff --git a/src/app/Config.h b/src/app/Config.h new file mode 100644 index 0000000..97175f1 --- /dev/null +++ b/src/app/Config.h @@ -0,0 +1,56 @@ +#ifndef TEST_APP_CONFIG_H_ +#define TEST_APP_CONFIG_H_ + +#include + +#include + +namespace app { + +class Config { + +public: + void Load() { + std::ifstream in("config.json"); + Json::Value json; + in >> json; + + if (json.isMember("width")) { + width = json["width"].asInt(); + } + if (json.isMember("height")) { + height = json["height"].asInt(); + } + if (json.isMember("fps")) { + fps = json["fps"].asInt(); + } + if (json.isMember("url")) { + url = json["url"].asString(); + } + if (json.isMember("shoutouts")) { + shoutouts = json["shoutouts"].asBool(); + } + if (json.isMember("chat_channel")) { + chat_channel = json["chat_channel"].asString(); + } + if (json.isMember("own_channel_id")) { + own_channel_id = json["own_channel_id"].asString(); + } + } + +public: + int width = 1280; + int height = 720; + int fps = 60; + + std::string url = "rtmp://localhost/horstiebot"; + bool shoutouts = true; + + std::string chat_channel = "#horstiebot"; + std::string own_channel_id = "1020523186"; + +}; + +} + +#endif diff --git a/src/app/Shoutout.h b/src/app/Shoutout.h index 0da797e..70a24fc 100644 --- a/src/app/Shoutout.h +++ b/src/app/Shoutout.h @@ -92,6 +92,7 @@ public: .Catch([this](ws::HttpsConnection *rsp) -> void { fetching_clip = false; std::cout << "failed to fetch clips" << std::endl; + std::cout << rsp->GetBody() << std::endl; }); } diff --git a/src/main.cpp b/src/main.cpp index 92b7370..c608e11 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include #include "app/Application.h" +#include "app/Config.h" namespace { @@ -16,20 +17,15 @@ void stop(int) { int main(int argc, char**argv) { - const int WIDTH = 1280; - const int HEIGHT = 720; - const int FPS = 60; - //const char *URL = "rtmp://localhost/horstiebot"; - //const char *URL = "rtmp://localhost/localhorsttv"; - //const char *URL = "out.flv"; + app::Config config; + config.Load(); - const char *URL = argc > 1 ? argv[1] : "rtmp://localhost/localhorsttv"; + if (argc > 1){ + config.url = argv[1]; + } - app::Application app(WIDTH, HEIGHT, FPS, URL); + app::Application app(config); app.EnableRealtime(); - if (std::strcmp(URL, "rtmp://localhost/horstiebot") == 0) { - app.EnableShoutouts(); - } signal(SIGINT, stop); diff --git a/src/twitch/LoginToken.cpp b/src/twitch/LoginToken.cpp index 91cd71f..3b8a61e 100644 --- a/src/twitch/LoginToken.cpp +++ b/src/twitch/LoginToken.cpp @@ -31,12 +31,10 @@ LoginToken::PromiseType &LoginToken::Refresh(ws::Context &ws) { void LoginToken::HandleRefreshComplete(ws::HttpsConnection &rsp) { is_refreshing = false; - std::cout << "completed https request with status " << rsp.GetStatus() << std::endl; - std::cout << "body: " << rsp.GetBody() << std::endl; - if (rsp.GetStatus() != 200) return; - // access_token - // refresh_token - // expires_in (seconds) + if (rsp.GetStatus() != 200) { + HandleRefreshError(rsp); + return; + } Json::Value json; json_reader.parse(rsp.GetBody(), json); access_token = json["access_token"].asString(); @@ -46,14 +44,16 @@ void LoginToken::HandleRefreshComplete(ws::HttpsConnection &rsp) { std::time(&now); expires = now + expires_in; Save(); - promise.Resolve(this); + PromiseType saved(promise); + promise = PromiseType(); + saved.Resolve(this); } void LoginToken::HandleRefreshError(ws::HttpsConnection &rsp) { is_refreshing = false; - std::cout << "errored https request with status " << rsp.GetStatus() << std::endl; - std::cout << "body: " << rsp.GetBody() << std::endl; - promise.Reject(&rsp); + PromiseType saved(promise); + promise = PromiseType(); + saved.Reject(&rsp); } }