]> sigrok.org Git - libsigrokflow.git/blobdiff - src/main.cpp
Add missing copyright lines.
[libsigrokflow.git] / src / main.cpp
index 2d6e037ad0039bc9dee4e85a8d4473139d2b41dd..d5beaa28a8fbeb3a60be76ca8a1bb02bcf3ca41d 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * This file is part of the libsigrokflow project.
  *
+ * Copyright (C) 2018 Martin Ling <martin-sigrok@earth.li>
  * Copyright (C) 2018 Uwe Hermann <uwe@hermann-uwe.de>
  *
  * This program is free software: you can redistribute it and/or modify
@@ -20,6 +21,8 @@
 #include <config.h>
 #include <libsigrokflow/libsigrokflow.hpp>
 
+#include <libsigrokdecode/libsigrokdecode.h>
+
 #include <iostream>
 
 namespace Srf
@@ -35,15 +38,25 @@ 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");
+       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");
 }
 
-GstBlock::GstBlock(GstElement *gobj) :
-       Gst::Element(gobj)
+Sink::Sink(GstBaseSink *gobj) :
+       Gst::BaseSink(gobj)
 {
 }
 
 Device::Device(GstElement *gobj) :
-       GstBlock(gobj)
+       Gst::Element(gobj)
 {
 }
 
@@ -103,9 +116,6 @@ Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange
                case Gst::STATE_CHANGE_READY_TO_PAUSED:
                        return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
                case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
-                       _libsigrok_device->open();
-                       _libsigrok_device->config_set(sigrok::ConfigKey::LIMIT_SAMPLES,
-                                       Glib::Variant<uint64_t>::create(10));
                        _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
                        _task->set_lock(_mutex);
                        _src_pad->set_active(true);
@@ -155,4 +165,144 @@ void LegacyCaptureDevice::_run()
        _task->stop();
 }
 
+void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *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<Gst::Plugin> plugin)
+{
+       Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
+                       0, Gst::register_mm_type<LegacyOutput>(
+                               "sigrok_legacy_output"));
+       return true;
+}
+
+LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
+       Glib::ObjectBase(typeid(LegacyOutput)),
+       Sink(gobj)
+{
+}
+
+Glib::RefPtr<LegacyOutput>LegacyOutput::create(
+       shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
+       shared_ptr<sigrok::Device> libsigrok_device,
+       map<string, Glib::VariantBase> options)
+{
+       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<LegacyOutput>::cast_static(element);
+       output->_libsigrok_output_format = libsigrok_output_format;
+       output->_libsigrok_device = libsigrok_device;
+       output->_options = options;
+       return output;
+}
+
+bool LegacyOutput::start_vfunc()
+{
+       _libsigrok_output = _libsigrok_output_format->create_output(
+                       _libsigrok_device, _options);
+       return true;
+}
+
+Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &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;
+}
+
+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;
+}
+
 }