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/>.
23 #include <libsigrokflow/libsigrokflow.hpp>
29 using namespace std::placeholders;
33 #ifdef HAVE_LIBSIGROKCXX
34 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
35 "sigrok_legacy_capture_device",
36 "Wrapper for capture devices using legacy libsigrok APIs",
37 sigc::ptr_fun(&LegacyCaptureDevice::register_element),
38 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
39 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
40 "sigrok_legacy_input",
41 "Wrapper for inputs using legacy libsigrok APIs",
42 sigc::ptr_fun(&LegacyInput::register_element),
43 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
44 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
45 "sigrok_legacy_output",
46 "Wrapper for outputs using legacy libsigrok APIs",
47 sigc::ptr_fun(&LegacyOutput::register_element),
48 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
50 #ifdef HAVE_LIBSIGROKDECODE
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");
59 Sink::Sink(GstBaseSink *gobj) :
64 Device::Device(GstElement *gobj) :
69 CaptureDevice::CaptureDevice(GstElement *gobj) :
74 #ifdef HAVE_LIBSIGROKCXX
75 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
77 klass->set_metadata("sigrok legacy capture device",
78 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
81 klass->add_pad_template(Gst::PadTemplate::create(
85 Gst::Caps::create_any()));
88 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
90 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
91 0, Gst::register_mm_type<LegacyCaptureDevice>(
92 "sigrok_legacy_capture_device"));
96 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
97 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
100 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
103 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
104 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
106 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
108 throw runtime_error("Failed to create element - plugin not registered?");
109 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
110 device->_libsigrok_device = libsigrok_device;
114 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
116 return _libsigrok_device;
119 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
123 case Gst::STATE_CHANGE_READY_TO_PAUSED:
124 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
125 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
126 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
127 _task->set_lock(_mutex);
128 _src_pad->set_active(true);
130 return Gst::STATE_CHANGE_SUCCESS;
132 return Gst::STATE_CHANGE_SUCCESS;
136 void LegacyCaptureDevice::_datafeed_callback(
137 shared_ptr<sigrok::Device> device,
138 shared_ptr<sigrok::Packet> packet)
141 switch (packet->type()->id()) {
144 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
145 auto mem = Gst::Memory::create(
146 Gst::MEMORY_FLAG_READONLY,
147 logic->data_pointer(),
148 logic->data_length(),
150 logic->data_length());
151 auto buf = Gst::Buffer::create();
152 buf->append_memory(move(mem));
153 _src_pad->push(move(buf));
158 _src_pad->push_event(Gst::EventEos::create());
165 void LegacyCaptureDevice::_run()
167 _session = _libsigrok_device->driver()->parent()->create_session();
168 _session->add_device(_libsigrok_device);
169 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
175 void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
177 klass->set_metadata("sigrok legacy input",
178 "Transform", "Wrapper for inputs using legacy libsigrok APIs",
181 klass->add_pad_template(Gst::PadTemplate::create(
185 Gst::Caps::create_any()));
187 klass->add_pad_template(Gst::PadTemplate::create(
191 Gst::Caps::create_any()));
194 bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
196 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
197 0, Gst::register_mm_type<LegacyInput>(
198 "sigrok_legacy_input"));
202 LegacyInput::LegacyInput(GstElement *gobj) :
203 Glib::ObjectBase(typeid(LegacyInput)),
206 add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink"));
207 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
208 _sink_pad->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
211 Glib::RefPtr<LegacyInput> LegacyInput::create(
212 shared_ptr<sigrok::InputFormat> libsigrok_input_format,
213 map<string, Glib::VariantBase> options)
215 auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
217 throw runtime_error("Failed to create element - plugin not registered?");
218 auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
219 input->_libsigrok_input_format = libsigrok_input_format;
220 input->_options = options;
224 bool LegacyInput::start_vfunc()
226 _libsigrok_input = _libsigrok_input_format->create_input(_options);
227 auto context = _libsigrok_input_format->parent();
228 _session = context->create_session();
229 _session->add_device(_libsigrok_input->device());
230 _session->add_datafeed_callback(bind(&LegacyInput::_datafeed_callback, this, _1, _2));
235 void LegacyInput::_datafeed_callback(
236 shared_ptr<sigrok::Device> device,
237 shared_ptr<sigrok::Packet> packet)
240 switch (packet->type()->id()) {
243 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
244 auto mem = Gst::Memory::create(
245 Gst::MEMORY_FLAG_READONLY,
246 logic->data_pointer(),
247 logic->data_length(),
249 logic->data_length());
250 auto buf = Gst::Buffer::create();
251 buf->append_memory(move(mem));
252 _src_pad->push(move(buf));
257 _src_pad->push_event(Gst::EventEos::create());
264 Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
265 const Glib::RefPtr<Gst::Buffer> &buf)
268 buf->map(info, Gst::MAP_READ);
269 _libsigrok_input->send(info.get_data(), info.get_size());
274 bool LegacyInput::stop_vfunc()
276 _libsigrok_input->end();
280 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
282 klass->set_metadata("sigrok legacy output",
283 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
286 klass->add_pad_template(Gst::PadTemplate::create(
290 Gst::Caps::create_any()));
293 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
295 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
296 0, Gst::register_mm_type<LegacyOutput>(
297 "sigrok_legacy_output"));
301 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
302 Glib::ObjectBase(typeid(LegacyOutput)),
307 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
308 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
309 shared_ptr<sigrok::Device> libsigrok_device,
310 map<string, Glib::VariantBase> options)
312 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
314 throw runtime_error("Failed to create element - plugin not registered?");
315 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
316 output->_libsigrok_output_format = libsigrok_output_format;
317 output->_libsigrok_device = libsigrok_device;
318 output->_options = options;
322 bool LegacyOutput::start_vfunc()
324 _libsigrok_output = _libsigrok_output_format->create_output(
325 _libsigrok_device, _options);
329 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
332 buffer->map(info, Gst::MAP_READ);
333 auto context = _libsigrok_output_format->parent();
334 auto packet = context->create_logic_packet(
335 info.get_data(), info.get_size(), 2);
336 auto result = _libsigrok_output->receive(packet);
342 bool LegacyOutput::stop_vfunc()
344 auto context = _libsigrok_output_format->parent();
345 auto end_packet = context->create_end_packet();
346 auto result = _libsigrok_output->receive(end_packet);
352 #ifdef HAVE_LIBSIGROKDECODE
353 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
355 klass->set_metadata("sigrok legacy decoder",
356 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
359 klass->add_pad_template(Gst::PadTemplate::create(
363 Gst::Caps::create_any()));
366 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
368 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
369 0, Gst::register_mm_type<LegacyDecoder>(
370 "sigrok_legacy_decoder"));
374 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
375 Glib::ObjectBase(typeid(LegacyDecoder)),
380 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
381 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
383 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
385 throw runtime_error("Failed to create element - plugin not registered?");
386 auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
387 decoder->_session = libsigrokdecode_session;
388 decoder->_unitsize = unitsize;
392 struct srd_session *LegacyDecoder::libsigrokdecode_session()
397 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
400 buffer->map(info, Gst::MAP_READ);
401 uint64_t num_samples = info.get_size() / _unitsize;
402 srd_session_send(_session, _abs_ss, _abs_ss + num_samples,
403 info.get_data(), info.get_size(), _unitsize);
404 _abs_ss += num_samples;
409 bool LegacyDecoder::start_vfunc()
412 srd_session_start(_session);
416 bool LegacyDecoder::stop_vfunc()
418 srd_session_terminate_reset(_session);