From: Martin Ling Date: Sun, 30 Dec 2018 03:02:35 +0000 (+0100) Subject: Implement LegacyOutput. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=e2cfc0efeadec350376bffbe9250a27c6aa9788d;p=libsigrokflow.git Implement LegacyOutput. --- diff --git a/include/libsigrokflow/libsigrokflow.hpp b/include/libsigrokflow/libsigrokflow.hpp index 72fdc9d..e9a725e 100644 --- a/include/libsigrokflow/libsigrokflow.hpp +++ b/include/libsigrokflow/libsigrokflow.hpp @@ -22,6 +22,7 @@ #include #include +#include #include namespace Srf @@ -36,16 +37,15 @@ class Block /* Config API etc goes here */ }; -class GstBlock : - public Gst::Element +class Sink : + public Gst::BaseSink { - /* Operations specific to sigrok GStreamer blocks go here. */ protected: - explicit GstBlock(GstElement *gobj); + explicit Sink(GstBaseSink *gobj); }; class Device : - public GstBlock + public Gst::Element { /* Operations specific to hardware devices go here */ protected: @@ -94,6 +94,36 @@ private: void _run(); }; +class LegacyOutput : + public Sink +{ +public: + /* Create from libsigrok output object */ + static Glib::RefPtr create( + shared_ptr libsigrok_output); + + /* Retrieve libsigrok output object */ + shared_ptr libsigrok_output(); + + /* Override render */ + Gst::FlowReturn render_vfunc(const Glib::RefPtr &buffer); + + /* 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); + + /* Constructor used by element factory */ + explicit LegacyOutput(GstBaseSink *gobj); +private: + shared_ptr _libsigrok_output; + Glib::RefPtr _sink_pad; +}; + } #endif diff --git a/src/main.cpp b/src/main.cpp index 1601d86..79af3ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,15 +35,20 @@ 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_output", + "Wrapper for outputs using legacy libsigrok APIs", + sigc::ptr_fun(&LegacyOutput::register_element), + "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org"); } -GstBlock::GstBlock(GstElement *gobj) : - Gst::Element(gobj) +Sink::Sink(GstBaseSink *gobj) : + Gst::BaseSink(gobj) { } Device::Device(GstElement *gobj) : - GstBlock(gobj) + Gst::Element(gobj) { } @@ -152,4 +157,70 @@ void LegacyCaptureDevice::_run() _task->stop(); } +void LegacyOutput::class_init(Gst::ElementClass *klass) +{ + klass->set_metadata("sigrok legacy output", + "Sink", "Wrapper for outputs using legacy libsigrok APIs", + "Martin Ling"); + + klass->add_pad_template(Gst::PadTemplate::create( + "sink", + Gst::PAD_SINK, + Gst::PAD_ALWAYS, + Gst::Caps::create_any())); +} + +bool LegacyOutput::register_element(Glib::RefPtr plugin) +{ + Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output", + 0, Gst::register_mm_type( + "sigrok_legacy_output")); + return true; +} + +LegacyOutput::LegacyOutput(GstBaseSink *gobj) : + Glib::ObjectBase(typeid(LegacyOutput)), + Sink(gobj) +{ + add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink")); +} + +Glib::RefPtrLegacyOutput::create( + shared_ptr libsigrok_output) +{ + auto element = Gst::ElementFactory::create_element("sigrok_legacy_output"); + if (!element) + throw runtime_error("Failed to create element - plugin not registered?"); + auto output = Glib::RefPtr::cast_static(element); + output->_libsigrok_output = libsigrok_output; + return output; +} + +shared_ptr LegacyOutput::libsigrok_output() +{ + return _libsigrok_output; +} + +Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr &buffer) +{ + Gst::MapInfo info; + buffer->map(info, Gst::MAP_READ); + auto context = _libsigrok_output->format()->parent(); + auto packet = context->create_logic_packet( + info.get_data(), info.get_size(), 2); + auto result = _libsigrok_output->receive(packet); + cout << result; + buffer->unmap(info); + return Gst::FLOW_OK; +} + +bool LegacyOutput::stop_vfunc() +{ + auto context = _libsigrok_output->format()->parent(); + auto end_packet = context->create_end_packet(); + auto result = _libsigrok_output->receive(end_packet); + cout << result; + return true; +} + }