satdump/src-interface/recorder/recorder_proc.cpp

397 lines
16 KiB
C++
Raw Permalink Normal View History

2022-12-17 16:55:11 +01:00
#include "recorder.h"
#include "main_ui.h"
2023-09-06 22:28:55 -04:00
#include "logger.h"
2022-12-17 16:55:11 +01:00
#include "processing.h"
2024-09-19 21:15:53 -04:00
#ifndef _MSC_VER
#include <sys/statvfs.h>
#endif
2022-12-17 16:55:11 +01:00
namespace satdump
{
nlohmann::json RecorderApplication::serialize_config()
{
nlohmann::json out;
out["show_waterfall"] = show_waterfall;
out["waterfall_ratio"] = waterfall_ratio;
out["panel_ratio"] = panel_ratio;
out["fft_size"] = fft_size;
out["fft_rate"] = fft_rate;
2023-02-11 01:07:39 +01:00
out["waterfall_rate"] = waterfall_rate;
2022-12-17 16:55:11 +01:00
out["waterfall_palette"] = waterfall_palettes[selected_waterfall_palette].name;
2024-08-19 14:57:16 -04:00
out["baseband_type"] = (std::string)baseband_format;
2022-12-17 16:55:11 +01:00
if (fft_plot && waterfall_plot && fft)
{
out["fft_min"] = fft_plot->scale_min;
out["fft_max"] = fft_plot->scale_max;
2023-12-13 15:42:08 +01:00
out["fft_avgn"] = fft->avg_num;
2022-12-17 16:55:11 +01:00
}
2024-08-19 14:57:16 -04:00
#if defined(BUILD_ZIQ) || defined(BUILD_ZIQ2)
out["ziq_depth"] = baseband_format.ziq_depth;
#endif
2022-12-17 16:55:11 +01:00
return out;
}
void RecorderApplication::deserialize_config(nlohmann::json in)
{
show_waterfall = in["show_waterfall"].get<bool>();
waterfall_ratio = in["waterfall_ratio"].get<float>();
panel_ratio = in["panel_ratio"].get<float>();
if (fft_plot && waterfall_plot && fft)
{
if (in.contains("fft_min"))
fft_plot->scale_min = in["fft_min"];
if (in.contains("fft_max"))
fft_plot->scale_max = in["fft_max"];
2023-12-13 15:42:08 +01:00
if (in.contains("fft_avgn"))
fft->avg_num = in["fft_avgn"];
2022-12-17 16:55:11 +01:00
}
if (in.contains("fft_size"))
{
fft_size = in["fft_size"].get<int>();
2024-01-22 23:03:51 +01:00
for (int i = 0; i < (int)fft_sizes_lut.size(); i++)
2022-12-17 16:55:11 +01:00
if (fft_sizes_lut[i] == fft_size)
selected_fft_size = i;
}
if (in.contains("fft_rate"))
fft_rate = in["fft_rate"];
2023-02-11 01:07:39 +01:00
if (in.contains("waterfall_rate"))
waterfall_rate = in["waterfall_rate"];
2024-08-19 14:57:16 -04:00
if (in.contains("baseband_type"))
baseband_format = in["baseband_type"].get<std::string>();
2022-12-17 16:55:11 +01:00
if (in.contains("waterfall_palette"))
{
std::string name = in["waterfall_palette"].get<std::string>();
for (int i = 0; i < (int)waterfall_palettes.size(); i++)
if (waterfall_palettes[i].name == name)
selected_waterfall_palette = i;
waterfall_plot->set_palette(waterfall_palettes[selected_waterfall_palette]);
}
2024-08-19 14:57:16 -04:00
#if defined(BUILD_ZIQ) || defined(BUILD_ZIQ2)
if (in.contains("ziq_depth"))
baseband_format.ziq_depth = in["ziq_depth"];
#endif
2022-12-17 16:55:11 +01:00
}
2023-01-23 17:58:03 +01:00
void RecorderApplication::start()
{
if (is_started)
return;
2023-12-18 14:53:28 -05:00
set_frequency(frequency_hz);
2023-01-23 17:58:03 +01:00
try
{
current_samplerate = source_ptr->get_samplerate();
if (current_samplerate == 0)
2024-03-14 12:12:34 +01:00
throw satdump_exception("Samplerate not set!");
source_ptr->start();
2023-01-23 17:58:03 +01:00
if (current_decimation > 1)
{
2023-01-31 02:37:41 +01:00
decim_ptr = std::make_shared<dsp::SmartResamplerBlock<complex_t>>(source_ptr->output_stream, 1, current_decimation);
2023-01-23 17:58:03 +01:00
decim_ptr->start();
logger->info("Setting up resampler...");
}
fft->set_fft_settings(fft_size, get_samplerate(), fft_rate);
2023-02-11 01:07:39 +01:00
waterfall_plot->set_rate(fft_rate, waterfall_rate);
fft_plot->bandwidth = get_samplerate();
2023-01-23 17:58:03 +01:00
splitter->input_stream = current_decimation > 1 ? decim_ptr->output_stream : source_ptr->output_stream;
splitter->start();
is_started = true;
}
catch (std::runtime_error &e)
{
2024-01-21 14:57:41 -05:00
sdr_error.set_message(style::theme.red, e.what());
2023-01-23 17:58:03 +01:00
logger->error(e.what());
}
}
void RecorderApplication::stop()
{
if (!is_started)
return;
2023-01-23 17:58:03 +01:00
splitter->stop_tmp();
if (current_decimation > 1)
decim_ptr->stop();
source_ptr->stop();
is_started = false;
config::main_cfg["user"]["recorder_sdr_settings"]["last_used_sdr"] = sources[sdr_select_id].name;
2023-01-23 17:58:03 +01:00
config::main_cfg["user"]["recorder_sdr_settings"][sources[sdr_select_id].name] = source_ptr->get_settings();
config::main_cfg["user"]["recorder_sdr_settings"][sources[sdr_select_id].name]["samplerate"] = source_ptr->get_samplerate();
2023-12-18 14:53:28 -05:00
config::main_cfg["user"]["recorder_sdr_settings"][sources[sdr_select_id].name]["frequency"] = frequency_hz;
config::main_cfg["user"]["recorder_sdr_settings"][sources[sdr_select_id].name]["xconverter_frequency"] = xconverter_frequency;
config::main_cfg["user"]["recorder_sdr_settings"][sources[sdr_select_id].name]["decimation"] = current_decimation;
2023-01-23 17:58:03 +01:00
config::saveUserConfig();
}
2022-12-17 16:55:11 +01:00
void RecorderApplication::try_load_sdr_settings()
{
if (config::main_cfg["user"].contains("recorder_sdr_settings"))
{
if (config::main_cfg["user"]["recorder_sdr_settings"].contains(sources[sdr_select_id].name))
{
2024-08-19 14:57:16 -04:00
auto &cfg = config::main_cfg["user"]["recorder_sdr_settings"][sources[sdr_select_id].name];
2022-12-17 16:55:11 +01:00
source_ptr->set_settings(cfg);
if (cfg.contains("samplerate"))
{
try
{
source_ptr->set_samplerate(cfg["samplerate"]);
}
catch (std::exception &)
2022-12-17 16:55:11 +01:00
{
}
}
if (cfg.contains("frequency"))
{
2023-12-18 14:53:28 -05:00
frequency_hz = cfg["frequency"].get<uint64_t>();
set_frequency(frequency_hz);
2022-12-17 16:55:11 +01:00
}
if (cfg.contains("xconverter_frequency"))
xconverter_frequency = cfg["xconverter_frequency"].get<double>();
else
xconverter_frequency = 0;
if (cfg.contains("decimation"))
current_decimation = cfg["decimation"].get<int>();
else
2023-07-29 21:04:02 +02:00
current_decimation = 1;
2022-12-17 16:55:11 +01:00
}
}
}
void RecorderApplication::start_processing()
{
if (pipeline_selector.outputdirselect.isValid() || automated_live_output_dir)
2022-12-17 16:55:11 +01:00
{
logger->trace("Start pipeline...");
pipeline_params = pipeline_selector.getParameters();
2023-01-23 17:58:03 +01:00
pipeline_params["samplerate"] = get_samplerate();
2024-05-15 17:53:20 +02:00
pipeline_params["baseband_format"] = "cf32";
2023-02-11 01:07:39 +01:00
pipeline_params["buffer_size"] = dsp::STREAM_BUFFER_SIZE; // This is required, as we WILL go over the (usually) default 8192 size
pipeline_params["start_timestamp"] = (double)time(0); // Some pipelines need this
2022-12-17 16:55:11 +01:00
2023-07-15 19:50:32 +02:00
try
{
2024-10-21 09:31:41 -04:00
if (automated_live_output_dir)
pipeline_output_dir = prepareAutomatedPipelineFolder(time(0), source_ptr->d_frequency, pipeline_selector.selected_pipeline.name);
else
pipeline_output_dir = pipeline_selector.outputdirselect.getPath();
2024-07-21 14:58:03 -04:00
live_pipeline = std::make_unique<LivePipeline>(pipeline_selector.selected_pipeline, pipeline_params, pipeline_output_dir);
2023-07-15 19:50:32 +02:00
splitter->reset_output("live");
live_pipeline->start(splitter->get_output("live"), ui_thread_pool);
splitter->set_enabled("live", true);
2022-12-17 16:55:11 +01:00
2023-07-15 19:50:32 +02:00
is_processing = true;
}
catch (std::runtime_error &e)
{
2024-01-21 14:57:41 -05:00
error.set_message(style::theme.red, e.what());
2023-07-15 19:50:32 +02:00
logger->error(e.what());
}
2022-12-17 16:55:11 +01:00
}
else
{
2024-01-21 14:57:41 -05:00
error.set_message(style::theme.red, "Please select a valid output directory!");
2022-12-17 16:55:11 +01:00
}
}
void RecorderApplication::stop_processing()
{
if (is_processing)
{
2024-08-02 23:43:19 +02:00
is_stopping_processing = true;
2022-12-17 16:55:11 +01:00
logger->trace("Stop pipeline...");
2023-02-19 18:19:01 +01:00
splitter->set_enabled("live", false);
2022-12-17 16:55:11 +01:00
live_pipeline->stop();
2024-08-02 23:43:19 +02:00
is_stopping_processing = is_processing = false;
2022-12-17 16:55:11 +01:00
if (config::main_cfg["user_interface"]["finish_processing_after_live"]["value"].get<bool>() && live_pipeline->getOutputFiles().size() > 0)
{
2024-07-21 14:58:03 -04:00
Pipeline pipeline = pipeline_selector.selected_pipeline;
2022-12-17 16:55:11 +01:00
std::string input_file = live_pipeline->getOutputFiles()[0];
int start_level = pipeline.live_cfg.normal_live[pipeline.live_cfg.normal_live.size() - 1].first;
std::string input_level = pipeline.steps[start_level].level_name;
ui_thread_pool.push([=](int)
2024-07-21 14:58:03 -04:00
{ processing::process(pipeline, input_level, input_file, pipeline_output_dir, pipeline_params); });
2022-12-17 16:55:11 +01:00
}
live_pipeline.reset();
}
}
void RecorderApplication::start_recording()
{
2023-02-19 18:19:01 +01:00
splitter->set_enabled("record", true);
2024-09-19 21:15:53 -04:00
load_rec_path_data();
2024-01-21 21:24:35 +01:00
std::string filename = recording_path + prepareBasebandFileName(getTime(), get_samplerate(), frequency_hz);
2024-08-19 14:57:16 -04:00
recorder_filename = file_sink->start_recording(filename, get_samplerate());
2022-12-17 16:55:11 +01:00
logger->info("Recording to " + recorder_filename);
is_recording = true;
}
void RecorderApplication::stop_recording()
{
if (is_recording)
{
file_sink->stop_recording();
2023-02-19 18:19:01 +01:00
splitter->set_enabled("record", false);
2022-12-17 16:55:11 +01:00
recorder_filename = "";
is_recording = false;
2024-09-19 21:15:53 -04:00
load_rec_path_data();
2022-12-17 16:55:11 +01:00
}
}
2024-09-19 21:15:53 -04:00
void RecorderApplication::load_rec_path_data()
2024-09-16 13:35:46 -04:00
{
recording_path = config::main_cfg["satdump_directories"]["recording_path"]["value"].get<std::string>();
#if defined(_MSC_VER)
recording_path += "\\";
#elif defined(__ANDROID__)
if (recording_path == ".")
recording_path = "/storage/emulated/0";
recording_path += "/";
#else
recording_path += "/";
#endif
2024-09-19 21:15:53 -04:00
#ifdef _MSC_VER
ULARGE_INTEGER bytes_available;
if (GetDiskFreeSpaceEx(recording_path.c_str(), &bytes_available, NULL, NULL))
disk_available = bytes_available.QuadPart;
#else
struct statvfs stat_buffer;
if (statvfs(recording_path.c_str(), &stat_buffer) == 0)
disk_available = stat_buffer.f_bavail * stat_buffer.f_bsize;
#endif
2024-09-16 13:35:46 -04:00
}
void RecorderApplication::try_init_tracking_widget()
{
if (tracking_widget == nullptr)
{
tracking_widget = new TrackingWidget();
2024-01-19 00:29:42 +01:00
tracking_widget->aos_callback = [this](AutoTrackCfg autotrack_cfg, SatellitePass, TrackedObject obj)
{
2024-01-22 23:03:51 +01:00
if (autotrack_cfg.multi_mode || obj.downlinks.size() > 1)
{
if (!autotrack_cfg.multi_mode)
{
double center_freq = 0;
for (auto &dl : obj.downlinks)
center_freq += dl.frequency;
center_freq /= obj.downlinks.size();
set_frequency(center_freq);
}
2024-01-20 11:22:03 +01:00
for (auto &dl : obj.downlinks)
{
2024-01-20 11:22:03 +01:00
if (dl.live || dl.record)
if (!is_started)
start();
2024-01-19 21:49:44 +01:00
2024-01-20 11:22:03 +01:00
if (dl.live)
{
2024-01-21 22:58:56 +01:00
std::string id = std::to_string(obj.norad) + "_" + std::to_string(dl.frequency) + "_live";
2024-01-20 11:22:03 +01:00
std::string name = std::to_string(obj.norad);
2024-01-21 22:58:56 +01:00
if (satdump::general_tle_registry.get_from_norad(obj.norad).has_value())
name = satdump::general_tle_registry.get_from_norad(obj.norad)->name;
2024-01-20 11:22:03 +01:00
name += " - " + format_notated(dl.frequency, "Hz");
2024-07-21 14:58:03 -04:00
add_vfo_live(id, name, dl.frequency, dl.pipeline_selector->selected_pipeline, dl.pipeline_selector->getParameters());
2024-01-20 11:22:03 +01:00
}
if (dl.record)
{
2024-01-21 22:58:56 +01:00
std::string id = std::to_string(obj.norad) + "_" + std::to_string(dl.frequency) + "_record";
std::string name = std::to_string(obj.norad);
if (satdump::general_tle_registry.get_from_norad(obj.norad).has_value())
name = satdump::general_tle_registry.get_from_norad(obj.norad)->name;
name += " - " + format_notated(dl.frequency, "Hz");
2024-08-18 11:43:57 -04:00
add_vfo_reco(id, name, dl.frequency, dl.baseband_format, dl.baseband_decimation);
2024-01-20 11:22:03 +01:00
}
}
}
2024-01-19 00:29:42 +01:00
else
{
2024-01-20 11:22:03 +01:00
if (obj.downlinks[0].live)
2024-01-19 00:29:42 +01:00
stop_processing();
2024-01-20 11:22:03 +01:00
if (obj.downlinks[0].record)
2024-01-19 00:29:42 +01:00
stop_recording();
2024-01-20 11:22:03 +01:00
if (obj.downlinks[0].live || obj.downlinks[0].record)
2024-01-19 00:29:42 +01:00
{
2024-01-20 11:22:03 +01:00
frequency_hz = obj.downlinks[0].frequency;
2024-01-19 00:29:42 +01:00
if (is_started)
set_frequency(frequency_hz);
else
start();
// Catch situations where source could not start
if (!is_started)
{
logger->error("Could not start recorder/processor since the source could not be started!");
return;
}
}
2024-01-20 11:22:03 +01:00
if (obj.downlinks[0].live)
2024-01-19 00:29:42 +01:00
{
2024-07-21 14:58:03 -04:00
pipeline_selector.select_pipeline(obj.downlinks[0].pipeline_selector->selected_pipeline.name);
2024-01-20 11:22:03 +01:00
pipeline_selector.setParameters(obj.downlinks[0].pipeline_selector->getParameters());
2024-07-21 14:58:03 -04:00
pipeline_selector.selected_pipeline.steps = obj.downlinks[0].pipeline_selector->selected_pipeline.steps;
2024-01-19 00:29:42 +01:00
start_processing();
}
2024-01-20 11:22:03 +01:00
if (obj.downlinks[0].record)
2024-01-19 00:29:42 +01:00
{
file_sink->set_output_sample_type(obj.downlinks[0].baseband_format);
2024-01-19 00:29:42 +01:00
start_recording();
}
}
};
tracking_widget->los_callback = [this](AutoTrackCfg autotrack_cfg, SatellitePass, TrackedObject obj)
{
2024-01-22 23:03:51 +01:00
if (autotrack_cfg.multi_mode || obj.downlinks.size() > 1)
2024-01-19 00:29:42 +01:00
{
2024-01-20 11:22:03 +01:00
for (auto &dl : obj.downlinks)
2024-01-19 00:29:42 +01:00
{
2024-01-20 11:22:03 +01:00
if (dl.live)
{
2024-01-21 22:58:56 +01:00
std::string id = std::to_string(obj.norad) + "_" + std::to_string(dl.frequency) + "_live";
del_vfo(id);
}
if (dl.record)
{
std::string id = std::to_string(obj.norad) + "_" + std::to_string(dl.frequency) + "_record";
2024-01-20 11:22:03 +01:00
del_vfo(id);
}
2024-01-19 00:29:42 +01:00
2024-01-20 11:22:03 +01:00
if (dl.live || dl.record)
if (is_started && vfo_list.size() == 0 && autotrack_cfg.stop_sdr_when_idle)
stop();
}
2024-01-19 00:29:42 +01:00
}
else
{
2024-01-20 11:22:03 +01:00
if (obj.downlinks[0].record)
2024-01-19 00:29:42 +01:00
stop_recording();
2024-01-20 11:22:03 +01:00
if (obj.downlinks[0].live)
2024-01-19 00:29:42 +01:00
stop_processing();
if (autotrack_cfg.stop_sdr_when_idle)
stop();
}
};
}
}
}