2021-02-14 12:05:44 +01:00
|
|
|
#include "logger.h"
|
2022-03-05 12:44:19 +01:00
|
|
|
#include "core/module.h"
|
|
|
|
|
#include "core/pipeline.h"
|
2021-02-14 12:05:44 +01:00
|
|
|
#include <signal.h>
|
|
|
|
|
#include <filesystem>
|
2021-02-16 13:47:53 +01:00
|
|
|
#include "nlohmann/json.hpp"
|
|
|
|
|
#include <fstream>
|
2021-05-04 12:41:47 +02:00
|
|
|
#include "init.h"
|
2022-02-21 23:32:11 +01:00
|
|
|
#include "common/cli_utils.h"
|
2022-02-27 15:14:36 +01:00
|
|
|
|
2022-05-07 01:39:23 +02:00
|
|
|
#include "common/dsp_sample_source/dsp_sample_source.h"
|
|
|
|
|
#include "live_pipeline.h"
|
|
|
|
|
|
2022-05-08 19:36:24 +02:00
|
|
|
#include "common/detect_header.h"
|
|
|
|
|
|
2022-05-07 10:39:24 +02:00
|
|
|
// Catch CTRL+C to exit live properly!
|
|
|
|
|
bool live_should_exit = false;
|
|
|
|
|
void sig_handler(int signo)
|
|
|
|
|
{
|
|
|
|
|
if (signo == SIGINT)
|
|
|
|
|
live_should_exit = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 12:05:44 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
2022-02-21 23:32:11 +01:00
|
|
|
// Init logger
|
2021-02-14 12:05:44 +01:00
|
|
|
initLogger();
|
|
|
|
|
|
2022-05-07 12:38:03 +02:00
|
|
|
if (std::string(argv[1]) == "live")
|
2021-10-29 15:44:15 +02:00
|
|
|
{
|
2022-05-07 12:38:03 +02:00
|
|
|
if (argc < 5) // Check overall command
|
|
|
|
|
{
|
|
|
|
|
logger->error("Usage : " + std::string(argv[0]) + " live [pipeline_id] [output_file_or_directory] [additional options as required]");
|
|
|
|
|
logger->error("Extra options (examples. Any parameter used in modules or sources can be used here) :");
|
|
|
|
|
logger->error(" --samplerate [baseband_samplerate] --baseband_format [f32/i16/i8/w8] --dc_block --iq_swap");
|
|
|
|
|
logger->error(" --source [airspy/rtlsdr/etc] --gain 20 --bias");
|
|
|
|
|
logger->error("As well as --timeout in seconds");
|
|
|
|
|
logger->error("Sample command :");
|
|
|
|
|
logger->error("./satdump live metop_ahrpt metop_output_directory --source airspy --samplerate 6e6 --frequency 1701.3e6 --general_gain 18 --bias --timeout 780");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2021-02-14 12:05:44 +01:00
|
|
|
|
2022-05-07 12:38:03 +02:00
|
|
|
// Init SatDump
|
|
|
|
|
satdump::initSatdump();
|
2021-09-30 17:33:32 +02:00
|
|
|
|
2022-05-07 01:39:23 +02:00
|
|
|
std::string downlink_pipeline = argv[2];
|
|
|
|
|
std::string output_file = argv[3];
|
2021-02-14 12:05:44 +01:00
|
|
|
|
2022-05-07 01:39:23 +02:00
|
|
|
// Parse flags
|
|
|
|
|
nlohmann::json parameters = parse_common_flags(argc - 4, &argv[4]);
|
2021-02-14 12:05:44 +01:00
|
|
|
|
2022-05-07 10:33:44 +02:00
|
|
|
uint64_t samplerate;
|
|
|
|
|
uint64_t frequency;
|
|
|
|
|
uint64_t timeout;
|
|
|
|
|
std::string handler_id;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
samplerate = parameters["samplerate"].get<uint64_t>();
|
|
|
|
|
frequency = parameters["frequency"].get<uint64_t>();
|
|
|
|
|
timeout = parameters.contains("timeout") ? parameters["timeout"].get<uint64_t>() : 0;
|
|
|
|
|
handler_id = parameters["source"].get<std::string>();
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception &e)
|
|
|
|
|
{
|
|
|
|
|
logger->error("Error parsing arguments! {:s}", e.what());
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2021-02-14 12:05:44 +01:00
|
|
|
|
2022-05-07 02:29:31 +02:00
|
|
|
// Create output dir
|
|
|
|
|
if (!std::filesystem::exists(output_file))
|
|
|
|
|
std::filesystem::create_directories(output_file);
|
|
|
|
|
|
2022-05-07 10:39:24 +02:00
|
|
|
// Get all sources
|
2022-05-07 01:39:23 +02:00
|
|
|
dsp::registerAllSources();
|
|
|
|
|
std::vector<dsp::SourceDescriptor> source_tr = dsp::getAllAvailableSources();
|
|
|
|
|
dsp::SourceDescriptor selected_src;
|
2021-02-14 12:05:44 +01:00
|
|
|
|
2022-05-07 01:39:23 +02:00
|
|
|
for (dsp::SourceDescriptor src : source_tr)
|
|
|
|
|
logger->debug("Device " + src.name);
|
2021-02-16 13:47:53 +01:00
|
|
|
|
2022-05-07 10:39:24 +02:00
|
|
|
// Try to find it and check it's usable
|
2022-05-07 10:33:44 +02:00
|
|
|
bool src_found = false;
|
2022-05-07 01:39:23 +02:00
|
|
|
for (dsp::SourceDescriptor src : source_tr)
|
2022-03-01 15:37:58 +01:00
|
|
|
{
|
2022-05-07 01:39:23 +02:00
|
|
|
if (handler_id == src.source_type)
|
2022-05-07 10:33:44 +02:00
|
|
|
{
|
2022-05-07 01:39:23 +02:00
|
|
|
selected_src = src;
|
2022-05-07 10:33:44 +02:00
|
|
|
src_found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!src_found)
|
|
|
|
|
{
|
|
|
|
|
logger->error("Could not find a handler for source type : {:s}!", handler_id.c_str());
|
|
|
|
|
return 1;
|
2022-03-01 15:37:58 +01:00
|
|
|
}
|
2022-05-07 01:39:23 +02:00
|
|
|
|
2022-05-07 10:39:24 +02:00
|
|
|
// Init source
|
2022-05-07 01:39:23 +02:00
|
|
|
std::shared_ptr<dsp::DSPSampleSource> source_ptr = getSourceFromDescriptor(selected_src);
|
|
|
|
|
source_ptr->open();
|
|
|
|
|
source_ptr->set_frequency(frequency);
|
|
|
|
|
source_ptr->set_samplerate(samplerate);
|
|
|
|
|
source_ptr->set_settings(parameters);
|
|
|
|
|
|
|
|
|
|
// Get pipeline
|
|
|
|
|
std::optional<satdump::Pipeline> pipeline = satdump::getPipelineFromName(downlink_pipeline);
|
2022-05-07 10:33:44 +02:00
|
|
|
|
|
|
|
|
if (!pipeline.has_value())
|
|
|
|
|
{
|
|
|
|
|
logger->critical("Pipeline " + downlink_pipeline + " does not exist!");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-07 10:39:24 +02:00
|
|
|
// Init pipeline
|
2022-05-07 01:39:23 +02:00
|
|
|
parameters["baseband_format"] = "f32";
|
|
|
|
|
parameters["buffer_size"] = STREAM_BUFFER_SIZE; // This is required, as we WILL go over the (usually) default 8192 size
|
|
|
|
|
std::unique_ptr<satdump::LivePipeline> live_pipeline = std::make_unique<satdump::LivePipeline>(pipeline.value(), parameters, output_file);
|
|
|
|
|
|
2022-05-07 10:39:24 +02:00
|
|
|
ctpl::thread_pool live_thread_pool(8);
|
2022-05-07 10:33:44 +02:00
|
|
|
|
2022-05-07 10:39:24 +02:00
|
|
|
// Attempt to start the source and pipeline
|
2022-05-07 10:33:44 +02:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
source_ptr->start();
|
2022-05-07 10:39:24 +02:00
|
|
|
live_pipeline->start(source_ptr->output_stream, live_thread_pool);
|
2022-05-07 10:33:44 +02:00
|
|
|
}
|
|
|
|
|
catch (std::exception &e)
|
|
|
|
|
{
|
|
|
|
|
logger->error("Fatal error running pipeline/device : " + std::string(e.what()));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2022-05-07 01:39:23 +02:00
|
|
|
|
2022-05-07 10:39:24 +02:00
|
|
|
// Attach signal
|
|
|
|
|
signal(SIGINT, sig_handler);
|
2022-05-07 01:39:23 +02:00
|
|
|
|
2022-05-07 10:39:24 +02:00
|
|
|
// Now, we wait
|
|
|
|
|
uint64_t start_time = time(0);
|
2022-05-07 01:39:23 +02:00
|
|
|
while (1)
|
2022-03-01 15:37:58 +01:00
|
|
|
{
|
2022-05-07 01:39:23 +02:00
|
|
|
if (timeout > 0)
|
|
|
|
|
{
|
2022-05-07 02:26:57 +02:00
|
|
|
uint64_t elapsed_time = time(0) - start_time;
|
2022-05-07 01:39:23 +02:00
|
|
|
if (elapsed_time >= timeout)
|
2022-05-07 10:39:24 +02:00
|
|
|
{
|
2022-05-07 10:45:14 +02:00
|
|
|
logger->warn("Timeout is over! ({:d}s >= {:d}s) Stopping.", elapsed_time, timeout);
|
2022-05-07 01:39:23 +02:00
|
|
|
break;
|
2022-05-07 10:39:24 +02:00
|
|
|
}
|
2022-05-07 01:39:23 +02:00
|
|
|
}
|
2022-05-07 10:39:24 +02:00
|
|
|
|
|
|
|
|
if (live_should_exit)
|
|
|
|
|
{
|
|
|
|
|
logger->warn("SIGINT Received. Stopping.");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-07 10:45:14 +02:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
2022-03-01 15:37:58 +01:00
|
|
|
}
|
2022-05-07 01:39:23 +02:00
|
|
|
|
2022-05-07 10:39:24 +02:00
|
|
|
// Stop cleanly
|
2022-05-07 01:39:23 +02:00
|
|
|
source_ptr->stop();
|
|
|
|
|
live_pipeline->stop();
|
2022-03-01 15:37:58 +01:00
|
|
|
}
|
2021-02-16 13:47:53 +01:00
|
|
|
else
|
2022-05-07 01:39:23 +02:00
|
|
|
{
|
2022-05-07 12:38:03 +02:00
|
|
|
if (argc < 5) // Check overall command
|
|
|
|
|
{
|
|
|
|
|
logger->error("Usage : " + std::string(argv[0]) + " [pipeline_id] [input_level] [input_file] [output_file_or_directory] [additional options as required]");
|
|
|
|
|
logger->error("Extra options (examples. Any parameter used in modules can be used here) :");
|
|
|
|
|
logger->error(" --samplerate [baseband_samplerate] --baseband_format [f32/s16/s8/u8] --dc_block --iq_swap");
|
|
|
|
|
logger->error("Sample command :");
|
|
|
|
|
logger->error("./satdump metop_ahrpt baseband /home/user/metop_baseband.s16 metop_output_directory --samplerate 6e6 --baseband_format s16");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Init SatDump
|
|
|
|
|
satdump::initSatdump();
|
|
|
|
|
|
2022-05-07 01:39:23 +02:00
|
|
|
std::string downlink_pipeline = argv[1];
|
|
|
|
|
std::string input_level = argv[2];
|
|
|
|
|
std::string input_file = argv[3];
|
|
|
|
|
std::string output_file = argv[4];
|
|
|
|
|
|
|
|
|
|
// Parse flags
|
|
|
|
|
nlohmann::json parameters = parse_common_flags(argc - 5, &argv[5]);
|
|
|
|
|
|
2022-05-08 19:36:24 +02:00
|
|
|
if (std::filesystem::exists(input_file) && !std::filesystem::is_directory(input_file))
|
|
|
|
|
{
|
|
|
|
|
HeaderInfo hdr = try_parse_header(input_file);
|
|
|
|
|
if (hdr.valid)
|
|
|
|
|
{
|
|
|
|
|
parameters["samplerate"] = hdr.samplerate;
|
|
|
|
|
parameters["baseband_format"] = hdr.type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-07 01:39:23 +02:00
|
|
|
// logger->warn("\n" + parameters.dump(4));
|
|
|
|
|
// exit(0);
|
|
|
|
|
|
|
|
|
|
// Print some useful info
|
|
|
|
|
logger->info("Starting processing pipeline " + downlink_pipeline + "...");
|
|
|
|
|
logger->debug("Input file (" + input_level + ") : " + input_file);
|
|
|
|
|
logger->debug("Output file : " + output_file);
|
|
|
|
|
|
|
|
|
|
// Create output dir
|
|
|
|
|
if (!std::filesystem::exists(output_file))
|
|
|
|
|
std::filesystem::create_directories(output_file);
|
|
|
|
|
|
|
|
|
|
// Get pipeline
|
|
|
|
|
std::optional<satdump::Pipeline> pipeline = satdump::getPipelineFromName(downlink_pipeline);
|
|
|
|
|
|
|
|
|
|
if (pipeline.has_value())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
pipeline.value().run(input_file, output_file, parameters, input_level);
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception &e)
|
|
|
|
|
{
|
|
|
|
|
logger->error("Fatal error running pipeline : " + std::string(e.what()));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
logger->critical("Pipeline " + downlink_pipeline + " does not exist!");
|
|
|
|
|
}
|
2021-02-14 12:05:44 +01:00
|
|
|
|
|
|
|
|
logger->info("Done! Goodbye");
|
2021-08-23 14:35:53 +01:00
|
|
|
}
|