|
2 | 2 | #include "config_base.h" |
3 | 3 |
|
4 | 4 | namespace { |
5 | | - constexpr auto CACHE_RELOAD_INTERVAL = std::chrono::seconds(5); |
6 | | - |
7 | | - std::mutex cache_mutex; |
8 | | - PresetSettings cached_config; |
9 | | - std::optional<std::chrono::steady_clock::time_point> cache_time; |
10 | | - |
11 | 5 | std::vector<std::string> get_ffmpeg_args(std::string params_str, int quality) { |
12 | 6 | // replace quality placeholder |
13 | 7 | params_str = u::replace_all(params_str, "{quality}", std::to_string(quality)); |
@@ -108,20 +102,17 @@ std::string config_presets::generate_config_string(const PresetSettings& setting |
108 | 102 | } |
109 | 103 |
|
110 | 104 | void config_presets::create(const std::filesystem::path& filepath, const PresetSettings& current_settings) { |
111 | | - std::ofstream output(filepath); |
112 | | - output << generate_config_string(current_settings); |
| 105 | + config_base::write_config_string(filepath, generate_config_string(current_settings)); |
113 | 106 | } |
114 | 107 |
|
115 | 108 | PresetSettings config_presets::parse(const std::filesystem::path& config_filepath) { |
116 | | - std::ifstream file(config_filepath); |
117 | | - if (!file) |
| 109 | + auto content = config_base::read_config_file(config_filepath); |
| 110 | + if (!content) |
118 | 111 | return DEFAULT_CONFIG; // defaults if file couldn't be opened |
119 | 112 |
|
120 | | - std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); |
121 | | - |
122 | | - auto settings = parse(content); |
| 113 | + auto settings = parse(*content); |
123 | 114 |
|
124 | | - // recreate the config file using the parsed values (keeps nice formatting) |
| 115 | + // write formatted file |
125 | 116 | create(config_filepath, settings); |
126 | 117 |
|
127 | 118 | return settings; |
@@ -210,33 +201,26 @@ std::filesystem::path config_presets::get_preset_config_path() { |
210 | 201 | } |
211 | 202 |
|
212 | 203 | PresetSettings config_presets::get_preset_config() { |
213 | | - std::lock_guard lock(cache_mutex); |
214 | | - |
215 | | - auto now = std::chrono::steady_clock::now(); |
216 | | - if (!cache_time || now - *cache_time >= CACHE_RELOAD_INTERVAL) { |
217 | | - cached_config = config_base::load_config<PresetSettings>(get_preset_config_path(), create, parse); |
218 | | - cache_time = now; |
219 | | - } |
220 | | - |
221 | | - return cached_config; |
| 204 | + return config_base::load_config<PresetSettings>(get_preset_config_path(), create, parse); |
222 | 205 | } |
223 | 206 |
|
224 | 207 | void config_presets::save(const PresetSettings& settings) { |
225 | 208 | create(get_preset_config_path(), settings); |
226 | | - |
227 | | - std::lock_guard lock(cache_mutex); |
228 | | - cached_config = settings; |
229 | | - cache_time = std::chrono::steady_clock::now(); |
230 | 209 | } |
231 | 210 |
|
232 | 211 | std::vector<config_presets::PresetDetails> config_presets::get_available_presets( |
233 | 212 | bool gpu_encoding, const std::string& gpu_type |
| 213 | +) { |
| 214 | + return get_available_presets(get_preset_config(), gpu_encoding, gpu_type); |
| 215 | +} |
| 216 | + |
| 217 | +std::vector<config_presets::PresetDetails> config_presets::get_available_presets( |
| 218 | + const PresetSettings& config, bool gpu_encoding, const std::string& gpu_type |
234 | 219 | ) { |
235 | 220 | std::vector<PresetDetails> available_presets; |
236 | 221 |
|
237 | 222 | std::string type_to_check = gpu_encoding ? gpu_type : "cpu"; |
238 | 223 |
|
239 | | - PresetSettings config = get_preset_config(); |
240 | 224 | const auto* preset_group = config.find_preset_group(type_to_check); |
241 | 225 |
|
242 | 226 | if (preset_group) { |
|
0 commit comments