aes67-daemon/daemon/main.cpp
Andrea Bondavalli 5deb6c1927 Added to the daemon the support for Multicast DNS (using Linux Avahi) to allow discovery of remote audio sources and of RTSP for SDP transfer.
Added to the WebUI the possibility to directly select a remote source SDP file for a Sink.

New files:
daemon/mdns_client.hpp,cpp -> mDNS client implementation using Avahi client library
daemon/rtsp_client.hpp,cpp -> RTSP client implementation used to transfer SDP file
daemon/utils.cpp -> used for common utility functions
.clang-format -> added clang-format configuration file

Modified files:
daemon/CMakeList.txt -> added support for Avahi and option WITH_AVAHI=[yes/no] to compile the daemon with or without Avahi mDNS support
daemon/config.hpp,cpp -> added configuration option mdns_enabled to enable or disable mDNS discovery at runtime
daemon/json.cpp -> extended JSON config with mdns_enabled option
daemon/browser.hpp,cpp -> added support for mDNS client to the browser
daemon/session_manager.cpp -> added support for RTSP protocol to Source URL field and fixed issue with SDP file parsing
webui/RemoteSources.js -> added visualization of mDNS remote sources
webui/SinkEdit.js -> added the possibility to directly select a remote source SDP file for a Sink
webui/SourceInfo.js -> added visualization of protocol source (SAP, mDNS or local) for a source
ubuntu-packages.sh -> added libavahi-client-dev to the list of required packages
build.sh -> added WITH_AVAHI=yes option when invoking CMake
README.md -> added notes about mDNS support via Avahi
daemon/README.md -> added notes about mDNS support via Avahi, support for RTSP protocol in source and new mdns_enabled config param

Additional minor changes to remaining files.
2020-03-29 19:41:56 +02:00

191 lines
5.7 KiB
C++

//
// main.cpp
//
// Copyright (c) 2019 2020 Andrea Bondavalli. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include <boost/program_options.hpp>
#include <iostream>
#include <thread>
#include "config.hpp"
#include "driver_manager.hpp"
#include "http_server.hpp"
#include "log.hpp"
#include "session_manager.hpp"
#include "interface.hpp"
#include "browser.hpp"
namespace po = boost::program_options;
namespace postyle = boost::program_options::command_line_style;
namespace logging = boost::log;
static std::atomic<bool> terminate = false;
void termination_handler(int signum) {
BOOST_LOG_TRIVIAL(info) << "main:: got signal " << signum;
// Terminate program
terminate = true;
}
bool is_terminated() {
return terminate.load();
}
int main(int argc, char* argv[]) {
int rc = EXIT_SUCCESS;
po::options_description desc("Options");
desc.add_options()
("config,c", po::value<std::string>()->default_value("/etc/daemon.conf"),
"daemon configuration file")
("interface_name,i", po::value<std::string>(), "Network interface name")
("http_port,p", po::value<int>(), "HTTP server port")
("help,h", "Print this help message");
int unix_style = postyle::unix_style | postyle::short_allow_next;
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv)
.options(desc)
.style(unix_style)
.run(),
vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << "USAGE: " << argv[0] << '\n' << desc << '\n';
return EXIT_SUCCESS;
}
} catch (po::error& poe) {
std::cerr << poe.what() << '\n'
<< "USAGE: " << argv[0] << '\n'
<< desc << '\n';
return EXIT_FAILURE;
}
signal(SIGINT, termination_handler);
signal(SIGTERM, termination_handler);
std::srand(std::time(nullptr));
std::string filename = vm["config"].as<std::string>();
while (!is_terminated() && rc == EXIT_SUCCESS) {
/* load configuration from file */
auto config = Config::parse(filename);
if (config == nullptr) {
return EXIT_FAILURE;
}
/* override configuration according to command line args */
if (vm.count("interface_name")) {
config->set_interface_name(vm["interface_name"].as<std::string>());
}
if (vm.count("http_port")) {
config->set_http_port(vm["http_port"].as<int>());
}
/* init logging */
log_init(*config);
BOOST_LOG_TRIVIAL(debug) << "main:: initializing daemon";
try {
auto driver = DriverManager::create();
/* setup and init driver */
if (driver == nullptr || !driver->init(*config)) {
throw std::runtime_error(std::string("DriverManager:: init failed"));
}
/* start session manager */
auto session_manager = SessionManager::create(driver, config);
if (session_manager == nullptr || !session_manager->init()) {
throw std::runtime_error(
std::string("SessionManager:: init failed"));
}
/* start browser */
auto browser = Browser::create(config);
if (browser == nullptr || !browser->init()) {
throw std::runtime_error(
std::string("Browser:: init failed"));
}
/* start http server */
HttpServer http_server(session_manager, browser, config);
if (!http_server.init()) {
throw std::runtime_error(std::string("HttpServer:: init failed"));
}
/* load session status from file */
session_manager->load_status();
BOOST_LOG_TRIVIAL(debug) << "main:: init done, entering loop...";
while (!is_terminated()) {
auto [ip_addr, ip_str] = get_interface_ip(config->get_interface_name());
if (!ip_str.empty() && config->get_ip_addr_str() != ip_str) {
BOOST_LOG_TRIVIAL(warning) << "main:: IP address changed, restarting ...";
config->set_ip_addr_str(ip_str);
config->set_ip_addr(ip_addr);
break;
}
if (config->get_need_restart()) {
BOOST_LOG_TRIVIAL(warning) << "main:: config changed, restarting ...";
break;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
/* save session status to file */
session_manager->save_status();
/* stop http server */
if (!http_server.terminate()) {
throw std::runtime_error(
std::string("HttpServer:: terminate failed"));
}
/* stop browser */
if (!browser->terminate()) {
throw std::runtime_error(
std::string("Browser:: terminate failed"));
}
/* stop session manager */
if (!session_manager->terminate()) {
throw std::runtime_error(
std::string("SessionManager:: terminate failed"));
}
/* stop driver manager */
if (!driver->terminate()) {
throw std::runtime_error(
std::string("DriverManager:: terminate failed"));
}
} catch (std::exception& e) {
BOOST_LOG_TRIVIAL(fatal) << "main:: fatal exception error: " << e.what();
rc = EXIT_FAILURE;
}
BOOST_LOG_TRIVIAL(info) << "main:: end ";
}
std::cout << "daemon exiting with code: " << rc << std::endl;
return rc;
}