From: Martin Ling Date: Sat, 29 Dec 2018 22:29:26 +0000 (+0100) Subject: Implement LegacyCaptureDevice. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d03b3a9898b6da83867b27ab76261fbecdcb54e3;p=libsigrokflow.git Implement LegacyCaptureDevice. --- diff --git a/include/libsigrokflow/libsigrokflow.hpp b/include/libsigrokflow/libsigrokflow.hpp index a5fc253..e800be0 100644 --- a/include/libsigrokflow/libsigrokflow.hpp +++ b/include/libsigrokflow/libsigrokflow.hpp @@ -60,12 +60,23 @@ class LegacyCaptureDevice : { public: /* Construct from libsigrok device object */ - LegacyCaptureDevice(shared_ptr); + LegacyCaptureDevice(shared_ptr); /* Retrieve libsigrok device object */ - shared_ptr libsigrok_device(); + shared_ptr libsigrok_device(); + + /* Override state change */ + Gst::StateChangeReturn change_state_vfunc(Gst::StateChange transition); private: - shared_ptr _device; + shared_ptr _device; + Glib::RefPtr _src_pad; + Glib::Threads::RecMutex _mutex; + Glib::RefPtr _task; + shared_ptr _session; + + void _datafeed_callback(shared_ptr device, + shared_ptr packet); + void _run(); }; diff --git a/src/main.cpp b/src/main.cpp index 7f0c8ab..d6bfe50 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,19 +26,85 @@ namespace Srf { using namespace std; +using namespace std::placeholders; void init() { } -LegacyCaptureDevice::LegacyCaptureDevice(shared_ptr device) : - _device(device) +auto src_template = Gst::PadTemplate::create("src", + Gst::PAD_SRC, + Gst::PAD_ALWAYS, + Gst::Caps::create_any()); + +LegacyCaptureDevice::LegacyCaptureDevice(shared_ptr device) : + _device(device), _src_pad(Gst::Pad::create(src_template)) { + add_pad(_src_pad); } -shared_ptr LegacyCaptureDevice::libsigrok_device() +shared_ptr LegacyCaptureDevice::libsigrok_device() { return _device; } +Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition) +{ + switch (transition) + { + case Gst::STATE_CHANGE_READY_TO_PAUSED: + return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL; + case Gst::STATE_CHANGE_PAUSED_TO_PLAYING: + _device->open(); + _device->config_set(sigrok::ConfigKey::LIMIT_SAMPLES, + Glib::Variant::create(10)); + _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this)); + _task->set_lock(_mutex); + _src_pad->set_active(true); + _task->start(); + return Gst::STATE_CHANGE_SUCCESS; + default: + return Gst::STATE_CHANGE_SUCCESS; + } +} + +void LegacyCaptureDevice::_datafeed_callback( + shared_ptr device, + shared_ptr packet) +{ + (void) device; + switch (packet->type()->id()) { + case SR_DF_LOGIC: + { + auto logic = static_pointer_cast(packet->payload()); + auto mem = Gst::Memory::create( + Gst::MEMORY_FLAG_READONLY, + logic->data_pointer(), + logic->data_length(), + 0, + logic->data_length()); + auto buf = Gst::Buffer::create(); + buf->append_memory(move(mem)); + _src_pad->push(move(buf)); + break; + } + case SR_DF_END: + _session->stop(); + _src_pad->push_event(Gst::EventEos::create()); + break; + default: + break; + } +} + +void LegacyCaptureDevice::_run() +{ + _session = _device->driver()->parent()->create_session(); + _session->add_device(_device); + _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2)); + _session->start(); + _session->run(); + _task->stop(); +} + }