2 * This file is part of the libsigrokflow project.
4 * Copyright (C) 2018 Uwe Hermann <uwe@hermann-uwe.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <libsigrokflow/libsigrokflow.hpp>
29 using namespace std::placeholders;
33 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
34 "sigrok_legacy_capture_device",
35 "Wrapper for capture devices using legacy libsigrok APIs",
36 sigc::ptr_fun(&LegacyCaptureDevice::register_element),
37 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
40 GstBlock::GstBlock(GstElement *gobj) :
45 Device::Device(GstElement *gobj) :
50 CaptureDevice::CaptureDevice(GstElement *gobj) :
55 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
57 klass->set_metadata("sigrok legacy capture device",
58 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
61 klass->add_pad_template(Gst::PadTemplate::create(
65 Gst::Caps::create_any()));
68 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
70 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
71 0, Gst::register_mm_type<LegacyCaptureDevice>(
72 "sigrok_legacy_capture_device"));
76 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
77 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
80 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
83 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
84 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
86 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
88 throw runtime_error("Failed to create element - plugin not registered?");
89 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
90 device->_libsigrok_device = libsigrok_device;
94 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
96 return _libsigrok_device;
99 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
103 case Gst::STATE_CHANGE_READY_TO_PAUSED:
104 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
105 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
106 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
107 _task->set_lock(_mutex);
108 _src_pad->set_active(true);
110 return Gst::STATE_CHANGE_SUCCESS;
112 return Gst::STATE_CHANGE_SUCCESS;
116 void LegacyCaptureDevice::_datafeed_callback(
117 shared_ptr<sigrok::Device> device,
118 shared_ptr<sigrok::Packet> packet)
121 switch (packet->type()->id()) {
124 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
125 auto mem = Gst::Memory::create(
126 Gst::MEMORY_FLAG_READONLY,
127 logic->data_pointer(),
128 logic->data_length(),
130 logic->data_length());
131 auto buf = Gst::Buffer::create();
132 buf->append_memory(move(mem));
133 _src_pad->push(move(buf));
138 _src_pad->push_event(Gst::EventEos::create());
145 void LegacyCaptureDevice::_run()
147 _session = _libsigrok_device->driver()->parent()->create_session();
148 _session->add_device(_libsigrok_device);
149 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));