]> sigrok.org Git - libsigrokflow.git/commitdiff
Implement LegacyCaptureDevice.
authorMartin Ling <redacted>
Sat, 29 Dec 2018 22:29:26 +0000 (23:29 +0100)
committerUwe Hermann <redacted>
Tue, 8 Jan 2019 15:04:19 +0000 (16:04 +0100)
include/libsigrokflow/libsigrokflow.hpp
src/main.cpp

index a5fc25314c57c3d518852a3fa6d40f55793ff254..e800be0d45cc38248fc55fd8a0ae92d3195b096a 100644 (file)
@@ -60,12 +60,23 @@ class LegacyCaptureDevice :
 {
 public:
         /* Construct from libsigrok device object */
-        LegacyCaptureDevice(shared_ptr<sigrok::Device>);
+        LegacyCaptureDevice(shared_ptr<sigrok::HardwareDevice>);
 
         /* Retrieve libsigrok device object */
-        shared_ptr<sigrok::Device> libsigrok_device();
+        shared_ptr<sigrok::HardwareDevice> libsigrok_device();
+
+        /* Override state change */
+        Gst::StateChangeReturn change_state_vfunc(Gst::StateChange transition);
 private:
-        shared_ptr<sigrok::Device> _device;
+        shared_ptr<sigrok::HardwareDevice> _device;
+        Glib::RefPtr<Gst::Pad> _src_pad;
+        Glib::Threads::RecMutex _mutex;
+        Glib::RefPtr<Gst::Task> _task;
+        shared_ptr<sigrok::Session> _session;
+
+        void _datafeed_callback(shared_ptr<sigrok::Device> device,
+                        shared_ptr<sigrok::Packet> packet);
+        void _run();
 };
 
 
index 7f0c8ab4ba1b3a0fbb2f825f742e5a56529f599f..d6bfe50e71196f7a482184f8d3e80f19a9d4f6e6 100644 (file)
@@ -26,19 +26,85 @@ namespace Srf
 {
 
 using namespace std;
+using namespace std::placeholders;
 
 void init()
 {
 }
 
-LegacyCaptureDevice::LegacyCaptureDevice(shared_ptr<sigrok::Device> device) :
-       _device(device)
+auto src_template = Gst::PadTemplate::create("src",
+               Gst::PAD_SRC,
+               Gst::PAD_ALWAYS,
+               Gst::Caps::create_any());
+
+LegacyCaptureDevice::LegacyCaptureDevice(shared_ptr<sigrok::HardwareDevice> device) :
+       _device(device), _src_pad(Gst::Pad::create(src_template))
 {
+       add_pad(_src_pad);
 }
 
-shared_ptr<sigrok::Device> LegacyCaptureDevice::libsigrok_device()
+shared_ptr<sigrok::HardwareDevice> 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<int>::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<sigrok::Device> device,
+       shared_ptr<sigrok::Packet> packet)
+{
+       (void) device;
+       switch (packet->type()->id()) {
+               case SR_DF_LOGIC:
+               {
+                       auto logic = static_pointer_cast<sigrok::Logic>(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();
+}
+
 }