2019-12-03 19:23:33 +00:00
|
|
|
//
|
|
|
|
// Created by loki on 5/30/19.
|
|
|
|
//
|
|
|
|
|
2020-01-01 17:47:34 +00:00
|
|
|
#include "process.h"
|
|
|
|
|
2020-01-19 23:22:13 +00:00
|
|
|
#include <csignal>
|
2021-06-04 19:12:06 +00:00
|
|
|
#include <filesystem>
|
2021-06-09 19:55:13 +00:00
|
|
|
#include <fstream>
|
2021-05-17 19:21:57 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <thread>
|
2019-12-03 19:23:33 +00:00
|
|
|
|
2021-05-17 19:21:57 +00:00
|
|
|
#include <boost/log/attributes/clock.hpp>
|
2020-01-09 21:02:01 +00:00
|
|
|
#include <boost/log/common.hpp>
|
|
|
|
#include <boost/log/expressions.hpp>
|
2021-05-17 19:21:57 +00:00
|
|
|
#include <boost/log/sinks.hpp>
|
2020-01-09 21:02:01 +00:00
|
|
|
#include <boost/log/sources/severity_logger.hpp>
|
|
|
|
|
2021-05-17 19:21:57 +00:00
|
|
|
#include "config.h"
|
2021-05-29 21:05:37 +00:00
|
|
|
#include "confighttp.h"
|
2021-06-08 19:58:32 +00:00
|
|
|
#include "httpcommon.h"
|
2021-06-21 19:21:52 +00:00
|
|
|
#include "main.h"
|
2019-12-03 19:23:33 +00:00
|
|
|
#include "nvhttp.h"
|
2020-02-08 15:26:38 +00:00
|
|
|
#include "rtsp.h"
|
2019-12-22 22:34:12 +00:00
|
|
|
#include "thread_pool.h"
|
2021-05-17 19:21:57 +00:00
|
|
|
#include "video.h"
|
2019-12-03 19:23:33 +00:00
|
|
|
|
2020-01-15 17:31:28 +00:00
|
|
|
#include "platform/common.h"
|
2019-12-03 19:23:33 +00:00
|
|
|
extern "C" {
|
2020-04-01 16:01:13 +00:00
|
|
|
#include <libavutil/log.h>
|
2021-05-17 19:21:57 +00:00
|
|
|
#include <rs.h>
|
2019-12-03 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 19:21:52 +00:00
|
|
|
safe::mail_t mail::man;
|
|
|
|
|
2019-12-03 19:23:33 +00:00
|
|
|
using namespace std::literals;
|
2020-01-09 21:02:01 +00:00
|
|
|
namespace bl = boost::log;
|
2019-12-22 22:34:12 +00:00
|
|
|
|
|
|
|
util::ThreadPool task_pool;
|
2020-01-09 21:02:01 +00:00
|
|
|
bl::sources::severity_logger<int> verbose(0); // Dominating output
|
|
|
|
bl::sources::severity_logger<int> debug(1); // Follow what is happening
|
|
|
|
bl::sources::severity_logger<int> info(2); // Should be informed about
|
|
|
|
bl::sources::severity_logger<int> warning(3); // Strange events
|
|
|
|
bl::sources::severity_logger<int> error(4); // Recoverable errors
|
|
|
|
bl::sources::severity_logger<int> fatal(5); // Unrecoverable errors
|
|
|
|
|
2019-12-25 19:57:23 +00:00
|
|
|
bool display_cursor;
|
|
|
|
|
2020-01-09 21:02:01 +00:00
|
|
|
using text_sink = bl::sinks::asynchronous_sink<bl::sinks::text_ostream_backend>;
|
|
|
|
boost::shared_ptr<text_sink> sink;
|
|
|
|
|
|
|
|
struct NoDelete {
|
|
|
|
void operator()(void *) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", int)
|
|
|
|
|
2021-06-09 19:55:13 +00:00
|
|
|
void print_help(const char *name) {
|
|
|
|
std::cout
|
|
|
|
<< "Usage: "sv << name << " [options] [/path/to/configuration_file] [--cmd]"sv << std::endl
|
|
|
|
<< " Any configurable option can be overwritten with: \"name=value\""sv << std::endl
|
|
|
|
<< std::endl
|
|
|
|
<< " --help | print help"sv << std::endl
|
|
|
|
<< " --creds username password | set user credentials for the Web manager" << std::endl
|
|
|
|
<< std::endl
|
|
|
|
<< " flags"sv << std::endl
|
|
|
|
<< " -0 | Read PIN from stdin"sv << std::endl
|
|
|
|
<< " -1 | Do not load previously saved state and do retain any state after shutdown"sv << std::endl
|
2021-06-20 13:29:51 +00:00
|
|
|
<< " | Effectively starting as if for the first time without overwriting any pairings with your devices"sv << std::endl
|
|
|
|
<< " -2 | Force replacement of headers in video stream" << std::endl;
|
2021-06-09 19:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace help {
|
|
|
|
int entry(const char *name, int argc, char *argv[]) {
|
|
|
|
print_help(name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace help
|
|
|
|
|
2020-01-09 21:02:01 +00:00
|
|
|
void log_flush() {
|
|
|
|
sink->flush();
|
|
|
|
}
|
|
|
|
|
2020-01-19 23:22:13 +00:00
|
|
|
std::map<int, std::function<void()>> signal_handlers;
|
|
|
|
void on_signal_forwarder(int sig) {
|
|
|
|
signal_handlers.at(sig)();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class FN>
|
|
|
|
void on_signal(int sig, FN &&fn) {
|
|
|
|
signal_handlers.emplace(sig, std::forward<FN>(fn));
|
|
|
|
|
|
|
|
std::signal(sig, on_signal_forwarder);
|
|
|
|
}
|
|
|
|
|
2021-06-09 19:55:13 +00:00
|
|
|
namespace gen_creds {
|
|
|
|
int entry(const char *name, int argc, char *argv[]) {
|
|
|
|
if(argc < 2 || argv[0] == "help"sv || argv[1] == "help"sv) {
|
|
|
|
print_help(name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
http::save_user_creds(config::sunshine.credentials_file, argv[0], argv[1]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace gen_creds
|
|
|
|
|
|
|
|
std::map<std::string_view, std::function<int(const char *name, int argc, char **argv)>> cmd_to_func {
|
|
|
|
{ "creds"sv, gen_creds::entry },
|
|
|
|
{ "help"sv, help::entry }
|
|
|
|
};
|
|
|
|
|
2019-12-15 13:30:00 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2021-06-21 19:21:52 +00:00
|
|
|
mail::man = std::make_shared<safe::mail_raw_t>();
|
|
|
|
|
2020-03-19 18:59:27 +00:00
|
|
|
if(config::parse(argc, argv)) {
|
|
|
|
return 0;
|
2019-12-03 22:19:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 20:44:56 +00:00
|
|
|
if(config::sunshine.min_log_level >= 1) {
|
2020-04-01 16:01:13 +00:00
|
|
|
av_log_set_level(AV_LOG_QUIET);
|
|
|
|
}
|
2021-06-04 19:12:06 +00:00
|
|
|
else {
|
|
|
|
av_log_set_level(AV_LOG_DEBUG);
|
|
|
|
}
|
2020-04-01 16:01:13 +00:00
|
|
|
|
2020-01-09 21:02:01 +00:00
|
|
|
sink = boost::make_shared<text_sink>();
|
|
|
|
|
|
|
|
boost::shared_ptr<std::ostream> stream { &std::cout, NoDelete {} };
|
|
|
|
sink->locked_backend()->add_stream(stream);
|
|
|
|
sink->set_filter(severity >= config::sunshine.min_log_level);
|
|
|
|
|
2021-05-17 19:21:57 +00:00
|
|
|
sink->set_formatter([message = "Message"s, severity = "Severity"s](const bl::record_view &view, bl::formatting_ostream &os) {
|
|
|
|
constexpr int DATE_BUFFER_SIZE = 21 + 2 + 1; // Full string plus ": \0"
|
2020-04-01 16:01:13 +00:00
|
|
|
|
2020-01-09 21:02:01 +00:00
|
|
|
auto log_level = view.attribute_values()[severity].extract<int>().get();
|
|
|
|
|
|
|
|
std::string_view log_type;
|
|
|
|
switch(log_level) {
|
2021-05-17 19:21:57 +00:00
|
|
|
case 0:
|
|
|
|
log_type = "Verbose: "sv;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
log_type = "Debug: "sv;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
log_type = "Info: "sv;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
log_type = "Warning: "sv;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
log_type = "Error: "sv;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
log_type = "Fatal: "sv;
|
|
|
|
break;
|
2020-01-09 21:02:01 +00:00
|
|
|
};
|
|
|
|
|
2020-04-01 16:01:13 +00:00
|
|
|
char _date[DATE_BUFFER_SIZE];
|
|
|
|
std::time_t t = std::time(nullptr);
|
|
|
|
strftime(_date, DATE_BUFFER_SIZE, "[%Y:%m:%d:%H:%M:%S]: ", std::localtime(&t));
|
|
|
|
|
|
|
|
os << _date << log_type << view.attribute_values()[message].extract<std::string>();
|
2020-01-09 21:02:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
bl::core::get()->add_sink(sink);
|
|
|
|
auto fg = util::fail_guard(log_flush);
|
|
|
|
|
2021-06-09 19:55:13 +00:00
|
|
|
if(!config::sunshine.cmd.name.empty()) {
|
|
|
|
auto fn = cmd_to_func.find(config::sunshine.cmd.name);
|
|
|
|
if(fn == std::end(cmd_to_func)) {
|
|
|
|
BOOST_LOG(fatal) << "Unknown command: "sv << config::sunshine.cmd.name;
|
|
|
|
|
|
|
|
BOOST_LOG(info) << "Possible commands:"sv;
|
|
|
|
for(auto &[key, _] : cmd_to_func) {
|
|
|
|
BOOST_LOG(info) << '\t' << key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn->second(argv[0], config::sunshine.cmd.argc, config::sunshine.cmd.argv);
|
|
|
|
}
|
|
|
|
|
2020-01-19 23:22:13 +00:00
|
|
|
// Create signal handler after logging has been initialized
|
2021-06-21 19:21:52 +00:00
|
|
|
auto shutdown_event = mail::man->event<bool>(mail::shutdown);
|
2020-01-19 23:22:13 +00:00
|
|
|
on_signal(SIGINT, [shutdown_event]() {
|
|
|
|
BOOST_LOG(info) << "Interrupt handler called"sv;
|
2021-06-21 19:21:52 +00:00
|
|
|
|
2020-01-19 23:22:13 +00:00
|
|
|
shutdown_event->raise(true);
|
|
|
|
});
|
|
|
|
|
2021-05-09 16:55:34 +00:00
|
|
|
proc::refresh(config::stream.file_apps);
|
2019-12-15 18:36:22 +00:00
|
|
|
|
2020-02-01 09:25:37 +00:00
|
|
|
auto deinit_guard = platf::init();
|
2021-05-02 20:35:19 +00:00
|
|
|
if(!deinit_guard) {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2019-12-03 19:23:33 +00:00
|
|
|
reed_solomon_init();
|
2020-04-13 21:15:24 +00:00
|
|
|
if(video::init()) {
|
|
|
|
return 2;
|
|
|
|
}
|
2021-06-09 20:01:18 +00:00
|
|
|
if(http::init()) {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2021-06-18 18:02:57 +00:00
|
|
|
//FIXME: Temporary workaround: Simple-Web_server needs to be updated or replaced
|
|
|
|
if(shutdown_event->peek()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:34:12 +00:00
|
|
|
task_pool.start(1);
|
|
|
|
|
2021-06-21 19:21:52 +00:00
|
|
|
std::thread httpThread { nvhttp::start };
|
|
|
|
std::thread configThread { confighttp::start };
|
|
|
|
stream::rtpThread();
|
2019-12-03 19:23:33 +00:00
|
|
|
|
|
|
|
httpThread.join();
|
2021-06-08 19:58:32 +00:00
|
|
|
configThread.join();
|
|
|
|
|
2021-05-05 13:53:22 +00:00
|
|
|
task_pool.stop();
|
|
|
|
task_pool.join();
|
2019-12-03 19:23:33 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-04 19:12:06 +00:00
|
|
|
|
|
|
|
std::string read_file(const char *path) {
|
|
|
|
if(!std::filesystem::exists(path)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ifstream in(path);
|
|
|
|
|
|
|
|
std::string input;
|
|
|
|
std::string base64_cert;
|
|
|
|
|
|
|
|
while(!in.eof()) {
|
|
|
|
std::getline(in, input);
|
|
|
|
base64_cert += input + '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
return base64_cert;
|
|
|
|
}
|
|
|
|
|
|
|
|
int write_file(const char *path, const std::string_view &contents) {
|
|
|
|
std::ofstream out(path);
|
|
|
|
|
|
|
|
if(!out.is_open()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
out << contents;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|