satdump/src-cli/record.cpp

249 lines
7.9 KiB
C++
Raw Normal View History

2022-05-21 14:15:14 +02:00
#include "live.h"
2022-08-26 15:17:10 +02:00
#include "common/dsp_source_sink/dsp_sample_source.h"
2023-07-27 20:58:02 +02:00
#include "common/dsp/resamp/smart_resampler.h"
2022-05-21 14:15:14 +02:00
#include <signal.h>
#include "logger.h"
#include "common/cli_utils.h"
2023-01-31 01:12:10 +01:00
#include "common/dsp/io/file_sink.h"
2022-05-21 14:15:14 +02:00
#include "init.h"
2023-02-21 16:44:44 +01:00
#include "common/dsp/path/splitter.h"
2023-01-31 01:12:10 +01:00
#include "common/dsp/fft/fft_pan.h"
2023-01-05 23:33:59 +01:00
#include "webserver.h"
2022-05-21 14:15:14 +02:00
// Catch CTRL+C to exit live properly!
bool rec_should_exit = false;
void sig_handler_rec(int signo)
{
if (signo == SIGINT)
rec_should_exit = true;
}
int main_record(int argc, char *argv[])
{
if (argc < 5) // Check overall command
{
logger->error("Usage : " + std::string(argv[0]) + " record [output_baseband (without extension!)] [additional options as required]");
logger->error("Extra options (examples. Any parameter used in sources can be used here) :");
logger->error(" --samplerate [baseband_samplerate] --baseband_format [f32/s16/s8/u8/w16/ziq] --dc_block --iq_swap");
2022-05-21 14:15:14 +02:00
logger->error(" --source [airspy/rtlsdr/etc] --gain 20 --bias");
logger->error("As well as --timeout in seconds");
logger->error("Sample command :");
logger->error("./satdump record baseband_name --source airspy --samplerate 6e6 --frequency 1701.3e6 --general_gain 18 --bias --timeout 780");
return 1;
}
// Init SatDump
satdump::initSatdump();
std::string output_file = argv[2];
// Parse flags
nlohmann::json parameters = parse_common_flags(argc - 3, &argv[3]);
uint64_t samplerate;
uint64_t frequency;
uint64_t timeout;
std::string handler_id;
2023-07-27 20:58:02 +02:00
double decimation = 1;
2022-05-21 14:15:14 +02:00
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>();
2023-07-27 20:58:02 +02:00
if (parameters.contains("decimation"))
decimation = parameters["decimation"].get<int>();
2022-05-21 14:15:14 +02:00
}
catch (std::exception &e)
{
2023-05-08 23:08:34 +02:00
logger->error("Error parsing arguments! %s", e.what());
2022-05-21 14:15:14 +02:00
return 1;
}
// Create output dir
if (std::filesystem::path(output_file).has_parent_path())
if (!std::filesystem::exists(std::filesystem::path(output_file).parent_path().string()))
std::filesystem::create_directories(std::filesystem::path(output_file).parent_path().string());
2022-05-21 14:15:14 +02:00
// Get all sources
dsp::registerAllSources();
std::vector<dsp::SourceDescriptor> source_tr = dsp::getAllAvailableSources();
dsp::SourceDescriptor selected_src;
for (dsp::SourceDescriptor src : source_tr)
logger->debug("Device " + src.name);
// Try to find it and check it's usable
bool src_found = false;
for (dsp::SourceDescriptor src : source_tr)
{
if (handler_id == src.source_type)
{
selected_src = src;
src_found = true;
}
}
if (!src_found)
{
2023-05-08 23:08:34 +02:00
logger->error("Could not find a handler for source type : %s!", handler_id.c_str());
2022-05-21 14:15:14 +02:00
return 1;
}
// Init source
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);
2023-07-27 20:58:02 +02:00
std::unique_ptr<dsp::SmartResamplerBlock<complex_t>> decim;
2023-01-05 23:33:59 +01:00
std::unique_ptr<dsp::SplitterBlock> splitter;
std::unique_ptr<dsp::FFTPanBlock> fft;
bool webserver_already_set = false;
2022-05-21 14:15:14 +02:00
// Attempt to start the source
try
{
source_ptr->start();
}
catch (std::exception &e)
{
logger->error("Fatal error starting device : " + std::string(e.what()));
return 1;
}
2023-07-27 20:58:02 +02:00
// Decimation if requested
if (decimation > 1)
decim = std::make_unique<dsp::SmartResamplerBlock<complex_t>>(source_ptr->output_stream, 1, decimation);
2023-01-05 23:33:59 +01:00
// Optional FFT
2023-07-27 20:58:02 +02:00
std::shared_ptr<dsp::stream<complex_t>> final_stream = decimation > 1 ? decim->output_stream : source_ptr->output_stream;
2023-01-05 23:33:59 +01:00
int fft_size = 0;
if (parameters.contains("fft_enable"))
{
fft_size = parameters.contains("fft_size") ? parameters["fft_size"].get<int>() : 512;
int fft_rate = parameters.contains("fft_rate") ? parameters["fft_rate"].get<int>() : 30;
splitter = std::make_unique<dsp::SplitterBlock>(source_ptr->output_stream);
2023-02-19 18:19:01 +01:00
splitter->add_output("fft");
splitter->set_enabled("fft", true);
2023-01-05 23:33:59 +01:00
final_stream = splitter->output_stream;
2023-02-19 18:19:01 +01:00
fft = std::make_unique<dsp::FFTPanBlock>(splitter->get_output("fft"));
2023-07-27 20:58:02 +02:00
fft->set_fft_settings(fft_size, samplerate / decimation, fft_rate);
2023-01-05 23:33:59 +01:00
if (parameters.contains("fft_avg"))
fft->avg_rate = parameters["fft_avg"].get<float>();
splitter->start();
fft->start();
}
2022-05-21 14:15:14 +02:00
// Setup file sink
2023-01-05 23:33:59 +01:00
std::shared_ptr<dsp::FileSinkBlock> file_sink = std::make_shared<dsp::FileSinkBlock>(final_stream);
if (parameters.contains("fft_enable"))
{
webserver::handle_callback = [&file_sink, &fft, fft_size]()
{
nlohmann::json stats;
stats["written"] = file_sink->get_written();
stats["written_raw"] = file_sink->get_written_raw();
for (int i = 0; i < fft_size; i++)
stats["fft_values"][i] = fft->output_stream->writeBuf[i];
return stats.dump(4);
};
webserver_already_set = true;
}
2022-05-21 14:15:14 +02:00
int ziq_bit_depth = 8;
if (parameters.contains("ziq_depth"))
ziq_bit_depth = parameters["ziq_depth"].get<int>();
if (parameters["baseband_format"].get<std::string>() == "ziq")
2023-05-08 23:08:34 +02:00
logger->info("Using ZIQ Depth %d", ziq_bit_depth);
2022-05-21 14:15:14 +02:00
if (parameters.contains("baseband_format"))
{
file_sink->set_output_sample_type(dsp::basebandTypeFromString(parameters["baseband_format"].get<std::string>()));
}
else
{
logger->error("baseband_format flag is required!");
return 1;
}
file_sink->start();
2023-07-27 20:58:02 +02:00
file_sink->start_recording(output_file, samplerate / decimation, ziq_bit_depth);
2022-05-21 14:15:14 +02:00
2023-01-05 23:33:59 +01:00
// If requested, boot up webserver
if (parameters.contains("http_server"))
{
std::string http_addr = parameters["http_server"].get<std::string>();
if (!webserver_already_set)
webserver::handle_callback = [&file_sink]()
{
nlohmann::json stats;
stats["written"] = file_sink->get_written();
stats["written_raw"] = file_sink->get_written_raw();
return stats.dump(4);
};
2023-05-08 23:08:34 +02:00
logger->info("Start webserver on %s", http_addr.c_str());
2023-01-05 23:33:59 +01:00
webserver::start(http_addr);
}
2022-05-21 14:15:14 +02:00
// Attach signal
signal(SIGINT, sig_handler_rec);
// Now, we wait
uint64_t start_time = time(0);
while (1)
{
uint64_t elapsed_time = time(0) - start_time;
if (timeout > 0)
{
if (elapsed_time >= timeout)
{
2023-05-08 23:08:34 +02:00
logger->warn("Timeout is over! (%ds >= %ds) Stopping.", elapsed_time, timeout);
2022-05-21 14:15:14 +02:00
break;
}
}
if (rec_should_exit)
{
logger->warn("SIGINT Received. Stopping.");
break;
}
if (int(elapsed_time) % 2 == 0)
{
if (parameters["baseband_format"].get<std::string>() == "ziq")
2023-05-08 23:08:34 +02:00
logger->info("Wrote %d MB, raw %d MB", int(file_sink->get_written() / 1e6), int(file_sink->get_written_raw() / 1e6));
2022-05-21 14:15:14 +02:00
else
2023-05-08 23:08:34 +02:00
logger->info("Wrote %d MB", int(file_sink->get_written() / 1e6));
2022-05-21 14:15:14 +02:00
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// Stop recording
file_sink->stop_recording();
// Stop cleanly
source_ptr->stop();
2023-01-05 23:33:59 +01:00
if (parameters.contains("fft_enable"))
{
splitter->stop();
fft->stop();
}
2022-05-21 14:15:14 +02:00
file_sink->stop();
2023-01-05 23:33:59 +01:00
if (parameters.contains("http_server"))
webserver::stop();
2022-05-21 14:15:14 +02:00
return 0;
}