]> sigrok.org Git - libsigrokflow.git/commitdiff
Implement LegacyInput.
authorMartin Ling <redacted>
Sun, 30 Dec 2018 09:33:33 +0000 (10:33 +0100)
committerUwe Hermann <redacted>
Tue, 8 Jan 2019 18:41:58 +0000 (19:41 +0100)
include/libsigrokflow/libsigrokflow.hpp
src/main.cpp

index cbae5c7e1adc371d7d5e57749b882e5114d900c7..5e8f48346793209a1ad32b61bdaffb71d0d08d9a 100644 (file)
@@ -96,6 +96,45 @@ private:
         void _run();
 };
 
+class LegacyInput :
+        public Gst::Element
+{
+public:
+        /* Create from libsigrok input */
+        static Glib::RefPtr<LegacyInput> create(
+                shared_ptr<sigrok::InputFormat> format,
+                map<string, Glib::VariantBase> options = map<string, Glib::VariantBase>());
+
+        /* Override start */
+        bool start_vfunc();
+
+        /* Chain function */
+        Gst::FlowReturn chain(const Glib::RefPtr<Gst::Pad> &pad,
+                        const Glib::RefPtr<Gst::Buffer> &buf);
+
+        /* Override stop */
+        bool stop_vfunc();
+
+        /* Gst class init */
+        static void class_init(Gst::ElementClass<LegacyInput> *klass);
+
+        /* Register class with plugin */
+        static bool register_element(Glib::RefPtr<Gst::Plugin> plugin);
+
+        /* Construcor used by element factory */
+        explicit LegacyInput(GstElement *gobj);
+private:
+        shared_ptr<sigrok::InputFormat> _libsigrok_input_format;
+        shared_ptr<sigrok::Input> _libsigrok_input;
+        shared_ptr<sigrok::Session> _session;
+        map<string, Glib::VariantBase> _options;
+        Glib::RefPtr<Gst::Pad> _sink_pad;
+        Glib::RefPtr<Gst::Pad> _src_pad;
+
+        void _datafeed_callback(shared_ptr<sigrok::Device> device,
+                        shared_ptr<sigrok::Packet> packet);
+};
+
 class LegacyOutput :
         public Sink
 {
index d5beaa28a8fbeb3a60be76ca8a1bb02bcf3ca41d..db3de8fad347ba9cc16eb339c1ef8db19944579e 100644 (file)
@@ -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<LegacyInput> *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<Gst::Plugin> plugin)
+{
+       Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
+                       0, Gst::register_mm_type<LegacyInput>(
+                               "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> LegacyInput::create(
+       shared_ptr<sigrok::InputFormat> libsigrok_input_format,
+       map<string, Glib::VariantBase> 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<LegacyInput>::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<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;
+       }
+}
+
+Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
+                        const Glib::RefPtr<Gst::Buffer> &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<LegacyOutput> *klass)
 {
        klass->set_metadata("sigrok legacy output",