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 static bool srf_initialized = false;
36 throw runtime_error("libsigrokflow is already initialized");
38 #ifdef HAVE_LIBSIGROKCXX
39 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
40 "sigrok_legacy_capture_device",
41 "Wrapper for capture devices using legacy libsigrok APIs",
42 sigc::ptr_fun(&LegacyCaptureDevice::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_input",
46 "Wrapper for inputs using legacy libsigrok APIs",
47 sigc::ptr_fun(&LegacyInput::register_element),
48 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
49 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
50 "sigrok_legacy_output",
51 "Wrapper for outputs using legacy libsigrok APIs",
52 sigc::ptr_fun(&LegacyOutput::register_element),
53 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
55 #ifdef HAVE_LIBSIGROKDECODE
56 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
57 "sigrok_legacy_decoder",
58 "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
59 sigc::ptr_fun(&LegacyDecoder::register_element),
60 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
63 srf_initialized = true;
66 Sink::Sink(GstBaseSink *gobj) :
71 Device::Device(GstElement *gobj) :
76 CaptureDevice::CaptureDevice(GstElement *gobj) :
81 #ifdef HAVE_LIBSIGROKCXX
82 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
84 klass->set_metadata("sigrok legacy capture device",
85 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
88 klass->add_pad_template(Gst::PadTemplate::create(
92 Gst::Caps::create_any()));
95 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
97 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
98 0, Gst::register_mm_type<LegacyCaptureDevice>(
99 "sigrok_legacy_capture_device"));
103 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
104 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
107 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
110 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
111 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
113 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
115 throw runtime_error("Failed to create element - plugin not registered?");
116 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
117 device->_libsigrok_device = libsigrok_device;
121 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
123 return _libsigrok_device;
126 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
130 case Gst::STATE_CHANGE_READY_TO_PAUSED:
131 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
132 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
133 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
134 _task->set_lock(_mutex);
135 _src_pad->set_active(true);
137 return Gst::STATE_CHANGE_SUCCESS;
139 return Gst::STATE_CHANGE_SUCCESS;
143 void LegacyCaptureDevice::_datafeed_callback(
144 shared_ptr<sigrok::Device> device,
145 shared_ptr<sigrok::Packet> packet)
148 switch (packet->type()->id()) {
151 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
152 auto mem = Gst::Memory::create(
153 Gst::MEMORY_FLAG_READONLY,
154 logic->data_pointer(),
155 logic->data_length(),
157 logic->data_length());
158 auto buf = Gst::Buffer::create();
159 buf->append_memory(move(mem));
160 _src_pad->push(move(buf));
165 _src_pad->push_event(Gst::EventEos::create());
172 void LegacyCaptureDevice::_run()
174 _session = _libsigrok_device->driver()->parent()->create_session();
175 _session->add_device(_libsigrok_device);
176 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
182 void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
184 klass->set_metadata("sigrok legacy input",
185 "Transform", "Wrapper for inputs using legacy libsigrok APIs",
188 klass->add_pad_template(Gst::PadTemplate::create(
192 Gst::Caps::create_any()));
194 klass->add_pad_template(Gst::PadTemplate::create(
198 Gst::Caps::create_any()));
201 bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
203 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
204 0, Gst::register_mm_type<LegacyInput>(
205 "sigrok_legacy_input"));
209 LegacyInput::LegacyInput(GstElement *gobj) :
210 Glib::ObjectBase(typeid(LegacyInput)),
213 add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink"));
214 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
215 _sink_pad->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
218 Glib::RefPtr<LegacyInput> LegacyInput::create(
219 shared_ptr<sigrok::InputFormat> libsigrok_input_format,
220 map<string, Glib::VariantBase> options)
222 auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
224 throw runtime_error("Failed to create element - plugin not registered?");
225 auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
226 input->_libsigrok_input_format = libsigrok_input_format;
227 input->_options = options;
231 bool LegacyInput::start_vfunc()
233 _libsigrok_input = _libsigrok_input_format->create_input(_options);
234 auto context = _libsigrok_input_format->parent();
235 _session = context->create_session();
236 _session->add_device(_libsigrok_input->device());
237 _session->add_datafeed_callback(bind(&LegacyInput::_datafeed_callback, this, _1, _2));
242 void LegacyInput::_datafeed_callback(
243 shared_ptr<sigrok::Device> device,
244 shared_ptr<sigrok::Packet> packet)
247 switch (packet->type()->id()) {
250 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
251 auto mem = Gst::Memory::create(
252 Gst::MEMORY_FLAG_READONLY,
253 logic->data_pointer(),
254 logic->data_length(),
256 logic->data_length());
257 auto buf = Gst::Buffer::create();
258 buf->append_memory(move(mem));
259 _src_pad->push(move(buf));
264 _src_pad->push_event(Gst::EventEos::create());
271 Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
272 const Glib::RefPtr<Gst::Buffer> &buf)
275 buf->map(info, Gst::MAP_READ);
276 _libsigrok_input->send(info.get_data(), info.get_size());
281 bool LegacyInput::stop_vfunc()
283 _libsigrok_input->end();
287 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
289 klass->set_metadata("sigrok legacy output",
290 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
293 klass->add_pad_template(Gst::PadTemplate::create(
297 Gst::Caps::create_any()));
300 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
302 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
303 0, Gst::register_mm_type<LegacyOutput>(
304 "sigrok_legacy_output"));
308 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
309 Glib::ObjectBase(typeid(LegacyOutput)),
314 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
315 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
316 shared_ptr<sigrok::Device> libsigrok_device,
317 map<string, Glib::VariantBase> options)
319 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
321 throw runtime_error("Failed to create element - plugin not registered?");
322 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
323 output->_libsigrok_output_format = libsigrok_output_format;
324 output->_libsigrok_device = libsigrok_device;
325 output->_options = options;
329 bool LegacyOutput::start_vfunc()
331 _libsigrok_output = _libsigrok_output_format->create_output(
332 _libsigrok_device, _options);
336 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
339 buffer->map(info, Gst::MAP_READ);
340 auto context = _libsigrok_output_format->parent();
341 auto packet = context->create_logic_packet(
342 info.get_data(), info.get_size(), 2);
343 auto result = _libsigrok_output->receive(packet);
349 bool LegacyOutput::stop_vfunc()
351 auto context = _libsigrok_output_format->parent();
352 auto end_packet = context->create_end_packet();
353 auto result = _libsigrok_output->receive(end_packet);
359 #ifdef HAVE_LIBSIGROKDECODE
360 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
362 klass->set_metadata("sigrok legacy decoder",
363 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
366 klass->add_pad_template(Gst::PadTemplate::create(
370 Gst::Caps::create_any()));
373 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
375 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
376 0, Gst::register_mm_type<LegacyDecoder>(
377 "sigrok_legacy_decoder"));
381 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
382 Glib::ObjectBase(typeid(LegacyDecoder)),
387 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
388 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
390 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
392 throw runtime_error("Failed to create element - plugin not registered?");
393 auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
394 decoder->_session = libsigrokdecode_session;
395 decoder->_unitsize = unitsize;
399 struct srd_session *LegacyDecoder::libsigrokdecode_session()
404 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
407 buffer->map(info, Gst::MAP_READ);
408 uint64_t num_samples = info.get_size() / _unitsize;
409 srd_session_send(_session, _abs_ss, _abs_ss + num_samples,
410 info.get_data(), info.get_size(), _unitsize);
411 _abs_ss += num_samples;
416 bool LegacyDecoder::start_vfunc()
419 srd_session_start(_session);
423 bool LegacyDecoder::stop_vfunc()
425 srd_session_terminate_reset(_session);