#include <json/value.h>
#include "ChannelInfo.h"
+#include "Config.h"
#include "DrawingGame.h"
#include "Mixer.h"
#include "Renderer.h"
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);
}
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 {
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 {
ChannelInfo &info = state.GetChannelInfo(channel_id);
info.Update(channel);
}
- ShoutoutChannel(33);
+ //ShoutoutChannel(33);
}
void HandlePusherChannel(const Json::Value &json) {
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);
}
}
private:
+ const Config &config;
ffmpeg::Network net;
uv::Loop loop;
ws::Context ws_ctx;
Mixer mixer;
Renderer renderer;
State state;
- std::string own_channel_id;
bool enable_realtime;
- bool enable_shoutouts;
DrawingGame drawing_game;
--- /dev/null
+#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
#include <iostream>
#include "app/Application.h"
+#include "app/Config.h"
namespace {
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);
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();
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);
}
}