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");
38 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
39 "sigrok_legacy_output",
40 "Wrapper for outputs using legacy libsigrok APIs",
41 sigc::ptr_fun(&LegacyOutput::register_element),
42 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
45 Sink::Sink(GstBaseSink *gobj) :
50 Device::Device(GstElement *gobj) :
55 CaptureDevice::CaptureDevice(GstElement *gobj) :
60 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
62 klass->set_metadata("sigrok legacy capture device",
63 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
66 klass->add_pad_template(Gst::PadTemplate::create(
70 Gst::Caps::create_any()));
73 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
75 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
76 0, Gst::register_mm_type<LegacyCaptureDevice>(
77 "sigrok_legacy_capture_device"));
81 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
82 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
85 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
88 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
89 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
91 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
93 throw runtime_error("Failed to create element - plugin not registered?");
94 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
95 device->_libsigrok_device = libsigrok_device;
99 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
101 return _libsigrok_device;
104 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
108 case Gst::STATE_CHANGE_READY_TO_PAUSED:
109 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
110 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
111 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
112 _task->set_lock(_mutex);
113 _src_pad->set_active(true);
115 return Gst::STATE_CHANGE_SUCCESS;
117 return Gst::STATE_CHANGE_SUCCESS;
121 void LegacyCaptureDevice::_datafeed_callback(
122 shared_ptr<sigrok::Device> device,
123 shared_ptr<sigrok::Packet> packet)
126 switch (packet->type()->id()) {
129 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
130 auto mem = Gst::Memory::create(
131 Gst::MEMORY_FLAG_READONLY,
132 logic->data_pointer(),
133 logic->data_length(),
135 logic->data_length());
136 auto buf = Gst::Buffer::create();
137 buf->append_memory(move(mem));
138 _src_pad->push(move(buf));
143 _src_pad->push_event(Gst::EventEos::create());
150 void LegacyCaptureDevice::_run()
152 _session = _libsigrok_device->driver()->parent()->create_session();
153 _session->add_device(_libsigrok_device);
154 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
160 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
162 klass->set_metadata("sigrok legacy output",
163 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
166 klass->add_pad_template(Gst::PadTemplate::create(
170 Gst::Caps::create_any()));
173 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
175 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
176 0, Gst::register_mm_type<LegacyOutput>(
177 "sigrok_legacy_output"));
181 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
182 Glib::ObjectBase(typeid(LegacyOutput)),
185 add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink"));
188 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
189 shared_ptr<sigrok::Output> libsigrok_output)
191 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
193 throw runtime_error("Failed to create element - plugin not registered?");
194 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
195 output->_libsigrok_output = libsigrok_output;
199 shared_ptr<sigrok::Output> LegacyOutput::libsigrok_output()
201 return _libsigrok_output;
204 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
207 buffer->map(info, Gst::MAP_READ);
208 auto context = _libsigrok_output->format()->parent();
209 auto packet = context->create_logic_packet(
210 info.get_data(), info.get_size(), 2);
211 auto result = _libsigrok_output->receive(packet);
217 bool LegacyOutput::stop_vfunc()
219 auto context = _libsigrok_output->format()->parent();
220 auto end_packet = context->create_end_packet();
221 auto result = _libsigrok_output->receive(end_packet);