Repository navigation
Expand file tree
/
Copy pathconfig.cpp
More file actions
156 lines (137 loc) · 4.96 KB
/
Copy pathconfig.cpp
File metadata and controls
156 lines (137 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// config.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 <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <boost/asio.hpp>
#include <boost/foreach.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <exception>
#include <iostream>
#include <sstream>
#include <string>
#include "interface.hpp"
#include "json.hpp"
#include "config.hpp"
#include "utils.hpp"
using namespace boost::asio;
std::shared_ptr<Config> Config::parse(const std::string& filename,
bool driver_restart) {
Config config;
std::ifstream jsonstream(filename);
if (!jsonstream) {
std::cerr << "Fatal: error opening config file " << filename << std::endl;
return nullptr;
}
try {
config = json_to_config(jsonstream);
} catch (std::exception const& e) {
std::cerr << "Configuration file " << filename << " : " << e.what()
<< std::endl;
return nullptr;
}
if (config.log_severity_ < 0 || config.log_severity_ > 5)
config.log_severity_ = 2;
if (config.playout_delay_ > 4000)
config.playout_delay_ = 4000;
if (config.tic_frame_size_at_1fs_ == 0 || config.tic_frame_size_at_1fs_ > 192)
config.tic_frame_size_at_1fs_ = 192;
if (config.max_tic_frame_size_ < config.tic_frame_size_at_1fs_ ||
config.max_tic_frame_size_ > 1024)
config.max_tic_frame_size_ = 1024;
if (config.sample_rate_ == 0)
config.sample_rate_ = 48000;
boost::system::error_code ec;
ip::address_v4::from_string(config.rtp_mcast_base_.c_str(), ec);
if (ec) {
config.rtp_mcast_base_ = "239.1.0.1";
}
ip::address_v4::from_string(config.sap_mcast_addr_.c_str(), ec);
if (ec) {
config.sap_mcast_addr_ = "224.2.127.254";
}
if (config.ptp_domain_ > 127)
config.ptp_domain_ = 0;
auto [mac_addr, mac_str] = get_interface_mac(config.interface_name_);
if (mac_str.empty()) {
std::cerr << "Cannot retrieve MAC address for interface "
<< config.interface_name_ << std::endl;
return nullptr;
}
config.mac_addr_ = mac_addr;
config.mac_str_ = mac_str;
auto interface_idx = get_interface_index(config.interface_name_);
if (interface_idx < 0) {
std::cerr << "Cannot retrieve index for interface "
<< config.interface_name_ << std::endl;
} else {
config.interface_idx_ = interface_idx;
}
auto [ip_addr, ip_str] = get_interface_ip(config.interface_name_);
if (ip_str.empty()) {
std::cerr << "Cannot retrieve IPv4 address for interface "
<< config.interface_name_ << std::endl;
} else {
config.ip_addr_ = ip_addr;
config.ip_str_ = ip_str;
}
config.config_filename_ = filename;
config.daemon_restart_ = false;
config.driver_restart_ = driver_restart;
return std::make_shared<Config>(config);
}
bool Config::save(const Config& config) {
if (*this != config) {
std::ofstream js(config_filename_);
if (!js) {
BOOST_LOG_TRIVIAL(fatal)
<< "Config:: cannot save to file " << config_filename_;
return false;
}
js << config_to_json(config);
driver_restart_ =
get_tic_frame_size_at_1fs() != config.get_tic_frame_size_at_1fs() ||
get_max_tic_frame_size() != config.get_max_tic_frame_size() ||
get_interface_name() != config.get_interface_name();
daemon_restart_ = driver_restart_ ||
get_http_port() != config.get_http_port() ||
get_rtsp_port() != config.get_rtsp_port() ||
get_http_base_dir() != config.get_http_base_dir() ||
get_rtp_mcast_base() != config.get_rtp_mcast_base() ||
get_sap_mcast_addr() != config.get_sap_mcast_addr() ||
get_rtp_port() != config.get_rtp_port() ||
get_status_file() != config.get_status_file() ||
get_mdns_enabled() != config.get_mdns_enabled() ||
get_custom_node_id() != config.get_custom_node_id();
if (!daemon_restart_)
*this = config;
BOOST_LOG_TRIVIAL(info) << "Config:: file saved";
} else {
BOOST_LOG_TRIVIAL(info) << "Config:: unchanged";
}
return true;
}
std::string Config::get_node_id() const {
if (custom_node_id_.empty()) {
return get_host_node_id(get_ip_addr());
} else {
return custom_node_id_;
}
}