From: Uwe Hermann Date: Sun, 30 Dec 2018 03:42:53 +0000 (+0100) Subject: Initial LegacyDecoder implementation. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=b1322000ef157ecc1a2716a3b89e967a1e0ce97a;p=libsigrokflow.git Initial LegacyDecoder implementation. --- diff --git a/include/libsigrokflow/libsigrokflow.hpp b/include/libsigrokflow/libsigrokflow.hpp index 59de83c..abde375 100644 --- a/include/libsigrokflow/libsigrokflow.hpp +++ b/include/libsigrokflow/libsigrokflow.hpp @@ -24,6 +24,7 @@ #include #include #include +#include namespace Srf { @@ -128,6 +129,38 @@ private: map _options; }; +class LegacyDecoder : + public Sink +{ +public: + static Glib::RefPtr create( + struct srd_session *libsigrokdecode_session, uint64_t unitsize); + + /* Retrieve libsigrokdecode session */ + struct srd_session *libsigrokdecode_session(); + + /* Override start */ + bool start_vfunc(); + + /* 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 LegacyDecoder(GstBaseSink *gobj); +private: + struct srd_session *_session; + uint64_t _abs_ss; + uint64_t _unitsize; +}; } #endif diff --git a/src/main.cpp b/src/main.cpp index cd9c134..b80d203 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,8 @@ #include #include +#include + #include namespace Srf @@ -40,6 +42,11 @@ void init() "Wrapper for outputs using legacy libsigrok APIs", sigc::ptr_fun(&LegacyOutput::register_element), "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org"); + Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR, + "sigrok_legacy_decoder", + "Wrapper for protocol decoders using legacy libsigrokdecode APIs", + sigc::ptr_fun(&LegacyDecoder::register_element), + "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org"); } Sink::Sink(GstBaseSink *gobj) : @@ -228,4 +235,73 @@ bool LegacyOutput::stop_vfunc() return true; } +void LegacyDecoder::class_init(Gst::ElementClass *klass) +{ + klass->set_metadata("sigrok legacy decoder", + "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs", + "Uwe Hermann"); + + klass->add_pad_template(Gst::PadTemplate::create( + "sink", + Gst::PAD_SINK, + Gst::PAD_ALWAYS, + Gst::Caps::create_any())); +} + +bool LegacyDecoder::register_element(Glib::RefPtr plugin) +{ + Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder", + 0, Gst::register_mm_type( + "sigrok_legacy_decoder")); + return true; +} + +LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) : + Glib::ObjectBase(typeid(LegacyDecoder)), + Sink(gobj) +{ +} + +Glib::RefPtrLegacyDecoder::create( + struct srd_session *libsigrokdecode_session, uint64_t unitsize) +{ + auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder"); + if (!element) + throw runtime_error("Failed to create element - plugin not registered?"); + auto decoder = Glib::RefPtr::cast_static(element); + decoder->_session = libsigrokdecode_session; + decoder->_unitsize = unitsize; + return decoder; +} + +struct srd_session *LegacyDecoder::libsigrokdecode_session() +{ + return _session; +} + +Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr &buffer) +{ + Gst::MapInfo info; + buffer->map(info, Gst::MAP_READ); + uint64_t num_samples = info.get_size() / _unitsize; + srd_session_send(_session, _abs_ss, _abs_ss + num_samples, + info.get_data(), info.get_size(), _unitsize); + _abs_ss += num_samples; + buffer->unmap(info); + return Gst::FLOW_OK; +} + +bool LegacyDecoder::start_vfunc() +{ + _abs_ss = 0; + srd_session_start(_session); + return true; +} + +bool LegacyDecoder::stop_vfunc() +{ + srd_session_terminate_reset(_session); + return true; +} + }