From: Martin Ling Date: Sun, 30 Dec 2018 09:33:33 +0000 (+0100) Subject: Implement LegacyInput. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=92d521e768ce580d32f9ea7154d69a935222e48c;p=libsigrokflow.git Implement LegacyInput. --- diff --git a/include/libsigrokflow/libsigrokflow.hpp b/include/libsigrokflow/libsigrokflow.hpp index cbae5c7..5e8f483 100644 --- a/include/libsigrokflow/libsigrokflow.hpp +++ b/include/libsigrokflow/libsigrokflow.hpp @@ -96,6 +96,45 @@ private: void _run(); }; +class LegacyInput : + public Gst::Element +{ +public: + /* Create from libsigrok input */ + static Glib::RefPtr create( + shared_ptr format, + map options = map()); + + /* Override start */ + bool start_vfunc(); + + /* Chain function */ + Gst::FlowReturn chain(const Glib::RefPtr &pad, + const Glib::RefPtr &buf); + + /* Override stop */ + bool stop_vfunc(); + + /* Gst class init */ + static void class_init(Gst::ElementClass *klass); + + /* Register class with plugin */ + static bool register_element(Glib::RefPtr plugin); + + /* Construcor used by element factory */ + explicit LegacyInput(GstElement *gobj); +private: + shared_ptr _libsigrok_input_format; + shared_ptr _libsigrok_input; + shared_ptr _session; + map _options; + Glib::RefPtr _sink_pad; + Glib::RefPtr _src_pad; + + void _datafeed_callback(shared_ptr device, + shared_ptr packet); +}; + class LegacyOutput : public Sink { diff --git a/src/main.cpp b/src/main.cpp index d5beaa2..db3de8f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,6 +38,11 @@ void init() "Wrapper for capture devices using legacy libsigrok APIs", sigc::ptr_fun(&LegacyCaptureDevice::register_element), "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org"); + Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR, + "sigrok_legacy_input", + "Wrapper for inputs using legacy libsigrok APIs", + sigc::ptr_fun(&LegacyInput::register_element), + "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org"); Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR, "sigrok_legacy_output", "Wrapper for outputs using legacy libsigrok APIs", @@ -165,6 +170,111 @@ void LegacyCaptureDevice::_run() _task->stop(); } +void LegacyInput::class_init(Gst::ElementClass *klass) +{ + klass->set_metadata("sigrok legacy input", + "Transform", "Wrapper for inputs using legacy libsigrok APIs", + "Martin Ling"); + + klass->add_pad_template(Gst::PadTemplate::create( + "sink", + Gst::PAD_SINK, + Gst::PAD_ALWAYS, + Gst::Caps::create_any())); + + klass->add_pad_template(Gst::PadTemplate::create( + "src", + Gst::PAD_SRC, + Gst::PAD_ALWAYS, + Gst::Caps::create_any())); +} + +bool LegacyInput::register_element(Glib::RefPtr plugin) +{ + Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input", + 0, Gst::register_mm_type( + "sigrok_legacy_input")); + return true; +} + +LegacyInput::LegacyInput(GstElement *gobj) : + Glib::ObjectBase(typeid(LegacyInput)), + Gst::Element(gobj) +{ + add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink")); + add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src")); + _sink_pad->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain)); +} + +Glib::RefPtr LegacyInput::create( + shared_ptr libsigrok_input_format, + map options) +{ + auto element = Gst::ElementFactory::create_element("sigrok_legacy_input"); + if (!element) + throw runtime_error("Failed to create element - plugin not registered?"); + auto input = Glib::RefPtr::cast_static(element); + input->_libsigrok_input_format = libsigrok_input_format; + input->_options = options; + return input; +} + +bool LegacyInput::start_vfunc() +{ + _libsigrok_input = _libsigrok_input_format->create_input(_options); + auto context = _libsigrok_input_format->parent(); + _session = context->create_session(); + _session->add_device(_libsigrok_input->device()); + _session->add_datafeed_callback(bind(&LegacyInput::_datafeed_callback, this, _1, _2)); + _session->start(); + return true; +} + +void LegacyInput::_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; + } +} + +Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr &, + const Glib::RefPtr &buf) +{ + Gst::MapInfo info; + buf->map(info, Gst::MAP_READ); + _libsigrok_input->send(info.get_data(), info.get_size()); + buf->unmap(info); + return Gst::FLOW_OK; +} + +bool LegacyInput::stop_vfunc() +{ + _libsigrok_input->end(); + return true; +} + void LegacyOutput::class_init(Gst::ElementClass *klass) { klass->set_metadata("sigrok legacy output",