]> sigrok.org Git - libsigrokflow.git/commitdiff
Initial LegacyDecoder implementation.
authorUwe Hermann <redacted>
Sun, 30 Dec 2018 03:42:53 +0000 (04:42 +0100)
committerUwe Hermann <redacted>
Tue, 8 Jan 2019 18:41:58 +0000 (19:41 +0100)
include/libsigrokflow/libsigrokflow.hpp
src/main.cpp

index 59de83c6307d6c3802fe58c20c48e12988fb6b43..abde3756255a0519277ac7f071a3947337e5bce5 100644 (file)
@@ -24,6 +24,7 @@
 #include <gstreamermm/private/element_p.h>
 #include <gstreamermm/private/basesink_p.h>
 #include <libsigrokcxx/libsigrokcxx.hpp>
+#include <libsigrokdecode/libsigrokdecode.h>
 
 namespace Srf
 {
@@ -128,6 +129,38 @@ private:
         map<string, Glib::VariantBase> _options;
 };
 
+class LegacyDecoder :
+        public Sink
+{
+public:
+        static Glib::RefPtr<LegacyDecoder> 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<Gst::Buffer> &buffer);
+
+        /* Override stop */
+        bool stop_vfunc();
+
+        /* Gst class init */
+        static void class_init(Gst::ElementClass<LegacyDecoder> *klass);
+
+        /* Register class with plugin */
+        static bool register_element(Glib::RefPtr<Gst::Plugin> plugin);
+
+        /* Constructor used by element factory */
+        explicit LegacyDecoder(GstBaseSink *gobj);
+private:
+        struct srd_session *_session;
+        uint64_t _abs_ss;
+        uint64_t _unitsize;
+};
 
 }
 #endif
index cd9c1347f761476e06961682f484c9f691fc2a9c..b80d203c804e77853dbe0da7cb19a0cfda5d880a 100644 (file)
@@ -20,6 +20,8 @@
 #include <config.h>
 #include <libsigrokflow/libsigrokflow.hpp>
 
+#include <libsigrokdecode/libsigrokdecode.h>
+
 #include <iostream>
 
 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<LegacyDecoder> *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<Gst::Plugin> plugin)
+{
+       Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
+                       0, Gst::register_mm_type<LegacyDecoder>(
+                               "sigrok_legacy_decoder"));
+       return true;
+}
+
+LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
+       Glib::ObjectBase(typeid(LegacyDecoder)),
+       Sink(gobj)
+{
+}
+
+Glib::RefPtr<LegacyDecoder>LegacyDecoder::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<LegacyDecoder>::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<Gst::Buffer> &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;
+}
+
 }