]> git.localhorst.tv Git - ffmpeg-test.git/commitdiff
move config to json file
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 3 Nov 2024 12:32:02 +0000 (13:32 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 3 Nov 2024 12:32:02 +0000 (13:32 +0100)
.gitignore
src/app/Application.h
src/app/Config.h [new file with mode: 0644]
src/app/Shoutout.h
src/main.cpp
src/twitch/LoginToken.cpp

index ae1bbc3bd363b05d8cf353d65c92546940335c44..1a45de10135ab32eac74e368ff80886477b60512 100644 (file)
@@ -1,5 +1,6 @@
 .gdb_history
 compile_flags.txt
+config.json
 main
 out.flv
 test.mkv
index 7629e593dda70fcb33f33839527dc9e75e37ba7a..79f0e552a08ab2de2830afc23f6125705e935be3 100644 (file)
@@ -6,6 +6,7 @@
 #include <json/value.h>
 
 #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 (file)
index 0000000..97175f1
--- /dev/null
@@ -0,0 +1,56 @@
+#ifndef TEST_APP_CONFIG_H_
+#define TEST_APP_CONFIG_H_
+
+#include <fstream>
+
+#include <json/json.h>
+
+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
index 0da797ea771b76d100e39e53709c433fa10baa3c..70a24fcc1573fc2a9f5888216439e3d2f8f46b81 100644 (file)
@@ -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;
                        });
        }
 
index 92b7370cb3cdf4a813a249feb0013eba9a242ab7..c608e11a5ab21011bb285c962e9ecf09369f332e 100644 (file)
@@ -3,6 +3,7 @@
 #include <iostream>
 
 #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);
 
index 91cd71f1f68b36ac0d8946f8606c40508e623dd1..3b8a61ecfaafb86a3cb34066c23b64f1be593858 100644 (file)
@@ -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);
 }
 
 }