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>
23 #include <libsigrokdecode/libsigrokdecode.h>
31 using namespace std::placeholders;
35 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
36 "sigrok_legacy_capture_device",
37 "Wrapper for capture devices using legacy libsigrok APIs",
38 sigc::ptr_fun(&LegacyCaptureDevice::register_element),
39 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
40 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
41 "sigrok_legacy_output",
42 "Wrapper for outputs using legacy libsigrok APIs",
43 sigc::ptr_fun(&LegacyOutput::register_element),
44 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
45 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
46 "sigrok_legacy_decoder",
47 "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
48 sigc::ptr_fun(&LegacyDecoder::register_element),
49 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
52 Sink::Sink(GstBaseSink *gobj) :
57 Device::Device(GstElement *gobj) :
62 CaptureDevice::CaptureDevice(GstElement *gobj) :
67 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
69 klass->set_metadata("sigrok legacy capture device",
70 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
73 klass->add_pad_template(Gst::PadTemplate::create(
77 Gst::Caps::create_any()));
80 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
82 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
83 0, Gst::register_mm_type<LegacyCaptureDevice>(
84 "sigrok_legacy_capture_device"));
88 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
89 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
92 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
95 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
96 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
98 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
100 throw runtime_error("Failed to create element - plugin not registered?");
101 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
102 device->_libsigrok_device = libsigrok_device;
106 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
108 return _libsigrok_device;
111 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
115 case Gst::STATE_CHANGE_READY_TO_PAUSED:
116 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
117 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
118 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
119 _task->set_lock(_mutex);
120 _src_pad->set_active(true);
122 return Gst::STATE_CHANGE_SUCCESS;
124 return Gst::STATE_CHANGE_SUCCESS;
128 void LegacyCaptureDevice::_datafeed_callback(
129 shared_ptr<sigrok::Device> device,
130 shared_ptr<sigrok::Packet> packet)
133 switch (packet->type()->id()) {
136 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
137 auto mem = Gst::Memory::create(
138 Gst::MEMORY_FLAG_READONLY,
139 logic->data_pointer(),
140 logic->data_length(),
142 logic->data_length());
143 auto buf = Gst::Buffer::create();
144 buf->append_memory(move(mem));
145 _src_pad->push(move(buf));
150 _src_pad->push_event(Gst::EventEos::create());
157 void LegacyCaptureDevice::_run()
159 _session = _libsigrok_device->driver()->parent()->create_session();
160 _session->add_device(_libsigrok_device);
161 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
167 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
169 klass->set_metadata("sigrok legacy output",
170 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
173 klass->add_pad_template(Gst::PadTemplate::create(
177 Gst::Caps::create_any()));
180 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
182 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
183 0, Gst::register_mm_type<LegacyOutput>(
184 "sigrok_legacy_output"));
188 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
189 Glib::ObjectBase(typeid(LegacyOutput)),
194 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
195 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
196 shared_ptr<sigrok::Device> libsigrok_device,
197 map<string, Glib::VariantBase> options)
199 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
201 throw runtime_error("Failed to create element - plugin not registered?");
202 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
203 output->_libsigrok_output_format = libsigrok_output_format;
204 output->_libsigrok_device = libsigrok_device;
205 output->_options = options;
209 bool LegacyOutput::start_vfunc()
211 _libsigrok_output = _libsigrok_output_format->create_output(
212 _libsigrok_device, _options);
216 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
219 buffer->map(info, Gst::MAP_READ);
220 auto context = _libsigrok_output_format->parent();
221 auto packet = context->create_logic_packet(
222 info.get_data(), info.get_size(), 2);
223 auto result = _libsigrok_output->receive(packet);
229 bool LegacyOutput::stop_vfunc()
231 auto context = _libsigrok_output_format->parent();
232 auto end_packet = context->create_end_packet();
233 auto result = _libsigrok_output->receive(end_packet);
238 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
240 klass->set_metadata("sigrok legacy decoder",
241 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
244 klass->add_pad_template(Gst::PadTemplate::create(
248 Gst::Caps::create_any()));
251 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
253 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
254 0, Gst::register_mm_type<LegacyDecoder>(
255 "sigrok_legacy_decoder"));
259 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
260 Glib::ObjectBase(typeid(LegacyDecoder)),
265 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
266 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
268 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
270 throw runtime_error("Failed to create element - plugin not registered?");
271 auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
272 decoder->_session = libsigrokdecode_session;
273 decoder->_unitsize = unitsize;
277 struct srd_session *LegacyDecoder::libsigrokdecode_session()
282 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
285 buffer->map(info, Gst::MAP_READ);
286 uint64_t num_samples = info.get_size() / _unitsize;
287 srd_session_send(_session, _abs_ss, _abs_ss + num_samples,
288 info.get_data(), info.get_size(), _unitsize);
289 _abs_ss += num_samples;
294 bool LegacyDecoder::start_vfunc()
297 srd_session_start(_session);
301 bool LegacyDecoder::stop_vfunc()
303 srd_session_terminate_reset(_session);