2 * This file is part of the libsigrokflow project.
4 * Copyright (C) 2018 Martin Ling <martin-sigrok@earth.li>
5 * Copyright (C) 2018 Uwe Hermann <uwe@hermann-uwe.de>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <libsigrokflow/libsigrokflow.hpp>
24 #include <libsigrokdecode/libsigrokdecode.h>
32 using namespace std::placeholders;
36 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
37 "sigrok_legacy_capture_device",
38 "Wrapper for capture devices using legacy libsigrok APIs",
39 sigc::ptr_fun(&LegacyCaptureDevice::register_element),
40 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
41 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
42 "sigrok_legacy_input",
43 "Wrapper for inputs using legacy libsigrok APIs",
44 sigc::ptr_fun(&LegacyInput::register_element),
45 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
46 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
47 "sigrok_legacy_output",
48 "Wrapper for outputs using legacy libsigrok APIs",
49 sigc::ptr_fun(&LegacyOutput::register_element),
50 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
51 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
52 "sigrok_legacy_decoder",
53 "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
54 sigc::ptr_fun(&LegacyDecoder::register_element),
55 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
58 Sink::Sink(GstBaseSink *gobj) :
63 Device::Device(GstElement *gobj) :
68 CaptureDevice::CaptureDevice(GstElement *gobj) :
73 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
75 klass->set_metadata("sigrok legacy capture device",
76 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
79 klass->add_pad_template(Gst::PadTemplate::create(
83 Gst::Caps::create_any()));
86 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
88 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
89 0, Gst::register_mm_type<LegacyCaptureDevice>(
90 "sigrok_legacy_capture_device"));
94 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
95 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
98 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
101 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
102 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
104 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
106 throw runtime_error("Failed to create element - plugin not registered?");
107 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
108 device->_libsigrok_device = libsigrok_device;
112 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
114 return _libsigrok_device;
117 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
121 case Gst::STATE_CHANGE_READY_TO_PAUSED:
122 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
123 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
124 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
125 _task->set_lock(_mutex);
126 _src_pad->set_active(true);
128 return Gst::STATE_CHANGE_SUCCESS;
130 return Gst::STATE_CHANGE_SUCCESS;
134 void LegacyCaptureDevice::_datafeed_callback(
135 shared_ptr<sigrok::Device> device,
136 shared_ptr<sigrok::Packet> packet)
139 switch (packet->type()->id()) {
142 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
143 auto mem = Gst::Memory::create(
144 Gst::MEMORY_FLAG_READONLY,
145 logic->data_pointer(),
146 logic->data_length(),
148 logic->data_length());
149 auto buf = Gst::Buffer::create();
150 buf->append_memory(move(mem));
151 _src_pad->push(move(buf));
156 _src_pad->push_event(Gst::EventEos::create());
163 void LegacyCaptureDevice::_run()
165 _session = _libsigrok_device->driver()->parent()->create_session();
166 _session->add_device(_libsigrok_device);
167 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
173 void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
175 klass->set_metadata("sigrok legacy input",
176 "Transform", "Wrapper for inputs using legacy libsigrok APIs",
179 klass->add_pad_template(Gst::PadTemplate::create(
183 Gst::Caps::create_any()));
185 klass->add_pad_template(Gst::PadTemplate::create(
189 Gst::Caps::create_any()));
192 bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
194 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
195 0, Gst::register_mm_type<LegacyInput>(
196 "sigrok_legacy_input"));
200 LegacyInput::LegacyInput(GstElement *gobj) :
201 Glib::ObjectBase(typeid(LegacyInput)),
204 add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink"));
205 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
206 _sink_pad->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
209 Glib::RefPtr<LegacyInput> LegacyInput::create(
210 shared_ptr<sigrok::InputFormat> libsigrok_input_format,
211 map<string, Glib::VariantBase> options)
213 auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
215 throw runtime_error("Failed to create element - plugin not registered?");
216 auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
217 input->_libsigrok_input_format = libsigrok_input_format;
218 input->_options = options;
222 bool LegacyInput::start_vfunc()
224 _libsigrok_input = _libsigrok_input_format->create_input(_options);
225 auto context = _libsigrok_input_format->parent();
226 _session = context->create_session();
227 _session->add_device(_libsigrok_input->device());
228 _session->add_datafeed_callback(bind(&LegacyInput::_datafeed_callback, this, _1, _2));
233 void LegacyInput::_datafeed_callback(
234 shared_ptr<sigrok::Device> device,
235 shared_ptr<sigrok::Packet> packet)
238 switch (packet->type()->id()) {
241 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
242 auto mem = Gst::Memory::create(
243 Gst::MEMORY_FLAG_READONLY,
244 logic->data_pointer(),
245 logic->data_length(),
247 logic->data_length());
248 auto buf = Gst::Buffer::create();
249 buf->append_memory(move(mem));
250 _src_pad->push(move(buf));
255 _src_pad->push_event(Gst::EventEos::create());
262 Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
263 const Glib::RefPtr<Gst::Buffer> &buf)
266 buf->map(info, Gst::MAP_READ);
267 _libsigrok_input->send(info.get_data(), info.get_size());
272 bool LegacyInput::stop_vfunc()
274 _libsigrok_input->end();
278 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
280 klass->set_metadata("sigrok legacy output",
281 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
284 klass->add_pad_template(Gst::PadTemplate::create(
288 Gst::Caps::create_any()));
291 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
293 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
294 0, Gst::register_mm_type<LegacyOutput>(
295 "sigrok_legacy_output"));
299 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
300 Glib::ObjectBase(typeid(LegacyOutput)),
305 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
306 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
307 shared_ptr<sigrok::Device> libsigrok_device,
308 map<string, Glib::VariantBase> options)
310 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
312 throw runtime_error("Failed to create element - plugin not registered?");
313 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
314 output->_libsigrok_output_format = libsigrok_output_format;
315 output->_libsigrok_device = libsigrok_device;
316 output->_options = options;
320 bool LegacyOutput::start_vfunc()
322 _libsigrok_output = _libsigrok_output_format->create_output(
323 _libsigrok_device, _options);
327 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
330 buffer->map(info, Gst::MAP_READ);
331 auto context = _libsigrok_output_format->parent();
332 auto packet = context->create_logic_packet(
333 info.get_data(), info.get_size(), 2);
334 auto result = _libsigrok_output->receive(packet);
340 bool LegacyOutput::stop_vfunc()
342 auto context = _libsigrok_output_format->parent();
343 auto end_packet = context->create_end_packet();
344 auto result = _libsigrok_output->receive(end_packet);
349 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
351 klass->set_metadata("sigrok legacy decoder",
352 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
355 klass->add_pad_template(Gst::PadTemplate::create(
359 Gst::Caps::create_any()));
362 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
364 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
365 0, Gst::register_mm_type<LegacyDecoder>(
366 "sigrok_legacy_decoder"));
370 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
371 Glib::ObjectBase(typeid(LegacyDecoder)),
376 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
377 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
379 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
381 throw runtime_error("Failed to create element - plugin not registered?");
382 auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
383 decoder->_session = libsigrokdecode_session;
384 decoder->_unitsize = unitsize;
388 struct srd_session *LegacyDecoder::libsigrokdecode_session()
393 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
396 buffer->map(info, Gst::MAP_READ);
397 uint64_t num_samples = info.get_size() / _unitsize;
398 srd_session_send(_session, _abs_ss, _abs_ss + num_samples,
399 info.get_data(), info.get_size(), _unitsize);
400 _abs_ss += num_samples;
405 bool LegacyDecoder::start_vfunc()
408 srd_session_start(_session);
412 bool LegacyDecoder::stop_vfunc()
414 srd_session_terminate_reset(_session);