2024-03-13 11:19:24 +00:00
|
|
|
// #define DEBUGTRACE_ENABLED
|
2022-09-03 18:59:14 +00:00
|
|
|
#include "lasp_streammgr.h"
|
2024-02-06 13:59:51 +00:00
|
|
|
|
2022-07-20 12:58:48 +00:00
|
|
|
#include <assert.h>
|
2024-02-06 13:59:51 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2022-07-20 12:58:48 +00:00
|
|
|
#include <functional>
|
2022-07-29 07:32:26 +00:00
|
|
|
#include <iostream>
|
2023-06-07 19:49:07 +00:00
|
|
|
#include <memory>
|
2023-06-10 13:47:52 +00:00
|
|
|
#include <mutex>
|
2024-03-14 07:25:47 +00:00
|
|
|
#include <thread>
|
2022-07-29 07:32:26 +00:00
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
#include "debugtrace.hpp"
|
|
|
|
#include "lasp_biquadbank.h"
|
|
|
|
#include "lasp_indatahandler.h"
|
|
|
|
#include "lasp_thread.h"
|
|
|
|
|
2024-03-13 11:19:24 +00:00
|
|
|
using namespace std::literals::chrono_literals;
|
|
|
|
|
2022-07-29 07:32:26 +00:00
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
2022-09-03 18:59:14 +00:00
|
|
|
using rte = std::runtime_error;
|
2022-06-29 10:25:32 +00:00
|
|
|
|
2023-06-07 19:49:07 +00:00
|
|
|
/**
|
2023-06-09 08:43:04 +00:00
|
|
|
* @brief The main global handle to a stream, stored in a weak pointer, if it
|
|
|
|
* does not yet exist, via StreamMgr::getInstance, a new stream mgr is created.
|
|
|
|
* It also makes sure that the stream manager is deleted once the latest handle
|
|
|
|
* to it has been destroyed (no global stuff left).
|
2023-06-07 19:49:07 +00:00
|
|
|
*/
|
2023-06-09 08:43:04 +00:00
|
|
|
std::weak_ptr<StreamMgr> _mgr;
|
2023-06-10 13:47:52 +00:00
|
|
|
std::mutex _mgr_mutex;
|
2022-06-29 10:25:32 +00:00
|
|
|
|
2024-03-13 11:19:24 +00:00
|
|
|
using Lck = std::scoped_lock<std::recursive_mutex>;
|
2022-07-29 07:32:26 +00:00
|
|
|
|
2023-06-09 08:43:04 +00:00
|
|
|
/**
|
|
|
|
* @brief The only way to obtain a stream manager, can only be called from the
|
|
|
|
* thread that does it the first time.
|
|
|
|
*
|
|
|
|
* @return Stream manager handle
|
|
|
|
*/
|
|
|
|
SmgrHandle StreamMgr::getInstance() {
|
2022-07-29 07:32:26 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2023-06-07 19:49:07 +00:00
|
|
|
|
2024-03-13 11:19:24 +00:00
|
|
|
std::scoped_lock<std::mutex> lck(_mgr_mutex);
|
2023-06-09 08:43:04 +00:00
|
|
|
auto mgr = _mgr.lock();
|
|
|
|
if (!mgr) {
|
2023-06-10 13:47:52 +00:00
|
|
|
// Double Check Locking Pattern, if two threads would simultaneously
|
|
|
|
// instantiate the singleton instance.
|
|
|
|
|
|
|
|
auto mgr = _mgr.lock();
|
|
|
|
if (mgr) {
|
|
|
|
return mgr;
|
|
|
|
}
|
2023-06-09 08:43:04 +00:00
|
|
|
|
|
|
|
mgr = SmgrHandle(new StreamMgr());
|
|
|
|
if (!mgr) {
|
2023-06-07 19:49:07 +00:00
|
|
|
throw rte("Fatal: could not allocate stream manager!");
|
|
|
|
}
|
2023-06-09 08:43:04 +00:00
|
|
|
// Update global weak pointer
|
|
|
|
_mgr = mgr;
|
|
|
|
return mgr;
|
2022-07-29 07:32:26 +00:00
|
|
|
}
|
2023-06-09 08:43:04 +00:00
|
|
|
#if LASP_DEBUG == 1
|
|
|
|
// Make sure we never ask for a new SmgrHandle from a different thread.
|
|
|
|
assert(std::this_thread::get_id() == mgr->main_thread_id);
|
2022-07-29 07:32:26 +00:00
|
|
|
#endif
|
|
|
|
|
2022-06-29 10:25:32 +00:00
|
|
|
return mgr;
|
|
|
|
}
|
2023-06-06 14:05:24 +00:00
|
|
|
|
2023-06-09 08:43:04 +00:00
|
|
|
StreamMgr::StreamMgr()
|
2022-09-03 18:59:14 +00:00
|
|
|
#if LASP_DEBUG == 1
|
2023-06-10 13:47:52 +00:00
|
|
|
: main_thread_id(std::this_thread::get_id())
|
2022-09-03 18:59:14 +00:00
|
|
|
#endif
|
2023-06-09 08:43:04 +00:00
|
|
|
{
|
|
|
|
DEBUGTRACE_ENTER;
|
2022-09-03 18:59:14 +00:00
|
|
|
// Trigger a scan for the available devices, in the background.
|
2024-03-27 12:45:13 +00:00
|
|
|
rescanDAQDevices(true);
|
2022-09-03 18:59:14 +00:00
|
|
|
}
|
|
|
|
#if LASP_DEBUG == 1
|
|
|
|
void StreamMgr::checkRightThread() const {
|
2023-06-09 08:43:04 +00:00
|
|
|
assert(std::this_thread::get_id() == main_thread_id);
|
2022-09-03 18:59:14 +00:00
|
|
|
}
|
|
|
|
#endif
|
2022-06-29 10:25:32 +00:00
|
|
|
|
2022-09-22 08:18:38 +00:00
|
|
|
void StreamMgr::rescanDAQDevices(bool background,
|
|
|
|
std::function<void()> callback) {
|
2022-09-27 15:20:45 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2023-06-11 12:33:16 +00:00
|
|
|
DEBUGTRACE_PRINT(background);
|
2024-03-14 07:25:47 +00:00
|
|
|
if (_scanningDevices) {
|
2024-03-13 11:19:24 +00:00
|
|
|
throw rte("A background device scan is already busy");
|
|
|
|
}
|
2022-09-27 15:20:45 +00:00
|
|
|
|
2024-03-13 11:19:24 +00:00
|
|
|
Lck lck(_mtx);
|
2022-09-03 18:59:14 +00:00
|
|
|
checkRightThread();
|
2023-06-06 14:05:24 +00:00
|
|
|
if (_inputStream || _outputStream) {
|
|
|
|
throw rte("Rescanning DAQ devices only possible when no stream is running");
|
|
|
|
}
|
2022-09-27 15:20:45 +00:00
|
|
|
|
2022-09-22 08:18:38 +00:00
|
|
|
_devices.clear();
|
2022-10-10 17:17:38 +00:00
|
|
|
if (!background) {
|
2024-03-13 11:19:24 +00:00
|
|
|
_scanningDevices = true;
|
2022-09-03 18:59:14 +00:00
|
|
|
rescanDAQDevices_impl(callback);
|
2022-10-10 17:17:38 +00:00
|
|
|
} else {
|
2023-06-09 08:43:04 +00:00
|
|
|
DEBUGTRACE_PRINT("Rescanning DAQ devices on different thread...");
|
2024-03-13 11:19:24 +00:00
|
|
|
_scanningDevices = true;
|
2023-06-10 13:47:52 +00:00
|
|
|
_pool.push_task(&StreamMgr::rescanDAQDevices_impl, this, callback);
|
2022-09-03 18:59:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void StreamMgr::rescanDAQDevices_impl(std::function<void()> callback) {
|
2022-09-27 15:20:45 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2024-03-27 12:45:13 +00:00
|
|
|
assert(!_inputStream && !_outputStream);
|
2024-03-13 11:19:24 +00:00
|
|
|
Lck lck(_mtx);
|
2024-03-27 12:45:13 +00:00
|
|
|
// Alsa spits out annoying messages that are not useful
|
|
|
|
{
|
|
|
|
|
|
|
|
_devices = DeviceInfo::getDeviceInfo();
|
|
|
|
}
|
2022-09-03 18:59:14 +00:00
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
2024-03-27 12:45:13 +00:00
|
|
|
|
2024-03-13 11:19:24 +00:00
|
|
|
_scanningDevices = false;
|
2022-09-03 18:59:14 +00:00
|
|
|
}
|
2023-06-09 08:43:04 +00:00
|
|
|
void StreamMgr::inCallback(const DaqData &data) {
|
2022-10-10 17:17:38 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2022-09-27 15:20:45 +00:00
|
|
|
|
2024-03-13 11:19:24 +00:00
|
|
|
Lck lck(_mtx);
|
2022-06-29 10:25:32 +00:00
|
|
|
|
2022-10-20 15:12:34 +00:00
|
|
|
assert(_inputFilters.size() == data.nchannels);
|
2022-09-28 07:41:02 +00:00
|
|
|
|
2022-10-20 15:12:34 +00:00
|
|
|
if (std::count_if(_inputFilters.cbegin(), _inputFilters.cend(),
|
|
|
|
[](const auto &a) { return bool(a); }) > 0) {
|
|
|
|
/// Found a filter in vector of input filters. So we have to apply the
|
|
|
|
/// filters to each channel.
|
|
|
|
|
|
|
|
DaqData input_filtered(data.nframes, data.nchannels, data.dtype);
|
|
|
|
|
|
|
|
for (us ch = 0; ch < data.nchannels; ch++) {
|
|
|
|
if (_inputFilters[ch]) {
|
|
|
|
DEBUGTRACE_PRINT("Filter ch:");
|
|
|
|
DEBUGTRACE_PRINT(ch);
|
|
|
|
vd inout = data.toFloat(ch);
|
|
|
|
_inputFilters[ch]->filter(inout);
|
|
|
|
input_filtered.fromFloat(ch, inout);
|
|
|
|
} else {
|
|
|
|
DEBUGTRACE_PRINT("No filter ch:");
|
|
|
|
DEBUGTRACE_PRINT(ch);
|
|
|
|
input_filtered.copyInFromRaw(ch, data.raw_ptr(0, ch));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 11:19:24 +00:00
|
|
|
DEBUGTRACE_PRINT("Calling incallback for handlers (filtered)...");
|
2022-10-20 15:12:34 +00:00
|
|
|
for (auto &handler : _inDataHandlers) {
|
2023-06-09 08:43:04 +00:00
|
|
|
handler->inCallback(input_filtered);
|
2022-10-20 15:12:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/// No input filters
|
2024-03-13 11:19:24 +00:00
|
|
|
DEBUGTRACE_PRINT("Calling incallback for handlers...");
|
2022-10-20 15:12:34 +00:00
|
|
|
for (auto &handler : _inDataHandlers) {
|
2023-06-09 08:43:04 +00:00
|
|
|
handler->inCallback(data);
|
2022-10-20 15:12:34 +00:00
|
|
|
}
|
2022-06-29 10:25:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 19:02:41 +00:00
|
|
|
void StreamMgr::setSiggen(std::shared_ptr<Siggen> siggen) {
|
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
checkRightThread();
|
|
|
|
|
2024-03-13 11:19:24 +00:00
|
|
|
Lck lck(_mtx);
|
2022-09-22 19:02:41 +00:00
|
|
|
// If not set to nullptr, and a stream is running, we update the signal
|
|
|
|
// generator by resetting it.
|
|
|
|
if (isStreamRunningOK(StreamType::output) && siggen) {
|
|
|
|
const Daq *daq = getDaq(StreamType::output);
|
|
|
|
assert(daq != nullptr);
|
|
|
|
// Reset the signal generator.
|
|
|
|
siggen->reset(daq->samplerate());
|
|
|
|
}
|
|
|
|
_siggen = siggen;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DEBUG_FILLDATA 0
|
2022-06-29 10:25:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Converts from double precision floating point to output signal in
|
|
|
|
* non-interleaving format.
|
|
|
|
*
|
|
|
|
* @tparam T
|
|
|
|
* @param data
|
|
|
|
* @param signal
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2024-02-06 13:59:51 +00:00
|
|
|
template <typename T>
|
|
|
|
bool fillData(DaqData &data, const vd &signal) {
|
2022-09-22 19:02:41 +00:00
|
|
|
/* DEBUGTRACE_ENTER; */
|
2022-06-29 10:25:32 +00:00
|
|
|
assert(data.nframes == signal.size());
|
|
|
|
|
2022-07-20 12:58:48 +00:00
|
|
|
T *res = reinterpret_cast<T *>(data.raw_ptr());
|
2022-06-29 10:25:32 +00:00
|
|
|
if (std::is_floating_point<T>()) {
|
|
|
|
for (us ch = 0; ch < data.nchannels; ch++) {
|
|
|
|
for (us frame = 0; frame < data.nframes; frame++) {
|
2022-09-22 19:02:41 +00:00
|
|
|
#if DEBUG_FILLDATA == 1
|
|
|
|
DEBUGTRACE_PRINT("SLOW flt");
|
|
|
|
data.setSlow(frame, ch,
|
|
|
|
reinterpret_cast<const int8_t *>(&signal[frame]));
|
|
|
|
#else
|
2022-07-20 12:58:48 +00:00
|
|
|
res[ch * data.nframes + frame] = signal[frame];
|
2022-09-22 19:02:41 +00:00
|
|
|
#endif
|
2022-06-29 10:25:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (us ch = 0; ch < data.nchannels; ch++) {
|
|
|
|
for (us frame = 0; frame < data.nframes; frame++) {
|
2022-09-22 19:02:41 +00:00
|
|
|
const T val = (signal[frame] * std::numeric_limits<T>::max());
|
|
|
|
#if DEBUG_FILLDATA == 1
|
|
|
|
data.setSlow(frame, ch, reinterpret_cast<const int8_t *>(&val));
|
|
|
|
#else
|
|
|
|
res[ch * data.nframes + frame] = val;
|
|
|
|
#endif
|
2022-06-29 10:25:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-06-09 08:43:04 +00:00
|
|
|
void StreamMgr::outCallback(DaqData &data) {
|
2024-03-13 11:19:24 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2022-07-20 12:58:48 +00:00
|
|
|
|
2024-03-13 11:19:24 +00:00
|
|
|
Lck lck(_mtx);
|
2022-09-22 19:02:41 +00:00
|
|
|
|
2022-07-20 12:58:48 +00:00
|
|
|
if (_siggen) {
|
2022-06-29 10:25:32 +00:00
|
|
|
vd signal = _siggen->genSignal(data.nframes);
|
|
|
|
switch (data.dtype) {
|
2024-02-06 13:59:51 +00:00
|
|
|
case (DataTypeDescriptor::DataType::dtype_fl32):
|
|
|
|
fillData<float>(data, signal);
|
|
|
|
break;
|
|
|
|
case (DataTypeDescriptor::DataType::dtype_fl64):
|
|
|
|
fillData<double>(data, signal);
|
|
|
|
break;
|
|
|
|
case (DataTypeDescriptor::DataType::dtype_int8):
|
|
|
|
fillData<int8_t>(data, signal);
|
|
|
|
break;
|
|
|
|
case (DataTypeDescriptor::DataType::dtype_int16):
|
|
|
|
fillData<int16_t>(data, signal);
|
|
|
|
break;
|
|
|
|
case (DataTypeDescriptor::DataType::dtype_int32):
|
|
|
|
fillData<int32_t>(data, signal);
|
|
|
|
break;
|
2022-06-29 10:25:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Set all values to 0.
|
2022-07-20 12:58:48 +00:00
|
|
|
std::fill(data.raw_ptr(), data.raw_ptr() + data.size_bytes(), 0);
|
2022-06-29 10:25:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 07:32:26 +00:00
|
|
|
StreamMgr::~StreamMgr() {
|
|
|
|
DEBUGTRACE_ENTER;
|
2024-03-13 11:19:24 +00:00
|
|
|
while (_scanningDevices) {
|
|
|
|
std::this_thread::sleep_for(10us);
|
|
|
|
}
|
2024-03-14 07:25:47 +00:00
|
|
|
|
2024-03-13 11:19:24 +00:00
|
|
|
#if LASP_DEBUG == 1
|
|
|
|
{ // Careful, this lock needs to be released to make sure the streams can
|
2024-03-14 07:25:47 +00:00
|
|
|
// obtain a lock to the stream manager.
|
2024-03-13 11:19:24 +00:00
|
|
|
Lck lck(_mtx);
|
|
|
|
checkRightThread();
|
|
|
|
}
|
|
|
|
#endif
|
2023-06-07 19:49:07 +00:00
|
|
|
// Stream manager now handled by shared pointer. Each indata handler gets a
|
|
|
|
// shared pointer to the stream manager, and stores a weak pointer to it.
|
|
|
|
// Hence, we do not have to do any cleanup here. It also makes sure that the
|
|
|
|
// order in which destructors are called does not matter anymore. As soon as
|
|
|
|
// the stream manager is destructed, the weak pointers loose there ref, and do
|
|
|
|
// not have to removeInDataHandler() anymore.
|
2023-06-18 19:13:09 +00:00
|
|
|
|
|
|
|
// Stop the streams in this phase, otherwise it might happen during the
|
|
|
|
// destruction of the Siggen, in which case we might get calls to pure
|
|
|
|
// virtual methods. This was really a bug.
|
|
|
|
_inputStream.reset();
|
|
|
|
_outputStream.reset();
|
2022-07-29 07:32:26 +00:00
|
|
|
}
|
2022-07-20 12:58:48 +00:00
|
|
|
void StreamMgr::stopAllStreams() {
|
2022-09-03 18:59:14 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2024-03-14 07:25:47 +00:00
|
|
|
{
|
|
|
|
Lck lck(_mtx);
|
|
|
|
checkRightThread();
|
|
|
|
}
|
2024-03-13 11:19:24 +00:00
|
|
|
// No lock here!
|
2022-07-20 12:58:48 +00:00
|
|
|
_inputStream.reset();
|
|
|
|
_outputStream.reset();
|
|
|
|
}
|
2022-06-29 10:25:32 +00:00
|
|
|
|
2022-09-03 18:59:14 +00:00
|
|
|
void StreamMgr::startStream(const DaqConfiguration &config) {
|
|
|
|
DEBUGTRACE_ENTER;
|
2024-03-14 07:25:47 +00:00
|
|
|
if (_scanningDevices) {
|
2024-03-13 11:19:24 +00:00
|
|
|
throw rte("DAQ device scan is busy. Cannot start stream.");
|
|
|
|
}
|
|
|
|
Lck lck(_mtx);
|
2022-09-03 18:59:14 +00:00
|
|
|
checkRightThread();
|
2022-06-29 10:25:32 +00:00
|
|
|
|
2022-07-20 12:58:48 +00:00
|
|
|
bool isInput = std::count_if(config.inchannel_config.cbegin(),
|
2022-07-29 07:32:26 +00:00
|
|
|
config.inchannel_config.cend(),
|
2024-02-06 13:59:51 +00:00
|
|
|
[](auto &i) { return i.enabled; }) > 0;
|
2022-09-03 18:59:14 +00:00
|
|
|
|
2022-10-10 17:17:38 +00:00
|
|
|
bool isOutput = std::count_if(config.outchannel_config.cbegin(),
|
|
|
|
config.outchannel_config.cend(),
|
2024-02-06 13:59:51 +00:00
|
|
|
[](auto &i) { return i.enabled; }) > 0;
|
2022-10-10 17:17:38 +00:00
|
|
|
|
2022-09-03 18:59:14 +00:00
|
|
|
// Find the first device that matches with the configuration
|
2023-01-20 14:50:51 +00:00
|
|
|
DeviceInfo *devinfo = nullptr;
|
2022-09-03 18:59:14 +00:00
|
|
|
|
2023-06-07 19:49:07 +00:00
|
|
|
// Match configuration to a device in the list of devices
|
2023-01-20 14:50:51 +00:00
|
|
|
for (auto &devinfoi : _devices) {
|
|
|
|
if (config.match(*devinfoi)) {
|
|
|
|
devinfo = devinfoi.get();
|
|
|
|
break;
|
2022-09-03 18:59:14 +00:00
|
|
|
}
|
2023-01-20 14:50:51 +00:00
|
|
|
}
|
2023-06-07 19:49:07 +00:00
|
|
|
if (devinfo == nullptr) {
|
2023-01-20 14:50:51 +00:00
|
|
|
throw rte("Could not find a device with name " + config.device_name +
|
|
|
|
" in list of devices.");
|
2022-09-03 18:59:14 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 14:50:51 +00:00
|
|
|
isInput |= (config.monitorOutput && devinfo->hasInternalOutputMonitor);
|
2022-10-19 08:03:13 +00:00
|
|
|
DEBUGTRACE_PRINT(isInput);
|
2022-07-20 12:58:48 +00:00
|
|
|
|
2022-06-29 10:25:32 +00:00
|
|
|
bool isDuplex = isInput && isOutput;
|
|
|
|
|
|
|
|
if (!isInput && !isOutput) {
|
2024-02-06 13:59:51 +00:00
|
|
|
throw rte(
|
|
|
|
"Attempted stream start failed, stream does not have any enabled "
|
|
|
|
"channels. Please first enable channels in the channel configuration.");
|
2022-06-29 10:25:32 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 17:17:38 +00:00
|
|
|
if (isInput && _inputStream) {
|
2024-02-06 13:59:51 +00:00
|
|
|
throw rte(
|
|
|
|
"Error: an input stream is already running. Please "
|
|
|
|
"first stop existing stream");
|
2022-07-20 12:58:48 +00:00
|
|
|
} else if (isOutput && _outputStream) {
|
2024-02-06 13:59:51 +00:00
|
|
|
throw rte(
|
|
|
|
"Error: output stream is already running. Please "
|
|
|
|
"first stop existing stream");
|
2022-10-10 17:17:38 +00:00
|
|
|
} else if (_inputStream) {
|
|
|
|
if (_inputStream->duplexMode() && isOutput) {
|
2024-02-06 13:59:51 +00:00
|
|
|
throw rte(
|
|
|
|
"Error: output stream is already running (in duplex mode). "
|
|
|
|
"Please "
|
|
|
|
"first stop existing stream");
|
2022-10-10 17:17:38 +00:00
|
|
|
}
|
2022-07-20 12:58:48 +00:00
|
|
|
}
|
2022-06-29 10:25:32 +00:00
|
|
|
|
2023-01-20 14:50:51 +00:00
|
|
|
if (_outputStream && isInput && _outputStream->duplexModeForced &&
|
|
|
|
config.match(*_outputStream)) {
|
2024-02-06 13:59:51 +00:00
|
|
|
throw rte(
|
|
|
|
"This device is already opened for output. If input is also "
|
|
|
|
"required, please enable duplex mode for this device");
|
2023-01-20 14:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_inputStream && isOutput && _inputStream->duplexModeForced &&
|
|
|
|
config.match(*_inputStream)) {
|
2024-02-06 13:59:51 +00:00
|
|
|
throw rte(
|
|
|
|
"This device is already opened for input. If output is also "
|
|
|
|
"required, please enable duplex mode for this device");
|
2023-01-20 14:50:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 12:58:48 +00:00
|
|
|
InDaqCallback inCallback;
|
|
|
|
OutDaqCallback outCallback;
|
|
|
|
|
|
|
|
using namespace std::placeholders;
|
2023-01-20 14:50:51 +00:00
|
|
|
std::unique_ptr<Daq> daq = Daq::createDaq(*devinfo, config);
|
2022-07-20 12:58:48 +00:00
|
|
|
|
2022-10-20 15:12:34 +00:00
|
|
|
assert(daq);
|
|
|
|
|
2022-07-29 07:32:26 +00:00
|
|
|
if (isInput) {
|
2022-10-19 09:57:51 +00:00
|
|
|
/// Give incallback as parameter to stream
|
2022-07-29 07:32:26 +00:00
|
|
|
inCallback = std::bind(&StreamMgr::inCallback, this, _1);
|
2022-10-19 09:57:51 +00:00
|
|
|
|
|
|
|
/// Reset handlers in case of an input stream
|
2022-10-17 17:51:10 +00:00
|
|
|
for (auto &handler : _inDataHandlers) {
|
|
|
|
handler->reset(daq.get());
|
|
|
|
}
|
2022-10-20 15:12:34 +00:00
|
|
|
|
|
|
|
d fs = daq->samplerate();
|
|
|
|
/// Create input filters
|
|
|
|
_inputFilters.clear();
|
2024-02-06 13:59:51 +00:00
|
|
|
|
|
|
|
/// No input filter for monitor channel, which comes as the first input
|
|
|
|
/// channel In the list
|
2023-01-20 14:50:51 +00:00
|
|
|
if (config.monitorOutput && devinfo->hasInternalOutputMonitor) {
|
2022-10-20 15:12:34 +00:00
|
|
|
_inputFilters.push_back(nullptr);
|
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
|
2022-10-20 15:12:34 +00:00
|
|
|
for (auto &ch : daq->inchannel_config) {
|
2022-10-24 11:31:57 +00:00
|
|
|
if (ch.enabled) {
|
|
|
|
if (ch.digitalHighPassCutOn < 0) {
|
|
|
|
_inputFilters.push_back(nullptr);
|
|
|
|
} else if (ch.digitalHighPassCutOn == 0) {
|
|
|
|
throw rte("Digital highpass cuton should be > 0 if activated");
|
|
|
|
} else {
|
|
|
|
// Put in a digital high-pass filter.
|
|
|
|
_inputFilters.emplace_back(std::make_unique<SeriesBiquad>(
|
|
|
|
SeriesBiquad::firstOrderHighPass(fs, ch.digitalHighPassCutOn)));
|
|
|
|
}
|
2022-10-20 15:12:34 +00:00
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
} // End of input filter creation
|
2022-07-20 12:58:48 +00:00
|
|
|
}
|
2022-10-19 09:57:51 +00:00
|
|
|
|
2022-07-29 07:32:26 +00:00
|
|
|
if (isOutput) {
|
2022-10-19 09:57:51 +00:00
|
|
|
/// Give outcallback as parameter to stream
|
|
|
|
outCallback = std::bind(&StreamMgr::outCallback, this, _1);
|
|
|
|
|
|
|
|
/// Reset signal generator in case of an output stream
|
2022-07-29 07:32:26 +00:00
|
|
|
if (_siggen) {
|
2022-09-22 19:02:41 +00:00
|
|
|
DEBUGTRACE_PRINT("Resetting _siggen with new samplerate of ");
|
|
|
|
DEBUGTRACE_PRINT(daq->samplerate());
|
2022-07-20 12:58:48 +00:00
|
|
|
_siggen->reset(daq->samplerate());
|
2022-06-29 10:25:32 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-20 12:58:48 +00:00
|
|
|
|
2022-10-20 15:12:34 +00:00
|
|
|
/// Start the DAQ. If it fails, everything is still nicely cleaned up and
|
|
|
|
/// the daq unique_ptr cleans up resources nicely.
|
2022-07-20 12:58:48 +00:00
|
|
|
daq->start(inCallback, outCallback);
|
|
|
|
|
2022-10-17 17:51:10 +00:00
|
|
|
// Move daq ptr to right place
|
2022-10-19 09:57:51 +00:00
|
|
|
if (isInput) {
|
|
|
|
_inputStream = std::move(daq);
|
|
|
|
} else {
|
|
|
|
_outputStream = std::move(daq);
|
|
|
|
}
|
2022-06-29 10:25:32 +00:00
|
|
|
}
|
2022-08-14 19:00:22 +00:00
|
|
|
void StreamMgr::stopStream(const StreamType t) {
|
2022-09-03 18:59:14 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
checkRightThread();
|
2024-03-14 07:25:47 +00:00
|
|
|
bool resetHandlers = false;
|
|
|
|
std::unique_ptr<Daq> *streamToStop = nullptr;
|
2022-10-19 09:57:51 +00:00
|
|
|
|
2024-03-14 07:25:47 +00:00
|
|
|
{ // Mutex locked in this scope
|
|
|
|
Lck lck(_mtx);
|
|
|
|
if (t == StreamType::input) {
|
|
|
|
if (!_inputStream) {
|
|
|
|
throw rte("Input stream is not running");
|
|
|
|
}
|
|
|
|
streamToStop = std::addressof(_inputStream);
|
|
|
|
resetHandlers = true;
|
|
|
|
} else {
|
|
|
|
/// t == output
|
|
|
|
/// Kill input stream in case that one is a duplex stream
|
|
|
|
if (_inputStream && _inputStream->duplexMode()) {
|
|
|
|
streamToStop = std::addressof(_inputStream);
|
|
|
|
} else {
|
|
|
|
if (!_outputStream) {
|
|
|
|
throw rte("Output stream is not running");
|
|
|
|
}
|
|
|
|
streamToStop = std::addressof(_outputStream);
|
|
|
|
} // end else
|
2022-08-14 19:00:22 +00:00
|
|
|
}
|
2024-03-14 07:25:47 +00:00
|
|
|
} // End of mutex lock. When stopping stream, mutex should be unlocked.
|
|
|
|
|
|
|
|
// If we arrive here, we should have a stream to stop.
|
|
|
|
assert(streamToStop != nullptr);
|
|
|
|
streamToStop->reset();
|
2024-03-13 11:19:24 +00:00
|
|
|
|
2024-03-14 07:25:47 +00:00
|
|
|
/// Send reset to all in data handlers
|
|
|
|
if (resetHandlers) {
|
2024-03-13 11:19:24 +00:00
|
|
|
Lck lck(_mtx);
|
2022-10-10 17:17:38 +00:00
|
|
|
for (auto &handler : _inDataHandlers) {
|
2022-10-04 07:27:27 +00:00
|
|
|
handler->reset(nullptr);
|
|
|
|
}
|
2022-08-14 19:00:22 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-29 10:25:32 +00:00
|
|
|
|
2023-06-07 19:49:07 +00:00
|
|
|
void StreamMgr::addInDataHandler(InDataHandler *handler) {
|
2022-10-06 19:13:21 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2024-03-13 11:19:24 +00:00
|
|
|
Lck lck(_mtx);
|
2022-09-03 18:59:14 +00:00
|
|
|
checkRightThread();
|
2023-06-07 19:49:07 +00:00
|
|
|
assert(handler);
|
|
|
|
handler->reset(_inputStream.get());
|
|
|
|
|
|
|
|
if (std::find(_inDataHandlers.cbegin(), _inDataHandlers.cend(), handler) !=
|
2022-10-17 17:51:10 +00:00
|
|
|
_inDataHandlers.cend()) {
|
2024-02-06 13:59:51 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"Error: handler already added. Probably start() "
|
|
|
|
"is called more than once on a handler object");
|
2022-10-16 19:26:06 +00:00
|
|
|
}
|
2023-06-07 19:49:07 +00:00
|
|
|
_inDataHandlers.push_back(handler);
|
2023-03-10 15:27:51 +00:00
|
|
|
DEBUGTRACE_PRINT(_inDataHandlers.size());
|
2022-06-29 10:25:32 +00:00
|
|
|
}
|
2023-03-10 15:27:51 +00:00
|
|
|
|
2022-06-29 10:25:32 +00:00
|
|
|
void StreamMgr::removeInDataHandler(InDataHandler &handler) {
|
2022-10-06 19:13:21 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2024-03-13 11:19:24 +00:00
|
|
|
Lck lck(_mtx);
|
|
|
|
// checkRightThread();
|
2022-06-29 10:25:32 +00:00
|
|
|
_inDataHandlers.remove(&handler);
|
2023-03-10 15:27:51 +00:00
|
|
|
|
|
|
|
DEBUGTRACE_PRINT(_inDataHandlers.size());
|
2022-06-29 10:25:32 +00:00
|
|
|
}
|
2022-07-20 12:58:48 +00:00
|
|
|
|
|
|
|
Daq::StreamStatus StreamMgr::getStreamStatus(const StreamType type) const {
|
2024-03-13 11:19:24 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2022-07-20 12:58:48 +00:00
|
|
|
|
2024-03-13 11:19:24 +00:00
|
|
|
Lck lck(_mtx);
|
2022-09-03 18:59:14 +00:00
|
|
|
checkRightThread();
|
2022-07-20 12:58:48 +00:00
|
|
|
// Default constructor, says stream is not running, but also no errors
|
|
|
|
|
|
|
|
const Daq *daq = getDaq(type);
|
|
|
|
if (daq) {
|
2022-10-10 17:17:38 +00:00
|
|
|
return daq->getStreamStatus();
|
|
|
|
} else {
|
|
|
|
return Daq::StreamStatus();
|
2022-07-20 12:58:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Daq *StreamMgr::getDaq(StreamType type) const {
|
2024-03-13 11:19:24 +00:00
|
|
|
Lck lck(_mtx);
|
2022-10-10 17:17:38 +00:00
|
|
|
checkRightThread();
|
|
|
|
|
2022-07-20 12:58:48 +00:00
|
|
|
if (type == StreamType::input) {
|
|
|
|
return _inputStream.get();
|
|
|
|
} else {
|
|
|
|
// Output stream. If input runs in duplex mode, this is also the output
|
2022-10-19 08:03:13 +00:00
|
|
|
// stream. In that case, we return the input stream
|
2022-07-20 12:58:48 +00:00
|
|
|
if (_inputStream && _inputStream->duplexMode()) {
|
|
|
|
return _inputStream.get();
|
2022-10-19 08:03:13 +00:00
|
|
|
} else {
|
|
|
|
return _outputStream.get();
|
2022-07-20 12:58:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|