From: Uwe Hermann Date: Wed, 9 Jan 2019 01:25:29 +0000 (+0100) Subject: Factor out src/legacy_input.cpp. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=aa03d5d8df84b99aba95843ae6af5c155411abcf;p=libsigrokflow.git Factor out src/legacy_input.cpp. --- diff --git a/Makefile.am b/Makefile.am index de5381f..2f2c418 100644 --- a/Makefile.am +++ b/Makefile.am @@ -30,7 +30,8 @@ lib_LTLIBRARIES = libsigrokflow.la libsigrokflow_la_SOURCES = \ src/main.cpp \ src/init.cpp \ - src/legacy_capture_device.cpp + src/legacy_capture_device.cpp \ + src/legacy_input.cpp libsigrokflow_la_LIBADD = $(LIBSIGROKFLOW_LIBS) libsigrokflow_la_LDFLAGS = -version-info $(SRF_LIB_VERSION) -no-undefined diff --git a/src/legacy_input.cpp b/src/legacy_input.cpp new file mode 100644 index 0000000..c29f127 --- /dev/null +++ b/src/legacy_input.cpp @@ -0,0 +1,142 @@ +/* + * This file is part of the libsigrokflow project. + * + * Copyright (C) 2018 Martin Ling + * Copyright (C) 2018 Uwe Hermann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +namespace Srf +{ + +using namespace std; +using namespace std::placeholders; + +#ifdef HAVE_LIBSIGROKCXX +void LegacyInput::class_init(Gst::ElementClass *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 plugin) +{ + Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input", + 0, Gst::register_mm_type( + "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::create( + shared_ptr libsigrok_input_format, + map 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::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 device, + shared_ptr packet) +{ + (void)device; + + switch (packet->type()->id()) { + case SR_DF_LOGIC: { + auto logic = static_pointer_cast(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 &, + const Glib::RefPtr &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; +} +#endif + +} diff --git a/src/main.cpp b/src/main.cpp index fadd19c..15c968e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,116 +44,6 @@ CaptureDevice::CaptureDevice(GstElement *gobj) : } #ifdef HAVE_LIBSIGROKCXX -void LegacyInput::class_init(Gst::ElementClass *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 plugin) -{ - Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input", - 0, Gst::register_mm_type( - "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::create( - shared_ptr libsigrok_input_format, - map 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::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 device, - shared_ptr packet) -{ - (void)device; - - switch (packet->type()->id()) { - case SR_DF_LOGIC: { - auto logic = static_pointer_cast(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 &, - const Glib::RefPtr &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 *klass) { klass->set_metadata("sigrok legacy output",