From: Uwe Hermann Date: Wed, 9 Jan 2019 01:20:38 +0000 (+0100) Subject: Factor out src/legacy_capture_device.cpp. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=58e83724147843f817994488ca14e905b65f38e9;p=libsigrokflow.git Factor out src/legacy_capture_device.cpp. --- diff --git a/Makefile.am b/Makefile.am index a0f34fe..de5381f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -29,7 +29,8 @@ lib_LTLIBRARIES = libsigrokflow.la libsigrokflow_la_SOURCES = \ src/main.cpp \ - src/init.cpp + src/init.cpp \ + src/legacy_capture_device.cpp libsigrokflow_la_LIBADD = $(LIBSIGROKFLOW_LIBS) libsigrokflow_la_LDFLAGS = -version-info $(SRF_LIB_VERSION) -no-undefined diff --git a/src/legacy_capture_device.cpp b/src/legacy_capture_device.cpp new file mode 100644 index 0000000..1a2cfa8 --- /dev/null +++ b/src/legacy_capture_device.cpp @@ -0,0 +1,133 @@ +/* + * 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 LegacyCaptureDevice::class_init(Gst::ElementClass *klass) +{ + klass->set_metadata("sigrok legacy capture device", + "Source", "Wrapper for capture devices using legacy libsigrok APIs", + "Martin Ling"); + + klass->add_pad_template(Gst::PadTemplate::create( + "src", + Gst::PAD_SRC, + Gst::PAD_ALWAYS, + Gst::Caps::create_any())); +} + +bool LegacyCaptureDevice::register_element(Glib::RefPtr plugin) +{ + Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device", + 0, Gst::register_mm_type( + "sigrok_legacy_capture_device")); + + return true; +} + +LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) : + Glib::ObjectBase(typeid(LegacyCaptureDevice)), + CaptureDevice(gobj) +{ + add_pad(src_pad_ = Gst::Pad::create(get_pad_template("src"), "src")); +} + +Glib::RefPtrLegacyCaptureDevice::create( + shared_ptr libsigrok_device) +{ + auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device"); + if (!element) + throw runtime_error("Failed to create element - plugin not registered?"); + auto device = Glib::RefPtr::cast_static(element); + device->libsigrok_device_ = libsigrok_device; + + return device; +} + +shared_ptr LegacyCaptureDevice::libsigrok_device() +{ + return libsigrok_device_; +} + +Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition) +{ + switch (transition) { + case Gst::STATE_CHANGE_READY_TO_PAUSED: + return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL; + case Gst::STATE_CHANGE_PAUSED_TO_PLAYING: + task_ = Gst::Task::create(std::bind(&LegacyCaptureDevice::run_, this)); + task_->set_lock(mutex_); + src_pad_->set_active(true); + task_->start(); + return Gst::STATE_CHANGE_SUCCESS; + default: + return Gst::STATE_CHANGE_SUCCESS; + } +} + +void LegacyCaptureDevice::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; + } +} + +void LegacyCaptureDevice::run_() +{ + session_ = libsigrok_device_->driver()->parent()->create_session(); + session_->add_device(libsigrok_device_); + session_->add_datafeed_callback(bind(&LegacyCaptureDevice::datafeed_callback_, this, _1, _2)); + session_->start(); + session_->run(); + task_->stop(); +} +#endif + +} diff --git a/src/main.cpp b/src/main.cpp index 1b1193f..fadd19c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,107 +44,6 @@ CaptureDevice::CaptureDevice(GstElement *gobj) : } #ifdef HAVE_LIBSIGROKCXX -void LegacyCaptureDevice::class_init(Gst::ElementClass *klass) -{ - klass->set_metadata("sigrok legacy capture device", - "Source", "Wrapper for capture devices using legacy libsigrok APIs", - "Martin Ling"); - - klass->add_pad_template(Gst::PadTemplate::create( - "src", - Gst::PAD_SRC, - Gst::PAD_ALWAYS, - Gst::Caps::create_any())); -} - -bool LegacyCaptureDevice::register_element(Glib::RefPtr plugin) -{ - Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device", - 0, Gst::register_mm_type( - "sigrok_legacy_capture_device")); - - return true; -} - -LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) : - Glib::ObjectBase(typeid(LegacyCaptureDevice)), - CaptureDevice(gobj) -{ - add_pad(src_pad_ = Gst::Pad::create(get_pad_template("src"), "src")); -} - -Glib::RefPtrLegacyCaptureDevice::create( - shared_ptr libsigrok_device) -{ - auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device"); - if (!element) - throw runtime_error("Failed to create element - plugin not registered?"); - auto device = Glib::RefPtr::cast_static(element); - device->libsigrok_device_ = libsigrok_device; - - return device; -} - -shared_ptr LegacyCaptureDevice::libsigrok_device() -{ - return libsigrok_device_; -} - -Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition) -{ - switch (transition) { - case Gst::STATE_CHANGE_READY_TO_PAUSED: - return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL; - case Gst::STATE_CHANGE_PAUSED_TO_PLAYING: - task_ = Gst::Task::create(std::bind(&LegacyCaptureDevice::run_, this)); - task_->set_lock(mutex_); - src_pad_->set_active(true); - task_->start(); - return Gst::STATE_CHANGE_SUCCESS; - default: - return Gst::STATE_CHANGE_SUCCESS; - } -} - -void LegacyCaptureDevice::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; - } -} - -void LegacyCaptureDevice::run_() -{ - session_ = libsigrok_device_->driver()->parent()->create_session(); - session_->add_device(libsigrok_device_); - session_->add_datafeed_callback(bind(&LegacyCaptureDevice::datafeed_callback_, this, _1, _2)); - session_->start(); - session_->run(); - task_->stop(); -} - void LegacyInput::class_init(Gst::ElementClass *klass) { klass->set_metadata("sigrok legacy input",